File:  [gforth] / gforth / prims2x.fs
Revision 1.44: download - view: text, annotated - select for diffs
Mon May 17 13:13:27 1999 UTC (24 years, 10 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
New calling conventions with wcall.

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

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