源码路径
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 manglingl
: GOFF manglingm
: Mips manglingo
: Mach-O manglingx
: Windows x86 COFF manglingw
: 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。