Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!purdue!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.std.c Subject: Re: Semi constant expressions Keywords: expressions, optimization, code generation Message-ID: <10937@smoke.BRL.MIL> Date: 5 Sep 89 16:45:04 GMT References: <1237@gmdzi.UUCP> <10885@smoke.BRL.MIL> <242@ssp1.idca.tds.philips.nl> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 29 In article <242@ssp1.idca.tds.philips.nl> dolf@idca.tds.PHILIPS.nl (Dolf Grunbauer) writes: >In article <10885@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >>In article <1237@gmdzi.UUCP> wittig@gmdzi.UUCP (Georg Wittig) writes: >>> 0 * x (x non-constant) >>>Does (p)ANSI C say anything about which code should be generated for such >>>expressions? >>This falls naturally out of the specification. The operands ARE evaluated, >>so if `x' has side-effects they do occur. If evaluation would produce what >>the Standard labels as "undefined behavior", then the implementation can do >>anything it likes including taking short cuts, but in cases where the >>behavior is well defined the only short cuts permitted are those that >>produce the same results as doing it the long way. >How can you tell what will happen when 'doing it the long way' ? I thought this was clear from what I said and from the context. In the case 0 * x if evaluation of the expression `x' would have observable side effects then they MUST occur for a correct implementation of C. In such a case, an implementation that replaced the expression "0 * x" with the constant "0" as an "optimization" would not be standard conforming. The run-time behavior of a C program is defined in terms of the operation of a "virtual machine" (with some deliberately fuzzy areas in its specification, so it's really a class of virtual machines) and input/output operations (which are usually what makes the program's behavior observable). A correct implementation must produce observable results identical to those that would be produced by the virtual machine (actually, by some member of the class of virtual machines).