version 1.202, 2006/10/30 15:50:52
|
version 1.213, 2007/06/01 18:40:20
|
Line 1
|
Line 1
|
\ Gforth primitives |
\ 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. |
\ This file is part of Gforth. |
|
|
Line 209 goto *next_code;
|
Line 209 goto *next_code;
|
a_retaddr = next_code; |
a_retaddr = next_code; |
a_body = PFA(CFA); |
a_body = PFA(CFA); |
INST_TAIL; |
INST_TAIL; |
|
#ifdef DEBUG |
|
fprintf(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body); |
|
#endif |
goto **(Label *)DOES_CODE1(CFA); |
goto **(Label *)DOES_CODE1(CFA); |
#else /* !defined(NO_IP) */ |
#else /* !defined(NO_IP) */ |
a_retaddr = (Cell *)IP; |
a_retaddr = (Cell *)IP; |
a_body = PFA(CFA); |
a_body = PFA(CFA); |
|
#ifdef DEBUG |
|
fprintf(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body); |
|
#endif |
SET_IP(DOES_CODE1(CFA)); |
SET_IP(DOES_CODE1(CFA)); |
#endif /* !defined(NO_IP) */ |
#endif /* !defined(NO_IP) */ |
|
|
Line 248 SET_IP((Xt *)a_callee);
|
Line 254 SET_IP((Xt *)a_callee);
|
|
|
execute ( xt -- ) core |
execute ( xt -- ) core |
""Perform the semantics represented by the execution token, @i{xt}."" |
""Perform the semantics represented by the execution token, @i{xt}."" |
|
#ifdef DEBUG |
|
fprintf(stderr, "execute %08x\n", xt); |
|
#endif |
#ifndef NO_IP |
#ifndef NO_IP |
ip=IP; |
ip=IP; |
#endif |
#endif |
Line 748 n = n1+n2;
|
Line 757 n = n1+n2;
|
\ lit+ / lit_plus = lit + |
\ lit+ / lit_plus = lit + |
|
|
lit+ ( n1 #n2 -- n ) new lit_plus |
lit+ ( n1 #n2 -- n ) new lit_plus |
|
#ifdef DEBUG |
|
fprintf(stderr, "lit+ %08x\n", n2); |
|
#endif |
n=n1+n2; |
n=n1+n2; |
|
|
\ PFE-0.9.14 has it differently, but the next release will have it as follows |
\ PFE-0.9.14 has it differently, but the next release will have it as follows |
Line 851 DCell d = (DCell)n1 * (DCell)n2;
|
Line 863 DCell d = (DCell)n1 * (DCell)n2;
|
#endif |
#endif |
#ifdef ASM_SM_SLASH_REM |
#ifdef ASM_SM_SLASH_REM |
ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, n4, n5); |
ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, n4, n5); |
if (((DHI(d)^n3)<0) && n4!=0) { |
if (FLOORED_DIV && ((DHI(d)^n3)<0) && n4!=0) { |
if (CHECK_DIVISION && n5 == CELL_MIN) |
if (CHECK_DIVISION && n5 == CELL_MIN) |
throw(BALL_RESULTRANGE); |
throw(BALL_RESULTRANGE); |
n5--; |
n5--; |
n4+=n3; |
n4+=n3; |
} |
} |
#else |
#else |
DCell r = fmdiv(d,n3); |
DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3); |
n4=DHI(r); |
n4=DHI(r); |
n5=DLO(r); |
n5=DLO(r); |
#endif |
#endif |
Line 875 DCell d = (DCell)n1 * (DCell)n2;
|
Line 887 DCell d = (DCell)n1 * (DCell)n2;
|
#ifdef ASM_SM_SLASH_REM |
#ifdef ASM_SM_SLASH_REM |
Cell remainder; |
Cell remainder; |
ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, remainder, n4); |
ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, remainder, n4); |
if (((DHI(d)^n3)<0) && remainder!=0) { |
if (FLOORED_DIV && ((DHI(d)^n3)<0) && remainder!=0) { |
if (CHECK_DIVISION && n4 == CELL_MIN) |
if (CHECK_DIVISION && n4 == CELL_MIN) |
throw(BALL_RESULTRANGE); |
throw(BALL_RESULTRANGE); |
n4--; |
n4--; |
} |
} |
#else |
#else |
DCell r = fmdiv(d,n3); |
DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3); |
n4=DLO(r); |
n4=DLO(r); |
#endif |
#endif |
: |
: |
Line 907 n2 = n1>>1;
|
Line 919 n2 = n1>>1;
|
fm/mod ( d1 n1 -- n2 n3 ) core f_m_slash_mod |
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}."" |
""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}."" |
#ifdef ASM_SM_SLASH_REM |
#ifdef ASM_SM_SLASH_REM |
#ifdef BUGGY_LL_DIV |
ASM_SM_SLASH_REM(DLO(d1), DHI(d1), n1, n2, n3); |
ASM_SM_SLASH_REM(d1.lo, d1.hi, n1, n2, n3); |
|
if (((DHI(d1)^n1)<0) && n2!=0) { |
|
if (CHECK_DIVISION && n3 == CELL_MIN) |
|
throw(BALL_RESULTRANGE); |
|
n3--; |
|
n2+=n1; |
|
} |
|
#else |
|
ASM_SM_SLASH_REM4(d1, n1, n2, n3); |
|
if (((DHI(d1)^n1)<0) && n2!=0) { |
if (((DHI(d1)^n1)<0) && n2!=0) { |
if (CHECK_DIVISION && n3 == CELL_MIN) |
if (CHECK_DIVISION && n3 == CELL_MIN) |
throw(BALL_RESULTRANGE); |
throw(BALL_RESULTRANGE); |
n3--; |
n3--; |
n2+=n1; |
n2+=n1; |
} |
} |
#endif |
|
#else /* !defined(ASM_SM_SLASH_REM) */ |
#else /* !defined(ASM_SM_SLASH_REM) */ |
DCell r = fmdiv(d1,n1); |
DCell r = fmdiv(d1,n1); |
n2=DHI(r); |
n2=DHI(r); |
n3=DLO(r); |
n3=DLO(r); |
#endif /* !defined(ADM_SM_SLASH_REM) */ |
#endif /* !defined(ASM_SM_SLASH_REM) */ |
: |
: |
dup >r dup 0< IF negate >r dnegate r> THEN |
dup >r dup 0< IF negate >r dnegate r> THEN |
over 0< IF tuck + swap THEN |
over 0< IF tuck + swap THEN |
Line 937 n3=DLO(r);
|
Line 939 n3=DLO(r);
|
|
|
sm/rem ( d1 n1 -- n2 n3 ) core s_m_slash_rem |
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."" |
""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 |
#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) */ |
#else /* !defined(ASM_SM_SLASH_REM) */ |
DCell r = smdiv(d1,n1); |
DCell r = smdiv(d1,n1); |
n2=DHI(r); |
n2=DHI(r); |
n3=DLO(r); |
n3=DLO(r); |
#endif /* !defined(ASM_SM_SLASH_REM) */ |
#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 |
over >r dup >r abs -rot |
dabs rot um/mod |
dabs rot um/mod |
Line 999 ud = (UDCell)u1 * (UDCell)u2;
|
Line 981 ud = (UDCell)u1 * (UDCell)u2;
|
|
|
um/mod ( ud u1 -- u2 u3 ) core u_m_slash_mod |
um/mod ( ud u1 -- u2 u3 ) core u_m_slash_mod |
""ud=u3*u1+u2, u1>u2>=0"" |
""ud=u3*u1+u2, u1>u2>=0"" |
#ifdef BUGGY_LL_DIV |
|
#ifdef ASM_UM_SLASH_MOD |
#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) */ |
#else /* !defined(ASM_UM_SLASH_MOD) */ |
UDCell r = umdiv(ud,u1); |
UDCell r = umdiv(ud,u1); |
u2=DHI(r); |
u2=DHI(r); |
u3=DLO(r); |
u3=DLO(r); |
#endif /* !defined(ASM_UM_SLASH_MOD) */ |
#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 |
0 swap [ 8 cells 1 + ] literal 0 |
?DO /modstep |
?DO /modstep |
Line 1072 d2 = -d1;
|
Line 1040 d2 = -d1;
|
|
|
d2* ( d1 -- d2 ) double d_two_star |
d2* ( d1 -- d2 ) double d_two_star |
""Shift left by 1; also works on unsigned numbers"" |
""Shift left by 1; also works on unsigned numbers"" |
#ifdef BUGGY_LL_SHIFT |
d2 = DLSHIFT(d1,1); |
DLO_IS(d2, DLO(d1)<<1); |
|
DHI_IS(d2, (DHI(d1)<<1) | (DLO(d1)>>(CELL_BITS-1))); |
|
#else |
|
d2 = 2*d1; |
|
#endif |
|
: |
: |
2dup d+ ; |
2dup d+ ; |
|
|
Line 1536 for (; f83name1 != NULL; f83name1 = (str
|
Line 1499 for (; f83name1 != NULL; f83name1 = (str
|
memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */) |
memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */) |
break; |
break; |
f83name2=f83name1; |
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 |
BEGIN dup WHILE (find-samelen) dup WHILE |
>r 2dup r@ cell+ char+ capscomp 0= |
>r 2dup r@ cell+ char+ capscomp 0= |
Line 1691 f = key_query((FILE*)wfileid);
|
Line 1659 f = key_query((FILE*)wfileid);
|
f = key_query(stdin); |
f = key_query(stdin); |
#endif |
#endif |
|
|
\+os |
|
|
|
stdin ( -- wfileid ) gforth |
stdin ( -- wfileid ) gforth |
""The standard input file of the Gforth process."" |
""The standard input file of the Gforth process."" |
wfileid = (Cell)stdin; |
wfileid = (Cell)stdin; |
Line 1705 stderr ( -- wfileid ) gforth
|
Line 1671 stderr ( -- wfileid ) gforth
|
""The standard error output file of the Gforth process."" |
""The standard error output file of the Gforth process."" |
wfileid = (Cell)stderr; |
wfileid = (Cell)stderr; |
|
|
|
\+os |
|
|
form ( -- urows ucols ) gforth |
form ( -- urows ucols ) gforth |
""The number of lines and columns in the terminal. These numbers may change |
""The number of lines and columns in the terminal. These numbers may |
with the window size."" |
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 |
/* we could block SIGWINCH here to get a consistent size, but I don't |
think this is necessary or always beneficial */ |
think this is necessary or always beneficial */ |
urows=rows; |
urows=rows; |
Line 1778 nhour =ltime->tm_hour;
|
Line 1751 nhour =ltime->tm_hour;
|
nmin =ltime->tm_min; |
nmin =ltime->tm_min; |
nsec =ltime->tm_sec; |
nsec =ltime->tm_sec; |
|
|
ms ( n -- ) facility-ext |
ms ( u -- ) facility-ext |
""Wait at least @i{n} milli-second."" |
""Wait at least @i{n} milli-second."" |
struct timeval timeout; |
gforth_ms(u); |
timeout.tv_sec=n/1000; |
|
timeout.tv_usec=1000*(n%1000); |
|
(void)select(0,0,0,0,&timeout); |
|
|
|
allocate ( u -- a_addr wior ) memory |
allocate ( u -- a_addr wior ) memory |
""Allocate @i{u} address units of contiguous data space. The initial |
""Allocate @i{u} address units of contiguous data space. The initial |
Line 1830 u = strlen((char *)c_addr);
|
Line 1800 u = strlen((char *)c_addr);
|
call-c ( ... w -- ... ) gforth call_c |
call-c ( ... w -- ... ) gforth call_c |
""Call the C function pointed to by @i{w}. The C function has to |
""Call the C function pointed to by @i{w}. The C function has to |
access the stack itself. The stack pointers are exported in the global |
access the stack itself. The stack pointers are exported in the global |
variables @code{SP} and @code{FP}."" |
variables @code{gforth_SP} and @code{gforth_FP}."" |
/* This is a first attempt at support for calls to C. This may change in |
/* This is a first attempt at support for calls to C. This may change in |
the future */ |
the future */ |
gforth_FP=fp; |
gforth_FP=fp; |
Line 2810 define(`uploop',
|
Line 2780 define(`uploop',
|
define(`_uploop', |
define(`_uploop', |
`ifelse($1, `$3', `$5', |
`ifelse($1, `$3', `$5', |
`$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')') |
`$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')') |
|
|
\ argflist(argnum): Forth argument list |
\ argflist(argnum): Forth argument list |
define(argflist, |
define(argflist, |
`ifelse($1, 0, `', |
`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 |
\ argdlist(argnum): declare C's arguments |
define(argdlist, |
define(argdlist, |
`ifelse($1, 0, `', |
`ifelse($1, 0, `', |
Line 2821 define(argdlist,
|
Line 2792 define(argdlist,
|
\ argclist(argnum): pass C's arguments |
\ argclist(argnum): pass C's arguments |
define(argclist, |
define(argclist, |
`ifelse($1, 0, `', |
`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) |
\ icall(argnum) |
define(icall, |
define(icall, |
`icall$1 ( argflist($1)u -- uret ) gforth |
`icall$1 ( argflist($1) u -- uret ) gforth |
uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1)); |
uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1)); |
|
|
') |
') |
define(fcall, |
define(fcall, |
`fcall$1 ( argflist($1)u -- rret ) gforth |
`fcall$1 ( argflist($1) u -- rret ) gforth |
rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1)); |
rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1)); |
|
|
') |
') |