File:  [gforth] / gforth / prims2x.fs
Revision 1.63: download - view: text, annotated - select for diffs
Tue Jan 9 16:11:36 2001 UTC (23 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
changes for better debugging output
made a ChangeLog for real
NEWLINE for Darwin (should be LF, but if does not define unix)

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

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