Annotation of gforth/prims2x.fs, revision 1.111

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

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