Annotation of gforth/primitives, revision 1.50

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

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