Merge pull request #1178 from Techatrix/stage2-zir

update stage2 sources
This commit is contained in:
Lee Cannon 2023-05-08 14:06:31 -07:00 committed by GitHub
commit f6c808a4b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 414 additions and 318 deletions

File diff suppressed because it is too large Load Diff

View File

@ -58,6 +58,7 @@ pub const Tag = enum {
has_decl,
has_field,
import,
in_comptime,
int_cast,
int_to_enum,
int_to_error,
@ -560,6 +561,13 @@ pub const list = list: {
.param_count = 1,
},
},
.{
"@inComptime",
.{
.tag = .in_comptime,
.param_count = 0,
},
},
.{
"@intCast",
.{
@ -600,28 +608,28 @@ pub const list = list: {
"@max",
.{
.tag = .max,
.param_count = 2,
.param_count = null,
},
},
.{
"@memcpy",
.{
.tag = .memcpy,
.param_count = 3,
.param_count = 2,
},
},
.{
"@memset",
.{
.tag = .memset,
.param_count = 3,
.param_count = 2,
},
},
.{
"@min",
.{
.tag = .min,
.param_count = 2,
.param_count = null,
},
},
.{

View File

@ -209,7 +209,7 @@ pub const SrcLoc = struct {
return nodeToSpan(tree, node_datas[asm_output].lhs);
},
.node_offset_for_cond, .node_offset_if_cond => |node_off| {
.node_offset_if_cond => |node_off| {
const tree = src_loc.handle.tree;
const node = src_loc.declRelativeToNodeIndex(node_off);
const node_tags = tree.nodes.items(.tag);
@ -231,6 +231,55 @@ pub const SrcLoc = struct {
};
return nodeToSpan(tree, src_node);
},
.for_input => |for_input| {
const tree = try src_loc.handle.tree;
const node = src_loc.declRelativeToNodeIndex(for_input.for_node_offset);
const for_full = tree.fullFor(node).?;
const src_node = for_full.ast.inputs[for_input.input_index];
return nodeToSpan(tree, src_node);
},
.for_capture_from_input => |node_off| {
const tree = try src_loc.handle.tree;
const token_tags = tree.tokens.items(.tag);
const input_node = src_loc.declRelativeToNodeIndex(node_off);
// We have to actually linear scan the whole AST to find the for loop
// that contains this input.
const node_tags = tree.nodes.items(.tag);
for (node_tags, 0..) |node_tag, node_usize| {
const node = @intCast(Ast.Node.Index, node_usize);
switch (node_tag) {
.for_simple, .@"for" => {
const for_full = tree.fullFor(node).?;
for (for_full.ast.inputs, 0..) |input, input_index| {
if (input_node == input) {
var count = input_index;
var tok = for_full.payload_token;
while (true) {
switch (token_tags[tok]) {
.comma => {
count -= 1;
tok += 1;
},
.identifier => {
if (count == 0)
return tokensToSpan(tree, tok, tok + 1, tok);
tok += 1;
},
.asterisk => {
if (count == 0)
return tokensToSpan(tree, tok, tok + 2, tok);
tok += 1;
},
else => unreachable,
}
}
}
}
},
else => continue,
}
} else unreachable;
},
.node_offset_bin_lhs => |node_off| {
const tree = src_loc.handle.tree;
const node = src_loc.declRelativeToNodeIndex(node_off);
@ -608,19 +657,19 @@ pub const LazySrcLoc = union(enum) {
/// value is being set to this tag.
unneeded,
/// Means the source location points to an entire file; not any particular
/// location within the file. `handle` union field will be active.
/// location within the file. `file_scope` union field will be active.
entire_file,
/// The source location points to a byte offset within a source file,
/// offset from 0. The source file is determined contextually.
/// Inside a `SrcLoc`, the `handle` union field will be active.
/// Inside a `SrcLoc`, the `file_scope` union field will be active.
byte_abs: u32,
/// The source location points to a token within a source file,
/// offset from 0. The source file is determined contextually.
/// Inside a `SrcLoc`, the `handle` union field will be active.
/// Inside a `SrcLoc`, the `file_scope` union field will be active.
token_abs: u32,
/// The source location points to an AST node within a source file,
/// offset from 0. The source file is determined contextually.
/// Inside a `SrcLoc`, the `handle` union field will be active.
/// Inside a `SrcLoc`, the `file_scope` union field will be active.
node_abs: u32,
/// The source location points to a byte offset within a source file,
/// offset from the byte offset of the Decl within the file.
@ -658,12 +707,6 @@ pub const LazySrcLoc = union(enum) {
/// The source location points to the initializer of a var decl.
/// The Decl is determined contextually.
node_offset_var_decl_init: i32,
/// The source location points to a for loop condition expression,
/// found by taking this AST node index offset from the containing
/// Decl AST node, which points to a for loop AST node. Next, navigate
/// to the condition expression.
/// The Decl is determined contextually.
node_offset_for_cond: i32,
/// The source location points to the first parameter of a builtin
/// function call, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a builtin call AST node. Next, navigate
@ -873,6 +916,20 @@ pub const LazySrcLoc = union(enum) {
/// The source location points to the RHS of an assignment.
/// The Decl is determined contextually.
node_offset_store_operand: i32,
/// The source location points to a for loop input.
/// The Decl is determined contextually.
for_input: struct {
/// Points to the for loop AST node.
for_node_offset: i32,
/// Picks one of the inputs from the condition.
input_index: u32,
},
/// The source location points to one of the captures of a for loop, found
/// by taking this AST node index offset from the containing
/// Decl AST node, which points to one of the input nodes of a for loop.
/// Next, navigate to the corresponding capture.
/// The Decl is determined contextually.
for_capture_from_input: i32,
pub fn nodeOffset(node_offset: i32) LazySrcLoc {
return .{ .node_offset = node_offset };
@ -901,7 +958,6 @@ pub const LazySrcLoc = union(enum) {
.node_offset_var_decl_section,
.node_offset_var_decl_addrspace,
.node_offset_var_decl_init,
.node_offset_for_cond,
.node_offset_builtin_call_arg0,
.node_offset_builtin_call_arg1,
.node_offset_builtin_call_arg2,
@ -950,6 +1006,8 @@ pub const LazySrcLoc = union(enum) {
.node_offset_init_ty,
.node_offset_store_ptr,
.node_offset_store_operand,
.for_input,
.for_capture_from_input,
=> .{
.handle = handle,
.parent_decl_node = src_node,

View File

@ -255,6 +255,9 @@ pub const Inst = struct {
/// A labeled block of code, which can return a value.
/// Uses the `pl_node` union field. Payload is `Block`.
block,
/// Like `block`, but forces full evaluation of its contents at compile-time.
/// Uses the `pl_node` union field. Payload is `Block`.
block_comptime,
/// A list of instructions which are analyzed in the parent context, without
/// generating a runtime block. Must terminate with an "inline" variant of
/// a noreturn instruction.
@ -335,14 +338,8 @@ pub const Inst = struct {
/// payload value, as if `err_union_payload_unsafe` was executed on the operand.
/// Uses the `pl_node` union field. Payload is `Try`.
@"try",
///// Same as `try` except the operand is coerced to a comptime value, and
///// only the taken branch is analyzed. The block must terminate with an "inline"
///// variant of a noreturn instruction.
//try_inline,
/// Same as `try` except the operand is a pointer and the result is a pointer.
try_ptr,
///// Same as `try_inline` except the operand is a pointer and the result is a pointer.
//try_ptr_inline,
/// An error set type definition. Contains a list of field names.
/// Uses the `pl_node` union field. Payload is `ErrorSetDecl`.
error_set_decl,
@ -720,9 +717,6 @@ pub const Inst = struct {
/// because it must use one of them to find out the struct type.
/// Uses the `pl_node` field. Payload is `Block`.
validate_struct_init,
/// Same as `validate_struct_init` but additionally communicates that the
/// resulting struct initialization value is within a comptime scope.
validate_struct_init_comptime,
/// Given a set of `elem_ptr_imm` instructions, assumes they are all part of an
/// array initialization expression, and emits a compile error if the number of
/// elements does not match the array type.
@ -730,9 +724,6 @@ pub const Inst = struct {
/// because it must use one of them to find out the array type.
/// Uses the `pl_node` field. Payload is `Block`.
validate_array_init,
/// Same as `validate_array_init` but additionally communicates that the
/// resulting array initialization value is within a comptime scope.
validate_array_init_comptime,
/// Check that operand type supports the dereference operand (.*).
/// Uses the `un_node` field.
validate_deref,
@ -803,8 +794,6 @@ pub const Inst = struct {
error_name,
/// Implement builtin `@panic`. Uses `un_node`.
panic,
/// Same as `panic` but forces comptime.
panic_comptime,
/// Implements `@trap`.
/// Uses the `node` field.
trap,
@ -930,15 +919,15 @@ pub const Inst = struct {
/// Uses the `pl_node` union field with payload `FieldParentPtr`.
field_parent_ptr,
/// Implements the `@memcpy` builtin.
/// Uses the `pl_node` union field with payload `Memcpy`.
/// Uses the `pl_node` union field with payload `Bin`.
memcpy,
/// Implements the `@memset` builtin.
/// Uses the `pl_node` union field with payload `Memset`.
/// Uses the `pl_node` union field with payload `Bin`.
memset,
/// Implements the `@min` builtin.
/// Implements the `@min` builtin for 2 args.
/// Uses the `pl_node` union field with payload `Bin`
min,
/// Implements the `@max` builtin.
/// Implements the `@max` builtin for 2 args.
/// Uses the `pl_node` union field with payload `Bin`
max,
/// Implements the `@cImport` builtin.
@ -1047,6 +1036,7 @@ pub const Inst = struct {
.bitcast,
.bit_or,
.block,
.block_comptime,
.block_inline,
.suspend_block,
.loop,
@ -1159,9 +1149,7 @@ pub const Inst = struct {
.validate_array_init_ty,
.validate_struct_init_ty,
.validate_struct_init,
.validate_struct_init_comptime,
.validate_array_init,
.validate_array_init_comptime,
.validate_deref,
.struct_init_empty,
.struct_init,
@ -1251,8 +1239,6 @@ pub const Inst = struct {
.ret_type,
.@"try",
.try_ptr,
//.try_inline,
//.try_ptr_inline,
.@"defer",
.defer_err_code,
.save_err_ret_index,
@ -1273,7 +1259,6 @@ pub const Inst = struct {
.repeat,
.repeat_inline,
.panic,
.panic_comptime,
.trap,
.check_comptime_control_flow,
=> true,
@ -1315,9 +1300,7 @@ pub const Inst = struct {
.validate_array_init_ty,
.validate_struct_init_ty,
.validate_struct_init,
.validate_struct_init_comptime,
.validate_array_init,
.validate_array_init_comptime,
.validate_deref,
.@"export",
.export_value,
@ -1362,6 +1345,7 @@ pub const Inst = struct {
.bitcast,
.bit_or,
.block,
.block_comptime,
.block_inline,
.suspend_block,
.loop,
@ -1549,13 +1533,10 @@ pub const Inst = struct {
.repeat,
.repeat_inline,
.panic,
.panic_comptime,
.trap,
.for_len,
.@"try",
.try_ptr,
//.try_inline,
//.try_ptr_inline,
=> false,
.extended => switch (data.extended.opcode) {
@ -1600,6 +1581,7 @@ pub const Inst = struct {
.bit_not = .un_node,
.bit_or = .pl_node,
.block = .pl_node,
.block_comptime = .pl_node,
.block_inline = .pl_node,
.suspend_block = .pl_node,
.bool_not = .un_node,
@ -1621,8 +1603,6 @@ pub const Inst = struct {
.condbr_inline = .pl_node,
.@"try" = .pl_node,
.try_ptr = .pl_node,
//.try_inline = .pl_node,
//.try_ptr_inline = .pl_node,
.error_set_decl = .pl_node,
.error_set_decl_anon = .pl_node,
.error_set_decl_func = .pl_node,
@ -1718,9 +1698,7 @@ pub const Inst = struct {
.validate_array_init_ty = .pl_node,
.validate_struct_init_ty = .un_node,
.validate_struct_init = .pl_node,
.validate_struct_init_comptime = .pl_node,
.validate_array_init = .pl_node,
.validate_array_init_comptime = .pl_node,
.validate_deref = .un_node,
.struct_init_empty = .un_node,
.field_type = .pl_node,
@ -1747,7 +1725,6 @@ pub const Inst = struct {
.embed_file = .un_node,
.error_name = .un_node,
.panic = .un_node,
.panic_comptime = .un_node,
.trap = .node,
.set_runtime_safety = .un_node,
.sqrt = .un_node,
@ -1925,10 +1902,20 @@ pub const Inst = struct {
compile_log,
/// The builtin `@TypeOf` which returns the type after Peer Type Resolution
/// of one or more params.
/// `operand` is payload index to `NodeMultiOp`.
/// `operand` is payload index to `TypeOfPeer`.
/// `small` is `operands_len`.
/// The AST node is the builtin call.
typeof_peer,
/// Implements the `@min` builtin for more than 2 args.
/// `operand` is payload index to `NodeMultiOp`.
/// `small` is `operands_len`.
/// The AST node is the builtin call.
min_multi,
/// Implements the `@max` builtin for more than 2 args.
/// `operand` is payload index to `NodeMultiOp`.
/// `small` is `operands_len`.
/// The AST node is the builtin call.
max_multi,
/// Implements the `@addWithOverflow` builtin.
/// `operand` is payload index to `BinNode`.
/// `small` is unused.
@ -1989,7 +1976,7 @@ pub const Inst = struct {
/// `operand` is `src_node: i32`.
breakpoint,
/// Implements the `@select` builtin.
/// operand` is payload index to `Select`.
/// `operand` is payload index to `Select`.
select,
/// Implement builtin `@errToInt`.
/// `operand` is payload index to `UnNode`.
@ -2009,15 +1996,15 @@ pub const Inst = struct {
/// `operand` is payload index to `Cmpxchg`.
cmpxchg,
/// Implement the builtin `@addrSpaceCast`
/// `Operand` is payload index to `BinNode`. `lhs` is dest type, `rhs` is operand.
/// `operand` is payload index to `BinNode`. `lhs` is dest type, `rhs` is operand.
addrspace_cast,
/// Implement builtin `@cVaArg`.
/// `operand` is payload index to `BinNode`.
c_va_arg,
/// Implement builtin `@cVaStart`.
/// Implement builtin `@cVaCopy`.
/// `operand` is payload index to `UnNode`.
c_va_copy,
/// Implement builtin `@cVaStart`.
/// Implement builtin `@cVaEnd`.
/// `operand` is payload index to `UnNode`.
c_va_end,
/// Implement builtin `@cVaStart`.
@ -2038,6 +2025,12 @@ pub const Inst = struct {
/// Implements the `@workGroupId` builtin.
/// `operand` is payload index to `UnNode`.
work_group_id,
/// Implements the `@inComptime` builtin.
/// `operand` is `src_node: i32`.
in_comptime,
/// Used as a placeholder for the capture of an `errdefer`.
/// This is replaced by Sema with the captured value.
errdefer_err_code,
pub const InstData = struct {
opcode: Extended,
@ -2287,7 +2280,6 @@ pub const Inst = struct {
/// Offset from Decl AST node index.
/// `Tag` determines which kind of AST node this points to.
src_node: i32,
force_comptime: bool,
pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
@ -2602,9 +2594,8 @@ pub const Inst = struct {
pub const Flags = packed struct {
is_nosuspend: bool,
is_comptime: bool,
ensure_result_used: bool,
_: u29 = undefined,
_: u30 = undefined,
comptime {
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
@ -3200,18 +3191,6 @@ pub const Inst = struct {
field_ptr: Ref,
};
pub const Memcpy = struct {
dest: Ref,
source: Ref,
byte_count: Ref,
};
pub const Memset = struct {
dest: Ref,
byte: Ref,
byte_count: Ref,
};
pub const Shuffle = struct {
elem_type: Ref,
a: Ref,
@ -3349,6 +3328,7 @@ pub const DeclIterator = struct {
pub const Item = struct {
name: [:0]const u8,
sub_index: u32,
flags: u4,
};
pub fn next(it: *DeclIterator) ?Item {
@ -3373,6 +3353,7 @@ pub const DeclIterator = struct {
return Item{
.sub_index = sub_index,
.name = name,
.flags = flags,
};
}
};
@ -3592,7 +3573,7 @@ fn findDeclsInner(
// Block instructions, recurse over the bodies.
.block, .block_inline => {
.block, .block_comptime, .block_inline => {
const inst_data = datas[inst].pl_node;
const extra = zir.extraData(Inst.Block, inst_data.payload_index);
const body = zir.extra[extra.end..][0..extra.data.body_len];
@ -3819,7 +3800,9 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
},
else => unreachable,
};
assert(tags[info.param_block] == .block or tags[info.param_block] == .block_inline);
assert(tags[info.param_block] == .block or
tags[info.param_block] == .block_comptime or
tags[info.param_block] == .block_inline);
const param_block = zir.extraData(Inst.Block, datas[info.param_block].pl_node.payload_index);
const param_body = zir.extra[param_block.end..][0..param_block.data.body_len];
var total_params_len: u32 = 0;