Annotation of gforth/prims2x.fs, revision 1.31

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: 
1.31    ! pazsan    356: : is-out-tos? ( item -- f )
        !           357: \ true if item has the same offset as the input TOS
        !           358:  item-d-offset @ 1+ effect-out-size 2@ drop = ;
        !           359: 
1.1       anton     360: : really-store-single ( item -- )
                    361:  >r
                    362:  r@ item-d-offset @ effect-out-size 2@ data-stack-access ."  = (Cell)"
                    363:  r@ item-name 2@ type ." ;"
                    364:  rdrop ;
                    365: 
                    366: : store-single ( item -- )
                    367:  >r
                    368:  r@ d-same-as-in?
                    369:  if
1.31    ! pazsan    370:    r@ is-in-tos? r@ is-out-tos? xor
1.1       anton     371:    if
                    372:      ." IF_TOS(" r@ really-store-single ." );" cr
                    373:    endif
                    374:  else
                    375:    r@ really-store-single cr
                    376:  endif
                    377:  rdrop ;
                    378: 
                    379: : store-double ( item -- )
                    380: \ !! store optimization is not performed, because it is not yet needed
                    381:  >r
1.20      anton     382:  ." STORE_DCELL(" r@ item-name 2@ type ." , "
                    383:  r@ item-d-offset @ dup    effect-out-size 2@ data-stack-access
                    384:  ." , "                        1+ effect-out-size 2@ data-stack-access
                    385:  ." );" cr
1.1       anton     386:  rdrop ;
                    387: 
                    388: : f-same-as-in? ( item -- f )
                    389: \ f is true iff the offset of item is the same as on input
                    390:  >r
                    391:  r@ item-name 2@ items @ search-wordlist 0=
1.8       pazsan    392:  abort" bug"
1.1       anton     393:  execute @
                    394:  dup r@ =
                    395:  if \ item first appeared in output
                    396:    drop false
                    397:  else
                    398:    item-f-offset @ r@ item-f-offset @ =
                    399:  endif
                    400:  rdrop ;
                    401: 
                    402: : is-in-ftos? ( item -- f )
                    403: \ true if item has the same offset as the input TOS
                    404:  item-f-offset @ 1+ effect-in-size 2@ nip = ;
                    405: 
                    406: : really-store-float ( item -- )
                    407:  >r
                    408:  r@ item-f-offset @ effect-out-size 2@ fp-stack-access ."  = "
                    409:  r@ item-name 2@ type ." ;"
                    410:  rdrop ;
                    411: 
                    412: : store-float ( item -- )
                    413:  >r
                    414:  r@ f-same-as-in?
                    415:  if
                    416:    r@ is-in-ftos?
                    417:    if
                    418:      ." IF_FTOS(" r@ really-store-float ." );" cr
                    419:    endif
                    420:  else
                    421:    r@ really-store-float cr
                    422:  endif
                    423:  rdrop ;
                    424:  
1.10      anton     425: : single-type ( -- xt1 xt2 n1 n2 )
1.1       anton     426:  ['] fetch-single ['] store-single 1 0 ;
                    427: 
1.10      anton     428: : double-type ( -- xt1 xt2 n1 n2 )
1.1       anton     429:  ['] fetch-double ['] store-double 2 0 ;
                    430: 
1.10      anton     431: : float-type ( -- xt1 xt2 n1 n2 )
1.1       anton     432:  ['] fetch-float ['] store-float 0 1 ;
                    433: 
                    434: : s, ( addr u -- )
                    435: \ allocate a string
                    436:  here swap dup allot move ;
                    437: 
                    438: : starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- )
                    439: \ describes a type
                    440: \ addr u specifies the C type name
                    441: \ n1 is the size of the type on the data stack
                    442: \ n2 is the size of the type on the FP stack
                    443: \ stack effect entries of the type start with prefix
                    444:  >r >r >r >r
                    445:  dup >r here >r s,
                    446:  create
                    447:  r> r> 2,
                    448:  r> r> r> , r> , swap , , ;
                    449: 
                    450: wordlist constant types
                    451: get-current
                    452: types set-current
                    453: 
                    454: s" Bool"       single-type starts-with f
                    455: s" Char"       single-type starts-with c
                    456: s" Cell"       single-type starts-with n
                    457: s" Cell"       single-type starts-with w
                    458: s" UCell"      single-type starts-with u
                    459: s" DCell"      double-type starts-with d
                    460: s" UDCell"     double-type starts-with ud
                    461: s" Float"      float-type  starts-with r
                    462: s" Cell *"     single-type starts-with a_
                    463: s" Char *"     single-type starts-with c_
                    464: s" Float *"    single-type starts-with f_
                    465: s" DFloat *"   single-type starts-with df_
                    466: s" SFloat *"   single-type starts-with sf_
                    467: s" Xt"         single-type starts-with xt
                    468: s" WID"                single-type starts-with wid
                    469: s" F83Name *"  single-type starts-with f83name
                    470: 
                    471: set-current
                    472: 
                    473: : get-type ( addr1 u1 -- type-descr )
                    474: \ get the type of the name in addr1 u1
                    475: \ type-descr is a pointer to a type-descriptor
                    476:  0 swap ?do
                    477:    dup i types search-wordlist
                    478:    if \ ok, we have the type ( addr1 xt )
                    479:      execute nip
                    480:      UNLOOP EXIT
                    481:    endif
1.9       anton     482:  -1 s+loop
1.1       anton     483:  \ we did not find a type, abort
1.8       pazsan    484:  true abort" unknown type prefix" ;
1.1       anton     485: 
                    486: : declare ( addr "name" -- )
                    487: \ remember that there is a stack item at addr called name
                    488:  create , ;
                    489: 
                    490: : declaration ( item -- )
                    491:  dup item-name 2@ items @ search-wordlist
                    492:  if \ already declared ( item xt )
                    493:    execute @ item-type @ swap item-type !
                    494:  else ( addr )
                    495:    dup item-name 2@ nextname dup declare ( addr )
                    496:    dup >r item-name 2@ 2dup get-type ( addr1 u type-descr )
                    497:    dup r> item-type ! ( addr1 u type-descr )
                    498:    type-c-name 2@ type space type ." ;" cr
                    499:  endif ;
                    500: 
                    501: : declaration-list ( addr1 addr2 -- )
                    502:  swap ?do
                    503:   i declaration
                    504:  item-descr +loop ;
                    505: 
1.8       pazsan    506: : fetch ( addr -- )
                    507:  dup item-type @ type-fetch-handler execute ;
                    508: 
1.1       anton     509: : declarations ( -- )
                    510:  wordlist dup items ! set-current
                    511:  effect-in effect-in-end @ declaration-list
                    512:  effect-out effect-out-end @ declaration-list ;
                    513: 
                    514: \ offset computation
                    515: \ the leftmost (i.e. deepest) item has offset 0
                    516: \ the rightmost item has the highest offset
                    517: 
                    518: : compute-offset ( n1 n2 item -- n3 n4 )
                    519: \ n1, n3 are data-stack-offsets
                    520: \ n2, n4 are the fp-stack-offsets
                    521:  >r
                    522:  swap dup r@ item-d-offset !
                    523:  r@ item-type @ type-d-size +
                    524:  swap dup r@ item-f-offset !
                    525:  r@ item-type @ type-f-size +
                    526:  rdrop ;
                    527: 
                    528: : compute-list ( addr1 addr2 -- n1 n2 )
                    529: \ n1, n2 are the final offsets
                    530:  0 0 2swap swap ?do
                    531:   i compute-offset
                    532:  item-descr +loop ;
                    533: 
                    534: : compute-offsets ( -- )
                    535:  effect-in effect-in-end @ compute-list effect-in-size 2!
                    536:  effect-out effect-out-end @ compute-list effect-out-size 2! ;
                    537: 
                    538: : flush-tos ( -- )
                    539:  effect-in-size 2@ effect-out-size 2@
                    540:  0<> rot 0= and
                    541:  if
1.13      anton     542:    ." IF_FTOS(fp[0] = FTOS);" cr
                    543:  endif
1.1       anton     544:  0<> swap 0= and
                    545:  if
1.13      anton     546:    ." IF_TOS(sp[0] = TOS);" cr
                    547:  endif ;
1.1       anton     548: 
                    549: : fill-tos ( -- )
                    550:  effect-in-size 2@ effect-out-size 2@
                    551:  0= rot 0<> and
                    552:  if
                    553:    ." IF_FTOS(FTOS = fp[0]);" cr
                    554:  endif
                    555:  0= swap 0<> and
                    556:  if
                    557:    ." IF_TOS(TOS = sp[0]);" cr
                    558:  endif ;
                    559: 
                    560: : fetches ( -- )
                    561:  effect-in-end @ effect-in ?do
                    562:    i fetch
                    563:  item-descr +loop ; 
                    564: 
                    565: : stack-pointer-updates ( -- )
1.8       pazsan    566: \ we need not check if an update is a noop; gcc does this for us
1.1       anton     567:  effect-in-size 2@
                    568:  effect-out-size 2@
                    569:  rot swap - ( d-in d-out f-diff )
                    570:  rot rot - ( f-diff d-diff )
1.2       pazsan    571:  ?dup IF  ." sp += " 0 .r ." ;" cr  THEN
                    572:  ?dup IF  ." fp += " 0 .r ." ;" cr  THEN ;
1.1       anton     573: 
                    574: : store ( item -- )
                    575: \ f is true if the item should be stored
                    576: \ f is false if the store is probably not necessary
                    577:  dup item-type @ type-store-handler execute ;
                    578: 
                    579: : stores ( -- )
                    580:  effect-out-end @ effect-out ?do
                    581:    i store
                    582:  item-descr +loop ; 
                    583: 
1.8       pazsan    584: : .stack-list ( start end -- )
                    585:  swap ?do
                    586:    i item-name 2@ type space
                    587:  item-descr +loop ; 
                    588: 
1.1       anton     589: : output-c ( -- )
1.2       pazsan    590:  ." I_" c-name 2@ type ." :    /* " forth-name 2@ type ."  ( " stack-string 2@ type ."  ) */" cr
1.1       anton     591:  ." /* " doc 2@ type ."  */" cr
1.13      anton     592:  ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
1.1       anton     593:  ." {" cr
                    594:  ." DEF_CA" cr
                    595:  declarations
                    596:  compute-offsets \ for everything else
1.13      anton     597:  ." NEXT_P0;" cr
                    598:  flush-tos
1.1       anton     599:  fetches
1.13      anton     600:  stack-pointer-updates
1.1       anton     601:  ." {" cr
1.17      anton     602:  ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
1.1       anton     603:  c-code 2@ type
                    604:  ." }" cr
                    605:  ." NEXT_P1;" cr
                    606:  stores
                    607:  fill-tos
1.9       anton     608:  ." NEXT_P2;" cr
1.1       anton     609:  ." }" cr
                    610:  cr
                    611: ;
                    612: 
1.30      pazsan    613: : output-funclabel ( -- )
                    614:   1 function-number +!
                    615:   ." &I_" c-name 2@ type ." ," cr ;
                    616: 
                    617: : output-forthname ( -- )
                    618:   1 function-number +!
                    619:   '" emit forth-name 2@ type '" emit ." ," cr ;
                    620: 
                    621: : output-c-func ( -- )
                    622:     1 function-number +!
                    623:     ." void I_" c-name 2@ type ." ()      /* " forth-name 2@ type
                    624:     ."  ( " stack-string 2@ type ."  ) */" cr
                    625:     ." /* " doc 2@ type ."  */" cr
                    626:     ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr
                    627:     \ debugging
                    628:     ." {" cr
                    629:     ." DEF_CA" cr
                    630:     declarations
                    631:     compute-offsets \ for everything else
                    632:     ." NEXT_P0;" cr
                    633:     flush-tos
                    634:     fetches
                    635:     stack-pointer-updates
                    636:     ." {" cr
                    637:     ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
                    638:     c-code 2@ type
                    639:     ." }" cr
                    640:     ." NEXT_P1;" cr
                    641:     stores
                    642:     fill-tos
                    643:     ." NEXT_P2;" cr
                    644:     ." }" cr
                    645:     cr ;
                    646: 
1.1       anton     647: : output-label ( -- )
1.30      pazsan    648:     ." &&I_" c-name 2@ type ." ," cr ;
1.1       anton     649: 
1.26      pazsan    650: : output-alias ( -- )  flush-comment on
                    651:  ?flush-comment
1.1       anton     652:  primitive-number @ . ." alias " forth-name 2@ type cr
                    653:  -1 primitive-number +! ;
                    654: 
1.26      pazsan    655: : output-forth ( -- )  flush-comment on
1.30      pazsan    656:     ?flush-comment
                    657:     forth-code @ 0=
                    658:     IF         \ output-alias
1.28      jwilke    659:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                    660:        \ JAW
1.30      pazsan    661:     ELSE  ." : " forth-name 2@ type ."   ( "
                    662:        effect-in effect-in-end @ .stack-list ." -- "
                    663:        effect-out effect-out-end @ .stack-list ." )" cr
                    664:        forth-code 2@ type cr
                    665:        -1 primitive-number +!
                    666:     THEN ;
1.10      anton     667: 
1.17      anton     668: : output-tag-file ( -- )
                    669:     name-filename 2@ last-name-filename 2@ compare if
                    670:        name-filename 2@ last-name-filename 2!
                    671:        #ff emit cr
                    672:        name-filename 2@ type
                    673:        ." ,0" cr
                    674:     endif ;
                    675: 
                    676: : output-tag ( -- )
                    677:     output-tag-file
                    678:     forth-name 2@ 1+ type
                    679:     127 emit
                    680:     space forth-name 2@ type space
                    681:     1 emit
                    682:     name-line @ 0 .r
                    683:     ." ,0" cr ;
                    684: 
1.10      anton     685: [IFDEF] documentation
                    686: : register-doc ( -- )
                    687:     get-current documentation set-current
                    688:     forth-name 2@ nextname create
                    689:     forth-name 2@ 2,
1.15      anton     690:     stack-string 2@ condition-stack-effect 2,
1.10      anton     691:     wordset 2@ 2,
1.15      anton     692:     c-name 2@ condition-pronounciation 2,
1.10      anton     693:     doc 2@ 2,
                    694:     set-current ;
                    695: [THEN]
1.8       pazsan    696: 
1.1       anton     697: : process-file ( addr u xt -- )
1.17      anton     698:     >r
                    699:     2dup filename 2!
1.30      pazsan    700:     0 function-number !
1.17      anton     701:     r/o open-file abort" cannot open file"
                    702:     warnings @ if
                    703:        ." ------------ CUT HERE -------------" cr  endif
                    704:     r> primfilter ;
1.30      pazsan    705: 
                    706: : process      ( xt -- )
                    707:     bl word count rot
                    708:     process-file ;
1.5       pazsan    709: 

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