File:  [gforth] / gforth / prims2x.fs
Revision 1.119: download - view: text, annotated - select for diffs
Sat Oct 12 18:36:25 2002 UTC (21 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
In superinstructions, move stores into the last part writing to the stack item.

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

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