File:  [gforth] / gforth / prims2x.fs
Revision 1.118: download - view: text, annotated - select for diffs
Sat Oct 12 11:05:22 2002 UTC (16 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Stack loads in superinstructions are now delayed until the part that needs them

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

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