Annotation of gforth/prims2x.fs, revision 1.71

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.71    ! anton      22: \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
        !            23: \ And it grew even worse when it aged.
1.1       anton      24: 
                     25: \ Optimizations:
                     26: \ superfluous stores are removed. GCC removes the superfluous loads by itself
                     27: \ TOS and FTOS can be kept in register( variable)s.
                     28: \ 
                     29: \ Problems:
                     30: \ The TOS optimization is somewhat hairy. The problems by example:
                     31: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
                     32: \    The store is not superfluous although the earlier opt. would think so
                     33: \    Alternatively:    sp[0]=TOS; w=TOS; sp-=1; TOS=w;
                     34: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
                     35: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
                     36: \ 4) ( -- ): /* but here they are unnecessary */
                     37: \ 5) Words that call NEXT themselves have to be done very carefully.
                     38: \
                     39: \ To do:
1.8       pazsan     40: \ add the store optimization for doubles
1.1       anton      41: \ regarding problem 1 above: It would be better (for over) to implement
                     42: \      the alternative
                     43: 
1.3       pazsan     44: warnings off
                     45: 
1.39      jwilke     46: [IFUNDEF] vocabulary   \ we are executed just with kernel image
                     47:                        \ load the rest that is needed
                     48:                        \ (require fails because this file is needed from a
                     49:                        \ different directory with the wordlibraries)
                     50: include ./search.fs                    
                     51: include ./extend.fs
1.40      anton      52: [THEN]
                     53: 
                     54: [IFUNDEF] environment?
1.39      jwilke     55: include ./environ.fs
                     56: [THEN]
1.25      pazsan     57: 
1.49      anton      58: : struct% struct ; \ struct is redefined in gray
                     59: 
1.39      jwilke     60: include ./gray.fs
1.1       anton      61: 
1.69      anton      62: 32 constant max-effect \ number of things on one side of a stack effect
1.71    ! anton      63: 4 constant max-stacks  \ the max. number of stacks (including inst-stream).
1.1       anton      64: 255 constant maxchar
                     65: maxchar 1+ constant eof-char
1.17      anton      66: #tab constant tab-char
                     67: #lf constant nl-char
1.1       anton      68: 
1.18      anton      69: variable rawinput \ pointer to next character to be scanned
                     70: variable endrawinput \ pointer to the end of the input (the char after the last)
                     71: variable cookedinput \ pointer to the next char to be parsed
1.17      anton      72: variable line \ line number of char pointed to by input
1.65      anton      73: variable line-start \ pointer to start of current line (for error messages)
                     74: 0 line !
1.17      anton      75: 2variable filename \ filename of original input file
                     76: 0 0 filename 2!
1.25      pazsan     77: 2variable f-comment
                     78: 0 0 f-comment 2!
1.17      anton      79: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
                     80: skipsynclines on 
1.1       anton      81: 
1.71    ! anton      82: variable next-stack-number 0 next-stack-number !
        !            83: 
1.1       anton      84: : start ( -- addr )
1.18      anton      85:  cookedinput @ ;
1.1       anton      86: 
                     87: : end ( addr -- addr u )
1.18      anton      88:  cookedinput @ over - ;
1.1       anton      89: 
1.71    ! anton      90: : print-error-line ( -- )
        !            91:     \ print the current line and position
        !            92:     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
        !            93:     over - type cr
        !            94:     line-start @ rawinput @ over - typewhite ." ^" cr ;
        !            95: 
        !            96: : ?print-error { f addr u -- }
        !            97:     f ?not? if
        !            98:        outfile-id >r try
        !            99:            stderr to outfile-id
        !           100:            filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
        !           101:            print-error-line
        !           102:            0
        !           103:        recover endtry
        !           104:        r> to outfile-id throw
        !           105:        abort
        !           106:     endif ;
        !           107: 
1.63      anton     108: : quote ( -- )
                    109:     [char] " emit ;
                    110: 
1.1       anton     111: variable output \ xt ( -- ) of output word
                    112: 
                    113: : printprim ( -- )
                    114:  output @ execute ;
                    115: 
1.49      anton     116: struct%
1.71    ! anton     117:     cell%    field stack-number \ the number of this stack
1.49      anton     118:     cell% 2* field stack-pointer \ stackpointer name
                    119:     cell% 2* field stack-cast \ cast string for assignments to stack elements
1.53      anton     120:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
1.49      anton     121: end-struct stack%
                    122: 
1.53      anton     123: struct%
                    124:  cell% 2* field item-name   \ name, excluding stack prefixes
                    125:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
                    126:  cell%    field item-type   \ descriptor for the item type
                    127:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
1.66      anton     128:  cell%   field item-first  \ true if this is the first occurence of the item
1.53      anton     129: end-struct item%
                    130: 
                    131: struct%
                    132:     cell% 2* field type-c-name
                    133:     cell%    field type-stack \ default stack
                    134:     cell%    field type-size  \ size of type in stack items
                    135:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
                    136:     cell%    field type-store \ xt of store code generator ( item -- )
                    137: end-struct type%
                    138: 
                    139: : stack-in-index ( in-size item -- in-index )
                    140:     item-offset @ - 1- ;
                    141: 
                    142: : inst-in-index ( in-size item -- in-index )
                    143:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
                    144: 
1.49      anton     145: : make-stack ( addr-ptr u1 addr-cast u2 "stack-name" -- )
                    146:     create stack% %allot >r
1.71    ! anton     147:     next-stack-number @ r@ stack-number !  1 next-stack-number +!
1.49      anton     148:     save-mem r@ stack-cast 2!
1.53      anton     149:     save-mem r@ stack-pointer 2! 
                    150:     ['] stack-in-index r> stack-in-index-xt ! ;
1.49      anton     151: 
                    152: s" sp" save-mem s" (Cell)" make-stack data-stack 
                    153: s" fp" save-mem s" "       make-stack fp-stack
1.51      anton     154: s" rp" save-mem s" (Cell)" make-stack return-stack
1.62      anton     155: s" IP" save-mem s" error don't use # on results" make-stack inst-stream
1.53      anton     156: ' inst-in-index inst-stream stack-in-index-xt !
1.49      anton     157: \ !! initialize stack-in and stack-out
                    158: 
                    159: \ stack items
                    160: 
                    161: : init-item ( addr u addr1 -- )
                    162:     \ initialize item at addr1 with name addr u
                    163:     \ !! remove stack prefix
                    164:     dup item% %size erase
                    165:     item-name 2! ;
                    166: 
1.64      anton     167: : map-items { addr end xt -- }
                    168:     \ perform xt for all items in array addr...end
                    169:     end addr ?do
                    170:        i xt execute
                    171:     item% %size +loop ;
                    172: 
1.49      anton     173: \ various variables for storing stuff of one primitive
1.1       anton     174: 
1.69      anton     175: struct%
                    176:     cell% 2* field prim-name
                    177:     cell% 2* field prim-wordset
                    178:     cell% 2* field prim-c-name
                    179:     cell% 2* field prim-doc
                    180:     cell% 2* field prim-c-code
                    181:     cell% 2* field prim-forth-code
                    182:     cell% 2* field prim-stack-string
                    183:     item% max-effect * field prim-effect-in
                    184:     item% max-effect * field prim-effect-out
                    185:     cell%    field prim-effect-in-end
                    186:     cell%    field prim-effect-out-end
1.71    ! anton     187:     cell% max-stacks * field prim-stacks-in  \ number of in items per stack
        !           188:     cell% max-stacks * field prim-stacks-out \ number of out items per stack
1.69      anton     189: end-struct prim%
                    190: 
1.70      anton     191: : make-prim ( -- prim )
                    192:     prim% %alloc { p }
                    193:     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
                    194:     p ;
                    195: 
                    196: 0 value prim
1.69      anton     197: 
1.71    ! anton     198: wordlist constant primitives
        !           199: 
        !           200: : create-prim ( prim -- )
        !           201:     get-current >r
        !           202:     primitives set-current
        !           203:     dup prim-name 2@ nextname constant
        !           204:     r> set-current ;
        !           205: 
        !           206: : stack-in ( stack -- addr )
        !           207:     \ address of number of stack items in effect in
        !           208:     stack-number @ cells prim prim-stacks-in + ;
        !           209: 
        !           210: : stack-out ( stack -- addr )
        !           211:     \ address of number of stack items in effect out
        !           212:     stack-number @ cells prim prim-stacks-out + ;
        !           213: 
1.69      anton     214: \ global vars
1.17      anton     215: variable c-line
                    216: 2variable c-filename
                    217: variable name-line
                    218: 2variable name-filename
                    219: 2variable last-name-filename
1.30      pazsan    220: Variable function-number 0 function-number !
1.1       anton     221: 
                    222: \ for several reasons stack items of a word are stored in a wordlist
                    223: \ since neither forget nor marker are implemented yet, we make a new
                    224: \ wordlist for every word and store it in the variable items
                    225: variable items
                    226: 
                    227: \ a few more set ops
                    228: 
                    229: : bit-equivalent ( w1 w2 -- w3 )
                    230:  xor invert ;
                    231: 
                    232: : complement ( set1 -- set2 )
                    233:  empty ['] bit-equivalent binary-set-operation ;
                    234: 
                    235: \ types
                    236: 
1.49      anton     237: : stack-access ( n stack -- )
                    238:     \ print a stack access at index n of stack
                    239:     stack-pointer 2@ type
                    240:     dup
                    241:     if
                    242:        ." [" 0 .r ." ]"
                    243:     else
                    244:        drop ." TOS"
                    245:     endif ;
1.1       anton     246: 
1.53      anton     247: : item-in-index { item -- n }
1.49      anton     248:     \ n is the index of item (in the in-effect)
1.53      anton     249:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
                    250:     item r> stack-in-index-xt @ execute ;
1.1       anton     251: 
                    252: : fetch-single ( item -- )
1.49      anton     253:  \ fetch a single stack item from its stack
1.1       anton     254:  >r
1.8       pazsan    255:  r@ item-name 2@ type
                    256:  ."  = (" 
1.1       anton     257:  r@ item-type @ type-c-name 2@ type ." ) "
1.49      anton     258:  r@ item-in-index r@ item-stack @ stack-access
                    259:  ." ;" cr
1.1       anton     260:  rdrop ; 
                    261: 
                    262: : fetch-double ( item -- )
1.49      anton     263:  \ fetch a double stack item from its stack
1.1       anton     264:  >r
1.20      anton     265:  ." FETCH_DCELL("
                    266:  r@ item-name 2@ type ." , "
1.61      anton     267:  r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
                    268:  ." , "                      -1 under+ ." (Cell)" stack-access
1.20      anton     269:  ." );" cr
1.1       anton     270:  rdrop ;
                    271: 
1.49      anton     272: : same-as-in? ( item -- f )
                    273:  \ f is true iff the offset and stack of item is the same as on input
1.1       anton     274:  >r
                    275:  r@ item-name 2@ items @ search-wordlist 0=
1.8       pazsan    276:  abort" bug"
1.1       anton     277:  execute @
                    278:  dup r@ =
                    279:  if \ item first appeared in output
                    280:    drop false
                    281:  else
1.49      anton     282:    dup  item-stack  @ r@ item-stack  @ = 
                    283:    swap item-offset @ r@ item-offset @ = and
1.1       anton     284:  endif
                    285:  rdrop ;
                    286: 
1.49      anton     287: : item-out-index ( item -- n )
                    288:     \ n is the index of item (in the in-effect)
                    289:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
1.31      pazsan    290: 
1.1       anton     291: : really-store-single ( item -- )
                    292:  >r
1.49      anton     293:  r@ item-out-index r@ item-stack @ stack-access ."  = "
                    294:  r@ item-stack @ stack-cast 2@ type
1.1       anton     295:  r@ item-name 2@ type ." ;"
                    296:  rdrop ;
                    297: 
                    298: : store-single ( item -- )
                    299:  >r
1.49      anton     300:  r@ same-as-in?
1.1       anton     301:  if
1.49      anton     302:    r@ item-in-index 0= r@ item-out-index 0= xor
1.1       anton     303:    if
1.49      anton     304:        ." IF_" r@ item-stack @ stack-pointer 2@ type
                    305:        ." TOS(" r@ really-store-single ." );" cr
1.1       anton     306:    endif
                    307:  else
                    308:    r@ really-store-single cr
                    309:  endif
                    310:  rdrop ;
                    311: 
                    312: : store-double ( item -- )
                    313: \ !! store optimization is not performed, because it is not yet needed
                    314:  >r
1.20      anton     315:  ." STORE_DCELL(" r@ item-name 2@ type ." , "
1.49      anton     316:  r@ item-out-index r@ item-stack @ 2dup stack-access
                    317:  ." , "                       -1 under+ stack-access
1.20      anton     318:  ." );" cr
1.1       anton     319:  rdrop ;
                    320: 
1.54      anton     321: : single ( -- xt1 xt2 n )
                    322:     ['] fetch-single ['] store-single 1 ;
1.1       anton     323: 
1.54      anton     324: : double ( -- xt1 xt2 n )
                    325:     ['] fetch-double ['] store-double 2 ;
1.1       anton     326: 
                    327: : s, ( addr u -- )
                    328: \ allocate a string
                    329:  here swap dup allot move ;
                    330: 
1.50      anton     331: wordlist constant prefixes
                    332: 
                    333: : declare ( addr "name" -- )
                    334: \ remember that there is a stack item at addr called name
                    335:  create , ;
                    336: 
                    337: : !default ( w addr -- )
                    338:     dup @ if
                    339:        2drop \ leave nonzero alone
                    340:     else
                    341:        !
                    342:     endif ;
                    343: 
                    344: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
1.49      anton     345:     \ describes a type
                    346:     \ addr u specifies the C type name
                    347:     \ stack effect entries of the type start with prefix
                    348:     create type% %allot >r
                    349:     addr u save-mem r@ type-c-name 2!
                    350:     xt1   r@ type-fetch !
                    351:     xt2   r@ type-store !
                    352:     n     r@ type-size !
                    353:     stack r@ type-stack !
                    354:     rdrop ;
1.1       anton     355: 
1.54      anton     356: : type-prefix ( xt1 xt2 n stack "prefix" -- )
1.50      anton     357:     create-type
                    358: does> ( item -- )
                    359:     \ initialize item
                    360:     { item typ }
                    361:     typ item item-type !
                    362:     typ type-stack @ item item-stack !default
                    363:     item item-name 2@ items @ search-wordlist 0= if \ new name
1.66      anton     364:        item item-name 2@ nextname item declare
                    365:        item item-first on
                    366:        \ typ type-c-name 2@ type space type  ." ;" cr
1.50      anton     367:     else
                    368:        drop
1.66      anton     369:        item item-first off
1.50      anton     370:     endif ;
                    371: 
                    372: : execute-prefix ( item addr1 u1 -- )
                    373:     \ execute the word ( item -- ) associated with the longest prefix
                    374:     \ of addr1 u1
                    375:     0 swap ?do
                    376:        dup i prefixes search-wordlist
                    377:        if \ ok, we have the type ( item addr1 xt )
                    378:            nip execute
                    379:            UNLOOP EXIT
                    380:        endif
                    381:        -1 s+loop
                    382:     \ we did not find a type, abort
                    383:     true abort" unknown prefix" ;
1.1       anton     384: 
                    385: : declaration ( item -- )
1.50      anton     386:     dup item-name 2@ execute-prefix ;
1.1       anton     387: 
1.64      anton     388: : declaration-list ( addr1 addr2 -- )
                    389:     ['] declaration map-items ;
                    390: 
                    391: : declarations ( -- )
                    392:  wordlist dup items ! set-current
1.69      anton     393:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
                    394:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
1.64      anton     395: 
1.66      anton     396: : print-declaration { item -- }
                    397:     item item-first @ if
                    398:        item item-type @ type-c-name 2@ type space
                    399:        item item-name 2@ type ." ;" cr
                    400:     endif ;
                    401: 
                    402: : print-declarations ( -- )
1.69      anton     403:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
                    404:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
1.66      anton     405:     
1.51      anton     406: : stack-prefix ( stack "prefix" -- )
                    407:     name tuck nextname create ( stack length ) 2,
                    408: does> ( item -- )
                    409:     2@ { item stack prefix-length }
                    410:     item item-name 2@ prefix-length /string item item-name 2!
                    411:     stack item item-stack !
                    412:     item declaration ;
1.1       anton     413: 
                    414: \ offset computation
                    415: \ the leftmost (i.e. deepest) item has offset 0
                    416: \ the rightmost item has the highest offset
                    417: 
1.49      anton     418: : compute-offset { item xt -- }
                    419:     \ xt specifies in/out; update stack-in/out and set item-offset
                    420:     item item-type @ type-size @
                    421:     item item-stack @ xt execute dup @ >r +!
                    422:     r> item item-offset ! ;
                    423: 
1.64      anton     424: : compute-offset-in ( addr1 addr2 -- )
                    425:     ['] stack-in compute-offset ;
                    426: 
                    427: : compute-offset-out ( addr1 addr2 -- )
                    428:     ['] stack-out compute-offset ;
1.49      anton     429: 
                    430: : clear-stack { -- }
                    431:     dup stack-in off stack-out off ;
1.1       anton     432: 
                    433: : compute-offsets ( -- )
1.51      anton     434:     data-stack clear-stack  fp-stack clear-stack return-stack clear-stack
1.53      anton     435:     inst-stream clear-stack
1.69      anton     436:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
                    437:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
1.53      anton     438:     inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
1.49      anton     439: 
                    440: : flush-a-tos { stack -- }
                    441:     stack stack-out @ 0<> stack stack-in @ 0= and
                    442:     if
                    443:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
                    444:        2dup type ." [0] = " type ." TOS);" cr
                    445:     endif ;
1.1       anton     446: 
                    447: : flush-tos ( -- )
1.51      anton     448:     data-stack   flush-a-tos
                    449:     fp-stack     flush-a-tos
                    450:     return-stack flush-a-tos ;
1.49      anton     451: 
                    452: : fill-a-tos { stack -- }
                    453:     stack stack-out @ 0= stack stack-in @ 0<> and
                    454:     if
                    455:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
                    456:        2dup type ." TOS = " type ." [0]);" cr
                    457:     endif ;
1.1       anton     458: 
                    459: : fill-tos ( -- )
1.53      anton     460:     \ !! inst-stream for prefetching?
1.51      anton     461:     fp-stack     fill-a-tos
                    462:     data-stack   fill-a-tos
                    463:     return-stack fill-a-tos ;
1.49      anton     464: 
                    465: : fetch ( addr -- )
                    466:  dup item-type @ type-fetch @ execute ;
1.1       anton     467: 
                    468: : fetches ( -- )
1.69      anton     469:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
1.49      anton     470: 
                    471: : stack-pointer-update { stack -- }
                    472:     \ stack grow downwards
                    473:     stack stack-in @ stack stack-out @ -
                    474:     ?dup-if \ this check is not necessary, gcc would do this for us
                    475:        stack stack-pointer 2@ type ."  += " 0 .r ." ;" cr
                    476:     endif ;
1.1       anton     477: 
1.55      anton     478: : inst-pointer-update ( -- )
                    479:     inst-stream stack-in @ ?dup-if
                    480:        ." INC_IP(" 0 .r ." );" cr
                    481:     endif ;
                    482: 
1.1       anton     483: : stack-pointer-updates ( -- )
1.55      anton     484:     inst-pointer-update
1.51      anton     485:     data-stack   stack-pointer-update
                    486:     fp-stack     stack-pointer-update
                    487:     return-stack stack-pointer-update ;
1.1       anton     488: 
                    489: : store ( item -- )
                    490: \ f is true if the item should be stored
                    491: \ f is false if the store is probably not necessary
1.49      anton     492:  dup item-type @ type-store @ execute ;
1.1       anton     493: 
                    494: : stores ( -- )
1.69      anton     495:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
1.8       pazsan    496: 
1.52      anton     497: : output-c-tail ( -- )
                    498:     \ the final part of the generated C code
                    499:     ." NEXT_P1;" cr
                    500:     stores
                    501:     fill-tos
                    502:     ." NEXT_P2;" cr ;
                    503: 
                    504: : type-c ( c-addr u -- )
                    505:     \ like TYPE, but replaces "TAIL;" with tail code
                    506:     begin ( c-addr1 u1 )
                    507:        2dup s" TAIL;" search
                    508:     while ( c-addr1 u1 c-addr3 u3 )
                    509:        2dup 2>r drop nip over - type
                    510:        output-c-tail
                    511:        2r> 5 /string
                    512:        \ !! resync #line missing
                    513:     repeat
                    514:     2drop type ;
                    515: 
1.63      anton     516: : print-type-prefix ( type -- )
                    517:     body> >head .name ;
                    518: 
                    519: : print-debug-arg { item -- }
                    520:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
                    521:     ." printarg_" item item-type @ print-type-prefix
                    522:     ." (" item item-name 2@ type ." );" cr ;
                    523:     
                    524: : print-debug-args ( -- )
                    525:     ." #ifdef VM_DEBUG" cr
                    526:     ." if (vm_debug) {" cr
1.69      anton     527:     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
1.63      anton     528:     ." fputc('\n', vm_out);" cr
                    529:     ." }" cr
                    530:     ." #endif" cr ;
                    531:     
1.43      jwilke    532: : output-c ( -- ) 
1.69      anton     533:  ." I_" prim prim-c-name 2@ type ." :  /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                    534:  ." /* " prim prim-doc 2@ type ."  */" cr
                    535:  ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1.1       anton     536:  ." {" cr
                    537:  ." DEF_CA" cr
1.66      anton     538:  print-declarations
1.13      anton     539:  ." NEXT_P0;" cr
                    540:  flush-tos
1.1       anton     541:  fetches
1.63      anton     542:  print-debug-args
1.13      anton     543:  stack-pointer-updates
1.1       anton     544:  ." {" cr
1.63      anton     545:  ." #line " c-line @ . quote c-filename 2@ type quote cr
1.69      anton     546:  prim prim-c-code 2@ type-c
1.1       anton     547:  ." }" cr
1.52      anton     548:  output-c-tail
1.1       anton     549:  ." }" cr
                    550:  cr
                    551: ;
                    552: 
1.56      anton     553: : disasm-arg { item -- }
                    554:     item item-stack @ inst-stream = if
1.63      anton     555:        ."   fputc(' ', vm_out); "
                    556:        ." printarg_" item item-type @ print-type-prefix
                    557:        ." ((" item item-type @ type-c-name 2@ type ." )"
                    558:        ." ip[" item item-offset @ 1+ 0 .r ." ]);" cr
1.56      anton     559:     endif ;
                    560: 
                    561: : disasm-args ( -- )
1.69      anton     562:     prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1.56      anton     563: 
                    564: : output-disasm ( -- )
                    565:     \ generate code for disassembling VM instructions
                    566:     ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
1.69      anton     567:     ."   fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1.56      anton     568:     disasm-args
                    569:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1.68      anton     570:     ." } else " ;
1.56      anton     571: 
1.60      anton     572: : gen-arg-parm { item -- }
                    573:     item item-stack @ inst-stream = if
                    574:        ." , " item item-type @ type-c-name 2@ type space
                    575:        item item-name 2@ type
                    576:     endif ;
                    577: 
                    578: : gen-args-parm ( -- )
1.69      anton     579:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1.60      anton     580: 
                    581: : gen-arg-gen { item -- }
                    582:     item item-stack @ inst-stream = if
                    583:        ."   genarg_" item item-type @ print-type-prefix
                    584:         ." (ctp, " item item-name 2@ type ." );" cr
                    585:     endif ;
                    586: 
                    587: : gen-args-gen ( -- )
1.69      anton     588:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1.60      anton     589: 
                    590: : output-gen ( -- )
                    591:     \ generate C code for generating VM instructions
1.69      anton     592:     ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
1.60      anton     593:     ." {" cr
                    594:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
                    595:     gen-args-gen
1.68      anton     596:     ." }" cr ;
1.60      anton     597: 
1.49      anton     598: : stack-used? { stack -- f }
                    599:     stack stack-in @ stack stack-out @ or 0<> ;
1.44      jwilke    600: 
1.30      pazsan    601: : output-funclabel ( -- )
1.69      anton     602:   ." &I_" prim prim-c-name 2@ type ." ," cr ;
1.30      pazsan    603: 
                    604: : output-forthname ( -- )
1.69      anton     605:   '" emit prim prim-name 2@ type '" emit ." ," cr ;
1.30      pazsan    606: 
                    607: : output-c-func ( -- )
1.44      jwilke    608: \ used for word libraries
1.69      anton     609:     ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP)      /* " prim prim-name 2@ type
                    610:     ."  ( " prim prim-stack-string 2@ type ."  ) */" cr
                    611:     ." /* " prim prim-doc 2@ type ."  */" cr
                    612:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr
1.30      pazsan    613:     \ debugging
                    614:     ." {" cr
1.66      anton     615:     print-declarations
1.53      anton     616:     inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
1.51      anton     617:     data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
                    618:     fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
                    619:     return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1.30      pazsan    620:     flush-tos
                    621:     fetches
                    622:     stack-pointer-updates
1.49      anton     623:     fp-stack   stack-used? IF ." *FP=fp;" cr THEN
1.30      pazsan    624:     ." {" cr
1.63      anton     625:     ." #line " c-line @ . quote c-filename 2@ type quote cr
1.69      anton     626:     prim prim-c-code 2@ type
1.30      pazsan    627:     ." }" cr
                    628:     stores
                    629:     fill-tos
1.44      jwilke    630:     ." return (sp);" cr
1.30      pazsan    631:     ." }" cr
                    632:     cr ;
                    633: 
1.43      jwilke    634: : output-label ( -- )  
1.69      anton     635:     ." (Label)&&I_" prim prim-c-name 2@ type ." ," cr ;
1.1       anton     636: 
1.43      jwilke    637: : output-alias ( -- ) 
1.69      anton     638:     ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1.1       anton     639: 
1.43      jwilke    640: : output-forth ( -- )  
1.69      anton     641:     prim prim-forth-code @ 0=
1.30      pazsan    642:     IF         \ output-alias
1.28      jwilke    643:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                    644:        \ JAW
1.69      anton     645:     ELSE  ." : " prim prim-name 2@ type ."   ( "
                    646:        prim prim-stack-string 2@ type ." )" cr
                    647:        prim prim-forth-code 2@ type cr
1.30      pazsan    648:     THEN ;
1.10      anton     649: 
1.17      anton     650: : output-tag-file ( -- )
                    651:     name-filename 2@ last-name-filename 2@ compare if
                    652:        name-filename 2@ last-name-filename 2!
                    653:        #ff emit cr
                    654:        name-filename 2@ type
                    655:        ." ,0" cr
                    656:     endif ;
                    657: 
                    658: : output-tag ( -- )
                    659:     output-tag-file
1.69      anton     660:     prim prim-name 2@ 1+ type
1.17      anton     661:     127 emit
1.69      anton     662:     space prim prim-name 2@ type space
1.17      anton     663:     1 emit
                    664:     name-line @ 0 .r
                    665:     ." ,0" cr ;
                    666: 
1.10      anton     667: [IFDEF] documentation
                    668: : register-doc ( -- )
                    669:     get-current documentation set-current
1.69      anton     670:     prim prim-name 2@ nextname create
                    671:     prim prim-name 2@ 2,
                    672:     prim prim-stack-string 2@ condition-stack-effect 2,
                    673:     prim prim-wordset 2@ 2,
                    674:     prim prim-c-name 2@ condition-pronounciation 2,
                    675:     prim prim-doc 2@ 2,
1.10      anton     676:     set-current ;
                    677: [THEN]
1.67      anton     678: 
                    679: 
1.69      anton     680: \ combining instructions
                    681: 
                    682: \ The input should look like this:
                    683: 
                    684: \ lit_+ = lit +
                    685: 
                    686: \ The output should look like this:
                    687: 
                    688: \  I_lit_+:
                    689: \  {
                    690: \  DEF_CA
                    691: \  Cell _x_ip0;
                    692: \  Cell _x_sp0;
                    693: \  Cell _x_sp1;
                    694: \  NEXT_P0;
                    695: \  _x_ip0 = (Cell) IPTOS;
                    696: \  _x_sp0 = (Cell) spTOS;
                    697: \  INC_IP(1);
                    698: \  /* sp += 0; */
                    699: \  /* lit ( #w -- w ) */
                    700: \  /*  */
                    701: \  NAME("lit")
                    702: \  {
                    703: \  Cell w;
                    704: \  w = (Cell) _x_ip0;
                    705: \  #ifdef VM_DEBUG
                    706: \  if (vm_debug) {
                    707: \  fputs(" w=", vm_out); printarg_w (w);
                    708: \  fputc('\n', vm_out);
                    709: \  }
                    710: \  #endif
                    711: \  {
                    712: \  #line 136 "./prim"
                    713: \  }
                    714: \  _x_sp1 = (Cell)w;
                    715: \  }
                    716: \  I_plus:     /* + ( n1 n2 -- n ) */
                    717: \  /*  */
                    718: \  NAME("+")
                    719: \  {
                    720: \  DEF_CA
                    721: \  Cell n1;
                    722: \  Cell n2;
                    723: \  Cell n;
                    724: \  NEXT_P0;
                    725: \  n1 = (Cell) _x_sp0;
                    726: \  n2 = (Cell) _x_sp1;
                    727: \  #ifdef VM_DEBUG
                    728: \  if (vm_debug) {
                    729: \  fputs(" n1=", vm_out); printarg_n (n1);
                    730: \  fputs(" n2=", vm_out); printarg_n (n2);
                    731: \  fputc('\n', vm_out);
                    732: \  }
                    733: \  #endif
                    734: \  {
                    735: \  #line 516 "./prim"
                    736: \  n = n1+n2;
                    737: \  }
                    738: \  NEXT_P1;
                    739: \  _x_sp0 = (Cell)n;
                    740: \  NEXT_P2;
                    741: \  }
                    742: \  NEXT_P1;
                    743: \  spTOS = (Cell)_x_sp0;
                    744: \  NEXT_P2;
                    745: 
1.71    ! anton     746: 1000 constant max-combined
        !           747: create combined-prims max-combined cells allot
        !           748: variable num-combined
        !           749: 
        !           750: create current-depth max-stacks cells allot
        !           751: create max-depth     max-stacks cells allot
        !           752: 
        !           753: : init-combined ( -- )
        !           754:     0 num-combined !
        !           755:     current-depth max-stacks cells erase
        !           756:     max-depth     max-stacks cells erase ;
        !           757: 
        !           758: : max! ( n addr -- )
        !           759:     tuck @ max swap ! ;
        !           760: 
        !           761: : add-depths { p -- }
        !           762:     \ combine stack effect of p with *-depths
        !           763:     max-stacks 0 ?do
        !           764:        current-depth i cells + @
        !           765:        p prim-stacks-in  i cells + @ +
        !           766:        dup max-depth i cells + max!
        !           767:        p prim-stacks-out i cells + @ -
        !           768:        current-depth i cells + !
        !           769:     loop ;
        !           770: 
        !           771: : add-prim ( addr u -- )
        !           772:     \ add primitive given by "addr u" to combined-prims
        !           773:     primitives search-wordlist s" unknown primitive" ?print-error
        !           774:     execute { p }
        !           775:     p combined-prims num-combined @ cells + !
        !           776:     1 num-combined +!
        !           777:     p add-depths ;
        !           778: 
        !           779: : compute-effects { q -- }
        !           780:     \ compute the stack effects of q from the depths
        !           781:     max-stacks 0 ?do
        !           782:        max-depth i cells + @ dup
        !           783:        q prim-stacks-in i cells + !
        !           784:        current-depth i cells + @ -
        !           785:        q prim-stacks-out i cells + !
        !           786:     loop ;
        !           787: 
        !           788: : process-combined ( -- )
        !           789:     prim compute-effects ;
1.69      anton     790: 
1.67      anton     791: \ the parser
                    792: 
                    793: eof-char max-member \ the whole character set + EOF
                    794: 
                    795: : getinput ( -- n )
                    796:  rawinput @ endrawinput @ =
                    797:  if
                    798:    eof-char
                    799:  else
                    800:    cookedinput @ c@
                    801:  endif ;
                    802: 
                    803: :noname ( n -- )
                    804:  dup bl > if
                    805:   emit space
                    806:  else
                    807:   .
                    808:  endif ;
                    809: print-token !
                    810: 
                    811: : testchar? ( set -- f )
                    812:  getinput member? ;
                    813: ' testchar? test-vector !
                    814: 
                    815: : checksyncline ( -- )
                    816:     \ when input points to a newline, check if the next line is a
                    817:     \ sync line.  If it is, perform the appropriate actions.
                    818:     rawinput @ >r
                    819:     s" #line " r@ over compare 0<> if
                    820:        rdrop 1 line +! EXIT
                    821:     endif
                    822:     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
                    823:     dup c@ bl = if
                    824:        char+ dup c@ [char] " <> abort" sync line syntax"
                    825:        char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
                    826:        char+
                    827:     endif
                    828:     dup c@ nl-char <> abort" sync line syntax"
                    829:     skipsynclines @ if
                    830:        dup char+ rawinput !
                    831:        rawinput @ c@ cookedinput @ c!
                    832:     endif
                    833:     drop ;
                    834: 
                    835: : ?nextchar ( f -- )
1.71    ! anton     836:     s" syntax error, wrong char" ?print-error
1.67      anton     837:     rawinput @ endrawinput @ <> if
                    838:        rawinput @ c@
                    839:        1 chars rawinput +!
                    840:        1 chars cookedinput +!
                    841:        nl-char = if
                    842:            checksyncline
                    843:            rawinput @ line-start !
                    844:        endif
                    845:        rawinput @ c@ cookedinput @ c!
                    846:     endif ;
                    847: 
                    848: : charclass ( set "name" -- )
                    849:  ['] ?nextchar terminal ;
                    850: 
                    851: : .. ( c1 c2 -- set )
                    852:  ( creates a set that includes the characters c, c1<=c<=c2 )
                    853:  empty copy-set
                    854:  swap 1+ rot do
                    855:   i over add-member
                    856:  loop ;
                    857: 
                    858: : ` ( -- terminal ) ( use: ` c )
                    859:  ( creates anonymous terminal for the character c )
                    860:  char singleton ['] ?nextchar make-terminal ;
                    861: 
                    862: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
                    863: char 0 char 9 ..                                       charclass digit
                    864: bl singleton tab-char over add-member                  charclass white
                    865: nl-char singleton eof-char over add-member complement  charclass nonl
                    866: nl-char singleton eof-char over add-member
                    867:     char : over add-member complement                   charclass nocolonnl
                    868: bl 1+ maxchar .. char \ singleton complement intersection
                    869:                                                         charclass nowhitebq
                    870: bl 1+ maxchar ..                                        charclass nowhite
                    871: char " singleton eof-char over add-member complement   charclass noquote
                    872: nl-char singleton                                      charclass nl
                    873: eof-char singleton                                     charclass eof
                    874: 
                    875: 
                    876: (( letter (( letter || digit )) **
                    877: )) <- c-ident ( -- )
                    878: 
                    879: (( ` # ?? (( letter || digit || ` : )) **
                    880: )) <- stack-ident ( -- )
                    881: 
                    882: (( nowhitebq nowhite ** ))
                    883: <- forth-ident ( -- )
                    884: 
                    885: Variable forth-flag
                    886: Variable c-flag
                    887: 
                    888: (( (( ` e || ` E )) {{ start }} nonl ** 
                    889:    {{ end evaluate }}
                    890: )) <- eval-comment ( ... -- ... )
                    891: 
                    892: (( (( ` f || ` F )) {{ start }} nonl ** 
                    893:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
                    894: )) <- forth-comment ( -- )
                    895: 
                    896: (( (( ` c || ` C )) {{ start }} nonl ** 
                    897:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
                    898: )) <- c-comment ( -- )
                    899: 
                    900: (( ` - nonl ** {{ 
                    901:        forth-flag @ IF ." [ELSE]" cr THEN
                    902:        c-flag @ IF ." #else" cr THEN }}
                    903: )) <- else-comment
                    904: 
                    905: (( ` + {{ start }} nonl ** {{ end
                    906:        dup
                    907:        IF      c-flag @
                    908:                IF    ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
                    909:                THEN
                    910:                forth-flag @
                    911:                IF  ." has? " type ."  [IF]"  cr THEN
                    912:        ELSE    2drop
                    913:            c-flag @      IF  ." #endif"  cr THEN
                    914:            forth-flag @  IF  ." [THEN]"  cr THEN
                    915:        THEN }}
                    916: )) <- if-comment
                    917: 
                    918: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
                    919: 
                    920: (( ` \ comment-body nl )) <- comment ( -- )
                    921: 
                    922: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
                    923: <- stack-items
                    924: 
1.69      anton     925: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
1.67      anton     926:    ` - ` - white **
1.69      anton     927:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1.67      anton     928: )) <- stack-effect ( -- )
                    929: 
1.71    ! anton     930: (( {{ prim create-prim }}
1.69      anton     931:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
                    932:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
                    933:       (( {{ start }}  c-ident {{ end prim prim-c-name 2! }} )) ??
                    934:    )) ??  nl
                    935:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nl )) ??
                    936:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl **  nl white ** )) ** {{ end prim prim-c-code 2! skipsynclines on }}
1.67      anton     937:    (( ` :  white ** nl
1.69      anton     938:       {{ start }} (( nonl ++  nl white ** )) ++ {{ end prim prim-forth-code 2! }}
1.68      anton     939:    )) ?? {{  declarations compute-offsets printprim 1 function-number +! }}
1.67      anton     940:    (( nl || eof ))
1.69      anton     941: )) <- simple-primitive ( -- )
                    942: 
1.71    ! anton     943: (( {{ init-combined }}
        !           944:    ` = (( white ++ {{ start }} forth-ident {{ end add-prim }} )) ++
        !           945:    (( nl || eof )) {{ process-combined }}
1.69      anton     946: )) <- combined-primitive
                    947: 
1.70      anton     948: (( {{ make-prim to prim
1.69      anton     949:       line @ name-line ! filename 2@ name-filename 2!
                    950:       start }} forth-ident {{ end 2dup prim prim-name 2! prim prim-c-name 2! }}  white ++
                    951:    (( simple-primitive || combined-primitive ))
1.67      anton     952: )) <- primitive ( -- )
                    953: 
                    954: (( (( comment || primitive || nl white ** )) ** eof ))
                    955: parser primitives2something
                    956: warnings @ [IF]
                    957: .( parser generated ok ) cr
                    958: [THEN]
                    959: 
1.69      anton     960: : primfilter ( addr u -- )
                    961:     \ process the string at addr u
                    962:     over dup rawinput ! dup line-start ! cookedinput !
                    963:     + endrawinput !
                    964:     checksyncline
                    965:     primitives2something ;    
1.8       pazsan    966: 
1.1       anton     967: : process-file ( addr u xt -- )
1.69      anton     968:     output !
1.61      anton     969:     save-mem 2dup filename 2!
1.69      anton     970:     slurp-file
1.17      anton     971:     warnings @ if
                    972:        ." ------------ CUT HERE -------------" cr  endif
1.69      anton     973:     primfilter ;
1.30      pazsan    974: 
                    975: : process      ( xt -- )
                    976:     bl word count rot
                    977:     process-file ;

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