Annotation of gforth/prims2x.fs, revision 1.157

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

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