LLVM学习笔记-代码实例HowToUseJIT 分析(一)

 

 

在HowToUseJIT这个例子是演示了如何用LLVM创建两个函数并执行的过程,其函数的c语言原型如下:

 int add1(int x) {
   return x+1;
 }

 int foo() {
   return add1(10);
 }

一、InitializeNativeTarget

在 上面的示例函数52行调用了InitializeNativeTarget函数,这个函数主要是用来初始化相应于主机的目标机,JIT应用程序通过这个

函数调用来确保目标机的相应库已经链接到程序中。其函数实现如下:

 

二、LLVMContext

HowToUseJIT代码的54行声明了一个类型为LLVMContext的实例Context,这个类在文件include/llvm/LLVMContext.h定义。在代码中对这个类注释如下:

 

/// This is an important class for using LLVM in a threaded context.  It
/// (opaquely) owns and manages the core "global" data of LLVM's core
/// infrastructure, including the type and constant uniquing tables.
/// LLVMContext itself provides no locking guarantees, so you should be careful
/// to have one context per thread.

我理解这个LLVMContext针对每一个线程记录了线程本地的变量,即对于每一个LLVM的线程,都对应了这样一个context的实例。

 

三、Module

然后在代码的57行初始化了一个module变量。在代码注释中对Module的描述如下:

/// A Module instance is used to store all the information related to an
/// LLVM module. Modules are the top level container of all other LLVM
/// Intermediate Representation (IR) objects. Each module directly contains a
/// list of globals variables, a list of functions, a list of libraries (or
/// other modules) this module depends on, a symbol table, and various data
/// about the target's characteristics.

一个Module的实例用于存储一个模块中的所有信息,是其他所有LLVM中间表示对象的容器。每个目标会直接包含一个全局变量的列表,

一个函数的列表,和这个模块依赖的函数库的列表,一个符号表,和这个目标特性的一些数据。

其中Module的类声明中有如下成员变量:

public:

  typedef iplist<GlobalVariable> GlobalListType;
  /// The type for the list of functions.
  typedef iplist<Function> FunctionListType;
  /// The type for the list of aliases.
  typedef iplist<GlobalAlias> AliasListType;
  /// The type for the list of named metadata.
  typedef ilist<NamedMDNode> NamedMDListType;

  /// The type for the list of dependent libraries.
  typedef std::vector<std::string> LibraryListType;

private:
  LLVMContext &Context;           ///< The LLVMContext from which types and
                                  ///< constants are allocated.
public:

  /// Get the target endian information.
  /// @returns Endianess - an enumeration for the endianess of the target
  Endianness getEndianness() const;

  /// Get the target pointer size.
  /// @returns PointerSize - an enumeration for the size of the target's pointer
  PointerSize getPointerSize() const;

  /// Get the global data context.
  /// @returns LLVMContext - a container for LLVM's global information
  LLVMContext &getContext() const { return Context; }

 

四、

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值