Annotation of gforth/primitives, revision 1.1

1.1     ! anton       1: /*
        !             2: $Id: primitives,v 1.20 1994/01/19 12:35:35 pazsan Exp $
        !             3: Copyright 1992 by the ANSI figForth Development Group
        !             4: 
        !             5: WARNING: This file is processed by m4. Make sure your identifiers
        !             6: don't collide with m4's (e.g. by undefining them).
        !             7: 
        !             8: This file contains instructions in the following format:
        !             9: 
        !            10: forth name     stack effect    category        [pronounciation]
        !            11: [""glossary entry""]
        !            12: C code
        !            13: [:
        !            14: Forth code]
        !            15: 
        !            16: The pronounciataion is also used for forming C names.
        !            17: 
        !            18: These informations are automagically translated into C-code for the
        !            19: interpreter and into some other files. The forth name of a word is
        !            20: automatically turned into upper case. I hope that your C compiler has
        !            21: decent optimization, otherwise the automatically generated code will
        !            22: be somewhat slow. The Forth version of the code is included for manual
        !            23: compilers, so they will need to compile only the important words.
        !            24: 
        !            25: Note that stack pointer adjustment is performed according to stack
        !            26: effect by automatically generated code and NEXT is automatically
        !            27: appended to the C code. Also, you can use the names in the stack
        !            28: effect in the C code. Stack access is automatic. One exception: if
        !            29: your code does not fall through, the results are not stored into the
        !            30: stack. Use different names on both sides of the '--', if you change a
        !            31: value (some stores to the stack are optimized away).
        !            32: 
        !            33: The stack variables have the following types:
        !            34: name matches   type
        !            35: f.*            Bool
        !            36: c.*            Char
        !            37: [nw].*         Cell
        !            38: u.*            UCell
        !            39: d.*            DCell
        !            40: ud.*           UDCell
        !            41: r.*            Float
        !            42: a_.*           Cell *
        !            43: c_.*           Char *
        !            44: f_.*           Float *
        !            45: df_.*          DFloat *
        !            46: sf_.*          SFloat *
        !            47: xt.*           XT
        !            48: wid.*          WID
        !            49: f83name.*      F83Name *
        !            50: 
        !            51: In addition the following names can be used:
        !            52: ip     the instruction pointer
        !            53: sp     the data stack pointer
        !            54: rp     the parameter stack pointer
        !            55: NEXT   executes NEXT
        !            56: cfa    
        !            57: NEXT1  executes NEXT1
        !            58: FLAG(x)        makes a Forth flag from a C flag
        !            59: 
        !            60: Percentages in comments are from Koopmans book: average/maximum use
        !            61: (taken from four, not very representattive benchmarks)
        !            62: 
        !            63: To do:
        !            64: make sensible error returns for file words
        !            65: 
        !            66: throw execute, cfa and NEXT1 out?
        !            67: macroize *ip, ip++, *ip++ (pipelining)?
        !            68: */
        !            69: 
        !            70: /* these m4 macros would collide with identifiers */
        !            71: undefine(`index')
        !            72: undefine(`shift')
        !            73: 
        !            74: noop   --              fig
        !            75: ;
        !            76: 
        !            77: lit    -- w            fig
        !            78: w = (Cell)*ip++;
        !            79: 
        !            80: /* no clit today */
        !            81: 
        !            82: execute                xt --           core,fig
        !            83: cfa = xt;
        !            84: IF_TOS(TOS = sp[0]);
        !            85: NEXT1;
        !            86: 
        !            87: branch --              fig
        !            88: branch:
        !            89: ip = (Xt *)(((int)ip)+(int)*ip);
        !            90: 
        !            91: ?branch                f --            f83     question_branch
        !            92: ""also known as 0branch""
        !            93: if (f==0) {
        !            94:     IF_TOS(TOS = sp[0]);
        !            95:     goto branch;
        !            96:     }
        !            97: else
        !            98:     ip++;
        !            99: 
        !           100: (next) --              cmFORTH paren_next
        !           101: if ((*rp)--) {
        !           102:     goto branch;
        !           103: } else {
        !           104:     ip++;
        !           105: }
        !           106: 
        !           107: (loop) --              fig     paren_loop
        !           108: int index = *rp+1;
        !           109: int limit = rp[1];
        !           110: if (index != limit) {
        !           111:     *rp = index;
        !           112:     goto branch;
        !           113: } else {
        !           114:     ip++;
        !           115: }
        !           116: 
        !           117: (+loop)                n --            fig     paren_plus_loop
        !           118: /* !! check this thoroughly */
        !           119: int index = *rp;
        !           120: int olddiff = index-rp[1];
        !           121: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
        !           122: /* dependent upon two's complement arithmetic */
        !           123: if ((olddiff^(olddiff+n))<0   /* the limit is crossed */
        !           124:     && (olddiff^n)<0          /* it is not a wrap-around effect */) {
        !           125:     /* break */
        !           126:     ip++;
        !           127: } else {
        !           128:     /* continue */
        !           129:     *rp = index+n;
        !           130:     IF_TOS(TOS = sp[0]);
        !           131:     goto branch;
        !           132: }
        !           133: 
        !           134: (s+loop)       n --            new     paren_symmetric_plus_loop
        !           135: ""The run-time procedure compiled by S+LOOP. It loops until the index
        !           136: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
        !           137: version of (+LOOP).""
        !           138: /* !! check this thoroughly */
        !           139: int oldindex = *rp;
        !           140: int diff = oldindex-rp[1];
        !           141: int newdiff = diff+n;
        !           142: if (n<0) {
        !           143:     diff = -diff;
        !           144:     newdiff = - newdiff;
        !           145: }
        !           146: if (diff>=0 || newdiff<0) {
        !           147:     *rp = oldindex+n;
        !           148:     IF_TOS(TOS = sp[0]);
        !           149:     goto branch;
        !           150: } else {
        !           151:     ip++;
        !           152: }
        !           153: 
        !           154: unloop         --      core
        !           155: rp += 2;
        !           156: 
        !           157: (for)  ncount --               cmFORTH         paren_for
        !           158: /* or (for) = >r -- collides with unloop! */
        !           159: *--rp = 0;
        !           160: *--rp = ncount;
        !           161: 
        !           162: (do)   nlimit nstart --                fig             paren_do
        !           163: /* or do it in high-level? 0.09/0.23% */
        !           164: *--rp = nlimit;
        !           165: *--rp = nstart;
        !           166: :
        !           167:  swap >r >r ;
        !           168: 
        !           169: (?do)  nlimit nstart --        core-ext        paren_question_do
        !           170: *--rp = nlimit;
        !           171: *--rp = nstart;
        !           172: if (nstart == nlimit) {
        !           173:     IF_TOS(TOS = sp[0]);
        !           174:     goto branch;
        !           175:     }
        !           176: else {
        !           177:     ip++;
        !           178: }
        !           179: 
        !           180: i      -- n            core,fig
        !           181: n = *rp;
        !           182: 
        !           183: j      -- n            core
        !           184: n = rp[2];
        !           185: 
        !           186: /* digit is high-level: 0/0% */
        !           187: 
        !           188: emit   c --            fig
        !           189: putchar(c);
        !           190: emitcounter++;
        !           191: 
        !           192: key    -- n            fig
        !           193: fflush(stdout);
        !           194: /* !! noecho */
        !           195: n = key();
        !           196: 
        !           197: cr     --              fig
        !           198: puts("");
        !           199: 
        !           200: move   c_from c_to ucount --           core
        !           201: memmove(c_to,c_from,ucount);
        !           202: /* make an ifdef for bsd and others? */
        !           203: 
        !           204: cmove  c_from c_to u --        string
        !           205: while (u-- > 0)
        !           206:   *c_to++ = *c_from++;
        !           207: 
        !           208: cmove> c_from c_to u --        string  c_move_up
        !           209: while (u-- > 0)
        !           210:   c_to[u] = c_from[u];
        !           211: 
        !           212: fill   c_addr u c --   core
        !           213: memset(c_addr,c,u);
        !           214: 
        !           215: compare                c_addr1 u1 c_addr2 u2 -- n      string
        !           216: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
        !           217: if (n==0)
        !           218:   n = u1-u2;
        !           219: if (n<0)
        !           220:   n = -1;
        !           221: else if (n>0)
        !           222:   n = 1;
        !           223: 
        !           224: -text          c_addr1 u c_addr2 -- n  new     dash_text
        !           225: n = memcmp(c_addr1, c_addr2, u);
        !           226: if (n<0)
        !           227:   n = -1;
        !           228: else if (n>0)
        !           229:   n = 1;
        !           230: 
        !           231: capscomp       c_addr1 u c_addr2 -- n  new
        !           232: Char c1, c2;
        !           233: for (;; u--, c_addr1++, c_addr2++) {
        !           234:   if (u == 0) {
        !           235:     n = 0;
        !           236:     break;
        !           237:   }
        !           238:   c1 = toupper(*c_addr1);
        !           239:   c2 = toupper(*c_addr2);
        !           240:   if (c1 != c2) {
        !           241:     if (c1 < c2)
        !           242:       n = -1;
        !           243:     else
        !           244:       n = 1;
        !           245:     break;
        !           246:   }
        !           247: }
        !           248: 
        !           249: -trailing      c_addr u1 -- c_addr u2          string  dash_trailing
        !           250: u2 = u1;
        !           251: while (c_addr[u2-1] == ' ')
        !           252:   u2--;
        !           253: 
        !           254: /string                c_addr1 u1 n -- c_addr2 u2      string  slash_string
        !           255: c_addr2 = c_addr1+n;
        !           256: u2 = u1-n;
        !           257: 
        !           258: +      n1 n2 -- n              core,fig        plus
        !           259: n = n1+n2;
        !           260: 
        !           261: -      n1 n2 -- n              core,fig        minus
        !           262: n = n1-n2;
        !           263: 
        !           264: negate n1 -- n2                core,fig
        !           265: /* use minus as alias */
        !           266: n2 = -n1;
        !           267: 
        !           268: 1+     n1 -- n2                core            one_plus
        !           269: n2 = n1+1;
        !           270: 
        !           271: 1-     n1 -- n2                core            one_minus
        !           272: n2 = n1-1;
        !           273: 
        !           274: max    n1 n2 -- n      core
        !           275: if (n1<n2)
        !           276:   n = n2;
        !           277: else
        !           278:   n = n1;
        !           279: :
        !           280:  2dup < if
        !           281:   swap drop
        !           282:  else
        !           283:   drop
        !           284:  endif ;
        !           285: 
        !           286: min    n1 n2 -- n      core
        !           287: if (n1<n2)
        !           288:   n = n1;
        !           289: else
        !           290:   n = n2;
        !           291: 
        !           292: abs    n1 -- n2        core
        !           293: if (n1<0)
        !           294:   n2 = -n1;
        !           295: else
        !           296:   n2 = n1;
        !           297: 
        !           298: *      n1 n2 -- n              core,fig        star
        !           299: n = n1*n2;
        !           300: 
        !           301: /      n1 n2 -- n              core,fig        slash
        !           302: n = n1/n2;
        !           303: 
        !           304: mod    n1 n2 -- n              core
        !           305: n = n1%n2;
        !           306: 
        !           307: /mod   n1 n2 -- n3 n4          core            slash_mod
        !           308: n4 = n1/n2;
        !           309: n3 = n1%n2; /* !! is this correct? look into C standard! */
        !           310: 
        !           311: 2*     n1 -- n2                core            two_star
        !           312: n2 = 2*n1;
        !           313: 
        !           314: 2/     n1 -- n2                core            two_slash
        !           315: /* !! is this still correct? */
        !           316: n2 = n1>>1;
        !           317: 
        !           318: fm/mod d1 n1 -- n2 n3          core            f_m_slash_mod
        !           319: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
        !           320: /* assumes that the processor uses either floored or symmetric division */
        !           321: n3 = d1/n1;
        !           322: n2 = d1%n1;
        !           323: /* note that this 1%-3>0 is optimized by the compiler */
        !           324: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
        !           325:   n3--;
        !           326:   n2+=n1;
        !           327: }
        !           328: 
        !           329: sm/rem d1 n1 -- n2 n3          core            s_m_slash_rem
        !           330: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
        !           331: /* assumes that the processor uses either floored or symmetric division */
        !           332: n3 = d1/n1;
        !           333: n2 = d1%n1;
        !           334: /* note that this 1%-3<0 is optimized by the compiler */
        !           335: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
        !           336:   n3++;
        !           337:   n2-=n1;
        !           338: }
        !           339: 
        !           340: m*     n1 n2 -- d              core    m_star
        !           341: d = (DCell)n1 * (DCell)n2;
        !           342: 
        !           343: um*    u1 u2 -- ud             core    u_m_star
        !           344: /* use u* as alias */
        !           345: ud = (UDCell)u1 * (UDCell)u2;
        !           346: 
        !           347: um/mod ud u1 -- u2 u3          core    u_m_slash_mod
        !           348: u3 = ud/u1;
        !           349: u2 = ud%u1;
        !           350: 
        !           351: m+     d1 n -- d2              double          m_plus
        !           352: d2 = d1+n;
        !           353: 
        !           354: d+     d1 d2 -- d              double,fig      d_plus
        !           355: d = d1+d2;
        !           356: 
        !           357: d-     d1 d2 -- d              double          d_minus
        !           358: d = d1-d2;
        !           359: 
        !           360: dnegate        d1 -- d2                double
        !           361: /* use dminus as alias */
        !           362: d2 = -d1;
        !           363: 
        !           364: dmax   d1 d2 -- d      double
        !           365: if (d1<d2)
        !           366:   d = d2;
        !           367: else
        !           368:   d = d1;
        !           369: 
        !           370: dmin   d1 d2 -- d      double
        !           371: if (d1<d2)
        !           372:   d = d1;
        !           373: else
        !           374:   d = d2;
        !           375: 
        !           376: dabs   d1 -- d2        double
        !           377: if (d1<0)
        !           378:   d2 = -d1;
        !           379: else
        !           380:   d2 = d1;
        !           381: 
        !           382: d2*    d1 -- d2                double          d_two_star
        !           383: d2 = 2*d1;
        !           384: 
        !           385: d2/    d1 -- d2                double          d_two_slash
        !           386: /* !! is this still correct? */
        !           387: d2 = d1/2;
        !           388: 
        !           389: d>s    d -- n                  double          d_to_s
        !           390: /* make this an alias for drop? */
        !           391: n = d;
        !           392: 
        !           393: and    w1 w2 -- w              core,fig
        !           394: w = w1&w2;
        !           395: 
        !           396: or     w1 w2 -- w              core,fig
        !           397: w = w1|w2;
        !           398: 
        !           399: xor    w1 w2 -- w              core,fig
        !           400: w = w1^w2;
        !           401: 
        !           402: invert w1 -- w2                core
        !           403: w2 = ~w1;
        !           404: 
        !           405: rshift u1 n -- u2              core
        !           406:   u2 = u1>>n;
        !           407: 
        !           408: lshift u1 n -- u2              core
        !           409:   u2 = u1<<n;
        !           410: 
        !           411: /* comparisons(prefix, args, prefix, arg1, arg2, wordsets...) */
        !           412: define(comparisons,
        !           413: $1=    $2 -- f         $6      $3equals
        !           414: f = FLAG($4==$5);
        !           415: 
        !           416: $1<>   $2 -- f         $7      $3different
        !           417: /* use != as alias ? */
        !           418: f = FLAG($4!=$5);
        !           419: 
        !           420: $1<    $2 -- f         $8      $3less
        !           421: f = FLAG($4<$5);
        !           422: 
        !           423: $1>    $2 -- f         $9      $3greater
        !           424: f = FLAG($4>$5);
        !           425: 
        !           426: $1<=   $2 -- f         new     $3less_or_equal
        !           427: f = FLAG($4<=$5);
        !           428: 
        !           429: $1>=   $2 -- f         new     $3greater_or_equal
        !           430: f = FLAG($4>=$5);
        !           431: 
        !           432: )
        !           433: 
        !           434: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
        !           435: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
        !           436: comparisons(u, u1 u2, u_, u1, u2, new, new, core, core-ext)
        !           437: comparisons(d, d1 d2, d_, d1, d2, double, new, double, new)
        !           438: comparisons(d0, d, d_zero_, d, 0, double, new, double, new)
        !           439: comparisons(du, ud1 ud2, d_u_, ud1, ud2, new, new, double-ext, new)
        !           440: 
        !           441: within u1 u2 u3 -- f           core-ext
        !           442: f = FLAG(u1-u2 < u3-u2);
        !           443: 
        !           444: sp@    -- a_addr               fig             spat
        !           445: a_addr = sp;
        !           446: 
        !           447: sp!    a_addr --               fig             spstore
        !           448: sp = a_addr+1;
        !           449: /* works with and without TOS caching */
        !           450: 
        !           451: rp@    -- a_addr               fig             rpat
        !           452: a_addr = rp;
        !           453: 
        !           454: rp!    a_addr --               fig             rpstore
        !           455: rp = a_addr;
        !           456: 
        !           457: fp@    -- f_addr       new     fp_fetch
        !           458: f_addr = fp;
        !           459: 
        !           460: fp!    f_addr --       new     fp_store
        !           461: fp = f_addr;
        !           462: 
        !           463: exit   --              core
        !           464: /* use ;s as alias */
        !           465: ip = (Xt *)(*rp++);
        !           466: 
        !           467: ?exit  w --            core            question_exit
        !           468: /* use ;s as alias */
        !           469: if(w)
        !           470:        ip = (Xt *)(*rp++);
        !           471: 
        !           472: >r     w --            core,fig        to_r
        !           473: *--rp = w;
        !           474: 
        !           475: r>     -- w            core,fig        r_from
        !           476: w = *rp++;
        !           477: 
        !           478: r@     -- w            core,fig        r_fetch
        !           479: /* use r as alias */
        !           480: /* make r@ an alias for i */
        !           481: w = *rp;
        !           482: 
        !           483: rdrop  --              fig
        !           484: rp++;
        !           485: 
        !           486: i'     -- w            fig             i_tick
        !           487: w=rp[1];
        !           488: 
        !           489: over   w1 w2 -- w1 w2 w1               core,fig
        !           490: 
        !           491: drop   w --            core,fig
        !           492: 
        !           493: swap   w1 w2 -- w2 w1          core,fig
        !           494: 
        !           495: dup    w -- w w                core,fig
        !           496: 
        !           497: rot    w1 w2 w3 -- w2 w3 w1    core    rote
        !           498: 
        !           499: -rot   w1 w2 w3 -- w3 w1 w2    fig     not_rote
        !           500: 
        !           501: nip    w1 w2 -- w2             core-ext
        !           502: 
        !           503: tuck   w1 w2 -- w2 w1 w2       core-ext
        !           504: 
        !           505: ?dup   w -- w                  core    question_dupe
        !           506: /* resulting C code suboptimal */
        !           507: /* make -dup an alias */
        !           508: if (w!=0) {
        !           509:   --sp;
        !           510: #ifndef USE_TOS
        !           511:   *sp = w;
        !           512: #endif
        !           513: }
        !           514: 
        !           515: pick   u -- w                  core-ext
        !           516: w = sp[u+1];
        !           517: 
        !           518: 2drop  w1 w2 --                core    two_drop
        !           519: 
        !           520: 2dup   w1 w2 -- w1 w2 w1 w2    core    two_dupe
        !           521: 
        !           522: 2over  w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2        core    two_over
        !           523: 
        !           524: 2swap  w1 w2 w3 w4 -- w3 w4 w1 w2      core    two_swap
        !           525: 
        !           526: 2rot   w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2  double  two_rote
        !           527: 
        !           528: /* toggle is high-level: 0.11/0.42% */
        !           529: 
        !           530: @      a_addr -- w             fig     fetch
        !           531: w = *a_addr;
        !           532: 
        !           533: !      w a_addr --             core,fig        store
        !           534: *a_addr = w;
        !           535: 
        !           536: +!     n a_addr --             core,fig        plus_store
        !           537: *a_addr += n;
        !           538: 
        !           539: c@     c_addr -- c             fig     cfetch
        !           540: c = *c_addr;
        !           541: 
        !           542: c!     c c_addr --             fig     cstore
        !           543: *c_addr = c;
        !           544: 
        !           545: 2!     w1 w2 a_addr --         core    two_store
        !           546: a_addr[0] = w2;
        !           547: a_addr[1] = w1;
        !           548: 
        !           549: 2@     a_addr -- w1 w2         core    two_fetch
        !           550: w2 = a_addr[0];
        !           551: w1 = a_addr[1];
        !           552: 
        !           553: d!     d a_addr --             double  d_store
        !           554: /* !! alignment problems on some machines */
        !           555: *(DCell *)a_addr = d;
        !           556: 
        !           557: d@     a_addr -- d             double  d_fetch
        !           558: d = *(DCell *)a_addr;
        !           559: 
        !           560: cell+  a_addr1 -- a_addr2      core    cell_plus
        !           561: a_addr2 = a_addr1+1;
        !           562: 
        !           563: cells  n1 -- n2                core
        !           564: n2 = n1 * sizeof(Cell);
        !           565: 
        !           566: char+  c_addr1 -- c_addr2      core    care_plus
        !           567: c_addr2 = c_addr1+1;
        !           568: 
        !           569: chars  n1 -- n2                core    cares
        !           570: n2 = n1 * sizeof(Char);
        !           571: 
        !           572: count  c_addr1 -- c_addr2 u    core
        !           573: u = *c_addr1;
        !           574: c_addr2 = c_addr1+1;
        !           575: 
        !           576: (bye)  n --    toolkit-ext     paren_bye
        !           577: deprep_terminal();
        !           578: exit(n);
        !           579: 
        !           580: system c_addr u -- n   own
        !           581: char pname[u+1];
        !           582: cstr(pname,c_addr,u);
        !           583: n=system(pname);
        !           584: 
        !           585: popen  c_addr u n -- wfileid   own
        !           586: char pname[u+1];
        !           587: static char* mode[2]={"r","w"};
        !           588: cstr(pname,c_addr,u);
        !           589: wfileid=(Cell)popen(pname,mode[n]);
        !           590: 
        !           591: pclose wfileid -- wior own
        !           592: wior=pclose((FILE *)wfileid);
        !           593: 
        !           594: allocate       u -- a_addr wior        memory
        !           595: a_addr = (Cell *)malloc(u);
        !           596: wior = a_addr==NULL;   /* !! define a return code */
        !           597: 
        !           598: free           a_addr -- wior          memory
        !           599: free(a_addr);
        !           600: wior = 0;
        !           601: 
        !           602: resize         a_addr1 u -- a_addr2 wior       memory
        !           603: a_addr2 = realloc(a_addr1, u);
        !           604: wior = a_addr2==NULL;  /* !! define a return code */
        !           605: 
        !           606: (f83find)      c_addr u f83name1 -- f83name2   new     paren_f83find
        !           607: for (; f83name1 != NULL; f83name1 = f83name1->next)
        !           608:   if (F83NAME_COUNT(f83name1)==u && !F83NAME_SMUDGE(f83name1) &&
        !           609:       strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
        !           610:     break;
        !           611: f83name2=f83name1;
        !           612: 
        !           613: (parse-white)  c_addr1 u1 -- c_addr2 u2        new     paren_parse_white
        !           614: /* use !isgraph instead of isspace? */
        !           615: Char *endp = c_addr1+u1;
        !           616: while (c_addr1<endp && isspace(*c_addr1))
        !           617:   c_addr1++;
        !           618: if (c_addr1<endp) {
        !           619:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
        !           620:     ;
        !           621:   u2 = c_addr1-c_addr2;
        !           622: }
        !           623: else {
        !           624:   c_addr2 = c_addr1;
        !           625:   u2 = 0;
        !           626: }
        !           627: 
        !           628: close-file     wfileid -- wior file    close_file
        !           629: wior = FLAG(fclose((FILE *)wfileid)==EOF);
        !           630: 
        !           631: open-file      c_addr u ntype -- w2 wior       file    open_file
        !           632: char fname[u+1];
        !           633: cstr(fname, c_addr, u);
        !           634: w2 = (Cell)fopen(fname, fileattr[ntype]);
        !           635: wior = FLAG(w2 == NULL);
        !           636: 
        !           637: create-file    c_addr u ntype -- w2 wior       file    create_file
        !           638: int    fd;
        !           639: char fname[u+1];
        !           640: cstr(fname, c_addr, u);
        !           641: fd = creat(fname, 0666);
        !           642: if (fd > -1) {
        !           643:   w2 = (Cell)fdopen(fd, fileattr[ntype]);
        !           644:   assert(w2 != NULL);
        !           645:   wior = 0;
        !           646: } else {
        !           647:   assert(fd == -1);
        !           648:   wior = fd;
        !           649:   w2 = 0;
        !           650: }
        !           651: 
        !           652: delete-file    c_addr u -- wior                file    delete_file
        !           653: char fname[u+1];
        !           654: cstr(fname, c_addr, u);
        !           655: wior = unlink(fname);
        !           656: 
        !           657: rename-file    c_addr1 u1 c_addr2 u2 -- wior   file-ext        rename_file
        !           658: char fname1[u1+1];
        !           659: char fname2[u2+1];
        !           660: cstr(fname1, c_addr1, u1);
        !           661: cstr(fname2, c_addr2, u2);
        !           662: wior = rename(fname1, fname2);
        !           663: 
        !           664: file-position  wfileid -- ud wior      file    file_position
        !           665: /* !! use tell and lseek? */
        !           666: ud = ftell((FILE *)wfileid);
        !           667: wior = 0; /* !! or wior = FLAG(ud<0) */
        !           668: 
        !           669: reposition-file        ud wfileid -- wior      file    reposition_file
        !           670: wior = fseek((FILE *)wfileid, (long)ud, SEEK_SET);
        !           671: 
        !           672: file-size      wfileid -- ud wior      file    file_size
        !           673: struct stat buf;
        !           674: wior = fstat(fileno((FILE *)wfileid), &buf);
        !           675: ud = buf.st_size;
        !           676: 
        !           677: resize-file    ud wfileid -- wior      file    resize_file
        !           678: wior = ftruncate(fileno((FILE *)wfileid), (int)ud);
        !           679: 
        !           680: read-file      c_addr u1 wfileid -- u2 wior    file    read_file
        !           681: /* !! fread does not guarantee enough */
        !           682: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
        !           683: wior = FLAG(u2<u1 && ferror((FILE *)wfileid));
        !           684: /* !! who performs clearerr((FILE *)wfileid); ? */
        !           685: 
        !           686: read-line      c_addr u1 wfileid -- u2 flag wior       file    read_line
        !           687: wior=(Cell)fgets(c_addr,u1+1,(FILE *)wfileid);
        !           688: flag=FLAG(!feof((FILE *)wfileid) && wior);
        !           689: wior=FLAG(ferror((FILE *)wfileid)) & flag;
        !           690: u2=(flag & strlen(c_addr));
        !           691: u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
        !           692: 
        !           693: write-file     c_addr u1 wfileid -- wior       file    write_file
        !           694: /* !! fwrite does not guarantee enough */
        !           695: {
        !           696:   int u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
        !           697:   wior = FLAG(u2<u1 && ferror((FILE *)wfileid));
        !           698: }
        !           699: 
        !           700: flush-file     wfileid -- wior         file-ext        flush_file
        !           701: wior = fflush((FILE *)wfileid);
        !           702: 
        !           703: comparisons(f, r1 r2, f_, r1, r2, new, new, float, new)
        !           704: comparisons(f0, r, f_zero_, r, 0., float, new, float, new)
        !           705: 
        !           706: d>f            d -- r          float   d_to_f
        !           707: r = d;
        !           708: 
        !           709: f>d            r -- d          float   f_to_d
        !           710: /* !! basis 15 is not very specific */
        !           711: d = r;
        !           712: 
        !           713: f!             r f_addr --     float   f_store
        !           714: *f_addr = r;
        !           715: 
        !           716: f@             f_addr -- r     float   f_fetch
        !           717: r = *f_addr;
        !           718: 
        !           719: df@            df_addr -- r    float-ext       d_f_fetch
        !           720: #ifdef IEEE_FP
        !           721: r = *df_addr;
        !           722: #else
        !           723: !! df@
        !           724: #endif
        !           725: 
        !           726: df!            r df_addr --    float-ext       d_f_store
        !           727: #ifdef IEEE_FP
        !           728: *df_addr = r;
        !           729: #else
        !           730: !! df!
        !           731: #endif
        !           732: 
        !           733: sf@            sf_addr -- r    float-ext       s_f_fetch
        !           734: #ifdef IEEE_FP
        !           735: r = *sf_addr;
        !           736: #else
        !           737: !! sf@
        !           738: #endif
        !           739: 
        !           740: sf!            r sf_addr --    float-ext       s_f_store
        !           741: #ifdef IEEE_FP
        !           742: *sf_addr = r;
        !           743: #else
        !           744: !! sf!
        !           745: #endif
        !           746: 
        !           747: f+             r1 r2 -- r3     float   f_plus
        !           748: r3 = r1+r2;
        !           749: 
        !           750: f-             r1 r2 -- r3     float   f_minus
        !           751: r3 = r1-r2;
        !           752: 
        !           753: f*             r1 r2 -- r3     float   f_star
        !           754: r3 = r1*r2;
        !           755: 
        !           756: f/             r1 r2 -- r3     float   f_slash
        !           757: r3 = r1/r2;
        !           758: 
        !           759: f**            r1 r2 -- r3     float-ext       f_star_star
        !           760: r3 = pow(r1,r2);
        !           761: 
        !           762: fnegate                r1 -- r2        float
        !           763: r2 = - r1;
        !           764: 
        !           765: fdrop          r --            float
        !           766: 
        !           767: fdup           r -- r r        float
        !           768: 
        !           769: fswap          r1 r2 -- r2 r1  float
        !           770: 
        !           771: fover          r1 r2 -- r1 r2 r1       float
        !           772: 
        !           773: frot           r1 r2 r3 -- r2 r3 r1    float
        !           774: 
        !           775: float+         f_addr1 -- f_addr2      float   float_plus
        !           776: f_addr2 = f_addr1+1;
        !           777: 
        !           778: floats         n1 -- n2        float
        !           779: n2 = n1*sizeof(Float);
        !           780: 
        !           781: floor          r1 -- r2        float
        !           782: /* !! unclear wording */
        !           783: r2 = floor(r1);
        !           784: 
        !           785: fround         r1 -- r2        float
        !           786: /* !! unclear wording */
        !           787: r2 = rint(r1);
        !           788: 
        !           789: fmax           r1 r2 -- r3     float
        !           790: if (r1<r2)
        !           791:   r3 = r2;
        !           792: else
        !           793:   r3 = r1;
        !           794: 
        !           795: fmin           r1 r2 -- r3     float
        !           796: if (r1<r2)
        !           797:   r3 = r1;
        !           798: else
        !           799:   r3 = r2;
        !           800: 
        !           801: represent              r c_addr u -- n f1 f2   float
        !           802: char *sig;
        !           803: int flag;
        !           804: sig=ecvt(r, u, &n, &flag);
        !           805: f1=FLAG(flag!=0);
        !           806: f2=FLAG(isdigit(sig[0])!=0);
        !           807: memmove(c_addr,sig,u);
        !           808: 
        !           809: >float c_addr u -- flag        float   to_float
        !           810: /* real signature: c_addr u -- r t / f */
        !           811: Float r;
        !           812: char number[u+1];
        !           813: char *endconv;
        !           814: cstr(number, c_addr, u);
        !           815: r=strtod(number,&endconv);
        !           816: if(flag=FLAG(!(int)*endconv))
        !           817: {
        !           818:        IF_FTOS(fp[0] = FTOS);
        !           819:        fp += -1;
        !           820:        FTOS = r;
        !           821: }
        !           822: else if(*endconv=='d' || *endconv=='D')
        !           823: {
        !           824:        *endconv='E';
        !           825:        r=strtod(number,&endconv);
        !           826:        if(flag=FLAG(!(int)*endconv))
        !           827:        {
        !           828:                IF_FTOS(fp[0] = FTOS);
        !           829:                fp += -1;
        !           830:                FTOS = r;
        !           831:        }
        !           832: }
        !           833: 
        !           834: fabs           r1 -- r2        float-ext
        !           835: r2 = fabs(r1);
        !           836: 
        !           837: facos          r1 -- r2        float-ext
        !           838: r2 = acos(r1);
        !           839: 
        !           840: fasin          r1 -- r2        float-ext
        !           841: r2 = asin(r1);
        !           842: 
        !           843: fatan          r1 -- r2        float-ext
        !           844: r2 = atan(r1);
        !           845: 
        !           846: fatan2         r1 r2 -- r3     float-ext
        !           847: r3 = atan2(r1,r2);
        !           848: 
        !           849: fcos           r1 -- r2        float-ext
        !           850: r2 = cos(r1);
        !           851: 
        !           852: fexp           r1 -- r2        float-ext
        !           853: r2 = exp(r1);
        !           854: 
        !           855: fln            r1 -- r2        float-ext
        !           856: r2 = log(r1);
        !           857: 
        !           858: flog           r1 -- r2        float-ext
        !           859: r2 = log10(r1);
        !           860: 
        !           861: fsin           r1 -- r2 r3     float-ext
        !           862: r2 = sin(r1);
        !           863: r3 = cos(r1);
        !           864: 
        !           865: fsqrt          r1 -- r2        float-ext
        !           866: r2 = sqrt(r1);
        !           867: 
        !           868: ftan           r1 -- r2        float-ext
        !           869: r2 = tan(r1);
        !           870: 
        !           871: /* The following words access machine/OS/installation-dependent ANSI
        !           872:    figForth internals */
        !           873: /* !! how about environmental queries DIRECT-THREADED,
        !           874:    INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
        !           875: 
        !           876: >body          xt -- a_addr    core    to_body
        !           877: a_addr = PFA(xt);
        !           878: 
        !           879: >code-address          xt -- c_addr            new     to_code_address
        !           880: ""c_addr is the code address of the word xt""
        !           881: /* !! This behaves installation-dependently for DOES-words */
        !           882: c_addr = CODE_ADDRESS(xt);
        !           883: 
        !           884: >does-code     xt -- a_addr            new     to_does_code
        !           885: ""If xt ist the execution token of a defining-word-defined word,
        !           886: a_addr is the start of the Forth code after the DOES>; Otherwise the
        !           887: behaviour is uundefined""
        !           888: /* !! there is currently no way to determine whether a word is
        !           889: defining-word-defined */
        !           890: a_addr = DOES_CODE(xt);
        !           891: 
        !           892: code-address!          c_addr xt --    new     code_address_store
        !           893: ""Creates a code field with code address c_addr at xt""
        !           894: MAKE_CF(xt, c_addr);
        !           895: 
        !           896: does-code!     a_addr xt --            new     does_code_store
        !           897: ""creates a code field at xt for a defining-word-defined word; a_addr
        !           898: is the start of the Forth code after DOES>""
        !           899: MAKE_DOES_CF(xt, a_addr);
        !           900: 
        !           901: does-handler!  a_addr --       new     does_jump_store
        !           902: ""creates a DOES>-handler at address a_addr. a_addr usually points
        !           903: just behind a DOES>.""
        !           904: MAKE_DOES_HANDLER(a_addr);
        !           905: 
        !           906: /does-handler  -- n    new     slash_does_handler
        !           907: ""the size of a does-handler (includes possible padding)""
        !           908: /* !! a constant or environmental query might be better */
        !           909: n = DOES_HANDLER_SIZE;
        !           910: 
        !           911: toupper        c1 -- c2        new
        !           912: c2 = toupper(c1);
        !           913: 
        !           914: /* local variable implementation primitives */
        !           915: @local#                -- w    new     fetch_local_number
        !           916: w = *(Cell *)(lp+(int)(*ip++));
        !           917: 
        !           918: f@local#       -- r    new     f_fetch_local_number
        !           919: r = *(Float *)(lp+(int)(*ip++));
        !           920: 
        !           921: laddr#         -- c_addr       new     laddr_number
        !           922: /* this can also be used to implement lp@ */
        !           923: c_addr = (Char *)(lp+(int)(*ip++));
        !           924: 
        !           925: lp+!#  --      new     lp_plus_store_number
        !           926: ""used with negative immediate values it allocates memory on the
        !           927: local stack, a positive immediate argument drops memory from the local
        !           928: stack""
        !           929: lp += (int)(*ip++);
        !           930: 
        !           931: lp!    c_addr --       new     lp_store
        !           932: lp = (Address)c_addr;
        !           933: 
        !           934: >l     w --    new     to_l
        !           935: lp -= sizeof(Cell);
        !           936: *(Cell *)lp = w;
        !           937: 
        !           938: f>l    r --    new     f_to_l
        !           939: lp -= sizeof(Float);
        !           940: *(Float *)lp = r;

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