Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating local variable in function LLVM

Tags:

c++

llvm

In llvm::Module there are 2 interesting fields:

typedef SymbolTableList<Function> FunctionListType;
typedef SymbolTableList<GlobalVariable> GlobalListType;

GlobalListType GlobalList;      ///< The Global Variables in the module
FunctionListType FunctionList;  ///< The Functions in the module

So, if we will define some functions or global variables, we will be able to use them from any other places of our program just asking our module for them. But what about function local variables? How to define them?

like image 952
koshachok Avatar asked Oct 16 '25 13:10

koshachok


1 Answers

Local variables are allocated via alloca at runtime.

To create AllocaInst you need to

llvm::BasicBlock::iterator I = ...
const llvm::Type *Ty = 
auto AI = new AllocaInst(Ty, 0, Name, I);

To find allocas in a function you need to iterate over instructions:

for (auto I = F->begin(), E = F->end(); I != E; ++I) {
  for (auto J = I->begin(), E = I->end(); J != E; ++J) {
    if (auto AI = dyn_cast<AllocaInst>(*J)) {
      ..
    }
  }
}
like image 90
yugr Avatar answered Oct 18 '25 02:10

yugr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!