问题
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