Annotation of gforth/prim, revision 1.52

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

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