Annotation of gforth/prim, revision 1.26

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

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