File:  [gforth] / gforth / prims2x.fs
Revision 1.74: download - view: text, annotated - select for diffs
Wed Jan 24 10:32:01 2001 UTC (18 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
factored out WORDLIST-WORDS from WORDS
more work on combined primitives

    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: : th ( addr1 n -- addr2 )
   83:     cells + ;
   84: 
   85: : holds ( addr u -- )
   86:     \ like HOLD, but for a string
   87:     tuck + swap 0 +do
   88: 	1- dup c@ hold
   89:     loop
   90:     drop ;
   91: 
   92: : start ( -- addr )
   93:  cookedinput @ ;
   94: 
   95: : end ( addr -- addr u )
   96:  cookedinput @ over - ;
   97: 
   98: : print-error-line ( -- )
   99:     \ print the current line and position
  100:     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
  101:     over - type cr
  102:     line-start @ rawinput @ over - typewhite ." ^" cr ;
  103: 
  104: : ?print-error { f addr u -- }
  105:     f ?not? if
  106: 	outfile-id >r try
  107: 	    stderr to outfile-id
  108: 	    filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
  109: 	    print-error-line
  110: 	    0
  111: 	recover endtry
  112: 	r> to outfile-id throw
  113: 	abort
  114:     endif ;
  115: 
  116: : quote ( -- )
  117:     [char] " emit ;
  118: 
  119: variable output          \ xt ( -- ) of output word for simple primitives
  120: variable output-combined \ xt ( -- ) of output word for combined primitives
  121: 
  122: : printprim ( -- )
  123:  output @ execute ;
  124: 
  125: struct%
  126:     cell%    field stack-number \ the number of this stack
  127:     cell% 2* field stack-pointer \ stackpointer name
  128:     cell%    field stack-type \ name for default type of stack items
  129:     cell% 2* field stack-cast \ cast string for assignments to stack elements
  130:     cell%    field stack-in-index-xt \ ( in-size item -- in-index )
  131: end-struct stack%
  132: 
  133: struct%
  134:  cell% 2* field item-name   \ name, excluding stack prefixes
  135:  cell%    field item-stack  \ descriptor for the stack used, 0 is default
  136:  cell%    field item-type   \ descriptor for the item type
  137:  cell%    field item-offset \ offset in stack items, 0 for the deepest element
  138:  cell%	  field item-first  \ true if this is the first occurence of the item
  139: end-struct item%
  140: 
  141: struct%
  142:     cell% 2* field type-c-name
  143:     cell%    field type-stack \ default stack
  144:     cell%    field type-size  \ size of type in stack items
  145:     cell%    field type-fetch \ xt of fetch code generator ( item -- )
  146:     cell%    field type-store \ xt of store code generator ( item -- )
  147: end-struct type%
  148: 
  149: variable next-stack-number 0 next-stack-number !
  150: create stacks max-stacks cells allot \ array of stacks
  151: 
  152: : stack-in-index ( in-size item -- in-index )
  153:     item-offset @ - 1- ;
  154: 
  155: : inst-in-index ( in-size item -- in-index )
  156:     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
  157: 
  158: : make-stack ( addr-ptr u1 type addr-cast u2 "stack-name" -- )
  159:     create stack% %allot >r
  160:     r@ stacks next-stack-number @ th !
  161:     next-stack-number @ r@ stack-number !  1 next-stack-number +!
  162:     save-mem r@ stack-cast 2!
  163:     r@ stack-type !
  164:     save-mem r@ stack-pointer 2! 
  165:     ['] stack-in-index r> stack-in-index-xt ! ;
  166: 
  167: \ stack items
  168: 
  169: : init-item ( addr u addr1 -- )
  170:     \ initialize item at addr1 with name addr u
  171:     \ !! remove stack prefix
  172:     dup item% %size erase
  173:     item-name 2! ;
  174: 
  175: : map-items { addr end xt -- }
  176:     \ perform xt for all items in array addr...end
  177:     end addr ?do
  178: 	i xt execute
  179:     item% %size +loop ;
  180: 
  181: \ various variables for storing stuff of one primitive
  182: 
  183: struct%
  184:     cell% 2* field prim-name
  185:     cell% 2* field prim-wordset
  186:     cell% 2* field prim-c-name
  187:     cell% 2* field prim-doc
  188:     cell% 2* field prim-c-code
  189:     cell% 2* field prim-forth-code
  190:     cell% 2* field prim-stack-string
  191:     item% max-effect * field prim-effect-in
  192:     item% max-effect * field prim-effect-out
  193:     cell%    field prim-effect-in-end
  194:     cell%    field prim-effect-out-end
  195:     cell% max-stacks * field prim-stacks-in  \ number of in items per stack
  196:     cell% max-stacks * field prim-stacks-out \ number of out items per stack
  197: end-struct prim%
  198: 
  199: : make-prim ( -- prim )
  200:     prim% %alloc { p }
  201:     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
  202:     p ;
  203: 
  204: 0 value prim
  205: 
  206: wordlist constant primitives
  207: 
  208: : create-prim ( prim -- )
  209:     get-current >r
  210:     primitives set-current
  211:     dup prim-name 2@ nextname constant
  212:     r> set-current ;
  213: 
  214: : stack-in ( stack -- addr )
  215:     \ address of number of stack items in effect in
  216:     stack-number @ cells prim prim-stacks-in + ;
  217: 
  218: : stack-out ( stack -- addr )
  219:     \ address of number of stack items in effect out
  220:     stack-number @ cells prim prim-stacks-out + ;
  221: 
  222: \ global vars
  223: variable c-line
  224: 2variable c-filename
  225: variable name-line
  226: 2variable name-filename
  227: 2variable last-name-filename
  228: Variable function-number 0 function-number !
  229: 
  230: \ for several reasons stack items of a word are stored in a wordlist
  231: \ since neither forget nor marker are implemented yet, we make a new
  232: \ wordlist for every word and store it in the variable items
  233: variable itemsqq
  234: 
  235: \ a few more set ops
  236: 
  237: : bit-equivalent ( w1 w2 -- w3 )
  238:  xor invert ;
  239: 
  240: : complement ( set1 -- set2 )
  241:  empty ['] bit-equivalent binary-set-operation ;
  242: 
  243: \ types
  244: 
  245: : stack-access ( n stack -- )
  246:     \ print a stack access at index n of stack
  247:     stack-pointer 2@ type
  248:     dup
  249:     if
  250: 	." [" 0 .r ." ]"
  251:     else
  252: 	drop ." TOS"
  253:     endif ;
  254: 
  255: : item-in-index { item -- n }
  256:     \ n is the index of item (in the in-effect)
  257:     item item-stack @ dup >r stack-in @ ( in-size r:stack )
  258:     item r> stack-in-index-xt @ execute ;
  259: 
  260: : fetch-single ( item -- )
  261:  \ fetch a single stack item from its stack
  262:  >r
  263:  r@ item-name 2@ type
  264:  ."  = (" 
  265:  r@ item-type @ type-c-name 2@ type ." ) "
  266:  r@ item-in-index r@ item-stack @ stack-access
  267:  ." ;" cr
  268:  rdrop ; 
  269: 
  270: : fetch-double ( item -- )
  271:  \ fetch a double stack item from its stack
  272:  >r
  273:  ." FETCH_DCELL("
  274:  r@ item-name 2@ type ." , "
  275:  r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
  276:  ." , "                      -1 under+ ." (Cell)" stack-access
  277:  ." );" cr
  278:  rdrop ;
  279: 
  280: : same-as-in? ( item -- f )
  281:  \ f is true iff the offset and stack of item is the same as on input
  282:  >r
  283:  r@ item-first @ if
  284:      rdrop false exit
  285:  endif
  286:  r@ item-name 2@ itemsqq @ search-wordlist 0= abort" bug"
  287:  execute @
  288:  dup r@ =
  289:  if \ item first appeared in output
  290:    drop false
  291:  else
  292:    dup  item-stack  @ r@ item-stack  @ = 
  293:    swap item-offset @ r@ item-offset @ = and
  294:  endif
  295:  rdrop ;
  296: 
  297: : item-out-index ( item -- n )
  298:     \ n is the index of item (in the in-effect)
  299:     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
  300: 
  301: : really-store-single ( item -- )
  302:  >r
  303:  r@ item-out-index r@ item-stack @ stack-access ."  = "
  304:  r@ item-stack @ stack-cast 2@ type
  305:  r@ item-name 2@ type ." ;"
  306:  rdrop ;
  307: 
  308: : store-single ( item -- )
  309:  >r
  310:  r@ same-as-in?
  311:  if
  312:    r@ item-in-index 0= r@ item-out-index 0= xor
  313:    if
  314:        ." IF_" r@ item-stack @ stack-pointer 2@ type
  315:        ." TOS(" r@ really-store-single ." );" cr
  316:    endif
  317:  else
  318:    r@ really-store-single cr
  319:  endif
  320:  rdrop ;
  321: 
  322: : store-double ( item -- )
  323: \ !! store optimization is not performed, because it is not yet needed
  324:  >r
  325:  ." STORE_DCELL(" r@ item-name 2@ type ." , "
  326:  r@ item-out-index r@ item-stack @ 2dup stack-access
  327:  ." , "                       -1 under+ stack-access
  328:  ." );" cr
  329:  rdrop ;
  330: 
  331: : single ( -- xt1 xt2 n )
  332:     ['] fetch-single ['] store-single 1 ;
  333: 
  334: : double ( -- xt1 xt2 n )
  335:     ['] fetch-double ['] store-double 2 ;
  336: 
  337: : s, ( addr u -- )
  338: \ allocate a string
  339:  here swap dup allot move ;
  340: 
  341: wordlist constant prefixes
  342: 
  343: : declare ( addr "name" -- )
  344: \ remember that there is a stack item at addr called name
  345:  create , ;
  346: 
  347: : !default ( w addr -- )
  348:     dup @ if
  349: 	2drop \ leave nonzero alone
  350:     else
  351: 	!
  352:     endif ;
  353: 
  354: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
  355:     \ describes a type
  356:     \ addr u specifies the C type name
  357:     \ stack effect entries of the type start with prefix
  358:     create type% %allot >r
  359:     addr u save-mem r@ type-c-name 2!
  360:     xt1   r@ type-fetch !
  361:     xt2   r@ type-store !
  362:     n     r@ type-size !
  363:     stack r@ type-stack !
  364:     rdrop ;
  365: 
  366: : type-prefix ( xt1 xt2 n stack "prefix" -- )
  367:     create-type
  368: does> ( item -- )
  369:     \ initialize item
  370:     { item typ }
  371:     typ item item-type !
  372:     typ type-stack @ item item-stack !default
  373:     item item-name 2@ itemsqq @ search-wordlist 0= if \ new name
  374: 	item item-name 2@ nextname item declare
  375: 	item item-first on
  376: 	\ typ type-c-name 2@ type space type  ." ;" cr
  377:     else
  378: 	drop
  379: 	item item-first off
  380:     endif ;
  381: 
  382: : execute-prefix ( item addr1 u1 -- )
  383:     \ execute the word ( item -- ) associated with the longest prefix
  384:     \ of addr1 u1
  385:     0 swap ?do
  386: 	dup i prefixes search-wordlist
  387: 	if \ ok, we have the type ( item addr1 xt )
  388: 	    nip execute
  389: 	    UNLOOP EXIT
  390: 	endif
  391: 	-1 s+loop
  392:     \ we did not find a type, abort
  393:     true abort" unknown prefix" ;
  394: 
  395: : declaration ( item -- )
  396:     dup item-name 2@ execute-prefix ;
  397: 
  398: : declaration-list ( addr1 addr2 -- )
  399:     ['] declaration map-items ;
  400: 
  401: : declarations ( -- )
  402:  wordlist dup itemsqq ! set-current
  403:  prim prim-effect-in prim prim-effect-in-end @ declaration-list
  404:  prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
  405: 
  406: : print-declaration { item -- }
  407:     item item-first @ if
  408: 	item item-type @ type-c-name 2@ type space
  409: 	item item-name 2@ type ." ;" cr
  410:     endif ;
  411: 
  412: : print-declarations ( -- )
  413:     prim prim-effect-in  prim prim-effect-in-end  @ ['] print-declaration map-items
  414:     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
  415:     
  416: : stack-prefix ( stack "prefix" -- )
  417:     name tuck nextname create ( stack length ) 2,
  418: does> ( item -- )
  419:     2@ { item stack prefix-length }
  420:     item item-name 2@ prefix-length /string item item-name 2!
  421:     stack item item-stack !
  422:     item declaration ;
  423: 
  424: \ types pointed to by stacks for use in combined prims
  425: s" Cell"  single 0 create-type cell-type
  426: s" Float" single 0 create-type float-type
  427: 
  428: s" sp" save-mem cell-type  s" (Cell)" make-stack data-stack 
  429: s" fp" save-mem cell-type  s" "       make-stack fp-stack
  430: s" rp" save-mem float-type s" (Cell)" make-stack return-stack
  431: s" IP" save-mem cell-type  s" error don't use # on results" make-stack inst-stream
  432: ' inst-in-index inst-stream stack-in-index-xt !
  433: \ !! initialize stack-in and stack-out
  434: 
  435: \ offset computation
  436: \ the leftmost (i.e. deepest) item has offset 0
  437: \ the rightmost item has the highest offset
  438: 
  439: : compute-offset { item xt -- }
  440:     \ xt specifies in/out; update stack-in/out and set item-offset
  441:     item item-type @ type-size @
  442:     item item-stack @ xt execute dup @ >r +!
  443:     r> item item-offset ! ;
  444: 
  445: : compute-offset-in ( addr1 addr2 -- )
  446:     ['] stack-in compute-offset ;
  447: 
  448: : compute-offset-out ( addr1 addr2 -- )
  449:     ['] stack-out compute-offset ;
  450: 
  451: : clear-stack { -- }
  452:     dup stack-in off stack-out off ;
  453: 
  454: : compute-offsets ( -- )
  455:     data-stack clear-stack  fp-stack clear-stack return-stack clear-stack
  456:     inst-stream clear-stack
  457:     prim prim-effect-in  prim prim-effect-in-end  @ ['] compute-offset-in  map-items
  458:     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
  459:     inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
  460: 
  461: : flush-a-tos { stack -- }
  462:     stack stack-out @ 0<> stack stack-in @ 0= and
  463:     if
  464: 	." IF_" stack stack-pointer 2@ 2dup type ." TOS("
  465: 	2dup type ." [0] = " type ." TOS);" cr
  466:     endif ;
  467: 
  468: : flush-tos ( -- )
  469:     data-stack   flush-a-tos
  470:     fp-stack     flush-a-tos
  471:     return-stack flush-a-tos ;
  472: 
  473: : fill-a-tos { stack -- }
  474:     stack stack-out @ 0= stack stack-in @ 0<> and
  475:     if
  476: 	." IF_" stack stack-pointer 2@ 2dup type ." TOS("
  477: 	2dup type ." TOS = " type ." [0]);" cr
  478:     endif ;
  479: 
  480: : fill-tos ( -- )
  481:     \ !! inst-stream for prefetching?
  482:     fp-stack     fill-a-tos
  483:     data-stack   fill-a-tos
  484:     return-stack fill-a-tos ;
  485: 
  486: : fetch ( addr -- )
  487:     dup item-type @ type-fetch @ execute ;
  488: 
  489: : fetches ( -- )
  490:     prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
  491: 
  492: : stack-pointer-update { stack -- }
  493:     \ stack grow downwards
  494:     stack stack-in @ stack stack-out @ -
  495:     ?dup-if \ this check is not necessary, gcc would do this for us
  496: 	stack stack-pointer 2@ type ."  += " 0 .r ." ;" cr
  497:     endif ;
  498: 
  499: : inst-pointer-update ( -- )
  500:     inst-stream stack-in @ ?dup-if
  501: 	." INC_IP(" 0 .r ." );" cr
  502:     endif ;
  503: 
  504: : stack-pointer-updates ( -- )
  505:     inst-pointer-update
  506:     data-stack   stack-pointer-update
  507:     fp-stack     stack-pointer-update
  508:     return-stack stack-pointer-update ;
  509: 
  510: : store ( item -- )
  511: \ f is true if the item should be stored
  512: \ f is false if the store is probably not necessary
  513:  dup item-type @ type-store @ execute ;
  514: 
  515: : stores ( -- )
  516:     prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
  517: 
  518: : output-c-tail ( -- )
  519:     \ the final part of the generated C code
  520:     ." NEXT_P1;" cr
  521:     stores
  522:     fill-tos
  523:     ." NEXT_P2;" cr ;
  524: 
  525: : type-c ( c-addr u -- )
  526:     \ like TYPE, but replaces "TAIL;" with tail code
  527:     begin ( c-addr1 u1 )
  528: 	2dup s" TAIL;" search
  529:     while ( c-addr1 u1 c-addr3 u3 )
  530: 	2dup 2>r drop nip over - type
  531: 	output-c-tail
  532: 	2r> 5 /string
  533: 	\ !! resync #line missing
  534:     repeat
  535:     2drop type ;
  536: 
  537: : print-type-prefix ( type -- )
  538:     body> >head .name ;
  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>