例如:
int x=0;
int y=0;
其中x和y是全局变量,在main()函数中我们执行以下操作:
x++;
y++;
如何在llvm中获取全局变量x和y的最新值。
当我尝试errs()<<g;
时,他们将初始值设为@BB0 = global i32
但是我需要通过使用llvm来获得像x=1
这样的实际值。
答案 0 :(得分:2)
全局基本上是一个指针。您可以通过ExecutionEngine::getGlobalValueAddress
获取主机程序中的地址,然后您可以取消引用该地址以获取存储的值。
答案 1 :(得分:1)
Assuming you're using LLVM's API:
If the global is constant you can access its initialization value directly, for example:
Constant* myGlobal = new GlobalVariable( myLlvmModule, myLlvmType, true, GlobalValue::InternalLinkage, initializationValue );
...
Constant* constValue = myGlobal->getInitializer();
And if that value is of e.g. integer type, you can retrieve it like so:
ConstantInt* constInt = cast<ConstantInt>( constValue );
int64_t constIntValue = constInt->getSExtValue();
If the global isn't constant, you must load the data it points to (all globals are actually pointers):
Value* loadedValue = new LoadInst( myGlobal );