<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Portability Blog</title>
    <link>http://portabilityblog.com/blog/</link>
    <description>tales about building software on many platforms</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.2.1 - http://www.s9y.org/</generator>
    <managingEditor>df.portabilityblog@erinye.com</managingEditor>
<webMaster>df.portabilityblog@erinye.com</webMaster>
<pubDate>Wed, 23 Jun 2010 06:34:21 GMT</pubDate>

    <image>
        <url>http://portabilityblog.com/blog/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: Portability Blog - tales about building software on many platforms</title>
        <link>http://portabilityblog.com/blog/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Perl exec</title>
    <link>http://portabilityblog.com/blog/archives/12-Perl-exec.html</link>
            <category>Compilers</category>
            <category>Operating Systems</category>
    
    <comments>http://portabilityblog.com/blog/archives/12-Perl-exec.html#comments</comments>
    <wfw:comment>http://portabilityblog.com/blog/wfwcomment.php?cid=12</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://portabilityblog.com/blog/rss.php?version=2.0&amp;type=comments&amp;cid=12</wfw:commentRss>
    

    <author>nospam@example.com (Daniel Fischer)</author>
    <content:encoded>
    Perl is a popular scripting language in the UNIX world, and there are several ports available for Windows. If your UNIX project uses perl, then porting that code to Windows is fairly painless. Perl doesn&#039;t attempt to be entirely portable, but most of the differences are quite obvious.&lt;br /&gt;
&lt;br /&gt;
One that is apparently not obvious is perl&#039;s exec function. I&#039;ve seen this happen to two different projects recently, in short succession. The symptom was that a process was started by a perl script, but no output from it was recorded on Windows. On UNIX, everything worked as expected.&lt;br /&gt;
&lt;br /&gt;
This turns out to be not very complicated. On UNIX, exec is implemented in terms of the execl()/execv() family of syscalls. This means the child process replaces the parent, and any process waiting for that parent automatically waits for the child instead. On Windows, there is no such thing. Windows perl implements exec by spawning a child process and returning control to the parent immediately, which then exits. Any process waiting for the parent regains control instantly and is left unaware of the child. A script written for UNIX will assume that the work is done before it has even started.&lt;br /&gt;
&lt;br /&gt;
There is a simple workaround: Use system (plus exit, if necessary) instead of exec. While the semantics are slightly different from exec, they are at least the same on both platforms; system spawns a new process and then waits for it, returning its exit code. Notice that system has a list form too so you don&#039;t need to worry about shell escaping either. 
    </content:encoded>

    <pubDate>Wed, 23 Jun 2010 08:34:21 +0200</pubDate>
    <guid isPermaLink="false">http://portabilityblog.com/blog/archives/12-guid.html</guid>
    <category>perl</category>
<category>unix</category>
<category>windows</category>

</item>
<item>
    <title>Bad register name &quot;dil&quot; (or &quot;sil&quot;)</title>
    <link>http://portabilityblog.com/blog/archives/11-Bad-register-name-dil-or-sil.html</link>
            <category>CPUs</category>
    
    <comments>http://portabilityblog.com/blog/archives/11-Bad-register-name-dil-or-sil.html#comments</comments>
    <wfw:comment>http://portabilityblog.com/blog/wfwcomment.php?cid=11</wfw:comment>

    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://portabilityblog.com/blog/rss.php?version=2.0&amp;type=comments&amp;cid=11</wfw:commentRss>
    

    <author>nospam@example.com (Daniel Fischer)</author>
    <content:encoded>
    This is a piece of code from a project that runs on both the x86 and x86_64 architectures:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-size:9pt;&quot;&gt;&lt;span style=&quot;color:#808080&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color:#8f0055&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;swap_int&lt;/span&gt;&lt;span style=&quot;color:#000000&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8f0055&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;*&lt;/span&gt;a&lt;span style=&quot;color:#000000&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;int&lt;/span&gt; b&lt;span style=&quot;color:#000000&quot;&gt;) {&lt;/span&gt;
&lt;span style=&quot;color:#808080&quot;&gt; 2 &lt;/span&gt;    &lt;span style=&quot;color:#8f0055&quot;&gt;asm&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;volatile&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#c00000&quot;&gt;&amp;quot;xchg %0, %1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color:#c00000&quot;&gt;&amp;quot;+r&amp;quot;&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;(&lt;/span&gt;b&lt;span style=&quot;color:#000000&quot;&gt;) ,&lt;/span&gt; &lt;span style=&quot;color:#c00000&quot;&gt;&amp;quot;+m&amp;quot;&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;(*&lt;/span&gt;a&lt;span style=&quot;color:#000000&quot;&gt;));&lt;/span&gt;
&lt;span style=&quot;color:#808080&quot;&gt; 3 &lt;/span&gt;    &lt;span style=&quot;color:#8f0055&quot;&gt;return&lt;/span&gt; b&lt;span style=&quot;color:#000000&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color:#808080&quot;&gt; 4 &lt;/span&gt;&lt;span style=&quot;color:#000000&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
It&#039;s fairly easy to see what it does: It swaps two values of type &lt;code&gt;int&lt;/code&gt;. This code works perfectly fine on both architectures, provided that you&#039;re using a compiler that understands the &lt;code&gt;asm&lt;/code&gt; statement, such as gcc. Later, this similar piece of code appears:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-size:9pt;&quot;&gt;&lt;span style=&quot;color:#808080&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color:#8f0055&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;swap_char&lt;/span&gt;&lt;span style=&quot;color:#000000&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#8f0055&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;*&lt;/span&gt;a&lt;span style=&quot;color:#000000&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;char&lt;/span&gt; b&lt;span style=&quot;color:#000000&quot;&gt;) {&lt;/span&gt;
&lt;span style=&quot;color:#808080&quot;&gt; 2 &lt;/span&gt;    &lt;span style=&quot;color:#8f0055&quot;&gt;asm&lt;/span&gt; &lt;span style=&quot;color:#8f0055&quot;&gt;volatile&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#c00000&quot;&gt;&amp;quot;xchg %0, %1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color:#c00000&quot;&gt;&amp;quot;+r&amp;quot;&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;(&lt;/span&gt;b&lt;span style=&quot;color:#000000&quot;&gt;) ,&lt;/span&gt; &lt;span style=&quot;color:#c00000&quot;&gt;&amp;quot;+m&amp;quot;&lt;/span&gt; &lt;span style=&quot;color:#000000&quot;&gt;(*&lt;/span&gt;a&lt;span style=&quot;color:#000000&quot;&gt;));&lt;/span&gt;
&lt;span style=&quot;color:#808080&quot;&gt; 3 &lt;/span&gt;    &lt;span style=&quot;color:#8f0055&quot;&gt;return&lt;/span&gt; b&lt;span style=&quot;color:#000000&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color:#808080&quot;&gt; 4 &lt;/span&gt;&lt;span style=&quot;color:#000000&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
This code still compiles and works just fine. For the most part. But when optimisation is turned on, you may get this error from gcc when building for x86:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;code&gt;Error: bad register name `%dil&#039;&lt;/code&gt;&lt;/blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
Then you try the same on x86_64 and the error is gone again. No wonder: As opposed to x86, the x86_64 architecture actually has the %dil register.&lt;br /&gt;
&lt;br /&gt;
At first, this appears to be a compiler bug. After all, the compiler is choosing a register that doesn&#039;t exist on x86. On closer look, it&#039;s a bug in the example code. The issue is that for the &lt;code&gt;b&lt;/code&gt; argument, the constraint &lt;code&gt;r&lt;/code&gt; is used, indicating that the value should be stored in any general-purpose register. In the first example, this is just fine. All of them will do for 32-bit operations. The second example, on closer examination, actually requires a register whose lower byte is accessible. On x86, there are only four general-purpose registers where this is true: EAX, EBX, ECX and EDX. On x86_64, this is also true for the ESI and EDI registers that are also treated as general-purpose registers on x86.&lt;br /&gt;
&lt;br /&gt;
So what happens is that the compiler correctly chooses the &lt;code&gt;%edi&lt;/code&gt; register, which satisfies the &lt;code&gt;r&lt;/code&gt; constraint. Later, the &lt;code&gt;xchg&lt;/code&gt; instruction is interpreted as referring to two byte-sized values due to the size of the arguments &lt;code&gt;*a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. Thus, the compiler translates the instruction to its 8-bit form and replaces the register placeholder &lt;code&gt;%0&lt;/code&gt; with the 8-bit form of the &lt;code&gt;%edi&lt;/code&gt; register, which is &lt;code&gt;%dil&lt;/code&gt;. During assembly, this fails because &lt;code&gt;%dil&lt;/code&gt; doesn&#039;t actually exist on x86.&lt;br /&gt;
&lt;br /&gt;
If there is a compiler bug, it is only that the error output is misleading. It shouldn&#039;t even try to use &lt;code&gt;%dil&lt;/code&gt;, it should warn about the real problem. &lt;br /&gt;
&lt;br /&gt;
The real bug is that in the example source, a byte-sized argument was qualified with a constraint that allowed any general-purpose register to be used, where instead, the set should be constrained to registers whose lower byte is available. In gcc, this can be achieved by using the &lt;code&gt;q&lt;/code&gt; constraint instead of &lt;code&gt;r&lt;/code&gt;. 
    </content:encoded>

    <pubDate>Fri, 29 May 2009 13:09:23 +0200</pubDate>
    <guid isPermaLink="false">http://portabilityblog.com/blog/archives/11-guid.html</guid>
    <category>32 bit</category>
<category>64 bit</category>
<category>gcc</category>
<category>x86</category>
<category>x86_64</category>

</item>

</channel>
</rss>