llvm libLLVMCore源码分析 26 - Module Class

源码路径

llvm\include\llvm\IR\Module.h

llvm\include\llvm\IR\DataLayout.h

llvm Module Class

在llvm中,Module类代表的是llvm程序(既IR编写的程序)的顶层结构。1个Module本质上就是原始程序的1个翻译单元(TU),或者是原始程序的多个翻译单元通过链接器链接而成。

Module中包含的内容:

class Module {
  LLVMContext &Context;           ///< The LLVMContext from which types and
                                  ///< constants are allocated.
  GlobalListType GlobalList;      ///< The Global Variables in the module
  FunctionListType FunctionList;  ///< The Functions in the module
  AliasListType AliasList;        ///< The Aliases in the module
  IFuncListType IFuncList;        ///< The IFuncs in the module
  NamedMDListType NamedMDList;    ///< The named metadata in the module
  std::string GlobalScopeAsm;     ///< Inline Asm at global scope.
  ValueSymbolTable *ValSymTab;    ///< Symbol table for values
  ComdatSymTabType ComdatSymTab;  ///< Symbol table for COMDATs
  std::unique_ptr<MemoryBuffer>
  OwnedMemoryBuffer;              ///< Memory buffer directly owned by this
                                  ///< module, for legacy clients only.
  std::unique_ptr<GVMaterializer>
  Materializer;                   ///< Used to materialize GlobalValues
  std::string ModuleID;           ///< Human readable identifier for the module
  std::string SourceFileName;     ///< Original source file name for module,
                                  ///< recorded in bitcode.
  std::string TargetTriple;       ///< Platform target triple Module compiled on
                                  ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
  void *NamedMDSymTab;            ///< NamedMDNode names.
  DataLayout DL;                  ///< DataLayout associated with the module
};

LLVMContext 

LLVMContext用于保存全局的状态,在多线程执行的时候,可以每个线程一个LLVMContext,避免竞争。LLVMContext保存的内容:Type和Constant的Map,保证在一个LLVMContext的唯一性。

GlobalList

Module内GlobalVariable列表。

FunctionList

Module内Function列表。

AliasList

Module内GlobalAlias列表。

IFuncList

Module内GlobalIFunc列表。

NamedMDList

Module内NamedMDNode列表。

ValSymTab

Module内Value的符号表。

ComdatSymTab

Module内Comdat的符号表。

Materializer

Module加载器。

NamedMDSymTab

Module内NamedMDNode符号表。

Target Triple

描述目标机器信息,典型的结构:

ARCHITECTURE-VENDOR-OPERATING_SYSTEM

ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT

示例:

target triple = "x86_64-w64-windows-gnu"

DataLayout

DataLayout既数据布局,是1个字符串,描述了数据在内存中的布局。语法如下:

target datalayout = "layout specification"

DataLayout是一个采用符号'-'分隔的规格列表,含义如下:

E:大端

e:小端

S<size>:栈自然对齐的的bit数。

P<address space>:默认为0,表示冯诺依曼架构,数据和程序放置到同一个地址空间。

G<address space>:全局变量放置的地址空间,默认为0。

A<address space>:alloca分配的地址空间,默认为0。

p[n]:<size>:<abi>[:<pref>][:<idx>]:n为地址空间编号,size为指针大小,abi为ABI中的指针大小?,pref为地址空间n的对齐(默认等于abi),idx为地址计算的index的大小(默认等于size)。

i<size>:<abi>[:<pref>]:size为整型的对齐,abi为ABI中的对齐,pref默认等于abi。

v<size>:<abi>[:<pref>]:size为矢量的对齐,abi为ABI中的对齐,pref默认等于abi。

f<size>:<abi>[:<pref>]size为浮点的对齐,abi为ABI中的对齐,pref默认等于abi。

a:<abi>[:<pref>]size为聚合类型的对齐,abi为ABI中的对齐,pref默认等于abi。

m:<mangling>:名称粉碎的类型:

  • e: ELF mangling
  • l: GOFF mangling
  • m: Mips mangling
  • o: Mach-O mangling
  • x: Windows x86 COFF mangling
  • w: Windows COFF mangling
  •  a _ prefix.
  • a: XCOFF mangling

n<size1>:<size2>:<size3>...:自然整型的大小。

DataLayout示例如下:

target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

根据上述可知,该Module是小端,采用Windows COFF mangling进行名称粉碎,地址空间270/271的指针大小为32bit,地址空间272的指针大小为64bit,整型64bit对齐,浮点80bit对齐,自然整型为:8/16/32/64bit,栈自然对齐为128bit。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值