File:  [gforth] / gforth / prims2x.fs
Revision 1.153: download - view: text, annotated - select for diffs
Sat Jan 22 22:16:59 2005 UTC (19 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
further work on gcc PR 15242 workaround (now works on AMD64 gforth-fast)
made treatment of Priminfo length and restlen more regular

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

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