Annotation of gforth/prim, revision 1.23

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

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