File:  [gforth] / gforth / prims2x.fs
Revision 1.7: download - view: text, annotated - select for diffs
Thu Sep 8 17:20:12 1994 UTC (29 years, 6 months ago) by anton
Branches: MAIN
CVS tags: HEAD
cleaned up NEXT macros; provided for CISC (united) and RISC (split) versions
cstr is now a function that can process arbitrarily long strings

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

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