Annotation of gforth/prims2x.fs, revision 1.115

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

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