File:  [gforth] / gforth / prims2x.fs
Revision 1.75: download - view: text, annotated - select for diffs
Wed Jan 24 13:53:32 2001 UTC (18 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
replaced items with field in prim

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

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