Annotation of gforth/primitives, revision 1.51

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

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