Annotation of gforth/prim, revision 1.14

1.1       anton       1: \ Gforth primitives
                      2: 
                      3: \ Copyright (C) 1995,1996 Free Software Foundation, Inc.
                      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: \ 
                     29: \ forth name   stack effect    category        [pronunciation]
                     30: \ [""glossary entry""]
                     31: \ C code
                     32: \ [:
                     33: \ Forth code]
                     34: \ 
                     35: \ prims2x is pedantic about tabs vs. blanks. The fields of the first
                     36: \ line of a primitive are separated by tabs, the stack items in a
                     37: \ stack effect by blanks.
                     38: \
                     39: \ Both pronounciation and stack items (in the stack effect) must
                     40: \ conform to the C name syntax or the C compiler will complain.
                     41: \ 
                     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: 
                    106: noop   --              gforth
                    107: ;
                    108: :
                    109:  ;
                    110: 
                    111: lit    -- w            gforth
                    112: w = (Cell)NEXT_INST;
                    113: INC_IP(1);
                    114: :
                    115:  r> dup @ swap cell+ >r ;
                    116: 
                    117: execute                xt --           core
                    118: ip=IP;
                    119: IF_TOS(TOS = sp[0]);
                    120: EXEC(xt);
                    121: 
                    122: perform                a_addr --       gforth
                    123: ""equivalent to @code{@ execute}""
                    124: /* and pfe */
                    125: ip=IP;
                    126: IF_TOS(TOS = sp[0]);
                    127: EXEC(*(Xt *)a_addr);
                    128: :
                    129:  @ execute ;
                    130: 
1.6       jwilke    131: \+has? glocals [IF]
1.1       anton     132: 
                    133: branch-lp+!#   --      gforth  branch_lp_plus_store_number
                    134: /* this will probably not be used */
                    135: branch_adjust_lp:
                    136: lp += (Cell)(IP[1]);
                    137: goto branch;
                    138: 
                    139: \+[THEN]
                    140: 
                    141: branch --              gforth
                    142: branch:
                    143: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
                    144: NEXT_P0;
                    145: :
                    146:  r> dup @ + >r ;
                    147: 
                    148: \ condbranch(forthname,restline,code,forthcode)
                    149: \ this is non-syntactical: code must open a brace that is closed by the macro
                    150: define(condbranch,
                    151: $1     $2
                    152: $3     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
                    153:         NEXT_P0;
                    154:        NEXT;
                    155: }
                    156: else
                    157:     INC_IP(1);
                    158: $4
                    159: 
1.6       jwilke    160: \+has? glocals [IF]
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: 
                    168: \+[THEN]
                    169: )
                    170: 
                    171: condbranch(?branch,f --                f83     question_branch,
                    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.6       jwilke    184: \+has? xconds [IF]
1.1       anton     185: 
                    186: ?dup-?branch   f -- f  new     question_dupe_question_branch
                    187: ""The run-time procedure compiled by @code{?DUP-IF}.""
                    188: if (f==0) {
                    189:   sp++;
                    190:   IF_TOS(TOS = sp[0]);
                    191:   ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
                    192:   NEXT_P0;
                    193:   NEXT;
                    194: }
                    195: else
                    196:   INC_IP(1);
                    197: 
                    198: ?dup-0=-?branch        f --    new     question_dupe_zero_equals_question_branch
                    199: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
                    200: /* the approach taken here of declaring the word as having the stack
                    201: effect ( f -- ) and correcting for it in the branch-taken case costs a
                    202: few cycles in that case, but is easy to convert to a CONDBRANCH
                    203: invocation */
                    204: if (f!=0) {
                    205:   sp--;
                    206:   ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
                    207:   NEXT_P0;
                    208:   NEXT;
                    209: }
                    210: else
                    211:   INC_IP(1);
                    212: 
                    213: \+[THEN]
                    214: 
                    215: condbranch((next),--           cmFORTH paren_next,
                    216: if ((*rp)--) {
                    217: ,:
                    218:  r> r> dup 1- >r
                    219:  IF dup @ + >r ELSE cell+ >r THEN ;)
                    220: 
                    221: condbranch((loop),--           gforth  paren_loop,
                    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: 
                    231: condbranch((+loop),n --                gforth  paren_plus_loop,
                    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.6       jwilke    253: \+has? xconds [IF]
1.1       anton     254: 
                    255: condbranch((-loop),u --                gforth  paren_minus_loop,
                    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: 
                    268: condbranch((s+loop),n --               gforth  paren_symmetric_plus_loop,
                    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: 
                    289: \+[THEN]
                    290: 
                    291: unloop         --      core
                    292: rp += 2;
                    293: :
                    294:  r> rdrop rdrop >r ;
                    295: 
                    296: (for)  ncount --               cmFORTH         paren_for
                    297: /* or (for) = >r -- collides with unloop! */
                    298: *--rp = 0;
                    299: *--rp = ncount;
                    300: :
                    301:  r> swap 0 >r >r >r ;
                    302: 
                    303: (do)   nlimit nstart --                gforth          paren_do
                    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: 
                    310: (?do)  nlimit nstart --        gforth  paren_question_do
                    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.6       jwilke    328: \+has? xconds [IF]
1.1       anton     329: 
                    330: (+do)  nlimit nstart --        gforth  paren_plus_do
                    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: 
                    350: (u+do) ulimit ustart --        gforth  paren_u_plus_do
                    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: 
                    370: (-do)  nlimit nstart --        gforth  paren_minus_do
                    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: 
                    390: (u-do) ulimit ustart --        gforth  paren_u_minus_do
                    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: 
                    410: \+[THEN]
                    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.1       anton     415: i      -- n            core
                    416: n = *rp;
                    417: :
1.5       jwilke    418: \ rp@ cell+ @ ;
                    419:   r> r> tuck >r >r ;
1.1       anton     420: 
                    421: i'     -- w            gforth          i_tick
                    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: 
                    429: j      -- n            core
                    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: 
                    436: k      -- n            gforth
                    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.1       anton     442: 
                    443: \ digit is high-level: 0/0%
                    444: 
                    445: move   c_from c_to ucount --           core
                    446: memmove(c_to,c_from,ucount);
                    447: /* make an Ifdef for bsd and others? */
                    448: :
                    449:  >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
                    450: 
                    451: cmove  c_from c_to u --        string
                    452: while (u-- > 0)
                    453:   *c_to++ = *c_from++;
                    454: :
                    455:  bounds ?DO  dup c@ I c! 1+  LOOP  drop ;
                    456: 
                    457: cmove> c_from c_to u --        string  c_move_up
                    458: while (u-- > 0)
                    459:   c_to[u] = c_from[u];
                    460: :
                    461:  dup 0= IF  drop 2drop exit  THEN
                    462:  rot over + -rot bounds swap 1-
                    463:  DO  1- dup c@ I c!  -1 +LOOP  drop ;
                    464: 
                    465: fill   c_addr u c --   core
                    466: memset(c_addr,c,u);
                    467: :
                    468:  -rot bounds
                    469:  ?DO  dup I c!  LOOP  drop ;
                    470: 
                    471: compare                c_addr1 u1 c_addr2 u2 -- n      string
                    472: ""Compare the strings lexicographically. If they are equal, n is 0; if
                    473: the first string is smaller, n is -1; if the first string is larger, n
                    474: is 1. Currently this is based on the machine's character
                    475: comparison. In the future, this may change to considering the current
                    476: locale and its collation order.""
                    477: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
                    478: if (n==0)
                    479:   n = u1-u2;
                    480: if (n<0)
                    481:   n = -1;
                    482: else if (n>0)
                    483:   n = 1;
                    484: :
                    485:  rot 2dup - >r min swap -text dup
                    486:  IF    rdrop
                    487:  ELSE  drop r@ 0>
                    488:        IF    rdrop -1
                    489:        ELSE  r> 1 and
                    490:        THEN
                    491:  THEN ;
                    492: 
                    493: -text          c_addr1 u c_addr2 -- n  new     dash_text
                    494: n = memcmp(c_addr1, c_addr2, u);
                    495: if (n<0)
                    496:   n = -1;
                    497: else if (n>0)
                    498:   n = 1;
                    499: :
                    500:  swap bounds
                    501:  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
                    502:  ELSE  c@ I c@ - unloop  THEN  -text-flag ;
                    503: : -text-flag ( n -- -1/0/1 )
                    504:  dup 0< IF  drop -1  ELSE  0>  1 and  THEN  ;
                    505: 
                    506: toupper        c1 -- c2        gforth
                    507: c2 = toupper(c1);
                    508: :
                    509:  dup [char] a - [ char z char a - 1 + ] Literal u<  bl and - ;
                    510: 
                    511: capscomp       c_addr1 u c_addr2 -- n  new
                    512: n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
                    513: if (n<0)
                    514:   n = -1;
                    515: else if (n>0)
                    516:   n = 1;
                    517: :
                    518:  swap bounds
                    519:  ?DO  dup c@ I c@ <>
                    520:      IF  dup c@ toupper I c@ toupper =
                    521:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
                    522:  ELSE  c@ toupper I c@ toupper - unloop  THEN  -text-flag ;
                    523: 
                    524: -trailing      c_addr u1 -- c_addr u2          string  dash_trailing
                    525: u2 = u1;
1.4       anton     526: while (u2>0 && c_addr[u2-1] == ' ')
1.1       anton     527:   u2--;
                    528: :
                    529:  BEGIN  1- 2dup + c@ bl =  WHILE
                    530:         dup  0= UNTIL  ELSE  1+  THEN ;
                    531: 
                    532: /string                c_addr1 u1 n -- c_addr2 u2      string  slash_string
                    533: c_addr2 = c_addr1+n;
                    534: u2 = u1-n;
                    535: :
                    536:  tuck - >r + r> dup 0< IF  - 0  THEN ;
                    537: 
                    538: +      n1 n2 -- n              core    plus
                    539: n = n1+n2;
                    540: 
                    541: \ PFE-0.9.14 has it differently, but the next release will have it as follows
                    542: under+ n1 n2 n3 -- n n2        gforth  under_plus
                    543: ""add @var{n3} to @var{n1} (giving @var{n})""
                    544: n = n1+n3;
                    545: :
                    546:  rot + swap ;
                    547: 
                    548: -      n1 n2 -- n              core    minus
                    549: n = n1-n2;
                    550: :
                    551:  negate + ;
                    552: 
                    553: negate n1 -- n2                core
                    554: /* use minus as alias */
                    555: n2 = -n1;
                    556: :
                    557:  invert 1+ ;
                    558: 
                    559: 1+     n1 -- n2                core            one_plus
                    560: n2 = n1+1;
                    561: :
                    562:  1 + ;
                    563: 
                    564: 1-     n1 -- n2                core            one_minus
                    565: n2 = n1-1;
                    566: :
                    567:  1 - ;
                    568: 
                    569: max    n1 n2 -- n      core
                    570: if (n1<n2)
                    571:   n = n2;
                    572: else
                    573:   n = n1;
                    574: :
                    575:  2dup < IF swap THEN drop ;
                    576: 
                    577: min    n1 n2 -- n      core
                    578: if (n1<n2)
                    579:   n = n1;
                    580: else
                    581:   n = n2;
                    582: :
                    583:  2dup > IF swap THEN drop ;
                    584: 
                    585: abs    n1 -- n2        core
                    586: if (n1<0)
                    587:   n2 = -n1;
                    588: else
                    589:   n2 = n1;
                    590: :
                    591:  dup 0< IF negate THEN ;
                    592: 
                    593: *      n1 n2 -- n              core    star
                    594: n = n1*n2;
                    595: :
                    596:  um* drop ;
                    597: 
                    598: /      n1 n2 -- n              core    slash
                    599: n = n1/n2;
                    600: :
                    601:  /mod nip ;
                    602: 
                    603: mod    n1 n2 -- n              core
                    604: n = n1%n2;
                    605: :
                    606:  /mod drop ;
                    607: 
                    608: /mod   n1 n2 -- n3 n4          core            slash_mod
                    609: n4 = n1/n2;
                    610: n3 = n1%n2; /* !! is this correct? look into C standard! */
                    611: :
                    612:  >r s>d r> fm/mod ;
                    613: 
                    614: 2*     n1 -- n2                core            two_star
                    615: n2 = 2*n1;
                    616: :
                    617:  dup + ;
                    618: 
                    619: 2/     n1 -- n2                core            two_slash
                    620: /* !! is this still correct? */
                    621: n2 = n1>>1;
                    622: :
                    623:  dup MINI and IF 1 ELSE 0 THEN
                    624:  [ bits/byte cell * 1- ] literal 
1.5       jwilke    625:  0 DO 2* swap dup 2* >r MINI and 
1.1       anton     626:      IF 1 ELSE 0 THEN or r> swap
                    627:  LOOP nip ;
                    628: 
                    629: fm/mod d1 n1 -- n2 n3          core            f_m_slash_mod
                    630: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
                    631: #ifdef BUGGY_LONG_LONG
                    632: DCell r = fmdiv(d1,n1);
                    633: n2=r.hi;
                    634: n3=r.lo;
                    635: #else
                    636: /* assumes that the processor uses either floored or symmetric division */
                    637: n3 = d1/n1;
                    638: n2 = d1%n1;
                    639: /* note that this 1%-3>0 is optimized by the compiler */
                    640: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
                    641:   n3--;
                    642:   n2+=n1;
                    643: }
                    644: #endif
                    645: :
                    646:  dup >r dup 0< IF  negate >r dnegate r>  THEN
                    647:  over       0< IF  tuck + swap  THEN
                    648:  um/mod
                    649:  r> 0< IF  swap negate swap  THEN ;
                    650: 
                    651: sm/rem d1 n1 -- n2 n3          core            s_m_slash_rem
                    652: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
                    653: #ifdef BUGGY_LONG_LONG
                    654: DCell r = smdiv(d1,n1);
                    655: n2=r.hi;
                    656: n3=r.lo;
                    657: #else
                    658: /* assumes that the processor uses either floored or symmetric division */
                    659: n3 = d1/n1;
                    660: n2 = d1%n1;
                    661: /* note that this 1%-3<0 is optimized by the compiler */
                    662: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
                    663:   n3++;
                    664:   n2-=n1;
                    665: }
                    666: #endif
                    667: :
                    668:  over >r dup >r abs -rot
                    669:  dabs rot um/mod
                    670:  r> r@ xor 0< IF       negate       THEN
                    671:  r>        0< IF  swap negate swap  THEN ;
                    672: 
                    673: m*     n1 n2 -- d              core    m_star
                    674: #ifdef BUGGY_LONG_LONG
                    675: d = mmul(n1,n2);
                    676: #else
                    677: d = (DCell)n1 * (DCell)n2;
                    678: #endif
                    679: :
                    680:  2dup      0< and >r
                    681:  2dup swap 0< and >r
                    682:  um* r> - r> - ;
                    683: 
                    684: um*    u1 u2 -- ud             core    u_m_star
                    685: /* use u* as alias */
                    686: #ifdef BUGGY_LONG_LONG
                    687: ud = ummul(u1,u2);
                    688: #else
                    689: ud = (UDCell)u1 * (UDCell)u2;
                    690: #endif
                    691: :
                    692:    >r >r 0 0 r> r> [ 8 cells ] literal 0
                    693:    DO
                    694:        over >r dup >r 0< and d2*+ drop
                    695:        r> 2* r> swap
                    696:    LOOP 2drop ;
                    697: : d2*+ ( ud n -- ud+n c )
                    698:    over MINI
                    699:    and >r >r 2dup d+ swap r> + swap r> ;
                    700: 
                    701: um/mod ud u1 -- u2 u3          core    u_m_slash_mod
                    702: #ifdef BUGGY_LONG_LONG
                    703: UDCell r = umdiv(ud,u1);
                    704: u2=r.hi;
                    705: u3=r.lo;
                    706: #else
                    707: u3 = ud/u1;
                    708: u2 = ud%u1;
                    709: #endif
                    710: :
                    711:    0 swap [ 8 cells 1 + ] literal 0
1.5       jwilke    712:    ?DO /modstep
1.1       anton     713:    LOOP drop swap 1 rshift or swap ;
                    714: : /modstep ( ud c R: u -- ud-?u c R: u )
1.5       jwilke    715:    >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN  d2*+ r> ;
1.1       anton     716: : d2*+ ( ud n -- ud+n c )
                    717:    over MINI
                    718:    and >r >r 2dup d+ swap r> + swap r> ;
                    719: 
                    720: m+     d1 n -- d2              double          m_plus
                    721: #ifdef BUGGY_LONG_LONG
                    722: d2.lo = d1.lo+n;
                    723: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
                    724: #else
                    725: d2 = d1+n;
                    726: #endif
                    727: :
                    728:  s>d d+ ;
                    729: 
                    730: d+     d1 d2 -- d              double  d_plus
                    731: #ifdef BUGGY_LONG_LONG
                    732: d.lo = d1.lo+d2.lo;
                    733: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
                    734: #else
                    735: d = d1+d2;
                    736: #endif
                    737: :
                    738:  rot + >r tuck + swap over u> r> swap - ;
                    739: 
                    740: d-     d1 d2 -- d              double          d_minus
                    741: #ifdef BUGGY_LONG_LONG
                    742: d.lo = d1.lo - d2.lo;
                    743: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
                    744: #else
                    745: d = d1-d2;
                    746: #endif
                    747: :
                    748:  dnegate d+ ;
                    749: 
                    750: dnegate        d1 -- d2                double
                    751: /* use dminus as alias */
                    752: #ifdef BUGGY_LONG_LONG
                    753: d2 = dnegate(d1);
                    754: #else
                    755: d2 = -d1;
                    756: #endif
                    757: :
                    758:  invert swap negate tuck 0= - ;
                    759: 
                    760: d2*    d1 -- d2                double          d_two_star
                    761: #ifdef BUGGY_LONG_LONG
                    762: d2.lo = d1.lo<<1;
                    763: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
                    764: #else
                    765: d2 = 2*d1;
                    766: #endif
                    767: :
                    768:  2dup d+ ;
                    769: 
                    770: d2/    d1 -- d2                double          d_two_slash
                    771: #ifdef BUGGY_LONG_LONG
                    772: d2.hi = d1.hi>>1;
                    773: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
                    774: #else
                    775: d2 = d1>>1;
                    776: #endif
                    777: :
                    778:  dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
                    779:  r> IF  [ 1 8 cells 1- lshift ] Literal + THEN  swap ;
                    780: 
                    781: and    w1 w2 -- w              core
                    782: w = w1&w2;
                    783: 
                    784: or     w1 w2 -- w              core
                    785: w = w1|w2;
                    786: :
                    787:  invert swap invert and invert ;
                    788: 
                    789: xor    w1 w2 -- w              core
                    790: w = w1^w2;
                    791: 
                    792: invert w1 -- w2                core
                    793: w2 = ~w1;
                    794: :
                    795:  MAXU xor ;
                    796: 
                    797: rshift u1 n -- u2              core
                    798:   u2 = u1>>n;
                    799: :
                    800:     0 ?DO 2/ MAXI and LOOP ;
                    801: 
                    802: lshift u1 n -- u2              core
                    803:   u2 = u1<<n;
                    804: :
                    805:     0 ?DO 2* LOOP ;
                    806: 
                    807: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
                    808: define(comparisons,
                    809: $1=    $2 -- f         $6      $3equals
                    810: f = FLAG($4==$5);
                    811: :
                    812:     [ char $1x char 0 = [IF]
                    813:        ] IF false ELSE true THEN [
                    814:     [ELSE]
                    815:        ] xor 0= [
                    816:     [THEN] ] ;
                    817: 
                    818: $1<>   $2 -- f         $7      $3different
                    819: f = FLAG($4!=$5);
                    820: :
                    821:     [ char $1x char 0 = [IF]
                    822:        ] IF true ELSE false THEN [
                    823:     [ELSE]
                    824:        ] xor 0<> [
                    825:     [THEN] ] ;
                    826: 
                    827: $1<    $2 -- f         $8      $3less
                    828: f = FLAG($4<$5);
                    829: :
                    830:     [ char $1x char 0 = [IF]
                    831:        ] MINI and 0<> [
                    832:     [ELSE] char $1x char u = [IF]
                    833:        ]   2dup xor 0<  IF nip ELSE - THEN 0<  [
                    834:        [ELSE]
                    835:            ] MINI xor >r MINI xor r> u< [
                    836:        [THEN]
                    837:     [THEN] ] ;
                    838: 
                    839: $1>    $2 -- f         $9      $3greater
                    840: f = FLAG($4>$5);
                    841: :
                    842:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    843:     $1< ;
                    844: 
                    845: $1<=   $2 -- f         gforth  $3less_or_equal
                    846: f = FLAG($4<=$5);
                    847: :
                    848:     $1> 0= ;
                    849: 
                    850: $1>=   $2 -- f         gforth  $3greater_or_equal
                    851: f = FLAG($4>=$5);
                    852: :
                    853:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    854:     $1<= ;
                    855: 
                    856: )
                    857: 
                    858: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
                    859: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
                    860: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
                    861: 
                    862: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
                    863: define(dcomparisons,
                    864: $1=    $2 -- f         $6      $3equals
                    865: #ifdef BUGGY_LONG_LONG
                    866: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
                    867: #else
                    868: f = FLAG($4==$5);
                    869: #endif
                    870: 
                    871: $1<>   $2 -- f         $7      $3different
                    872: #ifdef BUGGY_LONG_LONG
                    873: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
                    874: #else
                    875: f = FLAG($4!=$5);
                    876: #endif
                    877: 
                    878: $1<    $2 -- f         $8      $3less
                    879: #ifdef BUGGY_LONG_LONG
                    880: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
                    881: #else
                    882: f = FLAG($4<$5);
                    883: #endif
                    884: 
                    885: $1>    $2 -- f         $9      $3greater
                    886: #ifdef BUGGY_LONG_LONG
                    887: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
                    888: #else
                    889: f = FLAG($4>$5);
                    890: #endif
                    891: 
                    892: $1<=   $2 -- f         gforth  $3less_or_equal
                    893: #ifdef BUGGY_LONG_LONG
                    894: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
                    895: #else
                    896: f = FLAG($4<=$5);
                    897: #endif
                    898: 
                    899: $1>=   $2 -- f         gforth  $3greater_or_equal
                    900: #ifdef BUGGY_LONG_LONG
                    901: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
                    902: #else
                    903: f = FLAG($4>=$5);
                    904: #endif
                    905: 
                    906: )
                    907: 
1.6       jwilke    908: \+has? dcomps [IF]
1.1       anton     909: 
                    910: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
                    911: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
                    912: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
                    913: 
                    914: \+[THEN]
                    915: 
                    916: within u1 u2 u3 -- f           core-ext
                    917: f = FLAG(u1-u2 < u3-u2);
                    918: :
                    919:  over - >r - r> u< ;
                    920: 
                    921: sp@    -- a_addr               gforth          spat
                    922: a_addr = sp+1;
                    923: 
                    924: sp!    a_addr --               gforth          spstore
                    925: sp = a_addr;
                    926: /* works with and without TOS caching */
                    927: 
                    928: rp@    -- a_addr               gforth          rpat
                    929: a_addr = rp;
                    930: 
                    931: rp!    a_addr --               gforth          rpstore
                    932: rp = a_addr;
                    933: 
1.6       jwilke    934: \+has? floating [IF]
1.1       anton     935: 
                    936: fp@    -- f_addr       gforth  fp_fetch
                    937: f_addr = fp;
                    938: 
                    939: fp!    f_addr --       gforth  fp_store
                    940: fp = f_addr;
                    941: 
                    942: \+[THEN]
                    943: 
                    944: ;s     --              gforth  semis
                    945: ip = (Xt *)(*rp++);
                    946: NEXT_P0;
                    947: 
                    948: >r     w --            core    to_r
                    949: *--rp = w;
                    950: :
                    951:  (>r) ;
                    952: : (>r)  rp@ cell+ @ rp@ ! rp@ cell+ ! ;
                    953: 
                    954: r>     -- w            core    r_from
                    955: w = *rp++;
                    956: :
                    957:  rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
                    958: Create (rdrop) ' ;s A,
                    959: 
                    960: rdrop  --              gforth
                    961: rp++;
                    962: :
                    963:  r> r> drop >r ;
                    964: 
                    965: 2>r    w1 w2 --        core-ext        two_to_r
                    966: *--rp = w1;
                    967: *--rp = w2;
                    968: :
                    969:  swap r> swap >r swap >r >r ;
                    970: 
                    971: 2r>    -- w1 w2        core-ext        two_r_from
                    972: w2 = *rp++;
                    973: w1 = *rp++;
                    974: :
                    975:  r> r> swap r> swap >r swap ;
                    976: 
                    977: 2r@    -- w1 w2        core-ext        two_r_fetch
                    978: w2 = rp[0];
                    979: w1 = rp[1];
                    980: :
                    981:  i' j ;
                    982: 
                    983: 2rdrop --              gforth  two_r_drop
                    984: rp+=2;
                    985: :
                    986:  r> r> drop r> drop >r ;
                    987: 
                    988: over   w1 w2 -- w1 w2 w1               core
                    989: :
                    990:  sp@ cell+ @ ;
                    991: 
                    992: drop   w --            core
                    993: :
                    994:  IF THEN ;
                    995: 
                    996: swap   w1 w2 -- w2 w1          core
                    997: :
                    998:  >r (swap) ! r> (swap) @ ;
                    999: Variable (swap)
                   1000: 
                   1001: dup    w -- w w                core
                   1002: :
                   1003:  sp@ @ ;
                   1004: 
                   1005: rot    w1 w2 w3 -- w2 w3 w1    core    rote
                   1006: :
                   1007: [ defined? (swap) [IF] ]
                   1008:     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
                   1009: Variable (rot)
                   1010: [ELSE] ]
                   1011:     >r swap r> swap ;
                   1012: [THEN]
                   1013: 
                   1014: -rot   w1 w2 w3 -- w3 w1 w2    gforth  not_rote
                   1015: :
                   1016:  rot rot ;
                   1017: 
                   1018: nip    w1 w2 -- w2             core-ext
                   1019: :
1.6       jwilke   1020:  swap drop ;
1.1       anton    1021: 
                   1022: tuck   w1 w2 -- w2 w1 w2       core-ext
                   1023: :
                   1024:  swap over ;
                   1025: 
                   1026: ?dup   w -- w                  core    question_dupe
                   1027: if (w!=0) {
                   1028:   IF_TOS(*sp-- = w;)
                   1029: #ifndef USE_TOS
                   1030:   *--sp = w;
                   1031: #endif
                   1032: }
                   1033: :
                   1034:  dup IF dup THEN ;
                   1035: 
                   1036: pick   u -- w                  core-ext
                   1037: w = sp[u+1];
                   1038: :
                   1039:  1+ cells sp@ + @ ;
                   1040: 
                   1041: 2drop  w1 w2 --                core    two_drop
                   1042: :
                   1043:  drop drop ;
                   1044: 
                   1045: 2dup   w1 w2 -- w1 w2 w1 w2    core    two_dupe
                   1046: :
                   1047:  over over ;
                   1048: 
                   1049: 2over  w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2        core    two_over
                   1050: :
                   1051:  3 pick 3 pick ;
                   1052: 
                   1053: 2swap  w1 w2 w3 w4 -- w3 w4 w1 w2      core    two_swap
                   1054: :
                   1055:  rot >r rot r> ;
                   1056: 
                   1057: 2rot   w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2  double-ext      two_rote
                   1058: :
                   1059:  >r >r 2swap r> r> 2swap ;
                   1060: 
                   1061: 2nip   w1 w2 w3 w4 -- w3 w4    gforth  two_nip
                   1062: :
                   1063:  2swap 2drop ;
                   1064: 
                   1065: 2tuck  w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4        gforth  two_tuck
                   1066: :
                   1067:  2swap 2over ;
                   1068: 
                   1069: \ toggle is high-level: 0.11/0.42%
                   1070: 
                   1071: @      a_addr -- w             core    fetch
                   1072: w = *a_addr;
                   1073: 
                   1074: !      w a_addr --             core    store
                   1075: *a_addr = w;
                   1076: 
                   1077: +!     n a_addr --             core    plus_store
                   1078: *a_addr += n;
                   1079: :
                   1080:  tuck @ + swap ! ;
                   1081: 
                   1082: c@     c_addr -- c             core    cfetch
                   1083: c = *c_addr;
                   1084: :
                   1085: [ bigendian [IF] ]
                   1086:     [ cell>bit 4 = [IF] ]
                   1087:        dup [ 0 cell - ] Literal and @ swap 1 and
                   1088:        IF  $FF and  ELSE  8>>  THEN  ;
                   1089:     [ [ELSE] ]
                   1090:        dup [ cell 1- ] literal and
                   1091:        tuck - @ swap [ cell 1- ] literal xor
                   1092:        0 ?DO 8>> LOOP $FF and
                   1093:     [ [THEN] ]
                   1094: [ [ELSE] ]
                   1095:     [ cell>bit 4 = [IF] ]
                   1096:        dup [ 0 cell - ] Literal and @ swap 1 and
                   1097:        IF  8>>  ELSE  $FF and  THEN
                   1098:     [ [ELSE] ]
                   1099:        dup [ cell  1- ] literal and 
                   1100:        tuck - @ swap
                   1101:        0 ?DO 8>> LOOP 255 and
                   1102:     [ [THEN] ]
                   1103: [ [THEN] ]
                   1104: ;
                   1105: : 8>> 2/ 2/ 2/ 2/  2/ 2/ 2/ 2/ ;
                   1106: 
                   1107: c!     c c_addr --             core    cstore
                   1108: *c_addr = c;
                   1109: :
                   1110: [ bigendian [IF] ]
                   1111:     [ cell>bit 4 = [IF] ]
                   1112:        tuck 1 and IF  $FF and  ELSE  8<<  THEN >r
                   1113:        dup -2 and @ over 1 and cells masks + @ and
                   1114:        r> or swap -2 and ! ;
                   1115:        Create masks $00FF , $FF00 ,
                   1116:     [ELSE] ]
                   1117:        dup [ cell 1- ] literal and dup 
                   1118:        [ cell 1- ] literal xor >r
                   1119:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
                   1120:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1121:     [THEN]
                   1122: [ELSE] ]
                   1123:     [ cell>bit 4 = [IF] ]
                   1124:        tuck 1 and IF  8<<  ELSE  $FF and  THEN >r
                   1125:        dup -2 and @ over 1 and cells masks + @ and
                   1126:        r> or swap -2 and ! ;
                   1127:        Create masks $FF00 , $00FF ,
                   1128:     [ELSE] ]
                   1129:        dup [ cell 1- ] literal and dup >r
                   1130:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
                   1131:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1132:     [THEN]
                   1133: [THEN]
                   1134: : 8<< 2* 2* 2* 2*  2* 2* 2* 2* ;
                   1135: 
                   1136: 2!     w1 w2 a_addr --         core    two_store
                   1137: a_addr[0] = w2;
                   1138: a_addr[1] = w1;
                   1139: :
                   1140:  tuck ! cell+ ! ;
                   1141: 
                   1142: 2@     a_addr -- w1 w2         core    two_fetch
                   1143: w2 = a_addr[0];
                   1144: w1 = a_addr[1];
                   1145: :
                   1146:  dup cell+ @ swap @ ;
                   1147: 
                   1148: cell+  a_addr1 -- a_addr2      core    cell_plus
                   1149: a_addr2 = a_addr1+1;
                   1150: :
                   1151:  cell + ;
                   1152: 
                   1153: cells  n1 -- n2                core
                   1154: n2 = n1 * sizeof(Cell);
                   1155: :
                   1156:  [ cell
                   1157:  2/ dup [IF] ] 2* [ [THEN]
                   1158:  2/ dup [IF] ] 2* [ [THEN]
                   1159:  2/ dup [IF] ] 2* [ [THEN]
                   1160:  2/ dup [IF] ] 2* [ [THEN]
                   1161:  drop ] ;
                   1162: 
                   1163: char+  c_addr1 -- c_addr2      core    care_plus
                   1164: c_addr2 = c_addr1 + 1;
                   1165: :
                   1166:  1+ ;
                   1167: 
                   1168: (chars)                n1 -- n2        gforth  paren_cares
                   1169: n2 = n1 * sizeof(Char);
                   1170: :
                   1171:  ;
                   1172: 
                   1173: count  c_addr1 -- c_addr2 u    core
                   1174: u = *c_addr1;
                   1175: c_addr2 = c_addr1+1;
                   1176: :
                   1177:  dup 1+ swap c@ ;
                   1178: 
                   1179: (f83find)      c_addr u f83name1 -- f83name2   new     paren_f83find
1.13      pazsan   1180: for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
1.1       anton    1181:   if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1182:       memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1183:     break;
                   1184: f83name2=f83name1;
                   1185: :
                   1186:     BEGIN  dup WHILE  (find-samelen)  dup  WHILE
                   1187:        >r 2dup r@ cell+ char+ capscomp  0=
                   1188:        IF  2drop r>  EXIT  THEN
                   1189:        r> @
                   1190:     REPEAT  THEN  nip nip ;
                   1191: : (find-samelen) ( u f83name1 -- u f83name2/0 )
                   1192:     BEGIN  2dup cell+ c@ $1F and <> WHILE  @  dup 0= UNTIL  THEN ;
                   1193: 
1.6       jwilke   1194: \+has? hash [IF]
1.1       anton    1195: 
                   1196: (hashfind)     c_addr u a_addr -- f83name2     new     paren_hashfind
1.13      pazsan   1197: struct F83Name *f83name1;
1.1       anton    1198: f83name2=NULL;
                   1199: while(a_addr != NULL)
                   1200: {
1.13      pazsan   1201:    f83name1=(struct F83Name *)(a_addr[1]);
1.1       anton    1202:    a_addr=(Cell *)(a_addr[0]);
                   1203:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1204:        memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1205:      {
                   1206:        f83name2=f83name1;
                   1207:        break;
                   1208:      }
                   1209: }
                   1210: :
                   1211:  BEGIN  dup  WHILE
                   1212:         2@ >r >r dup r@ cell+ c@ $1F and =
                   1213:         IF  2dup r@ cell+ char+ capscomp 0=
                   1214:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   1215:        rdrop r>
                   1216:  REPEAT nip nip ;
                   1217: 
                   1218: (tablefind)    c_addr u a_addr -- f83name2     new     paren_tablefind
                   1219: ""A case-sensitive variant of @code{(hashfind)}""
1.13      pazsan   1220: struct F83Name *f83name1;
1.1       anton    1221: f83name2=NULL;
                   1222: while(a_addr != NULL)
                   1223: {
1.13      pazsan   1224:    f83name1=(struct F83Name *)(a_addr[1]);
1.1       anton    1225:    a_addr=(Cell *)(a_addr[0]);
                   1226:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
                   1227:        memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
                   1228:      {
                   1229:        f83name2=f83name1;
                   1230:        break;
                   1231:      }
                   1232: }
                   1233: :
                   1234:  BEGIN  dup  WHILE
                   1235:         2@ >r >r dup r@ cell+ c@ $1F and =
                   1236:         IF  2dup r@ cell+ char+ -text 0=
                   1237:            IF  2drop r> rdrop  EXIT  THEN  THEN
                   1238:        rdrop r>
                   1239:  REPEAT nip nip ;
                   1240: 
                   1241: (hashkey)      c_addr u1 -- u2         gforth  paren_hashkey
                   1242: u2=0;
                   1243: while(u1--)
                   1244:    u2+=(Cell)toupper(*c_addr++);
                   1245: :
                   1246:  0 -rot bounds ?DO  I c@ toupper +  LOOP ;
                   1247: 
                   1248: (hashkey1)     c_addr u ubits -- ukey          gforth  paren_hashkey1
                   1249: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
                   1250: /* this hash function rotates the key at every step by rot bits within
                   1251:    ubits bits and xors it with the character. This function does ok in
                   1252:    the chi-sqare-test.  Rot should be <=7 (preferably <=5) for
                   1253:    ASCII strings (larger if ubits is large), and should share no
                   1254:    divisors with ubits.
                   1255: */
                   1256: 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];
                   1257: Char *cp = c_addr;
                   1258: for (ukey=0; cp<c_addr+u; cp++)
                   1259:     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot))) 
                   1260:             ^ toupper(*cp))
                   1261:            & ((1<<ubits)-1));
                   1262: :
                   1263:  dup rot-values + c@ over 1 swap lshift 1- >r
                   1264:  tuck - 2swap r> 0 2swap bounds
                   1265:  ?DO  dup 4 pick lshift swap 3 pick rshift or
                   1266:       I c@ toupper xor
                   1267:       over and  LOOP
                   1268:  nip nip nip ;
                   1269: Create rot-values
                   1270:   5 c, 0 c, 1 c, 2 c, 3 c,  4 c, 5 c, 5 c, 5 c, 5 c,
                   1271:   3 c, 5 c, 5 c, 5 c, 5 c,  7 c, 5 c, 5 c, 5 c, 5 c,
                   1272:   7 c, 5 c, 5 c, 5 c, 5 c,  6 c, 5 c, 5 c, 5 c, 5 c,
                   1273:   7 c, 5 c, 5 c,
                   1274: 
                   1275: \+[THEN]
                   1276: 
                   1277: (parse-white)  c_addr1 u1 -- c_addr2 u2        gforth  paren_parse_white
                   1278: /* use !isgraph instead of isspace? */
                   1279: Char *endp = c_addr1+u1;
                   1280: while (c_addr1<endp && isspace(*c_addr1))
                   1281:   c_addr1++;
                   1282: if (c_addr1<endp) {
                   1283:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
                   1284:     ;
                   1285:   u2 = c_addr1-c_addr2;
                   1286: }
                   1287: else {
                   1288:   c_addr2 = c_addr1;
                   1289:   u2 = 0;
                   1290: }
                   1291: :
                   1292:  BEGIN  dup  WHILE  over c@ bl <=  WHILE  1 /string
                   1293:  REPEAT  THEN  2dup
                   1294:  BEGIN  dup  WHILE  over c@ bl >   WHILE  1 /string
                   1295:  REPEAT  THEN  nip - ;
                   1296: 
                   1297: aligned                c_addr -- a_addr        core
                   1298: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
                   1299: :
                   1300:  [ cell 1- ] Literal + [ -1 cells ] Literal and ;
                   1301: 
                   1302: faligned       c_addr -- f_addr        float   f_aligned
                   1303: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
                   1304: :
                   1305:  [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
                   1306: 
                   1307: >body          xt -- a_addr    core    to_body
                   1308: a_addr = PFA(xt);
                   1309: :
                   1310:     2 cells + ;
                   1311: 
                   1312: >code-address          xt -- c_addr            gforth  to_code_address
                   1313: ""c_addr is the code address of the word xt""
                   1314: /* !! This behaves installation-dependently for DOES-words */
                   1315: c_addr = (Address)CODE_ADDRESS(xt);
                   1316: :
                   1317:     @ ;
                   1318: 
                   1319: >does-code     xt -- a_addr            gforth  to_does_code
                   1320: ""If xt ist the execution token of a defining-word-defined word,
                   1321: a_addr is the start of the Forth code after the DOES>;
                   1322: Otherwise a_addr is 0.""
                   1323: a_addr = (Cell *)DOES_CODE(xt);
                   1324: :
                   1325:     cell+ @ ;
                   1326: 
                   1327: code-address!          c_addr xt --            gforth  code_address_store
                   1328: ""Creates a code field with code address c_addr at xt""
                   1329: MAKE_CF(xt, c_addr);
1.10      anton    1330: CACHE_FLUSH(xt,(size_t)PFA(0));
1.1       anton    1331: :
                   1332:     ! ;
                   1333: 
                   1334: does-code!     a_addr xt --            gforth  does_code_store
                   1335: ""creates a code field at xt for a defining-word-defined word; a_addr
                   1336: is the start of the Forth code after DOES>""
                   1337: MAKE_DOES_CF(xt, a_addr);
1.10      anton    1338: CACHE_FLUSH(xt,(size_t)PFA(0));
1.1       anton    1339: :
                   1340:     dodoes: over ! cell+ ! ;
                   1341: 
                   1342: does-handler!  a_addr --       gforth  does_handler_store
                   1343: ""creates a DOES>-handler at address a_addr. a_addr usually points
                   1344: just behind a DOES>.""
                   1345: MAKE_DOES_HANDLER(a_addr);
1.10      anton    1346: CACHE_FLUSH((caddr_t)a_addr,DOES_HANDLER_SIZE);
1.1       anton    1347: :
                   1348:     drop ;
                   1349: 
                   1350: /does-handler  -- n    gforth  slash_does_handler
                   1351: ""the size of a does-handler (includes possible padding)""
                   1352: /* !! a constant or environmental query might be better */
                   1353: n = DOES_HANDLER_SIZE;
                   1354: :
                   1355:     2 cells ;
                   1356: 
                   1357: threading-method       -- n    gforth  threading_method
                   1358: ""0 if the engine is direct threaded. Note that this may change during
                   1359: the lifetime of an image.""
                   1360: #if defined(DOUBLY_INDIRECT)
                   1361: n=2;
                   1362: #else
                   1363: # if defined(DIRECT_THREADED)
                   1364: n=0;
                   1365: # else
                   1366: n=1;
                   1367: # endif
                   1368: #endif
                   1369: :
                   1370:  1 ;
                   1371: 
1.6       jwilke   1372: \+has? os [IF]
1.1       anton    1373: 
1.12      pazsan   1374: key-file       wfileid -- n            gforth  paren_key_file
1.1       anton    1375: fflush(stdout);
1.12      pazsan   1376: n = key((FILE*)wfileid);
1.1       anton    1377: 
1.12      pazsan   1378: key?-file      wfileid -- n            facility        key_q_file
1.1       anton    1379: fflush(stdout);
1.12      pazsan   1380: n = key_query((FILE*)wfileid);
                   1381: 
                   1382: stdin  -- wfileid      gforth
                   1383: wfileid = (Cell)stdin;
1.1       anton    1384: 
                   1385: stdout -- wfileid      gforth
                   1386: wfileid = (Cell)stdout;
                   1387: 
                   1388: stderr -- wfileid      gforth
                   1389: wfileid = (Cell)stderr;
                   1390: 
                   1391: form   -- urows ucols  gforth
                   1392: ""The number of lines and columns in the terminal. These numbers may change
                   1393: with the window size.""
                   1394: /* we could block SIGWINCH here to get a consistent size, but I don't
                   1395:  think this is necessary or always beneficial */
                   1396: urows=rows;
                   1397: ucols=cols;
                   1398: 
                   1399: flush-icache   c_addr u --     gforth  flush_icache
                   1400: ""Make sure that the instruction cache of the processor (if there is
                   1401: one) does not contain stale data at @var{c_addr} and @var{u} bytes
                   1402: afterwards. @code{END-CODE} performs a @code{flush-icache}
                   1403: automatically. Caveat: @code{flush-icache} might not work on your
                   1404: installation; this is usually the case if direct threading is not
                   1405: supported on your machine (take a look at your @file{machine.h}) and
                   1406: your machine has a separate instruction cache. In such cases,
                   1407: @code{flush-icache} does nothing instead of flushing the instruction
                   1408: cache.""
                   1409: FLUSH_ICACHE(c_addr,u);
                   1410: 
                   1411: (bye)  n --    gforth  paren_bye
                   1412: return (Label *)n;
                   1413: 
                   1414: (system)       c_addr u -- wretval wior        gforth  peren_system
                   1415: int old_tp=terminal_prepped;
                   1416: deprep_terminal();
                   1417: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
                   1418: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
                   1419: if (old_tp)
                   1420:   prep_terminal();
                   1421: 
                   1422: getenv c_addr1 u1 -- c_addr2 u2        gforth
                   1423: c_addr2 = getenv(cstr(c_addr1,u1,1));
                   1424: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
                   1425: 
                   1426: open-pipe      c_addr u ntype -- wfileid wior  gforth  open_pipe
                   1427: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
                   1428: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
                   1429: 
                   1430: close-pipe     wfileid -- wretval wior         gforth  close_pipe
                   1431: wretval = pclose((FILE *)wfileid);
                   1432: wior = IOR(wretval==-1);
                   1433: 
                   1434: time&date      -- nsec nmin nhour nday nmonth nyear    facility-ext    time_and_date
                   1435: struct timeval time1;
                   1436: struct timezone zone1;
                   1437: struct tm *ltime;
                   1438: gettimeofday(&time1,&zone1);
                   1439: ltime=localtime((time_t *)&time1.tv_sec);
                   1440: nyear =ltime->tm_year+1900;
                   1441: nmonth=ltime->tm_mon+1;
                   1442: nday  =ltime->tm_mday;
                   1443: nhour =ltime->tm_hour;
                   1444: nmin  =ltime->tm_min;
                   1445: nsec  =ltime->tm_sec;
                   1446: 
                   1447: ms     n --    facility-ext
                   1448: struct timeval timeout;
                   1449: timeout.tv_sec=n/1000;
                   1450: timeout.tv_usec=1000*(n%1000);
                   1451: (void)select(0,0,0,0,&timeout);
                   1452: 
                   1453: allocate       u -- a_addr wior        memory
                   1454: a_addr = (Cell *)malloc(u?u:1);
                   1455: wior = IOR(a_addr==NULL);
                   1456: 
                   1457: free           a_addr -- wior          memory
                   1458: free(a_addr);
                   1459: wior = 0;
                   1460: 
                   1461: resize         a_addr1 u -- a_addr2 wior       memory
                   1462: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
                   1463: address units, possibly moving the contents to a different
                   1464: area. @i{a_addr2} is the address of the resulting area. If
                   1465: @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
                   1466: @code{allocate}s @i{u} address units.""
                   1467: /* the following check is not necessary on most OSs, but it is needed
                   1468:    on SunOS 4.1.2. */
                   1469: if (a_addr1==NULL)
                   1470:   a_addr2 = (Cell *)malloc(u);
                   1471: else
                   1472:   a_addr2 = (Cell *)realloc(a_addr1, u);
                   1473: wior = IOR(a_addr2==NULL);     /* !! Define a return code */
                   1474: 
                   1475: strerror       n -- c_addr u   gforth
                   1476: c_addr = strerror(n);
                   1477: u = strlen(c_addr);
                   1478: 
                   1479: strsignal      n -- c_addr u   gforth
                   1480: c_addr = strsignal(n);
                   1481: u = strlen(c_addr);
                   1482: 
                   1483: call-c w --    gforth  call_c
                   1484: ""Call the C function pointed to by @i{w}. The C function has to
                   1485: access the stack itself. The stack pointers are exported in the global
                   1486: variables @code{SP} and @code{FP}.""
                   1487: /* This is a first attempt at support for calls to C. This may change in
                   1488:    the future */
                   1489: IF_FTOS(fp[0]=FTOS);
                   1490: FP=fp;
                   1491: SP=sp;
                   1492: ((void (*)())w)();
                   1493: sp=SP;
                   1494: fp=FP;
                   1495: IF_TOS(TOS=sp[0]);
                   1496: IF_FTOS(FTOS=fp[0]);
                   1497: 
1.6       jwilke   1498: \+[THEN] ( has? os ) has? file [IF]
1.1       anton    1499: 
                   1500: close-file     wfileid -- wior         file    close_file
                   1501: wior = IOR(fclose((FILE *)wfileid)==EOF);
                   1502: 
                   1503: open-file      c_addr u ntype -- w2 wior       file    open_file
                   1504: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1.14    ! pazsan   1505: #if defined(GO32) && defined(MSDOS)
        !          1506: if(w2 && !(ntype & 1))
        !          1507:   setbuf((FILE*)w2, NULL);
        !          1508: #endif
1.1       anton    1509: wior =  IOR(w2 == 0);
                   1510: 
                   1511: create-file    c_addr u ntype -- w2 wior       file    create_file
                   1512: Cell   fd;
                   1513: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
                   1514: if (fd != -1) {
                   1515:   w2 = (Cell)fdopen(fd, fileattr[ntype]);
1.14    ! pazsan   1516: #if defined(GO32) && defined(MSDOS)
        !          1517:   if(w2 && !(ntype & 1))
        !          1518:     setbuf((FILE*)w2, NULL);
        !          1519: #endif
1.1       anton    1520:   wior = IOR(w2 == 0);
                   1521: } else {
                   1522:   w2 = 0;
                   1523:   wior = IOR(1);
                   1524: }
                   1525: 
                   1526: delete-file    c_addr u -- wior                file    delete_file
                   1527: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
                   1528: 
                   1529: rename-file    c_addr1 u1 c_addr2 u2 -- wior   file-ext        rename_file
                   1530: char *s1=tilde_cstr(c_addr2, u2, 1);
                   1531: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
                   1532: 
                   1533: file-position  wfileid -- ud wior      file    file_position
                   1534: /* !! use tell and lseek? */
                   1535: ud = LONG2UD(ftell((FILE *)wfileid));
                   1536: wior = IOR(UD2LONG(ud)==-1);
                   1537: 
                   1538: reposition-file        ud wfileid -- wior      file    reposition_file
                   1539: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
                   1540: 
                   1541: file-size      wfileid -- ud wior      file    file_size
                   1542: struct stat buf;
                   1543: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
                   1544: ud = LONG2UD(buf.st_size);
                   1545: 
                   1546: resize-file    ud wfileid -- wior      file    resize_file
                   1547: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
                   1548: 
                   1549: read-file      c_addr u1 wfileid -- u2 wior    file    read_file
                   1550: /* !! fread does not guarantee enough */
                   1551: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
                   1552: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
                   1553: /* !! is the value of ferror errno-compatible? */
                   1554: if (wior)
                   1555:   clearerr((FILE *)wfileid);
                   1556: 
                   1557: read-line      c_addr u1 wfileid -- u2 flag wior       file    read_line
                   1558: /*
                   1559: Cell c;
                   1560: flag=-1;
                   1561: for(u2=0; u2<u1; u2++)
                   1562: {
                   1563:    *c_addr++ = (Char)(c = getc((FILE *)wfileid));
                   1564:    if(c=='\n') break;
                   1565:    if(c==EOF)
                   1566:      {
                   1567:        flag=FLAG(u2!=0);
                   1568:        break;
                   1569:      }
                   1570: }
                   1571: wior=FILEIO(ferror((FILE *)wfileid));
                   1572: */
                   1573: if ((flag=FLAG(!feof((FILE *)wfileid) &&
                   1574:               fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
                   1575:   wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
                   1576:   if (wior)
                   1577:     clearerr((FILE *)wfileid);
                   1578:   u2 = strlen(c_addr);
                   1579:   u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
                   1580: }
                   1581: else {
                   1582:   wior=0;
                   1583:   u2=0;
                   1584: }
                   1585: 
1.6       jwilke   1586: \+[THEN]  has? file [IF] -1 [ELSE] has? os [THEN] [IF]
1.1       anton    1587: 
                   1588: write-file     c_addr u1 wfileid -- wior       file    write_file
                   1589: /* !! fwrite does not guarantee enough */
                   1590: {
                   1591:   UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
                   1592:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
                   1593:   if (wior)
                   1594:     clearerr((FILE *)wfileid);
                   1595: }
                   1596: 
                   1597: emit-file      c wfileid -- wior       gforth  emit_file
                   1598: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
                   1599: if (wior)
                   1600:   clearerr((FILE *)wfileid);
                   1601: 
1.6       jwilke   1602: \+[THEN]  has? file [IF]
1.1       anton    1603: 
                   1604: flush-file     wfileid -- wior         file-ext        flush_file
                   1605: wior = IOR(fflush((FILE *) wfileid)==EOF);
                   1606: 
                   1607: file-status    c_addr u -- ntype wior  file-ext        file_status
                   1608: char *filename=tilde_cstr(c_addr, u, 1);
                   1609: if (access (filename, F_OK) != 0) {
                   1610:   ntype=0;
                   1611:   wior=IOR(1);
                   1612: }
                   1613: else if (access (filename, R_OK | W_OK) == 0) {
                   1614:   ntype=2; /* r/w */
                   1615:   wior=0;
                   1616: }
                   1617: else if (access (filename, R_OK) == 0) {
                   1618:   ntype=0; /* r/o */
                   1619:   wior=0;
                   1620: }
                   1621: else if (access (filename, W_OK) == 0) {
                   1622:   ntype=4; /* w/o */
                   1623:   wior=0;
                   1624: }
                   1625: else {
                   1626:   ntype=1; /* well, we cannot access the file, but better deliver a legal
                   1627:            access mode (r/o bin), so we get a decent error later upon open. */
                   1628:   wior=0;
                   1629: }
                   1630: 
1.6       jwilke   1631: \+[THEN] ( has? file ) has? floating [IF]
1.1       anton    1632: 
                   1633: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
                   1634: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
                   1635: 
                   1636: d>f            d -- r          float   d_to_f
                   1637: #ifdef BUGGY_LONG_LONG
                   1638: extern double ldexp(double x, int exp);
                   1639: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
                   1640: #else
                   1641: r = d;
                   1642: #endif
                   1643: 
                   1644: f>d            r -- d          float   f_to_d
                   1645: #ifdef BUGGY_LONG_LONG
                   1646: d.hi = ldexp(r,-CELL_BITS) - (r<0);
                   1647: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
                   1648: #else
                   1649: d = r;
                   1650: #endif
                   1651: 
                   1652: f!             r f_addr --     float   f_store
                   1653: *f_addr = r;
                   1654: 
                   1655: f@             f_addr -- r     float   f_fetch
                   1656: r = *f_addr;
                   1657: 
                   1658: df@            df_addr -- r    float-ext       d_f_fetch
                   1659: #ifdef IEEE_FP
                   1660: r = *df_addr;
                   1661: #else
                   1662: !! df@
                   1663: #endif
                   1664: 
                   1665: df!            r df_addr --    float-ext       d_f_store
                   1666: #ifdef IEEE_FP
                   1667: *df_addr = r;
                   1668: #else
                   1669: !! df!
                   1670: #endif
                   1671: 
                   1672: sf@            sf_addr -- r    float-ext       s_f_fetch
                   1673: #ifdef IEEE_FP
                   1674: r = *sf_addr;
                   1675: #else
                   1676: !! sf@
                   1677: #endif
                   1678: 
                   1679: sf!            r sf_addr --    float-ext       s_f_store
                   1680: #ifdef IEEE_FP
                   1681: *sf_addr = r;
                   1682: #else
                   1683: !! sf!
                   1684: #endif
                   1685: 
                   1686: f+             r1 r2 -- r3     float   f_plus
                   1687: r3 = r1+r2;
                   1688: 
                   1689: f-             r1 r2 -- r3     float   f_minus
                   1690: r3 = r1-r2;
                   1691: 
                   1692: f*             r1 r2 -- r3     float   f_star
                   1693: r3 = r1*r2;
                   1694: 
                   1695: f/             r1 r2 -- r3     float   f_slash
                   1696: r3 = r1/r2;
                   1697: 
                   1698: f**            r1 r2 -- r3     float-ext       f_star_star
                   1699: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
                   1700: r3 = pow(r1,r2);
                   1701: 
                   1702: fnegate                r1 -- r2        float
                   1703: r2 = - r1;
                   1704: 
                   1705: fdrop          r --            float
                   1706: 
                   1707: fdup           r -- r r        float
                   1708: 
                   1709: fswap          r1 r2 -- r2 r1  float
                   1710: 
                   1711: fover          r1 r2 -- r1 r2 r1       float
                   1712: 
                   1713: frot           r1 r2 r3 -- r2 r3 r1    float
                   1714: 
                   1715: fnip           r1 r2 -- r2     gforth
                   1716: 
                   1717: ftuck          r1 r2 -- r2 r1 r2       gforth
                   1718: 
                   1719: float+         f_addr1 -- f_addr2      float   float_plus
                   1720: f_addr2 = f_addr1+1;
                   1721: 
                   1722: floats         n1 -- n2        float
                   1723: n2 = n1*sizeof(Float);
                   1724: 
                   1725: floor          r1 -- r2        float
                   1726: ""round towards the next smaller integral value, i.e., round toward negative infinity""
                   1727: /* !! unclear wording */
                   1728: r2 = floor(r1);
                   1729: 
                   1730: fround         r1 -- r2        float
                   1731: ""round to the nearest integral value""
                   1732: /* !! unclear wording */
                   1733: #ifdef HAVE_RINT
                   1734: r2 = rint(r1);
                   1735: #else
                   1736: r2 = floor(r1+0.5);
                   1737: /* !! This is not quite true to the rounding rules given in the standard */
                   1738: #endif
                   1739: 
                   1740: fmax           r1 r2 -- r3     float
                   1741: if (r1<r2)
                   1742:   r3 = r2;
                   1743: else
                   1744:   r3 = r1;
                   1745: 
                   1746: fmin           r1 r2 -- r3     float
                   1747: if (r1<r2)
                   1748:   r3 = r1;
                   1749: else
                   1750:   r3 = r2;
                   1751: 
                   1752: represent              r c_addr u -- n f1 f2   float
                   1753: char *sig;
                   1754: int flag;
                   1755: int decpt;
                   1756: sig=ecvt(r, u, &decpt, &flag);
                   1757: n=(r==0 ? 1 : decpt);
                   1758: f1=FLAG(flag!=0);
                   1759: f2=FLAG(isdigit(sig[0])!=0);
                   1760: memmove(c_addr,sig,u);
                   1761: 
                   1762: >float c_addr u -- flag        float   to_float
                   1763: /* real signature: c_addr u -- r t / f */
                   1764: Float r;
                   1765: char *number=cstr(c_addr, u, 1);
                   1766: char *endconv;
                   1767: while(isspace(number[--u]) && u>0);
                   1768: switch(number[u])
                   1769: {
                   1770:    case 'd':
                   1771:    case 'D':
                   1772:    case 'e':
                   1773:    case 'E':  break;
                   1774:    default :  u++; break;
                   1775: }
                   1776: number[u]='\0';
                   1777: r=strtod(number,&endconv);
                   1778: if((flag=FLAG(!(Cell)*endconv)))
                   1779: {
                   1780:    IF_FTOS(fp[0] = FTOS);
                   1781:    fp += -1;
                   1782:    FTOS = r;
                   1783: }
                   1784: else if(*endconv=='d' || *endconv=='D')
                   1785: {
                   1786:    *endconv='E';
                   1787:    r=strtod(number,&endconv);
                   1788:    if((flag=FLAG(!(Cell)*endconv)))
                   1789:      {
                   1790:        IF_FTOS(fp[0] = FTOS);
                   1791:        fp += -1;
                   1792:        FTOS = r;
                   1793:      }
                   1794: }
                   1795: 
                   1796: fabs           r1 -- r2        float-ext
                   1797: r2 = fabs(r1);
                   1798: 
                   1799: facos          r1 -- r2        float-ext
                   1800: r2 = acos(r1);
                   1801: 
                   1802: fasin          r1 -- r2        float-ext
                   1803: r2 = asin(r1);
                   1804: 
                   1805: fatan          r1 -- r2        float-ext
                   1806: r2 = atan(r1);
                   1807: 
                   1808: fatan2         r1 r2 -- r3     float-ext
                   1809: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
                   1810: intends this to be the inverse of @code{fsincos}. In gforth it is.""
                   1811: r3 = atan2(r1,r2);
                   1812: 
                   1813: fcos           r1 -- r2        float-ext
                   1814: r2 = cos(r1);
                   1815: 
                   1816: fexp           r1 -- r2        float-ext
                   1817: r2 = exp(r1);
                   1818: 
                   1819: fexpm1         r1 -- r2        float-ext
                   1820: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
                   1821: #ifdef HAVE_EXPM1
1.3       pazsan   1822: extern double
                   1823: #ifdef NeXT
                   1824:               const
                   1825: #endif
                   1826:                     expm1(double);
1.1       anton    1827: r2 = expm1(r1);
                   1828: #else
                   1829: r2 = exp(r1)-1.;
                   1830: #endif
                   1831: 
                   1832: fln            r1 -- r2        float-ext
                   1833: r2 = log(r1);
                   1834: 
                   1835: flnp1          r1 -- r2        float-ext
                   1836: ""@i{r2}=ln(@i{r1}+1)""
                   1837: #ifdef HAVE_LOG1P
1.3       pazsan   1838: extern double
                   1839: #ifdef NeXT
                   1840:               const
                   1841: #endif
                   1842:                     log1p(double);
1.1       anton    1843: r2 = log1p(r1);
                   1844: #else
                   1845: r2 = log(r1+1.);
                   1846: #endif
                   1847: 
                   1848: flog           r1 -- r2        float-ext
                   1849: ""the decimal logarithm""
                   1850: r2 = log10(r1);
                   1851: 
                   1852: falog          r1 -- r2        float-ext
                   1853: ""@i{r2}=10**@i{r1}""
                   1854: extern double pow10(double);
                   1855: r2 = pow10(r1);
                   1856: 
                   1857: fsin           r1 -- r2        float-ext
                   1858: r2 = sin(r1);
                   1859: 
                   1860: fsincos                r1 -- r2 r3     float-ext
                   1861: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
                   1862: r2 = sin(r1);
                   1863: r3 = cos(r1);
                   1864: 
                   1865: fsqrt          r1 -- r2        float-ext
                   1866: r2 = sqrt(r1);
                   1867: 
                   1868: ftan           r1 -- r2        float-ext
                   1869: r2 = tan(r1);
                   1870: :
                   1871:  fsincos f/ ;
                   1872: 
                   1873: fsinh          r1 -- r2        float-ext
                   1874: r2 = sinh(r1);
                   1875: :
                   1876:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
                   1877: 
                   1878: fcosh          r1 -- r2        float-ext
                   1879: r2 = cosh(r1);
                   1880: :
                   1881:  fexp fdup 1/f f+ f2/ ;
                   1882: 
                   1883: ftanh          r1 -- r2        float-ext
                   1884: r2 = tanh(r1);
                   1885: :
                   1886:  f2* fexpm1 fdup 2. d>f f+ f/ ;
                   1887: 
                   1888: fasinh         r1 -- r2        float-ext
                   1889: r2 = asinh(r1);
                   1890: :
                   1891:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
                   1892: 
                   1893: facosh         r1 -- r2        float-ext
                   1894: r2 = acosh(r1);
                   1895: :
                   1896:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
                   1897: 
                   1898: fatanh         r1 -- r2        float-ext
                   1899: r2 = atanh(r1);
                   1900: :
                   1901:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
                   1902:  r> IF  fnegate  THEN ;
                   1903: 
                   1904: sfloats                n1 -- n2        float-ext       s_floats
                   1905: n2 = n1*sizeof(SFloat);
                   1906: 
                   1907: dfloats                n1 -- n2        float-ext       d_floats
                   1908: n2 = n1*sizeof(DFloat);
                   1909: 
                   1910: sfaligned      c_addr -- sf_addr       float-ext       s_f_aligned
                   1911: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
                   1912: :
                   1913:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
                   1914: 
                   1915: dfaligned      c_addr -- df_addr       float-ext       d_f_aligned
                   1916: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
                   1917: :
                   1918:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
                   1919: 
                   1920: \ The following words access machine/OS/installation-dependent
                   1921: \   Gforth internals
                   1922: \ !! how about environmental queries DIRECT-THREADED,
                   1923: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
                   1924: 
                   1925: \ local variable implementation primitives
1.6       jwilke   1926: \+[THEN] ( has? floats ) has? glocals [IF]
1.1       anton    1927: 
                   1928: @local#                -- w    gforth  fetch_local_number
                   1929: w = *(Cell *)(lp+(Cell)NEXT_INST);
                   1930: INC_IP(1);
                   1931: 
                   1932: @local0        -- w    new     fetch_local_zero
                   1933: w = *(Cell *)(lp+0*sizeof(Cell));
                   1934: 
                   1935: @local1        -- w    new     fetch_local_four
                   1936: w = *(Cell *)(lp+1*sizeof(Cell));
                   1937: 
                   1938: @local2        -- w    new     fetch_local_eight
                   1939: w = *(Cell *)(lp+2*sizeof(Cell));
                   1940: 
                   1941: @local3        -- w    new     fetch_local_twelve
                   1942: w = *(Cell *)(lp+3*sizeof(Cell));
                   1943: 
1.6       jwilke   1944: \+has? floating [IF]
1.1       anton    1945: 
                   1946: f@local#       -- r    gforth  f_fetch_local_number
                   1947: r = *(Float *)(lp+(Cell)NEXT_INST);
                   1948: INC_IP(1);
                   1949: 
                   1950: f@local0       -- r    new     f_fetch_local_zero
                   1951: r = *(Float *)(lp+0*sizeof(Float));
                   1952: 
                   1953: f@local1       -- r    new     f_fetch_local_eight
                   1954: r = *(Float *)(lp+1*sizeof(Float));
                   1955: 
                   1956: \+[THEN]
                   1957: 
                   1958: laddr#         -- c_addr       gforth  laddr_number
                   1959: /* this can also be used to implement lp@ */
                   1960: c_addr = (Char *)(lp+(Cell)NEXT_INST);
                   1961: INC_IP(1);
                   1962: 
                   1963: lp+!#  --      gforth  lp_plus_store_number
                   1964: ""used with negative immediate values it allocates memory on the
                   1965: local stack, a positive immediate argument drops memory from the local
                   1966: stack""
                   1967: lp += (Cell)NEXT_INST;
                   1968: INC_IP(1);
                   1969: 
                   1970: lp-    --      new     minus_four_lp_plus_store
                   1971: lp += -sizeof(Cell);
                   1972: 
                   1973: lp+    --      new     eight_lp_plus_store
                   1974: lp += sizeof(Float);
                   1975: 
                   1976: lp+2   --      new     sixteen_lp_plus_store
                   1977: lp += 2*sizeof(Float);
                   1978: 
                   1979: lp!    c_addr --       gforth  lp_store
                   1980: lp = (Address)c_addr;
                   1981: 
                   1982: >l     w --    gforth  to_l
                   1983: lp -= sizeof(Cell);
                   1984: *(Cell *)lp = w;
                   1985: 
1.6       jwilke   1986: \+has? floating [IF]
1.1       anton    1987: 
                   1988: f>l    r --    gforth  f_to_l
                   1989: lp -= sizeof(Float);
                   1990: *(Float *)lp = r;
                   1991: 
1.11      anton    1992: fpick  u -- r          gforth
                   1993: r = fp[u+1]; /* +1, because update of fp happens before this fragment */
                   1994: :
                   1995:  floats fp@ + f@ ;
                   1996: 
1.6       jwilke   1997: \+[THEN]  [THEN] \ has? glocals
1.1       anton    1998: 
1.6       jwilke   1999: \+has? OS [IF]
1.1       anton    2000: 
                   2001: define(`uploop',
                   2002:        `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
                   2003: define(`_uploop',
                   2004:        `ifelse($1, `$3', `$5',
                   2005:               `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
                   2006: \ argflist(argnum): Forth argument list
                   2007: define(argflist,
                   2008:        `ifelse($1, 0, `',
                   2009:                `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
                   2010: \ argdlist(argnum): declare C's arguments
                   2011: define(argdlist,
                   2012:        `ifelse($1, 0, `',
                   2013:                `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
                   2014: \ argclist(argnum): pass C's arguments
                   2015: define(argclist,
                   2016:        `ifelse($1, 0, `',
                   2017:                `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
                   2018: \ icall(argnum)
                   2019: define(icall,
                   2020: `icall$1       argflist($1)u -- uret   gforth
1.9       pazsan   2021: uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
1.1       anton    2022: 
                   2023: ')
                   2024: define(fcall,
                   2025: `fcall$1       argflist($1)u -- rret   gforth
1.9       pazsan   2026: rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
1.1       anton    2027: 
                   2028: ')
                   2029: 
                   2030: 
                   2031: open-lib       c_addr1 u1 -- u2        gforth  open_lib
                   2032: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
1.8       anton    2033: #ifndef RTLD_GLOBAL
                   2034: #define RTLD_GLOBAL 0
                   2035: #endif
1.7       pazsan   2036: u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_GLOBAL | RTLD_LAZY);
1.1       anton    2037: #else
                   2038: #  ifdef HAVE_LIBKERNEL32
                   2039: u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
                   2040: #  else
                   2041: #warning Define open-lib!
                   2042: u2 = 0;
                   2043: #  endif
                   2044: #endif
                   2045: 
                   2046: lib-sym        c_addr1 u1 u2 -- u3     gforth  lib_sym
                   2047: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
                   2048: u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
                   2049: #else
                   2050: #  ifdef HAVE_LIBKERNEL32
                   2051: u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
                   2052: #  else
                   2053: #warning Define lib-sym!
                   2054: u3 = 0;
                   2055: #  endif
                   2056: #endif
                   2057: 
                   2058: uploop(i, 0, 7, `icall(i)')
                   2059: icall(20)
                   2060: uploop(i, 0, 7, `fcall(i)')
                   2061: fcall(20)
                   2062: 
1.6       jwilke   2063: \+[THEN] \ has? OS
1.1       anton    2064: 
                   2065: up!    a_addr --       gforth  up_store
                   2066: UP=up=(char *)a_addr;
                   2067: :
                   2068:  up ! ;
                   2069: Variable UP
                   2070: 

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