Annotation of gforth/prims2x.fs, revision 1.52

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

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