Zig 中间表示。
Astgen.zig 将 AST 节点转换成这些无类型的中间表示指令,随后 Sema.zig 将它们处理为 AIR。 包含表示 ZIR 指令列表所需的最少信息。此结构完成后,无需再访问 AST 树的词法单元列表、节点列表或源码字节,就能生成 AIR,进而生成机器码。例外包括:
- 编译错误:可能需要访问上述数据结构,以生成有用的诊断报告。
- 将来可能包括内联汇编:它需要由代码生成后端解析和处理,并在那里报告错误。不过,目前内联汇编并非例外。
字段
instructions: std.MultiArrayList(Inst).Slice
string_bytes: []u8为用更少字节存储字符串引用,将所有字符串字节复制到此处。字符串字节可以为零。引用此处数据的使用者可自行决定:同时存储索引与长度以支持空字节,或者仅存储索引并使用零终止形式。
string_bytes 数组不限定其中任何一种用法。
索引 0 为特殊情况保留。
extra: []u32此数据的含义由 Inst.Tag 值决定。
最前面的几个索引保留。具体值参见 ExtraIndex。
ZirbuiltinstdIomemAllocatorassertBigIntConstBigIntMutableAstHeaderExtraIndexExtraDataextraDataNullTerminatedStringnullTerminatedStringrefSlicebodySlicehasCompileErrorsloweringFaileddeinitInstDeclContentsfindTrackablefindTrackableFieldsfindTrackableInnerfindTrackableBodyFnInfogetParamBodygetParamNamegetFnInfogetDeclarationgetAssociatedSrcHashgetSwitchBlockUnwrappedSwitchBlockinst_tracking_versionassertTrackabletypeDeclsgetStructDeclUnwrappedStructDeclgetUnionDeclUnwrappedUnionDeclgetEnumDeclUnwrappedEnumDeclgetOpaqueDeclUnwrappedOpaqueDecl
std.zig.Zir.Allocator
别名
别名目标 std.mem.Allocator
std.zig.Zir.assert
别名
别名目标 std.debug.assert
std.zig.Zir.assertTrackable
函数
断言 ZIR 指令会跨增量更新跟踪,
因此可以分配 InternPool.TrackedInst。
pub fn assertTrackable(zir: Zir, inst_idx: Zir.Inst.Index) void
参数
std.zig.Zir.Ast
别名
别名目标 std.zig.Ast
std.zig.Zir.BigIntConst
别名
std.zig.Zir.BigIntMutable
别名
std.zig.Zir.bodySlice
函数
pub fn bodySlice(zir: Zir, start: usize, len: usize) []Inst.Index
参数
std.zig.Zir.builtin
常量
std.zig.Zir.DeclContents
容器
DeclContents 包含 findTrackable 在声明内找到的全部“值得关注”的指令。
这些指令划分为若干不同集合,因为这能使 ZIR 指令映射
更加有效。
字段
std.zig.Zir.DeclContents.clear
函数
pub fn clear(contents: *DeclContents) void
参数
contents: *DeclContents
std.zig.Zir.DeclContents.deinit
函数
pub fn deinit(contents: *DeclContents, gpa: Allocator) void
参数
contents: *DeclContents
gpa: Allocator
std.zig.Zir.DeclContents.init
常量
DeclContents
std.zig.Zir.deinit
函数
pub fn deinit(code: *Zir, gpa: Allocator) void
参数
std.zig.Zir.extraData
函数
返回请求的数据,以及位于该对象尾部数据起点的 新索引。
pub fn extraData(code: Zir, comptime T: type, index: usize) ExtraData(T)
参数
std.zig.Zir.ExtraData
类型函数
fn ExtraData(comptime T: type) type
参数
T: type
字段
data: T
end: usize
std.zig.Zir.ExtraIndex
容器
字段
compile_errors若为 0,则没有编译错误。
否则,此索引处存在 CompileErrors 载荷。
imports若为 0,此文件没有导入。否则,此索引处有一个 Imports
载荷。
_
std.zig.Zir.findTrackable
函数
递归查找一条 declaration 指令内所有受跟踪的 ZIR 指令。不递归进入
嵌套声明;要查找所有声明,请对 contents.type_decls 中发现的
类型声明递归调用此函数。
结果填入 ArrayList,因为即使使用迭代器也仍需分配内存。
pub fn findTrackable(zir: Zir, gpa: Allocator, contents: *DeclContents, decl_inst: Zir.Inst.Index) !void
参数
std.zig.Zir.findTrackableBody
函数
fn findTrackableBody( zir: Zir, gpa: Allocator, contents: *DeclContents, defers: *std.AutoHashMapUnmanaged(u32, void), body: []const Inst.Index, ) Allocator.Error!void
参数
zir: Zir
gpa: Allocator
contents: *DeclContents
defers: *std.AutoHashMapUnmanaged(u32, void)
body: []const Inst.Index
错误
- OutOfMemory
std.zig.Zir.findTrackableFields
函数
findTrackable 不会递归进入类型内的字段表达式。此函数则专门
扫描给定类型声明中的字段表达式,查找可跟踪的 ZIR 指令。
pub fn findTrackableFields( zir: *const Zir, gpa: Allocator, contents: *DeclContents, type_decl_inst: Zir.Inst.Index, ) Allocator.Error!void
参数
错误
- OutOfMemory
std.zig.Zir.findTrackableInner
函数
fn findTrackableInner( zir: Zir, gpa: Allocator, contents: *DeclContents, defers: *std.AutoHashMapUnmanaged(u32, void), inst: Inst.Index, ) Allocator.Error!void
参数
zir: Zir
gpa: Allocator
contents: *DeclContents
defers: *std.AutoHashMapUnmanaged(u32, void)
inst: Inst.Index
错误
- OutOfMemory
std.zig.Zir.FnInfo
容器
字段
std.zig.Zir.getAssociatedSrcHash
函数
pub fn getAssociatedSrcHash(zir: Zir, inst: Zir.Inst.Index) ?std.zig.SrcHash
参数
std.zig.Zir.getDeclaration
函数
pub fn getDeclaration(zir: Zir, inst: Zir.Inst.Index) Inst.Declaration.Unwrapped
参数
std.zig.Zir.getEnumDecl
函数
pub fn getEnumDecl(zir: *const Zir, enum_decl: Inst.Index) UnwrappedEnumDecl
参数
std.zig.Zir.getFnInfo
函数
pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo
参数
std.zig.Zir.getOpaqueDecl
函数
pub fn getOpaqueDecl(zir: *const Zir, opaque_decl: Inst.Index) UnwrappedOpaqueDecl
参数
std.zig.Zir.getParamBody
函数
pub fn getParamBody(zir: Zir, fn_inst: Inst.Index) []const Zir.Inst.Index
参数
std.zig.Zir.getParamName
函数
pub fn getParamName(zir: Zir, param_inst: Inst.Index) ?NullTerminatedString
参数
std.zig.Zir.getStructDecl
函数
pub fn getStructDecl(zir: *const Zir, struct_decl: Inst.Index) UnwrappedStructDecl
参数
std.zig.Zir.getSwitchBlock
函数
pub fn getSwitchBlock(zir: *const Zir, switch_inst: Inst.Index) UnwrappedSwitchBlock
参数
std.zig.Zir.getUnionDecl
函数
pub fn getUnionDecl(zir: *const Zir, union_decl: Inst.Index) UnwrappedUnionDecl
参数
std.zig.Zir.hasCompileErrors
函数
pub fn hasCompileErrors(code: Zir) bool
参数
code: Zir
std.zig.Zir.Header
容器
将 ZIR 存入文件时,位于字节偏移 0 的数据。
字段
std.zig.Zir.Inst
容器
这些是由抽象语法树生成的无类型指令。 此处的数据不可变,因为同一份 ZIR 可能同时被多个分析过程使用。
字段
TagExtendedIndexOptionalIndexRefDataBreakAsmFuncFuncFancyMultiOpNodeMultiOpBlockBlockComptimeBoolBrDeclarationCallFieldCallTypeOfPeerBuiltinCallCondBrTryPtrTypeArrayTypeSentinelSliceStartSliceEndSliceSentinelSliceLengthBinBinNodeReifySliceArgInfoRoundOpUnNodeElemPtrImmReifyPointerReifyFnReifyStructReifyUnionReifyEnumReifySpirvTypeSwitchBlockArrayInitRefTyFieldFieldNamedAsStructDeclCaptureNameStrategyFullPtrCastFlagsStdLangValueInplaceOpEnumDeclUnionDeclOpaqueDeclTupleDeclErrorSetDeclFloat128StructInitStructInitAnonFieldTypeFieldTypeRefCmpxchgAtomicRmwUnionInitAtomicStoreAtomicLoadMulAddFieldParentPtrShuffleSelectParamAllocExtendedExportCompileErrorsImportsLineColumnArrayInitSrcValidateDestructureRestoreErrRetIndexImport
std.zig.Zir.inst_tracking_version
常量
需要修改 ZIR 更新跟踪逻辑以考虑新指令时, 更改此常量可在所有相关位置触发编译错误。
std.zig.Zir.Inst.AllocExtended
容器
尾随内容:
- type_inst: Ref, // 如果设置了 small 0b000X
- align_inst: Ref, // 如果设置了 small 0b00X0
字段
std.zig.Zir.Inst.AllocExtended.Small
容器
字段
has_type: bool
has_align: bool
is_const: bool
is_comptime: bool
_: u12 = 0
std.zig.Zir.Inst.ArrayInit
容器
字段
ty: Ref
init_count: u32
std.zig.Zir.Inst.ArrayInitRefTy
容器
字段
ptr_ty: Ref
elem_count: u32
std.zig.Zir.Inst.ArrayTypeSentinel
容器
字段
std.zig.Zir.Inst.As
容器
字段
std.zig.Zir.Inst.Asm
容器
尾随数据:
- 对每个 outputs_len 的 Output
- 对每个 inputs_len 的 Input
字段
src_node: Ast.Node.Offset
asm_source: NullTerminatedString
output_type_bits: u32每个 outputs_len 对应 1 位,表示是否使用 -> T。
0b0 - 操作数是指向输出存储位置的指针。
0b1 - 操作数是类型;asm 表达式以输出作为结果。
0b0X 对应第一个输出,0bX0 对应第二个输出,依此类推。
clobbers: Ref
std.zig.Zir.Inst.Asm.Input
容器
字段
name: NullTerminatedStringstring_bytes 中的索引(以空字符终止)
constraint: NullTerminatedStringstring_bytes 中的索引(以空字符终止)
operand: Ref
std.zig.Zir.Inst.Asm.Output
容器
字段
name: NullTerminatedStringstring_bytes 中的索引(以空字符终止)
constraint: NullTerminatedStringstring_bytes 中的索引(以空字符终止)
operand: Ref如何解释此内容由 output_type_bits 决定。
std.zig.Zir.Inst.Asm.Small
容器
字段
is_volatile: bool
outputs_len: u7
inputs_len: u8
std.zig.Zir.Inst.AtomicLoad
容器
字段
std.zig.Zir.Inst.AtomicRmw
容器
字段
std.zig.Zir.Inst.AtomicStore
容器
字段
std.zig.Zir.Inst.Bin
容器
这些操作数的含义取决于对应的 Tag。
字段
std.zig.Zir.Inst.BinNode
容器
字段
std.zig.Zir.Inst.Block
容器
此数据存储在 extra 中,根据 body_len 尾随相应数量的操作数。
每个操作数均为 Index。
字段
body_len: u32
std.zig.Zir.Inst.BlockComptime
容器
尾随数据:
- 每个
body_len对应一个 inst(索引)
字段
reason: std.zig.SimpleComptimeReason
body_len: u32
std.zig.Zir.Inst.BoolBr
容器
尾随数据:
- 每个
body_len对应一个 inst(索引)
字段
lhs: Ref
body_len: u32
std.zig.Zir.Inst.Break
容器
字段
operand_src_node: Ast.Node.OptionalOffset
block_inst: Index
std.zig.Zir.Inst.BuiltinCall
容器
字段
std.zig.Zir.Inst.BuiltinCall.Flags
容器
字段
is_nosuspend: bool
ensure_result_used: bool
_: u30 = 0
std.zig.Zir.Inst.Call
容器
存储在 extra 中,尾随参数数量由 args_len 指定。
隐含的第 0 项:arg_0_start: u32,// 始终与 args_len 相同
- arg_end: u32,// 每个
args_len对应一项 arg_N_start 与 arg_N-1_end 相同。
字段
std.zig.Zir.Inst.Call.Flags
容器
字段
packed_modifier: PackedModifier
ensure_result_used: bool = false
pop_error_return_trace: bool
args_len: PackedArgsLen
std.zig.Zir.Inst.Call.Flags.PackedArgsLen
类型
std.zig.Zir.Inst.Call.Flags.PackedModifier
类型
打包形式的 std.lang.CallModifier
std.zig.Zir.Inst.Capture
容器
表示类型声明的闭包所捕获的单个值。
字段
tag: enum(u3) {
/// `data` 是父闭包中的 `u16` 索引。
nested,
/// `data` 是 `Zir.Inst.Index`,指向其值正在被捕获的指令。
instruction,
/// `data` 是一个 `Zir.Inst.Index`,指向表示分配操作的指令,其分配内容正在被捕获。
instruction_load,
/// `data` 是引用声明名称的 `NullTerminatedString`。
decl_val,
/// `data` 是引用声明名称的 `NullTerminatedString`。
decl_ref,
}
data: u29
std.zig.Zir.Inst.Capture.unwrap
函数
pub fn unwrap(cap: Capture) Unwrapped
参数
cap: Capture
std.zig.Zir.Inst.Capture.Unwrapped
容器
字段
nested: u16
instruction: Zir.Inst.Index
instruction_load: Zir.Inst.Index
decl_val: NullTerminatedString
decl_ref: NullTerminatedString
std.zig.Zir.Inst.Capture.wrap
函数
pub fn wrap(cap: Unwrapped) Capture
参数
cap: Unwrapped
std.zig.Zir.Inst.Cmpxchg
容器
字段
std.zig.Zir.Inst.CompileErrors
容器
尾随:每个 items_len 对应一个 CompileErrors.Item。
字段
items_len: u32
std.zig.Zir.Inst.CompileErrors.Item
容器
尾随内容:每个 notes_len 对应一个 note_payload_index: u32。
它是另一个 Item 的载荷索引。
字段
msg: NullTerminatedString以空字符结尾的字符串索引
node: Ast.Node.OptionalIndex
token: Ast.OptionalTokenIndex若 node 为 .none,则填充此字段。
byte_offset: u32可与 token 组合使用。
notes: u32
std.zig.Zir.Inst.CompileErrors.Item.notesLen
函数
pub fn notesLen(item: Item, zir: Zir) u32
参数
std.zig.Zir.Inst.CondBr
容器
此数据存储在 extra 中,后接两组 Ref:
- then 分支体,长度由
then_body_len指定。
- then 分支体,长度由
- else 分支体,长度由
else_body_len指定。
- else 分支体,长度由
字段
std.zig.Zir.Inst.Data
容器
所有指令都具有 8 字节载荷,存储在
此联合体中。Tag 决定哪个联合体字段处于活动状态,
以及如何解释其中的数据。
字段
extended: Extended.InstData用于 Tag.extended。扩展操作码决定
small 和 operand 字段的含义。
un_node: struct {
/// 相对于 Decl AST 节点索引的偏移。
src_node: Ast.Node.Offset,
/// 此操作数的含义取决于对应的 `Tag`。
operand: Ref,
}用于带 AST 节点源码位置的一元运算符。
un_tok: struct {
/// 相对于 Decl AST 词法标记索引的偏移量。
src_tok: Ast.TokenOffset,
/// 此操作数的含义取决于对应的 `Tag`。
operand: Ref,
}用于一元运算符,带有 token 源码位置。
pl_node: struct {
/// 相对于 Decl AST 节点索引的偏移量。
/// `Tag` 决定所指向的 AST 节点类型。
src_node: Ast.Node.Offset,
/// extra 中的索引。
/// `Tag` 决定该位置存储的内容。
payload_index: u32,
}
pl_tok: struct {
/// 相对于 Decl AST 词法标记索引的偏移量。
src_tok: Ast.TokenOffset,
/// extra 中的索引。
/// `Tag` 决定该位置存储的内容。
payload_index: u32,
}
bin: Bin
str: struct {
/// `string_bytes` 中的偏移。
start: NullTerminatedString,
/// 字符串的字节数。
len: u32,
pub fn get(self: @This(), code: Zir) []const u8 {
return code.string_bytes[@backingInt(self.start)..][0..self.len];
}
}用于可能包含空字节的字符串。
str_tok: struct {
/// `string_bytes` 中的偏移量。以空字符终止。
start: NullTerminatedString,
/// 相对于 Decl AST 词法标记索引的偏移量。
src_tok: Ast.TokenOffset,
pub fn get(self: @This(), code: Zir) [:0]const u8 {
return code.nullTerminatedString(self.start);
}
}
tok: Ast.TokenOffset相对于 Decl AST 词法标记索引的偏移量。
node: Ast.Node.Offset相对于 Decl AST 节点索引的偏移。
int: u64
float: f64
ptr_type: struct {
flags: packed struct {
is_allowzero: bool,
is_mutable: bool,
is_volatile: bool,
has_sentinel: bool,
has_align: bool,
has_addrspace: bool,
has_bit_range: bool,
_: u1 = 0,
},
size: std.lang.Type.Pointer.Size,
/// 指向 extra 的索引。参见 `PtrType`。
payload_index: u32,
}
int_type: struct {
/// 相对于 Decl AST 节点索引的偏移量。
/// `Tag` 决定所指向的 AST 节点类型。
src_node: Ast.Node.Offset,
signedness: std.lang.Signedness,
bit_count: u16,
}
@"unreachable": struct {
/// 相对于 Decl AST 节点索引的偏移量。
/// `Tag` 决定所指向的 AST 节点类型。
src_node: Ast.Node.Offset,
}
@"break": struct {
operand: Ref,
/// `Break` 载荷的索引。
payload_index: u32,
}
dbg_stmt: LineColumn
inst_node: struct {
/// 相对于 Decl AST 节点索引的偏移。
src_node: Ast.Node.Offset,
/// 此操作数的含义取决于对应的 `Tag`。
inst: Index,
}用于引用一条指令、 并带有 AST 节点源码位置的一元运算符。
str_op: struct {
/// `string_bytes` 中的偏移量。以空字符终止。
str: NullTerminatedString,
operand: Ref,
pub fn getStr(self: @This(), zir: Zir) [:0]const u8 {
return zir.nullTerminatedString(self.str);
}
}
@"defer": struct {
index: u32,
len: u32,
}
save_err_ret_index: struct {
operand: Ref, // 若为错误类型(或 .none),保存新的跟踪索引
}
elem_val_imm: struct {
/// 正在访问的可索引值。
operand: Ref,
/// 被访问的索引。
idx: u32,
}
declaration: struct {
/// 此节点为该结构体内的全部指令提供新的绝对基准节点。
src_node: Ast.Node.Index,
/// extra 中指向 `Declaration` 载荷的索引。
payload_index: u32,
}
std.zig.Zir.Inst.Data.FieldEnum
容器
字段
extended
un_node
un_tok
pl_node
pl_tok
bin
str
str_tok
tok
node
int
float
ptr_type
int_type
@"unreachable"
@"break"
dbg_stmt
inst_node
str_op
@"defer"
save_err_ret_index
elem_val_imm
declaration
std.zig.Zir.Inst.Data.get
函数
pub fn get(self: @This(), code: Zir) []const u8
参数
self: @This()
code: Zir
std.zig.Zir.Inst.Data.get
函数
pub fn get(self: @This(), code: Zir) [:0]const u8
参数
self: @This()
code: Zir
std.zig.Zir.Inst.Data.getStr
函数
pub fn getStr(self: @This(), zir: Zir) [:0]const u8
参数
self: @This()
zir: Zir
std.zig.Zir.Inst.Declaration
容器
尾随数据:
- name: NullTerminatedString // 若
flags.id.hasName() - lib_name: NullTerminatedString // 若
flags.id.hasLibName() - type_body_len: u32 // 若
flags.id.hasTypeBody() - align_body_len: u32 // 若
flags.id.hasSpecialBodies() - linksection_body_len: u32 // 若
flags.id.hasSpecialBodies() - addrspace_body_len: u32 // 若
flags.id.hasSpecialBodies() - value_body_len: u32 // 若
flags.id.hasValueBody() - type_body_inst: Zir.Inst.Index
- 每个
type_body_len对应一项 - 此主体通过
break_inline退出并跳转到该declaration指令
- 每个
- align_body_inst: Zir.Inst.Index
- 每个
align_body_len对应一项 - 此主体通过
break_inline退出并跳转到该declaration指令
- 每个
- linksection_body_inst: Zir.Inst.Index
- 每个
linksection_body_len对应一项 - 此主体通过
break_inline退出并跳转到该declaration指令
- 每个
- addrspace_body_inst: Zir.Inst.Index
- 每个
addrspace_body_len对应一项 - 此主体通过
break_inline退出并跳转到该declaration指令
- value_body_inst: Zir.Inst.Index
- 每个
value_body_len对应一项 - 此主体通过
break_inline退出并跳转到该declaration指令 - 在此主体内,
declaration指令表示类型主体已解析出的类型
字段
src_hash_0: u32
src_hash_1: u32
src_hash_2: u32
src_hash_3: u32
flags_0: u32
flags_1: u32
std.zig.Zir.Inst.Declaration.Bodies
容器
字段
std.zig.Zir.Inst.Declaration.Flags
容器
字段
std.zig.Zir.Inst.Declaration.Flags.Id
容器
字段
unnamed_test
@"test"
decltest
@"comptime"
const_simple
const_typed
@"const"
pub_const_simple
pub_const_typed
pub_const
extern_const_simple
extern_const
pub_extern_const_simple
pub_extern_const
export_const
pub_export_const
var_simple
@"var"
var_threadlocal
pub_var_simple
pub_var
pub_var_threadlocal
extern_var
extern_var_threadlocal
pub_extern_var
pub_extern_var_threadlocal
export_var
export_var_threadlocal
pub_export_var
pub_export_var_threadlocal
std.zig.Zir.Inst.Declaration.Flags.Id.hasLibName
函数
pub fn hasLibName(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.hasName
函数
pub fn hasName(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.hasSpecialBodies
函数
pub fn hasSpecialBodies(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.hasTypeBody
函数
pub fn hasTypeBody(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.hasValueBody
函数
pub fn hasValueBody(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.isPub
函数
pub fn isPub(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.isThreadlocal
函数
pub fn isThreadlocal(id: Id) bool
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.kind
函数
pub fn kind(id: Id) Declaration.Unwrapped.Kind
参数
id: Id
std.zig.Zir.Inst.Declaration.Flags.Id.linkage
函数
pub fn linkage(id: Id) Declaration.Unwrapped.Linkage
参数
id: Id
std.zig.Zir.Inst.Declaration.getBodies
函数
pub fn getBodies(declaration: Declaration, extra_end: u32, zir: Zir) Bodies
参数
std.zig.Zir.Inst.Declaration.Name
容器
字段
std.zig.Zir.Inst.Declaration.Name.isNamedTest
函数
pub fn isNamedTest(name: Name, zir: Zir) bool
参数
std.zig.Zir.Inst.Declaration.Name.toString
函数
pub fn toString(name: Name, zir: Zir) ?NullTerminatedString
参数
std.zig.Zir.Inst.Declaration.Unwrapped
容器
字段
src_node: Ast.Node.Index
src_line: u32
src_column: u32
kind: Kind
name: NullTerminatedStringkind 为 unnamed_test 或 .@"comptime" 时,始终为 .empty
is_pub: bool当 kind 为 unnamed_test、.@"test"、.decltest 或 .@"comptime" 时,始终为 false。
is_threadlocal: bool始终为 false(当 kind != .@"var" 时)。
linkage: Linkage对于 kind != .@"const" and kind != .@"var",始终为 .normal。
lib_name: NullTerminatedString始终为 .empty(当 linkage != .@"extern" 时)。
type_body: ?[]const Inst.Index当 linkage == .@"extern" 时始终填充。
align_body: ?[]const Inst.Index
linksection_body: ?[]const Inst.Index
addrspace_body: ?[]const Inst.Index
value_body: ?[]const Inst.Index当 linkage != .@"extern" 时始终填充。
std.zig.Zir.Inst.Declaration.Unwrapped.Kind
容器
字段
unnamed_test
@"test"
decltest
@"comptime"
@"const"
@"var"
std.zig.Zir.Inst.Declaration.Unwrapped.Linkage
容器
字段
normal
@"extern"
@"export"
std.zig.Zir.Inst.ElemPtrImm
容器
字段
ptr: Ref
index: u32
std.zig.Zir.Inst.EnumDecl
容器
尾随内容:
- captures_len: u32, // 如果 has_captures_len 为真
- decls_len: u32, // 如果 has_decls_len 为真
- fields_len: u32, // 如果 has_fields_len 为真
- tag_type_body_len: u32, // 如果 has_tag_type 为真
- capture: Capture // 每个
captures_len对应一个 - capture_name: NullTerminatedString // 每个
captures_len对应一个 - decl: Index, // 每个
decls_len对应一个;指向declaration指令 - field_name: NullTerminatedString // 每个
fields_len对应一个 - field_value_body_len: u32 // 如果
any_field_values为真,则每个fields_len对应一个 - tag_type_body_inst: Inst.Index // 每个
tag_type_body_len对应一个 - body_inst: Inst.Index // 每个字段的值表达式体
字段
std.zig.Zir.Inst.EnumDecl.Small
容器
字段
has_captures_len: bool
has_decls_len: bool
has_fields_len: bool
name_strategy: NameStrategy
has_tag_type: bool
nonexhaustive: bool
any_field_values: bool
_: u8 = 0
std.zig.Zir.Inst.ErrorSetDecl
容器
尾随:
- field_name: NullTerminatedString // 对每个 fields_len
字段
fields_len: u32
std.zig.Zir.Inst.Export
容器
字段
std.zig.Zir.Inst.Extended
容器
字段
struct_decl结构体类型定义。包含指向字段类型、默认值及对齐值
所对应 ZIR 指令的引用。
operand 是指向 StructDecl 的负载索引。
small 为 StructDecl.Small。
enum_decl枚举类型定义。包含指向字段值表达式以及
可选类型标签表达式的 ZIR 指令引用。
operand 是 EnumDecl 的载荷索引。
small 为 EnumDecl.Small。
union_decl联合体类型定义。包含指向字段类型及可选类型标签表达式
所对应 ZIR 指令的引用。
operand 是指向 UnionDecl 的负载索引。
small 为 UnionDecl.Small。
opaque_decl不透明类型定义。包含对声明及捕获的引用。
operand 是指向 OpaqueDecl 的载荷索引。
small 为 OpaqueDecl.Small。
tuple_decl元组类型。注意,元组不是命名空间/容器类型。
operand 是指向 TupleDecl 的载荷索引。
small 为 fields_len: u16。
this实现 @This 内置函数。
operand 为 src_node: Ast.Node.Offset。
ret_addr实现 @returnAddress 内置函数。
operand 为 src_node: Ast.Node.Offset。
builtin_src实现 @src 内置函数。
operand 是指向 LineColumn 的载荷索引。
error_return_trace实现 @errorReturnTrace 内置函数。
operand 为 src_node: Ast.Node.Offset。
frame实现 @frame 内置函数。
operand 为 src_node: Ast.Node.Offset。
frame_address实现 @frameAddress 内置函数。
operand 为 src_node: Ast.Node.Offset。
alloc与 Tag 的 alloc 相同,但可能包含对齐指令。
operand 是指向 AllocExtended 的负载索引。
small:
- 0b000X - 有类型
- 0b00X0 - 有对齐值
- 0b0X00 - 1=const, 0=var
- 0bX000 - 为编译期
builtin_extern@extern 内置函数。
operand 是指向 BinNode 的载荷索引。
@"asm"内联汇编。
operand 为指向 Asm 的载荷索引。
asm_expr与 asm 相同,但汇编模板不是字符串字面量,而是编译时
表达式。
Asm 的 asm_source 字段不是以 null 结尾的字符串,
而是 Ref。
compile_log记录编译期变量,并产生错误消息。
operand 是指向 NodeMultiOp 的负载索引。
small 为 operands_len。
AST 节点是编译日志内置函数调用。
typeof_peer内置函数 @TypeOf 返回一个或多个参数经过
同级类型解析后的类型。
operand 是指向 TypeOfPeer 的负载索引。
small 为 operands_len。
AST 节点是内置函数调用。
min_multi实现参数超过两个时的 @min 内置函数。
operand 是指向 NodeMultiOp 的负载索引。
small 为 operands_len。
AST 节点是内置函数调用。
max_multi实现参数多于两个的内置函数 @max。
operand 是 NodeMultiOp 的载荷索引。
small 为 operands_len。
AST 节点为内置函数调用。
add_with_overflow实现内置函数 @addWithOverflow。
operand 是指向 BinNode 的载荷索引。
small 未使用。
sub_with_overflow实现 @subWithOverflow 内置函数。
operand 为指向 BinNode 的载荷索引。
small 未使用。
mul_with_overflow实现 @mulWithOverflow 内置函数。
operand 是指向 BinNode 的载荷索引。
small 未使用。
shl_with_overflow实现内置函数 @shlWithOverflow。
operand 是指向 BinNode 的载荷索引。
small 未使用。
round_op
round_op_ty返回舍入操作的操作数类型。
operand 为 UnNode。
small 未使用。
wasm_memory_sizeoperand 是指向 UnNode 的载荷索引。
wasm_memory_growoperand 是指向 BinNode 的载荷索引。
prefetch@prefetch 内置函数。
operand 为指向 BinNode 的载荷索引。
set_float_mode实现内置函数 @setFloatMode。
operand 是指向 UnNode 的载荷索引。
error_cast实现 @errorCast 内置函数。
operand 是指向 BinNode 的载荷索引。lhs 是目标类型,rhs 是操作数。
breakpoint实现 @breakpoint。
operand 为 src_node: Ast.Node.Offset。
disable_instrumentation实现内置函数 @disableInstrumentation。operand 为 src_node: Ast.Node.Offset。
disable_intrinsics实现内置函数 @disableIntrinsics。operand 为 src_node: i32。
select实现 @select 内置函数。
operand 是指向 Select 的载荷索引。
int_from_error实现内置函数 @errToInt。
operand 为指向 UnNode 的载荷索引。
error_from_int实现内置函数 @errorFromInt。
operand 是指向 UnNode 的载荷索引。
reify_slice_arg_ty给定类型为 []const A 的编译期已知操作数,返回类型 *const [operand.len]B。
类型 A 和 B 由 ReifySliceArgInfo 确定。
此指令用于为 @Fn、@Struct 等的参数提供结果类型。
operand 是指向 UnNode 的载荷索引。
small 是按位转换后的 ReifySliceArgInfo。
reify_enum_value_slice_ty类似 reify_slice_arg_ty,但专用于 @Enum 所需的 []const []const u8 到 []const TagInt 转换。
operand 是指向 BinNode 的负载索引。lhs 为类型 TagInt。rhs 为 []const []const u8 值。
small 未使用。
reify_pointer_sentinel_ty给定编译时已知、类型为 type 的操作数,如果可行,则返回类型 ?operand,否则返回 ?noreturn。
用于 @Pointer 的最后一个参数,以允许将不透明类型的指针实体化。
operand 是 UnNode 的载荷索引。
small 未使用。
reify_tuple实现内置函数 @Tuple。
operand 是指向 UnNode 的载荷索引。
reify_pointer实现内置函数 @Pointer。
operand 为指向 ReifyPointer 的载荷索引。
reify_fn实现内置函数 @Fn。
operand 是指向 ReifyFn 的载荷索引。
reify_struct实现内置函数 @Struct。
operand 是指向 ReifyStruct 的负载索引。
small 包含 NameStrategy。
reify_union实现内置函数 @Union。
operand 为指向 ReifyUnion 的载荷索引。
small 包含 NameStrategy。
reify_enum实现内置函数 @Enum。
operand 是指向 ReifyEnum 的载荷索引。
small 包含 NameStrategy。
reify_spirv_type实现内置函数 @SpirvType。
operand 是指向 ReifyFn 的载荷索引。
cmpxchg实现 @cmpxchgStrong 和 @cmpxchgWeak 内置函数。
small 0=>弱 1=>强
operand 是指向 Cmpxchg 的负载索引。
c_va_arg实现内置函数 @cVaArg。
operand 是指向 BinNode 的载荷索引。
c_va_copy实现内置函数 @cVaCopy。
operand 为指向 UnNode 的载荷索引。
c_va_end实现内置函数 @cVaEnd。
operand 是指向 UnNode 的载荷索引。
c_va_start实现内置函数 @cVaStart。
operand 为 src_node: Ast.Node.Offset。
ptr_cast_full实现以下内置函数:
@ptrCast、@alignCast、@addrSpaceCast、@constCast、@volatileCast。
表示上述内置函数的任意嵌套。这类嵌套视为
单个操作,可以修改指针类型的多个组成部分。
operand 为指向 BinNode 的载荷索引。
small 包含 FullPtrCastFlags。
AST 节点为嵌套转换的根。
lhs 为目标类型,rhs 为操作数。
ptr_cast_no_destoperand 是指向 UnNode 的负载索引。
small 包含 FullPtrCastFlags。
保证仅含不需要显式目标类型的标志,
即 const_cast 和 volatile_cast。
AST 节点是嵌套转换的根。
work_item_id实现 @workItemId 内置函数。
operand 为指向 UnNode 的载荷索引。
work_group_size实现 @workGroupSize 内置函数。
operand 为指向 UnNode 的载荷索引。
work_group_id实现 @workGroupId 内置函数。
operand 是指向 UnNode 的载荷索引。
in_comptime实现 @inComptime 内置函数。
operand 为 src_node: Ast.Node.Offset。
restore_err_ret_index将错误返回索引恢复为给定块中最后保存的状态。
若块为 .none,则恢复为函数入口时的状态。若操作数不是 .none,则仅当操作数值不是错误时执行恢复。
operand 是指向 RestoreErrRetIndex 的载荷索引。
small 未定义。
closure_get从当前类型声明作用域的闭包中获取值。
operand 为 src_node: Ast.Node.Offset。
small 为闭包索引。
value_placeholder用作占位指令,仅为一个虚拟索引,供 Sema 替换为特定值。
例如用于捕获 errdefer 的错误。
此项绝不应出现在主体中。
field_parent_ptr实现内置函数 @fieldParentPtr。
operand 是指向 FieldParentPtr 的载荷索引。
small 包含 FullPtrCastFlags。
保证没有 ptr_cast 标志。
使用 pl_node 联合体字段,载荷为 FieldParentPtr。
std_lang_value从 std.lang 获取类型或值。
operand 为 src_node: Ast.Node.Offset。
small 为 Inst.StdLangValue。
branch_hint为当前块提供 @branchHint。
operand 是指向 UnNode 的载荷索引。
small 未使用。
inplace_arith_result_ty计算原地算术运算的结果类型,例如 +=。
operand 是已加载左值的 Zir.Inst.Ref,而非其类型。
small 是 Inst.InplaceOp。
dbg_empty_stmt标记一个可以单步执行到达、但不生成代码的语句。
忽略 operand 和 small。
astgen_error此处 AstGen 遇到致命错误,导致此主体的 ZIR 降级终止。
已经报告文件级错误,Sema 应终止语义分析。
忽略 operand 和 small。
此指令始终为 noreturn,但 ZIR 层查询不将其视为此类指令,使 AstGen 可以假定此处原本可能存在任意代码,避免错误报告“不可达代码”。
float_op_result_ty给定类型,去除其外层堆叠的所有错误联合体或可选类型,
返回基础类型。基础类型必须是浮点类型。
例如,提供 error{Foo}!?f64 时,返回 f64。
operand 是指向 UnNode 的载荷索引。
std.zig.Zir.Inst.Extended.InstData
容器
字段
std.zig.Zir.Inst.Field
容器
字段
std.zig.Zir.Inst.FieldCall
容器
存储在 extra 中,尾随参数数量由 args_len 指定。
隐含的第 0 项:arg_0_start: u32,// 始终与 args_len 相同
- arg_end: u32,// 每个
args_len对应一项 arg_N_start 与 arg_N-1_end 相同。
字段
std.zig.Zir.Inst.FieldNamed
容器
字段
std.zig.Zir.Inst.FieldParentPtr
容器
字段
std.zig.Zir.Inst.FieldType
容器
字段
std.zig.Zir.Inst.FieldTypeRef
容器
字段
std.zig.Zir.Inst.Float128
容器
将 f128 值拆成 4 个 u32 部分。
字段
piece0: u32
piece1: u32
piece2: u32
piece3: u32
std.zig.Zir.Inst.Float128.get
函数
pub fn get(self: Float128) f128
参数
self: Float128
std.zig.Zir.Inst.FullPtrCastFlags
容器
字段
ptr_cast: bool = false
align_cast: bool = false
addrspace_cast: bool = false
const_cast: bool = false
volatile_cast: bool = false
std.zig.Zir.Inst.FullPtrCastFlags.needResultTypeBuiltinName
函数
pub inline fn needResultTypeBuiltinName(flags: FullPtrCastFlags) []const u8
参数
flags: FullPtrCastFlags
std.zig.Zir.Inst.Func
容器
尾随内容: if (ret_ty.body_len == 1) {
- return_type: Ref } if (ret_ty.body_len > 1) {
- return_type: Index // 每个 ret_ty.body_len 对应一个 }
- body: Index // 每个 body_len 对应一个
- src_locs: SrcLocs // 如果 body_len != 0
- proto_hash: std.zig.SrcHash // 如果 body_len != 0;函数原型的哈希
字段
std.zig.Zir.Inst.Func.RetTy
容器
字段
std.zig.Zir.Inst.Func.SrcLocs
容器
字段
lbrace_line: u32源码文件中相对于父声明的行索引。
rbrace_line: u32源码文件中相对于父声明的行索引。
columns: u32lbrace_column 为低位 u16 rbrace_column 为高位 u16
std.zig.Zir.Inst.FuncFancy
容器
尾随数据: if (has_cc_ref and !has_cc_body) {
- cc: Ref, } if (has_cc_body) {
- cc_body_len: u32
- cc_body: u32 // 每个 cc_body_len 对应一个 } if (has_ret_ty_ref and !has_ret_ty_body) {
- ret_ty: Ref, } if (has_ret_ty_body) {
- ret_ty_body_len: u32
- ret_ty_body: u32 // 每个 ret_ty_body_len 对应一个 }
- noalias_bits: u32 // 存在 has_any_noalias 时
- 从最低有效位开始,每一位对应一个参数索引
- body: Index // 每个 body_len 对应一个
- src_locs: Func.SrcLocs // body_len != 0 时
- proto_hash: std.zig.SrcHash // body_len != 0 时;函数原型的哈希值
字段
std.zig.Zir.Inst.FuncFancy.Bits
容器
若 has_cc_ref 和 has_cc_body 均为 false,则表示自动调用约定。 若 has_ret_ty_ref 和 has_ret_ty_body 均为 false,则表示 void 返回类型。
字段
is_var_args: bool
is_inferred_error: bool
is_noinline: bool
has_cc_ref: bool
has_cc_body: bool
has_ret_ty_ref: bool
has_ret_ty_body: bool
has_any_noalias: bool
ret_ty_is_generic: bool
_: u23 = 0
std.zig.Zir.Inst.Import
容器
字段
std.zig.Zir.Inst.Imports
容器
尾随数据:每个 imports_len 对应一个 Item
字段
imports_len: u32
std.zig.Zir.Inst.Imports.Item
容器
字段
std.zig.Zir.Inst.Index
容器
ZIR 指令在 Zir 指令数组中的位置。
字段
main_struct_inst = 0ZIR 的结构保证任意文件最外层的"main"结构体 始终位于索引 0。
_
std.zig.Zir.Inst.Index.toOptional
函数
pub fn toOptional(i: Index) OptionalIndex
参数
i: Index
std.zig.Zir.Inst.Index.toRef
函数
pub fn toRef(i: Index) Inst.Ref
参数
i: Index
std.zig.Zir.Inst.InplaceOp
容器
字段
add_eq
sub_eq
std.zig.Zir.Inst.LineColumn
容器
字段
line: u32
column: u32
std.zig.Zir.Inst.MulAdd
容器
字段
std.zig.Zir.Inst.MultiOp
容器
此数据存储在 extra 中,后跟由 operands_len 指定的操作数。
每个操作数均为 Ref。
字段
operands_len: u32
std.zig.Zir.Inst.NameStrategy
容器
字段
parent使用与父声明相同的名称。
例如 const Foo = struct {...};。
func使用当前执行的编译期函数调用名称及当前参数,
例如 ArrayList(i32)。
anon为此声明创建匿名名称。 例如:"ParentDeclName_struct_69"
dbg_var使用下一条 dbg_var_{val,ptr} 指令指定的名称。
std.zig.Zir.Inst.NodeMultiOp
容器
尾随:operand: Ref, // 对每个 operands_len(存储于 small)。
字段
std.zig.Zir.Inst.OpaqueDecl
容器
尾随数据:
- captures_len: u32, // 若 has_captures_len
- decls_len: u32, // 若 has_decls_len
- capture: Capture, // 每个 captures_len 对应一项
- capture_name: NullTerminatedString // 每个 captures_len 对应一项
- decl: Index, // 每个 decls_len 对应一项;指向
declaration指令
字段
std.zig.Zir.Inst.OpaqueDecl.Small
容器
字段
std.zig.Zir.Inst.OptionalIndex
容器
字段
std.zig.Zir.Inst.OptionalIndex.unwrap
函数
pub fn unwrap(oi: OptionalIndex) ?Index
参数
oi: OptionalIndex
std.zig.Zir.Inst.Param
容器
尾随内容:inst: Index // 每个 body_len 项对应一个
字段
std.zig.Zir.Inst.Param.Type
容器
字段
body_len: u31主体包含参数类型。
is_generic: bool该类型是否为泛型,即引用一个或多个先前的参数。
std.zig.Zir.Inst.PtrType
容器
存储在 extra 中。根据 Data 中的标志,其后最多有 5 个 Ref 字段:
- sentinel: Ref // 若设置
has_sentinel标志 - align: Ref // 若设置
has_align标志 - address_space: Ref // 若设置
has_addrspace标志 - bit_start: Ref // 若设置
has_bit_range标志 - host_size: Ref // 若设置
has_bit_range标志
字段
std.zig.Zir.Inst.Ref
容器
对 ZIR 指令或 InternPool 索引的引用,也可能两者都不是。
如果整数标签值 < InternPool.static_len, 则对应 InternPool 索引。否则,引用 ZIR 指令。
显式指定标签类型,使 []u32 与 []Ref
之间的位转换安全。
字段
u0_type
u1_type
u8_type
i8_type
u16_type
i16_type
u29_type
u32_type
i32_type
u64_type
i64_type
u80_type
u128_type
i128_type
u256_type
usize_type
isize_type
c_char_type
c_short_type
c_ushort_type
c_int_type
c_uint_type
c_long_type
c_ulong_type
c_longlong_type
c_ulonglong_type
c_longdouble_type
f16_type
f32_type
f64_type
f80_type
f128_type
anyopaque_type
bool_type
void_type
type_type
anyerror_type
comptime_int_type
comptime_float_type
noreturn_type
anyframe_type
null_type
undefined_type
enum_literal_type
ptr_usize_type
ptr_const_comptime_int_type
manyptr_u8_type
manyptr_const_u8_type
manyptr_const_u8_sentinel_0_type
slice_const_u8_type
slice_const_u8_sentinel_0_type
manyptr_const_slice_const_u8_type
slice_const_slice_const_u8_type
optional_type_type
manyptr_const_type_type
slice_const_type_type
vector_8_i8_type
vector_16_i8_type
vector_32_i8_type
vector_64_i8_type
vector_1_u8_type
vector_2_u8_type
vector_4_u8_type
vector_8_u8_type
vector_16_u8_type
vector_32_u8_type
vector_64_u8_type
vector_2_i16_type
vector_4_i16_type
vector_8_i16_type
vector_16_i16_type
vector_32_i16_type
vector_4_u16_type
vector_8_u16_type
vector_16_u16_type
vector_32_u16_type
vector_2_i32_type
vector_4_i32_type
vector_8_i32_type
vector_16_i32_type
vector_4_u32_type
vector_8_u32_type
vector_16_u32_type
vector_2_i64_type
vector_4_i64_type
vector_8_i64_type
vector_2_u64_type
vector_4_u64_type
vector_8_u64_type
vector_1_u128_type
vector_2_u128_type
vector_1_u256_type
vector_4_f16_type
vector_8_f16_type
vector_16_f16_type
vector_32_f16_type
vector_2_f32_type
vector_4_f32_type
vector_8_f32_type
vector_16_f32_type
vector_2_f64_type
vector_4_f64_type
vector_8_f64_type
optional_noreturn_type
anyerror_void_error_union_type
adhoc_inferred_error_set_type
generic_poison_type
empty_tuple_type
undef
undef_bool
undef_usize
undef_u1
zero
zero_usize
zero_u1
zero_u8
one
one_usize
one_u1
one_u8
four_u8
negative_one
void_value
unreachable_value
null_value
bool_true
bool_false
empty_tuple
none = std.math.maxInt(u32)此 Ref 不对应任何 ZIR 指令或常量 值,而可用作表示 null 的哨兵。
_
std.zig.Zir.Inst.Ref.static_len
常量
std.zig.Zir.Inst.Ref.toIndex
函数
pub fn toIndex(inst: Ref) ?Index
参数
inst: Ref
std.zig.Zir.Inst.Ref.toIndexAllowNone
函数
pub fn toIndexAllowNone(inst: Ref) ?Index
参数
inst: Ref
std.zig.Zir.Inst.ReifyEnum
容器
字段
std.zig.Zir.Inst.ReifyFn
容器
字段
std.zig.Zir.Inst.ReifyPointer
容器
字段
std.zig.Zir.Inst.ReifySliceArgInfo
容器
字段
type_to_fn_param_attrs输入元素类型为 type。
输出元素类型为 std.lang.Type.Fn.ParamAttributes。
string_to_struct_field_type输入元素类型为 []const u8。
输出元素类型为 type。
string_to_union_field_type除生成的错误消息略有不同外,与 string_to_struct_field_type 完全相同。
string_to_struct_field_attrs输入元素类型为 []const u8。
输出元素类型为 std.lang.Type.Struct.FieldAttributes。
string_to_union_field_attrs输入元素类型为 []const u8。
输出元素类型为 std.lang.Type.Union.FieldAttributes。
std.zig.Zir.Inst.ReifySpirvType
容器
字段
std.zig.Zir.Inst.ReifyStruct
容器
字段
std.zig.Zir.Inst.ReifyUnion
容器
字段
std.zig.Zir.Inst.RestoreErrRetIndex
容器
字段
std.zig.Zir.Inst.RoundOp
容器
字段
round
floor
ceil
trunc
std.zig.Zir.Inst.Select
容器
字段
std.zig.Zir.Inst.Shuffle
容器
字段
std.zig.Zir.Inst.SliceEnd
容器
字段
std.zig.Zir.Inst.SliceLength
容器
字段
std.zig.Zir.Inst.SliceSentinel
容器
字段
std.zig.Zir.Inst.SliceStart
容器
字段
std.zig.Zir.Inst.Src
容器
字段
std.zig.Zir.Inst.StdLangValue
容器
字段
atomic_order
atomic_rmw_op
calling_convention
address_space
float_mode
signedness
reduce_op
call_modifier
prefetch_options
export_options
extern_options
branch_hint
clobbers
pointer_size
pointer_attributes
fn_attributes
container_layout
enum_mode
spirv_type_options
calling_convention_c
calling_convention_inline
std.zig.Zir.Inst.StructDecl
容器
尾随数据:
- captures_len: u32 // 存在
has_captures_len时 - decls_len: u32, // 存在
has_decls_len时 - fields_len: u32, // 存在
has_fields_len时 - arg_baseline_src_node: Ast.Node.Index // 存在
has_backing_int时 - fields_baseline_src_node: Ast.Node.Index // 存在
has_fields_len时 - backing_int_body_len: u32 // 存在
has_backing_int时 - capture: Capture // 每个
captures_len对应一个 - capture_name: NullTerminatedString // 每个
captures_len对应一个 - decl: Index, // 每个
decls_len对应一个;指向declaration指令 - field_name: NullTerminatedString // 每个
fields_len对应一个 - field_type_body_len: u32 // 每个
fields_len对应一个 - field_align_body_len: u32 // 存在
fields_len且any_field_aligns时,每个字段对应一个 - field_default_body_len: u32 // 存在
fields_len且any_field_defaults时,每个字段对应一个 - field_comptime_bits: u32 // 存在
fields_len且any_comptime_fields时,每个字段对应一位 // 最低有效位对应第一个字段;使用最少数量的u32 - backing_int_body_inst: Inst.Index // 每个
backing_int_body_len对应一个 - body_inst: Inst.Index // 逐字段保存类型体、对齐体,最后是默认值体
字段
std.zig.Zir.Inst.StructDecl.Small
容器
字段
has_captures_len: bool
has_decls_len: bool
has_fields_len: bool
name_strategy: NameStrategy
layout: std.lang.Type.ContainerLayout
has_backing_int_type: bool始终为 false(当 layout != .@"packed" 时)。
any_field_aligns: bool
any_field_defaults: bool
any_comptime_fields: bool
_: u5 = 0
std.zig.Zir.Inst.StructInit
容器
尾随数据中每个字段对应一项。
字段
std.zig.Zir.Inst.StructInit.Item
容器
字段
std.zig.Zir.Inst.StructInitAnon
容器
尾随数据为每个字段对应的 Item。 TODO: 改为初始化值数组后跟名称数组,因为 这样可简化 Sema 代码并改善处理器缓存利用。
字段
std.zig.Zir.Inst.StructInitAnon.Item
容器
字段
std.zig.Zir.Inst.SwitchBlock
容器
尾随数据:
- multi_cases_len: u32, // 若已设置 has_multi_cases。
- payload_capture_placeholder: Inst.Index, // 若 payload_capture_inst_is_placeholder 已设置。 // 指令分支引用其载荷捕获时使用的索引。
- tag_capture_placeholder: Inst.Index, // 若 tag_capture_inst_is_placeholder 已设置。 // 指令分支引用其标签捕获时使用的索引。
- catch_or_if_src_node_offset: Ast.Node.Offset, // 若 inst 为 switch_block_err_union。
- non_err_info: ProngInfo.NonErr, // 若 inst 为 switch_block_err_union。
- else_info: ProngInfo.Else, // 若已设置 has_else。
- scalar_prong_info: ProngInfo, // 对每个 scalar_cases_len
- multi_prong_info: ProngInfo, // 对每个 multi_cases_len
- multi_case_items_len: u32, // 对每个 multi_cases_len
- multi_case_ranges_len: u32, // 若已设置 has_ranges:对每个 multi_cases_len
- scalar_item_info: ItemInfo, // 对每个 scalar_cases_len
- multi_items_info: { // 对每个 multi_cases_len item_info: ItemInfo, // 对每个 multi_case_items_len range_items_info: { // 对每个 multi_case_ranges_len first_info: ItemInfo, last_info: ItemInfo, } }
- non_err_body { body_inst: Index // 对每个 non_err_info.body_len }
- else_body: { // 若已设置 has_else。 body_inst: Inst.Index, // 对每个 else_info.body_len }
- scalar_bodies: { // 对每个 scalar_cases_len prong_body: { // 对 scalar_prong_info 中的每个 body_len body_inst: Inst.Index, // 对每个 body_len } item_body: { // 对 scalar_item_info 中的每个 body_len body_inst: Inst.Index, // 对每个 body_len } }
- multi_bodies: { // 对 multi_items_info 中的每一项 prong_body: { body_inst: Inst.Index, // 对 multi_prong_info.body_len 中的每个值 } item_body: { // 对每个 item_info body_inst: Inst.Index, // 对每个 item_info.body_len } range_bodies: { // 对 range_items_info 中的每个 .{first_info, last_info} first_body_inst: Inst.Index, // 对每个 first_info.body_len last_body_inst: Inst.Index, // 对每个 last_info.body_len } }
字段
std.zig.Zir.Inst.SwitchBlock.Bits
容器
字段
has_multi_cases: bool若为 true,则一个或多个分支包含多个项。
any_ranges: bool若为 true,则一个或多个分支包含范围。
仅当同时设置 has_multi_cases 时有效。
has_else: bool
has_under: bool
has_continue: bool为 true 时,至少一个分支包含 continue。
仅在设置 has_label 时有效。
any_maybe_runtime_capture: bool
payload_capture_inst_is_placeholder: bool
tag_capture_inst_is_placeholder: bool
scalar_cases_len: ScalarCasesLen
std.zig.Zir.Inst.SwitchBlock.Bits.ScalarCasesLen
类型
std.zig.Zir.Inst.SwitchBlock.ItemInfo
容器
字段
std.zig.Zir.Inst.SwitchBlock.ItemInfo.bodyLen
函数
pub fn bodyLen(item_info: ItemInfo) ?u32
参数
item_info: ItemInfo
std.zig.Zir.Inst.SwitchBlock.ItemInfo.Kind
容器
字段
enum_literal
error_value
body_len
under
std.zig.Zir.Inst.SwitchBlock.ItemInfo.unwrap
函数
pub fn unwrap(item_info: ItemInfo) ItemInfo.Unwrapped
参数
item_info: ItemInfo
std.zig.Zir.Inst.SwitchBlock.ItemInfo.Unwrapped
容器
字段
std.zig.Zir.Inst.SwitchBlock.ItemInfo.wrap
函数
pub fn wrap(unwrapped: ItemInfo.Unwrapped) ItemInfo
参数
std.zig.Zir.Inst.SwitchBlock.ProngInfo
容器
字段
std.zig.Zir.Inst.SwitchBlock.ProngInfo.Capture
容器
字段
none
by_val
by_ref
std.zig.Zir.Inst.SwitchBlock.ProngInfo.Else
容器
字段
std.zig.Zir.Inst.SwitchBlock.ProngInfo.NonErr
容器
字段
std.zig.Zir.Inst.Tag
容器
字段
add算术加法,断言不会发生整数溢出。
使用 pl_node 联合体字段。载荷为 Bin。
addwrap二进制补码回绕整数加法。
使用 pl_node 联合体字段。载荷为 Bin。
add_sat饱和加法。
使用载荷为 Bin 的 pl_node 联合体字段。
add_unsafe与 add 相同,但不进行安全检查。
sub算术减法。断言整数不会溢出。
使用 pl_node 联合体字段,载荷为 Bin。
subwrap采用二进制补码回绕的整数减法。
使用 pl_node 联合体字段,载荷为 Bin。
sub_sat饱和减法。
使用 pl_node 联合体字段。载荷为 Bin。
mul算术乘法。断言整数不会溢出。
使用 pl_node 联合体字段,载荷为 Bin。
mulwrap采用二进制补码回绕的整数乘法。
使用 pl_node 联合体字段,载荷为 Bin。
mul_sat饱和乘法。
使用载荷为 Bin 的 pl_node 联合体字段。
div_exact实现 @divExact 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
div_floor实现 @divFloor 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
div_ceil实现 @divCeil 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
div_trunc实现 @divTrunc 内置函数。
使用带有 Bin 载荷的 pl_node 联合体字段。
mod实现 @mod 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
rem实现 @rem 内置函数。
使用载荷为 Bin 的 pl_node 联合体字段。
mod_rem含义可能为取余或取模。如果两种运算
可能产生不同值,则发出
编译错误;否则执行计算。
使用 pl_node 联合体字段,载荷为 Bin。
shl整数左移。从右侧移入零。
使用 pl_node 联合体字段。载荷为 Bin。
shl_exact实现 @shlExact 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
shl_sat饱和左移。
使用载荷为 Bin 的 pl_node 联合体字段。
shr整数右移。根据整数类型是否有符号,执行算术右移或逻辑右移。
使用载荷为 Bin 的 pl_node 联合体字段。
shr_exact实现 @shrExact 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
param声明当前函数的参数,用于:
- 调试信息。
- 检查是否遮蔽当前命名空间中的声明。
- 使参数类型表达式能引用其他参数。
这些指令出现在函数主体之外的块中,与包含 func 指令的是同一个块。
使用
pl_tok字段。词法单元为参数名,载荷为Param。
param_comptime与 param 相同,但参数被标记为 comptime。
param_anytype与 param 相同,但参数被标记为 anytype。
使用 str_tok 字段。token 是参数名称,字符串也是参数名称。
param_anytype_comptime与 param 相同,但参数同时标记为 comptime 和 anytype。
使用 str_tok 字段。词法标记是参数名。字符串也是参数名。
array_cat数组拼接。a ++ b
使用 pl_node 联合体字段。载荷为 Bin。
array_type[N]T 语法。不提供源码位置。
使用 pl_node 联合体字段。负载为 Bin。lhs 为长度,rhs 为元素类型。
array_type_sentinel[N:S]T 语法。源码位置为数组类型表达式节点。
使用 pl_node 联合体字段。载荷为 ArrayTypeSentinel。
reify_int@Int 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
lhs 为符号性,rhs 为位数。
vector_type@Vector 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
lhs 为长度,rhs 为元素类型。
elem_type给定指针类型,返回其元素类型。穿透包装该指针的任意可选类型或错误
联合体类型。断言底层类型为指针类型。
如果元素类型为 anyopaque,则返回泛型 poison。
使用 un_node 字段。
indexable_ptr_elem_type给定可索引指针,如切片、多项指针或指向数组的单项指针,返回其
元素类型。若类型不是可索引指针,则产生编译错误。
使用 un_node 字段。
splat_op_result_ty给定向量或数组类型,去掉其外层的所有错误联合体或 可选类型,并返回元素类型。
!?[N]T -> T
使用 un_node 字段。
from_backing_int_arg_ty给定一个类型,移除其外层叠加的所有错误联合体
或可选类型,验证其是否可用于 @fromBackingInt,
并返回其底层整数类型。
E!?enum(T) { _ } -> T
使用 un_node 字段。
indexable_ptr_len给定指向可索引对象的指针,返回其 len 属性。此指令
供 for 循环使用。如果对象不可索引,此指令还会生成 for 循环专用的编译
错误。
使用 un_node 字段。AST 节点为 for 循环节点。
anyframe_type创建 anyframe->T 类型。
使用 un_node 字段。
as_node强制转换为函数返回类型。
使用 pl_node 字段。负载为 As。AST 节点可能有多种形式。
as_shift_operand与 as_node 相同,但忽略运行期整数转编译期整数的错误。
bit_and按位与。&
bitcast将值的内存表示重新解释为另一种类型。
使用 pl_node 字段,载荷为 Bin。
bit_not按位非。~
使用 un_node。
bit_or按位或。|
block可返回值的带标签代码块。
使用 pl_node 联合体字段。载荷为 Block。
block_comptime类似 block,但强制在编译期完整求值其内容。
通过 break_inline 退出。
使用 pl_node 联合体字段。载荷为 BlockComptime。
block_inline一组在父上下文中分析、而不生成运行时块的指令。
必须以 noreturn 指令的“inline”变体结束。
使用 pl_node 联合体字段。载荷为 Block。
declaration此指令只能出现在命名空间类型的声明列表中,例如 struct_decl 指令内部。
它表示单个源码声明,即 const、var 或 fn,包含声明的名称、属性、类型和值。
使用 declaration 联合体字段,载荷为 Declaration。
suspend_block实现 suspend {...}。
使用 pl_node 联合体字段。载荷为 Block。
bool_not布尔非。另见 bit_not。
使用 un_node 字段。
bool_br_and
bool_br_or
@"break"从块返回值。
使用 break 联合体字段。
使用前一条指令的源码信息。
break_inline从块中返回值。此指令用作 block_inline 的
终结指令,使得可以使用 Sema.analyzeBody 的返回值。
当已知块中只有一个 break 指令,且目标块是父块时,
也可以使用此指令。
使用 break 联合体字段。
switch_continue从 switch 分支内部跳转到操作数指定的分支。
使用 break 联合体字段。block_inst 指向 switch_block/
switch_block_ref/switch_block_err_union。
check_comptime_control_flow检查编译期控制流未发生在运行时块内部。
使用 un_node 联合体字段。
call函数调用。
使用 pl_node 联合体字段,载荷为 Call。
AST 节点即为函数调用。
field_call使用 a.b() 语法进行函数调用。
使用具名字段作为被调用方。若不存在该字段,则在类型中搜索与字段名匹配的声明。解析该声明,并确认它是能够接受该对象作为第一个参数的函数,允许一次指针修正。随后以该函数作为被调用方,对象作为隐式的第一个参数。
使用 pl_node 联合体字段,载荷为 FieldCall。
AST 节点为函数调用。
builtin_call实现 @call 内置函数。
使用 pl_node 联合体字段,载荷为 BuiltinCall。
AST 节点为内置函数调用。
cmp_lt<
使用 pl_node 联合体字段。载荷为 Bin。
cmp_lte<=
使用 pl_node 联合体字段。载荷为 Bin。
cmp_eq==
使用 pl_node 联合体字段。载荷为 Bin。
cmp_gte>=
使用 pl_node 联合体字段。载荷为 Bin。
cmp_gt>
使用 pl_node 联合体字段。载荷为 Bin。
cmp_neq!=
使用 pl_node 联合体字段。载荷为 Bin。
condbr条件分支。根据布尔条件值拆分控制流。
使用 pl_node 联合体字段。AST 节点为 if、while、for 等。
载荷为 CondBr。
condbr_inline与 condbr 相同,但条件被强制转换为编译时值,
且仅分析实际选择的分支。then 块及 else 块必须
以 noreturn 指令的“inline”变体终止。
@"try"给定错误联合体操作数,拆分控制流。
发生错误时,控制流进入此指令包含的
块,该块保证以返回指令结束,
绝不会跳出此块。
没有错误时,控制流继续执行 try 后的下一条指令,
此指令的结果是解包后的载荷值,
如同对操作数执行了 err_union_payload_unsafe。
使用 pl_node 联合体字段,载荷为 Try。
try_ptr与 try 相同,但操作数和结果均为指针。
error_set_decl错误集合类型定义。包含字段名称列表。
使用载荷为 ErrorSetDecl 的 pl_node 联合体字段。
dbg_stmt声明语句的开始,用于调试信息。
使用 dbg_stmt 联合体字段。行和列相对于父声明偏移。
dbg_var_ptr标记变量声明。用于调试信息。
使用 str_op 联合体字段。字符串为局部变量名,
操作数为指向变量位置的指针。
该局部变量可以是 const 或 var。
dbg_var_val与 dbg_var_ptr 相同,但局部变量始终为 const,操作数
为该局部变量的值。
decl_ref按名称标识 Decl,并获取指向它的指针。
使用 str_tok 联合体字段。
decl_val使用名称标识 Decl,并将其作为值使用。
使用 str_tok 联合体字段。
load从指针加载值。假定采用 x.* 语法。
使用 un_node 字段。AST 节点为 x.* 语法。
div算术除法。断言无整数溢出。
使用 pl_node 联合体字段。载荷为 Bin。
elem_ptr_node给定指向数组、切片或指针的指针,返回指向
指定索引处元素的指针。
使用 pl_node 联合体字段。AST 节点是 a[b] 语法。负载为 Bin。
elem_ptr与 elem_ptr_node 相同,但仅用于 for 循环。
使用 pl_node 联合体字段。AST 节点为 for 循环条件。
载荷为 Bin。
不生成越界安全检查。
elem_ptr_load给定指向数组、切片或指针的指针,加载 指定索引处的元素。
使用 pl_node 联合体字段。AST 节点为 a[b] 语法。载荷为 Bin。
elem_val
elem_val_imm与 elem_val 相同,但将索引作为立即数传入。
不生成越界安全检查。先前指令必须验证此操作。
使用 elem_val_imm 联合体字段。
ensure_result_used若操作数不是 void,则产生编译错误。
使用 un_node 字段。
ensure_result_non_error若忽略错误,则产生编译错误。
使用 un_node 字段。
ensure_err_union_payload_void错误联合的载荷不为 void 时,生成编译错误。
error_union_type创建 E!T 类型。
使用载荷为 Bin 的 pl_node 字段。
error_valueerror.Foo 语法。使用 Data 联合体的 str_tok 字段。
@"export"实现 @export 内置函数。
使用 pl_node 联合体字段。载荷为 Export。
field_ptr给定指向结构体或含虚拟字段对象的指针,返回
指向指定名称字段的指针。字段名存储在 string_bytes 中,用于 a.b 语法。
使用 pl_node 字段,AST 节点为 a.b 语法,载荷为 Field。
field_ptr_load给定指向结构体或含虚拟字段对象的指针,从指定名称的字段加载。
字段名存储在 string_bytes 中,由 a.b 语法使用。
此指令也接受指针。
使用 pl_node 字段。AST 节点为 a.b 语法,载荷为 Field。
field_ptr_named给定包含虚拟字段的结构体或对象指针,返回指向指定字段的指针。
字段名是编译时指令。供 @field 使用。
使用 pl_node 字段。AST 节点为内置函数调用。载荷为 FieldNamed。
field_ptr_named_load给定指向包含虚拟字段的结构体或对象的指针, 从指定名称的字段加载。
字段名称为编译期指令。由 @field 使用。
使用 pl_node 字段。AST 节点是内置调用。负载为 FieldNamed。
func根据 body_len 是否为 0,返回函数类型或函数实例。
调用约定为 auto。
使用 pl_node 联合体字段。payload_index 指向 Func。
func_inferred与 func 相同,但使用推断的错误集合。
func_fancy表示函数声明或函数原型,取决于
body_len 是否为 0。
使用 pl_node 联合体字段。payload_index 指向 FuncFancy。
import实现 @import 内置函数。
使用 pl_tok 字段。
int可由 u64 表示的整数字面量。使用 int 联合体字段。
int_big任意大小的整数字面量。使用 str 联合体字段。
float可容纳于 f64 的浮点字面量。使用 float 联合体值。
float128可以用 f128 表示的浮点数字面量。使用 pl_node 联合体值。
载荷为 Float128。
int_type由符号性和位数构造整数类型。
载荷为 int_type
is_non_null可选值为 null 时返回布尔值 false。x != null
使用 un_node 字段。
is_non_null_ptr如果可选值为 null,则返回布尔值 false。x.* != null
使用 un_node 字段。
is_non_err值为错误时返回布尔值 false
使用 un_node 字段。
is_non_err_ptr若解引用的指针是错误,则返回布尔值 false。
使用 un_node 字段。
ret_is_non_err与 is_non_er 相同,但不验证该类型是否可以是错误。
使用 un_node 字段。
loop永久循环的带标签代码块。主体末尾会有 repeat 或 repeat_inline 指令。
使用 pl_node 字段,AST 节点为 for 或 while 循环。
需要此 ZIR 指令,是因为 AIR 尚未与 ZIR 一致,Sema 需要为它生成多个 AIR 块。
载荷为 Block。
repeat将运行时控制流送回当前块的开头。
使用 node 字段。
repeat_inline将编译期控制流转回当前块的起始位置。
使用 node 字段。
for_len断言提供的全部长度一致。用于构建 for 循环。
返回值为 usize 类型的长度。
使用 pl_node 字段,其载荷为 MultiOp。
for 循环条件中的每个 AST 节点对应两个项目。
若一对中的两个项目均为 .none,该节点表示无界范围。
若只有第二个项目为 .none,第一个项目表示可索引对象。
否则,节点表示有界范围 a..b,两个项目分别为 a 和 b。
非法行为:
- 全部长度均为无界范围,始终产生编译错误。
- 任意两个长度不一致。
merge_error_sets将两个错误集合合并为一个:E1 || E2。
使用载荷为 Bin 的 pl_node 字段。
ref将右值转换为常量左值,即取一个值,
将其存入内存位置,并返回指向它的常量单元素指针。
若值为 comptime,内存位置为全局静态常量数据。
否则,内存位置位于栈帧中,局部于
包含该指令的作用域。
使用 un_tok 联合体字段。
deref实现解引用运算符(.*)。检查操作数是否为
支持直接解引用的指针。
使用 un_node 联合体字段。
ref_deref对结果位置为引用的解引用操作 .* 生成此指令。
检查操作数是支持直接解引用的指针,
并返回指向解引用内存位置的单项指针。
使用 un_node 联合体字段。
ret_node将控制流送回函数的被调用方(原文如此)。
包含作为返回值的操作数。
包含 AST 节点的源码位置。
使用 un_node 联合体字段。
ret_load将控制流返回给函数的被调用方。
操作数为 ret_ptr 指令,返回值可在其指向的位置找到。
包含 AST 节点源码位置。
使用 un_node 联合体字段。
ret_implicit将控制流送回函数的被调用方。
包含作为返回值的操作数。
包含标记源码位置。
使用 un_tok 联合体字段。
ret_err_value将控制流返回到函数的被调用方(原文如此)。
返回操作数为 error.foo,其中 foo 由字符串指定。
若当前函数具有推断错误集合,则将此名称指定的错误
加入该集合。
使用 str_tok 联合体字段。
ret_ptr获取指向返回值的指针。
使用 node 联合体字段。
ret_type获取当前作用域内函数的返回类型。
使用 node 联合体字段。
ptr_type创建可具有哨兵、对齐、地址空间和/或位范围的指针类型。
使用 ptr_type 联合体字段。
slice_start切片操作 lhs[rhs..]。没有哨兵,也没有结束偏移量。
返回指向子切片的指针。
使用 pl_node 字段。AST 节点为切片语法。载荷为 SliceStart。
slice_end切片操作 array_ptr[start..end]。没有哨兵。
返回指向子切片的指针。
使用 pl_node 字段。AST 节点为切片语法。载荷为 SliceEnd。
slice_sentinel切片操作 array_ptr[start..end:sentinel]。
返回指向子切片的指针。
使用 pl_node 字段。AST 节点是切片语法。负载为 SliceSentinel。
slice_length切片操作 array_ptr[start..][0..len]。可选哨兵。
返回指向子切片的指针。
使用 pl_node 字段。AST 节点为切片语法。载荷为 SliceLength。
slice_sentinel_ty给定一个指向切片操作左侧表达式的指针值,返回哨兵类型,
用作切片哨兵的结果类型,即 lhs[a..b :s] 中的 s。
使用 un_node 字段。AST 节点为切片语法。操作数为 lhs。
store_node与 store 相同,但提供源码位置。
使用 pl_node 联合体字段,载荷为 Bin。
store_to_inferred_ptr与 store_node 相同,但将使用待存储值的类型,
推断 alloc_inferred 的指针类型。
使用 pl_node 联合体字段。负载为 Bin。
str字符串字面量。创建匿名 Decl,然后取得指向它的指针。
使用 str 联合体字段。
negate算术取负。断言不发生整数溢出。
相当于左操作数为 0 的 sub;拆为独立指令以节省内存。
使用 un_node。
negate_wrap采用二进制补码回绕语义的整数取负。
相当于左操作数为 0 的 subwrap;拆为独立指令以节省内存。
使用 un_node。
typeof返回值的类型。
使用 un_node 字段。
typeof_builtin针对单个操作数实现 @TypeOf。
使用 pl_node 字段,载荷为 Block。
typeof_log2_int_type给定一个值,检查其类型,该类型必须是整数。
返回移位操作右侧操作数的整数类型。
使用 un_node 字段。
@"unreachable"断言控制流不会到达此指令(unreachable)。
使用 @"unreachable" 联合体字段。
xor按位异或。^
使用载荷为 Bin 的 pl_node 联合体字段。
optional_type创建可选类型 '?T'
使用 un_node 字段。
optional_payload_safe?T => T,带安全检查。
给定可选值,返回其载荷值,并执行非空
安全检查。用于 orelse、if 和 while。
使用 un_node 字段。
optional_payload_unsafe?T => T,不执行安全检查。
给定可选值,返回负载值。没有安全检查。
使用 un_node 字段。
optional_payload_safe_ptr*?T => *T,带安全检查。
给定指向可选值的指针,返回指向载荷值的指针,
并安全检查该值非 null。供 orelse、if 和 while 使用。
使用 un_node 字段。
optional_payload_unsafe_ptr*?T => *T,不进行安全检查。
给定指向可选值的指针,返回指向载荷值的指针。
不进行安全检查。
使用 un_node 字段。
err_union_payload_unsafe不进行安全检查的 E!T => T。
给定错误联合体值,返回载荷值。不执行安全检查。
使用 un_node 字段。
err_union_payload_unsafe_ptr*E!T => *T,不进行安全检查。
给定指向错误联合体值的指针,返回指向载荷值的指针。
不进行安全检查。
使用 un_node 字段。
err_union_codeE!T => E,不执行安全检查。
给定错误联合体值,返回错误码。没有安全检查。
使用 un_node 字段。
err_union_code_ptr*E!T => E,不执行安全检查。
给定错误联合体值的指针,返回错误码。没有安全检查。
使用 un_node 字段。
enum_literal枚举字面量。使用 str_tok 联合体字段。
decl_literal声明字面量。类似 field,但会解包错误联合体和可选值,
并将结果强制转换为给定类型。
使用 pl_node 联合体字段。载荷为 Field。
decl_literal_no_coerce与 decl_literal 相同,但省略强制转换。用于声明字面量
函数调用语法,即 .foo()。
使用 pl_node 联合体字段。负载为 Field。
switch_blockswitch 表达式。使用 pl_node 联合体字段。
AST 节点为 switch,载荷为 SwitchBlock。
switch_block_refswitch 表达式。使用 pl_node 联合体字段。
AST 节点为 switch,载荷为 SwitchBlock,操作数为指针。
switch_block_err_union针对错误联合的 switch:
eu catch |err| switch (err) {...},AST 节点为catch。if (eu) |payload| {...} else |err| {...},AST 节点为if。 使用pl_node联合体字段。载荷为SwitchBlock。
validate_destructure检查操作数类型是否为包含给定数量元素的数组或元组。
使用 pl_node 字段,载荷为 ValidateDestructure。
field_type_ref给定结构体或联合体,以及用 Ref 表示的字段名称,
返回字段类型。使用 pl_node 字段。载荷为 FieldTypeRef。
opt_eu_base_ptr_init给定指针,将被指对象中的所有错误联合体和可选值初始化为载荷,
并返回基础载荷指针。例如,将 *E!?T 转换为有效的 *T
(覆盖任何已有错误值或 null 值)。
使用 un_node 字段。
coerce_ptr_elem_ty强制转换给定值,使取得其引用后得到的指针
可强制转换为给定类型。例如,给定 u32 类型值和指针
类型 *u64,将该值转换为 u64。断言给定类型为指针类型。
使用 pl_node 字段,载荷为 Bin。
左操作数是指针类型,右操作数是值。
validate_ref_ty给定一个类型,验证其是否为适合由取地址运算符返回的指针类型。
若不是,则产生编译错误。
使用 un_tok 联合体字段。词法标记为 & 运算符。操作数为该类型。
validate_const给定一个值,检查它是否为此作用域内有效的局部常量。
在运行时作用域中,始终不执行任何操作。
在编译时作用域中,如果该值仅在运行时已知,则产生编译错误。
结果始终为 void。
使用 un_node 联合体字段。节点为初始化表达式,操作数为初始化值。
struct_init_empty带有指定显式类型且不含字段的结构体字面量。
使用 un_node 字段。
struct_init_empty_result结果类型已知且无字段的匿名结构体字面量。
使用 un_node 字段。
struct_init_empty_ref_result无字段的匿名结构体字面量,以引用形式返回,且指针的
结果类型已知。断言该类型为指针。
使用 un_node 字段。
struct_init_anon无类型的结构体初始化。创建匿名结构体类型的值。
使用 pl_node 字段,载荷为 StructInitAnon。
struct_init完成带类型的结构体或联合体初始化,执行验证,并返回
结构体或联合体值。给定类型必须在此指令之前,使用
validate_struct_init_ty 或 validate_struct_init_result_ty 验证。若类型为
泛型毒值,则降级为匿名初始化。
使用 pl_node 字段,载荷为 StructInit。
struct_init_ref结构体初始化语法,使结果为指针。等价于 struct_init 后跟 ref;
此 ZIR 标签用于优化这一常见模式。
使用 pl_node 字段。载荷为 StructInit。
validate_struct_init_ty检查类型是否支持结构体初始化语法。始终返回 void。
使用 un_node 字段。
validate_struct_init_result_ty类似 validate_struct_init_ty,但额外接受结构体可强制转换到的类型。
用于结构体初始化表达式已知的结果类型。始终返回 void。
使用 un_node 字段。
validate_ptr_struct_init给定一组 struct_init_field_ptr 指令,假定它们均属于同一个结构体初始化表达式,并在适用时对重复字段和缺失字段产生编译错误。
此指令断言至少存在一条 struct_init_field_ptr 指令,因为必须通过其中一条确定结构体类型。
使用 pl_node 字段,载荷为 Block。
struct_init_field_type给定用于结构体初始化表达式的类型,返回指定名称字段的类型。
使用 pl_node 字段。负载为 FieldType。
struct_init_field_ptr给定用作结构体初始化表达式结果指针的指针,
返回指向给定名称字段的指针。
使用 pl_node 字段。AST 节点为字段初始化器。载荷为 Field。
array_init_anon无类型数组初始化。创建元组类型的值。
使用 pl_node 字段,载荷为 MultiOp。
array_init具有已知类型的数组初始化语法。必须在此指令之前,
通过某个 validate_array_init_*_ty 指令验证给定类型。
使用 pl_node 字段,载荷为 MultiOp,其中首个操作数为类型。
array_init_ref数组初始化语法,使结果为指针。等同于 array_init
后跟 ref;此 ZIR 标签是对常见模式的优化。
使用 pl_node 字段,载荷为 MultiOp,其中首个操作数为类型。
validate_array_init_ty检查类型是否支持数组初始化语法。始终返回 void。
使用 pl_node 字段。载荷为 ArrayInit。
validate_array_init_result_ty类似 validate_array_init_ty,但额外接受数组可强制转换到的类型。
用于数组初始化表达式已知的结果类型。始终返回 void。
使用 pl_node 字段。载荷为 ArrayInit。
validate_array_init_ref_ty给定指针或切片类型及元素数量,返回数组初始化器的预期类型,
使指向该初始化器的指针具有给定指针类型。检查
该类型是否支持数组初始化语法,不支持时产生编译错误。若数组类型
带有错误联合体或可选包装,则保留这些包装。
断言给定类型为指针或切片类型。
使用 pl_node 字段,载荷为 ArrayInitRefTy。
validate_ptr_array_init给定一组 array_init_elem_ptr 指令,假定它们均属于同一数组
初始化表达式;若元素数量与数组类型
不匹配,产生编译错误。
此指令断言至少存在一条 array_init_elem_ptr 指令,
因为必须借助其中一条确定数组类型。
使用 pl_node 字段,载荷为 Block。
array_init_elem_type给定用于数组初始化表达式的类型,返回
指定索引处元素的类型。
使用 bin 联合体字段。lhs 为可索引类型,rhs 为索引。
array_init_elem_ptr给定一个正被用作数组初始化表达式结果指针的指针,
返回指向给定索引处元素的指针。
使用 pl_node 联合体字段。AST 节点为数组初始化
语法中的一个元素。载荷为 ElemPtrImm。
union_init实现 @unionInit 内置函数。
使用 pl_node 字段。载荷为 UnionInit。
type_info实现 @typeInfo 内置函数。使用 un_node。
size_of实现 @sizeOf 内置函数。使用 un_node。
bit_size_of实现 @bitSizeOf 内置函数。使用 un_node。
int_from_ptr实现内置函数 @intFromPtr。使用 un_node。
将指针转换为 usize 整数。
compile_error输出错误消息并使编译失败。
使用 un_node 字段。
set_eval_branch_quota更改编译时执行代码所能使用的最大向后分支次数;
超过此次数将放弃执行并产生编译错误。
使用 un_node 联合体字段。
int_from_enum将枚举值转换为整数。结果类型为枚举的标签类型。
使用 un_node。
backing_int实现 @backingInt 内置函数。
使用 un_node。
align_of实现内置函数 @alignOf。使用 un_node。
int_from_bool实现内置函数 @intFromBool。使用 un_node。
embed_file实现内置函数 @embedFile。使用 un_node。
error_name实现内置函数 @errorName。使用 un_node。
panic实现内置函数 @panic。使用 un_node。
trap实现 @trap。
使用 node 字段。
set_runtime_safety实现内置函数 @setRuntimeSafety。使用 un_node。
sqrt实现内置函数 @sqrt。使用 un_node。
sin实现内置函数 @sin。使用 un_node。
cos实现内置函数 @cos。使用 un_node。
tan实现内置函数 @tan。使用 un_node。
exp实现内置函数 @exp。使用 un_node。
exp2实现内置函数 @exp2。使用 un_node。
log实现内置函数 @log。使用 un_node。
log2实现内置函数 @log2。使用 un_node。
log10实现内置函数 @log10。使用 un_node。
abs实现内置函数 @abs。使用 un_node。
floor实现内置函数 @floor。使用 un_node。
ceil实现内置函数 @ceil。使用 un_node。
trunc实现内置函数 @trunc。使用 un_node。
round实现内置函数 @round。使用 un_node。
tag_name实现内置函数 @tagName。使用 un_node。
type_name实现内置函数 @typeName。使用 un_node。
frame_type实现内置函数 @Frame。使用 un_node。
int_from_float实现内置函数 @intFromFloat。
使用载荷为 Bin 的 pl_node;lhs 是目标类型,rhs 是操作数。
float_from_int实现 @floatFromInt 内置函数。
使用 pl_node,负载为 Bin。lhs 是目标类型,rhs 是操作数。
ptr_from_int实现内置函数 @ptrFromInt。
使用载荷为 Bin 的 pl_node;lhs 是目标类型,rhs 是操作数。
enum_from_int将整数转换为枚举值。
使用 pl_node,载荷为 Bin。lhs 为目标类型,rhs 为操作数。
from_backing_int实现 @fromBackingInt 内置函数。
使用载荷为 Bin 的 pl_node。lhs 是目标类型,rhs 是操作数。
float_cast将较大的浮点类型转换为其他任意浮点类型,可能导致
精度损失。
使用 pl_node 字段。AST 为 @floatCast 语法。
载荷为 Bin,lhs 为目标类型,rhs 为操作数。
int_cast实现内置函数 @intCast。
使用 pl_node,载荷为 Bin。lhs 是目标类型,rhs 是操作数。
将整数值转换为另一整数类型,并断言目标类型
能够容纳相同数学值。
ptr_cast实现内置函数 @ptrCast。
使用 pl_node,载荷为 Bin。lhs 是目标类型,rhs 是操作数。
并非每个 @ptrCast 都对应此指令,另见
Extended 中的 ptr_cast_full。
truncate实现 @truncate 内置函数。
使用 pl_node,载荷为 Bin。lhs 为目标类型,rhs 为操作数。
has_decl实现 @hasDecl 内置函数。
使用 pl_node 联合体字段。载荷为 Bin。
has_field实现 @hasField 内置函数。
使用载荷为 Bin 的 pl_node 联合体字段。
clz实现 @clz 内置函数。使用 un_node 联合体字段。
ctz实现 @ctz 内置函数。使用 un_node 联合体字段。
pop_count实现 @popCount 内置函数。使用 un_node 联合体字段。
byte_swap实现 @byteSwap 内置函数。使用 un_node 联合体字段。
bit_reverse实现 @bitReverse 内置函数。使用 un_node 联合体字段。
bit_offset_of实现 @bitOffsetOf 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
offset_of实现 @offsetOf 内置函数。
使用带有 Bin 载荷的 pl_node 联合体字段。
splat实现 @splat 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
reduce实现 @reduce 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
shuffle实现 @shuffle 内置函数。
使用带有 Shuffle 载荷的 pl_node 联合体字段。
atomic_load实现内置函数 @atomicLoad。
使用 pl_node 联合体字段,载荷为 AtomicLoad。
atomic_rmw实现内置函数 @atomicRmw。
使用 pl_node 联合体字段,载荷为 AtomicRmw。
atomic_store实现 @atomicStore 内置函数。
使用 pl_node 联合体字段,载荷为 AtomicStore。
mul_add实现内置函数 @mulAdd。
使用 pl_node 联合体字段,载荷为 MulAdd。
加数传递此内置函数的类型。
两个乘数需强制转换为相同类型。
memcpy实现 @memcpy 内置函数。
使用带有 Bin 载荷的 pl_node 联合体字段。
memmove实现 @memmove 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
memset实现 @memset 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
min实现接受 2 个参数的 @min 内置函数。
使用 pl_node 联合体字段,载荷为 Bin。
max实现接受 2 个参数的 @max 内置函数。
使用 pl_node 联合体字段,载荷为 Bin
alloc分配栈局部内存。
使用 un_node 联合体字段,操作数为所分配对象的类型。
节点源码位置指向变量声明节点。
值存入该分配后,应使用 make_ptr_const 指令。为保证编译期值检测正常工作,在执行 make_ptr_const 指令之前,此指针的使用存在一些限制:不得从块返回由此 alloc 派生的任何指针,也不得将它们存入其他地址。换言之,必须能够直接判断任意给定指针是否由此指针派生。
alloc_mut与 alloc 相同,但结果可变。因此无需使用 make_ptr_const,
且指针的使用不受限制。
alloc_comptime_mut分配可在编译期修改的内存。
使用 un_node 联合体字段。操作数为所分配对象的类型。
节点源码位置指向 var 声明节点。
alloc_inferred与 alloc 相同,但会推断类型。
使用 node 联合体字段。
alloc_inferred_mut与 alloc_inferred 相同,但为可变分配。
alloc_inferred_comptime分配编译期常量内存。
使用 node 联合体字段。所分配对象的类型由推断得到。
节点源码位置指向变量声明节点。
alloc_inferred_comptime_mut与 alloc_comptime_mut 相同,但类型通过推断确定。
resolve_inferred_alloc每条 store_to_inferred_ptr 将所存值的类型放入集合,
随后 resolve_inferred_alloc 对该集合触发同级类型解析。
操作数是 alloc_inferred 或 alloc_inferred_mut 指令,
即需要推断类型的分配。
结果为最终解析出的指针。此指令之后不得再引用
alloc_inferred[_comptime][_mut] 指令。
使用 un_node 字段,AST 节点为变量声明。
make_ptr_const将来自 alloc 或 Extended.alloc 的指针转换为
同一指针的常量版本。对于推导分配,则改由
resolve_inferred_alloc 指令隐式处理。
使用 un_node 联合体字段。
@"resume"实现 resume 语法。使用 un_node 字段。
@"defer"defer 语句。
使用 defer 联合体字段。
save_err_ret_index若操作数为 .none,或具有错误/错误联合体类型,则请求 Sema
更新外围块所保存的错误返回轨迹索引。
使用 save_err_ret_index 字段。
restore_err_ret_index_unconditionalExtended.restore_err_ret_index 的专用形式。
无条件将错误返回索引恢复为
operand 引用块中最后保存的状态。如果 operand 为 none,则恢复
到函数入口处。
使用 un_node 字段。
restore_err_ret_index_fn_entryExtended.restore_err_ret_index 的特化形式。
在 operand 为非错误值的条件下,
将错误返回索引恢复到当前函数入口处的状态。
若 operand 为 none,则无条件恢复。
使用 un_node 字段。
extendedZIR 指令标签属于 Extended 中的一种。
使用 extended 联合体字段。
std.zig.Zir.Inst.Tag.data_tags
常量
由调试安全检查代码使用。
std.zig.Zir.Inst.Tag.isAlwaysVoid
函数
AstGen 通过此项确定是否应使用 Ref.void_value
代替给定指令的结果。这样,Sema 在分析后
就无需将该指令加入映射。
pub fn isAlwaysVoid(tag: Tag, data: Data) bool
参数
std.zig.Zir.Inst.Tag.isNoReturn
函数
返回该指令是否属于控制流中的“noreturn”类型。 函数调用不计入其中。
pub fn isNoReturn(tag: Tag) bool
参数
tag: Tag
std.zig.Zir.Inst.Try
容器
此数据存储在 extra 中,后接:
- body: Index // 对每个
body_len
- body: Index // 对每个
字段
std.zig.Zir.Inst.TupleDecl
容器
尾随:
- fields: { // 每个
fields_len(存储在extended.small中) type: Inst.Ref, init: Inst.Ref, // 非comptime字段为.none}
字段
std.zig.Zir.Inst.TypeOfPeer
容器
指令主体位于 extra[body_index..][0..body_len]。
尾随:
- operand: Ref // 每个
operands_len对应一个
字段
std.zig.Zir.Inst.UnionDecl
容器
尾随数据:
- captures_len: u32 // 若
has_captures_len - decls_len: u32, // 若
has_decls_len - fields_len: u32, // 若
has_fields_len - arg_type_body_len: u32, // 若
kind.hasArgType() - capture: Capture // 每个
captures_len对应一项 - capture_name: NullTerminatedString // 每个
captures_len对应一项 - decl: Index, // 每个
decls_len对应一项;指向declaration指令 - field_name: NullTerminatedString // 每个
fields_len对应一项 - field_type_body_len: u32 // 每个
fields_len对应一项 9 . field_align_body_len: u32 // 每个fields_len对应一项,若any_field_aligns - field_value_body_len: u32 // 每个
fields_len对应一项,若any_field_values - arg_type_body_inst: Inst.Index // 每个
arg_type_body_len对应一项 - body_inst: Inst.Index // 对每个字段,依次为类型主体、对齐主体和值主体
字段
std.zig.Zir.Inst.UnionDecl.Kind
容器
字段
autounion
tagged_explicitunion(T)
tagged_enumunion(enum)
tagged_enum_explicitunion(enum(T))
@"extern"extern union
@"packed"packed union
packed_explicitpacked union(T)
std.zig.Zir.Inst.UnionDecl.Kind.hasArgType
函数
pub fn hasArgType(k: Kind) bool
参数
k: Kind
std.zig.Zir.Inst.UnionDecl.Kind.layout
函数
pub fn layout(k: Kind) std.lang.Type.ContainerLayout
参数
k: Kind
std.zig.Zir.Inst.UnionDecl.Small
容器
字段
has_captures_len: bool
has_decls_len: bool
has_fields_len: bool
name_strategy: NameStrategy
kind: Kind
any_field_aligns: bool
any_field_values: bool
_: u6 = 0
std.zig.Zir.Inst.UnionInit
容器
字段
std.zig.Zir.Inst.UnNode
容器
字段
std.zig.Zir.Inst.ValidateDestructure
容器
字段
std.zig.Zir.Io
别名
别名目标 std.Io
std.zig.Zir.loweringFailed
函数
pub fn loweringFailed(code: Zir) bool
参数
code: Zir
std.zig.Zir.mem
别名
别名目标 std.mem
std.zig.Zir.nullTerminatedString
函数
给定 string_bytes 中的索引,返回其中找到的空终止字符串。
pub fn nullTerminatedString(code: Zir, index: NullTerminatedString) [:0]const u8
参数
code: Zir
index: NullTerminatedString
std.zig.Zir.NullTerminatedString
容器
字段
empty = 0
_
std.zig.Zir.refSlice
函数
pub fn refSlice(code: Zir, start: usize, len: usize) []Inst.Ref
参数
std.zig.Zir.std
别名
别名目标 std
std.zig.Zir.typeDecls
函数
pub fn typeDecls(zir: Zir, type_decl: Inst.Index) []const Zir.Inst.Index
参数
std.zig.Zir.UnwrappedEnumDecl
容器
字段
src_line: u32
src_column: u32
src_node: Ast.Node.Index
name_strategy: Inst.NameStrategy
arg_baseline_src_node: Ast.Node.Index
fields_baseline_src_node: Ast.Node.Index
captures: []const Inst.Capture
capture_names: []const NullTerminatedString
decls: []const Inst.Index
tag_type_body: ?[]const Inst.Index
nonexhaustive: bool
field_names: []const NullTerminatedString
field_value_body_lens: ?[]const u32
field_bodies_overlong: []const Inst.Index
std.zig.Zir.UnwrappedEnumDecl.FieldIterator
容器
字段
next_idx: u32
names: []const NullTerminatedString
value_body_lens: ?[]const u32
bodies_overlong: []const Inst.Index
std.zig.Zir.UnwrappedEnumDecl.FieldIterator.body
函数
fn body(it: *FieldIterator, len: u32) ?[]const Inst.Index
参数
it: *FieldIterator
len: u32
std.zig.Zir.UnwrappedEnumDecl.FieldIterator.Field
容器
字段
std.zig.Zir.UnwrappedEnumDecl.FieldIterator.next
函数
pub fn next(it: *FieldIterator) ?Field
参数
it: *FieldIterator
std.zig.Zir.UnwrappedEnumDecl.iterateFields
函数
pub fn iterateFields(enum_decl: UnwrappedEnumDecl) FieldIterator
参数
enum_decl: UnwrappedEnumDecl
std.zig.Zir.UnwrappedOpaqueDecl
容器
字段
src_line: u32
src_column: u32
src_node: Ast.Node.Index
name_strategy: Inst.NameStrategy
captures: []const Inst.Capture
capture_names: []const NullTerminatedString
decls: []const Inst.Index
std.zig.Zir.UnwrappedStructDecl
容器
字段
src_line: u32
src_column: u32
src_node: Ast.Node.Index
name_strategy: Inst.NameStrategy
arg_baseline_src_node: Ast.Node.Index
fields_baseline_src_node: Ast.Node.Index
captures: []const Inst.Capture
capture_names: []const NullTerminatedString
decls: []const Inst.Index
layout: std.lang.Type.ContainerLayout
backing_int_type_body: ?[]const Inst.Index
field_names: []const NullTerminatedString
field_type_body_lens: []const u32
field_align_body_lens: ?[]const u32
field_default_body_lens: ?[]const u32
field_comptime_bits: ?[]const u32
field_bodies_overlong: []const Inst.Index
std.zig.Zir.UnwrappedStructDecl.FieldIterator
容器
字段
next_idx: u32
names: []const NullTerminatedString
type_body_lens: []const u32
align_body_lens: ?[]const u32
default_body_lens: ?[]const u32
comptime_bits: ?[]const u32
bodies_overlong: []const Inst.Index
std.zig.Zir.UnwrappedStructDecl.FieldIterator.body
函数
fn body(it: *FieldIterator, len: u32) ?[]const Inst.Index
参数
it: *FieldIterator
len: u32
std.zig.Zir.UnwrappedStructDecl.FieldIterator.Field
容器
字段
std.zig.Zir.UnwrappedStructDecl.FieldIterator.next
函数
pub fn next(it: *FieldIterator) ?Field
参数
it: *FieldIterator
std.zig.Zir.UnwrappedStructDecl.iterateFields
函数
pub fn iterateFields(struct_decl: UnwrappedStructDecl) FieldIterator
参数
struct_decl: UnwrappedStructDecl
std.zig.Zir.UnwrappedSwitchBlock
容器
尾随内容,从 end 开始:
- case_bodies: { // Case.Iterator.next() 中的每个 case 对应一个 prong_body: { body_inst: Inst.Index, // 每个 case.prong_info.body_len 对应一个 } item_body: { // case.item_infos 中的每个 body_len 对应一个 body_inst: Inst.Index, // 每个 body_len 对应一个 } range_bodies: { // case.range_infos 中的每个 .{first_info, last_info} 对应一个 first_body_inst: Inst.Index, // 每个 first_info.body_len 对应一个 last_body_inst: Inst.Index, // 每个 last_info.body_len 对应一个 } }
字段
main_operand: Inst.Refcatch/if 或 switch 操作数。
switch_src_node_offset: Ast.Node.Offset
catch_or_if_src_node_offset: Ast.Node.OptionalOffset
payload_capture_placeholder: Inst.OptionalIndex
tag_capture_placeholder: Inst.OptionalIndex
has_continue: bool
any_maybe_runtime_capture: bool
non_err_case: ?Case.NonErr
else_case: ?Case.Else
has_under: bool
prong_infos: []const Inst.SwitchBlock.ProngInfo
multi_case_items_lens: []const u32
multi_case_ranges_lens: ?[]const u32
item_infos: []const Inst.SwitchBlock.ItemInfo
end: usize
std.zig.Zir.UnwrappedSwitchBlock.anyRanges
函数
pub fn anyRanges(unwrapped: *const UnwrappedSwitchBlock) bool
参数
unwrapped: *const UnwrappedSwitchBlock
std.zig.Zir.UnwrappedSwitchBlock.Case
容器
字段
index: Case.Index
prong_info: Inst.SwitchBlock.ProngInfo
item_infos: []const Inst.SwitchBlock.ItemInfo
range_infos: []const [2]Inst.SwitchBlock.ItemInfo
std.zig.Zir.UnwrappedSwitchBlock.Case.Else
容器
字段
std.zig.Zir.UnwrappedSwitchBlock.Case.Index
容器
字段
kind: enum(u1) { scalar, multi }
value: u31
std.zig.Zir.UnwrappedSwitchBlock.Case.Index.@"else"
常量
Case.Index
std.zig.Zir.UnwrappedSwitchBlock.Case.Iterator
容器
字段
next_idx: u32
prong_infos: []const Inst.SwitchBlock.ProngInfo
multi_case_items_lens: []const u32
multi_case_ranges_lens: ?[]const u32
item_infos: []const Inst.SwitchBlock.ItemInfo
std.zig.Zir.UnwrappedSwitchBlock.Case.Iterator.itemInfos
函数
fn itemInfos(it: *Iterator, count: u32) []const Inst.SwitchBlock.ItemInfo
参数
it: *Iterator
count: u32
std.zig.Zir.UnwrappedSwitchBlock.Case.Iterator.next
函数
pub fn next(it: *Iterator) ?Case
参数
it: *Iterator
std.zig.Zir.UnwrappedSwitchBlock.Case.NonErr
容器
字段
std.zig.Zir.UnwrappedSwitchBlock.iterateCases
函数
pub fn iterateCases(unwrapped: UnwrappedSwitchBlock) Case.Iterator
参数
unwrapped: UnwrappedSwitchBlock
std.zig.Zir.UnwrappedSwitchBlock.multiCasesLen
函数
pub fn multiCasesLen(unwrapped: *const UnwrappedSwitchBlock) u32
参数
unwrapped: *const UnwrappedSwitchBlock
std.zig.Zir.UnwrappedSwitchBlock.scalarCasesLen
函数
pub fn scalarCasesLen(unwrapped: *const UnwrappedSwitchBlock) u32
参数
unwrapped: *const UnwrappedSwitchBlock
std.zig.Zir.UnwrappedSwitchBlock.totalItemsLen
函数
pub fn totalItemsLen(unwrapped: *const UnwrappedSwitchBlock) u32
参数
unwrapped: *const UnwrappedSwitchBlock
std.zig.Zir.UnwrappedUnionDecl
容器
字段
src_line: u32
src_column: u32
src_node: Ast.Node.Index
name_strategy: Inst.NameStrategy
arg_baseline_src_node: Ast.Node.Index
fields_baseline_src_node: Ast.Node.Index
captures: []const Inst.Capture
capture_names: []const NullTerminatedString
decls: []const Inst.Index
kind: Inst.UnionDecl.Kind
arg_type_body: ?[]const Inst.Index
field_names: []const NullTerminatedString
field_type_body_lens: []const u32
field_align_body_lens: ?[]const u32
field_value_body_lens: ?[]const u32
field_bodies_overlong: []const Inst.Index
std.zig.Zir.UnwrappedUnionDecl.FieldIterator
容器
字段
next_idx: u32
names: []const NullTerminatedString
type_body_lens: []const u32
align_body_lens: ?[]const u32
value_body_lens: ?[]const u32
bodies_overlong: []const Inst.Index
std.zig.Zir.UnwrappedUnionDecl.FieldIterator.body
函数
fn body(it: *FieldIterator, len: u32) ?[]const Inst.Index
参数
it: *FieldIterator
len: u32
std.zig.Zir.UnwrappedUnionDecl.FieldIterator.Field
容器
字段
std.zig.Zir.UnwrappedUnionDecl.FieldIterator.next
函数
pub fn next(it: *FieldIterator) ?Field
参数
it: *FieldIterator
std.zig.Zir.UnwrappedUnionDecl.iterateFields
函数
pub fn iterateFields(union_decl: UnwrappedUnionDecl) FieldIterator
参数
union_decl: UnwrappedUnionDecl
std.zig.Zir.Zir
别名
别名目标 std.zig.Zir
源码
std/zig/Zir.zig
//! Zig 中间表示。
//!
//! Astgen.zig 将 AST 节点转换成这些无类型的中间表示指令,随后 Sema.zig 将它们处理为 AIR。
//! 包含表示 ZIR 指令列表所需的最少信息。此结构完成后,无需再访问 AST 树的词法单元列表、节点列表或源码字节,就能生成 AIR,进而生成机器码。例外包括:
//! * 编译错误:可能需要访问上述数据结构,以生成有用的诊断报告。
//! * 将来可能包括内联汇编:它需要由代码生成后端解析和处理,并在那里报告错误。不过,目前内联汇编并非例外。
//!
//!
//!
//!
//!
//!
//!
const Zir = @This();
const builtin = @import("builtin");
const std = @import("std");
const Io = std.Io;
const mem = std.mem;
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const BigIntConst = std.math.big.int.Const;
const BigIntMutable = std.math.big.int.Mutable;
const Ast = std.zig.Ast;
instructions: std.MultiArrayList(Inst).Slice,
/// 为用更少字节存储字符串引用,将所有字符串字节复制到此处。字符串字节可以为零。引用此处数据的使用者可自行决定:同时存储索引与长度以支持空字节,或者仅存储索引并使用零终止形式。
/// `string_bytes` 数组不限定其中任何一种用法。
/// 索引 0 为特殊情况保留。
///
///
///
string_bytes: []u8,
/// 此数据的含义由 `Inst.Tag` 值决定。
/// 最前面的几个索引保留。具体值参见 `ExtraIndex`。
extra: []u32,
/// 将 ZIR 存入文件时,位于字节偏移 0 的数据。
pub const Header = extern struct {
instructions_len: u32,
string_bytes_len: u32,
extra_len: u32,
/// 这里可以保留为填充区,但这会触发 Valgrind 警告,因为
/// 我们会向文件系统读写未定义的字节。这虽无害,但
/// 在此使用零值字段几乎没有代价,还可消除警告,
/// 从而使后续 Valgrind 警告更有可能得到认真处理。
unused: u32 = 0,
stat_inode: Io.File.INode,
stat_size: u64,
stat_mtime: i128,
};
pub const ExtraIndex = enum(u32) {
/// 若为 0,则没有编译错误。
/// 否则,此索引处存在 `CompileErrors` 载荷。
compile_errors,
/// 若为 0,此文件没有导入。否则,此索引处有一个 `Imports`
/// 载荷。
imports,
_,
};
fn ExtraData(comptime T: type) type {
return struct { data: T, end: usize };
}
/// 返回请求的数据,以及位于该对象尾部数据起点的
/// 新索引。
pub fn extraData(code: Zir, comptime T: type, index: usize) ExtraData(T) {
const info = @typeInfo(T).@"struct";
var i: usize = index;
var result: T = undefined;
inline for (info.field_names, info.field_types) |field_name, field_type| {
@field(result, field_name) = switch (field_type) {
u32 => code.extra[i],
Inst.Ref,
Inst.Index,
Inst.Declaration.Name,
std.zig.SimpleComptimeReason,
NullTerminatedString,
// Ast.TokenIndex 因为是 u32 而被省略。
Ast.OptionalTokenIndex,
Ast.Node.Index,
Ast.Node.OptionalIndex,
=> @fromBackingInt(@intCast(code.extra[i])),
Ast.TokenOffset,
Ast.OptionalTokenOffset,
Ast.Node.Offset,
Ast.Node.OptionalOffset,
=> @fromBackingInt(@intCast(@as(i32, @bitCast(code.extra[i])))),
Inst.Call.Flags,
Inst.BuiltinCall.Flags,
Inst.SwitchBlock.Bits,
Inst.FuncFancy.Bits,
Inst.Declaration.Flags,
Inst.Param.Type,
Inst.Func.RetTy,
=> @bitCast(code.extra[i]),
else => @compileError("bad field type"),
};
i += 1;
}
return .{
.data = result,
.end = i,
};
}
pub const NullTerminatedString = enum(u32) {
empty = 0,
_,
};
/// 给定 `string_bytes` 中的索引,返回其中找到的空终止字符串。
pub fn nullTerminatedString(code: Zir, index: NullTerminatedString) [:0]const u8 {
const slice = code.string_bytes[@backingInt(index)..];
return slice[0..std.mem.findScalar(u8, slice, 0).? :0];
}
pub fn refSlice(code: Zir, start: usize, len: usize) []Inst.Ref {
return @ptrCast(code.extra[start..][0..len]);
}
pub fn bodySlice(zir: Zir, start: usize, len: usize) []Inst.Index {
return @ptrCast(zir.extra[start..][0..len]);
}
pub fn hasCompileErrors(code: Zir) bool {
if (code.extra[@backingInt(ExtraIndex.compile_errors)] != 0) {
return true;
} else {
assert(code.instructions.len != 0); // 即降低过程未失败
return false;
}
}
pub fn loweringFailed(code: Zir) bool {
if (code.instructions.len == 0) {
assert(code.hasCompileErrors());
return true;
} else {
return false;
}
}
pub fn deinit(code: *Zir, gpa: Allocator) void {
code.instructions.deinit(gpa);
gpa.free(code.string_bytes);
gpa.free(code.extra);
code.* = undefined;
}
/// 这些是由抽象语法树生成的无类型指令。
/// 此处的数据不可变,因为同一份 ZIR
/// 可能同时被多个分析过程使用。
pub const Inst = struct {
tag: Tag,
data: Data,
/// 这些名称直接作为文本格式中的指令名使用。
/// 每个 `Tag` 使用哪些 `Data` 字段,请参见 `data_field_map`。
pub const Tag = enum(u8) {
/// 算术加法,断言不会发生整数溢出。
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
add,
/// 二进制补码回绕整数加法。
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
addwrap,
/// 饱和加法。
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
add_sat,
/// 与 `add` 相同,但不进行安全检查。
add_unsafe,
/// 算术减法。断言整数不会溢出。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
sub,
/// 采用二进制补码回绕的整数减法。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
subwrap,
/// 饱和减法。
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
sub_sat,
/// 算术乘法。断言整数不会溢出。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
mul,
/// 采用二进制补码回绕的整数乘法。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
mulwrap,
/// 饱和乘法。
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
mul_sat,
/// 实现 `@divExact` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
div_exact,
/// 实现 `@divFloor` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
div_floor,
/// 实现 `@divCeil` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
div_ceil,
/// 实现 `@divTrunc` 内置函数。
/// 使用带有 `Bin` 载荷的 `pl_node` 联合体字段。
div_trunc,
/// 实现 `@mod` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
mod,
/// 实现 `@rem` 内置函数。
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
rem,
/// 含义可能为取余或取模。如果两种运算
/// 可能产生不同值,则发出
/// 编译错误;否则执行计算。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
mod_rem,
/// 整数左移。从右侧移入零。
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
shl,
/// 实现 `@shlExact` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
shl_exact,
/// 饱和左移。
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
shl_sat,
/// 整数右移。根据整数类型是否有符号,执行算术右移或逻辑右移。
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
///
shr,
/// 实现 `@shrExact` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
shr_exact,
/// 声明当前函数的参数,用于:
/// * 调试信息。
/// * 检查是否遮蔽当前命名空间中的声明。
/// * 使参数类型表达式能引用其他参数。
/// 这些指令出现在函数主体之外的块中,与包含 func 指令的是同一个块。
/// 使用 `pl_tok` 字段。词法单元为参数名,载荷为 `Param`。
///
param,
/// 与 `param` 相同,但参数被标记为 comptime。
param_comptime,
/// 与 `param` 相同,但参数被标记为 anytype。
/// 使用 `str_tok` 字段。token 是参数名称,字符串也是参数名称。
param_anytype,
/// 与 `param` 相同,但参数同时标记为 comptime 和 anytype。
/// 使用 `str_tok` 字段。词法标记是参数名。字符串也是参数名。
param_anytype_comptime,
/// 数组拼接。`a ++ b`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
array_cat,
/// `[N]T` 语法。不提供源码位置。
/// 使用 `pl_node` 联合体字段。负载为 `Bin`。lhs 为长度,rhs 为元素类型。
array_type,
/// `[N:S]T` 语法。源码位置为数组类型表达式节点。
/// 使用 `pl_node` 联合体字段。载荷为 `ArrayTypeSentinel`。
array_type_sentinel,
/// `@Int` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
/// lhs 为符号性,rhs 为位数。
reify_int,
/// `@Vector` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
/// lhs 为长度,rhs 为元素类型。
vector_type,
/// 给定指针类型,返回其元素类型。穿透包装该指针的任意可选类型或错误
/// 联合体类型。断言底层类型为指针类型。
/// 如果元素类型为 `anyopaque`,则返回泛型 poison。
/// 使用 `un_node` 字段。
elem_type,
/// 给定可索引指针,如切片、多项指针或指向数组的单项指针,返回其
/// 元素类型。若类型不是可索引指针,则产生编译错误。
/// 使用 `un_node` 字段。
indexable_ptr_elem_type,
/// 给定向量或数组类型,去掉其外层的所有错误联合体或
/// 可选类型,并返回元素类型。
///
/// `!?[N]T` -> `T`
///
/// 使用 `un_node` 字段。
splat_op_result_ty,
/// 给定一个类型,移除其外层叠加的所有错误联合体
/// 或可选类型,验证其是否可用于 `@fromBackingInt`,
/// 并返回其底层整数类型。
///
/// `E!?enum(T) { _ }` -> `T`
///
/// 使用 `un_node` 字段。
from_backing_int_arg_ty,
/// 给定指向可索引对象的指针,返回其 len 属性。此指令
/// 供 for 循环使用。如果对象不可索引,此指令还会生成 for 循环专用的编译
/// 错误。
/// 使用 `un_node` 字段。AST 节点为 for 循环节点。
indexable_ptr_len,
/// 创建 `anyframe->T` 类型。
/// 使用 `un_node` 字段。
anyframe_type,
/// 强制转换为函数返回类型。
/// 使用 `pl_node` 字段。负载为 `As`。AST 节点可能有多种形式。
as_node,
/// 与 `as_node` 相同,但忽略运行期整数转编译期整数的错误。
as_shift_operand,
/// 按位与。`&`
bit_and,
/// 将值的内存表示重新解释为另一种类型。
/// 使用 pl_node 字段,载荷为 `Bin`。
bitcast,
/// 按位非。`~`
/// 使用 `un_node`。
bit_not,
/// 按位或。`|`
bit_or,
/// 可返回值的带标签代码块。
/// 使用 `pl_node` 联合体字段。载荷为 `Block`。
block,
/// 类似 `block`,但强制在编译期完整求值其内容。
/// 通过 `break_inline` 退出。
/// 使用 `pl_node` 联合体字段。载荷为 `BlockComptime`。
block_comptime,
/// 一组在父上下文中分析、而不生成运行时块的指令。
/// 必须以 noreturn 指令的“inline”变体结束。
/// 使用 `pl_node` 联合体字段。载荷为 `Block`。
///
block_inline,
/// 此指令只能出现在命名空间类型的声明列表中,例如 `struct_decl` 指令内部。
/// 它表示单个源码声明,即 `const`、`var` 或 `fn`,包含声明的名称、属性、类型和值。
/// 使用 `declaration` 联合体字段,载荷为 `Declaration`。
///
///
declaration,
/// 实现 `suspend {...}`。
/// 使用 `pl_node` 联合体字段。载荷为 `Block`。
suspend_block,
/// 布尔非。另见 `bit_not`。
/// 使用 `un_node` 字段。
bool_not,
/// 短路布尔 `and`。`lhs` 为布尔类型的 `Ref`,另一操作数
/// 为块,仅当 `lhs` 为 `true` 时求值。
/// 使用 `pl_node` 联合体字段。载荷为 `BoolBr`。
bool_br_and,
/// 短路布尔 `or`。`lhs` 是布尔 `Ref`;另一个操作数
/// 为块,仅当 `lhs` 为 `false` 时求值。
/// 使用 `pl_node` 联合体字段。负载为 `BoolBr`。
bool_br_or,
/// 从块返回值。
/// 使用 `break` 联合体字段。
/// 使用前一条指令的源码信息。
@"break",
/// 从块中返回值。此指令用作 `block_inline` 的
/// 终结指令,使得可以使用 `Sema.analyzeBody` 的返回值。
/// 当已知块中只有一个 break 指令,且目标块是父块时,
/// 也可以使用此指令。
/// 使用 `break` 联合体字段。
break_inline,
/// 从 switch 分支内部跳转到操作数指定的分支。
/// 使用 `break` 联合体字段。`block_inst` 指向 `switch_block`/
/// `switch_block_ref`/`switch_block_err_union`。
switch_continue,
/// 检查编译期控制流未发生在运行时块内部。
/// 使用 `un_node` 联合体字段。
check_comptime_control_flow,
/// 函数调用。
/// 使用 `pl_node` 联合体字段,载荷为 `Call`。
/// AST 节点即为函数调用。
call,
/// 使用 `a.b()` 语法进行函数调用。
/// 使用具名字段作为被调用方。若不存在该字段,则在类型中搜索与字段名匹配的声明。解析该声明,并确认它是能够接受该对象作为第一个参数的函数,允许一次指针修正。随后以该函数作为被调用方,对象作为隐式的第一个参数。
/// 使用 `pl_node` 联合体字段,载荷为 `FieldCall`。
/// AST 节点为函数调用。
///
///
///
field_call,
/// 实现 `@call` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `BuiltinCall`。
/// AST 节点为内置函数调用。
builtin_call,
/// `<`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
cmp_lt,
/// `<=`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
cmp_lte,
/// `==`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
cmp_eq,
/// `>=`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
cmp_gte,
/// `>`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
cmp_gt,
/// `!=`
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
cmp_neq,
/// 条件分支。根据布尔条件值拆分控制流。
/// 使用 `pl_node` 联合体字段。AST 节点为 if、while、for 等。
/// 载荷为 `CondBr`。
condbr,
/// 与 `condbr` 相同,但条件被强制转换为编译时值,
/// 且仅分析实际选择的分支。then 块及 else 块必须
/// 以 noreturn 指令的“inline”变体终止。
condbr_inline,
/// 给定错误联合体操作数,拆分控制流。
/// 发生错误时,控制流进入此指令包含的
/// 块,该块保证以返回指令结束,
/// 绝不会跳出此块。
/// 没有错误时,控制流继续执行 `try` 后的下一条指令,
/// 此指令的结果是解包后的载荷值,
/// 如同对操作数执行了 `err_union_payload_unsafe`。
/// 使用 `pl_node` 联合体字段,载荷为 `Try`。
@"try",
/// 与 `try` 相同,但操作数和结果均为指针。
try_ptr,
/// 错误集合类型定义。包含字段名称列表。
/// 使用载荷为 `ErrorSetDecl` 的 `pl_node` 联合体字段。
error_set_decl,
/// 声明语句的开始,用于调试信息。
/// 使用 `dbg_stmt` 联合体字段。行和列相对于父声明偏移。
///
dbg_stmt,
/// 标记变量声明。用于调试信息。
/// 使用 `str_op` 联合体字段。字符串为局部变量名,
/// 操作数为指向变量位置的指针。
/// 该局部变量可以是 const 或 var。
dbg_var_ptr,
/// 与 `dbg_var_ptr` 相同,但局部变量始终为 const,操作数
/// 为该局部变量的值。
dbg_var_val,
/// 按名称标识 Decl,并获取指向它的指针。
///
/// 使用 `str_tok` 联合体字段。
decl_ref,
/// 使用名称标识 Decl,并将其作为值使用。
/// 使用 `str_tok` 联合体字段。
decl_val,
/// 从指针加载值。假定采用 `x.*` 语法。
/// 使用 `un_node` 字段。AST 节点为 `x.*` 语法。
load,
/// 算术除法。断言无整数溢出。
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
div,
/// 给定指向数组、切片或指针的指针,返回指向
/// 指定索引处元素的指针。
/// 使用 `pl_node` 联合体字段。AST 节点是 a[b] 语法。负载为 `Bin`。
elem_ptr_node,
/// 与 `elem_ptr_node` 相同,但仅用于 for 循环。
/// 使用 `pl_node` 联合体字段。AST 节点为 for 循环条件。
/// 载荷为 `Bin`。
/// 不生成越界安全检查。
elem_ptr,
/// 给定指向数组、切片或指针的指针,加载
/// 指定索引处的元素。
///
/// 使用 `pl_node` 联合体字段。AST 节点为 a[b] 语法。载荷为 `Bin`。
elem_ptr_load,
/// 给定数组、切片或指针,返回
/// 指定索引处的元素。
///
/// 使用 `pl_node` 联合体字段。AST 节点为 for 循环的条件。
/// 载荷为 `Bin`。
///
/// 不生成越界安全检查。
elem_val,
/// 与 `elem_val` 相同,但将索引作为立即数传入。
/// 不生成越界安全检查。先前指令必须验证此操作。
/// 使用 `elem_val_imm` 联合体字段。
elem_val_imm,
/// 若操作数不是 `void`,则产生编译错误。
/// 使用 `un_node` 字段。
ensure_result_used,
/// 若忽略错误,则产生编译错误。
/// 使用 `un_node` 字段。
ensure_result_non_error,
/// 错误联合的载荷不为 void 时,生成编译错误。
ensure_err_union_payload_void,
/// 创建 `E!T` 类型。
/// 使用载荷为 `Bin` 的 `pl_node` 字段。
error_union_type,
/// `error.Foo` 语法。使用 Data 联合体的 `str_tok` 字段。
error_value,
/// 实现 `@export` 内置函数。
/// 使用 `pl_node` 联合体字段。载荷为 `Export`。
@"export",
/// 给定指向结构体或含虚拟字段对象的指针,返回
/// 指向指定名称字段的指针。字段名存储在 string_bytes 中,用于 a.b 语法。
/// 使用 `pl_node` 字段,AST 节点为 a.b 语法,载荷为 Field。
field_ptr,
/// 给定指向结构体或含虚拟字段对象的指针,从指定名称的字段加载。
///
/// 字段名存储在 string_bytes 中,由 a.b 语法使用。
///
/// 此指令也接受指针。
///
/// 使用 `pl_node` 字段。AST 节点为 a.b 语法,载荷为 Field。
///
field_ptr_load,
/// 给定包含虚拟字段的结构体或对象指针,返回指向指定字段的指针。
/// 字段名是编译时指令。供 @field 使用。
/// 使用 `pl_node` 字段。AST 节点为内置函数调用。载荷为 FieldNamed。
field_ptr_named,
/// 给定指向包含虚拟字段的结构体或对象的指针,
/// 从指定名称的字段加载。
///
/// 字段名称为编译期指令。由 @field 使用。
///
/// 使用 `pl_node` 字段。AST 节点是内置调用。负载为 FieldNamed。
field_ptr_named_load,
/// 根据 body_len 是否为 0,返回函数类型或函数实例。
/// 调用约定为 auto。
/// 使用 `pl_node` 联合体字段。`payload_index` 指向 `Func`。
func,
/// 与 `func` 相同,但使用推断的错误集合。
func_inferred,
/// 表示函数声明或函数原型,取决于
/// body_len 是否为 0。
/// 使用 `pl_node` 联合体字段。`payload_index` 指向 `FuncFancy`。
func_fancy,
/// 实现 `@import` 内置函数。
/// 使用 `pl_tok` 字段。
import,
/// 可由 u64 表示的整数字面量。使用 `int` 联合体字段。
int,
/// 任意大小的整数字面量。使用 `str` 联合体字段。
int_big,
/// 可容纳于 f64 的浮点字面量。使用 float 联合体值。
float,
/// 可以用 f128 表示的浮点数字面量。使用 `pl_node` 联合体值。
/// 载荷为 `Float128`。
float128,
/// 由符号性和位数构造整数类型。
/// 载荷为 `int_type`
int_type,
/// 可选值为 null 时返回布尔值 false。`x != null`
/// 使用 `un_node` 字段。
is_non_null,
/// 如果可选值为 null,则返回布尔值 false。`x.* != null`
/// 使用 `un_node` 字段。
is_non_null_ptr,
/// 值为错误时返回布尔值 false
/// 使用 `un_node` 字段。
is_non_err,
/// 若解引用的指针是错误,则返回布尔值 false。
/// 使用 `un_node` 字段。
is_non_err_ptr,
/// 与 `is_non_er` 相同,但不验证该类型是否可以是错误。
/// 使用 `un_node` 字段。
ret_is_non_err,
/// 永久循环的带标签代码块。主体末尾会有 `repeat` 或 `repeat_inline` 指令。
/// 使用 `pl_node` 字段,AST 节点为 for 或 while 循环。
/// 需要此 ZIR 指令,是因为 AIR 尚未与 ZIR 一致,Sema 需要为它生成多个 AIR 块。
/// 载荷为 `Block`。
///
///
loop,
/// 将运行时控制流送回当前块的开头。
/// 使用 `node` 字段。
repeat,
/// 将编译期控制流转回当前块的起始位置。
/// 使用 `node` 字段。
repeat_inline,
/// 断言提供的全部长度一致。用于构建 for 循环。
/// 返回值为 usize 类型的长度。
/// 使用 `pl_node` 字段,其载荷为 `MultiOp`。
/// for 循环条件中的每个 AST 节点对应两个项目。
/// 若一对中的两个项目均为 `.none`,该节点表示无界范围。
/// 若只有第二个项目为 `.none`,第一个项目表示可索引对象。
/// 否则,节点表示有界范围 `a..b`,两个项目分别为 `a` 和 `b`。
/// 非法行为:
/// * 全部长度均为无界范围,始终产生编译错误。
/// * 任意两个长度不一致。
for_len,
/// 将两个错误集合合并为一个:`E1 || E2`。
/// 使用载荷为 `Bin` 的 `pl_node` 字段。
merge_error_sets,
/// 将右值转换为常量左值,即取一个值,
/// 将其存入内存位置,并返回指向它的常量单元素指针。
/// 若值为 `comptime`,内存位置为全局静态常量数据。
/// 否则,内存位置位于栈帧中,局部于
/// 包含该指令的作用域。
/// 使用 `un_tok` 联合体字段。
ref,
/// 实现解引用运算符(`.*`)。检查操作数是否为
/// 支持直接解引用的指针。
/// 使用 `un_node` 联合体字段。
deref,
/// 对结果位置为引用的解引用操作 `.*` 生成此指令。
/// 检查操作数是支持直接解引用的指针,
/// 并返回指向解引用内存位置的单项指针。
/// 使用 `un_node` 联合体字段。
ref_deref,
/// 将控制流送回函数的被调用方(原文如此)。
/// 包含作为返回值的操作数。
/// 包含 AST 节点的源码位置。
/// 使用 `un_node` 联合体字段。
ret_node,
/// 将控制流返回给函数的被调用方。
/// 操作数为 `ret_ptr` 指令,返回值可在其指向的位置找到。
/// 包含 AST 节点源码位置。
/// 使用 `un_node` 联合体字段。
ret_load,
/// 将控制流送回函数的被调用方。
/// 包含作为返回值的操作数。
/// 包含标记源码位置。
/// 使用 `un_tok` 联合体字段。
ret_implicit,
/// 将控制流返回到函数的被调用方(原文如此)。
/// 返回操作数为 `error.foo`,其中 `foo` 由字符串指定。
/// 若当前函数具有推断错误集合,则将此名称指定的错误
/// 加入该集合。
/// 使用 `str_tok` 联合体字段。
ret_err_value,
/// 获取指向返回值的指针。
/// 使用 `node` 联合体字段。
ret_ptr,
/// 获取当前作用域内函数的返回类型。
/// 使用 `node` 联合体字段。
ret_type,
/// 创建可具有哨兵、对齐、地址空间和/或位范围的指针类型。
/// 使用 `ptr_type` 联合体字段。
ptr_type,
/// 切片操作 `lhs[rhs..]`。没有哨兵,也没有结束偏移量。
/// 返回指向子切片的指针。
/// 使用 `pl_node` 字段。AST 节点为切片语法。载荷为 `SliceStart`。
slice_start,
/// 切片操作 `array_ptr[start..end]`。没有哨兵。
/// 返回指向子切片的指针。
/// 使用 `pl_node` 字段。AST 节点为切片语法。载荷为 `SliceEnd`。
slice_end,
/// 切片操作 `array_ptr[start..end:sentinel]`。
/// 返回指向子切片的指针。
/// 使用 `pl_node` 字段。AST 节点是切片语法。负载为 `SliceSentinel`。
slice_sentinel,
/// 切片操作 `array_ptr[start..][0..len]`。可选哨兵。
/// 返回指向子切片的指针。
/// 使用 `pl_node` 字段。AST 节点为切片语法。载荷为 `SliceLength`。
slice_length,
/// 给定一个指向切片操作左侧表达式的指针值,返回哨兵类型,
/// 用作切片哨兵的结果类型,即 `lhs[a..b :s]` 中的 `s`。
/// 使用 `un_node` 字段。AST 节点为切片语法。操作数为 `lhs`。
slice_sentinel_ty,
/// 与 `store` 相同,但提供源码位置。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
store_node,
/// 与 `store_node` 相同,但将使用待存储值的类型,
/// 推断 `alloc_inferred` 的指针类型。
/// 使用 `pl_node` 联合体字段。负载为 `Bin`。
store_to_inferred_ptr,
/// 字符串字面量。创建匿名 Decl,然后取得指向它的指针。
/// 使用 `str` 联合体字段。
str,
/// 算术取负。断言不发生整数溢出。
/// 相当于左操作数为 0 的 sub;拆为独立指令以节省内存。
/// 使用 `un_node`。
negate,
/// 采用二进制补码回绕语义的整数取负。
/// 相当于左操作数为 0 的 subwrap;拆为独立指令以节省内存。
/// 使用 `un_node`。
negate_wrap,
/// 返回值的类型。
/// 使用 `un_node` 字段。
typeof,
/// 针对单个操作数实现 `@TypeOf`。
/// 使用 `pl_node` 字段,载荷为 `Block`。
typeof_builtin,
/// 给定一个值,检查其类型,该类型必须是整数。
/// 返回移位操作右侧操作数的整数类型。
/// 使用 `un_node` 字段。
typeof_log2_int_type,
/// 断言控制流不会到达此指令(`unreachable`)。
/// 使用 `@"unreachable"` 联合体字段。
@"unreachable",
/// 按位异或。`^`
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
xor,
/// 创建可选类型 '?T'
/// 使用 `un_node` 字段。
optional_type,
/// ?T => T,带安全检查。
/// 给定可选值,返回其载荷值,并执行非空
/// 安全检查。用于 `orelse`、`if` 和 `while`。
/// 使用 `un_node` 字段。
optional_payload_safe,
/// ?T => T,不执行安全检查。
/// 给定可选值,返回负载值。没有安全检查。
/// 使用 `un_node` 字段。
optional_payload_unsafe,
/// *?T => *T,带安全检查。
/// 给定指向可选值的指针,返回指向载荷值的指针,
/// 并安全检查该值非 null。供 `orelse`、`if` 和 `while` 使用。
/// 使用 `un_node` 字段。
optional_payload_safe_ptr,
/// *?T => *T,不进行安全检查。
/// 给定指向可选值的指针,返回指向载荷值的指针。
/// 不进行安全检查。
/// 使用 `un_node` 字段。
optional_payload_unsafe_ptr,
/// 不进行安全检查的 E!T => T。
/// 给定错误联合体值,返回载荷值。不执行安全检查。
/// 使用 `un_node` 字段。
err_union_payload_unsafe,
/// *E!T => *T,不进行安全检查。
/// 给定指向错误联合体值的指针,返回指向载荷值的指针。
/// 不进行安全检查。
/// 使用 `un_node` 字段。
err_union_payload_unsafe_ptr,
/// E!T => E,不执行安全检查。
/// 给定错误联合体值,返回错误码。没有安全检查。
/// 使用 `un_node` 字段。
err_union_code,
/// *E!T => E,不执行安全检查。
/// 给定错误联合体值的指针,返回错误码。没有安全检查。
/// 使用 `un_node` 字段。
err_union_code_ptr,
/// 枚举字面量。使用 `str_tok` 联合体字段。
enum_literal,
/// 声明字面量。类似 `field`,但会解包错误联合体和可选值,
/// 并将结果强制转换为给定类型。
/// 使用 `pl_node` 联合体字段。载荷为 `Field`。
decl_literal,
/// 与 `decl_literal` 相同,但省略强制转换。用于声明字面量
/// 函数调用语法,即 `.foo()`。
/// 使用 `pl_node` 联合体字段。负载为 `Field`。
decl_literal_no_coerce,
/// switch 表达式。使用 `pl_node` 联合体字段。
/// AST 节点为 switch,载荷为 `SwitchBlock`。
switch_block,
/// switch 表达式。使用 `pl_node` 联合体字段。
/// AST 节点为 switch,载荷为 `SwitchBlock`,操作数为指针。
switch_block_ref,
/// 针对错误联合的 switch:
/// - `eu catch |err| switch (err) {...}`,AST 节点为 `catch`。
/// - `if (eu) |payload| {...} else |err| {...}`,AST 节点为 `if`。
/// 使用 `pl_node` 联合体字段。载荷为 `SwitchBlock`。
switch_block_err_union,
/// 检查操作数类型是否为包含给定数量元素的数组或元组。
/// 使用 `pl_node` 字段,载荷为 `ValidateDestructure`。
validate_destructure,
/// 给定结构体或联合体,以及用 Ref 表示的字段名称,
/// 返回字段类型。使用 `pl_node` 字段。载荷为 `FieldTypeRef`。
field_type_ref,
/// 给定指针,将被指对象中的所有错误联合体和可选值初始化为载荷,
/// 并返回基础载荷指针。例如,将 *E!?T 转换为有效的 *T
/// (覆盖任何已有错误值或 null 值)。
/// 使用 `un_node` 字段。
opt_eu_base_ptr_init,
/// 强制转换给定值,使取得其引用后得到的指针
/// 可强制转换为给定类型。例如,给定 u32 类型值和指针
/// 类型 *u64,将该值转换为 u64。断言给定类型为指针类型。
/// 使用 `pl_node` 字段,载荷为 `Bin`。
/// 左操作数是指针类型,右操作数是值。
coerce_ptr_elem_ty,
/// 给定一个类型,验证其是否为适合由取地址运算符返回的指针类型。
/// 若不是,则产生编译错误。
/// 使用 `un_tok` 联合体字段。词法标记为 `&` 运算符。操作数为该类型。
validate_ref_ty,
/// 给定一个值,检查它是否为此作用域内有效的局部常量。
/// 在运行时作用域中,始终不执行任何操作。
/// 在编译时作用域中,如果该值仅在运行时已知,则产生编译错误。
/// 结果始终为 void。
/// 使用 `un_node` 联合体字段。节点为初始化表达式,操作数为初始化值。
validate_const,
// 以下标签均与结构体初始化表达式有关。
/// 带有指定显式类型且不含字段的结构体字面量。
/// 使用 `un_node` 字段。
struct_init_empty,
/// 结果类型已知且无字段的匿名结构体字面量。
/// 使用 `un_node` 字段。
struct_init_empty_result,
/// 无字段的匿名结构体字面量,以引用形式返回,且指针的
/// 结果类型已知。断言该类型为指针。
/// 使用 `un_node` 字段。
struct_init_empty_ref_result,
/// 无类型的结构体初始化。创建匿名结构体类型的值。
/// 使用 `pl_node` 字段,载荷为 `StructInitAnon`。
struct_init_anon,
/// 完成带类型的结构体或联合体初始化,执行验证,并返回
/// 结构体或联合体值。给定类型必须在此指令之前,使用
/// `validate_struct_init_ty` 或 `validate_struct_init_result_ty` 验证。若类型为
/// 泛型毒值,则降级为匿名初始化。
/// 使用 `pl_node` 字段,载荷为 `StructInit`。
struct_init,
/// 结构体初始化语法,使结果为指针。等价于 `struct_init` 后跟 `ref`;
/// 此 ZIR 标签用于优化这一常见模式。
/// 使用 `pl_node` 字段。载荷为 `StructInit`。
struct_init_ref,
/// 检查类型是否支持结构体初始化语法。始终返回 void。
/// 使用 `un_node` 字段。
validate_struct_init_ty,
/// 类似 `validate_struct_init_ty`,但额外接受结构体可强制转换到的类型。
/// 用于结构体初始化表达式已知的结果类型。始终返回 void。
/// 使用 `un_node` 字段。
validate_struct_init_result_ty,
/// 给定一组 `struct_init_field_ptr` 指令,假定它们均属于同一个结构体初始化表达式,并在适用时对重复字段和缺失字段产生编译错误。
/// 此指令断言至少存在一条 struct_init_field_ptr 指令,因为必须通过其中一条确定结构体类型。
/// 使用 `pl_node` 字段,载荷为 `Block`。
///
///
///
validate_ptr_struct_init,
/// 给定用于结构体初始化表达式的类型,返回指定名称字段的类型。
/// 使用 `pl_node` 字段。负载为 `FieldType`。
///
struct_init_field_type,
/// 给定用作结构体初始化表达式结果指针的指针,
/// 返回指向给定名称字段的指针。
/// 使用 `pl_node` 字段。AST 节点为字段初始化器。载荷为 Field。
struct_init_field_ptr,
// 以下标签均与数组初始化表达式有关。
/// 无类型数组初始化。创建元组类型的值。
/// 使用 `pl_node` 字段,载荷为 `MultiOp`。
array_init_anon,
/// 具有已知类型的数组初始化语法。必须在此指令之前,
/// 通过某个 `validate_array_init_*_ty` 指令验证给定类型。
/// 使用 `pl_node` 字段,载荷为 `MultiOp`,其中首个操作数为类型。
array_init,
/// 数组初始化语法,使结果为指针。等同于 `array_init`
/// 后跟 `ref`;此 ZIR 标签是对常见模式的优化。
/// 使用 `pl_node` 字段,载荷为 `MultiOp`,其中首个操作数为类型。
array_init_ref,
/// 检查类型是否支持数组初始化语法。始终返回 void。
/// 使用 `pl_node` 字段。载荷为 `ArrayInit`。
validate_array_init_ty,
/// 类似 `validate_array_init_ty`,但额外接受数组可强制转换到的类型。
/// 用于数组初始化表达式已知的结果类型。始终返回 void。
/// 使用 `pl_node` 字段。载荷为 `ArrayInit`。
validate_array_init_result_ty,
/// 给定指针或切片类型及元素数量,返回数组初始化器的预期类型,
/// 使指向该初始化器的指针具有给定指针类型。检查
/// 该类型是否支持数组初始化语法,不支持时产生编译错误。若数组类型
/// 带有错误联合体或可选包装,则保留这些包装。
/// 断言给定类型为指针或切片类型。
/// 使用 `pl_node` 字段,载荷为 `ArrayInitRefTy`。
validate_array_init_ref_ty,
/// 给定一组 `array_init_elem_ptr` 指令,假定它们均属于同一数组
/// 初始化表达式;若元素数量与数组类型
/// 不匹配,产生编译错误。
/// 此指令断言至少存在一条 `array_init_elem_ptr` 指令,
/// 因为必须借助其中一条确定数组类型。
/// 使用 `pl_node` 字段,载荷为 `Block`。
validate_ptr_array_init,
/// 给定用于数组初始化表达式的类型,返回
/// 指定索引处元素的类型。
/// 使用 `bin` 联合体字段。lhs 为可索引类型,rhs 为索引。
array_init_elem_type,
/// 给定一个正被用作数组初始化表达式结果指针的指针,
/// 返回指向给定索引处元素的指针。
/// 使用 `pl_node` 联合体字段。AST 节点为数组初始化
/// 语法中的一个元素。载荷为 `ElemPtrImm`。
array_init_elem_ptr,
/// 实现 `@unionInit` 内置函数。
/// 使用 `pl_node` 字段。载荷为 `UnionInit`。
union_init,
/// 实现 `@typeInfo` 内置函数。使用 `un_node`。
type_info,
/// 实现 `@sizeOf` 内置函数。使用 `un_node`。
size_of,
/// 实现 `@bitSizeOf` 内置函数。使用 `un_node`。
bit_size_of,
/// 实现内置函数 `@intFromPtr`。使用 `un_node`。
/// 将指针转换为 `usize` 整数。
int_from_ptr,
/// 输出错误消息并使编译失败。
/// 使用 `un_node` 字段。
compile_error,
/// 更改编译时执行代码所能使用的最大向后分支次数;
/// 超过此次数将放弃执行并产生编译错误。
/// 使用 `un_node` 联合体字段。
set_eval_branch_quota,
/// 将枚举值转换为整数。结果类型为枚举的标签类型。
/// 使用 `un_node`。
int_from_enum,
/// 实现 `@backingInt` 内置函数。
/// 使用 `un_node`。
backing_int,
/// 实现内置函数 `@alignOf`。使用 `un_node`。
align_of,
/// 实现内置函数 `@intFromBool`。使用 `un_node`。
int_from_bool,
/// 实现内置函数 `@embedFile`。使用 `un_node`。
embed_file,
/// 实现内置函数 `@errorName`。使用 `un_node`。
error_name,
/// 实现内置函数 `@panic`。使用 `un_node`。
panic,
/// 实现 `@trap`。
/// 使用 `node` 字段。
trap,
/// 实现内置函数 `@setRuntimeSafety`。使用 `un_node`。
set_runtime_safety,
/// 实现内置函数 `@sqrt`。使用 `un_node`。
sqrt,
/// 实现内置函数 `@sin`。使用 `un_node`。
sin,
/// 实现内置函数 `@cos`。使用 `un_node`。
cos,
/// 实现内置函数 `@tan`。使用 `un_node`。
tan,
/// 实现内置函数 `@exp`。使用 `un_node`。
exp,
/// 实现内置函数 `@exp2`。使用 `un_node`。
exp2,
/// 实现内置函数 `@log`。使用 `un_node`。
log,
/// 实现内置函数 `@log2`。使用 `un_node`。
log2,
/// 实现内置函数 `@log10`。使用 `un_node`。
log10,
/// 实现内置函数 `@abs`。使用 `un_node`。
abs,
/// 实现内置函数 `@floor`。使用 `un_node`。
floor,
/// 实现内置函数 `@ceil`。使用 `un_node`。
ceil,
/// 实现内置函数 `@trunc`。使用 `un_node`。
trunc,
/// 实现内置函数 `@round`。使用 `un_node`。
round,
/// 实现内置函数 `@tagName`。使用 `un_node`。
tag_name,
/// 实现内置函数 `@typeName`。使用 `un_node`。
type_name,
/// 实现内置函数 `@Frame`。使用 `un_node`。
frame_type,
/// 实现内置函数 `@intFromFloat`。
/// 使用载荷为 `Bin` 的 `pl_node`;`lhs` 是目标类型,`rhs` 是操作数。
int_from_float,
/// 实现 `@floatFromInt` 内置函数。
/// 使用 `pl_node`,负载为 `Bin`。`lhs` 是目标类型,`rhs` 是操作数。
float_from_int,
/// 实现内置函数 `@ptrFromInt`。
/// 使用载荷为 `Bin` 的 `pl_node`;`lhs` 是目标类型,`rhs` 是操作数。
ptr_from_int,
/// 将整数转换为枚举值。
/// 使用 `pl_node`,载荷为 `Bin`。`lhs` 为目标类型,`rhs` 为操作数。
enum_from_int,
/// 实现 `@fromBackingInt` 内置函数。
/// 使用载荷为 `Bin` 的 `pl_node`。`lhs` 是目标类型,`rhs` 是操作数。
from_backing_int,
/// 将较大的浮点类型转换为其他任意浮点类型,可能导致
/// 精度损失。
/// 使用 `pl_node` 字段。AST 为 `@floatCast` 语法。
/// 载荷为 `Bin`,lhs 为目标类型,rhs 为操作数。
float_cast,
/// 实现内置函数 `@intCast`。
/// 使用 `pl_node`,载荷为 `Bin`。`lhs` 是目标类型,`rhs` 是操作数。
/// 将整数值转换为另一整数类型,并断言目标类型
/// 能够容纳相同数学值。
int_cast,
/// 实现内置函数 `@ptrCast`。
/// 使用 `pl_node`,载荷为 `Bin`。`lhs` 是目标类型,`rhs` 是操作数。
/// 并非每个 `@ptrCast` 都对应此指令,另见
/// `Extended` 中的 `ptr_cast_full`。
ptr_cast,
/// 实现 `@truncate` 内置函数。
/// 使用 `pl_node`,载荷为 `Bin`。`lhs` 为目标类型,`rhs` 为操作数。
truncate,
/// 实现 `@hasDecl` 内置函数。
/// 使用 `pl_node` 联合体字段。载荷为 `Bin`。
has_decl,
/// 实现 `@hasField` 内置函数。
/// 使用载荷为 `Bin` 的 `pl_node` 联合体字段。
has_field,
/// 实现 `@clz` 内置函数。使用 `un_node` 联合体字段。
clz,
/// 实现 `@ctz` 内置函数。使用 `un_node` 联合体字段。
ctz,
/// 实现 `@popCount` 内置函数。使用 `un_node` 联合体字段。
pop_count,
/// 实现 `@byteSwap` 内置函数。使用 `un_node` 联合体字段。
byte_swap,
/// 实现 `@bitReverse` 内置函数。使用 `un_node` 联合体字段。
bit_reverse,
/// 实现 `@bitOffsetOf` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
bit_offset_of,
/// 实现 `@offsetOf` 内置函数。
/// 使用带有 `Bin` 载荷的 `pl_node` 联合体字段。
offset_of,
/// 实现 `@splat` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
splat,
/// 实现 `@reduce` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
reduce,
/// 实现 `@shuffle` 内置函数。
/// 使用带有 `Shuffle` 载荷的 `pl_node` 联合体字段。
shuffle,
/// 实现内置函数 `@atomicLoad`。
/// 使用 `pl_node` 联合体字段,载荷为 `AtomicLoad`。
atomic_load,
/// 实现内置函数 `@atomicRmw`。
/// 使用 `pl_node` 联合体字段,载荷为 `AtomicRmw`。
atomic_rmw,
/// 实现 `@atomicStore` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `AtomicStore`。
atomic_store,
/// 实现内置函数 `@mulAdd`。
/// 使用 `pl_node` 联合体字段,载荷为 `MulAdd`。
/// 加数传递此内置函数的类型。
/// 两个乘数需强制转换为相同类型。
mul_add,
/// 实现 `@memcpy` 内置函数。
/// 使用带有 `Bin` 载荷的 `pl_node` 联合体字段。
memcpy,
/// 实现 `@memmove` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
memmove,
/// 实现 `@memset` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
memset,
/// 实现接受 2 个参数的 `@min` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`。
min,
/// 实现接受 2 个参数的 `@max` 内置函数。
/// 使用 `pl_node` 联合体字段,载荷为 `Bin`
max,
/// 分配栈局部内存。
/// 使用 `un_node` 联合体字段,操作数为所分配对象的类型。
/// 节点源码位置指向变量声明节点。
/// 值存入该分配后,应使用 `make_ptr_const` 指令。为保证编译期值检测正常工作,在执行 `make_ptr_const` 指令之前,此指针的使用存在一些限制:不得从块返回由此 `alloc` 派生的任何指针,也不得将它们存入其他地址。换言之,必须能够直接判断任意给定指针是否由此指针派生。
///
///
///
///
///
///
alloc,
/// 与 `alloc` 相同,但结果可变。因此无需使用 `make_ptr_const`,
/// 且指针的使用不受限制。
alloc_mut,
/// 分配可在编译期修改的内存。
/// 使用 `un_node` 联合体字段。操作数为所分配对象的类型。
/// 节点源码位置指向 var 声明节点。
alloc_comptime_mut,
/// 与 `alloc` 相同,但会推断类型。
/// 使用 `node` 联合体字段。
alloc_inferred,
/// 与 `alloc_inferred` 相同,但为可变分配。
alloc_inferred_mut,
/// 分配编译期常量内存。
/// 使用 `node` 联合体字段。所分配对象的类型由推断得到。
/// 节点源码位置指向变量声明节点。
alloc_inferred_comptime,
/// 与 `alloc_comptime_mut` 相同,但类型通过推断确定。
alloc_inferred_comptime_mut,
/// 每条 `store_to_inferred_ptr` 将所存值的类型放入集合,
/// 随后 `resolve_inferred_alloc` 对该集合触发同级类型解析。
/// 操作数是 `alloc_inferred` 或 `alloc_inferred_mut` 指令,
/// 即需要推断类型的分配。
/// 结果为最终解析出的指针。此指令之后不得再引用
/// `alloc_inferred[_comptime][_mut]` 指令。
/// 使用 `un_node` 字段,AST 节点为变量声明。
resolve_inferred_alloc,
/// 将来自 `alloc` 或 `Extended.alloc` 的指针转换为
/// 同一指针的常量版本。对于推导分配,则改由
/// `resolve_inferred_alloc` 指令隐式处理。
/// 使用 `un_node` 联合体字段。
make_ptr_const,
/// 实现 `resume` 语法。使用 `un_node` 字段。
@"resume",
/// defer 语句。
/// 使用 `defer` 联合体字段。
@"defer",
/// 若操作数为 .none,或具有错误/错误联合体类型,则请求 Sema
/// 更新外围块所保存的错误返回轨迹索引。
/// 使用 `save_err_ret_index` 字段。
save_err_ret_index,
/// `Extended.restore_err_ret_index` 的专用形式。
/// 无条件将错误返回索引恢复为
/// `operand` 引用块中最后保存的状态。如果 `operand` 为 `none`,则恢复
/// 到函数入口处。
/// 使用 `un_node` 字段。
restore_err_ret_index_unconditional,
/// `Extended.restore_err_ret_index` 的特化形式。
/// 在 `operand` 为非错误值的条件下,
/// 将错误返回索引恢复到当前函数入口处的状态。
/// 若 `operand` 为 `none`,则无条件恢复。
/// 使用 `un_node` 字段。
restore_err_ret_index_fn_entry,
/// ZIR 指令标签属于 `Extended` 中的一种。
/// 使用 `extended` 联合体字段。
extended,
/// 返回该指令是否属于控制流中的“noreturn”类型。
/// 函数调用不计入其中。
pub fn isNoReturn(tag: Tag) bool {
return switch (tag) {
.param,
.param_comptime,
.param_anytype,
.param_anytype_comptime,
.add,
.addwrap,
.add_sat,
.add_unsafe,
.alloc,
.alloc_mut,
.alloc_comptime_mut,
.alloc_inferred,
.alloc_inferred_mut,
.alloc_inferred_comptime,
.alloc_inferred_comptime_mut,
.make_ptr_const,
.array_cat,
.array_type,
.array_type_sentinel,
.reify_int,
.vector_type,
.elem_type,
.indexable_ptr_elem_type,
.splat_op_result_ty,
.from_backing_int_arg_ty,
.indexable_ptr_len,
.anyframe_type,
.as_node,
.as_shift_operand,
.bit_and,
.bitcast,
.bit_or,
.block,
.block_comptime,
.block_inline,
.declaration,
.suspend_block,
.loop,
.bool_br_and,
.bool_br_or,
.bool_not,
.call,
.field_call,
.cmp_lt,
.cmp_lte,
.cmp_eq,
.cmp_gte,
.cmp_gt,
.cmp_neq,
.error_set_decl,
.dbg_stmt,
.dbg_var_ptr,
.dbg_var_val,
.decl_ref,
.decl_val,
.load,
.div,
.elem_ptr,
.elem_val,
.elem_ptr_node,
.elem_ptr_load,
.elem_val_imm,
.ensure_result_used,
.ensure_result_non_error,
.ensure_err_union_payload_void,
.@"export",
.field_ptr,
.field_ptr_load,
.field_ptr_named,
.field_ptr_named_load,
.func,
.func_inferred,
.func_fancy,
.has_decl,
.int,
.int_big,
.float,
.float128,
.int_type,
.is_non_null,
.is_non_null_ptr,
.is_non_err,
.is_non_err_ptr,
.ret_is_non_err,
.mod_rem,
.mul,
.mulwrap,
.mul_sat,
.ref,
.deref,
.ref_deref,
.shl,
.shl_sat,
.shr,
.store_node,
.store_to_inferred_ptr,
.str,
.sub,
.subwrap,
.sub_sat,
.negate,
.negate_wrap,
.typeof,
.typeof_builtin,
.xor,
.optional_type,
.optional_payload_safe,
.optional_payload_unsafe,
.optional_payload_safe_ptr,
.optional_payload_unsafe_ptr,
.err_union_payload_unsafe,
.err_union_payload_unsafe_ptr,
.err_union_code,
.err_union_code_ptr,
.ptr_type,
.enum_literal,
.decl_literal,
.decl_literal_no_coerce,
.merge_error_sets,
.error_union_type,
.bit_not,
.error_value,
.slice_start,
.slice_end,
.slice_sentinel,
.slice_length,
.slice_sentinel_ty,
.import,
.typeof_log2_int_type,
.resolve_inferred_alloc,
.set_eval_branch_quota,
.switch_block,
.switch_block_ref,
.switch_block_err_union,
.validate_destructure,
.union_init,
.field_type_ref,
.enum_from_int,
.int_from_enum,
.backing_int,
.from_backing_int,
.type_info,
.size_of,
.bit_size_of,
.int_from_ptr,
.align_of,
.int_from_bool,
.embed_file,
.error_name,
.set_runtime_safety,
.sqrt,
.sin,
.cos,
.tan,
.exp,
.exp2,
.log,
.log2,
.log10,
.abs,
.floor,
.ceil,
.trunc,
.round,
.tag_name,
.type_name,
.frame_type,
.int_from_float,
.float_from_int,
.ptr_from_int,
.float_cast,
.int_cast,
.ptr_cast,
.truncate,
.has_field,
.clz,
.ctz,
.pop_count,
.byte_swap,
.bit_reverse,
.div_exact,
.div_floor,
.div_ceil,
.div_trunc,
.mod,
.rem,
.shl_exact,
.shr_exact,
.bit_offset_of,
.offset_of,
.splat,
.reduce,
.shuffle,
.atomic_load,
.atomic_rmw,
.atomic_store,
.mul_add,
.builtin_call,
.max,
.memcpy,
.memset,
.memmove,
.min,
.@"resume",
.extended,
.ret_ptr,
.ret_type,
.@"try",
.try_ptr,
.@"defer",
.save_err_ret_index,
.for_len,
.opt_eu_base_ptr_init,
.coerce_ptr_elem_ty,
.struct_init_empty,
.struct_init_empty_result,
.struct_init_empty_ref_result,
.struct_init_anon,
.struct_init,
.struct_init_ref,
.validate_struct_init_ty,
.validate_struct_init_result_ty,
.validate_ptr_struct_init,
.struct_init_field_type,
.struct_init_field_ptr,
.array_init_anon,
.array_init,
.array_init_ref,
.validate_array_init_ty,
.validate_array_init_result_ty,
.validate_array_init_ref_ty,
.validate_ptr_array_init,
.array_init_elem_type,
.array_init_elem_ptr,
.validate_ref_ty,
.validate_const,
.restore_err_ret_index_unconditional,
.restore_err_ret_index_fn_entry,
=> false,
.@"break",
.break_inline,
.condbr,
.condbr_inline,
.compile_error,
.ret_node,
.ret_load,
.ret_implicit,
.ret_err_value,
.@"unreachable",
.repeat,
.repeat_inline,
.panic,
.trap,
.check_comptime_control_flow,
.switch_continue,
=> true,
};
}
/// AstGen 通过此项确定是否应使用 `Ref.void_value`
/// 代替给定指令的结果。这样,Sema 在分析后
/// 就无需将该指令加入映射。
pub fn isAlwaysVoid(tag: Tag, data: Data) bool {
return switch (tag) {
.dbg_stmt,
.dbg_var_ptr,
.dbg_var_val,
.ensure_result_used,
.ensure_result_non_error,
.ensure_err_union_payload_void,
.set_eval_branch_quota,
.atomic_store,
.store_node,
.store_to_inferred_ptr,
.validate_destructure,
.@"export",
.set_runtime_safety,
.memcpy,
.memset,
.memmove,
.check_comptime_control_flow,
.@"defer",
.save_err_ret_index,
.restore_err_ret_index_unconditional,
.restore_err_ret_index_fn_entry,
.validate_struct_init_ty,
.validate_struct_init_result_ty,
.validate_ptr_struct_init,
.validate_array_init_ty,
.validate_array_init_result_ty,
.validate_ptr_array_init,
.validate_ref_ty,
.validate_const,
=> true,
.param,
.param_comptime,
.param_anytype,
.param_anytype_comptime,
.add,
.addwrap,
.add_sat,
.add_unsafe,
.alloc,
.alloc_mut,
.alloc_comptime_mut,
.alloc_inferred,
.alloc_inferred_mut,
.alloc_inferred_comptime,
.alloc_inferred_comptime_mut,
.resolve_inferred_alloc,
.make_ptr_const,
.array_cat,
.array_type,
.array_type_sentinel,
.reify_int,
.vector_type,
.elem_type,
.indexable_ptr_elem_type,
.splat_op_result_ty,
.from_backing_int_arg_ty,
.indexable_ptr_len,
.anyframe_type,
.as_node,
.as_shift_operand,
.bit_and,
.bitcast,
.bit_or,
.block,
.block_comptime,
.block_inline,
.declaration,
.suspend_block,
.loop,
.bool_br_and,
.bool_br_or,
.bool_not,
.call,
.field_call,
.cmp_lt,
.cmp_lte,
.cmp_eq,
.cmp_gte,
.cmp_gt,
.cmp_neq,
.error_set_decl,
.decl_ref,
.decl_val,
.load,
.div,
.elem_ptr,
.elem_val,
.elem_ptr_node,
.elem_ptr_load,
.elem_val_imm,
.field_ptr,
.field_ptr_load,
.field_ptr_named,
.field_ptr_named_load,
.func,
.func_inferred,
.func_fancy,
.has_decl,
.int,
.int_big,
.float,
.float128,
.int_type,
.is_non_null,
.is_non_null_ptr,
.is_non_err,
.is_non_err_ptr,
.ret_is_non_err,
.mod_rem,
.mul,
.mulwrap,
.mul_sat,
.ref,
.deref,
.ref_deref,
.shl,
.shl_sat,
.shr,
.str,
.sub,
.subwrap,
.sub_sat,
.negate,
.negate_wrap,
.typeof,
.typeof_builtin,
.xor,
.optional_type,
.optional_payload_safe,
.optional_payload_unsafe,
.optional_payload_safe_ptr,
.optional_payload_unsafe_ptr,
.err_union_payload_unsafe,
.err_union_payload_unsafe_ptr,
.err_union_code,
.err_union_code_ptr,
.ptr_type,
.enum_literal,
.decl_literal,
.decl_literal_no_coerce,
.merge_error_sets,
.error_union_type,
.bit_not,
.error_value,
.slice_start,
.slice_end,
.slice_sentinel,
.slice_length,
.slice_sentinel_ty,
.import,
.typeof_log2_int_type,
.switch_block,
.switch_block_ref,
.switch_block_err_union,
.union_init,
.field_type_ref,
.enum_from_int,
.int_from_enum,
.backing_int,
.from_backing_int,
.type_info,
.size_of,
.bit_size_of,
.int_from_ptr,
.align_of,
.int_from_bool,
.embed_file,
.error_name,
.sqrt,
.sin,
.cos,
.tan,
.exp,
.exp2,
.log,
.log2,
.log10,
.abs,
.floor,
.ceil,
.trunc,
.round,
.tag_name,
.type_name,
.frame_type,
.int_from_float,
.float_from_int,
.ptr_from_int,
.float_cast,
.int_cast,
.ptr_cast,
.truncate,
.has_field,
.clz,
.ctz,
.pop_count,
.byte_swap,
.bit_reverse,
.div_exact,
.div_floor,
.div_ceil,
.div_trunc,
.mod,
.rem,
.shl_exact,
.shr_exact,
.bit_offset_of,
.offset_of,
.splat,
.reduce,
.shuffle,
.atomic_load,
.atomic_rmw,
.mul_add,
.builtin_call,
.max,
.min,
.@"resume",
.@"break",
.break_inline,
.condbr,
.condbr_inline,
.switch_continue,
.compile_error,
.ret_node,
.ret_load,
.ret_implicit,
.ret_err_value,
.ret_ptr,
.ret_type,
.@"unreachable",
.repeat,
.repeat_inline,
.panic,
.trap,
.for_len,
.@"try",
.try_ptr,
.opt_eu_base_ptr_init,
.coerce_ptr_elem_ty,
.struct_init_empty,
.struct_init_empty_result,
.struct_init_empty_ref_result,
.struct_init_anon,
.struct_init,
.struct_init_ref,
.struct_init_field_type,
.struct_init_field_ptr,
.array_init_anon,
.array_init,
.array_init_ref,
.validate_array_init_ref_ty,
.array_init_elem_type,
.array_init_elem_ptr,
=> false,
.extended => switch (data.extended.opcode) {
.branch_hint,
.breakpoint,
.disable_instrumentation,
.disable_intrinsics,
=> true,
else => false,
},
};
}
/// 由调试安全检查代码使用。
pub const data_tags = list: {
@setEvalBranchQuota(2000);
break :list std.enums.directEnumArray(Tag, Data.FieldEnum, 0, .{
.add = .pl_node,
.addwrap = .pl_node,
.add_sat = .pl_node,
.add_unsafe = .pl_node,
.sub = .pl_node,
.subwrap = .pl_node,
.sub_sat = .pl_node,
.mul = .pl_node,
.mulwrap = .pl_node,
.mul_sat = .pl_node,
.param = .pl_tok,
.param_comptime = .pl_tok,
.param_anytype = .str_tok,
.param_anytype_comptime = .str_tok,
.array_cat = .pl_node,
.array_type = .pl_node,
.array_type_sentinel = .pl_node,
.reify_int = .pl_node,
.vector_type = .pl_node,
.elem_type = .un_node,
.indexable_ptr_elem_type = .un_node,
.splat_op_result_ty = .un_node,
.from_backing_int_arg_ty = .un_node,
.indexable_ptr_len = .un_node,
.anyframe_type = .un_node,
.as_node = .pl_node,
.as_shift_operand = .pl_node,
.bit_and = .pl_node,
.bitcast = .pl_node,
.bit_not = .un_node,
.bit_or = .pl_node,
.block = .pl_node,
.block_comptime = .pl_node,
.block_inline = .pl_node,
.declaration = .declaration,
.suspend_block = .pl_node,
.bool_not = .un_node,
.bool_br_and = .pl_node,
.bool_br_or = .pl_node,
.@"break" = .@"break",
.break_inline = .@"break",
.switch_continue = .@"break",
.check_comptime_control_flow = .un_node,
.for_len = .pl_node,
.call = .pl_node,
.field_call = .pl_node,
.cmp_lt = .pl_node,
.cmp_lte = .pl_node,
.cmp_eq = .pl_node,
.cmp_gte = .pl_node,
.cmp_gt = .pl_node,
.cmp_neq = .pl_node,
.condbr = .pl_node,
.condbr_inline = .pl_node,
.@"try" = .pl_node,
.try_ptr = .pl_node,
.error_set_decl = .pl_node,
.dbg_stmt = .dbg_stmt,
.dbg_var_ptr = .str_op,
.dbg_var_val = .str_op,
.decl_ref = .str_tok,
.decl_val = .str_tok,
.load = .un_node,
.div = .pl_node,
.elem_ptr = .pl_node,
.elem_ptr_node = .pl_node,
.elem_val = .pl_node,
.elem_ptr_load = .pl_node,
.elem_val_imm = .elem_val_imm,
.ensure_result_used = .un_node,
.ensure_result_non_error = .un_node,
.ensure_err_union_payload_void = .un_node,
.error_union_type = .pl_node,
.error_value = .str_tok,
.@"export" = .pl_node,
.field_ptr = .pl_node,
.field_ptr_load = .pl_node,
.field_ptr_named = .pl_node,
.field_ptr_named_load = .pl_node,
.func = .pl_node,
.func_inferred = .pl_node,
.func_fancy = .pl_node,
.import = .pl_tok,
.int = .int,
.int_big = .str,
.float = .float,
.float128 = .pl_node,
.int_type = .int_type,
.is_non_null = .un_node,
.is_non_null_ptr = .un_node,
.is_non_err = .un_node,
.is_non_err_ptr = .un_node,
.ret_is_non_err = .un_node,
.loop = .pl_node,
.repeat = .node,
.repeat_inline = .node,
.merge_error_sets = .pl_node,
.mod_rem = .pl_node,
.ref = .un_tok,
.deref = .un_node,
.ref_deref = .un_node,
.ret_node = .un_node,
.ret_load = .un_node,
.ret_implicit = .un_tok,
.ret_err_value = .str_tok,
.ret_ptr = .node,
.ret_type = .node,
.ptr_type = .ptr_type,
.slice_start = .pl_node,
.slice_end = .pl_node,
.slice_sentinel = .pl_node,
.slice_length = .pl_node,
.slice_sentinel_ty = .un_node,
.store_node = .pl_node,
.store_to_inferred_ptr = .pl_node,
.str = .str,
.negate = .un_node,
.negate_wrap = .un_node,
.typeof = .un_node,
.typeof_log2_int_type = .un_node,
.@"unreachable" = .@"unreachable",
.xor = .pl_node,
.optional_type = .un_node,
.optional_payload_safe = .un_node,
.optional_payload_unsafe = .un_node,
.optional_payload_safe_ptr = .un_node,
.optional_payload_unsafe_ptr = .un_node,
.err_union_payload_unsafe = .un_node,
.err_union_payload_unsafe_ptr = .un_node,
.err_union_code = .un_node,
.err_union_code_ptr = .un_node,
.enum_literal = .str_tok,
.decl_literal = .pl_node,
.decl_literal_no_coerce = .pl_node,
.switch_block = .pl_node,
.switch_block_ref = .pl_node,
.switch_block_err_union = .pl_node,
.validate_destructure = .pl_node,
.field_type_ref = .pl_node,
.union_init = .pl_node,
.type_info = .un_node,
.size_of = .un_node,
.bit_size_of = .un_node,
.opt_eu_base_ptr_init = .un_node,
.coerce_ptr_elem_ty = .pl_node,
.validate_ref_ty = .un_tok,
.validate_const = .un_node,
.int_from_ptr = .un_node,
.compile_error = .un_node,
.set_eval_branch_quota = .un_node,
.int_from_enum = .un_node,
.backing_int = .un_node,
.align_of = .un_node,
.int_from_bool = .un_node,
.embed_file = .un_node,
.error_name = .un_node,
.panic = .un_node,
.trap = .node,
.set_runtime_safety = .un_node,
.sqrt = .un_node,
.sin = .un_node,
.cos = .un_node,
.tan = .un_node,
.exp = .un_node,
.exp2 = .un_node,
.log = .un_node,
.log2 = .un_node,
.log10 = .un_node,
.abs = .un_node,
.floor = .un_node,
.ceil = .un_node,
.trunc = .un_node,
.round = .un_node,
.tag_name = .un_node,
.type_name = .un_node,
.frame_type = .un_node,
.int_from_float = .pl_node,
.float_from_int = .pl_node,
.ptr_from_int = .pl_node,
.enum_from_int = .pl_node,
.from_backing_int = .pl_node,
.float_cast = .pl_node,
.int_cast = .pl_node,
.ptr_cast = .pl_node,
.truncate = .pl_node,
.typeof_builtin = .pl_node,
.has_decl = .pl_node,
.has_field = .pl_node,
.clz = .un_node,
.ctz = .un_node,
.pop_count = .un_node,
.byte_swap = .un_node,
.bit_reverse = .un_node,
.div_exact = .pl_node,
.div_floor = .pl_node,
.div_ceil = .pl_node,
.div_trunc = .pl_node,
.mod = .pl_node,
.rem = .pl_node,
.shl = .pl_node,
.shl_exact = .pl_node,
.shl_sat = .pl_node,
.shr = .pl_node,
.shr_exact = .pl_node,
.bit_offset_of = .pl_node,
.offset_of = .pl_node,
.splat = .pl_node,
.reduce = .pl_node,
.shuffle = .pl_node,
.atomic_load = .pl_node,
.atomic_rmw = .pl_node,
.atomic_store = .pl_node,
.mul_add = .pl_node,
.builtin_call = .pl_node,
.max = .pl_node,
.memcpy = .pl_node,
.memset = .pl_node,
.memmove = .pl_node,
.min = .pl_node,
.alloc = .un_node,
.alloc_mut = .un_node,
.alloc_comptime_mut = .un_node,
.alloc_inferred = .node,
.alloc_inferred_mut = .node,
.alloc_inferred_comptime = .node,
.alloc_inferred_comptime_mut = .node,
.resolve_inferred_alloc = .un_node,
.make_ptr_const = .un_node,
.@"resume" = .un_node,
.@"defer" = .@"defer",
.save_err_ret_index = .save_err_ret_index,
.restore_err_ret_index_unconditional = .un_node,
.restore_err_ret_index_fn_entry = .un_node,
.struct_init_empty = .un_node,
.struct_init_empty_result = .un_node,
.struct_init_empty_ref_result = .un_node,
.struct_init_anon = .pl_node,
.struct_init = .pl_node,
.struct_init_ref = .pl_node,
.validate_struct_init_ty = .un_node,
.validate_struct_init_result_ty = .un_node,
.validate_ptr_struct_init = .pl_node,
.struct_init_field_type = .pl_node,
.struct_init_field_ptr = .pl_node,
.array_init_anon = .pl_node,
.array_init = .pl_node,
.array_init_ref = .pl_node,
.validate_array_init_ty = .pl_node,
.validate_array_init_result_ty = .pl_node,
.validate_array_init_ref_ty = .pl_node,
.validate_ptr_array_init = .pl_node,
.array_init_elem_type = .bin,
.array_init_elem_ptr = .pl_node,
.extended = .extended,
});
};
// 取消注释可查看还剩多少标签槽位。
// comptime {
// @compileLog("ZIR tags left: ", 256 - @typeInfo(Tag).@"enum".field_names.len);
// }
};
/// 这里存放较少见、无法纳入 8 位 `Tag` 枚举的指令。
/// `noreturn` 指令不能放在这里,必须属于主 `Tag` 枚举。
pub const Extended = enum(u16) {
/// 结构体类型定义。包含指向字段类型、默认值及对齐值
/// 所对应 ZIR 指令的引用。
/// `operand` 是指向 `StructDecl` 的负载索引。
/// `small` 为 `StructDecl.Small`。
struct_decl,
/// 枚举类型定义。包含指向字段值表达式以及
/// 可选类型标签表达式的 ZIR 指令引用。
/// `operand` 是 `EnumDecl` 的载荷索引。
/// `small` 为 `EnumDecl.Small`。
enum_decl,
/// 联合体类型定义。包含指向字段类型及可选类型标签表达式
/// 所对应 ZIR 指令的引用。
/// `operand` 是指向 `UnionDecl` 的负载索引。
/// `small` 为 `UnionDecl.Small`。
union_decl,
/// 不透明类型定义。包含对声明及捕获的引用。
/// `operand` 是指向 `OpaqueDecl` 的载荷索引。
/// `small` 为 `OpaqueDecl.Small`。
opaque_decl,
/// 元组类型。注意,元组不是命名空间/容器类型。
/// `operand` 是指向 `TupleDecl` 的载荷索引。
/// `small` 为 `fields_len: u16`。
tuple_decl,
/// 实现 `@This` 内置函数。
/// `operand` 为 `src_node: Ast.Node.Offset`。
this,
/// 实现 `@returnAddress` 内置函数。
/// `operand` 为 `src_node: Ast.Node.Offset`。
ret_addr,
/// 实现 `@src` 内置函数。
/// `operand` 是指向 `LineColumn` 的载荷索引。
builtin_src,
/// 实现 `@errorReturnTrace` 内置函数。
/// `operand` 为 `src_node: Ast.Node.Offset`。
error_return_trace,
/// 实现 `@frame` 内置函数。
/// `operand` 为 `src_node: Ast.Node.Offset`。
frame,
/// 实现 `@frameAddress` 内置函数。
/// `operand` 为 `src_node: Ast.Node.Offset`。
frame_address,
/// 与 `Tag` 的 `alloc` 相同,但可能包含对齐指令。
/// `operand` 是指向 `AllocExtended` 的负载索引。
/// `small`:
/// * 0b000X - 有类型
/// * 0b00X0 - 有对齐值
/// * 0b0X00 - 1=const, 0=var
/// * 0bX000 - 为编译期
alloc,
/// `@extern` 内置函数。
/// `operand` 是指向 `BinNode` 的载荷索引。
builtin_extern,
/// 内联汇编。
/// `operand` 为指向 `Asm` 的载荷索引。
@"asm",
/// 与 `asm` 相同,但汇编模板不是字符串字面量,而是编译时
/// 表达式。
/// Asm 的 `asm_source` 字段不是以 null 结尾的字符串,
/// 而是 Ref。
asm_expr,
/// 记录编译期变量,并产生错误消息。
/// `operand` 是指向 `NodeMultiOp` 的负载索引。
/// `small` 为 `operands_len`。
/// AST 节点是编译日志内置函数调用。
compile_log,
/// 内置函数 `@TypeOf` 返回一个或多个参数经过
/// 同级类型解析后的类型。
/// `operand` 是指向 `TypeOfPeer` 的负载索引。
/// `small` 为 `operands_len`。
/// AST 节点是内置函数调用。
typeof_peer,
/// 实现参数超过两个时的 `@min` 内置函数。
/// `operand` 是指向 `NodeMultiOp` 的负载索引。
/// `small` 为 `operands_len`。
/// AST 节点是内置函数调用。
min_multi,
/// 实现参数多于两个的内置函数 `@max`。
/// `operand` 是 `NodeMultiOp` 的载荷索引。
/// `small` 为 `operands_len`。
/// AST 节点为内置函数调用。
max_multi,
/// 实现内置函数 `@addWithOverflow`。
/// `operand` 是指向 `BinNode` 的载荷索引。
/// `small` 未使用。
add_with_overflow,
/// 实现 `@subWithOverflow` 内置函数。
/// `operand` 为指向 `BinNode` 的载荷索引。
/// `small` 未使用。
sub_with_overflow,
/// 实现 `@mulWithOverflow` 内置函数。
/// `operand` 是指向 `BinNode` 的载荷索引。
/// `small` 未使用。
mul_with_overflow,
/// 实现内置函数 `@shlWithOverflow`。
/// `operand` 是指向 `BinNode` 的载荷索引。
/// `small` 未使用。
shl_with_overflow,
/// 带有结果类型的 `@round`、`@floor`、`@ceil` 或 `@trunc`。
/// `operand` 为指向 `BinNode` 的载荷索引。
/// `small` 为表示所执行具体操作的 `RoundOp`。
round_op,
/// 返回舍入操作的操作数类型。
/// `operand` 为 `UnNode`。
/// `small` 未使用。
round_op_ty,
/// `operand` 是指向 `UnNode` 的载荷索引。
wasm_memory_size,
/// `operand` 是指向 `BinNode` 的载荷索引。
wasm_memory_grow,
/// `@prefetch` 内置函数。
/// `operand` 为指向 `BinNode` 的载荷索引。
prefetch,
/// 实现内置函数 `@setFloatMode`。
/// `operand` 是指向 `UnNode` 的载荷索引。
set_float_mode,
/// 实现 `@errorCast` 内置函数。
/// `operand` 是指向 `BinNode` 的载荷索引。`lhs` 是目标类型,`rhs` 是操作数。
error_cast,
/// 实现 `@breakpoint`。
/// `operand` 为 `src_node: Ast.Node.Offset`。
breakpoint,
/// 实现内置函数 `@disableInstrumentation`。`operand` 为 `src_node: Ast.Node.Offset`。
disable_instrumentation,
/// 实现内置函数 `@disableIntrinsics`。`operand` 为 `src_node: i32`。
disable_intrinsics,
/// 实现 `@select` 内置函数。
/// `operand` 是指向 `Select` 的载荷索引。
select,
/// 实现内置函数 `@errToInt`。
/// `operand` 为指向 `UnNode` 的载荷索引。
int_from_error,
/// 实现内置函数 `@errorFromInt`。
/// `operand` 是指向 `UnNode` 的载荷索引。
error_from_int,
/// 给定类型为 `[]const A` 的编译期已知操作数,返回类型 `*const [operand.len]B`。
/// 类型 `A` 和 `B` 由 `ReifySliceArgInfo` 确定。
/// 此指令用于为 `@Fn`、`@Struct` 等的参数提供结果类型。
/// `operand` 是指向 `UnNode` 的载荷索引。
/// `small` 是按位转换后的 `ReifySliceArgInfo`。
reify_slice_arg_ty,
/// 类似 `reify_slice_arg_ty`,但专用于 `@Enum` 所需的 `[]const []const u8` 到 `[]const TagInt` 转换。
///
/// `operand` 是指向 `BinNode` 的负载索引。lhs 为类型 `TagInt`。rhs 为 `[]const []const u8` 值。
/// `small` 未使用。
reify_enum_value_slice_ty,
/// 给定编译时已知、类型为 `type` 的操作数,如果可行,则返回类型 `?operand`,否则返回 `?noreturn`。
/// 用于 `@Pointer` 的最后一个参数,以允许将不透明类型的指针实体化。
/// `operand` 是 `UnNode` 的载荷索引。
/// `small` 未使用。
reify_pointer_sentinel_ty,
/// 实现内置函数 `@Tuple`。
/// `operand` 是指向 `UnNode` 的载荷索引。
reify_tuple,
/// 实现内置函数 `@Pointer`。
/// `operand` 为指向 `ReifyPointer` 的载荷索引。
reify_pointer,
/// 实现内置函数 `@Fn`。
/// `operand` 是指向 `ReifyFn` 的载荷索引。
reify_fn,
/// 实现内置函数 `@Struct`。
/// `operand` 是指向 `ReifyStruct` 的负载索引。
/// `small` 包含 `NameStrategy`。
reify_struct,
/// 实现内置函数 `@Union`。
/// `operand` 为指向 `ReifyUnion` 的载荷索引。
/// `small` 包含 `NameStrategy`。
reify_union,
/// 实现内置函数 `@Enum`。
/// `operand` 是指向 `ReifyEnum` 的载荷索引。
/// `small` 包含 `NameStrategy`。
reify_enum,
/// 实现内置函数 `@SpirvType`。
/// `operand` 是指向 `ReifyFn` 的载荷索引。
reify_spirv_type,
/// 实现 `@cmpxchgStrong` 和 `@cmpxchgWeak` 内置函数。
/// `small` 0=>弱 1=>强
/// `operand` 是指向 `Cmpxchg` 的负载索引。
cmpxchg,
/// 实现内置函数 `@cVaArg`。
/// `operand` 是指向 `BinNode` 的载荷索引。
c_va_arg,
/// 实现内置函数 `@cVaCopy`。
/// `operand` 为指向 `UnNode` 的载荷索引。
c_va_copy,
/// 实现内置函数 `@cVaEnd`。
/// `operand` 是指向 `UnNode` 的载荷索引。
c_va_end,
/// 实现内置函数 `@cVaStart`。
/// `operand` 为 `src_node: Ast.Node.Offset`。
c_va_start,
/// 实现以下内置函数:
/// `@ptrCast`、`@alignCast`、`@addrSpaceCast`、`@constCast`、`@volatileCast`。
/// 表示上述内置函数的任意嵌套。这类嵌套视为
/// 单个操作,可以修改指针类型的多个组成部分。
/// `operand` 为指向 `BinNode` 的载荷索引。
/// `small` 包含 `FullPtrCastFlags`。
/// AST 节点为嵌套转换的根。
/// `lhs` 为目标类型,`rhs` 为操作数。
ptr_cast_full,
/// `operand` 是指向 `UnNode` 的负载索引。
/// `small` 包含 `FullPtrCastFlags`。
/// 保证仅含不需要显式目标类型的标志,
/// 即 const_cast 和 volatile_cast。
/// AST 节点是嵌套转换的根。
ptr_cast_no_dest,
/// 实现 `@workItemId` 内置函数。
/// `operand` 为指向 `UnNode` 的载荷索引。
work_item_id,
/// 实现 `@workGroupSize` 内置函数。
/// `operand` 为指向 `UnNode` 的载荷索引。
work_group_size,
/// 实现 `@workGroupId` 内置函数。
/// `operand` 是指向 `UnNode` 的载荷索引。
work_group_id,
/// 实现 `@inComptime` 内置函数。
/// `operand` 为 `src_node: Ast.Node.Offset`。
in_comptime,
/// 将错误返回索引恢复为给定块中最后保存的状态。
/// 若块为 `.none`,则恢复为函数入口时的状态。若操作数不是 `.none`,则仅当操作数值不是错误时执行恢复。
/// `operand` 是指向 `RestoreErrRetIndex` 的载荷索引。
/// `small` 未定义。
///
///
restore_err_ret_index,
/// 从当前类型声明作用域的闭包中获取值。
/// `operand` 为 `src_node: Ast.Node.Offset`。
/// `small` 为闭包索引。
closure_get,
/// 用作占位指令,仅为一个虚拟索引,供 Sema 替换为特定值。
/// 例如用于捕获 `errdefer` 的错误。
/// 此项绝不应出现在主体中。
value_placeholder,
/// 实现内置函数 `@fieldParentPtr`。
/// `operand` 是指向 `FieldParentPtr` 的载荷索引。
/// `small` 包含 `FullPtrCastFlags`。
/// 保证没有 `ptr_cast` 标志。
/// 使用 `pl_node` 联合体字段,载荷为 `FieldParentPtr`。
field_parent_ptr,
/// 从 `std.lang` 获取类型或值。
/// `operand` 为 `src_node: Ast.Node.Offset`。
/// `small` 为 `Inst.StdLangValue`。
std_lang_value,
/// 为当前块提供 `@branchHint`。
/// `operand` 是指向 `UnNode` 的载荷索引。
/// `small` 未使用。
branch_hint,
/// 计算原地算术运算的结果类型,例如 `+=`。
/// `operand` 是已加载左值的 `Zir.Inst.Ref`,而非其类型。
/// `small` 是 `Inst.InplaceOp`。
inplace_arith_result_ty,
/// 标记一个可以单步执行到达、但不生成代码的语句。
/// 忽略 `operand` 和 `small`。
dbg_empty_stmt,
/// 此处 AstGen 遇到致命错误,导致此主体的 ZIR 降级终止。
/// 已经报告文件级错误,Sema 应终止语义分析。
/// 忽略 `operand` 和 `small`。
/// 此指令始终为 `noreturn`,但 ZIR 层查询不将其视为此类指令,使 AstGen 可以假定此处原本可能存在任意代码,避免错误报告“不可达代码”。
///
astgen_error,
/// 给定类型,去除其外层堆叠的所有错误联合体或可选类型,
/// 返回基础类型。基础类型必须是浮点类型。
/// 例如,提供 `error{Foo}!?f64` 时,返回 `f64`。
/// `operand` 是指向 `UnNode` 的载荷索引。
float_op_result_ty,
pub const InstData = struct {
opcode: Extended,
small: u16,
operand: u32,
};
};
/// ZIR 指令在 `Zir` 指令数组中的位置。
pub const Index = enum(u32) {
/// ZIR 的结构保证任意文件最外层的"main"结构体
/// 始终位于索引 0。
main_struct_inst = 0,
_,
pub fn toRef(i: Index) Inst.Ref {
return @fromBackingInt(@intCast(Ref.static_len + @backingInt(i)));
}
pub fn toOptional(i: Index) OptionalIndex {
return @fromBackingInt(@intCast(@backingInt(i)));
}
};
pub const OptionalIndex = enum(u32) {
/// ZIR 的结构保证任意文件最外层的"main"结构体
/// 始终位于索引 0。
main_struct_inst = 0,
none = std.math.maxInt(u32),
_,
pub fn unwrap(oi: OptionalIndex) ?Index {
return if (oi == .none) null else @fromBackingInt(@intCast(@backingInt(oi)));
}
};
/// 对 ZIR 指令或 InternPool 索引的引用,也可能两者都不是。
///
/// 如果整数标签值 < InternPool.static_len,
/// 则对应 InternPool 索引。否则,引用 ZIR
/// 指令。
///
/// 显式指定标签类型,使 `[]u32` 与 `[]Ref`
/// 之间的位转换安全。
pub const Ref = enum(u32) {
u0_type,
u1_type,
u8_type,
i8_type,
u16_type,
i16_type,
u29_type,
u32_type,
i32_type,
u64_type,
i64_type,
u80_type,
u128_type,
i128_type,
u256_type,
usize_type,
isize_type,
c_char_type,
c_short_type,
c_ushort_type,
c_int_type,
c_uint_type,
c_long_type,
c_ulong_type,
c_longlong_type,
c_ulonglong_type,
c_longdouble_type,
f16_type,
f32_type,
f64_type,
f80_type,
f128_type,
anyopaque_type,
bool_type,
void_type,
type_type,
anyerror_type,
comptime_int_type,
comptime_float_type,
noreturn_type,
anyframe_type,
null_type,
undefined_type,
enum_literal_type,
ptr_usize_type,
ptr_const_comptime_int_type,
manyptr_u8_type,
manyptr_const_u8_type,
manyptr_const_u8_sentinel_0_type,
slice_const_u8_type,
slice_const_u8_sentinel_0_type,
manyptr_const_slice_const_u8_type,
slice_const_slice_const_u8_type,
optional_type_type,
manyptr_const_type_type,
slice_const_type_type,
vector_8_i8_type,
vector_16_i8_type,
vector_32_i8_type,
vector_64_i8_type,
vector_1_u8_type,
vector_2_u8_type,
vector_4_u8_type,
vector_8_u8_type,
vector_16_u8_type,
vector_32_u8_type,
vector_64_u8_type,
vector_2_i16_type,
vector_4_i16_type,
vector_8_i16_type,
vector_16_i16_type,
vector_32_i16_type,
vector_4_u16_type,
vector_8_u16_type,
vector_16_u16_type,
vector_32_u16_type,
vector_2_i32_type,
vector_4_i32_type,
vector_8_i32_type,
vector_16_i32_type,
vector_4_u32_type,
vector_8_u32_type,
vector_16_u32_type,
vector_2_i64_type,
vector_4_i64_type,
vector_8_i64_type,
vector_2_u64_type,
vector_4_u64_type,
vector_8_u64_type,
vector_1_u128_type,
vector_2_u128_type,
vector_1_u256_type,
vector_4_f16_type,
vector_8_f16_type,
vector_16_f16_type,
vector_32_f16_type,
vector_2_f32_type,
vector_4_f32_type,
vector_8_f32_type,
vector_16_f32_type,
vector_2_f64_type,
vector_4_f64_type,
vector_8_f64_type,
optional_noreturn_type,
anyerror_void_error_union_type,
adhoc_inferred_error_set_type,
generic_poison_type,
empty_tuple_type,
undef,
undef_bool,
undef_usize,
undef_u1,
zero,
zero_usize,
zero_u1,
zero_u8,
one,
one_usize,
one_u1,
one_u8,
four_u8,
negative_one,
void_value,
unreachable_value,
null_value,
bool_true,
bool_false,
empty_tuple,
/// 此 Ref 不对应任何 ZIR 指令或常量
/// 值,而可用作表示 null 的哨兵。
none = std.math.maxInt(u32),
_,
pub const static_len = @typeInfo(@This()).@"enum".field_names.len - 1;
pub fn toIndex(inst: Ref) ?Index {
assert(inst != .none);
const ref_int = @backingInt(inst);
if (ref_int >= static_len) {
return @fromBackingInt(@intCast(ref_int - static_len));
} else {
return null;
}
}
pub fn toIndexAllowNone(inst: Ref) ?Index {
if (inst == .none) return null;
return toIndex(inst);
}
};
/// 所有指令都具有 8 字节载荷,存储在
/// 此联合体中。`Tag` 决定哪个联合体字段处于活动状态,
/// 以及如何解释其中的数据。
pub const Data = union {
/// 用于 `Tag.extended`。扩展操作码决定
/// `small` 和 `operand` 字段的含义。
extended: Extended.InstData,
/// 用于带 AST 节点源码位置的一元运算符。
un_node: struct {
/// 相对于 Decl AST 节点索引的偏移。
src_node: Ast.Node.Offset,
/// 此操作数的含义取决于对应的 `Tag`。
operand: Ref,
},
/// 用于一元运算符,带有 token 源码位置。
un_tok: struct {
/// 相对于 Decl AST 词法标记索引的偏移量。
src_tok: Ast.TokenOffset,
/// 此操作数的含义取决于对应的 `Tag`。
operand: Ref,
},
pl_node: struct {
/// 相对于 Decl AST 节点索引的偏移量。
/// `Tag` 决定所指向的 AST 节点类型。
src_node: Ast.Node.Offset,
/// extra 中的索引。
/// `Tag` 决定该位置存储的内容。
payload_index: u32,
},
pl_tok: struct {
/// 相对于 Decl AST 词法标记索引的偏移量。
src_tok: Ast.TokenOffset,
/// extra 中的索引。
/// `Tag` 决定该位置存储的内容。
payload_index: u32,
},
bin: Bin,
/// 用于可能包含空字节的字符串。
str: struct {
/// `string_bytes` 中的偏移。
start: NullTerminatedString,
/// 字符串的字节数。
len: u32,
pub fn get(self: @This(), code: Zir) []const u8 {
return code.string_bytes[@backingInt(self.start)..][0..self.len];
}
},
str_tok: struct {
/// `string_bytes` 中的偏移量。以空字符终止。
start: NullTerminatedString,
/// 相对于 Decl AST 词法标记索引的偏移量。
src_tok: Ast.TokenOffset,
pub fn get(self: @This(), code: Zir) [:0]const u8 {
return code.nullTerminatedString(self.start);
}
},
/// 相对于 Decl AST 词法标记索引的偏移量。
tok: Ast.TokenOffset,
/// 相对于 Decl AST 节点索引的偏移。
node: Ast.Node.Offset,
int: u64,
float: f64,
ptr_type: struct {
flags: packed struct {
is_allowzero: bool,
is_mutable: bool,
is_volatile: bool,
has_sentinel: bool,
has_align: bool,
has_addrspace: bool,
has_bit_range: bool,
_: u1 = 0,
},
size: std.lang.Type.Pointer.Size,
/// 指向 extra 的索引。参见 `PtrType`。
payload_index: u32,
},
int_type: struct {
/// 相对于 Decl AST 节点索引的偏移量。
/// `Tag` 决定所指向的 AST 节点类型。
src_node: Ast.Node.Offset,
signedness: std.lang.Signedness,
bit_count: u16,
},
@"unreachable": struct {
/// 相对于 Decl AST 节点索引的偏移量。
/// `Tag` 决定所指向的 AST 节点类型。
src_node: Ast.Node.Offset,
},
@"break": struct {
operand: Ref,
/// `Break` 载荷的索引。
payload_index: u32,
},
dbg_stmt: LineColumn,
/// 用于引用一条指令、
/// 并带有 AST 节点源码位置的一元运算符。
inst_node: struct {
/// 相对于 Decl AST 节点索引的偏移。
src_node: Ast.Node.Offset,
/// 此操作数的含义取决于对应的 `Tag`。
inst: Index,
},
str_op: struct {
/// `string_bytes` 中的偏移量。以空字符终止。
str: NullTerminatedString,
operand: Ref,
pub fn getStr(self: @This(), zir: Zir) [:0]const u8 {
return zir.nullTerminatedString(self.str);
}
},
@"defer": struct {
index: u32,
len: u32,
},
save_err_ret_index: struct {
operand: Ref, // 若为错误类型(或 .none),保存新的跟踪索引
},
elem_val_imm: struct {
/// 正在访问的可索引值。
operand: Ref,
/// 被访问的索引。
idx: u32,
},
declaration: struct {
/// 此节点为该结构体内的全部指令提供新的绝对基准节点。
src_node: Ast.Node.Index,
/// extra 中指向 `Declaration` 载荷的索引。
payload_index: u32,
},
// 确保没有意外添加字段,导致此联合体
// 大于预期。注意,在 Debug 构建中,允许 Zig
// 插入用于安全检查的隐藏字段。
comptime {
if (builtin.mode != .debug and builtin.mode != .safe) {
assert(@sizeOf(Data) == 8);
}
}
/// TODO: 必须使其与 `Data` 保持同步,而该类型希望采用无标签联合体。
/// 这里涉及一项语言使用上的不便:从文件反序列化无标签联合体,此处为 `Data`,同时尝试保留隐藏的安全字段。
///
///
pub const FieldEnum = enum {
extended,
un_node,
un_tok,
pl_node,
pl_tok,
bin,
str,
str_tok,
tok,
node,
int,
float,
ptr_type,
int_type,
@"unreachable",
@"break",
dbg_stmt,
inst_node,
str_op,
@"defer",
save_err_ret_index,
elem_val_imm,
declaration,
};
};
pub const Break = struct {
operand_src_node: Ast.Node.OptionalOffset,
block_inst: Index,
};
/// 尾随数据:
/// 0. 对每个 outputs_len 的 Output
/// 1. 对每个 inputs_len 的 Input
pub const Asm = struct {
src_node: Ast.Node.Offset,
// 以空字符结尾的字符串索引
asm_source: NullTerminatedString,
/// 每个 outputs_len 对应 1 位,表示是否使用 `-> T`。
/// 0b0 - 操作数是指向输出存储位置的指针。
/// 0b1 - 操作数是类型;asm 表达式以输出作为结果。
/// 0b0X 对应第一个输出,0bX0 对应第二个输出,依此类推。
output_type_bits: u32,
clobbers: Ref,
pub const Small = packed struct(u16) {
is_volatile: bool,
outputs_len: u7,
inputs_len: u8,
};
pub const Output = struct {
/// string_bytes 中的索引(以空字符终止)
name: NullTerminatedString,
/// string_bytes 中的索引(以空字符终止)
constraint: NullTerminatedString,
/// 如何解释此内容由 `output_type_bits` 决定。
operand: Ref,
};
pub const Input = struct {
/// string_bytes 中的索引(以空字符终止)
name: NullTerminatedString,
/// string_bytes 中的索引(以空字符终止)
constraint: NullTerminatedString,
operand: Ref,
};
};
/// 尾随内容:
/// if (ret_ty.body_len == 1) {
/// 0. return_type: Ref
/// }
/// if (ret_ty.body_len > 1) {
/// 1. return_type: Index // 每个 ret_ty.body_len 对应一个
/// }
/// 2. body: Index // 每个 body_len 对应一个
/// 3. src_locs: SrcLocs // 如果 body_len != 0
/// 4. proto_hash: std.zig.SrcHash // 如果 body_len != 0;函数原型的哈希
pub const Func = struct {
ret_ty: RetTy,
/// 指向包含此函数参数指令的块。
/// 若这是 `declaration`,则引用该声明的值体。
param_block: Index,
body_len: u32,
pub const RetTy = packed struct(u32) {
/// 0 表示 `void`。
/// 1 表示类型是一个简单的 `Ref`。
/// 否则,表示尾随主体的长度。
body_len: u31,
/// 返回类型是否为泛型,即引用一个或多个先前参数。
is_generic: bool,
};
pub const SrcLocs = struct {
/// 源码文件中相对于父声明的行索引。
lbrace_line: u32,
/// 源码文件中相对于父声明的行索引。
rbrace_line: u32,
/// lbrace_column 为低位 u16
/// rbrace_column 为高位 u16
columns: u32,
};
};
/// 尾随数据:
/// if (has_cc_ref and !has_cc_body) {
/// 0. cc: Ref,
/// }
/// if (has_cc_body) {
/// 1. cc_body_len: u32
/// 2. cc_body: u32 // 每个 cc_body_len 对应一个
/// }
/// if (has_ret_ty_ref and !has_ret_ty_body) {
/// 3. ret_ty: Ref,
/// }
/// if (has_ret_ty_body) {
/// 4. ret_ty_body_len: u32
/// 5. ret_ty_body: u32 // 每个 ret_ty_body_len 对应一个
/// }
/// 6. noalias_bits: u32 // 存在 has_any_noalias 时
/// - 从最低有效位开始,每一位对应一个参数索引
/// 7. body: Index // 每个 body_len 对应一个
/// 8. src_locs: Func.SrcLocs // body_len != 0 时
/// 9. proto_hash: std.zig.SrcHash // body_len != 0 时;函数原型的哈希值
pub const FuncFancy = struct {
/// 指向包含此函数参数指令的块。
/// 若这是 `declaration`,则引用该声明的值体。
param_block: Index,
body_len: u32,
bits: Bits,
/// 若 has_cc_ref 和 has_cc_body 均为 false,则表示自动调用约定。
/// 若 has_ret_ty_ref 和 has_ret_ty_body 均为 false,则表示 void 返回类型。
pub const Bits = packed struct(u32) {
is_var_args: bool,
is_inferred_error: bool,
is_noinline: bool,
has_cc_ref: bool,
has_cc_body: bool,
has_ret_ty_ref: bool,
has_ret_ty_body: bool,
has_any_noalias: bool,
ret_ty_is_generic: bool,
_: u23 = 0,
};
};
/// 此数据存储在 extra 中,后跟由 `operands_len` 指定的操作数。
/// 每个操作数均为 `Ref`。
pub const MultiOp = struct {
operands_len: u32,
};
/// 尾随:operand: Ref, // 对每个 `operands_len`(存储于 `small`)。
pub const NodeMultiOp = struct {
src_node: Ast.Node.Offset,
};
/// 此数据存储在 extra 中,根据 `body_len` 尾随相应数量的操作数。
/// 每个操作数均为 `Index`。
pub const Block = struct {
body_len: u32,
};
/// 尾随数据:
/// * 每个 `body_len` 对应一个 inst(索引)
pub const BlockComptime = struct {
reason: std.zig.SimpleComptimeReason,
body_len: u32,
};
/// 尾随数据:
/// * 每个 `body_len` 对应一个 inst(索引)
pub const BoolBr = struct {
lhs: Ref,
body_len: u32,
};
/// 尾随数据:
/// 0. name: NullTerminatedString // 若 `flags.id.hasName()`
/// 1. lib_name: NullTerminatedString // 若 `flags.id.hasLibName()`
/// 2. type_body_len: u32 // 若 `flags.id.hasTypeBody()`
/// 3. align_body_len: u32 // 若 `flags.id.hasSpecialBodies()`
/// 4. linksection_body_len: u32 // 若 `flags.id.hasSpecialBodies()`
/// 5. addrspace_body_len: u32 // 若 `flags.id.hasSpecialBodies()`
/// 6. value_body_len: u32 // 若 `flags.id.hasValueBody()`
/// 7. type_body_inst: Zir.Inst.Index
/// - 每个 `type_body_len` 对应一项
/// - 此主体通过 `break_inline` 退出并跳转到该 `declaration` 指令
/// 8. align_body_inst: Zir.Inst.Index
/// - 每个 `align_body_len` 对应一项
/// - 此主体通过 `break_inline` 退出并跳转到该 `declaration` 指令
/// 9. linksection_body_inst: Zir.Inst.Index
/// - 每个 `linksection_body_len` 对应一项
/// - 此主体通过 `break_inline` 退出并跳转到该 `declaration` 指令
/// 10. addrspace_body_inst: Zir.Inst.Index
/// - 每个 `addrspace_body_len` 对应一项
/// - 此主体通过 `break_inline` 退出并跳转到该 `declaration` 指令
/// 11. value_body_inst: Zir.Inst.Index
/// - 每个 `value_body_len` 对应一项
/// - 此主体通过 `break_inline` 退出并跳转到该 `declaration` 指令
/// - 在此主体内,`declaration` 指令表示类型主体已解析出的类型
pub const Declaration = struct {
// 应将这些字段连接后,重新解释为 `std.zig.SrcHash`。
src_hash_0: u32,
src_hash_1: u32,
src_hash_2: u32,
src_hash_3: u32,
// 应将这些字段连接后,重新解释为 `Flags`。
flags_0: u32,
flags_1: u32,
pub const Unwrapped = struct {
pub const Kind = enum {
unnamed_test,
@"test",
decltest,
@"comptime",
@"const",
@"var",
};
pub const Linkage = enum {
normal,
@"extern",
@"export",
};
src_node: Ast.Node.Index,
src_line: u32,
src_column: u32,
kind: Kind,
/// `kind` 为 `unnamed_test` 或 `.@"comptime"` 时,始终为 `.empty`
name: NullTerminatedString,
/// 当 `kind` 为 `unnamed_test`、`.@"test"`、`.decltest` 或 `.@"comptime"` 时,始终为 `false`。
is_pub: bool,
/// 始终为 `false`(当 `kind != .@"var"` 时)。
is_threadlocal: bool,
/// 对于 `kind != .@"const" and kind != .@"var"`,始终为 `.normal`。
linkage: Linkage,
/// 始终为 `.empty`(当 `linkage != .@"extern"` 时)。
lib_name: NullTerminatedString,
/// 当 `linkage == .@"extern"` 时始终填充。
type_body: ?[]const Inst.Index,
align_body: ?[]const Inst.Index,
linksection_body: ?[]const Inst.Index,
addrspace_body: ?[]const Inst.Index,
/// 当 `linkage != .@"extern"` 时始终填充。
value_body: ?[]const Inst.Index,
};
pub const Flags = packed struct(u64) {
src_line: u30,
src_column: u29,
id: Id,
pub const Id = enum(u5) {
unnamed_test,
@"test",
decltest,
@"comptime",
const_simple,
const_typed,
@"const",
pub_const_simple,
pub_const_typed,
pub_const,
extern_const_simple,
extern_const,
pub_extern_const_simple,
pub_extern_const,
export_const,
pub_export_const,
var_simple,
@"var",
var_threadlocal,
pub_var_simple,
pub_var,
pub_var_threadlocal,
extern_var,
extern_var_threadlocal,
pub_extern_var,
pub_extern_var_threadlocal,
export_var,
export_var_threadlocal,
pub_export_var,
pub_export_var_threadlocal,
pub fn hasName(id: Id) bool {
return switch (id) {
.unnamed_test,
.@"comptime",
=> false,
else => true,
};
}
pub fn hasLibName(id: Id) bool {
return switch (id) {
.extern_const,
.pub_extern_const,
.extern_var,
.extern_var_threadlocal,
.pub_extern_var,
.pub_extern_var_threadlocal,
=> true,
else => false,
};
}
pub fn hasTypeBody(id: Id) bool {
return switch (id) {
.unnamed_test,
.@"test",
.decltest,
.@"comptime",
=> false, // 这些构造没有类型
.const_simple,
.pub_const_simple,
.var_simple,
.pub_var_simple,
=> false, // 这些表示省略类型体
else => true,
};
}
pub fn hasValueBody(id: Id) bool {
return switch (id) {
.extern_const_simple,
.extern_const,
.pub_extern_const_simple,
.pub_extern_const,
.extern_var,
.extern_var_threadlocal,
.pub_extern_var,
.pub_extern_var_threadlocal,
=> false, // extern 不包含值
else => true,
};
}
pub fn hasSpecialBodies(id: Id) bool {
return switch (id) {
.unnamed_test,
.@"test",
.decltest,
.@"comptime",
=> false, // 这些构造没有类型
.const_simple,
.const_typed,
.pub_const_simple,
.pub_const_typed,
.extern_const_simple,
.pub_extern_const_simple,
.var_simple,
.pub_var_simple,
=> false, // 这些表示省略特殊主体
else => true,
};
}
pub fn linkage(id: Id) Declaration.Unwrapped.Linkage {
return switch (id) {
.extern_const_simple,
.extern_const,
.pub_extern_const_simple,
.pub_extern_const,
.extern_var,
.extern_var_threadlocal,
.pub_extern_var,
.pub_extern_var_threadlocal,
=> .@"extern",
.export_const,
.pub_export_const,
.export_var,
.export_var_threadlocal,
.pub_export_var,
.pub_export_var_threadlocal,
=> .@"export",
else => .normal,
};
}
pub fn kind(id: Id) Declaration.Unwrapped.Kind {
return switch (id) {
.unnamed_test => .unnamed_test,
.@"test" => .@"test",
.decltest => .decltest,
.@"comptime" => .@"comptime",
.const_simple,
.const_typed,
.@"const",
.pub_const_simple,
.pub_const_typed,
.pub_const,
.extern_const_simple,
.extern_const,
.pub_extern_const_simple,
.pub_extern_const,
.export_const,
.pub_export_const,
=> .@"const",
.var_simple,
.@"var",
.var_threadlocal,
.pub_var_simple,
.pub_var,
.pub_var_threadlocal,
.extern_var,
.extern_var_threadlocal,
.pub_extern_var,
.pub_extern_var_threadlocal,
.export_var,
.export_var_threadlocal,
.pub_export_var,
.pub_export_var_threadlocal,
=> .@"var",
};
}
pub fn isPub(id: Id) bool {
return switch (id) {
.pub_const_simple,
.pub_const_typed,
.pub_const,
.pub_extern_const_simple,
.pub_extern_const,
.pub_export_const,
.pub_var_simple,
.pub_var,
.pub_var_threadlocal,
.pub_extern_var,
.pub_extern_var_threadlocal,
.pub_export_var,
.pub_export_var_threadlocal,
=> true,
else => false,
};
}
pub fn isThreadlocal(id: Id) bool {
return switch (id) {
.var_threadlocal,
.pub_var_threadlocal,
.extern_var_threadlocal,
.pub_extern_var_threadlocal,
.export_var_threadlocal,
.pub_export_var_threadlocal,
=> true,
else => false,
};
}
};
};
pub const Name = enum(u32) {
@"comptime" = std.math.maxInt(u32),
unnamed_test = std.math.maxInt(u32) - 1,
/// 其他值均为 `NullTerminatedString` 值,即
/// `string_bytes` 的索引。若引用的字节为 0,则此声明为具名
/// 测试,实际名称从下一个字节开始。
_,
pub fn isNamedTest(name: Name, zir: Zir) bool {
return switch (name) {
.@"comptime", .unnamed_test => false,
_ => zir.string_bytes[@backingInt(name)] == 0,
};
}
pub fn toString(name: Name, zir: Zir) ?NullTerminatedString {
switch (name) {
.@"comptime", .unnamed_test => return null,
_ => {},
}
const idx: u32 = @backingInt(name);
if (zir.string_bytes[idx] == 0) {
// 具名测试
return @fromBackingInt(@intCast(idx + 1));
}
return @fromBackingInt(@intCast(idx));
}
};
pub const Bodies = struct {
type_body: ?[]const Index,
align_body: ?[]const Index,
linksection_body: ?[]const Index,
addrspace_body: ?[]const Index,
value_body: ?[]const Index,
};
pub fn getBodies(declaration: Declaration, extra_end: u32, zir: Zir) Bodies {
var extra_index: u32 = extra_end;
const value_body_len = declaration.value_body_len;
const type_body_len: u32 = len: {
if (!declaration.flags().kind.hasTypeBody()) break :len 0;
const len = zir.extra[extra_index];
extra_index += 1;
break :len len;
};
const align_body_len, const linksection_body_len, const addrspace_body_len = lens: {
if (!declaration.flags.kind.hasSpecialBodies()) {
break :lens .{ 0, 0, 0 };
}
const lens = zir.extra[extra_index..][0..3].*;
extra_index += 3;
break :lens lens;
};
return .{
.type_body = if (type_body_len == 0) null else b: {
const b = zir.bodySlice(extra_index, type_body_len);
extra_index += type_body_len;
break :b b;
},
.align_body = if (align_body_len == 0) null else b: {
const b = zir.bodySlice(extra_index, align_body_len);
extra_index += align_body_len;
break :b b;
},
.linksection_body = if (linksection_body_len == 0) null else b: {
const b = zir.bodySlice(extra_index, linksection_body_len);
extra_index += linksection_body_len;
break :b b;
},
.addrspace_body = if (addrspace_body_len == 0) null else b: {
const b = zir.bodySlice(extra_index, addrspace_body_len);
extra_index += addrspace_body_len;
break :b b;
},
.value_body = if (value_body_len == 0) null else b: {
const b = zir.bodySlice(extra_index, value_body_len);
extra_index += value_body_len;
break :b b;
},
};
}
};
/// 存储在 extra 中,尾随参数数量由 `args_len` 指定。
/// 隐含的第 0 项:arg_0_start: u32,// 始终与 `args_len` 相同
/// 1. arg_end: u32,// 每个 `args_len` 对应一项
/// arg_N_start 与 arg_N-1_end 相同。
pub const Call = struct {
// 注意:Flags *必须*位于最前面,
// 以便 unusedResultExpr 在修改时找到它。
flags: Flags,
callee: Ref,
pub const Flags = packed struct {
/// 打包形式的 std.lang.CallModifier
pub const PackedModifier = u3;
pub const PackedArgsLen = u27;
packed_modifier: PackedModifier,
ensure_result_used: bool = false,
pop_error_return_trace: bool,
args_len: PackedArgsLen,
comptime {
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
@compileError("Layout of Call.Flags needs to be updated!");
if (@bitSizeOf(std.lang.CallModifier) != @bitSizeOf(PackedModifier))
@compileError("Call.Flags.PackedModifier needs to be updated!");
}
};
};
/// 存储在 extra 中,尾随参数数量由 `args_len` 指定。
/// 隐含的第 0 项:arg_0_start: u32,// 始终与 `args_len` 相同
/// 1. arg_end: u32,// 每个 `args_len` 对应一项
/// arg_N_start 与 arg_N-1_end 相同。
pub const FieldCall = struct {
// 注意:Flags *必须*位于最前面,
// 以便 unusedResultExpr 在修改时找到它。
flags: Call.Flags,
obj_ptr: Ref,
/// `string_bytes` 中的偏移。
field_name_start: NullTerminatedString,
};
/// 指令主体位于 `extra[body_index..][0..body_len]`。
/// 尾随:
/// 0. operand: Ref // 每个 `operands_len` 对应一个
pub const TypeOfPeer = struct {
src_node: Ast.Node.Offset,
body_len: u32,
body_index: u32,
};
pub const BuiltinCall = struct {
// 注意:Flags *必须*位于最前面,
// 以便 unusedResultExpr 在修改时找到它。
flags: Flags,
modifier: Ref,
callee: Ref,
args: Ref,
pub const Flags = packed struct {
is_nosuspend: bool,
ensure_result_used: bool,
_: u30 = 0,
comptime {
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
@compileError("Layout of BuiltinCall.Flags needs to be updated!");
}
};
};
/// 此数据存储在 extra 中,后接两组 `Ref`:
/// * 0. then 分支体,长度由 `then_body_len` 指定。
/// * 1. else 分支体,长度由 `else_body_len` 指定。
pub const CondBr = struct {
condition: Ref,
then_body_len: u32,
else_body_len: u32,
};
/// 此数据存储在 extra 中,后接:
/// * 0. body: Index // 对每个 `body_len`
pub const Try = struct {
/// 要解包的错误联合。
operand: Ref,
body_len: u32,
};
/// 存储在 extra 中。根据 Data 中的标志,其后最多有 5 个 Ref 字段:
/// 0. sentinel: Ref // 若设置 `has_sentinel` 标志
/// 1. align: Ref // 若设置 `has_align` 标志
/// 2. address_space: Ref // 若设置 `has_addrspace` 标志
/// 3. bit_start: Ref // 若设置 `has_bit_range` 标志
/// 4. host_size: Ref // 若设置 `has_bit_range` 标志
///
pub const PtrType = struct {
elem_type: Ref,
src_node: Ast.Node.Offset,
};
pub const ArrayTypeSentinel = struct {
len: Ref,
sentinel: Ref,
elem_type: Ref,
};
pub const SliceStart = struct {
lhs: Ref,
start: Ref,
};
pub const SliceEnd = struct {
lhs: Ref,
start: Ref,
end: Ref,
};
pub const SliceSentinel = struct {
lhs: Ref,
start: Ref,
end: Ref,
sentinel: Ref,
};
pub const SliceLength = struct {
lhs: Ref,
start: Ref,
len: Ref,
sentinel: Ref,
start_src_node_offset: Ast.Node.Offset,
};
/// 这些操作数的含义取决于对应的 `Tag`。
pub const Bin = struct {
lhs: Ref,
rhs: Ref,
};
pub const BinNode = struct {
node: Ast.Node.Offset,
lhs: Ref,
rhs: Ref,
};
pub const ReifySliceArgInfo = enum(u16) {
/// 输入元素类型为 `type`。
/// 输出元素类型为 `std.lang.Type.Fn.ParamAttributes`。
type_to_fn_param_attrs,
/// 输入元素类型为 `[]const u8`。
/// 输出元素类型为 `type`。
string_to_struct_field_type,
/// 除生成的错误消息略有不同外,与 `string_to_struct_field_type` 完全相同。
string_to_union_field_type,
/// 输入元素类型为 `[]const u8`。
/// 输出元素类型为 `std.lang.Type.Struct.FieldAttributes`。
string_to_struct_field_attrs,
/// 输入元素类型为 `[]const u8`。
/// 输出元素类型为 `std.lang.Type.Union.FieldAttributes`。
string_to_union_field_attrs,
};
pub const RoundOp = enum(u16) {
round,
floor,
ceil,
trunc,
};
pub const UnNode = struct {
node: Ast.Node.Offset,
operand: Ref,
};
pub const ElemPtrImm = struct {
ptr: Ref,
index: u32,
};
pub const ReifyPointer = struct {
node: Ast.Node.Offset,
size: Ref,
attrs: Ref,
elem_ty: Ref,
sentinel: Ref,
};
pub const ReifyFn = struct {
node: Ast.Node.Offset,
param_types: Ref,
param_attrs: Ref,
ret_ty: Ref,
fn_attrs: Ref,
};
pub const ReifyStruct = struct {
src_line: u32,
src_column: u32,
/// 该节点使用绝对位置,因为 `reify` 指令会跨更新跟踪,
/// 这样可以简化获取类型源码位置的逻辑。
node: Ast.Node.Index,
layout: Ref,
backing_ty: Ref,
field_names: Ref,
field_types: Ref,
field_attrs: Ref,
};
pub const ReifyUnion = struct {
src_line: u32,
src_column: u32,
/// 该节点使用绝对位置,因为 `reify` 指令会跨更新跟踪,
/// 这样可以简化获取类型源码位置的逻辑。
node: Ast.Node.Index,
layout: Ref,
arg_ty: Ref,
field_names: Ref,
field_types: Ref,
field_attrs: Ref,
};
pub const ReifyEnum = struct {
src_line: u32,
src_column: u32,
/// 该节点使用绝对位置,因为 `reify` 指令会跨更新跟踪,
/// 这样可以简化获取类型源码位置的逻辑。
node: Ast.Node.Index,
tag_ty: Ref,
mode: Ref,
field_names: Ref,
field_values: Ref,
};
pub const ReifySpirvType = struct {
src_line: u32,
src_column: u32,
/// 该节点使用绝对位置,因为 `reify` 指令会跨更新跟踪,
/// 这样可以简化获取类型源码位置的逻辑。
node: Ast.Node.Index,
operand: Ref,
};
/// 尾随数据:
/// 0. multi_cases_len: u32, // 若已设置 has_multi_cases。
/// 1. payload_capture_placeholder: Inst.Index, // 若 payload_capture_inst_is_placeholder 已设置。
/// // 指令分支引用其载荷捕获时使用的索引。
/// 2. tag_capture_placeholder: Inst.Index, // 若 tag_capture_inst_is_placeholder 已设置。
/// // 指令分支引用其标签捕获时使用的索引。
/// 3. catch_or_if_src_node_offset: Ast.Node.Offset, // 若 inst 为 switch_block_err_union。
/// 4. non_err_info: ProngInfo.NonErr, // 若 inst 为 switch_block_err_union。
/// 5. else_info: ProngInfo.Else, // 若已设置 has_else。
/// 6. scalar_prong_info: ProngInfo, // 对每个 scalar_cases_len
/// 7. multi_prong_info: ProngInfo, // 对每个 multi_cases_len
/// 8. multi_case_items_len: u32, // 对每个 multi_cases_len
/// 9. multi_case_ranges_len: u32, // 若已设置 has_ranges:对每个 multi_cases_len
/// 10. scalar_item_info: ItemInfo, // 对每个 scalar_cases_len
/// 11. multi_items_info: { // 对每个 multi_cases_len
/// item_info: ItemInfo, // 对每个 multi_case_items_len
/// range_items_info: { // 对每个 multi_case_ranges_len
/// first_info: ItemInfo,
/// last_info: ItemInfo,
/// }
/// }
/// 12. non_err_body {
/// body_inst: Index // 对每个 non_err_info.body_len
/// }
/// 13. else_body: { // 若已设置 has_else。
/// body_inst: Inst.Index, // 对每个 else_info.body_len
/// }
/// 14. scalar_bodies: { // 对每个 scalar_cases_len
/// prong_body: { // 对 scalar_prong_info 中的每个 body_len
/// body_inst: Inst.Index, // 对每个 body_len
/// }
/// item_body: { // 对 scalar_item_info 中的每个 body_len
/// body_inst: Inst.Index, // 对每个 body_len
/// }
/// }
/// 15. multi_bodies: { // 对 multi_items_info 中的每一项
/// prong_body: {
/// body_inst: Inst.Index, // 对 multi_prong_info.body_len 中的每个值
/// }
/// item_body: { // 对每个 item_info
/// body_inst: Inst.Index, // 对每个 item_info.body_len
/// }
/// range_bodies: { // 对 range_items_info 中的每个 .{first_info, last_info}
/// first_body_inst: Inst.Index, // 对每个 first_info.body_len
/// last_body_inst: Inst.Index, // 对每个 last_info.body_len
/// }
/// }
pub const SwitchBlock = struct {
/// `catch`/`if` 或 `switch` 操作数。
raw_operand: Ref,
bits: Bits,
pub const Bits = packed struct(u32) {
/// 若为 true,则一个或多个分支包含多个项。
has_multi_cases: bool,
/// 若为 true,则一个或多个分支包含范围。
/// 仅当同时设置 `has_multi_cases` 时有效。
any_ranges: bool,
has_else: bool,
has_under: bool,
/// 为 true 时,至少一个分支包含 `continue`。
/// 仅在设置 `has_label` 时有效。
has_continue: bool,
// 若为 true,则至少一个分支具有非内联载荷或标签捕获。
any_maybe_runtime_capture: bool,
payload_capture_inst_is_placeholder: bool,
tag_capture_inst_is_placeholder: bool,
scalar_cases_len: ScalarCasesLen,
// 注意,也许不应再从可怜的 `scalar_cases_len` 窃取位,
// 而应将 `Bits` 拆成两部分;`raw_operand` 想必
// 不会介意为此贡献几位……
pub const ScalarCasesLen = u24;
};
pub const ProngInfo = packed struct(u32) {
body_len: u27,
capture: ProngInfo.Capture,
is_inline: bool,
has_tag_capture: bool,
is_comptime_unreach: bool,
pub const Capture = enum(u2) {
none,
by_val,
by_ref,
};
pub const NonErr = packed struct(u32) {
body_len: u29,
capture: ProngInfo.Capture,
operand_is_ref: bool,
};
pub const Else = packed struct(u32) {
body_len: u27,
capture: ProngInfo.Capture,
is_inline: bool,
has_tag_capture: bool,
is_simple_noreturn: bool,
};
};
pub const ItemInfo = packed struct(u32) {
kind: ItemInfo.Kind,
data: u30,
pub const Kind = enum(u2) {
enum_literal,
error_value,
body_len,
under,
};
pub const Unwrapped = union(ItemInfo.Kind) {
enum_literal: Zir.NullTerminatedString,
error_value: Zir.NullTerminatedString,
body_len: u32,
under,
};
pub fn wrap(unwrapped: ItemInfo.Unwrapped) ItemInfo {
const data_uncasted: u32 = switch (unwrapped) {
.enum_literal => |str_index| @backingInt(str_index),
.error_value => |str_index| @backingInt(str_index),
.body_len => |body_len| body_len,
.under => 0,
};
return .{ .kind = unwrapped, .data = @intCast(data_uncasted) };
}
pub fn unwrap(item_info: ItemInfo) ItemInfo.Unwrapped {
return switch (item_info.kind) {
.enum_literal => .{ .enum_literal = @fromBackingInt(@intCast(item_info.data)) },
.error_value => .{ .error_value = @fromBackingInt(@intCast(item_info.data)) },
.body_len => .{ .body_len = item_info.data },
.under => .under,
};
}
pub fn bodyLen(item_info: ItemInfo) ?u32 {
return if (item_info.kind == .body_len) item_info.data else null;
}
};
};
pub const ArrayInitRefTy = struct {
ptr_ty: Ref,
elem_count: u32,
};
pub const Field = struct {
lhs: Ref,
/// `string_bytes` 中的偏移。
field_name_start: NullTerminatedString,
};
pub const FieldNamed = struct {
lhs: Ref,
field_name: Ref,
};
pub const As = struct {
dest_type: Ref,
operand: Ref,
};
/// 尾随数据:
/// 0. captures_len: u32 // 存在 `has_captures_len` 时
/// 1. decls_len: u32, // 存在 `has_decls_len` 时
/// 2. fields_len: u32, // 存在 `has_fields_len` 时
/// 3. arg_baseline_src_node: Ast.Node.Index // 存在 `has_backing_int` 时
/// 4. fields_baseline_src_node: Ast.Node.Index // 存在 `has_fields_len` 时
/// 5. backing_int_body_len: u32 // 存在 `has_backing_int` 时
/// 6. capture: Capture // 每个 `captures_len` 对应一个
/// 7. capture_name: NullTerminatedString // 每个 `captures_len` 对应一个
/// 8. decl: Index, // 每个 `decls_len` 对应一个;指向 `declaration` 指令
/// 9. field_name: NullTerminatedString // 每个 `fields_len` 对应一个
/// 10. field_type_body_len: u32 // 每个 `fields_len` 对应一个
/// 11. field_align_body_len: u32 // 存在 `fields_len` 且 `any_field_aligns` 时,每个字段对应一个
/// 12. field_default_body_len: u32 // 存在 `fields_len` 且 `any_field_defaults` 时,每个字段对应一个
/// 13. field_comptime_bits: u32 // 存在 `fields_len` 且 `any_comptime_fields` 时,每个字段对应一位
/// // 最低有效位对应第一个字段;使用最少数量的 `u32`
/// 14. backing_int_body_inst: Inst.Index // 每个 `backing_int_body_len` 对应一个
/// 15. body_inst: Inst.Index // 逐字段保存类型体、对齐体,最后是默认值体
pub const StructDecl = struct {
// 这些字段应拼接并重新解释为 `std.zig.SrcHash`。
// 此哈希包含所有字段的源码以及任何指定属性(`extern`、底层类型等)。
fields_hash_0: u32,
fields_hash_1: u32,
fields_hash_2: u32,
fields_hash_3: u32,
src_line: u32,
src_column: u32,
/// 此节点为该结构体内的全部指令提供新的绝对基准节点。
src_node: Ast.Node.Index,
pub const Small = packed struct(u16) {
has_captures_len: bool,
has_decls_len: bool,
has_fields_len: bool,
name_strategy: NameStrategy,
layout: std.lang.Type.ContainerLayout,
/// 始终为 `false`(当 `layout != .@"packed"` 时)。
has_backing_int_type: bool,
any_field_aligns: bool,
any_field_defaults: bool,
any_comptime_fields: bool,
_: u5 = 0,
};
};
/// 表示类型声明的闭包所捕获的单个值。
pub const Capture = packed struct(u32) {
tag: enum(u3) {
/// `data` 是父闭包中的 `u16` 索引。
nested,
/// `data` 是 `Zir.Inst.Index`,指向其值正在被捕获的指令。
instruction,
/// `data` 是一个 `Zir.Inst.Index`,指向表示分配操作的指令,其分配内容正在被捕获。
instruction_load,
/// `data` 是引用声明名称的 `NullTerminatedString`。
decl_val,
/// `data` 是引用声明名称的 `NullTerminatedString`。
decl_ref,
},
data: u29,
pub const Unwrapped = union(enum) {
nested: u16,
instruction: Zir.Inst.Index,
instruction_load: Zir.Inst.Index,
decl_val: NullTerminatedString,
decl_ref: NullTerminatedString,
};
pub fn wrap(cap: Unwrapped) Capture {
return switch (cap) {
.nested => |idx| .{
.tag = .nested,
.data = idx,
},
.instruction => |inst| .{
.tag = .instruction,
.data = @intCast(@backingInt(inst)),
},
.instruction_load => |inst| .{
.tag = .instruction_load,
.data = @intCast(@backingInt(inst)),
},
.decl_val => |str| .{
.tag = .decl_val,
.data = @intCast(@backingInt(str)),
},
.decl_ref => |str| .{
.tag = .decl_ref,
.data = @intCast(@backingInt(str)),
},
};
}
pub fn unwrap(cap: Capture) Unwrapped {
return switch (cap.tag) {
.nested => .{ .nested = @intCast(cap.data) },
.instruction => .{ .instruction = @fromBackingInt(@intCast(cap.data)) },
.instruction_load => .{ .instruction_load = @fromBackingInt(@intCast(cap.data)) },
.decl_val => .{ .decl_val = @fromBackingInt(@intCast(cap.data)) },
.decl_ref => .{ .decl_ref = @fromBackingInt(@intCast(cap.data)) },
};
}
};
pub const NameStrategy = enum(u2) {
/// 使用与父声明相同的名称。
/// 例如 `const Foo = struct {...};`。
parent,
/// 使用当前执行的编译期函数调用名称及当前参数,
/// 例如 `ArrayList(i32)`。
func,
/// 为此声明创建匿名名称。
/// 例如:"ParentDeclName_struct_69"
anon,
/// 使用下一条 `dbg_var_{val,ptr}` 指令指定的名称。
dbg_var,
};
pub const FullPtrCastFlags = packed struct(u5) {
ptr_cast: bool = false,
align_cast: bool = false,
addrspace_cast: bool = false,
const_cast: bool = false,
volatile_cast: bool = false,
pub inline fn needResultTypeBuiltinName(flags: FullPtrCastFlags) []const u8 {
if (flags.ptr_cast) return "@ptrCast";
if (flags.align_cast) return "@alignCast";
if (flags.addrspace_cast) return "@addrSpaceCast";
unreachable;
}
};
pub const StdLangValue = enum(u16) {
// 类型
atomic_order,
atomic_rmw_op,
calling_convention,
address_space,
float_mode,
signedness,
reduce_op,
call_modifier,
prefetch_options,
export_options,
extern_options,
branch_hint,
clobbers,
pointer_size,
pointer_attributes,
fn_attributes,
container_layout,
enum_mode,
spirv_type_options,
// 值
calling_convention_c,
calling_convention_inline,
};
pub const InplaceOp = enum(u16) {
add_eq,
sub_eq,
};
/// 尾随内容:
/// 0. captures_len: u32, // 如果 has_captures_len 为真
/// 1. decls_len: u32, // 如果 has_decls_len 为真
/// 2. fields_len: u32, // 如果 has_fields_len 为真
/// 3. tag_type_body_len: u32, // 如果 has_tag_type 为真
/// 4. capture: Capture // 每个 `captures_len` 对应一个
/// 5. capture_name: NullTerminatedString // 每个 `captures_len` 对应一个
/// 6. decl: Index, // 每个 `decls_len` 对应一个;指向 `declaration` 指令
/// 7. field_name: NullTerminatedString // 每个 `fields_len` 对应一个
/// 8. field_value_body_len: u32 // 如果 `any_field_values` 为真,则每个 `fields_len` 对应一个
/// 9. tag_type_body_inst: Inst.Index // 每个 `tag_type_body_len` 对应一个
/// 10. body_inst: Inst.Index // 每个字段的值表达式体
pub const EnumDecl = struct {
// 应将这些字段连接并重新解释为 `std.zig.SrcHash`。
// 此哈希包含所有字段的源码,以及显式指定的后备类型。
fields_hash_0: u32,
fields_hash_1: u32,
fields_hash_2: u32,
fields_hash_3: u32,
src_line: u32,
src_column: u32,
/// 此节点为该结构体内的全部指令提供新的绝对基准节点。
src_node: Ast.Node.Index,
pub const Small = packed struct(u16) {
has_captures_len: bool,
has_decls_len: bool,
has_fields_len: bool,
name_strategy: NameStrategy,
has_tag_type: bool,
nonexhaustive: bool,
any_field_values: bool,
_: u8 = 0,
};
};
/// 尾随数据:
/// 0. captures_len: u32 // 若 `has_captures_len`
/// 1. decls_len: u32, // 若 `has_decls_len`
/// 2. fields_len: u32, // 若 `has_fields_len`
/// 3. arg_type_body_len: u32, // 若 `kind.hasArgType()`
/// 4. capture: Capture // 每个 `captures_len` 对应一项
/// 5. capture_name: NullTerminatedString // 每个 `captures_len` 对应一项
/// 6. decl: Index, // 每个 `decls_len` 对应一项;指向 `declaration` 指令
/// 7. field_name: NullTerminatedString // 每个 `fields_len` 对应一项
/// 8. field_type_body_len: u32 // 每个 `fields_len` 对应一项
/// 9 . field_align_body_len: u32 // 每个 `fields_len` 对应一项,若 `any_field_aligns`
/// 10. field_value_body_len: u32 // 每个 `fields_len` 对应一项,若 `any_field_values`
/// 11. arg_type_body_inst: Inst.Index // 每个 `arg_type_body_len` 对应一项
/// 12. body_inst: Inst.Index // 对每个字段,依次为类型主体、对齐主体和值主体
pub const UnionDecl = struct {
// 应将这些字段拼接并重新解释为 `std.zig.SrcHash`。
// 此哈希包含所有字段的源代码以及指定属性(如 `extern`)。
fields_hash_0: u32,
fields_hash_1: u32,
fields_hash_2: u32,
fields_hash_3: u32,
src_line: u32,
src_column: u32,
/// 此节点为该结构体内的全部指令提供新的绝对基准节点。
src_node: Ast.Node.Index,
pub const Small = packed struct(u16) {
has_captures_len: bool,
has_decls_len: bool,
has_fields_len: bool,
name_strategy: NameStrategy,
kind: Kind,
any_field_aligns: bool,
any_field_values: bool,
_: u6 = 0,
};
pub const Kind = enum(u3) {
/// `union`
auto,
/// `union(T)`
tagged_explicit,
/// `union(enum)`
tagged_enum,
/// `union(enum(T))`
tagged_enum_explicit,
/// `extern union`
@"extern",
/// `packed union`
@"packed",
/// `packed union(T)`
packed_explicit,
pub fn hasArgType(k: Kind) bool {
return switch (k) {
.auto, .tagged_enum, .@"extern", .@"packed" => false,
.tagged_explicit, .tagged_enum_explicit, .packed_explicit => true,
};
}
pub fn layout(k: Kind) std.lang.Type.ContainerLayout {
return switch (k) {
.auto, .tagged_explicit, .tagged_enum, .tagged_enum_explicit => .auto,
.@"extern" => .@"extern",
.@"packed", .packed_explicit => .@"packed",
};
}
};
};
/// 尾随数据:
/// 0. captures_len: u32, // 若 has_captures_len
/// 1. decls_len: u32, // 若 has_decls_len
/// 2. capture: Capture, // 每个 captures_len 对应一项
/// 3. capture_name: NullTerminatedString // 每个 captures_len 对应一项
/// 4. decl: Index, // 每个 decls_len 对应一项;指向 `declaration` 指令
pub const OpaqueDecl = struct {
src_line: u32,
src_column: u32,
/// 此节点为该结构体内的全部指令提供新的绝对基准节点。
src_node: Ast.Node.Index,
pub const Small = packed struct(u16) {
has_captures_len: bool,
has_decls_len: bool,
name_strategy: NameStrategy,
_: u12 = 0,
};
};
/// 尾随:
/// 1. fields: { // 每个 `fields_len`(存储在 `extended.small` 中)
/// type: Inst.Ref,
/// init: Inst.Ref, // 非 `comptime` 字段为 `.none`
/// }
pub const TupleDecl = struct {
src_node: Ast.Node.Offset,
};
/// 尾随:
/// 0. field_name: NullTerminatedString // 对每个 fields_len
pub const ErrorSetDecl = struct {
fields_len: u32,
};
/// 将 f128 值拆成 4 个 u32 部分。
pub const Float128 = struct {
piece0: u32,
piece1: u32,
piece2: u32,
piece3: u32,
pub fn get(self: Float128) f128 {
const int_bits = @as(u128, self.piece0) |
(@as(u128, self.piece1) << 32) |
(@as(u128, self.piece2) << 64) |
(@as(u128, self.piece3) << 96);
return @as(f128, @bitCast(int_bits));
}
};
/// 尾随数据中每个字段对应一项。
pub const StructInit = struct {
/// 若为匿名初始化(操作数为 poison),则此指令成为类型的所有者。
/// 要解析源码位置,需要绝对源码节点。
abs_node: Ast.Node.Index,
src_line: u32,
src_column: u32,
fields_len: u32,
pub const Item = struct {
/// 此字段初始化对应的 `struct_init_field_type` ZIR 指令。
field_type: Index,
/// 用作字段值的字段初始化表达式。
/// 若其类型尚不匹配字段类型,则执行强制转换。
init: Ref,
};
};
/// 尾随数据为每个字段对应的 Item。
/// TODO: 改为初始化值数组后跟名称数组,因为
/// 这样可简化 Sema 代码并改善处理器缓存利用。
pub const StructInitAnon = struct {
/// 这是匿名初始化,意味着该指令成为一个类型的所有者。
/// 解析源码位置需要绝对源码节点。
abs_node: Ast.Node.Index,
src_line: u32,
src_column: u32,
fields_len: u32,
pub const Item = struct {
/// 以空字符终止的字符串表索引。
field_name: NullTerminatedString,
/// 用作字段值的字段初始化表达式。
init: Ref,
};
};
pub const FieldType = struct {
container_type: Ref,
/// 在 `string_bytes` 中的偏移量,以null 结尾。
name_start: NullTerminatedString,
};
pub const FieldTypeRef = struct {
container_type: Ref,
field_name: Ref,
};
pub const Cmpxchg = struct {
node: Ast.Node.Offset,
ptr: Ref,
expected_value: Ref,
new_value: Ref,
success_order: Ref,
failure_order: Ref,
};
pub const AtomicRmw = struct {
ptr: Ref,
operation: Ref,
operand: Ref,
ordering: Ref,
};
pub const UnionInit = struct {
union_type: Ref,
field_name: Ref,
init: Ref,
};
pub const AtomicStore = struct {
ptr: Ref,
operand: Ref,
ordering: Ref,
};
pub const AtomicLoad = struct {
elem_type: Ref,
ptr: Ref,
ordering: Ref,
};
pub const MulAdd = struct {
mulend1: Ref,
mulend2: Ref,
addend: Ref,
};
pub const FieldParentPtr = struct {
src_node: Ast.Node.Offset,
parent_ptr_type: Ref,
field_name: Ref,
field_ptr: Ref,
};
pub const Shuffle = struct {
elem_type: Ref,
a: Ref,
b: Ref,
mask: Ref,
};
pub const Select = struct {
node: Ast.Node.Offset,
elem_type: Ref,
pred: Ref,
a: Ref,
b: Ref,
};
/// 尾随内容:inst: Index // 每个 body_len 项对应一个
pub const Param = struct {
/// 以 null 结尾的字符串的索引。
name: NullTerminatedString,
type: Type,
pub const Type = packed struct(u32) {
/// 主体包含参数类型。
body_len: u31,
/// 该类型是否为泛型,即引用一个或多个先前的参数。
is_generic: bool,
};
};
/// 尾随内容:
/// 0. type_inst: Ref, // 如果设置了 small 0b000X
/// 1. align_inst: Ref, // 如果设置了 small 0b00X0
pub const AllocExtended = struct {
src_node: Ast.Node.Offset,
pub const Small = packed struct(u16) {
has_type: bool,
has_align: bool,
is_const: bool,
is_comptime: bool,
_: u12 = 0,
};
};
pub const Export = struct {
exported: Ref,
options: Ref,
};
/// 尾随:每个 `items_len` 对应一个 `CompileErrors.Item`。
pub const CompileErrors = struct {
items_len: u32,
/// 尾随内容:每个 `notes_len` 对应一个 `note_payload_index: u32`。
/// 它是另一个 `Item` 的载荷索引。
pub const Item = struct {
/// 以空字符结尾的字符串索引
msg: NullTerminatedString,
node: Ast.Node.OptionalIndex,
/// 若 node 为 .none,则填充此字段。
token: Ast.OptionalTokenIndex,
/// 可与 `token` 组合使用。
byte_offset: u32,
/// 0,或指向 `Block` 的载荷索引,
/// 其中每一项均为指向另一个 `Item` 的载荷索引。
notes: u32,
pub fn notesLen(item: Item, zir: Zir) u32 {
if (item.notes == 0) return 0;
const block = zir.extraData(Block, item.notes);
return block.data.body_len;
}
};
};
/// 尾随数据:每个 `imports_len` 对应一个 Item
pub const Imports = struct {
imports_len: u32,
pub const Item = struct {
/// 以空字符结尾的字符串索引
name: NullTerminatedString,
/// 指向导入名称
token: Ast.TokenIndex,
};
};
pub const LineColumn = struct {
line: u32,
column: u32,
};
pub const ArrayInit = struct {
ty: Ref,
init_count: u32,
};
pub const Src = struct {
node: Ast.Node.Offset,
line: u32,
column: u32,
};
pub const ValidateDestructure = struct {
/// 正在解构的值。
operand: Ref,
/// `destructure_assign` 节点。
destructure_node: Ast.Node.Offset,
/// 预期字段数。
expect_len: u32,
};
pub const RestoreErrRetIndex = struct {
src_node: Ast.Node.Offset,
/// 若为 `.none`,将跟踪恢复至函数进入时的状态。
block: Ref,
/// 若为 `.none`,则无条件恢复。
operand: Ref,
};
pub const Import = struct {
/// 导入结果的类型;若不存在,则为 `.none`。
res_ty: Ref,
/// 导入路径。
path: NullTerminatedString,
};
};
/// `DeclContents` 包含 `findTrackable` 在声明内找到的全部“值得关注”的指令。
/// 这些指令划分为若干不同集合,因为这能使 ZIR 指令映射
/// 更加有效。
pub const DeclContents = struct {
/// 这里只需使用简单的可选值,因为 ZIR 保证每个 `declaration` 中
/// `func`/`func_inferred`/`func_fancy` 指令最多出现一次。
func_decl: ?Inst.Index,
type_decls: std.ArrayList(Inst.Index),
other: std.ArrayList(Inst.Index),
pub const init: DeclContents = .{
.func_decl = null,
.type_decls = .empty,
.other = .empty,
};
pub fn clear(contents: *DeclContents) void {
contents.func_decl = null;
contents.type_decls.clearRetainingCapacity();
contents.other.clearRetainingCapacity();
}
pub fn deinit(contents: *DeclContents, gpa: Allocator) void {
contents.type_decls.deinit(gpa);
contents.other.deinit(gpa);
}
};
/// 递归查找一条 `declaration` 指令内所有受跟踪的 ZIR 指令。不递归进入
/// 嵌套声明;要查找所有声明,请对 `contents.type_decls` 中发现的
/// 类型声明递归调用此函数。
///
/// 结果填入 `ArrayList`,因为即使使用迭代器也仍需分配内存。
pub fn findTrackable(zir: Zir, gpa: Allocator, contents: *DeclContents, decl_inst: Zir.Inst.Index) !void {
contents.clear();
const decl = zir.getDeclaration(decl_inst);
// `defer` 指令可能任意多次复制同一主体,但每个 defer 的内容只需遍历一次。
// 因此,此处存储主体的额外索引,用于去重。
var found_defers: std.AutoHashMapUnmanaged(u32, void) = .empty;
defer found_defers.deinit(gpa);
if (decl.type_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (decl.align_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (decl.linksection_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (decl.addrspace_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (decl.value_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
}
/// `findTrackable` 不会递归进入类型内的字段表达式。此函数则专门
/// 扫描给定类型声明中的字段表达式,查找可跟踪的 ZIR 指令。
pub fn findTrackableFields(
zir: *const Zir,
gpa: Allocator,
contents: *DeclContents,
type_decl_inst: Zir.Inst.Index,
) Allocator.Error!void {
contents.clear();
var found_defers: std.AutoHashMapUnmanaged(u32, void) = .empty;
defer found_defers.deinit(gpa);
assert(zir.instructions.items(.tag)[@backingInt(type_decl_inst)] == .extended);
switch (zir.instructions.items(.data)[@backingInt(type_decl_inst)].extended.opcode) {
.struct_decl => {
const struct_decl = zir.getStructDecl(type_decl_inst);
var it = struct_decl.iterateFields();
while (it.next()) |field| {
try zir.findTrackableBody(gpa, contents, &found_defers, field.type_body);
if (field.align_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (field.default_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
}
},
.union_decl => {
const union_decl = zir.getUnionDecl(type_decl_inst);
var it = union_decl.iterateFields();
while (it.next()) |field| {
if (field.type_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (field.align_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
if (field.value_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
}
},
.enum_decl => {
const enum_decl = zir.getEnumDecl(type_decl_inst);
var it = enum_decl.iterateFields();
while (it.next()) |field| {
if (field.value_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
}
},
.opaque_decl => {},
else => unreachable,
}
}
fn findTrackableInner(
zir: Zir,
gpa: Allocator,
contents: *DeclContents,
defers: *std.AutoHashMapUnmanaged(u32, void),
inst: Inst.Index,
) Allocator.Error!void {
comptime assert(Zir.inst_tracking_version == 0);
const tags = zir.instructions.items(.tag);
const datas = zir.instructions.items(.data);
switch (tags[@backingInt(inst)]) {
.declaration => unreachable,
// 首先是普通指令标签。它们没有体,也不是声明或类型声明。
.add,
.addwrap,
.add_sat,
.add_unsafe,
.sub,
.subwrap,
.sub_sat,
.mul,
.mulwrap,
.mul_sat,
.div_exact,
.div_floor,
.div_ceil,
.div_trunc,
.mod,
.rem,
.mod_rem,
.shl,
.shl_exact,
.shl_sat,
.shr,
.shr_exact,
.param_anytype,
.param_anytype_comptime,
.array_cat,
.array_type,
.array_type_sentinel,
.reify_int,
.vector_type,
.elem_type,
.indexable_ptr_elem_type,
.splat_op_result_ty,
.from_backing_int_arg_ty,
.indexable_ptr_len,
.anyframe_type,
.as_node,
.as_shift_operand,
.bit_and,
.bitcast,
.bit_not,
.bit_or,
.bool_not,
.bool_br_and,
.bool_br_or,
.@"break",
.break_inline,
.switch_continue,
.check_comptime_control_flow,
.builtin_call,
.cmp_lt,
.cmp_lte,
.cmp_eq,
.cmp_gte,
.cmp_gt,
.cmp_neq,
.error_set_decl,
.dbg_stmt,
.dbg_var_ptr,
.dbg_var_val,
.decl_ref,
.decl_val,
.load,
.div,
.elem_ptr_node,
.elem_ptr,
.elem_ptr_load,
.elem_val,
.elem_val_imm,
.ensure_result_used,
.ensure_result_non_error,
.ensure_err_union_payload_void,
.error_union_type,
.error_value,
.@"export",
.field_ptr,
.field_ptr_load,
.field_ptr_named,
.field_ptr_named_load,
.import,
.int,
.int_big,
.float,
.float128,
.int_type,
.is_non_null,
.is_non_null_ptr,
.is_non_err,
.is_non_err_ptr,
.ret_is_non_err,
.repeat,
.repeat_inline,
.for_len,
.merge_error_sets,
.ref,
.deref,
.ref_deref,
.ret_node,
.ret_load,
.ret_implicit,
.ret_err_value,
.ret_ptr,
.ret_type,
.ptr_type,
.slice_start,
.slice_end,
.slice_sentinel,
.slice_length,
.slice_sentinel_ty,
.store_node,
.store_to_inferred_ptr,
.str,
.negate,
.negate_wrap,
.typeof,
.typeof_log2_int_type,
.@"unreachable",
.xor,
.optional_type,
.optional_payload_safe,
.optional_payload_unsafe,
.optional_payload_safe_ptr,
.optional_payload_unsafe_ptr,
.err_union_payload_unsafe,
.err_union_payload_unsafe_ptr,
.err_union_code,
.err_union_code_ptr,
.enum_literal,
.decl_literal,
.decl_literal_no_coerce,
.validate_destructure,
.field_type_ref,
.opt_eu_base_ptr_init,
.coerce_ptr_elem_ty,
.validate_ref_ty,
.validate_const,
.struct_init_empty,
.struct_init_empty_result,
.struct_init_empty_ref_result,
.validate_struct_init_ty,
.validate_struct_init_result_ty,
.validate_ptr_struct_init,
.struct_init_field_type,
.struct_init_field_ptr,
.array_init_anon,
.array_init,
.array_init_ref,
.validate_array_init_ty,
.validate_array_init_result_ty,
.validate_array_init_ref_ty,
.validate_ptr_array_init,
.array_init_elem_type,
.array_init_elem_ptr,
.union_init,
.type_info,
.size_of,
.bit_size_of,
.int_from_ptr,
.compile_error,
.set_eval_branch_quota,
.int_from_enum,
.align_of,
.int_from_bool,
.embed_file,
.error_name,
.panic,
.trap,
.set_runtime_safety,
.sqrt,
.sin,
.cos,
.tan,
.exp,
.exp2,
.log,
.log2,
.log10,
.abs,
.floor,
.ceil,
.trunc,
.round,
.tag_name,
.type_name,
.frame_type,
.int_from_float,
.float_from_int,
.ptr_from_int,
.enum_from_int,
.backing_int,
.from_backing_int,
.float_cast,
.int_cast,
.ptr_cast,
.truncate,
.has_decl,
.has_field,
.clz,
.ctz,
.pop_count,
.byte_swap,
.bit_reverse,
.bit_offset_of,
.offset_of,
.splat,
.reduce,
.shuffle,
.atomic_load,
.atomic_rmw,
.atomic_store,
.mul_add,
.memcpy,
.memset,
.memmove,
.min,
.max,
.alloc,
.alloc_mut,
.alloc_comptime_mut,
.alloc_inferred,
.alloc_inferred_mut,
.alloc_inferred_comptime,
.alloc_inferred_comptime_mut,
.resolve_inferred_alloc,
.make_ptr_const,
.@"resume",
.save_err_ret_index,
.restore_err_ret_index_unconditional,
.restore_err_ret_index_fn_entry,
=> return,
// 结构体初始化需要跟踪,因为可能创建匿名结构体类型。
.struct_init,
.struct_init_ref,
.struct_init_anon,
=> return contents.other.append(gpa, inst),
.extended => {
const extended = datas[@backingInt(inst)].extended;
switch (extended.opcode) {
.value_placeholder => unreachable,
// 仍然从普通标签开始处理。
.this,
.ret_addr,
.builtin_src,
.error_return_trace,
.frame,
.frame_address,
.alloc,
.builtin_extern,
.@"asm",
.asm_expr,
.compile_log,
.min_multi,
.max_multi,
.add_with_overflow,
.sub_with_overflow,
.mul_with_overflow,
.shl_with_overflow,
.round_op,
.wasm_memory_size,
.wasm_memory_grow,
.prefetch,
.set_float_mode,
.error_cast,
.breakpoint,
.disable_instrumentation,
.disable_intrinsics,
.select,
.int_from_error,
.error_from_int,
.reify_slice_arg_ty,
.reify_enum_value_slice_ty,
.reify_pointer_sentinel_ty,
.reify_tuple,
.reify_pointer,
.reify_fn,
.cmpxchg,
.c_va_arg,
.c_va_copy,
.c_va_end,
.c_va_start,
.ptr_cast_full,
.ptr_cast_no_dest,
.work_item_id,
.work_group_size,
.work_group_id,
.in_comptime,
.restore_err_ret_index,
.closure_get,
.field_parent_ptr,
.std_lang_value,
.branch_hint,
.inplace_arith_result_ty,
.tuple_decl,
.dbg_empty_stmt,
.astgen_error,
.float_op_result_ty,
.round_op_ty,
=> return,
// `@TypeOf` 带有主体。
.typeof_peer => {
const extra = zir.extraData(Zir.Inst.TypeOfPeer, extended.operand);
const body = zir.bodySlice(extra.data.body_index, extra.data.body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
},
// 具体化需要跟踪。
.reify_enum,
.reify_struct,
.reify_union,
.reify_spirv_type,
=> return contents.other.append(gpa, inst),
// 需要跟踪类型声明。
.struct_decl,
.union_decl,
.enum_decl,
.opaque_decl,
=> return contents.type_decls.append(gpa, inst),
}
},
// 函数指令属于需要关注的指令,并且具有函数体。
.func,
.func_inferred,
=> {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.Func, inst_data.payload_index);
if (extra.data.body_len == 0) {
// 这只是原型,无需跟踪。
assert(extra.data.ret_ty.body_len < 2);
return;
}
assert(contents.func_decl == null);
contents.func_decl = inst;
var extra_index: usize = extra.end;
switch (extra.data.ret_ty.body_len) {
0 => {},
1 => extra_index += 1,
else => {
const body = zir.bodySlice(extra_index, extra.data.ret_ty.body_len);
extra_index += body.len;
try zir.findTrackableBody(gpa, contents, defers, body);
},
}
const body = zir.bodySlice(extra_index, extra.data.body_len);
return zir.findTrackableBody(gpa, contents, defers, body);
},
.func_fancy => {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index);
if (extra.data.body_len == 0) {
// 这只是原型,无需跟踪。
assert(!extra.data.bits.has_cc_body);
assert(!extra.data.bits.has_ret_ty_body);
return;
}
assert(contents.func_decl == null);
contents.func_decl = inst;
var extra_index: usize = extra.end;
if (extra.data.bits.has_cc_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
const body = zir.bodySlice(extra_index, body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
extra_index += body.len;
} else if (extra.data.bits.has_cc_ref) {
extra_index += 1;
}
if (extra.data.bits.has_ret_ty_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
const body = zir.bodySlice(extra_index, body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
extra_index += body.len;
} else if (extra.data.bits.has_ret_ty_ref) {
extra_index += 1;
}
extra_index += @intFromBool(extra.data.bits.has_any_noalias);
const body = zir.bodySlice(extra_index, extra.data.body_len);
return zir.findTrackableBody(gpa, contents, defers, body);
},
// 块指令,递归处理各主体。
.block,
.block_inline,
.typeof_builtin,
.loop,
=> {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.Block, inst_data.payload_index);
const body = zir.bodySlice(extra.end, extra.data.body_len);
return zir.findTrackableBody(gpa, contents, defers, body);
},
.block_comptime => {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.BlockComptime, inst_data.payload_index);
const body = zir.bodySlice(extra.end, extra.data.body_len);
return zir.findTrackableBody(gpa, contents, defers, body);
},
.condbr, .condbr_inline => {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.CondBr, inst_data.payload_index);
const then_body = zir.bodySlice(extra.end, extra.data.then_body_len);
const else_body = zir.bodySlice(extra.end + then_body.len, extra.data.else_body_len);
try zir.findTrackableBody(gpa, contents, defers, then_body);
try zir.findTrackableBody(gpa, contents, defers, else_body);
},
.@"try", .try_ptr => {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.Try, inst_data.payload_index);
const body = zir.bodySlice(extra.end, extra.data.body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
},
.switch_block,
.switch_block_ref,
.switch_block_err_union,
=> {
const zir_switch = zir.getSwitchBlock(inst);
if (zir_switch.non_err_case) |non_err_case| {
try zir.findTrackableBody(gpa, contents, defers, non_err_case.body);
}
if (zir_switch.else_case) |else_case| {
try zir.findTrackableBody(gpa, contents, defers, else_case.body);
}
var extra_index = zir_switch.end;
var case_it = zir_switch.iterateCases();
while (case_it.next()) |case| {
const prong_body = zir.bodySlice(extra_index, case.prong_info.body_len);
extra_index += prong_body.len;
try zir.findTrackableBody(gpa, contents, defers, prong_body);
for (case.item_infos) |item_info| {
if (item_info.bodyLen()) |body_len| {
const item_body = zir.bodySlice(extra_index, body_len);
extra_index += item_body.len;
try zir.findTrackableBody(gpa, contents, defers, item_body);
}
}
for (case.range_infos) |range_info| {
if (range_info[0].bodyLen()) |body_len| {
const first_body = zir.bodySlice(extra_index, body_len);
extra_index += first_body.len;
try zir.findTrackableBody(gpa, contents, defers, first_body);
}
if (range_info[1].bodyLen()) |body_len| {
const last_body = zir.bodySlice(extra_index, body_len);
extra_index += last_body.len;
try zir.findTrackableBody(gpa, contents, defers, last_body);
}
}
}
},
.suspend_block => @panic("TODO iterate suspend block"),
.param, .param_comptime => {
const inst_data = datas[@backingInt(inst)].pl_tok;
const extra = zir.extraData(Inst.Param, inst_data.payload_index);
const body = zir.bodySlice(extra.end, extra.data.type.body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
},
inline .call, .field_call => |tag| {
const inst_data = datas[@backingInt(inst)].pl_node;
const extra = zir.extraData(switch (tag) {
.call => Inst.Call,
.field_call => Inst.FieldCall,
else => unreachable,
}, inst_data.payload_index);
// 最容易的方式是将所有参数体合并为一个体,正如上面对 `struct_decl` 所做的那样。
const args_len = extra.data.flags.args_len;
if (args_len > 0) {
const first_arg_start_off = args_len;
const final_arg_end_off = zir.extra[extra.end + args_len - 1];
const args_body = zir.bodySlice(extra.end + first_arg_start_off, final_arg_end_off - first_arg_start_off);
try zir.findTrackableBody(gpa, contents, defers, args_body);
}
},
.@"defer" => {
const inst_data = datas[@backingInt(inst)].@"defer";
const gop = try defers.getOrPut(gpa, inst_data.index);
if (!gop.found_existing) {
const body = zir.bodySlice(inst_data.index, inst_data.len);
try zir.findTrackableBody(gpa, contents, defers, body);
}
},
}
}
fn findTrackableBody(
zir: Zir,
gpa: Allocator,
contents: *DeclContents,
defers: *std.AutoHashMapUnmanaged(u32, void),
body: []const Inst.Index,
) Allocator.Error!void {
for (body) |member| {
try zir.findTrackableInner(gpa, contents, defers, member);
}
}
pub const FnInfo = struct {
param_body: []const Inst.Index,
param_body_inst: Inst.Index,
ret_ty_body: []const Inst.Index,
body: []const Inst.Index,
ret_ty_ref: Zir.Inst.Ref,
ret_ty_is_generic: bool,
total_params_len: u32,
inferred_error_set: bool,
};
pub fn getParamBody(zir: Zir, fn_inst: Inst.Index) []const Zir.Inst.Index {
const tags = zir.instructions.items(.tag);
const datas = zir.instructions.items(.data);
const inst_data = datas[@backingInt(fn_inst)].pl_node;
const param_block_index = switch (tags[@backingInt(fn_inst)]) {
.func, .func_inferred => blk: {
const extra = zir.extraData(Inst.Func, inst_data.payload_index);
break :blk extra.data.param_block;
},
.func_fancy => blk: {
const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index);
break :blk extra.data.param_block;
},
else => unreachable,
};
switch (tags[@backingInt(param_block_index)]) {
.block, .block_comptime, .block_inline => {
const param_block = zir.extraData(Inst.Block, datas[@backingInt(param_block_index)].pl_node.payload_index);
return zir.bodySlice(param_block.end, param_block.data.body_len);
},
.declaration => {
return zir.getDeclaration(param_block_index).value_body.?;
},
else => unreachable,
}
}
pub fn getParamName(zir: Zir, param_inst: Inst.Index) ?NullTerminatedString {
const inst = zir.instructions.get(@backingInt(param_inst));
return switch (inst.tag) {
.param, .param_comptime => zir.extraData(Inst.Param, inst.data.pl_tok.payload_index).data.name,
.param_anytype, .param_anytype_comptime => inst.data.str_tok.start,
else => null,
};
}
pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
const tags = zir.instructions.items(.tag);
const datas = zir.instructions.items(.data);
const info: struct {
param_block: Inst.Index,
body: []const Inst.Index,
ret_ty_ref: Inst.Ref,
ret_ty_body: []const Inst.Index,
ret_ty_is_generic: bool,
ies: bool,
} = switch (tags[@backingInt(fn_inst)]) {
.func, .func_inferred => |tag| blk: {
const inst_data = datas[@backingInt(fn_inst)].pl_node;
const extra = zir.extraData(Inst.Func, inst_data.payload_index);
var extra_index: usize = extra.end;
var ret_ty_ref: Inst.Ref = .none;
var ret_ty_body: []const Inst.Index = &.{};
switch (extra.data.ret_ty.body_len) {
0 => {
ret_ty_ref = .void_type;
},
1 => {
ret_ty_ref = @fromBackingInt(@intCast(zir.extra[extra_index]));
extra_index += 1;
},
else => {
ret_ty_body = zir.bodySlice(extra_index, extra.data.ret_ty.body_len);
extra_index += ret_ty_body.len;
},
}
const body = zir.bodySlice(extra_index, extra.data.body_len);
extra_index += body.len;
break :blk .{
.param_block = extra.data.param_block,
.ret_ty_ref = ret_ty_ref,
.ret_ty_body = ret_ty_body,
.body = body,
.ret_ty_is_generic = extra.data.ret_ty.is_generic,
.ies = tag == .func_inferred,
};
},
.func_fancy => blk: {
const inst_data = datas[@backingInt(fn_inst)].pl_node;
const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index);
var extra_index: usize = extra.end;
var ret_ty_ref: Inst.Ref = .none;
var ret_ty_body: []const Inst.Index = &.{};
if (extra.data.bits.has_cc_body) {
extra_index += zir.extra[extra_index] + 1;
} else if (extra.data.bits.has_cc_ref) {
extra_index += 1;
}
if (extra.data.bits.has_ret_ty_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
ret_ty_body = zir.bodySlice(extra_index, body_len);
extra_index += ret_ty_body.len;
} else if (extra.data.bits.has_ret_ty_ref) {
ret_ty_ref = @fromBackingInt(@intCast(zir.extra[extra_index]));
extra_index += 1;
} else {
ret_ty_ref = .void_type;
}
extra_index += @intFromBool(extra.data.bits.has_any_noalias);
const body = zir.bodySlice(extra_index, extra.data.body_len);
extra_index += body.len;
break :blk .{
.param_block = extra.data.param_block,
.ret_ty_ref = ret_ty_ref,
.ret_ty_body = ret_ty_body,
.body = body,
.ret_ty_is_generic = extra.data.bits.ret_ty_is_generic,
.ies = extra.data.bits.is_inferred_error,
};
},
else => unreachable,
};
const param_body = zir.getParamBody(fn_inst);
var total_params_len: u32 = 0;
for (param_body) |inst| {
switch (tags[@backingInt(inst)]) {
.param, .param_comptime, .param_anytype, .param_anytype_comptime => {
total_params_len += 1;
},
else => continue,
}
}
return .{
.param_body = param_body,
.param_body_inst = info.param_block,
.ret_ty_body = info.ret_ty_body,
.ret_ty_ref = info.ret_ty_ref,
.body = info.body,
.total_params_len = total_params_len,
.ret_ty_is_generic = info.ret_ty_is_generic,
.inferred_error_set = info.ies,
};
}
pub fn getDeclaration(zir: Zir, inst: Zir.Inst.Index) Inst.Declaration.Unwrapped {
assert(zir.instructions.items(.tag)[@backingInt(inst)] == .declaration);
const pl_node = zir.instructions.items(.data)[@backingInt(inst)].declaration;
const extra = zir.extraData(Inst.Declaration, pl_node.payload_index);
const flags_vals: [2]u32 = .{ extra.data.flags_0, extra.data.flags_1 };
const flags: Inst.Declaration.Flags = @bitCast(flags_vals);
var extra_index = extra.end;
const name: NullTerminatedString = if (flags.id.hasName()) name: {
const name = zir.extra[extra_index];
extra_index += 1;
break :name @fromBackingInt(@intCast(name));
} else .empty;
const lib_name: NullTerminatedString = if (flags.id.hasLibName()) lib_name: {
const lib_name = zir.extra[extra_index];
extra_index += 1;
break :lib_name @fromBackingInt(@intCast(lib_name));
} else .empty;
const type_body_len: u32 = if (flags.id.hasTypeBody()) len: {
const len = zir.extra[extra_index];
extra_index += 1;
break :len len;
} else 0;
const align_body_len: u32, const linksection_body_len: u32, const addrspace_body_len: u32 = lens: {
if (!flags.id.hasSpecialBodies()) break :lens .{ 0, 0, 0 };
const lens = zir.extra[extra_index..][0..3].*;
extra_index += 3;
break :lens lens;
};
const value_body_len: u32 = if (flags.id.hasValueBody()) len: {
const len = zir.extra[extra_index];
extra_index += 1;
break :len len;
} else 0;
const type_body = zir.bodySlice(extra_index, type_body_len);
extra_index += type_body_len;
const align_body = zir.bodySlice(extra_index, align_body_len);
extra_index += align_body_len;
const linksection_body = zir.bodySlice(extra_index, linksection_body_len);
extra_index += linksection_body_len;
const addrspace_body = zir.bodySlice(extra_index, addrspace_body_len);
extra_index += addrspace_body_len;
const value_body = zir.bodySlice(extra_index, value_body_len);
extra_index += value_body_len;
return .{
.src_node = pl_node.src_node,
.src_line = flags.src_line,
.src_column = flags.src_column,
.kind = flags.id.kind(),
.name = name,
.is_pub = flags.id.isPub(),
.is_threadlocal = flags.id.isThreadlocal(),
.linkage = flags.id.linkage(),
.lib_name = lib_name,
.type_body = if (type_body_len == 0) null else type_body,
.align_body = if (align_body_len == 0) null else align_body,
.linksection_body = if (linksection_body_len == 0) null else linksection_body,
.addrspace_body = if (addrspace_body_len == 0) null else addrspace_body,
.value_body = if (value_body_len == 0) null else value_body,
};
}
pub fn getAssociatedSrcHash(zir: Zir, inst: Zir.Inst.Index) ?std.zig.SrcHash {
const tag = zir.instructions.items(.tag);
const data = zir.instructions.items(.data);
switch (tag[@backingInt(inst)]) {
.declaration => {
const declaration = data[@backingInt(inst)].declaration;
const extra = zir.extraData(Inst.Declaration, declaration.payload_index);
return @bitCast([4]u32{
extra.data.src_hash_0,
extra.data.src_hash_1,
extra.data.src_hash_2,
extra.data.src_hash_3,
});
},
.func, .func_inferred => {
const pl_node = data[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.Func, pl_node.payload_index);
if (extra.data.body_len == 0) {
// 函数类型或 extern fn:没有关联哈希
return null;
}
const extra_index = extra.end +
extra.data.ret_ty.body_len +
extra.data.body_len +
@typeInfo(Inst.Func.SrcLocs).@"struct".field_names.len;
return @bitCast([4]u32{
zir.extra[extra_index + 0],
zir.extra[extra_index + 1],
zir.extra[extra_index + 2],
zir.extra[extra_index + 3],
});
},
.func_fancy => {
const pl_node = data[@backingInt(inst)].pl_node;
const extra = zir.extraData(Inst.FuncFancy, pl_node.payload_index);
if (extra.data.body_len == 0) {
// 函数类型或 extern fn:没有关联哈希
return null;
}
const bits = extra.data.bits;
var extra_index = extra.end;
if (bits.has_cc_body) {
const body_len = zir.extra[extra_index];
extra_index += 1 + body_len;
} else extra_index += @intFromBool(bits.has_cc_ref);
if (bits.has_ret_ty_body) {
const body_len = zir.extra[extra_index];
extra_index += 1 + body_len;
} else extra_index += @intFromBool(bits.has_ret_ty_ref);
extra_index += @intFromBool(bits.has_any_noalias);
extra_index += extra.data.body_len;
extra_index += @typeInfo(Zir.Inst.Func.SrcLocs).@"struct".field_names.len;
return @bitCast([4]u32{
zir.extra[extra_index + 0],
zir.extra[extra_index + 1],
zir.extra[extra_index + 2],
zir.extra[extra_index + 3],
});
},
.extended => {},
else => return null,
}
const extended = data[@backingInt(inst)].extended;
switch (extended.opcode) {
.struct_decl => {
const extra = zir.extraData(Inst.StructDecl, extended.operand).data;
return @bitCast([4]u32{
extra.fields_hash_0,
extra.fields_hash_1,
extra.fields_hash_2,
extra.fields_hash_3,
});
},
.union_decl => {
const extra = zir.extraData(Inst.UnionDecl, extended.operand).data;
return @bitCast([4]u32{
extra.fields_hash_0,
extra.fields_hash_1,
extra.fields_hash_2,
extra.fields_hash_3,
});
},
.enum_decl => {
const extra = zir.extraData(Inst.EnumDecl, extended.operand).data;
return @bitCast([4]u32{
extra.fields_hash_0,
extra.fields_hash_1,
extra.fields_hash_2,
extra.fields_hash_3,
});
},
else => return null,
}
}
pub fn getSwitchBlock(zir: *const Zir, switch_inst: Inst.Index) UnwrappedSwitchBlock {
const has_non_err = switch (zir.instructions.items(.tag)[@backingInt(switch_inst)]) {
.switch_block, .switch_block_ref => false,
.switch_block_err_union => true,
else => unreachable,
};
const inst_data = zir.instructions.items(.data)[@backingInt(switch_inst)].pl_node;
const extra = zir.extraData(Inst.SwitchBlock, inst_data.payload_index);
const bits = extra.data.bits;
var extra_index = extra.end;
const multi_cases_len = if (bits.has_multi_cases) len: {
const multi_cases_len = zir.extra[extra_index];
extra_index += 1;
break :len multi_cases_len;
} else 0;
const payload_capture_placeholder: Inst.OptionalIndex = if (bits.payload_capture_inst_is_placeholder) inst: {
const inst: Inst.Index = @fromBackingInt(@intCast(zir.extra[extra_index]));
extra_index += 1;
break :inst inst.toOptional();
} else .none;
const tag_capture_placeholder: Inst.OptionalIndex = if (bits.tag_capture_inst_is_placeholder) inst: {
const inst: Inst.Index = @fromBackingInt(@intCast(zir.extra[extra_index]));
extra_index += 1;
break :inst inst.toOptional();
} else .none;
const catch_or_if_src_node_offset: Ast.Node.OptionalOffset = if (has_non_err) node_offset: {
const node_offset: Ast.Node.Offset = @fromBackingInt(@intCast(@as(i32, @bitCast(zir.extra[extra_index]))));
extra_index += 1;
break :node_offset node_offset.toOptional();
} else .none;
const non_err_info: Inst.SwitchBlock.ProngInfo.NonErr = if (has_non_err) non_err_info: {
const non_err_info: Inst.SwitchBlock.ProngInfo.NonErr = @bitCast(zir.extra[extra_index]);
extra_index += 1;
break :non_err_info non_err_info;
} else undefined;
const else_info: Inst.SwitchBlock.ProngInfo.Else = if (bits.has_else) else_info: {
const else_info: Inst.SwitchBlock.ProngInfo.Else = @bitCast(zir.extra[extra_index]);
extra_index += 1;
break :else_info else_info;
} else undefined;
const scalar_cases_len: u32 = bits.scalar_cases_len;
const prong_infos: []const Inst.SwitchBlock.ProngInfo =
@ptrCast(zir.extra[extra_index..][0 .. scalar_cases_len + multi_cases_len]);
extra_index += prong_infos.len;
const multi_case_items_lens = zir.extra[extra_index..][0..multi_cases_len];
extra_index += multi_case_items_lens.len;
const multi_case_ranges_lens: ?[]const u32 = if (bits.any_ranges) lens: {
const multi_case_ranges_lens = zir.extra[extra_index..][0..multi_cases_len];
extra_index += multi_case_ranges_lens.len;
break :lens multi_case_ranges_lens;
} else null;
var total_items_len: usize = scalar_cases_len;
for (multi_case_items_lens) |items_len| {
total_items_len += items_len;
}
if (multi_case_ranges_lens) |ranges_lens| for (ranges_lens) |ranges_len| {
total_items_len += 2 * ranges_len;
};
const item_infos: []const Inst.SwitchBlock.ItemInfo =
@ptrCast(zir.extra[extra_index..][0..total_items_len]);
extra_index += item_infos.len;
const non_err_case: ?UnwrappedSwitchBlock.Case.NonErr = if (has_non_err) non_err_case: {
const body = zir.bodySlice(extra_index, non_err_info.body_len);
extra_index += body.len;
break :non_err_case .{
.body = body,
.capture = non_err_info.capture,
.operand_is_ref = non_err_info.operand_is_ref,
};
} else null;
const else_case: ?UnwrappedSwitchBlock.Case.Else = if (bits.has_else) else_case: {
const body = zir.bodySlice(extra_index, else_info.body_len);
extra_index += body.len;
break :else_case .{
.index = .@"else",
.body = body,
.capture = else_info.capture,
.is_inline = else_info.is_inline,
.has_tag_capture = else_info.has_tag_capture,
.is_simple_noreturn = else_info.is_simple_noreturn,
};
} else null;
return .{
.main_operand = extra.data.raw_operand,
.switch_src_node_offset = inst_data.src_node,
.catch_or_if_src_node_offset = catch_or_if_src_node_offset,
.payload_capture_placeholder = payload_capture_placeholder,
.tag_capture_placeholder = tag_capture_placeholder,
.has_continue = bits.has_continue,
.any_maybe_runtime_capture = bits.any_maybe_runtime_capture,
.non_err_case = non_err_case,
.else_case = else_case,
.has_under = bits.has_under,
.prong_infos = prong_infos,
.multi_case_items_lens = multi_case_items_lens,
.multi_case_ranges_lens = multi_case_ranges_lens,
.item_infos = item_infos,
.end = extra_index,
};
}
/// 尾随内容,从 `end` 开始:
/// 0. case_bodies: { // Case.Iterator.next() 中的每个 case 对应一个
/// prong_body: {
/// body_inst: Inst.Index, // 每个 case.prong_info.body_len 对应一个
/// }
/// item_body: { // case.item_infos 中的每个 body_len 对应一个
/// body_inst: Inst.Index, // 每个 body_len 对应一个
/// }
/// range_bodies: { // case.range_infos 中的每个 .{first_info, last_info} 对应一个
/// first_body_inst: Inst.Index, // 每个 first_info.body_len 对应一个
/// last_body_inst: Inst.Index, // 每个 last_info.body_len 对应一个
/// }
/// }
pub const UnwrappedSwitchBlock = struct {
/// `catch`/`if` 或 `switch` 操作数。
main_operand: Inst.Ref,
switch_src_node_offset: Ast.Node.Offset,
catch_or_if_src_node_offset: Ast.Node.OptionalOffset,
payload_capture_placeholder: Inst.OptionalIndex,
tag_capture_placeholder: Inst.OptionalIndex,
has_continue: bool,
any_maybe_runtime_capture: bool,
non_err_case: ?Case.NonErr,
else_case: ?Case.Else,
has_under: bool,
// 参照文档注释和 `iterateCases`,正确访问下面的所有内容。
prong_infos: []const Inst.SwitchBlock.ProngInfo,
multi_case_items_lens: []const u32,
multi_case_ranges_lens: ?[]const u32,
item_infos: []const Inst.SwitchBlock.ItemInfo,
end: usize,
pub fn anyRanges(unwrapped: *const UnwrappedSwitchBlock) bool {
return unwrapped.multi_case_ranges_lens != null;
}
pub fn scalarCasesLen(unwrapped: *const UnwrappedSwitchBlock) u32 {
return @intCast(unwrapped.prong_infos.len - unwrapped.multi_case_items_lens.len);
}
pub fn multiCasesLen(unwrapped: *const UnwrappedSwitchBlock) u32 {
return @intCast(unwrapped.multi_case_items_lens.len);
}
pub fn totalItemsLen(unwrapped: *const UnwrappedSwitchBlock) u32 {
var total_items_len: u32 = @intCast(unwrapped.item_infos.len);
if (unwrapped.multi_case_ranges_lens) |ranges_lens| {
for (ranges_lens) |len| total_items_len -= len;
}
return total_items_len;
}
pub const Case = struct {
index: Case.Index,
prong_info: Inst.SwitchBlock.ProngInfo,
item_infos: []const Inst.SwitchBlock.ItemInfo,
range_infos: []const [2]Inst.SwitchBlock.ItemInfo,
pub const Index = packed struct(u32) {
kind: enum(u1) { scalar, multi },
value: u31,
pub const @"else": Case.Index = .{
.kind = .scalar,
.value = std.math.maxInt(u31),
};
};
pub const NonErr = struct {
body: []const Inst.Index,
capture: Inst.SwitchBlock.ProngInfo.Capture,
operand_is_ref: bool,
};
pub const Else = struct {
index: Case.Index,
body: []const Inst.Index,
capture: Inst.SwitchBlock.ProngInfo.Capture,
is_inline: bool,
has_tag_capture: bool,
is_simple_noreturn: bool,
};
pub const Iterator = struct {
next_idx: u32,
prong_infos: []const Inst.SwitchBlock.ProngInfo,
multi_case_items_lens: []const u32,
multi_case_ranges_lens: ?[]const u32,
item_infos: []const Inst.SwitchBlock.ItemInfo,
pub fn next(it: *Iterator) ?Case {
const idx = it.next_idx;
if (idx == it.prong_infos.len) return null;
it.next_idx += 1;
const scalar_cases_len = it.prong_infos.len - it.multi_case_items_lens.len;
return if (idx < scalar_cases_len) .{
.index = .{
.kind = .scalar,
.value = @intCast(idx),
},
.prong_info = it.prong_infos[idx],
.item_infos = it.itemInfos(1),
.range_infos = &.{},
} else .{
.index = .{
.kind = .multi,
.value = @intCast(idx - scalar_cases_len),
},
.prong_info = it.prong_infos[idx],
.item_infos = it.itemInfos(it.multi_case_items_lens[idx - scalar_cases_len]),
.range_infos = if (it.multi_case_ranges_lens) |ranges_lens| b: {
break :b @ptrCast(it.itemInfos(2 * ranges_lens[idx - scalar_cases_len]));
} else &.{},
};
}
fn itemInfos(it: *Iterator, count: u32) []const Inst.SwitchBlock.ItemInfo {
const lens = it.item_infos[0..count];
it.item_infos = it.item_infos[count..];
return lens;
}
};
};
pub fn iterateCases(unwrapped: UnwrappedSwitchBlock) Case.Iterator {
return .{
.next_idx = 0,
.prong_infos = unwrapped.prong_infos,
.multi_case_items_lens = unwrapped.multi_case_items_lens,
.multi_case_ranges_lens = unwrapped.multi_case_ranges_lens,
.item_infos = unwrapped.item_infos,
};
}
};
/// 需要修改 ZIR 更新跟踪逻辑以考虑新指令时,
/// 更改此常量可在所有相关位置触发编译错误。
pub const inst_tracking_version = 0;
/// 断言 ZIR 指令会跨增量更新跟踪,
/// 因此可以分配 `InternPool.TrackedInst`。
pub fn assertTrackable(zir: Zir, inst_idx: Zir.Inst.Index) void {
comptime assert(Zir.inst_tracking_version == 0);
const inst = zir.instructions.get(@backingInt(inst_idx));
switch (inst.tag) {
.struct_init,
.struct_init_ref,
.struct_init_anon,
=> {}, // 作为匿名结构体类型的所属指令,按顺序跟踪
.func, .func_inferred => {
// 如果它们是实际函数声明而非原型,则进行跟踪。
const extra = zir.extraData(Inst.Func, inst.data.pl_node.payload_index);
assert(extra.data.body_len != 0);
},
.func_fancy => {
// 如果它们是实际函数声明而非原型,则进行跟踪。
const extra = zir.extraData(Inst.FuncFancy, inst.data.pl_node.payload_index);
assert(extra.data.body_len != 0);
},
.declaration => {}, // 通过关联父容器命名空间中的名称进行跟踪。
.extended => switch (inst.data.extended.opcode) {
.struct_decl,
.union_decl,
.enum_decl,
.opaque_decl,
.reify_enum,
.reify_struct,
.reify_union,
.reify_spirv_type,
=> {}, // 作为显式容器类型的所属指令,按顺序跟踪
else => unreachable, // 断言失败;无法跟踪
},
else => unreachable, // 断言失败;无法跟踪
}
}
pub fn typeDecls(zir: Zir, type_decl: Inst.Index) []const Zir.Inst.Index {
const inst = zir.instructions.get(@backingInt(type_decl));
assert(inst.tag == .extended);
return switch (inst.data.extended.opcode) {
.struct_decl => zir.getStructDecl(type_decl).decls,
.union_decl => zir.getUnionDecl(type_decl).decls,
.enum_decl => zir.getEnumDecl(type_decl).decls,
.opaque_decl => zir.getOpaqueDecl(type_decl).decls,
else => unreachable,
};
}
pub fn getStructDecl(zir: *const Zir, struct_decl: Inst.Index) UnwrappedStructDecl {
const inst_data = zir.instructions.get(@backingInt(struct_decl));
assert(inst_data.tag == .extended);
assert(inst_data.data.extended.opcode == .struct_decl);
const small: Inst.StructDecl.Small = @bitCast(inst_data.data.extended.small);
const extra = zir.extraData(Inst.StructDecl, inst_data.data.extended.operand);
var extra_index = extra.end;
const captures_len: u32 = if (small.has_captures_len) blk: {
const captures_len = zir.extra[extra_index];
extra_index += 1;
break :blk captures_len;
} else 0;
const decls_len: u32 = if (small.has_decls_len) blk: {
const decls_len = zir.extra[extra_index];
extra_index += 1;
break :blk decls_len;
} else 0;
const fields_len: u32 = if (small.has_fields_len) blk: {
const fields_len = zir.extra[extra_index];
extra_index += 1;
break :blk fields_len;
} else 0;
const arg_baseline_src_node: Ast.Node.Index = if (small.has_backing_int_type) node: {
defer extra_index += 1;
break :node @fromBackingInt(zir.extra[extra_index]);
} else extra.data.src_node;
const fields_baseline_src_node: Ast.Node.Index = if (small.has_fields_len) node: {
defer extra_index += 1;
break :node @fromBackingInt(zir.extra[extra_index]);
} else extra.data.src_node;
const backing_int_type_body_len: u32 = if (small.has_backing_int_type) len: {
const body_len = zir.extra[extra_index];
extra_index += 1;
break :len body_len;
} else 0;
const captures: []const Inst.Capture = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const capture_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const decls: []const Inst.Index = @ptrCast(zir.extra[extra_index..][0..decls_len]);
extra_index += decls_len;
const field_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..fields_len]);
extra_index += fields_len;
const field_type_body_lens: []const u32 = @ptrCast(zir.extra[extra_index..][0..fields_len]);
extra_index += fields_len;
const field_align_body_lens: ?[]const u32 = if (small.any_field_aligns) lens: {
const lens = zir.extra[extra_index..][0..fields_len];
extra_index += fields_len;
break :lens @ptrCast(lens);
} else null;
const field_default_body_lens: ?[]const u32 = if (small.any_field_defaults) lens: {
const lens = zir.extra[extra_index..][0..fields_len];
extra_index += fields_len;
break :lens @ptrCast(lens);
} else null;
const field_comptime_bits: ?[]const u32 = if (small.any_comptime_fields) bits: {
const bits_len = @divCeil(fields_len, 32);
const bits = zir.extra[extra_index..][0..bits_len];
extra_index += bits_len;
break :bits bits;
} else null;
const backing_int_type_body: ?[]const Zir.Inst.Index = switch (backing_int_type_body_len) {
0 => null,
else => |n| zir.bodySlice(extra_index, n),
};
extra_index += backing_int_type_body_len;
const field_bodies_overlong: []const Inst.Index = @ptrCast(zir.extra[extra_index..]);
return .{
.src_line = extra.data.src_line,
.src_column = extra.data.src_column,
.src_node = extra.data.src_node,
.name_strategy = small.name_strategy,
.arg_baseline_src_node = arg_baseline_src_node,
.fields_baseline_src_node = fields_baseline_src_node,
.captures = captures,
.capture_names = capture_names,
.decls = decls,
.layout = small.layout,
.backing_int_type_body = backing_int_type_body,
.field_names = field_names,
.field_type_body_lens = field_type_body_lens,
.field_align_body_lens = field_align_body_lens,
.field_default_body_lens = field_default_body_lens,
.field_comptime_bits = field_comptime_bits,
.field_bodies_overlong = field_bodies_overlong,
};
}
pub const UnwrappedStructDecl = struct {
src_line: u32,
src_column: u32,
src_node: Ast.Node.Index,
name_strategy: Inst.NameStrategy,
arg_baseline_src_node: Ast.Node.Index,
fields_baseline_src_node: Ast.Node.Index,
captures: []const Inst.Capture,
capture_names: []const NullTerminatedString,
decls: []const Inst.Index,
layout: std.lang.Type.ContainerLayout,
backing_int_type_body: ?[]const Inst.Index,
field_names: []const NullTerminatedString,
field_type_body_lens: []const u32,
field_align_body_lens: ?[]const u32,
field_default_body_lens: ?[]const u32,
field_comptime_bits: ?[]const u32,
field_bodies_overlong: []const Inst.Index,
pub fn iterateFields(struct_decl: UnwrappedStructDecl) FieldIterator {
return .{
.next_idx = 0,
.names = struct_decl.field_names,
.type_body_lens = struct_decl.field_type_body_lens,
.align_body_lens = struct_decl.field_align_body_lens,
.default_body_lens = struct_decl.field_default_body_lens,
.comptime_bits = struct_decl.field_comptime_bits,
.bodies_overlong = struct_decl.field_bodies_overlong,
};
}
pub const FieldIterator = struct {
next_idx: u32,
names: []const NullTerminatedString,
type_body_lens: []const u32,
align_body_lens: ?[]const u32,
default_body_lens: ?[]const u32,
comptime_bits: ?[]const u32,
bodies_overlong: []const Inst.Index,
pub const Field = struct {
idx: u32,
name: NullTerminatedString,
type_body: []const Inst.Index,
align_body: ?[]const Inst.Index,
default_body: ?[]const Inst.Index,
is_comptime: bool,
};
pub fn next(it: *FieldIterator) ?Field {
const idx = it.next_idx;
if (idx == it.names.len) return null;
it.next_idx += 1;
return .{
.idx = idx,
.name = it.names[idx],
.type_body = it.body(it.type_body_lens[idx]).?,
.align_body = it.body(if (it.align_body_lens) |l| l[idx] else 0),
.default_body = it.body(if (it.default_body_lens) |l| l[idx] else 0),
.is_comptime = ct: {
const bits = it.comptime_bits orelse break :ct false;
const big = bits[idx / 32];
const shifted = big >> @intCast(idx % 32);
break :ct @as(u1, @truncate(shifted)) == 1;
},
};
}
fn body(it: *FieldIterator, len: u32) ?[]const Inst.Index {
if (len == 0) return null;
const b = it.bodies_overlong[0..len];
it.bodies_overlong = it.bodies_overlong[len..];
return b;
}
};
};
pub fn getUnionDecl(zir: *const Zir, union_decl: Inst.Index) UnwrappedUnionDecl {
const inst_data = zir.instructions.get(@backingInt(union_decl));
assert(inst_data.tag == .extended);
assert(inst_data.data.extended.opcode == .union_decl);
const small: Inst.UnionDecl.Small = @bitCast(inst_data.data.extended.small);
const extra = zir.extraData(Inst.UnionDecl, inst_data.data.extended.operand);
var extra_index = extra.end;
const captures_len: u32 = if (small.has_captures_len) blk: {
const captures_len = zir.extra[extra_index];
extra_index += 1;
break :blk captures_len;
} else 0;
const decls_len: u32 = if (small.has_decls_len) blk: {
const decls_len = zir.extra[extra_index];
extra_index += 1;
break :blk decls_len;
} else 0;
const fields_len: u32 = if (small.has_fields_len) blk: {
const fields_len = zir.extra[extra_index];
extra_index += 1;
break :blk fields_len;
} else 0;
const arg_baseline_src_node: Ast.Node.Index = if (small.kind.hasArgType()) node: {
defer extra_index += 1;
break :node @fromBackingInt(zir.extra[extra_index]);
} else extra.data.src_node;
const fields_baseline_src_node: Ast.Node.Index = if (small.has_fields_len) node: {
defer extra_index += 1;
break :node @fromBackingInt(zir.extra[extra_index]);
} else extra.data.src_node;
const arg_type_body_len: u32 = if (small.kind.hasArgType()) len: {
const body_len = zir.extra[extra_index];
extra_index += 1;
break :len body_len;
} else 0;
const captures: []const Inst.Capture = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const capture_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const decls: []const Inst.Index = @ptrCast(zir.extra[extra_index..][0..decls_len]);
extra_index += decls_len;
const field_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..fields_len]);
extra_index += fields_len;
const field_type_body_lens: []const u32 = @ptrCast(zir.extra[extra_index..][0..fields_len]);
extra_index += fields_len;
const field_align_body_lens: ?[]const u32 = if (small.any_field_aligns) lens: {
const lens = zir.extra[extra_index..][0..fields_len];
extra_index += fields_len;
break :lens @ptrCast(lens);
} else null;
const field_value_body_lens: ?[]const u32 = if (small.any_field_values) lens: {
const lens = zir.extra[extra_index..][0..fields_len];
extra_index += fields_len;
break :lens @ptrCast(lens);
} else null;
const arg_type_body: ?[]const Zir.Inst.Index = switch (arg_type_body_len) {
0 => null,
else => |n| zir.bodySlice(extra_index, n),
};
extra_index += arg_type_body_len;
const field_bodies_overlong: []const Inst.Index = @ptrCast(zir.extra[extra_index..]);
return .{
.src_line = extra.data.src_line,
.src_column = extra.data.src_column,
.src_node = extra.data.src_node,
.name_strategy = small.name_strategy,
.arg_baseline_src_node = arg_baseline_src_node,
.fields_baseline_src_node = fields_baseline_src_node,
.captures = captures,
.capture_names = capture_names,
.decls = decls,
.kind = small.kind,
.arg_type_body = arg_type_body,
.field_names = field_names,
.field_type_body_lens = field_type_body_lens,
.field_align_body_lens = field_align_body_lens,
.field_value_body_lens = field_value_body_lens,
.field_bodies_overlong = field_bodies_overlong,
};
}
pub const UnwrappedUnionDecl = struct {
src_line: u32,
src_column: u32,
src_node: Ast.Node.Index,
name_strategy: Inst.NameStrategy,
arg_baseline_src_node: Ast.Node.Index,
fields_baseline_src_node: Ast.Node.Index,
captures: []const Inst.Capture,
capture_names: []const NullTerminatedString,
decls: []const Inst.Index,
kind: Inst.UnionDecl.Kind,
arg_type_body: ?[]const Inst.Index,
field_names: []const NullTerminatedString,
field_type_body_lens: []const u32,
field_align_body_lens: ?[]const u32,
field_value_body_lens: ?[]const u32,
field_bodies_overlong: []const Inst.Index,
pub fn iterateFields(union_decl: UnwrappedUnionDecl) FieldIterator {
return .{
.next_idx = 0,
.names = union_decl.field_names,
.type_body_lens = union_decl.field_type_body_lens,
.align_body_lens = union_decl.field_align_body_lens,
.value_body_lens = union_decl.field_value_body_lens,
.bodies_overlong = union_decl.field_bodies_overlong,
};
}
pub const FieldIterator = struct {
next_idx: u32,
names: []const NullTerminatedString,
type_body_lens: []const u32,
align_body_lens: ?[]const u32,
value_body_lens: ?[]const u32,
bodies_overlong: []const Inst.Index,
pub const Field = struct {
idx: u32,
name: NullTerminatedString,
type_body: ?[]const Inst.Index,
align_body: ?[]const Inst.Index,
value_body: ?[]const Inst.Index,
};
pub fn next(it: *FieldIterator) ?Field {
const idx = it.next_idx;
if (idx == it.names.len) return null;
it.next_idx += 1;
return .{
.idx = idx,
.name = it.names[idx],
.type_body = it.body(it.type_body_lens[idx]),
.align_body = it.body(if (it.align_body_lens) |l| l[idx] else 0),
.value_body = it.body(if (it.value_body_lens) |l| l[idx] else 0),
};
}
fn body(it: *FieldIterator, len: u32) ?[]const Inst.Index {
if (len == 0) return null;
const b = it.bodies_overlong[0..len];
it.bodies_overlong = it.bodies_overlong[len..];
return b;
}
};
};
pub fn getEnumDecl(zir: *const Zir, enum_decl: Inst.Index) UnwrappedEnumDecl {
const inst_data = zir.instructions.get(@backingInt(enum_decl));
assert(inst_data.tag == .extended);
assert(inst_data.data.extended.opcode == .enum_decl);
const small: Inst.EnumDecl.Small = @bitCast(inst_data.data.extended.small);
const extra = zir.extraData(Inst.EnumDecl, inst_data.data.extended.operand);
var extra_index = extra.end;
const captures_len: u32 = if (small.has_captures_len) blk: {
const captures_len = zir.extra[extra_index];
extra_index += 1;
break :blk captures_len;
} else 0;
const decls_len: u32 = if (small.has_decls_len) blk: {
const decls_len = zir.extra[extra_index];
extra_index += 1;
break :blk decls_len;
} else 0;
const fields_len: u32 = if (small.has_fields_len) blk: {
const fields_len = zir.extra[extra_index];
extra_index += 1;
break :blk fields_len;
} else 0;
const arg_baseline_src_node: Ast.Node.Index = if (small.has_tag_type) node: {
defer extra_index += 1;
break :node @fromBackingInt(zir.extra[extra_index]);
} else extra.data.src_node;
const fields_baseline_src_node: Ast.Node.Index = if (small.has_fields_len) node: {
defer extra_index += 1;
break :node @fromBackingInt(zir.extra[extra_index]);
} else extra.data.src_node;
const tag_type_body_len: u32 = if (small.has_tag_type) len: {
const body_len = zir.extra[extra_index];
extra_index += 1;
break :len body_len;
} else 0;
const captures: []const Inst.Capture = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const capture_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const decls: []const Inst.Index = @ptrCast(zir.extra[extra_index..][0..decls_len]);
extra_index += decls_len;
const field_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..fields_len]);
extra_index += fields_len;
const field_value_body_lens: ?[]const u32 = if (small.any_field_values) lens: {
const lens = zir.extra[extra_index..][0..fields_len];
extra_index += fields_len;
break :lens @ptrCast(lens);
} else null;
const tag_type_body: ?[]const Zir.Inst.Index = switch (tag_type_body_len) {
0 => null,
else => |n| zir.bodySlice(extra_index, n),
};
extra_index += tag_type_body_len;
const field_bodies_overlong: []const Inst.Index = @ptrCast(zir.extra[extra_index..]);
return .{
.src_line = extra.data.src_line,
.src_column = extra.data.src_column,
.src_node = extra.data.src_node,
.name_strategy = small.name_strategy,
.arg_baseline_src_node = arg_baseline_src_node,
.fields_baseline_src_node = fields_baseline_src_node,
.captures = captures,
.capture_names = capture_names,
.decls = decls,
.tag_type_body = tag_type_body,
.nonexhaustive = small.nonexhaustive,
.field_names = field_names,
.field_value_body_lens = field_value_body_lens,
.field_bodies_overlong = field_bodies_overlong,
};
}
pub const UnwrappedEnumDecl = struct {
src_line: u32,
src_column: u32,
src_node: Ast.Node.Index,
name_strategy: Inst.NameStrategy,
arg_baseline_src_node: Ast.Node.Index,
fields_baseline_src_node: Ast.Node.Index,
captures: []const Inst.Capture,
capture_names: []const NullTerminatedString,
decls: []const Inst.Index,
tag_type_body: ?[]const Inst.Index,
nonexhaustive: bool,
field_names: []const NullTerminatedString,
field_value_body_lens: ?[]const u32,
field_bodies_overlong: []const Inst.Index,
pub fn iterateFields(enum_decl: UnwrappedEnumDecl) FieldIterator {
return .{
.next_idx = 0,
.names = enum_decl.field_names,
.value_body_lens = enum_decl.field_value_body_lens,
.bodies_overlong = enum_decl.field_bodies_overlong,
};
}
pub const FieldIterator = struct {
next_idx: u32,
names: []const NullTerminatedString,
value_body_lens: ?[]const u32,
bodies_overlong: []const Inst.Index,
pub const Field = struct {
idx: u32,
name: NullTerminatedString,
value_body: ?[]const Inst.Index,
};
pub fn next(it: *FieldIterator) ?Field {
const idx = it.next_idx;
if (idx == it.names.len) return null;
it.next_idx += 1;
return .{
.idx = idx,
.name = it.names[idx],
.value_body = it.body(if (it.value_body_lens) |l| l[idx] else 0),
};
}
fn body(it: *FieldIterator, len: u32) ?[]const Inst.Index {
if (len == 0) return null;
const b = it.bodies_overlong[0..len];
it.bodies_overlong = it.bodies_overlong[len..];
return b;
}
};
};
pub fn getOpaqueDecl(zir: *const Zir, opaque_decl: Inst.Index) UnwrappedOpaqueDecl {
const inst_data = zir.instructions.get(@backingInt(opaque_decl));
assert(inst_data.tag == .extended);
assert(inst_data.data.extended.opcode == .opaque_decl);
const small: Inst.OpaqueDecl.Small = @bitCast(inst_data.data.extended.small);
const extra = zir.extraData(Inst.OpaqueDecl, inst_data.data.extended.operand);
var extra_index = extra.end;
const captures_len: u32 = if (small.has_captures_len) blk: {
const captures_len = zir.extra[extra_index];
extra_index += 1;
break :blk captures_len;
} else 0;
const decls_len: u32 = if (small.has_decls_len) blk: {
const decls_len = zir.extra[extra_index];
extra_index += 1;
break :blk decls_len;
} else 0;
const captures: []const Inst.Capture = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const capture_names: []const NullTerminatedString = @ptrCast(zir.extra[extra_index..][0..captures_len]);
extra_index += captures_len;
const decls: []const Inst.Index = @ptrCast(zir.extra[extra_index..][0..decls_len]);
extra_index += decls_len;
return .{
.src_line = extra.data.src_line,
.src_column = extra.data.src_column,
.src_node = extra.data.src_node,
.name_strategy = small.name_strategy,
.captures = captures,
.capture_names = capture_names,
.decls = decls,
};
}
pub const UnwrappedOpaqueDecl = struct {
src_line: u32,
src_column: u32,
src_node: Ast.Node.Index,
name_strategy: Inst.NameStrategy,
captures: []const Inst.Capture,
capture_names: []const NullTerminatedString,
decls: []const Inst.Index,
};