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

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