Annotation of gforth/prims2x.fs, revision 1.42

1.16      anton       1: \ converts primitives to, e.g., C code 
                      2: 
1.35      anton       3: \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
1.16      anton       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.39      jwilke     45: [IFUNDEF] vocabulary   \ we are executed just with kernel image
                     46:                        \ load the rest that is needed
                     47:                        \ (require fails because this file is needed from a
                     48:                        \ different directory with the wordlibraries)
                     49: include ./search.fs                    
                     50: include ./extend.fs
1.40      anton      51: [THEN]
                     52: 
                     53: [IFUNDEF] environment?
1.39      jwilke     54: include ./environ.fs
                     55: [THEN]
1.25      pazsan     56: 
1.39      jwilke     57: include ./gray.fs
1.1       anton      58: 
                     59: 100 constant max-effect \ number of things on one side of a stack effect
                     60: 255 constant maxchar
                     61: maxchar 1+ constant eof-char
1.17      anton      62: #tab constant tab-char
                     63: #lf constant nl-char
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
1.34      pazsan     80:     flush-comment @ 0= ?EXIT
                     81:     f-comment 2@ nip
                     82:     IF  cr f-comment 2@ 2 /string 1-
                     83:        dup IF
1.36      pazsan     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
1.34      pazsan     94:        ELSE    flush-comment @ 1 = IF  ." #endif"  ELSE  ." [THEN]"  THEN
                     95:            cr  THEN
                     96:        0 0 f-comment 2! THEN ;
1.26      pazsan     97: 
1.1       anton      98: : start ( -- addr )
1.18      anton      99:  cookedinput @ ;
1.1       anton     100: 
                    101: : end ( addr -- addr u )
1.18      anton     102:  cookedinput @ over - ;
1.1       anton     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
1.17      anton     139: variable c-line
                    140: 2variable c-filename
                    141: variable name-line
                    142: 2variable name-filename
                    143: 2variable last-name-filename
1.1       anton     144: 
1.14      pazsan    145: variable primitive-number -10 primitive-number !
1.30      pazsan    146: Variable function-number 0 function-number !
1.1       anton     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 )
1.18      anton     166:  rawinput @ endrawinput @ =
1.1       anton     167:  if
1.18      anton     168:    eof-char
1.1       anton     169:  else
1.18      anton     170:    cookedinput @ c@
1.1       anton     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: 
1.17      anton     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.
1.18      anton     188:     rawinput @ >r
1.17      anton     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"
1.24      anton     195:        char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1.17      anton     196:        char+
                    197:     endif
                    198:     dup c@ nl-char <> abort" sync line syntax"
                    199:     skipsynclines @ if
1.18      anton     200:        dup char+ rawinput !
                    201:        rawinput @ c@ cookedinput @ c!
1.17      anton     202:     endif
                    203:     drop ;
                    204: 
1.1       anton     205: : ?nextchar ( f -- )
1.17      anton     206:     ?not? if
1.18      anton     207:        filename 2@ type ." :" line @ 0 .r ." : syntax error, wrong char:"
1.17      anton     208:        getinput . cr
1.18      anton     209:        rawinput @ endrawinput @ over - 100 min type cr
1.17      anton     210:        abort
                    211:     endif
1.18      anton     212:     rawinput @ endrawinput @ <> if
                    213:        rawinput @ c@
                    214:        1 chars rawinput +!
                    215:        1 chars cookedinput +!
1.17      anton     216:        nl-char = if
                    217:            checksyncline
                    218:        endif
1.18      anton     219:        rawinput @ c@ cookedinput @ c!
1.17      anton     220:     endif ;
1.1       anton     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 )
1.21      anton     234:  char singleton ['] ?nextchar make-terminal ;
1.1       anton     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: 
1.42    ! jwilke    254: Variable forth-flag
        !           255: Variable c-flag
        !           256: 
        !           257: (( (( ` f || ` F )) {{ start }} nonl ** 
        !           258:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
        !           259: )) <- forth-comment ( -- )
        !           260: 
        !           261: (( (( ` c || ` C )) {{ start }} nonl ** 
        !           262:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
        !           263: )) <- c-comment ( -- )
        !           264: 
        !           265: (( (( forth-comment || c-comment )) ?? nonl ** )) <- comment-body
        !           266: 
        !           267: (( {{ ?flush-comment start }} ` \ comment-body nl {{ end
1.25      pazsan    268:       2dup 2 min s" \+" compare 0= IF  f-comment 2!  ELSE  2drop  THEN }}
1.1       anton     269: )) <- comment ( -- )
                    270: 
                    271: (( {{ effect-in }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-in-end ! }}
                    272:    ` - ` - blank **
                    273:    {{ effect-out }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-out-end ! }}
                    274: )) <- stack-effect ( -- )
                    275: 
                    276: (( {{ s" " doc 2! s" " forth-code 2! }}
                    277:    (( comment || nl )) **
1.17      anton     278:    (( {{ line @ name-line ! filename 2@ name-filename 2! }}
                    279:       {{ start }} name {{ end 2dup forth-name 2! c-name 2! }}  tab ++
1.1       anton     280:       {{ start }} stack-effect {{ end stack-string 2! }} tab ++
                    281:         {{ start }} name {{ end wordset 2! }} tab **
                    282:         (( {{ start }}  c-name {{ end c-name 2! }} )) ??  nl
                    283:    ))
                    284:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " nl )) ??
1.18      anton     285:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl **  nl )) ** {{ end c-code 2! skipsynclines on }}
1.1       anton     286:    (( ` :  nl
                    287:       {{ start }} (( nonl ++  nl )) ++ {{ end forth-code 2! }}
                    288:    )) ??
                    289:    (( nl || eof ))
                    290: )) <- primitive ( -- )
                    291: 
1.36      pazsan    292: (( (( primitive {{ printprim }} )) ** eof ))
1.1       anton     293: parser primitives2something
1.3       pazsan    294: warnings @ [IF]
1.1       anton     295: .( parser generated ok ) cr
1.3       pazsan    296: [THEN]
1.1       anton     297: 
                    298: : primfilter ( file-id xt -- )
                    299: \ fileid is for the input file, xt ( -- ) is for the output word
                    300:  output !
1.18      anton     301:  here dup rawinput ! cookedinput !
1.41      anton     302:  here unused rot read-file throw
                    303:  dup here + endrawinput !
                    304:  allot
1.2       pazsan    305:  align
1.17      anton     306:  checksyncline
1.18      anton     307: \ begin
                    308: \     getinput dup eof-char = ?EXIT emit true ?nextchar
                    309: \ again ;
1.1       anton     310:  primitives2something ;
                    311: 
                    312: \ types
                    313: 
                    314: struct
                    315:  2 cells field type-c-name
                    316:  cell const-field type-d-size
                    317:  cell const-field type-f-size
                    318:  cell const-field type-fetch-handler
                    319:  cell const-field type-store-handler
                    320: constant type-description
                    321: 
                    322: : data-stack-access ( n1 n2 n3 -- )
                    323: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
                    324:  drop swap - 1- dup
                    325:  if
1.2       pazsan    326:    ." sp[" 0 .r ." ]"
1.1       anton     327:  else
                    328:    drop ." TOS"
                    329:  endif ;
                    330: 
                    331: : fp-stack-access ( n1 n2 n3 -- )
                    332: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
                    333:  nip swap - 1- dup
                    334:  if
1.2       pazsan    335:    ." fp[" 0 .r ." ]"
1.1       anton     336:  else
                    337:    drop ." FTOS"
                    338:  endif ;
                    339: 
                    340: : fetch-single ( item -- )
                    341:  >r
1.8       pazsan    342:  r@ item-name 2@ type
                    343:  ."  = (" 
1.1       anton     344:  r@ item-type @ type-c-name 2@ type ." ) "
                    345:  r@ item-d-offset @ effect-in-size 2@ data-stack-access ." ;" cr
                    346:  rdrop ; 
                    347: 
                    348: : fetch-double ( item -- )
                    349:  >r
1.20      anton     350:  ." FETCH_DCELL("
                    351:  r@ item-name 2@ type ." , "
1.1       anton     352:  r@ item-d-offset @ dup    effect-in-size 2@ data-stack-access
1.20      anton     353:  ." , "                        1+ effect-in-size 2@ data-stack-access
                    354:  ." );" cr
1.1       anton     355:  rdrop ;
                    356: 
                    357: : fetch-float ( item -- )
                    358:  >r
1.8       pazsan    359:  r@ item-name 2@ type
                    360:  ."  = "
1.1       anton     361:  \ ." (" r@ item-type @ type-c-name 2@ type ." ) "
                    362:  r@ item-f-offset @ effect-in-size 2@ fp-stack-access ." ;" cr
                    363:  rdrop ;
                    364: 
                    365: : d-same-as-in? ( item -- f )
                    366: \ f is true iff the offset of item is the same as on input
                    367:  >r
                    368:  r@ item-name 2@ items @ search-wordlist 0=
1.8       pazsan    369:  abort" bug"
1.1       anton     370:  execute @
                    371:  dup r@ =
                    372:  if \ item first appeared in output
                    373:    drop false
                    374:  else
                    375:    item-d-offset @ r@ item-d-offset @ =
                    376:  endif
                    377:  rdrop ;
                    378: 
                    379: : is-in-tos? ( item -- f )
                    380: \ true if item has the same offset as the input TOS
                    381:  item-d-offset @ 1+ effect-in-size 2@ drop = ;
                    382: 
1.31      pazsan    383: : is-out-tos? ( item -- f )
                    384: \ true if item has the same offset as the input TOS
                    385:  item-d-offset @ 1+ effect-out-size 2@ drop = ;
                    386: 
1.1       anton     387: : really-store-single ( item -- )
                    388:  >r
                    389:  r@ item-d-offset @ effect-out-size 2@ data-stack-access ."  = (Cell)"
                    390:  r@ item-name 2@ type ." ;"
                    391:  rdrop ;
                    392: 
                    393: : store-single ( item -- )
                    394:  >r
                    395:  r@ d-same-as-in?
                    396:  if
1.31      pazsan    397:    r@ is-in-tos? r@ is-out-tos? xor
1.1       anton     398:    if
                    399:      ." IF_TOS(" r@ really-store-single ." );" cr
                    400:    endif
                    401:  else
                    402:    r@ really-store-single cr
                    403:  endif
                    404:  rdrop ;
                    405: 
                    406: : store-double ( item -- )
                    407: \ !! store optimization is not performed, because it is not yet needed
                    408:  >r
1.20      anton     409:  ." STORE_DCELL(" r@ item-name 2@ type ." , "
                    410:  r@ item-d-offset @ dup    effect-out-size 2@ data-stack-access
                    411:  ." , "                        1+ effect-out-size 2@ data-stack-access
                    412:  ." );" cr
1.1       anton     413:  rdrop ;
                    414: 
                    415: : f-same-as-in? ( item -- f )
                    416: \ f is true iff the offset of item is the same as on input
                    417:  >r
                    418:  r@ item-name 2@ items @ search-wordlist 0=
1.8       pazsan    419:  abort" bug"
1.1       anton     420:  execute @
                    421:  dup r@ =
                    422:  if \ item first appeared in output
                    423:    drop false
                    424:  else
                    425:    item-f-offset @ r@ item-f-offset @ =
                    426:  endif
                    427:  rdrop ;
                    428: 
                    429: : is-in-ftos? ( item -- f )
                    430: \ true if item has the same offset as the input TOS
                    431:  item-f-offset @ 1+ effect-in-size 2@ nip = ;
                    432: 
                    433: : really-store-float ( item -- )
                    434:  >r
                    435:  r@ item-f-offset @ effect-out-size 2@ fp-stack-access ."  = "
                    436:  r@ item-name 2@ type ." ;"
                    437:  rdrop ;
                    438: 
                    439: : store-float ( item -- )
                    440:  >r
                    441:  r@ f-same-as-in?
                    442:  if
                    443:    r@ is-in-ftos?
                    444:    if
                    445:      ." IF_FTOS(" r@ really-store-float ." );" cr
                    446:    endif
                    447:  else
                    448:    r@ really-store-float cr
                    449:  endif
                    450:  rdrop ;
                    451:  
1.10      anton     452: : single-type ( -- xt1 xt2 n1 n2 )
1.1       anton     453:  ['] fetch-single ['] store-single 1 0 ;
                    454: 
1.10      anton     455: : double-type ( -- xt1 xt2 n1 n2 )
1.1       anton     456:  ['] fetch-double ['] store-double 2 0 ;
                    457: 
1.10      anton     458: : float-type ( -- xt1 xt2 n1 n2 )
1.1       anton     459:  ['] fetch-float ['] store-float 0 1 ;
                    460: 
                    461: : s, ( addr u -- )
                    462: \ allocate a string
                    463:  here swap dup allot move ;
                    464: 
                    465: : starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- )
                    466: \ describes a type
                    467: \ addr u specifies the C type name
                    468: \ n1 is the size of the type on the data stack
                    469: \ n2 is the size of the type on the FP stack
                    470: \ stack effect entries of the type start with prefix
                    471:  >r >r >r >r
                    472:  dup >r here >r s,
                    473:  create
                    474:  r> r> 2,
                    475:  r> r> r> , r> , swap , , ;
                    476: 
                    477: wordlist constant types
                    478: get-current
                    479: types set-current
                    480: 
                    481: s" Bool"       single-type starts-with f
                    482: s" Char"       single-type starts-with c
                    483: s" Cell"       single-type starts-with n
                    484: s" Cell"       single-type starts-with w
                    485: s" UCell"      single-type starts-with u
                    486: s" DCell"      double-type starts-with d
                    487: s" UDCell"     double-type starts-with ud
                    488: s" Float"      float-type  starts-with r
                    489: s" Cell *"     single-type starts-with a_
                    490: s" Char *"     single-type starts-with c_
                    491: s" Float *"    single-type starts-with f_
                    492: s" DFloat *"   single-type starts-with df_
                    493: s" SFloat *"   single-type starts-with sf_
                    494: s" Xt"         single-type starts-with xt
                    495: s" WID"                single-type starts-with wid
1.33      pazsan    496: s" struct F83Name *"   single-type starts-with f83name
1.1       anton     497: 
                    498: set-current
                    499: 
                    500: : get-type ( addr1 u1 -- type-descr )
                    501: \ get the type of the name in addr1 u1
                    502: \ type-descr is a pointer to a type-descriptor
                    503:  0 swap ?do
                    504:    dup i types search-wordlist
                    505:    if \ ok, we have the type ( addr1 xt )
                    506:      execute nip
                    507:      UNLOOP EXIT
                    508:    endif
1.9       anton     509:  -1 s+loop
1.1       anton     510:  \ we did not find a type, abort
1.8       pazsan    511:  true abort" unknown type prefix" ;
1.1       anton     512: 
                    513: : declare ( addr "name" -- )
                    514: \ remember that there is a stack item at addr called name
                    515:  create , ;
                    516: 
                    517: : declaration ( item -- )
                    518:  dup item-name 2@ items @ search-wordlist
                    519:  if \ already declared ( item xt )
                    520:    execute @ item-type @ swap item-type !
                    521:  else ( addr )
                    522:    dup item-name 2@ nextname dup declare ( addr )
                    523:    dup >r item-name 2@ 2dup get-type ( addr1 u type-descr )
                    524:    dup r> item-type ! ( addr1 u type-descr )
                    525:    type-c-name 2@ type space type ." ;" cr
                    526:  endif ;
                    527: 
                    528: : declaration-list ( addr1 addr2 -- )
                    529:  swap ?do
                    530:   i declaration
                    531:  item-descr +loop ;
                    532: 
1.8       pazsan    533: : fetch ( addr -- )
                    534:  dup item-type @ type-fetch-handler execute ;
                    535: 
1.1       anton     536: : declarations ( -- )
                    537:  wordlist dup items ! set-current
                    538:  effect-in effect-in-end @ declaration-list
                    539:  effect-out effect-out-end @ declaration-list ;
                    540: 
                    541: \ offset computation
                    542: \ the leftmost (i.e. deepest) item has offset 0
                    543: \ the rightmost item has the highest offset
                    544: 
                    545: : compute-offset ( n1 n2 item -- n3 n4 )
                    546: \ n1, n3 are data-stack-offsets
                    547: \ n2, n4 are the fp-stack-offsets
                    548:  >r
                    549:  swap dup r@ item-d-offset !
                    550:  r@ item-type @ type-d-size +
                    551:  swap dup r@ item-f-offset !
                    552:  r@ item-type @ type-f-size +
                    553:  rdrop ;
                    554: 
                    555: : compute-list ( addr1 addr2 -- n1 n2 )
                    556: \ n1, n2 are the final offsets
                    557:  0 0 2swap swap ?do
                    558:   i compute-offset
                    559:  item-descr +loop ;
                    560: 
                    561: : compute-offsets ( -- )
                    562:  effect-in effect-in-end @ compute-list effect-in-size 2!
                    563:  effect-out effect-out-end @ compute-list effect-out-size 2! ;
                    564: 
                    565: : flush-tos ( -- )
                    566:  effect-in-size 2@ effect-out-size 2@
                    567:  0<> rot 0= and
                    568:  if
1.13      anton     569:    ." IF_FTOS(fp[0] = FTOS);" cr
                    570:  endif
1.1       anton     571:  0<> swap 0= and
                    572:  if
1.13      anton     573:    ." IF_TOS(sp[0] = TOS);" cr
                    574:  endif ;
1.1       anton     575: 
                    576: : fill-tos ( -- )
                    577:  effect-in-size 2@ effect-out-size 2@
                    578:  0= rot 0<> and
                    579:  if
                    580:    ." IF_FTOS(FTOS = fp[0]);" cr
                    581:  endif
                    582:  0= swap 0<> and
                    583:  if
                    584:    ." IF_TOS(TOS = sp[0]);" cr
                    585:  endif ;
                    586: 
                    587: : fetches ( -- )
                    588:  effect-in-end @ effect-in ?do
                    589:    i fetch
                    590:  item-descr +loop ; 
                    591: 
                    592: : stack-pointer-updates ( -- )
1.8       pazsan    593: \ we need not check if an update is a noop; gcc does this for us
1.1       anton     594:  effect-in-size 2@
                    595:  effect-out-size 2@
                    596:  rot swap - ( d-in d-out f-diff )
                    597:  rot rot - ( f-diff d-diff )
1.2       pazsan    598:  ?dup IF  ." sp += " 0 .r ." ;" cr  THEN
                    599:  ?dup IF  ." fp += " 0 .r ." ;" cr  THEN ;
1.1       anton     600: 
                    601: : store ( item -- )
                    602: \ f is true if the item should be stored
                    603: \ f is false if the store is probably not necessary
                    604:  dup item-type @ type-store-handler execute ;
                    605: 
                    606: : stores ( -- )
                    607:  effect-out-end @ effect-out ?do
                    608:    i store
                    609:  item-descr +loop ; 
                    610: 
1.8       pazsan    611: : .stack-list ( start end -- )
                    612:  swap ?do
                    613:    i item-name 2@ type space
                    614:  item-descr +loop ; 
                    615: 
1.34      pazsan    616: : output-c ( -- ) 1 flush-comment !
                    617:     ?flush-comment
1.2       pazsan    618:  ." I_" c-name 2@ type ." :    /* " forth-name 2@ type ."  ( " stack-string 2@ type ."  ) */" cr
1.1       anton     619:  ." /* " doc 2@ type ."  */" cr
1.13      anton     620:  ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
1.1       anton     621:  ." {" cr
                    622:  ." DEF_CA" cr
                    623:  declarations
                    624:  compute-offsets \ for everything else
1.13      anton     625:  ." NEXT_P0;" cr
                    626:  flush-tos
1.1       anton     627:  fetches
1.13      anton     628:  stack-pointer-updates
1.1       anton     629:  ." {" cr
1.17      anton     630:  ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
1.1       anton     631:  c-code 2@ type
                    632:  ." }" cr
                    633:  ." NEXT_P1;" cr
                    634:  stores
                    635:  fill-tos
1.9       anton     636:  ." NEXT_P2;" cr
1.1       anton     637:  ." }" cr
                    638:  cr
                    639: ;
                    640: 
1.30      pazsan    641: : output-funclabel ( -- )
                    642:   1 function-number +!
                    643:   ." &I_" c-name 2@ type ." ," cr ;
                    644: 
                    645: : output-forthname ( -- )
                    646:   1 function-number +!
                    647:   '" emit forth-name 2@ type '" emit ." ," cr ;
                    648: 
                    649: : output-c-func ( -- )
                    650:     1 function-number +!
                    651:     ." void I_" c-name 2@ type ." ()      /* " forth-name 2@ type
                    652:     ."  ( " stack-string 2@ type ."  ) */" cr
                    653:     ." /* " doc 2@ type ."  */" cr
                    654:     ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr
                    655:     \ debugging
                    656:     ." {" cr
                    657:     ." DEF_CA" cr
                    658:     declarations
                    659:     compute-offsets \ for everything else
                    660:     ." NEXT_P0;" cr
                    661:     flush-tos
                    662:     fetches
                    663:     stack-pointer-updates
                    664:     ." {" cr
                    665:     ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
                    666:     c-code 2@ type
                    667:     ." }" cr
                    668:     ." NEXT_P1;" cr
                    669:     stores
                    670:     fill-tos
                    671:     ." NEXT_P2;" cr
                    672:     ." }" cr
                    673:     cr ;
                    674: 
1.34      pazsan    675: : output-label ( -- )  1 flush-comment !
                    676:     ?flush-comment
                    677:     ." (Label)&&I_" c-name 2@ type ." ," cr
                    678:     -1 primitive-number +! ;
1.1       anton     679: 
1.26      pazsan    680: : output-alias ( -- )  flush-comment on
1.34      pazsan    681:     ?flush-comment
1.38      pazsan    682:     ( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr
1.34      pazsan    683:     -1 primitive-number +! ;
1.1       anton     684: 
1.26      pazsan    685: : output-forth ( -- )  flush-comment on
1.30      pazsan    686:     ?flush-comment
                    687:     forth-code @ 0=
                    688:     IF         \ output-alias
1.28      jwilke    689:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                    690:        \ JAW
1.30      pazsan    691:     ELSE  ." : " forth-name 2@ type ."   ( "
                    692:        effect-in effect-in-end @ .stack-list ." -- "
                    693:        effect-out effect-out-end @ .stack-list ." )" cr
                    694:        forth-code 2@ type cr
                    695:        -1 primitive-number +!
                    696:     THEN ;
1.10      anton     697: 
1.17      anton     698: : output-tag-file ( -- )
                    699:     name-filename 2@ last-name-filename 2@ compare if
                    700:        name-filename 2@ last-name-filename 2!
                    701:        #ff emit cr
                    702:        name-filename 2@ type
                    703:        ." ,0" cr
                    704:     endif ;
                    705: 
                    706: : output-tag ( -- )
                    707:     output-tag-file
                    708:     forth-name 2@ 1+ type
                    709:     127 emit
                    710:     space forth-name 2@ type space
                    711:     1 emit
                    712:     name-line @ 0 .r
                    713:     ." ,0" cr ;
                    714: 
1.10      anton     715: [IFDEF] documentation
                    716: : register-doc ( -- )
                    717:     get-current documentation set-current
                    718:     forth-name 2@ nextname create
                    719:     forth-name 2@ 2,
1.15      anton     720:     stack-string 2@ condition-stack-effect 2,
1.10      anton     721:     wordset 2@ 2,
1.15      anton     722:     c-name 2@ condition-pronounciation 2,
1.10      anton     723:     doc 2@ 2,
                    724:     set-current ;
                    725: [THEN]
1.8       pazsan    726: 
1.1       anton     727: : process-file ( addr u xt -- )
1.17      anton     728:     >r
                    729:     2dup filename 2!
1.30      pazsan    730:     0 function-number !
1.17      anton     731:     r/o open-file abort" cannot open file"
                    732:     warnings @ if
                    733:        ." ------------ CUT HERE -------------" cr  endif
                    734:     r> primfilter ;
1.30      pazsan    735: 
                    736: : process      ( xt -- )
                    737:     bl word count rot
                    738:     process-file ;

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