Annotation of gforth/prims2x.fs, revision 1.49

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

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