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