File:  [gforth] / gforth / prims2x.fs
Revision 1.36: download - view: text, annotated - select for diffs
Fri Dec 11 22:54:27 1998 UTC (25 years, 3 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added further options to shrink a kernel down
Cleaned up conditional primitives (works now for C-generated part, too)
Cleaned up mach files for embedded architectures
Cleaned up options in the kernel

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

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