File:  [gforth] / gforth / prims2x.fs
Revision 1.76: download - view: text, annotated - select for diffs
Sun Jan 28 22:43:39 2001 UTC (23 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed cell-size dependent masks

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

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