<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>experimentalworks &#187; gcc</title>
	<atom:link href="http://blog.experimentalworks.net/tag/gcc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.experimentalworks.net</link>
	<description></description>
	<lastBuildDate>Wed, 14 Jul 2010 15:35:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Calling Conventions &#8211; when you need to know C to understand PHP</title>
		<link>http://blog.experimentalworks.net/2009/07/calling-conventions-when-you-need-to-know-c-to-understand-php/</link>
		<comments>http://blog.experimentalworks.net/2009/07/calling-conventions-when-you-need-to-know-c-to-understand-php/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 16:26:59 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[gcc]]></category>

		<guid isPermaLink="false">http://blog.experimentalworks.net/?p=260</guid>
		<description><![CDATA[I think most of the people using PHP wonder from time t [...]]]></description>
			<content:encoded><![CDATA[<p>I think most of the people using PHP wonder from time to time about particular behavior of the language. That&#8217;s pretty much the same case with every language. Pythoneers have their <em>wtf</em> moments, Ruby programmers have their wtf moments and C programmers tend to live in a whole wtf universe. But lately I stumbled over a nice one. It looked like a bug in PHP, but turns out to be an interesting, curious, part of the C-language. Imagine the following PHP code sample and note that $a and $b are not defined (yeah I know, it&#8217;s bad coding style..blabla..):<br />
<code><br />
&lt;?php<br />
var_dump($a + $b);<br />
?&gt;<br />
</code><br />
What is the expected result with error_reporting set to E_ALL?<br />
<code><br />
PHP Notice: Undefined variable: b in /var/foo/bla on line 1<br />
PHP Notice: Undefined variable: a in /var/foo/bla on line 1<br />
</code>.<br />
Are you sure? I&#8217;m not. On x86 hardware b is fetched before a is fetched and therefore the executor detects that b is not set first. But wait. Let&#8217;s test this on a SPARC machine:<br />
<code><br />
PHP Notice: Undefined variable: a in /var/foo/bla on line 1<br />
PHP Notice: Undefined variable: b in /var/foo/bla on line 1<br />
</code>.<br />
What? It evaluates it in the reversed oder? What is happening? So I spend a few minutes with my lovely debugger and it turns out that this is what happens in the engine (I use pseudo code here):</p>
<pre>
result ZEND_ADD_OPCODE()
{
   return add_function(get_op1(), get_op2());
}
</pre>
<p>Voila, that&#8217;s the problem. On SPARC get_op1() is executed before get_op2(), while it&#8217;s the other way round on x86. As get_opX() detects if a variable exists, the error messages appear in reversed oder. I did a little bit research (thank you <a href="http://developers.sun.com/sunstudio/">SunCC Team</a> for your answer!) and it turned out, that <a href="http://en.wikipedia.org/wiki/C99">C99</a> doesn&#8217;t define the way function calls in parameter lists are executed. Therefore, every system and compiler is free to use it&#8217;s own ordering mechanism. My current plan: Write a compiler that does this by random(). The fix is trivial:</p>
<pre>
result ZEND_ADD_OPCODE()
{
  op2 = get_op2();
  return add_function(get_op1(), op2);
}
</pre>
<p>. It&#8217;s a lovely curiosity.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.experimentalworks.net/2009/07/calling-conventions-when-you-need-to-know-c-to-understand-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
