For example:
int x=0;
int y=0;
where x and y are global variables, and in main() function we do the following:
x++;
y++;
How to get the newest value of global variables x and y in llvm.
when I try to do errs()<<g;
they give the initial value as @BB0 = global i32
but I need to get the actual value like x=1
, by using llvm.
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With