home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / compiler / 1434 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  1.9 KB

  1. Path: sparky!uunet!ogicse!das-news.harvard.edu!spdcc!iecc!compilers-sender
  2. From: buehlman@iwf.mabp.ethz.ch (Buehlmann Thomas)
  3. Newsgroups: comp.compilers
  4. Subject: Re: constant folding at parse time
  5. Keywords: parse, attribute
  6. Message-ID: <92-08-134@comp.compilers>
  7. Date: 21 Aug 92 11:51:46 GMT
  8. Article-I.D.: comp.92-08-134
  9. References: <92-08-040@comp.compilers> <92-08-114@comp.compilers>
  10. Sender: compilers-sender@iecc.cambridge.ma.us
  11. Reply-To: buehlman@iwf.mabp.ethz.ch (Buehlmann Thomas)
  12. Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
  13. Lines: 32
  14. Approved: compilers@iecc.cambridge.ma.us
  15.  
  16. twpierce@amhux1.amherst.EDU (Tim Pierce) writes:
  17.  
  18. >The discussion about constant folding has intrigued me a little bit, and
  19. >I've been a little stumped figuring out how I would implement this in the
  20. >general case.  Writing code into mknode() to fold the addition or
  21. >multiplication of two nodes with constant value makes sense to me, for
  22. >example.  But what about an expression such as "4 + y + 4"?  As I see it,
  23. >a shift-reduce parser following the rule
  24.  
  25. >expr -> expr + expr
  26. >      | CONST
  27. >      | ID
  28.  
  29. Watch out, this optimization may be inherently unsafe since you change the
  30. semantical meaning between the optimized version and the non-optimized
  31. version in such a way that different results may be produced. Just two
  32. short examples:
  33.  
  34. 1) x/12 * 12
  35. You may reduce this to x, but what happens, if x is not divisible by 12?
  36. In this case you obviously got a clash between the mathematical meaning of
  37. the expression and the 'procedural' meaning the programmer wanted to
  38. achieve.
  39.  
  40. 2) assume the maximal size of int be 16!
  41. 12 + x + 5
  42. depending on x and the implementation you might get different results,
  43. maybe even the compiler complains while folding the constants. However the
  44. writer might have known that x is always strictly less than -1!
  45. -- 
  46. Send compilers articles to compilers@iecc.cambridge.ma.us or
  47. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  48.