Question

I have written a function pass that initializes a global variable and then inserts a Function into the IR. I would like to set the result of the function call to the global variable, but am unsure of how to convert the CallInst type to GlobalVariable type. Here is the code I have written for the global variable declaration:

GlobalVariable *virtAddr = new GlobalVariable(*F.getParent(), 
    Type::getInt8PtrTy(F.getContext(),8),
    false,
    GlobalValue::ExternalLinkage,
    0,
    "virt_addr");
virtAddr->setAlignment(4);

Then I try to set the global variable in this line:

virtAddr = builder.CreateCall(mmap,putsArgs,"mmap");

When I try to compile, I get this error:

error: assigning to 'llvm::GlobalVariable *' from incompatible type
'llvm::CallInst *'

Thanks for any help!

Was it helpful?

Solution

This code:

GlobalVariable *virtAddr = new GlobalVariable(...);
virtAddr = builder.CreateCall(mmap,putsArgs,"mmap");

does not "convert a CallInst to a GlobalVariable" any more than this code will "convert 3 to 4":

int x = 3;
x = 4;

In other words, it's a regular C++ assignment into a variable. In your case the value and the variable do not even have compatible types so you're getting a standard C++ type error.

So how do you assign the results of a function call (or any other value) into a global variable? Well, global variables always represent a pointer to some memory location; so if you want to store anything there, you need to use a StoreInst, giving it the global variable as the address and the call instruction as the value to store.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top