[cfe-dev] Gobal register variables marked local

Reid Kleckner rnk at google.com
Wed May 14 09:40:14 PDT 2014


What about making a new LValue type that doesn't have an address?  At the
source level, current_stack_pointer is an lvalue that can be stored to.  If
we ever want to add support for the 'register' class specifier as it was
originally designed, we will need a new 'Register' LValue type that can be
read from and stored to.  We probably don't want to do that, but it feels
like the right model to me.

On Wed, May 14, 2014 at 4:27 AM, Renato Golin <renato.golin at linaro.org>wrote:

> Hi Reid,
>
> So, following up my example...
>
>   register unsigned long current_stack_pointer asm("sp");
>   unsigned long get_stack_pointer_addr() {
>     return current_stack_pointer;
>   }
>
> In this case, EmitReturnStmt() will call CreateStore(EmitScalarExpr())
> which we don't need at this stage, but it doesn't matter because this
> will be removed later by the backend.
>
> The IR I want to generate is:
>
>   %sp = call i32 @llvm.read_register.i32(metadata !0)
>   ret i32 %sp
>
> Though, this would also work:
>
>   %ret = alloca i32*
>   %0 = call i32 @llvm.read_register.i32(metadata !0)
>   store i32 %0, i32* %ret
>   ret i32 %ret
>
> which is what I believe Clang is trying to do.
>
> What I did was to go all the way down to:
>
>   LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E);
>
> adding an "if named register, emit a call to the intrinsic". However,
> the result of the call is an RValue, not an LValue. But the return
> statement is asking for the RValue, so it passes through a cast
> (CK_LValueToRValue) because it's expecting the variable to have an
> address, but ours doesn't.
>
> This is the ast:
>
> `-FunctionDecl 0x7e9f50 <line:3:1, line:5:1> line:3:15
> get_stack_pointer_addr 'unsigned long ()'
>   `-CompoundStmt 0x7ea050 <col:40, line:5:1>
>     `-ReturnStmt 0x7ea030 <line:4:3, col:10>
>       `-ImplicitCastExpr 0x7ea018 <col:10> 'unsigned long' <LValueToRValue>
>         `-DeclRefExpr 0x7e9ff0 <col:10> 'unsigned long' lvalue Var
> 0x7e9e50 'current_stack_pointer' 'unsigned long'
>
> I want the Var current_stack_pointer NOT to be an lvalue from the
> beginning, so that the return gets directly the value itself, not a
> cast to RValue.
>
> Also, would be good to mark it volatile, but
> NewVD->getType().addVolatile() doesn't seem to have effect.
>
> cheers,
> --renato
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140514/64fe47b2/attachment.html>


More information about the cfe-dev mailing list