Annotation of gforth/prims2y.fs, revision 1.6

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: 
1.6     ! anton    1310: \ reprocessing (typically to generate versions for another cache states)
        !          1311: 
        !          1312: variable reprocessed-num 0 reprocessed-num !
        !          1313: 
        !          1314: : new-name ( -- c-addr u )
        !          1315:     reprocessed-num @ 0
        !          1316:     1 reprocessed-num +!
        !          1317:     <# #s 'p hold '_ hold #> save-mem ;
        !          1318: 
        !          1319: : reprocess-simple ( prim -- )
        !          1320:     to prim
        !          1321:     new-name prim prim-c-name 2!
        !          1322:     output @ execute ;
        !          1323: 
        !          1324: : lookup-prim ( c-addr u -- prim )
        !          1325:     primitives search-wordlist 0= -13 and throw execute ;
        !          1326: 
        !          1327: : state-prim1 { in-state out-state prim -- }
        !          1328:     in-state  to state-in
        !          1329:     out-state to state-out
        !          1330:     prim reprocess-simple ;
        !          1331: 
        !          1332: : state-prim ( in-state out-state "name" -- )
        !          1333:     parse-word lookup-prim state-prim1 ;
        !          1334: 
        !          1335: \ reprocessing with default states
        !          1336: 
        !          1337: \ This is a simple scheme and should be generalized
        !          1338: \ assumes we only cache one stack and use simple states for that
        !          1339: 
        !          1340: 0 value cache-stack  \ stack that we cache
        !          1341: 2variable cache-states \ states of the cache, starting with the empty state
        !          1342: 
        !          1343: : compute-default-state-out ( n-in -- n-out )
        !          1344:     \ for the current prim
        !          1345:     cache-stack stack-in @ - 0 max
        !          1346:     cache-stack stack-out @ + cache-states 2@ nip 1- min ;
        !          1347: 
        !          1348: : gen-prim-states ( prim -- )
        !          1349:     to prim
        !          1350:     cache-states 2@ swap { states } ( nstates )
        !          1351:     cache-stack stack-in @ +do
        !          1352:        states i th @
        !          1353:        states i compute-default-state-out th @
        !          1354:        prim state-prim1
        !          1355:     loop ;
        !          1356: 
        !          1357: : prim-states ( "name" -- )
        !          1358:     parse-word lookup-prim gen-prim-states ;
        !          1359: 
1.1       anton    1360: \ C output
                   1361: 
                   1362: : print-item { n stack -- }
                   1363:     \ print nth stack item name
                   1364:     stack stack-type @ type-c-name 2@ type space
                   1365:     ." MAYBE_UNUSED _" stack stack-pointer 2@ type n 0 .r ;
                   1366: 
                   1367: : print-declarations-combined ( -- )
                   1368:     max-stacks 0 ?do
                   1369:        max-depth i th @ min-depth i th @ - 0 +do
                   1370:            i stacks j th @ print-item ." ;" cr
                   1371:        loop
                   1372:     loop ;
                   1373: 
                   1374: : part-fetches ( -- )
                   1375:     fetches ;
                   1376: 
                   1377: : part-output-c-tail ( -- )
                   1378:     print-debug-results
                   1379:     stores ;
                   1380: 
                   1381: : output-combined-tail ( -- )
                   1382:     part-output-c-tail
                   1383:     in-part @ >r in-part off
                   1384:     combined ['] output-c-tail-no-stores prim-context
                   1385:     r> in-part ! ;
                   1386: 
                   1387: : part-stack-pointer-updates ( -- )
                   1388:     next-stack-number @ 0 +do
                   1389:        i part-num @ 1+ s-c-max-depth @ dup
                   1390:        i num-combined @ s-c-max-depth @ =    \ final depth
                   1391:        swap i part-num @ s-c-max-depth @ <> \ just reached now
                   1392:        part-num @ 0= \ first part
                   1393:        or and if
                   1394:            stacks i th @ stack-pointer-update
                   1395:        endif
                   1396:     loop ;
                   1397: 
                   1398: : output-part ( p -- )
                   1399:     to prim
                   1400:     ." /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                   1401:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
                   1402:     ." {" cr
                   1403:     print-declarations
                   1404:     part-fetches
                   1405:     print-debug-args
                   1406:     combined ['] part-stack-pointer-updates prim-context
                   1407:     1 part-num +!
                   1408:     prim add-depths \ !! right place?
                   1409:     prim prim-c-code 2@ ['] output-combined-tail type-c-code
                   1410:     part-output-c-tail
                   1411:     ." }" cr ;
                   1412: 
                   1413: : output-parts ( -- )
                   1414:     prim >r in-part on
                   1415:     current-depth max-stacks cells erase
                   1416:     0 part-num !
                   1417:     ['] output-part map-combined
                   1418:     in-part off
                   1419:     r> to prim ;
                   1420: 
                   1421: : output-c-combined ( -- )
                   1422:     print-entry cr
                   1423:     \ debugging messages just in parts
                   1424:     ." {" cr
                   1425:     ." DEF_CA" cr
                   1426:     print-declarations-combined
                   1427:     ." NEXT_P0;" cr
1.5       anton    1428:     spill-state
1.1       anton    1429:     \ fetches \ now in parts
                   1430:     \ print-debug-args
                   1431:     \ stack-pointer-updates now in parts
                   1432:     output-parts
                   1433:     output-c-tail2-no-stores
                   1434:     ." }" cr
                   1435:     cr ;
                   1436: 
                   1437: : output-forth-combined ( -- )
                   1438: ;
                   1439: 
                   1440: 
                   1441: \ peephole optimization rules
                   1442: 
                   1443: \ data for a simple peephole optimizer that always tries to combine
                   1444: \ the currently compiled instruction with the last one.
                   1445: 
                   1446: \ in order for this to work as intended, shorter combinations for each
                   1447: \ length must be present, and the longer combinations must follow
                   1448: \ shorter ones (this restriction may go away in the future).
                   1449:   
                   1450: : output-peephole ( -- )
                   1451:     combined-prims num-combined @ 1- cells combinations search-wordlist
                   1452:     s" the prefix for this superinstruction must be defined earlier" ?print-error
                   1453:     ." {"
                   1454:     execute prim-num @ 5 .r ." ,"
                   1455:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
                   1456:     combined prim-num @ 5 .r ." }, /* "
                   1457:     combined prim-c-name 2@ type ."  */"
                   1458:     cr ;
                   1459: 
                   1460: 
                   1461: \ cost and superinstruction data for a sophisticated combiner (e.g.,
                   1462: \ shortest path)
                   1463: 
                   1464: \ This is intended as initializer for a structure like this
                   1465: 
                   1466: \  struct cost {
                   1467: \    int loads;       /* number of stack loads */
                   1468: \    int stores;      /* number of stack stores */
                   1469: \    int updates;     /* number of stack pointer updates */
                   1470: \    int offset;      /* offset into super2 table */
                   1471: \    int length;      /* number of components */
                   1472: \  };
                   1473: 
                   1474: \ How do you know which primitive or combined instruction this
                   1475: \ structure refers to?  By the order of cost structures, as in most
                   1476: \ other cases.
                   1477: 
                   1478: : super2-length ( -- n )
                   1479:     combined if
                   1480:        num-combined @
                   1481:     else
                   1482:        1
                   1483:     endif ;
                   1484: 
                   1485: : compute-costs { p -- nloads nstores nupdates }
                   1486:     \ compute the number of loads, stores, and stack pointer updates
                   1487:     \ of a primitive or combined instruction; does not take TOS
                   1488:     \ caching into account
                   1489:     0 max-stacks 0 +do
                   1490:        p prim-stacks-in i th @ +
                   1491:     loop
                   1492:     super2-length 1- - \ don't count instruction fetches of subsumed insts
                   1493:     0 max-stacks 0 +do
                   1494:        p prim-stacks-out i th @ +
                   1495:     loop
                   1496:     0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
                   1497:        p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
                   1498:     loop ;
                   1499: 
                   1500: : output-num-part ( p -- )
                   1501:     ." N_" prim-c-name 2@ type ." ," ;
                   1502:     \ prim-num @ 4 .r ." ," ;
                   1503: 
                   1504: : output-name-comment ( -- )
                   1505:     ."  /* " prim prim-name 2@ type ."  */" ;
                   1506: 
                   1507: variable offset-super2  0 offset-super2 ! \ offset into the super2 table
                   1508: 
                   1509: : output-costs-prefix ( -- )
                   1510:     ." {" prim compute-costs
                   1511:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
                   1512:     prim prim-branch? negate . ." ," ;
                   1513: 
                   1514: : output-costs-gforth-simple ( -- )
                   1515:     output-costs-prefix
                   1516:     prim output-num-part
                   1517:     1 2 .r ." },"
                   1518:     output-name-comment
                   1519:     cr ;
                   1520: 
                   1521: : output-costs-gforth-combined ( -- )
                   1522:     output-costs-prefix
                   1523:     ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
                   1524:     super2-length dup 2 .r ." }," offset-super2 +!
                   1525:     output-name-comment
                   1526:     cr ;
                   1527: 
                   1528: : output-costs ( -- )
                   1529:     \ description of superinstructions and simple instructions
                   1530:     ." {" prim compute-costs
                   1531:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
                   1532:     offset-super2 @ 5 .r ." ,"
                   1533:     super2-length dup 2 .r ." }," offset-super2 +!
                   1534:     output-name-comment
                   1535:     cr ;
                   1536: 
                   1537: : output-super2 ( -- )
                   1538:     \ table of superinstructions without requirement for existing prefixes
                   1539:     combined if
                   1540:        ['] output-num-part map-combined 
                   1541:     else
                   1542:        prim output-num-part
                   1543:     endif
                   1544:     output-name-comment
                   1545:     cr ;   
                   1546: 
                   1547: \ the parser
                   1548: 
                   1549: eof-char max-member \ the whole character set + EOF
                   1550: 
                   1551: : getinput ( -- n )
                   1552:  rawinput @ endrawinput @ =
                   1553:  if
                   1554:    eof-char
                   1555:  else
                   1556:    cookedinput @ c@
                   1557:  endif ;
                   1558: 
                   1559: :noname ( n -- )
                   1560:  dup bl > if
                   1561:   emit space
                   1562:  else
                   1563:   .
                   1564:  endif ;
                   1565: print-token !
                   1566: 
                   1567: : testchar? ( set -- f )
                   1568:  getinput member? ;
                   1569: ' testchar? test-vector !
                   1570: 
                   1571: : checksynclines ( -- )
                   1572:     \ when input points to a newline, check if the next line is a
                   1573:     \ sync line.  If it is, perform the appropriate actions.
                   1574:     rawinput @ begin >r
                   1575:        s" #line " r@ over compare if
                   1576:            rdrop 1 line +! EXIT
                   1577:        endif
                   1578:        0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
                   1579:        dup c@ bl = if
                   1580:            char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
                   1581:            char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
                   1582:            char+
                   1583:        endif
                   1584:        dup c@ nl-char <> 0= s" sync line syntax" ?print-error
                   1585:        skipsynclines @ if
                   1586:            char+ dup rawinput !
                   1587:            rawinput @ c@ cookedinput @ c!
                   1588:        endif
                   1589:     again ;
                   1590: 
                   1591: : ?nextchar ( f -- )
                   1592:     s" syntax error, wrong char" ?print-error
                   1593:     rawinput @ endrawinput @ <> if
                   1594:        rawinput @ c@
                   1595:        1 chars rawinput +!
                   1596:        1 chars cookedinput +!
                   1597:        nl-char = if
                   1598:            checksynclines
                   1599:            rawinput @ line-start !
                   1600:        endif
                   1601:        rawinput @ c@
                   1602:        cookedinput @ c!
                   1603:     endif ;
                   1604: 
                   1605: : charclass ( set "name" -- )
                   1606:  ['] ?nextchar terminal ;
                   1607: 
                   1608: : .. ( c1 c2 -- set )
                   1609:  ( creates a set that includes the characters c, c1<=c<=c2 )
                   1610:  empty copy-set
                   1611:  swap 1+ rot do
                   1612:   i over add-member
                   1613:  loop ;
                   1614: 
                   1615: : ` ( -- terminal ) ( use: ` c )
                   1616:  ( creates anonymous terminal for the character c )
                   1617:  char singleton ['] ?nextchar make-terminal ;
                   1618: 
                   1619: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
                   1620: char 0 char 9 ..                                       charclass digit
                   1621: bl singleton tab-char over add-member                  charclass white
                   1622: nl-char singleton eof-char over add-member complement  charclass nonl
                   1623: nl-char singleton eof-char over add-member
                   1624:     char : over add-member complement                   charclass nocolonnl
                   1625: nl-char singleton eof-char over add-member
                   1626:     char } over add-member complement                   charclass nobracenl
                   1627: bl 1+ maxchar .. char \ singleton complement intersection
                   1628:                                                         charclass nowhitebq
                   1629: bl 1+ maxchar ..                                        charclass nowhite
                   1630: char " singleton eof-char over add-member complement   charclass noquote
                   1631: nl-char singleton                                      charclass nl
                   1632: eof-char singleton                                     charclass eof
                   1633: nl-char singleton eof-char over add-member             charclass nleof
                   1634: 
                   1635: (( letter (( letter || digit )) **
                   1636: )) <- c-ident ( -- )
                   1637: 
                   1638: (( ` # ?? (( letter || digit || ` : )) ++
                   1639: )) <- stack-ident ( -- )
                   1640: 
                   1641: (( nowhitebq nowhite ** ))
                   1642: <- forth-ident ( -- )
                   1643: 
                   1644: Variable forth-flag
                   1645: Variable c-flag
                   1646: 
                   1647: (( (( ` e || ` E )) {{ start }} nonl ** 
                   1648:    {{ end evaluate }}
                   1649: )) <- eval-comment ( ... -- ... )
                   1650: 
                   1651: (( (( ` f || ` F )) {{ start }} nonl ** 
                   1652:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
                   1653: )) <- forth-comment ( -- )
                   1654: 
                   1655: (( (( ` c || ` C )) {{ start }} nonl ** 
                   1656:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
                   1657: )) <- c-comment ( -- )
                   1658: 
                   1659: (( ` - nonl ** {{ 
                   1660:        forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
                   1661:        c-flag @ IF
                   1662:            function-diff
                   1663:            ." #else /* " function-number @ 0 .r ."  */" cr THEN }}
                   1664: )) <- else-comment
                   1665: 
                   1666: (( ` + {{ start }} nonl ** {{ end
                   1667:        dup
                   1668:        IF      c-flag @
                   1669:            IF
                   1670:                function-diff
                   1671:                ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
                   1672:                THEN
                   1673:                forth-flag @
                   1674:                IF  forth-fdiff  ." has? " type ."  [IF]"  cr THEN
                   1675:        ELSE    2drop
                   1676:            c-flag @      IF
                   1677:                function-diff  ." #endif" cr THEN
                   1678:            forth-flag @  IF  forth-fdiff  ." [THEN]"  cr THEN
                   1679:        THEN }}
                   1680: )) <- if-comment
                   1681: 
                   1682: (( (( ` g || ` G )) {{ start }} nonl **
                   1683:    {{ end
                   1684:       forth-flag @ IF  forth-fdiff  ." group " type cr  THEN
                   1685:       c-flag @     IF  function-diff
                   1686:          ." GROUP(" type ." , " function-number @ 0 .r ." )" cr  THEN }}
                   1687: )) <- group-comment
                   1688: 
                   1689: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
                   1690: 
                   1691: (( ` \ comment-body nleof )) <- comment ( -- )
                   1692: 
                   1693: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
                   1694: <- stack-items
                   1695: 
                   1696: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
                   1697:    ` - ` - white **
                   1698:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
                   1699: )) <- stack-effect ( -- )
                   1700: 
                   1701: (( {{ prim create-prim }}
                   1702:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
                   1703:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
                   1704:       (( {{ start }}  c-ident {{ end prim prim-c-name 2! }} )) ??
                   1705:    )) ??  nleof
                   1706:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
                   1707:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
                   1708:    (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
                   1709:    || (( nocolonnl nonl **  nleof white ** )) ** ))
                   1710:    {{ end prim prim-c-code 2! skipsynclines on }}
                   1711:    (( ` :  white ** nleof
                   1712:       {{ start }} (( nonl ++  nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
                   1713:    )) ?? {{ process-simple }}
                   1714:    nleof
                   1715: )) <- simple-primitive ( -- )
                   1716: 
                   1717: (( {{ init-combined }}
                   1718:    ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
                   1719:    nleof {{ process-combined }}
                   1720: )) <- combined-primitive
                   1721: 
                   1722: (( {{ make-prim to prim 0 to combined
                   1723:       line @ name-line ! filename 2@ name-filename 2!
                   1724:       function-number @ prim prim-num !
                   1725:       start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
                   1726:       2dup prim prim-name 2! prim prim-c-name 2! }}  white **
                   1727:    (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
                   1728:    (( simple-primitive || combined-primitive ))
                   1729:    {{ 1 function-number +! }}
                   1730: )) <- primitive ( -- )
                   1731: 
                   1732: (( (( comment || primitive || nl white ** )) ** eof ))
                   1733: parser primitives2something
                   1734: warnings @ [IF]
                   1735: .( parser generated ok ) cr
                   1736: [THEN]
                   1737: 
                   1738: 
                   1739: \ run with gforth-0.5.0 (slurp-file is missing)
                   1740: [IFUNDEF] slurp-file
                   1741: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
                   1742:     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
                   1743:     r/o bin open-file throw >r
                   1744:     r@ file-size throw abort" file too large"
                   1745:     dup allocate throw swap
                   1746:     2dup r@ read-file throw over <> abort" could not read whole file"
                   1747:     r> close-file throw ;
                   1748: [THEN]
                   1749: 
                   1750: : primfilter ( addr u -- )
                   1751:     \ process the string at addr u
                   1752:     over dup rawinput ! dup line-start ! cookedinput !
                   1753:     + endrawinput !
                   1754:     checksynclines
                   1755:     primitives2something ;    
                   1756: 
                   1757: : unixify ( c-addr u1 -- c-addr u2 )
                   1758:     \ delete crs from the string
                   1759:     bounds tuck tuck ?do ( c-addr1 )
                   1760:        i c@ dup #cr <> if
                   1761:            over c! char+
                   1762:        else
                   1763:            drop
                   1764:        endif
                   1765:     loop
                   1766:     over - ;
                   1767: 
                   1768: : process-file ( addr u xt-simple x-combined -- )
                   1769:     output-combined ! output !
                   1770:     save-mem 2dup filename 2!
                   1771:     slurp-file unixify
                   1772:     warnings @ if
                   1773:        ." ------------ CUT HERE -------------" cr  endif
                   1774:     primfilter ;
                   1775: 
                   1776: \  : process      ( xt -- )
                   1777: \      bl word count rot
                   1778: \      process-file ;

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