File:  [gforth] / gforth / prims2x.fs
Revision 1.145: download - view: text, annotated - select for diffs
Thu Oct 9 20:25:59 2003 UTC (16 years ago) by anton
Branches: MAIN
CVS tags: HEAD
replaced gforth-prof with $(PROFEXES) in Makedist.in
more stack caching work

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

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