Annotation of gforth/prims2y.fs, revision 1.7

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

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