Annotation of gforth/prims2x.fs, revision 1.54

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

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