Annotation of gforth/prims2x.fs, revision 1.70

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

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