File:  [gforth] / gforth / prims2x.fs
Revision 1.158: download - view: text, annotated - select for diffs
Thu Jul 28 19:15:00 2005 UTC (14 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
fixed a few bugs in "..." handling in prims2x.fs
changed direct uses of sp and fp in prim to use "..."

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

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