File:  [gforth] / gforth / prims2x.fs
Revision 1.46: download - view: text, annotated - select for diffs
Mon Aug 14 19:15:53 2000 UTC (23 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added conditions to the new primitives (floating)
Fixed prims2x.fs to accept comments after the last primitive
Fixed newline Forth definition
Small docs fixes

    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 tab-char over add-member			charclass white
  218: nl-char singleton eof-char over add-member complement	charclass nonl
  219: nl-char singleton eof-char over add-member
  220:     char : over add-member complement                   charclass nocolonnl
  221: bl 1+ maxchar .. char \ singleton complement intersection
  222:                                                         charclass nowhitebq
  223: bl 1+ maxchar ..                                        charclass nowhite
  224: char " singleton eof-char over add-member complement	charclass noquote
  225: nl-char singleton					charclass nl
  226: eof-char singleton					charclass eof
  227: 
  228: 
  229: (( letter (( letter || digit )) **
  230: )) <- c-name ( -- )
  231: 
  232: (( nowhitebq nowhite ** ))
  233: <- name ( -- )
  234: 
  235: Variable forth-flag
  236: Variable c-flag
  237: 
  238: (( (( ` f || ` F )) {{ start }} nonl ** 
  239:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
  240: )) <- forth-comment ( -- )
  241: 
  242: (( (( ` c || ` C )) {{ start }} nonl ** 
  243:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
  244: )) <- c-comment ( -- )
  245: 
  246: (( ` - nonl ** {{ 
  247: 	forth-flag @ IF ." [ELSE]" cr THEN
  248: 	c-flag @ IF ." #else" cr THEN }}
  249: )) <- else-comment
  250: 
  251: (( ` + {{ start }} nonl ** {{ end
  252: 	dup
  253: 	IF	c-flag @
  254: 		IF    ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
  255: 		THEN
  256: 		forth-flag @
  257: 		IF  ." has? " type ."  [IF]"  cr THEN
  258: 	ELSE	2drop
  259: 	    c-flag @      IF  ." #endif"  cr THEN
  260: 	    forth-flag @  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 + }} white ** )) ** {{ effect-in-end ! }}
  269:    ` - ` - white **
  270:    {{ effect-out }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} white ** )) ** {{ effect-out-end ! }}
  271: )) <- stack-effect ( -- )
  272: 
  273: (( {{ s" " doc 2! s" " forth-code 2! }}
  274:    (( {{ line @ name-line ! filename 2@ name-filename 2! }}
  275:       {{ start }} name {{ end 2dup forth-name 2! c-name 2! }}  white ++
  276:       ` ( white ** {{ start }} stack-effect {{ end stack-string 2! }} ` ) white **
  277:         {{ start }} name {{ end wordset 2! }} white **
  278:         (( {{ start }}  c-name {{ end c-name 2! }} )) ??  nl
  279:    ))
  280:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " nl )) ??
  281:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl **  nl )) ** {{ end c-code 2! skipsynclines on }}
  282:    (( ` :  nl
  283:       {{ start }} (( nonl ++  nl )) ++ {{ end forth-code 2! }}
  284:    )) ?? {{ printprim }}
  285:    (( nl || eof ))
  286: )) <- primitive ( -- )
  287: 
  288: (( (( comment || primitive || nl )) ** eof ))
  289: parser primitives2something
  290: warnings @ [IF]
  291: .( parser generated ok ) cr
  292: [THEN]
  293: 
  294: : primfilter ( file-id xt -- )
  295: \ fileid is for the input file, xt ( -- ) is for the output word
  296:  output !
  297:  here dup rawinput ! cookedinput !
  298:  here unused rot read-file throw
  299:  dup here + endrawinput !
  300:  allot
  301:  align
  302:  checksyncline
  303: \ begin
  304: \     getinput dup eof-char = ?EXIT emit true ?nextchar
  305: \ again ;
  306:  primitives2something ;
  307: 
  308: \ types
  309: 
  310: struct
  311:  2 cells field type-c-name
  312:  cell const-field type-d-size
  313:  cell const-field type-f-size
  314:  cell const-field type-fetch-handler
  315:  cell const-field type-store-handler
  316: constant type-description
  317: 
  318: : data-stack-access ( n1 n2 n3 -- )
  319: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
  320:  drop swap - 1- dup
  321:  if
  322:    ." sp[" 0 .r ." ]"
  323:  else
  324:    drop ." TOS"
  325:  endif ;
  326: 
  327: : fp-stack-access ( n1 n2 n3 -- )
  328: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
  329:  nip swap - 1- dup
  330:  if
  331:    ." fp[" 0 .r ." ]"
  332:  else
  333:    drop ." FTOS"
  334:  endif ;
  335: 
  336: : fetch-single ( item -- )
  337:  >r
  338:  r@ item-name 2@ type
  339:  ."  = (" 
  340:  r@ item-type @ type-c-name 2@ type ." ) "
  341:  r@ item-d-offset @ effect-in-size 2@ data-stack-access ." ;" cr
  342:  rdrop ; 
  343: 
  344: : fetch-double ( item -- )
  345:  >r
  346:  ." FETCH_DCELL("
  347:  r@ item-name 2@ type ." , "
  348:  r@ item-d-offset @ dup    effect-in-size 2@ data-stack-access
  349:  ." , "			1+ effect-in-size 2@ data-stack-access
  350:  ." );" cr
  351:  rdrop ;
  352: 
  353: : fetch-float ( item -- )
  354:  >r
  355:  r@ item-name 2@ type
  356:  ."  = "
  357:  \ ." (" r@ item-type @ type-c-name 2@ type ." ) "
  358:  r@ item-f-offset @ effect-in-size 2@ fp-stack-access ." ;" cr
  359:  rdrop ;
  360: 
  361: : d-same-as-in? ( item -- f )
  362: \ f is true iff the offset of item is the same as on input
  363:  >r
  364:  r@ item-name 2@ items @ search-wordlist 0=
  365:  abort" bug"
  366:  execute @
  367:  dup r@ =
  368:  if \ item first appeared in output
  369:    drop false
  370:  else
  371:    item-d-offset @ r@ item-d-offset @ =
  372:  endif
  373:  rdrop ;
  374: 
  375: : is-in-tos? ( item -- f )
  376: \ true if item has the same offset as the input TOS
  377:  item-d-offset @ 1+ effect-in-size 2@ drop = ;
  378: 
  379: : is-out-tos? ( item -- f )
  380: \ true if item has the same offset as the input TOS
  381:  item-d-offset @ 1+ effect-out-size 2@ drop = ;
  382: 
  383: : really-store-single ( item -- )
  384:  >r
  385:  r@ item-d-offset @ effect-out-size 2@ data-stack-access ."  = (Cell)"
  386:  r@ item-name 2@ type ." ;"
  387:  rdrop ;
  388: 
  389: : store-single ( item -- )
  390:  >r
  391:  r@ d-same-as-in?
  392:  if
  393:    r@ is-in-tos? r@ is-out-tos? xor
  394:    if
  395:      ." IF_TOS(" r@ really-store-single ." );" cr
  396:    endif
  397:  else
  398:    r@ really-store-single cr
  399:  endif
  400:  rdrop ;
  401: 
  402: : store-double ( item -- )
  403: \ !! store optimization is not performed, because it is not yet needed
  404:  >r
  405:  ." STORE_DCELL(" r@ item-name 2@ type ." , "
  406:  r@ item-d-offset @ dup    effect-out-size 2@ data-stack-access
  407:  ." , "			1+ effect-out-size 2@ data-stack-access
  408:  ." );" cr
  409:  rdrop ;
  410: 
  411: : f-same-as-in? ( item -- f )
  412: \ f is true iff the offset of item is the same as on input
  413:  >r
  414:  r@ item-name 2@ items @ search-wordlist 0=
  415:  abort" bug"
  416:  execute @
  417:  dup r@ =
  418:  if \ item first appeared in output
  419:    drop false
  420:  else
  421:    item-f-offset @ r@ item-f-offset @ =
  422:  endif
  423:  rdrop ;
  424: 
  425: : is-in-ftos? ( item -- f )
  426: \ true if item has the same offset as the input TOS
  427:  item-f-offset @ 1+ effect-in-size 2@ nip = ;
  428: 
  429: : really-store-float ( item -- )
  430:  >r
  431:  r@ item-f-offset @ effect-out-size 2@ fp-stack-access ."  = "
  432:  r@ item-name 2@ type ." ;"
  433:  rdrop ;
  434: 
  435: : store-float ( item -- )
  436:  >r
  437:  r@ f-same-as-in?
  438:  if
  439:    r@ is-in-ftos?
  440:    if
  441:      ." IF_FTOS(" r@ really-store-float ." );" cr
  442:    endif
  443:  else
  444:    r@ really-store-float cr
  445:  endif
  446:  rdrop ;
  447:  
  448: : single-type ( -- xt1 xt2 n1 n2 )
  449:  ['] fetch-single ['] store-single 1 0 ;
  450: 
  451: : double-type ( -- xt1 xt2 n1 n2 )
  452:  ['] fetch-double ['] store-double 2 0 ;
  453: 
  454: : float-type ( -- xt1 xt2 n1 n2 )
  455:  ['] fetch-float ['] store-float 0 1 ;
  456: 
  457: : s, ( addr u -- )
  458: \ allocate a string
  459:  here swap dup allot move ;
  460: 
  461: : starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- )
  462: \ describes a type
  463: \ addr u specifies the C type name
  464: \ n1 is the size of the type on the data stack
  465: \ n2 is the size of the type on the FP stack
  466: \ stack effect entries of the type start with prefix
  467:  >r >r >r >r
  468:  dup >r here >r s,
  469:  create
  470:  r> r> 2,
  471:  r> r> r> , r> , swap , , ;
  472: 
  473: wordlist constant types
  474: get-current
  475: types set-current
  476: 
  477: s" Bool"	single-type starts-with f
  478: s" Char"	single-type starts-with c
  479: s" Cell"	single-type starts-with n
  480: s" Cell"	single-type starts-with w
  481: s" UCell"	single-type starts-with u
  482: s" DCell"	double-type starts-with d
  483: s" UDCell"	double-type starts-with ud
  484: s" Float"	float-type  starts-with r
  485: s" Cell *"	single-type starts-with a_
  486: s" Char *"	single-type starts-with c_
  487: s" Float *"	single-type starts-with f_
  488: s" DFloat *"	single-type starts-with df_
  489: s" SFloat *"	single-type starts-with sf_
  490: s" Xt"		single-type starts-with xt
  491: s" WID"		single-type starts-with wid
  492: s" struct F83Name *"	single-type starts-with f83name
  493: 
  494: set-current
  495: 
  496: : get-type ( addr1 u1 -- type-descr )
  497: \ get the type of the name in addr1 u1
  498: \ type-descr is a pointer to a type-descriptor
  499:  0 swap ?do
  500:    dup i types search-wordlist
  501:    if \ ok, we have the type ( addr1 xt )
  502:      execute nip
  503:      UNLOOP EXIT
  504:    endif
  505:  -1 s+loop
  506:  \ we did not find a type, abort
  507:  true abort" unknown type prefix" ;
  508: 
  509: : declare ( addr "name" -- )
  510: \ remember that there is a stack item at addr called name
  511:  create , ;
  512: 
  513: : declaration ( item -- )
  514:  dup item-name 2@ items @ search-wordlist
  515:  if \ already declared ( item xt )
  516:    execute @ item-type @ swap item-type !
  517:  else ( addr )
  518:    dup item-name 2@ nextname dup declare ( addr )
  519:    dup >r item-name 2@ 2dup get-type ( addr1 u type-descr )
  520:    dup r> item-type ! ( addr1 u type-descr )
  521:    type-c-name 2@ type space type ." ;" cr
  522:  endif ;
  523: 
  524: : declaration-list ( addr1 addr2 -- )
  525:  swap ?do
  526:   i declaration
  527:  item-descr +loop ;
  528: 
  529: : fetch ( addr -- )
  530:  dup item-type @ type-fetch-handler execute ;
  531: 
  532: : declarations ( -- )
  533:  wordlist dup items ! set-current
  534:  effect-in effect-in-end @ declaration-list
  535:  effect-out effect-out-end @ declaration-list ;
  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 ( n1 n2 item -- n3 n4 )
  542: \ n1, n3 are data-stack-offsets
  543: \ n2, n4 are the fp-stack-offsets
  544:  >r
  545:  swap dup r@ item-d-offset !
  546:  r@ item-type @ type-d-size +
  547:  swap dup r@ item-f-offset !
  548:  r@ item-type @ type-f-size +
  549:  rdrop ;
  550: 
  551: : compute-list ( addr1 addr2 -- n1 n2 )
  552: \ n1, n2 are the final offsets
  553:  0 0 2swap swap ?do
  554:   i compute-offset
  555:  item-descr +loop ;
  556: 
  557: : compute-offsets ( -- )
  558:  effect-in effect-in-end @ compute-list effect-in-size 2!
  559:  effect-out effect-out-end @ compute-list effect-out-size 2! ;
  560: 
  561: : flush-tos ( -- )
  562:  effect-in-size 2@ effect-out-size 2@
  563:  0<> rot 0= and
  564:  if
  565:    ." IF_FTOS(fp[0] = FTOS);" cr
  566:  endif
  567:  0<> swap 0= and
  568:  if
  569:    ." IF_TOS(sp[0] = TOS);" cr
  570:  endif ;
  571: 
  572: : fill-tos ( -- )
  573:  effect-in-size 2@ effect-out-size 2@
  574:  0= rot 0<> and
  575:  if
  576:    ." IF_FTOS(FTOS = fp[0]);" cr
  577:  endif
  578:  0= swap 0<> and
  579:  if
  580:    ." IF_TOS(TOS = sp[0]);" cr
  581:  endif ;
  582: 
  583: : fetches ( -- )
  584:  effect-in-end @ effect-in ?do
  585:    i fetch
  586:  item-descr +loop ; 
  587: 
  588: : stack-pointer-updates ( -- )
  589: \ we need not check if an update is a noop; gcc does this for us
  590:  effect-in-size 2@
  591:  effect-out-size 2@
  592:  rot swap - ( d-in d-out f-diff )
  593:  rot rot - ( f-diff d-diff )
  594:  ?dup IF  ." sp += " 0 .r ." ;" cr  THEN
  595:  ?dup IF  ." fp += " 0 .r ." ;" cr  THEN ;
  596: 
  597: : store ( item -- )
  598: \ f is true if the item should be stored
  599: \ f is false if the store is probably not necessary
  600:  dup item-type @ type-store-handler execute ;
  601: 
  602: : stores ( -- )
  603:  effect-out-end @ effect-out ?do
  604:    i store
  605:  item-descr +loop ; 
  606: 
  607: : .stack-list ( start end -- )
  608:  swap ?do
  609:    i item-name 2@ type space
  610:  item-descr +loop ; 
  611: 
  612: : output-c ( -- ) 
  613:  ." I_" c-name 2@ type ." :	/* " forth-name 2@ type ."  ( " stack-string 2@ type ." ) */" cr
  614:  ." /* " doc 2@ type ."  */" cr
  615:  ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
  616:  ." {" cr
  617:  ." DEF_CA" cr
  618:  declarations
  619:  compute-offsets \ for everything else
  620:  ." NEXT_P0;" cr
  621:  flush-tos
  622:  fetches
  623:  stack-pointer-updates
  624:  ." {" cr
  625:  ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
  626:  c-code 2@ type
  627:  ." }" cr
  628:  ." NEXT_P1;" cr
  629:  stores
  630:  fill-tos
  631:  ." NEXT_P2;" cr
  632:  ." }" cr
  633:  cr
  634: ;
  635: 
  636: : dstack-used?
  637:   effect-in-size 2@ drop
  638:   effect-out-size 2@ drop max 0<> ;
  639: 
  640: : fstack-used?
  641:   effect-in-size 2@ nip
  642:   effect-out-size 2@ nip max 0<> ;
  643: 
  644: : output-funclabel ( -- )
  645:   1 function-number +!
  646:   ." &I_" c-name 2@ type ." ," cr ;
  647: 
  648: : output-forthname ( -- )
  649:   1 function-number +!
  650:   '" emit forth-name 2@ type '" emit ." ," cr ;
  651: 
  652: : output-c-func ( -- )
  653: \ used for word libraries
  654:     1 function-number +!
  655:     ." Cell * I_" c-name 2@ type ." (Cell *SP, Cell **FP)      /* " forth-name 2@ type
  656:     ."  ( " stack-string 2@ type ."  ) */" cr
  657:     ." /* " doc 2@ type ."  */" cr
  658:     ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr
  659:     \ debugging
  660:     ." {" cr
  661:     declarations
  662:     compute-offsets \ for everything else
  663:     dstack-used? IF ." Cell *sp=SP;" cr THEN
  664:     fstack-used? IF ." Cell *fp=*FP;" cr THEN
  665:     flush-tos
  666:     fetches
  667:     stack-pointer-updates
  668:     fstack-used? IF ." *FP=fp;" cr THEN
  669:     ." {" cr
  670:     ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
  671:     c-code 2@ type
  672:     ." }" cr
  673:     stores
  674:     fill-tos
  675:     ." return (sp);" cr
  676:     ." }" cr
  677:     cr ;
  678: 
  679: : output-label ( -- )  
  680:     ." (Label)&&I_" c-name 2@ type ." ," cr
  681:     -1 primitive-number +! ;
  682: 
  683: : output-alias ( -- ) 
  684:     ( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr
  685:     -1 primitive-number +! ;
  686: 
  687: : output-forth ( -- )  
  688:     forth-code @ 0=
  689:     IF    	\ output-alias
  690: 	\ this is bad for ec: an alias is compiled if tho word does not exist!
  691: 	\ JAW
  692:     ELSE  ." : " forth-name 2@ type ."   ( "
  693: 	effect-in effect-in-end @ .stack-list ." -- "
  694: 	effect-out effect-out-end @ .stack-list ." )" cr
  695: 	forth-code 2@ type cr
  696: 	-1 primitive-number +!
  697:     THEN ;
  698: 
  699: : output-tag-file ( -- )
  700:     name-filename 2@ last-name-filename 2@ compare if
  701: 	name-filename 2@ last-name-filename 2!
  702: 	#ff emit cr
  703: 	name-filename 2@ type
  704: 	." ,0" cr
  705:     endif ;
  706: 
  707: : output-tag ( -- )
  708:     output-tag-file
  709:     forth-name 2@ 1+ type
  710:     127 emit
  711:     space forth-name 2@ type space
  712:     1 emit
  713:     name-line @ 0 .r
  714:     ." ,0" cr ;
  715: 
  716: [IFDEF] documentation
  717: : register-doc ( -- )
  718:     get-current documentation set-current
  719:     forth-name 2@ nextname create
  720:     forth-name 2@ 2,
  721:     stack-string 2@ condition-stack-effect 2,
  722:     wordset 2@ 2,
  723:     c-name 2@ condition-pronounciation 2,
  724:     doc 2@ 2,
  725:     set-current ;
  726: [THEN]
  727: 
  728: : process-file ( addr u xt -- )
  729:     >r
  730:     2dup filename 2!
  731:     0 function-number !
  732:     r/o open-file abort" cannot open file"
  733:     warnings @ if
  734: 	." ------------ CUT HERE -------------" cr  endif
  735:     r> primfilter ;
  736: 
  737: : process      ( xt -- )
  738:     bl word count rot
  739:     process-file ;

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