[cfe-dev] Gobal register variables marked local

Renato Golin renato.golin at linaro.org
Wed May 14 04:27:32 PDT 2014


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



More information about the cfe-dev mailing list