<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Dec 1, 2015 at 10:16 AM, Jeffrey Walton via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Everyone,<br>
<br>
(Re-asking on CFE-Dev). We took a bug report on some line assembly<br>
code (<a href="https://github.com/weidai11/cryptopp/issues/72" rel="noreferrer" target="_blank">https://github.com/weidai11/cryptopp/issues/72</a>). I have two<br>
questions at the end on ILP32, Clang and Apple.<br>
<br>
The code that took the bug performs the following:<br>
<br>
        asm volatile<br>
        (<br>
            // save ebx in case -fPIC is being used<br>
# if BOOL_X32 || BOOL_X64<br>
            "pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"<br>
# else<br>
            "push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"<br>
# endif<br>
            : "=a" (output[0]), "=D" (output[1]), "=c" (output[2]),<br>
"=d" (output[3])<br>
            : "a" (input), "c" (0)<br>
        );<br>
<br>
The BOOL_* defines are mutually exclusive. BOOL_X32 is set when<br>
__ILP32__ is defined. BOOL_X86 is defined when __i386__ (and friends)<br>
is defined. BOOL_X64 is defined when __x86_64__ (and friends) is<br>
defined. We set the defines based on " System V Application Binary<br>
Interface, AMD64 (With LP64 and ILP32 Programming Models)" [1].<br></blockquote><div><br></div><div>Since no-one else has explicitly pointed this out: this is the wrong document. When you're targeting i386, the contents of the x86_64 psABI document are irrelevant, since it is not the ABI document for your platform. When targeting x86-64, we'll define __ILP32__ as specified in that document (and it's a bug if we deviate from that), but when targeting i386, we get to define it however we think is best (and it makes a lot of sense to define it, because i386 is an ILP32 target) since it's a reserved identifier.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The compiler error is:<br>
<br>
    $ make cpu.o<br>
    clang++ -DNDEBUG -g2 -O2 -arch i386 -fPIC -march=native -pipe -c cpu.cpp<br>
    cpu.cpp:104:4: error: register %rbx is only available in 64-bit mode<br>
                            "pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"<br>
                           ^<br>
    <inline asm>:1:8: note: instantiated into assembly here<br>
           pushq %rbx; cpuid; mov %ebx, %edi; popq %rbx<br>
              ^~~~~<br>
    cpu.cpp:104:4: error: register %rbx is only available in 64-bit mode<br>
                           "pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"<br>
                            ^<br>
    <inline asm>:1:42: note: instantiated into assembly here<br>
           pushq %rbx; cpuid; mov %ebx, %edi; popq %rbx<br>
                                                    ^~~~<br>
    2 errors generated.<br>
<br>
It appears Clang sets ILP32 related defines when using -arch i386 on x86_64:<br>
<br>
    $ clang++ -arch i386 -dM -E - < /dev/null | egrep -i "(86|64|ilp)"<br>
    #define _ILP32 1<br>
    #define __ILP32__ 1<br>
    ...<br>
    #define __i386 1<br>
    #define __i386__ 1<br>
    #define i386 1<br>
<br>
As far as I know, when using ILP32 on x86_64, we must interact with<br>
the stack using 64-bit values and registers.<br>
<br>
My first question is, is _ILP32_ and _i386_ supposed to be defined<br>
together? According to the docs I found, its not an expected/valid<br>
configuration. _ILP32_ and _x86_64_ is an expected/valid<br>
configuration.<br>
<br>
My second question is, how should I work around this? Should I special<br>
case Clang on Apple and attempt to push a 32-bit value? Or should I do<br>
something else?<br>
<br>
Thanks in advance.<br>
<br>
*****<br>
<br>
A related bug report was opened at "LLVM Bug 25688 - Clang defines<br>
__ILP32__ when using -arch i386 on an x86_64 Intel",<br>
<a href="https://llvm.org/bugs/show_bug.cgi?id=25688" rel="noreferrer" target="_blank">https://llvm.org/bugs/show_bug.cgi?id=25688</a>.<br>
<br>
*****<br>
<br>
Here are the docs I found, but its not clear to me what document is<br>
controlling in this case.<br>
<br>
The "OS X ABI Function Call Guide" [0] does not discuss it, and often<br>
defers to the System V guides.<br>
<br>
The "System V Application Binary Interface, AMD64 (With LP64 and ILP32<br>
Programming Models)" [1] states we should see either (1) __ILP32__<br>
alone; or (2) both __ILP32__ and __x86_64__ (some hand waiving).<br>
<br>
The "SYSTEM V APPLICATION BINARY INTERFACE for Intel 386" [2] does not<br>
discuss it, and does not mention __ILP32__.<br>
<br>
[0] <a href="http://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/Mac_OS_X_ABI_Function_Calls.pdf" rel="noreferrer" target="_blank">http://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/Mac_OS_X_ABI_Function_Calls.pdf</a><br>
[1] <a href="http://sites.google.com/site/x32abi/documents/abi.pdf" rel="noreferrer" target="_blank">http://sites.google.com/site/x32abi/documents/abi.pdf</a><br>
[2] <a href="http://www.sco.com/developers/devspecs/abi386-4.pdf" rel="noreferrer" target="_blank">http://www.sco.com/developers/devspecs/abi386-4.pdf</a><br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div><br></div></div>