Annotation of gforth/prim, revision 1.109

1.1       anton       1: \ Gforth primitives
                      2: 
1.62      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
1.1       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
1.63      anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      20: 
                     21: 
                     22: \ WARNING: This file is processed by m4. Make sure your identifiers
                     23: \ don't collide with m4's (e.g. by undefining them).
                     24: \ 
                     25: \ 
                     26: \ 
                     27: \ This file contains primitive specifications in the following format:
                     28: \ 
1.47      anton      29: \ forth name   ( stack effect )        category        [pronunciation]
1.1       anton      30: \ [""glossary entry""]
                     31: \ C code
                     32: \ [:
                     33: \ Forth code]
                     34: \ 
1.47      anton      35: \ Note: Fields in brackets are optional.  Word specifications have to
                     36: \ be separated by at least one empty line
1.1       anton      37: \
                     38: \ Both pronounciation and stack items (in the stack effect) must
1.48      anton      39: \ conform to the C identifier syntax or the C compiler will complain.
                     40: \ If you don't have a pronounciation field, the Forth name is used,
                     41: \ and has to conform to the C identifier syntax.
1.1       anton      42: \ 
                     43: \ These specifications are automatically translated into C-code for the
                     44: \ interpreter and into some other files. I hope that your C compiler has
                     45: \ decent optimization, otherwise the automatically generated code will
                     46: \ be somewhat slow. The Forth version of the code is included for manual
                     47: \ compilers, so they will need to compile only the important words.
                     48: \ 
                     49: \ Note that stack pointer adjustment is performed according to stack
                     50: \ effect by automatically generated code and NEXT is automatically
                     51: \ appended to the C code. Also, you can use the names in the stack
                     52: \ effect in the C code. Stack access is automatic. One exception: if
                     53: \ your code does not fall through, the results are not stored into the
                     54: \ stack. Use different names on both sides of the '--', if you change a
                     55: \ value (some stores to the stack are optimized away).
1.93      anton      56: \
                     57: \ For superinstructions the syntax is:
                     58: \
                     59: \ forth-name [/ c-name] = forth-name forth-name ...
                     60: \
1.1       anton      61: \ 
                     62: \ The stack variables have the following types:
                     63: \ 
                     64: \ name matches type
                     65: \ f.*          Bool
                     66: \ c.*          Char
1.93      anton      67: \ [nw].*       Cell
1.1       anton      68: \ u.*          UCell
                     69: \ d.*          DCell
                     70: \ ud.*         UDCell
                     71: \ r.*          Float
                     72: \ a_.*         Cell *
                     73: \ c_.*         Char *
                     74: \ f_.*         Float *
                     75: \ df_.*                DFloat *
                     76: \ sf_.*                SFloat *
                     77: \ xt.*         XT
                     78: \ f83name.*    F83Name *
1.67      anton      79: 
1.79      anton      80: \E stack data-stack   sp Cell
                     81: \E stack fp-stack     fp Float
                     82: \E stack return-stack rp Cell
                     83: \E
1.67      anton      84: \E get-current prefixes set-current
                     85: \E 
                     86: \E s" Bool"            single data-stack type-prefix f
                     87: \E s" Char"            single data-stack type-prefix c
                     88: \E s" Cell"            single data-stack type-prefix n
                     89: \E s" Cell"            single data-stack type-prefix w
                     90: \E s" UCell"           single data-stack type-prefix u
                     91: \E s" DCell"           double data-stack type-prefix d
                     92: \E s" UDCell"          double data-stack type-prefix ud
                     93: \E s" Float"           single fp-stack   type-prefix r
                     94: \E s" Cell *"          single data-stack type-prefix a_
                     95: \E s" Char *"          single data-stack type-prefix c_
                     96: \E s" Float *"         single data-stack type-prefix f_
                     97: \E s" DFloat *"                single data-stack type-prefix df_
                     98: \E s" SFloat *"                single data-stack type-prefix sf_
                     99: \E s" Xt"              single data-stack type-prefix xt
                    100: \E s" struct F83Name *"        single data-stack type-prefix f83name
1.71      anton     101: \E s" struct Longname *" single data-stack type-prefix longname
1.67      anton     102: \E 
                    103: \E return-stack stack-prefix R:
                    104: \E inst-stream  stack-prefix #
                    105: \E 
                    106: \E set-current
1.97      anton     107: \E store-optimization on
1.109   ! anton     108: \E ' noop tail-nextp2 ! \ now INST_TAIL just stores, but does not jump
1.67      anton     109: 
1.1       anton     110: \ 
                    111: \ 
                    112: \ 
                    113: \ In addition the following names can be used:
                    114: \ ip   the instruction pointer
                    115: \ sp   the data stack pointer
                    116: \ rp   the parameter stack pointer
                    117: \ lp   the locals stack pointer
                    118: \ NEXT executes NEXT
                    119: \ cfa  
                    120: \ NEXT1        executes NEXT1
                    121: \ FLAG(x)      makes a Forth flag from a C flag
                    122: \ 
                    123: \ 
                    124: \ 
                    125: \ Percentages in comments are from Koopmans book: average/maximum use
                    126: \ (taken from four, not very representative benchmarks)
                    127: \ 
                    128: \ 
                    129: \ 
                    130: \ To do:
                    131: \ 
                    132: \ throw execute, cfa and NEXT1 out?
                    133: \ macroize *ip, ip++, *ip++ (pipelining)?
                    134: 
                    135: \ these m4 macros would collide with identifiers
                    136: undefine(`index')
                    137: undefine(`shift')
1.78      pazsan    138: undefine(`symbols')
1.1       anton     139: 
1.83      pazsan    140: \g control
                    141: 
1.47      anton     142: noop   ( -- )          gforth
1.1       anton     143: :
                    144:  ;
                    145: 
1.68      anton     146: lit    ( #w -- w )             gforth
1.1       anton     147: :
                    148:  r> dup @ swap cell+ >r ;
                    149: 
1.47      anton     150: execute        ( xt -- )               core
1.29      crook     151: ""Perform the semantics represented by the execution token, @i{xt}.""
1.102     anton     152: #ifndef NO_IP
1.1       anton     153: ip=IP;
1.102     anton     154: #endif
1.64      anton     155: IF_spTOS(spTOS = sp[0]);
1.76      anton     156: SUPER_END;
1.1       anton     157: EXEC(xt);
                    158: 
1.47      anton     159: perform        ( a_addr -- )   gforth
1.55      anton     160: ""@code{@@ execute}.""
1.1       anton     161: /* and pfe */
1.102     anton     162: #ifndef NO_IP
1.1       anton     163: ip=IP;
1.102     anton     164: #endif
1.64      anton     165: IF_spTOS(spTOS = sp[0]);
1.76      anton     166: SUPER_END;
1.1       anton     167: EXEC(*(Xt *)a_addr);
                    168: :
                    169:  @ execute ;
                    170: 
1.31      jwilke    171: \fhas? skipbranchprims 0= [IF]
1.15      pazsan    172: \+glocals
1.1       anton     173: 
1.68      anton     174: branch-lp+!#   ( #ndisp #nlocals -- )  gforth  branch_lp_plus_store_number
1.1       anton     175: /* this will probably not be used */
1.68      anton     176: lp += nlocals;
                    177: SET_IP((Xt *)(((Cell)(IP-2))+ndisp));
1.1       anton     178: 
1.15      pazsan    179: \+
1.1       anton     180: 
1.68      anton     181: branch ( #ndisp -- )           gforth
                    182: SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.1       anton     183: :
                    184:  r> dup @ + >r ;
                    185: 
1.68      anton     186: \ condbranch(forthname,stackeffect,restline,code,forthcode)
1.1       anton     187: \ this is non-syntactical: code must open a brace that is closed by the macro
                    188: define(condbranch,
1.68      anton     189: $1 ( `#'ndisp $2 ) $3
                    190: $4     SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     191: INST_TAIL;
1.1       anton     192: }
1.87      anton     193: SUPER_CONTINUE;
1.68      anton     194: $5
1.1       anton     195: 
1.15      pazsan    196: \+glocals
1.1       anton     197: 
1.68      anton     198: $1-lp+!`#' ( `#'ndisp `#'nlocals $2 ) $3_lp_plus_store_number
                    199: $4    lp += nlocals;
                    200: SET_IP((Xt *)(((Cell)(IP-2))+ndisp));
1.96      anton     201: INST_TAIL;
1.1       anton     202: }
1.87      anton     203: SUPER_CONTINUE;
1.1       anton     204: 
1.15      pazsan    205: \+
1.1       anton     206: )
                    207: 
1.68      anton     208: condbranch(?branch,f --,f83    question_branch,
1.1       anton     209: if (f==0) {
1.5       jwilke    210: ,:
                    211:  0= dup     \ !f !f
                    212:  r> dup @   \ !f !f IP branchoffset
                    213:  rot and +  \ !f IP|IP+branchoffset
                    214:  swap 0= cell and + \ IP''
                    215:  >r ;)
1.1       anton     216: 
                    217: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
                    218: \ is only used in if's (yet)
                    219: 
1.15      pazsan    220: \+xconds
1.1       anton     221: 
1.68      anton     222: ?dup-?branch   ( #ndisp f -- f )       new     question_dupe_question_branch
1.1       anton     223: ""The run-time procedure compiled by @code{?DUP-IF}.""
                    224: if (f==0) {
                    225:   sp++;
1.64      anton     226:   IF_spTOS(spTOS = sp[0]);
1.68      anton     227:   SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     228:   INST_TAIL;
1.1       anton     229: }
1.87      anton     230: SUPER_CONTINUE;
1.1       anton     231: 
1.68      anton     232: ?dup-0=-?branch        ( #ndisp f -- ) new     question_dupe_zero_equals_question_branch
1.1       anton     233: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
                    234: /* the approach taken here of declaring the word as having the stack
                    235: effect ( f -- ) and correcting for it in the branch-taken case costs a
                    236: few cycles in that case, but is easy to convert to a CONDBRANCH
                    237: invocation */
                    238: if (f!=0) {
                    239:   sp--;
1.68      anton     240:   SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.1       anton     241:   NEXT;
                    242: }
1.87      anton     243: SUPER_CONTINUE;
1.1       anton     244: 
1.15      pazsan    245: \+
1.31      jwilke    246: \f[THEN]
                    247: \fhas? skiploopprims 0= [IF]
1.1       anton     248: 
1.68      anton     249: condbranch((next),R:n1 -- R:n2,cmFORTH paren_next,
1.65      anton     250: n2=n1-1;
                    251: if (n1) {
1.1       anton     252: ,:
                    253:  r> r> dup 1- >r
                    254:  IF dup @ + >r ELSE cell+ >r THEN ;)
                    255: 
1.68      anton     256: condbranch((loop),R:nlimit R:n1 -- R:nlimit R:n2,gforth        paren_loop,
1.65      anton     257: n2=n1+1;
                    258: if (n2 != nlimit) {
1.1       anton     259: ,:
                    260:  r> r> 1+ r> 2dup =
                    261:  IF >r 1- >r cell+ >r
                    262:  ELSE >r >r dup @ + >r THEN ;)
                    263: 
1.68      anton     264: condbranch((+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_plus_loop,
1.1       anton     265: /* !! check this thoroughly */
                    266: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
                    267: /* dependent upon two's complement arithmetic */
1.65      anton     268: Cell olddiff = n1-nlimit;
                    269: n2=n1+n;       
1.1       anton     270: if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
                    271:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
                    272: ,:
                    273:  r> swap
                    274:  r> r> 2dup - >r
                    275:  2 pick r@ + r@ xor 0< 0=
                    276:  3 pick r> xor 0< 0= or
                    277:  IF    >r + >r dup @ + >r
                    278:  ELSE  >r >r drop cell+ >r THEN ;)
                    279: 
1.15      pazsan    280: \+xconds
1.1       anton     281: 
1.68      anton     282: condbranch((-loop),u R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_minus_loop,
1.65      anton     283: UCell olddiff = n1-nlimit;
                    284: n2=n1-u;
1.1       anton     285: if (olddiff>u) {
                    286: ,)
                    287: 
1.68      anton     288: condbranch((s+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth    paren_symmetric_plus_loop,
1.1       anton     289: ""The run-time procedure compiled by S+LOOP. It loops until the index
                    290: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
                    291: version of (+LOOP).""
                    292: /* !! check this thoroughly */
1.65      anton     293: Cell diff = n1-nlimit;
1.1       anton     294: Cell newdiff = diff+n;
                    295: if (n<0) {
                    296:     diff = -diff;
                    297:     newdiff = -newdiff;
                    298: }
1.65      anton     299: n2=n1+n;
1.1       anton     300: if (diff>=0 || newdiff<0) {
                    301: ,)
                    302: 
1.15      pazsan    303: \+
1.1       anton     304: 
1.65      anton     305: unloop ( R:w1 R:w2 -- )        core
                    306: /* !! alias for 2rdrop */
1.1       anton     307: :
                    308:  r> rdrop rdrop >r ;
                    309: 
1.65      anton     310: (for)  ( ncount -- R:nlimit R:ncount )         cmFORTH         paren_for
1.1       anton     311: /* or (for) = >r -- collides with unloop! */
1.65      anton     312: nlimit=0;
1.1       anton     313: :
                    314:  r> swap 0 >r >r >r ;
                    315: 
1.65      anton     316: (do)   ( nlimit nstart -- R:nlimit R:nstart )  gforth          paren_do
1.1       anton     317: :
                    318:  r> swap rot >r >r >r ;
                    319: 
1.68      anton     320: (?do)  ( #ndisp nlimit nstart -- R:nlimit R:nstart )   gforth  paren_question_do
1.1       anton     321: if (nstart == nlimit) {
1.68      anton     322:     SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     323:     INST_TAIL;
1.1       anton     324: }
1.87      anton     325: SUPER_CONTINUE;
1.1       anton     326: :
                    327:   2dup =
                    328:   IF   r> swap rot >r >r
                    329:        dup @ + >r
                    330:   ELSE r> swap rot >r >r
                    331:        cell+ >r
                    332:   THEN ;                               \ --> CORE-EXT
                    333: 
1.15      pazsan    334: \+xconds
1.1       anton     335: 
1.68      anton     336: (+do)  ( #ndisp nlimit nstart -- R:nlimit R:nstart )   gforth  paren_plus_do
1.1       anton     337: if (nstart >= nlimit) {
1.68      anton     338:     SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     339:     INST_TAIL;
1.1       anton     340: }
1.87      anton     341: SUPER_CONTINUE;
1.1       anton     342: :
                    343:  swap 2dup
                    344:  r> swap >r swap >r
                    345:  >=
                    346:  IF
                    347:      dup @ +
                    348:  ELSE
                    349:      cell+
                    350:  THEN  >r ;
                    351: 
1.68      anton     352: (u+do) ( #ndisp ulimit ustart -- R:ulimit R:ustart )   gforth  paren_u_plus_do
1.1       anton     353: if (ustart >= ulimit) {
1.68      anton     354:     SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     355:     INST_TAIL;
1.1       anton     356: }
1.87      anton     357: SUPER_CONTINUE;
1.1       anton     358: :
                    359:  swap 2dup
                    360:  r> swap >r swap >r
                    361:  u>=
                    362:  IF
                    363:      dup @ +
                    364:  ELSE
                    365:      cell+
                    366:  THEN  >r ;
                    367: 
1.68      anton     368: (-do)  ( #ndisp nlimit nstart -- R:nlimit R:nstart )   gforth  paren_minus_do
1.1       anton     369: if (nstart <= nlimit) {
1.68      anton     370:     SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     371:     INST_TAIL;
1.1       anton     372: }
1.87      anton     373: SUPER_CONTINUE;
1.1       anton     374: :
                    375:  swap 2dup
                    376:  r> swap >r swap >r
                    377:  <=
                    378:  IF
                    379:      dup @ +
                    380:  ELSE
                    381:      cell+
                    382:  THEN  >r ;
                    383: 
1.68      anton     384: (u-do) ( #ndisp ulimit ustart -- R:ulimit R:ustart )   gforth  paren_u_minus_do
1.1       anton     385: if (ustart <= ulimit) {
1.68      anton     386:     SET_IP((Xt *)(((Cell)(IP-1))+ndisp));
1.96      anton     387:     INST_TAIL;
1.1       anton     388: }
1.87      anton     389: SUPER_CONTINUE;
1.1       anton     390: :
                    391:  swap 2dup
                    392:  r> swap >r swap >r
                    393:  u<=
                    394:  IF
                    395:      dup @ +
                    396:  ELSE
                    397:      cell+
                    398:  THEN  >r ;
                    399: 
1.15      pazsan    400: \+
1.1       anton     401: 
1.5       jwilke    402: \ don't make any assumptions where the return stack is!!
                    403: \ implement this in machine code if it should run quickly!
                    404: 
1.65      anton     405: i      ( R:n -- R:n n )                core
1.1       anton     406: :
1.5       jwilke    407: \ rp@ cell+ @ ;
                    408:   r> r> tuck >r >r ;
1.1       anton     409: 
1.65      anton     410: i'     ( R:w R:w2 -- R:w R:w2 w )              gforth          i_tick
1.1       anton     411: :
1.5       jwilke    412: \ rp@ cell+ cell+ @ ;
                    413:   r> r> r> dup itmp ! >r >r >r itmp @ ;
                    414: variable itmp
1.1       anton     415: 
1.65      anton     416: j      ( R:n R:d1 -- n R:n R:d1 )              core
1.1       anton     417: :
1.5       jwilke    418: \ rp@ cell+ cell+ cell+ @ ;
                    419:   r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
                    420: [IFUNDEF] itmp variable itmp [THEN]
1.1       anton     421: 
1.65      anton     422: k      ( R:n R:d1 R:d2 -- n R:n R:d1 R:d2 )            gforth
1.1       anton     423: :
1.5       jwilke    424: \ rp@ [ 5 cells ] Literal + @ ;
                    425:   r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
                    426: [IFUNDEF] itmp variable itmp [THEN]
1.31      jwilke    427: 
                    428: \f[THEN]
1.1       anton     429: 
                    430: \ digit is high-level: 0/0%
                    431: 
1.83      pazsan    432: \g strings
                    433: 
1.47      anton     434: move   ( c_from c_to ucount -- )               core
1.52      anton     435: ""Copy the contents of @i{ucount} aus at @i{c-from} to
1.33      anton     436: @i{c-to}. @code{move} works correctly even if the two areas overlap.""
1.52      anton     437: /* !! note that the standard specifies addr, not c-addr */
1.1       anton     438: memmove(c_to,c_from,ucount);
                    439: /* make an Ifdef for bsd and others? */
                    440: :
                    441:  >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
                    442: 
1.47      anton     443: cmove  ( c_from c_to u -- )    string  c_move
1.33      anton     444: ""Copy the contents of @i{ucount} characters from data space at
                    445: @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
                    446: from low address to high address; i.e., for overlapping areas it is
                    447: safe if @i{c-to}=<@i{c-from}.""
1.1       anton     448: while (u-- > 0)
                    449:   *c_to++ = *c_from++;
                    450: :
                    451:  bounds ?DO  dup c@ I c! 1+  LOOP  drop ;
                    452: 
1.47      anton     453: cmove> ( c_from c_to u -- )    string  c_move_up
1.33      anton     454: ""Copy the contents of @i{ucount} characters from data space at
                    455: @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
                    456: from high address to low address; i.e., for overlapping areas it is
                    457: safe if @i{c-to}>=@i{c-from}.""
1.1       anton     458: while (u-- > 0)
                    459:   c_to[u] = c_from[u];
                    460: :
                    461:  dup 0= IF  drop 2drop exit  THEN
                    462:  rot over + -rot bounds swap 1-
                    463:  DO  1- dup c@ I c!  -1 +LOOP  drop ;
                    464: 
1.47      anton     465: fill   ( c_addr u c -- )       core
1.52      anton     466: ""Store @i{c} in @i{u} chars starting at @i{c-addr}.""
1.1       anton     467: memset(c_addr,c,u);
                    468: :
                    469:  -rot bounds
                    470:  ?DO  dup I c!  LOOP  drop ;
                    471: 
1.47      anton     472: compare        ( c_addr1 u1 c_addr2 u2 -- n )  string
1.29      crook     473: ""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
                    474: the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
1.1       anton     475: is 1. Currently this is based on the machine's character
1.26      crook     476: comparison. In the future, this may change to consider the current
1.1       anton     477: locale and its collation order.""
1.46      pazsan    478: /* close ' to keep fontify happy */ 
1.1       anton     479: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
                    480: if (n==0)
                    481:   n = u1-u2;
                    482: if (n<0)
                    483:   n = -1;
                    484: else if (n>0)
                    485:   n = 1;
                    486: :
1.43      pazsan    487:  rot 2dup swap - >r min swap -text dup
                    488:  IF  rdrop  ELSE  drop r> sgn  THEN ;
                    489: : sgn ( n -- -1/0/1 )
                    490:  dup 0= IF EXIT THEN  0< 2* 1+ ;
1.1       anton     491: 
1.47      anton     492: -text  ( c_addr1 u c_addr2 -- n )      new     dash_text
1.1       anton     493: n = memcmp(c_addr1, c_addr2, u);
                    494: if (n<0)
                    495:   n = -1;
                    496: else if (n>0)
                    497:   n = 1;
                    498: :
                    499:  swap bounds
                    500:  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
1.49      pazsan    501:  ELSE  c@ I c@ - unloop  THEN  sgn ;
1.43      pazsan    502: : sgn ( n -- -1/0/1 )
                    503:  dup 0= IF EXIT THEN  0< 2* 1+ ;
1.1       anton     504: 
1.47      anton     505: toupper        ( c1 -- c2 )    gforth
1.29      crook     506: ""If @i{c1} is a lower-case character (in the current locale), @i{c2}
1.25      anton     507: is the equivalent upper-case character. All other characters are unchanged.""
1.1       anton     508: c2 = toupper(c1);
                    509: :
                    510:  dup [char] a - [ char z char a - 1 + ] Literal u<  bl and - ;
                    511: 
1.47      anton     512: capscomp       ( c_addr1 u c_addr2 -- n )      new
1.1       anton     513: n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
                    514: if (n<0)
                    515:   n = -1;
                    516: else if (n>0)
                    517:   n = 1;
                    518: :
                    519:  swap bounds
                    520:  ?DO  dup c@ I c@ <>
                    521:      IF  dup c@ toupper I c@ toupper =
                    522:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
1.49      pazsan    523:  ELSE  c@ toupper I c@ toupper - unloop  THEN  sgn ;
1.1       anton     524: 
1.47      anton     525: -trailing      ( c_addr u1 -- c_addr u2 )              string  dash_trailing
1.29      crook     526: ""Adjust the string specified by @i{c-addr, u1} to remove all trailing
                    527: spaces. @i{u2} is the length of the modified string.""
1.1       anton     528: u2 = u1;
1.4       anton     529: while (u2>0 && c_addr[u2-1] == ' ')
1.1       anton     530:   u2--;
                    531: :
                    532:  BEGIN  1- 2dup + c@ bl =  WHILE
                    533:         dup  0= UNTIL  ELSE  1+  THEN ;
                    534: 
1.47      anton     535: /string        ( c_addr1 u1 n -- c_addr2 u2 )  string  slash_string
1.29      crook     536: ""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
1.27      crook     537: characters from the start of the string.""
1.1       anton     538: c_addr2 = c_addr1+n;
                    539: u2 = u1-n;
                    540: :
                    541:  tuck - >r + r> dup 0< IF  - 0  THEN ;
                    542: 
1.83      pazsan    543: \g arith
                    544: 
1.47      anton     545: +      ( n1 n2 -- n )          core    plus
1.1       anton     546: n = n1+n2;
                    547: 
                    548: \ PFE-0.9.14 has it differently, but the next release will have it as follows
1.47      anton     549: under+ ( n1 n2 n3 -- n n2 )    gforth  under_plus
1.29      crook     550: ""add @i{n3} to @i{n1} (giving @i{n})""
1.1       anton     551: n = n1+n3;
                    552: :
                    553:  rot + swap ;
                    554: 
1.47      anton     555: -      ( n1 n2 -- n )          core    minus
1.1       anton     556: n = n1-n2;
                    557: :
                    558:  negate + ;
                    559: 
1.47      anton     560: negate ( n1 -- n2 )            core
1.1       anton     561: /* use minus as alias */
                    562: n2 = -n1;
                    563: :
                    564:  invert 1+ ;
                    565: 
1.47      anton     566: 1+     ( n1 -- n2 )            core            one_plus
1.1       anton     567: n2 = n1+1;
                    568: :
                    569:  1 + ;
                    570: 
1.47      anton     571: 1-     ( n1 -- n2 )            core            one_minus
1.1       anton     572: n2 = n1-1;
                    573: :
                    574:  1 - ;
                    575: 
1.47      anton     576: max    ( n1 n2 -- n )  core
1.1       anton     577: if (n1<n2)
                    578:   n = n2;
                    579: else
                    580:   n = n1;
                    581: :
                    582:  2dup < IF swap THEN drop ;
                    583: 
1.47      anton     584: min    ( n1 n2 -- n )  core
1.1       anton     585: if (n1<n2)
                    586:   n = n1;
                    587: else
                    588:   n = n2;
                    589: :
                    590:  2dup > IF swap THEN drop ;
                    591: 
1.52      anton     592: abs    ( n -- u )      core
                    593: if (n<0)
                    594:   u = -n;
1.1       anton     595: else
1.52      anton     596:   u = n;
1.1       anton     597: :
                    598:  dup 0< IF negate THEN ;
                    599: 
1.47      anton     600: *      ( n1 n2 -- n )          core    star
1.1       anton     601: n = n1*n2;
                    602: :
                    603:  um* drop ;
                    604: 
1.47      anton     605: /      ( n1 n2 -- n )          core    slash
1.1       anton     606: n = n1/n2;
                    607: :
                    608:  /mod nip ;
                    609: 
1.47      anton     610: mod    ( n1 n2 -- n )          core
1.1       anton     611: n = n1%n2;
                    612: :
                    613:  /mod drop ;
                    614: 
1.47      anton     615: /mod   ( n1 n2 -- n3 n4 )              core            slash_mod
1.1       anton     616: n4 = n1/n2;
                    617: n3 = n1%n2; /* !! is this correct? look into C standard! */
                    618: :
                    619:  >r s>d r> fm/mod ;
                    620: 
1.47      anton     621: 2*     ( n1 -- n2 )            core            two_star
1.52      anton     622: ""Shift left by 1; also works on unsigned numbers""
1.1       anton     623: n2 = 2*n1;
                    624: :
                    625:  dup + ;
                    626: 
1.47      anton     627: 2/     ( n1 -- n2 )            core            two_slash
1.52      anton     628: ""Arithmetic shift right by 1.  For signed numbers this is a floored
                    629: division by 2 (note that @code{/} not necessarily floors).""
1.1       anton     630: n2 = n1>>1;
                    631: :
                    632:  dup MINI and IF 1 ELSE 0 THEN
                    633:  [ bits/byte cell * 1- ] literal 
1.5       jwilke    634:  0 DO 2* swap dup 2* >r MINI and 
1.1       anton     635:      IF 1 ELSE 0 THEN or r> swap
                    636:  LOOP nip ;
                    637: 
1.47      anton     638: fm/mod ( d1 n1 -- n2 n3 )              core            f_m_slash_mod
1.29      crook     639: ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}.""
1.1       anton     640: #ifdef BUGGY_LONG_LONG
                    641: DCell r = fmdiv(d1,n1);
                    642: n2=r.hi;
                    643: n3=r.lo;
                    644: #else
                    645: /* assumes that the processor uses either floored or symmetric division */
                    646: n3 = d1/n1;
                    647: n2 = d1%n1;
                    648: /* note that this 1%-3>0 is optimized by the compiler */
                    649: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
                    650:   n3--;
                    651:   n2+=n1;
                    652: }
                    653: #endif
                    654: :
                    655:  dup >r dup 0< IF  negate >r dnegate r>  THEN
                    656:  over       0< IF  tuck + swap  THEN
                    657:  um/mod
                    658:  r> 0< IF  swap negate swap  THEN ;
                    659: 
1.47      anton     660: sm/rem ( d1 n1 -- n2 n3 )              core            s_m_slash_rem
1.29      crook     661: ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0.""
1.1       anton     662: #ifdef BUGGY_LONG_LONG
                    663: DCell r = smdiv(d1,n1);
                    664: n2=r.hi;
                    665: n3=r.lo;
                    666: #else
                    667: /* assumes that the processor uses either floored or symmetric division */
                    668: n3 = d1/n1;
                    669: n2 = d1%n1;
                    670: /* note that this 1%-3<0 is optimized by the compiler */
                    671: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
                    672:   n3++;
                    673:   n2-=n1;
                    674: }
                    675: #endif
                    676: :
                    677:  over >r dup >r abs -rot
                    678:  dabs rot um/mod
                    679:  r> r@ xor 0< IF       negate       THEN
                    680:  r>        0< IF  swap negate swap  THEN ;
                    681: 
1.47      anton     682: m*     ( n1 n2 -- d )          core    m_star
1.1       anton     683: #ifdef BUGGY_LONG_LONG
                    684: d = mmul(n1,n2);
                    685: #else
                    686: d = (DCell)n1 * (DCell)n2;
                    687: #endif
                    688: :
                    689:  2dup      0< and >r
                    690:  2dup swap 0< and >r
                    691:  um* r> - r> - ;
                    692: 
1.47      anton     693: um*    ( u1 u2 -- ud )         core    u_m_star
1.1       anton     694: /* use u* as alias */
                    695: #ifdef BUGGY_LONG_LONG
                    696: ud = ummul(u1,u2);
                    697: #else
                    698: ud = (UDCell)u1 * (UDCell)u2;
                    699: #endif
                    700: :
                    701:    >r >r 0 0 r> r> [ 8 cells ] literal 0
                    702:    DO
                    703:        over >r dup >r 0< and d2*+ drop
                    704:        r> 2* r> swap
                    705:    LOOP 2drop ;
                    706: : d2*+ ( ud n -- ud+n c )
                    707:    over MINI
                    708:    and >r >r 2dup d+ swap r> + swap r> ;
                    709: 
1.47      anton     710: um/mod ( ud u1 -- u2 u3 )              core    u_m_slash_mod
1.32      anton     711: ""ud=u3*u1+u2, u1>u2>=0""
1.1       anton     712: #ifdef BUGGY_LONG_LONG
                    713: UDCell r = umdiv(ud,u1);
                    714: u2=r.hi;
                    715: u3=r.lo;
                    716: #else
                    717: u3 = ud/u1;
                    718: u2 = ud%u1;
                    719: #endif
                    720: :
                    721:    0 swap [ 8 cells 1 + ] literal 0
1.5       jwilke    722:    ?DO /modstep
1.1       anton     723:    LOOP drop swap 1 rshift or swap ;
                    724: : /modstep ( ud c R: u -- ud-?u c R: u )
1.5       jwilke    725:    >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN  d2*+ r> ;
1.1       anton     726: : d2*+ ( ud n -- ud+n c )
                    727:    over MINI
                    728:    and >r >r 2dup d+ swap r> + swap r> ;
                    729: 
1.47      anton     730: m+     ( d1 n -- d2 )          double          m_plus
1.1       anton     731: #ifdef BUGGY_LONG_LONG
                    732: d2.lo = d1.lo+n;
                    733: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
                    734: #else
                    735: d2 = d1+n;
                    736: #endif
                    737: :
                    738:  s>d d+ ;
                    739: 
1.47      anton     740: d+     ( d1 d2 -- d )          double  d_plus
1.1       anton     741: #ifdef BUGGY_LONG_LONG
                    742: d.lo = d1.lo+d2.lo;
                    743: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
                    744: #else
                    745: d = d1+d2;
                    746: #endif
                    747: :
                    748:  rot + >r tuck + swap over u> r> swap - ;
                    749: 
1.47      anton     750: d-     ( d1 d2 -- d )          double          d_minus
1.1       anton     751: #ifdef BUGGY_LONG_LONG
                    752: d.lo = d1.lo - d2.lo;
                    753: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
                    754: #else
                    755: d = d1-d2;
                    756: #endif
                    757: :
                    758:  dnegate d+ ;
                    759: 
1.47      anton     760: dnegate        ( d1 -- d2 )            double  d_negate
1.1       anton     761: /* use dminus as alias */
                    762: #ifdef BUGGY_LONG_LONG
                    763: d2 = dnegate(d1);
                    764: #else
                    765: d2 = -d1;
                    766: #endif
                    767: :
                    768:  invert swap negate tuck 0= - ;
                    769: 
1.47      anton     770: d2*    ( d1 -- d2 )            double          d_two_star
1.52      anton     771: ""Shift left by 1; also works on unsigned numbers""
1.1       anton     772: #ifdef BUGGY_LONG_LONG
                    773: d2.lo = d1.lo<<1;
                    774: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
                    775: #else
                    776: d2 = 2*d1;
                    777: #endif
                    778: :
                    779:  2dup d+ ;
                    780: 
1.47      anton     781: d2/    ( d1 -- d2 )            double          d_two_slash
1.52      anton     782: ""Arithmetic shift right by 1.  For signed numbers this is a floored
                    783: division by 2.""
1.1       anton     784: #ifdef BUGGY_LONG_LONG
                    785: d2.hi = d1.hi>>1;
                    786: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
                    787: #else
                    788: d2 = d1>>1;
                    789: #endif
                    790: :
                    791:  dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
                    792:  r> IF  [ 1 8 cells 1- lshift ] Literal + THEN  swap ;
                    793: 
1.47      anton     794: and    ( w1 w2 -- w )          core
1.1       anton     795: w = w1&w2;
                    796: 
1.47      anton     797: or     ( w1 w2 -- w )          core
1.1       anton     798: w = w1|w2;
                    799: :
                    800:  invert swap invert and invert ;
                    801: 
1.47      anton     802: xor    ( w1 w2 -- w )          core    x_or
1.1       anton     803: w = w1^w2;
                    804: 
1.47      anton     805: invert ( w1 -- w2 )            core
1.1       anton     806: w2 = ~w1;
                    807: :
                    808:  MAXU xor ;
                    809: 
1.47      anton     810: rshift ( u1 n -- u2 )          core    r_shift
1.53      anton     811: ""Logical shift right by @i{n} bits.""
1.1       anton     812:   u2 = u1>>n;
                    813: :
                    814:     0 ?DO 2/ MAXI and LOOP ;
                    815: 
1.47      anton     816: lshift ( u1 n -- u2 )          core    l_shift
1.1       anton     817:   u2 = u1<<n;
                    818: :
                    819:     0 ?DO 2* LOOP ;
                    820: 
                    821: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
                    822: define(comparisons,
1.47      anton     823: $1=    ( $2 -- f )             $6      $3equals
1.1       anton     824: f = FLAG($4==$5);
                    825: :
                    826:     [ char $1x char 0 = [IF]
                    827:        ] IF false ELSE true THEN [
                    828:     [ELSE]
                    829:        ] xor 0= [
                    830:     [THEN] ] ;
                    831: 
1.47      anton     832: $1<>   ( $2 -- f )             $7      $3not_equals
1.1       anton     833: f = FLAG($4!=$5);
                    834: :
                    835:     [ char $1x char 0 = [IF]
                    836:        ] IF true ELSE false THEN [
                    837:     [ELSE]
                    838:        ] xor 0<> [
                    839:     [THEN] ] ;
                    840: 
1.47      anton     841: $1<    ( $2 -- f )             $8      $3less_than
1.1       anton     842: f = FLAG($4<$5);
                    843: :
                    844:     [ char $1x char 0 = [IF]
                    845:        ] MINI and 0<> [
                    846:     [ELSE] char $1x char u = [IF]
                    847:        ]   2dup xor 0<  IF nip ELSE - THEN 0<  [
                    848:        [ELSE]
                    849:            ] MINI xor >r MINI xor r> u< [
                    850:        [THEN]
                    851:     [THEN] ] ;
                    852: 
1.47      anton     853: $1>    ( $2 -- f )             $9      $3greater_than
1.1       anton     854: f = FLAG($4>$5);
                    855: :
                    856:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    857:     $1< ;
                    858: 
1.47      anton     859: $1<=   ( $2 -- f )             gforth  $3less_or_equal
1.1       anton     860: f = FLAG($4<=$5);
                    861: :
                    862:     $1> 0= ;
                    863: 
1.47      anton     864: $1>=   ( $2 -- f )             gforth  $3greater_or_equal
1.1       anton     865: f = FLAG($4>=$5);
                    866: :
                    867:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    868:     $1<= ;
                    869: 
                    870: )
                    871: 
                    872: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
                    873: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
                    874: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
                    875: 
                    876: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
                    877: define(dcomparisons,
1.47      anton     878: $1=    ( $2 -- f )             $6      $3equals
1.1       anton     879: #ifdef BUGGY_LONG_LONG
                    880: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
                    881: #else
                    882: f = FLAG($4==$5);
                    883: #endif
                    884: 
1.47      anton     885: $1<>   ( $2 -- f )             $7      $3not_equals
1.1       anton     886: #ifdef BUGGY_LONG_LONG
                    887: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
                    888: #else
                    889: f = FLAG($4!=$5);
                    890: #endif
                    891: 
1.47      anton     892: $1<    ( $2 -- f )             $8      $3less_than
1.1       anton     893: #ifdef BUGGY_LONG_LONG
                    894: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
                    895: #else
                    896: f = FLAG($4<$5);
                    897: #endif
                    898: 
1.47      anton     899: $1>    ( $2 -- f )             $9      $3greater_than
1.1       anton     900: #ifdef BUGGY_LONG_LONG
                    901: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
                    902: #else
                    903: f = FLAG($4>$5);
                    904: #endif
                    905: 
1.47      anton     906: $1<=   ( $2 -- f )             gforth  $3less_or_equal
1.1       anton     907: #ifdef BUGGY_LONG_LONG
                    908: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
                    909: #else
                    910: f = FLAG($4<=$5);
                    911: #endif
                    912: 
1.47      anton     913: $1>=   ( $2 -- f )             gforth  $3greater_or_equal
1.1       anton     914: #ifdef BUGGY_LONG_LONG
                    915: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
                    916: #else
                    917: f = FLAG($4>=$5);
                    918: #endif
                    919: 
                    920: )
                    921: 
1.15      pazsan    922: \+dcomps
1.1       anton     923: 
                    924: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
                    925: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
                    926: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
                    927: 
1.15      pazsan    928: \+
1.1       anton     929: 
1.47      anton     930: within ( u1 u2 u3 -- f )               core-ext
1.32      anton     931: ""u2=<u1<u3 or: u3=<u2 and u1 is not in [u3,u2).  This works for
                    932: unsigned and signed numbers (but not a mixture).  Another way to think
                    933: about this word is to consider the numbers as a circle (wrapping
                    934: around from @code{max-u} to 0 for unsigned, and from @code{max-n} to
                    935: min-n for signed numbers); now consider the range from u2 towards
                    936: increasing numbers up to and excluding u3 (giving an empty range if
1.52      anton     937: u2=u3); if u1 is in this range, @code{within} returns true.""
1.1       anton     938: f = FLAG(u1-u2 < u3-u2);
                    939: :
                    940:  over - >r - r> u< ;
                    941: 
1.83      pazsan    942: \g internal
                    943: 
1.47      anton     944: sp@    ( -- a_addr )           gforth          sp_fetch
1.1       anton     945: a_addr = sp+1;
                    946: 
1.47      anton     947: sp!    ( a_addr -- )           gforth          sp_store
1.1       anton     948: sp = a_addr;
1.64      anton     949: /* works with and without spTOS caching */
1.1       anton     950: 
1.47      anton     951: rp@    ( -- a_addr )           gforth          rp_fetch
1.1       anton     952: a_addr = rp;
                    953: 
1.47      anton     954: rp!    ( a_addr -- )           gforth          rp_store
1.1       anton     955: rp = a_addr;
                    956: 
1.15      pazsan    957: \+floating
1.1       anton     958: 
1.47      anton     959: fp@    ( -- f_addr )   gforth  fp_fetch
1.1       anton     960: f_addr = fp;
                    961: 
1.47      anton     962: fp!    ( f_addr -- )   gforth  fp_store
1.1       anton     963: fp = f_addr;
                    964: 
1.15      pazsan    965: \+
1.1       anton     966: 
1.65      anton     967: ;s     ( R:w -- )              gforth  semis
1.22      crook     968: ""The primitive compiled by @code{EXIT}.""
1.102     anton     969: #ifdef NO_IP
                    970: INST_TAIL;
                    971: goto *(void *)w;
                    972: #else
1.65      anton     973: SET_IP((Xt *)w);
1.102     anton     974: #endif
1.1       anton     975: 
1.83      pazsan    976: \g stack
                    977: 
1.65      anton     978: >r     ( w -- R:w )            core    to_r
1.1       anton     979: :
                    980:  (>r) ;
                    981: : (>r)  rp@ cell+ @ rp@ ! rp@ cell+ ! ;
                    982: 
1.65      anton     983: r>     ( R:w -- w )            core    r_from
1.1       anton     984: :
                    985:  rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
                    986: Create (rdrop) ' ;s A,
                    987: 
1.65      anton     988: rdrop  ( R:w -- )              gforth
1.1       anton     989: :
                    990:  r> r> drop >r ;
                    991: 
1.65      anton     992: 2>r    ( w1 w2 -- R:w1 R:w2 )  core-ext        two_to_r
1.1       anton     993: :
                    994:  swap r> swap >r swap >r >r ;
                    995: 
1.65      anton     996: 2r>    ( R:w1 R:w2 -- w1 w2 )  core-ext        two_r_from
1.1       anton     997: :
                    998:  r> r> swap r> swap >r swap ;
                    999: 
1.65      anton    1000: 2r@    ( R:w1 R:w2 -- R:w1 R:w2 w1 w2 )        core-ext        two_r_fetch
1.1       anton    1001: :
                   1002:  i' j ;
                   1003: 
1.65      anton    1004: 2rdrop (  R:w1 R:w2 -- )               gforth  two_r_drop
1.1       anton    1005: :
                   1006:  r> r> drop r> drop >r ;
                   1007: 
1.47      anton    1008: over   ( w1 w2 -- w1 w2 w1 )           core
1.1       anton    1009: :
                   1010:  sp@ cell+ @ ;
                   1011: 
1.47      anton    1012: drop   ( w -- )                core
1.1       anton    1013: :
                   1014:  IF THEN ;
                   1015: 
1.47      anton    1016: swap   ( w1 w2 -- w2 w1 )              core
1.1       anton    1017: :
                   1018:  >r (swap) ! r> (swap) @ ;
                   1019: Variable (swap)
                   1020: 
1.47      anton    1021: dup    ( w -- w w )            core    dupe
1.1       anton    1022: :
                   1023:  sp@ @ ;
                   1024: 
1.47      anton    1025: rot    ( w1 w2 w3 -- w2 w3 w1 )        core    rote
1.1       anton    1026: :
                   1027: [ defined? (swap) [IF] ]
                   1028:     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
                   1029: Variable (rot)
                   1030: [ELSE] ]
                   1031:     >r swap r> swap ;
                   1032: [THEN]
                   1033: 
1.47      anton    1034: -rot   ( w1 w2 w3 -- w3 w1 w2 )        gforth  not_rote
1.1       anton    1035: :
                   1036:  rot rot ;
                   1037: 
1.47      anton    1038: nip    ( w1 w2 -- w2 )         core-ext
1.1       anton    1039: :
1.6       jwilke   1040:  swap drop ;
1.1       anton    1041: 
1.47      anton    1042: tuck   ( w1 w2 -- w2 w1 w2 )   core-ext
1.1       anton    1043: :
                   1044:  swap over ;
                   1045: 
1.47      anton    1046: ?dup   ( w -- w )                      core    question_dupe
1.52      anton    1047: ""Actually the stack effect is: @code{( w -- 0 | w w )}.  It performs a
                   1048: @code{dup} if w is nonzero.""
1.1       anton    1049: if (w!=0) {
1.64      anton    1050:   IF_spTOS(*sp-- = w;)
1.1       anton    1051: #ifndef USE_TOS
                   1052:   *--sp = w;
                   1053: #endif
                   1054: }
                   1055: :
                   1056:  dup IF dup THEN ;
                   1057: 
1.47      anton    1058: pick   ( u -- w )                      core-ext
1.52      anton    1059: ""Actually the stack effect is @code{ x0 ... xu u -- x0 ... xu x0 }.""
1.1       anton    1060: w = sp[u+1];
                   1061: :
                   1062:  1+ cells sp@ + @ ;
                   1063: 
1.47      anton    1064: 2drop  ( w1 w2 -- )            core    two_drop
1.1       anton    1065: :
                   1066:  drop drop ;
                   1067: 
1.47      anton    1068: 2dup   ( w1 w2 -- w1 w2 w1 w2 )        core    two_dupe
1.1       anton    1069: :
                   1070:  over over ;
                   1071: 
1.47      anton    1072: 2over  ( w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 )    core    two_over
1.1       anton    1073: :
                   1074:  3 pick 3 pick ;
                   1075: 
1.47      anton    1076: 2swap  ( w1 w2 w3 w4 -- w3 w4 w1 w2 )  core    two_swap
1.1       anton    1077: :
                   1078:  rot >r rot r> ;
                   1079: 
1.47      anton    1080: 2rot   ( w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 )      double-ext      two_rote
1.1       anton    1081: :
                   1082:  >r >r 2swap r> r> 2swap ;
                   1083: 
1.47      anton    1084: 2nip   ( w1 w2 w3 w4 -- w3 w4 )        gforth  two_nip
1.1       anton    1085: :
                   1086:  2swap 2drop ;
                   1087: 
1.47      anton    1088: 2tuck  ( w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 )    gforth  two_tuck
1.1       anton    1089: :
                   1090:  2swap 2over ;
                   1091: 
                   1092: \ toggle is high-level: 0.11/0.42%
                   1093: 
1.47      anton    1094: @      ( a_addr -- w )         core    fetch
1.52      anton    1095: ""@i{w} is the cell stored at @i{a_addr}.""
1.1       anton    1096: w = *a_addr;
                   1097: 
1.47      anton    1098: !      ( w a_addr -- )         core    store
1.52      anton    1099: ""Store @i{w} into the cell at @i{a-addr}.""
1.1       anton    1100: *a_addr = w;
                   1101: 
1.47      anton    1102: +!     ( n a_addr -- )         core    plus_store
1.52      anton    1103: ""Add @i{n} to the cell at @i{a-addr}.""
1.1       anton    1104: *a_addr += n;
                   1105: :
                   1106:  tuck @ + swap ! ;
                   1107: 
1.47      anton    1108: c@     ( c_addr -- c )         core    c_fetch
1.52      anton    1109: ""@i{c} is the char stored at @i{c_addr}.""
1.1       anton    1110: c = *c_addr;
                   1111: :
                   1112: [ bigendian [IF] ]
                   1113:     [ cell>bit 4 = [IF] ]
                   1114:        dup [ 0 cell - ] Literal and @ swap 1 and
                   1115:        IF  $FF and  ELSE  8>>  THEN  ;
                   1116:     [ [ELSE] ]
                   1117:        dup [ cell 1- ] literal and
                   1118:        tuck - @ swap [ cell 1- ] literal xor
                   1119:        0 ?DO 8>> LOOP $FF and
                   1120:     [ [THEN] ]
                   1121: [ [ELSE] ]
                   1122:     [ cell>bit 4 = [IF] ]
                   1123:        dup [ 0 cell - ] Literal and @ swap 1 and
                   1124:        IF  8>>  ELSE  $FF and  THEN
                   1125:     [ [ELSE] ]
                   1126:        dup [ cell  1- ] literal and 
                   1127:        tuck - @ swap
                   1128:        0 ?DO 8>> LOOP 255 and
                   1129:     [ [THEN] ]
                   1130: [ [THEN] ]
                   1131: ;
                   1132: : 8>> 2/ 2/ 2/ 2/  2/ 2/ 2/ 2/ ;
                   1133: 
1.47      anton    1134: c!     ( c c_addr -- )         core    c_store
1.52      anton    1135: ""Store @i{c} into the char at @i{c-addr}.""
1.1       anton    1136: *c_addr = c;
                   1137: :
                   1138: [ bigendian [IF] ]
                   1139:     [ cell>bit 4 = [IF] ]
                   1140:        tuck 1 and IF  $FF and  ELSE  8<<  THEN >r
                   1141:        dup -2 and @ over 1 and cells masks + @ and
                   1142:        r> or swap -2 and ! ;
                   1143:        Create masks $00FF , $FF00 ,
                   1144:     [ELSE] ]
                   1145:        dup [ cell 1- ] literal and dup 
                   1146:        [ cell 1- ] literal xor >r
                   1147:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
                   1148:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1149:     [THEN]
                   1150: [ELSE] ]
                   1151:     [ cell>bit 4 = [IF] ]
                   1152:        tuck 1 and IF  8<<  ELSE  $FF and  THEN >r
                   1153:        dup -2 and @ over 1 and cells masks + @ and
                   1154:        r> or swap -2 and ! ;
                   1155:        Create masks $FF00 , $00FF ,
                   1156:     [ELSE] ]
                   1157:        dup [ cell 1- ] literal and dup >r
                   1158:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
                   1159:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1160:     [THEN]
                   1161: [THEN]
                   1162: : 8<< 2* 2* 2* 2*  2* 2* 2* 2* ;
                   1163: 
1.47      anton    1164: 2!     ( w1 w2 a_addr -- )             core    two_store
1.52      anton    1165: ""Store @i{w2} into the cell at @i{c-addr} and @i{w1} into the next cell.""
1.1       anton    1166: a_addr[0] = w2;
                   1167: a_addr[1] = w1;
                   1168: :
                   1169:  tuck ! cell+ ! ;
                   1170: 
1.47      anton    1171: 2@     ( a_addr -- w1 w2 )             core    two_fetch
1.52      anton    1172: ""@i{w2} is the content of the cell stored at @i{a-addr}, @i{w1} is
                   1173: the content of the next cell.""
1.1       anton    1174: w2 = a_addr[0];
                   1175: w1 = a_addr[1];
                   1176: :
                   1177:  dup cell+ @ swap @ ;
                   1178: 
1.47      anton    1179: cell+  ( a_addr1 -- a_addr2 )  core    cell_plus
1.52      anton    1180: ""@code{1 cells +}""
1.1       anton    1181: a_addr2 = a_addr1+1;
                   1182: :
                   1183:  cell + ;
                   1184: 
1.47      anton    1185: cells  ( n1 -- n2 )            core
1.52      anton    1186: "" @i{n2} is the number of address units of @i{n1} cells.""
1.1       anton    1187: n2 = n1 * sizeof(Cell);
                   1188: :
                   1189:  [ cell
                   1190:  2/ dup [IF] ] 2* [ [THEN]
                   1191:  2/ dup [IF] ] 2* [ [THEN]
                   1192:  2/ dup [IF] ] 2* [ [THEN]
                   1193:  2/ dup [IF] ] 2* [ [THEN]
                   1194:  drop ] ;
                   1195: 
1.47      anton    1196: char+  ( c_addr1 -- c_addr2 )  core    char_plus
1.52      anton    1197: ""@code{1 chars +}.""
1.1       anton    1198: c_addr2 = c_addr1 + 1;
                   1199: :
                   1200:  1+ ;
                   1201: 
1.47      anton    1202: (chars)        ( n1 -- n2 )    gforth  paren_chars
1.1       anton    1203: n2 = n1 * sizeof(Char);
                   1204: :
                   1205:  ;
                   1206: 
1.47      anton    1207: count  ( c_addr1 -- c_addr2 u )        core
1.56      anton    1208: ""@i{c-addr2} is the first character and @i{u} the length of the
                   1209: counted string at @i{c-addr1}.""
1.1       anton    1210: u = *c_addr1;
                   1211: c_addr2 = c_addr1+1;
                   1212: :
                   1213:  dup 1+ swap c@ ;
                   1214: 
1.47      anton    1215: (f83find)      ( c_addr u f83name1 -- f83name2 )       new     paren_f83find
1.13      pazsan   1216: for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
1.1       anton    1217:   if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1218:       memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1219:     break;
                   1220: f83name2=f83name1;
                   1221: :
                   1222:     BEGIN  dup WHILE  (find-samelen)  dup  WHILE
                   1223:        >r 2dup r@ cell+ char+ capscomp  0=
                   1224:        IF  2drop r>  EXIT  THEN
                   1225:        r> @
                   1226:     REPEAT  THEN  nip nip ;
                   1227: : (find-samelen) ( u f83name1 -- u f83name2/0 )
1.72      pazsan   1228:     BEGIN  2dup cell+ c@ $1F and <> WHILE  @  dup 0= UNTIL THEN ;
1.1       anton    1229: 
1.15      pazsan   1230: \+hash
1.1       anton    1231: 
1.47      anton    1232: (hashfind)     ( c_addr u a_addr -- f83name2 ) new     paren_hashfind
1.13      pazsan   1233: struct F83Name *f83name1;
1.1       anton    1234: f83name2=NULL;
                   1235: while(a_addr != NULL)
                   1236: {
1.13      pazsan   1237:    f83name1=(struct F83Name *)(a_addr[1]);
1.1       anton    1238:    a_addr=(Cell *)(a_addr[0]);
                   1239:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1240:        memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1241:      {
                   1242:        f83name2=f83name1;
                   1243:        break;
                   1244:      }
                   1245: }
                   1246: :
                   1247:  BEGIN  dup  WHILE
                   1248:         2@ >r >r dup r@ cell+ c@ $1F and =
                   1249:         IF  2dup r@ cell+ char+ capscomp 0=
                   1250:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   1251:        rdrop r>
                   1252:  REPEAT nip nip ;
                   1253: 
1.47      anton    1254: (tablefind)    ( c_addr u a_addr -- f83name2 ) new     paren_tablefind
1.1       anton    1255: ""A case-sensitive variant of @code{(hashfind)}""
1.13      pazsan   1256: struct F83Name *f83name1;
1.1       anton    1257: f83name2=NULL;
                   1258: while(a_addr != NULL)
                   1259: {
1.13      pazsan   1260:    f83name1=(struct F83Name *)(a_addr[1]);
1.1       anton    1261:    a_addr=(Cell *)(a_addr[0]);
                   1262:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1263:        memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1264:      {
                   1265:        f83name2=f83name1;
                   1266:        break;
                   1267:      }
                   1268: }
                   1269: :
                   1270:  BEGIN  dup  WHILE
                   1271:         2@ >r >r dup r@ cell+ c@ $1F and =
                   1272:         IF  2dup r@ cell+ char+ -text 0=
                   1273:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   1274:        rdrop r>
                   1275:  REPEAT nip nip ;
                   1276: 
1.47      anton    1277: (hashkey)      ( c_addr u1 -- u2 )             gforth  paren_hashkey
1.1       anton    1278: u2=0;
                   1279: while(u1--)
                   1280:    u2+=(Cell)toupper(*c_addr++);
                   1281: :
                   1282:  0 -rot bounds ?DO  I c@ toupper +  LOOP ;
                   1283: 
1.47      anton    1284: (hashkey1)     ( c_addr u ubits -- ukey )              gforth  paren_hashkey1
1.1       anton    1285: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
                   1286: /* this hash function rotates the key at every step by rot bits within
                   1287:    ubits bits and xors it with the character. This function does ok in
                   1288:    the chi-sqare-test.  Rot should be <=7 (preferably <=5) for
                   1289:    ASCII strings (larger if ubits is large), and should share no
                   1290:    divisors with ubits.
                   1291: */
1.106     pazsan   1292: static char rot_values[] = {5,0,1,2,3,4,5,5,5,5,3,5,5,5,5,7,5,5,5,5,7,5,5,5,5,6,5,5,5,5,7,5,5};
                   1293: unsigned rot = rot_values[ubits];
1.1       anton    1294: Char *cp = c_addr;
                   1295: for (ukey=0; cp<c_addr+u; cp++)
                   1296:     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot))) 
                   1297:             ^ toupper(*cp))
                   1298:            & ((1<<ubits)-1));
                   1299: :
                   1300:  dup rot-values + c@ over 1 swap lshift 1- >r
                   1301:  tuck - 2swap r> 0 2swap bounds
                   1302:  ?DO  dup 4 pick lshift swap 3 pick rshift or
                   1303:       I c@ toupper xor
                   1304:       over and  LOOP
                   1305:  nip nip nip ;
                   1306: Create rot-values
                   1307:   5 c, 0 c, 1 c, 2 c, 3 c,  4 c, 5 c, 5 c, 5 c, 5 c,
                   1308:   3 c, 5 c, 5 c, 5 c, 5 c,  7 c, 5 c, 5 c, 5 c, 5 c,
                   1309:   7 c, 5 c, 5 c, 5 c, 5 c,  6 c, 5 c, 5 c, 5 c, 5 c,
                   1310:   7 c, 5 c, 5 c,
                   1311: 
1.15      pazsan   1312: \+
1.1       anton    1313: 
1.47      anton    1314: (parse-white)  ( c_addr1 u1 -- c_addr2 u2 )    gforth  paren_parse_white
1.1       anton    1315: /* use !isgraph instead of isspace? */
                   1316: Char *endp = c_addr1+u1;
                   1317: while (c_addr1<endp && isspace(*c_addr1))
                   1318:   c_addr1++;
                   1319: if (c_addr1<endp) {
                   1320:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
                   1321:     ;
                   1322:   u2 = c_addr1-c_addr2;
                   1323: }
                   1324: else {
                   1325:   c_addr2 = c_addr1;
                   1326:   u2 = 0;
                   1327: }
                   1328: :
                   1329:  BEGIN  dup  WHILE  over c@ bl <=  WHILE  1 /string
                   1330:  REPEAT  THEN  2dup
                   1331:  BEGIN  dup  WHILE  over c@ bl >   WHILE  1 /string
                   1332:  REPEAT  THEN  nip - ;
                   1333: 
1.47      anton    1334: aligned        ( c_addr -- a_addr )    core
1.29      crook    1335: "" @i{a-addr} is the first aligned address greater than or equal to @i{c-addr}.""
1.1       anton    1336: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
                   1337: :
                   1338:  [ cell 1- ] Literal + [ -1 cells ] Literal and ;
                   1339: 
1.47      anton    1340: faligned       ( c_addr -- f_addr )    float   f_aligned
1.29      crook    1341: "" @i{f-addr} is the first float-aligned address greater than or equal to @i{c-addr}.""
1.1       anton    1342: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
                   1343: :
                   1344:  [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
                   1345: 
1.47      anton    1346: >body  ( xt -- a_addr )        core    to_body
1.40      crook    1347: "" Get the address of the body of the word represented by @i{xt} (the address
                   1348: of the word's data field).""
1.1       anton    1349: a_addr = PFA(xt);
                   1350: :
                   1351:     2 cells + ;
                   1352: 
1.35      jwilke   1353: \ threading stuff is currently only interesting if we have a compiler
                   1354: \fhas? standardthreading has? compiler and [IF]
1.28      jwilke   1355: 
1.47      anton    1356: >code-address  ( xt -- c_addr )                gforth  to_code_address
1.29      crook    1357: ""@i{c-addr} is the code address of the word @i{xt}.""
1.1       anton    1358: /* !! This behaves installation-dependently for DOES-words */
                   1359: c_addr = (Address)CODE_ADDRESS(xt);
                   1360: :
                   1361:     @ ;
                   1362: 
1.47      anton    1363: >does-code     ( xt -- a_addr )                gforth  to_does_code
1.58      anton    1364: ""If @i{xt} is the execution token of a child of a @code{DOES>} word,
1.29      crook    1365: @i{a-addr} is the start of the Forth code after the @code{DOES>};
                   1366: Otherwise @i{a-addr} is 0.""
1.1       anton    1367: a_addr = (Cell *)DOES_CODE(xt);
                   1368: :
                   1369:     cell+ @ ;
                   1370: 
1.47      anton    1371: code-address!  ( c_addr xt -- )                gforth  code_address_store
1.29      crook    1372: ""Create a code field with code address @i{c-addr} at @i{xt}.""
1.1       anton    1373: MAKE_CF(xt, c_addr);
                   1374: :
                   1375:     ! ;
                   1376: 
1.47      anton    1377: does-code!     ( a_addr xt -- )                gforth  does_code_store
1.58      anton    1378: ""Create a code field at @i{xt} for a child of a @code{DOES>}-word;
                   1379: @i{a-addr} is the start of the Forth code after @code{DOES>}.""
1.1       anton    1380: MAKE_DOES_CF(xt, a_addr);
                   1381: :
                   1382:     dodoes: over ! cell+ ! ;
                   1383: 
1.47      anton    1384: does-handler!  ( a_addr -- )   gforth  does_handler_store
1.58      anton    1385: ""Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
                   1386: @i{a-addr} points just behind a @code{DOES>}.""
1.1       anton    1387: MAKE_DOES_HANDLER(a_addr);
                   1388: :
                   1389:     drop ;
                   1390: 
1.47      anton    1391: /does-handler  ( -- n )        gforth  slash_does_handler
1.26      crook    1392: ""The size of a @code{DOES>}-handler (includes possible padding).""
1.1       anton    1393: /* !! a constant or environmental query might be better */
                   1394: n = DOES_HANDLER_SIZE;
                   1395: :
                   1396:     2 cells ;
                   1397: 
1.47      anton    1398: threading-method       ( -- n )        gforth  threading_method
1.1       anton    1399: ""0 if the engine is direct threaded. Note that this may change during
                   1400: the lifetime of an image.""
                   1401: #if defined(DOUBLY_INDIRECT)
                   1402: n=2;
                   1403: #else
                   1404: # if defined(DIRECT_THREADED)
                   1405: n=0;
                   1406: # else
                   1407: n=1;
                   1408: # endif
                   1409: #endif
                   1410: :
                   1411:  1 ;
1.28      jwilke   1412: 
1.35      jwilke   1413: \f[THEN]
1.1       anton    1414: 
1.83      pazsan   1415: \g hostos
                   1416: 
1.47      anton    1417: key-file       ( wfileid -- n )                gforth  paren_key_file
1.17      pazsan   1418: #ifdef HAS_FILE
1.1       anton    1419: fflush(stdout);
1.12      pazsan   1420: n = key((FILE*)wfileid);
1.17      pazsan   1421: #else
                   1422: n = key(stdin);
                   1423: #endif
1.1       anton    1424: 
1.47      anton    1425: key?-file      ( wfileid -- n )                facility        key_q_file
1.17      pazsan   1426: #ifdef HAS_FILE
1.1       anton    1427: fflush(stdout);
1.12      pazsan   1428: n = key_query((FILE*)wfileid);
1.17      pazsan   1429: #else
                   1430: n = key_query(stdin);
                   1431: #endif
                   1432: 
                   1433: \+os
1.12      pazsan   1434: 
1.47      anton    1435: stdin  ( -- wfileid )  gforth
1.12      pazsan   1436: wfileid = (Cell)stdin;
1.1       anton    1437: 
1.47      anton    1438: stdout ( -- wfileid )  gforth
1.1       anton    1439: wfileid = (Cell)stdout;
                   1440: 
1.47      anton    1441: stderr ( -- wfileid )  gforth
1.1       anton    1442: wfileid = (Cell)stderr;
                   1443: 
1.47      anton    1444: form   ( -- urows ucols )      gforth
1.1       anton    1445: ""The number of lines and columns in the terminal. These numbers may change
                   1446: with the window size.""
                   1447: /* we could block SIGWINCH here to get a consistent size, but I don't
                   1448:  think this is necessary or always beneficial */
                   1449: urows=rows;
                   1450: ucols=cols;
                   1451: 
1.47      anton    1452: flush-icache   ( c_addr u -- ) gforth  flush_icache
1.1       anton    1453: ""Make sure that the instruction cache of the processor (if there is
1.29      crook    1454: one) does not contain stale data at @i{c-addr} and @i{u} bytes
1.1       anton    1455: afterwards. @code{END-CODE} performs a @code{flush-icache}
                   1456: automatically. Caveat: @code{flush-icache} might not work on your
                   1457: installation; this is usually the case if direct threading is not
                   1458: supported on your machine (take a look at your @file{machine.h}) and
                   1459: your machine has a separate instruction cache. In such cases,
                   1460: @code{flush-icache} does nothing instead of flushing the instruction
                   1461: cache.""
                   1462: FLUSH_ICACHE(c_addr,u);
                   1463: 
1.47      anton    1464: (bye)  ( n -- )        gforth  paren_bye
1.77      anton    1465: SUPER_END;
1.1       anton    1466: return (Label *)n;
                   1467: 
1.47      anton    1468: (system)       ( c_addr u -- wretval wior )    gforth  peren_system
1.20      pazsan   1469: #ifndef MSDOS
1.1       anton    1470: int old_tp=terminal_prepped;
                   1471: deprep_terminal();
1.20      pazsan   1472: #endif
1.1       anton    1473: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
                   1474: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1.20      pazsan   1475: #ifndef MSDOS
1.1       anton    1476: if (old_tp)
                   1477:   prep_terminal();
1.20      pazsan   1478: #endif
1.1       anton    1479: 
1.47      anton    1480: getenv ( c_addr1 u1 -- c_addr2 u2 )    gforth
1.29      crook    1481: ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2}
1.24      crook    1482: is the host operating system's expansion of that environment variable. If the
1.29      crook    1483: environment variable does not exist, @i{c-addr2 u2} specifies a string 0 characters
1.24      crook    1484: in length.""
1.46      pazsan   1485: /* close ' to keep fontify happy */
1.1       anton    1486: c_addr2 = getenv(cstr(c_addr1,u1,1));
                   1487: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
                   1488: 
1.56      anton    1489: open-pipe      ( c_addr u wfam -- wfileid wior )       gforth  open_pipe
1.84      pazsan   1490: wfileid=(Cell)popen(cstr(c_addr,u,1),pfileattr[wfam]); /* ~ expansion of 1st arg? */
1.1       anton    1491: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
                   1492: 
1.47      anton    1493: close-pipe     ( wfileid -- wretval wior )             gforth  close_pipe
1.1       anton    1494: wretval = pclose((FILE *)wfileid);
                   1495: wior = IOR(wretval==-1);
                   1496: 
1.47      anton    1497: time&date      ( -- nsec nmin nhour nday nmonth nyear )        facility-ext    time_and_date
1.44      crook    1498: ""Report the current time of day. Seconds, minutes and hours are numbered from 0.
                   1499: Months are numbered from 1.""
1.1       anton    1500: struct timeval time1;
                   1501: struct timezone zone1;
                   1502: struct tm *ltime;
                   1503: gettimeofday(&time1,&zone1);
1.51      anton    1504: /* !! Single Unix specification: 
                   1505:    If tzp is not a null pointer, the behaviour is unspecified. */
1.1       anton    1506: ltime=localtime((time_t *)&time1.tv_sec);
                   1507: nyear =ltime->tm_year+1900;
                   1508: nmonth=ltime->tm_mon+1;
                   1509: nday  =ltime->tm_mday;
                   1510: nhour =ltime->tm_hour;
                   1511: nmin  =ltime->tm_min;
                   1512: nsec  =ltime->tm_sec;
                   1513: 
1.47      anton    1514: ms     ( n -- )        facility-ext
1.44      crook    1515: ""Wait at least @i{n} milli-second.""
1.1       anton    1516: struct timeval timeout;
                   1517: timeout.tv_sec=n/1000;
                   1518: timeout.tv_usec=1000*(n%1000);
                   1519: (void)select(0,0,0,0,&timeout);
                   1520: 
1.47      anton    1521: allocate       ( u -- a_addr wior )    memory
1.29      crook    1522: ""Allocate @i{u} address units of contiguous data space. The initial
1.27      crook    1523: contents of the data space is undefined. If the allocation is successful,
1.29      crook    1524: @i{a-addr} is the start address of the allocated region and @i{wior}
                   1525: is 0. If the allocation fails, @i{a-addr} is undefined and @i{wior}
1.52      anton    1526: is a non-zero I/O result code.""
1.1       anton    1527: a_addr = (Cell *)malloc(u?u:1);
                   1528: wior = IOR(a_addr==NULL);
                   1529: 
1.47      anton    1530: free   ( a_addr -- wior )              memory
1.29      crook    1531: ""Return the region of data space starting at @i{a-addr} to the system.
1.52      anton    1532: The region must originally have been obtained using @code{allocate} or
1.29      crook    1533: @code{resize}. If the operational is successful, @i{wior} is 0.
1.52      anton    1534: If the operation fails, @i{wior} is a non-zero I/O result code.""
1.1       anton    1535: free(a_addr);
                   1536: wior = 0;
                   1537: 
1.47      anton    1538: resize ( a_addr1 u -- a_addr2 wior )   memory
1.26      crook    1539: ""Change the size of the allocated area at @i{a-addr1} to @i{u}
1.1       anton    1540: address units, possibly moving the contents to a different
1.27      crook    1541: area. @i{a-addr2} is the address of the resulting area.
1.52      anton    1542: If the operation is successful, @i{wior} is 0.
                   1543: If the operation fails, @i{wior} is a non-zero
1.29      crook    1544: I/O result code. If @i{a-addr1} is 0, Gforth's (but not the Standard)
1.27      crook    1545: @code{resize} @code{allocate}s @i{u} address units.""
1.1       anton    1546: /* the following check is not necessary on most OSs, but it is needed
                   1547:    on SunOS 4.1.2. */
1.46      pazsan   1548: /* close ' to keep fontify happy */
1.1       anton    1549: if (a_addr1==NULL)
                   1550:   a_addr2 = (Cell *)malloc(u);
                   1551: else
                   1552:   a_addr2 = (Cell *)realloc(a_addr1, u);
                   1553: wior = IOR(a_addr2==NULL);     /* !! Define a return code */
                   1554: 
1.47      anton    1555: strerror       ( n -- c_addr u )       gforth
1.1       anton    1556: c_addr = strerror(n);
                   1557: u = strlen(c_addr);
                   1558: 
1.47      anton    1559: strsignal      ( n -- c_addr u )       gforth
1.1       anton    1560: c_addr = strsignal(n);
                   1561: u = strlen(c_addr);
                   1562: 
1.47      anton    1563: call-c ( w -- )        gforth  call_c
1.1       anton    1564: ""Call the C function pointed to by @i{w}. The C function has to
                   1565: access the stack itself. The stack pointers are exported in the global
                   1566: variables @code{SP} and @code{FP}.""
                   1567: /* This is a first attempt at support for calls to C. This may change in
                   1568:    the future */
1.64      anton    1569: IF_fpTOS(fp[0]=fpTOS);
1.1       anton    1570: FP=fp;
                   1571: SP=sp;
                   1572: ((void (*)())w)();
                   1573: sp=SP;
                   1574: fp=FP;
1.64      anton    1575: IF_spTOS(spTOS=sp[0]);
                   1576: IF_fpTOS(fpTOS=fp[0]);
1.1       anton    1577: 
1.15      pazsan   1578: \+
                   1579: \+file
1.1       anton    1580: 
1.47      anton    1581: close-file     ( wfileid -- wior )             file    close_file
1.1       anton    1582: wior = IOR(fclose((FILE *)wfileid)==EOF);
                   1583: 
1.56      anton    1584: open-file      ( c_addr u wfam -- wfileid wior )       file    open_file
                   1585: wfileid = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[wfam]);
1.22      crook    1586: wior =  IOR(wfileid == 0);
1.1       anton    1587: 
1.56      anton    1588: create-file    ( c_addr u wfam -- wfileid wior )       file    create_file
1.1       anton    1589: Cell   fd;
1.56      anton    1590: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[wfam], 0666);
1.1       anton    1591: if (fd != -1) {
1.56      anton    1592:   wfileid = (Cell)fdopen(fd, fileattr[wfam]);
1.22      crook    1593:   wior = IOR(wfileid == 0);
1.1       anton    1594: } else {
1.22      crook    1595:   wfileid = 0;
1.1       anton    1596:   wior = IOR(1);
                   1597: }
                   1598: 
1.47      anton    1599: delete-file    ( c_addr u -- wior )            file    delete_file
1.1       anton    1600: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
                   1601: 
1.47      anton    1602: rename-file    ( c_addr1 u1 c_addr2 u2 -- wior )       file-ext        rename_file
1.29      crook    1603: ""Rename file @i{c_addr1 u1} to new name @i{c_addr2 u2}""
1.1       anton    1604: char *s1=tilde_cstr(c_addr2, u2, 1);
                   1605: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
                   1606: 
1.47      anton    1607: file-position  ( wfileid -- ud wior )  file    file_position
1.1       anton    1608: /* !! use tell and lseek? */
1.108     anton    1609: ud = OFF2UD(ftello((FILE *)wfileid));
                   1610: wior = IOR(UD2OFF(ud)==-1);
1.1       anton    1611: 
1.47      anton    1612: reposition-file        ( ud wfileid -- wior )  file    reposition_file
1.108     anton    1613: wior = IOR(fseeko((FILE *)wfileid, UD2OFF(ud), SEEK_SET)==-1);
1.1       anton    1614: 
1.47      anton    1615: file-size      ( wfileid -- ud wior )  file    file_size
1.1       anton    1616: struct stat buf;
                   1617: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1.108     anton    1618: ud = OFF2UD(buf.st_size);
1.1       anton    1619: 
1.47      anton    1620: resize-file    ( ud wfileid -- wior )  file    resize_file
1.108     anton    1621: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2OFF(ud))==-1);
1.1       anton    1622: 
1.47      anton    1623: read-file      ( c_addr u1 wfileid -- u2 wior )        file    read_file
1.1       anton    1624: /* !! fread does not guarantee enough */
                   1625: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
                   1626: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
                   1627: /* !! is the value of ferror errno-compatible? */
                   1628: if (wior)
                   1629:   clearerr((FILE *)wfileid);
                   1630: 
1.60      pazsan   1631: read-line      ( c_addr u1 wfileid -- u2 flag wior )   file    read_line
1.85      anton    1632: /* this may one day be replaced with : read-line (read-line) nip ; */
1.1       anton    1633: Cell c;
                   1634: flag=-1;
                   1635: for(u2=0; u2<u1; u2++)
                   1636: {
1.45      anton    1637:    c = getc((FILE *)wfileid);
                   1638:    if (c=='\n') break;
                   1639:    if (c=='\r') {
                   1640:      if ((c = getc((FILE *)wfileid))!='\n')
                   1641:        ungetc(c,(FILE *)wfileid);
                   1642:      break;
                   1643:    }
                   1644:    if (c==EOF) {
1.1       anton    1645:        flag=FLAG(u2!=0);
                   1646:        break;
                   1647:      }
1.45      anton    1648:    c_addr[u2] = (Char)c;
1.1       anton    1649: }
                   1650: wior=FILEIO(ferror((FILE *)wfileid));
                   1651: 
1.15      pazsan   1652: \+
1.1       anton    1653: 
1.47      anton    1654: write-file     ( c_addr u1 wfileid -- wior )   file    write_file
1.1       anton    1655: /* !! fwrite does not guarantee enough */
1.39      pazsan   1656: #ifdef HAS_FILE
1.1       anton    1657: {
                   1658:   UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
                   1659:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
                   1660:   if (wior)
                   1661:     clearerr((FILE *)wfileid);
                   1662: }
1.39      pazsan   1663: #else
                   1664: TYPE(c_addr, u1);
                   1665: #endif
1.17      pazsan   1666: 
1.47      anton    1667: emit-file      ( c wfileid -- wior )   gforth  emit_file
1.17      pazsan   1668: #ifdef HAS_FILE
1.1       anton    1669: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
                   1670: if (wior)
                   1671:   clearerr((FILE *)wfileid);
1.17      pazsan   1672: #else
1.36      pazsan   1673: PUTC(c);
1.17      pazsan   1674: #endif
1.1       anton    1675: 
1.15      pazsan   1676: \+file
1.1       anton    1677: 
1.47      anton    1678: flush-file     ( wfileid -- wior )             file-ext        flush_file
1.1       anton    1679: wior = IOR(fflush((FILE *) wfileid)==EOF);
                   1680: 
1.56      anton    1681: file-status    ( c_addr u -- wfam wior )       file-ext        file_status
1.1       anton    1682: char *filename=tilde_cstr(c_addr, u, 1);
                   1683: if (access (filename, F_OK) != 0) {
1.56      anton    1684:   wfam=0;
1.1       anton    1685:   wior=IOR(1);
                   1686: }
                   1687: else if (access (filename, R_OK | W_OK) == 0) {
1.56      anton    1688:   wfam=2; /* r/w */
1.1       anton    1689:   wior=0;
                   1690: }
                   1691: else if (access (filename, R_OK) == 0) {
1.56      anton    1692:   wfam=0; /* r/o */
1.1       anton    1693:   wior=0;
                   1694: }
                   1695: else if (access (filename, W_OK) == 0) {
1.56      anton    1696:   wfam=4; /* w/o */
1.1       anton    1697:   wior=0;
                   1698: }
                   1699: else {
1.56      anton    1700:   wfam=1; /* well, we cannot access the file, but better deliver a legal
1.1       anton    1701:            access mode (r/o bin), so we get a decent error later upon open. */
                   1702:   wior=0;
                   1703: }
                   1704: 
1.15      pazsan   1705: \+
                   1706: \+floating
1.1       anton    1707: 
1.83      pazsan   1708: \g floating
                   1709: 
1.1       anton    1710: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
                   1711: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
                   1712: 
1.47      anton    1713: d>f    ( d -- r )              float   d_to_f
1.1       anton    1714: #ifdef BUGGY_LONG_LONG
                   1715: extern double ldexp(double x, int exp);
                   1716: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
                   1717: #else
                   1718: r = d;
                   1719: #endif
                   1720: 
1.47      anton    1721: f>d    ( r -- d )              float   f_to_d
1.100     pazsan   1722: extern DCell double2ll(Float r);
                   1723: d = double2ll(r);
1.1       anton    1724: 
1.47      anton    1725: f!     ( r f_addr -- ) float   f_store
1.52      anton    1726: ""Store @i{r} into the float at address @i{f-addr}.""
1.1       anton    1727: *f_addr = r;
                   1728: 
1.47      anton    1729: f@     ( f_addr -- r ) float   f_fetch
1.52      anton    1730: ""@i{r} is the float at address @i{f-addr}.""
1.1       anton    1731: r = *f_addr;
                   1732: 
1.47      anton    1733: df@    ( df_addr -- r )        float-ext       d_f_fetch
1.52      anton    1734: ""Fetch the double-precision IEEE floating-point value @i{r} from the address @i{df-addr}.""
1.1       anton    1735: #ifdef IEEE_FP
                   1736: r = *df_addr;
                   1737: #else
                   1738: !! df@
                   1739: #endif
                   1740: 
1.47      anton    1741: df!    ( r df_addr -- )        float-ext       d_f_store
1.52      anton    1742: ""Store @i{r} as double-precision IEEE floating-point value to the
                   1743: address @i{df-addr}.""
1.1       anton    1744: #ifdef IEEE_FP
                   1745: *df_addr = r;
                   1746: #else
                   1747: !! df!
                   1748: #endif
                   1749: 
1.47      anton    1750: sf@    ( sf_addr -- r )        float-ext       s_f_fetch
1.52      anton    1751: ""Fetch the single-precision IEEE floating-point value @i{r} from the address @i{sf-addr}.""
1.1       anton    1752: #ifdef IEEE_FP
                   1753: r = *sf_addr;
                   1754: #else
                   1755: !! sf@
                   1756: #endif
                   1757: 
1.47      anton    1758: sf!    ( r sf_addr -- )        float-ext       s_f_store
1.52      anton    1759: ""Store @i{r} as single-precision IEEE floating-point value to the
                   1760: address @i{sf-addr}.""
1.1       anton    1761: #ifdef IEEE_FP
                   1762: *sf_addr = r;
                   1763: #else
                   1764: !! sf!
                   1765: #endif
                   1766: 
1.47      anton    1767: f+     ( r1 r2 -- r3 ) float   f_plus
1.1       anton    1768: r3 = r1+r2;
                   1769: 
1.47      anton    1770: f-     ( r1 r2 -- r3 ) float   f_minus
1.1       anton    1771: r3 = r1-r2;
                   1772: 
1.47      anton    1773: f*     ( r1 r2 -- r3 ) float   f_star
1.1       anton    1774: r3 = r1*r2;
                   1775: 
1.47      anton    1776: f/     ( r1 r2 -- r3 ) float   f_slash
1.1       anton    1777: r3 = r1/r2;
                   1778: 
1.47      anton    1779: f**    ( r1 r2 -- r3 ) float-ext       f_star_star
1.26      crook    1780: ""@i{r3} is @i{r1} raised to the @i{r2}th power.""
1.1       anton    1781: r3 = pow(r1,r2);
                   1782: 
1.47      anton    1783: fnegate        ( r1 -- r2 )    float   f_negate
1.1       anton    1784: r2 = - r1;
                   1785: 
1.47      anton    1786: fdrop  ( r -- )                float   f_drop
1.1       anton    1787: 
1.47      anton    1788: fdup   ( r -- r r )    float   f_dupe
1.1       anton    1789: 
1.47      anton    1790: fswap  ( r1 r2 -- r2 r1 )      float   f_swap
1.1       anton    1791: 
1.47      anton    1792: fover  ( r1 r2 -- r1 r2 r1 )   float   f_over
1.1       anton    1793: 
1.47      anton    1794: frot   ( r1 r2 r3 -- r2 r3 r1 )        float   f_rote
1.1       anton    1795: 
1.47      anton    1796: fnip   ( r1 r2 -- r2 ) gforth  f_nip
1.1       anton    1797: 
1.47      anton    1798: ftuck  ( r1 r2 -- r2 r1 r2 )   gforth  f_tuck
1.1       anton    1799: 
1.47      anton    1800: float+ ( f_addr1 -- f_addr2 )  float   float_plus
1.52      anton    1801: ""@code{1 floats +}.""
1.1       anton    1802: f_addr2 = f_addr1+1;
                   1803: 
1.47      anton    1804: floats ( n1 -- n2 )    float
1.52      anton    1805: ""@i{n2} is the number of address units of @i{n1} floats.""
1.1       anton    1806: n2 = n1*sizeof(Float);
                   1807: 
1.47      anton    1808: floor  ( r1 -- r2 )    float
1.26      crook    1809: ""Round towards the next smaller integral value, i.e., round toward negative infinity.""
1.1       anton    1810: /* !! unclear wording */
                   1811: r2 = floor(r1);
                   1812: 
1.105     anton    1813: fround ( r1 -- r2 )    gforth  f_round
                   1814: ""Round to the nearest integral value.""
1.1       anton    1815: r2 = rint(r1);
                   1816: 
1.47      anton    1817: fmax   ( r1 r2 -- r3 ) float   f_max
1.1       anton    1818: if (r1<r2)
                   1819:   r3 = r2;
                   1820: else
                   1821:   r3 = r1;
                   1822: 
1.47      anton    1823: fmin   ( r1 r2 -- r3 ) float   f_min
1.1       anton    1824: if (r1<r2)
                   1825:   r3 = r1;
                   1826: else
                   1827:   r3 = r2;
                   1828: 
1.47      anton    1829: represent      ( r c_addr u -- n f1 f2 )       float
1.1       anton    1830: char *sig;
                   1831: int flag;
                   1832: int decpt;
                   1833: sig=ecvt(r, u, &decpt, &flag);
                   1834: n=(r==0 ? 1 : decpt);
                   1835: f1=FLAG(flag!=0);
1.21      anton    1836: f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
1.1       anton    1837: memmove(c_addr,sig,u);
                   1838: 
1.47      anton    1839: >float ( c_addr u -- flag )    float   to_float
1.56      anton    1840: ""Actual stack effect: ( c_addr u -- r t | f ).  Attempt to convert the
                   1841: character string @i{c-addr u} to internal floating-point
                   1842: representation. If the string represents a valid floating-point number
                   1843: @i{r} is placed on the floating-point stack and @i{flag} is
                   1844: true. Otherwise, @i{flag} is false. A string of blanks is a special
                   1845: case and represents the floating-point number 0.""
1.1       anton    1846: /* real signature: c_addr u -- r t / f */
                   1847: Float r;
                   1848: char *number=cstr(c_addr, u, 1);
                   1849: char *endconv;
1.42      pazsan   1850: int sign = 0;
                   1851: if(number[0]=='-') {
                   1852:    sign = 1;
                   1853:    number++;
                   1854:    u--;
                   1855: }
1.21      anton    1856: while(isspace((unsigned)(number[--u])) && u>0);
1.1       anton    1857: switch(number[u])
                   1858: {
                   1859:    case 'd':
                   1860:    case 'D':
                   1861:    case 'e':
                   1862:    case 'E':  break;
                   1863:    default :  u++; break;
                   1864: }
                   1865: number[u]='\0';
                   1866: r=strtod(number,&endconv);
                   1867: if((flag=FLAG(!(Cell)*endconv)))
                   1868: {
1.64      anton    1869:    IF_fpTOS(fp[0] = fpTOS);
1.1       anton    1870:    fp += -1;
1.64      anton    1871:    fpTOS = sign ? -r : r;
1.1       anton    1872: }
                   1873: else if(*endconv=='d' || *endconv=='D')
                   1874: {
                   1875:    *endconv='E';
                   1876:    r=strtod(number,&endconv);
                   1877:    if((flag=FLAG(!(Cell)*endconv)))
                   1878:      {
1.64      anton    1879:        IF_fpTOS(fp[0] = fpTOS);
1.1       anton    1880:        fp += -1;
1.64      anton    1881:        fpTOS = sign ? -r : r;
1.1       anton    1882:      }
                   1883: }
                   1884: 
1.47      anton    1885: fabs   ( r1 -- r2 )    float-ext       f_abs
1.1       anton    1886: r2 = fabs(r1);
                   1887: 
1.47      anton    1888: facos  ( r1 -- r2 )    float-ext       f_a_cos
1.1       anton    1889: r2 = acos(r1);
                   1890: 
1.47      anton    1891: fasin  ( r1 -- r2 )    float-ext       f_a_sine
1.1       anton    1892: r2 = asin(r1);
                   1893: 
1.47      anton    1894: fatan  ( r1 -- r2 )    float-ext       f_a_tan
1.1       anton    1895: r2 = atan(r1);
                   1896: 
1.47      anton    1897: fatan2 ( r1 r2 -- r3 ) float-ext       f_a_tan_two
1.26      crook    1898: ""@i{r1/r2}=tan(@i{r3}). ANS Forth does not require, but probably
1.1       anton    1899: intends this to be the inverse of @code{fsincos}. In gforth it is.""
                   1900: r3 = atan2(r1,r2);
                   1901: 
1.47      anton    1902: fcos   ( r1 -- r2 )    float-ext       f_cos
1.1       anton    1903: r2 = cos(r1);
                   1904: 
1.47      anton    1905: fexp   ( r1 -- r2 )    float-ext       f_e_x_p
1.1       anton    1906: r2 = exp(r1);
                   1907: 
1.47      anton    1908: fexpm1 ( r1 -- r2 )    float-ext       f_e_x_p_m_one
1.1       anton    1909: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
                   1910: #ifdef HAVE_EXPM1
1.3       pazsan   1911: extern double
                   1912: #ifdef NeXT
                   1913:               const
                   1914: #endif
                   1915:                     expm1(double);
1.1       anton    1916: r2 = expm1(r1);
                   1917: #else
                   1918: r2 = exp(r1)-1.;
                   1919: #endif
                   1920: 
1.47      anton    1921: fln    ( r1 -- r2 )    float-ext       f_l_n
1.1       anton    1922: r2 = log(r1);
                   1923: 
1.47      anton    1924: flnp1  ( r1 -- r2 )    float-ext       f_l_n_p_one
1.1       anton    1925: ""@i{r2}=ln(@i{r1}+1)""
                   1926: #ifdef HAVE_LOG1P
1.3       pazsan   1927: extern double
                   1928: #ifdef NeXT
                   1929:               const
                   1930: #endif
                   1931:                     log1p(double);
1.1       anton    1932: r2 = log1p(r1);
                   1933: #else
                   1934: r2 = log(r1+1.);
                   1935: #endif
                   1936: 
1.47      anton    1937: flog   ( r1 -- r2 )    float-ext       f_log
1.26      crook    1938: ""The decimal logarithm.""
1.1       anton    1939: r2 = log10(r1);
                   1940: 
1.47      anton    1941: falog  ( r1 -- r2 )    float-ext       f_a_log
1.1       anton    1942: ""@i{r2}=10**@i{r1}""
                   1943: extern double pow10(double);
                   1944: r2 = pow10(r1);
                   1945: 
1.47      anton    1946: fsin   ( r1 -- r2 )    float-ext       f_sine
1.1       anton    1947: r2 = sin(r1);
                   1948: 
1.47      anton    1949: fsincos        ( r1 -- r2 r3 ) float-ext       f_sine_cos
1.1       anton    1950: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
                   1951: r2 = sin(r1);
                   1952: r3 = cos(r1);
                   1953: 
1.47      anton    1954: fsqrt  ( r1 -- r2 )    float-ext       f_square_root
1.1       anton    1955: r2 = sqrt(r1);
                   1956: 
1.47      anton    1957: ftan   ( r1 -- r2 )    float-ext       f_tan
1.1       anton    1958: r2 = tan(r1);
                   1959: :
                   1960:  fsincos f/ ;
                   1961: 
1.47      anton    1962: fsinh  ( r1 -- r2 )    float-ext       f_cinch
1.1       anton    1963: r2 = sinh(r1);
                   1964: :
                   1965:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
                   1966: 
1.47      anton    1967: fcosh  ( r1 -- r2 )    float-ext       f_cosh
1.1       anton    1968: r2 = cosh(r1);
                   1969: :
                   1970:  fexp fdup 1/f f+ f2/ ;
                   1971: 
1.47      anton    1972: ftanh  ( r1 -- r2 )    float-ext       f_tan_h
1.1       anton    1973: r2 = tanh(r1);
                   1974: :
                   1975:  f2* fexpm1 fdup 2. d>f f+ f/ ;
                   1976: 
1.47      anton    1977: fasinh ( r1 -- r2 )    float-ext       f_a_cinch
1.1       anton    1978: r2 = asinh(r1);
                   1979: :
                   1980:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
                   1981: 
1.47      anton    1982: facosh ( r1 -- r2 )    float-ext       f_a_cosh
1.1       anton    1983: r2 = acosh(r1);
                   1984: :
                   1985:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
                   1986: 
1.47      anton    1987: fatanh ( r1 -- r2 )    float-ext       f_a_tan_h
1.1       anton    1988: r2 = atanh(r1);
                   1989: :
                   1990:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
                   1991:  r> IF  fnegate  THEN ;
                   1992: 
1.47      anton    1993: sfloats        ( n1 -- n2 )    float-ext       s_floats
1.52      anton    1994: ""@i{n2} is the number of address units of @i{n1}
1.29      crook    1995: single-precision IEEE floating-point numbers.""
1.1       anton    1996: n2 = n1*sizeof(SFloat);
                   1997: 
1.47      anton    1998: dfloats        ( n1 -- n2 )    float-ext       d_floats
1.52      anton    1999: ""@i{n2} is the number of address units of @i{n1}
1.29      crook    2000: double-precision IEEE floating-point numbers.""
1.1       anton    2001: n2 = n1*sizeof(DFloat);
                   2002: 
1.47      anton    2003: sfaligned      ( c_addr -- sf_addr )   float-ext       s_f_aligned
1.52      anton    2004: ""@i{sf-addr} is the first single-float-aligned address greater
1.29      crook    2005: than or equal to @i{c-addr}.""
1.1       anton    2006: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
                   2007: :
                   2008:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
                   2009: 
1.47      anton    2010: dfaligned      ( c_addr -- df_addr )   float-ext       d_f_aligned
1.52      anton    2011: ""@i{df-addr} is the first double-float-aligned address greater
1.29      crook    2012: than or equal to @i{c-addr}.""
1.1       anton    2013: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
                   2014: :
                   2015:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
                   2016: 
                   2017: \ The following words access machine/OS/installation-dependent
                   2018: \   Gforth internals
                   2019: \ !! how about environmental queries DIRECT-THREADED,
                   2020: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
                   2021: 
                   2022: \ local variable implementation primitives
1.15      pazsan   2023: \+
                   2024: \+glocals
1.1       anton    2025: 
1.68      anton    2026: @local#        ( #noffset -- w )       gforth  fetch_local_number
                   2027: w = *(Cell *)(lp+noffset);
1.1       anton    2028: 
1.47      anton    2029: @local0        ( -- w )        new     fetch_local_zero
1.1       anton    2030: w = *(Cell *)(lp+0*sizeof(Cell));
                   2031: 
1.47      anton    2032: @local1        ( -- w )        new     fetch_local_four
1.1       anton    2033: w = *(Cell *)(lp+1*sizeof(Cell));
                   2034: 
1.47      anton    2035: @local2        ( -- w )        new     fetch_local_eight
1.1       anton    2036: w = *(Cell *)(lp+2*sizeof(Cell));
                   2037: 
1.47      anton    2038: @local3        ( -- w )        new     fetch_local_twelve
1.1       anton    2039: w = *(Cell *)(lp+3*sizeof(Cell));
                   2040: 
1.15      pazsan   2041: \+floating
1.1       anton    2042: 
1.68      anton    2043: f@local#       ( #noffset -- r )       gforth  f_fetch_local_number
                   2044: r = *(Float *)(lp+noffset);
1.1       anton    2045: 
1.47      anton    2046: f@local0       ( -- r )        new     f_fetch_local_zero
1.1       anton    2047: r = *(Float *)(lp+0*sizeof(Float));
                   2048: 
1.47      anton    2049: f@local1       ( -- r )        new     f_fetch_local_eight
1.1       anton    2050: r = *(Float *)(lp+1*sizeof(Float));
                   2051: 
1.15      pazsan   2052: \+
1.1       anton    2053: 
1.68      anton    2054: laddr# ( #noffset -- c_addr )  gforth  laddr_number
1.1       anton    2055: /* this can also be used to implement lp@ */
1.68      anton    2056: c_addr = (Char *)(lp+noffset);
1.1       anton    2057: 
1.68      anton    2058: lp+!#  ( #noffset -- ) gforth  lp_plus_store_number
1.1       anton    2059: ""used with negative immediate values it allocates memory on the
                   2060: local stack, a positive immediate argument drops memory from the local
                   2061: stack""
1.68      anton    2062: lp += noffset;
1.1       anton    2063: 
1.47      anton    2064: lp-    ( -- )  new     minus_four_lp_plus_store
1.1       anton    2065: lp += -sizeof(Cell);
                   2066: 
1.47      anton    2067: lp+    ( -- )  new     eight_lp_plus_store
1.1       anton    2068: lp += sizeof(Float);
                   2069: 
1.47      anton    2070: lp+2   ( -- )  new     sixteen_lp_plus_store
1.1       anton    2071: lp += 2*sizeof(Float);
                   2072: 
1.47      anton    2073: lp!    ( c_addr -- )   gforth  lp_store
1.1       anton    2074: lp = (Address)c_addr;
                   2075: 
1.47      anton    2076: >l     ( w -- )        gforth  to_l
1.1       anton    2077: lp -= sizeof(Cell);
                   2078: *(Cell *)lp = w;
                   2079: 
1.15      pazsan   2080: \+floating
1.1       anton    2081: 
1.47      anton    2082: f>l    ( r -- )        gforth  f_to_l
1.1       anton    2083: lp -= sizeof(Float);
                   2084: *(Float *)lp = r;
                   2085: 
1.47      anton    2086: fpick  ( u -- r )              gforth
1.52      anton    2087: ""Actually the stack effect is @code{ r0 ... ru u -- r0 ... ru r0 }.""
1.11      anton    2088: r = fp[u+1]; /* +1, because update of fp happens before this fragment */
                   2089: :
                   2090:  floats fp@ + f@ ;
                   2091: 
1.15      pazsan   2092: \+
                   2093: \+
1.1       anton    2094: 
1.15      pazsan   2095: \+OS
1.1       anton    2096: 
                   2097: define(`uploop',
                   2098:        `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
                   2099: define(`_uploop',
                   2100:        `ifelse($1, `$3', `$5',
                   2101:               `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
                   2102: \ argflist(argnum): Forth argument list
                   2103: define(argflist,
                   2104:        `ifelse($1, 0, `',
                   2105:                `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
                   2106: \ argdlist(argnum): declare C's arguments
                   2107: define(argdlist,
                   2108:        `ifelse($1, 0, `',
                   2109:                `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
                   2110: \ argclist(argnum): pass C's arguments
                   2111: define(argclist,
                   2112:        `ifelse($1, 0, `',
                   2113:                `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
                   2114: \ icall(argnum)
                   2115: define(icall,
1.47      anton    2116: `icall$1       ( argflist($1)u -- uret )       gforth
1.9       pazsan   2117: uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
1.1       anton    2118: 
                   2119: ')
                   2120: define(fcall,
1.47      anton    2121: `fcall$1       ( argflist($1)u -- rret )       gforth
1.9       pazsan   2122: rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
1.1       anton    2123: 
                   2124: ')
                   2125: 
1.46      pazsan   2126: \ close ' to keep fontify happy
1.1       anton    2127: 
1.47      anton    2128: open-lib       ( c_addr1 u1 -- u2 )    gforth  open_lib
1.1       anton    2129: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
1.8       anton    2130: #ifndef RTLD_GLOBAL
                   2131: #define RTLD_GLOBAL 0
                   2132: #endif
1.7       pazsan   2133: u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_GLOBAL | RTLD_LAZY);
1.1       anton    2134: #else
1.18      pazsan   2135: #  ifdef _WIN32
1.1       anton    2136: u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
                   2137: #  else
                   2138: #warning Define open-lib!
                   2139: u2 = 0;
                   2140: #  endif
                   2141: #endif
                   2142: 
1.47      anton    2143: lib-sym        ( c_addr1 u1 u2 -- u3 ) gforth  lib_sym
1.1       anton    2144: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
                   2145: u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
                   2146: #else
1.18      pazsan   2147: #  ifdef _WIN32
1.1       anton    2148: u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
                   2149: #  else
                   2150: #warning Define lib-sym!
                   2151: u3 = 0;
                   2152: #  endif
                   2153: #endif
                   2154: 
                   2155: uploop(i, 0, 7, `icall(i)')
                   2156: icall(20)
                   2157: uploop(i, 0, 7, `fcall(i)')
                   2158: fcall(20)
                   2159: 
1.15      pazsan   2160: \+
1.1       anton    2161: 
1.47      anton    2162: up!    ( a_addr -- )   gforth  up_store
1.1       anton    2163: UP=up=(char *)a_addr;
                   2164: :
                   2165:  up ! ;
                   2166: Variable UP
1.34      jwilke   2167: 
1.47      anton    2168: wcall  ( u -- )        gforth
1.64      anton    2169: IF_fpTOS(fp[0]=fpTOS);
1.34      jwilke   2170: FP=fp;
1.95      pazsan   2171: sp=(Cell*)(SYSCALL(Cell*(*)(Cell *, void *))u)(sp, &FP);
1.34      jwilke   2172: fp=FP;
1.64      anton    2173: IF_spTOS(spTOS=sp[0];)
                   2174: IF_fpTOS(fpTOS=fp[0]);
1.46      pazsan   2175: 
                   2176: \+file
                   2177: 
1.47      anton    2178: open-dir       ( c_addr u -- wdirid wior )     gforth  open_dir
1.94      pazsan   2179: ""Open the directory specified by @i{c-addr, u}
                   2180: and return @i{dir-id} for futher access to it.""
1.46      pazsan   2181: wdirid = (Cell)opendir(tilde_cstr(c_addr, u, 1));
                   2182: wior =  IOR(wdirid == 0);
                   2183: 
1.47      anton    2184: read-dir       ( c_addr u1 wdirid -- u2 flag wior )    gforth  read_dir
1.94      pazsan   2185: ""Attempt to read the next entry from the directory specified
                   2186: by @i{dir-id} to the buffer of length @i{u1} at address @i{c-addr}. 
                   2187: If the attempt fails because there is no more entries,
                   2188: @i{ior}=0, @i{flag}=0, @i{u2}=0, and the buffer is unmodified.
                   2189: If the attempt to read the next entry fails because of any other reason, 
                   2190: return @i{ior}<>0.
                   2191: If the attempt succeeds, store file name to the buffer at @i{c-addr}
                   2192: and return @i{ior}=0, @i{flag}=true and @i{u2} equal to the size of the file name.
                   2193: If the length of the file name is greater than @i{u1}, 
                   2194: store first @i{u1} characters from file name into the buffer and
                   2195: indicate "name too long" with @i{ior}, @i{flag}=true, and @i{u2}=@i{u1}.""
1.46      pazsan   2196: struct dirent * dent;
                   2197: dent = readdir((DIR *)wdirid);
                   2198: wior = 0;
                   2199: flag = -1;
                   2200: if(dent == NULL) {
                   2201:   u2 = 0;
                   2202:   flag = 0;
                   2203: } else {
                   2204:   u2 = strlen(dent->d_name);
1.70      pazsan   2205:   if(u2 > u1) {
1.46      pazsan   2206:     u2 = u1;
1.70      pazsan   2207:     wior = -512-ENAMETOOLONG;
                   2208:   }
1.46      pazsan   2209:   memmove(c_addr, dent->d_name, u2);
                   2210: }
                   2211: 
1.47      anton    2212: close-dir      ( wdirid -- wior )      gforth  close_dir
1.94      pazsan   2213: ""Close the directory specified by @i{dir-id}.""
1.46      pazsan   2214: wior = IOR(closedir((DIR *)wdirid));
                   2215: 
1.47      anton    2216: filename-match ( c_addr1 u1 c_addr2 u2 -- flag )       gforth  match_file
1.46      pazsan   2217: char * string = cstr(c_addr1, u1, 1);
                   2218: char * pattern = cstr(c_addr2, u2, 0);
                   2219: flag = FLAG(!fnmatch(pattern, string, 0));
                   2220: 
                   2221: \+
1.34      jwilke   2222: 
1.47      anton    2223: newline        ( -- c_addr u ) gforth
1.45      anton    2224: ""String containing the newline sequence of the host OS""
                   2225: char newline[] = {
1.69      anton    2226: #if defined(unix) || defined(__MACH__)
                   2227: /* Darwin/MacOS X sets __MACH__, but not unix. */
1.45      anton    2228: '\n'
                   2229: #else
                   2230: '\r','\n'
                   2231: #endif
                   2232: };
                   2233: c_addr=newline;
                   2234: u=sizeof(newline);
1.49      pazsan   2235: :
                   2236:  "newline count ;
1.54      pazsan   2237: Create "newline e? crlf [IF] 2 c, $0D c, [ELSE] 1 c, [THEN] $0A c,
                   2238: 
                   2239: \+os
1.51      anton    2240: 
                   2241: utime  ( -- dtime )    gforth
                   2242: ""Report the current time in microseconds since some epoch.""
                   2243: struct timeval time1;
                   2244: gettimeofday(&time1,NULL);
                   2245: dtime = timeval2us(&time1);
                   2246: 
                   2247: cputime ( -- duser dsystem ) gforth
                   2248: ""duser and dsystem are the respective user- and system-level CPU
                   2249: times used since the start of the Forth system (excluding child
                   2250: processes), in microseconds (the granularity may be much larger,
                   2251: however).  On platforms without the getrusage call, it reports elapsed
                   2252: time (since some epoch) for duser and 0 for dsystem.""
                   2253: #ifdef HAVE_GETRUSAGE
                   2254: struct rusage usage;
                   2255: getrusage(RUSAGE_SELF, &usage);
                   2256: duser = timeval2us(&usage.ru_utime);
                   2257: dsystem = timeval2us(&usage.ru_stime);
                   2258: #else
                   2259: struct timeval time1;
                   2260: gettimeofday(&time1,NULL);
                   2261: duser = timeval2us(&time1);
1.57      anton    2262: #ifndef BUGGY_LONG_LONG
1.51      anton    2263: dsystem = (DCell)0;
1.57      anton    2264: #else
                   2265: dsystem=(DCell){0,0};
                   2266: #endif
1.51      anton    2267: #endif
                   2268: 
1.54      pazsan   2269: \+
                   2270: 
                   2271: \+floating
                   2272: 
1.51      anton    2273: v*     ( f_addr1 nstride1 f_addr2 nstride2 ucount -- r ) gforth v_star
                   2274: ""dot-product: r=v1*v2.  The first element of v1 is at f_addr1, the
                   2275: next at f_addr1+nstride1 and so on (similar for v2). Both vectors have
                   2276: ucount elements.""
                   2277: for (r=0.; ucount>0; ucount--) {
                   2278:   r += *f_addr1 * *f_addr2;
                   2279:   f_addr1 = (Float *)(((Address)f_addr1)+nstride1);
                   2280:   f_addr2 = (Float *)(((Address)f_addr2)+nstride2);
                   2281: }
1.54      pazsan   2282: :
                   2283:  >r swap 2swap swap 0e r> 0 ?DO
                   2284:      dup f@ over + 2swap dup f@ f* f+ over + 2swap
                   2285:  LOOP 2drop 2drop ; 
1.51      anton    2286: 
                   2287: faxpy  ( ra f_x nstridex f_y nstridey ucount -- )      gforth
                   2288: ""vy=ra*vx+vy""
                   2289: for (; ucount>0; ucount--) {
                   2290:   *f_y += ra * *f_x;
                   2291:   f_x = (Float *)(((Address)f_x)+nstridex);
                   2292:   f_y = (Float *)(((Address)f_y)+nstridey);
                   2293: }
1.54      pazsan   2294: :
                   2295:  >r swap 2swap swap r> 0 ?DO
                   2296:      fdup dup f@ f* over + 2swap dup f@ f+ dup f! over + 2swap
                   2297:  LOOP 2drop 2drop fdrop ;
1.60      pazsan   2298: 
                   2299: \+
                   2300: 
                   2301: \+file
                   2302: 
                   2303: (read-line)    ( c_addr u1 wfileid -- u2 flag u3 wior )        file    paren_read_line
                   2304: Cell c;
                   2305: flag=-1;
                   2306: u3=0;
                   2307: for(u2=0; u2<u1; u2++)
                   2308: {
                   2309:    c = getc((FILE *)wfileid);
                   2310:    u3++;
                   2311:    if (c=='\n') break;
                   2312:    if (c=='\r') {
                   2313:      if ((c = getc((FILE *)wfileid))!='\n')
                   2314:        ungetc(c,(FILE *)wfileid);
                   2315:      else
                   2316:        u3++;
                   2317:      break;
                   2318:    }
                   2319:    if (c==EOF) {
                   2320:        flag=FLAG(u2!=0);
                   2321:        break;
                   2322:      }
                   2323:    c_addr[u2] = (Char)c;
                   2324: }
                   2325: wior=FILEIO(ferror((FILE *)wfileid));
1.71      anton    2326: 
                   2327: \+
                   2328: 
                   2329: (listlfind)    ( c_addr u longname1 -- longname2 )     new     paren_listlfind
                   2330: for (; longname1 != NULL; longname1 = (struct Longname *)(longname1->next))
                   2331:   if ((UCell)LONGNAME_COUNT(longname1)==u &&
                   2332:       memcasecmp(c_addr, longname1->name, u)== 0 /* or inline? */)
                   2333:     break;
                   2334: longname2=longname1;
                   2335: :
1.72      pazsan   2336:     BEGIN  dup WHILE  (findl-samelen)  dup  WHILE
                   2337:        >r 2dup r@ cell+ cell+ capscomp  0=
1.71      anton    2338:        IF  2drop r>  EXIT  THEN
                   2339:        r> @
                   2340:     REPEAT  THEN  nip nip ;
1.72      pazsan   2341: : (findl-samelen) ( u longname1 -- u longname2/0 )
                   2342:     BEGIN  2dup cell+ @ lcount-mask and <> WHILE  @  dup 0= UNTIL  THEN ;
1.71      anton    2343: 
                   2344: \+hash
                   2345: 
                   2346: (hashlfind)    ( c_addr u a_addr -- longname2 )        new     paren_hashlfind
                   2347: struct Longname *longname1;
                   2348: longname2=NULL;
                   2349: while(a_addr != NULL)
                   2350: {
                   2351:    longname1=(struct Longname *)(a_addr[1]);
                   2352:    a_addr=(Cell *)(a_addr[0]);
                   2353:    if ((UCell)LONGNAME_COUNT(longname1)==u &&
                   2354:        memcasecmp(c_addr, longname1->name, u)== 0 /* or inline? */)
                   2355:      {
                   2356:        longname2=longname1;
                   2357:        break;
                   2358:      }
                   2359: }
                   2360: :
                   2361:  BEGIN  dup  WHILE
1.72      pazsan   2362:         2@ >r >r dup r@ cell+ @ lcount-mask and =
                   2363:         IF  2dup r@ cell+ cell+ capscomp 0=
1.71      anton    2364:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   2365:        rdrop r>
                   2366:  REPEAT nip nip ;
                   2367: 
                   2368: (tablelfind)   ( c_addr u a_addr -- longname2 )        new     paren_tablelfind
                   2369: ""A case-sensitive variant of @code{(hashfind)}""
                   2370: struct Longname *longname1;
                   2371: longname2=NULL;
                   2372: while(a_addr != NULL)
                   2373: {
                   2374:    longname1=(struct Longname *)(a_addr[1]);
                   2375:    a_addr=(Cell *)(a_addr[0]);
                   2376:    if ((UCell)LONGNAME_COUNT(longname1)==u &&
                   2377:        memcmp(c_addr, longname1->name, u)== 0 /* or inline? */)
                   2378:      {
                   2379:        longname2=longname1;
                   2380:        break;
                   2381:      }
                   2382: }
                   2383: :
                   2384:  BEGIN  dup  WHILE
1.72      pazsan   2385:         2@ >r >r dup r@ cell+ @ lcount-mask and =
                   2386:         IF  2dup r@ cell+ cell+ -text 0=
1.71      anton    2387:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   2388:        rdrop r>
                   2389:  REPEAT nip nip ;
1.54      pazsan   2390: 
                   2391: \+
1.72      pazsan   2392: 
1.80      pazsan   2393: \+peephole
1.83      pazsan   2394: 
                   2395: \g peephole
1.80      pazsan   2396: 
1.74      anton    2397: primtable      ( -- wprimtable )       new
                   2398: ""wprimtable is a table containing the xts of the primitives indexed
                   2399: by sequence-number in prim (for use in prepare-peephole-table).""
1.75      anton    2400: wprimtable = (Cell)primtable(symbols+DOESJUMP+1,MAX_SYMBOLS-DOESJUMP-1);
1.74      anton    2401: 
                   2402: prepare-peephole-table ( wprimtable -- wpeeptable ) new prepare_peephole_opt
                   2403: ""wpeeptable is a data structure used by @code{peephole-opt}; it is
                   2404: constructed by combining a primitives table with a simple peephole
                   2405: optimization table.""
                   2406: wpeeptable = prepare_peephole_table((Xt *)wprimtable);
                   2407: 
                   2408: peephole-opt   ( xt1 xt2 wpeeptable -- xt )    new     peephole_opt
                   2409: ""xt is the combination of xt1 and xt2 (according to wpeeptable); if
                   2410: they cannot be combined, xt is 0.""
                   2411: xt = peephole_opt(xt1, xt2, wpeeptable);
                   2412: 
1.86      anton    2413: call   ( #a_callee -- R:a_retaddr )    new
1.75      anton    2414: ""Call callee (a variant of docol with inline argument).""
1.102     anton    2415: #ifdef NO_IP
                   2416: INST_TAIL;
                   2417: JUMP(a_callee);
                   2418: #else
1.88      pazsan   2419: #ifdef DEBUG
                   2420:     {
                   2421:       CFA_TO_NAME((((Cell *)a_callee)-2));
                   2422:       fprintf(stderr,"%08lx: call %08lx %.*s\n",(Cell)ip,(Cell)a_callee,
                   2423:              len,name);
                   2424:     }
                   2425: #endif
1.75      anton    2426: a_retaddr = (Cell *)IP;
                   2427: SET_IP((Xt *)a_callee);
1.102     anton    2428: #endif
1.75      anton    2429: 
1.86      anton    2430: useraddr       ( #u -- a_addr )        new
1.75      anton    2431: a_addr = (Cell *)(up+u);
1.86      anton    2432: 
1.107     anton    2433: compile-prim ( xt1 -- xt2 )    obsolete        compile_prim
1.86      anton    2434: xt2 = (Xt)compile_prim((Label)xt1);
1.82      anton    2435: 
1.98      anton    2436: \ lit@ / lit_fetch = lit @
                   2437: 
                   2438: lit@           ( #a_addr -- w ) new    lit_fetch
                   2439: w = *a_addr;
1.89      pazsan   2440: 
                   2441: lit-perform    ( #a_addr -- )  new     lit_perform
1.102     anton    2442: #ifndef NO_IP
1.89      pazsan   2443: ip=IP;
1.102     anton    2444: #endif
1.89      pazsan   2445: SUPER_END;
                   2446: EXEC(*(Xt *)a_addr);
                   2447: 
1.98      anton    2448: \ lit+ / lit_plus = lit +
                   2449: 
                   2450: lit+   ( n1 #n2 -- n )         new     lit_plus
                   2451: n=n1+n2;
1.89      pazsan   2452: 
                   2453: does-exec ( #a_cfa -- R:nest a_pfa )   new     does_exec
1.102     anton    2454: #ifdef NO_IP
                   2455: /* compiled to LIT CALL by compile_prim */
                   2456: assert(0);
                   2457: #else
1.89      pazsan   2458: a_pfa = PFA(a_cfa);
1.103     anton    2459: nest = (Cell)IP;
1.89      pazsan   2460: IF_spTOS(spTOS = sp[0]);
1.90      pazsan   2461: #ifdef DEBUG
                   2462:     {
                   2463:       CFA_TO_NAME(a_cfa);
                   2464:       fprintf(stderr,"%08lx: does %08lx %.*s\n",
                   2465:              (Cell)ip,(Cell)a_cfa,len,name);
                   2466:     }
                   2467: #endif
1.89      pazsan   2468: SET_IP(DOES_CODE1(a_cfa));
1.102     anton    2469: #endif
1.89      pazsan   2470: 
1.99      anton    2471: abranch-lp+!# ( #a_target #nlocals -- )        gforth  abranch_lp_plus_store_number
                   2472: /* this will probably not be used */
                   2473: lp += nlocals;
1.102     anton    2474: #ifdef NO_IP
                   2475: INST_TAIL;
                   2476: JUMP(a_target);
                   2477: #else
1.99      anton    2478: SET_IP((Xt *)a_target);
1.102     anton    2479: #endif
1.99      anton    2480: 
                   2481: \+
                   2482: 
                   2483: abranch        ( #a_target -- )        gforth
1.102     anton    2484: #ifdef NO_IP
                   2485: INST_TAIL;
                   2486: JUMP(a_target);
                   2487: #else
1.99      anton    2488: SET_IP((Xt *)a_target);
1.102     anton    2489: #endif
1.99      anton    2490: :
                   2491:  r> @ >r ;
                   2492: 
1.102     anton    2493: \ acondbranch(forthname,stackeffect,restline,code1,code2,forthcode)
1.99      anton    2494: \ this is non-syntactical: code must open a brace that is closed by the macro
                   2495: define(acondbranch,
                   2496: $1 ( `#'a_target $2 ) $3
1.102     anton    2497: $4     #ifdef NO_IP
1.99      anton    2498: INST_TAIL;
1.102     anton    2499: #endif
                   2500: $5     #ifdef NO_IP
                   2501: JUMP(a_target);
                   2502: #else
                   2503: SET_IP((Xt *)a_target);
                   2504: INST_TAIL; NEXT_P2;
                   2505: #endif
1.99      anton    2506: }
                   2507: SUPER_CONTINUE;
1.102     anton    2508: $6
1.99      anton    2509: 
                   2510: \+glocals
                   2511: 
                   2512: $1-lp+!`#' ( `#'a_target `#'nlocals $2 ) $3_lp_plus_store_number
1.102     anton    2513: $4     #ifdef NO_IP
                   2514: INST_TAIL;
                   2515: #endif
                   2516: $5     lp += nlocals;
                   2517: #ifdef NO_IP
                   2518: JUMP(a_target);
                   2519: #else
1.99      anton    2520: SET_IP((Xt *)a_target);
1.102     anton    2521: INST_TAIL; NEXT_P2;
                   2522: #endif
1.99      anton    2523: }
                   2524: SUPER_CONTINUE;
                   2525: 
                   2526: \+
                   2527: )
                   2528: 
                   2529: acondbranch(a?branch,f --,f83  aquestion_branch,
1.102     anton    2530: ,if (f==0) {
1.99      anton    2531: ,:
                   2532:  0= dup     \ !f !f \ !! still uses relative addresses
                   2533:  r> dup @   \ !f !f IP branchoffset
                   2534:  rot and +  \ !f IP|IP+branchoffset
                   2535:  swap 0= cell and + \ IP''
                   2536:  >r ;)
                   2537: 
                   2538: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
                   2539: \ is only used in if's (yet)
                   2540: 
                   2541: \+xconds
                   2542: 
                   2543: a?dup-?branch  ( #a_target f -- f )    new     aquestion_dupe_question_branch
                   2544: ""The run-time procedure compiled by @code{?DUP-IF}.""
                   2545: if (f==0) {
                   2546:   sp++;
                   2547:   IF_spTOS(spTOS = sp[0]);
1.102     anton    2548: #ifdef NO_IP
                   2549: INST_TAIL;
                   2550: JUMP(a_target);
                   2551: #else
                   2552: SET_IP((Xt *)a_target);
                   2553:   INST_TAIL; NEXT_P2;
                   2554: #endif
1.99      anton    2555: }
                   2556: SUPER_CONTINUE;
                   2557: 
                   2558: a?dup-0=-?branch ( #a_target f -- ) new        aquestion_dupe_zero_equals_question_branch
                   2559: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
                   2560: /* the approach taken here of declaring the word as having the stack
                   2561: effect ( f -- ) and correcting for it in the branch-taken case costs a
                   2562: few cycles in that case, but is easy to convert to a CONDBRANCH
                   2563: invocation */
                   2564: if (f!=0) {
                   2565:   sp--;
1.102     anton    2566: #ifdef NO_IP
                   2567:   JUMP(a_target);
                   2568: #else
1.99      anton    2569:   SET_IP((Xt *)a_target);
                   2570:   NEXT;
1.102     anton    2571: #endif
1.99      anton    2572: }
                   2573: SUPER_CONTINUE;
                   2574: 
                   2575: \+
                   2576: \f[THEN]
                   2577: \fhas? skiploopprims 0= [IF]
                   2578: 
                   2579: acondbranch(a(next),R:n1 -- R:n2,cmFORTH       aparen_next,
                   2580: n2=n1-1;
1.102     anton    2581: ,if (n1) {
1.99      anton    2582: ,:
                   2583:  r> r> dup 1- >r
                   2584:  IF @ >r ELSE cell+ >r THEN ;)
                   2585: 
                   2586: acondbranch(a(loop),R:nlimit R:n1 -- R:nlimit R:n2,gforth      aparen_loop,
                   2587: n2=n1+1;
1.102     anton    2588: ,if (n2 != nlimit) {
1.99      anton    2589: ,:
                   2590:  r> r> 1+ r> 2dup =
                   2591:  IF >r 1- >r cell+ >r
                   2592:  ELSE >r >r @ >r THEN ;)
                   2593: 
                   2594: acondbranch(a(+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth aparen_plus_loop,
                   2595: /* !! check this thoroughly */
                   2596: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
                   2597: /* dependent upon two's complement arithmetic */
                   2598: Cell olddiff = n1-nlimit;
                   2599: n2=n1+n;       
1.102     anton    2600: ,if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
1.99      anton    2601:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
                   2602: ,:
                   2603:  r> swap
                   2604:  r> r> 2dup - >r
                   2605:  2 pick r@ + r@ xor 0< 0=
                   2606:  3 pick r> xor 0< 0= or
                   2607:  IF    >r + >r @ >r
                   2608:  ELSE  >r >r drop cell+ >r THEN ;)
                   2609: 
                   2610: \+xconds
                   2611: 
                   2612: acondbranch(a(-loop),u R:nlimit R:n1 -- R:nlimit R:n2,gforth aparen_minus_loop,
                   2613: UCell olddiff = n1-nlimit;
                   2614: n2=n1-u;
1.102     anton    2615: ,if (olddiff>u) {
1.99      anton    2616: ,)
                   2617: 
                   2618: acondbranch(a(s+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth  aparen_symmetric_plus_loop,
                   2619: ""The run-time procedure compiled by S+LOOP. It loops until the index
                   2620: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
                   2621: version of (+LOOP).""
                   2622: /* !! check this thoroughly */
                   2623: Cell diff = n1-nlimit;
                   2624: Cell newdiff = diff+n;
                   2625: if (n<0) {
                   2626:     diff = -diff;
                   2627:     newdiff = -newdiff;
                   2628: }
                   2629: n2=n1+n;
1.102     anton    2630: ,if (diff>=0 || newdiff<0) {
1.99      anton    2631: ,)
                   2632: 
                   2633: a(?do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth aparen_question_do
1.102     anton    2634: #ifdef NO_IP
                   2635:     INST_TAIL;
                   2636: #endif
1.99      anton    2637: if (nstart == nlimit) {
1.102     anton    2638: #ifdef NO_IP
                   2639:     JUMP(a_target);
                   2640: #else
1.99      anton    2641:     SET_IP((Xt *)a_target);
1.102     anton    2642:     INST_TAIL; NEXT_P2;
                   2643: #endif
1.99      anton    2644: }
                   2645: SUPER_CONTINUE;
                   2646: :
                   2647:   2dup =
                   2648:   IF   r> swap rot >r >r
                   2649:        @ >r
                   2650:   ELSE r> swap rot >r >r
                   2651:        cell+ >r
                   2652:   THEN ;                               \ --> CORE-EXT
                   2653: 
                   2654: \+xconds
                   2655: 
                   2656: a(+do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth aparen_plus_do
1.102     anton    2657: #ifdef NO_IP
                   2658:     INST_TAIL;
                   2659: #endif
1.99      anton    2660: if (nstart >= nlimit) {
1.102     anton    2661: #ifdef NO_IP
                   2662:     JUMP(a_target);
                   2663: #else
1.99      anton    2664:     SET_IP((Xt *)a_target);
1.102     anton    2665:     INST_TAIL; NEXT_P2;
                   2666: #endif
1.99      anton    2667: }
                   2668: SUPER_CONTINUE;
                   2669: :
                   2670:  swap 2dup
                   2671:  r> swap >r swap >r
                   2672:  >=
                   2673:  IF
                   2674:      @
                   2675:  ELSE
                   2676:      cell+
                   2677:  THEN  >r ;
                   2678: 
                   2679: a(u+do)        ( #a_target ulimit ustart -- R:ulimit R:ustart ) gforth aparen_u_plus_do
1.102     anton    2680: #ifdef NO_IP
                   2681:     INST_TAIL;
                   2682: #endif
1.99      anton    2683: if (ustart >= ulimit) {
1.102     anton    2684: #ifdef NO_IP
                   2685: JUMP(a_target);
                   2686: #else
                   2687: SET_IP((Xt *)a_target);
                   2688: INST_TAIL; NEXT_P2;
                   2689: #endif
1.99      anton    2690: }
                   2691: SUPER_CONTINUE;
                   2692: :
                   2693:  swap 2dup
                   2694:  r> swap >r swap >r
                   2695:  u>=
                   2696:  IF
                   2697:      @
                   2698:  ELSE
                   2699:      cell+
                   2700:  THEN  >r ;
                   2701: 
                   2702: a(-do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth aparen_minus_do
1.102     anton    2703: #ifdef NO_IP
                   2704:     INST_TAIL;
                   2705: #endif
1.99      anton    2706: if (nstart <= nlimit) {
1.102     anton    2707: #ifdef NO_IP
                   2708: JUMP(a_target);
                   2709: #else
                   2710: SET_IP((Xt *)a_target);
                   2711: INST_TAIL; NEXT_P2;
                   2712: #endif
1.99      anton    2713: }
                   2714: SUPER_CONTINUE;
                   2715: :
                   2716:  swap 2dup
                   2717:  r> swap >r swap >r
                   2718:  <=
                   2719:  IF
                   2720:      @
                   2721:  ELSE
                   2722:      cell+
                   2723:  THEN  >r ;
                   2724: 
                   2725: a(u-do)        ( #a_target ulimit ustart -- R:ulimit R:ustart ) gforth aparen_u_minus_do
1.102     anton    2726: #ifdef NO_IP
                   2727:     INST_TAIL;
                   2728: #endif
1.99      anton    2729: if (ustart <= ulimit) {
1.102     anton    2730: #ifdef NO_IP
                   2731: JUMP(a_target);
                   2732: #else
                   2733: SET_IP((Xt *)a_target);
                   2734: INST_TAIL; NEXT_P2;
                   2735: #endif
1.99      anton    2736: }
                   2737: SUPER_CONTINUE;
                   2738: :
                   2739:  swap 2dup
                   2740:  r> swap >r swap >r
                   2741:  u<=
                   2742:  IF
                   2743:      @
                   2744:  ELSE
                   2745:      cell+
                   2746:  THEN  >r ;
1.102     anton    2747: 
1.104     anton    2748: \ set-next-code and call2 do not appear in images and can be
                   2749: \ renumbered arbitrarily
                   2750: 
1.102     anton    2751: set-next-code ( #w -- ) gforth set_next_code
                   2752: #ifdef NO_IP
                   2753: next_code = (Label)w;
                   2754: #endif
                   2755: 
                   2756: call2 ( #a_callee #a_ret_addr -- R:a_ret_addr ) gforth
                   2757: /* call with explicit return address */
                   2758: #ifdef NO_IP
                   2759: INST_TAIL;
                   2760: JUMP(a_callee);
                   2761: #else
                   2762: assert(0);
                   2763: #endif
                   2764: 
                   2765: compile-prim1 ( a_prim -- ) gforth compile_prim1
                   2766: ""compile prim (incl. immargs) at @var{a_prim}""
                   2767: compile_prim1(a_prim);
                   2768: 
                   2769: finish-code ( -- ) gforth finish_code
                   2770: ""Perform delayed steps in code generation (branch resolution, I-cache
                   2771: flushing).""
                   2772: finish_code();
1.104     anton    2773: 
1.105     anton    2774: forget-dyncode ( c_code -- f ) gforth-internal forget_dyncode
                   2775: f = forget_dyncode(c_code);
1.104     anton    2776: 
                   2777: decompile-prim ( a_code -- a_prim ) gforth-internal decompile_prim
                   2778: ""a_prim is the code address of the primitive that has been
                   2779: compile_prim1ed to a_code""
                   2780: a_prim = decompile_code(a_code);
1.99      anton    2781: 
                   2782: \+
                   2783: 
1.82      anton    2784: include(peeprules.vmg)
1.75      anton    2785: 
1.80      pazsan   2786: \+

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>