--- gforth/prim 2006/04/02 09:18:56 1.192 +++ gforth/prim 2007/03/02 22:06:55 1.207 @@ -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. @@ -809,12 +809,21 @@ n = n1*n2; / ( n1 n2 -- n ) core slash n = n1/n2; -if(FLOORED_DIV && ((n1^n2) < 0) && (n1%n2 != 0)) n--; +if (CHECK_DIVISION_SW && n2 == 0) + throw(BALL_DIVZERO); +if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN) + throw(BALL_RESULTRANGE); +if (FLOORED_DIV && ((n1^n2) < 0) && (n1%n2 != 0)) + n--; : /mod nip ; mod ( n1 n2 -- n ) core n = n1%n2; +if (CHECK_DIVISION_SW && n2 == 0) + throw(BALL_DIVZERO); +if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN) + throw(BALL_RESULTRANGE); if(FLOORED_DIV && ((n1^n2) < 0) && n!=0) n += n2; : /mod drop ; @@ -822,6 +831,10 @@ if(FLOORED_DIV && ((n1^n2) < 0) && n!=0) /mod ( n1 n2 -- n3 n4 ) core slash_mod n4 = n1/n2; n3 = n1%n2; /* !! is this correct? look into C standard! */ +if (CHECK_DIVISION_SW && n2 == 0) + throw(BALL_DIVZERO); +if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN) + throw(BALL_RESULTRANGE); if (FLOORED_DIV && ((n1^n2) < 0) && n3!=0) { n4--; n3+=n2; @@ -836,18 +849,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 */ -n5 = d/n3; -n4 = d%n3; +#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) { + if (CHECK_DIVISION && n5 == CELL_MIN) + throw(BALL_RESULTRANGE); n5--; n4+=n3; } +#else +DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3); +n4=DHI(r); +n5=DLO(r); #endif : >r m* r> fm/mod ; @@ -859,13 +872,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 */ -n4 = d/n3; -if (FLOORED_DIV && ((DHI(d)^n3)<0) && (d%n3)!=0) n4--; +DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3); +n4=DLO(r); #endif : */mod nip ; @@ -889,10 +906,11 @@ 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); n3--; n2+=n1; } @@ -901,24 +919,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) { - n3--; - n2+=n1; -} -#else /* !defined(ASM_SM_SLASH_REM4) */ -/* assumes that the processor uses either floored or symmetric division */ -n3 = d1/n1; -n2 = d1%n1; -/* note that this 1%-3>0 is optimized by the compiler */ -if (1%-3>0 && ((DHI(d1)^n1)<0) && n2!=0) { - n3--; - n2+=n1; -} -#endif /* !defined(ASM_SM_SLASH_REM4) */ -#endif : dup >r dup 0< IF negate >r dnegate r> THEN over 0< IF tuck + swap THEN @@ -927,28 +927,13 @@ if (1%-3>0 && ((DHI(d1)^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_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 */ -n3 = d1/n1; -n2 = d1%n1; -/* note that this 1%-3<0 is optimized by the compiler */ -if (1%-3<0 && ((DHI(d1)^n1)<0) && n2!=0) { - n3++; - n2-=n1; -} -#endif /* !defined(ASM_SM_SLASH_REM4) */ -#endif : over >r dup >r abs -rot dabs rot um/mod @@ -984,22 +969,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) */ -u3 = ud/u1; -u2 = ud%u1; -#endif /* !defined(ASM_UM_SLASH_MOD4) */ -#endif : 0 swap [ 8 cells 1 + ] literal 0 ?DO /modstep @@ -1052,12 +1028,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+ ; @@ -1647,7 +1618,11 @@ n=1; \g hostos key-file ( wfileid -- c ) gforth paren_key_file -""Read one character @i{c} from @i{wfileid}. "" +""Read one character @i{c} from @i{wfileid}. This word disables +buffering for @i{wfileid}. If you want to read characters from a +terminal in non-canonical (raw) mode, you have to put the terminal in +non-canonical mode yourself (using the C interface); the exception is +@code{stdin}: Gforth automatically puts it into non-canonical mode."" #ifdef HAS_FILE fflush(stdout); c = key((FILE*)wfileid); @@ -1657,7 +1632,9 @@ c = key(stdin); key?-file ( wfileid -- f ) gforth key_q_file ""@i{f} is true if at least one character can be read from @i{wfileid} -without blocking."" +without blocking. If you also want to use @code{read-file} or +@code{read-line} on the file, you have to call @code{key?-file} or +@code{key-file} first (these two words disable buffering)."" #ifdef HAS_FILE fflush(stdout); f = key_query((FILE*)wfileid); @@ -1668,12 +1645,15 @@ f = key_query(stdin); \+os stdin ( -- wfileid ) gforth +""The standard input file of the Gforth process."" wfileid = (Cell)stdin; stdout ( -- wfileid ) gforth +""The standard output file of the Gforth process."" wfileid = (Cell)stdout; stderr ( -- wfileid ) gforth +""The standard error output file of the Gforth process."" wfileid = (Cell)stderr; form ( -- urows ucols ) gforth @@ -2689,7 +2669,7 @@ w = ffi_prep_closure((ffi_closure *)a_cl ffi-2@ ( a_addr -- d ) gforth ffi_2fetch #ifdef BUGGY_LONG_LONG -DLO_IS(d, (Cell*)(*a_addr)); +DLO_IS(d, *(Cell*)(*a_addr)); DHI_IS(d, 0); #else d = *(DCell*)(a_addr); @@ -2705,14 +2685,25 @@ ffi-2! ( d a_addr -- ) gforth ffi_2store ffi-arg-int ( -- w ) gforth ffi_arg_int w = *(int *)(*gforth_clist++); +ffi-arg-long ( -- w ) gforth ffi_arg_long +w = *(long *)(*gforth_clist++); + ffi-arg-longlong ( -- d ) gforth ffi_arg_longlong #ifdef BUGGY_LONG_LONG -DLO_IS(d, (Cell*)(*gforth_clist++)); -DHI_IS(d, 0); +DLO_IS(d, *(Cell*)(*gforth_clist++)); +DHI_IS(d, -(*(Cell*)(*gforth_clist++)<0)); #else d = *(DCell*)(*gforth_clist++); #endif +ffi-arg-dlong ( -- d ) gforth ffi_arg_dlong +#ifdef BUGGY_LONG_LONG +DLO_IS(d, *(Cell*)(*gforth_clist++)); +DHI_IS(d, -(*(Cell*)(*gforth_clist++)<0)); +#else +d = *(Cell*)(*gforth_clist++); +#endif + ffi-arg-ptr ( -- c_addr ) gforth ffi_arg_ptr c_addr = *(Char **)(*gforth_clist++); @@ -2737,6 +2728,18 @@ ffi-ret-longlong ( d -- ) gforth ffi_ret #endif return 0; +ffi-ret-dlong ( d -- ) gforth ffi_ret_dlong +#ifdef BUGGY_LONG_LONG +*(Cell*)(gforth_ritem) = DLO(d); +#else +*(Cell*)(gforth_ritem) = d; +#endif +return 0; + +ffi-ret-long ( n -- ) gforth ffi_ret_long +*(Cell*)(gforth_ritem) = n; +return 0; + ffi-ret-ptr ( c_addr -- ) gforth ffi_ret_ptr *(Char **)(gforth_ritem) = c_addr; return 0; @@ -2758,10 +2761,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, `', @@ -2769,15 +2773,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)); ')