File:  [gforth] / gforth / prims2x.fs
Revision 1.165: download - view: text, annotated - select for diffs
Fri Feb 23 22:33:21 2007 UTC (17 years ago) by anton
Branches: MAIN
CVS tags: HEAD
changed >OUTFILE ... OUTFILE< to OUTFILE-EXECUTE
changed >INFILE ... INFILE< to INFILE-EXECUTE
added BASE-EXECUTE
related documentation changes

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

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