File:  [gforth] / gforth / prims2x.fs
Revision 1.16: download - view: text, annotated - select for diffs
Tue Nov 7 18:06:57 1995 UTC (28 years, 4 months ago) by anton
Branches: MAIN
CVS tags: gforth-0_1beta, HEAD
added copyright headers
changes to loadfilename & co. to make savesystem transparent to
 assertions and ~~

    1: \ converts primitives to, e.g., C code 
    2: 
    3: \ Copyright (C) 1995 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., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: 
   22: \ This is not very nice (hard limits, no checking, assumes 1 chars = 1)
   23: 
   24: \ Optimizations:
   25: \ superfluous stores are removed. GCC removes the superfluous loads by itself
   26: \ TOS and FTOS can be kept in register( variable)s.
   27: \ 
   28: \ Problems:
   29: \ The TOS optimization is somewhat hairy. The problems by example:
   30: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
   31: \    The store is not superfluous although the earlier opt. would think so
   32: \    Alternatively:    sp[0]=TOS; w=TOS; sp-=1; TOS=w;
   33: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
   34: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
   35: \ 4) ( -- ): /* but here they are unnecessary */
   36: \ 5) Words that call NEXT themselves have to be done very carefully.
   37: \
   38: \ To do:
   39: \ add the store optimization for doubles
   40: \ regarding problem 1 above: It would be better (for over) to implement
   41: \ 	the alternative
   42: 
   43: warnings off
   44: 
   45: [IFUNDEF] vocabulary    include search-order.fs [THEN]
   46: [IFUNDEF] environment?  include environ.fs      [THEN]
   47: include gray.fs
   48: 
   49: 100 constant max-effect \ number of things on one side of a stack effect
   50: 4096 constant batch-size \ no meaning, just make sure it's >0
   51: 255 constant maxchar
   52: maxchar 1+ constant eof-char
   53: 9 constant tab-char
   54: 10 constant nl-char
   55: 
   56: : read-whole-file ( c-addr1 file-id -- c-addr2 )
   57: \ reads the contents of the file file-id puts it into memory at c-addr1
   58: \ c-addr2 is the first address after the file block
   59:   begin ( c-addr file-id )
   60:     2dup batch-size swap read-file 
   61:     if
   62:       true abort" I/O error"
   63:     endif
   64:     ( c-addr file-id actual-size ) rot over + -rot
   65:     batch-size <>
   66:   until
   67:   drop ;
   68: 
   69: variable input \ pointer to next character to be parsed
   70: variable endinput \ pointer to the end of the input (the char after the last)
   71: 
   72: : start ( -- addr )
   73:  input @ ;
   74: 
   75: : end ( addr -- addr u )
   76:  input @ over - ;
   77: 
   78: variable output \ xt ( -- ) of output word
   79: 
   80: : printprim ( -- )
   81:  output @ execute ;
   82: 
   83: : field
   84:  <builds-field ( n1 n2 -- n3 )
   85:  does>         ( addr1 -- addr2 )
   86:    @ + ;
   87: 
   88: : const-field
   89:  <builds-field ( n1 n2 -- n3 )
   90:  does>         ( addr -- w )
   91:    @ + @ ;
   92: 
   93: struct
   94:  2 cells field item-name
   95:  cell field item-d-offset
   96:  cell field item-f-offset
   97:  cell field item-type
   98: constant item-descr
   99: 
  100: 2variable forth-name
  101: 2variable wordset
  102: 2variable c-name
  103: 2variable doc
  104: 2variable c-code
  105: 2variable forth-code
  106: 2variable stack-string
  107: create effect-in  max-effect item-descr * allot
  108: create effect-out max-effect item-descr * allot
  109: variable effect-in-end ( pointer )
  110: variable effect-out-end ( pointer )
  111: 2variable effect-in-size
  112: 2variable effect-out-size
  113: 
  114: variable primitive-number -10 primitive-number !
  115: 
  116: \ for several reasons stack items of a word are stored in a wordlist
  117: \ since neither forget nor marker are implemented yet, we make a new
  118: \ wordlist for every word and store it in the variable items
  119: variable items
  120: 
  121: \ a few more set ops
  122: 
  123: : bit-equivalent ( w1 w2 -- w3 )
  124:  xor invert ;
  125: 
  126: : complement ( set1 -- set2 )
  127:  empty ['] bit-equivalent binary-set-operation ;
  128: 
  129: \ the parser
  130: 
  131: eof-char max-member \ the whole character set + EOF
  132: 
  133: : getinput ( -- n )
  134:  input @
  135:  dup endinput @ =
  136:  if
  137:    drop eof-char
  138:  else
  139:    c@
  140:  endif ;
  141: 
  142: :noname ( n -- )
  143:  dup bl > if
  144:   emit space
  145:  else
  146:   .
  147:  endif ;
  148: print-token !
  149: 
  150: : testchar? ( set -- f )
  151:  getinput member? ;
  152: ' testchar? test-vector !
  153: 
  154: : ?nextchar ( f -- )
  155:  ?not? if
  156:    ." syntax error" cr
  157:    getinput . cr
  158:    input @ endinput @ over - 100 min type cr
  159:    abort
  160:  endif
  161:  input @ endinput @ <> if
  162:    1 input +!
  163:  endif ;
  164: 
  165: : charclass ( set "name" -- )
  166:  ['] ?nextchar terminal ;
  167: 
  168: : .. ( c1 c2 -- set )
  169:  ( creates a set that includes the characters c, c1<=c<=c2 )
  170:  empty copy-set
  171:  swap 1+ rot do
  172:   i over add-member
  173:  loop ;
  174: 
  175: : ` ( -- terminal ) ( use: ` c )
  176:  ( creates anonymous terminal for the character c )
  177:  [compile] ascii singleton ['] ?nextchar make-terminal ;
  178: 
  179: char a char z ..  char A char Z ..  union char _ singleton union  charclass letter
  180: char 0 char 9 ..					charclass digit
  181: bl singleton						charclass blank
  182: tab-char singleton					charclass tab
  183: nl-char singleton eof-char over add-member complement	charclass nonl
  184: nl-char singleton eof-char over add-member char : over add-member complement  charclass nocolonnl
  185: bl 1+ maxchar ..					charclass nowhite
  186: char " singleton eof-char over add-member complement	charclass noquote
  187: nl-char singleton					charclass nl
  188: eof-char singleton					charclass eof
  189: 
  190: 
  191: (( letter (( letter || digit )) **
  192: )) <- c-name ( -- )
  193: 
  194: nowhite ++
  195: <- name ( -- )
  196: 
  197: (( ` \ nonl ** nl
  198: )) <- comment ( -- )
  199: 
  200: (( {{ effect-in }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-in-end ! }}
  201:    ` - ` - blank **
  202:    {{ effect-out }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-out-end ! }}
  203: )) <- stack-effect ( -- )
  204: 
  205: (( {{ s" " doc 2! s" " forth-code 2! }}
  206:    (( comment || nl )) **
  207:    (( {{ start }} name {{ end 2dup forth-name 2! c-name 2! }}  tab ++
  208:       {{ start }} stack-effect {{ end stack-string 2! }} tab ++
  209:         {{ start }} name {{ end wordset 2! }} tab **
  210:         (( {{ start }}  c-name {{ end c-name 2! }} )) ??  nl
  211:    ))
  212:    (( ` " ` "  {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " nl )) ??
  213:    {{ start }} (( nocolonnl nonl **  nl )) ** {{ end c-code 2! }}
  214:    (( ` :  nl
  215:       {{ start }} (( nonl ++  nl )) ++ {{ end forth-code 2! }}
  216:    )) ??
  217:    (( nl || eof ))
  218: )) <- primitive ( -- )
  219: 
  220: (( (( primitive {{ printprim }} )) **  eof ))
  221: parser primitives2something
  222: warnings @ [IF]
  223: .( parser generated ok ) cr
  224: [THEN]
  225: 
  226: : primfilter ( file-id xt -- )
  227: \ fileid is for the input file, xt ( -- ) is for the output word
  228:  output !
  229:  here input !
  230:  here swap read-whole-file
  231:  dup endinput !
  232:  here - allot
  233:  align
  234:  primitives2something ;
  235: 
  236: \ types
  237: 
  238: struct
  239:  2 cells field type-c-name
  240:  cell const-field type-d-size
  241:  cell const-field type-f-size
  242:  cell const-field type-fetch-handler
  243:  cell const-field type-store-handler
  244: constant type-description
  245: 
  246: : data-stack-access ( n1 n2 n3 -- )
  247: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
  248:  drop swap - 1- dup
  249:  if
  250:    ." sp[" 0 .r ." ]"
  251:  else
  252:    drop ." TOS"
  253:  endif ;
  254: 
  255: : fp-stack-access ( n1 n2 n3 -- )
  256: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
  257:  nip swap - 1- dup
  258:  if
  259:    ." fp[" 0 .r ." ]"
  260:  else
  261:    drop ." FTOS"
  262:  endif ;
  263: 
  264: : fetch-single ( item -- )
  265:  >r
  266:  r@ item-name 2@ type
  267:  ."  = (" 
  268:  r@ item-type @ type-c-name 2@ type ." ) "
  269:  r@ item-d-offset @ effect-in-size 2@ data-stack-access ." ;" cr
  270:  rdrop ; 
  271: 
  272: : fetch-double ( item -- )
  273:  >r
  274:  r@ item-name 2@ type 
  275:  ." = ({Double_Store _d; _d.cells.low = "
  276:  r@ item-d-offset @ dup    effect-in-size 2@ data-stack-access
  277:  ." ; _d.cells.high = " 1+ effect-in-size 2@ data-stack-access
  278:  ." ; _d.dcell;});" cr
  279:  rdrop ;
  280: 
  281: : fetch-float ( item -- )
  282:  >r
  283:  r@ item-name 2@ type
  284:  ."  = "
  285:  \ ." (" r@ item-type @ type-c-name 2@ type ." ) "
  286:  r@ item-f-offset @ effect-in-size 2@ fp-stack-access ." ;" cr
  287:  rdrop ;
  288: 
  289: : d-same-as-in? ( item -- f )
  290: \ f is true iff the offset of item is the same as on input
  291:  >r
  292:  r@ item-name 2@ items @ search-wordlist 0=
  293:  abort" bug"
  294:  execute @
  295:  dup r@ =
  296:  if \ item first appeared in output
  297:    drop false
  298:  else
  299:    item-d-offset @ r@ item-d-offset @ =
  300:  endif
  301:  rdrop ;
  302: 
  303: : is-in-tos? ( item -- f )
  304: \ true if item has the same offset as the input TOS
  305:  item-d-offset @ 1+ effect-in-size 2@ drop = ;
  306: 
  307: : really-store-single ( item -- )
  308:  >r
  309:  r@ item-d-offset @ effect-out-size 2@ data-stack-access ."  = (Cell)"
  310:  r@ item-name 2@ type ." ;"
  311:  rdrop ;
  312: 
  313: : store-single ( item -- )
  314:  >r
  315:  r@ d-same-as-in?
  316:  if
  317:    r@ is-in-tos?
  318:    if
  319:      ." IF_TOS(" r@ really-store-single ." );" cr
  320:    endif
  321:  else
  322:    r@ really-store-single cr
  323:  endif
  324:  rdrop ;
  325: 
  326: : store-double ( item -- )
  327: \ !! store optimization is not performed, because it is not yet needed
  328:  >r
  329:  ." {Double_Store _d; _d.dcell = " r@ item-name 2@ type ." ; "
  330:  r@ item-d-offset @ dup    effect-out-size 2@ data-stack-access 
  331:  ."  = _d.cells.low; " 1+ effect-out-size 2@ data-stack-access
  332:  ." = _d.cells.high;}" cr
  333:  rdrop ;
  334: 
  335: : f-same-as-in? ( item -- f )
  336: \ f is true iff the offset of item is the same as on input
  337:  >r
  338:  r@ item-name 2@ items @ search-wordlist 0=
  339:  abort" bug"
  340:  execute @
  341:  dup r@ =
  342:  if \ item first appeared in output
  343:    drop false
  344:  else
  345:    item-f-offset @ r@ item-f-offset @ =
  346:  endif
  347:  rdrop ;
  348: 
  349: : is-in-ftos? ( item -- f )
  350: \ true if item has the same offset as the input TOS
  351:  item-f-offset @ 1+ effect-in-size 2@ nip = ;
  352: 
  353: : really-store-float ( item -- )
  354:  >r
  355:  r@ item-f-offset @ effect-out-size 2@ fp-stack-access ."  = "
  356:  r@ item-name 2@ type ." ;"
  357:  rdrop ;
  358: 
  359: : store-float ( item -- )
  360:  >r
  361:  r@ f-same-as-in?
  362:  if
  363:    r@ is-in-ftos?
  364:    if
  365:      ." IF_FTOS(" r@ really-store-float ." );" cr
  366:    endif
  367:  else
  368:    r@ really-store-float cr
  369:  endif
  370:  rdrop ;
  371:  
  372: : single-type ( -- xt1 xt2 n1 n2 )
  373:  ['] fetch-single ['] store-single 1 0 ;
  374: 
  375: : double-type ( -- xt1 xt2 n1 n2 )
  376:  ['] fetch-double ['] store-double 2 0 ;
  377: 
  378: : float-type ( -- xt1 xt2 n1 n2 )
  379:  ['] fetch-float ['] store-float 0 1 ;
  380: 
  381: : s, ( addr u -- )
  382: \ allocate a string
  383:  here swap dup allot move ;
  384: 
  385: : starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- )
  386: \ describes a type
  387: \ addr u specifies the C type name
  388: \ n1 is the size of the type on the data stack
  389: \ n2 is the size of the type on the FP stack
  390: \ stack effect entries of the type start with prefix
  391:  >r >r >r >r
  392:  dup >r here >r s,
  393:  create
  394:  r> r> 2,
  395:  r> r> r> , r> , swap , , ;
  396: 
  397: wordlist constant types
  398: get-current
  399: types set-current
  400: 
  401: s" Bool"	single-type starts-with f
  402: s" Char"	single-type starts-with c
  403: s" Cell"	single-type starts-with n
  404: s" Cell"	single-type starts-with w
  405: s" UCell"	single-type starts-with u
  406: s" DCell"	double-type starts-with d
  407: s" UDCell"	double-type starts-with ud
  408: s" Float"	float-type  starts-with r
  409: s" Cell *"	single-type starts-with a_
  410: s" Char *"	single-type starts-with c_
  411: s" Float *"	single-type starts-with f_
  412: s" DFloat *"	single-type starts-with df_
  413: s" SFloat *"	single-type starts-with sf_
  414: s" Xt"		single-type starts-with xt
  415: s" WID"		single-type starts-with wid
  416: s" F83Name *"	single-type starts-with f83name
  417: 
  418: set-current
  419: 
  420: : get-type ( addr1 u1 -- type-descr )
  421: \ get the type of the name in addr1 u1
  422: \ type-descr is a pointer to a type-descriptor
  423:  0 swap ?do
  424:    dup i types search-wordlist
  425:    if \ ok, we have the type ( addr1 xt )
  426:      execute nip
  427:      UNLOOP EXIT
  428:    endif
  429:  -1 s+loop
  430:  \ we did not find a type, abort
  431:  true abort" unknown type prefix" ;
  432: 
  433: : declare ( addr "name" -- )
  434: \ remember that there is a stack item at addr called name
  435:  create , ;
  436: 
  437: : declaration ( item -- )
  438:  dup item-name 2@ items @ search-wordlist
  439:  if \ already declared ( item xt )
  440:    execute @ item-type @ swap item-type !
  441:  else ( addr )
  442:    dup item-name 2@ nextname dup declare ( addr )
  443:    dup >r item-name 2@ 2dup get-type ( addr1 u type-descr )
  444:    dup r> item-type ! ( addr1 u type-descr )
  445:    type-c-name 2@ type space type ." ;" cr
  446:  endif ;
  447: 
  448: : declaration-list ( addr1 addr2 -- )
  449:  swap ?do
  450:   i declaration
  451:  item-descr +loop ;
  452: 
  453: : fetch ( addr -- )
  454:  dup item-type @ type-fetch-handler execute ;
  455: 
  456: : declarations ( -- )
  457:  wordlist dup items ! set-current
  458:  effect-in effect-in-end @ declaration-list
  459:  effect-out effect-out-end @ declaration-list ;
  460: 
  461: \ offset computation
  462: \ the leftmost (i.e. deepest) item has offset 0
  463: \ the rightmost item has the highest offset
  464: 
  465: : compute-offset ( n1 n2 item -- n3 n4 )
  466: \ n1, n3 are data-stack-offsets
  467: \ n2, n4 are the fp-stack-offsets
  468:  >r
  469:  swap dup r@ item-d-offset !
  470:  r@ item-type @ type-d-size +
  471:  swap dup r@ item-f-offset !
  472:  r@ item-type @ type-f-size +
  473:  rdrop ;
  474: 
  475: : compute-list ( addr1 addr2 -- n1 n2 )
  476: \ n1, n2 are the final offsets
  477:  0 0 2swap swap ?do
  478:   i compute-offset
  479:  item-descr +loop ;
  480: 
  481: : compute-offsets ( -- )
  482:  effect-in effect-in-end @ compute-list effect-in-size 2!
  483:  effect-out effect-out-end @ compute-list effect-out-size 2! ;
  484: 
  485: : flush-tos ( -- )
  486:  effect-in-size 2@ effect-out-size 2@
  487:  0<> rot 0= and
  488:  if
  489:    ." IF_FTOS(fp[0] = FTOS);" cr
  490:  endif
  491:  0<> swap 0= and
  492:  if
  493:    ." IF_TOS(sp[0] = TOS);" cr
  494:  endif ;
  495: 
  496: : fill-tos ( -- )
  497:  effect-in-size 2@ effect-out-size 2@
  498:  0= rot 0<> and
  499:  if
  500:    ." IF_FTOS(FTOS = fp[0]);" cr
  501:  endif
  502:  0= swap 0<> and
  503:  if
  504:    ." IF_TOS(TOS = sp[0]);" cr
  505:  endif ;
  506: 
  507: : fetches ( -- )
  508:  effect-in-end @ effect-in ?do
  509:    i fetch
  510:  item-descr +loop ; 
  511: 
  512: : stack-pointer-updates ( -- )
  513: \ we need not check if an update is a noop; gcc does this for us
  514:  effect-in-size 2@
  515:  effect-out-size 2@
  516:  rot swap - ( d-in d-out f-diff )
  517:  rot rot - ( f-diff d-diff )
  518:  ?dup IF  ." sp += " 0 .r ." ;" cr  THEN
  519:  ?dup IF  ." fp += " 0 .r ." ;" cr  THEN ;
  520: 
  521: : store ( item -- )
  522: \ f is true if the item should be stored
  523: \ f is false if the store is probably not necessary
  524:  dup item-type @ type-store-handler execute ;
  525: 
  526: : stores ( -- )
  527:  effect-out-end @ effect-out ?do
  528:    i store
  529:  item-descr +loop ; 
  530: 
  531: : .stack-list ( start end -- )
  532:  swap ?do
  533:    i item-name 2@ type space
  534:  item-descr +loop ; 
  535: 
  536: : output-c ( -- )
  537:  ." I_" c-name 2@ type ." :	/* " forth-name 2@ type ."  ( " stack-string 2@ type ."  ) */" cr
  538:  ." /* " doc 2@ type ."  */" cr
  539:  ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
  540:  ." {" cr
  541:  ." DEF_CA" cr
  542:  declarations
  543:  compute-offsets \ for everything else
  544:  ." NEXT_P0;" cr
  545:  flush-tos
  546:  fetches
  547:  stack-pointer-updates
  548:  ." {" cr
  549:  c-code 2@ type
  550:  ." }" cr
  551:  ." NEXT_P1;" cr
  552:  stores
  553:  fill-tos
  554:  ." NEXT_P2;" cr
  555:  ." }" cr
  556:  cr
  557: ;
  558: 
  559: : output-label ( -- )
  560:  ." &&I_" c-name 2@ type ." ," cr ;
  561: 
  562: : output-alias ( -- )
  563:  primitive-number @ . ." alias " forth-name 2@ type cr
  564:  -1 primitive-number +! ;
  565: 
  566: : output-forth ( -- )
  567:  forth-code @ 0=
  568:  IF    output-alias
  569:  ELSE  ." : " forth-name 2@ type ."   ( "
  570:        effect-in effect-in-end @ .stack-list ." -- "
  571:        effect-out effect-out-end @ .stack-list ." )" cr
  572:        forth-code 2@ type cr
  573:        -1 primitive-number +!
  574:  THEN ;
  575: 
  576: [IFDEF] documentation
  577: : register-doc ( -- )
  578:     get-current documentation set-current
  579:     forth-name 2@ nextname create
  580:     forth-name 2@ 2,
  581:     stack-string 2@ condition-stack-effect 2,
  582:     wordset 2@ 2,
  583:     c-name 2@ condition-pronounciation 2,
  584:     doc 2@ 2,
  585:     set-current ;
  586: [THEN]
  587: 
  588: : process-file ( addr u xt -- )
  589:  >r r/o open-file abort" cannot open file"
  590:  warnings @ if
  591:  ." ------------ CUT HERE -------------" cr  endif
  592:  r> primfilter ;
  593: 

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