File:  [gforth] / gforth / prims2x.fs
Revision 1.81: download - view: text, annotated - select for diffs
Fri Feb 23 10:43:40 2001 UTC (23 years ago) by anton
Branches: MAIN
CVS tags: HEAD
first take on generating combined instructions

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

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