File:  [gforth] / gforth / prims2x.fs
Revision 1.65: download - view: text, annotated - select for diffs
Thu Jan 18 14:57:37 2001 UTC (23 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Better error reporting in prims2x.fs
Bug fixed by changing the docs: RECOVER is not optional in TRY...ENDTRY.

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

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