Annotation of gforth/prims2x.fs, revision 1.30

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

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