File:  [gforth] / gforth / prims2x.fs
Revision 1.64: download - view: text, annotated - select for diffs
Wed Jan 17 09:35:12 2001 UTC (23 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
some refactoring

    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: 
   24: \ Optimizations:
   25: \ superfluous stores are removed. GCC removes the superfluous loads by itself
   26: \ TOS and FTOS can be kept in register( variable)s.
   27: \ 
   28: \ Problems:
   29: \ The TOS optimization is somewhat hairy. The problems by example:
   30: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
   31: \    The store is not superfluous although the earlier opt. would think so
   32: \    Alternatively:    sp[0]=TOS; w=TOS; sp-=1; TOS=w;
   33: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
   34: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
   35: \ 4) ( -- ): /* but here they are unnecessary */
   36: \ 5) Words that call NEXT themselves have to be done very carefully.
   37: \
   38: \ To do:
   39: \ add the store optimization for doubles
   40: \ regarding problem 1 above: It would be better (for over) to implement
   41: \ 	the alternative
   42: 
   43: warnings off
   44: 
   45: [IFUNDEF] vocabulary	\ we are executed just with kernel image
   46: 			\ load the rest that is needed
   47: 			\ (require fails because this file is needed from a
   48: 			\ different directory with the wordlibraries)
   49: include ./search.fs			
   50: include ./extend.fs
   51: [THEN]
   52: 
   53: [IFUNDEF] environment?
   54: include ./environ.fs
   55: [THEN]
   56: 
   57: : struct% struct ; \ struct is redefined in gray
   58: 
   59: include ./gray.fs
   60: 
   61: 100 constant max-effect \ number of things on one side of a stack effect
   62: 255 constant maxchar
   63: maxchar 1+ constant eof-char
   64: #tab constant tab-char
   65: #lf constant nl-char
   66: 
   67: variable rawinput \ pointer to next character to be scanned
   68: variable endrawinput \ pointer to the end of the input (the char after the last)
   69: variable cookedinput \ pointer to the next char to be parsed
   70: variable line \ line number of char pointed to by input
   71: 1 line !
   72: 2variable filename \ filename of original input file
   73: 0 0 filename 2!
   74: 2variable f-comment
   75: 0 0 f-comment 2!
   76: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
   77: skipsynclines on 
   78: 
   79: : start ( -- addr )
   80:  cookedinput @ ;
   81: 
   82: : end ( addr -- addr u )
   83:  cookedinput @ over - ;
   84: 
   85: : quote ( -- )
   86:     [char] " emit ;
   87: 
   88: variable output \ xt ( -- ) of output word
   89: 
   90: : printprim ( -- )
   91:  output @ execute ;
   92: 
   93: struct%
   94:     cell% 2* field stack-pointer \ stackpointer name
   95:     cell% 2* field stack-cast \ cast string for assignments to stack elements
   96:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
   97:     cell%    field stack-in  \ number of stack items in effect in
   98:     cell%    field stack-out \ number of stack items in effect out
   99: end-struct stack%
  100: 
  101: struct%
  102:  cell% 2* field item-name   \ name, excluding stack prefixes
  103:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
  104:  cell%    field item-type   \ descriptor for the item type
  105:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
  106: end-struct item%
  107: 
  108: struct%
  109:     cell% 2* field type-c-name
  110:     cell%    field type-stack \ default stack
  111:     cell%    field type-size  \ size of type in stack items
  112:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
  113:     cell%    field type-store \ xt of store code generator ( item -- )
  114: end-struct type%
  115: 
  116: : stack-in-index ( in-size item -- in-index )
  117:     item-offset @ - 1- ;
  118: 
  119: : inst-in-index ( in-size item -- in-index )
  120:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
  121: 
  122: : make-stack ( addr-ptr u1 addr-cast u2 "stack-name" -- )
  123:     create stack% %allot >r
  124:     save-mem r@ stack-cast 2!
  125:     save-mem r@ stack-pointer 2! 
  126:     ['] stack-in-index r> stack-in-index-xt ! ;
  127: 
  128: s" sp" save-mem s" (Cell)" make-stack data-stack 
  129: s" fp" save-mem s" "       make-stack fp-stack
  130: s" rp" save-mem s" (Cell)" make-stack return-stack
  131: s" IP" save-mem s" error don't use # on results" make-stack inst-stream
  132: ' inst-in-index inst-stream stack-in-index-xt !
  133: \ !! initialize stack-in and stack-out
  134: 
  135: \ stack items
  136: 
  137: : init-item ( addr u addr1 -- )
  138:     \ initialize item at addr1 with name addr u
  139:     \ !! remove stack prefix
  140:     dup item% %size erase
  141:     item-name 2! ;
  142: 
  143: : map-items { addr end xt -- }
  144:     \ perform xt for all items in array addr...end
  145:     end addr ?do
  146: 	i xt execute
  147:     item% %size +loop ;
  148: 
  149: \ various variables for storing stuff of one primitive
  150: 
  151: 2variable forth-name
  152: 2variable wordset
  153: 2variable c-name
  154: 2variable doc
  155: 2variable c-code
  156: 2variable forth-code
  157: 2variable stack-string
  158: create effect-in  max-effect item% %size * allot
  159: create effect-out max-effect item% %size * allot
  160: variable effect-in-end ( pointer )
  161: variable effect-out-end ( pointer )
  162: variable c-line
  163: 2variable c-filename
  164: variable name-line
  165: 2variable name-filename
  166: 2variable last-name-filename
  167: 
  168: variable primitive-number -10 primitive-number !
  169: Variable function-number 0 function-number !
  170: 
  171: \ for several reasons stack items of a word are stored in a wordlist
  172: \ since neither forget nor marker are implemented yet, we make a new
  173: \ wordlist for every word and store it in the variable items
  174: variable items
  175: 
  176: \ a few more set ops
  177: 
  178: : bit-equivalent ( w1 w2 -- w3 )
  179:  xor invert ;
  180: 
  181: : complement ( set1 -- set2 )
  182:  empty ['] bit-equivalent binary-set-operation ;
  183: 
  184: \ the parser
  185: 
  186: eof-char max-member \ the whole character set + EOF
  187: 
  188: : getinput ( -- n )
  189:  rawinput @ endrawinput @ =
  190:  if
  191:    eof-char
  192:  else
  193:    cookedinput @ c@
  194:  endif ;
  195: 
  196: :noname ( n -- )
  197:  dup bl > if
  198:   emit space
  199:  else
  200:   .
  201:  endif ;
  202: print-token !
  203: 
  204: : testchar? ( set -- f )
  205:  getinput member? ;
  206: ' testchar? test-vector !
  207: 
  208: : checksyncline ( -- )
  209:     \ when input points to a newline, check if the next line is a
  210:     \ sync line.  If it is, perform the appropriate actions.
  211:     rawinput @ >r
  212:     s" #line " r@ over compare 0<> if
  213: 	rdrop 1 line +! EXIT
  214:     endif
  215:     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
  216:     dup c@ bl = if
  217: 	char+ dup c@ [char] " <> abort" sync line syntax"
  218: 	char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
  219: 	char+
  220:     endif
  221:     dup c@ nl-char <> abort" sync line syntax"
  222:     skipsynclines @ if
  223: 	dup char+ rawinput !
  224: 	rawinput @ c@ cookedinput @ c!
  225:     endif
  226:     drop ;
  227: 
  228: : ?nextchar ( f -- )
  229:     ?not? if
  230: 	filename 2@ type ." :" line @ 0 .r ." : syntax error, wrong char:"
  231: 	getinput . cr
  232: 	rawinput @ endrawinput @ over - 100 min type cr
  233: 	abort
  234:     endif
  235:     rawinput @ endrawinput @ <> if
  236: 	rawinput @ c@
  237: 	1 chars rawinput +!
  238: 	1 chars cookedinput +!
  239: 	nl-char = if
  240: 	    checksyncline
  241: 	endif
  242: 	rawinput @ c@ cookedinput @ c!
  243:     endif ;
  244: 
  245: : charclass ( set "name" -- )
  246:  ['] ?nextchar terminal ;
  247: 
  248: : .. ( c1 c2 -- set )
  249:  ( creates a set that includes the characters c, c1<=c<=c2 )
  250:  empty copy-set
  251:  swap 1+ rot do
  252:   i over add-member
  253:  loop ;
  254: 
  255: : ` ( -- terminal ) ( use: ` c )
  256:  ( creates anonymous terminal for the character c )
  257:  char singleton ['] ?nextchar make-terminal ;
  258: 
  259: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
  260: char 0 char 9 ..					charclass digit
  261: bl singleton tab-char over add-member			charclass white
  262: nl-char singleton eof-char over add-member complement	charclass nonl
  263: nl-char singleton eof-char over add-member
  264:     char : over add-member complement                   charclass nocolonnl
  265: bl 1+ maxchar .. char \ singleton complement intersection
  266:                                                         charclass nowhitebq
  267: bl 1+ maxchar ..                                        charclass nowhite
  268: char " singleton eof-char over add-member complement	charclass noquote
  269: nl-char singleton					charclass nl
  270: eof-char singleton					charclass eof
  271: 
  272: 
  273: (( letter (( letter || digit )) **
  274: )) <- c-ident ( -- )
  275: 
  276: (( ` # ?? (( letter || digit || ` : )) **
  277: )) <- stack-ident ( -- )
  278: 
  279: (( nowhitebq nowhite ** ))
  280: <- forth-ident ( -- )
  281: 
  282: Variable forth-flag
  283: Variable c-flag
  284: 
  285: (( (( ` e || ` E )) {{ start }} nonl ** 
  286:    {{ end evaluate }}
  287: )) <- eval-comment ( ... -- ... )
  288: 
  289: (( (( ` f || ` F )) {{ start }} nonl ** 
  290:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
  291: )) <- forth-comment ( -- )
  292: 
  293: (( (( ` c || ` C )) {{ start }} nonl ** 
  294:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
  295: )) <- c-comment ( -- )
  296: 
  297: (( ` - nonl ** {{ 
  298: 	forth-flag @ IF ." [ELSE]" cr THEN
  299: 	c-flag @ IF ." #else" cr THEN }}
  300: )) <- else-comment
  301: 
  302: (( ` + {{ start }} nonl ** {{ end
  303: 	dup
  304: 	IF	c-flag @
  305: 		IF    ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
  306: 		THEN
  307: 		forth-flag @
  308: 		IF  ." has? " type ."  [IF]"  cr THEN
  309: 	ELSE	2drop
  310: 	    c-flag @      IF  ." #endif"  cr THEN
  311: 	    forth-flag @  IF  ." [THEN]"  cr THEN
  312: 	THEN }}
  313: )) <- if-comment
  314: 
  315: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
  316: 
  317: (( ` \ comment-body nl )) <- comment ( -- )
  318: 
  319: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
  320: <- stack-items
  321: 
  322: (( {{ effect-in }}  stack-items {{ effect-in-end ! }}
  323:    ` - ` - white **
  324:    {{ effect-out }} stack-items {{ effect-out-end ! }}
  325: )) <- stack-effect ( -- )
  326: 
  327: (( {{ s" " doc 2! s" " forth-code 2! s" " wordset 2! }}
  328:    (( {{ line @ name-line ! filename 2@ name-filename 2! }}
  329:       {{ start }} forth-ident {{ end 2dup forth-name 2! c-name 2! }}  white ++
  330:       ` ( white ** {{ start }} stack-effect {{ end stack-string 2! }} ` ) white **
  331:         (( {{ start }} forth-ident {{ end wordset 2! }} white **
  332: 	   (( {{ start }}  c-ident {{ end c-name 2! }} )) ??
  333: 	)) ??  nl
  334:    ))
  335:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " white ** nl )) ??
  336:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl **  nl white ** )) ** {{ end c-code 2! skipsynclines on }}
  337:    (( ` :  white ** nl
  338:       {{ start }} (( nonl ++  nl white ** )) ++ {{ end forth-code 2! }}
  339:    )) ?? {{ printprim }}
  340:    (( nl || eof ))
  341: )) <- primitive ( -- )
  342: 
  343: (( (( comment || primitive || nl white ** )) ** eof ))
  344: parser primitives2something
  345: warnings @ [IF]
  346: .( parser generated ok ) cr
  347: [THEN]
  348: 
  349: : primfilter ( file-id xt -- )
  350: \ fileid is for the input file, xt ( -- ) is for the output word
  351:  output !
  352:  here dup rawinput ! cookedinput !
  353:  here unused rot read-file throw
  354:  dup here + endrawinput !
  355:  allot
  356:  align
  357:  checksyncline
  358: \ begin
  359: \     getinput dup eof-char = ?EXIT emit true ?nextchar
  360: \ again ;
  361:  primitives2something ;
  362: 
  363: \ types
  364: 
  365: : stack-access ( n stack -- )
  366:     \ print a stack access at index n of stack
  367:     stack-pointer 2@ type
  368:     dup
  369:     if
  370: 	." [" 0 .r ." ]"
  371:     else
  372: 	drop ." TOS"
  373:     endif ;
  374: 
  375: : item-in-index { item -- n }
  376:     \ n is the index of item (in the in-effect)
  377:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
  378:     item r> stack-in-index-xt @ execute ;
  379: 
  380: : fetch-single ( item -- )
  381:  \ fetch a single stack item from its stack
  382:  >r
  383:  r@ item-name 2@ type
  384:  ."  = (" 
  385:  r@ item-type @ type-c-name 2@ type ." ) "
  386:  r@ item-in-index r@ item-stack @ stack-access
  387:  ." ;" cr
  388:  rdrop ; 
  389: 
  390: : fetch-double ( item -- )
  391:  \ fetch a double stack item from its stack
  392:  >r
  393:  ." FETCH_DCELL("
  394:  r@ item-name 2@ type ." , "
  395:  r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
  396:  ." , "                      -1 under+ ." (Cell)" stack-access
  397:  ." );" cr
  398:  rdrop ;
  399: 
  400: : same-as-in? ( item -- f )
  401:  \ f is true iff the offset and stack of item is the same as on input
  402:  >r
  403:  r@ item-name 2@ items @ search-wordlist 0=
  404:  abort" bug"
  405:  execute @
  406:  dup r@ =
  407:  if \ item first appeared in output
  408:    drop false
  409:  else
  410:    dup  item-stack  @ r@ item-stack  @ = 
  411:    swap item-offset @ r@ item-offset @ = and
  412:  endif
  413:  rdrop ;
  414: 
  415: : item-out-index ( item -- n )
  416:     \ n is the index of item (in the in-effect)
  417:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
  418: 
  419: : really-store-single ( item -- )
  420:  >r
  421:  r@ item-out-index r@ item-stack @ stack-access ."  = "
  422:  r@ item-stack @ stack-cast 2@ type
  423:  r@ item-name 2@ type ." ;"
  424:  rdrop ;
  425: 
  426: : store-single ( item -- )
  427:  >r
  428:  r@ same-as-in?
  429:  if
  430:    r@ item-in-index 0= r@ item-out-index 0= xor
  431:    if
  432:        ." IF_" r@ item-stack @ stack-pointer 2@ type
  433:        ." TOS(" r@ really-store-single ." );" cr
  434:    endif
  435:  else
  436:    r@ really-store-single cr
  437:  endif
  438:  rdrop ;
  439: 
  440: : store-double ( item -- )
  441: \ !! store optimization is not performed, because it is not yet needed
  442:  >r
  443:  ." STORE_DCELL(" r@ item-name 2@ type ." , "
  444:  r@ item-out-index r@ item-stack @ 2dup stack-access
  445:  ." , "                       -1 under+ stack-access
  446:  ." );" cr
  447:  rdrop ;
  448: 
  449: : single ( -- xt1 xt2 n )
  450:     ['] fetch-single ['] store-single 1 ;
  451: 
  452: : double ( -- xt1 xt2 n )
  453:     ['] fetch-double ['] store-double 2 ;
  454: 
  455: : s, ( addr u -- )
  456: \ allocate a string
  457:  here swap dup allot move ;
  458: 
  459: wordlist constant prefixes
  460: 
  461: : declare ( addr "name" -- )
  462: \ remember that there is a stack item at addr called name
  463:  create , ;
  464: 
  465: : !default ( w addr -- )
  466:     dup @ if
  467: 	2drop \ leave nonzero alone
  468:     else
  469: 	!
  470:     endif ;
  471: 
  472: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
  473:     \ describes a type
  474:     \ addr u specifies the C type name
  475:     \ stack effect entries of the type start with prefix
  476:     create type% %allot >r
  477:     addr u save-mem r@ type-c-name 2!
  478:     xt1   r@ type-fetch !
  479:     xt2   r@ type-store !
  480:     n     r@ type-size !
  481:     stack r@ type-stack !
  482:     rdrop ;
  483: 
  484: : type-prefix ( xt1 xt2 n stack "prefix" -- )
  485:     create-type
  486: does> ( item -- )
  487:     \ initialize item
  488:     { item typ }
  489:     typ item item-type !
  490:     typ type-stack @ item item-stack !default
  491:     item item-name 2@ items @ search-wordlist 0= if \ new name
  492: 	item item-name 2@ 2dup nextname item declare
  493: 	typ type-c-name 2@ type space type  ." ;" cr
  494:     else
  495: 	drop
  496:     endif ;
  497: 
  498: : execute-prefix ( item addr1 u1 -- )
  499:     \ execute the word ( item -- ) associated with the longest prefix
  500:     \ of addr1 u1
  501:     0 swap ?do
  502: 	dup i prefixes search-wordlist
  503: 	if \ ok, we have the type ( item addr1 xt )
  504: 	    nip execute
  505: 	    UNLOOP EXIT
  506: 	endif
  507: 	-1 s+loop
  508:     \ we did not find a type, abort
  509:     true abort" unknown prefix" ;
  510: 
  511: : declaration ( item -- )
  512:     dup item-name 2@ execute-prefix ;
  513: 
  514: : declaration-list ( addr1 addr2 -- )
  515:     ['] declaration map-items ;
  516: 
  517: : declarations ( -- )
  518:  wordlist dup items ! set-current
  519:  effect-in effect-in-end @ declaration-list
  520:  effect-out effect-out-end @ declaration-list ;
  521: 
  522: : stack-prefix ( stack "prefix" -- )
  523:     name tuck nextname create ( stack length ) 2,
  524: does> ( item -- )
  525:     2@ { item stack prefix-length }
  526:     item item-name 2@ prefix-length /string item item-name 2!
  527:     stack item item-stack !
  528:     item declaration ;
  529: 
  530: \ offset computation
  531: \ the leftmost (i.e. deepest) item has offset 0
  532: \ the rightmost item has the highest offset
  533: 
  534: : compute-offset { item xt -- }
  535:     \ xt specifies in/out; update stack-in/out and set item-offset
  536:     item item-type @ type-size @
  537:     item item-stack @ xt execute dup @ >r +!
  538:     r> item item-offset ! ;
  539: 
  540: : compute-offset-in ( addr1 addr2 -- )
  541:     ['] stack-in compute-offset ;
  542: 
  543: : compute-offset-out ( addr1 addr2 -- )
  544:     ['] stack-out compute-offset ;
  545: 
  546: : clear-stack { -- }
  547:     dup stack-in off stack-out off ;
  548: 
  549: : compute-offsets ( -- )
  550:     data-stack clear-stack  fp-stack clear-stack return-stack clear-stack
  551:     inst-stream clear-stack
  552:     effect-in  effect-in-end  @ ['] compute-offset-in  map-items
  553:     effect-out effect-out-end @ ['] compute-offset-out map-items
  554:     inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
  555: 
  556: : flush-a-tos { stack -- }
  557:     stack stack-out @ 0<> stack stack-in @ 0= and
  558:     if
  559: 	." IF_" stack stack-pointer 2@ 2dup type ." TOS("
  560: 	2dup type ." [0] = " type ." TOS);" cr
  561:     endif ;
  562: 
  563: : flush-tos ( -- )
  564:     data-stack   flush-a-tos
  565:     fp-stack     flush-a-tos
  566:     return-stack flush-a-tos ;
  567: 
  568: : fill-a-tos { stack -- }
  569:     stack stack-out @ 0= stack stack-in @ 0<> and
  570:     if
  571: 	." IF_" stack stack-pointer 2@ 2dup type ." TOS("
  572: 	2dup type ." TOS = " type ." [0]);" cr
  573:     endif ;
  574: 
  575: : fill-tos ( -- )
  576:     \ !! inst-stream for prefetching?
  577:     fp-stack     fill-a-tos
  578:     data-stack   fill-a-tos
  579:     return-stack fill-a-tos ;
  580: 
  581: : fetch ( addr -- )
  582:  dup item-type @ type-fetch @ execute ;
  583: 
  584: : fetches ( -- )
  585:     effect-in effect-in-end @ ['] fetch map-items ;
  586: 
  587: : stack-pointer-update { stack -- }
  588:     \ stack grow downwards
  589:     stack stack-in @ stack stack-out @ -
  590:     ?dup-if \ this check is not necessary, gcc would do this for us
  591: 	stack stack-pointer 2@ type ."  += " 0 .r ." ;" cr
  592:     endif ;
  593: 
  594: : inst-pointer-update ( -- )
  595:     inst-stream stack-in @ ?dup-if
  596: 	." INC_IP(" 0 .r ." );" cr
  597:     endif ;
  598: 
  599: : stack-pointer-updates ( -- )
  600:     inst-pointer-update
  601:     data-stack   stack-pointer-update
  602:     fp-stack     stack-pointer-update
  603:     return-stack stack-pointer-update ;
  604: 
  605: : store ( item -- )
  606: \ f is true if the item should be stored
  607: \ f is false if the store is probably not necessary
  608:  dup item-type @ type-store @ execute ;
  609: 
  610: : stores ( -- )
  611:     effect-out effect-out-end @ ['] store map-items ;
  612: 
  613: : output-c-tail ( -- )
  614:     \ the final part of the generated C code
  615:     ." NEXT_P1;" cr
  616:     stores
  617:     fill-tos
  618:     ." NEXT_P2;" cr ;
  619: 
  620: : type-c ( c-addr u -- )
  621:     \ like TYPE, but replaces "TAIL;" with tail code
  622:     begin ( c-addr1 u1 )
  623: 	2dup s" TAIL;" search
  624:     while ( c-addr1 u1 c-addr3 u3 )
  625: 	2dup 2>r drop nip over - type
  626: 	output-c-tail
  627: 	2r> 5 /string
  628: 	\ !! resync #line missing
  629:     repeat
  630:     2drop type ;
  631: 
  632: : print-type-prefix ( type -- )
  633:     body> >head .name ;
  634: 
  635: : print-debug-arg { item -- }
  636:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
  637:     ." printarg_" item item-type @ print-type-prefix
  638:     ." (" item item-name 2@ type ." );" cr ;
  639:     
  640: : print-debug-args ( -- )
  641:     ." #ifdef VM_DEBUG" cr
  642:     ." if (vm_debug) {" cr
  643:     effect-in effect-in-end @ ['] print-debug-arg map-items
  644:     ." fputc('\n', vm_out);" cr
  645:     ." }" cr
  646:     ." #endif" cr ;
  647:     
  648: : output-c ( -- ) 
  649:  ." I_" c-name 2@ type ." :	/* " forth-name 2@ type ."  ( " stack-string 2@ type ." ) */" cr
  650:  ." /* " doc 2@ type ."  */" cr
  651:  ." NAME(" quote forth-name 2@ type quote ." )" cr \ debugging
  652:  ." {" cr
  653:  ." DEF_CA" cr
  654:  declarations
  655:  compute-offsets \ for everything else
  656:  ." NEXT_P0;" cr
  657:  flush-tos
  658:  fetches
  659:  print-debug-args
  660:  stack-pointer-updates
  661:  ." {" cr
  662:  ." #line " c-line @ . quote c-filename 2@ type quote cr
  663:  c-code 2@ type-c
  664:  ." }" cr
  665:  output-c-tail
  666:  ." }" cr
  667:  cr
  668: ;
  669: 
  670: : disasm-arg { item -- }
  671:     item item-stack @ inst-stream = if
  672: 	."   fputc(' ', vm_out); "
  673: 	." printarg_" item item-type @ print-type-prefix
  674: 	." ((" item item-type @ type-c-name 2@ type ." )"
  675: 	." ip[" item item-offset @ 1+ 0 .r ." ]);" cr
  676:     endif ;
  677: 
  678: : disasm-args ( -- )
  679:     effect-in effect-in-end @ ['] disasm-arg map-items ;
  680: 
  681: : output-disasm ( -- )
  682:     \ generate code for disassembling VM instructions
  683:     ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
  684:     ."   fputs(" quote forth-name 2@ type quote ." , vm_out);" cr
  685:     ." /* " declarations ." */" cr
  686:     compute-offsets
  687:     disasm-args
  688:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
  689:     ." } else "
  690:     1 function-number +! ;
  691: 
  692: : gen-arg-parm { item -- }
  693:     item item-stack @ inst-stream = if
  694: 	." , " item item-type @ type-c-name 2@ type space
  695: 	item item-name 2@ type
  696:     endif ;
  697: 
  698: : gen-args-parm ( -- )
  699:     effect-in effect-in-end @ ['] gen-arg-parm map-items ;
  700: 
  701: : gen-arg-gen { item -- }
  702:     item item-stack @ inst-stream = if
  703: 	."   genarg_" item item-type @ print-type-prefix
  704:         ." (ctp, " item item-name 2@ type ." );" cr
  705:     endif ;
  706: 
  707: : gen-args-gen ( -- )
  708:     effect-in effect-in-end @ ['] gen-arg-gen map-items ;
  709: 
  710: : output-gen ( -- )
  711:     \ generate C code for generating VM instructions
  712:     ." /* " declarations ." */" cr
  713:     compute-offsets
  714:     ." void gen_" c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
  715:     ." {" cr
  716:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
  717:     gen-args-gen
  718:     ." }" cr
  719:     1 function-number +! ;
  720: 
  721: : stack-used? { stack -- f }
  722:     stack stack-in @ stack stack-out @ or 0<> ;
  723: 
  724: : output-funclabel ( -- )
  725:   1 function-number +!
  726:   ." &I_" c-name 2@ type ." ," cr ;
  727: 
  728: : output-forthname ( -- )
  729:   1 function-number +!
  730:   '" emit forth-name 2@ type '" emit ." ," cr ;
  731: 
  732: : output-c-func ( -- )
  733: \ used for word libraries
  734:     1 function-number +!
  735:     ." Cell * I_" c-name 2@ type ." (Cell *SP, Cell **FP)      /* " forth-name 2@ type
  736:     ."  ( " stack-string 2@ type ."  ) */" cr
  737:     ." /* " doc 2@ type ."  */" cr
  738:     ." NAME(" quote forth-name 2@ type quote ." )" cr
  739:     \ debugging
  740:     ." {" cr
  741:     declarations
  742:     compute-offsets \ for everything else
  743:     inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
  744:     data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
  745:     fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
  746:     return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
  747:     flush-tos
  748:     fetches
  749:     stack-pointer-updates
  750:     fp-stack   stack-used? IF ." *FP=fp;" cr THEN
  751:     ." {" cr
  752:     ." #line " c-line @ . quote c-filename 2@ type quote cr
  753:     c-code 2@ type
  754:     ." }" cr
  755:     stores
  756:     fill-tos
  757:     ." return (sp);" cr
  758:     ." }" cr
  759:     cr ;
  760: 
  761: : output-label ( -- )  
  762:     ." (Label)&&I_" c-name 2@ type ." ," cr
  763:     -1 primitive-number +! ;
  764: 
  765: : output-alias ( -- ) 
  766:     ( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr
  767:     -1 primitive-number +! ;
  768: 
  769: : output-forth ( -- )  
  770:     forth-code @ 0=
  771:     IF    	\ output-alias
  772: 	\ this is bad for ec: an alias is compiled if tho word does not exist!
  773: 	\ JAW
  774:     ELSE  ." : " forth-name 2@ type ."   ( "
  775: 	stack-string 2@ type ." )" cr
  776: 	forth-code 2@ type cr
  777: 	-1 primitive-number +!
  778:     THEN ;
  779: 
  780: : output-tag-file ( -- )
  781:     name-filename 2@ last-name-filename 2@ compare if
  782: 	name-filename 2@ last-name-filename 2!
  783: 	#ff emit cr
  784: 	name-filename 2@ type
  785: 	." ,0" cr
  786:     endif ;
  787: 
  788: : output-tag ( -- )
  789:     output-tag-file
  790:     forth-name 2@ 1+ type
  791:     127 emit
  792:     space forth-name 2@ type space
  793:     1 emit
  794:     name-line @ 0 .r
  795:     ." ,0" cr ;
  796: 
  797: [IFDEF] documentation
  798: : register-doc ( -- )
  799:     get-current documentation set-current
  800:     forth-name 2@ nextname create
  801:     forth-name 2@ 2,
  802:     stack-string 2@ condition-stack-effect 2,
  803:     wordset 2@ 2,
  804:     c-name 2@ condition-pronounciation 2,
  805:     doc 2@ 2,
  806:     set-current ;
  807: [THEN]
  808: 
  809: : process-file ( addr u xt -- )
  810:     >r
  811:     save-mem 2dup filename 2!
  812:     0 function-number !
  813:     r/o open-file abort" cannot open file"
  814:     warnings @ if
  815: 	." ------------ CUT HERE -------------" cr  endif
  816:     r> primfilter ;
  817: 
  818: : process      ( xt -- )
  819:     bl word count rot
  820:     process-file ;

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