Annotation of gforth/prims2x.fs, revision 1.64

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

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