File:  [gforth] / gforth / prims2x.fs
Revision 1.2: download - view: text, annotated - select for diffs
Wed May 18 17:29:59 1994 UTC (29 years, 11 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed problem with ?dup
Deleted noop output like sp+=0 from prims2x.fs
Made wordinfo.fs work with DTC on i386
Added a faster (???) relocater

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

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