<div dir="ltr"><div><div><div>I believe clang does support getting the stack-pointer via the "gcc register variable extension". The syntax and behaviour would follow this:<br><a href="https://gcc.gnu.org/onlinedocs/gcc/Global-Register-Variables.html#Global-Register-Variables">https://gcc.gnu.org/onlinedocs/gcc/Global-Register-Variables.html#Global-Register-Variables</a><br><br></div>however, it is restricted specifically to the stack-pointer.<br><br>#include <stdio.h><br><br>register size_t stack asm("rsp");<br><br>int main()<br>{<br>    printf("%zx\n", stack);<br>}<br><br></div>It compiles to this LLVM IR:<br>define i32 @main() #0 {<br>entry:<br>  %0 = call i64 @llvm.read_register.i64(metadata !0)<br>  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i64 %0)<br>  ret i32 0<br>}<br><br>--<br></div>Mats<br><div><br><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 11 December 2017 at 02:58, Seth Goldstein 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 dir="ltr"><div>I need to use the stack pointer, e.g., RSP on X86, to get some</div><div>information off the stack.  I am not sure what the best way to do this is.</div><div><br></div><div>My plan was to create an intrinsic, e.g., </div><div><br></div><div>  let TargetPrefix = "x86" in {</div><div>     def int_x86_read_sp : GCCBuiltin<"__builtin_read_sp"<wbr>>, </div><div>  <span style="white-space:pre-wrap"> </span>    Intrinsic<[llvm_i64_ty], [], [IntrNoMem]>;</div><div>  }</div><div>  </div><div>I thought I would define a PseudoI, e.g., </div><div><br></div><div>  let Uses = [RSP] in</div><div>      def READSP64 : PseudoI<(outs GR64:$dst), (ins),</div><div>            [(set GR64:$dst, RSP)]>,</div><div>                Requires<[In64BitMode]>;</div><div><span style="white-space:pre-wrap">                          </span></div><div>But, actually I am not sure how this would come into play.  (Do I need this?  If so, how do I use it?)</div><div><br></div><div>To test it out I added a Builtin to BuiltinsX86.def, e.g.,</div><div><br></div><div>  BUILTIN(__builtin_read_sp, "ULLi", "")</div><div>  </div><div>When I try and compile, I get a proper .ll file from clang with the instruction</div><div><br></div><div>    %0 = call i64 @llvm.x86.read.sp()</div><div><br></div><div>However, it fails, with </div><div><br></div><div>llc: ./llvm/lib/CodeGen/<wbr>SelectionDAG/InstrEmitter.cpp:<wbr>303: unsigned int llvm::InstrEmitter::getVR(<wbr>llvm::SDValue, llvm::DenseMap<llvm::SDValue, unsigned int>&): Assertion `I != VRBaseMap.end() && "Node emitted out of order - late"' failed.</div><div><br></div><div>I am not sure how to introduce the IR to read the RSP so I can create</div><div>code to do a calculation on the RSP to retrieve a value buried in the</div><div>stack.  I figure I need to lower the `call i64 @llvm.x86.read.sp` in</div><div>X86TargetLowering::<wbr>LowerOperation.  I am not sure if this would be</div><div>ISD::INTRINSIC_WO_CHAIN or ISD::INTRINSIC_W_CHAIN (and I don't</div><div>actually understand what would go in the intrinsic definition to make</div><div>it one or the other.  Any pointers would be appreciated.  Thanks,</div><div><br></div><div>seth</div><div><br></div><div><div class="m_-3311095469257713726gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><br></div><div>--------------</div><div>Seth Copen Goldstein</div><div>Carnegie Mellon University</div><div>Computer Science Dept</div><div>7111 GHC</div><div><a href="tel:(412)%20268-3828" value="+14122683828" target="_blank">412-268-3828</a></div></div></div></div></div></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>