Annotation of gforth/prims2x.fs, revision 1.141

1.16      anton       1: \ converts primitives to, e.g., C code 
                      2: 
1.134     anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 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.137     jwilke     54: \ for backwards compatibility, jaw
                     55: require compat/strcomp.fs
                     56: 
1.3       pazsan     57: warnings off
                     58: 
1.136     anton      59: \ redefinitions of kernel words not present in gforth-0.6.1
                     60: : latestxt lastcfa @ ;
                     61: : latest last @ ;
                     62: 
1.97      jwilke     63: [IFUNDEF] try
                     64: include startup.fs
                     65: [THEN]
                     66: 
1.49      anton      67: : struct% struct ; \ struct is redefined in gray
                     68: 
1.98      pazsan     69: warnings off
1.110     anton      70: \ warnings on
1.98      pazsan     71: 
1.39      jwilke     72: include ./gray.fs
1.133     anton      73: 128 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.111     anton      88: 2variable out-filename \ filename of the output file (for sync lines)
                     89: 0 0 out-filename 2!
1.25      pazsan     90: 2variable f-comment
                     91: 0 0 f-comment 2!
1.17      anton      92: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
1.111     anton      93: skipsynclines on
                     94: variable out-nls \ newlines in output (for output sync lines)
                     95: 0 out-nls !
1.112     anton      96: variable store-optimization \ use store optimization?
                     97: store-optimization off
                     98: 
1.116     anton      99: variable include-skipped-insts
                    100: \ does the threaded code for a combined instruction include the cells
                    101: \ for the component instructions (true) or only the cells for the
                    102: \ inline arguments (false)
                    103: include-skipped-insts off
1.1       anton     104: 
1.121     anton     105: variable immarg \ values for immediate arguments (to be used in IMM_ARG macros)
                    106: $12340000 immarg !
                    107: 
1.72      anton     108: : th ( addr1 n -- addr2 )
                    109:     cells + ;
                    110: 
                    111: : holds ( addr u -- )
                    112:     \ like HOLD, but for a string
                    113:     tuck + swap 0 +do
                    114:        1- dup c@ hold
                    115:     loop
                    116:     drop ;
1.71      anton     117: 
1.82      anton     118: : insert-wordlist { c-addr u wordlist xt -- }
1.81      anton     119:     \ adds name "addr u" to wordlist using defining word xt
                    120:     \ xt may cause additional stack effects
                    121:     get-current >r wordlist set-current
                    122:     c-addr u nextname xt execute
                    123:     r> set-current ;
                    124: 
1.1       anton     125: : start ( -- addr )
1.18      anton     126:  cookedinput @ ;
1.1       anton     127: 
                    128: : end ( addr -- addr u )
1.18      anton     129:  cookedinput @ over - ;
1.1       anton     130: 
1.71      anton     131: : print-error-line ( -- )
                    132:     \ print the current line and position
                    133:     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
                    134:     over - type cr
                    135:     line-start @ rawinput @ over - typewhite ." ^" cr ;
                    136: 
                    137: : ?print-error { f addr u -- }
                    138:     f ?not? if
                    139:        outfile-id >r try
                    140:            stderr to outfile-id
                    141:            filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
                    142:            print-error-line
                    143:            0
                    144:        recover endtry
                    145:        r> to outfile-id throw
1.111     anton     146:        1 (bye) \ abort
1.71      anton     147:     endif ;
                    148: 
1.63      anton     149: : quote ( -- )
                    150:     [char] " emit ;
                    151: 
1.111     anton     152: \ count output lines to generate sync lines for output
                    153: 
                    154: : count-nls ( addr u -- )
                    155:     bounds u+do
                    156:        i c@ nl-char = negate out-nls +!
                    157:     loop ;
                    158: 
                    159: :noname ( addr u -- )
                    160:     2dup count-nls
                    161:     defers type ;
                    162: is type
                    163: 
1.72      anton     164: variable output          \ xt ( -- ) of output word for simple primitives
                    165: variable output-combined \ xt ( -- ) of output word for combined primitives
1.1       anton     166: 
1.49      anton     167: struct%
1.71      anton     168:     cell%    field stack-number \ the number of this stack
1.49      anton     169:     cell% 2* field stack-pointer \ stackpointer name
1.74      anton     170:     cell%    field stack-type \ name for default type of stack items
1.53      anton     171:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
1.126     anton     172:     cell%    field stack-access-transform \ ( nitem -- index )
1.49      anton     173: end-struct stack%
                    174: 
1.53      anton     175: struct%
                    176:  cell% 2* field item-name   \ name, excluding stack prefixes
                    177:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
                    178:  cell%    field item-type   \ descriptor for the item type
                    179:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
1.66      anton     180:  cell%   field item-first  \ true if this is the first occurence of the item
1.53      anton     181: end-struct item%
                    182: 
                    183: struct%
                    184:     cell% 2* field type-c-name
                    185:     cell%    field type-stack \ default stack
                    186:     cell%    field type-size  \ size of type in stack items
                    187:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
                    188:     cell%    field type-store \ xt of store code generator ( item -- )
                    189: end-struct type%
                    190: 
1.72      anton     191: variable next-stack-number 0 next-stack-number !
                    192: create stacks max-stacks cells allot \ array of stacks
                    193: 
1.53      anton     194: : stack-in-index ( in-size item -- in-index )
                    195:     item-offset @ - 1- ;
                    196: 
                    197: : inst-in-index ( in-size item -- in-index )
                    198:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
                    199: 
1.92      anton     200: : make-stack ( addr-ptr u1 type "stack-name" -- )
                    201:     next-stack-number @ max-stacks < s" too many stacks" ?print-error
1.49      anton     202:     create stack% %allot >r
1.72      anton     203:     r@ stacks next-stack-number @ th !
1.92      anton     204:     next-stack-number @ r@ stack-number !
                    205:     1 next-stack-number +!
1.74      anton     206:     r@ stack-type !
1.53      anton     207:     save-mem r@ stack-pointer 2! 
1.126     anton     208:     ['] stack-in-index r@ stack-in-index-xt !
                    209:     ['] noop r@ stack-access-transform !
                    210:     rdrop ;
1.49      anton     211: 
1.92      anton     212: : map-stacks { xt -- }
1.118     anton     213:     \ perform xt for all stacks
                    214:     next-stack-number @ 0 +do
                    215:        stacks i th @ xt execute
                    216:     loop ;
                    217: 
                    218: : map-stacks1 { xt -- }
1.92      anton     219:     \ perform xt for all stacks except inst-stream
                    220:     next-stack-number @ 1 +do
                    221:        stacks i th @ xt execute
                    222:     loop ;
                    223: 
1.49      anton     224: \ stack items
                    225: 
                    226: : init-item ( addr u addr1 -- )
                    227:     \ initialize item at addr1 with name addr u
                    228:     \ !! remove stack prefix
                    229:     dup item% %size erase
                    230:     item-name 2! ;
                    231: 
1.64      anton     232: : map-items { addr end xt -- }
                    233:     \ perform xt for all items in array addr...end
                    234:     end addr ?do
                    235:        i xt execute
                    236:     item% %size +loop ;
                    237: 
1.77      anton     238: \ types
                    239: 
                    240: : print-type-prefix ( type -- )
                    241:     body> >head name>string type ;
                    242: 
1.49      anton     243: \ various variables for storing stuff of one primitive
1.1       anton     244: 
1.69      anton     245: struct%
                    246:     cell% 2* field prim-name
                    247:     cell% 2* field prim-wordset
                    248:     cell% 2* field prim-c-name
                    249:     cell% 2* field prim-doc
                    250:     cell% 2* field prim-c-code
                    251:     cell% 2* field prim-forth-code
                    252:     cell% 2* field prim-stack-string
1.82      anton     253:     cell%    field prim-num            \ ordinal number
1.75      anton     254:     cell%    field prim-items-wordlist \ unique items
1.69      anton     255:     item% max-effect * field prim-effect-in
                    256:     item% max-effect * field prim-effect-out
                    257:     cell%    field prim-effect-in-end
                    258:     cell%    field prim-effect-out-end
1.71      anton     259:     cell% max-stacks * field prim-stacks-in  \ number of in items per stack
                    260:     cell% max-stacks * field prim-stacks-out \ number of out items per stack
1.69      anton     261: end-struct prim%
                    262: 
1.70      anton     263: : make-prim ( -- prim )
                    264:     prim% %alloc { p }
                    265:     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
                    266:     p ;
                    267: 
1.79      anton     268: 0 value prim     \ in combined prims either combined or a part
                    269: 0 value combined \ in combined prims the combined prim
                    270: variable in-part \ true if processing a part
                    271:  in-part off
                    272: 
1.118     anton     273: : prim-context ( ... p xt -- ... )
                    274:     \ execute xt with prim set to p
                    275:     prim >r
                    276:     swap to prim
                    277:     catch
                    278:     r> to prim
                    279:     throw ;
                    280: 
1.79      anton     281: 1000 constant max-combined
                    282: create combined-prims max-combined cells allot
                    283: variable num-combined
1.118     anton     284: variable part-num \ current part number during process-combined
1.79      anton     285: 
1.114     anton     286: : map-combined { xt -- }
                    287:     \ perform xt for all components of the current combined instruction
                    288:     num-combined @ 0 +do
                    289:        combined-prims i th @ xt execute
                    290:     loop ;
                    291: 
1.81      anton     292: table constant combinations
                    293:   \ the keys are the sequences of pointers to primitives
                    294: 
1.79      anton     295: create current-depth max-stacks cells allot
                    296: create max-depth     max-stacks cells allot
                    297: create min-depth     max-stacks cells allot
1.69      anton     298: 
1.118     anton     299: create sp-update-in max-stacks cells allot
                    300: \ where max-depth occured the first time
                    301: create max-depths max-stacks max-combined 1+ * cells allot
1.119     anton     302: \ maximum depth at start of each part: array[parts] of array[stack]
                    303: create max-back-depths max-stacks max-combined 1+ * cells allot
                    304: \ maximun depth from end of the combination to the start of the each part
1.118     anton     305: 
                    306: : s-c-max-depth ( nstack ncomponent -- addr )
                    307:     max-stacks * + cells max-depths + ;
                    308: 
1.119     anton     309: : s-c-max-back-depth ( nstack ncomponent -- addr )
                    310:     max-stacks * + cells max-back-depths + ;
                    311: 
1.71      anton     312: wordlist constant primitives
                    313: 
                    314: : create-prim ( prim -- )
1.82      anton     315:     dup prim-name 2@ primitives ['] constant insert-wordlist ;
1.71      anton     316: 
                    317: : stack-in ( stack -- addr )
                    318:     \ address of number of stack items in effect in
                    319:     stack-number @ cells prim prim-stacks-in + ;
                    320: 
                    321: : stack-out ( stack -- addr )
                    322:     \ address of number of stack items in effect out
                    323:     stack-number @ cells prim prim-stacks-out + ;
                    324: 
1.69      anton     325: \ global vars
1.17      anton     326: variable c-line
                    327: 2variable c-filename
                    328: variable name-line
                    329: 2variable name-filename
                    330: 2variable last-name-filename
1.30      pazsan    331: Variable function-number 0 function-number !
1.140     pazsan    332: Variable function-old 0 function-old !
                    333: : function-diff ( n -- )
                    334:     ." GROUPADD(" function-number @ function-old @ - 0 .r ." )" cr
                    335:     function-number @ function-old ! ;
                    336: : forth-fdiff ( -- )
                    337:     function-number @ function-old @ - 0 .r ."  groupadd" cr
                    338:     function-number @ function-old ! ;
1.1       anton     339: 
                    340: \ a few more set ops
                    341: 
                    342: : bit-equivalent ( w1 w2 -- w3 )
                    343:  xor invert ;
                    344: 
                    345: : complement ( set1 -- set2 )
                    346:  empty ['] bit-equivalent binary-set-operation ;
                    347: 
1.121     anton     348: \ forward declaration for inst-stream (breaks cycle in definitions)
                    349: defer inst-stream-f ( -- stack )
                    350: 
1.80      anton     351: \ stack access stuff
1.79      anton     352: 
1.126     anton     353: : normal-stack-access0 { n stack -- }
                    354:     n stack stack-access-transform @ execute ." [" 0 .r ." ]" ;
                    355:     
                    356: : normal-stack-access1 { n stack -- }
                    357:     stack stack-pointer 2@ type
                    358:     n if
                    359:        n stack normal-stack-access0
1.49      anton     360:     else
1.126     anton     361:        ." TOS"
1.49      anton     362:     endif ;
1.1       anton     363: 
1.121     anton     364: : normal-stack-access ( n stack -- )
                    365:     dup inst-stream-f = if
                    366:        ." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )"
                    367:        1 immarg +!
                    368:     else
                    369:        normal-stack-access1
                    370:     endif ;
1.80      anton     371: 
1.118     anton     372: : stack-depth { stack -- n }
                    373:     current-depth stack stack-number @ th @ ;
                    374: 
1.79      anton     375: : part-stack-access { n stack -- }
1.80      anton     376:     \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
1.79      anton     377:     ." _" stack stack-pointer 2@ type
                    378:     stack stack-number @ { stack# }
1.118     anton     379:     stack stack-depth n + { access-depth }
1.80      anton     380:     stack inst-stream-f = if
                    381:        access-depth
                    382:     else
                    383:        combined prim-stacks-in stack# th @
                    384:        assert( dup max-depth stack# th @ = )
                    385:        access-depth - 1-
                    386:     endif
1.79      anton     387:     0 .r ;
                    388: 
1.118     anton     389: : part-stack-read { n stack -- }
                    390:     stack stack-depth n + ( ndepth )
                    391:     stack stack-number @ part-num @ s-c-max-depth @
                    392: \    max-depth stack stack-number @ th @ ( ndepth nmaxdepth )
                    393:     over <= if ( ndepth ) \ load from memory
                    394:        stack normal-stack-access
                    395:     else
                    396:        drop n stack part-stack-access
                    397:     endif ;
                    398: 
1.119     anton     399: : stack-diff ( stack -- n )
                    400:     \ in-out
                    401:     dup stack-in @ swap stack-out @ - ;
                    402: 
                    403: : part-stack-write { n stack -- }
                    404:     stack stack-depth n +
                    405:     stack stack-number @ part-num @ s-c-max-back-depth @
                    406:     over <= if ( ndepth )
                    407:        stack combined ['] stack-diff prim-context -
                    408:        stack normal-stack-access
                    409:     else
                    410:        drop n stack part-stack-access
                    411:     endif ;
1.118     anton     412: 
                    413: : stack-read ( n stack -- )
                    414:     \ print a stack access at index n of stack
                    415:     in-part @ if
                    416:        part-stack-read
                    417:     else
                    418:        normal-stack-access
                    419:     endif ;
                    420: 
                    421: : stack-write ( n stack -- )
1.79      anton     422:     \ print a stack access at index n of stack
                    423:     in-part @ if
1.118     anton     424:        part-stack-write
1.79      anton     425:     else
                    426:        normal-stack-access
                    427:     endif ;
                    428: 
1.53      anton     429: : item-in-index { item -- n }
1.49      anton     430:     \ n is the index of item (in the in-effect)
1.53      anton     431:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
                    432:     item r> stack-in-index-xt @ execute ;
1.1       anton     433: 
1.78      anton     434: : item-stack-type-name ( item -- addr u )
                    435:     item-stack @ stack-type @ type-c-name 2@ ;
                    436: 
1.1       anton     437: : fetch-single ( item -- )
1.106     anton     438:     \ fetch a single stack item from its stack
                    439:     >r
                    440:     ." vm_" r@ item-stack-type-name type
                    441:     ." 2" r@ item-type @ print-type-prefix ." ("
1.118     anton     442:     r@ item-in-index r@ item-stack @ stack-read ." ,"
1.106     anton     443:     r@ item-name 2@ type
                    444:     ." );" cr
                    445:     rdrop ; 
1.1       anton     446: 
                    447: : fetch-double ( item -- )
1.106     anton     448:     \ fetch a double stack item from its stack
                    449:     >r
                    450:     ." vm_two"
                    451:     r@ item-stack-type-name type ." 2"
                    452:     r@ item-type @ print-type-prefix ." ("
1.118     anton     453:     r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-read
                    454:     ." , "                      -1 under+ ." (Cell)" stack-read
1.106     anton     455:     ." , " r@ item-name 2@ type
                    456:     ." )" cr
                    457:     rdrop ;
1.1       anton     458: 
1.49      anton     459: : same-as-in? ( item -- f )
                    460:  \ f is true iff the offset and stack of item is the same as on input
1.1       anton     461:  >r
1.74      anton     462:  r@ item-first @ if
                    463:      rdrop false exit
                    464:  endif
1.75      anton     465:  r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
1.1       anton     466:  execute @
                    467:  dup r@ =
                    468:  if \ item first appeared in output
                    469:    drop false
                    470:  else
1.49      anton     471:    dup  item-stack  @ r@ item-stack  @ = 
                    472:    swap item-offset @ r@ item-offset @ = and
1.1       anton     473:  endif
                    474:  rdrop ;
                    475: 
1.49      anton     476: : item-out-index ( item -- n )
                    477:     \ n is the index of item (in the in-effect)
                    478:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
1.31      pazsan    479: 
1.1       anton     480: : really-store-single ( item -- )
1.106     anton     481:     >r
                    482:     ." vm_"
                    483:     r@ item-type @ print-type-prefix ." 2"
                    484:     r@ item-stack-type-name type ." ("
                    485:     r@ item-name 2@ type ." ,"
1.118     anton     486:     r@ item-out-index r@ item-stack @ stack-write ." );"
1.106     anton     487:     rdrop ;
1.1       anton     488: 
                    489: : store-single ( item -- )
1.112     anton     490:     >r
1.118     anton     491:     store-optimization @ in-part @ 0= and r@ same-as-in? and if
1.112     anton     492:        r@ item-in-index 0= r@ item-out-index 0= xor if
                    493:            ." IF_" r@ item-stack @ stack-pointer 2@ type
                    494:            ." TOS(" r@ really-store-single ." );" cr
                    495:        endif
                    496:     else
                    497:        r@ really-store-single cr
                    498:     endif
                    499:     rdrop ;
1.1       anton     500: 
                    501: : store-double ( item -- )
                    502: \ !! store optimization is not performed, because it is not yet needed
                    503:  >r
1.78      anton     504:  ." vm_"
                    505:  r@ item-type @ print-type-prefix ." 2two"
                    506:  r@ item-stack-type-name type ." ("
                    507:  r@ item-name 2@ type ." , "
1.118     anton     508:  r@ item-out-index r@ item-stack @ 2dup stack-write
                    509:  ." , "                       -1 under+ stack-write
1.106     anton     510:  ." )" cr
1.1       anton     511:  rdrop ;
                    512: 
1.54      anton     513: : single ( -- xt1 xt2 n )
                    514:     ['] fetch-single ['] store-single 1 ;
1.1       anton     515: 
1.54      anton     516: : double ( -- xt1 xt2 n )
                    517:     ['] fetch-double ['] store-double 2 ;
1.1       anton     518: 
                    519: : s, ( addr u -- )
                    520: \ allocate a string
                    521:  here swap dup allot move ;
                    522: 
1.50      anton     523: wordlist constant prefixes
                    524: 
                    525: : declare ( addr "name" -- )
                    526: \ remember that there is a stack item at addr called name
                    527:  create , ;
                    528: 
                    529: : !default ( w addr -- )
                    530:     dup @ if
                    531:        2drop \ leave nonzero alone
                    532:     else
                    533:        !
                    534:     endif ;
                    535: 
                    536: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
1.49      anton     537:     \ describes a type
                    538:     \ addr u specifies the C type name
                    539:     \ stack effect entries of the type start with prefix
                    540:     create type% %allot >r
                    541:     addr u save-mem r@ type-c-name 2!
                    542:     xt1   r@ type-fetch !
                    543:     xt2   r@ type-store !
                    544:     n     r@ type-size !
                    545:     stack r@ type-stack !
                    546:     rdrop ;
1.1       anton     547: 
1.105     anton     548: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
1.94      anton     549:     get-current >r prefixes set-current
                    550:     create-type r> set-current
1.50      anton     551: does> ( item -- )
                    552:     \ initialize item
                    553:     { item typ }
                    554:     typ item item-type !
                    555:     typ type-stack @ item item-stack !default
1.75      anton     556:     item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
1.66      anton     557:        item item-name 2@ nextname item declare
                    558:        item item-first on
                    559:        \ typ type-c-name 2@ type space type  ." ;" cr
1.50      anton     560:     else
                    561:        drop
1.66      anton     562:        item item-first off
1.50      anton     563:     endif ;
                    564: 
                    565: : execute-prefix ( item addr1 u1 -- )
                    566:     \ execute the word ( item -- ) associated with the longest prefix
                    567:     \ of addr1 u1
                    568:     0 swap ?do
                    569:        dup i prefixes search-wordlist
                    570:        if \ ok, we have the type ( item addr1 xt )
                    571:            nip execute
                    572:            UNLOOP EXIT
                    573:        endif
                    574:        -1 s+loop
                    575:     \ we did not find a type, abort
1.81      anton     576:     false s" unknown prefix" ?print-error ;
1.1       anton     577: 
                    578: : declaration ( item -- )
1.50      anton     579:     dup item-name 2@ execute-prefix ;
1.1       anton     580: 
1.64      anton     581: : declaration-list ( addr1 addr2 -- )
                    582:     ['] declaration map-items ;
                    583: 
                    584: : declarations ( -- )
1.75      anton     585:  wordlist dup prim prim-items-wordlist ! set-current
1.69      anton     586:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
                    587:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
1.64      anton     588: 
1.66      anton     589: : print-declaration { item -- }
                    590:     item item-first @ if
                    591:        item item-type @ type-c-name 2@ type space
                    592:        item item-name 2@ type ." ;" cr
                    593:     endif ;
                    594: 
                    595: : print-declarations ( -- )
1.69      anton     596:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
                    597:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
1.66      anton     598:     
1.51      anton     599: : stack-prefix ( stack "prefix" -- )
1.94      anton     600:     get-current >r prefixes set-current
1.51      anton     601:     name tuck nextname create ( stack length ) 2,
1.94      anton     602:     r> set-current
1.51      anton     603: does> ( item -- )
                    604:     2@ { item stack prefix-length }
                    605:     item item-name 2@ prefix-length /string item item-name 2!
                    606:     stack item item-stack !
                    607:     item declaration ;
1.73      anton     608: 
1.74      anton     609: \ types pointed to by stacks for use in combined prims
1.83      anton     610: \ !! output-c-combined shouldn't use these names!
1.92      anton     611: : stack-type-name ( addr u "name" -- )
                    612:     single 0 create-type ;
                    613: 
1.93      anton     614: wordlist constant type-names \ this is here just to meet the requirement
                    615:                     \ that a type be a word; it is never used for lookup
1.83      anton     616: 
1.93      anton     617: : stack ( "name" "stack-pointer" "type" -- )
                    618:     \ define stack
                    619:     name { d: stack-name }
                    620:     name { d: stack-pointer }
                    621:     name { d: stack-type }
                    622:     get-current type-names set-current
                    623:     stack-type 2dup nextname stack-type-name
                    624:     set-current
1.135     anton     625:     stack-pointer latestxt >body stack-name nextname make-stack ;
1.93      anton     626: 
                    627: stack inst-stream IP Cell
1.73      anton     628: ' inst-in-index inst-stream stack-in-index-xt !
1.80      anton     629: ' inst-stream <is> inst-stream-f
1.73      anton     630: \ !! initialize stack-in and stack-out
1.1       anton     631: 
                    632: \ offset computation
                    633: \ the leftmost (i.e. deepest) item has offset 0
                    634: \ the rightmost item has the highest offset
                    635: 
1.49      anton     636: : compute-offset { item xt -- }
                    637:     \ xt specifies in/out; update stack-in/out and set item-offset
                    638:     item item-type @ type-size @
                    639:     item item-stack @ xt execute dup @ >r +!
                    640:     r> item item-offset ! ;
                    641: 
1.64      anton     642: : compute-offset-in ( addr1 addr2 -- )
                    643:     ['] stack-in compute-offset ;
                    644: 
                    645: : compute-offset-out ( addr1 addr2 -- )
                    646:     ['] stack-out compute-offset ;
1.49      anton     647: 
1.1       anton     648: : compute-offsets ( -- )
1.132     anton     649:     prim prim-stacks-in  max-stacks cells erase
                    650:     prim prim-stacks-out max-stacks cells erase
1.69      anton     651:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
                    652:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
1.81      anton     653:     inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
                    654: 
                    655: : process-simple ( -- )
                    656:     prim prim { W^ key } key cell
1.82      anton     657:     combinations ['] constant insert-wordlist
1.81      anton     658:     declarations compute-offsets
1.82      anton     659:     output @ execute ;
1.49      anton     660: 
                    661: : flush-a-tos { stack -- }
                    662:     stack stack-out @ 0<> stack stack-in @ 0= and
                    663:     if
                    664:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
1.126     anton     665:        2dup type 0 stack normal-stack-access0 ."  = " type ." TOS);" cr
1.49      anton     666:     endif ;
1.1       anton     667: 
                    668: : flush-tos ( -- )
1.118     anton     669:     ['] flush-a-tos map-stacks1 ;
1.49      anton     670: 
                    671: : fill-a-tos { stack -- }
                    672:     stack stack-out @ 0= stack stack-in @ 0<> and
                    673:     if
                    674:        ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
1.126     anton     675:        2dup type ." TOS = " type 0 stack normal-stack-access0 ." );" cr
1.49      anton     676:     endif ;
1.1       anton     677: 
                    678: : fill-tos ( -- )
1.53      anton     679:     \ !! inst-stream for prefetching?
1.118     anton     680:     ['] fill-a-tos map-stacks1 ;
1.49      anton     681: 
                    682: : fetch ( addr -- )
1.72      anton     683:     dup item-type @ type-fetch @ execute ;
1.1       anton     684: 
                    685: : fetches ( -- )
1.69      anton     686:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
1.49      anton     687: 
1.126     anton     688: : stack-update-transform ( n1 stack -- n2 )
                    689:     \ n2 is the number by which the stack pointer should be
                    690:     \ incremented to pop n1 items
                    691:     stack-access-transform @ dup >r execute
                    692:     0 r> execute - ;
                    693: 
1.49      anton     694: : stack-pointer-update { stack -- }
1.123     anton     695:     \ stacks grow downwards
1.119     anton     696:     stack stack-diff
1.49      anton     697:     ?dup-if \ this check is not necessary, gcc would do this for us
1.118     anton     698:        stack inst-stream = if
1.120     anton     699:            ." INC_IP(" 0 .r ." );" cr
1.118     anton     700:        else
1.126     anton     701:            stack stack-pointer 2@ type ."  += "
                    702:            stack stack-update-transform 0 .r ." ;" cr
1.118     anton     703:        endif
1.55      anton     704:     endif ;
                    705: 
1.1       anton     706: : stack-pointer-updates ( -- )
1.92      anton     707:     ['] stack-pointer-update map-stacks ;
1.1       anton     708: 
                    709: : store ( item -- )
                    710: \ f is true if the item should be stored
                    711: \ f is false if the store is probably not necessary
1.49      anton     712:  dup item-type @ type-store @ execute ;
1.1       anton     713: 
                    714: : stores ( -- )
1.69      anton     715:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
1.8       pazsan    716: 
1.91      anton     717: : print-debug-arg { item -- }
                    718:     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
                    719:     ." printarg_" item item-type @ print-type-prefix
                    720:     ." (" item item-name 2@ type ." );" cr ;
                    721:     
                    722: : print-debug-args ( -- )
                    723:     ." #ifdef VM_DEBUG" cr
                    724:     ." if (vm_debug) {" cr
                    725:     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
                    726: \    ." fputc('\n', vm_out);" cr
                    727:     ." }" cr
                    728:     ." #endif" cr ;
                    729: 
                    730: : print-debug-result { item -- }
                    731:     item item-first @ if
                    732:        item print-debug-arg
                    733:     endif ;
                    734: 
                    735: : print-debug-results ( -- )
                    736:     cr
                    737:     ." #ifdef VM_DEBUG" cr
                    738:     ." if (vm_debug) {" cr
                    739:     ." fputs(" quote ."  -- " quote ." , vm_out); "
                    740:     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
                    741:     ." fputc('\n', vm_out);" cr
                    742:     ." }" cr
                    743:     ." #endif" cr ;
                    744: 
1.86      anton     745: : output-super-end ( -- )
                    746:     prim prim-c-code 2@ s" SET_IP" search if
                    747:        ." SUPER_END;" cr
                    748:     endif
                    749:     2drop ;
                    750: 
1.124     anton     751: : output-nextp2 ( -- )
                    752:     ." NEXT_P2;" cr ;
                    753: 
                    754: variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL
                    755: ' output-nextp2 tail-nextp2 !
                    756: 
1.120     anton     757: : output-label2 ( -- )
1.121     anton     758:     ." LABEL2(" prim prim-c-name 2@ type ." )" cr
                    759:     ." NEXT_P2;" cr ;
1.120     anton     760: 
                    761: : output-c-tail1 { xt -- }
                    762:     \ the final part of the generated C code, with xt printing LABEL2 or not.
1.86      anton     763:     output-super-end
1.91      anton     764:     print-debug-results
1.120     anton     765:     ." NEXT_P1;" cr
1.52      anton     766:     stores
1.119     anton     767:     fill-tos 
1.121     anton     768:     xt execute ;
1.108     anton     769: 
1.120     anton     770: : output-c-tail1-no-stores { xt -- }
                    771:     \ the final part of the generated C code for combinations
                    772:     output-super-end
                    773:     ." NEXT_P1;" cr
1.119     anton     774:     fill-tos 
1.121     anton     775:     xt execute ;
1.120     anton     776: 
                    777: : output-c-tail ( -- )
1.124     anton     778:     tail-nextp2 @ output-c-tail1 ;
1.52      anton     779: 
1.108     anton     780: : output-c-tail2 ( -- )
1.120     anton     781:     ['] output-label2 output-c-tail1 ;
                    782: 
                    783: : output-c-tail-no-stores ( -- )
1.124     anton     784:     tail-nextp2 @ output-c-tail1-no-stores ;
1.119     anton     785: 
                    786: : output-c-tail2-no-stores ( -- )
1.120     anton     787:     ['] output-label2 output-c-tail1-no-stores ;
1.108     anton     788: 
1.85      anton     789: : type-c-code ( c-addr u xt -- )
1.109     anton     790:     \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
1.85      anton     791:     { xt }
1.111     anton     792:     ." {" cr
                    793:     ." #line " c-line @ . quote c-filename 2@ type quote cr
1.52      anton     794:     begin ( c-addr1 u1 )
1.109     anton     795:        2dup s" INST_TAIL;" search
1.52      anton     796:     while ( c-addr1 u1 c-addr3 u3 )
                    797:        2dup 2>r drop nip over - type
1.85      anton     798:        xt execute
1.109     anton     799:        2r> 10 /string
1.52      anton     800:        \ !! resync #line missing
                    801:     repeat
1.111     anton     802:     2drop type
                    803:     ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
                    804:     ." }" cr ;
1.63      anton     805: 
1.72      anton     806: : print-entry ( -- )
1.109     anton     807:     ." LABEL(" prim prim-c-name 2@ type ." )" ;
1.63      anton     808:     
1.43      jwilke    809: : output-c ( -- ) 
1.111     anton     810:     print-entry ."  /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                    811:     ." /* " prim prim-doc 2@ type ."  */" cr
                    812:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
                    813:     ." {" cr
                    814:     ." DEF_CA" cr
                    815:     print-declarations
                    816:     ." NEXT_P0;" cr
                    817:     flush-tos
                    818:     fetches
                    819:     print-debug-args
                    820:     stack-pointer-updates
                    821:     prim prim-c-code 2@ ['] output-c-tail type-c-code
                    822:     output-c-tail2
                    823:     ." }" cr
                    824:     cr
1.1       anton     825: ;
                    826: 
1.56      anton     827: : disasm-arg { item -- }
                    828:     item item-stack @ inst-stream = if
1.107     anton     829:        ." {" cr
                    830:        item print-declaration
                    831:        item fetch
                    832:        item print-debug-arg
                    833:        ." }" cr
1.56      anton     834:     endif ;
                    835: 
                    836: : disasm-args ( -- )
1.69      anton     837:     prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1.56      anton     838: 
                    839: : output-disasm ( -- )
                    840:     \ generate code for disassembling VM instructions
1.106     anton     841:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.69      anton     842:     ."   fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1.56      anton     843:     disasm-args
                    844:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1.91      anton     845:     ."   goto _endif_;" cr
                    846:     ." }" cr ;
1.56      anton     847: 
1.86      anton     848: : output-profile ( -- )
                    849:     \ generate code for postprocessing the VM block profile stuff
1.87      anton     850:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.104     anton     851:     ."   add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
1.86      anton     852:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
                    853:     prim prim-c-code 2@  s" SET_IP"    search nip nip
                    854:     prim prim-c-code 2@  s" SUPER_END" search nip nip or if
                    855:        ."   return;" cr
1.91      anton     856:     else
                    857:        ."   goto _endif_;" cr
1.86      anton     858:     endif
1.91      anton     859:     ." }" cr ;
1.86      anton     860: 
1.114     anton     861: : output-profile-part ( p )
                    862:     ."   add_inst(b, " quote
                    863:     prim-name 2@ type
                    864:     quote ." );" cr ;
                    865:     
1.104     anton     866: : output-profile-combined ( -- )
                    867:     \ generate code for postprocessing the VM block profile stuff
                    868:     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.114     anton     869:     ['] output-profile-part map-combined
1.104     anton     870:     ."   ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
                    871:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SET_IP"    search nip nip
                    872:     combined-prims num-combined @ 1- th @ prim-c-code 2@  s" SUPER_END" search nip nip or if
                    873:        ."   return;" cr
                    874:     else
                    875:        ."   goto _endif_;" cr
                    876:     endif
                    877:     ." }" cr ;
                    878: 
1.103     anton     879: : output-superend ( -- )
                    880:     \ output flag specifying whether the current word ends a dynamic superinst
                    881:     prim prim-c-code 2@  s" SET_IP"    search nip nip
                    882:     prim prim-c-code 2@  s" SUPER_END" search nip nip or 0<>
                    883:     prim prim-c-code 2@  s" SUPER_CONTINUE" search nip nip 0= and
                    884:     negate 0 .r ." , /* " prim prim-name 2@ type ."  */" cr ;
                    885: 
1.60      anton     886: : gen-arg-parm { item -- }
                    887:     item item-stack @ inst-stream = if
                    888:        ." , " item item-type @ type-c-name 2@ type space
                    889:        item item-name 2@ type
                    890:     endif ;
                    891: 
                    892: : gen-args-parm ( -- )
1.69      anton     893:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1.60      anton     894: 
                    895: : gen-arg-gen { item -- }
                    896:     item item-stack @ inst-stream = if
                    897:        ."   genarg_" item item-type @ print-type-prefix
                    898:         ." (ctp, " item item-name 2@ type ." );" cr
                    899:     endif ;
                    900: 
                    901: : gen-args-gen ( -- )
1.69      anton     902:     prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1.60      anton     903: 
                    904: : output-gen ( -- )
                    905:     \ generate C code for generating VM instructions
1.69      anton     906:     ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
1.60      anton     907:     ." {" cr
                    908:     ."   gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
                    909:     gen-args-gen
1.68      anton     910:     ." }" cr ;
1.60      anton     911: 
1.49      anton     912: : stack-used? { stack -- f }
                    913:     stack stack-in @ stack stack-out @ or 0<> ;
1.44      jwilke    914: 
1.30      pazsan    915: : output-funclabel ( -- )
1.69      anton     916:   ." &I_" prim prim-c-name 2@ type ." ," cr ;
1.30      pazsan    917: 
                    918: : output-forthname ( -- )
1.69      anton     919:   '" emit prim prim-name 2@ type '" emit ." ," cr ;
1.30      pazsan    920: 
1.92      anton     921: \  : output-c-func ( -- )
                    922: \  \ used for word libraries
                    923: \      ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP)      /* " prim prim-name 2@ type
                    924: \      ."  ( " prim prim-stack-string 2@ type ."  ) */" cr
                    925: \      ." /* " prim prim-doc 2@ type ."  */" cr
                    926: \      ." NAME(" quote prim prim-name 2@ type quote ." )" cr
                    927: \      \ debugging
                    928: \      ." {" cr
                    929: \      print-declarations
                    930: \      \ !! don't know what to do about that
                    931: \      inst-stream  stack-used? IF ." Cell *ip=IP;" cr THEN
                    932: \      data-stack   stack-used? IF ." Cell *sp=SP;" cr THEN
                    933: \      fp-stack     stack-used? IF ." Cell *fp=*FP;" cr THEN
                    934: \      return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
                    935: \      flush-tos
                    936: \      fetches
                    937: \      stack-pointer-updates
                    938: \      fp-stack   stack-used? IF ." *FP=fp;" cr THEN
                    939: \      ." {" cr
                    940: \      ." #line " c-line @ . quote c-filename 2@ type quote cr
                    941: \      prim prim-c-code 2@ type
                    942: \      ." }" cr
                    943: \      stores
                    944: \      fill-tos
                    945: \      ." return (sp);" cr
                    946: \      ." }" cr
                    947: \      cr ;
1.30      pazsan    948: 
1.43      jwilke    949: : output-label ( -- )  
1.127     anton     950:     ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
1.1       anton     951: 
1.43      jwilke    952: : output-alias ( -- ) 
1.69      anton     953:     ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1.1       anton     954: 
1.122     anton     955: : output-c-prim-num ( -- )
1.141   ! anton     956:     ." N_" prim prim-c-name 2@ type ." ," cr ;
1.114     anton     957: 
1.43      jwilke    958: : output-forth ( -- )  
1.69      anton     959:     prim prim-forth-code @ 0=
1.30      pazsan    960:     IF         \ output-alias
1.28      jwilke    961:        \ this is bad for ec: an alias is compiled if tho word does not exist!
                    962:        \ JAW
1.69      anton     963:     ELSE  ." : " prim prim-name 2@ type ."   ( "
                    964:        prim prim-stack-string 2@ type ." )" cr
                    965:        prim prim-forth-code 2@ type cr
1.30      pazsan    966:     THEN ;
1.10      anton     967: 
1.17      anton     968: : output-tag-file ( -- )
1.117     pazsan    969:     name-filename 2@ last-name-filename 2@ compare if
1.17      anton     970:        name-filename 2@ last-name-filename 2!
                    971:        #ff emit cr
                    972:        name-filename 2@ type
                    973:        ." ,0" cr
                    974:     endif ;
                    975: 
                    976: : output-tag ( -- )
                    977:     output-tag-file
1.69      anton     978:     prim prim-name 2@ 1+ type
1.17      anton     979:     127 emit
1.69      anton     980:     space prim prim-name 2@ type space
1.17      anton     981:     1 emit
                    982:     name-line @ 0 .r
                    983:     ." ,0" cr ;
                    984: 
1.100     pazsan    985: : output-vi-tag ( -- )
                    986:     name-filename 2@ type #tab emit
                    987:     prim prim-name 2@ type #tab emit
                    988:     ." /^" prim prim-name 2@ type ."  *(/" cr ;
                    989: 
1.10      anton     990: [IFDEF] documentation
                    991: : register-doc ( -- )
1.82      anton     992:     prim prim-name 2@ documentation ['] create insert-wordlist
1.69      anton     993:     prim prim-name 2@ 2,
                    994:     prim prim-stack-string 2@ condition-stack-effect 2,
                    995:     prim prim-wordset 2@ 2,
                    996:     prim prim-c-name 2@ condition-pronounciation 2,
1.82      anton     997:     prim prim-doc 2@ 2, ;
1.10      anton     998: [THEN]
1.67      anton     999: 
                   1000: 
1.69      anton    1001: \ combining instructions
                   1002: 
                   1003: \ The input should look like this:
                   1004: 
                   1005: \ lit_+ = lit +
                   1006: 
                   1007: \ The output should look like this:
                   1008: 
                   1009: \  I_lit_+:
                   1010: \  {
                   1011: \  DEF_CA
                   1012: \  Cell _x_ip0;
                   1013: \  Cell _x_sp0;
                   1014: \  Cell _x_sp1;
                   1015: \  NEXT_P0;
                   1016: \  _x_ip0 = (Cell) IPTOS;
                   1017: \  _x_sp0 = (Cell) spTOS;
                   1018: \  INC_IP(1);
                   1019: \  /* sp += 0; */
                   1020: \  /* lit ( #w -- w ) */
                   1021: \  /*  */
                   1022: \  NAME("lit")
                   1023: \  {
                   1024: \  Cell w;
                   1025: \  w = (Cell) _x_ip0;
                   1026: \  #ifdef VM_DEBUG
                   1027: \  if (vm_debug) {
                   1028: \  fputs(" w=", vm_out); printarg_w (w);
                   1029: \  fputc('\n', vm_out);
                   1030: \  }
                   1031: \  #endif
                   1032: \  {
                   1033: \  #line 136 "./prim"
                   1034: \  }
                   1035: \  _x_sp1 = (Cell)w;
                   1036: \  }
                   1037: \  I_plus:     /* + ( n1 n2 -- n ) */
                   1038: \  /*  */
                   1039: \  NAME("+")
                   1040: \  {
                   1041: \  DEF_CA
                   1042: \  Cell n1;
                   1043: \  Cell n2;
                   1044: \  Cell n;
                   1045: \  NEXT_P0;
                   1046: \  n1 = (Cell) _x_sp0;
                   1047: \  n2 = (Cell) _x_sp1;
                   1048: \  #ifdef VM_DEBUG
                   1049: \  if (vm_debug) {
                   1050: \  fputs(" n1=", vm_out); printarg_n (n1);
                   1051: \  fputs(" n2=", vm_out); printarg_n (n2);
                   1052: \  fputc('\n', vm_out);
                   1053: \  }
                   1054: \  #endif
                   1055: \  {
                   1056: \  #line 516 "./prim"
                   1057: \  n = n1+n2;
                   1058: \  }
                   1059: \  _x_sp0 = (Cell)n;
                   1060: \  }
                   1061: \  NEXT_P1;
                   1062: \  spTOS = (Cell)_x_sp0;
                   1063: \  NEXT_P2;
                   1064: 
1.71      anton    1065: : init-combined ( -- )
1.79      anton    1066:     prim to combined
1.71      anton    1067:     0 num-combined !
                   1068:     current-depth max-stacks cells erase
1.116     anton    1069:     include-skipped-insts @ current-depth 0 th !
1.72      anton    1070:     max-depth     max-stacks cells erase
                   1071:     min-depth     max-stacks cells erase
                   1072:     prim prim-effect-in  prim prim-effect-in-end  !
                   1073:     prim prim-effect-out prim prim-effect-out-end ! ;
1.71      anton    1074: 
                   1075: : max! ( n addr -- )
                   1076:     tuck @ max swap ! ;
                   1077: 
1.72      anton    1078: : min! ( n addr -- )
                   1079:     tuck @ min swap ! ;
                   1080: 
1.119     anton    1081: : inst-stream-adjustment ( nstack -- n )
                   1082:     \ number of stack items to add for each part
                   1083:     0= include-skipped-insts @ and negate ;
1.116     anton    1084: 
1.71      anton    1085: : add-depths { p -- }
                   1086:     \ combine stack effect of p with *-depths
                   1087:     max-stacks 0 ?do
1.72      anton    1088:        current-depth i th @
1.119     anton    1089:        p prim-stacks-in  i th @ + i inst-stream-adjustment +
1.72      anton    1090:        dup max-depth i th max!
                   1091:        p prim-stacks-out i th @ -
                   1092:        dup min-depth i th min!
                   1093:        current-depth i th !
1.71      anton    1094:     loop ;
                   1095: 
1.118     anton    1096: : copy-maxdepths ( n -- )
                   1097:     max-depth max-depths rot max-stacks * th max-stacks cells move ;
                   1098: 
1.71      anton    1099: : add-prim ( addr u -- )
                   1100:     \ add primitive given by "addr u" to combined-prims
                   1101:     primitives search-wordlist s" unknown primitive" ?print-error
                   1102:     execute { p }
1.72      anton    1103:     p combined-prims num-combined @ th !
1.118     anton    1104:     num-combined @ copy-maxdepths
1.71      anton    1105:     1 num-combined +!
1.118     anton    1106:     p add-depths
                   1107:     num-combined @ copy-maxdepths ;
1.71      anton    1108: 
                   1109: : compute-effects { q -- }
                   1110:     \ compute the stack effects of q from the depths
                   1111:     max-stacks 0 ?do
1.72      anton    1112:        max-depth i th @ dup
                   1113:        q prim-stacks-in i th !
                   1114:        current-depth i th @ -
                   1115:        q prim-stacks-out i th !
                   1116:     loop ;
                   1117: 
                   1118: : make-effect-items { stack# items effect-endp -- }
                   1119:     \ effect-endp points to a pointer to the end of the current item-array
                   1120:     \ and has to be updated
                   1121:     stacks stack# th @ { stack }
                   1122:     items 0 +do
                   1123:        effect-endp @ { item }
                   1124:        i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
                   1125:        item item-name 2!
                   1126:        stack item item-stack !
1.74      anton    1127:        stack stack-type @ item item-type !
1.72      anton    1128:        i item item-offset !
                   1129:        item item-first on
                   1130:        item% %size effect-endp +!
                   1131:     loop ;
                   1132: 
                   1133: : init-effects { q -- }
                   1134:     \ initialize effects field for FETCHES and STORES
                   1135:     max-stacks 0 ?do
                   1136:        i q prim-stacks-in  i th @ q prim-effect-in-end  make-effect-items
                   1137:        i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1.71      anton    1138:     loop ;
                   1139: 
1.119     anton    1140: : compute-stack-max-back-depths ( stack -- )
                   1141:     stack-number @ { stack# }
                   1142:     current-depth stack# th @ dup
                   1143:     dup stack# num-combined @ s-c-max-back-depth !
                   1144:     -1 num-combined @ 1- -do ( max-depth current-depth )
                   1145:        combined-prims i th @ { p }
                   1146:        p prim-stacks-out stack# th @ +
                   1147:        dup >r max r>
                   1148:        over stack# i s-c-max-back-depth !
                   1149:        p prim-stacks-in stack# th @ -
                   1150:        stack# inst-stream-adjustment -
                   1151:     1 -loop
                   1152:     assert( dup stack# inst-stream-adjustment negate = )
                   1153:     assert( over max-depth stack# th @ = )
                   1154:     2drop ;
                   1155: 
                   1156: : compute-max-back-depths ( -- )
                   1157:     \ compute max-back-depths.
                   1158:     \ assumes that current-depths is correct for the end of the combination
                   1159:     ['] compute-stack-max-back-depths map-stacks ;
                   1160: 
1.71      anton    1161: : process-combined ( -- )
1.81      anton    1162:     combined combined-prims num-combined @ cells
1.82      anton    1163:     combinations ['] constant insert-wordlist
1.86      anton    1164:     combined-prims num-combined @ 1- th ( last-part )
                   1165:     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1.72      anton    1166:     prim compute-effects
                   1167:     prim init-effects
1.119     anton    1168:     compute-max-back-depths
1.72      anton    1169:     output-combined perform ;
                   1170: 
                   1171: \ C output
                   1172: 
                   1173: : print-item { n stack -- }
                   1174:     \ print nth stack item name
1.79      anton    1175:     stack stack-type @ type-c-name 2@ type space
                   1176:     ." _" stack stack-pointer 2@ type n 0 .r ;
1.72      anton    1177: 
                   1178: : print-declarations-combined ( -- )
                   1179:     max-stacks 0 ?do
                   1180:        max-depth i th @ min-depth i th @ - 0 +do
                   1181:            i stacks j th @ print-item ." ;" cr
                   1182:        loop
                   1183:     loop ;
1.79      anton    1184: 
                   1185: : part-fetches ( -- )
                   1186:     fetches ;
                   1187: 
                   1188: : part-output-c-tail ( -- )
1.91      anton    1189:     print-debug-results
1.85      anton    1190:     stores ;
                   1191: 
                   1192: : output-combined-tail ( -- )
                   1193:     part-output-c-tail
                   1194:     in-part @ >r in-part off
1.119     anton    1195:     combined ['] output-c-tail-no-stores prim-context
1.118     anton    1196:     r> in-part ! ;
                   1197: 
                   1198: : part-stack-pointer-updates ( -- )
1.123     anton    1199:     next-stack-number @ 0 +do
1.118     anton    1200:        i part-num @ 1+ s-c-max-depth @ dup
                   1201:        i num-combined @ s-c-max-depth @ =    \ final depth
                   1202:        swap i part-num @ s-c-max-depth @ <> \ just reached now
                   1203:        part-num @ 0= \ first part
                   1204:        or and if
                   1205:            stacks i th @ stack-pointer-update
                   1206:        endif
                   1207:     loop ;
1.79      anton    1208: 
                   1209: : output-part ( p -- )
                   1210:     to prim
                   1211:     ." /* " prim prim-name 2@ type ."  ( " prim prim-stack-string 2@ type ." ) */" cr
                   1212:     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
                   1213:     ." {" cr
                   1214:     print-declarations
                   1215:     part-fetches
                   1216:     print-debug-args
1.118     anton    1217:     combined ['] part-stack-pointer-updates prim-context
                   1218:     1 part-num +!
1.79      anton    1219:     prim add-depths \ !! right place?
1.85      anton    1220:     prim prim-c-code 2@ ['] output-combined-tail type-c-code
1.79      anton    1221:     part-output-c-tail
                   1222:     ." }" cr ;
                   1223: 
1.74      anton    1224: : output-parts ( -- )
1.79      anton    1225:     prim >r in-part on
                   1226:     current-depth max-stacks cells erase
1.118     anton    1227:     0 part-num !
1.114     anton    1228:     ['] output-part map-combined
1.79      anton    1229:     in-part off
1.74      anton    1230:     r> to prim ;
                   1231: 
1.72      anton    1232: : output-c-combined ( -- )
                   1233:     print-entry cr
1.74      anton    1234:     \ debugging messages just in parts
1.72      anton    1235:     ." {" cr
                   1236:     ." DEF_CA" cr
                   1237:     print-declarations-combined
                   1238:     ." NEXT_P0;" cr
                   1239:     flush-tos
1.118     anton    1240:     \ fetches \ now in parts
1.74      anton    1241:     \ print-debug-args
1.118     anton    1242:     \ stack-pointer-updates now in parts
1.74      anton    1243:     output-parts
1.119     anton    1244:     output-c-tail2-no-stores
1.74      anton    1245:     ." }" cr
                   1246:     cr ;
1.72      anton    1247: 
                   1248: : output-forth-combined ( -- )
1.81      anton    1249: ;
                   1250: 
                   1251: 
1.83      anton    1252: \ peephole optimization rules
1.81      anton    1253: 
1.114     anton    1254: \ data for a simple peephole optimizer that always tries to combine
                   1255: \ the currently compiled instruction with the last one.
                   1256: 
1.81      anton    1257: \ in order for this to work as intended, shorter combinations for each
                   1258: \ length must be present, and the longer combinations must follow
                   1259: \ shorter ones (this restriction may go away in the future).
                   1260:   
1.83      anton    1261: : output-peephole ( -- )
1.81      anton    1262:     combined-prims num-combined @ 1- cells combinations search-wordlist
1.114     anton    1263:     s" the prefix for this superinstruction must be defined earlier" ?print-error
1.82      anton    1264:     ." {"
                   1265:     execute prim-num @ 5 .r ." ,"
                   1266:     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
                   1267:     combined prim-num @ 5 .r ." }, /* "
                   1268:     combined prim-c-name 2@ type ."  */"
                   1269:     cr ;
                   1270: 
1.114     anton    1271: 
1.115     anton    1272: \ cost and superinstruction data for a sophisticated combiner (e.g.,
                   1273: \ shortest path)
1.114     anton    1274: 
                   1275: \ This is intended as initializer for a structure like this
                   1276: 
1.116     anton    1277: \  struct cost {
1.114     anton    1278: \    int loads;       /* number of stack loads */
                   1279: \    int stores;      /* number of stack stores */
                   1280: \    int updates;     /* number of stack pointer updates */
1.138     anton    1281: \    int offset;      /* offset into super2 table */
1.114     anton    1282: \    int length;      /* number of components */
                   1283: \  };
                   1284: 
1.115     anton    1285: \ How do you know which primitive or combined instruction this
                   1286: \ structure refers to?  By the order of cost structures, as in most
                   1287: \ other cases.
                   1288: 
1.139     anton    1289: : super2-length ( -- n )
                   1290:     combined if
                   1291:        num-combined @
                   1292:     else
                   1293:        1
                   1294:     endif ;
                   1295: 
1.115     anton    1296: : compute-costs { p -- nloads nstores nupdates }
                   1297:     \ compute the number of loads, stores, and stack pointer updates
                   1298:     \ of a primitive or combined instruction; does not take TOS
1.139     anton    1299:     \ caching into account
1.115     anton    1300:     0 max-stacks 0 +do
                   1301:        p prim-stacks-in i th @ +
                   1302:     loop
1.139     anton    1303:     super2-length 1- - \ don't count instruction fetches of subsumed insts
1.115     anton    1304:     0 max-stacks 0 +do
                   1305:        p prim-stacks-out i th @ +
                   1306:     loop
1.139     anton    1307:     0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
1.115     anton    1308:        p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
                   1309:     loop ;
1.114     anton    1310: 
                   1311: : output-num-part ( p -- )
1.141   ! anton    1312:     ." N_" prim-c-name 2@ type ." ," ;
        !          1313:     \ prim-num @ 4 .r ." ," ;
1.138     anton    1314: 
                   1315: : output-name-comment ( -- )
                   1316:     ."  /* " prim prim-name 2@ type ."  */" ;
                   1317: 
                   1318: variable offset-super2  0 offset-super2 ! \ offset into the super2 table
1.141   ! anton    1319: 
        !          1320: : output-costs-gforth-simple ( -- )
        !          1321:      ." {" prim compute-costs
        !          1322:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
        !          1323:     prim output-num-part
        !          1324:     1 2 .r ." },"
        !          1325:     output-name-comment
        !          1326:     cr ;
        !          1327: 
        !          1328: : output-costs-gforth-combined ( -- )
        !          1329:     ." {" prim compute-costs
        !          1330:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
        !          1331:     ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
        !          1332:     super2-length dup 2 .r ." }," offset-super2 +!
        !          1333:     output-name-comment
        !          1334:     cr ;
1.138     anton    1335: 
1.115     anton    1336: : output-costs ( -- )
1.138     anton    1337:     \ description of superinstructions and simple instructions
1.115     anton    1338:     ." {" prim compute-costs
                   1339:     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
1.138     anton    1340:     offset-super2 @ 5 .r ." ,"
                   1341:     super2-length dup 2 .r ." }," offset-super2 +!
                   1342:     output-name-comment
                   1343:     cr ;
                   1344: 
                   1345: : output-super2 ( -- )
                   1346:     \ table of superinstructions without requirement for existing prefixes
1.115     anton    1347:     combined if
1.138     anton    1348:        ['] output-num-part map-combined 
1.115     anton    1349:     else
1.138     anton    1350:        prim output-num-part
1.115     anton    1351:     endif
1.138     anton    1352:     output-name-comment
                   1353:     cr ;   
1.69      anton    1354: 
1.67      anton    1355: \ the parser
                   1356: 
                   1357: eof-char max-member \ the whole character set + EOF
                   1358: 
                   1359: : getinput ( -- n )
                   1360:  rawinput @ endrawinput @ =
                   1361:  if
                   1362:    eof-char
                   1363:  else
                   1364:    cookedinput @ c@
                   1365:  endif ;
                   1366: 
                   1367: :noname ( n -- )
                   1368:  dup bl > if
                   1369:   emit space
                   1370:  else
                   1371:   .
                   1372:  endif ;
                   1373: print-token !
                   1374: 
                   1375: : testchar? ( set -- f )
                   1376:  getinput member? ;
                   1377: ' testchar? test-vector !
                   1378: 
1.130     anton    1379: : checksynclines ( -- )
1.67      anton    1380:     \ when input points to a newline, check if the next line is a
                   1381:     \ sync line.  If it is, perform the appropriate actions.
1.131     anton    1382:     rawinput @ begin >r
1.130     anton    1383:        s" #line " r@ over compare if
                   1384:            rdrop 1 line +! EXIT
                   1385:        endif
                   1386:        0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
                   1387:        dup c@ bl = if
                   1388:            char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
                   1389:            char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
                   1390:            char+
                   1391:        endif
                   1392:        dup c@ nl-char <> 0= s" sync line syntax" ?print-error
                   1393:        skipsynclines @ if
1.131     anton    1394:            char+ dup rawinput !
1.130     anton    1395:            rawinput @ c@ cookedinput @ c!
                   1396:        endif
                   1397:     again ;
1.67      anton    1398: 
                   1399: : ?nextchar ( f -- )
1.71      anton    1400:     s" syntax error, wrong char" ?print-error
1.67      anton    1401:     rawinput @ endrawinput @ <> if
                   1402:        rawinput @ c@
                   1403:        1 chars rawinput +!
                   1404:        1 chars cookedinput +!
                   1405:        nl-char = if
1.130     anton    1406:            checksynclines
1.67      anton    1407:            rawinput @ line-start !
                   1408:        endif
1.130     anton    1409:        rawinput @ c@
                   1410:        cookedinput @ c!
1.67      anton    1411:     endif ;
                   1412: 
                   1413: : charclass ( set "name" -- )
                   1414:  ['] ?nextchar terminal ;
                   1415: 
                   1416: : .. ( c1 c2 -- set )
                   1417:  ( creates a set that includes the characters c, c1<=c<=c2 )
                   1418:  empty copy-set
                   1419:  swap 1+ rot do
                   1420:   i over add-member
                   1421:  loop ;
                   1422: 
                   1423: : ` ( -- terminal ) ( use: ` c )
                   1424:  ( creates anonymous terminal for the character c )
                   1425:  char singleton ['] ?nextchar make-terminal ;
                   1426: 
                   1427: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
                   1428: char 0 char 9 ..                                       charclass digit
                   1429: bl singleton tab-char over add-member                  charclass white
                   1430: nl-char singleton eof-char over add-member complement  charclass nonl
                   1431: nl-char singleton eof-char over add-member
                   1432:     char : over add-member complement                   charclass nocolonnl
1.110     anton    1433: nl-char singleton eof-char over add-member
                   1434:     char } over add-member complement                   charclass nobracenl
1.67      anton    1435: bl 1+ maxchar .. char \ singleton complement intersection
                   1436:                                                         charclass nowhitebq
                   1437: bl 1+ maxchar ..                                        charclass nowhite
                   1438: char " singleton eof-char over add-member complement   charclass noquote
                   1439: nl-char singleton                                      charclass nl
                   1440: eof-char singleton                                     charclass eof
1.79      anton    1441: nl-char singleton eof-char over add-member             charclass nleof
1.67      anton    1442: 
                   1443: (( letter (( letter || digit )) **
                   1444: )) <- c-ident ( -- )
                   1445: 
1.110     anton    1446: (( ` # ?? (( letter || digit || ` : )) ++
1.67      anton    1447: )) <- stack-ident ( -- )
                   1448: 
                   1449: (( nowhitebq nowhite ** ))
                   1450: <- forth-ident ( -- )
                   1451: 
                   1452: Variable forth-flag
                   1453: Variable c-flag
                   1454: 
                   1455: (( (( ` e || ` E )) {{ start }} nonl ** 
                   1456:    {{ end evaluate }}
                   1457: )) <- eval-comment ( ... -- ... )
                   1458: 
                   1459: (( (( ` f || ` F )) {{ start }} nonl ** 
                   1460:    {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
                   1461: )) <- forth-comment ( -- )
                   1462: 
                   1463: (( (( ` c || ` C )) {{ start }} nonl ** 
                   1464:    {{ end c-flag @ IF type cr ELSE 2drop THEN }}
                   1465: )) <- c-comment ( -- )
                   1466: 
                   1467: (( ` - nonl ** {{ 
1.140     pazsan   1468:        forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
                   1469:        c-flag @ IF
                   1470:            function-diff
                   1471:            ." #else /* " function-number @ 0 .r ."  */" cr THEN }}
1.67      anton    1472: )) <- else-comment
                   1473: 
                   1474: (( ` + {{ start }} nonl ** {{ end
                   1475:        dup
                   1476:        IF      c-flag @
1.140     pazsan   1477:            IF
                   1478:                function-diff
                   1479:                ." #ifdef HAS_" bounds ?DO  I c@ toupper emit  LOOP cr
1.67      anton    1480:                THEN
                   1481:                forth-flag @
1.140     pazsan   1482:                IF  forth-fdiff  ." has? " type ."  [IF]"  cr THEN
1.67      anton    1483:        ELSE    2drop
1.140     pazsan   1484:            c-flag @      IF
                   1485:                function-diff  ." #endif" cr THEN
                   1486:            forth-flag @  IF  forth-fdiff  ." [THEN]"  cr THEN
1.67      anton    1487:        THEN }}
                   1488: )) <- if-comment
                   1489: 
1.98      pazsan   1490: (( (( ` g || ` G )) {{ start }} nonl **
                   1491:    {{ end
1.140     pazsan   1492:       forth-flag @ IF  forth-fdiff  ." group " type cr  THEN
                   1493:       c-flag @     IF  function-diff
                   1494:          ." GROUP(" type ." , " function-number @ 0 .r ." )" cr  THEN }}
1.98      pazsan   1495: )) <- group-comment
                   1496: 
                   1497: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
1.67      anton    1498: 
1.79      anton    1499: (( ` \ comment-body nleof )) <- comment ( -- )
1.67      anton    1500: 
                   1501: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
                   1502: <- stack-items
                   1503: 
1.69      anton    1504: (( {{ prim prim-effect-in }}  stack-items {{ prim prim-effect-in-end ! }}
1.67      anton    1505:    ` - ` - white **
1.69      anton    1506:    {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1.67      anton    1507: )) <- stack-effect ( -- )
                   1508: 
1.71      anton    1509: (( {{ prim create-prim }}
1.69      anton    1510:    ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
                   1511:    (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
                   1512:       (( {{ start }}  c-ident {{ end prim prim-c-name 2! }} )) ??
1.79      anton    1513:    )) ??  nleof
                   1514:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
1.110     anton    1515:    {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
                   1516:    (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
                   1517:    || (( nocolonnl nonl **  nleof white ** )) ** ))
                   1518:    {{ end prim prim-c-code 2! skipsynclines on }}
1.79      anton    1519:    (( ` :  white ** nleof
                   1520:       {{ start }} (( nonl ++  nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1.81      anton    1521:    )) ?? {{ process-simple }}
1.79      anton    1522:    nleof
1.69      anton    1523: )) <- simple-primitive ( -- )
                   1524: 
1.71      anton    1525: (( {{ init-combined }}
1.89      anton    1526:    ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1.79      anton    1527:    nleof {{ process-combined }}
1.69      anton    1528: )) <- combined-primitive
                   1529: 
1.79      anton    1530: (( {{ make-prim to prim 0 to combined
1.69      anton    1531:       line @ name-line ! filename 2@ name-filename 2!
1.82      anton    1532:       function-number @ prim prim-num !
1.110     anton    1533:       start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
                   1534:       2dup prim prim-name 2! prim prim-c-name 2! }}  white **
1.104     anton    1535:    (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
1.138     anton    1536:    (( simple-primitive || combined-primitive ))
                   1537:    {{ 1 function-number +! }}
1.67      anton    1538: )) <- primitive ( -- )
                   1539: 
                   1540: (( (( comment || primitive || nl white ** )) ** eof ))
                   1541: parser primitives2something
                   1542: warnings @ [IF]
                   1543: .( parser generated ok ) cr
                   1544: [THEN]
                   1545: 
1.95      jwilke   1546: 
1.97      jwilke   1547: \ run with gforth-0.5.0 (slurp-file is missing)
1.95      jwilke   1548: [IFUNDEF] slurp-file
                   1549: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
                   1550:     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
                   1551:     r/o bin open-file throw >r
                   1552:     r@ file-size throw abort" file too large"
                   1553:     dup allocate throw swap
                   1554:     2dup r@ read-file throw over <> abort" could not read whole file"
                   1555:     r> close-file throw ;
                   1556: [THEN]
                   1557: 
1.69      anton    1558: : primfilter ( addr u -- )
                   1559:     \ process the string at addr u
                   1560:     over dup rawinput ! dup line-start ! cookedinput !
                   1561:     + endrawinput !
1.130     anton    1562:     checksynclines
1.69      anton    1563:     primitives2something ;    
1.8       pazsan   1564: 
1.130     anton    1565: : unixify ( c-addr u1 -- c-addr u2 )
                   1566:     \ delete crs from the string
                   1567:     bounds tuck tuck ?do ( c-addr1 )
                   1568:        i c@ dup #cr <> if
                   1569:            over c! char+
                   1570:        else
                   1571:            drop
                   1572:        endif
                   1573:     loop
                   1574:     over - ;
                   1575: 
1.72      anton    1576: : process-file ( addr u xt-simple x-combined -- )
                   1577:     output-combined ! output !
1.61      anton    1578:     save-mem 2dup filename 2!
1.130     anton    1579:     slurp-file unixify
1.17      anton    1580:     warnings @ if
                   1581:        ." ------------ CUT HERE -------------" cr  endif
1.69      anton    1582:     primfilter ;
1.30      pazsan   1583: 
1.72      anton    1584: \  : process      ( xt -- )
                   1585: \      bl word count rot
                   1586: \      process-file ;

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