<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 11/1/2016 1:49 PM, Peeter Joot via
llvm-dev wrote:<br>
</div>
<blockquote
cite="mid:K-6Ho0lqVD34wORDnZNBZIylVEzLKCTBaWcLV5pygYLrbZtuJHzznXFPsy7wnu8FCl6iynB__jq_3vrKetsHEStigmTv7S3pA3WD1JZl-aI=@protonmail.com"
type="cite">
<div>I'm attempting to implement a clang/llvm prototype of mixed
mode x86 and x86_64 code. My intent is to be able to generate
"32-bit" code that emulates another 32bit platform (without
porting that code to 64-bit or intel, so that all the pointer
references in that code can still be 32-bit), <span
class="colour" style="color:rgb(34, 34, 34)"><span
class="font" style="font-family:Arial, "Helvetica
Neue", Helvetica, sans-serif"><span class="size"
style="font-size:14px">and run that code in a thread that
explicitly allocates stack in "low memory". </span></span></span>I've
hacked enough of clang and llvm to make this work in a limited
fashion, defining two address spaces:<br>
</div>
<div><br>
</div>
<div><span class="s1"> -p0:32:32:32-p1:64:64:64</span><br>
</div>
<div><br>
</div>
<div>(and following the x64 linux model in some places (but not
all, like the x64 specific ELF format)).<br>
</div>
<div><br>
</div>
<div>I'm now able to generate 32-bit memory model code and execute
it in a 32-bit process. Pointers and stack and data references
are all 32-bit, but the code is able to use 64-bit registers
(like the x64 memory model, but all in the same process).<br>
</div>
<div><br>
</div>
<div>Now, I'm trying to see what's required to call out to 64-bit
code from this "32-bit" code, and access that "high" memory
range for calls to runtime infrastructure implemented 64-bit
shared libraries. I see that I can define types (and indirectly
pointers to those types) in my "far" address space:<br>
</div>
<div><br>
</div>
<div><span class="s1"> typedef int a1int __attribute__( (
address_space( 1 ) ) );</span><br>
</div>
<p class="p1">The int * references in the IR do have the
desired(1) address space, and generated code accessing a 32-bit
pointer vs. a 64-bit pointer looks about right:<br>
</p>
<p class="p1"><span class="s1">00000000000001c0 <addr0>:</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1c0:
<span class="Apple-converted-space"> </span>55<span
class="Apple-converted-space"> </span>push
<span class="Apple-converted-space"> </span>%rbp</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1c1:
<span class="Apple-converted-space"> </span>89 e5 <span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>%esp,%ebp</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1c3:
<span class="Apple-converted-space"> </span>89 7d fc<span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>%edi,-0x4(%rbp)</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1c6:
<span class="Apple-converted-space"> </span>8b 7d fc<span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>-0x4(%rbp),%edi</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1c9:
<span class="Apple-converted-space"> </span>67 8b 07<span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>(%edi),%eax</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1cc:
<span class="Apple-converted-space"> </span>5d<span
class="Apple-converted-space"> </span>pop<span
class="Apple-converted-space"> </span>%rbp</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1cd:
<span class="Apple-converted-space"> </span>c3<span
class="Apple-converted-space"> </span>retq<span
class="Apple-converted-space"> </span></span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1ce:
<span class="Apple-converted-space"> </span>66 90 <span
class="Apple-converted-space"> </span>xchg
<span class="Apple-converted-space"> </span>%ax,%ax</span><br>
</p>
<p class="p2"><br>
</p>
<p class="p1"><span class="s1">00000000000001d0 <addr1>:</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1d0:
<span class="Apple-converted-space"> </span>55<span
class="Apple-converted-space"> </span>push
<span class="Apple-converted-space"> </span>%rbp</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1d1:
<span class="Apple-converted-space"> </span>89 e5 <span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>%esp,%ebp</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1d3:
<span class="Apple-converted-space"> </span>48 89 7d f8 <span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>%rdi,-0x8(%rbp)</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1d7:
<span class="Apple-converted-space"> </span>48 8b 7d f8 <span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>-0x8(%rbp),%rdi</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1db:
<span class="Apple-converted-space"> </span>8b 07 <span
class="Apple-converted-space"> </span>mov<span
class="Apple-converted-space"> </span>(%rdi),%eax</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1dd:
<span class="Apple-converted-space"> </span>5d<span
class="Apple-converted-space"> </span>pop<span
class="Apple-converted-space"> </span>%rbp</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span>1de:
<span class="Apple-converted-space"> </span>c3<span
class="Apple-converted-space"> </span>retq<span
class="Apple-converted-space"> </span></span><br>
</p>
<div>With IR:<br>
</div>
<div><br>
</div>
<p class="p1"><span class="s1">; Function Attrs: nounwind uwtable</span><br>
</p>
<p class="p1"><span class="s1">define i32 @addr0(i32* %p) #0 {</span><br>
</p>
<p class="p1"><span class="s1">entry:</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>%p.addr = alloca i32*, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>store i32* %p, i32** %p.addr, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>%0 = load i32*, i32** %p.addr, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>%1 = load i32, i32* %0, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>ret i32 %1</span><br>
</p>
<p class="p1"><span class="s1">}</span><br>
</p>
<p class="p2"><br>
</p>
<p class="p1"><span class="s1">; Function Attrs: nounwind uwtable</span><br>
</p>
<p class="p1"><span class="s1">define i32 @addr1(i32 addrspace(1)*
%p) #0 {</span><br>
</p>
<p class="p1"><span class="s1">entry:</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>%p.addr = alloca i32 addrspace(1)*, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>store i32 addrspace(1)* %p, i32 addrspace(1)** %p.addr,
align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>%0 = load i32 addrspace(1)*, i32 addrspace(1)**
%p.addr, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>%1 = load i32, i32 addrspace(1)* %0, align 4</span><br>
</p>
<p class="p1"><span class="s1"><span class="Apple-converted-space">
</span>ret i32 %1</span><br>
</p>
<p class="p1"><span class="s1">}</span><br>
</p>
<div><br>
</div>
<div>However, sizeof() for such a pointer is still 4. <br>
</div>
<br>
</blockquote>
<br>
sizeof(), as in the C sizeof() operator? You probably need to
override TargetInfo::getPointerWidthV() and
TargetInfo::getPointerAlignV() for your target. See
clang/lib/Basic/Targets.cpp.<br>
<p>-Eli<br>
</p>
<pre class="moz-signature" cols="72">--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
</body>
</html>