Creating local variable in function LLVM

此生再无相见时 提交于 2019-12-02 10:48:20

问题


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?


回答1:


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)) {
      ..
    }
  }
}


来源:https://stackoverflow.com/questions/50308253/creating-local-variable-in-function-llvm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!