Annotation of gforth/prims2y.fs, revision 1.3

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

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