Annotation of gforth/prims2x.fs, revision 1.124

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

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