Annotation of gforth/prims2x.fs, revision 1.94

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

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