在函数LLVM中创建局部变量 [英] Creating local variable in function LLVM

查看:115
本文介绍了在函数LLVM中创建局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

llvm::Module中有2个有趣的字段:

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?

推荐答案

在运行时通过alloca分配局部变量.

Local variables are allocated via alloca at runtime.

要创建AllocaInst,您需要

To create AllocaInst you need to

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

要在函数中查找alloca,您需要遍历说明:

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

这篇关于在函数LLVM中创建局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆