Annotation of gforth/prim, revision 1.67

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).
                     56: \ 
                     57: \ 
                     58: \ 
                     59: \ The stack variables have the following types:
                     60: \ 
                     61: \ name matches type
                     62: \ f.*          Bool
                     63: \ c.*          Char
                     64: \ [nw].*               Cell
                     65: \ u.*          UCell
                     66: \ d.*          DCell
                     67: \ ud.*         UDCell
                     68: \ r.*          Float
                     69: \ a_.*         Cell *
                     70: \ c_.*         Char *
                     71: \ f_.*         Float *
                     72: \ df_.*                DFloat *
                     73: \ sf_.*                SFloat *
                     74: \ xt.*         XT
                     75: \ wid.*                WID
                     76: \ f83name.*    F83Name *
1.67    ! anton      77: 
        !            78: \E get-current prefixes set-current
        !            79: \E 
        !            80: \E s" Bool"            single data-stack type-prefix f
        !            81: \E s" Char"            single data-stack type-prefix c
        !            82: \E s" Cell"            single data-stack type-prefix n
        !            83: \E s" Cell"            single data-stack type-prefix w
        !            84: \E s" UCell"           single data-stack type-prefix u
        !            85: \E s" DCell"           double data-stack type-prefix d
        !            86: \E s" UDCell"          double data-stack type-prefix ud
        !            87: \E s" Float"           single fp-stack   type-prefix r
        !            88: \E s" Cell *"          single data-stack type-prefix a_
        !            89: \E s" Char *"          single data-stack type-prefix c_
        !            90: \E s" Float *"         single data-stack type-prefix f_
        !            91: \E s" DFloat *"                single data-stack type-prefix df_
        !            92: \E s" SFloat *"                single data-stack type-prefix sf_
        !            93: \E s" Xt"              single data-stack type-prefix xt
        !            94: \E s" WID"             single data-stack type-prefix wid
        !            95: \E s" struct F83Name *"        single data-stack type-prefix f83name
        !            96: \E 
        !            97: \E return-stack stack-prefix R:
        !            98: \E inst-stream  stack-prefix #
        !            99: \E 
        !           100: \E set-current
        !           101: 
1.1       anton     102: \ 
                    103: \ 
                    104: \ 
                    105: \ In addition the following names can be used:
                    106: \ ip   the instruction pointer
                    107: \ sp   the data stack pointer
                    108: \ rp   the parameter stack pointer
                    109: \ lp   the locals stack pointer
                    110: \ NEXT executes NEXT
                    111: \ cfa  
                    112: \ NEXT1        executes NEXT1
                    113: \ FLAG(x)      makes a Forth flag from a C flag
                    114: \ 
                    115: \ 
                    116: \ 
                    117: \ Percentages in comments are from Koopmans book: average/maximum use
                    118: \ (taken from four, not very representative benchmarks)
                    119: \ 
                    120: \ 
                    121: \ 
                    122: \ To do:
                    123: \ 
                    124: \ throw execute, cfa and NEXT1 out?
                    125: \ macroize *ip, ip++, *ip++ (pipelining)?
                    126: 
                    127: \ these m4 macros would collide with identifiers
                    128: undefine(`index')
                    129: undefine(`shift')
                    130: 
1.47      anton     131: noop   ( -- )          gforth
1.1       anton     132: ;
                    133: :
                    134:  ;
                    135: 
1.47      anton     136: lit    ( -- w )                gforth
1.1       anton     137: w = (Cell)NEXT_INST;
                    138: INC_IP(1);
                    139: :
                    140:  r> dup @ swap cell+ >r ;
                    141: 
1.47      anton     142: execute        ( xt -- )               core
1.29      crook     143: ""Perform the semantics represented by the execution token, @i{xt}.""
1.1       anton     144: ip=IP;
1.64      anton     145: IF_spTOS(spTOS = sp[0]);
1.1       anton     146: EXEC(xt);
                    147: 
1.47      anton     148: perform        ( a_addr -- )   gforth
1.55      anton     149: ""@code{@@ execute}.""
1.1       anton     150: /* and pfe */
                    151: ip=IP;
1.64      anton     152: IF_spTOS(spTOS = sp[0]);
1.1       anton     153: EXEC(*(Xt *)a_addr);
                    154: :
                    155:  @ execute ;
                    156: 
1.31      jwilke    157: \fhas? skipbranchprims 0= [IF]
1.15      pazsan    158: \+glocals
1.1       anton     159: 
1.47      anton     160: branch-lp+!#   ( -- )  gforth  branch_lp_plus_store_number
1.1       anton     161: /* this will probably not be used */
                    162: lp += (Cell)(IP[1]);
                    163: goto branch;
                    164: 
1.15      pazsan    165: \+
1.1       anton     166: 
1.47      anton     167: branch ( -- )          gforth
1.1       anton     168: branch:
1.23      anton     169: SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
1.1       anton     170: :
                    171:  r> dup @ + >r ;
                    172: 
                    173: \ condbranch(forthname,restline,code,forthcode)
                    174: \ this is non-syntactical: code must open a brace that is closed by the macro
                    175: define(condbranch,
                    176: $1     $2
1.23      anton     177: $3     SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
1.66      anton     178: TAIL;
1.1       anton     179: }
                    180: else
                    181:     INC_IP(1);
                    182: $4
                    183: 
1.15      pazsan    184: \+glocals
1.1       anton     185: 
                    186: $1-lp+!#       $2_lp_plus_store_number
1.65      anton     187: $3    lp += (Cell)(IP[1]);
                    188: SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
1.66      anton     189: TAIL;
1.1       anton     190: }
                    191: else
                    192:     INC_IP(2);
                    193: 
1.15      pazsan    194: \+
1.1       anton     195: )
                    196: 
1.47      anton     197: condbranch(?branch,( f -- )            f83     question_branch,
1.1       anton     198: if (f==0) {
1.5       jwilke    199: ,:
                    200:  0= dup     \ !f !f
                    201:  r> dup @   \ !f !f IP branchoffset
                    202:  rot and +  \ !f IP|IP+branchoffset
                    203:  swap 0= cell and + \ IP''
                    204:  >r ;)
1.1       anton     205: 
                    206: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
                    207: \ is only used in if's (yet)
                    208: 
1.15      pazsan    209: \+xconds
1.1       anton     210: 
1.47      anton     211: ?dup-?branch   ( f -- f )      new     question_dupe_question_branch
1.1       anton     212: ""The run-time procedure compiled by @code{?DUP-IF}.""
                    213: if (f==0) {
                    214:   sp++;
1.64      anton     215:   IF_spTOS(spTOS = sp[0]);
1.23      anton     216:   SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
1.1       anton     217:   NEXT;
                    218: }
                    219: else
                    220:   INC_IP(1);
                    221: 
1.47      anton     222: ?dup-0=-?branch        ( f -- )        new     question_dupe_zero_equals_question_branch
1.1       anton     223: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
                    224: /* the approach taken here of declaring the word as having the stack
                    225: effect ( f -- ) and correcting for it in the branch-taken case costs a
                    226: few cycles in that case, but is easy to convert to a CONDBRANCH
                    227: invocation */
                    228: if (f!=0) {
                    229:   sp--;
1.23      anton     230:   SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
1.1       anton     231:   NEXT;
                    232: }
                    233: else
                    234:   INC_IP(1);
                    235: 
1.15      pazsan    236: \+
1.31      jwilke    237: \f[THEN]
                    238: \fhas? skiploopprims 0= [IF]
1.1       anton     239: 
1.65      anton     240: condbranch((next),( R:n1 -- R:n2 )             cmFORTH paren_next,
                    241: n2=n1-1;
                    242: if (n1) {
1.1       anton     243: ,:
                    244:  r> r> dup 1- >r
                    245:  IF dup @ + >r ELSE cell+ >r THEN ;)
                    246: 
1.65      anton     247: condbranch((loop),( R:nlimit R:n1 -- R:nlimit R:n2 )   gforth  paren_loop,
                    248: n2=n1+1;
                    249: if (n2 != nlimit) {
1.1       anton     250: ,:
                    251:  r> r> 1+ r> 2dup =
                    252:  IF >r 1- >r cell+ >r
                    253:  ELSE >r >r dup @ + >r THEN ;)
                    254: 
1.65      anton     255: condbranch((+loop),( n R:nlimit R:n1 -- R:nlimit R:n2 )        gforth paren_plus_loop,
1.1       anton     256: /* !! check this thoroughly */
                    257: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
                    258: /* dependent upon two's complement arithmetic */
1.65      anton     259: Cell olddiff = n1-nlimit;
                    260: n2=n1+n;       
1.1       anton     261: if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
                    262:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
                    263: ,:
                    264:  r> swap
                    265:  r> r> 2dup - >r
                    266:  2 pick r@ + r@ xor 0< 0=
                    267:  3 pick r> xor 0< 0= or
                    268:  IF    >r + >r dup @ + >r
                    269:  ELSE  >r >r drop cell+ >r THEN ;)
                    270: 
1.15      pazsan    271: \+xconds
1.1       anton     272: 
1.65      anton     273: condbranch((-loop),( u R:nlimit R:n1 -- R:nlimit R:n2 )        gforth  paren_minus_loop,
                    274: UCell olddiff = n1-nlimit;
                    275: n2=n1-u;
1.1       anton     276: if (olddiff>u) {
                    277: ,)
                    278: 
1.65      anton     279: condbranch((s+loop),( n R:nlimit R:n1 -- R:nlimit R:n2 )               gforth  paren_symmetric_plus_loop,
1.1       anton     280: ""The run-time procedure compiled by S+LOOP. It loops until the index
                    281: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
                    282: version of (+LOOP).""
                    283: /* !! check this thoroughly */
1.65      anton     284: Cell diff = n1-nlimit;
1.1       anton     285: Cell newdiff = diff+n;
                    286: if (n<0) {
                    287:     diff = -diff;
                    288:     newdiff = -newdiff;
                    289: }
1.65      anton     290: n2=n1+n;
1.1       anton     291: if (diff>=0 || newdiff<0) {
                    292: ,)
                    293: 
1.15      pazsan    294: \+
1.1       anton     295: 
1.65      anton     296: unloop ( R:w1 R:w2 -- )        core
                    297: /* !! alias for 2rdrop */
1.1       anton     298: :
                    299:  r> rdrop rdrop >r ;
                    300: 
1.65      anton     301: (for)  ( ncount -- R:nlimit R:ncount )         cmFORTH         paren_for
1.1       anton     302: /* or (for) = >r -- collides with unloop! */
1.65      anton     303: nlimit=0;
1.1       anton     304: :
                    305:  r> swap 0 >r >r >r ;
                    306: 
1.65      anton     307: (do)   ( nlimit nstart -- R:nlimit R:nstart )  gforth          paren_do
1.1       anton     308: :
                    309:  r> swap rot >r >r >r ;
                    310: 
1.65      anton     311: (?do)  ( nlimit nstart -- R:nlimit R:nstart )  gforth  paren_question_do
1.1       anton     312: if (nstart == nlimit) {
1.64      anton     313:     IF_spTOS(spTOS = sp[0]);
1.1       anton     314:     goto branch;
                    315:     }
                    316: else {
                    317:     INC_IP(1);
                    318: }
                    319: :
                    320:   2dup =
                    321:   IF   r> swap rot >r >r
                    322:        dup @ + >r
                    323:   ELSE r> swap rot >r >r
                    324:        cell+ >r
                    325:   THEN ;                               \ --> CORE-EXT
                    326: 
1.15      pazsan    327: \+xconds
1.1       anton     328: 
1.65      anton     329: (+do)  ( nlimit nstart -- R:nlimit R:nstart )  gforth  paren_plus_do
1.1       anton     330: if (nstart >= nlimit) {
1.64      anton     331:     IF_spTOS(spTOS = sp[0]);
1.1       anton     332:     goto branch;
                    333:     }
                    334: else {
                    335:     INC_IP(1);
                    336: }
                    337: :
                    338:  swap 2dup
                    339:  r> swap >r swap >r
                    340:  >=
                    341:  IF
                    342:      dup @ +
                    343:  ELSE
                    344:      cell+
                    345:  THEN  >r ;
                    346: 
1.65      anton     347: (u+do) ( ulimit ustart -- R:ulimit R:ustart )  gforth  paren_u_plus_do
1.1       anton     348: if (ustart >= ulimit) {
1.64      anton     349:     IF_spTOS(spTOS = sp[0]);
1.1       anton     350:     goto branch;
                    351:     }
                    352: else {
                    353:     INC_IP(1);
                    354: }
                    355: :
                    356:  swap 2dup
                    357:  r> swap >r swap >r
                    358:  u>=
                    359:  IF
                    360:      dup @ +
                    361:  ELSE
                    362:      cell+
                    363:  THEN  >r ;
                    364: 
1.65      anton     365: (-do)  ( nlimit nstart -- R:nlimit R:nstart )  gforth  paren_minus_do
1.1       anton     366: if (nstart <= nlimit) {
1.64      anton     367:     IF_spTOS(spTOS = sp[0]);
1.1       anton     368:     goto branch;
                    369:     }
                    370: else {
                    371:     INC_IP(1);
                    372: }
                    373: :
                    374:  swap 2dup
                    375:  r> swap >r swap >r
                    376:  <=
                    377:  IF
                    378:      dup @ +
                    379:  ELSE
                    380:      cell+
                    381:  THEN  >r ;
                    382: 
1.65      anton     383: (u-do) ( ulimit ustart -- R:ulimit R:ustart )  gforth  paren_u_minus_do
1.1       anton     384: if (ustart <= ulimit) {
1.64      anton     385:     IF_spTOS(spTOS = sp[0]);
1.1       anton     386:     goto branch;
                    387:     }
                    388: else {
                    389:     INC_IP(1);
                    390: }
                    391: :
                    392:  swap 2dup
                    393:  r> swap >r swap >r
                    394:  u<=
                    395:  IF
                    396:      dup @ +
                    397:  ELSE
                    398:      cell+
                    399:  THEN  >r ;
                    400: 
1.15      pazsan    401: \+
1.1       anton     402: 
1.5       jwilke    403: \ don't make any assumptions where the return stack is!!
                    404: \ implement this in machine code if it should run quickly!
                    405: 
1.65      anton     406: i      ( R:n -- R:n n )                core
1.1       anton     407: :
1.5       jwilke    408: \ rp@ cell+ @ ;
                    409:   r> r> tuck >r >r ;
1.1       anton     410: 
1.65      anton     411: i'     ( R:w R:w2 -- R:w R:w2 w )              gforth          i_tick
1.1       anton     412: :
1.5       jwilke    413: \ rp@ cell+ cell+ @ ;
                    414:   r> r> r> dup itmp ! >r >r >r itmp @ ;
                    415: variable itmp
1.1       anton     416: 
1.65      anton     417: j      ( R:n R:d1 -- n R:n R:d1 )              core
1.1       anton     418: :
1.5       jwilke    419: \ rp@ cell+ cell+ cell+ @ ;
                    420:   r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
                    421: [IFUNDEF] itmp variable itmp [THEN]
1.1       anton     422: 
1.65      anton     423: k      ( R:n R:d1 R:d2 -- n R:n R:d1 R:d2 )            gforth
1.1       anton     424: :
1.5       jwilke    425: \ rp@ [ 5 cells ] Literal + @ ;
                    426:   r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
                    427: [IFUNDEF] itmp variable itmp [THEN]
1.31      jwilke    428: 
                    429: \f[THEN]
1.1       anton     430: 
                    431: \ digit is high-level: 0/0%
                    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.47      anton     542: +      ( n1 n2 -- n )          core    plus
1.1       anton     543: n = n1+n2;
                    544: 
                    545: \ PFE-0.9.14 has it differently, but the next release will have it as follows
1.47      anton     546: under+ ( n1 n2 n3 -- n n2 )    gforth  under_plus
1.29      crook     547: ""add @i{n3} to @i{n1} (giving @i{n})""
1.1       anton     548: n = n1+n3;
                    549: :
                    550:  rot + swap ;
                    551: 
1.47      anton     552: -      ( n1 n2 -- n )          core    minus
1.1       anton     553: n = n1-n2;
                    554: :
                    555:  negate + ;
                    556: 
1.47      anton     557: negate ( n1 -- n2 )            core
1.1       anton     558: /* use minus as alias */
                    559: n2 = -n1;
                    560: :
                    561:  invert 1+ ;
                    562: 
1.47      anton     563: 1+     ( n1 -- n2 )            core            one_plus
1.1       anton     564: n2 = n1+1;
                    565: :
                    566:  1 + ;
                    567: 
1.47      anton     568: 1-     ( n1 -- n2 )            core            one_minus
1.1       anton     569: n2 = n1-1;
                    570: :
                    571:  1 - ;
                    572: 
1.47      anton     573: max    ( n1 n2 -- n )  core
1.1       anton     574: if (n1<n2)
                    575:   n = n2;
                    576: else
                    577:   n = n1;
                    578: :
                    579:  2dup < IF swap THEN drop ;
                    580: 
1.47      anton     581: min    ( n1 n2 -- n )  core
1.1       anton     582: if (n1<n2)
                    583:   n = n1;
                    584: else
                    585:   n = n2;
                    586: :
                    587:  2dup > IF swap THEN drop ;
                    588: 
1.52      anton     589: abs    ( n -- u )      core
                    590: if (n<0)
                    591:   u = -n;
1.1       anton     592: else
1.52      anton     593:   u = n;
1.1       anton     594: :
                    595:  dup 0< IF negate THEN ;
                    596: 
1.47      anton     597: *      ( n1 n2 -- n )          core    star
1.1       anton     598: n = n1*n2;
                    599: :
                    600:  um* drop ;
                    601: 
1.47      anton     602: /      ( n1 n2 -- n )          core    slash
1.1       anton     603: n = n1/n2;
                    604: :
                    605:  /mod nip ;
                    606: 
1.47      anton     607: mod    ( n1 n2 -- n )          core
1.1       anton     608: n = n1%n2;
                    609: :
                    610:  /mod drop ;
                    611: 
1.47      anton     612: /mod   ( n1 n2 -- n3 n4 )              core            slash_mod
1.1       anton     613: n4 = n1/n2;
                    614: n3 = n1%n2; /* !! is this correct? look into C standard! */
                    615: :
                    616:  >r s>d r> fm/mod ;
                    617: 
1.47      anton     618: 2*     ( n1 -- n2 )            core            two_star
1.52      anton     619: ""Shift left by 1; also works on unsigned numbers""
1.1       anton     620: n2 = 2*n1;
                    621: :
                    622:  dup + ;
                    623: 
1.47      anton     624: 2/     ( n1 -- n2 )            core            two_slash
1.52      anton     625: ""Arithmetic shift right by 1.  For signed numbers this is a floored
                    626: division by 2 (note that @code{/} not necessarily floors).""
1.1       anton     627: n2 = n1>>1;
                    628: :
                    629:  dup MINI and IF 1 ELSE 0 THEN
                    630:  [ bits/byte cell * 1- ] literal 
1.5       jwilke    631:  0 DO 2* swap dup 2* >r MINI and 
1.1       anton     632:      IF 1 ELSE 0 THEN or r> swap
                    633:  LOOP nip ;
                    634: 
1.47      anton     635: fm/mod ( d1 n1 -- n2 n3 )              core            f_m_slash_mod
1.29      crook     636: ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}.""
1.1       anton     637: #ifdef BUGGY_LONG_LONG
                    638: DCell r = fmdiv(d1,n1);
                    639: n2=r.hi;
                    640: n3=r.lo;
                    641: #else
                    642: /* assumes that the processor uses either floored or symmetric division */
                    643: n3 = d1/n1;
                    644: n2 = d1%n1;
                    645: /* note that this 1%-3>0 is optimized by the compiler */
                    646: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
                    647:   n3--;
                    648:   n2+=n1;
                    649: }
                    650: #endif
                    651: :
                    652:  dup >r dup 0< IF  negate >r dnegate r>  THEN
                    653:  over       0< IF  tuck + swap  THEN
                    654:  um/mod
                    655:  r> 0< IF  swap negate swap  THEN ;
                    656: 
1.47      anton     657: sm/rem ( d1 n1 -- n2 n3 )              core            s_m_slash_rem
1.29      crook     658: ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0.""
1.1       anton     659: #ifdef BUGGY_LONG_LONG
                    660: DCell r = smdiv(d1,n1);
                    661: n2=r.hi;
                    662: n3=r.lo;
                    663: #else
                    664: /* assumes that the processor uses either floored or symmetric division */
                    665: n3 = d1/n1;
                    666: n2 = d1%n1;
                    667: /* note that this 1%-3<0 is optimized by the compiler */
                    668: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
                    669:   n3++;
                    670:   n2-=n1;
                    671: }
                    672: #endif
                    673: :
                    674:  over >r dup >r abs -rot
                    675:  dabs rot um/mod
                    676:  r> r@ xor 0< IF       negate       THEN
                    677:  r>        0< IF  swap negate swap  THEN ;
                    678: 
1.47      anton     679: m*     ( n1 n2 -- d )          core    m_star
1.1       anton     680: #ifdef BUGGY_LONG_LONG
                    681: d = mmul(n1,n2);
                    682: #else
                    683: d = (DCell)n1 * (DCell)n2;
                    684: #endif
                    685: :
                    686:  2dup      0< and >r
                    687:  2dup swap 0< and >r
                    688:  um* r> - r> - ;
                    689: 
1.47      anton     690: um*    ( u1 u2 -- ud )         core    u_m_star
1.1       anton     691: /* use u* as alias */
                    692: #ifdef BUGGY_LONG_LONG
                    693: ud = ummul(u1,u2);
                    694: #else
                    695: ud = (UDCell)u1 * (UDCell)u2;
                    696: #endif
                    697: :
                    698:    >r >r 0 0 r> r> [ 8 cells ] literal 0
                    699:    DO
                    700:        over >r dup >r 0< and d2*+ drop
                    701:        r> 2* r> swap
                    702:    LOOP 2drop ;
                    703: : d2*+ ( ud n -- ud+n c )
                    704:    over MINI
                    705:    and >r >r 2dup d+ swap r> + swap r> ;
                    706: 
1.47      anton     707: um/mod ( ud u1 -- u2 u3 )              core    u_m_slash_mod
1.32      anton     708: ""ud=u3*u1+u2, u1>u2>=0""
1.1       anton     709: #ifdef BUGGY_LONG_LONG
                    710: UDCell r = umdiv(ud,u1);
                    711: u2=r.hi;
                    712: u3=r.lo;
                    713: #else
                    714: u3 = ud/u1;
                    715: u2 = ud%u1;
                    716: #endif
                    717: :
                    718:    0 swap [ 8 cells 1 + ] literal 0
1.5       jwilke    719:    ?DO /modstep
1.1       anton     720:    LOOP drop swap 1 rshift or swap ;
                    721: : /modstep ( ud c R: u -- ud-?u c R: u )
1.5       jwilke    722:    >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN  d2*+ r> ;
1.1       anton     723: : d2*+ ( ud n -- ud+n c )
                    724:    over MINI
                    725:    and >r >r 2dup d+ swap r> + swap r> ;
                    726: 
1.47      anton     727: m+     ( d1 n -- d2 )          double          m_plus
1.1       anton     728: #ifdef BUGGY_LONG_LONG
                    729: d2.lo = d1.lo+n;
                    730: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
                    731: #else
                    732: d2 = d1+n;
                    733: #endif
                    734: :
                    735:  s>d d+ ;
                    736: 
1.47      anton     737: d+     ( d1 d2 -- d )          double  d_plus
1.1       anton     738: #ifdef BUGGY_LONG_LONG
                    739: d.lo = d1.lo+d2.lo;
                    740: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
                    741: #else
                    742: d = d1+d2;
                    743: #endif
                    744: :
                    745:  rot + >r tuck + swap over u> r> swap - ;
                    746: 
1.47      anton     747: d-     ( d1 d2 -- d )          double          d_minus
1.1       anton     748: #ifdef BUGGY_LONG_LONG
                    749: d.lo = d1.lo - d2.lo;
                    750: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
                    751: #else
                    752: d = d1-d2;
                    753: #endif
                    754: :
                    755:  dnegate d+ ;
                    756: 
1.47      anton     757: dnegate        ( d1 -- d2 )            double  d_negate
1.1       anton     758: /* use dminus as alias */
                    759: #ifdef BUGGY_LONG_LONG
                    760: d2 = dnegate(d1);
                    761: #else
                    762: d2 = -d1;
                    763: #endif
                    764: :
                    765:  invert swap negate tuck 0= - ;
                    766: 
1.47      anton     767: d2*    ( d1 -- d2 )            double          d_two_star
1.52      anton     768: ""Shift left by 1; also works on unsigned numbers""
1.1       anton     769: #ifdef BUGGY_LONG_LONG
                    770: d2.lo = d1.lo<<1;
                    771: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
                    772: #else
                    773: d2 = 2*d1;
                    774: #endif
                    775: :
                    776:  2dup d+ ;
                    777: 
1.47      anton     778: d2/    ( d1 -- d2 )            double          d_two_slash
1.52      anton     779: ""Arithmetic shift right by 1.  For signed numbers this is a floored
                    780: division by 2.""
1.1       anton     781: #ifdef BUGGY_LONG_LONG
                    782: d2.hi = d1.hi>>1;
                    783: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
                    784: #else
                    785: d2 = d1>>1;
                    786: #endif
                    787: :
                    788:  dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
                    789:  r> IF  [ 1 8 cells 1- lshift ] Literal + THEN  swap ;
                    790: 
1.47      anton     791: and    ( w1 w2 -- w )          core
1.1       anton     792: w = w1&w2;
                    793: 
1.47      anton     794: or     ( w1 w2 -- w )          core
1.1       anton     795: w = w1|w2;
                    796: :
                    797:  invert swap invert and invert ;
                    798: 
1.47      anton     799: xor    ( w1 w2 -- w )          core    x_or
1.1       anton     800: w = w1^w2;
                    801: 
1.47      anton     802: invert ( w1 -- w2 )            core
1.1       anton     803: w2 = ~w1;
                    804: :
                    805:  MAXU xor ;
                    806: 
1.47      anton     807: rshift ( u1 n -- u2 )          core    r_shift
1.53      anton     808: ""Logical shift right by @i{n} bits.""
1.1       anton     809:   u2 = u1>>n;
                    810: :
                    811:     0 ?DO 2/ MAXI and LOOP ;
                    812: 
1.47      anton     813: lshift ( u1 n -- u2 )          core    l_shift
1.1       anton     814:   u2 = u1<<n;
                    815: :
                    816:     0 ?DO 2* LOOP ;
                    817: 
                    818: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
                    819: define(comparisons,
1.47      anton     820: $1=    ( $2 -- f )             $6      $3equals
1.1       anton     821: f = FLAG($4==$5);
                    822: :
                    823:     [ char $1x char 0 = [IF]
                    824:        ] IF false ELSE true THEN [
                    825:     [ELSE]
                    826:        ] xor 0= [
                    827:     [THEN] ] ;
                    828: 
1.47      anton     829: $1<>   ( $2 -- f )             $7      $3not_equals
1.1       anton     830: f = FLAG($4!=$5);
                    831: :
                    832:     [ char $1x char 0 = [IF]
                    833:        ] IF true ELSE false THEN [
                    834:     [ELSE]
                    835:        ] xor 0<> [
                    836:     [THEN] ] ;
                    837: 
1.47      anton     838: $1<    ( $2 -- f )             $8      $3less_than
1.1       anton     839: f = FLAG($4<$5);
                    840: :
                    841:     [ char $1x char 0 = [IF]
                    842:        ] MINI and 0<> [
                    843:     [ELSE] char $1x char u = [IF]
                    844:        ]   2dup xor 0<  IF nip ELSE - THEN 0<  [
                    845:        [ELSE]
                    846:            ] MINI xor >r MINI xor r> u< [
                    847:        [THEN]
                    848:     [THEN] ] ;
                    849: 
1.47      anton     850: $1>    ( $2 -- f )             $9      $3greater_than
1.1       anton     851: f = FLAG($4>$5);
                    852: :
                    853:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    854:     $1< ;
                    855: 
1.47      anton     856: $1<=   ( $2 -- f )             gforth  $3less_or_equal
1.1       anton     857: f = FLAG($4<=$5);
                    858: :
                    859:     $1> 0= ;
                    860: 
1.47      anton     861: $1>=   ( $2 -- f )             gforth  $3greater_or_equal
1.1       anton     862: f = FLAG($4>=$5);
                    863: :
                    864:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    865:     $1<= ;
                    866: 
                    867: )
                    868: 
                    869: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
                    870: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
                    871: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
                    872: 
                    873: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
                    874: define(dcomparisons,
1.47      anton     875: $1=    ( $2 -- f )             $6      $3equals
1.1       anton     876: #ifdef BUGGY_LONG_LONG
                    877: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
                    878: #else
                    879: f = FLAG($4==$5);
                    880: #endif
                    881: 
1.47      anton     882: $1<>   ( $2 -- f )             $7      $3not_equals
1.1       anton     883: #ifdef BUGGY_LONG_LONG
                    884: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
                    885: #else
                    886: f = FLAG($4!=$5);
                    887: #endif
                    888: 
1.47      anton     889: $1<    ( $2 -- f )             $8      $3less_than
1.1       anton     890: #ifdef BUGGY_LONG_LONG
                    891: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
                    892: #else
                    893: f = FLAG($4<$5);
                    894: #endif
                    895: 
1.47      anton     896: $1>    ( $2 -- f )             $9      $3greater_than
1.1       anton     897: #ifdef BUGGY_LONG_LONG
                    898: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
                    899: #else
                    900: f = FLAG($4>$5);
                    901: #endif
                    902: 
1.47      anton     903: $1<=   ( $2 -- f )             gforth  $3less_or_equal
1.1       anton     904: #ifdef BUGGY_LONG_LONG
                    905: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
                    906: #else
                    907: f = FLAG($4<=$5);
                    908: #endif
                    909: 
1.47      anton     910: $1>=   ( $2 -- f )             gforth  $3greater_or_equal
1.1       anton     911: #ifdef BUGGY_LONG_LONG
                    912: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
                    913: #else
                    914: f = FLAG($4>=$5);
                    915: #endif
                    916: 
                    917: )
                    918: 
1.15      pazsan    919: \+dcomps
1.1       anton     920: 
                    921: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
                    922: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
                    923: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
                    924: 
1.15      pazsan    925: \+
1.1       anton     926: 
1.47      anton     927: within ( u1 u2 u3 -- f )               core-ext
1.32      anton     928: ""u2=<u1<u3 or: u3=<u2 and u1 is not in [u3,u2).  This works for
                    929: unsigned and signed numbers (but not a mixture).  Another way to think
                    930: about this word is to consider the numbers as a circle (wrapping
                    931: around from @code{max-u} to 0 for unsigned, and from @code{max-n} to
                    932: min-n for signed numbers); now consider the range from u2 towards
                    933: increasing numbers up to and excluding u3 (giving an empty range if
1.52      anton     934: u2=u3); if u1 is in this range, @code{within} returns true.""
1.1       anton     935: f = FLAG(u1-u2 < u3-u2);
                    936: :
                    937:  over - >r - r> u< ;
                    938: 
1.47      anton     939: sp@    ( -- a_addr )           gforth          sp_fetch
1.1       anton     940: a_addr = sp+1;
                    941: 
1.47      anton     942: sp!    ( a_addr -- )           gforth          sp_store
1.1       anton     943: sp = a_addr;
1.64      anton     944: /* works with and without spTOS caching */
1.1       anton     945: 
1.47      anton     946: rp@    ( -- a_addr )           gforth          rp_fetch
1.1       anton     947: a_addr = rp;
                    948: 
1.47      anton     949: rp!    ( a_addr -- )           gforth          rp_store
1.1       anton     950: rp = a_addr;
                    951: 
1.15      pazsan    952: \+floating
1.1       anton     953: 
1.47      anton     954: fp@    ( -- f_addr )   gforth  fp_fetch
1.1       anton     955: f_addr = fp;
                    956: 
1.47      anton     957: fp!    ( f_addr -- )   gforth  fp_store
1.1       anton     958: fp = f_addr;
                    959: 
1.15      pazsan    960: \+
1.1       anton     961: 
1.65      anton     962: ;s     ( R:w -- )              gforth  semis
1.22      crook     963: ""The primitive compiled by @code{EXIT}.""
1.65      anton     964: SET_IP((Xt *)w);
1.1       anton     965: 
1.65      anton     966: >r     ( w -- R:w )            core    to_r
1.1       anton     967: :
                    968:  (>r) ;
                    969: : (>r)  rp@ cell+ @ rp@ ! rp@ cell+ ! ;
                    970: 
1.65      anton     971: r>     ( R:w -- w )            core    r_from
1.1       anton     972: :
                    973:  rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
                    974: Create (rdrop) ' ;s A,
                    975: 
1.65      anton     976: rdrop  ( R:w -- )              gforth
1.1       anton     977: :
                    978:  r> r> drop >r ;
                    979: 
1.65      anton     980: 2>r    ( w1 w2 -- R:w1 R:w2 )  core-ext        two_to_r
1.1       anton     981: :
                    982:  swap r> swap >r swap >r >r ;
                    983: 
1.65      anton     984: 2r>    ( R:w1 R:w2 -- w1 w2 )  core-ext        two_r_from
1.1       anton     985: :
                    986:  r> r> swap r> swap >r swap ;
                    987: 
1.65      anton     988: 2r@    ( R:w1 R:w2 -- R:w1 R:w2 w1 w2 )        core-ext        two_r_fetch
1.1       anton     989: :
                    990:  i' j ;
                    991: 
1.65      anton     992: 2rdrop (  R:w1 R:w2 -- )               gforth  two_r_drop
1.1       anton     993: :
                    994:  r> r> drop r> drop >r ;
                    995: 
1.47      anton     996: over   ( w1 w2 -- w1 w2 w1 )           core
1.1       anton     997: :
                    998:  sp@ cell+ @ ;
                    999: 
1.47      anton    1000: drop   ( w -- )                core
1.1       anton    1001: :
                   1002:  IF THEN ;
                   1003: 
1.47      anton    1004: swap   ( w1 w2 -- w2 w1 )              core
1.1       anton    1005: :
                   1006:  >r (swap) ! r> (swap) @ ;
                   1007: Variable (swap)
                   1008: 
1.47      anton    1009: dup    ( w -- w w )            core    dupe
1.1       anton    1010: :
                   1011:  sp@ @ ;
                   1012: 
1.47      anton    1013: rot    ( w1 w2 w3 -- w2 w3 w1 )        core    rote
1.1       anton    1014: :
                   1015: [ defined? (swap) [IF] ]
                   1016:     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
                   1017: Variable (rot)
                   1018: [ELSE] ]
                   1019:     >r swap r> swap ;
                   1020: [THEN]
                   1021: 
1.47      anton    1022: -rot   ( w1 w2 w3 -- w3 w1 w2 )        gforth  not_rote
1.1       anton    1023: :
                   1024:  rot rot ;
                   1025: 
1.47      anton    1026: nip    ( w1 w2 -- w2 )         core-ext
1.1       anton    1027: :
1.6       jwilke   1028:  swap drop ;
1.1       anton    1029: 
1.47      anton    1030: tuck   ( w1 w2 -- w2 w1 w2 )   core-ext
1.1       anton    1031: :
                   1032:  swap over ;
                   1033: 
1.47      anton    1034: ?dup   ( w -- w )                      core    question_dupe
1.52      anton    1035: ""Actually the stack effect is: @code{( w -- 0 | w w )}.  It performs a
                   1036: @code{dup} if w is nonzero.""
1.1       anton    1037: if (w!=0) {
1.64      anton    1038:   IF_spTOS(*sp-- = w;)
1.1       anton    1039: #ifndef USE_TOS
                   1040:   *--sp = w;
                   1041: #endif
                   1042: }
                   1043: :
                   1044:  dup IF dup THEN ;
                   1045: 
1.47      anton    1046: pick   ( u -- w )                      core-ext
1.52      anton    1047: ""Actually the stack effect is @code{ x0 ... xu u -- x0 ... xu x0 }.""
1.1       anton    1048: w = sp[u+1];
                   1049: :
                   1050:  1+ cells sp@ + @ ;
                   1051: 
1.47      anton    1052: 2drop  ( w1 w2 -- )            core    two_drop
1.1       anton    1053: :
                   1054:  drop drop ;
                   1055: 
1.47      anton    1056: 2dup   ( w1 w2 -- w1 w2 w1 w2 )        core    two_dupe
1.1       anton    1057: :
                   1058:  over over ;
                   1059: 
1.47      anton    1060: 2over  ( w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 )    core    two_over
1.1       anton    1061: :
                   1062:  3 pick 3 pick ;
                   1063: 
1.47      anton    1064: 2swap  ( w1 w2 w3 w4 -- w3 w4 w1 w2 )  core    two_swap
1.1       anton    1065: :
                   1066:  rot >r rot r> ;
                   1067: 
1.47      anton    1068: 2rot   ( w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 )      double-ext      two_rote
1.1       anton    1069: :
                   1070:  >r >r 2swap r> r> 2swap ;
                   1071: 
1.47      anton    1072: 2nip   ( w1 w2 w3 w4 -- w3 w4 )        gforth  two_nip
1.1       anton    1073: :
                   1074:  2swap 2drop ;
                   1075: 
1.47      anton    1076: 2tuck  ( w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 )    gforth  two_tuck
1.1       anton    1077: :
                   1078:  2swap 2over ;
                   1079: 
                   1080: \ toggle is high-level: 0.11/0.42%
                   1081: 
1.47      anton    1082: @      ( a_addr -- w )         core    fetch
1.52      anton    1083: ""@i{w} is the cell stored at @i{a_addr}.""
1.1       anton    1084: w = *a_addr;
                   1085: 
1.47      anton    1086: !      ( w a_addr -- )         core    store
1.52      anton    1087: ""Store @i{w} into the cell at @i{a-addr}.""
1.1       anton    1088: *a_addr = w;
                   1089: 
1.47      anton    1090: +!     ( n a_addr -- )         core    plus_store
1.52      anton    1091: ""Add @i{n} to the cell at @i{a-addr}.""
1.1       anton    1092: *a_addr += n;
                   1093: :
                   1094:  tuck @ + swap ! ;
                   1095: 
1.47      anton    1096: c@     ( c_addr -- c )         core    c_fetch
1.52      anton    1097: ""@i{c} is the char stored at @i{c_addr}.""
1.1       anton    1098: c = *c_addr;
                   1099: :
                   1100: [ bigendian [IF] ]
                   1101:     [ cell>bit 4 = [IF] ]
                   1102:        dup [ 0 cell - ] Literal and @ swap 1 and
                   1103:        IF  $FF and  ELSE  8>>  THEN  ;
                   1104:     [ [ELSE] ]
                   1105:        dup [ cell 1- ] literal and
                   1106:        tuck - @ swap [ cell 1- ] literal xor
                   1107:        0 ?DO 8>> LOOP $FF and
                   1108:     [ [THEN] ]
                   1109: [ [ELSE] ]
                   1110:     [ cell>bit 4 = [IF] ]
                   1111:        dup [ 0 cell - ] Literal and @ swap 1 and
                   1112:        IF  8>>  ELSE  $FF and  THEN
                   1113:     [ [ELSE] ]
                   1114:        dup [ cell  1- ] literal and 
                   1115:        tuck - @ swap
                   1116:        0 ?DO 8>> LOOP 255 and
                   1117:     [ [THEN] ]
                   1118: [ [THEN] ]
                   1119: ;
                   1120: : 8>> 2/ 2/ 2/ 2/  2/ 2/ 2/ 2/ ;
                   1121: 
1.47      anton    1122: c!     ( c c_addr -- )         core    c_store
1.52      anton    1123: ""Store @i{c} into the char at @i{c-addr}.""
1.1       anton    1124: *c_addr = c;
                   1125: :
                   1126: [ bigendian [IF] ]
                   1127:     [ cell>bit 4 = [IF] ]
                   1128:        tuck 1 and IF  $FF and  ELSE  8<<  THEN >r
                   1129:        dup -2 and @ over 1 and cells masks + @ and
                   1130:        r> or swap -2 and ! ;
                   1131:        Create masks $00FF , $FF00 ,
                   1132:     [ELSE] ]
                   1133:        dup [ cell 1- ] literal and dup 
                   1134:        [ cell 1- ] literal xor >r
                   1135:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
                   1136:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1137:     [THEN]
                   1138: [ELSE] ]
                   1139:     [ cell>bit 4 = [IF] ]
                   1140:        tuck 1 and IF  8<<  ELSE  $FF and  THEN >r
                   1141:        dup -2 and @ over 1 and cells masks + @ and
                   1142:        r> or swap -2 and ! ;
                   1143:        Create masks $FF00 , $00FF ,
                   1144:     [ELSE] ]
                   1145:        dup [ cell 1- ] literal and dup >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: [THEN]
                   1150: : 8<< 2* 2* 2* 2*  2* 2* 2* 2* ;
                   1151: 
1.47      anton    1152: 2!     ( w1 w2 a_addr -- )             core    two_store
1.52      anton    1153: ""Store @i{w2} into the cell at @i{c-addr} and @i{w1} into the next cell.""
1.1       anton    1154: a_addr[0] = w2;
                   1155: a_addr[1] = w1;
                   1156: :
                   1157:  tuck ! cell+ ! ;
                   1158: 
1.47      anton    1159: 2@     ( a_addr -- w1 w2 )             core    two_fetch
1.52      anton    1160: ""@i{w2} is the content of the cell stored at @i{a-addr}, @i{w1} is
                   1161: the content of the next cell.""
1.1       anton    1162: w2 = a_addr[0];
                   1163: w1 = a_addr[1];
                   1164: :
                   1165:  dup cell+ @ swap @ ;
                   1166: 
1.47      anton    1167: cell+  ( a_addr1 -- a_addr2 )  core    cell_plus
1.52      anton    1168: ""@code{1 cells +}""
1.1       anton    1169: a_addr2 = a_addr1+1;
                   1170: :
                   1171:  cell + ;
                   1172: 
1.47      anton    1173: cells  ( n1 -- n2 )            core
1.52      anton    1174: "" @i{n2} is the number of address units of @i{n1} cells.""
1.1       anton    1175: n2 = n1 * sizeof(Cell);
                   1176: :
                   1177:  [ cell
                   1178:  2/ dup [IF] ] 2* [ [THEN]
                   1179:  2/ dup [IF] ] 2* [ [THEN]
                   1180:  2/ dup [IF] ] 2* [ [THEN]
                   1181:  2/ dup [IF] ] 2* [ [THEN]
                   1182:  drop ] ;
                   1183: 
1.47      anton    1184: char+  ( c_addr1 -- c_addr2 )  core    char_plus
1.52      anton    1185: ""@code{1 chars +}.""
1.1       anton    1186: c_addr2 = c_addr1 + 1;
                   1187: :
                   1188:  1+ ;
                   1189: 
1.47      anton    1190: (chars)        ( n1 -- n2 )    gforth  paren_chars
1.1       anton    1191: n2 = n1 * sizeof(Char);
                   1192: :
                   1193:  ;
                   1194: 
1.47      anton    1195: count  ( c_addr1 -- c_addr2 u )        core
1.56      anton    1196: ""@i{c-addr2} is the first character and @i{u} the length of the
                   1197: counted string at @i{c-addr1}.""
1.1       anton    1198: u = *c_addr1;
                   1199: c_addr2 = c_addr1+1;
                   1200: :
                   1201:  dup 1+ swap c@ ;
                   1202: 
1.47      anton    1203: (f83find)      ( c_addr u f83name1 -- f83name2 )       new     paren_f83find
1.13      pazsan   1204: for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
1.1       anton    1205:   if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1206:       memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1207:     break;
                   1208: f83name2=f83name1;
                   1209: :
                   1210:     BEGIN  dup WHILE  (find-samelen)  dup  WHILE
                   1211:        >r 2dup r@ cell+ char+ capscomp  0=
                   1212:        IF  2drop r>  EXIT  THEN
                   1213:        r> @
                   1214:     REPEAT  THEN  nip nip ;
                   1215: : (find-samelen) ( u f83name1 -- u f83name2/0 )
                   1216:     BEGIN  2dup cell+ c@ $1F and <> WHILE  @  dup 0= UNTIL  THEN ;
                   1217: 
1.15      pazsan   1218: \+hash
1.1       anton    1219: 
1.47      anton    1220: (hashfind)     ( c_addr u a_addr -- f83name2 ) new     paren_hashfind
1.13      pazsan   1221: struct F83Name *f83name1;
1.1       anton    1222: f83name2=NULL;
                   1223: while(a_addr != NULL)
                   1224: {
1.13      pazsan   1225:    f83name1=(struct F83Name *)(a_addr[1]);
1.1       anton    1226:    a_addr=(Cell *)(a_addr[0]);
                   1227:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1228:        memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1229:      {
                   1230:        f83name2=f83name1;
                   1231:        break;
                   1232:      }
                   1233: }
                   1234: :
                   1235:  BEGIN  dup  WHILE
                   1236:         2@ >r >r dup r@ cell+ c@ $1F and =
                   1237:         IF  2dup r@ cell+ char+ capscomp 0=
                   1238:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   1239:        rdrop r>
                   1240:  REPEAT nip nip ;
                   1241: 
1.47      anton    1242: (tablefind)    ( c_addr u a_addr -- f83name2 ) new     paren_tablefind
1.1       anton    1243: ""A case-sensitive variant of @code{(hashfind)}""
1.13      pazsan   1244: struct F83Name *f83name1;
1.1       anton    1245: f83name2=NULL;
                   1246: while(a_addr != NULL)
                   1247: {
1.13      pazsan   1248:    f83name1=(struct F83Name *)(a_addr[1]);
1.1       anton    1249:    a_addr=(Cell *)(a_addr[0]);
                   1250:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1251:        memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1252:      {
                   1253:        f83name2=f83name1;
                   1254:        break;
                   1255:      }
                   1256: }
                   1257: :
                   1258:  BEGIN  dup  WHILE
                   1259:         2@ >r >r dup r@ cell+ c@ $1F and =
                   1260:         IF  2dup r@ cell+ char+ -text 0=
                   1261:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   1262:        rdrop r>
                   1263:  REPEAT nip nip ;
                   1264: 
1.47      anton    1265: (hashkey)      ( c_addr u1 -- u2 )             gforth  paren_hashkey
1.1       anton    1266: u2=0;
                   1267: while(u1--)
                   1268:    u2+=(Cell)toupper(*c_addr++);
                   1269: :
                   1270:  0 -rot bounds ?DO  I c@ toupper +  LOOP ;
                   1271: 
1.47      anton    1272: (hashkey1)     ( c_addr u ubits -- ukey )              gforth  paren_hashkey1
1.1       anton    1273: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
                   1274: /* this hash function rotates the key at every step by rot bits within
                   1275:    ubits bits and xors it with the character. This function does ok in
                   1276:    the chi-sqare-test.  Rot should be <=7 (preferably <=5) for
                   1277:    ASCII strings (larger if ubits is large), and should share no
                   1278:    divisors with ubits.
                   1279: */
                   1280: unsigned rot = ((char []){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})[ubits];
                   1281: Char *cp = c_addr;
                   1282: for (ukey=0; cp<c_addr+u; cp++)
                   1283:     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot))) 
                   1284:             ^ toupper(*cp))
                   1285:            & ((1<<ubits)-1));
                   1286: :
                   1287:  dup rot-values + c@ over 1 swap lshift 1- >r
                   1288:  tuck - 2swap r> 0 2swap bounds
                   1289:  ?DO  dup 4 pick lshift swap 3 pick rshift or
                   1290:       I c@ toupper xor
                   1291:       over and  LOOP
                   1292:  nip nip nip ;
                   1293: Create rot-values
                   1294:   5 c, 0 c, 1 c, 2 c, 3 c,  4 c, 5 c, 5 c, 5 c, 5 c,
                   1295:   3 c, 5 c, 5 c, 5 c, 5 c,  7 c, 5 c, 5 c, 5 c, 5 c,
                   1296:   7 c, 5 c, 5 c, 5 c, 5 c,  6 c, 5 c, 5 c, 5 c, 5 c,
                   1297:   7 c, 5 c, 5 c,
                   1298: 
1.15      pazsan   1299: \+
1.1       anton    1300: 
1.47      anton    1301: (parse-white)  ( c_addr1 u1 -- c_addr2 u2 )    gforth  paren_parse_white
1.1       anton    1302: /* use !isgraph instead of isspace? */
                   1303: Char *endp = c_addr1+u1;
                   1304: while (c_addr1<endp && isspace(*c_addr1))
                   1305:   c_addr1++;
                   1306: if (c_addr1<endp) {
                   1307:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
                   1308:     ;
                   1309:   u2 = c_addr1-c_addr2;
                   1310: }
                   1311: else {
                   1312:   c_addr2 = c_addr1;
                   1313:   u2 = 0;
                   1314: }
                   1315: :
                   1316:  BEGIN  dup  WHILE  over c@ bl <=  WHILE  1 /string
                   1317:  REPEAT  THEN  2dup
                   1318:  BEGIN  dup  WHILE  over c@ bl >   WHILE  1 /string
                   1319:  REPEAT  THEN  nip - ;
                   1320: 
1.47      anton    1321: aligned        ( c_addr -- a_addr )    core
1.29      crook    1322: "" @i{a-addr} is the first aligned address greater than or equal to @i{c-addr}.""
1.1       anton    1323: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
                   1324: :
                   1325:  [ cell 1- ] Literal + [ -1 cells ] Literal and ;
                   1326: 
1.47      anton    1327: faligned       ( c_addr -- f_addr )    float   f_aligned
1.29      crook    1328: "" @i{f-addr} is the first float-aligned address greater than or equal to @i{c-addr}.""
1.1       anton    1329: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
                   1330: :
                   1331:  [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
                   1332: 
1.47      anton    1333: >body  ( xt -- a_addr )        core    to_body
1.40      crook    1334: "" Get the address of the body of the word represented by @i{xt} (the address
                   1335: of the word's data field).""
1.1       anton    1336: a_addr = PFA(xt);
                   1337: :
                   1338:     2 cells + ;
                   1339: 
1.35      jwilke   1340: \ threading stuff is currently only interesting if we have a compiler
                   1341: \fhas? standardthreading has? compiler and [IF]
1.28      jwilke   1342: 
1.47      anton    1343: >code-address  ( xt -- c_addr )                gforth  to_code_address
1.29      crook    1344: ""@i{c-addr} is the code address of the word @i{xt}.""
1.1       anton    1345: /* !! This behaves installation-dependently for DOES-words */
                   1346: c_addr = (Address)CODE_ADDRESS(xt);
                   1347: :
                   1348:     @ ;
                   1349: 
1.47      anton    1350: >does-code     ( xt -- a_addr )                gforth  to_does_code
1.58      anton    1351: ""If @i{xt} is the execution token of a child of a @code{DOES>} word,
1.29      crook    1352: @i{a-addr} is the start of the Forth code after the @code{DOES>};
                   1353: Otherwise @i{a-addr} is 0.""
1.1       anton    1354: a_addr = (Cell *)DOES_CODE(xt);
                   1355: :
                   1356:     cell+ @ ;
                   1357: 
1.47      anton    1358: code-address!  ( c_addr xt -- )                gforth  code_address_store
1.29      crook    1359: ""Create a code field with code address @i{c-addr} at @i{xt}.""
1.1       anton    1360: MAKE_CF(xt, c_addr);
1.10      anton    1361: CACHE_FLUSH(xt,(size_t)PFA(0));
1.1       anton    1362: :
                   1363:     ! ;
                   1364: 
1.47      anton    1365: does-code!     ( a_addr xt -- )                gforth  does_code_store
1.58      anton    1366: ""Create a code field at @i{xt} for a child of a @code{DOES>}-word;
                   1367: @i{a-addr} is the start of the Forth code after @code{DOES>}.""
1.1       anton    1368: MAKE_DOES_CF(xt, a_addr);
1.10      anton    1369: CACHE_FLUSH(xt,(size_t)PFA(0));
1.1       anton    1370: :
                   1371:     dodoes: over ! cell+ ! ;
                   1372: 
1.47      anton    1373: does-handler!  ( a_addr -- )   gforth  does_handler_store
1.58      anton    1374: ""Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
                   1375: @i{a-addr} points just behind a @code{DOES>}.""
1.1       anton    1376: MAKE_DOES_HANDLER(a_addr);
1.10      anton    1377: CACHE_FLUSH((caddr_t)a_addr,DOES_HANDLER_SIZE);
1.1       anton    1378: :
                   1379:     drop ;
                   1380: 
1.47      anton    1381: /does-handler  ( -- n )        gforth  slash_does_handler
1.26      crook    1382: ""The size of a @code{DOES>}-handler (includes possible padding).""
1.1       anton    1383: /* !! a constant or environmental query might be better */
                   1384: n = DOES_HANDLER_SIZE;
                   1385: :
                   1386:     2 cells ;
                   1387: 
1.47      anton    1388: threading-method       ( -- n )        gforth  threading_method
1.1       anton    1389: ""0 if the engine is direct threaded. Note that this may change during
                   1390: the lifetime of an image.""
                   1391: #if defined(DOUBLY_INDIRECT)
                   1392: n=2;
                   1393: #else
                   1394: # if defined(DIRECT_THREADED)
                   1395: n=0;
                   1396: # else
                   1397: n=1;
                   1398: # endif
                   1399: #endif
                   1400: :
                   1401:  1 ;
1.28      jwilke   1402: 
1.35      jwilke   1403: \f[THEN]
1.1       anton    1404: 
1.47      anton    1405: key-file       ( wfileid -- n )                gforth  paren_key_file
1.17      pazsan   1406: #ifdef HAS_FILE
1.1       anton    1407: fflush(stdout);
1.12      pazsan   1408: n = key((FILE*)wfileid);
1.17      pazsan   1409: #else
                   1410: n = key(stdin);
                   1411: #endif
1.1       anton    1412: 
1.47      anton    1413: key?-file      ( wfileid -- n )                facility        key_q_file
1.17      pazsan   1414: #ifdef HAS_FILE
1.1       anton    1415: fflush(stdout);
1.12      pazsan   1416: n = key_query((FILE*)wfileid);
1.17      pazsan   1417: #else
                   1418: n = key_query(stdin);
                   1419: #endif
                   1420: 
                   1421: \+os
1.12      pazsan   1422: 
1.47      anton    1423: stdin  ( -- wfileid )  gforth
1.12      pazsan   1424: wfileid = (Cell)stdin;
1.1       anton    1425: 
1.47      anton    1426: stdout ( -- wfileid )  gforth
1.1       anton    1427: wfileid = (Cell)stdout;
                   1428: 
1.47      anton    1429: stderr ( -- wfileid )  gforth
1.1       anton    1430: wfileid = (Cell)stderr;
                   1431: 
1.47      anton    1432: form   ( -- urows ucols )      gforth
1.1       anton    1433: ""The number of lines and columns in the terminal. These numbers may change
                   1434: with the window size.""
                   1435: /* we could block SIGWINCH here to get a consistent size, but I don't
                   1436:  think this is necessary or always beneficial */
                   1437: urows=rows;
                   1438: ucols=cols;
                   1439: 
1.47      anton    1440: flush-icache   ( c_addr u -- ) gforth  flush_icache
1.1       anton    1441: ""Make sure that the instruction cache of the processor (if there is
1.29      crook    1442: one) does not contain stale data at @i{c-addr} and @i{u} bytes
1.1       anton    1443: afterwards. @code{END-CODE} performs a @code{flush-icache}
                   1444: automatically. Caveat: @code{flush-icache} might not work on your
                   1445: installation; this is usually the case if direct threading is not
                   1446: supported on your machine (take a look at your @file{machine.h}) and
                   1447: your machine has a separate instruction cache. In such cases,
                   1448: @code{flush-icache} does nothing instead of flushing the instruction
                   1449: cache.""
                   1450: FLUSH_ICACHE(c_addr,u);
                   1451: 
1.47      anton    1452: (bye)  ( n -- )        gforth  paren_bye
1.1       anton    1453: return (Label *)n;
                   1454: 
1.47      anton    1455: (system)       ( c_addr u -- wretval wior )    gforth  peren_system
1.20      pazsan   1456: #ifndef MSDOS
1.1       anton    1457: int old_tp=terminal_prepped;
                   1458: deprep_terminal();
1.20      pazsan   1459: #endif
1.1       anton    1460: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
                   1461: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1.20      pazsan   1462: #ifndef MSDOS
1.1       anton    1463: if (old_tp)
                   1464:   prep_terminal();
1.20      pazsan   1465: #endif
1.1       anton    1466: 
1.47      anton    1467: getenv ( c_addr1 u1 -- c_addr2 u2 )    gforth
1.29      crook    1468: ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2}
1.24      crook    1469: is the host operating system's expansion of that environment variable. If the
1.29      crook    1470: environment variable does not exist, @i{c-addr2 u2} specifies a string 0 characters
1.24      crook    1471: in length.""
1.46      pazsan   1472: /* close ' to keep fontify happy */
1.1       anton    1473: c_addr2 = getenv(cstr(c_addr1,u1,1));
                   1474: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
                   1475: 
1.56      anton    1476: open-pipe      ( c_addr u wfam -- wfileid wior )       gforth  open_pipe
                   1477: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[wfam]); /* ~ expansion of 1st arg? */
1.1       anton    1478: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
                   1479: 
1.47      anton    1480: close-pipe     ( wfileid -- wretval wior )             gforth  close_pipe
1.1       anton    1481: wretval = pclose((FILE *)wfileid);
                   1482: wior = IOR(wretval==-1);
                   1483: 
1.47      anton    1484: time&date      ( -- nsec nmin nhour nday nmonth nyear )        facility-ext    time_and_date
1.44      crook    1485: ""Report the current time of day. Seconds, minutes and hours are numbered from 0.
                   1486: Months are numbered from 1.""
1.1       anton    1487: struct timeval time1;
                   1488: struct timezone zone1;
                   1489: struct tm *ltime;
                   1490: gettimeofday(&time1,&zone1);
1.51      anton    1491: /* !! Single Unix specification: 
                   1492:    If tzp is not a null pointer, the behaviour is unspecified. */
1.1       anton    1493: ltime=localtime((time_t *)&time1.tv_sec);
                   1494: nyear =ltime->tm_year+1900;
                   1495: nmonth=ltime->tm_mon+1;
                   1496: nday  =ltime->tm_mday;
                   1497: nhour =ltime->tm_hour;
                   1498: nmin  =ltime->tm_min;
                   1499: nsec  =ltime->tm_sec;
                   1500: 
1.47      anton    1501: ms     ( n -- )        facility-ext
1.44      crook    1502: ""Wait at least @i{n} milli-second.""
1.1       anton    1503: struct timeval timeout;
                   1504: timeout.tv_sec=n/1000;
                   1505: timeout.tv_usec=1000*(n%1000);
                   1506: (void)select(0,0,0,0,&timeout);
                   1507: 
1.47      anton    1508: allocate       ( u -- a_addr wior )    memory
1.29      crook    1509: ""Allocate @i{u} address units of contiguous data space. The initial
1.27      crook    1510: contents of the data space is undefined. If the allocation is successful,
1.29      crook    1511: @i{a-addr} is the start address of the allocated region and @i{wior}
                   1512: is 0. If the allocation fails, @i{a-addr} is undefined and @i{wior}
1.52      anton    1513: is a non-zero I/O result code.""
1.1       anton    1514: a_addr = (Cell *)malloc(u?u:1);
                   1515: wior = IOR(a_addr==NULL);
                   1516: 
1.47      anton    1517: free   ( a_addr -- wior )              memory
1.29      crook    1518: ""Return the region of data space starting at @i{a-addr} to the system.
1.52      anton    1519: The region must originally have been obtained using @code{allocate} or
1.29      crook    1520: @code{resize}. If the operational is successful, @i{wior} is 0.
1.52      anton    1521: If the operation fails, @i{wior} is a non-zero I/O result code.""
1.1       anton    1522: free(a_addr);
                   1523: wior = 0;
                   1524: 
1.47      anton    1525: resize ( a_addr1 u -- a_addr2 wior )   memory
1.26      crook    1526: ""Change the size of the allocated area at @i{a-addr1} to @i{u}
1.1       anton    1527: address units, possibly moving the contents to a different
1.27      crook    1528: area. @i{a-addr2} is the address of the resulting area.
1.52      anton    1529: If the operation is successful, @i{wior} is 0.
                   1530: If the operation fails, @i{wior} is a non-zero
1.29      crook    1531: I/O result code. If @i{a-addr1} is 0, Gforth's (but not the Standard)
1.27      crook    1532: @code{resize} @code{allocate}s @i{u} address units.""
1.1       anton    1533: /* the following check is not necessary on most OSs, but it is needed
                   1534:    on SunOS 4.1.2. */
1.46      pazsan   1535: /* close ' to keep fontify happy */
1.1       anton    1536: if (a_addr1==NULL)
                   1537:   a_addr2 = (Cell *)malloc(u);
                   1538: else
                   1539:   a_addr2 = (Cell *)realloc(a_addr1, u);
                   1540: wior = IOR(a_addr2==NULL);     /* !! Define a return code */
                   1541: 
1.47      anton    1542: strerror       ( n -- c_addr u )       gforth
1.1       anton    1543: c_addr = strerror(n);
                   1544: u = strlen(c_addr);
                   1545: 
1.47      anton    1546: strsignal      ( n -- c_addr u )       gforth
1.1       anton    1547: c_addr = strsignal(n);
                   1548: u = strlen(c_addr);
                   1549: 
1.47      anton    1550: call-c ( w -- )        gforth  call_c
1.1       anton    1551: ""Call the C function pointed to by @i{w}. The C function has to
                   1552: access the stack itself. The stack pointers are exported in the global
                   1553: variables @code{SP} and @code{FP}.""
                   1554: /* This is a first attempt at support for calls to C. This may change in
                   1555:    the future */
1.64      anton    1556: IF_fpTOS(fp[0]=fpTOS);
1.1       anton    1557: FP=fp;
                   1558: SP=sp;
                   1559: ((void (*)())w)();
                   1560: sp=SP;
                   1561: fp=FP;
1.64      anton    1562: IF_spTOS(spTOS=sp[0]);
                   1563: IF_fpTOS(fpTOS=fp[0]);
1.1       anton    1564: 
1.15      pazsan   1565: \+
                   1566: \+file
1.1       anton    1567: 
1.47      anton    1568: close-file     ( wfileid -- wior )             file    close_file
1.1       anton    1569: wior = IOR(fclose((FILE *)wfileid)==EOF);
                   1570: 
1.56      anton    1571: open-file      ( c_addr u wfam -- wfileid wior )       file    open_file
                   1572: wfileid = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[wfam]);
1.22      crook    1573: wior =  IOR(wfileid == 0);
1.1       anton    1574: 
1.56      anton    1575: create-file    ( c_addr u wfam -- wfileid wior )       file    create_file
1.1       anton    1576: Cell   fd;
1.56      anton    1577: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[wfam], 0666);
1.1       anton    1578: if (fd != -1) {
1.56      anton    1579:   wfileid = (Cell)fdopen(fd, fileattr[wfam]);
1.22      crook    1580:   wior = IOR(wfileid == 0);
1.1       anton    1581: } else {
1.22      crook    1582:   wfileid = 0;
1.1       anton    1583:   wior = IOR(1);
                   1584: }
                   1585: 
1.47      anton    1586: delete-file    ( c_addr u -- wior )            file    delete_file
1.1       anton    1587: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
                   1588: 
1.47      anton    1589: rename-file    ( c_addr1 u1 c_addr2 u2 -- wior )       file-ext        rename_file
1.29      crook    1590: ""Rename file @i{c_addr1 u1} to new name @i{c_addr2 u2}""
1.1       anton    1591: char *s1=tilde_cstr(c_addr2, u2, 1);
                   1592: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
                   1593: 
1.47      anton    1594: file-position  ( wfileid -- ud wior )  file    file_position
1.1       anton    1595: /* !! use tell and lseek? */
                   1596: ud = LONG2UD(ftell((FILE *)wfileid));
                   1597: wior = IOR(UD2LONG(ud)==-1);
                   1598: 
1.47      anton    1599: reposition-file        ( ud wfileid -- wior )  file    reposition_file
1.1       anton    1600: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
                   1601: 
1.47      anton    1602: file-size      ( wfileid -- ud wior )  file    file_size
1.1       anton    1603: struct stat buf;
                   1604: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
                   1605: ud = LONG2UD(buf.st_size);
                   1606: 
1.47      anton    1607: resize-file    ( ud wfileid -- wior )  file    resize_file
1.1       anton    1608: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
                   1609: 
1.47      anton    1610: read-file      ( c_addr u1 wfileid -- u2 wior )        file    read_file
1.1       anton    1611: /* !! fread does not guarantee enough */
                   1612: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
                   1613: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
                   1614: /* !! is the value of ferror errno-compatible? */
                   1615: if (wior)
                   1616:   clearerr((FILE *)wfileid);
                   1617: 
1.60      pazsan   1618: read-line      ( c_addr u1 wfileid -- u2 flag wior )   file    read_line
                   1619: ""this is only for backward compatibility""
1.1       anton    1620: Cell c;
                   1621: flag=-1;
                   1622: for(u2=0; u2<u1; u2++)
                   1623: {
1.45      anton    1624:    c = getc((FILE *)wfileid);
                   1625:    if (c=='\n') break;
                   1626:    if (c=='\r') {
                   1627:      if ((c = getc((FILE *)wfileid))!='\n')
                   1628:        ungetc(c,(FILE *)wfileid);
                   1629:      break;
                   1630:    }
                   1631:    if (c==EOF) {
1.1       anton    1632:        flag=FLAG(u2!=0);
                   1633:        break;
                   1634:      }
1.45      anton    1635:    c_addr[u2] = (Char)c;
1.1       anton    1636: }
                   1637: wior=FILEIO(ferror((FILE *)wfileid));
                   1638: 
1.15      pazsan   1639: \+
1.1       anton    1640: 
1.47      anton    1641: write-file     ( c_addr u1 wfileid -- wior )   file    write_file
1.1       anton    1642: /* !! fwrite does not guarantee enough */
1.39      pazsan   1643: #ifdef HAS_FILE
1.1       anton    1644: {
                   1645:   UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
                   1646:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
                   1647:   if (wior)
                   1648:     clearerr((FILE *)wfileid);
                   1649: }
1.39      pazsan   1650: #else
                   1651: TYPE(c_addr, u1);
                   1652: #endif
1.17      pazsan   1653: 
1.47      anton    1654: emit-file      ( c wfileid -- wior )   gforth  emit_file
1.17      pazsan   1655: #ifdef HAS_FILE
1.1       anton    1656: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
                   1657: if (wior)
                   1658:   clearerr((FILE *)wfileid);
1.17      pazsan   1659: #else
1.36      pazsan   1660: PUTC(c);
1.17      pazsan   1661: #endif
1.1       anton    1662: 
1.15      pazsan   1663: \+file
1.1       anton    1664: 
1.47      anton    1665: flush-file     ( wfileid -- wior )             file-ext        flush_file
1.1       anton    1666: wior = IOR(fflush((FILE *) wfileid)==EOF);
                   1667: 
1.56      anton    1668: file-status    ( c_addr u -- wfam wior )       file-ext        file_status
1.1       anton    1669: char *filename=tilde_cstr(c_addr, u, 1);
                   1670: if (access (filename, F_OK) != 0) {
1.56      anton    1671:   wfam=0;
1.1       anton    1672:   wior=IOR(1);
                   1673: }
                   1674: else if (access (filename, R_OK | W_OK) == 0) {
1.56      anton    1675:   wfam=2; /* r/w */
1.1       anton    1676:   wior=0;
                   1677: }
                   1678: else if (access (filename, R_OK) == 0) {
1.56      anton    1679:   wfam=0; /* r/o */
1.1       anton    1680:   wior=0;
                   1681: }
                   1682: else if (access (filename, W_OK) == 0) {
1.56      anton    1683:   wfam=4; /* w/o */
1.1       anton    1684:   wior=0;
                   1685: }
                   1686: else {
1.56      anton    1687:   wfam=1; /* well, we cannot access the file, but better deliver a legal
1.1       anton    1688:            access mode (r/o bin), so we get a decent error later upon open. */
                   1689:   wior=0;
                   1690: }
                   1691: 
1.15      pazsan   1692: \+
                   1693: \+floating
1.1       anton    1694: 
                   1695: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
                   1696: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
                   1697: 
1.47      anton    1698: d>f    ( d -- r )              float   d_to_f
1.1       anton    1699: #ifdef BUGGY_LONG_LONG
                   1700: extern double ldexp(double x, int exp);
                   1701: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
                   1702: #else
                   1703: r = d;
                   1704: #endif
                   1705: 
1.47      anton    1706: f>d    ( r -- d )              float   f_to_d
1.1       anton    1707: #ifdef BUGGY_LONG_LONG
1.21      anton    1708: d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
1.1       anton    1709: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
                   1710: #else
                   1711: d = r;
                   1712: #endif
                   1713: 
1.47      anton    1714: f!     ( r f_addr -- ) float   f_store
1.52      anton    1715: ""Store @i{r} into the float at address @i{f-addr}.""
1.1       anton    1716: *f_addr = r;
                   1717: 
1.47      anton    1718: f@     ( f_addr -- r ) float   f_fetch
1.52      anton    1719: ""@i{r} is the float at address @i{f-addr}.""
1.1       anton    1720: r = *f_addr;
                   1721: 
1.47      anton    1722: df@    ( df_addr -- r )        float-ext       d_f_fetch
1.52      anton    1723: ""Fetch the double-precision IEEE floating-point value @i{r} from the address @i{df-addr}.""
1.1       anton    1724: #ifdef IEEE_FP
                   1725: r = *df_addr;
                   1726: #else
                   1727: !! df@
                   1728: #endif
                   1729: 
1.47      anton    1730: df!    ( r df_addr -- )        float-ext       d_f_store
1.52      anton    1731: ""Store @i{r} as double-precision IEEE floating-point value to the
                   1732: address @i{df-addr}.""
1.1       anton    1733: #ifdef IEEE_FP
                   1734: *df_addr = r;
                   1735: #else
                   1736: !! df!
                   1737: #endif
                   1738: 
1.47      anton    1739: sf@    ( sf_addr -- r )        float-ext       s_f_fetch
1.52      anton    1740: ""Fetch the single-precision IEEE floating-point value @i{r} from the address @i{sf-addr}.""
1.1       anton    1741: #ifdef IEEE_FP
                   1742: r = *sf_addr;
                   1743: #else
                   1744: !! sf@
                   1745: #endif
                   1746: 
1.47      anton    1747: sf!    ( r sf_addr -- )        float-ext       s_f_store
1.52      anton    1748: ""Store @i{r} as single-precision IEEE floating-point value to the
                   1749: address @i{sf-addr}.""
1.1       anton    1750: #ifdef IEEE_FP
                   1751: *sf_addr = r;
                   1752: #else
                   1753: !! sf!
                   1754: #endif
                   1755: 
1.47      anton    1756: f+     ( r1 r2 -- r3 ) float   f_plus
1.1       anton    1757: r3 = r1+r2;
                   1758: 
1.47      anton    1759: f-     ( r1 r2 -- r3 ) float   f_minus
1.1       anton    1760: r3 = r1-r2;
                   1761: 
1.47      anton    1762: f*     ( r1 r2 -- r3 ) float   f_star
1.1       anton    1763: r3 = r1*r2;
                   1764: 
1.47      anton    1765: f/     ( r1 r2 -- r3 ) float   f_slash
1.1       anton    1766: r3 = r1/r2;
                   1767: 
1.47      anton    1768: f**    ( r1 r2 -- r3 ) float-ext       f_star_star
1.26      crook    1769: ""@i{r3} is @i{r1} raised to the @i{r2}th power.""
1.1       anton    1770: r3 = pow(r1,r2);
                   1771: 
1.47      anton    1772: fnegate        ( r1 -- r2 )    float   f_negate
1.1       anton    1773: r2 = - r1;
                   1774: 
1.47      anton    1775: fdrop  ( r -- )                float   f_drop
1.1       anton    1776: 
1.47      anton    1777: fdup   ( r -- r r )    float   f_dupe
1.1       anton    1778: 
1.47      anton    1779: fswap  ( r1 r2 -- r2 r1 )      float   f_swap
1.1       anton    1780: 
1.47      anton    1781: fover  ( r1 r2 -- r1 r2 r1 )   float   f_over
1.1       anton    1782: 
1.47      anton    1783: frot   ( r1 r2 r3 -- r2 r3 r1 )        float   f_rote
1.1       anton    1784: 
1.47      anton    1785: fnip   ( r1 r2 -- r2 ) gforth  f_nip
1.1       anton    1786: 
1.47      anton    1787: ftuck  ( r1 r2 -- r2 r1 r2 )   gforth  f_tuck
1.1       anton    1788: 
1.47      anton    1789: float+ ( f_addr1 -- f_addr2 )  float   float_plus
1.52      anton    1790: ""@code{1 floats +}.""
1.1       anton    1791: f_addr2 = f_addr1+1;
                   1792: 
1.47      anton    1793: floats ( n1 -- n2 )    float
1.52      anton    1794: ""@i{n2} is the number of address units of @i{n1} floats.""
1.1       anton    1795: n2 = n1*sizeof(Float);
                   1796: 
1.47      anton    1797: floor  ( r1 -- r2 )    float
1.26      crook    1798: ""Round towards the next smaller integral value, i.e., round toward negative infinity.""
1.1       anton    1799: /* !! unclear wording */
                   1800: r2 = floor(r1);
                   1801: 
1.47      anton    1802: fround ( r1 -- r2 )    float   f_round
1.26      crook    1803: ""Round to the nearest integral value.""
1.1       anton    1804: /* !! unclear wording */
                   1805: #ifdef HAVE_RINT
                   1806: r2 = rint(r1);
                   1807: #else
                   1808: r2 = floor(r1+0.5);
                   1809: /* !! This is not quite true to the rounding rules given in the standard */
                   1810: #endif
                   1811: 
1.47      anton    1812: fmax   ( r1 r2 -- r3 ) float   f_max
1.1       anton    1813: if (r1<r2)
                   1814:   r3 = r2;
                   1815: else
                   1816:   r3 = r1;
                   1817: 
1.47      anton    1818: fmin   ( r1 r2 -- r3 ) float   f_min
1.1       anton    1819: if (r1<r2)
                   1820:   r3 = r1;
                   1821: else
                   1822:   r3 = r2;
                   1823: 
1.47      anton    1824: represent      ( r c_addr u -- n f1 f2 )       float
1.1       anton    1825: char *sig;
                   1826: int flag;
                   1827: int decpt;
                   1828: sig=ecvt(r, u, &decpt, &flag);
                   1829: n=(r==0 ? 1 : decpt);
                   1830: f1=FLAG(flag!=0);
1.21      anton    1831: f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
1.1       anton    1832: memmove(c_addr,sig,u);
                   1833: 
1.47      anton    1834: >float ( c_addr u -- flag )    float   to_float
1.56      anton    1835: ""Actual stack effect: ( c_addr u -- r t | f ).  Attempt to convert the
                   1836: character string @i{c-addr u} to internal floating-point
                   1837: representation. If the string represents a valid floating-point number
                   1838: @i{r} is placed on the floating-point stack and @i{flag} is
                   1839: true. Otherwise, @i{flag} is false. A string of blanks is a special
                   1840: case and represents the floating-point number 0.""
1.1       anton    1841: /* real signature: c_addr u -- r t / f */
                   1842: Float r;
                   1843: char *number=cstr(c_addr, u, 1);
                   1844: char *endconv;
1.42      pazsan   1845: int sign = 0;
                   1846: if(number[0]=='-') {
                   1847:    sign = 1;
                   1848:    number++;
                   1849:    u--;
                   1850: }
1.21      anton    1851: while(isspace((unsigned)(number[--u])) && u>0);
1.1       anton    1852: switch(number[u])
                   1853: {
                   1854:    case 'd':
                   1855:    case 'D':
                   1856:    case 'e':
                   1857:    case 'E':  break;
                   1858:    default :  u++; break;
                   1859: }
                   1860: number[u]='\0';
                   1861: r=strtod(number,&endconv);
                   1862: if((flag=FLAG(!(Cell)*endconv)))
                   1863: {
1.64      anton    1864:    IF_fpTOS(fp[0] = fpTOS);
1.1       anton    1865:    fp += -1;
1.64      anton    1866:    fpTOS = sign ? -r : r;
1.1       anton    1867: }
                   1868: else if(*endconv=='d' || *endconv=='D')
                   1869: {
                   1870:    *endconv='E';
                   1871:    r=strtod(number,&endconv);
                   1872:    if((flag=FLAG(!(Cell)*endconv)))
                   1873:      {
1.64      anton    1874:        IF_fpTOS(fp[0] = fpTOS);
1.1       anton    1875:        fp += -1;
1.64      anton    1876:        fpTOS = sign ? -r : r;
1.1       anton    1877:      }
                   1878: }
                   1879: 
1.47      anton    1880: fabs   ( r1 -- r2 )    float-ext       f_abs
1.1       anton    1881: r2 = fabs(r1);
                   1882: 
1.47      anton    1883: facos  ( r1 -- r2 )    float-ext       f_a_cos
1.1       anton    1884: r2 = acos(r1);
                   1885: 
1.47      anton    1886: fasin  ( r1 -- r2 )    float-ext       f_a_sine
1.1       anton    1887: r2 = asin(r1);
                   1888: 
1.47      anton    1889: fatan  ( r1 -- r2 )    float-ext       f_a_tan
1.1       anton    1890: r2 = atan(r1);
                   1891: 
1.47      anton    1892: fatan2 ( r1 r2 -- r3 ) float-ext       f_a_tan_two
1.26      crook    1893: ""@i{r1/r2}=tan(@i{r3}). ANS Forth does not require, but probably
1.1       anton    1894: intends this to be the inverse of @code{fsincos}. In gforth it is.""
                   1895: r3 = atan2(r1,r2);
                   1896: 
1.47      anton    1897: fcos   ( r1 -- r2 )    float-ext       f_cos
1.1       anton    1898: r2 = cos(r1);
                   1899: 
1.47      anton    1900: fexp   ( r1 -- r2 )    float-ext       f_e_x_p
1.1       anton    1901: r2 = exp(r1);
                   1902: 
1.47      anton    1903: fexpm1 ( r1 -- r2 )    float-ext       f_e_x_p_m_one
1.1       anton    1904: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
                   1905: #ifdef HAVE_EXPM1
1.3       pazsan   1906: extern double
                   1907: #ifdef NeXT
                   1908:               const
                   1909: #endif
                   1910:                     expm1(double);
1.1       anton    1911: r2 = expm1(r1);
                   1912: #else
                   1913: r2 = exp(r1)-1.;
                   1914: #endif
                   1915: 
1.47      anton    1916: fln    ( r1 -- r2 )    float-ext       f_l_n
1.1       anton    1917: r2 = log(r1);
                   1918: 
1.47      anton    1919: flnp1  ( r1 -- r2 )    float-ext       f_l_n_p_one
1.1       anton    1920: ""@i{r2}=ln(@i{r1}+1)""
                   1921: #ifdef HAVE_LOG1P
1.3       pazsan   1922: extern double
                   1923: #ifdef NeXT
                   1924:               const
                   1925: #endif
                   1926:                     log1p(double);
1.1       anton    1927: r2 = log1p(r1);
                   1928: #else
                   1929: r2 = log(r1+1.);
                   1930: #endif
                   1931: 
1.47      anton    1932: flog   ( r1 -- r2 )    float-ext       f_log
1.26      crook    1933: ""The decimal logarithm.""
1.1       anton    1934: r2 = log10(r1);
                   1935: 
1.47      anton    1936: falog  ( r1 -- r2 )    float-ext       f_a_log
1.1       anton    1937: ""@i{r2}=10**@i{r1}""
                   1938: extern double pow10(double);
                   1939: r2 = pow10(r1);
                   1940: 
1.47      anton    1941: fsin   ( r1 -- r2 )    float-ext       f_sine
1.1       anton    1942: r2 = sin(r1);
                   1943: 
1.47      anton    1944: fsincos        ( r1 -- r2 r3 ) float-ext       f_sine_cos
1.1       anton    1945: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
                   1946: r2 = sin(r1);
                   1947: r3 = cos(r1);
                   1948: 
1.47      anton    1949: fsqrt  ( r1 -- r2 )    float-ext       f_square_root
1.1       anton    1950: r2 = sqrt(r1);
                   1951: 
1.47      anton    1952: ftan   ( r1 -- r2 )    float-ext       f_tan
1.1       anton    1953: r2 = tan(r1);
                   1954: :
                   1955:  fsincos f/ ;
                   1956: 
1.47      anton    1957: fsinh  ( r1 -- r2 )    float-ext       f_cinch
1.1       anton    1958: r2 = sinh(r1);
                   1959: :
                   1960:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
                   1961: 
1.47      anton    1962: fcosh  ( r1 -- r2 )    float-ext       f_cosh
1.1       anton    1963: r2 = cosh(r1);
                   1964: :
                   1965:  fexp fdup 1/f f+ f2/ ;
                   1966: 
1.47      anton    1967: ftanh  ( r1 -- r2 )    float-ext       f_tan_h
1.1       anton    1968: r2 = tanh(r1);
                   1969: :
                   1970:  f2* fexpm1 fdup 2. d>f f+ f/ ;
                   1971: 
1.47      anton    1972: fasinh ( r1 -- r2 )    float-ext       f_a_cinch
1.1       anton    1973: r2 = asinh(r1);
                   1974: :
                   1975:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
                   1976: 
1.47      anton    1977: facosh ( r1 -- r2 )    float-ext       f_a_cosh
1.1       anton    1978: r2 = acosh(r1);
                   1979: :
                   1980:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
                   1981: 
1.47      anton    1982: fatanh ( r1 -- r2 )    float-ext       f_a_tan_h
1.1       anton    1983: r2 = atanh(r1);
                   1984: :
                   1985:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
                   1986:  r> IF  fnegate  THEN ;
                   1987: 
1.47      anton    1988: sfloats        ( n1 -- n2 )    float-ext       s_floats
1.52      anton    1989: ""@i{n2} is the number of address units of @i{n1}
1.29      crook    1990: single-precision IEEE floating-point numbers.""
1.1       anton    1991: n2 = n1*sizeof(SFloat);
                   1992: 
1.47      anton    1993: dfloats        ( n1 -- n2 )    float-ext       d_floats
1.52      anton    1994: ""@i{n2} is the number of address units of @i{n1}
1.29      crook    1995: double-precision IEEE floating-point numbers.""
1.1       anton    1996: n2 = n1*sizeof(DFloat);
                   1997: 
1.47      anton    1998: sfaligned      ( c_addr -- sf_addr )   float-ext       s_f_aligned
1.52      anton    1999: ""@i{sf-addr} is the first single-float-aligned address greater
1.29      crook    2000: than or equal to @i{c-addr}.""
1.1       anton    2001: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
                   2002: :
                   2003:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
                   2004: 
1.47      anton    2005: dfaligned      ( c_addr -- df_addr )   float-ext       d_f_aligned
1.52      anton    2006: ""@i{df-addr} is the first double-float-aligned address greater
1.29      crook    2007: than or equal to @i{c-addr}.""
1.1       anton    2008: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
                   2009: :
                   2010:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
                   2011: 
                   2012: \ The following words access machine/OS/installation-dependent
                   2013: \   Gforth internals
                   2014: \ !! how about environmental queries DIRECT-THREADED,
                   2015: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
                   2016: 
                   2017: \ local variable implementation primitives
1.15      pazsan   2018: \+
                   2019: \+glocals
1.1       anton    2020: 
1.47      anton    2021: @local#        ( -- w )        gforth  fetch_local_number
1.1       anton    2022: w = *(Cell *)(lp+(Cell)NEXT_INST);
                   2023: INC_IP(1);
                   2024: 
1.47      anton    2025: @local0        ( -- w )        new     fetch_local_zero
1.1       anton    2026: w = *(Cell *)(lp+0*sizeof(Cell));
                   2027: 
1.47      anton    2028: @local1        ( -- w )        new     fetch_local_four
1.1       anton    2029: w = *(Cell *)(lp+1*sizeof(Cell));
                   2030: 
1.47      anton    2031: @local2        ( -- w )        new     fetch_local_eight
1.1       anton    2032: w = *(Cell *)(lp+2*sizeof(Cell));
                   2033: 
1.47      anton    2034: @local3        ( -- w )        new     fetch_local_twelve
1.1       anton    2035: w = *(Cell *)(lp+3*sizeof(Cell));
                   2036: 
1.15      pazsan   2037: \+floating
1.1       anton    2038: 
1.47      anton    2039: f@local#       ( -- r )        gforth  f_fetch_local_number
1.1       anton    2040: r = *(Float *)(lp+(Cell)NEXT_INST);
                   2041: INC_IP(1);
                   2042: 
1.47      anton    2043: f@local0       ( -- r )        new     f_fetch_local_zero
1.1       anton    2044: r = *(Float *)(lp+0*sizeof(Float));
                   2045: 
1.47      anton    2046: f@local1       ( -- r )        new     f_fetch_local_eight
1.1       anton    2047: r = *(Float *)(lp+1*sizeof(Float));
                   2048: 
1.15      pazsan   2049: \+
1.1       anton    2050: 
1.47      anton    2051: laddr# ( -- c_addr )   gforth  laddr_number
1.1       anton    2052: /* this can also be used to implement lp@ */
                   2053: c_addr = (Char *)(lp+(Cell)NEXT_INST);
                   2054: INC_IP(1);
                   2055: 
1.47      anton    2056: lp+!#  ( -- )  gforth  lp_plus_store_number
1.1       anton    2057: ""used with negative immediate values it allocates memory on the
                   2058: local stack, a positive immediate argument drops memory from the local
                   2059: stack""
                   2060: lp += (Cell)NEXT_INST;
                   2061: INC_IP(1);
                   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;
                   2170: sp=(SYSCALL(Cell(*)(Cell *, void *))u)(sp, &FP);
                   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.46      pazsan   2178: wdirid = (Cell)opendir(tilde_cstr(c_addr, u, 1));
                   2179: wior =  IOR(wdirid == 0);
                   2180: 
1.47      anton    2181: read-dir       ( c_addr u1 wdirid -- u2 flag wior )    gforth  read_dir
1.46      pazsan   2182: struct dirent * dent;
                   2183: dent = readdir((DIR *)wdirid);
                   2184: wior = 0;
                   2185: flag = -1;
                   2186: if(dent == NULL) {
                   2187:   u2 = 0;
                   2188:   flag = 0;
                   2189: } else {
                   2190:   u2 = strlen(dent->d_name);
                   2191:   if(u2 > u1)
                   2192:     u2 = u1;
                   2193:   memmove(c_addr, dent->d_name, u2);
                   2194: }
                   2195: 
1.47      anton    2196: close-dir      ( wdirid -- wior )      gforth  close_dir
1.46      pazsan   2197: wior = IOR(closedir((DIR *)wdirid));
                   2198: 
1.47      anton    2199: filename-match ( c_addr1 u1 c_addr2 u2 -- flag )       gforth  match_file
1.46      pazsan   2200: char * string = cstr(c_addr1, u1, 1);
                   2201: char * pattern = cstr(c_addr2, u2, 0);
                   2202: flag = FLAG(!fnmatch(pattern, string, 0));
                   2203: 
                   2204: \+
1.34      jwilke   2205: 
1.47      anton    2206: newline        ( -- c_addr u ) gforth
1.45      anton    2207: ""String containing the newline sequence of the host OS""
                   2208: char newline[] = {
                   2209: #ifdef unix
                   2210: '\n'
                   2211: #else
                   2212: '\r','\n'
                   2213: #endif
                   2214: };
                   2215: c_addr=newline;
                   2216: u=sizeof(newline);
1.49      pazsan   2217: :
                   2218:  "newline count ;
1.54      pazsan   2219: Create "newline e? crlf [IF] 2 c, $0D c, [ELSE] 1 c, [THEN] $0A c,
                   2220: 
                   2221: \+os
1.51      anton    2222: 
                   2223: utime  ( -- dtime )    gforth
                   2224: ""Report the current time in microseconds since some epoch.""
                   2225: struct timeval time1;
                   2226: gettimeofday(&time1,NULL);
                   2227: dtime = timeval2us(&time1);
                   2228: 
                   2229: cputime ( -- duser dsystem ) gforth
                   2230: ""duser and dsystem are the respective user- and system-level CPU
                   2231: times used since the start of the Forth system (excluding child
                   2232: processes), in microseconds (the granularity may be much larger,
                   2233: however).  On platforms without the getrusage call, it reports elapsed
                   2234: time (since some epoch) for duser and 0 for dsystem.""
                   2235: #ifdef HAVE_GETRUSAGE
                   2236: struct rusage usage;
                   2237: getrusage(RUSAGE_SELF, &usage);
                   2238: duser = timeval2us(&usage.ru_utime);
                   2239: dsystem = timeval2us(&usage.ru_stime);
                   2240: #else
                   2241: struct timeval time1;
                   2242: gettimeofday(&time1,NULL);
                   2243: duser = timeval2us(&time1);
1.57      anton    2244: #ifndef BUGGY_LONG_LONG
1.51      anton    2245: dsystem = (DCell)0;
1.57      anton    2246: #else
                   2247: dsystem=(DCell){0,0};
                   2248: #endif
1.51      anton    2249: #endif
                   2250: 
1.54      pazsan   2251: \+
                   2252: 
                   2253: \+floating
                   2254: 
1.51      anton    2255: v*     ( f_addr1 nstride1 f_addr2 nstride2 ucount -- r ) gforth v_star
                   2256: ""dot-product: r=v1*v2.  The first element of v1 is at f_addr1, the
                   2257: next at f_addr1+nstride1 and so on (similar for v2). Both vectors have
                   2258: ucount elements.""
                   2259: for (r=0.; ucount>0; ucount--) {
                   2260:   r += *f_addr1 * *f_addr2;
                   2261:   f_addr1 = (Float *)(((Address)f_addr1)+nstride1);
                   2262:   f_addr2 = (Float *)(((Address)f_addr2)+nstride2);
                   2263: }
1.54      pazsan   2264: :
                   2265:  >r swap 2swap swap 0e r> 0 ?DO
                   2266:      dup f@ over + 2swap dup f@ f* f+ over + 2swap
                   2267:  LOOP 2drop 2drop ; 
1.51      anton    2268: 
                   2269: faxpy  ( ra f_x nstridex f_y nstridey ucount -- )      gforth
                   2270: ""vy=ra*vx+vy""
                   2271: for (; ucount>0; ucount--) {
                   2272:   *f_y += ra * *f_x;
                   2273:   f_x = (Float *)(((Address)f_x)+nstridex);
                   2274:   f_y = (Float *)(((Address)f_y)+nstridey);
                   2275: }
1.54      pazsan   2276: :
                   2277:  >r swap 2swap swap r> 0 ?DO
                   2278:      fdup dup f@ f* over + 2swap dup f@ f+ dup f! over + 2swap
                   2279:  LOOP 2drop 2drop fdrop ;
1.60      pazsan   2280: 
                   2281: \+
                   2282: 
                   2283: \+file
                   2284: 
                   2285: (read-line)    ( c_addr u1 wfileid -- u2 flag u3 wior )        file    paren_read_line
                   2286: Cell c;
                   2287: flag=-1;
                   2288: u3=0;
                   2289: for(u2=0; u2<u1; u2++)
                   2290: {
                   2291:    c = getc((FILE *)wfileid);
                   2292:    u3++;
                   2293:    if (c=='\n') break;
                   2294:    if (c=='\r') {
                   2295:      if ((c = getc((FILE *)wfileid))!='\n')
                   2296:        ungetc(c,(FILE *)wfileid);
                   2297:      else
                   2298:        u3++;
                   2299:      break;
                   2300:    }
                   2301:    if (c==EOF) {
                   2302:        flag=FLAG(u2!=0);
                   2303:        break;
                   2304:      }
                   2305:    c_addr[u2] = (Char)c;
                   2306: }
                   2307: wior=FILEIO(ferror((FILE *)wfileid));
1.54      pazsan   2308: 
                   2309: \+

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