<div dir="ltr"><div class="gmail_extra">On Wed, Nov 6, 2013 at 5:46 PM, Behan Webster <span dir="ltr"><<a href="mailto:behanw@converseincode.com" target="_blank">behanw@converseincode.com</a>></span> wrote:<br><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">I originally posted this to the llvm-dev mailing list, when I should<br>
have posted it here. So here it is reposted, and updated a bit.<br>
<br>
One of the issues the LLVMLinux project is having is with the use of<br>
named registers in the Linux kernel code. The kernel uses something like<br>
this in order to assign a C variable name to a register (one for each<br>
kernel arch).<br>
<br>
register unsigned long current_stack_pointer asm("esp");<br>
<br>
clang doesn't allow this kind of thing which required a patch which less<br>
efficient:<br>
<br>
#define current_stack_pointer ({ \<br>
unsigned long esp; \<br>
asm("mov %%esp, %0" : "=r"(esp)); \<br>
esp; \<br>
})<br>
<br>
This works for both gcc and clang, but often adds in 3 extra<br>
instructions since you need to copy the stack pointer to another<br>
register, but first that register needs to be saved to the stack and<br>
then restored after the stackpointer has been used; inefficient.<br>
<br>
Jakob Stoklund Olesen <<a href="mailto:stoklund@2pi.dk">stoklund@2pi.dk</a>> suggested the following would be<br>
better, and indeed it is.<br>
<br>
> #define current_stack_pointer ({ \<br>
> register unsigned long esp asm("esp"); \<br>
> asm("" : "=r"(esp)); \<br>
> esp; \<br>
> })<br>
<br>
Another way would be to introduce a new builtin in parallel to others<br>
like __builtin_return_address(). Essentially adding a<br>
__builtin_stack_pointer() which does what the above code does. The idea<br>
would be to get something like this added to both clang and gcc in order<br>
to make this work across compilers, and across arches.<br>
<br>
It ends up being a trivial patch for clang (see below). We're still<br>
looking for someone to help us on the gcc side.<br>
<br>
The goal is to ideally make the kernel code work equally well with both<br>
compilers (clang and gcc).<br>
<br>
Thoughts?<br></blockquote><div><br></div><div>The LLVM LangRef is pretty clear:</div><div><br></div><div>llvm.stacksave:<br><br>"This intrinsic returns a <b>opaque pointer value</b> that can be passed to llvm.stackrestore. When an llvm.stackrestore intrinsic is executed with a value saved from llvm.stacksave, it effectively restores the state of the stack to the state it was in when the llvm.stacksave intrinsic executed. In practice, this pops any alloca blocks from the stack that were allocated after the llvm.stacksave was executed."<br>
<br>An opaque pointer value doesn't sound like it's guaranteed to be a stack pointer to me, so this patch isn't correct. __builtin_frame_address(0) looks like it would give approximately what you want (though it's hard to see how you could use this in correct code...).</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Thanks to Mark Charlebois for writing the patch below.<br>
<br>
Behan<br>
<br>
--<br>
Behan Webster<br>
<a href="mailto:behanw@converseincode.com">behanw@converseincode.com</a><br>
<br>
<br>
diff --git a/include/clang/Basic/Builtins.def<br>
b/include/clang/Basic/Builtins.def<br>
index 0a513ef..ca68f7e 100644<br>
--- a/include/clang/Basic/Builtins.def<br>
+++ b/include/clang/Basic/Builtins.def<br>
@@ -437,6 +437,7 @@ BUILTIN(__builtin_strstr, "c*cC*cC*", "nF")<br>
BUILTIN(__builtin_return_address, "v*IUi", "n")<br>
BUILTIN(__builtin_extract_return_addr, "v*v*", "n")<br>
BUILTIN(__builtin_frame_address, "v*IUi", "n")<br>
+BUILTIN(__builtin_stack_pointer, "v*", "n")<br>
BUILTIN(__builtin_flt_rounds, "i", "nc")<br>
BUILTIN(__builtin_setjmp, "iv**", "j")<br>
BUILTIN(__builtin_longjmp, "vv**i", "r")<br>
diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp<br>
index d187678..f66f506 100644<br>
--- a/lib/CodeGen/CGBuiltin.cpp<br>
+++ b/lib/CodeGen/CGBuiltin.cpp<br>
@@ -736,6 +736,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const<br>
FunctionDecl *FD,<br>
Value *F = CGM.getIntrinsic(Intrinsic::frameaddress);<br>
return RValue::get(Builder.CreateCall(F, Depth));<br>
}<br>
+ case Builtin::BI__builtin_stack_pointer: {<br>
+ Value *StackAddr =<br>
+ Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));<br>
+ return RValue::get(StackAddr);<br>
+ }<br>
case Builtin::BI__builtin_extract_return_addr: {<br>
Value *Address = EmitScalarExpr(E->getArg(0));<br>
Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</blockquote></div><br></div></div>