update stage2 sources

This commit is contained in:
Techarix 2023-05-08 20:38:05 +02:00
parent 28863f4158
commit f2c064c957
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_decl,
has_field, has_field,
import, import,
in_comptime,
int_cast, int_cast,
int_to_enum, int_to_enum,
int_to_error, int_to_error,
@ -560,6 +561,13 @@ pub const list = list: {
.param_count = 1, .param_count = 1,
}, },
}, },
.{
"@inComptime",
.{
.tag = .in_comptime,
.param_count = 0,
},
},
.{ .{
"@intCast", "@intCast",
.{ .{
@ -600,28 +608,28 @@ pub const list = list: {
"@max", "@max",
.{ .{
.tag = .max, .tag = .max,
.param_count = 2, .param_count = null,
}, },
}, },
.{ .{
"@memcpy", "@memcpy",
.{ .{
.tag = .memcpy, .tag = .memcpy,
.param_count = 3, .param_count = 2,
}, },
}, },
.{ .{
"@memset", "@memset",
.{ .{
.tag = .memset, .tag = .memset,
.param_count = 3, .param_count = 2,
}, },
}, },
.{ .{
"@min", "@min",
.{ .{
.tag = .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); 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 tree = src_loc.handle.tree;
const node = src_loc.declRelativeToNodeIndex(node_off); const node = src_loc.declRelativeToNodeIndex(node_off);
const node_tags = tree.nodes.items(.tag); const node_tags = tree.nodes.items(.tag);
@ -231,6 +231,55 @@ pub const SrcLoc = struct {
}; };
return nodeToSpan(tree, src_node); 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| { .node_offset_bin_lhs => |node_off| {
const tree = src_loc.handle.tree; const tree = src_loc.handle.tree;
const node = src_loc.declRelativeToNodeIndex(node_off); const node = src_loc.declRelativeToNodeIndex(node_off);
@ -608,19 +657,19 @@ pub const LazySrcLoc = union(enum) {
/// value is being set to this tag. /// value is being set to this tag.
unneeded, unneeded,
/// Means the source location points to an entire file; not any particular /// 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, entire_file,
/// The source location points to a byte offset within a source file, /// The source location points to a byte offset within a source file,
/// offset from 0. The source file is determined contextually. /// 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, byte_abs: u32,
/// The source location points to a token within a source file, /// The source location points to a token within a source file,
/// offset from 0. The source file is determined contextually. /// 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, token_abs: u32,
/// The source location points to an AST node within a source file, /// The source location points to an AST node within a source file,
/// offset from 0. The source file is determined contextually. /// 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, node_abs: u32,
/// The source location points to a byte offset within a source file, /// The source location points to a byte offset within a source file,
/// offset from the byte offset of the Decl within the 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 source location points to the initializer of a var decl.
/// The Decl is determined contextually. /// The Decl is determined contextually.
node_offset_var_decl_init: i32, 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 /// The source location points to the first parameter of a builtin
/// function call, found by taking this AST node index offset from the containing /// 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 /// 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 source location points to the RHS of an assignment.
/// The Decl is determined contextually. /// The Decl is determined contextually.
node_offset_store_operand: i32, 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 { pub fn nodeOffset(node_offset: i32) LazySrcLoc {
return .{ .node_offset = node_offset }; return .{ .node_offset = node_offset };
@ -901,7 +958,6 @@ pub const LazySrcLoc = union(enum) {
.node_offset_var_decl_section, .node_offset_var_decl_section,
.node_offset_var_decl_addrspace, .node_offset_var_decl_addrspace,
.node_offset_var_decl_init, .node_offset_var_decl_init,
.node_offset_for_cond,
.node_offset_builtin_call_arg0, .node_offset_builtin_call_arg0,
.node_offset_builtin_call_arg1, .node_offset_builtin_call_arg1,
.node_offset_builtin_call_arg2, .node_offset_builtin_call_arg2,
@ -950,6 +1006,8 @@ pub const LazySrcLoc = union(enum) {
.node_offset_init_ty, .node_offset_init_ty,
.node_offset_store_ptr, .node_offset_store_ptr,
.node_offset_store_operand, .node_offset_store_operand,
.for_input,
.for_capture_from_input,
=> .{ => .{
.handle = handle, .handle = handle,
.parent_decl_node = src_node, .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. /// A labeled block of code, which can return a value.
/// Uses the `pl_node` union field. Payload is `Block`. /// Uses the `pl_node` union field. Payload is `Block`.
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 /// A list of instructions which are analyzed in the parent context, without
/// generating a runtime block. Must terminate with an "inline" variant of /// generating a runtime block. Must terminate with an "inline" variant of
/// a noreturn instruction. /// a noreturn instruction.
@ -335,14 +338,8 @@ pub const Inst = struct {
/// payload value, as if `err_union_payload_unsafe` was executed on the operand. /// payload value, as if `err_union_payload_unsafe` was executed on the operand.
/// Uses the `pl_node` union field. Payload is `Try`. /// Uses the `pl_node` union field. Payload is `Try`.
@"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. /// Same as `try` except the operand is a pointer and the result is a pointer.
try_ptr, 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. /// An error set type definition. Contains a list of field names.
/// Uses the `pl_node` union field. Payload is `ErrorSetDecl`. /// Uses the `pl_node` union field. Payload is `ErrorSetDecl`.
error_set_decl, error_set_decl,
@ -720,9 +717,6 @@ pub const Inst = struct {
/// because it must use one of them to find out the struct type. /// because it must use one of them to find out the struct type.
/// Uses the `pl_node` field. Payload is `Block`. /// Uses the `pl_node` field. Payload is `Block`.
validate_struct_init, 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 /// 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 /// array initialization expression, and emits a compile error if the number of
/// elements does not match the array type. /// 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. /// because it must use one of them to find out the array type.
/// Uses the `pl_node` field. Payload is `Block`. /// Uses the `pl_node` field. Payload is `Block`.
validate_array_init, 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 (.*). /// Check that operand type supports the dereference operand (.*).
/// Uses the `un_node` field. /// Uses the `un_node` field.
validate_deref, validate_deref,
@ -803,8 +794,6 @@ pub const Inst = struct {
error_name, error_name,
/// Implement builtin `@panic`. Uses `un_node`. /// Implement builtin `@panic`. Uses `un_node`.
panic, panic,
/// Same as `panic` but forces comptime.
panic_comptime,
/// Implements `@trap`. /// Implements `@trap`.
/// Uses the `node` field. /// Uses the `node` field.
trap, trap,
@ -930,15 +919,15 @@ pub const Inst = struct {
/// Uses the `pl_node` union field with payload `FieldParentPtr`. /// Uses the `pl_node` union field with payload `FieldParentPtr`.
field_parent_ptr, field_parent_ptr,
/// Implements the `@memcpy` builtin. /// Implements the `@memcpy` builtin.
/// Uses the `pl_node` union field with payload `Memcpy`. /// Uses the `pl_node` union field with payload `Bin`.
memcpy, memcpy,
/// Implements the `@memset` builtin. /// Implements the `@memset` builtin.
/// Uses the `pl_node` union field with payload `Memset`. /// Uses the `pl_node` union field with payload `Bin`.
memset, memset,
/// Implements the `@min` builtin. /// Implements the `@min` builtin for 2 args.
/// Uses the `pl_node` union field with payload `Bin` /// Uses the `pl_node` union field with payload `Bin`
min, min,
/// Implements the `@max` builtin. /// Implements the `@max` builtin for 2 args.
/// Uses the `pl_node` union field with payload `Bin` /// Uses the `pl_node` union field with payload `Bin`
max, max,
/// Implements the `@cImport` builtin. /// Implements the `@cImport` builtin.
@ -1047,6 +1036,7 @@ pub const Inst = struct {
.bitcast, .bitcast,
.bit_or, .bit_or,
.block, .block,
.block_comptime,
.block_inline, .block_inline,
.suspend_block, .suspend_block,
.loop, .loop,
@ -1159,9 +1149,7 @@ pub const Inst = struct {
.validate_array_init_ty, .validate_array_init_ty,
.validate_struct_init_ty, .validate_struct_init_ty,
.validate_struct_init, .validate_struct_init,
.validate_struct_init_comptime,
.validate_array_init, .validate_array_init,
.validate_array_init_comptime,
.validate_deref, .validate_deref,
.struct_init_empty, .struct_init_empty,
.struct_init, .struct_init,
@ -1251,8 +1239,6 @@ pub const Inst = struct {
.ret_type, .ret_type,
.@"try", .@"try",
.try_ptr, .try_ptr,
//.try_inline,
//.try_ptr_inline,
.@"defer", .@"defer",
.defer_err_code, .defer_err_code,
.save_err_ret_index, .save_err_ret_index,
@ -1273,7 +1259,6 @@ pub const Inst = struct {
.repeat, .repeat,
.repeat_inline, .repeat_inline,
.panic, .panic,
.panic_comptime,
.trap, .trap,
.check_comptime_control_flow, .check_comptime_control_flow,
=> true, => true,
@ -1315,9 +1300,7 @@ pub const Inst = struct {
.validate_array_init_ty, .validate_array_init_ty,
.validate_struct_init_ty, .validate_struct_init_ty,
.validate_struct_init, .validate_struct_init,
.validate_struct_init_comptime,
.validate_array_init, .validate_array_init,
.validate_array_init_comptime,
.validate_deref, .validate_deref,
.@"export", .@"export",
.export_value, .export_value,
@ -1362,6 +1345,7 @@ pub const Inst = struct {
.bitcast, .bitcast,
.bit_or, .bit_or,
.block, .block,
.block_comptime,
.block_inline, .block_inline,
.suspend_block, .suspend_block,
.loop, .loop,
@ -1549,13 +1533,10 @@ pub const Inst = struct {
.repeat, .repeat,
.repeat_inline, .repeat_inline,
.panic, .panic,
.panic_comptime,
.trap, .trap,
.for_len, .for_len,
.@"try", .@"try",
.try_ptr, .try_ptr,
//.try_inline,
//.try_ptr_inline,
=> false, => false,
.extended => switch (data.extended.opcode) { .extended => switch (data.extended.opcode) {
@ -1600,6 +1581,7 @@ pub const Inst = struct {
.bit_not = .un_node, .bit_not = .un_node,
.bit_or = .pl_node, .bit_or = .pl_node,
.block = .pl_node, .block = .pl_node,
.block_comptime = .pl_node,
.block_inline = .pl_node, .block_inline = .pl_node,
.suspend_block = .pl_node, .suspend_block = .pl_node,
.bool_not = .un_node, .bool_not = .un_node,
@ -1621,8 +1603,6 @@ pub const Inst = struct {
.condbr_inline = .pl_node, .condbr_inline = .pl_node,
.@"try" = .pl_node, .@"try" = .pl_node,
.try_ptr = .pl_node, .try_ptr = .pl_node,
//.try_inline = .pl_node,
//.try_ptr_inline = .pl_node,
.error_set_decl = .pl_node, .error_set_decl = .pl_node,
.error_set_decl_anon = .pl_node, .error_set_decl_anon = .pl_node,
.error_set_decl_func = .pl_node, .error_set_decl_func = .pl_node,
@ -1718,9 +1698,7 @@ pub const Inst = struct {
.validate_array_init_ty = .pl_node, .validate_array_init_ty = .pl_node,
.validate_struct_init_ty = .un_node, .validate_struct_init_ty = .un_node,
.validate_struct_init = .pl_node, .validate_struct_init = .pl_node,
.validate_struct_init_comptime = .pl_node,
.validate_array_init = .pl_node, .validate_array_init = .pl_node,
.validate_array_init_comptime = .pl_node,
.validate_deref = .un_node, .validate_deref = .un_node,
.struct_init_empty = .un_node, .struct_init_empty = .un_node,
.field_type = .pl_node, .field_type = .pl_node,
@ -1747,7 +1725,6 @@ pub const Inst = struct {
.embed_file = .un_node, .embed_file = .un_node,
.error_name = .un_node, .error_name = .un_node,
.panic = .un_node, .panic = .un_node,
.panic_comptime = .un_node,
.trap = .node, .trap = .node,
.set_runtime_safety = .un_node, .set_runtime_safety = .un_node,
.sqrt = .un_node, .sqrt = .un_node,
@ -1925,10 +1902,20 @@ pub const Inst = struct {
compile_log, compile_log,
/// The builtin `@TypeOf` which returns the type after Peer Type Resolution /// The builtin `@TypeOf` which returns the type after Peer Type Resolution
/// of one or more params. /// of one or more params.
/// `operand` is payload index to `NodeMultiOp`. /// `operand` is payload index to `TypeOfPeer`.
/// `small` is `operands_len`. /// `small` is `operands_len`.
/// The AST node is the builtin call. /// The AST node is the builtin call.
typeof_peer, 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. /// Implements the `@addWithOverflow` builtin.
/// `operand` is payload index to `BinNode`. /// `operand` is payload index to `BinNode`.
/// `small` is unused. /// `small` is unused.
@ -1989,7 +1976,7 @@ pub const Inst = struct {
/// `operand` is `src_node: i32`. /// `operand` is `src_node: i32`.
breakpoint, breakpoint,
/// Implements the `@select` builtin. /// Implements the `@select` builtin.
/// operand` is payload index to `Select`. /// `operand` is payload index to `Select`.
select, select,
/// Implement builtin `@errToInt`. /// Implement builtin `@errToInt`.
/// `operand` is payload index to `UnNode`. /// `operand` is payload index to `UnNode`.
@ -2009,15 +1996,15 @@ pub const Inst = struct {
/// `operand` is payload index to `Cmpxchg`. /// `operand` is payload index to `Cmpxchg`.
cmpxchg, cmpxchg,
/// Implement the builtin `@addrSpaceCast` /// 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, addrspace_cast,
/// Implement builtin `@cVaArg`. /// Implement builtin `@cVaArg`.
/// `operand` is payload index to `BinNode`. /// `operand` is payload index to `BinNode`.
c_va_arg, c_va_arg,
/// Implement builtin `@cVaStart`. /// Implement builtin `@cVaCopy`.
/// `operand` is payload index to `UnNode`. /// `operand` is payload index to `UnNode`.
c_va_copy, c_va_copy,
/// Implement builtin `@cVaStart`. /// Implement builtin `@cVaEnd`.
/// `operand` is payload index to `UnNode`. /// `operand` is payload index to `UnNode`.
c_va_end, c_va_end,
/// Implement builtin `@cVaStart`. /// Implement builtin `@cVaStart`.
@ -2038,6 +2025,12 @@ pub const Inst = struct {
/// Implements the `@workGroupId` builtin. /// Implements the `@workGroupId` builtin.
/// `operand` is payload index to `UnNode`. /// `operand` is payload index to `UnNode`.
work_group_id, 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 { pub const InstData = struct {
opcode: Extended, opcode: Extended,
@ -2287,7 +2280,6 @@ pub const Inst = struct {
/// Offset from Decl AST node index. /// Offset from Decl AST node index.
/// `Tag` determines which kind of AST node this points to. /// `Tag` determines which kind of AST node this points to.
src_node: i32, src_node: i32,
force_comptime: bool,
pub fn src(self: @This()) LazySrcLoc { pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node); return LazySrcLoc.nodeOffset(self.src_node);
@ -2602,9 +2594,8 @@ pub const Inst = struct {
pub const Flags = packed struct { pub const Flags = packed struct {
is_nosuspend: bool, is_nosuspend: bool,
is_comptime: bool,
ensure_result_used: bool, ensure_result_used: bool,
_: u29 = undefined, _: u30 = undefined,
comptime { comptime {
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32) if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
@ -3200,18 +3191,6 @@ pub const Inst = struct {
field_ptr: Ref, 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 { pub const Shuffle = struct {
elem_type: Ref, elem_type: Ref,
a: Ref, a: Ref,
@ -3349,6 +3328,7 @@ pub const DeclIterator = struct {
pub const Item = struct { pub const Item = struct {
name: [:0]const u8, name: [:0]const u8,
sub_index: u32, sub_index: u32,
flags: u4,
}; };
pub fn next(it: *DeclIterator) ?Item { pub fn next(it: *DeclIterator) ?Item {
@ -3373,6 +3353,7 @@ pub const DeclIterator = struct {
return Item{ return Item{
.sub_index = sub_index, .sub_index = sub_index,
.name = name, .name = name,
.flags = flags,
}; };
} }
}; };
@ -3592,7 +3573,7 @@ fn findDeclsInner(
// Block instructions, recurse over the bodies. // Block instructions, recurse over the bodies.
.block, .block_inline => { .block, .block_comptime, .block_inline => {
const inst_data = datas[inst].pl_node; const inst_data = datas[inst].pl_node;
const extra = zir.extraData(Inst.Block, inst_data.payload_index); const extra = zir.extraData(Inst.Block, inst_data.payload_index);
const body = zir.extra[extra.end..][0..extra.data.body_len]; 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, 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_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]; const param_body = zir.extra[param_block.end..][0..param_block.data.body_len];
var total_params_len: u32 = 0; var total_params_len: u32 = 0;