File:  [gforth] / gforth / prims2x.fs
Revision 1.52: download - view: text, annotated - select for diffs
Tue Nov 14 10:36:02 2000 UTC (23 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
prims2x now replaces "TAIL;" in the C code with appropriate code for
   terminating the primitive
Most conditional branches now use "TAIL;" to have two NEXTs
   This brings performance back to the level before Nov 12th 2000

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

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