File:  [gforth] / gforth / prims2x.fs
Revision 1.80: download - view: text, annotated - select for diffs
Fri Feb 9 20:15:31 2001 UTC (18 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
fixed inst-stream access in parts of combined instructions

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

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