File:  [gforth] / gforth / prims2x.fs
Revision 1.172: download - view: text, annotated - select for diffs
Sun May 2 16:21:32 2010 UTC (13 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
renamed ;CODE-EXEC into LIT-EXECUTE (more general)
static stack caching for faster abi-call and ;abi-code-exec
Usage of VM_JUMP in static superinstructions is now flagged

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

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