Annotation of gforth/prims2x.fs, revision 1.136

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

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