File:  [gforth] / gforth / prims2x.fs
Revision 1.53: download - view: text, annotated - select for diffs
Wed Dec 13 09:51:28 2000 UTC (18 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added support for # prefix in prims2x.fs (not complete)

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

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