Annotation of gforth/prim, revision 1.66

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

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