值
一个 模块 主要由值组成。
- llvmlite.ir.Undefined
一个未定义的值,对应于 LLVM 的
undef
。
- class llvmlite.ir.Value
所有 IR 值的基类。
- class llvmlite.ir._ConstOpMixin
这是
Constant
和GlobalValue
的基类;请勿直接实例化。整数算术运算
- add(other)
整数加法 self 和 other。
- sub(other)
整数减法 self 减去 other。
- mul(other)
整数乘法 self 乘以 other。
- udiv(other)
无符号整数除法 self 除以 other。
- sdiv(other)a
有符号整数除法 self 除以 other。
- urem(other)
无符号整数余数 self 除以 other。
- srem(other)
有符号整数余数 self 除以 other。
- neg()
对 self 取反。
整数逻辑运算
- shl(other)
将 self 左移 other 位。
- ashr(other)
算术右移(有符号)self other 位。
- lshr(other)
逻辑右移 self other 位。
- or_(other)
按位或 self 与 other。
- and_(other)
按位与 self 与 other。
- xor(other)
按位异或 self 与 other。
浮点算术
- fadd(other)
浮点加法 self 和 other。
- fsub(other)
浮点减法 self 减去 other。
- fmul(other)
浮点乘法 self 乘以 other。
- fdiv(other)
浮点除法 self 除以 other。
- frem(other)
浮点余数 self 除以 other。
比较
- icmp_signed(cmpop, other)
有符号整数比较 self 与 other。字符串 cmpop 可以是
<
,<=
,==
,!=
,>=
或>
之一。
- icmp_unsigned(cmpop, other)
无符号整数比较 self 与 other。字符串 cmpop 可以是
<
,<=
,==
,!=
,>=
或>
之一。
- fcmp_ordered(cmpop, other)
浮点有序比较 self 与 other。字符串 cmpop 可以是
<
,<=
,==
,!=
,>=
或>
之一。
- fcmp_unordered(cmpop, other)
浮点无序比较 self 与 other。字符串 cmpop 可以是
<
,<=
,==
,!=
,>=
或>
之一。
整数类型转换
- trunc(typ)
将 self 截断为整数类型 typ。
- zext(typ)
将 self 零扩展到整数类型 typ。
- sext(typ)
将 self 符号扩展到整数类型 typ。
- bitcast(typ)
将此指针常量转换为给定指针类型 typ 的常量。
浮点类型转换
整数/浮点转换
- fptoui(typ)
将浮点值 self 转换为无符号整数类型 typ。
- uitofp(typ)
将无符号整数值 self 转换为浮点类型 typ。
- fptosi(typ)
将浮点值 self 转换为有符号整数类型 typ。
- sitofp(typ)
将有符号整数值 self 转换为浮点类型 typ。
整数/指针转换
- class llvmlite.ir.Constant(typ, constant)
一个字面值。
typ 是所表示值的类型——一个
Type
实例。constant 是要表示的 Python 值。
constant 允许的 Python 类型取决于 typ
所有类型都接受
Undefined
,并将其转换为 LLVM 的undef
。所有类型都接受
None
,并将其转换为 LLVM 的zeroinitializer
。IntType
接受任何 Python 整数或布尔值。FloatType
和DoubleType
接受任何 Python 实数。聚合类型——数组和结构类型——接受与该类型元素类型对应的 Python 值序列。
另请参阅
_ConstOpMixin
。- classmethod literal_array(elements)
常量数组的替代构造函数。
elements 是一个值序列,可以是
Constant
或其他类型。所有 elements 必须具有相同的类型。
返回一个包含按序排列的 elements 的常量数组。
- classmethod literal_struct(elements, packed=False)
常量结构体的替代构造函数。
elements 是一个值序列,可以是
Constant
或其他类型。packed 控制是否使用紧凑布局。
返回一个包含按序排列的 elements 的常量结构体。
- gep(indices)
计算由 indices 序列给出的内部元素的地址。该常量必须具有指针类型。
注意:您不能定义常量函数。请改用函数声明。
- class llvmlite.ir.Argument
函数的一个参数。参数具有
add_attribute()
方法。- add_attribute(attr)
为此参数添加一个参数属性。attr 是一个 Python 字符串。
- class llvmlite.ir.BlockAddress
表示基本块地址的常量。
块地址常量具有以下属性
元数据
元数据值有多种类型。
- class llvmlite.ir.MetaDataString(module, value)
用于元数据的字符串字面量。
module 是元数据所属的模块。
value 是一个 Python 字符串。
- class llvmlite.ir.MDValue
一个元数据节点。要创建实例,请调用
Module.add_metadata()
。
- class llvmlite.ir.DIValue
一个调试信息描述符,包含键值对。要创建实例,请调用
Module.add_debug_info()
。
- class llvmlite.ir.DIToken(value)
一个调试信息“令牌”,表示一个众所周知的枚举值。value 是枚举名称。
示例:
'DW_LANG_Python'
- class llvmlite.ir.NamedMetaData
一个命名元数据节点。要创建实例,请调用
Module.add_named_metadata()
。NamedMetaData
具有add()
方法。- add(md)
将给定的元数据片段附加到由
NamedMetaData
引用的操作数集合中。md 可以是MetaDataString
或MDValue
。
全局值
全局值是使用模块范围名称可访问的值。
- class llvmlite.ir.GlobalValue
全局值的基类。全局值具有以下可写属性
- linkage
一个 Python 字符串,描述全局值的链接行为——例如,它是否对其他模块可见。默认值为空字符串,表示“外部(external)”。
- storage_class
一个 Python 字符串,描述全局值的存储类。
默认值为空字符串,表示“默认(default)”。
其他可能的值包括
dllimport
和dllexport
。
- section
一个 Python 字符串,描述全局值在转换后应位于哪个节。默认值为空字符串,表示没有特定节。
另请参阅
_ConstOpMixin
。
- class llvmlite.ir.GlobalVariable(module, typ, name, addrspace=0)
一个全局变量。
module 是定义变量的位置。
typ 是变量的类型。它不能是函数类型。要声明全局函数,请使用
Function
。返回的 Value 的类型是指向 typ 的指针。要读取变量内容,您需要从返回的 Value 中
加载(load)
。要写入变量,您需要向返回的 Value存储(store)
。name 是变量的名称——一个 Python 字符串。
addrspace 是一个可选的地址空间,用于存储变量。
全局变量具有以下可写属性
- class llvmlite.ir.Function(module, typ, name)
一个全局函数。
module 是定义函数的位置。
typ 是函数的类型——一个
FunctionType
实例。name 是函数的名称——一个 Python 字符串。
如果一个全局函数有任何基本块,它就是一个函数定义。否则,它是一个函数声明。
函数具有以下方法和属性
- insert_basic_block(before, name='')
类似于
append_basic_block()
,但将其插入到函数基本块列表中的基本块 before 之前。
- attributes
一组函数属性。每个可选属性都是一个 Python 字符串。默认情况下,这是空的。使用 .add() 方法添加属性。
fnty = ir.FunctionType(ir.DoubleType(), (ir.DoubleType(),)) fn = Function(module, fnty, "sqrt") fn.attributes.add("alwaysinline")
- calling_convention
函数的调用约定——一个 Python 字符串。默认值为空字符串。
- is_declaration
指示全局函数是声明还是定义。
如果为
True
,则为声明。如果为
False
,则为定义。
指令
每条指令也都是一个值。
它有一个名称——接收者的名称。
它有一个定义明确的类型。
它可以作为其他指令或字面量中的操作数使用。
通常,您不应直接实例化指令类型。请使用 IRBuilder
类中的辅助方法。
- class llvmlite.ir.Instruction
所有指令的基类。指令具有以下方法和属性
- class llvmlite.ir.PredictableInstr
可用于指定不同结果概率的指令类——例如,开关指令或条件分支指令。可预测指令具有
set_weights()
方法。- set_weights(weights)
设置指令可能结果的权重。weights 是一个正整数序列,每个整数对应一个不同的结果,并指定其相对于其他结果的相对概率。数值越大,结果越可能发生。
- class llvmlite.ir.SwitchInstr
一个开关指令。开关指令具有
add_case()
方法。
- class llvmlite.ir.IndirectBranch
一个间接分支指令。间接分支指令具有
add_destination()
方法。- add_destination(value, block)
添加一个传出边。间接分支指令必须引用其可以转移控制的所有基本块。
- class llvmlite.ir.PhiInstr
一个 phi 指令。phi 指令具有
add_incoming()
方法。
- class llvmlite.ir.LandingPad
一个着陆点。着陆点具有
add_clause()
方法。- add_clause(value, block)
添加一个捕获或过滤子句。使用
CatchClause
创建捕获子句,使用FilterClause
创建过滤子句。
着陆点子句
着陆点关联了以下类。
- class llvmlite.ir.CatchClause(value)
一个捕获子句。指示个性化函数将进行中的异常类型信息与 value 进行比较,value 的类型应为 IntType(8).as_pointer().as_pointer()。
- class llvmlite.ir.FilterClause(value)
一个过滤子句。指示个性化函数检查进行中的异常类型信息是否包含在 value 中,value 的类型应为 ArrayType(IntType(8).as_pointer().as_pointer(), …)。