Annotation of gforth/prims2x.fs, revision 1.37

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

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