Annotation of gforth/prims2x.fs, revision 1.63

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

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