Annotation of gforth/prim, revision 1.106

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

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