File:  [gforth] / gforth / Attic / prims2y.fs
Revision 1.2: download - view: text, annotated - select for diffs
Sun Sep 21 18:54:38 2003 UTC (17 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
started on stack caching

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

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