Annotation of gforth/primitives, revision 1.46

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

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