--- gforth/prim 2003/11/02 22:15:28 1.147 +++ gforth/prim 2005/01/23 23:16:21 1.160 @@ -1,6 +1,6 @@ \ Gforth primitives -\ Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc. +\ Copyright (C) 1995,1996,1997,1998,2000,2003,2004 Free Software Foundation, Inc. \ This file is part of Gforth. @@ -152,36 +152,67 @@ undefine(`symbols') (docol) ( -- R:a_retaddr ) gforth-internal paren_docol ""run-time routine for colon definitions"" +#ifdef NO_IP +a_retaddr = next_code; +INST_TAIL; +goto **(Label *)PFA(CFA); +#else /* !defined(NO_IP) */ a_retaddr = (Cell *)IP; SET_IP((Xt *)PFA(CFA)); +#endif /* !defined(NO_IP) */ (docon) ( -- w ) gforth-internal paren_docon ""run-time routine for constants"" w = *(Cell *)PFA(CFA); +#ifdef NO_IP +INST_TAIL; +goto *next_code; +#endif /* defined(NO_IP) */ (dovar) ( -- a_body ) gforth-internal paren_dovar ""run-time routine for variables and CREATEd words"" a_body = PFA(CFA); +#ifdef NO_IP +INST_TAIL; +goto *next_code; +#endif /* defined(NO_IP) */ (douser) ( -- a_user ) gforth-internal paren_douser ""run-time routine for constants"" a_user = (Cell *)(up+*(Cell *)PFA(CFA)); +#ifdef NO_IP +INST_TAIL; +goto *next_code; +#endif /* defined(NO_IP) */ (dodefer) ( -- ) gforth-internal paren_dodefer ""run-time routine for deferred words"" +#ifndef NO_IP ip=IP; /* undo any ip updating that may have been performed by NEXT_P0 */ +#endif /* !defined(NO_IP) */ SUPER_END; /* !! probably unnecessary and may lead to measurement errors */ EXEC(*(Xt *)PFA(CFA)); (dofield) ( n1 -- n2 ) gforth-internal paren_field ""run-time routine for fields"" n2 = n1 + *(Cell *)PFA(CFA); +#ifdef NO_IP +INST_TAIL; +goto *next_code; +#endif /* defined(NO_IP) */ (dodoes) ( -- a_body R:a_retaddr ) gforth-internal paren_dodoes ""run-time routine for @code{does>}-defined words"" +#ifdef NO_IP +a_retaddr = next_code; +a_body = PFA(CFA); +INST_TAIL; +goto **(Label *)DOES_CODE1(CFA); +#else /* !defined(NO_IP) */ a_retaddr = (Cell *)IP; a_body = PFA(CFA); SET_IP(DOES_CODE1(CFA)); +#endif /* !defined(NO_IP) */ (does-handler) ( -- ) gforth-internal paren_does_handler ""just a slot to have an encoding for the DOESJUMP, @@ -198,6 +229,7 @@ noop ( -- ) gforth call ( #a_callee -- R:a_retaddr ) new ""Call callee (a variant of docol with inline argument)."" #ifdef NO_IP +assert(0); INST_TAIL; JUMP(a_callee); #else @@ -217,7 +249,7 @@ execute ( xt -- ) core #ifndef NO_IP ip=IP; #endif -IF_spTOS(spTOS = sp[0]); +IF_spTOS(spTOS = sp[0]); /* inst_tail would produce a NEXT_P1 */ SUPER_END; EXEC(xt); @@ -227,7 +259,7 @@ perform ( a_addr -- ) gforth #ifndef NO_IP ip=IP; #endif -IF_spTOS(spTOS = sp[0]); +IF_spTOS(spTOS = sp[0]); /* inst_tail would produce a NEXT_P1 */ SUPER_END; EXEC(*(Xt *)a_addr); : @@ -298,6 +330,8 @@ SET_IP((Xt *)a_target); \ condbranch(forthname,stackeffect,restline,code1,code2,forthcode) \ this is non-syntactical: code must open a brace that is closed by the macro +\ condbranch(forthname,stackeffect,restline,code1,code2,forthcode) +\ this is non-syntactical: code must open a brace that is closed by the macro define(condbranch, $1 ( `#'a_target $2 ) $3 $4 #ifdef NO_IP @@ -307,6 +341,37 @@ $5 #ifdef NO_IP JUMP(a_target); #else SET_IP((Xt *)a_target); +#endif +} +$6 + +\+glocals + +$1-lp+!`#' ( `#'a_target `#'nlocals $2 ) $3_lp_plus_store_number +$4 #ifdef NO_IP +INST_TAIL; +#endif +$5 lp += nlocals; +#ifdef NO_IP +JUMP(a_target); +#else +SET_IP((Xt *)a_target); +#endif +} + +\+ +) + +\ version that generates two jumps (not good for PR 15242 workaround) +define(condbranch_twojump, +$1 ( `#'a_target $2 ) $3 +$4 #ifdef NO_IP +INST_TAIL; +#endif +$5 #ifdef NO_IP +JUMP(a_target); +#else +SET_IP((Xt *)a_target); INST_TAIL; NEXT_P2; #endif } @@ -401,8 +466,9 @@ condbranch((+loop),n R:nlimit R:n1 -- R: /* dependent upon two's complement arithmetic */ Cell olddiff = n1-nlimit; n2=n1+n; -,if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */ - || (olddiff^n)>=0 /* it is a wrap-around effect */) { +,if (((olddiff^(olddiff+n)) /* the limit is not crossed */ + &(olddiff^n)) /* OR it is a wrap-around effect */ + >=0) { /* & is used to avoid having two branches for gforth-native */ ,: r> swap r> r> 2dup - >r @@ -431,7 +497,7 @@ if (n<0) { newdiff = -newdiff; } n2=n1+n; -,if (diff>=0 || newdiff<0) { +,if (((~diff)|newdiff)<0) { /* use | to avoid two branches for gforth-native */ ,) \+ @@ -784,7 +850,7 @@ n2 = n1>>1; fm/mod ( d1 n1 -- n2 n3 ) core f_m_slash_mod ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}."" -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_DIV DCell r = fmdiv(d1,n1); n2=r.hi; n3=r.lo; @@ -806,7 +872,7 @@ if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) sm/rem ( d1 n1 -- n2 n3 ) core s_m_slash_rem ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0."" -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_DIV DCell r = smdiv(d1,n1); n2=r.hi; n3=r.lo; @@ -827,7 +893,7 @@ if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) r> 0< IF swap negate swap THEN ; m* ( n1 n2 -- d ) core m_star -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_MUL d = mmul(n1,n2); #else d = (DCell)n1 * (DCell)n2; @@ -839,7 +905,7 @@ d = (DCell)n1 * (DCell)n2; um* ( u1 u2 -- ud ) core u_m_star /* use u* as alias */ -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_MUL ud = ummul(u1,u2); #else ud = (UDCell)u1 * (UDCell)u2; @@ -855,7 +921,7 @@ ud = (UDCell)u1 * (UDCell)u2; um/mod ( ud u1 -- u2 u3 ) core u_m_slash_mod ""ud=u3*u1+u2, u1>u2>=0"" -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_DIV UDCell r = umdiv(ud,u1); u2=r.hi; u3=r.lo; @@ -874,9 +940,9 @@ u2 = ud%u1; and >r >r 2dup d+ swap r> + swap r> ; m+ ( d1 n -- d2 ) double m_plus -#ifdef BUGGY_LONG_LONG -d2.lo = d1.lo+n; -d2.hi = d1.hi - (n<0) + (d2.lod d+ ; d+ ( d1 d2 -- d ) double d_plus -#ifdef BUGGY_LONG_LONG -d.lo = d1.lo+d2.lo; -d.hi = d1.hi + d2.hi + (d.lor tuck + swap over u> r> swap - ; d- ( d1 d2 -- d ) double d_minus -#ifdef BUGGY_LONG_LONG -d.lo = d1.lo - d2.lo; -d.hi = d1.hi-d2.hi-(d1.lo>(CELL_BITS-1)); +#ifdef BUGGY_LL_SHIFT +DLO_IS(d2, DLO(d1)<<1); +DHI_IS(d2, (DHI(d1)<<1) | (DLO(d1)>>(CELL_BITS-1))); #else d2 = 2*d1; #endif @@ -927,9 +993,9 @@ d2 = 2*d1; d2/ ( d1 -- d2 ) double d_two_slash ""Arithmetic shift right by 1. For signed numbers this is a floored division by 2."" -#ifdef BUGGY_LONG_LONG -d2.hi = d1.hi>>1; -d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1)); +#ifdef BUGGY_LL_SHIFT +DHI_IS(d2, DHI(d1)>>1); +DLO_IS(d2, (DLO(d1)>>1) | (DHI(d1)<<(CELL_BITS-1))); #else d2 = d1>>1; #endif @@ -955,12 +1021,20 @@ w2 = ~w1; rshift ( u1 n -- u2 ) core r_shift ""Logical shift right by @i{n} bits."" - u2 = u1>>n; +#ifdef BROKEN_SHIFT + u2 = rshift(u1, n); +#else + u2 = u1 >> n; +#endif : 0 ?DO 2/ MAXI and LOOP ; lshift ( u1 n -- u2 ) core l_shift - u2 = u1< ( $2 -- f ) $7 $3not_equals -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_CMP f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi); #else f = FLAG($4!=$5); #endif $1< ( $2 -- f ) $8 $3less_than -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_CMP f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi); #else f = FLAG($4<$5); #endif $1> ( $2 -- f ) $9 $3greater_than -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_CMP f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi); #else f = FLAG($4>$5); #endif $1<= ( $2 -- f ) gforth $3less_or_equal -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_CMP f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi); #else f = FLAG($4<=$5); #endif $1>= ( $2 -- f ) gforth $3greater_or_equal -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_CMP f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi); #else f = FLAG($4>=$5); @@ -1557,16 +1631,8 @@ SUPER_END; return (Label *)n; (system) ( c_addr u -- wretval wior ) gforth paren_system -#ifndef MSDOS -int old_tp=terminal_prepped; -deprep_terminal(); -#endif -wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */ +wretval = gforth_system(c_addr, u); wior = IOR(wretval==-1 || (wretval==127 && errno != 0)); -#ifndef MSDOS -if (old_tp) - prep_terminal(); -#endif getenv ( c_addr1 u1 -- c_addr2 u2 ) gforth ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2} @@ -1812,6 +1878,21 @@ char * string = cstr(c_addr1, u1, 1); char * pattern = cstr(c_addr2, u2, 0); flag = FLAG(!fnmatch(pattern, string, 0)); +set-dir ( c_addr u -- wior ) gforth set_dir +""Change the current directory to @i{c-addr, u}. +Return an error if this is not possible"" +wior = IOR(chdir(tilde_cstr(c_addr, u, 1))); + +get-dir ( c_addr1 u1 -- c_addr2 u2 ) gforth get_dir +""Store the current directory in the buffer specified by @{c-addr1, u1}. +If the buffer size is not sufficient, return 0 0"" +c_addr2 = getcwd(c_addr1, u1); +if(c_addr2 != NULL) { + u2 = strlen(c_addr2); +} else { + u2 = 0; +} + \+ newline ( -- c_addr u ) gforth @@ -1854,11 +1935,7 @@ dsystem = timeval2us(&usage.ru_stime); struct timeval time1; gettimeofday(&time1,NULL); duser = timeval2us(&time1); -#ifndef BUGGY_LONG_LONG -dsystem = (DCell)0; -#else -dsystem=(DCell){0,0}; -#endif +dsystem = DZERO; #endif \+ @@ -1871,13 +1948,17 @@ comparisons(f, r1 r2, f_, r1, r2, gforth comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth) d>f ( d -- r ) float d_to_f -#ifdef BUGGY_LONG_LONG +#ifdef BUGGY_LL_D2F extern double ldexp(double x, int exp); -if (d.hi<0) { +if (DHI(d)<0) { +#ifdef BUGGY_LL_ADD DCell d2=dnegate(d); - r = -(ldexp((Float)d2.hi,CELL_BITS) + (Float)d2.lo); +#else + DCell d2=-d; +#endif + r = -(ldexp((Float)DHI(d2),CELL_BITS) + (Float)DLO(d2)); } else - r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo; + r = ldexp((Float)DHI(d),CELL_BITS) + (Float)DLO(d); #else r = d; #endif @@ -2321,7 +2402,11 @@ av-double ( r -- ) gforth av_double av_double(alist, r); av-longlong ( d -- ) gforth av_longlong +#ifdef BUGGY_LL_SIZE +av_longlong(alist, DLO(d)); +#else av_longlong(alist, d); +#endif av-ptr ( c_addr -- ) gforth av_ptr av_ptr(alist, void*, c_addr); @@ -2340,7 +2425,11 @@ lp += sizeof(Float); av_double(alist, r); av-longlong-r ( R:d -- ) gforth av_longlong_r +#ifdef BUGGY_LL_SIZE +av_longlong(alist, DLO(d)); +#else av_longlong(alist, d); +#endif av-ptr-r ( R:c_addr -- ) gforth av_ptr_r av_ptr(alist, void*, c_addr); @@ -2372,7 +2461,12 @@ av-call-longlong ( -- d ) gforth av_cal SAVE_REGS av_call(alist); REST_REGS +#ifdef BUGGY_LONG_LONG +DLO_IS(d, llrv); +DHI_IS(d, 0); +#else d = llrv; +#endif av-call-ptr ( -- c_addr ) gforth av_call_ptr SAVE_REGS @@ -2405,7 +2499,12 @@ va-arg-int ( -- w ) gforth va_arg_int w = va_arg_int(clist); va-arg-longlong ( -- d ) gforth va_arg_longlong +#ifdef BUGGY_LONG_LONG +DLO_IS(d, va_arg_longlong(clist)); +DHI_IS(d, 0); +#else d = va_arg_longlong(clist); +#endif va-arg-ptr ( -- c_addr ) gforth va_arg_ptr c_addr = (char *)va_arg_ptr(clist,char*); @@ -2429,7 +2528,11 @@ va_return_ptr(clist, void *, c_addr); return 0; va-return-longlong ( d -- ) gforth va_return_longlong +#ifdef BUGGY_LONG_LONG +va_return_longlong(clist, d.lo); +#else va_return_longlong(clist, d); +#endif return 0; va-return-float ( r -- ) gforth va_return_float @@ -2494,7 +2597,10 @@ compile_prim1(a_prim); finish-code ( -- ) gforth finish_code ""Perform delayed steps in code generation (branch resolution, I-cache flushing)."" +IF_spTOS(sp[0]=spTOS); /* workaround for failing to save spTOS + (gcc-2.95.1, gforth-fast --enable-force-reg) */ finish_code(); +IF_spTOS(spTOS=sp[0]); forget-dyncode ( c_code -- f ) gforth-internal forget_dyncode f = forget_dyncode(c_code);