<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Reading a register for its side effects in C and C++</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Mon, 06 Sep 2010 14:57:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-1563</link>
		<dc:creator>Ashleigh</dc:creator>
		<pubDate>Mon, 16 Aug 2010 08:34:38 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-1563</guid>
		<description>I think you will find it depends on compiler and optimiation level. In a number of cases I&#039;ve seen the assignment optimised out of existence, for C compilers not C++!</description>
		<content:encoded><![CDATA[<p>I think you will find it depends on compiler and optimiation level. In a number of cases I&#8217;ve seen the assignment optimised out of existence, for C compilers not C++!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Regehr</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-799</link>
		<dc:creator>John Regehr</dc:creator>
		<pubDate>Sat, 22 May 2010 19:06:24 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-799</guid>
		<description>Hi Daniel.

You are arguing that it is implementation-defined whether a C compiler performs side-effects while evaluating an expression.  I&#039;ll first try to show you that this interpretation does not make sense, and second back this up using the standard.

You are arguing that when a C implementation evaluates an expression like this:

x;

(where x is volatile), the implementation is not required to perform the volatile access.  Let us look at a different expression:

1+printf(&quot;hello&quot;);

You are arguing that the compiler does not need to print anything when evaluating this expression.  Clearly this is wrong.

Next let&#039;s look at the standard.  I&#039;m using N1425.  You are interpreting &quot;may&quot; as &quot;evaluation of a side-effecting expression (in the abstract machine) may or may not produce side effects in the actual computation.&quot;  This seems to be a misreading.  I read it as &quot;evaluating some expressions (like the examples above) produces side effects, whereas other expressions have no side effects.&quot;

If you look at the standard you&#039;ll see that &quot;shall&quot; and &quot;shall not&quot; have a codified meaning, whereas &quot;may&quot; does not.  It is plain English.  For example the standard says &quot;Attention is drawn to the possibility that some of the elements of this document MAY be the subject of patent rights.&quot;  Clearly this is a plain English &quot;may&quot; and not some codified requirement about a C implementation.

If I haven&#039;t convinced you, let me know, and we can continue to argue.  However, since your interpretation leads to a useless language I cannot see how it could be a valid reading of the standard.</description>
		<content:encoded><![CDATA[<p>Hi Daniel.</p>
<p>You are arguing that it is implementation-defined whether a C compiler performs side-effects while evaluating an expression.  I&#8217;ll first try to show you that this interpretation does not make sense, and second back this up using the standard.</p>
<p>You are arguing that when a C implementation evaluates an expression like this:</p>
<p>x;</p>
<p>(where x is volatile), the implementation is not required to perform the volatile access.  Let us look at a different expression:</p>
<p>1+printf(&#8220;hello&#8221;);</p>
<p>You are arguing that the compiler does not need to print anything when evaluating this expression.  Clearly this is wrong.</p>
<p>Next let&#8217;s look at the standard.  I&#8217;m using N1425.  You are interpreting &#8220;may&#8221; as &#8220;evaluation of a side-effecting expression (in the abstract machine) may or may not produce side effects in the actual computation.&#8221;  This seems to be a misreading.  I read it as &#8220;evaluating some expressions (like the examples above) produces side effects, whereas other expressions have no side effects.&#8221;</p>
<p>If you look at the standard you&#8217;ll see that &#8220;shall&#8221; and &#8220;shall not&#8221; have a codified meaning, whereas &#8220;may&#8221; does not.  It is plain English.  For example the standard says &#8220;Attention is drawn to the possibility that some of the elements of this document MAY be the subject of patent rights.&#8221;  Clearly this is a plain English &#8220;may&#8221; and not some codified requirement about a C implementation.</p>
<p>If I haven&#8217;t convinced you, let me know, and we can continue to argue.  However, since your interpretation leads to a useless language I cannot see how it could be a valid reading of the standard.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-795</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Fri, 21 May 2010 14:33:51 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-795</guid>
		<description>I have some doubt in this article. Can you please cite where in the C standard the code given has an &quot;unambiguous meaning&quot;?

In ISO C 9899:1999 5.1.2.3 &quot;Program execution&quot; we can read that accessing a volatile object is a &quot;side effect&quot; and that &quot;Evaluation of an expression may produce side effects&quot;. 

It says &quot;may&quot; and not &quot;shall&quot;. 

Then in 6.8.3 &quot;Expression and null statements&quot; we can read that an expression is evaluated for its side effects, although the result is ignored (void expression).

So the expression will be -evaluated- but there is no guarantee for side effects, i.e actual access of the volatile. So I don&#039;t think the C standard is &quot;unambiguous&quot;, on the contrary I think the standard is pretty clear that this is implementation-defined behavior. Feel free to cite the standard to prove me wrong.

---

For your information, I tested cload_reg1() and cload_reg2() on: 
Freescale Codewarrior 4.7 for HCS12
Freescale Codewarrior 6.0 for HCS08. 

- Optimization was enabled in both cases. 
- On both compilers code is generated to access the variable. 
- On both compilers I get a warning for &quot;result not used&quot;.</description>
		<content:encoded><![CDATA[<p>I have some doubt in this article. Can you please cite where in the C standard the code given has an &#8220;unambiguous meaning&#8221;?</p>
<p>In ISO C 9899:1999 5.1.2.3 &#8220;Program execution&#8221; we can read that accessing a volatile object is a &#8220;side effect&#8221; and that &#8220;Evaluation of an expression may produce side effects&#8221;. </p>
<p>It says &#8220;may&#8221; and not &#8220;shall&#8221;. </p>
<p>Then in 6.8.3 &#8220;Expression and null statements&#8221; we can read that an expression is evaluated for its side effects, although the result is ignored (void expression).</p>
<p>So the expression will be -evaluated- but there is no guarantee for side effects, i.e actual access of the volatile. So I don&#8217;t think the C standard is &#8220;unambiguous&#8221;, on the contrary I think the standard is pretty clear that this is implementation-defined behavior. Feel free to cite the standard to prove me wrong.</p>
<p>&#8212;</p>
<p>For your information, I tested cload_reg1() and cload_reg2() on:<br />
Freescale Codewarrior 4.7 for HCS12<br />
Freescale Codewarrior 6.0 for HCS08. </p>
<p>- Optimization was enabled in both cases.<br />
- On both compilers code is generated to access the variable.<br />
- On both compilers I get a warning for &#8220;result not used&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Sawyer</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-751</link>
		<dc:creator>Jim Sawyer</dc:creator>
		<pubDate>Tue, 04 May 2010 23:09:54 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-751</guid>
		<description>Interesting response from the vendors.

Because the premise:

  &quot; The operation that turns into a load instruction in the executable code is what the C++ standard calls
     the lvalue-to-rvalue conversion;...  &quot;

does not apply to the context:

  &quot;volatile&quot;

since &quot;volatile&quot; values NEVER convert to rvalues:

  &quot; an rvalue (or just value; something whose address can’t be taken and can be in a register) &quot;

The disconnect seems to occur at the phrase

  &quot; can be in a register &quot;

which is of course still true for volatile values,
but misleading, because the (missing?) phrase

  &quot; can be cached in a register &quot;

is true for rvalues, but false for volatile values.

Therefore, the spec is apparently silent
on the issue of LOADing volatile values.

That said, if I were talking to volatile registers,
I&#039;d be inclined to consider the C spec in error.

I might have tried something like:

void cload_reg (void)
{
   register unsigned char sink;
   sink = SBUF;
}

expecting it to work in all cases.

Truth be told, I&#039;ve used &quot;volatile&quot; in proprietary languages,
e.g. PLUS, because it was reliable -- the compiler guy was
right down the hall, and beatings were seldom required ;-)

For C, I&#039;ve always punted, and done volatile access in ASM.</description>
		<content:encoded><![CDATA[<p>Interesting response from the vendors.</p>
<p>Because the premise:</p>
<p>  &#8221; The operation that turns into a load instruction in the executable code is what the C++ standard calls<br />
     the lvalue-to-rvalue conversion;&#8230;  &#8221;</p>
<p>does not apply to the context:</p>
<p>  &#8220;volatile&#8221;</p>
<p>since &#8220;volatile&#8221; values NEVER convert to rvalues:</p>
<p>  &#8221; an rvalue (or just value; something whose address can’t be taken and can be in a register) &#8221;</p>
<p>The disconnect seems to occur at the phrase</p>
<p>  &#8221; can be in a register &#8221;</p>
<p>which is of course still true for volatile values,<br />
but misleading, because the (missing?) phrase</p>
<p>  &#8221; can be cached in a register &#8221;</p>
<p>is true for rvalues, but false for volatile values.</p>
<p>Therefore, the spec is apparently silent<br />
on the issue of LOADing volatile values.</p>
<p>That said, if I were talking to volatile registers,<br />
I&#8217;d be inclined to consider the C spec in error.</p>
<p>I might have tried something like:</p>
<p>void cload_reg (void)<br />
{<br />
   register unsigned char sink;<br />
   sink = SBUF;<br />
}</p>
<p>expecting it to work in all cases.</p>
<p>Truth be told, I&#8217;ve used &#8220;volatile&#8221; in proprietary languages,<br />
e.g. PLUS, because it was reliable &#8212; the compiler guy was<br />
right down the hall, and beatings were seldom required <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>For C, I&#8217;ve always punted, and done volatile access in ASM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-661</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Wed, 07 Apr 2010 10:58:25 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-661</guid>
		<description>Thanks Ignacio - much appreciated.</description>
		<content:encoded><![CDATA[<p>Thanks Ignacio &#8211; much appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ignacio G. T.</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-660</link>
		<dc:creator>Ignacio G. T.</dc:creator>
		<pubDate>Wed, 07 Apr 2010 07:50:33 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-660</guid>
		<description>Renesas C compiler for M32C

I tested M32C/90,80,M16C/80,70 Series C Compiler V.5.41 Release 01, hosted on Windows XP, using maximum speed optimization (-O3 -Os). The C compiler performed the load in all four cases. No C++ compiler is available.</description>
		<content:encoded><![CDATA[<p>Renesas C compiler for M32C</p>
<p>I tested M32C/90,80,M16C/80,70 Series C Compiler V.5.41 Release 01, hosted on Windows XP, using maximum speed optimization (-O3 -Os). The C compiler performed the load in all four cases. No C++ compiler is available.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-527</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Sun, 28 Mar 2010 14:50:44 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-527</guid>
		<description>Although I agree using assembly language ensures you get what you wrote (if not what you wanted!), I&#039;m not sure it&#039;s the solution here (at least for C programs). The C language specification is quite clear about what the compiler should do in this circumstance. Thus the question becomes where do you draw the line, because in the limit one would have to write entirely in assembly language.  Changing to your off topic comment. What scripting language did you use? I ask because in my CFT (copious free time) I&#039;m intending to become proficient in a scripting language, with Perl being the current front runner.</description>
		<content:encoded><![CDATA[<p>Although I agree using assembly language ensures you get what you wrote (if not what you wanted!), I&#8217;m not sure it&#8217;s the solution here (at least for C programs). The C language specification is quite clear about what the compiler should do in this circumstance. Thus the question becomes where do you draw the line, because in the limit one would have to write entirely in assembly language.  Changing to your off topic comment. What scripting language did you use? I ask because in my CFT (copious free time) I&#8217;m intending to become proficient in a scripting language, with Perl being the current front runner.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter B</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-521</link>
		<dc:creator>Peter B</dc:creator>
		<pubDate>Sat, 27 Mar 2010 23:58:45 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-521</guid>
		<description>This is why I do low-level code in assembly.  I KNOW what it does.  

When calling assembly code from C++ I often must use extern &quot;C&quot; ... because the assembler refuses to create/use externals like “fifoInit()” for the C++ “void fifoInit(void)”.  At least the linker error is obvious.

My thinking goes like this --

Any change in chip vendor such as between Luminary Micro and ST for Cortex-M3 is far from a “recompile and you are done” effort.  Thus microcontroller vendor unique code will have to be rethought anyway.

My assembly language skills have other uses.  Once I had to resolve a struct that appeared to act differently under C and C++.  The problem was “typedef BOOL” in C++ it turned into bool, while in C is was unsigned int.  (This was 32-bit x86 code.)  Nobody else realized that there was no reason for bool to be 32 bits.  A quick conformation using sizeof in a C and C++ module confirmed my supposition.  Changed int to char and all was well.

Please allow a somewhat OT drift

While employed at a disk drive company I worked on an embedded drive test system.  I was responsible for buffer allocation.  The system featured a script from which the actual tests were called.  My buffer allocation ran as script pre-allocating one large chunk of RAM then splitting this RAM into various sized buffers.  Script said how many buffers of each size.  There was zero heap allocation/deallocation at run time.</description>
		<content:encoded><![CDATA[<p>This is why I do low-level code in assembly.  I KNOW what it does.  </p>
<p>When calling assembly code from C++ I often must use extern &#8220;C&#8221; &#8230; because the assembler refuses to create/use externals like “fifoInit()” for the C++ “void fifoInit(void)”.  At least the linker error is obvious.</p>
<p>My thinking goes like this &#8211;</p>
<p>Any change in chip vendor such as between Luminary Micro and ST for Cortex-M3 is far from a “recompile and you are done” effort.  Thus microcontroller vendor unique code will have to be rethought anyway.</p>
<p>My assembly language skills have other uses.  Once I had to resolve a struct that appeared to act differently under C and C++.  The problem was “typedef BOOL” in C++ it turned into bool, while in C is was unsigned int.  (This was 32-bit x86 code.)  Nobody else realized that there was no reason for bool to be 32 bits.  A quick conformation using sizeof in a C and C++ module confirmed my supposition.  Changed int to char and all was well.</p>
<p>Please allow a somewhat OT drift</p>
<p>While employed at a disk drive company I worked on an embedded drive test system.  I was responsible for buffer allocation.  The system featured a script from which the actual tests were called.  My buffer allocation ran as script pre-allocating one large chunk of RAM then splitting this RAM into various sized buffers.  Script said how many buffers of each size.  There was zero heap allocation/deallocation at run time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-500</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Thu, 25 Mar 2010 12:04:14 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-500</guid>
		<description>Amol: Thanks for the information. Did you compile the code as C or C++? Were there any differences between the two?</description>
		<content:encoded><![CDATA[<p>Amol: Thanks for the information. Did you compile the code as C or C++? Were there any differences between the two?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amol</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/03/reading-a-register-for-its-side-effects-in-c-and-c/comment-page-1/#comment-496</link>
		<dc:creator>Amol</dc:creator>
		<pubDate>Thu, 25 Mar 2010 02:42:36 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=224#comment-496</guid>
		<description>I tried with &quot;CodeWarrior C/C++ for ColdFire&quot; Version 5.2 Build 26 with Optimization level 4  (Best optimization level). It generated load in all the four cases AND it also gave a warning in cload_reg1() and cload_reg3() that &quot;expression has no side effect&quot;</description>
		<content:encoded><![CDATA[<p>I tried with &#8220;CodeWarrior C/C++ for ColdFire&#8221; Version 5.2 Build 26 with Optimization level 4  (Best optimization level). It generated load in all the four cases AND it also gave a warning in cload_reg1() and cload_reg3() that &#8220;expression has no side effect&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
