Annotation of gforth/prims2x.fs, revision 1.91

1.16      anton       1: \ converts primitives to, e.g., C code 
                      2: 
1.47      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
1.16      anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
1.48      anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.16      anton      20: 
                     21: 
1.71      anton      22: \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
                     23: \ And it grew even worse when it aged.
1.1       anton      24: 
                     25: \ Optimizations:
                     26: \ superfluous stores are removed. GCC removes the superfluous loads by itself
                     27: \ TOS and FTOS can be kept in register( variable)s.
                     28: \ 
                     29: \ Problems:
                     30: \ The TOS optimization is somewhat hairy. The problems by example:
                     31: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
                     32: \    The store is not superfluous although the earlier opt. would think so
                     33: \    Alternatively:    sp[0]=TOS; w=TOS; sp-=1; TOS=w;
                     34: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
                     35: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
                     36: \ 4) ( -- ): /* but here they are unnecessary */
                     37: \ 5) Words that call NEXT themselves have to be done very carefully.
                     38: \
                     39: \ To do:
1.8       pazsan     40: \ add the store optimization for doubles
1.1       anton      41: \ regarding problem 1 above: It would be better (for over) to implement
                     42: \      the alternative
1.80      anton      43: \ store optimization for combined instructions.
                     44: \ eliminate stack-cast (no longer used)
                     45: 
                     46: \ Design Uglyness:
                     47: 
                     48: \ - global state (values, variables) in connection with combined instructions.
                     49: 
                     50: \ - index computation is different for instruction-stream and the
                     51: \ stacks; there are two mechanisms for dealing with that
                     52: \ (stack-in-index-xt and a test for stack==instruction-stream); there
                     53: \ should be only one.
1.1       anton      54: 
1.3       pazsan     55: warnings off
                     56: 
1.39      jwilke     57: [IFUNDEF] vocabulary   \ we are executed just with kernel image
                     58:                        \ load the rest that is needed
                     59:                        \ (require fails because this file is needed from a
                     60:                        \ different directory with the wordlibraries)
                     61: include ./search.fs                    
                     62: include ./extend.fs
1.88      anton      63: include ./stuff.fs
1.40      anton      64: [THEN]
                     65: 
                     66: [IFUNDEF] environment?
1.39      jwilke     67: include ./environ.fs
                     68: [THEN]
1.25      pazsan     69: 
1.49      anton      70: : struct% struct ; \ struct is redefined in gray
                     71: 
1.39      jwilke     72: include ./gray.fs
1.1       anton      73: 
1.69      anton      74: 32 constant max-effect \ number of things on one side of a stack effect
1.71      anton      75: 4 constant max-stacks  \ the max. number of stacks (including inst-stream).
1.1       anton      76: 255 constant maxchar
                     77: maxchar 1+ constant eof-char
1.17      anton      78: #tab constant tab-char
                     79: #lf constant nl-char
1.1       anton      80: 
1.18      anton      81: variable rawinput \ pointer to next character to be scanned
                     82: variable endrawinput \ pointer to the end of the input (the char after the last)
                     83: variable cookedinput \ pointer to the next char to be parsed
1.17      anton      84: variable line \ line number of char pointed to by input
1.65      anton      85: variable line-start \ pointer to start of current line (for error messages)
                     86: 0 line !
1.17      anton      87: 2variable filename \ filename of original input file
                     88: 0 0 filename 2!
1.25      pazsan     89: 2variable f-comment
                     90: 0 0 f-comment 2!
1.17      anton      91: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
                     92: skipsynclines on 
1.1       anton      93: 
1.72      anton      94: : th ( addr1 n -- addr2 )
                     95:     cells + ;
                     96: 
                     97: : holds ( addr u -- )
                     98:     \ like HOLD, but for a string
                     99:     tuck + swap 0 +do
                    100:        1- dup c@ hold
                    101:     loop
                    102:     drop ;
1.71      anton     103: 
1.82      anton     104: : insert-wordlist { c-addr u wordlist xt -- }
1.81      anton     105:     \ adds name "addr u" to wordlist using defining word xt
                    106:     \ xt may cause additional stack effects
                    107:     get-current >r wordlist set-current
                    108:     c-addr u nextname xt execute
                    109:     r> set-current ;
                    110: 
1.1       anton     111: : start ( -- addr )
1.18      anton     112:  cookedinput @ ;
1.1       anton     113: 
                    114: : end ( addr -- addr u )
1.18      anton     115:  cookedinput @ over - ;
1.1       anton     116: 
1.71      anton     117: : print-error-line ( -- )
                    118:     \ print the current line and position
                    119:     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
                    120:     over - type cr
                    121:     line-start @ rawinput @ over - typewhite ." ^" cr ;
                    122: 
                    123: : ?print-error { f addr u -- }
                    124:     f ?not? if
                    125:        outfile-id >r try
                    126:            stderr to outfile-id
                    127:            filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
                    128:            print-error-line
                    129:            0
                    130:        recover endtry
                    131:        r> to outfile-id throw
                    132:        abort
                    133:     endif ;
                    134: 
1.63      anton     135: : quote ( -- )
                    136:     [char] " emit ;
                    137: 
1.72      anton     138: variable output          \ xt ( -- ) of output word for simple primitives
                    139: variable output-combined \ xt ( -- ) of output word for combined primitives
1.1       anton     140: 
1.49      anton     141: struct%
1.71      anton     142:     cell%    field stack-number \ the number of this stack
1.49      anton     143:     cell% 2* field stack-pointer \ stackpointer name
1.74      anton     144:     cell%    field stack-type \ name for default type of stack items
1.49      anton     145:     cell% 2* field stack-cast \ cast string for assignments to stack elements
1.53      anton     146:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
1.49      anton     147: end-struct stack%
                    148: 
1.53      anton     149: struct%
                    150:  cell% 2* field item-name   \ name, excluding stack prefixes
                    151:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
                    152:  cell%    field item-type   \ descriptor for the item type
                    153:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
1.66      anton     154:  cell%   field item-first  \ true if this is the first occurence of the item
1.53      anton     155: end-struct item%
                    156: 
                    157: struct%
                    158:     cell% 2* field type-c-name
                    159:     cell%    field type-stack \ default stack
                    160:     cell%    field type-size  \ size of type in stack items
                    161:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
                    162:     cell%    field type-store \ xt of store code generator ( item -- )
                    163: end-struct type%
                    164: 
1.72      anton     165: variable next-stack-number 0 next-stack-number !
                    166: create stacks max-stacks cells allot \ array of stacks
                    167: 
1.53      anton     168: : stack-in-index ( in-size item -- in-index )
                    169:     item-offset @ - 1- ;
                    170: 
                    171: : inst-in-index ( in-size item -- in-index )
                    172:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
                    173: 
1.74      anton     174: : make-stack ( addr-ptr u1 type addr-cast u2 "stack-name" -- )
1.49      anton     175:     create stack% %allot >r
1.72      anton     176:     r@ stacks next-stack-number @ th !
1.71      anton     177:     next-stack-number @ r@ stack-number !  1 next-stack-number +!
1.49      anton     178:     save-mem r@ stack-cast 2!
1.74      anton     179:     r@ stack-type !
1.53      anton     180:     save-mem r@ stack-pointer 2! 
                    181:     ['] stack-in-index r> stack-in-index-xt ! ;
1.49      anton     182: 
                    183: \ stack items
                    184: 
                    185: : init-item ( addr u addr1 -- )
                    186:     \ initialize item at addr1 with name addr u
                    187:     \ !! remove stack prefix
                    188:     dup item% %size erase
                    189:     item-name 2! ;
                    190: 
1.64      anton     191: : map-items { addr end xt -- }
                    192:     \ perform xt for all items in array addr...end
                    193:     end addr ?do
                    194:        i xt execute
                    195:     item% %size +loop ;
                    196: 
1.77      anton     197: \ types
                    198: 
                    199: : print-type-prefix ( type -- )
                    200:     body> >head name>string type ;
                    201: 
1.49      anton     202: \ various variables for storing stuff of one primitive
1.1       anton     203: 
1.69      anton     204: struct%
                    205:     cell% 2* field prim-name
                    206:     cell% 2* field prim-wordset
                    207:     cell% 2* field prim-c-name
                    208:     cell% 2* field prim-doc
                    209:     cell% 2* field prim-c-code
                    210:     cell% 2* field prim-forth-code
                    211:     cell% 2* field prim-stack-string
1.82      anton     212:     cell%    field prim-num            \ ordinal number
1.75      anton     213:     cell%    field prim-items-wordlist \ unique items
1.69      anton     214:     item% max-effect * field prim-effect-in
                    215:     item% max-effect * field prim-effect-out
                    216:     cell%    field prim-effect-in-end
                    217:     cell%    field prim-effect-out-end
1.71      anton     218:     cell% max-stacks * field prim-stacks-in  \ number of in items per stack
                    219:     cell% max-stacks * field prim-stacks-out \ number of out items per stack
1.69      anton     220: end-struct prim%
                    221: 
1.70      anton     222: : make-prim ( -- prim )
                    223:     prim% %alloc { p }
                    224:     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
                    225:     p ;
                    226: 
1.79      anton     227: 0 value prim     \ in combined prims either combined or a part
                    228: 0 value combined \ in combined prims the combined prim
                    229: variable in-part \ true if processing a part
                    230:  in-part off
                    231: 
                    232: 1000 constant max-combined
                    233: create combined-prims max-combined cells allot
                    234: variable num-combined
                    235: 
1.81      anton     236: table constant combinations
                    237:   \ the keys are the sequences of pointers to primitives
                    238: 
1.79      anton     239: create current-depth max-stacks cells allot
                    240: create max-depth     max-stacks cells allot
                    241: create min-depth     max-stacks cells allot
1.69      anton     242: 
1.71      anton     243: wordlist constant primitives
                    244: 
                    245: : create-prim ( prim -- )
1.82      anton     246:     dup prim-name 2@ primitives ['] constant insert-wordlist ;
1.71      anton     247: 
                    248: : stack-in ( stack -- addr )
                    249:     \ address of number of stack items in effect in
                    250:     stack-number @ cells prim prim-stacks-in + ;
                    251: 
                    252: : stack-out ( stack -- addr )
                    253:     \ address of number of stack items in effect out
                    254:     stack-number @ cells prim prim-stacks-out + ;
                    255: 
1.69      anton     256: \ global vars
1.17      anton     257: variable c-line
                    258: 2variable c-filename
                    259: variable name-line
                    260: 2variable name-filename
                    261: 2variable last-name-filename
1.30      pazsan    262: Variable function-number 0 function-number !
1.1       anton     263: 
                    264: \ a few more set ops
                    265: 
                    266: : bit-equivalent ( w1 w2 -- w3 )
                    267:  xor invert ;
                    268: 
                    269: : complement ( set1 -- set2 )
                    270:  empty ['] bit-equivalent binary-set-operation ;
                    271: 
1.80      anton     272: \ stack access stuff
1.79      anton     273: 
                    274: : normal-stack-access ( n stack -- )
1.49      anton     275:     stack-pointer 2@ type
                    276:     dup
                    277:     if
                    278:        ." [" 0 .r ." ]"
                    279:     else
                    280:        drop ." TOS"
                    281:     endif ;
1.1       anton     282: 
1.80      anton     283: \ forward declaration for inst-stream (breaks cycle in definitions)
                    284: defer inst-stream-f ( -- stack )
                    285: 
1.79      anton     286: : part-stack-access { n stack -- }
1.80      anton     287:     \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
1.79      anton     288:     ." _" stack stack-pointer 2@ type
                    289:     stack stack-number @ { stack# }
1.80      anton     290:     current-depth stack# th @ n + { access-depth }
                    291:     stack inst-stream-f = if
                    292:        access-depth
                    293:     else
                    294:        combined prim-stacks-in stack# th @
                    295:        assert( dup max-depth stack# th @ = )
                    296:        access-depth - 1-
                    297:     endif
1.79      anton     298:     0 .r ;
                    299: 
                    300: : stack-access ( n stack -- )
                    301:     \ print a stack access at index n of stack
                    302:     in-part @ if
                    303:        part-stack-access
                    304:     else
                    305:        normal-stack-access
                    306:     endif ;
                    307: 
1.53      anton     308: : item-in-index { item -- n }
1.49      anton     309:     \ n is the index of item (in the in-effect)
1.53      anton     310:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
                    311:     item r> stack-in-index-xt @ execute ;
1.1       anton     312: 
1.78      anton     313: : item-stack-type-name ( item -- addr u )
                    314:     item-stack @ stack-type @ type-c-name 2@ ;
                    315: 
1.1       anton     316: : fetch-single ( item -- )
1.49      anton     317:  \ fetch a single stack item from its stack
1.1       anton     318:  >r
1.8       pazsan    319:  r@ item-name 2@ type
1.78      anton     320:  ."  = vm_" r@ item-stack-type-name type
1.77      anton     321:  ." 2" r@ item-type @ print-type-prefix ." ("
1.49      anton     322:  r@ item-in-index r@ item-stack @ stack-access
1.77      anton     323:  ." );" cr
1.1       anton     324:  rdrop ; 
                    325: 
                    326: : fetch-double ( item -- )
1.49      anton     327:  \ fetch a double stack item from its stack
1.1       anton     328:  >r
1.78      anton     329:  ." vm_two"
                    330:  r@ item-stack-type-name type ." 2"
                    331:  r@ item-type @ print-type-prefix ." ("
1.20      anton     332:  r@ item-name 2@ type ." , "
1.61      anton     333:  r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
                    334:  ." , "                      -1 under+ ." (Cell)" stack-access
1.20      anton     335:  ." );" cr
1.1       anton     336:  rdrop ;
                    337: 
1.49      anton     338: : same-as-in? ( item -- f )
                    339:  \ f is true iff the offset and stack of item is the same as on input
1.1       anton     340:  >r
1.74      anton     341:  r@ item-first @ if
                    342:      rdrop false exit
                    343:  endif
1.75      anton     344:  r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
1.1       anton     345:  execute @
                    346:  dup r@ =
                    347:  if \ item first appeared in output
                    348:    drop false
                    349:  else
1.49      anton     350:    dup  item-stack  @ r@ item-stack  @ = 
                    351:    swap item-offset @ r@ item-offset @ = and
1.1       anton     352:  endif
                    353:  rdrop ;
                    354: 
1.49      anton     355: : item-out-index ( item -- n )
                    356:     \ n is the index of item (in the in-effect)
                    357:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
1.31      pazsan    358: 
1.1       anton     359: : really-store-single ( item -- )
                    360:  >r
1.77      anton     361:  r@ item-out-index r@ item-stack @ stack-access ."  = vm_"
                    362:  r@ item-type @ print-type-prefix ." 2"
1.78      anton     363:  r@ item-stack-type-name type ." ("
1.77      anton     364:  r@ item-name 2@ type ." );"
1.1       anton     365:  rdrop ;
                    366: 
                    367: : store-single ( item -- )
                    368:  >r
1.49      anton     369:  r@ same-as-in?
1.1       anton     370:  if
1.49      anton     371:    r@ item-in-index 0= r@ item-out-index 0= xor
1.1       anton     372:    if
1.49      anton     373:        ." IF_" r@ item-stack @ stack-pointer 2@ type
                    374:        ." TOS(" r@ really-store-single ." );" cr
1.1       anton     375:    endif
                    376:  else
                    377:    r@ really-store-single cr
                    378:  endif
                    379:  rdrop ;
                    380: 
                    381: : store-double ( item -- )
                    382: \ !! store optimization is not performed, because it is not yet needed
                    383:  >r
1.78      anton     384:  ." vm_"
                    385:  r@ item-type @ print-type-prefix ." 2two"
                    386:  r@ item-stack-type-name type ." ("
                    387:  r@ item-name 2@ type ." , "
1.49      anton     388:  r@ item-out-index r@ item-stack @ 2dup stack-access
                    389:  ." , "                       -1 under+ stack-access
1.20      anton     390:  ." );" cr
1.1       anton     391:  rdrop ;
                    392: 
1.54      anton     393: : single ( -- xt1 xt2 n )
                    394:     ['] fetch-single ['] store-single 1 ;
1.1       anton     395: 
1.54      anton     396: : double ( -- xt1 xt2 n )
                    397:     ['] fetch-double ['] store-double 2 ;
1.1       anton     398: 
                    399: : s, ( addr u -- )
                    400: \ allocate a string
                    401:  here swap dup allot move ;
                    402: 
1.50      anton     403: wordlist constant prefixes
                    404: 
                    405: : declare ( addr "name" -- )
                    406: \ remember that there is a stack item at addr called name
                    407:  create , ;
                    408: 
                    409: : !default ( w addr -- )
                    410:     dup @ if
                    411:        2drop \ leave nonzero alone
                    412:     else
                    413:        !
                    414:     endif ;
                    415: 
                    416: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
1.49      anton     417:     \ describes a type
                    418:     \ addr u specifies the C type name
                    419:     \ stack effect entries of the type start with prefix
                    420:     create type% %allot >r
                    421:     addr u save-mem r@ type-c-name 2!
                    422:     xt1   r@ type-fetch !
                    423:     xt2   r@ type-store !
                    424:     n     r@ type-size !
                    425:     stack r@ type-stack !
                    426:     rdrop ;
1.1       anton     427: 
1.54      anton     428: : type-prefix ( xt1 xt2 n stack "prefix" -- )
1.50      anton     429:     create-type
                    430: does> ( item -- )
                    431:     \ initialize item
                    432:     { item typ }
                    433:     typ item item-type !
                    434:     typ type-stack @ item item-stack !default
1.75      anton     435:     item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
1.66      anton     436:        item item-name 2@ nextname item declare
                    437:        item item-first on
                    438:        \ typ type-c-name 2@ type space type  ." ;" cr
1.50      anton     439:     else
                    440:        drop
1.66      anton     441:        item item-first off
1.50      anton     442:     endif ;
                    443: 
                    444: : execute-prefix ( item addr1 u1 -- )
                    445:     \ execute the word ( item -- ) associated with the longest prefix
                    446:     \ of addr1 u1
                    447:     0 swap ?do
                    448:        dup i prefixes search-wordlist
                    449:        if \ ok, we have the type ( item addr1 xt )
                    450:            nip execute
                    451:            UNLOOP EXIT
                    452:        endif
                    453:        -1 s+loop
                    454:     \ we did not find a type, abort
1.81      anton     455:     false s" unknown prefix" ?print-error ;
1.1       anton     456: 
                    457: : declaration ( item -- )
1.50      anton     458:     dup item-name 2@ execute-prefix ;
1.1       anton     459: 
1.64      anton     460: : declaration-list ( addr1 addr2 -- )
                    461:     ['] declaration map-items ;
                    462: 
                    463: : declarations ( -- )
1.75      anton     464:  wordlist dup prim prim-items-wordlist ! set-current
1.69      anton     465:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
                    466:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
1.64      anton     467: 
1.66      anton     468: : print-declaration { item -- }
                    469:     item item-first @ if
                    470:        item item-type @ type-c-name 2@ type space
                    471:        item item-name 2@ type ." ;" cr
                    472:     endif ;
                    473: 
                    474: : print-declarations ( -- )
1.69      anton     475:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
                    476:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
1.66      anton     477:     
1.51      anton     478: : stack-prefix ( stack "prefix" -- )
                    479:     name tuck nextname create ( stack length ) 2,
                    480: does> ( item -- )
                    481:     2@ { item stack prefix-length }
                    482:     item item-name 2@ prefix-length /string item item-name 2!
                    483:     stack item item-stack !
                    484:     item declaration ;
1.73      anton     485: 
1.74      anton     486: \ types pointed to by stacks for use in combined prims
1.83      anton     487: \ !! output-c-combined shouldn't use these names!
                    488: s" Cell"  single 0 create-type w
                    489: s" Float" single 0 create-type r
                    490: 
                    491: s" sp" save-mem w s" (Cell)" make-stack data-stack 
                    492: s" fp" save-mem r s" "       make-stack fp-stack
                    493: s" rp" save-mem w s" (Cell)" make-stack return-stack
                    494: s" IP" save-mem w s" error don't use # on results" make-stack inst-stream
1.73      anton     495: ' inst-in-index inst-stream stack-in-index-xt !
1.80      anton     496: ' inst-stream <is> inst-stream-f
1.73      anton     497: \ !! initialize stack-in and stack-out
1.1       anton     498: 
                    499: \ offset computation
                    500: \ the leftmost (i.e. deepest) item has offset 0
                    501: \ the rightmost item has the highest offset
                    502: 
1.49      anton     503: : compute-offset { item xt -- }
                    504:     \ xt specifies in/out; update stack-in/out and set item-offset
                    505:     item item-type @ type-size @
                    506:     item item-stack @ xt execute dup @ >r +!
                    507:     r> item item-offset ! ;
                    508: 
1.64      anton     509: : compute-offset-in ( addr1 addr2 -- )
                    510:     ['] stack-in compute-offset ;
                    511: 
                    512: : compute-offset-out ( addr1 addr2 -- )
                    513:     ['] stack-out compute-offset ;
1.49      anton     514: 
                    515: : clear-stack { -- }
                    516:     dup stack-in off stack-out off ;
1.1       anton     517: 
                    518: : compute-offsets ( -- )
1.51      anton     519:     data-stack clear-stack  fp-stack clear-stack return-stack clear-stack
1.53      anton     520:     inst-stream clear-stack
1.69      anton     521:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
                    522:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
1.81      anton     523:     inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
                    524: 
                    525: : process-simple ( -- )
                    526:     prim prim { W^ key } key cell
1.82      anton     527:     combinations ['] constant insert-wordlist
1.81      anton     528:     declarations compute-offsets
1.82      anton     529:     output @ execute ;
1.49      anton     530: 
                    531: : flush-a-tos { stack -- }
                    532:     stack stack-out @ 0<> stack stack-in @ 0= and
                    533:     if
                    534:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
                    535:        2dup type ." [0] = " type ." TOS);" cr
                    536:     endif ;
1.1       anton     537: 
                    538: : flush-tos ( -- )
1.51      anton     539:     data-stack   flush-a-tos
                    540:     fp-stack     flush-a-tos
                    541:     return-stack flush-a-tos ;
1.49      anton     542: 
                    543: : fill-a-tos { stack -- }
                    544:     stack stack-out @ 0= stack stack-in @ 0<> and
                    545:     if
                    546:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
                    547:        2dup type ." TOS = " type ." [0]);" cr
                    548:     endif ;
1.1       anton     549: 
                    550: : fill-tos ( -- )
1.53      anton     551:     \ !! inst-stream for prefetching?
1.51      anton     552:     fp-stack     fill-a-tos
                    553:     data-stack   fill-a-tos
                    554:     return-stack fill-a-tos ;
1.49      anton     555: 
                    556: : fetch ( addr -- )
1.72      anton     557:     dup item-type @ type-fetch @ execute ;
1.1       anton     558: 
                    559: : fetches ( -- )
1.69      anton     560:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
1.49      anton     561: 
                    562: : stack-pointer-update { stack -- }
                    563:     \ stack grow downwards
                    564:     stack stack-in @ stack stack-out @ -
                    565:     ?dup-if \ this check is not necessary, gcc would do this for us
                    566:        stack stack-pointer 2@ type ."  += " 0 .r ." ;" cr
                    567:     endif ;
1.1       anton     568: 
1.55      anton     569: : inst-pointer-update ( -- )
                    570:     inst-stream stack-in @ ?dup-if
                    571:        ." INC_IP(" 0 .r ." );" cr
                    572:     endif ;
                    573: 
1.1       anton     574: : stack-pointer-updates ( -- )
1.55      anton     575:     inst-pointer-update
1.51      anton     576:     data-stack   stack-pointer-update
                    577:     fp-stack     stack-pointer-update
                    578:     return-stack stack-pointer-update ;
1.1       anton     579: 
                    580: : store ( item -- )
                    581: \ f is true if the item should be stored
                    582: \ f is false if the store is probably not necessary
1.49      anton     583:  dup item-type @ type-store @ execute ;
1.1       anton     584: 
                    585: : stores ( -- )
1.69      anton     586:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
1.8       pazsan    587: 
1.91    ! anton     588: : print-debug-arg { item -- }
        !           589:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
        !           590:     ." printarg_" item item-type @ print-type-prefix
        !           591:     ." (" item item-name 2@ type ." );" cr ;
        !           592:     
        !           593: : print-debug-args ( -- )
        !           594:     ." #ifdef VM_DEBUG" cr
        !           595:     ." if (vm_debug) {" cr
        !           596:     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
        !           597: \    ." fputc('\n', vm_out);" cr
        !           598:     ." }" cr
        !           599:     ." #endif" cr ;
        !           600: 
        !           601: : print-debug-result { item -- }
        !           602:     item item-first @ if
        !           603:        item print-debug-arg
        !           604:     endif ;
        !           605: 
        !           606: : print-debug-results ( -- )
        !           607:     cr
        !           608:     ." #ifdef VM_DEBUG" cr
        !           609:     ." if (vm_debug) {" cr
        !           610:     ." fputs(" quote ."  -- " quote ." , vm_out); "
        !           611:     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
        !           612:     ." fputc('\n', vm_out);" cr
        !           613:     ." }" cr
        !           614:     ." #endif" cr ;
        !           615: 
1.86      anton     616: : output-super-end ( -- )
                    617:     prim prim-c-code 2@ s" SET_IP" search if
                    618:        ." SUPER_END;" cr
                    619:     endif
                    620:     2drop ;
                    621: 
1.52      anton     622: : output-c-tail ( -- )
                    623:     \ the final part of the generated C code
1.86      anton     624:     output-super-end
1.91    ! anton     625:     print-debug-results
1.52      anton     626:     ." NEXT_P1;" cr
                    627:     stores
                    628:     fill-tos
1.85      anton     629:     ." NEXT_P2;" ;
1.52      anton     630: 
1.85      anton     631: : type-c-code ( c-addr u xt -- )
                    632:     \ like TYPE, but replaces "TAIL;" with tail code produced by xt
                    633:     { xt }
1.52      anton     634:     begin ( c-addr1 u1 )
                    635:        2dup s" TAIL;" search
                    636:     while ( c-addr1 u1 c-addr3 u3 )
                    637:        2dup 2>r drop nip over - type
1.85      anton     638:        xt execute
1.52      anton     639:        2r> 5 /string
                    640:        \ !! resync #line missing
                    641:     repeat
                    642:     2drop type ;
1.63      anton     643: 
1.72      anton     644: : print-entry ( -- )
                    645:     ." I_" prim prim-c-name 2@ type ." :" ;
1.63      anton     646:     
1.43      jwilke    647: : output-c ( -- ) 
1.72      anton     648:  print-entry ."  /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
1.69      anton     649:  ." /* " prim prim-doc 2@ type ."  */" cr
                    650:  ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1.1       anton     651:  ." {" cr
                    652:  ." DEF_CA" cr
1.66      anton     653:  print-declarations
1.13      anton     654:  ." NEXT_P0;" cr
                    655:  flush-tos
1.1       anton     656:  fetches
1.63      anton     657:  print-debug-args
1.13      anton     658:  stack-pointer-updates
1.1       anton     659:  ." {" cr
1.63      anton     660:  ." #line " c-line @ . quote c-filename 2@ type quote cr
1.85      anton     661:  prim prim-c-code 2@ ['] output-c-tail type-c-code
1.1       anton     662:  ." }" cr
1.52      anton     663:  output-c-tail
1.1       anton     664:  ." }" cr
                    665:  cr
                    666: ;
                    667: 
1.56      anton     668: : disasm-arg { item -- }
                    669:     item item-stack @ inst-stream = if
1.63      anton     670:        ."   fputc(' ', vm_out); "
                    671:        ." printarg_" item item-type @ print-type-prefix
                    672:        ." ((" item item-type @ type-c-name 2@ type ." )"
                    673:        ." ip[" item item-offset @ 1+ 0 .r ." ]);" cr
1.56      anton     674:     endif ;
                    675: 
                    676: : disasm-args ( -- )
1.69      anton     677:     prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1.56      anton     678: 
                    679: : output-disasm ( -- )
                    680:     \ generate code for disassembling VM instructions
                    681:     ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
1.69      anton     682:     ."   fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1.56      anton     683:     disasm-args
                    684:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1.91    ! anton     685:     ."   goto _endif_;" cr
        !           686:     ." }" cr ;
1.56      anton     687: 
1.86      anton     688: : output-profile ( -- )
                    689:     \ generate code for postprocessing the VM block profile stuff
1.87      anton     690:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.86      anton     691:     ."   add_inst(b, " quote  prim prim-name 2@ type quote ." );" cr
                    692:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
                    693:     prim prim-c-code 2@  s" SET_IP"    search nip nip
                    694:     prim prim-c-code 2@  s" SUPER_END" search nip nip or if
                    695:        ."   return;" cr
1.91    ! anton     696:     else
        !           697:        ."   goto _endif_;" cr
1.86      anton     698:     endif
1.91    ! anton     699:     ." }" cr ;
1.86      anton     700: 
1.60      anton     701: : gen-arg-parm { item -- }
                    702:     item item-stack @ inst-stream = if
                    703:        ." , " item item-type @ type-c-name 2@ type space
                    704:        item item-name 2@ type
                    705:     endif ;
                    706: 
                    707: : gen-args-parm ( -- )
1.69      anton     708:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1.60      anton     709: 
                    710: : gen-arg-gen { item -- }
                    711:     item item-stack @ inst-stream = if
                    712:        ."   genarg_" item item-type @ print-type-prefix
                    713:         ." (ctp, " item item-name 2@ type ." );" cr
                    714:     endif ;
                    715: 
                    716: : gen-args-gen ( -- )
1.69      anton     717:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1.60      anton     718: 
                    719: : output-gen ( -- )
                    720:     \ generate C code for generating VM instructions
1.69      anton     721:     ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
1.60      anton     722:     ." {" cr
                    723:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
                    724:     gen-args-gen
1.68      anton     725:     ." }" cr ;
1.60      anton     726: 
1.49      anton     727: : stack-used? { stack -- f }
                    728:     stack stack-in @ stack stack-out @ or 0<> ;
1.44      jwilke    729: 
1.30      pazsan    730: : output-funclabel ( -- )
1.69      anton     731:   ." &I_" prim prim-c-name 2@ type ." ," cr ;
1.30      pazsan    732: 
                    733: : output-forthname ( -- )
1.69      anton     734:   '" emit prim prim-name 2@ type '" emit ." ," cr ;
1.30      pazsan    735: 
                    736: : output-c-func ( -- )
1.44      jwilke    737: \ used for word libraries
1.69      anton     738:     ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP)      /* " prim prim-name 2@ type
                    739:     ."  ( " prim prim-stack-string 2@ type ."  ) */" cr
                    740:     ." /* " prim prim-doc 2@ type ."  */" cr
                    741:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr
1.30      pazsan    742:     \ debugging
                    743:     ." {" cr
1.66      anton     744:     print-declarations
1.53      anton     745:     inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
1.51      anton     746:     data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
                    747:     fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
                    748:     return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1.30      pazsan    749:     flush-tos
                    750:     fetches
                    751:     stack-pointer-updates
1.49      anton     752:     fp-stack   stack-used? IF ." *FP=fp;" cr THEN
1.30      pazsan    753:     ." {" cr
1.63      anton     754:     ." #line " c-line @ . quote c-filename 2@ type quote cr
1.69      anton     755:     prim prim-c-code 2@ type
1.30      pazsan    756:     ." }" cr
                    757:     stores
                    758:     fill-tos
1.44      jwilke    759:     ." return (sp);" cr
1.30      pazsan    760:     ." }" cr
                    761:     cr ;
                    762: 
1.43      jwilke    763: : output-label ( -- )  
1.69      anton     764:     ." (Label)&&I_" prim prim-c-name 2@ type ." ," cr ;
1.1       anton     765: 
1.43      jwilke    766: : output-alias ( -- ) 
1.69      anton     767:     ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1.1       anton     768: 
1.43      jwilke    769: : output-forth ( -- )  
1.69      anton     770:     prim prim-forth-code @ 0=
1.30      pazsan    771:     IF         \ output-alias
1.28      jwilke    772:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                    773:        \ JAW
1.69      anton     774:     ELSE  ." : " prim prim-name 2@ type ."   ( "
                    775:        prim prim-stack-string 2@ type ." )" cr
                    776:        prim prim-forth-code 2@ type cr
1.30      pazsan    777:     THEN ;
1.10      anton     778: 
1.17      anton     779: : output-tag-file ( -- )
                    780:     name-filename 2@ last-name-filename 2@ compare if
                    781:        name-filename 2@ last-name-filename 2!
                    782:        #ff emit cr
                    783:        name-filename 2@ type
                    784:        ." ,0" cr
                    785:     endif ;
                    786: 
                    787: : output-tag ( -- )
                    788:     output-tag-file
1.69      anton     789:     prim prim-name 2@ 1+ type
1.17      anton     790:     127 emit
1.69      anton     791:     space prim prim-name 2@ type space
1.17      anton     792:     1 emit
                    793:     name-line @ 0 .r
                    794:     ." ,0" cr ;
                    795: 
1.10      anton     796: [IFDEF] documentation
                    797: : register-doc ( -- )
1.82      anton     798:     prim prim-name 2@ documentation ['] create insert-wordlist
1.69      anton     799:     prim prim-name 2@ 2,
                    800:     prim prim-stack-string 2@ condition-stack-effect 2,
                    801:     prim prim-wordset 2@ 2,
                    802:     prim prim-c-name 2@ condition-pronounciation 2,
1.82      anton     803:     prim prim-doc 2@ 2, ;
1.10      anton     804: [THEN]
1.67      anton     805: 
                    806: 
1.69      anton     807: \ combining instructions
                    808: 
                    809: \ The input should look like this:
                    810: 
                    811: \ lit_+ = lit +
                    812: 
                    813: \ The output should look like this:
                    814: 
                    815: \  I_lit_+:
                    816: \  {
                    817: \  DEF_CA
                    818: \  Cell _x_ip0;
                    819: \  Cell _x_sp0;
                    820: \  Cell _x_sp1;
                    821: \  NEXT_P0;
                    822: \  _x_ip0 = (Cell) IPTOS;
                    823: \  _x_sp0 = (Cell) spTOS;
                    824: \  INC_IP(1);
                    825: \  /* sp += 0; */
                    826: \  /* lit ( #w -- w ) */
                    827: \  /*  */
                    828: \  NAME("lit")
                    829: \  {
                    830: \  Cell w;
                    831: \  w = (Cell) _x_ip0;
                    832: \  #ifdef VM_DEBUG
                    833: \  if (vm_debug) {
                    834: \  fputs(" w=", vm_out); printarg_w (w);
                    835: \  fputc('\n', vm_out);
                    836: \  }
                    837: \  #endif
                    838: \  {
                    839: \  #line 136 "./prim"
                    840: \  }
                    841: \  _x_sp1 = (Cell)w;
                    842: \  }
                    843: \  I_plus:     /* + ( n1 n2 -- n ) */
                    844: \  /*  */
                    845: \  NAME("+")
                    846: \  {
                    847: \  DEF_CA
                    848: \  Cell n1;
                    849: \  Cell n2;
                    850: \  Cell n;
                    851: \  NEXT_P0;
                    852: \  n1 = (Cell) _x_sp0;
                    853: \  n2 = (Cell) _x_sp1;
                    854: \  #ifdef VM_DEBUG
                    855: \  if (vm_debug) {
                    856: \  fputs(" n1=", vm_out); printarg_n (n1);
                    857: \  fputs(" n2=", vm_out); printarg_n (n2);
                    858: \  fputc('\n', vm_out);
                    859: \  }
                    860: \  #endif
                    861: \  {
                    862: \  #line 516 "./prim"
                    863: \  n = n1+n2;
                    864: \  }
                    865: \  NEXT_P1;
                    866: \  _x_sp0 = (Cell)n;
                    867: \  NEXT_P2;
                    868: \  }
                    869: \  NEXT_P1;
                    870: \  spTOS = (Cell)_x_sp0;
                    871: \  NEXT_P2;
                    872: 
1.71      anton     873: : init-combined ( -- )
1.79      anton     874:     prim to combined
1.71      anton     875:     0 num-combined !
                    876:     current-depth max-stacks cells erase
1.72      anton     877:     max-depth     max-stacks cells erase
                    878:     min-depth     max-stacks cells erase
                    879:     prim prim-effect-in  prim prim-effect-in-end  !
                    880:     prim prim-effect-out prim prim-effect-out-end ! ;
1.71      anton     881: 
                    882: : max! ( n addr -- )
                    883:     tuck @ max swap ! ;
                    884: 
1.72      anton     885: : min! ( n addr -- )
                    886:     tuck @ min swap ! ;
                    887: 
1.71      anton     888: : add-depths { p -- }
                    889:     \ combine stack effect of p with *-depths
                    890:     max-stacks 0 ?do
1.72      anton     891:        current-depth i th @
                    892:        p prim-stacks-in  i th @ +
                    893:        dup max-depth i th max!
                    894:        p prim-stacks-out i th @ -
                    895:        dup min-depth i th min!
                    896:        current-depth i th !
1.71      anton     897:     loop ;
                    898: 
                    899: : add-prim ( addr u -- )
                    900:     \ add primitive given by "addr u" to combined-prims
                    901:     primitives search-wordlist s" unknown primitive" ?print-error
                    902:     execute { p }
1.72      anton     903:     p combined-prims num-combined @ th !
1.71      anton     904:     1 num-combined +!
                    905:     p add-depths ;
                    906: 
                    907: : compute-effects { q -- }
                    908:     \ compute the stack effects of q from the depths
                    909:     max-stacks 0 ?do
1.72      anton     910:        max-depth i th @ dup
                    911:        q prim-stacks-in i th !
                    912:        current-depth i th @ -
                    913:        q prim-stacks-out i th !
                    914:     loop ;
                    915: 
                    916: : make-effect-items { stack# items effect-endp -- }
                    917:     \ effect-endp points to a pointer to the end of the current item-array
                    918:     \ and has to be updated
                    919:     stacks stack# th @ { stack }
                    920:     items 0 +do
                    921:        effect-endp @ { item }
                    922:        i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
                    923:        item item-name 2!
                    924:        stack item item-stack !
1.74      anton     925:        stack stack-type @ item item-type !
1.72      anton     926:        i item item-offset !
                    927:        item item-first on
                    928:        item% %size effect-endp +!
                    929:     loop ;
                    930: 
                    931: : init-effects { q -- }
                    932:     \ initialize effects field for FETCHES and STORES
                    933:     max-stacks 0 ?do
                    934:        i q prim-stacks-in  i th @ q prim-effect-in-end  make-effect-items
                    935:        i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1.71      anton     936:     loop ;
                    937: 
                    938: : process-combined ( -- )
1.81      anton     939:     combined combined-prims num-combined @ cells
1.82      anton     940:     combinations ['] constant insert-wordlist
1.86      anton     941:     combined-prims num-combined @ 1- th ( last-part )
                    942:     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1.72      anton     943:     prim compute-effects
                    944:     prim init-effects
                    945:     output-combined perform ;
                    946: 
                    947: \ C output
                    948: 
                    949: : print-item { n stack -- }
                    950:     \ print nth stack item name
1.79      anton     951:     stack stack-type @ type-c-name 2@ type space
                    952:     ." _" stack stack-pointer 2@ type n 0 .r ;
1.72      anton     953: 
                    954: : print-declarations-combined ( -- )
                    955:     max-stacks 0 ?do
                    956:        max-depth i th @ min-depth i th @ - 0 +do
                    957:            i stacks j th @ print-item ." ;" cr
                    958:        loop
                    959:     loop ;
1.79      anton     960: 
                    961: : part-fetches ( -- )
                    962:     fetches ;
                    963: 
                    964: : part-output-c-tail ( -- )
1.91    ! anton     965:     print-debug-results
1.85      anton     966:     stores ;
                    967: 
                    968: : output-combined-tail ( -- )
                    969:     part-output-c-tail
                    970:     prim >r combined to prim
                    971:     in-part @ >r in-part off
                    972:     output-c-tail
                    973:     r> in-part ! r> to prim ;
1.79      anton     974: 
                    975: : output-part ( p -- )
                    976:     to prim
                    977:     ." /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                    978:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
                    979:     ." {" cr
                    980:     print-declarations
                    981:     part-fetches
                    982:     print-debug-args
                    983:     prim add-depths \ !! right place?
                    984:     ." {" cr
                    985:     ." #line " c-line @ . quote c-filename 2@ type quote cr
1.85      anton     986:     prim prim-c-code 2@ ['] output-combined-tail type-c-code
1.79      anton     987:     ." }" cr
                    988:     part-output-c-tail
                    989:     ." }" cr ;
                    990: 
1.74      anton     991: : output-parts ( -- )
1.79      anton     992:     prim >r in-part on
                    993:     current-depth max-stacks cells erase
1.74      anton     994:     num-combined @ 0 +do
1.79      anton     995:        combined-prims i th @ output-part
1.74      anton     996:     loop
1.79      anton     997:     in-part off
1.74      anton     998:     r> to prim ;
                    999: 
1.72      anton    1000: : output-c-combined ( -- )
                   1001:     print-entry cr
1.74      anton    1002:     \ debugging messages just in parts
1.72      anton    1003:     ." {" cr
                   1004:     ." DEF_CA" cr
                   1005:     print-declarations-combined
                   1006:     ." NEXT_P0;" cr
                   1007:     flush-tos
                   1008:     fetches
1.74      anton    1009:     \ print-debug-args
                   1010:     stack-pointer-updates
                   1011:     output-parts
                   1012:     output-c-tail
                   1013:     ." }" cr
                   1014:     cr ;
1.72      anton    1015: 
                   1016: : output-forth-combined ( -- )
1.81      anton    1017: ;
                   1018: 
                   1019: 
1.83      anton    1020: \ peephole optimization rules
1.81      anton    1021: 
                   1022: \ in order for this to work as intended, shorter combinations for each
                   1023: \ length must be present, and the longer combinations must follow
                   1024: \ shorter ones (this restriction may go away in the future).
                   1025:   
1.83      anton    1026: : output-peephole ( -- )
1.81      anton    1027:     combined-prims num-combined @ 1- cells combinations search-wordlist
                   1028:     s" the prefix for this combination must be defined earlier" ?print-error
1.82      anton    1029:     ." {"
                   1030:     execute prim-num @ 5 .r ." ,"
                   1031:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
                   1032:     combined prim-num @ 5 .r ." }, /* "
                   1033:     combined prim-c-name 2@ type ."  */"
                   1034:     cr ;
                   1035: 
1.90      pazsan   1036: : output-forth-peephole ( -- )
                   1037:     combined-prims num-combined @ 1- cells combinations search-wordlist
                   1038:     s" the prefix for this combination must be defined earlier" ?print-error
                   1039:     execute prim-num @ 5 .r
                   1040:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r
                   1041:     combined prim-num @ 5 .r ."  prim, \ "
                   1042:     combined prim-c-name 2@ type
                   1043:     cr ;
                   1044: 
1.69      anton    1045: 
1.67      anton    1046: \ the parser
                   1047: 
                   1048: eof-char max-member \ the whole character set + EOF
                   1049: 
                   1050: : getinput ( -- n )
                   1051:  rawinput @ endrawinput @ =
                   1052:  if
                   1053:    eof-char
                   1054:  else
                   1055:    cookedinput @ c@
                   1056:  endif ;
                   1057: 
                   1058: :noname ( n -- )
                   1059:  dup bl > if
                   1060:   emit space
                   1061:  else
                   1062:   .
                   1063:  endif ;
                   1064: print-token !
                   1065: 
                   1066: : testchar? ( set -- f )
                   1067:  getinput member? ;
                   1068: ' testchar? test-vector !
                   1069: 
                   1070: : checksyncline ( -- )
                   1071:     \ when input points to a newline, check if the next line is a
                   1072:     \ sync line.  If it is, perform the appropriate actions.
                   1073:     rawinput @ >r
                   1074:     s" #line " r@ over compare 0<> if
                   1075:        rdrop 1 line +! EXIT
                   1076:     endif
                   1077:     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
                   1078:     dup c@ bl = if
1.81      anton    1079:        char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
1.67      anton    1080:        char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
                   1081:        char+
                   1082:     endif
1.81      anton    1083:     dup c@ nl-char <> 0= s" sync line syntax" ?print-error
1.67      anton    1084:     skipsynclines @ if
                   1085:        dup char+ rawinput !
                   1086:        rawinput @ c@ cookedinput @ c!
                   1087:     endif
                   1088:     drop ;
                   1089: 
                   1090: : ?nextchar ( f -- )
1.71      anton    1091:     s" syntax error, wrong char" ?print-error
1.67      anton    1092:     rawinput @ endrawinput @ <> if
                   1093:        rawinput @ c@
                   1094:        1 chars rawinput +!
                   1095:        1 chars cookedinput +!
                   1096:        nl-char = if
                   1097:            checksyncline
                   1098:            rawinput @ line-start !
                   1099:        endif
                   1100:        rawinput @ c@ cookedinput @ c!
                   1101:     endif ;
                   1102: 
                   1103: : charclass ( set "name" -- )
                   1104:  ['] ?nextchar terminal ;
                   1105: 
                   1106: : .. ( c1 c2 -- set )
                   1107:  ( creates a set that includes the characters c, c1<=c<=c2 )
                   1108:  empty copy-set
                   1109:  swap 1+ rot do
                   1110:   i over add-member
                   1111:  loop ;
                   1112: 
                   1113: : ` ( -- terminal ) ( use: ` c )
                   1114:  ( creates anonymous terminal for the character c )
                   1115:  char singleton ['] ?nextchar make-terminal ;
                   1116: 
                   1117: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
                   1118: char 0 char 9 ..                                       charclass digit
                   1119: bl singleton tab-char over add-member                  charclass white
                   1120: nl-char singleton eof-char over add-member complement  charclass nonl
                   1121: nl-char singleton eof-char over add-member
                   1122:     char : over add-member complement                   charclass nocolonnl
                   1123: bl 1+ maxchar .. char \ singleton complement intersection
                   1124:                                                         charclass nowhitebq
                   1125: bl 1+ maxchar ..                                        charclass nowhite
                   1126: char " singleton eof-char over add-member complement   charclass noquote
                   1127: nl-char singleton                                      charclass nl
                   1128: eof-char singleton                                     charclass eof
1.79      anton    1129: nl-char singleton eof-char over add-member             charclass nleof
1.67      anton    1130: 
                   1131: (( letter (( letter || digit )) **
                   1132: )) <- c-ident ( -- )
                   1133: 
                   1134: (( ` # ?? (( letter || digit || ` : )) **
                   1135: )) <- stack-ident ( -- )
                   1136: 
                   1137: (( nowhitebq nowhite ** ))
                   1138: <- forth-ident ( -- )
                   1139: 
                   1140: Variable forth-flag
                   1141: Variable c-flag
                   1142: 
                   1143: (( (( ` e || ` E )) {{ start }} nonl ** 
                   1144:    {{ end evaluate }}
                   1145: )) <- eval-comment ( ... -- ... )
                   1146: 
                   1147: (( (( ` f || ` F )) {{ start }} nonl ** 
                   1148:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
                   1149: )) <- forth-comment ( -- )
                   1150: 
                   1151: (( (( ` c || ` C )) {{ start }} nonl ** 
                   1152:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
                   1153: )) <- c-comment ( -- )
                   1154: 
                   1155: (( ` - nonl ** {{ 
                   1156:        forth-flag @ IF ." [ELSE]" cr THEN
                   1157:        c-flag @ IF ." #else" cr THEN }}
                   1158: )) <- else-comment
                   1159: 
                   1160: (( ` + {{ start }} nonl ** {{ end
                   1161:        dup
                   1162:        IF      c-flag @
                   1163:                IF    ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
                   1164:                THEN
                   1165:                forth-flag @
                   1166:                IF  ." has? " type ."  [IF]"  cr THEN
                   1167:        ELSE    2drop
                   1168:            c-flag @      IF  ." #endif"  cr THEN
                   1169:            forth-flag @  IF  ." [THEN]"  cr THEN
                   1170:        THEN }}
                   1171: )) <- if-comment
                   1172: 
                   1173: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
                   1174: 
1.79      anton    1175: (( ` \ comment-body nleof )) <- comment ( -- )
1.67      anton    1176: 
                   1177: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
                   1178: <- stack-items
                   1179: 
1.69      anton    1180: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
1.67      anton    1181:    ` - ` - white **
1.69      anton    1182:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1.67      anton    1183: )) <- stack-effect ( -- )
                   1184: 
1.71      anton    1185: (( {{ prim create-prim }}
1.69      anton    1186:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
                   1187:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
                   1188:       (( {{ start }}  c-ident {{ end prim prim-c-name 2! }} )) ??
1.79      anton    1189:    )) ??  nleof
                   1190:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
                   1191:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl **  nleof white ** )) ** {{ end prim prim-c-code 2! skipsynclines on }}
                   1192:    (( ` :  white ** nleof
                   1193:       {{ start }} (( nonl ++  nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1.81      anton    1194:    )) ?? {{ process-simple }}
1.79      anton    1195:    nleof
1.69      anton    1196: )) <- simple-primitive ( -- )
                   1197: 
1.71      anton    1198: (( {{ init-combined }}
1.89      anton    1199:    ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1.79      anton    1200:    nleof {{ process-combined }}
1.69      anton    1201: )) <- combined-primitive
                   1202: 
1.79      anton    1203: (( {{ make-prim to prim 0 to combined
1.69      anton    1204:       line @ name-line ! filename 2@ name-filename 2!
1.82      anton    1205:       function-number @ prim prim-num !
1.69      anton    1206:       start }} forth-ident {{ end 2dup prim prim-name 2! prim prim-c-name 2! }}  white ++
1.82      anton    1207:    (( simple-primitive || combined-primitive )) {{ 1 function-number +! }}
1.67      anton    1208: )) <- primitive ( -- )
                   1209: 
                   1210: (( (( comment || primitive || nl white ** )) ** eof ))
                   1211: parser primitives2something
                   1212: warnings @ [IF]
                   1213: .( parser generated ok ) cr
                   1214: [THEN]
                   1215: 
1.69      anton    1216: : primfilter ( addr u -- )
                   1217:     \ process the string at addr u
                   1218:     over dup rawinput ! dup line-start ! cookedinput !
                   1219:     + endrawinput !
                   1220:     checksyncline
                   1221:     primitives2something ;    
1.8       pazsan   1222: 
1.72      anton    1223: : process-file ( addr u xt-simple x-combined -- )
                   1224:     output-combined ! output !
1.61      anton    1225:     save-mem 2dup filename 2!
1.69      anton    1226:     slurp-file
1.17      anton    1227:     warnings @ if
                   1228:        ." ------------ CUT HERE -------------" cr  endif
1.69      anton    1229:     primfilter ;
1.30      pazsan   1230: 
1.72      anton    1231: \  : process      ( xt -- )
                   1232: \      bl word count rot
                   1233: \      process-file ;

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