Annotation of gforth/prims2x.fs, revision 1.44

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

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