LLVM的类型系统(转

LLVM中的类型系统很重要!有类型,可以使得很多优化成为可能,而且不需要额外的分析。一个强类型系统可使生成的代码更容易理解,并且更容易优化和转换。
1.类型的类别
llvm有以下几种类型:

integer 
i1, i2, i3, ... i8, ... i16, ... i32, ... i64, ...
floating point 
float, double, x86_fp80, fp128, ppc_fp128
(第一类)first class是最重要的。指令只能产生这些类型的值。
2.原始类型(primitive types)
原始类型是llvm系统中的基础。
integer类型
整数类型是一个位宽度从1到2^23-1的整数。
语法:
iN
其中N为位宽。
例子:
i1
i32
i1942652
Floating Point类型
float 
32-bit floating point value
double 
64-bit floating point value
fp128 
128-bit floating point value (112-bit mantissa)
x86_fp80 
80-bit floating point value (X87)
ppc_fp128 
128-bit floating point value (two 64-bits)
X86mmx类型
x86mmx类型标识一个在x86机器上的MMX寄存器。能够对这个类型进行操作的种类十分有限,有:参数,返回值,load,store,bitcast。这个指令没有数组,向量或常量类型。
void类型
没有大小。
Label类型
代表代码中的标签
metadata类型:
表示嵌入的元数据类型。
继承类型
llvm的真正强大之处在于其继承类型。这允许程序员表示arrays,functions, pointers和其他有用的类型。这些类型总是由其他基本类型组成,或者其他继承类型组成。
Aggregate类型
Aggregate类型是继承类型的子集,包括以下几种类型:Arrays,structs,vectors。
Array类型
array类型需要一个大小(元素个数)和其中的数据类型。
[<# elements> x <elementtype>]
例子:
[40 x i32] 
Array of 40 32-bit integer values.
[41 x i32] 
Array of 41 32-bit integer values.
[4 x i8] 
Array of 4 8-bit integer values
多维数组的例子如小:
[3 x [4 x i32]] 
3x4 array of 32-bit integer values.
[12 x [10 x float]] 
12x10 array of single precision floating point values.
[2 x [3 x [4 x i16]]] 
2x3x4 array of 16-bit integer values.
Function类型:
函数类型可以认为是函数的签名,包含一个返回类型和一系列参数类型,返回类型必须是第一类型。
语法:
<returntype> (<parameter list>)
例子:
i32 (i32) 
function taking an i32, returning an i32
float (i16, i32 *) * 
Pointer to a function that takes an i16 and a pointer to i32, returning float.
i32 (i8*, ...) 
A vararg function that takes at least one pointer to i8 (char in C), which returns an integer. This is the signature for printf in LLVM.
{i32, i32} (i32) 
A function taking an i32, returning a structure containing two i32values
Structure类型
通过得到域的指针(getelementptr指令)。structure可以使用load和store操作。寄存器中的structure可以使用extractvalue和insertvalue指令操作。
语法:
{ <type list> }
例子:
{ i32, i32, i32 } 
A triple of three i32 values
{ float, i32 (i32) * } 
A pair, where the first element is a float and the second element is a pointer to a functionthat takes an i32, returning an i32.
Packed structure类型
包结构类型用于表示内存中数据的集合,域之间没有填充。一个packed structure类型是1字节对齐的。可以使用load和store操作,前提是使用getelementptr指令得到指针。
语法:
< { <type list> } >
例子:
< { i32, i32, i32 } > 
A triple of three i32 values
< { float, i32 (i32)* } > 
A pair, where the first element is a float and the second element is a pointer to a functionthat takes an i32, returning an i32.
指针类型:
<type> *
例子:
[4 x i32]* 
pointer to array of four i32 values.
i32 (i32*) * 
pointer to a function that takes an i32*, returning an i32.
i32 addrspace(5)* 
pointer to an i32 value that resides in address space #5.
向量类型:
vector类型表示元素的向量。向量类型用于多个基本类型的SIMD并行操作。
< <* elements> x <elementtype> >
例子:
<4 x i32> 
Vector of 4 32-bit integer values.
<8 x float> 
Vector of 8 32-bit floating-point values.
<2 x i64> 
Vector of 2 64-bit integer values.
opaque类型:
opaque类型标识未知的类型,通常用于前向声明。

评论

此博客中的热门博文

使__attribute__((annotate("xxx")))能作用于OC函数

viewWillAppear不执行的解决办法

iOS中使用xpc/xpc.h