File:  [gforth] / gforth / prims2x.fs
Revision 1.161: download - view: text, annotated - select for diffs
Fri Jan 27 10:43:52 2006 UTC (18 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
added support for disabling states; disabled S3-S8 (otherwise gcc-2.95
  ICEs on i386).
added G5 benchmark results
documentation changes

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

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