<div dir="ltr"><div>I think you're on the right track with address spaces, but you need to modify clang/lib/Basic/Targets.cpp to override TargetInfo::getPointerWidthV to give a different size.</div><div><br></div><div>If you go down this road, consider making __ptr32 and __ptr64 work better. MSVC accepts this code but clang rejects:</div><div><br></div><div><div>struct Foo { int *__ptr32 p; };</div><div>int *__ptr32 p;</div><div>int *__ptr64 q;</div><div>static_assert(sizeof(Foo) == 4, "Foo");</div><div>static_assert(sizeof(p) == 4, "p");</div><div>static_assert(sizeof(q) == 8, "q");</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 2, 2017 at 8:05 AM, Patrik Eklöf 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">
<div lang="EN-GB" link="#0563C1" vlink="#954F72">
<div class="m_3509826034501417726WordSection1">
<p class="MsoNormal"><a name="m_3509826034501417726__Toc454264584">Dear community,<u></u><u></u></a></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>I am looking at trying to use clang in such a way that it is possible to use both 32-bit and 64-bit pointers in the same translation unit. Is this currently supported?<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>I would like to enable it on a per-pointer basis, and it would be ideal if it is part of the type. I am thinking that one would do something like…<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>[[ptr_32]] int* p;<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>…or the like to tell the compiler specifically that it is a 32-bit pointer (the rest will be 64-bit). I am compiling to a 64-bit target, so all pointers should natively be 64-bit.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>I’ve been looking over the source a little, and see references to address space and an attribute __address_space__ that enables one to choose the address space for variables. It seems that depending
on the address space, different sizes for pointers can be used. I tried modifying the DataLayout::reset function (I realize I should probably modify the targets instead of the layout class itself) to add another entry to the Pointers vector:<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span><span style="font-size:9.5pt;font-family:Consolas;color:green">setPointerAlignment(0, 8, 8, 8);</span></span><span><span style="font-size:9.5pt;font-family:Consolas;color:black"><u></u><u></u></span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span><span style="font-size:9.5pt;font-family:Consolas;color:green">setPointerAlignment(4, 4, 4, 4);</span></span><span><span style="font-size:9.5pt;font-family:Consolas;color:black"><u></u><u></u></span></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>…and using the address space extension to pick out the right size for some pointers:<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>__attribute__((address_space(<wbr>0))) int* HiIAmA64BitPtr{ (int*)0xFFFF'FFFF'FFFF'FFFF };<u></u><u></u></span></p>
<p class="MsoNormal"><span>__attribute__((address_space(<wbr>4))) int* HiIAmA32BitPtr{ (__attribute__((address_space(<wbr>4))) int*)0xFFFF'FFFF'FFFF'FFFF };<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>But that did not appear to work. When running the code and printing the size of the variables, I see that they are both 8 bytes:<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>std::cout << "Size of 64-bit ptr: " << sizeof(HiIAmA64BitPtr) << std::endl; // Prints 8<u></u><u></u></span></p>
<p class="MsoNormal"><span>std::cout << "Size of 32-bit ptr: " << sizeof(HiIAmA32BitPtr) << std::endl; // Prints 8<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Furthermore, for some strange reason, if I print the LLVM IR using clang++ -S -emit-llvm, I see that all pointers seem to be 32 bits long even without using the address space extension. Yet, a long
long variable (called Test here) has a 64-bit size:<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>target triple = "x86_64-pc-windows-msvc19.0.0"<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>@"\01?HiIAmA64BitPtr@@3PEAHEA" = global i32* inttoptr (i64 -1 to i32*), align 8<u></u><u></u></span></p>
<p class="MsoNormal"><span>@"\01?HiIAmA32BitPtr@@3PEAHEA" = global i32 addrspace(4)* inttoptr (i64 -1 to i32 addrspace(4)*), align 8<u></u><u></u></span></p>
<p class="MsoNormal"><span>@"\01?g_Test@@3_KA" = global i64 0, align 8<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>This seems very odd to me. Can someone point me in the right direction on how to approach this?<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>My command line for compiling:<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>clang++ "Test.cpp" -std=c++1z -Wall -fms-compatibility-version=19 -c -o Test.o && "link.exe" -out:Test.exe -defaultlib:libcmt -nologo Test.o<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>(For some reason, clang is unable to invoke the linker correctly on its own.)<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-top:12.0pt;page-break-after:avoid"><span><span style="font-size:16.0pt;color:#a5a5a5">Regards,<br>
Patrik Eklöf</span></span><span></span><span><u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
<br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>