File:  [gforth] / gforth / prims2x.fs
Revision 1.106: download - view: text, annotated - select for diffs
Sun Jun 2 15:46:16 2002 UTC (21 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
vmgen-related changes:
in prims2x:
  Conversion macros for single items now take 2 arguments
  Converting from two items to a type has changed order
  argument printing for disassembler disabled (for now)
  disassembler now also uses VM_IS_INST
in Gforth and vmgen-ex: adapted to work with changed prims2x
new: vmgen-ex2: uses union for Cell instead of casting (lots of
   changes compared to vmgen-ex)

    1: \ converts primitives to, e.g., C code 
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000 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: warnings off
   55: 
   56: [IFUNDEF] try
   57: include startup.fs
   58: [THEN]
   59: 
   60: : struct% struct ; \ struct is redefined in gray
   61: 
   62: warnings off
   63: 
   64: include ./gray.fs
   65: 
   66: 32 constant max-effect \ number of things on one side of a stack effect
   67: 4 constant max-stacks  \ the max. number of stacks (including inst-stream).
   68: 255 constant maxchar
   69: maxchar 1+ constant eof-char
   70: #tab constant tab-char
   71: #lf constant nl-char
   72: 
   73: variable rawinput \ pointer to next character to be scanned
   74: variable endrawinput \ pointer to the end of the input (the char after the last)
   75: variable cookedinput \ pointer to the next char to be parsed
   76: variable line \ line number of char pointed to by input
   77: variable line-start \ pointer to start of current line (for error messages)
   78: 0 line !
   79: 2variable filename \ filename of original input file
   80: 0 0 filename 2!
   81: 2variable f-comment
   82: 0 0 f-comment 2!
   83: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
   84: skipsynclines on 
   85: 
   86: : th ( addr1 n -- addr2 )
   87:     cells + ;
   88: 
   89: : holds ( addr u -- )
   90:     \ like HOLD, but for a string
   91:     tuck + swap 0 +do
   92: 	1- dup c@ hold
   93:     loop
   94:     drop ;
   95: 
   96: : insert-wordlist { c-addr u wordlist xt -- }
   97:     \ adds name "addr u" to wordlist using defining word xt
   98:     \ xt may cause additional stack effects
   99:     get-current >r wordlist set-current
  100:     c-addr u nextname xt execute
  101:     r> set-current ;
  102: 
  103: : start ( -- addr )
  104:  cookedinput @ ;
  105: 
  106: : end ( addr -- addr u )
  107:  cookedinput @ over - ;
  108: 
  109: : print-error-line ( -- )
  110:     \ print the current line and position
  111:     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
  112:     over - type cr
  113:     line-start @ rawinput @ over - typewhite ." ^" cr ;
  114: 
  115: : ?print-error { f addr u -- }
  116:     f ?not? if
  117: 	outfile-id >r try
  118: 	    stderr to outfile-id
  119: 	    filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
  120: 	    print-error-line
  121: 	    0
  122: 	recover endtry
  123: 	r> to outfile-id throw
  124: 	abort
  125:     endif ;
  126: 
  127: : quote ( -- )
  128:     [char] " emit ;
  129: 
  130: variable output          \ xt ( -- ) of output word for simple primitives
  131: variable output-combined \ xt ( -- ) of output word for combined primitives
  132: 
  133: struct%
  134:     cell%    field stack-number \ the number of this stack
  135:     cell% 2* field stack-pointer \ stackpointer name
  136:     cell%    field stack-type \ name for default type of stack items
  137:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
  138: end-struct stack%
  139: 
  140: struct%
  141:  cell% 2* field item-name   \ name, excluding stack prefixes
  142:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
  143:  cell%    field item-type   \ descriptor for the item type
  144:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
  145:  cell%	  field item-first  \ true if this is the first occurence of the item
  146: end-struct item%
  147: 
  148: struct%
  149:     cell% 2* field type-c-name
  150:     cell%    field type-stack \ default stack
  151:     cell%    field type-size  \ size of type in stack items
  152:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
  153:     cell%    field type-store \ xt of store code generator ( item -- )
  154: end-struct type%
  155: 
  156: variable next-stack-number 0 next-stack-number !
  157: create stacks max-stacks cells allot \ array of stacks
  158: 
  159: : stack-in-index ( in-size item -- in-index )
  160:     item-offset @ - 1- ;
  161: 
  162: : inst-in-index ( in-size item -- in-index )
  163:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
  164: 
  165: : make-stack ( addr-ptr u1 type "stack-name" -- )
  166:     next-stack-number @ max-stacks < s" too many stacks" ?print-error
  167:     create stack% %allot >r
  168:     r@ stacks next-stack-number @ th !
  169:     next-stack-number @ r@ stack-number !
  170:     1 next-stack-number +!
  171:     r@ stack-type !
  172:     save-mem r@ stack-pointer 2! 
  173:     ['] stack-in-index r> stack-in-index-xt ! ;
  174: 
  175: : map-stacks { xt -- }
  176:     \ perform xt for all stacks except inst-stream
  177:     next-stack-number @ 1 +do
  178: 	stacks i th @ xt execute
  179:     loop ;
  180: 
  181: \ stack items
  182: 
  183: : init-item ( addr u addr1 -- )
  184:     \ initialize item at addr1 with name addr u
  185:     \ !! remove stack prefix
  186:     dup item% %size erase
  187:     item-name 2! ;
  188: 
  189: : map-items { addr end xt -- }
  190:     \ perform xt for all items in array addr...end
  191:     end addr ?do
  192: 	i xt execute
  193:     item% %size +loop ;
  194: 
  195: \ types
  196: 
  197: : print-type-prefix ( type -- )
  198:     body> >head name>string type ;
  199: 
  200: \ various variables for storing stuff of one primitive
  201: 
  202: struct%
  203:     cell% 2* field prim-name
  204:     cell% 2* field prim-wordset
  205:     cell% 2* field prim-c-name
  206:     cell% 2* field prim-doc
  207:     cell% 2* field prim-c-code
  208:     cell% 2* field prim-forth-code
  209:     cell% 2* field prim-stack-string
  210:     cell%    field prim-num            \ ordinal number
  211:     cell%    field prim-items-wordlist \ unique items
  212:     item% max-effect * field prim-effect-in
  213:     item% max-effect * field prim-effect-out
  214:     cell%    field prim-effect-in-end
  215:     cell%    field prim-effect-out-end
  216:     cell% max-stacks * field prim-stacks-in  \ number of in items per stack
  217:     cell% max-stacks * field prim-stacks-out \ number of out items per stack
  218: end-struct prim%
  219: 
  220: : make-prim ( -- prim )
  221:     prim% %alloc { p }
  222:     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
  223:     p ;
  224: 
  225: 0 value prim     \ in combined prims either combined or a part
  226: 0 value combined \ in combined prims the combined prim
  227: variable in-part \ true if processing a part
  228:  in-part off
  229: 
  230: 1000 constant max-combined
  231: create combined-prims max-combined cells allot
  232: variable num-combined
  233: 
  234: table constant combinations
  235:   \ the keys are the sequences of pointers to primitives
  236: 
  237: create current-depth max-stacks cells allot
  238: create max-depth     max-stacks cells allot
  239: create min-depth     max-stacks cells allot
  240: 
  241: wordlist constant primitives
  242: 
  243: : create-prim ( prim -- )
  244:     dup prim-name 2@ primitives ['] constant insert-wordlist ;
  245: 
  246: : stack-in ( stack -- addr )
  247:     \ address of number of stack items in effect in
  248:     stack-number @ cells prim prim-stacks-in + ;
  249: 
  250: : stack-out ( stack -- addr )
  251:     \ address of number of stack items in effect out
  252:     stack-number @ cells prim prim-stacks-out + ;
  253: 
  254: \ global vars
  255: variable c-line
  256: 2variable c-filename
  257: variable name-line
  258: 2variable name-filename
  259: 2variable last-name-filename
  260: Variable function-number 0 function-number !
  261: 
  262: \ a few more set ops
  263: 
  264: : bit-equivalent ( w1 w2 -- w3 )
  265:  xor invert ;
  266: 
  267: : complement ( set1 -- set2 )
  268:  empty ['] bit-equivalent binary-set-operation ;
  269: 
  270: \ stack access stuff
  271: 
  272: : normal-stack-access ( n stack -- )
  273:     stack-pointer 2@ type
  274:     dup
  275:     if
  276: 	." [" 0 .r ." ]"
  277:     else
  278: 	drop ." TOS"
  279:     endif ;
  280: 
  281: \ forward declaration for inst-stream (breaks cycle in definitions)
  282: defer inst-stream-f ( -- stack )
  283: 
  284: : part-stack-access { n stack -- }
  285:     \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
  286:     ." _" stack stack-pointer 2@ type
  287:     stack stack-number @ { stack# }
  288:     current-depth stack# th @ n + { access-depth }
  289:     stack inst-stream-f = if
  290: 	access-depth
  291:     else
  292: 	combined prim-stacks-in stack# th @
  293: 	assert( dup max-depth stack# th @ = )
  294: 	access-depth - 1-
  295:     endif
  296:     0 .r ;
  297: 
  298: : stack-access ( n stack -- )
  299:     \ print a stack access at index n of stack
  300:     in-part @ if
  301: 	part-stack-access
  302:     else
  303: 	normal-stack-access
  304:     endif ;
  305: 
  306: : item-in-index { item -- n }
  307:     \ n is the index of item (in the in-effect)
  308:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
  309:     item r> stack-in-index-xt @ execute ;
  310: 
  311: : item-stack-type-name ( item -- addr u )
  312:     item-stack @ stack-type @ type-c-name 2@ ;
  313: 
  314: : fetch-single ( item -- )
  315:     \ fetch a single stack item from its stack
  316:     >r
  317:     ." vm_" r@ item-stack-type-name type
  318:     ." 2" r@ item-type @ print-type-prefix ." ("
  319:     r@ item-in-index r@ item-stack @ stack-access ." ,"
  320:     r@ item-name 2@ type
  321:     ." );" cr
  322:     rdrop ; 
  323: 
  324: : fetch-double ( item -- )
  325:     \ fetch a double stack item from its stack
  326:     >r
  327:     ." vm_two"
  328:     r@ item-stack-type-name type ." 2"
  329:     r@ item-type @ print-type-prefix ." ("
  330:     r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
  331:     ." , "                      -1 under+ ." (Cell)" stack-access
  332:     ." , " r@ item-name 2@ type
  333:     ." )" cr
  334:     rdrop ;
  335: 
  336: : same-as-in? ( item -- f )
  337:  \ f is true iff the offset and stack of item is the same as on input
  338:  >r
  339:  r@ item-first @ if
  340:      rdrop false exit
  341:  endif
  342:  r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
  343:  execute @
  344:  dup r@ =
  345:  if \ item first appeared in output
  346:    drop false
  347:  else
  348:    dup  item-stack  @ r@ item-stack  @ = 
  349:    swap item-offset @ r@ item-offset @ = and
  350:  endif
  351:  rdrop ;
  352: 
  353: : item-out-index ( item -- n )
  354:     \ n is the index of item (in the in-effect)
  355:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
  356: 
  357: : really-store-single ( item -- )
  358:     >r
  359:     ." vm_"
  360:     r@ item-type @ print-type-prefix ." 2"
  361:     r@ item-stack-type-name type ." ("
  362:     r@ item-name 2@ type ." ,"
  363:     r@ item-out-index r@ item-stack @ stack-access ." );"
  364:     rdrop ;
  365: 
  366: : store-single ( item -- )
  367:  >r
  368:  r@ same-as-in?
  369:  if
  370:    r@ item-in-index 0= r@ item-out-index 0= xor
  371:    if
  372:        ." IF_" r@ item-stack @ stack-pointer 2@ type
  373:        ." TOS(" r@ really-store-single ." );" cr
  374:    endif
  375:  else
  376:    r@ really-store-single cr
  377:  endif
  378:  rdrop ;
  379: 
  380: : store-double ( item -- )
  381: \ !! store optimization is not performed, because it is not yet needed
  382:  >r
  383:  ." vm_"
  384:  r@ item-type @ print-type-prefix ." 2two"
  385:  r@ item-stack-type-name type ." ("
  386:  r@ item-name 2@ type ." , "
  387:  r@ item-out-index r@ item-stack @ 2dup stack-access
  388:  ." , "                       -1 under+ stack-access
  389:  ." )" cr
  390:  rdrop ;
  391: 
  392: : single ( -- xt1 xt2 n )
  393:     ['] fetch-single ['] store-single 1 ;
  394: 
  395: : double ( -- xt1 xt2 n )
  396:     ['] fetch-double ['] store-double 2 ;
  397: 
  398: : s, ( addr u -- )
  399: \ allocate a string
  400:  here swap dup allot move ;
  401: 
  402: wordlist constant prefixes
  403: 
  404: : declare ( addr "name" -- )
  405: \ remember that there is a stack item at addr called name
  406:  create , ;
  407: 
  408: : !default ( w addr -- )
  409:     dup @ if
  410: 	2drop \ leave nonzero alone
  411:     else
  412: 	!
  413:     endif ;
  414: 
  415: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
  416:     \ describes a type
  417:     \ addr u specifies the C type name
  418:     \ stack effect entries of the type start with prefix
  419:     create type% %allot >r
  420:     addr u save-mem r@ type-c-name 2!
  421:     xt1   r@ type-fetch !
  422:     xt2   r@ type-store !
  423:     n     r@ type-size !
  424:     stack r@ type-stack !
  425:     rdrop ;
  426: 
  427: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
  428:     get-current >r prefixes set-current
  429:     create-type r> set-current
  430: does> ( item -- )
  431:     \ initialize item
  432:     { item typ }
  433:     typ item item-type !
  434:     typ type-stack @ item item-stack !default
  435:     item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
  436: 	item item-name 2@ nextname item declare
  437: 	item item-first on
  438: 	\ typ type-c-name 2@ type space type  ." ;" cr
  439:     else
  440: 	drop
  441: 	item item-first off
  442:     endif ;
  443: 
  444: : execute-prefix ( item addr1 u1 -- )
  445:     \ execute the word ( item -- ) associated with the longest prefix
  446:     \ of addr1 u1
  447:     0 swap ?do
  448: 	dup i prefixes search-wordlist
  449: 	if \ ok, we have the type ( item addr1 xt )
  450: 	    nip execute
  451: 	    UNLOOP EXIT
  452: 	endif
  453: 	-1 s+loop
  454:     \ we did not find a type, abort
  455:     false s" unknown prefix" ?print-error ;
  456: 
  457: : declaration ( item -- )
  458:     dup item-name 2@ execute-prefix ;
  459: 
  460: : declaration-list ( addr1 addr2 -- )
  461:     ['] declaration map-items ;
  462: 
  463: : declarations ( -- )
  464:  wordlist dup prim prim-items-wordlist ! set-current
  465:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
  466:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
  467: 
  468: : print-declaration { item -- }
  469:     item item-first @ if
  470: 	item item-type @ type-c-name 2@ type space
  471: 	item item-name 2@ type ." ;" cr
  472:     endif ;
  473: 
  474: : print-declarations ( -- )
  475:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
  476:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
  477:     
  478: : stack-prefix ( stack "prefix" -- )
  479:     get-current >r prefixes set-current
  480:     name tuck nextname create ( stack length ) 2,
  481:     r> set-current
  482: does> ( item -- )
  483:     2@ { item stack prefix-length }
  484:     item item-name 2@ prefix-length /string item item-name 2!
  485:     stack item item-stack !
  486:     item declaration ;
  487: 
  488: \ types pointed to by stacks for use in combined prims
  489: \ !! output-c-combined shouldn't use these names!
  490: : stack-type-name ( addr u "name" -- )
  491:     single 0 create-type ;
  492: 
  493: wordlist constant type-names \ this is here just to meet the requirement
  494:                     \ that a type be a word; it is never used for lookup
  495: 
  496: : stack ( "name" "stack-pointer" "type" -- )
  497:     \ define stack
  498:     name { d: stack-name }
  499:     name { d: stack-pointer }
  500:     name { d: stack-type }
  501:     get-current type-names set-current
  502:     stack-type 2dup nextname stack-type-name
  503:     set-current
  504:     stack-pointer lastxt >body stack-name nextname make-stack ;
  505: 
  506: stack inst-stream IP Cell
  507: ' inst-in-index inst-stream stack-in-index-xt !
  508: ' inst-stream <is> inst-stream-f
  509: \ !! initialize stack-in and stack-out
  510: 
  511: \ offset computation
  512: \ the leftmost (i.e. deepest) item has offset 0
  513: \ the rightmost item has the highest offset
  514: 
  515: : compute-offset { item xt -- }
  516:     \ xt specifies in/out; update stack-in/out and set item-offset
  517:     item item-type @ type-size @
  518:     item item-stack @ xt execute dup @ >r +!
  519:     r> item item-offset ! ;
  520: 
  521: : compute-offset-in ( addr1 addr2 -- )
  522:     ['] stack-in compute-offset ;
  523: 
  524: : compute-offset-out ( addr1 addr2 -- )
  525:     ['] stack-out compute-offset ;
  526: 
  527: : clear-stack { -- }
  528:     dup stack-in off stack-out off ;
  529: 
  530: : compute-offsets ( -- )
  531:     ['] clear-stack map-stacks
  532:     inst-stream clear-stack
  533:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
  534:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
  535:     inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
  536: 
  537: : process-simple ( -- )
  538:     prim prim { W^ key } key cell
  539:     combinations ['] constant insert-wordlist
  540:     declarations compute-offsets
  541:     output @ execute ;
  542: 
  543: : flush-a-tos { stack -- }
  544:     stack stack-out @ 0<> stack stack-in @ 0= and
  545:     if
  546: 	." IF_" stack stack-pointer 2@ 2dup type ." TOS("
  547: 	2dup type ." [0] = " type ." TOS);" cr
  548:     endif ;
  549: 
  550: : flush-tos ( -- )
  551:     ['] flush-a-tos map-stacks ;
  552: 
  553: : fill-a-tos { stack -- }
  554:     stack stack-out @ 0= stack stack-in @ 0<> and
  555:     if
  556: 	." IF_" stack stack-pointer 2@ 2dup type ." TOS("
  557: 	2dup type ." TOS = " type ." [0]);" cr
  558:     endif ;
  559: 
  560: : fill-tos ( -- )
  561:     \ !! inst-stream for prefetching?
  562:     ['] fill-a-tos map-stacks ;
  563: 
  564: : fetch ( addr -- )
  565:     dup item-type @ type-fetch @ execute ;
  566: 
  567: : fetches ( -- )
  568:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
  569: 
  570: : stack-pointer-update { stack -- }
  571:     \ stack grow downwards
  572:     stack stack-in @ stack stack-out @ -
  573:     ?dup-if \ this check is not necessary, gcc would do this for us
  574: 	stack stack-pointer 2@ type ."  += " 0 .r ." ;" cr
  575:     endif ;
  576: 
  577: : inst-pointer-update ( -- )
  578:     inst-stream stack-in @ ?dup-if
  579: 	." INC_IP(" 0 .r ." );" cr
  580:     endif ;
  581: 
  582: : stack-pointer-updates ( -- )
  583:     inst-pointer-update
  584:     ['] stack-pointer-update map-stacks ;
  585: 
  586: : store ( item -- )
  587: \ f is true if the item should be stored
  588: \ f is false if the store is probably not necessary
  589:  dup item-type @ type-store @ execute ;
  590: 
  591: : stores ( -- )
  592:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
  593: 
  594: : print-debug-arg { item -- }
  595:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
  596:     ." printarg_" item item-type @ print-type-prefix
  597:     ." (" item item-name 2@ type ." );" cr ;
  598:     
  599: : print-debug-args ( -- )
  600:     ." #ifdef VM_DEBUG" cr
  601:     ." if (vm_debug) {" cr
  602:     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
  603: \    ." fputc('\n', vm_out);" cr
  604:     ." }" cr
  605:     ." #endif" cr ;
  606: 
  607: : print-debug-result { item -- }
  608:     item item-first @ if
  609: 	item print-debug-arg
  610:     endif ;
  611: 
  612: : print-debug-results ( -- )
  613:     cr
  614:     ." #ifdef VM_DEBUG" cr
  615:     ." if (vm_debug) {" cr
  616:     ." fputs(" quote ."  -- " quote ." , vm_out); "
  617:     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
  618:     ." fputc('\n', vm_out);" cr
  619:     ." }" cr
  620:     ." #endif" cr ;
  621: 
  622: : output-super-end ( -- )
  623:     prim prim-c-code 2@ s" SET_IP" search if
  624: 	." SUPER_END;" cr
  625:     endif
  626:     2drop ;
  627: 
  628: : output-c-tail ( -- )
  629:     \ the final part of the generated C code
  630:     output-super-end
  631:     print-debug-results
  632:     ." NEXT_P1;" cr
  633:     stores
  634:     fill-tos
  635:     ." NEXT_P2;" ;
  636: 
  637: : type-c-code ( c-addr u xt -- )
  638:     \ like TYPE, but replaces "TAIL;" with tail code produced by xt
  639:     { xt }
  640:     begin ( c-addr1 u1 )
  641: 	2dup s" TAIL;" search
  642:     while ( c-addr1 u1 c-addr3 u3 )
  643: 	2dup 2>r drop nip over - type
  644: 	xt execute
  645: 	2r> 5 /string
  646: 	\ !! resync #line missing
  647:     repeat
  648:     2drop type ;
  649: 
  650: : print-entry ( -- )
  651:     ." LABEL(" prim prim-c-name 2@ type ." ):" ;
  652:     
  653: : output-c ( -- ) 
  654:  print-entry ."  /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
  655:  ." /* " prim prim-doc 2@ type ."  */" cr
  656:  ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
  657:  ." {" cr
  658:  ." DEF_CA" cr
  659:  print-declarations
  660:  ." NEXT_P0;" cr
  661:  flush-tos
  662:  fetches
  663:  print-debug-args
  664:  stack-pointer-updates
  665:  ." {" cr
  666:  ." #line " c-line @ . quote c-filename 2@ type quote cr
  667:  prim prim-c-code 2@ ['] output-c-tail type-c-code
  668:  ." }" cr
  669:  output-c-tail
  670:  ." }" cr
  671:  cr
  672: ;
  673: 
  674: : disasm-arg { item -- }
  675:     item item-stack @ inst-stream = if
  676: 	."   fputc(' ', vm_out); "
  677: 	\ !! change this to first convert args to the right type and
  678: 	\ then print them
  679: 	." /* printarg_" item item-type @ print-type-prefix
  680: 	." ((" item item-type @ type-c-name 2@ type ." )"
  681: 	." ip[" item item-offset @ 1+ 0 .r ." ]); */" cr
  682:     endif ;
  683: 
  684: : disasm-args ( -- )
  685:     prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
  686: 
  687: : output-disasm ( -- )
  688:     \ generate code for disassembling VM instructions
  689:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
  690:     ."   fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
  691:     disasm-args
  692:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
  693:     ."   goto _endif_;" cr
  694:     ." }" cr ;
  695: 
  696: : output-profile ( -- )
  697:     \ generate code for postprocessing the VM block profile stuff
  698:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
  699:     ."   add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
  700:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
  701:     prim prim-c-code 2@  s" SET_IP"    search nip nip
  702:     prim prim-c-code 2@  s" SUPER_END" search nip nip or if
  703: 	."   return;" cr
  704:     else
  705: 	."   goto _endif_;" cr
  706:     endif
  707:     ." }" cr ;
  708: 
  709: : output-profile-combined ( -- )
  710:     \ generate code for postprocessing the VM block profile stuff
  711:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
  712:     num-combined @ 0 +do
  713: 	."   add_inst(b, " quote
  714: 	combined-prims i th @ prim-name 2@ type
  715: 	quote ." );" cr
  716:     loop
  717:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
  718:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SET_IP"    search nip nip
  719:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SUPER_END" search nip nip or if
  720: 	."   return;" cr
  721:     else
  722: 	."   goto _endif_;" cr
  723:     endif
  724:     ." }" cr ;
  725: 
  726: : output-superend ( -- )
  727:     \ output flag specifying whether the current word ends a dynamic superinst
  728:     prim prim-c-code 2@  s" SET_IP"    search nip nip
  729:     prim prim-c-code 2@  s" SUPER_END" search nip nip or 0<>
  730:     prim prim-c-code 2@  s" SUPER_CONTINUE" search nip nip 0= and
  731:     negate 0 .r ." , /* " prim prim-name 2@ type ."  */" cr ;
  732: 
  733: : gen-arg-parm { item -- }
  734:     item item-stack @ inst-stream = if
  735: 	." , " item item-type @ type-c-name 2@ type space
  736: 	item item-name 2@ type
  737:     endif ;
  738: 
  739: : gen-args-parm ( -- )
  740:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
  741: 
  742: : gen-arg-gen { item -- }
  743:     item item-stack @ inst-stream = if
  744: 	."   genarg_" item item-type @ print-type-prefix
  745:         ." (ctp, " item item-name 2@ type ." );" cr
  746:     endif ;
  747: 
  748: : gen-args-gen ( -- )
  749:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
  750: 
  751: : output-gen ( -- )
  752:     \ generate C code for generating VM instructions
  753:     ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
  754:     ." {" cr
  755:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
  756:     gen-args-gen
  757:     ." }" cr ;
  758: 
  759: : stack-used? { stack -- f }
  760:     stack stack-in @ stack stack-out @ or 0<> ;
  761: 
  762: : output-funclabel ( -- )
  763:   ." &I_" prim prim-c-name 2@ type ." ," cr ;
  764: 
  765: : output-forthname ( -- )
  766:   '" emit prim prim-name 2@ type '" emit ." ," cr ;
  767: 
  768: \  : output-c-func ( -- )
  769: \  \ used for word libraries
  770: \      ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP)      /* " prim prim-name 2@ type
  771: \      ."  ( " prim prim-stack-string 2@ type ."  ) */" cr
  772: \      ." /* " prim prim-doc 2@ type ."  */" cr
  773: \      ." NAME(" quote prim prim-name 2@ type quote ." )" cr
  774: \      \ debugging
  775: \      ." {" cr
  776: \      print-declarations
  777: \      \ !! don't know what to do about that
  778: \      inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
  779: \      data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
  780: \      fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
  781: \      return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
  782: \      flush-tos
  783: \      fetches
  784: \      stack-pointer-updates
  785: \      fp-stack   stack-used? IF ." *FP=fp;" cr THEN
  786: \      ." {" cr
  787: \      ." #line " c-line @ . quote c-filename 2@ type quote cr
  788: \      prim prim-c-code 2@ type
  789: \      ." }" cr
  790: \      stores
  791: \      fill-tos
  792: \      ." return (sp);" cr
  793: \      ." }" cr
  794: \      cr ;
  795: 
  796: : output-label ( -- )  
  797:     ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
  798: 
  799: : output-alias ( -- ) 
  800:     ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
  801: 
  802: : output-forth ( -- )  
  803:     prim prim-forth-code @ 0=
  804:     IF    	\ output-alias
  805: 	\ this is bad for ec: an alias is compiled if tho word does not exist!
  806: 	\ JAW
  807:     ELSE  ." : " prim prim-name 2@ type ."   ( "
  808: 	prim prim-stack-string 2@ type ." )" cr
  809: 	prim prim-forth-code 2@ type cr
  810:     THEN ;
  811: 
  812: : output-tag-file ( -- )
  813:     name-filename 2@ last-name-filename 2@ compare if
  814: 	name-filename 2@ last-name-filename 2!
  815: 	#ff emit cr
  816: 	name-filename 2@ type
  817: 	." ,0" cr
  818:     endif ;
  819: 
  820: : output-tag ( -- )
  821:     output-tag-file
  822:     prim prim-name 2@ 1+ type
  823:     127 emit
  824:     space prim prim-name 2@ type space
  825:     1 emit
  826:     name-line @ 0 .r
  827:     ." ,0" cr ;
  828: 
  829: : output-vi-tag ( -- )
  830:     name-filename 2@ type #tab emit
  831:     prim prim-name 2@ type #tab emit
  832:     ." /^" prim prim-name 2@ type ."  *(/" cr ;
  833: 
  834: [IFDEF] documentation
  835: : register-doc ( -- )
  836:     prim prim-name 2@ documentation ['] create insert-wordlist
  837:     prim prim-name 2@ 2,
  838:     prim prim-stack-string 2@ condition-stack-effect 2,
  839:     prim prim-wordset 2@ 2,
  840:     prim prim-c-name 2@ condition-pronounciation 2,
  841:     prim prim-doc 2@ 2, ;
  842: [THEN]
  843: 
  844: 
  845: \ combining instructions
  846: 
  847: \ The input should look like this:
  848: 
  849: \ lit_+ = lit +
  850: 
  851: \ The output should look like this:
  852: 
  853: \  I_lit_+:
  854: \  {
  855: \  DEF_CA
  856: \  Cell _x_ip0;
  857: \  Cell _x_sp0;
  858: \  Cell _x_sp1;
  859: \  NEXT_P0;
  860: \  _x_ip0 = (Cell) IPTOS;
  861: \  _x_sp0 = (Cell) spTOS;
  862: \  INC_IP(1);
  863: \  /* sp += 0; */
  864: \  /* lit ( #w -- w ) */
  865: \  /*  */
  866: \  NAME("lit")
  867: \  {
  868: \  Cell w;
  869: \  w = (Cell) _x_ip0;
  870: \  #ifdef VM_DEBUG
  871: \  if (vm_debug) {
  872: \  fputs(" w=", vm_out); printarg_w (w);
  873: \  fputc('\n', vm_out);
  874: \  }
  875: \  #endif
  876: \  {
  877: \  #line 136 "./prim"
  878: \  }
  879: \  _x_sp1 = (Cell)w;
  880: \  }
  881: \  I_plus:	/* + ( n1 n2 -- n ) */
  882: \  /*  */
  883: \  NAME("+")
  884: \  {
  885: \  DEF_CA
  886: \  Cell n1;
  887: \  Cell n2;
  888: \  Cell n;
  889: \  NEXT_P0;
  890: \  n1 = (Cell) _x_sp0;
  891: \  n2 = (Cell) _x_sp1;
  892: \  #ifdef VM_DEBUG
  893: \  if (vm_debug) {
  894: \  fputs(" n1=", vm_out); printarg_n (n1);
  895: \  fputs(" n2=", vm_out); printarg_n (n2);
  896: \  fputc('\n', vm_out);
  897: \  }
  898: \  #endif
  899: \  {
  900: \  #line 516 "./prim"
  901: \  n = n1+n2;
  902: \  }
  903: \  NEXT_P1;
  904: \  _x_sp0 = (Cell)n;
  905: \  NEXT_P2;
  906: \  }
  907: \  NEXT_P1;
  908: \  spTOS = (Cell)_x_sp0;
  909: \  NEXT_P2;
  910: 
  911: : init-combined ( -- )
  912:     prim to combined
  913:     0 num-combined !
  914:     current-depth max-stacks cells erase
  915:     max-depth     max-stacks cells erase
  916:     min-depth     max-stacks cells erase
  917:     prim prim-effect-in  prim prim-effect-in-end  !
  918:     prim prim-effect-out prim prim-effect-out-end ! ;
  919: 
  920: : max! ( n addr -- )
  921:     tuck @ max swap ! ;
  922: 
  923: : min! ( n addr -- )
  924:     tuck @ min swap ! ;
  925: 
  926: : add-depths { p -- }
  927:     \ combine stack effect of p with *-depths
  928:     max-stacks 0 ?do
  929: 	current-depth i th @
  930: 	p prim-stacks-in  i th @ +
  931: 	dup max-depth i th max!
  932: 	p prim-stacks-out i th @ -
  933: 	dup min-depth i th min!
  934: 	current-depth i th !
  935:     loop ;
  936: 
  937: : add-prim ( addr u -- )
  938:     \ add primitive given by "addr u" to combined-prims
  939:     primitives search-wordlist s" unknown primitive" ?print-error
  940:     execute { p }
  941:     p combined-prims num-combined @ th !
  942:     1 num-combined +!
  943:     p add-depths ;
  944: 
  945: : compute-effects { q -- }
  946:     \ compute the stack effects of q from the depths
  947:     max-stacks 0 ?do
  948: 	max-depth i th @ dup
  949: 	q prim-stacks-in i th !
  950: 	current-depth i th @ -
  951: 	q prim-stacks-out i th !
  952:     loop ;
  953: 
  954: : make-effect-items { stack# items effect-endp -- }
  955:     \ effect-endp points to a pointer to the end of the current item-array
  956:     \ and has to be updated
  957:     stacks stack# th @ { stack }
  958:     items 0 +do
  959: 	effect-endp @ { item }
  960: 	i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
  961: 	item item-name 2!
  962: 	stack item item-stack !
  963: 	stack stack-type @ item item-type !
  964: 	i item item-offset !
  965: 	item item-first on
  966: 	item% %size effect-endp +!
  967:     loop ;
  968: 
  969: : init-effects { q -- }
  970:     \ initialize effects field for FETCHES and STORES
  971:     max-stacks 0 ?do
  972: 	i q prim-stacks-in  i th @ q prim-effect-in-end  make-effect-items
  973: 	i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
  974:     loop ;
  975: 
  976: : process-combined ( -- )
  977:     combined combined-prims num-combined @ cells
  978:     combinations ['] constant insert-wordlist
  979:     combined-prims num-combined @ 1- th ( last-part )
  980:     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
  981:     prim compute-effects
  982:     prim init-effects
  983:     output-combined perform ;
  984: 
  985: \ C output
  986: 
  987: : print-item { n stack -- }
  988:     \ print nth stack item name
  989:     stack stack-type @ type-c-name 2@ type space
  990:     ." _" stack stack-pointer 2@ type n 0 .r ;
  991: 
  992: : print-declarations-combined ( -- )
  993:     max-stacks 0 ?do
  994: 	max-depth i th @ min-depth i th @ - 0 +do
  995: 	    i stacks j th @ print-item ." ;" cr
  996: 	loop
  997:     loop ;
  998: 
  999: : part-fetches ( -- )
 1000:     fetches ;
 1001: 
 1002: : part-output-c-tail ( -- )
 1003:     print-debug-results
 1004:     stores ;
 1005: 
 1006: : output-combined-tail ( -- )
 1007:     part-output-c-tail
 1008:     prim >r combined to prim
 1009:     in-part @ >r in-part off
 1010:     output-c-tail
 1011:     r> in-part ! r> to prim ;
 1012: 
 1013: : output-part ( p -- )
 1014:     to prim
 1015:     ." /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
 1016:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
 1017:     ." {" cr
 1018:     print-declarations
 1019:     part-fetches
 1020:     print-debug-args
 1021:     prim add-depths \ !! right place?
 1022:     ." {" cr
 1023:     ." #line " c-line @ . quote c-filename 2@ type quote cr
 1024:     prim prim-c-code 2@ ['] output-combined-tail type-c-code
 1025:     ." }" cr
 1026:     part-output-c-tail
 1027:     ." }" cr ;
 1028: 
 1029: : output-parts ( -- )
 1030:     prim >r in-part on
 1031:     current-depth max-stacks cells erase
 1032:     num-combined @ 0 +do
 1033: 	combined-prims i th @ output-part
 1034:     loop
 1035:     in-part off
 1036:     r> to prim ;
 1037: 
 1038: : output-c-combined ( -- )
 1039:     print-entry cr
 1040:     \ debugging messages just in parts
 1041:     ." {" cr
 1042:     ." DEF_CA" cr
 1043:     print-declarations-combined
 1044:     ." NEXT_P0;" cr
 1045:     flush-tos
 1046:     fetches
 1047:     \ print-debug-args
 1048:     stack-pointer-updates
 1049:     output-parts
 1050:     output-c-tail
 1051:     ." }" cr
 1052:     cr ;
 1053: 
 1054: : output-forth-combined ( -- )
 1055: ;
 1056: 
 1057: 
 1058: \ peephole optimization rules
 1059: 
 1060: \ in order for this to work as intended, shorter combinations for each
 1061: \ length must be present, and the longer combinations must follow
 1062: \ shorter ones (this restriction may go away in the future).
 1063:   
 1064: : output-peephole ( -- )
 1065:     combined-prims num-combined @ 1- cells combinations search-wordlist
 1066:     s" the prefix for this combination must be defined earlier" ?print-error
 1067:     ." {"
 1068:     execute prim-num @ 5 .r ." ,"
 1069:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
 1070:     combined prim-num @ 5 .r ." }, /* "
 1071:     combined prim-c-name 2@ type ."  */"
 1072:     cr ;
 1073: 
 1074: : output-forth-peephole ( -- )
 1075:     combined-prims num-combined @ 1- cells combinations search-wordlist
 1076:     s" the prefix for this combination must be defined earlier" ?print-error
 1077:     execute prim-num @ 5 .r
 1078:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r
 1079:     combined prim-num @ 5 .r ."  prim, \ "
 1080:     combined prim-c-name 2@ type
 1081:     cr ;
 1082: 
 1083: 
 1084: \ the parser
 1085: 
 1086: eof-char max-member \ the whole character set + EOF
 1087: 
 1088: : getinput ( -- n )
 1089:  rawinput @ endrawinput @ =
 1090:  if
 1091:    eof-char
 1092:  else
 1093:    cookedinput @ c@
 1094:  endif ;
 1095: 
 1096: :noname ( n -- )
 1097:  dup bl > if
 1098:   emit space
 1099:  else
 1100:   .
 1101:  endif ;
 1102: print-token !
 1103: 
 1104: : testchar? ( set -- f )
 1105:  getinput member? ;
 1106: ' testchar? test-vector !
 1107: 
 1108: : checksyncline ( -- )
 1109:     \ when input points to a newline, check if the next line is a
 1110:     \ sync line.  If it is, perform the appropriate actions.
 1111:     rawinput @ >r
 1112:     s" #line " r@ over compare 0<> if
 1113: 	rdrop 1 line +! EXIT
 1114:     endif
 1115:     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
 1116:     dup c@ bl = if
 1117: 	char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
 1118: 	char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
 1119: 	char+
 1120:     endif
 1121:     dup c@ nl-char <> 0= s" sync line syntax" ?print-error
 1122:     skipsynclines @ if
 1123: 	dup char+ rawinput !
 1124: 	rawinput @ c@ cookedinput @ c!
 1125:     endif
 1126:     drop ;
 1127: 
 1128: : ?nextchar ( f -- )
 1129:     s" syntax error, wrong char" ?print-error
 1130:     rawinput @ endrawinput @ <> if
 1131: 	rawinput @ c@
 1132: 	1 chars rawinput +!
 1133: 	1 chars cookedinput +!
 1134: 	nl-char = if
 1135: 	    checksyncline
 1136: 	    rawinput @ line-start !
 1137: 	endif
 1138: 	rawinput @ c@ cookedinput @ c!
 1139:     endif ;
 1140: 
 1141: : charclass ( set "name" -- )
 1142:  ['] ?nextchar terminal ;
 1143: 
 1144: : .. ( c1 c2 -- set )
 1145:  ( creates a set that includes the characters c, c1<=c<=c2 )
 1146:  empty copy-set
 1147:  swap 1+ rot do
 1148:   i over add-member
 1149:  loop ;
 1150: 
 1151: : ` ( -- terminal ) ( use: ` c )
 1152:  ( creates anonymous terminal for the character c )
 1153:  char singleton ['] ?nextchar make-terminal ;
 1154: 
 1155: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
 1156: char 0 char 9 ..					charclass digit
 1157: bl singleton tab-char over add-member			charclass white
 1158: nl-char singleton eof-char over add-member complement	charclass nonl
 1159: nl-char singleton eof-char over add-member
 1160:     char : over add-member complement                   charclass nocolonnl
 1161: bl 1+ maxchar .. char \ singleton complement intersection
 1162:                                                         charclass nowhitebq
 1163: bl 1+ maxchar ..                                        charclass nowhite
 1164: char " singleton eof-char over add-member complement	charclass noquote
 1165: nl-char singleton					charclass nl
 1166: eof-char singleton					charclass eof
 1167: nl-char singleton eof-char over add-member		charclass nleof
 1168: 
 1169: (( letter (( letter || digit )) **
 1170: )) <- c-ident ( -- )
 1171: 
 1172: (( ` # ?? (( letter || digit || ` : )) **
 1173: )) <- stack-ident ( -- )
 1174: 
 1175: (( nowhitebq nowhite ** ))
 1176: <- forth-ident ( -- )
 1177: 
 1178: Variable forth-flag
 1179: Variable c-flag
 1180: 
 1181: (( (( ` e || ` E )) {{ start }} nonl ** 
 1182:    {{ end evaluate }}
 1183: )) <- eval-comment ( ... -- ... )
 1184: 
 1185: (( (( ` f || ` F )) {{ start }} nonl ** 
 1186:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
 1187: )) <- forth-comment ( -- )
 1188: 
 1189: (( (( ` c || ` C )) {{ start }} nonl ** 
 1190:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
 1191: )) <- c-comment ( -- )
 1192: 
 1193: (( ` - nonl ** {{ 
 1194: 	forth-flag @ IF ." [ELSE]" cr THEN
 1195: 	c-flag @ IF ." #else" cr THEN }}
 1196: )) <- else-comment
 1197: 
 1198: (( ` + {{ start }} nonl ** {{ end
 1199: 	dup
 1200: 	IF	c-flag @
 1201: 		IF    ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
 1202: 		THEN
 1203: 		forth-flag @
 1204: 		IF  ." has? " type ."  [IF]"  cr THEN
 1205: 	ELSE	2drop
 1206: 	    c-flag @      IF  ." #endif"  cr THEN
 1207: 	    forth-flag @  IF  ." [THEN]"  cr THEN
 1208: 	THEN }}
 1209: )) <- if-comment
 1210: 
 1211: (( (( ` g || ` G )) {{ start }} nonl **
 1212:    {{ end
 1213:       forth-flag @ IF  ." group " type cr  THEN
 1214:       c-flag @     IF  ." GROUP(" type ." )" cr  THEN }}
 1215: )) <- group-comment
 1216: 
 1217: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
 1218: 
 1219: (( ` \ comment-body nleof )) <- comment ( -- )
 1220: 
 1221: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
 1222: <- stack-items
 1223: 
 1224: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
 1225:    ` - ` - white **
 1226:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
 1227: )) <- stack-effect ( -- )
 1228: 
 1229: (( {{ prim create-prim }}
 1230:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
 1231:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
 1232:       (( {{ start }}  c-ident {{ end prim prim-c-name 2! }} )) ??
 1233:    )) ??  nleof
 1234:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
 1235:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl **  nleof white ** )) ** {{ end prim prim-c-code 2! skipsynclines on }}
 1236:    (( ` :  white ** nleof
 1237:       {{ start }} (( nonl ++  nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
 1238:    )) ?? {{ process-simple }}
 1239:    nleof
 1240: )) <- simple-primitive ( -- )
 1241: 
 1242: (( {{ init-combined }}
 1243:    ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
 1244:    nleof {{ process-combined }}
 1245: )) <- combined-primitive
 1246: 
 1247: (( {{ make-prim to prim 0 to combined
 1248:       line @ name-line ! filename 2@ name-filename 2!
 1249:       function-number @ prim prim-num !
 1250:       start }} forth-ident {{ end 2dup prim prim-name 2! prim prim-c-name 2! }}  white ++
 1251:    (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
 1252:    (( simple-primitive || combined-primitive )) {{ 1 function-number +! }}
 1253: )) <- primitive ( -- )
 1254: 
 1255: (( (( comment || primitive || nl white ** )) ** eof ))
 1256: parser primitives2something
 1257: warnings @ [IF]
 1258: .( parser generated ok ) cr
 1259: [THEN]
 1260: 
 1261: 
 1262: \ run with gforth-0.5.0 (slurp-file is missing)
 1263: [IFUNDEF] slurp-file
 1264: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
 1265:     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
 1266:     r/o bin open-file throw >r
 1267:     r@ file-size throw abort" file too large"
 1268:     dup allocate throw swap
 1269:     2dup r@ read-file throw over <> abort" could not read whole file"
 1270:     r> close-file throw ;
 1271: [THEN]
 1272: 
 1273: : primfilter ( addr u -- )
 1274:     \ process the string at addr u
 1275:     over dup rawinput ! dup line-start ! cookedinput !
 1276:     + endrawinput !
 1277:     checksyncline
 1278:     primitives2something ;    
 1279: 
 1280: : process-file ( addr u xt-simple x-combined -- )
 1281:     output-combined ! output !
 1282:     save-mem 2dup filename 2!
 1283:     slurp-file
 1284:     warnings @ if
 1285: 	." ------------ CUT HERE -------------" cr  endif
 1286:     primfilter ;
 1287: 
 1288: \  : process      ( xt -- )
 1289: \      bl word count rot
 1290: \      process-file ;

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