Annotation of gforth/prims2y.fs, revision 1.5

1.1       anton       1: \ converts primitives to, e.g., C code 
                      2: 
                      3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
                     20: 
                     21: 
                     22: \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
                     23: \ And it grew even worse when it aged.
                     24: 
                     25: \ Optimizations:
                     26: \ superfluous stores are removed. GCC removes the superfluous loads by itself
                     27: \ TOS and FTOS can be kept in register( variable)s.
                     28: \ 
                     29: \ Problems:
                     30: \ The TOS optimization is somewhat hairy. The problems by example:
                     31: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
                     32: \    The store is not superfluous although the earlier opt. would think so
                     33: \    Alternatively:    sp[0]=TOS; w=TOS; sp-=1; TOS=w;
                     34: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
                     35: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
                     36: \ 4) ( -- ): /* but here they are unnecessary */
                     37: \ 5) Words that call NEXT themselves have to be done very carefully.
                     38: \
                     39: \ To do:
                     40: \ add the store optimization for doubles
                     41: \ regarding problem 1 above: It would be better (for over) to implement
                     42: \      the alternative
                     43: \ store optimization for combined instructions.
                     44: 
                     45: \ Design Uglyness:
                     46: 
                     47: \ - global state (values, variables) in connection with combined instructions.
                     48: 
                     49: \ - index computation is different for instruction-stream and the
                     50: \ stacks; there are two mechanisms for dealing with that
                     51: \ (stack-in-index-xt and a test for stack==instruction-stream); there
                     52: \ should be only one.
                     53: 
                     54: \ for backwards compatibility, jaw
                     55: require compat/strcomp.fs
                     56: 
                     57: warnings off
                     58: 
                     59: \ redefinitions of kernel words not present in gforth-0.6.1
                     60: : latestxt lastcfa @ ;
                     61: : latest last @ ;
                     62: 
                     63: [IFUNDEF] try
                     64: include startup.fs
                     65: [THEN]
                     66: 
                     67: : struct% struct ; \ struct is redefined in gray
                     68: 
                     69: warnings off
                     70: \ warnings on
                     71: 
                     72: include ./gray.fs
                     73: 128 constant max-effect \ number of things on one side of a stack effect
                     74: 4 constant max-stacks  \ the max. number of stacks (including inst-stream).
                     75: 255 constant maxchar
                     76: maxchar 1+ constant eof-char
                     77: #tab constant tab-char
                     78: #lf constant nl-char
                     79: 
                     80: variable rawinput \ pointer to next character to be scanned
                     81: variable endrawinput \ pointer to the end of the input (the char after the last)
                     82: variable cookedinput \ pointer to the next char to be parsed
                     83: variable line \ line number of char pointed to by input
                     84: variable line-start \ pointer to start of current line (for error messages)
                     85: 0 line !
                     86: 2variable filename \ filename of original input file
                     87: 0 0 filename 2!
                     88: 2variable out-filename \ filename of the output file (for sync lines)
                     89: 0 0 out-filename 2!
                     90: 2variable f-comment
                     91: 0 0 f-comment 2!
                     92: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
                     93: skipsynclines on
                     94: variable out-nls \ newlines in output (for output sync lines)
                     95: 0 out-nls !
                     96: variable store-optimization \ use store optimization?
                     97: store-optimization off
                     98: 
                     99: variable include-skipped-insts
                    100: \ does the threaded code for a combined instruction include the cells
                    101: \ for the component instructions (true) or only the cells for the
                    102: \ inline arguments (false)
                    103: include-skipped-insts off
                    104: 
                    105: variable immarg \ values for immediate arguments (to be used in IMM_ARG macros)
                    106: $12340000 immarg !
                    107: 
                    108: : th ( addr1 n -- addr2 )
                    109:     cells + ;
                    110: 
                    111: : holds ( addr u -- )
                    112:     \ like HOLD, but for a string
                    113:     tuck + swap 0 +do
                    114:        1- dup c@ hold
                    115:     loop
                    116:     drop ;
                    117: 
                    118: : insert-wordlist { c-addr u wordlist xt -- }
                    119:     \ adds name "addr u" to wordlist using defining word xt
                    120:     \ xt may cause additional stack effects
                    121:     get-current >r wordlist set-current
                    122:     c-addr u nextname xt execute
                    123:     r> set-current ;
                    124: 
                    125: : start ( -- addr )
                    126:  cookedinput @ ;
                    127: 
                    128: : end ( addr -- addr u )
                    129:  cookedinput @ over - ;
                    130: 
                    131: : print-error-line ( -- )
                    132:     \ print the current line and position
                    133:     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
                    134:     over - type cr
                    135:     line-start @ rawinput @ over - typewhite ." ^" cr ;
                    136: 
                    137: : ?print-error { f addr u -- }
                    138:     f ?not? if
                    139:        outfile-id >r try
                    140:            stderr to outfile-id
                    141:            filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
                    142:            print-error-line
                    143:            0
                    144:        recover endtry
                    145:        r> to outfile-id throw
                    146:        1 (bye) \ abort
                    147:     endif ;
                    148: 
                    149: : quote ( -- )
                    150:     [char] " emit ;
                    151: 
                    152: \ count output lines to generate sync lines for output
                    153: 
                    154: : count-nls ( addr u -- )
                    155:     bounds u+do
                    156:        i c@ nl-char = negate out-nls +!
                    157:     loop ;
                    158: 
                    159: :noname ( addr u -- )
                    160:     2dup count-nls
                    161:     defers type ;
                    162: is type
                    163: 
                    164: variable output          \ xt ( -- ) of output word for simple primitives
                    165: variable output-combined \ xt ( -- ) of output word for combined primitives
                    166: 
                    167: struct%
                    168:     cell%    field stack-number \ the number of this stack
                    169:     cell% 2* field stack-pointer \ stackpointer name
                    170:     cell%    field stack-type \ name for default type of stack items
                    171:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
                    172:     cell%    field stack-access-transform \ ( nitem -- index )
                    173: end-struct stack%
                    174: 
                    175: struct%
                    176:  cell% 2* field item-name   \ name, excluding stack prefixes
                    177:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
                    178:  cell%    field item-type   \ descriptor for the item type
                    179:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
                    180:  cell%   field item-first  \ true if this is the first occurence of the item
                    181: end-struct item%
                    182: 
                    183: struct%
                    184:     cell% 2* field type-c-name
                    185:     cell%    field type-stack \ default stack
                    186:     cell%    field type-size  \ size of type in stack items
                    187:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
                    188:     cell%    field type-store \ xt of store code generator ( item -- )
                    189: end-struct type%
                    190: 
1.2       anton     191: struct%
                    192:     cell%    field register-number
                    193:     cell%    field register-type \ pointer to type
                    194:     cell% 2* field register-name \ c name
                    195: end-struct register%
                    196: 
                    197: struct%
                    198:     cell% 2* field ss-registers  \ addr u; ss-registers[0] is TOS
1.3       anton     199:                                  \ 0 means: use memory
1.2       anton     200:     cell%    field ss-offset     \ stack pointer offset: sp[-offset] is TOS
                    201: end-struct ss% \ stack-state
                    202: 
                    203: struct%
                    204:     cell% max-stacks * field state-sss
                    205: end-struct state%
                    206: 
1.1       anton     207: variable next-stack-number 0 next-stack-number !
                    208: create stacks max-stacks cells allot \ array of stacks
1.2       anton     209: 256 constant max-registers
                    210: create registers max-registers cells allot \ array of registers
                    211: variable nregisters 0 nregisters ! \ number of registers
1.1       anton     212: 
                    213: : stack-in-index ( in-size item -- in-index )
                    214:     item-offset @ - 1- ;
                    215: 
                    216: : inst-in-index ( in-size item -- in-index )
                    217:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
                    218: 
                    219: : make-stack ( addr-ptr u1 type "stack-name" -- )
                    220:     next-stack-number @ max-stacks < s" too many stacks" ?print-error
                    221:     create stack% %allot >r
                    222:     r@ stacks next-stack-number @ th !
                    223:     next-stack-number @ r@ stack-number !
                    224:     1 next-stack-number +!
                    225:     r@ stack-type !
                    226:     save-mem r@ stack-pointer 2! 
                    227:     ['] stack-in-index r@ stack-in-index-xt !
                    228:     ['] noop r@ stack-access-transform !
                    229:     rdrop ;
                    230: 
                    231: : map-stacks { xt -- }
                    232:     \ perform xt for all stacks
                    233:     next-stack-number @ 0 +do
                    234:        stacks i th @ xt execute
                    235:     loop ;
                    236: 
                    237: : map-stacks1 { xt -- }
                    238:     \ perform xt for all stacks except inst-stream
                    239:     next-stack-number @ 1 +do
                    240:        stacks i th @ xt execute
                    241:     loop ;
                    242: 
                    243: \ stack items
                    244: 
                    245: : init-item ( addr u addr1 -- )
                    246:     \ initialize item at addr1 with name addr u
                    247:     \ !! remove stack prefix
                    248:     dup item% %size erase
                    249:     item-name 2! ;
                    250: 
                    251: : map-items { addr end xt -- }
                    252:     \ perform xt for all items in array addr...end
                    253:     end addr ?do
                    254:        i xt execute
                    255:     item% %size +loop ;
                    256: 
                    257: \ types
                    258: 
                    259: : print-type-prefix ( type -- )
                    260:     body> >head name>string type ;
                    261: 
                    262: \ various variables for storing stuff of one primitive
                    263: 
                    264: struct%
                    265:     cell% 2* field prim-name
                    266:     cell% 2* field prim-wordset
                    267:     cell% 2* field prim-c-name
                    268:     cell% 2* field prim-doc
                    269:     cell% 2* field prim-c-code
                    270:     cell% 2* field prim-forth-code
                    271:     cell% 2* field prim-stack-string
                    272:     cell%    field prim-num            \ ordinal number
                    273:     cell%    field prim-items-wordlist \ unique items
                    274:     item% max-effect * field prim-effect-in
                    275:     item% max-effect * field prim-effect-out
                    276:     cell%    field prim-effect-in-end
                    277:     cell%    field prim-effect-out-end
                    278:     cell% max-stacks * field prim-stacks-in  \ number of in items per stack
                    279:     cell% max-stacks * field prim-stacks-out \ number of out items per stack
                    280: end-struct prim%
                    281: 
                    282: : make-prim ( -- prim )
                    283:     prim% %alloc { p }
                    284:     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
                    285:     p ;
                    286: 
                    287: 0 value prim     \ in combined prims either combined or a part
                    288: 0 value combined \ in combined prims the combined prim
                    289: variable in-part \ true if processing a part
                    290:  in-part off
1.3       anton     291: 0 value state-in  \ state on entering prim
                    292: 0 value state-out \ state on exiting prim
1.1       anton     293: 
                    294: : prim-context ( ... p xt -- ... )
                    295:     \ execute xt with prim set to p
                    296:     prim >r
                    297:     swap to prim
                    298:     catch
                    299:     r> to prim
                    300:     throw ;
                    301: 
                    302: 1000 constant max-combined
                    303: create combined-prims max-combined cells allot
                    304: variable num-combined
                    305: variable part-num \ current part number during process-combined
                    306: 
                    307: : map-combined { xt -- }
                    308:     \ perform xt for all components of the current combined instruction
                    309:     num-combined @ 0 +do
                    310:        combined-prims i th @ xt execute
                    311:     loop ;
                    312: 
                    313: table constant combinations
                    314:   \ the keys are the sequences of pointers to primitives
                    315: 
                    316: create current-depth max-stacks cells allot
                    317: create max-depth     max-stacks cells allot
                    318: create min-depth     max-stacks cells allot
                    319: 
                    320: create sp-update-in max-stacks cells allot
                    321: \ where max-depth occured the first time
                    322: create max-depths max-stacks max-combined 1+ * cells allot
                    323: \ maximum depth at start of each part: array[parts] of array[stack]
                    324: create max-back-depths max-stacks max-combined 1+ * cells allot
                    325: \ maximun depth from end of the combination to the start of the each part
                    326: 
                    327: : s-c-max-depth ( nstack ncomponent -- addr )
                    328:     max-stacks * + cells max-depths + ;
                    329: 
                    330: : s-c-max-back-depth ( nstack ncomponent -- addr )
                    331:     max-stacks * + cells max-back-depths + ;
                    332: 
                    333: wordlist constant primitives
                    334: 
                    335: : create-prim ( prim -- )
                    336:     dup prim-name 2@ primitives ['] constant insert-wordlist ;
                    337: 
                    338: : stack-in ( stack -- addr )
                    339:     \ address of number of stack items in effect in
                    340:     stack-number @ cells prim prim-stacks-in + ;
                    341: 
                    342: : stack-out ( stack -- addr )
                    343:     \ address of number of stack items in effect out
                    344:     stack-number @ cells prim prim-stacks-out + ;
                    345: 
                    346: \ global vars
                    347: variable c-line
                    348: 2variable c-filename
                    349: variable name-line
                    350: 2variable name-filename
                    351: 2variable last-name-filename
                    352: Variable function-number 0 function-number !
                    353: Variable function-old 0 function-old !
                    354: : function-diff ( n -- )
                    355:     ." GROUPADD(" function-number @ function-old @ - 0 .r ." )" cr
                    356:     function-number @ function-old ! ;
                    357: : forth-fdiff ( -- )
                    358:     function-number @ function-old @ - 0 .r ."  groupadd" cr
                    359:     function-number @ function-old ! ;
                    360: 
                    361: \ a few more set ops
                    362: 
                    363: : bit-equivalent ( w1 w2 -- w3 )
                    364:  xor invert ;
                    365: 
                    366: : complement ( set1 -- set2 )
                    367:  empty ['] bit-equivalent binary-set-operation ;
                    368: 
                    369: \ forward declaration for inst-stream (breaks cycle in definitions)
                    370: defer inst-stream-f ( -- stack )
                    371: 
                    372: \ stack access stuff
                    373: 
                    374: : normal-stack-access0 { n stack -- }
1.3       anton     375:     \ n has the ss-offset already applied (see ...-access1)
1.1       anton     376:     n stack stack-access-transform @ execute ." [" 0 .r ." ]" ;
1.4       anton     377: 
                    378: : state-ss { stack state -- ss }
                    379:     state state-sss stack stack-number @ th @ ;
                    380: 
                    381: : stack-reg { n stack state -- reg }
                    382:     \ n is the index (TOS=0); reg is 0 if the access is to memory
                    383:     stack state state-ss ss-registers 2@ n u> if ( addr ) \ in ss-registers?
                    384:        n th @
                    385:     else
                    386:        drop 0
                    387:     endif ;
                    388: 
1.5     ! anton     389: : .reg ( reg -- )
        !           390:     register-name 2@ type ;
        !           391: 
        !           392: : stack-offset ( stack state -- n )
        !           393:     \ offset for stack in state
        !           394:     state-ss ss-offset @ ;
        !           395: 
1.3       anton     396: : normal-stack-access1 { n stack state -- }
1.4       anton     397:     n stack state stack-reg ?dup-if
1.5     ! anton     398:        .reg exit
1.3       anton     399:     endif
1.1       anton     400:     stack stack-pointer 2@ type
1.5     ! anton     401:     n stack state stack-offset - stack normal-stack-access0 ;
1.1       anton     402: 
1.3       anton     403: : normal-stack-access ( n stack state -- )
                    404:     over inst-stream-f = if
1.1       anton     405:        ." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )"
                    406:        1 immarg +!
                    407:     else
                    408:        normal-stack-access1
                    409:     endif ;
                    410: 
                    411: : stack-depth { stack -- n }
                    412:     current-depth stack stack-number @ th @ ;
                    413: 
                    414: : part-stack-access { n stack -- }
                    415:     \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
                    416:     ." _" stack stack-pointer 2@ type
                    417:     stack stack-number @ { stack# }
                    418:     stack stack-depth n + { access-depth }
                    419:     stack inst-stream-f = if
                    420:        access-depth
                    421:     else
                    422:        combined prim-stacks-in stack# th @
                    423:        assert( dup max-depth stack# th @ = )
                    424:        access-depth - 1-
                    425:     endif
                    426:     0 .r ;
                    427: 
                    428: : part-stack-read { n stack -- }
                    429:     stack stack-depth n + ( ndepth )
                    430:     stack stack-number @ part-num @ s-c-max-depth @
                    431: \    max-depth stack stack-number @ th @ ( ndepth nmaxdepth )
                    432:     over <= if ( ndepth ) \ load from memory
1.3       anton     433:        stack state-in normal-stack-access
1.1       anton     434:     else
                    435:        drop n stack part-stack-access
                    436:     endif ;
                    437: 
                    438: : stack-diff ( stack -- n )
                    439:     \ in-out
                    440:     dup stack-in @ swap stack-out @ - ;
                    441: 
                    442: : part-stack-write { n stack -- }
                    443:     stack stack-depth n +
                    444:     stack stack-number @ part-num @ s-c-max-back-depth @
                    445:     over <= if ( ndepth )
                    446:        stack combined ['] stack-diff prim-context -
1.3       anton     447:        stack state-out normal-stack-access
1.1       anton     448:     else
                    449:        drop n stack part-stack-access
                    450:     endif ;
                    451: 
                    452: : stack-read ( n stack -- )
                    453:     \ print a stack access at index n of stack
                    454:     in-part @ if
                    455:        part-stack-read
                    456:     else
1.3       anton     457:        state-in normal-stack-access
1.1       anton     458:     endif ;
                    459: 
                    460: : stack-write ( n stack -- )
                    461:     \ print a stack access at index n of stack
                    462:     in-part @ if
                    463:        part-stack-write
                    464:     else
1.3       anton     465:        state-out normal-stack-access
1.1       anton     466:     endif ;
                    467: 
                    468: : item-in-index { item -- n }
                    469:     \ n is the index of item (in the in-effect)
                    470:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
                    471:     item r> stack-in-index-xt @ execute ;
                    472: 
                    473: : item-stack-type-name ( item -- addr u )
                    474:     item-stack @ stack-type @ type-c-name 2@ ;
                    475: 
                    476: : fetch-single ( item -- )
                    477:     \ fetch a single stack item from its stack
                    478:     >r
                    479:     ." vm_" r@ item-stack-type-name type
                    480:     ." 2" r@ item-type @ print-type-prefix ." ("
                    481:     r@ item-in-index r@ item-stack @ stack-read ." ,"
                    482:     r@ item-name 2@ type
                    483:     ." );" cr
                    484:     rdrop ; 
                    485: 
                    486: : fetch-double ( item -- )
                    487:     \ fetch a double stack item from its stack
                    488:     >r
                    489:     ." vm_two"
                    490:     r@ item-stack-type-name type ." 2"
                    491:     r@ item-type @ print-type-prefix ." ("
                    492:     r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-read
                    493:     ." , "                      -1 under+ ." (Cell)" stack-read
                    494:     ." , " r@ item-name 2@ type
                    495:     ." )" cr
                    496:     rdrop ;
                    497: 
                    498: : same-as-in? ( item -- f )
                    499:  \ f is true iff the offset and stack of item is the same as on input
                    500:  >r
                    501:  r@ item-first @ if
                    502:      rdrop false exit
                    503:  endif
                    504:  r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
                    505:  execute @
                    506:  dup r@ =
                    507:  if \ item first appeared in output
                    508:    drop false
                    509:  else
                    510:    dup  item-stack  @ r@ item-stack  @ = 
                    511:    swap item-offset @ r@ item-offset @ = and
                    512:  endif
                    513:  rdrop ;
                    514: 
                    515: : item-out-index ( item -- n )
1.4       anton     516:     \ n is the index of item (in the out-effect)
1.1       anton     517:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
                    518: 
                    519: : really-store-single ( item -- )
                    520:     >r
                    521:     ." vm_"
                    522:     r@ item-type @ print-type-prefix ." 2"
                    523:     r@ item-stack-type-name type ." ("
                    524:     r@ item-name 2@ type ." ,"
                    525:     r@ item-out-index r@ item-stack @ stack-write ." );"
                    526:     rdrop ;
                    527: 
1.4       anton     528: : store-single { item -- }
                    529:     item item-stack @ { stack }
                    530:     store-optimization @ in-part @ 0= and item same-as-in? and
                    531:     item item-in-index  stack state-in  stack-reg 0= and \  in in memory?
                    532:     item item-out-index stack state-out stack-reg 0= and \ out in memory?
                    533:     0= if
                    534:        item really-store-single cr
                    535:     endif ;
1.1       anton     536: 
                    537: : store-double ( item -- )
                    538: \ !! store optimization is not performed, because it is not yet needed
                    539:  >r
                    540:  ." vm_"
                    541:  r@ item-type @ print-type-prefix ." 2two"
                    542:  r@ item-stack-type-name type ." ("
                    543:  r@ item-name 2@ type ." , "
                    544:  r@ item-out-index r@ item-stack @ 2dup stack-write
                    545:  ." , "                       -1 under+ stack-write
                    546:  ." )" cr
                    547:  rdrop ;
                    548: 
                    549: : single ( -- xt1 xt2 n )
                    550:     ['] fetch-single ['] store-single 1 ;
                    551: 
                    552: : double ( -- xt1 xt2 n )
                    553:     ['] fetch-double ['] store-double 2 ;
                    554: 
                    555: : s, ( addr u -- )
                    556: \ allocate a string
                    557:  here swap dup allot move ;
                    558: 
                    559: wordlist constant prefixes
                    560: 
                    561: : declare ( addr "name" -- )
                    562: \ remember that there is a stack item at addr called name
                    563:  create , ;
                    564: 
                    565: : !default ( w addr -- )
                    566:     dup @ if
                    567:        2drop \ leave nonzero alone
                    568:     else
                    569:        !
                    570:     endif ;
                    571: 
                    572: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
                    573:     \ describes a type
                    574:     \ addr u specifies the C type name
                    575:     \ stack effect entries of the type start with prefix
                    576:     create type% %allot >r
                    577:     addr u save-mem r@ type-c-name 2!
                    578:     xt1   r@ type-fetch !
                    579:     xt2   r@ type-store !
                    580:     n     r@ type-size !
                    581:     stack r@ type-stack !
                    582:     rdrop ;
                    583: 
                    584: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
                    585:     get-current >r prefixes set-current
                    586:     create-type r> set-current
                    587: does> ( item -- )
                    588:     \ initialize item
                    589:     { item typ }
                    590:     typ item item-type !
                    591:     typ type-stack @ item item-stack !default
                    592:     item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
                    593:        item item-name 2@ nextname item declare
                    594:        item item-first on
                    595:        \ typ type-c-name 2@ type space type  ." ;" cr
                    596:     else
                    597:        drop
                    598:        item item-first off
                    599:     endif ;
                    600: 
                    601: : execute-prefix ( item addr1 u1 -- )
                    602:     \ execute the word ( item -- ) associated with the longest prefix
                    603:     \ of addr1 u1
                    604:     0 swap ?do
                    605:        dup i prefixes search-wordlist
                    606:        if \ ok, we have the type ( item addr1 xt )
                    607:            nip execute
                    608:            UNLOOP EXIT
                    609:        endif
                    610:        -1 s+loop
                    611:     \ we did not find a type, abort
                    612:     false s" unknown prefix" ?print-error ;
                    613: 
                    614: : declaration ( item -- )
                    615:     dup item-name 2@ execute-prefix ;
                    616: 
                    617: : declaration-list ( addr1 addr2 -- )
                    618:     ['] declaration map-items ;
                    619: 
                    620: : declarations ( -- )
                    621:  wordlist dup prim prim-items-wordlist ! set-current
                    622:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
                    623:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
                    624: 
                    625: : print-declaration { item -- }
                    626:     item item-first @ if
                    627:        item item-type @ type-c-name 2@ type space
                    628:        item item-name 2@ type ." ;" cr
                    629:     endif ;
                    630: 
                    631: : print-declarations ( -- )
                    632:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
                    633:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
                    634:     
                    635: : stack-prefix ( stack "prefix" -- )
                    636:     get-current >r prefixes set-current
                    637:     name tuck nextname create ( stack length ) 2,
                    638:     r> set-current
                    639: does> ( item -- )
                    640:     2@ { item stack prefix-length }
                    641:     item item-name 2@ prefix-length /string item item-name 2!
                    642:     stack item item-stack !
                    643:     item declaration ;
                    644: 
                    645: \ types pointed to by stacks for use in combined prims
                    646: \ !! output-c-combined shouldn't use these names!
                    647: : stack-type-name ( addr u "name" -- )
                    648:     single 0 create-type ;
                    649: 
                    650: wordlist constant type-names \ this is here just to meet the requirement
                    651:                     \ that a type be a word; it is never used for lookup
                    652: 
1.2       anton     653: : define-type ( addr u -- xt )
                    654:     \ define single type with name addr u, without stack
                    655:     get-current type-names set-current >r
                    656:     2dup nextname stack-type-name
                    657:     r> set-current
                    658:     latestxt ;
                    659: 
1.1       anton     660: : stack ( "name" "stack-pointer" "type" -- )
                    661:     \ define stack
                    662:     name { d: stack-name }
                    663:     name { d: stack-pointer }
                    664:     name { d: stack-type }
1.2       anton     665:     stack-type define-type
                    666:     stack-pointer rot >body stack-name nextname make-stack ;
1.1       anton     667: 
                    668: stack inst-stream IP Cell
                    669: ' inst-in-index inst-stream stack-in-index-xt !
                    670: ' inst-stream <is> inst-stream-f
                    671: \ !! initialize stack-in and stack-out
1.2       anton     672: 
                    673: \ registers
                    674: 
                    675: : make-register ( type addr u -- )
                    676:     \ define register with type TYPE and name ADDR U.
                    677:     nregisters @ max-registers < s" too many registers" ?print-error
                    678:     2dup nextname create register% %allot >r
                    679:     r@ register-name 2!
                    680:     r@ register-type !
                    681:     nregisters @ r@ register-number !
                    682:     1 nregisters +!
                    683:     rdrop ;
                    684: 
                    685: : register ( "name" "type" -- )
                    686:     \ define register
                    687:     name { d: reg-name }
                    688:     name { d: reg-type }
                    689:     reg-type define-type >body
                    690:     reg-name make-register ;
                    691: 
                    692: \ stack-states
                    693: 
                    694: : stack-state ( a-addr u uoffset "name" -- )
                    695:     create ss% %allot >r
                    696:     r@ ss-offset !
                    697:     r@ ss-registers 2!
                    698:     rdrop ;
                    699: 
                    700: 0 0 0 stack-state default-ss
                    701: 
                    702: \ state
                    703: 
                    704: : state ( "name" -- )
                    705:     \ create a state initialized with default-sss
                    706:     create state% %allot state-sss { sss }
                    707:     max-stacks 0 ?do
                    708:        default-ss sss i th !
                    709:     loop ;
                    710: 
                    711: : set-ss ( ss stack state -- )
                    712:     state-sss swap stack-number @ th ! ;
1.1       anton     713: 
                    714: \ offset computation
                    715: \ the leftmost (i.e. deepest) item has offset 0
                    716: \ the rightmost item has the highest offset
                    717: 
                    718: : compute-offset { item xt -- }
                    719:     \ xt specifies in/out; update stack-in/out and set item-offset
                    720:     item item-type @ type-size @
                    721:     item item-stack @ xt execute dup @ >r +!
                    722:     r> item item-offset ! ;
                    723: 
                    724: : compute-offset-in ( addr1 addr2 -- )
                    725:     ['] stack-in compute-offset ;
                    726: 
                    727: : compute-offset-out ( addr1 addr2 -- )
                    728:     ['] stack-out compute-offset ;
                    729: 
                    730: : compute-offsets ( -- )
                    731:     prim prim-stacks-in  max-stacks cells erase
                    732:     prim prim-stacks-out max-stacks cells erase
                    733:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
                    734:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
                    735:     inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
                    736: 
                    737: : process-simple ( -- )
                    738:     prim prim { W^ key } key cell
                    739:     combinations ['] constant insert-wordlist
                    740:     declarations compute-offsets
                    741:     output @ execute ;
                    742: 
1.5     ! anton     743: : stack-state-items ( stack state -- n )
        !           744:     state-ss ss-registers 2@ nip ;
        !           745: 
        !           746: : unused-stack-items { stack -- n-in n-out }
        !           747:     \ n-in  are the stack items in state-in  not used    by prim
        !           748:     \ n-out are the stack items in state-out not written by prim
        !           749:     stack state-in  stack-state-items stack stack-in  @ - 0 max
        !           750:     stack state-out stack-state-items stack stack-out @ - 0 max ;
        !           751: 
        !           752: : spill-stack { stack -- }
        !           753:     \ spill regs of state-in that are not used by prim and are not in state-out
        !           754:     stack state-in stack-offset { offset }
        !           755:     stack state-in stack-state-items ( items )
        !           756:     dup stack unused-stack-items - - +do
        !           757:        \ loop through the bottom items
        !           758:        stack stack-pointer 2@ type
        !           759:        i offset - stack normal-stack-access0 ."  = "
        !           760:        i stack state-in normal-stack-access1 ." ;" cr
        !           761:     loop ;
1.1       anton     762: 
1.5     ! anton     763: : spill-state ( -- )
        !           764:     ['] spill-stack map-stacks1 ;
1.1       anton     765: 
1.5     ! anton     766: : fill-stack { stack -- }
        !           767:     stack state-out stack-offset { offset }
        !           768:     stack state-out stack-state-items ( items )
        !           769:     dup stack unused-stack-items - + +do
        !           770:        \ loop through the bottom items
        !           771:        i stack state-out normal-stack-access1 ."  = "
        !           772:        stack stack-pointer 2@ type
        !           773:        i offset - stack normal-stack-access0 ." ;" cr
        !           774:     loop ;
1.1       anton     775: 
1.5     ! anton     776: : fill-state ( -- )
1.1       anton     777:     \ !! inst-stream for prefetching?
1.5     ! anton     778:     ['] fill-stack map-stacks1 ;
1.1       anton     779: 
                    780: : fetch ( addr -- )
                    781:     dup item-type @ type-fetch @ execute ;
                    782: 
                    783: : fetches ( -- )
                    784:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
                    785: 
1.5     ! anton     786: : reg-reg-move ( reg-from reg-to -- )
        !           787:     2dup = if
        !           788:        2drop
        !           789:     else
        !           790:        .reg ."  = " .reg ." ;" cr
        !           791:     endif ;
        !           792: 
        !           793: : stack-bottom-reg { n stack state -- reg }
        !           794:     stack state stack-state-items n - 1- stack state stack-reg ;
        !           795: 
        !           796: : stack-moves { stack -- }
        !           797:     \ generate moves between registers in state-in/state-out that are
        !           798:     \ not spilled or consumed/produced by prim.
        !           799:     \ !! this works only for a simple stack cache, not e.g., for
        !           800:     \ rotating stack caches, or registers shared between stacks (the
        !           801:     \ latter would also require a change in interface)
        !           802:     \ !! maybe place this after NEXT_P1?
        !           803:     stack unused-stack-items 2dup < if ( n-in n-out )
        !           804:        \ move registers from 0..n_in-1 to n_out-n_in..n_out-1
        !           805:        over - { diff } ( n-in )
        !           806:        -1 swap 1- -do
        !           807:            i stack state-in stack-bottom-reg ( reg-from )
        !           808:            i diff + stack state-out stack-bottom-reg reg-reg-move
        !           809:        1 -loop
        !           810:     else
        !           811:        \ move registers from n_in-n_out..n_in-1 to 0..n_out-1
        !           812:        swap over - { diff } ( n-out )
        !           813:        0 +do
        !           814:            i diff + stack state-in stack-bottom-reg ( reg-from )
        !           815:            i stack state-out stack-bottom-reg reg-reg-move
        !           816:        loop
        !           817:     endif ;
        !           818: 
1.1       anton     819: : stack-update-transform ( n1 stack -- n2 )
                    820:     \ n2 is the number by which the stack pointer should be
                    821:     \ incremented to pop n1 items
                    822:     stack-access-transform @ dup >r execute
                    823:     0 r> execute - ;
                    824: 
                    825: : stack-pointer-update { stack -- }
1.5     ! anton     826:     \ and moves
1.1       anton     827:     \ stacks grow downwards
1.5     ! anton     828:     stack stack-diff ( in-out )
        !           829:     stack state-in  stack-offset -
        !           830:     stack state-out stack-offset + ( [in-in_offset]-[out-out_offset] )
1.1       anton     831:     ?dup-if \ this check is not necessary, gcc would do this for us
                    832:        stack inst-stream = if
                    833:            ." INC_IP(" 0 .r ." );" cr
                    834:        else
                    835:            stack stack-pointer 2@ type ."  += "
                    836:            stack stack-update-transform 0 .r ." ;" cr
                    837:        endif
1.5     ! anton     838:     endif
        !           839:     stack stack-moves ;
1.1       anton     840: 
                    841: : stack-pointer-updates ( -- )
                    842:     ['] stack-pointer-update map-stacks ;
                    843: 
                    844: : store ( item -- )
                    845: \ f is true if the item should be stored
                    846: \ f is false if the store is probably not necessary
                    847:  dup item-type @ type-store @ execute ;
                    848: 
                    849: : stores ( -- )
                    850:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
                    851: 
                    852: : print-debug-arg { item -- }
                    853:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
                    854:     ." printarg_" item item-type @ print-type-prefix
                    855:     ." (" item item-name 2@ type ." );" cr ;
                    856:     
                    857: : print-debug-args ( -- )
                    858:     ." #ifdef VM_DEBUG" cr
                    859:     ." if (vm_debug) {" cr
                    860:     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
                    861: \    ." fputc('\n', vm_out);" cr
                    862:     ." }" cr
                    863:     ." #endif" cr ;
                    864: 
                    865: : print-debug-result { item -- }
                    866:     item item-first @ if
                    867:        item print-debug-arg
                    868:     endif ;
                    869: 
                    870: : print-debug-results ( -- )
                    871:     cr
                    872:     ." #ifdef VM_DEBUG" cr
                    873:     ." if (vm_debug) {" cr
                    874:     ." fputs(" quote ."  -- " quote ." , vm_out); "
                    875:     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
                    876:     ." fputc('\n', vm_out);" cr
                    877:     ." }" cr
                    878:     ." #endif" cr ;
                    879: 
                    880: : output-super-end ( -- )
                    881:     prim prim-c-code 2@ s" SET_IP" search if
                    882:        ." SUPER_END;" cr
                    883:     endif
                    884:     2drop ;
                    885: 
                    886: : output-nextp2 ( -- )
                    887:     ." NEXT_P2;" cr ;
                    888: 
                    889: variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL
                    890: ' output-nextp2 tail-nextp2 !
                    891: 
                    892: : output-label2 ( -- )
                    893:     ." LABEL2(" prim prim-c-name 2@ type ." )" cr
                    894:     ." NEXT_P2;" cr ;
                    895: 
                    896: : output-c-tail1 { xt -- }
                    897:     \ the final part of the generated C code, with xt printing LABEL2 or not.
                    898:     output-super-end
                    899:     print-debug-results
                    900:     ." NEXT_P1;" cr
                    901:     stores
1.5     ! anton     902:     fill-state 
1.1       anton     903:     xt execute ;
                    904: 
                    905: : output-c-tail1-no-stores { xt -- }
                    906:     \ the final part of the generated C code for combinations
                    907:     output-super-end
                    908:     ." NEXT_P1;" cr
1.5     ! anton     909:     fill-state 
1.1       anton     910:     xt execute ;
                    911: 
                    912: : output-c-tail ( -- )
                    913:     tail-nextp2 @ output-c-tail1 ;
                    914: 
                    915: : output-c-tail2 ( -- )
                    916:     ['] output-label2 output-c-tail1 ;
                    917: 
                    918: : output-c-tail-no-stores ( -- )
                    919:     tail-nextp2 @ output-c-tail1-no-stores ;
                    920: 
                    921: : output-c-tail2-no-stores ( -- )
                    922:     ['] output-label2 output-c-tail1-no-stores ;
                    923: 
                    924: : type-c-code ( c-addr u xt -- )
                    925:     \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
                    926:     { xt }
                    927:     ." {" cr
                    928:     ." #line " c-line @ . quote c-filename 2@ type quote cr
                    929:     begin ( c-addr1 u1 )
                    930:        2dup s" INST_TAIL;" search
                    931:     while ( c-addr1 u1 c-addr3 u3 )
                    932:        2dup 2>r drop nip over - type
                    933:        xt execute
                    934:        2r> 10 /string
                    935:        \ !! resync #line missing
                    936:     repeat
                    937:     2drop type
                    938:     ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
                    939:     ." }" cr ;
                    940: 
                    941: : print-entry ( -- )
                    942:     ." LABEL(" prim prim-c-name 2@ type ." )" ;
                    943:     
                    944: : output-c ( -- ) 
                    945:     print-entry ."  /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                    946:     ." /* " prim prim-doc 2@ type ."  */" cr
                    947:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
                    948:     ." {" cr
                    949:     ." DEF_CA" cr
                    950:     print-declarations
                    951:     ." NEXT_P0;" cr
1.5     ! anton     952:     spill-state
1.1       anton     953:     fetches
                    954:     print-debug-args
                    955:     stack-pointer-updates
                    956:     prim prim-c-code 2@ ['] output-c-tail type-c-code
                    957:     output-c-tail2
                    958:     ." }" cr
                    959:     cr
                    960: ;
                    961: 
                    962: : disasm-arg { item -- }
                    963:     item item-stack @ inst-stream = if
                    964:        ." {" cr
                    965:        item print-declaration
                    966:        item fetch
                    967:        item print-debug-arg
                    968:        ." }" cr
                    969:     endif ;
                    970: 
                    971: : disasm-args ( -- )
                    972:     prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
                    973: 
                    974: : output-disasm ( -- )
                    975:     \ generate code for disassembling VM instructions
                    976:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
                    977:     ."   fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
                    978:     disasm-args
                    979:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
                    980:     ."   goto _endif_;" cr
                    981:     ." }" cr ;
                    982: 
                    983: : output-profile ( -- )
                    984:     \ generate code for postprocessing the VM block profile stuff
                    985:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
                    986:     ."   add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
                    987:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
                    988:     prim prim-c-code 2@  s" SET_IP"    search nip nip
                    989:     prim prim-c-code 2@  s" SUPER_END" search nip nip or if
                    990:        ."   return;" cr
                    991:     else
                    992:        ."   goto _endif_;" cr
                    993:     endif
                    994:     ." }" cr ;
                    995: 
                    996: : output-profile-part ( p )
                    997:     ."   add_inst(b, " quote
                    998:     prim-name 2@ type
                    999:     quote ." );" cr ;
                   1000:     
                   1001: : output-profile-combined ( -- )
                   1002:     \ generate code for postprocessing the VM block profile stuff
                   1003:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
                   1004:     ['] output-profile-part map-combined
                   1005:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
                   1006:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SET_IP"    search nip nip
                   1007:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SUPER_END" search nip nip or if
                   1008:        ."   return;" cr
                   1009:     else
                   1010:        ."   goto _endif_;" cr
                   1011:     endif
                   1012:     ." }" cr ;
                   1013: 
                   1014: : prim-branch? { prim -- f }
                   1015:     \ true if prim is a branch or super-end
                   1016:     prim prim-c-code 2@  s" SET_IP" search nip nip 0<> ;
                   1017: 
                   1018: : output-superend ( -- )
                   1019:     \ output flag specifying whether the current word ends a dynamic superinst
                   1020:     prim prim-branch?
                   1021:     prim prim-c-code 2@  s" SUPER_END" search nip nip 0<> or
                   1022:     prim prim-c-code 2@  s" SUPER_CONTINUE" search nip nip 0= and
                   1023:     negate 0 .r ." , /* " prim prim-name 2@ type ."  */" cr ;
                   1024: 
                   1025: : gen-arg-parm { item -- }
                   1026:     item item-stack @ inst-stream = if
                   1027:        ." , " item item-type @ type-c-name 2@ type space
                   1028:        item item-name 2@ type
                   1029:     endif ;
                   1030: 
                   1031: : gen-args-parm ( -- )
                   1032:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
                   1033: 
                   1034: : gen-arg-gen { item -- }
                   1035:     item item-stack @ inst-stream = if
                   1036:        ."   genarg_" item item-type @ print-type-prefix
                   1037:         ." (ctp, " item item-name 2@ type ." );" cr
                   1038:     endif ;
                   1039: 
                   1040: : gen-args-gen ( -- )
                   1041:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
                   1042: 
                   1043: : output-gen ( -- )
                   1044:     \ generate C code for generating VM instructions
                   1045:     ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
                   1046:     ." {" cr
                   1047:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
                   1048:     gen-args-gen
                   1049:     ." }" cr ;
                   1050: 
                   1051: : stack-used? { stack -- f }
                   1052:     stack stack-in @ stack stack-out @ or 0<> ;
                   1053: 
                   1054: : output-funclabel ( -- )
                   1055:   ." &I_" prim prim-c-name 2@ type ." ," cr ;
                   1056: 
                   1057: : output-forthname ( -- )
                   1058:   '" emit prim prim-name 2@ type '" emit ." ," cr ;
                   1059: 
                   1060: \  : output-c-func ( -- )
                   1061: \  \ used for word libraries
                   1062: \      ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP)      /* " prim prim-name 2@ type
                   1063: \      ."  ( " prim prim-stack-string 2@ type ."  ) */" cr
                   1064: \      ." /* " prim prim-doc 2@ type ."  */" cr
                   1065: \      ." NAME(" quote prim prim-name 2@ type quote ." )" cr
                   1066: \      \ debugging
                   1067: \      ." {" cr
                   1068: \      print-declarations
                   1069: \      \ !! don't know what to do about that
                   1070: \      inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
                   1071: \      data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
                   1072: \      fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
                   1073: \      return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1.5     ! anton    1074: \      spill-state
1.1       anton    1075: \      fetches
                   1076: \      stack-pointer-updates
                   1077: \      fp-stack   stack-used? IF ." *FP=fp;" cr THEN
                   1078: \      ." {" cr
                   1079: \      ." #line " c-line @ . quote c-filename 2@ type quote cr
                   1080: \      prim prim-c-code 2@ type
                   1081: \      ." }" cr
                   1082: \      stores
1.5     ! anton    1083: \      fill-state
1.1       anton    1084: \      ." return (sp);" cr
                   1085: \      ." }" cr
                   1086: \      cr ;
                   1087: 
                   1088: : output-label ( -- )  
                   1089:     ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
                   1090: 
                   1091: : output-alias ( -- ) 
                   1092:     ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
                   1093: 
                   1094: : output-c-prim-num ( -- )
                   1095:     ." N_" prim prim-c-name 2@ type ." ," cr ;
                   1096: 
                   1097: : output-forth ( -- )  
                   1098:     prim prim-forth-code @ 0=
                   1099:     IF         \ output-alias
                   1100:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                   1101:        \ JAW
                   1102:     ELSE  ." : " prim prim-name 2@ type ."   ( "
                   1103:        prim prim-stack-string 2@ type ." )" cr
                   1104:        prim prim-forth-code 2@ type cr
                   1105:     THEN ;
                   1106: 
                   1107: : output-tag-file ( -- )
                   1108:     name-filename 2@ last-name-filename 2@ compare if
                   1109:        name-filename 2@ last-name-filename 2!
                   1110:        #ff emit cr
                   1111:        name-filename 2@ type
                   1112:        ." ,0" cr
                   1113:     endif ;
                   1114: 
                   1115: : output-tag ( -- )
                   1116:     output-tag-file
                   1117:     prim prim-name 2@ 1+ type
                   1118:     127 emit
                   1119:     space prim prim-name 2@ type space
                   1120:     1 emit
                   1121:     name-line @ 0 .r
                   1122:     ." ,0" cr ;
                   1123: 
                   1124: : output-vi-tag ( -- )
                   1125:     name-filename 2@ type #tab emit
                   1126:     prim prim-name 2@ type #tab emit
                   1127:     ." /^" prim prim-name 2@ type ."  *(/" cr ;
                   1128: 
                   1129: [IFDEF] documentation
                   1130: : register-doc ( -- )
                   1131:     prim prim-name 2@ documentation ['] create insert-wordlist
                   1132:     prim prim-name 2@ 2,
                   1133:     prim prim-stack-string 2@ condition-stack-effect 2,
                   1134:     prim prim-wordset 2@ 2,
                   1135:     prim prim-c-name 2@ condition-pronounciation 2,
                   1136:     prim prim-doc 2@ 2, ;
                   1137: [THEN]
                   1138: 
                   1139: 
                   1140: \ combining instructions
                   1141: 
                   1142: \ The input should look like this:
                   1143: 
                   1144: \ lit_+ = lit +
                   1145: 
                   1146: \ The output should look like this:
                   1147: 
                   1148: \  I_lit_+:
                   1149: \  {
                   1150: \  DEF_CA
                   1151: \  Cell _x_ip0;
                   1152: \  Cell _x_sp0;
                   1153: \  Cell _x_sp1;
                   1154: \  NEXT_P0;
                   1155: \  _x_ip0 = (Cell) IPTOS;
                   1156: \  _x_sp0 = (Cell) spTOS;
                   1157: \  INC_IP(1);
                   1158: \  /* sp += 0; */
                   1159: \  /* lit ( #w -- w ) */
                   1160: \  /*  */
                   1161: \  NAME("lit")
                   1162: \  {
                   1163: \  Cell w;
                   1164: \  w = (Cell) _x_ip0;
                   1165: \  #ifdef VM_DEBUG
                   1166: \  if (vm_debug) {
                   1167: \  fputs(" w=", vm_out); printarg_w (w);
                   1168: \  fputc('\n', vm_out);
                   1169: \  }
                   1170: \  #endif
                   1171: \  {
                   1172: \  #line 136 "./prim"
                   1173: \  }
                   1174: \  _x_sp1 = (Cell)w;
                   1175: \  }
                   1176: \  I_plus:     /* + ( n1 n2 -- n ) */
                   1177: \  /*  */
                   1178: \  NAME("+")
                   1179: \  {
                   1180: \  DEF_CA
                   1181: \  Cell n1;
                   1182: \  Cell n2;
                   1183: \  Cell n;
                   1184: \  NEXT_P0;
                   1185: \  n1 = (Cell) _x_sp0;
                   1186: \  n2 = (Cell) _x_sp1;
                   1187: \  #ifdef VM_DEBUG
                   1188: \  if (vm_debug) {
                   1189: \  fputs(" n1=", vm_out); printarg_n (n1);
                   1190: \  fputs(" n2=", vm_out); printarg_n (n2);
                   1191: \  fputc('\n', vm_out);
                   1192: \  }
                   1193: \  #endif
                   1194: \  {
                   1195: \  #line 516 "./prim"
                   1196: \  n = n1+n2;
                   1197: \  }
                   1198: \  _x_sp0 = (Cell)n;
                   1199: \  }
                   1200: \  NEXT_P1;
                   1201: \  spTOS = (Cell)_x_sp0;
                   1202: \  NEXT_P2;
                   1203: 
                   1204: : init-combined ( -- )
                   1205:     prim to combined
                   1206:     0 num-combined !
                   1207:     current-depth max-stacks cells erase
                   1208:     include-skipped-insts @ current-depth 0 th !
                   1209:     max-depth     max-stacks cells erase
                   1210:     min-depth     max-stacks cells erase
                   1211:     prim prim-effect-in  prim prim-effect-in-end  !
                   1212:     prim prim-effect-out prim prim-effect-out-end ! ;
                   1213: 
                   1214: : max! ( n addr -- )
                   1215:     tuck @ max swap ! ;
                   1216: 
                   1217: : min! ( n addr -- )
                   1218:     tuck @ min swap ! ;
                   1219: 
                   1220: : inst-stream-adjustment ( nstack -- n )
                   1221:     \ number of stack items to add for each part
                   1222:     0= include-skipped-insts @ and negate ;
                   1223: 
                   1224: : add-depths { p -- }
                   1225:     \ combine stack effect of p with *-depths
                   1226:     max-stacks 0 ?do
                   1227:        current-depth i th @
                   1228:        p prim-stacks-in  i th @ + i inst-stream-adjustment +
                   1229:        dup max-depth i th max!
                   1230:        p prim-stacks-out i th @ -
                   1231:        dup min-depth i th min!
                   1232:        current-depth i th !
                   1233:     loop ;
                   1234: 
                   1235: : copy-maxdepths ( n -- )
                   1236:     max-depth max-depths rot max-stacks * th max-stacks cells move ;
                   1237: 
                   1238: : add-prim ( addr u -- )
                   1239:     \ add primitive given by "addr u" to combined-prims
                   1240:     primitives search-wordlist s" unknown primitive" ?print-error
                   1241:     execute { p }
                   1242:     p combined-prims num-combined @ th !
                   1243:     num-combined @ copy-maxdepths
                   1244:     1 num-combined +!
                   1245:     p add-depths
                   1246:     num-combined @ copy-maxdepths ;
                   1247: 
                   1248: : compute-effects { q -- }
                   1249:     \ compute the stack effects of q from the depths
                   1250:     max-stacks 0 ?do
                   1251:        max-depth i th @ dup
                   1252:        q prim-stacks-in i th !
                   1253:        current-depth i th @ -
                   1254:        q prim-stacks-out i th !
                   1255:     loop ;
                   1256: 
                   1257: : make-effect-items { stack# items effect-endp -- }
                   1258:     \ effect-endp points to a pointer to the end of the current item-array
                   1259:     \ and has to be updated
                   1260:     stacks stack# th @ { stack }
                   1261:     items 0 +do
                   1262:        effect-endp @ { item }
                   1263:        i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
                   1264:        item item-name 2!
                   1265:        stack item item-stack !
                   1266:        stack stack-type @ item item-type !
                   1267:        i item item-offset !
                   1268:        item item-first on
                   1269:        item% %size effect-endp +!
                   1270:     loop ;
                   1271: 
                   1272: : init-effects { q -- }
                   1273:     \ initialize effects field for FETCHES and STORES
                   1274:     max-stacks 0 ?do
                   1275:        i q prim-stacks-in  i th @ q prim-effect-in-end  make-effect-items
                   1276:        i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
                   1277:     loop ;
                   1278: 
                   1279: : compute-stack-max-back-depths ( stack -- )
                   1280:     stack-number @ { stack# }
                   1281:     current-depth stack# th @ dup
                   1282:     dup stack# num-combined @ s-c-max-back-depth !
                   1283:     -1 num-combined @ 1- -do ( max-depth current-depth )
                   1284:        combined-prims i th @ { p }
                   1285:        p prim-stacks-out stack# th @ +
                   1286:        dup >r max r>
                   1287:        over stack# i s-c-max-back-depth !
                   1288:        p prim-stacks-in stack# th @ -
                   1289:        stack# inst-stream-adjustment -
                   1290:     1 -loop
                   1291:     assert( dup stack# inst-stream-adjustment negate = )
                   1292:     assert( over max-depth stack# th @ = )
                   1293:     2drop ;
                   1294: 
                   1295: : compute-max-back-depths ( -- )
                   1296:     \ compute max-back-depths.
                   1297:     \ assumes that current-depths is correct for the end of the combination
                   1298:     ['] compute-stack-max-back-depths map-stacks ;
                   1299: 
                   1300: : process-combined ( -- )
                   1301:     combined combined-prims num-combined @ cells
                   1302:     combinations ['] constant insert-wordlist
                   1303:     combined-prims num-combined @ 1- th ( last-part )
                   1304:     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
                   1305:     prim compute-effects
                   1306:     prim init-effects
                   1307:     compute-max-back-depths
                   1308:     output-combined perform ;
                   1309: 
                   1310: \ C output
                   1311: 
                   1312: : print-item { n stack -- }
                   1313:     \ print nth stack item name
                   1314:     stack stack-type @ type-c-name 2@ type space
                   1315:     ." MAYBE_UNUSED _" stack stack-pointer 2@ type n 0 .r ;
                   1316: 
                   1317: : print-declarations-combined ( -- )
                   1318:     max-stacks 0 ?do
                   1319:        max-depth i th @ min-depth i th @ - 0 +do
                   1320:            i stacks j th @ print-item ." ;" cr
                   1321:        loop
                   1322:     loop ;
                   1323: 
                   1324: : part-fetches ( -- )
                   1325:     fetches ;
                   1326: 
                   1327: : part-output-c-tail ( -- )
                   1328:     print-debug-results
                   1329:     stores ;
                   1330: 
                   1331: : output-combined-tail ( -- )
                   1332:     part-output-c-tail
                   1333:     in-part @ >r in-part off
                   1334:     combined ['] output-c-tail-no-stores prim-context
                   1335:     r> in-part ! ;
                   1336: 
                   1337: : part-stack-pointer-updates ( -- )
                   1338:     next-stack-number @ 0 +do
                   1339:        i part-num @ 1+ s-c-max-depth @ dup
                   1340:        i num-combined @ s-c-max-depth @ =    \ final depth
                   1341:        swap i part-num @ s-c-max-depth @ <> \ just reached now
                   1342:        part-num @ 0= \ first part
                   1343:        or and if
                   1344:            stacks i th @ stack-pointer-update
                   1345:        endif
                   1346:     loop ;
                   1347: 
                   1348: : output-part ( p -- )
                   1349:     to prim
                   1350:     ." /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                   1351:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
                   1352:     ." {" cr
                   1353:     print-declarations
                   1354:     part-fetches
                   1355:     print-debug-args
                   1356:     combined ['] part-stack-pointer-updates prim-context
                   1357:     1 part-num +!
                   1358:     prim add-depths \ !! right place?
                   1359:     prim prim-c-code 2@ ['] output-combined-tail type-c-code
                   1360:     part-output-c-tail
                   1361:     ." }" cr ;
                   1362: 
                   1363: : output-parts ( -- )
                   1364:     prim >r in-part on
                   1365:     current-depth max-stacks cells erase
                   1366:     0 part-num !
                   1367:     ['] output-part map-combined
                   1368:     in-part off
                   1369:     r> to prim ;
                   1370: 
                   1371: : output-c-combined ( -- )
                   1372:     print-entry cr
                   1373:     \ debugging messages just in parts
                   1374:     ." {" cr
                   1375:     ." DEF_CA" cr
                   1376:     print-declarations-combined
                   1377:     ." NEXT_P0;" cr
1.5     ! anton    1378:     spill-state
1.1       anton    1379:     \ fetches \ now in parts
                   1380:     \ print-debug-args
                   1381:     \ stack-pointer-updates now in parts
                   1382:     output-parts
                   1383:     output-c-tail2-no-stores
                   1384:     ." }" cr
                   1385:     cr ;
                   1386: 
                   1387: : output-forth-combined ( -- )
                   1388: ;
                   1389: 
                   1390: 
                   1391: \ peephole optimization rules
                   1392: 
                   1393: \ data for a simple peephole optimizer that always tries to combine
                   1394: \ the currently compiled instruction with the last one.
                   1395: 
                   1396: \ in order for this to work as intended, shorter combinations for each
                   1397: \ length must be present, and the longer combinations must follow
                   1398: \ shorter ones (this restriction may go away in the future).
                   1399:   
                   1400: : output-peephole ( -- )
                   1401:     combined-prims num-combined @ 1- cells combinations search-wordlist
                   1402:     s" the prefix for this superinstruction must be defined earlier" ?print-error
                   1403:     ." {"
                   1404:     execute prim-num @ 5 .r ." ,"
                   1405:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
                   1406:     combined prim-num @ 5 .r ." }, /* "
                   1407:     combined prim-c-name 2@ type ."  */"
                   1408:     cr ;
                   1409: 
                   1410: 
                   1411: \ cost and superinstruction data for a sophisticated combiner (e.g.,
                   1412: \ shortest path)
                   1413: 
                   1414: \ This is intended as initializer for a structure like this
                   1415: 
                   1416: \  struct cost {
                   1417: \    int loads;       /* number of stack loads */
                   1418: \    int stores;      /* number of stack stores */
                   1419: \    int updates;     /* number of stack pointer updates */
                   1420: \    int offset;      /* offset into super2 table */
                   1421: \    int length;      /* number of components */
                   1422: \  };
                   1423: 
                   1424: \ How do you know which primitive or combined instruction this
                   1425: \ structure refers to?  By the order of cost structures, as in most
                   1426: \ other cases.
                   1427: 
                   1428: : super2-length ( -- n )
                   1429:     combined if
                   1430:        num-combined @
                   1431:     else
                   1432:        1
                   1433:     endif ;
                   1434: 
                   1435: : compute-costs { p -- nloads nstores nupdates }
                   1436:     \ compute the number of loads, stores, and stack pointer updates
                   1437:     \ of a primitive or combined instruction; does not take TOS
                   1438:     \ caching into account
                   1439:     0 max-stacks 0 +do
                   1440:        p prim-stacks-in i th @ +
                   1441:     loop
                   1442:     super2-length 1- - \ don't count instruction fetches of subsumed insts
                   1443:     0 max-stacks 0 +do
                   1444:        p prim-stacks-out i th @ +
                   1445:     loop
                   1446:     0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
                   1447:        p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
                   1448:     loop ;
                   1449: 
                   1450: : output-num-part ( p -- )
                   1451:     ." N_" prim-c-name 2@ type ." ," ;
                   1452:     \ prim-num @ 4 .r ." ," ;
                   1453: 
                   1454: : output-name-comment ( -- )
                   1455:     ."  /* " prim prim-name 2@ type ."  */" ;
                   1456: 
                   1457: variable offset-super2  0 offset-super2 ! \ offset into the super2 table
                   1458: 
                   1459: : output-costs-prefix ( -- )
                   1460:     ." {" prim compute-costs
                   1461:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
                   1462:     prim prim-branch? negate . ." ," ;
                   1463: 
                   1464: : output-costs-gforth-simple ( -- )
                   1465:     output-costs-prefix
                   1466:     prim output-num-part
                   1467:     1 2 .r ." },"
                   1468:     output-name-comment
                   1469:     cr ;
                   1470: 
                   1471: : output-costs-gforth-combined ( -- )
                   1472:     output-costs-prefix
                   1473:     ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
                   1474:     super2-length dup 2 .r ." }," offset-super2 +!
                   1475:     output-name-comment
                   1476:     cr ;
                   1477: 
                   1478: : output-costs ( -- )
                   1479:     \ description of superinstructions and simple instructions
                   1480:     ." {" prim compute-costs
                   1481:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
                   1482:     offset-super2 @ 5 .r ." ,"
                   1483:     super2-length dup 2 .r ." }," offset-super2 +!
                   1484:     output-name-comment
                   1485:     cr ;
                   1486: 
                   1487: : output-super2 ( -- )
                   1488:     \ table of superinstructions without requirement for existing prefixes
                   1489:     combined if
                   1490:        ['] output-num-part map-combined 
                   1491:     else
                   1492:        prim output-num-part
                   1493:     endif
                   1494:     output-name-comment
                   1495:     cr ;   
                   1496: 
                   1497: \ the parser
                   1498: 
                   1499: eof-char max-member \ the whole character set + EOF
                   1500: 
                   1501: : getinput ( -- n )
                   1502:  rawinput @ endrawinput @ =
                   1503:  if
                   1504:    eof-char
                   1505:  else
                   1506:    cookedinput @ c@
                   1507:  endif ;
                   1508: 
                   1509: :noname ( n -- )
                   1510:  dup bl > if
                   1511:   emit space
                   1512:  else
                   1513:   .
                   1514:  endif ;
                   1515: print-token !
                   1516: 
                   1517: : testchar? ( set -- f )
                   1518:  getinput member? ;
                   1519: ' testchar? test-vector !
                   1520: 
                   1521: : checksynclines ( -- )
                   1522:     \ when input points to a newline, check if the next line is a
                   1523:     \ sync line.  If it is, perform the appropriate actions.
                   1524:     rawinput @ begin >r
                   1525:        s" #line " r@ over compare if
                   1526:            rdrop 1 line +! EXIT
                   1527:        endif
                   1528:        0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
                   1529:        dup c@ bl = if
                   1530:            char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
                   1531:            char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
                   1532:            char+
                   1533:        endif
                   1534:        dup c@ nl-char <> 0= s" sync line syntax" ?print-error
                   1535:        skipsynclines @ if
                   1536:            char+ dup rawinput !
                   1537:            rawinput @ c@ cookedinput @ c!
                   1538:        endif
                   1539:     again ;
                   1540: 
                   1541: : ?nextchar ( f -- )
                   1542:     s" syntax error, wrong char" ?print-error
                   1543:     rawinput @ endrawinput @ <> if
                   1544:        rawinput @ c@
                   1545:        1 chars rawinput +!
                   1546:        1 chars cookedinput +!
                   1547:        nl-char = if
                   1548:            checksynclines
                   1549:            rawinput @ line-start !
                   1550:        endif
                   1551:        rawinput @ c@
                   1552:        cookedinput @ c!
                   1553:     endif ;
                   1554: 
                   1555: : charclass ( set "name" -- )
                   1556:  ['] ?nextchar terminal ;
                   1557: 
                   1558: : .. ( c1 c2 -- set )
                   1559:  ( creates a set that includes the characters c, c1<=c<=c2 )
                   1560:  empty copy-set
                   1561:  swap 1+ rot do
                   1562:   i over add-member
                   1563:  loop ;
                   1564: 
                   1565: : ` ( -- terminal ) ( use: ` c )
                   1566:  ( creates anonymous terminal for the character c )
                   1567:  char singleton ['] ?nextchar make-terminal ;
                   1568: 
                   1569: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
                   1570: char 0 char 9 ..                                       charclass digit
                   1571: bl singleton tab-char over add-member                  charclass white
                   1572: nl-char singleton eof-char over add-member complement  charclass nonl
                   1573: nl-char singleton eof-char over add-member
                   1574:     char : over add-member complement                   charclass nocolonnl
                   1575: nl-char singleton eof-char over add-member
                   1576:     char } over add-member complement                   charclass nobracenl
                   1577: bl 1+ maxchar .. char \ singleton complement intersection
                   1578:                                                         charclass nowhitebq
                   1579: bl 1+ maxchar ..                                        charclass nowhite
                   1580: char " singleton eof-char over add-member complement   charclass noquote
                   1581: nl-char singleton                                      charclass nl
                   1582: eof-char singleton                                     charclass eof
                   1583: nl-char singleton eof-char over add-member             charclass nleof
                   1584: 
                   1585: (( letter (( letter || digit )) **
                   1586: )) <- c-ident ( -- )
                   1587: 
                   1588: (( ` # ?? (( letter || digit || ` : )) ++
                   1589: )) <- stack-ident ( -- )
                   1590: 
                   1591: (( nowhitebq nowhite ** ))
                   1592: <- forth-ident ( -- )
                   1593: 
                   1594: Variable forth-flag
                   1595: Variable c-flag
                   1596: 
                   1597: (( (( ` e || ` E )) {{ start }} nonl ** 
                   1598:    {{ end evaluate }}
                   1599: )) <- eval-comment ( ... -- ... )
                   1600: 
                   1601: (( (( ` f || ` F )) {{ start }} nonl ** 
                   1602:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
                   1603: )) <- forth-comment ( -- )
                   1604: 
                   1605: (( (( ` c || ` C )) {{ start }} nonl ** 
                   1606:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
                   1607: )) <- c-comment ( -- )
                   1608: 
                   1609: (( ` - nonl ** {{ 
                   1610:        forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
                   1611:        c-flag @ IF
                   1612:            function-diff
                   1613:            ." #else /* " function-number @ 0 .r ."  */" cr THEN }}
                   1614: )) <- else-comment
                   1615: 
                   1616: (( ` + {{ start }} nonl ** {{ end
                   1617:        dup
                   1618:        IF      c-flag @
                   1619:            IF
                   1620:                function-diff
                   1621:                ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
                   1622:                THEN
                   1623:                forth-flag @
                   1624:                IF  forth-fdiff  ." has? " type ."  [IF]"  cr THEN
                   1625:        ELSE    2drop
                   1626:            c-flag @      IF
                   1627:                function-diff  ." #endif" cr THEN
                   1628:            forth-flag @  IF  forth-fdiff  ." [THEN]"  cr THEN
                   1629:        THEN }}
                   1630: )) <- if-comment
                   1631: 
                   1632: (( (( ` g || ` G )) {{ start }} nonl **
                   1633:    {{ end
                   1634:       forth-flag @ IF  forth-fdiff  ." group " type cr  THEN
                   1635:       c-flag @     IF  function-diff
                   1636:          ." GROUP(" type ." , " function-number @ 0 .r ." )" cr  THEN }}
                   1637: )) <- group-comment
                   1638: 
                   1639: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
                   1640: 
                   1641: (( ` \ comment-body nleof )) <- comment ( -- )
                   1642: 
                   1643: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
                   1644: <- stack-items
                   1645: 
                   1646: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
                   1647:    ` - ` - white **
                   1648:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
                   1649: )) <- stack-effect ( -- )
                   1650: 
                   1651: (( {{ prim create-prim }}
                   1652:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
                   1653:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
                   1654:       (( {{ start }}  c-ident {{ end prim prim-c-name 2! }} )) ??
                   1655:    )) ??  nleof
                   1656:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
                   1657:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
                   1658:    (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
                   1659:    || (( nocolonnl nonl **  nleof white ** )) ** ))
                   1660:    {{ end prim prim-c-code 2! skipsynclines on }}
                   1661:    (( ` :  white ** nleof
                   1662:       {{ start }} (( nonl ++  nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
                   1663:    )) ?? {{ process-simple }}
                   1664:    nleof
                   1665: )) <- simple-primitive ( -- )
                   1666: 
                   1667: (( {{ init-combined }}
                   1668:    ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
                   1669:    nleof {{ process-combined }}
                   1670: )) <- combined-primitive
                   1671: 
                   1672: (( {{ make-prim to prim 0 to combined
                   1673:       line @ name-line ! filename 2@ name-filename 2!
                   1674:       function-number @ prim prim-num !
                   1675:       start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
                   1676:       2dup prim prim-name 2! prim prim-c-name 2! }}  white **
                   1677:    (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
                   1678:    (( simple-primitive || combined-primitive ))
                   1679:    {{ 1 function-number +! }}
                   1680: )) <- primitive ( -- )
                   1681: 
                   1682: (( (( comment || primitive || nl white ** )) ** eof ))
                   1683: parser primitives2something
                   1684: warnings @ [IF]
                   1685: .( parser generated ok ) cr
                   1686: [THEN]
                   1687: 
                   1688: 
                   1689: \ run with gforth-0.5.0 (slurp-file is missing)
                   1690: [IFUNDEF] slurp-file
                   1691: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
                   1692:     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
                   1693:     r/o bin open-file throw >r
                   1694:     r@ file-size throw abort" file too large"
                   1695:     dup allocate throw swap
                   1696:     2dup r@ read-file throw over <> abort" could not read whole file"
                   1697:     r> close-file throw ;
                   1698: [THEN]
                   1699: 
                   1700: : primfilter ( addr u -- )
                   1701:     \ process the string at addr u
                   1702:     over dup rawinput ! dup line-start ! cookedinput !
                   1703:     + endrawinput !
                   1704:     checksynclines
                   1705:     primitives2something ;    
                   1706: 
                   1707: : unixify ( c-addr u1 -- c-addr u2 )
                   1708:     \ delete crs from the string
                   1709:     bounds tuck tuck ?do ( c-addr1 )
                   1710:        i c@ dup #cr <> if
                   1711:            over c! char+
                   1712:        else
                   1713:            drop
                   1714:        endif
                   1715:     loop
                   1716:     over - ;
                   1717: 
                   1718: : process-file ( addr u xt-simple x-combined -- )
                   1719:     output-combined ! output !
                   1720:     save-mem 2dup filename 2!
                   1721:     slurp-file unixify
                   1722:     warnings @ if
                   1723:        ." ------------ CUT HERE -------------" cr  endif
                   1724:     primfilter ;
                   1725: 
                   1726: \  : process      ( xt -- )
                   1727: \      bl word count rot
                   1728: \      process-file ;

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