Annotation of gforth/prims2x.fs, revision 1.50

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.50    ! anton     433: wordlist constant prefixes
        !           434: 
        !           435: : declare ( addr "name" -- )
        !           436: \ remember that there is a stack item at addr called name
        !           437:  create , ;
        !           438: 
        !           439: : !default ( w addr -- )
        !           440:     dup @ if
        !           441:        2drop \ leave nonzero alone
        !           442:     else
        !           443:        !
        !           444:     endif ;
        !           445: 
        !           446: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
1.49      anton     447:     \ describes a type
                    448:     \ addr u specifies the C type name
                    449:     \ stack effect entries of the type start with prefix
                    450:     create type% %allot >r
                    451:     addr u save-mem r@ type-c-name 2!
                    452:     xt1   r@ type-fetch !
                    453:     xt2   r@ type-store !
                    454:     n     r@ type-size !
                    455:     stack r@ type-stack !
                    456:     rdrop ;
1.1       anton     457: 
1.50    ! anton     458: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
        !           459:     create-type
        !           460: does> ( item -- )
        !           461:     \ initialize item
        !           462:     { item typ }
        !           463:     typ item item-type !
        !           464:     typ type-stack @ item item-stack !default
        !           465:     item item-name 2@ items @ search-wordlist 0= if \ new name
        !           466:        item item-name 2@ 2dup nextname item declare
        !           467:        typ type-c-name 2@ type space type  ." ;" cr
        !           468:     else
        !           469:        drop
        !           470:     endif ;
        !           471: 
        !           472: : execute-prefix ( item addr1 u1 -- )
        !           473:     \ execute the word ( item -- ) associated with the longest prefix
        !           474:     \ of addr1 u1
        !           475:     0 swap ?do
        !           476:        dup i prefixes search-wordlist
        !           477:        if \ ok, we have the type ( item addr1 xt )
        !           478:            nip execute
        !           479:            UNLOOP EXIT
        !           480:        endif
        !           481:        -1 s+loop
        !           482:     \ we did not find a type, abort
        !           483:     true abort" unknown prefix" ;
1.1       anton     484: 
                    485: : declaration ( item -- )
1.50    ! anton     486:     dup item-name 2@ execute-prefix ;
1.1       anton     487: 
                    488: : declaration-list ( addr1 addr2 -- )
                    489:  swap ?do
                    490:   i declaration
1.49      anton     491:  item% %size +loop ;
1.8       pazsan    492: 
1.1       anton     493: : declarations ( -- )
                    494:  wordlist dup items ! set-current
                    495:  effect-in effect-in-end @ declaration-list
                    496:  effect-out effect-out-end @ declaration-list ;
1.50    ! anton     497: 
        !           498: get-current
        !           499: prefixes set-current
        !           500: 
        !           501: s" Bool"       single-type type-prefix f
        !           502: s" Char"       single-type type-prefix c
        !           503: s" Cell"       single-type type-prefix n
        !           504: s" Cell"       single-type type-prefix w
        !           505: s" UCell"      single-type type-prefix u
        !           506: s" DCell"      double-type type-prefix d
        !           507: s" UDCell"     double-type type-prefix ud
        !           508: s" Float"      float-type  type-prefix r
        !           509: s" Cell *"     single-type type-prefix a_
        !           510: s" Char *"     single-type type-prefix c_
        !           511: s" Float *"    single-type type-prefix f_
        !           512: s" DFloat *"   single-type type-prefix df_
        !           513: s" SFloat *"   single-type type-prefix sf_
        !           514: s" Xt"         single-type type-prefix xt
        !           515: s" WID"                single-type type-prefix wid
        !           516: s" struct F83Name *"   single-type type-prefix f83name
        !           517: 
        !           518: set-current
1.1       anton     519: 
                    520: \ offset computation
                    521: \ the leftmost (i.e. deepest) item has offset 0
                    522: \ the rightmost item has the highest offset
                    523: 
1.49      anton     524: : compute-offset { item xt -- }
                    525:     \ xt specifies in/out; update stack-in/out and set item-offset
                    526:     item item-type @ type-size @
                    527:     item item-stack @ xt execute dup @ >r +!
                    528:     r> item item-offset ! ;
                    529: 
                    530: : compute-list ( addr1 addr2 xt -- )
                    531:     { xt }
                    532:     swap u+do
                    533:        i xt compute-offset
                    534:     item% %size +loop ;
                    535: 
                    536: : clear-stack { -- }
                    537:     dup stack-in off stack-out off ;
1.1       anton     538: 
                    539: 
                    540: : compute-offsets ( -- )
1.49      anton     541:     data-stack clear-stack  fp-stack clear-stack
                    542:     effect-in  effect-in-end  @ ['] stack-in  compute-list
                    543:     effect-out effect-out-end @ ['] stack-out compute-list ;
                    544: 
                    545: : flush-a-tos { stack -- }
                    546:     stack stack-out @ 0<> stack stack-in @ 0= and
                    547:     if
                    548:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
                    549:        2dup type ." [0] = " type ." TOS);" cr
                    550:     endif ;
1.1       anton     551: 
                    552: : flush-tos ( -- )
1.49      anton     553:     data-stack flush-a-tos
                    554:     fp-stack   flush-a-tos ;
                    555: 
                    556: : fill-a-tos { stack -- }
                    557:     stack stack-out @ 0= stack stack-in @ 0<> and
                    558:     if
                    559:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
                    560:        2dup type ." TOS = " type ." [0]);" cr
                    561:     endif ;
1.1       anton     562: 
                    563: : fill-tos ( -- )
1.49      anton     564:     fp-stack   fill-a-tos
                    565:     data-stack fill-a-tos ;
                    566: 
                    567: : fetch ( addr -- )
                    568:  dup item-type @ type-fetch @ execute ;
1.1       anton     569: 
                    570: : fetches ( -- )
                    571:  effect-in-end @ effect-in ?do
                    572:    i fetch
1.49      anton     573:  item% %size +loop ; 
                    574: 
                    575: : stack-pointer-update { stack -- }
                    576:     \ stack grow downwards
                    577:     stack stack-in @ stack stack-out @ -
                    578:     ?dup-if \ this check is not necessary, gcc would do this for us
                    579:        stack stack-pointer 2@ type ."  += " 0 .r ." ;" cr
                    580:     endif ;
1.1       anton     581: 
                    582: : stack-pointer-updates ( -- )
1.49      anton     583:     data-stack stack-pointer-update
                    584:     fp-stack   stack-pointer-update ;
1.1       anton     585: 
                    586: : store ( item -- )
                    587: \ f is true if the item should be stored
                    588: \ f is false if the store is probably not necessary
1.49      anton     589:  dup item-type @ type-store @ execute ;
1.1       anton     590: 
                    591: : stores ( -- )
                    592:  effect-out-end @ effect-out ?do
                    593:    i store
1.49      anton     594:  item% %size +loop ; 
1.8       pazsan    595: 
1.43      jwilke    596: : output-c ( -- ) 
1.45      anton     597:  ." I_" c-name 2@ type ." :    /* " forth-name 2@ type ."  ( " stack-string 2@ type ." ) */" cr
1.1       anton     598:  ." /* " doc 2@ type ."  */" cr
1.13      anton     599:  ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
1.1       anton     600:  ." {" cr
                    601:  ." DEF_CA" cr
                    602:  declarations
                    603:  compute-offsets \ for everything else
1.13      anton     604:  ." NEXT_P0;" cr
                    605:  flush-tos
1.1       anton     606:  fetches
1.13      anton     607:  stack-pointer-updates
1.1       anton     608:  ." {" cr
1.17      anton     609:  ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
1.1       anton     610:  c-code 2@ type
                    611:  ." }" cr
                    612:  ." NEXT_P1;" cr
                    613:  stores
                    614:  fill-tos
1.9       anton     615:  ." NEXT_P2;" cr
1.1       anton     616:  ." }" cr
                    617:  cr
                    618: ;
                    619: 
1.49      anton     620: : stack-used? { stack -- f }
                    621:     stack stack-in @ stack stack-out @ or 0<> ;
1.44      jwilke    622: 
1.30      pazsan    623: : output-funclabel ( -- )
                    624:   1 function-number +!
                    625:   ." &I_" c-name 2@ type ." ," cr ;
                    626: 
                    627: : output-forthname ( -- )
                    628:   1 function-number +!
                    629:   '" emit forth-name 2@ type '" emit ." ," cr ;
                    630: 
                    631: : output-c-func ( -- )
1.44      jwilke    632: \ used for word libraries
1.30      pazsan    633:     1 function-number +!
1.44      jwilke    634:     ." Cell * I_" c-name 2@ type ." (Cell *SP, Cell **FP)      /* " forth-name 2@ type
1.30      pazsan    635:     ."  ( " stack-string 2@ type ."  ) */" cr
                    636:     ." /* " doc 2@ type ."  */" cr
                    637:     ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr
                    638:     \ debugging
                    639:     ." {" cr
                    640:     declarations
                    641:     compute-offsets \ for everything else
1.49      anton     642:     data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
                    643:     fp-stack   stack-used? IF ." Cell *fp=*FP;" cr THEN
1.30      pazsan    644:     flush-tos
                    645:     fetches
                    646:     stack-pointer-updates
1.49      anton     647:     fp-stack   stack-used? IF ." *FP=fp;" cr THEN
1.30      pazsan    648:     ." {" cr
                    649:     ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
                    650:     c-code 2@ type
                    651:     ." }" cr
                    652:     stores
                    653:     fill-tos
1.44      jwilke    654:     ." return (sp);" cr
1.30      pazsan    655:     ." }" cr
                    656:     cr ;
                    657: 
1.43      jwilke    658: : output-label ( -- )  
1.34      pazsan    659:     ." (Label)&&I_" c-name 2@ type ." ," cr
                    660:     -1 primitive-number +! ;
1.1       anton     661: 
1.43      jwilke    662: : output-alias ( -- ) 
1.38      pazsan    663:     ( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr
1.34      pazsan    664:     -1 primitive-number +! ;
1.1       anton     665: 
1.43      jwilke    666: : output-forth ( -- )  
1.30      pazsan    667:     forth-code @ 0=
                    668:     IF         \ output-alias
1.28      jwilke    669:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                    670:        \ JAW
1.30      pazsan    671:     ELSE  ." : " forth-name 2@ type ."   ( "
1.49      anton     672:        stack-string 2@ type ." )" cr
1.30      pazsan    673:        forth-code 2@ type cr
                    674:        -1 primitive-number +!
                    675:     THEN ;
1.10      anton     676: 
1.17      anton     677: : output-tag-file ( -- )
                    678:     name-filename 2@ last-name-filename 2@ compare if
                    679:        name-filename 2@ last-name-filename 2!
                    680:        #ff emit cr
                    681:        name-filename 2@ type
                    682:        ." ,0" cr
                    683:     endif ;
                    684: 
                    685: : output-tag ( -- )
                    686:     output-tag-file
                    687:     forth-name 2@ 1+ type
                    688:     127 emit
                    689:     space forth-name 2@ type space
                    690:     1 emit
                    691:     name-line @ 0 .r
                    692:     ." ,0" cr ;
                    693: 
1.10      anton     694: [IFDEF] documentation
                    695: : register-doc ( -- )
                    696:     get-current documentation set-current
                    697:     forth-name 2@ nextname create
                    698:     forth-name 2@ 2,
1.15      anton     699:     stack-string 2@ condition-stack-effect 2,
1.10      anton     700:     wordset 2@ 2,
1.15      anton     701:     c-name 2@ condition-pronounciation 2,
1.10      anton     702:     doc 2@ 2,
                    703:     set-current ;
                    704: [THEN]
1.8       pazsan    705: 
1.1       anton     706: : process-file ( addr u xt -- )
1.17      anton     707:     >r
                    708:     2dup filename 2!
1.30      pazsan    709:     0 function-number !
1.17      anton     710:     r/o open-file abort" cannot open file"
                    711:     warnings @ if
                    712:        ." ------------ CUT HERE -------------" cr  endif
                    713:     r> primfilter ;
1.30      pazsan    714: 
                    715: : process      ( xt -- )
                    716:     bl word count rot
                    717:     process-file ;

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