--- gforth/prim 2006/10/27 21:47:01 1.200 +++ gforth/prim 2007/03/31 21:43:18 1.210 @@ -1,6 +1,6 @@ \ Gforth primitives -\ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005 Free Software Foundation, Inc. +\ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 Free Software Foundation, Inc. \ This file is part of Gforth. @@ -248,6 +248,9 @@ SET_IP((Xt *)a_callee); execute ( xt -- ) core ""Perform the semantics represented by the execution token, @i{xt}."" +#ifdef DEBUG +fprintf(stderr, "execute %08x\n", xt); +#endif #ifndef NO_IP ip=IP; #endif @@ -849,23 +852,18 @@ DCell d = mmul(n1,n2); #else DCell d = (DCell)n1 * (DCell)n2; #endif -#ifdef BUGGY_LL_DIV -DCell r = fmdiv(d,n3); -n4=DHI(r); -n5=DLO(r); -#else -/* assumes that the processor uses either floored or symmetric division */ -DCell d5 = d/n3; -n4 = d%n3; -if (CHECK_DIVISION_SW && n3 == 0) - throw(BALL_DIVZERO); +#ifdef ASM_SM_SLASH_REM +ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, n4, n5); if (FLOORED_DIV && ((DHI(d)^n3)<0) && n4!=0) { - d5--; + if (CHECK_DIVISION && n5 == CELL_MIN) + throw(BALL_RESULTRANGE); + n5--; n4+=n3; } -n5 = d5; -if (CHECK_DIVISION && d5 != n5) - throw(BALL_RESULTRANGE); +#else +DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3); +n4=DHI(r); +n5=DLO(r); #endif : >r m* r> fm/mod ; @@ -877,19 +875,17 @@ DCell d = mmul(n1,n2); #else DCell d = (DCell)n1 * (DCell)n2; #endif -#ifdef BUGGY_LL_DIV -DCell r = fmdiv(d,n3); -n4=DLO(r); +#ifdef ASM_SM_SLASH_REM +Cell remainder; +ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, remainder, n4); +if (FLOORED_DIV && ((DHI(d)^n3)<0) && remainder!=0) { + if (CHECK_DIVISION && n4 == CELL_MIN) + throw(BALL_RESULTRANGE); + n4--; +} #else -/* assumes that the processor uses either floored or symmetric division */ -DCell d4 = d/n3; -if (CHECK_DIVISION_SW && n3 == 0) - throw(BALL_DIVZERO); -if (FLOORED_DIV && ((DHI(d)^n3)<0) && (d%n3)!=0) - d4--; -n4 = d4; -if (CHECK_DIVISION && d4 != n4) - throw(BALL_RESULTRANGE); +DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3); +n4=DLO(r); #endif : */mod nip ; @@ -913,9 +909,8 @@ 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_LL_DIV #ifdef ASM_SM_SLASH_REM -ASM_SM_SLASH_REM(d1.lo, d1.hi, n1, n2, n3); +ASM_SM_SLASH_REM(DLO(d1), DHI(d1), n1, n2, n3); if (((DHI(d1)^n1)<0) && n2!=0) { if (CHECK_DIVISION && n3 == CELL_MIN) throw(BALL_RESULTRANGE); @@ -927,31 +922,6 @@ DCell r = fmdiv(d1,n1); n2=DHI(r); n3=DLO(r); #endif /* !defined(ASM_SM_SLASH_REM) */ -#else -#ifdef ASM_SM_SLASH_REM4 -ASM_SM_SLASH_REM4(d1, n1, n2, n3); -if (((DHI(d1)^n1)<0) && n2!=0) { - if (CHECK_DIVISION && n3 == CELL_MIN) - throw(BALL_RESULTRANGE); - n3--; - n2+=n1; -} -#else /* !defined(ASM_SM_SLASH_REM4) */ -/* assumes that the processor uses either floored or symmetric division */ -DCell d3 = d1/n1; -n2 = d1%n1; -if (CHECK_DIVISION_SW && n1 == 0) - throw(BALL_DIVZERO); -/* note that this 1%-3>0 is optimized by the compiler */ -if (1%-3>0 && ((DHI(d1)^n1)<0) && n2!=0) { - d3--; - n2+=n1; -} -n3 = d3; -if (CHECK_DIVISION && d3 != n3) - throw(BALL_RESULTRANGE); -#endif /* !defined(ASM_SM_SLASH_REM4) */ -#endif : dup >r dup 0< IF negate >r dnegate r> THEN over 0< IF tuck + swap THEN @@ -960,33 +930,13 @@ if (CHECK_DIVISION && d3 != n3) 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_LL_DIV #ifdef ASM_SM_SLASH_REM -ASM_SM_SLASH_REM(d1.lo, d1.hi, n1, n2, n3); +ASM_SM_SLASH_REM(DLO(d1), DHI(d1), n1, n2, n3); #else /* !defined(ASM_SM_SLASH_REM) */ DCell r = smdiv(d1,n1); n2=DHI(r); n3=DLO(r); #endif /* !defined(ASM_SM_SLASH_REM) */ -#else -#ifdef ASM_SM_SLASH_REM4 -ASM_SM_SLASH_REM4(d1, n1, n2, n3); -#else /* !defined(ASM_SM_SLASH_REM4) */ -/* assumes that the processor uses either floored or symmetric division */ -DCell d3 = d1/n1; -n2 = d1%n1; -if (CHECK_DIVISION_SW && n1 == 0) - throw(BALL_DIVZERO); -/* note that this 1%-3<0 is optimized by the compiler */ -if (1%-3<0 && ((DHI(d1)^n1)<0) && n2!=0) { - d3++; - n2-=n1; -} -n3 = d3; -if (CHECK_DIVISION && d3 != n3) - throw(BALL_RESULTRANGE); -#endif /* !defined(ASM_SM_SLASH_REM4) */ -#endif : over >r dup >r abs -rot dabs rot um/mod @@ -1022,27 +972,13 @@ 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_LL_DIV #ifdef ASM_UM_SLASH_MOD -ASM_UM_SLASH_MOD(ud.lo, ud.hi, u1, u2, u3); +ASM_UM_SLASH_MOD(DLO(ud), DHI(ud), u1, u2, u3); #else /* !defined(ASM_UM_SLASH_MOD) */ UDCell r = umdiv(ud,u1); u2=DHI(r); u3=DLO(r); #endif /* !defined(ASM_UM_SLASH_MOD) */ -#else -#ifdef ASM_UM_SLASH_MOD4 -ASM_UM_SLASH_MOD4(ud, u1, u2, u3); -#else /* !defined(ASM_UM_SLASH_MOD4) */ -UDCell ud3 = ud/u1; -u2 = ud%u1; -if (CHECK_DIVISION_SW && u1 == 0) - throw(BALL_DIVZERO); -u3 = ud3; -if (CHECK_DIVISION && ud3 != u3) - throw(BALL_RESULTRANGE); -#endif /* !defined(ASM_UM_SLASH_MOD4) */ -#endif : 0 swap [ 8 cells 1 + ] literal 0 ?DO /modstep @@ -1095,12 +1031,7 @@ d2 = -d1; d2* ( d1 -- d2 ) double d_two_star ""Shift left by 1; also works on unsigned numbers"" -#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 +d2 = DLSHIFT(d1,1); : 2dup d+ ; @@ -1559,6 +1490,11 @@ for (; f83name1 != NULL; f83name1 = (str memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */) break; f83name2=f83name1; +#ifdef DEBUG +fprintf(stderr, "F83find "); +fwrite(c_addr, u, 1, stderr); +fprintf(stderr, " found %08x\n", f83name2); +#endif : BEGIN dup WHILE (find-samelen) dup WHILE >r 2dup r@ cell+ char+ capscomp 0= @@ -1714,8 +1650,6 @@ f = key_query((FILE*)wfileid); f = key_query(stdin); #endif -\+os - stdin ( -- wfileid ) gforth ""The standard input file of the Gforth process."" wfileid = (Cell)stdin; @@ -1728,9 +1662,16 @@ stderr ( -- wfileid ) gforth ""The standard error output file of the Gforth process."" wfileid = (Cell)stderr; +\+os + form ( -- urows ucols ) gforth -""The number of lines and columns in the terminal. These numbers may change -with the window size."" +""The number of lines and columns in the terminal. These numbers may +change with the window size. Note that it depends on the OS whether +this reflects the actual size and changes with the window size +(currently only on Unix-like OSs). On other OSs you just get a +default, and can tell Gforth the terminal size by setting the +environment variables @code{COLUMNS} and @code{LINES} before starting +Gforth."" /* we could block SIGWINCH here to get a consistent size, but I don't think this is necessary or always beneficial */ urows=rows; @@ -2833,10 +2774,11 @@ define(`uploop', define(`_uploop', `ifelse($1, `$3', `$5', `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')') + \ argflist(argnum): Forth argument list define(argflist, `ifelse($1, 0, `', - `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')') + `uploop(`_i', 1, $1, ``u''`_i ', ``u''`_i')')') \ argdlist(argnum): declare C's arguments define(argdlist, `ifelse($1, 0, `', @@ -2844,15 +2786,15 @@ define(argdlist, \ argclist(argnum): pass C's arguments define(argclist, `ifelse($1, 0, `', - `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')') + `uploop(`_i', 1, $1, ``u''`_i, ', ``u''`_i')')') \ icall(argnum) define(icall, -`icall$1 ( argflist($1)u -- uret ) gforth +`icall$1 ( argflist($1) u -- uret ) gforth uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1)); ') define(fcall, -`fcall$1 ( argflist($1)u -- rret ) gforth +`fcall$1 ( argflist($1) u -- rret ) gforth rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1)); ')