Annotation of gforth/prims2x.fs, revision 1.170

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

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