File:  [gforth] / gforth / prims2x.fs
Revision 1.156: download - view: text, annotated - select for diffs
Wed Jul 27 19:44:20 2005 UTC (14 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added support for ... stack items to prims2x.fs
minor cleanup in prim

    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 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 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: \ global vars
  355: variable c-line
  356: 2variable c-filename
  357: variable name-line
  358: 2variable name-filename
  359: 2variable last-name-filename
  360: Variable function-number 0 function-number !
  361: Variable function-old 0 function-old !
  362: : function-diff ( n -- )
  363:     ." GROUPADD(" function-number @ function-old @ - 0 .r ." )" cr
  364:     function-number @ function-old ! ;
  365: : forth-fdiff ( -- )
  366:     function-number @ function-old @ - 0 .r ."  groupadd" cr
  367:     function-number @ function-old ! ;
  368: 
  369: \ a few more set ops
  370: 
  371: : bit-equivalent ( w1 w2 -- w3 )
  372:  xor invert ;
  373: 
  374: : complement ( set1 -- set2 )
  375:  empty ['] bit-equivalent binary-set-operation ;
  376: 
  377: \ forward declaration for inst-stream (breaks cycle in definitions)
  378: defer inst-stream-f ( -- stack )
  379: 
  380: \ stack access stuff
  381: 
  382: : normal-stack-access0 { n stack -- }
  383:     \ n has the ss-offset already applied (see ...-access1)
  384:     n stack stack-access-transform @ execute ." [" 0 .r ." ]" ;
  385: 
  386: : state-ss { stack state -- ss }
  387:     state state-sss stack stack-number @ th @ ;
  388: 
  389: : stack-reg { n stack state -- reg }
  390:     \ n is the index (TOS=0); reg is 0 if the access is to memory
  391:     stack state state-ss ss-registers 2@ n u> if ( addr ) \ in ss-registers?
  392: 	n th @
  393:     else
  394: 	drop 0
  395:     endif ;
  396: 
  397: : .reg ( reg -- )
  398:     register-name 2@ type ;
  399: 
  400: : stack-offset ( stack state -- n )
  401:     \ offset for stack in state
  402:     state-ss ss-offset @ ;
  403: 
  404: : normal-stack-access1 { n stack state -- }
  405:     n stack state stack-reg ?dup-if
  406: 	.reg exit
  407:     endif
  408:     stack stack-pointer 2@ type
  409:     n stack state stack-offset - stack normal-stack-access0 ;
  410: 
  411: : normal-stack-access ( n stack state -- )
  412:     over inst-stream-f = if
  413: 	." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )"
  414: 	1 immarg +!
  415:     else
  416: 	normal-stack-access1
  417:     endif ;
  418: 
  419: : stack-depth { stack -- n }
  420:     current-depth stack stack-number @ th @ ;
  421: 
  422: : part-stack-access { n stack -- }
  423:     \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
  424:     ." _" stack stack-pointer 2@ type
  425:     stack stack-number @ { stack# }
  426:     stack stack-depth n + { access-depth }
  427:     stack inst-stream-f = if
  428: 	access-depth
  429:     else
  430: 	combined prim-stacks-in stack# th @
  431: 	assert( dup max-depth stack# th @ = )
  432: 	access-depth - 1-
  433:     endif
  434:     0 .r ;
  435: 
  436: : part-stack-read { n stack -- }
  437:     stack stack-depth n + ( ndepth )
  438:     stack stack-number @ part-num @ s-c-max-depth @
  439: \    max-depth stack stack-number @ th @ ( ndepth nmaxdepth )
  440:     over <= if ( ndepth ) \ load from memory
  441: 	stack state-in normal-stack-access
  442:     else
  443: 	drop n stack part-stack-access
  444:     endif ;
  445: 
  446: : stack-diff ( stack -- n )
  447:     \ in-out
  448:     dup stack-in @ swap stack-out @ - ;
  449: 
  450: : part-stack-write { n stack -- }
  451:     stack stack-depth n +
  452:     stack stack-number @ part-num @ s-c-max-back-depth @
  453:     over <= if ( ndepth )
  454: 	stack combined ['] stack-diff prim-context -
  455: 	stack state-out normal-stack-access
  456:     else
  457: 	drop n stack part-stack-access
  458:     endif ;
  459: 
  460: : stack-read ( n stack -- )
  461:     \ print a stack access at index n of stack
  462:     in-part @ if
  463: 	part-stack-read
  464:     else
  465: 	state-in normal-stack-access
  466:     endif ;
  467: 
  468: : stack-write ( n stack -- )
  469:     \ print a stack access at index n of stack
  470:     in-part @ if
  471: 	part-stack-write
  472:     else
  473: 	state-out normal-stack-access
  474:     endif ;
  475: 
  476: : item-in-index { item -- n }
  477:     \ n is the index of item (in the in-effect)
  478:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
  479:     item r> stack-in-index-xt @ execute ;
  480: 
  481: : item-stack-type-name ( item -- addr u )
  482:     item-stack @ stack-type @ type-c-name 2@ ;
  483: 
  484: : fetch-single ( item -- )
  485:     \ fetch a single stack item from its stack
  486:     >r
  487:     ." vm_" r@ item-stack-type-name type
  488:     ." 2" r@ item-type @ print-type-prefix ." ("
  489:     r@ item-in-index r@ item-stack @ stack-read ." ,"
  490:     r@ item-name 2@ type
  491:     ." );" cr
  492:     rdrop ; 
  493: 
  494: : fetch-double ( item -- )
  495:     \ fetch a double stack item from its stack
  496:     >r
  497:     ." vm_two"
  498:     r@ item-stack-type-name type ." 2"
  499:     r@ item-type @ print-type-prefix ." ("
  500:     r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-read
  501:     ." , "                      -1 under+ ." (Cell)" stack-read
  502:     ." , " r@ item-name 2@ type
  503:     ." )" cr
  504:     rdrop ;
  505: 
  506: : same-as-in? ( item -- f )
  507:  \ f is true iff the offset and stack of item is the same as on input
  508:  >r
  509:  r@ item-first @ if
  510:      rdrop false exit
  511:  endif
  512:  r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
  513:  execute @
  514:  dup r@ =
  515:  if \ item first appeared in output
  516:    drop false
  517:  else
  518:    dup  item-stack  @ r@ item-stack  @ = 
  519:    swap item-offset @ r@ item-offset @ = and
  520:  endif
  521:  rdrop ;
  522: 
  523: : item-out-index ( item -- n )
  524:     \ n is the index of item (in the out-effect)
  525:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
  526: 
  527: : really-store-single ( item -- )
  528:     >r
  529:     ." vm_"
  530:     r@ item-type @ print-type-prefix ." 2"
  531:     r@ item-stack-type-name type ." ("
  532:     r@ item-name 2@ type ." ,"
  533:     r@ item-out-index r@ item-stack @ stack-write ." );"
  534:     rdrop ;
  535: 
  536: : store-single { item -- }
  537:     item item-stack @ { stack }
  538:     store-optimization @ in-part @ 0= and item same-as-in? and
  539:     item item-in-index  stack state-in  stack-reg       \  in reg/mem
  540:     item item-out-index stack state-out stack-reg = and \ out reg/mem
  541:     0= if
  542: 	item really-store-single cr
  543:     endif ;
  544: 
  545: : store-double ( item -- )
  546: \ !! store optimization is not performed, because it is not yet needed
  547:  >r
  548:  ." vm_"
  549:  r@ item-type @ print-type-prefix ." 2two"
  550:  r@ item-stack-type-name type ." ("
  551:  r@ item-name 2@ type ." , "
  552:  r@ item-out-index r@ item-stack @ 2dup stack-write
  553:  ." , "                       -1 under+ stack-write
  554:  ." )" cr
  555:  rdrop ;
  556: 
  557: : single ( -- xt1 xt2 n )
  558:     ['] fetch-single ['] store-single 1 ;
  559: 
  560: : double ( -- xt1 xt2 n )
  561:     ['] fetch-double ['] store-double 2 ;
  562: 
  563: : s, ( addr u -- )
  564: \ allocate a string
  565:  here swap dup allot move ;
  566: 
  567: wordlist constant prefixes
  568: 
  569: : declare ( addr "name" -- )
  570: \ remember that there is a stack item at addr called name
  571:  create , ;
  572: 
  573: : !default ( w addr -- )
  574:     dup @ if
  575: 	2drop \ leave nonzero alone
  576:     else
  577: 	!
  578:     endif ;
  579: 
  580: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
  581:     \ describes a type
  582:     \ addr u specifies the C type name
  583:     \ stack effect entries of the type start with prefix
  584:     create type% %allot >r
  585:     addr u save-mem r@ type-c-name 2!
  586:     xt1   r@ type-fetch !
  587:     xt2   r@ type-store !
  588:     n     r@ type-size !
  589:     stack r@ type-stack !
  590:     rdrop ;
  591: 
  592: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
  593:     get-current >r prefixes set-current
  594:     create-type r> set-current
  595: does> ( item -- )
  596:     \ initialize item
  597:     { item typ }
  598:     typ item item-type !
  599:     typ type-stack @ item item-stack !default
  600:     item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
  601: 	item item-name 2@ nextname item declare
  602: 	item item-first on
  603: 	\ typ type-c-name 2@ type space type  ." ;" cr
  604:     else
  605: 	drop
  606: 	item item-first off
  607:     endif ;
  608: 
  609: : execute-prefix ( item addr1 u1 -- )
  610:     \ execute the word ( item -- ) associated with the longest prefix
  611:     \ of addr1 u1
  612:     0 swap ?do
  613: 	dup i prefixes search-wordlist
  614: 	if \ ok, we have the type ( item addr1 xt )
  615: 	    nip execute
  616: 	    UNLOOP EXIT
  617: 	endif
  618: 	-1 s+loop
  619:     \ we did not find a type, abort
  620:     false s" unknown prefix" ?print-error ;
  621: 
  622: : declaration ( item -- )
  623:     dup item-name 2@ execute-prefix ;
  624: 
  625: : declaration-list ( addr1 addr2 -- )
  626:     ['] declaration map-items ;
  627: 
  628: : declarations ( -- )
  629:  wordlist dup prim prim-items-wordlist ! set-current
  630:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
  631:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
  632: 
  633: : print-declaration { item -- }
  634:     item item-first @ if
  635: 	item item-type @ type-c-name 2@ type space
  636: 	item item-name 2@ type ." ;" cr
  637:     endif ;
  638: 
  639: : print-declarations ( -- )
  640:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
  641:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
  642:     
  643: : stack-prefix ( stack "prefix" -- )
  644:     get-current >r prefixes set-current
  645:     name tuck nextname create ( stack length ) 2,
  646:     r> set-current
  647: does> ( item -- )
  648:     2@ { item stack prefix-length }
  649:     item item-name 2@ prefix-length /string item item-name 2!
  650:     stack item item-stack !
  651:     item declaration ;
  652: 
  653: get-current prefixes set-current
  654: : ... ( item -- )
  655:     \ this "prefix" ensures that the appropriate stack is synced with memory
  656:     dup item-name 2@ s" ..." str= 0= abort" '...' must end the item name"
  657:     item-stack @ dup if
  658: 	stack-number @ prim prim-stacks-sync swap th on
  659:     else \ prefixless "..." syncs all stacks
  660: 	max-stacks 0 +do
  661: 	    prim prim-stacks-sync i th on
  662: 	loop
  663:     endif ;
  664: set-current
  665: 
  666: create ...-item ( -- addr ) \ just used for letting stack-prefixes work on it
  667: item% %allot                \ stores the stack temporarily until used by ...
  668: 
  669: : init-item1 ( addr1 addr u -- addr2 )
  670:     \ initialize item at addr1 with name addr u, next item is at addr2
  671:     \ !! make sure that any mention of "..." is only stack-prefixed
  672:     2dup s" ..." search nip nip if ( addr1 addr u )
  673: 	0 ...-item item-stack ! \ initialize to prefixless
  674: 	2dup ...-item item-name 2!
  675: 	...-item rot rot execute-prefix ( addr1 )
  676:     else
  677: 	2 pick init-item item% %size +
  678:     endif ;
  679: 
  680: \ types pointed to by stacks for use in combined prims
  681: \ !! output-c-combined shouldn't use these names!
  682: : stack-type-name ( addr u "name" -- )
  683:     single 0 create-type ;
  684: 
  685: wordlist constant type-names \ this is here just to meet the requirement
  686:                     \ that a type be a word; it is never used for lookup
  687: 
  688: : define-type ( addr u -- xt )
  689:     \ define single type with name addr u, without stack
  690:     get-current type-names set-current >r
  691:     2dup nextname stack-type-name
  692:     r> set-current
  693:     latestxt ;
  694: 
  695: : stack ( "name" "stack-pointer" "type" -- )
  696:     \ define stack
  697:     name { d: stack-name }
  698:     name { d: stack-pointer }
  699:     name { d: stack-type }
  700:     stack-type define-type
  701:     stack-pointer rot >body stack-name nextname make-stack ;
  702: 
  703: stack inst-stream IP Cell
  704: ' inst-in-index inst-stream stack-in-index-xt !
  705: ' inst-stream <is> inst-stream-f
  706: \ !! initialize stack-in and stack-out
  707: 
  708: \ registers
  709: 
  710: : make-register ( type addr u -- )
  711:     \ define register with type TYPE and name ADDR U.
  712:     nregisters @ max-registers < s" too many registers" ?print-error
  713:     2dup nextname create register% %allot >r
  714:     r@ register-name 2!
  715:     r@ register-type !
  716:     nregisters @ r@ register-number !
  717:     1 nregisters +!
  718:     rdrop ;
  719: 
  720: : register ( "name" "type" -- )
  721:     \ define register
  722:     name { d: reg-name }
  723:     name { d: reg-type }
  724:     reg-type define-type >body
  725:     reg-name make-register ;
  726: 
  727: \ stack-states
  728: 
  729: : stack-state ( a-addr u uoffset "name" -- )
  730:     create ss% %allot >r
  731:     r@ ss-offset !
  732:     r@ ss-registers 2!
  733:     rdrop ;
  734: 
  735: 0 0 0 stack-state default-ss
  736: 
  737: \ state
  738: 
  739: : state ( "name" -- )
  740:     \ create a state initialized with default-sss
  741:     create state% %allot { s }
  742:     next-state-number @ s state-number ! 1 next-state-number +!
  743:     max-stacks 0 ?do
  744: 	default-ss s state-sss i th !
  745:     loop ;
  746: 
  747: : .state ( state -- )
  748:     0 >body - >name .name ;
  749: 
  750: : set-ss ( ss stack state -- )
  751:     state-sss swap stack-number @ th ! ;
  752: 
  753: \ offset computation
  754: \ the leftmost (i.e. deepest) item has offset 0
  755: \ the rightmost item has the highest offset
  756: 
  757: : compute-offset { item xt -- }
  758:     \ xt specifies in/out; update stack-in/out and set item-offset
  759:     item item-type @ type-size @
  760:     item item-stack @ xt execute dup @ >r +!
  761:     r> item item-offset ! ;
  762: 
  763: : compute-offset-in ( addr1 addr2 -- )
  764:     ['] stack-in compute-offset ;
  765: 
  766: : compute-offset-out ( addr1 addr2 -- )
  767:     ['] stack-out compute-offset ;
  768: 
  769: : compute-offsets ( -- )
  770:     prim prim-stacks-in  max-stacks cells erase
  771:     prim prim-stacks-out max-stacks cells erase
  772:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
  773:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
  774:     inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
  775: 
  776: : init-simple { prim -- }
  777:     \ much of the initialization is elsewhere
  778:     max-stacks 0 +do
  779: 	prim prim-stacks-sync i th off
  780:     loop ;
  781: 
  782: : process-simple ( -- )
  783:     prim init-simple
  784:     prim prim { W^ key } key cell
  785:     combinations ['] constant insert-wordlist
  786:     declarations compute-offsets
  787:     output @ execute ;
  788: 
  789: : stack-state-items ( stack state -- n )
  790:     state-ss ss-registers 2@ nip ;
  791: 
  792: : unused-stack-items { stack -- n-in n-out }
  793:     \ n-in  are the stack items in state-in  not used    by prim
  794:     \ n-out are the stack items in state-out not written by prim
  795:     stack state-in  stack-state-items stack stack-in  @ - 0 max
  796:     stack state-out stack-state-items stack stack-out @ - 0 max ;
  797: 
  798: : spill-stack { stack -- }
  799:     \ spill regs of state-in that are not used by prim and are not in state-out
  800:     stack state-in stack-offset { offset }
  801:     stack state-in stack-state-items ( items )
  802:     dup stack unused-stack-items - - +do
  803: 	\ loop through the bottom items
  804: 	stack stack-pointer 2@ type
  805: 	i offset - stack normal-stack-access0 ."  = "
  806: 	i stack state-in normal-stack-access1 ." ;" cr
  807:     loop ;
  808: 
  809: : spill-state ( -- )
  810:     ['] spill-stack map-stacks1 ;
  811: 
  812: : fill-stack { stack -- }
  813:     stack state-out stack-offset { offset }
  814:     stack state-out stack-state-items ( items )
  815:     dup stack unused-stack-items - + +do
  816: 	\ loop through the bottom items
  817: 	i stack state-out normal-stack-access1 ."  = "
  818: 	stack stack-pointer 2@ type
  819: 	i offset - stack normal-stack-access0 ." ;" cr
  820:     loop ;
  821: 
  822: : fill-state ( -- )
  823:     \ !! inst-stream for prefetching?
  824:     ['] fill-stack map-stacks1 ;
  825: 
  826: : fetch ( addr -- )
  827:     dup item-type @ type-fetch @ execute ;
  828: 
  829: : fetches ( -- )
  830:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
  831: 
  832: : reg-reg-move ( reg-from reg-to -- )
  833:     2dup = if
  834: 	2drop
  835:     else
  836: 	.reg ."  = " .reg ." ;" cr
  837:     endif ;
  838: 
  839: : stack-bottom-reg { n stack state -- reg }
  840:     stack state stack-state-items n - 1- stack state stack-reg ;
  841: 
  842: : stack-moves { stack -- }
  843:     \ generate moves between registers in state-in/state-out that are
  844:     \ not spilled or consumed/produced by prim.
  845:     \ !! this works only for a simple stack cache, not e.g., for
  846:     \ rotating stack caches, or registers shared between stacks (the
  847:     \ latter would also require a change in interface)
  848:     \ !! maybe place this after NEXT_P1?
  849:     stack unused-stack-items 2dup < if ( n-in n-out )
  850: 	\ move registers from 0..n_in-1 to n_out-n_in..n_out-1
  851: 	over - { diff } ( n-in )
  852: 	-1 swap 1- -do
  853: 	    i stack state-in stack-bottom-reg ( reg-from )
  854: 	    i diff + stack state-out stack-bottom-reg reg-reg-move
  855: 	1 -loop
  856:     else
  857: 	\ move registers from n_in-n_out..n_in-1 to 0..n_out-1
  858: 	swap over - { diff } ( n-out )
  859: 	0 +do
  860: 	    i diff + stack state-in stack-bottom-reg ( reg-from )
  861: 	    i stack state-out stack-bottom-reg reg-reg-move
  862: 	loop
  863:     endif ;
  864: 
  865: : stack-update-transform ( n1 stack -- n2 )
  866:     \ n2 is the number by which the stack pointer should be
  867:     \ incremented to pop n1 items
  868:     stack-access-transform @ dup >r execute
  869:     0 r> execute - ;
  870: 
  871: : stack-pointer-update { stack -- }
  872:     \ and moves
  873:     \ stacks grow downwards
  874:     stack stack-diff ( in-out )
  875:     stack state-in  stack-offset -
  876:     stack state-out stack-offset + ( [in-in_offset]-[out-out_offset] )
  877:     ?dup-if \ this check is not necessary, gcc would do this for us
  878: 	stack inst-stream = if
  879: 	    ." INC_IP(" 0 .r ." );" cr
  880: 	else
  881: 	    stack stack-pointer 2@ type ."  += "
  882: 	    stack stack-update-transform 0 .r ." ;" cr
  883: 	endif
  884:     endif
  885:     stack stack-moves ;
  886: 
  887: : stack-pointer-updates ( -- )
  888:     ['] stack-pointer-update map-stacks ;
  889: 
  890: : store ( item -- )
  891: \ f is true if the item should be stored
  892: \ f is false if the store is probably not necessary
  893:  dup item-type @ type-store @ execute ;
  894: 
  895: : stores ( -- )
  896:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
  897: 
  898: : print-debug-arg { item -- }
  899:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
  900:     ." printarg_" item item-type @ print-type-prefix
  901:     ." (" item item-name 2@ type ." );" cr ;
  902:     
  903: : print-debug-args ( -- )
  904:     ." #ifdef VM_DEBUG" cr
  905:     ." if (vm_debug) {" cr
  906:     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
  907: \    ." fputc('\n', vm_out);" cr
  908:     ." }" cr
  909:     ." #endif" cr ;
  910: 
  911: : print-debug-result { item -- }
  912:     item item-first @ if
  913: 	item print-debug-arg
  914:     endif ;
  915: 
  916: : print-debug-results ( -- )
  917:     cr
  918:     ." #ifdef VM_DEBUG" cr
  919:     ." if (vm_debug) {" cr
  920:     ." fputs(" quote ."  -- " quote ." , vm_out); "
  921:     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
  922:     ." fputc('\n', vm_out);" cr
  923:     ." }" cr
  924:     ." #endif" cr ;
  925: 
  926: : output-super-end ( -- )
  927:     prim prim-c-code 2@ s" SET_IP" search if
  928: 	." SUPER_END;" cr
  929:     endif
  930:     2drop ;
  931: 
  932: 
  933: defer output-nextp0
  934: :noname ( -- )
  935:     ." NEXT_P0;" cr ;
  936: is output-nextp0
  937: 
  938: defer output-nextp1
  939: :noname ( -- )
  940:     ." NEXT_P1;" cr ;
  941: is output-nextp1
  942: 
  943: : output-nextp2 ( -- )
  944:     ." NEXT_P2;" cr ;
  945: 
  946: variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL
  947: ' output-nextp2 tail-nextp2 !
  948: 
  949: : output-label2 ( -- )
  950:     ." LABEL2(" prim prim-c-name 2@ type ." )" cr
  951:     ." NEXT_P1_5;" cr
  952:     ." LABEL3(" prim prim-c-name 2@ type ." )" cr
  953:     ." DO_GOTO;" cr ;
  954: 
  955: : output-c-tail1 { xt -- }
  956:     \ the final part of the generated C code, with xt printing LABEL2 or not.
  957:     output-super-end
  958:     print-debug-results
  959:     output-nextp1
  960:     stores
  961:     fill-state 
  962:     xt execute ;
  963: 
  964: : output-c-vm-jump-tail ( -- )
  965:     \ !! this functionality not yet implemented for superinstructions
  966:     output-super-end
  967:     print-debug-results
  968:     stores
  969:     fill-state 
  970:     ." LABEL2(" prim prim-c-name 2@ type ." )" cr
  971:     ." LABEL3(" prim prim-c-name 2@ type ." )" cr
  972:     ." DO_GOTO;" cr ;
  973: 
  974: : output-c-tail1-no-stores { xt -- }
  975:     \ the final part of the generated C code for combinations
  976:     output-super-end
  977:     output-nextp1
  978:     fill-state 
  979:     xt execute ;
  980: 
  981: : output-c-tail ( -- )
  982:     tail-nextp2 @ output-c-tail1 ;
  983: 
  984: : output-c-tail2 ( -- )
  985:     prim prim-c-code 2@ s" VM_JUMP(" search nip nip if
  986: 	output-c-vm-jump-tail
  987:     else
  988: 	['] output-label2 output-c-tail1
  989:     endif ;
  990: 
  991: : output-c-tail-no-stores ( -- )
  992:     tail-nextp2 @ output-c-tail1-no-stores ;
  993: 
  994: : output-c-tail2-no-stores ( -- )
  995:     ['] output-label2 output-c-tail1-no-stores ;
  996: 
  997: : type-c-code ( c-addr u xt -- )
  998:     \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
  999:     { xt }
 1000:     ." {" cr
 1001:     ." #line " c-line @ . quote c-filename 2@ type quote cr
 1002:     begin ( c-addr1 u1 )
 1003: 	2dup s" INST_TAIL;" search
 1004:     while ( c-addr1 u1 c-addr3 u3 )
 1005: 	2dup 2>r drop nip over - type
 1006: 	xt execute
 1007: 	2r> 10 /string
 1008: 	\ !! resync #line missing
 1009:     repeat
 1010:     2drop type
 1011:     ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
 1012:     ." }" cr ;
 1013: 
 1014: : print-entry ( -- )
 1015:     ." LABEL(" prim prim-c-name 2@ type ." )" ;
 1016: 
 1017: : prim-type ( addr u -- )
 1018:     \ print out a primitive, but avoid "*/"
 1019:     2dup s" */" search  nip nip  IF
 1020: 	bounds ?DO  I c@ dup '* = IF  drop 'x  THEN  emit  LOOP
 1021:     ELSE  type  THEN ;
 1022: 
 1023: : output-c ( -- ) 
 1024:     print-entry ."  /* " prim prim-name 2@ prim-type
 1025:     ."  ( " prim prim-stack-string 2@ type ." ) "
 1026:     state-in .state ." -- " state-out .state ."  */" cr
 1027:     ." /* " prim prim-doc 2@ type ."  */" cr
 1028:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
 1029:     ." {" cr
 1030:     ." DEF_CA" cr
 1031:     print-declarations
 1032:     output-nextp0
 1033:     spill-state
 1034:     fetches
 1035:     print-debug-args
 1036:     stack-pointer-updates
 1037:     prim prim-c-code 2@ ['] output-c-tail type-c-code
 1038:     output-c-tail2
 1039:     ." }" cr
 1040:     cr
 1041: ;
 1042: 
 1043: : disasm-arg { item -- }
 1044:     item item-stack @ inst-stream = if
 1045: 	." {" cr
 1046: 	item print-declaration
 1047: 	item fetch
 1048: 	item print-debug-arg
 1049: 	." }" cr
 1050:     endif ;
 1051: 
 1052: : disasm-args ( -- )
 1053:     prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
 1054: 
 1055: : output-disasm ( -- )
 1056:     \ generate code for disassembling VM instructions
 1057:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
 1058:     ."   fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
 1059:     disasm-args
 1060:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
 1061:     ."   goto _endif_;" cr
 1062:     ." }" cr ;
 1063: 
 1064: : output-profile ( -- )
 1065:     \ generate code for postprocessing the VM block profile stuff
 1066:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
 1067:     ."   add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
 1068:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
 1069:     prim prim-c-code 2@  s" SET_IP"    search nip nip
 1070:     prim prim-c-code 2@  s" SUPER_END" search nip nip or if
 1071: 	."   return;" cr
 1072:     else
 1073: 	."   goto _endif_;" cr
 1074:     endif
 1075:     ." }" cr ;
 1076: 
 1077: : output-profile-part ( p )
 1078:     ."   add_inst(b, " quote
 1079:     prim-name 2@ type
 1080:     quote ." );" cr ;
 1081:     
 1082: : output-profile-combined ( -- )
 1083:     \ generate code for postprocessing the VM block profile stuff
 1084:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
 1085:     ['] output-profile-part map-combined
 1086:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
 1087:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SET_IP"    search nip nip
 1088:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SUPER_END" search nip nip or if
 1089: 	."   return;" cr
 1090:     else
 1091: 	."   goto _endif_;" cr
 1092:     endif
 1093:     ." }" cr ;
 1094: 
 1095: : prim-branch? { prim -- f }
 1096:     \ true if prim is a branch or super-end
 1097:     prim prim-c-code 2@  s" SET_IP" search nip nip 0<> ;
 1098: 
 1099: : output-superend ( -- )
 1100:     \ output flag specifying whether the current word ends a dynamic superinst
 1101:     prim prim-branch?
 1102:     prim prim-c-code 2@  s" SUPER_END" search nip nip 0<> or
 1103:     prim prim-c-code 2@  s" SUPER_CONTINUE" search nip nip 0= and
 1104:     negate 0 .r ." , /* " prim prim-name 2@ prim-type ."  */" cr ;
 1105: 
 1106: : gen-arg-parm { item -- }
 1107:     item item-stack @ inst-stream = if
 1108: 	." , " item item-type @ type-c-name 2@ type space
 1109: 	item item-name 2@ type
 1110:     endif ;
 1111: 
 1112: : gen-args-parm ( -- )
 1113:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
 1114: 
 1115: : gen-arg-gen { item -- }
 1116:     item item-stack @ inst-stream = if
 1117: 	."   genarg_" item item-type @ print-type-prefix
 1118:         ." (ctp, " item item-name 2@ type ." );" cr
 1119:     endif ;
 1120: 
 1121: : gen-args-gen ( -- )
 1122:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
 1123: 
 1124: : output-gen ( -- )
 1125:     \ generate C code for generating VM instructions
 1126:     ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
 1127:     ." {" cr
 1128:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
 1129:     gen-args-gen
 1130:     ." }" cr ;
 1131: 
 1132: : stack-used? { stack -- f }
 1133:     stack stack-in @ stack stack-out @ or 0<> ;
 1134: 
 1135: : output-funclabel ( -- )
 1136:   ." &I_" prim prim-c-name 2@ type ." ," cr ;
 1137: 
 1138: : output-forthname ( -- )
 1139:   '" emit prim prim-name 2@ type '" emit ." ," cr ;
 1140: 
 1141: \  : output-c-func ( -- )
 1142: \  \ used for word libraries
 1143: \      ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP)      /* " prim prim-name 2@ type
 1144: \      ."  ( " prim prim-stack-string 2@ type ."  ) */" cr
 1145: \      ." /* " prim prim-doc 2@ type ."  */" cr
 1146: \      ." NAME(" quote prim prim-name 2@ type quote ." )" cr
 1147: \      \ debugging
 1148: \      ." {" cr
 1149: \      print-declarations
 1150: \      \ !! don't know what to do about that
 1151: \      inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
 1152: \      data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
 1153: \      fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
 1154: \      return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
 1155: \      spill-state
 1156: \      fetches
 1157: \      stack-pointer-updates
 1158: \      fp-stack   stack-used? IF ." *FP=fp;" cr THEN
 1159: \      ." {" cr
 1160: \      ." #line " c-line @ . quote c-filename 2@ type quote cr
 1161: \      prim prim-c-code 2@ type
 1162: \      ." }" cr
 1163: \      stores
 1164: \      fill-state
 1165: \      ." return (sp);" cr
 1166: \      ." }" cr
 1167: \      cr ;
 1168: 
 1169: : output-label ( -- )  
 1170:     ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
 1171: 
 1172: : output-alias ( -- ) 
 1173:     ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
 1174: 
 1175: defer output-c-prim-num ( -- )
 1176: 
 1177: :noname ( -- )
 1178:     ." N_" prim prim-c-name 2@ type ." ," cr ;
 1179: is output-c-prim-num
 1180: 
 1181: : output-forth ( -- )  
 1182:     prim prim-forth-code @ 0=
 1183:     IF    	\ output-alias
 1184: 	\ this is bad for ec: an alias is compiled if tho word does not exist!
 1185: 	\ JAW
 1186:     ELSE  ." : " prim prim-name 2@ type ."   ( "
 1187: 	prim prim-stack-string 2@ type ." )" cr
 1188: 	prim prim-forth-code 2@ type cr
 1189:     THEN ;
 1190: 
 1191: : output-tag-file ( -- )
 1192:     name-filename 2@ last-name-filename 2@ compare if
 1193: 	name-filename 2@ last-name-filename 2!
 1194: 	#ff emit cr
 1195: 	name-filename 2@ type
 1196: 	." ,0" cr
 1197:     endif ;
 1198: 
 1199: : output-tag ( -- )
 1200:     output-tag-file
 1201:     prim prim-name 2@ 1+ type
 1202:     127 emit
 1203:     space prim prim-name 2@ type space
 1204:     1 emit
 1205:     name-line @ 0 .r
 1206:     ." ,0" cr ;
 1207: 
 1208: : output-vi-tag ( -- )
 1209:     name-filename 2@ type #tab emit
 1210:     prim prim-name 2@ type #tab emit
 1211:     ." /^" prim prim-name 2@ type ."  *(/" cr ;
 1212: 
 1213: [IFDEF] documentation
 1214: : register-doc ( -- )
 1215:     prim prim-name 2@ documentation ['] create insert-wordlist
 1216:     prim prim-name 2@ 2,
 1217:     prim prim-stack-string 2@ condition-stack-effect 2,
 1218:     prim prim-wordset 2@ 2,
 1219:     prim prim-c-name 2@ condition-pronounciation 2,
 1220:     prim prim-doc 2@ 2, ;
 1221: [THEN]
 1222: 
 1223: 
 1224: \ combining instructions
 1225: 
 1226: \ The input should look like this:
 1227: 
 1228: \ lit_+ = lit +
 1229: 
 1230: \ The output should look like this:
 1231: 
 1232: \  I_lit_+:
 1233: \  {
 1234: \  DEF_CA
 1235: \  Cell _x_ip0;
 1236: \  Cell _x_sp0;
 1237: \  Cell _x_sp1;
 1238: \  NEXT_P0;
 1239: \  _x_ip0 = (Cell) IPTOS;
 1240: \  _x_sp0 = (Cell) spTOS;
 1241: \  INC_IP(1);
 1242: \  /* sp += 0; */
 1243: \  /* lit ( #w -- w ) */
 1244: \  /*  */
 1245: \  NAME("lit")
 1246: \  {
 1247: \  Cell w;
 1248: \  w = (Cell) _x_ip0;
 1249: \  #ifdef VM_DEBUG
 1250: \  if (vm_debug) {
 1251: \  fputs(" w=", vm_out); printarg_w (w);
 1252: \  fputc('\n', vm_out);
 1253: \  }
 1254: \  #endif
 1255: \  {
 1256: \  #line 136 "./prim"
 1257: \  }
 1258: \  _x_sp1 = (Cell)w;
 1259: \  }
 1260: \  I_plus:	/* + ( n1 n2 -- n ) */
 1261: \  /*  */
 1262: \  NAME("+")
 1263: \  {
 1264: \  DEF_CA
 1265: \  Cell n1;
 1266: \  Cell n2;
 1267: \  Cell n;
 1268: \  NEXT_P0;
 1269: \  n1 = (Cell) _x_sp0;
 1270: \  n2 = (Cell) _x_sp1;
 1271: \  #ifdef VM_DEBUG
 1272: \  if (vm_debug) {
 1273: \  fputs(" n1=", vm_out); printarg_n (n1);
 1274: \  fputs(" n2=", vm_out); printarg_n (n2);
 1275: \  fputc('\n', vm_out);
 1276: \  }
 1277: \  #endif
 1278: \  {
 1279: \  #line 516 "./prim"
 1280: \  n = n1+n2;
 1281: \  }
 1282: \  _x_sp0 = (Cell)n;
 1283: \  }
 1284: \  NEXT_P1;
 1285: \  spTOS = (Cell)_x_sp0;
 1286: \  NEXT_P2;
 1287: 
 1288: : init-combined ( -- )
 1289:     prim to combined
 1290:     0 num-combined !
 1291:     current-depth max-stacks cells erase
 1292:     include-skipped-insts @ current-depth 0 th !
 1293:     max-depth     max-stacks cells erase
 1294:     min-depth     max-stacks cells erase
 1295:     prim prim-effect-in  prim prim-effect-in-end  !
 1296:     prim prim-effect-out prim prim-effect-out-end ! ;
 1297: 
 1298: : max! ( n addr -- )
 1299:     tuck @ max swap ! ;
 1300: 
 1301: : min! ( n addr -- )
 1302:     tuck @ min swap ! ;
 1303: 
 1304: : inst-stream-adjustment ( nstack -- n )
 1305:     \ number of stack items to add for each part
 1306:     0= include-skipped-insts @ and negate ;
 1307: 
 1308: : add-depths { p -- }
 1309:     \ combine stack effect of p with *-depths
 1310:     max-stacks 0 ?do
 1311: 	current-depth i th @
 1312: 	p prim-stacks-in  i th @ + i inst-stream-adjustment +
 1313: 	dup max-depth i th max!
 1314: 	p prim-stacks-out i th @ -
 1315: 	dup min-depth i th min!
 1316: 	current-depth i th !
 1317:     loop ;
 1318: 
 1319: : copy-maxdepths ( n -- )
 1320:     max-depth max-depths rot max-stacks * th max-stacks cells move ;
 1321: 
 1322: : add-prim ( addr u -- )
 1323:     \ add primitive given by "addr u" to combined-prims
 1324:     primitives search-wordlist s" unknown primitive" ?print-error
 1325:     execute { p }
 1326:     p combined-prims num-combined @ th !
 1327:     num-combined @ copy-maxdepths
 1328:     1 num-combined +!
 1329:     p add-depths
 1330:     num-combined @ copy-maxdepths ;
 1331: 
 1332: : compute-effects { q -- }
 1333:     \ compute the stack effects of q from the depths
 1334:     max-stacks 0 ?do
 1335: 	max-depth i th @ dup
 1336: 	q prim-stacks-in i th !
 1337: 	current-depth i th @ -
 1338: 	q prim-stacks-out i th !
 1339:     loop ;
 1340: 
 1341: : make-effect-items { stack# items effect-endp -- }
 1342:     \ effect-endp points to a pointer to the end of the current item-array
 1343:     \ and has to be updated
 1344:     stacks stack# th @ { stack }
 1345:     items 0 +do
 1346: 	effect-endp @ { item }
 1347: 	i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
 1348: 	item item-name 2!
 1349: 	stack item item-stack !
 1350: 	stack stack-type @ item item-type !
 1351: 	i item item-offset !
 1352: 	item item-first on
 1353: 	item% %size effect-endp +!
 1354:     loop ;
 1355: 
 1356: : init-effects { q -- }
 1357:     \ initialize effects field for FETCHES and STORES
 1358:     max-stacks 0 ?do
 1359: 	i q prim-stacks-in  i th @ q prim-effect-in-end  make-effect-items
 1360: 	i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
 1361:     loop ;
 1362: 
 1363: : compute-stack-max-back-depths ( stack -- )
 1364:     stack-number @ { stack# }
 1365:     current-depth stack# th @ dup
 1366:     dup stack# num-combined @ s-c-max-back-depth !
 1367:     -1 num-combined @ 1- -do ( max-depth current-depth )
 1368: 	combined-prims i th @ { p }
 1369: 	p prim-stacks-out stack# th @ +
 1370: 	dup >r max r>
 1371: 	over stack# i s-c-max-back-depth !
 1372: 	p prim-stacks-in stack# th @ -
 1373: 	stack# inst-stream-adjustment -
 1374:     1 -loop
 1375:     assert( dup stack# inst-stream-adjustment negate = )
 1376:     assert( over max-depth stack# th @ = )
 1377:     2drop ;
 1378: 
 1379: : compute-max-back-depths ( -- )
 1380:     \ compute max-back-depths.
 1381:     \ assumes that current-depths is correct for the end of the combination
 1382:     ['] compute-stack-max-back-depths map-stacks ;
 1383: 
 1384: : process-combined ( -- )
 1385:     combined combined-prims num-combined @ cells
 1386:     combinations ['] constant insert-wordlist
 1387:     combined-prims num-combined @ 1- th ( last-part )
 1388:     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
 1389:     prim compute-effects
 1390:     prim init-effects
 1391:     compute-max-back-depths
 1392:     output-combined perform ;
 1393: 
 1394: \ reprocessing (typically to generate versions for another cache states)
 1395: \ !! use prim-context
 1396: 
 1397: variable reprocessed-num 0 reprocessed-num !
 1398: 
 1399: : new-name ( -- c-addr u )
 1400:     reprocessed-num @ 0
 1401:     1 reprocessed-num +!
 1402:     <# #s 'p hold '_ hold #> save-mem ;
 1403: 
 1404: : reprocess-simple ( prim -- )
 1405:     to prim
 1406:     new-name prim prim-c-name 2!
 1407:     output @ execute ;
 1408: 
 1409: : lookup-prim ( c-addr u -- prim )
 1410:     primitives search-wordlist 0= -13 and throw execute ;
 1411: 
 1412: : state-prim1 { in-state out-state prim -- }
 1413:     in-state out-state state-default dup d= ?EXIT
 1414:     in-state  to state-in
 1415:     out-state to state-out
 1416:     prim reprocess-simple ;
 1417: 
 1418: : state-prim ( in-state out-state "name" -- )
 1419:     parse-word lookup-prim state-prim1 ;
 1420: 
 1421: \ reprocessing with default states
 1422: 
 1423: \ This is a simple scheme and should be generalized
 1424: \ assumes we only cache one stack and use simple states for that
 1425: 
 1426: 0 value cache-stack  \ stack that we cache
 1427: 2variable cache-states \ states of the cache, starting with the empty state
 1428: 
 1429: : compute-default-state-out ( n-in -- n-out )
 1430:     \ for the current prim
 1431:     cache-stack stack-in @ - 0 max
 1432:     cache-stack stack-out @ + cache-states 2@ nip 1- min ;
 1433: 
 1434: : gen-prim-states ( prim -- )
 1435:     to prim
 1436:     cache-states 2@ swap { states } ( nstates )
 1437:     cache-stack stack-in @ +do
 1438: 	states i th @
 1439: 	states i compute-default-state-out th @
 1440: 	prim state-prim1
 1441:     loop ;
 1442: 
 1443: : prim-states ( "name" -- )
 1444:     parse-word lookup-prim gen-prim-states ;
 1445: 
 1446: : gen-branch-states ( prim -- )
 1447:     \ generate versions that produce state-default; useful for branches
 1448:     to prim
 1449:     cache-states 2@ swap { states } ( nstates )
 1450:     cache-stack stack-in @ +do
 1451: 	states i th @ state-default prim state-prim1
 1452:     loop ;
 1453: 
 1454: : branch-states ( out-state "name" -- )
 1455:     parse-word lookup-prim gen-branch-states ;
 1456: 
 1457: \ producing state transitions
 1458: 
 1459: : gen-transitions ( "name" -- )
 1460:     parse-word lookup-prim { prim }
 1461:     cache-states 2@ { states nstates }
 1462:     nstates 0 +do
 1463: 	nstates 0 +do
 1464: 	    i j <> if
 1465: 		states i th @ states j th @ prim state-prim1
 1466: 	    endif
 1467: 	loop
 1468:     loop ;
 1469: 
 1470: \ C output
 1471: 
 1472: : print-item { n stack -- }
 1473:     \ print nth stack item name
 1474:     stack stack-type @ type-c-name 2@ type space
 1475:     ." MAYBE_UNUSED _" stack stack-pointer 2@ type n 0 .r ;
 1476: 
 1477: : print-declarations-combined ( -- )
 1478:     max-stacks 0 ?do
 1479: 	max-depth i th @ min-depth i th @ - 0 +do
 1480: 	    i stacks j th @ print-item ." ;" cr
 1481: 	loop
 1482:     loop ;
 1483: 
 1484: : part-fetches ( -- )
 1485:     fetches ;
 1486: 
 1487: : part-output-c-tail ( -- )
 1488:     print-debug-results
 1489:     stores ;
 1490: 
 1491: : output-combined-tail ( -- )
 1492:     part-output-c-tail
 1493:     in-part @ >r in-part off
 1494:     combined ['] output-c-tail-no-stores prim-context
 1495:     r> in-part ! ;
 1496: 
 1497: : part-stack-pointer-updates ( -- )
 1498:     next-stack-number @ 0 +do
 1499: 	i part-num @ 1+ s-c-max-depth @ dup
 1500: 	i num-combined @ s-c-max-depth @ =    \ final depth
 1501: 	swap i part-num @ s-c-max-depth @ <> \ just reached now
 1502: 	part-num @ 0= \ first part
 1503: 	or and if
 1504: 	    stacks i th @ stack-pointer-update
 1505: 	endif
 1506:     loop ;
 1507: 
 1508: : output-part ( p -- )
 1509:     to prim
 1510:     ." /* " prim prim-name 2@ prim-type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
 1511:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
 1512:     ." {" cr
 1513:     print-declarations
 1514:     part-fetches
 1515:     print-debug-args
 1516:     combined ['] part-stack-pointer-updates prim-context
 1517:     1 part-num +!
 1518:     prim add-depths \ !! right place?
 1519:     prim prim-c-code 2@ ['] output-combined-tail type-c-code
 1520:     part-output-c-tail
 1521:     ." }" cr ;
 1522: 
 1523: : output-parts ( -- )
 1524:     prim >r in-part on
 1525:     current-depth max-stacks cells erase
 1526:     0 part-num !
 1527:     ['] output-part map-combined
 1528:     in-part off
 1529:     r> to prim ;
 1530: 
 1531: : output-c-combined ( -- )
 1532:     print-entry cr
 1533:     \ debugging messages just in parts
 1534:     ." {" cr
 1535:     ." DEF_CA" cr
 1536:     print-declarations-combined
 1537:     output-nextp0
 1538:     spill-state
 1539:     \ fetches \ now in parts
 1540:     \ print-debug-args
 1541:     \ stack-pointer-updates now in parts
 1542:     output-parts
 1543:     output-c-tail2-no-stores
 1544:     ." }" cr
 1545:     cr ;
 1546: 
 1547: : output-forth-combined ( -- )
 1548: ;
 1549: 
 1550: 
 1551: \ peephole optimization rules
 1552: 
 1553: \ data for a simple peephole optimizer that always tries to combine
 1554: \ the currently compiled instruction with the last one.
 1555: 
 1556: \ in order for this to work as intended, shorter combinations for each
 1557: \ length must be present, and the longer combinations must follow
 1558: \ shorter ones (this restriction may go away in the future).
 1559:   
 1560: : output-peephole ( -- )
 1561:     combined-prims num-combined @ 1- cells combinations search-wordlist
 1562:     s" the prefix for this superinstruction must be defined earlier" ?print-error
 1563:     ." {"
 1564:     execute prim-num @ 5 .r ." ,"
 1565:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
 1566:     combined prim-num @ 5 .r ." }, /* "
 1567:     combined prim-c-name 2@ type ."  */"
 1568:     cr ;
 1569: 
 1570: 
 1571: \ cost and superinstruction data for a sophisticated combiner (e.g.,
 1572: \ shortest path)
 1573: 
 1574: \ This is intended as initializer for a structure like this
 1575: 
 1576: \  struct cost {
 1577: \    char loads;       /* number of stack loads */
 1578: \    char stores;      /* number of stack stores */
 1579: \    char updates;     /* number of stack pointer updates */
 1580: \    char branch;      /* is it a branch (SET_IP) */
 1581: \    char state_in;    /* state on entry */
 1582: \    char state_out;   /* state on exit */
 1583: \    short offset;     /* offset into super2 table */
 1584: \    char length;      /* number of components */
 1585: \  };
 1586: 
 1587: \ How do you know which primitive or combined instruction this
 1588: \ structure refers to?  By the order of cost structures, as in most
 1589: \ other cases.
 1590: 
 1591: : super2-length ( -- n )
 1592:     combined if
 1593: 	num-combined @
 1594:     else
 1595: 	1
 1596:     endif ;
 1597: 
 1598: : compute-costs { p -- nloads nstores nupdates }
 1599:     \ compute the number of loads, stores, and stack pointer updates
 1600:     \ of a primitive or combined instruction; does not take TOS
 1601:     \ caching into account
 1602:     0 max-stacks 0 +do
 1603: 	p prim-stacks-in i th @ +
 1604:     loop
 1605:     super2-length 1- - \ don't count instruction fetches of subsumed insts
 1606:     0 max-stacks 0 +do
 1607: 	p prim-stacks-out i th @ +
 1608:     loop
 1609:     0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
 1610: 	p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
 1611:     loop ;
 1612: 
 1613: : output-num-part ( p -- )
 1614:     ." N_" prim-c-name-orig 2@ type ." ," ;
 1615:     \ prim-num @ 4 .r ." ," ;
 1616: 
 1617: : output-name-comment ( -- )
 1618:     ."  /* " prim prim-name 2@ prim-type ."  */" ;
 1619: 
 1620: variable offset-super2  0 offset-super2 ! \ offset into the super2 table
 1621: 
 1622: : output-costs-prefix ( -- )
 1623:     ." {" prim compute-costs
 1624:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
 1625:     prim prim-branch? negate . ." ,"
 1626:     state-in  state-number @ 2 .r ." ,"
 1627:     state-out state-number @ 2 .r ." ,"
 1628:     inst-stream stack-in @ 1 .r ." ,"
 1629: ;
 1630: 
 1631: : output-costs-gforth-simple ( -- )
 1632:     output-costs-prefix
 1633:     prim output-num-part
 1634:     1 2 .r ." },"
 1635:     output-name-comment
 1636:     cr ;
 1637: 
 1638: : output-costs-gforth-combined ( -- )
 1639:     output-costs-prefix
 1640:     ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
 1641:     super2-length dup 2 .r ." }," offset-super2 +!
 1642:     output-name-comment
 1643:     cr ;
 1644: 
 1645: \  : output-costs ( -- )
 1646: \      \ description of superinstructions and simple instructions
 1647: \      ." {" prim compute-costs
 1648: \      rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
 1649: \      offset-super2 @ 5 .r ." ,"
 1650: \      super2-length dup 2 .r ." ," offset-super2 +!
 1651: \      inst-stream stack-in @ 1 .r ." },"
 1652: \      output-name-comment
 1653: \      cr ;
 1654: 
 1655: : output-super2-simple ( -- )
 1656:     prim prim-c-name 2@ prim prim-c-name-orig 2@ d= if
 1657: 	prim output-num-part
 1658: 	output-name-comment
 1659: 	cr
 1660:     endif ;   
 1661:   
 1662: : output-super2-combined ( -- )
 1663:     ['] output-num-part map-combined 
 1664:     output-name-comment
 1665:     cr ;   
 1666: 
 1667: \ the parser
 1668: 
 1669: eof-char max-member \ the whole character set + EOF
 1670: 
 1671: : getinput ( -- n )
 1672:  rawinput @ endrawinput @ =
 1673:  if
 1674:    eof-char
 1675:  else
 1676:    cookedinput @ c@
 1677:  endif ;
 1678: 
 1679: :noname ( n -- )
 1680:  dup bl > if
 1681:   emit space
 1682:  else
 1683:   .
 1684:  endif ;
 1685: print-token !
 1686: 
 1687: : testchar? ( set -- f )
 1688:  getinput member? ;
 1689: ' testchar? test-vector !
 1690: 
 1691: : checksynclines ( -- )
 1692:     \ when input points to a newline, check if the next line is a
 1693:     \ sync line.  If it is, perform the appropriate actions.
 1694:     rawinput @ begin >r
 1695: 	s" #line " r@ over compare if
 1696: 	    rdrop 1 line +! EXIT
 1697: 	endif
 1698: 	0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
 1699: 	dup c@ bl = if
 1700: 	    char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
 1701: 	    char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
 1702: 	    char+
 1703: 	endif
 1704: 	dup c@ nl-char <> 0= s" sync line syntax" ?print-error
 1705: 	skipsynclines @ if
 1706: 	    char+ dup rawinput !
 1707: 	    rawinput @ c@ cookedinput @ c!
 1708: 	endif
 1709:     again ;
 1710: 
 1711: : ?nextchar ( f -- )
 1712:     s" syntax error, wrong char" ?print-error
 1713:     rawinput @ endrawinput @ <> if
 1714: 	rawinput @ c@
 1715: 	1 chars rawinput +!
 1716: 	1 chars cookedinput +!
 1717: 	nl-char = if
 1718: 	    checksynclines
 1719: 	    rawinput @ line-start !
 1720: 	endif
 1721: 	rawinput @ c@
 1722: 	cookedinput @ c!
 1723:     endif ;
 1724: 
 1725: : charclass ( set "name" -- )
 1726:  ['] ?nextchar terminal ;
 1727: 
 1728: : .. ( c1 c2 -- set )
 1729:  ( creates a set that includes the characters c, c1<=c<=c2 )
 1730:  empty copy-set
 1731:  swap 1+ rot do
 1732:   i over add-member
 1733:  loop ;
 1734: 
 1735: : ` ( -- terminal ) ( use: ` c )
 1736:  ( creates anonymous terminal for the character c )
 1737:  char singleton ['] ?nextchar make-terminal ;
 1738: 
 1739: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
 1740: char 0 char 9 ..					charclass digit
 1741: bl singleton tab-char over add-member			charclass white
 1742: nl-char singleton eof-char over add-member complement	charclass nonl
 1743: nl-char singleton eof-char over add-member
 1744:     char : over add-member complement                   charclass nocolonnl
 1745: nl-char singleton eof-char over add-member
 1746:     char } over add-member complement                   charclass nobracenl
 1747: bl 1+ maxchar .. char \ singleton complement intersection
 1748:                                                         charclass nowhitebq
 1749: bl 1+ maxchar ..                                        charclass nowhite
 1750: char " singleton eof-char over add-member complement	charclass noquote
 1751: nl-char singleton					charclass nl
 1752: eof-char singleton					charclass eof
 1753: nl-char singleton eof-char over add-member		charclass nleof
 1754: 
 1755: (( letter (( letter || digit )) **
 1756: )) <- c-ident ( -- )
 1757: 
 1758: (( ` # ?? (( letter || digit || ` : )) ++ (( ` . ` . ` . )) ??
 1759: )) <- stack-ident ( -- )
 1760: 
 1761: (( nowhitebq nowhite ** ))
 1762: <- forth-ident ( -- )
 1763: 
 1764: Variable forth-flag
 1765: Variable c-flag
 1766: 
 1767: (( (( ` e || ` E )) {{ start }} nonl ** 
 1768:    {{ end evaluate }}
 1769: )) <- eval-comment ( ... -- ... )
 1770: 
 1771: (( (( ` f || ` F )) {{ start }} nonl ** 
 1772:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
 1773: )) <- forth-comment ( -- )
 1774: 
 1775: (( (( ` c || ` C )) {{ start }} nonl ** 
 1776:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
 1777: )) <- c-comment ( -- )
 1778: 
 1779: (( ` - nonl ** {{ 
 1780: 	forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
 1781: 	c-flag @ IF
 1782: 	    function-diff
 1783: 	    ." #else /* " function-number @ 0 .r ."  */" cr THEN }}
 1784: )) <- else-comment
 1785: 
 1786: (( ` + {{ start }} nonl ** {{ end
 1787: 	dup
 1788: 	IF	c-flag @
 1789: 	    IF
 1790: 		function-diff
 1791: 		." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
 1792: 		THEN
 1793: 		forth-flag @
 1794: 		IF  forth-fdiff  ." has? " type ."  [IF]"  cr THEN
 1795: 	ELSE	2drop
 1796: 	    c-flag @      IF
 1797: 		function-diff  ." #endif" cr THEN
 1798: 	    forth-flag @  IF  forth-fdiff  ." [THEN]"  cr THEN
 1799: 	THEN }}
 1800: )) <- if-comment
 1801: 
 1802: (( (( ` g || ` G )) {{ start }} nonl **
 1803:    {{ end
 1804:       forth-flag @ IF  forth-fdiff  ." group " type cr  THEN
 1805:       c-flag @     IF  function-diff
 1806: 	  ." GROUP(" type ." , " function-number @ 0 .r ." )" cr  THEN }}
 1807: )) <- group-comment
 1808: 
 1809: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
 1810: 
 1811: (( ` \ comment-body nleof )) <- comment ( -- )
 1812: 
 1813: (( {{ start }} stack-ident {{ end init-item1 }} white ** )) **
 1814: <- stack-items ( addr1 -- addr2 )
 1815: 
 1816: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
 1817:    ` - ` - white **
 1818:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
 1819: )) <- stack-effect ( -- )
 1820: 
 1821: (( {{ prim create-prim }}
 1822:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
 1823:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
 1824:       (( {{ start }}  c-ident {{ end 2dup prim-c-name-2! }} )) ??
 1825:    )) ??  nleof
 1826:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
 1827:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
 1828:    (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
 1829:    || (( nocolonnl nonl **  nleof white ** )) ** ))
 1830:    {{ end prim prim-c-code 2! skipsynclines on }}
 1831:    (( ` :  white ** nleof
 1832:       {{ start }} (( nonl ++  nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
 1833:    )) ?? {{ process-simple }}
 1834:    nleof
 1835: )) <- simple-primitive ( -- )
 1836: 
 1837: (( {{ init-combined }}
 1838:    ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
 1839:    nleof {{ process-combined }}
 1840: )) <- combined-primitive
 1841: 
 1842: (( {{ make-prim to prim 0 to combined
 1843:       line @ name-line ! filename 2@ name-filename 2!
 1844:       function-number @ prim prim-num !
 1845:       start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
 1846:       2dup prim prim-name 2! prim-c-name-2! }}  white **
 1847:    (( ` / white ** {{ start }} c-ident {{ end prim-c-name-2! }} white ** )) ??
 1848:    (( simple-primitive || combined-primitive ))
 1849:    {{ 1 function-number +! }}
 1850: )) <- primitive ( -- )
 1851: 
 1852: (( (( comment || primitive || nl white ** )) ** eof ))
 1853: parser primitives2something
 1854: warnings @ [IF]
 1855: .( parser generated ok ) cr
 1856: [THEN]
 1857: 
 1858: 
 1859: \ run with gforth-0.5.0 (slurp-file is missing)
 1860: [IFUNDEF] slurp-file
 1861: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
 1862:     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
 1863:     r/o bin open-file throw >r
 1864:     r@ file-size throw abort" file too large"
 1865:     dup allocate throw swap
 1866:     2dup r@ read-file throw over <> abort" could not read whole file"
 1867:     r> close-file throw ;
 1868: [THEN]
 1869: 
 1870: : primfilter ( addr u -- )
 1871:     \ process the string at addr u
 1872:     over dup rawinput ! dup line-start ! cookedinput !
 1873:     + endrawinput !
 1874:     checksynclines
 1875:     primitives2something ;    
 1876: 
 1877: : unixify ( c-addr u1 -- c-addr u2 )
 1878:     \ delete crs from the string
 1879:     bounds tuck tuck ?do ( c-addr1 )
 1880: 	i c@ dup #cr <> if
 1881: 	    over c! char+
 1882: 	else
 1883: 	    drop
 1884: 	endif
 1885:     loop
 1886:     over - ;
 1887: 
 1888: : process-file ( addr u xt-simple x-combined -- )
 1889:     output-combined ! output !
 1890:     save-mem 2dup filename 2!
 1891:     slurp-file unixify
 1892:     warnings @ if
 1893: 	." ------------ CUT HERE -------------" cr  endif
 1894:     primfilter ;
 1895: 
 1896: \  : process      ( xt -- )
 1897: \      bl word count rot
 1898: \      process-file ;

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