Annotation of gforth/primitives, revision 1.68

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

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