File:  [gforth] / gforth / prims2x.fs
Revision 1.77: download - view: text, annotated - select for diffs
Tue Feb 6 16:53:06 2001 UTC (23 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
type conversion macros instead of casts for fetch/store-single (prims2x.fs)

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

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