Annotation of gforth/prims2x.fs, revision 1.69

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

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