File:  [gforth] / gforth / prims2x0.6.2.fs
Revision 1.9: download - view: text, annotated - select for diffs
Thu Dec 31 15:32:35 2009 UTC (9 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

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

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