|
From: Rick M. <rm...@la...> - 2013-10-10 20:13:08
|
Just saw this on the LLVM list, is it in our AVR port as well?
Begin forwarded message:
> From: Behan Webster <be...@co...>
> Subject: [LLVMdev] A new builtin: __builtin_stack_pointer()
> Date: October 10, 2013 12:32:42 PDT
> To: "ll...@cs... List" <ll...@cs...>
> Cc: Mark Charlebois <cha...@gm...>
>
> One of the issues the LLVMLinux project is having is with the use of
> named registers in the Linux kernel code. The kernel uses something like
> this in order to assign a C variable name to a register (one for each
> kernel arch).
>
> register unsigned long current_stack_pointer asm("esp");
>
> clang doesn't allow this kind of thing which required a patch which less
> efficient:
>
> #define current_stack_pointer ({ \
> unsigned long esp; \
> asm("mov %%esp, %0" : "=r"(esp)); \
> esp; \
> })
>
> This works for both gcc and clang, but often adds in 3 extra
> instructions since you need to copy the stack pointer to another
> register, but first that register needs to be saved to the stack and
> then restored after the stackpointer has been used; inefficient.
>
> Another way would be to introduce a new builtin in parallel to others
> like __builtin_return_address(). Essentially adding a
> __builtin_stack_pointer() which does what the above code does. The idea
> would be to get something like this added to both clang and gcc in order
> to make this work across compilers, and across arches.
>
> It ends up being a trivial patch for clang (see below). We're still
> looking for someone to help us on the gcc side.
>
> The goal is to ideally make the kernel code work equally well with both
> compilers (clang and gcc).
>
> Thoughts?
>
> Thanks to Mark Charlebois for writing the patch below.
>
> Behan
>
> --
> Behan Webster
> be...@co...
>
>
> diff --git a/include/clang/Basic/Builtins.def b/include/clang/Basic/Builtins.def
> index 0a513ef..ca68f7e 100644
> --- a/include/clang/Basic/Builtins.def
> +++ b/include/clang/Basic/Builtins.def
> @@ -437,6 +437,7 @@ BUILTIN(__builtin_strstr, "c*cC*cC*", "nF")
> BUILTIN(__builtin_return_address, "v*IUi", "n")
> BUILTIN(__builtin_extract_return_addr, "v*v*", "n")
> BUILTIN(__builtin_frame_address, "v*IUi", "n")
> +BUILTIN(__builtin_stack_pointer, "v*", "n")
> BUILTIN(__builtin_flt_rounds, "i", "nc")
> BUILTIN(__builtin_setjmp, "iv**", "j")
> BUILTIN(__builtin_longjmp, "vv**i", "r")
> diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp
> index d187678..f66f506 100644
> --- a/lib/CodeGen/CGBuiltin.cpp
> +++ b/lib/CodeGen/CGBuiltin.cpp
> @@ -736,6 +736,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
> Value *F = CGM.getIntrinsic(Intrinsic::frameaddress);
> return RValue::get(Builder.CreateCall(F, Depth));
> }
> + case Builtin::BI__builtin_stack_pointer: {
> + Value *StackAddr =
> + Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
> + return RValue::get(StackAddr);
> + }
> case Builtin::BI__builtin_extract_return_addr: {
> Value *Address = EmitScalarExpr(E->getArg(0));
> Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
>
> _______________________________________________
> LLVM Developers mailing list
> LL...@cs... http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
--
Rick
|