authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-29 22:50:53-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-29 22:50:53-04:00
logcc39d453c45645296cd771c94d97b8a77469be91
tree8d9c857255ac15fe2f8125789f8a42a7a03573eb
parenta0a2ce92ca129d28e22c63f7bace1672c43776b5
parent596f7df02e78adf334eed4a1f14eafa31ca611b9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11549 from Vexu/stage2-fixes

Stage2: fix comptime unreachable, adjust Zir.Extended

8 files changed, 283 insertions(+), 245 deletions(-)

src/AstGen.zig+73-41
...@@ -72,6 +72,7 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {...@@ -72,6 +72,7 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
72 i32 => @bitCast(u32, @field(extra, field.name)),72 i32 => @bitCast(u32, @field(extra, field.name)),
73 Zir.Inst.Call.Flags => @bitCast(u32, @field(extra, field.name)),73 Zir.Inst.Call.Flags => @bitCast(u32, @field(extra, field.name)),
74 Zir.Inst.SwitchBlock.Bits => @bitCast(u32, @field(extra, field.name)),74 Zir.Inst.SwitchBlock.Bits => @bitCast(u32, @field(extra, field.name)),
75 Zir.Inst.ExtendedFunc.Bits => @bitCast(u32, @field(extra, field.name)),
75 else => @compileError("bad field type"),76 else => @compileError("bad field type"),
76 };77 };
77 i += 1;78 i += 1;
...@@ -740,7 +741,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr...@@ -740,7 +741,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
740 _ = try gz.addAsIndex(.{741 _ = try gz.addAsIndex(.{
741 .tag = .@"unreachable",742 .tag = .@"unreachable",
742 .data = .{ .@"unreachable" = .{743 .data = .{ .@"unreachable" = .{
743 .safety = true,744 .force_comptime = gz.force_comptime,
744 .src_node = gz.nodeIndexToRelative(node),745 .src_node = gz.nodeIndexToRelative(node),
745 } },746 } },
746 });747 });
...@@ -1078,8 +1079,14 @@ fn awaitExpr(...@@ -1078,8 +1079,14 @@ fn awaitExpr(
1078 });1079 });
1079 }1080 }
1080 const operand = try expr(gz, scope, .none, rhs_node);1081 const operand = try expr(gz, scope, .none, rhs_node);
1081 const tag: Zir.Inst.Tag = if (gz.nosuspend_node != 0) .await_nosuspend else .@"await";1082 const result = if (gz.nosuspend_node != 0)
1082 const result = try gz.addUnNode(tag, operand, node);1083 try gz.addExtendedPayload(.await_nosuspend, Zir.Inst.UnNode{
1084 .node = gz.nodeIndexToRelative(node),
1085 .operand = operand,
1086 })
1087 else
1088 try gz.addUnNode(.@"await", operand, node);
1089
1083 return rvalue(gz, rl, result, node);1090 return rvalue(gz, rl, result, node);
1084}1091}
10851092
...@@ -2239,6 +2246,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner...@@ -2239,6 +2246,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2239 .field_val_named,2246 .field_val_named,
2240 .func,2247 .func,
2241 .func_inferred,2248 .func_inferred,
2249 .func_extended,
2242 .int,2250 .int,
2243 .int_big,2251 .int_big,
2244 .float,2252 .float,
...@@ -2349,7 +2357,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner...@@ -2349,7 +2357,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2349 .int_to_ptr,2357 .int_to_ptr,
2350 .float_cast,2358 .float_cast,
2351 .int_cast,2359 .int_cast,
2352 .err_set_cast,
2353 .ptr_cast,2360 .ptr_cast,
2354 .truncate,2361 .truncate,
2355 .align_cast,2362 .align_cast,
...@@ -2386,15 +2393,24 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner...@@ -2386,15 +2393,24 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2386 .c_import,2393 .c_import,
2387 .@"resume",2394 .@"resume",
2388 .@"await",2395 .@"await",
2389 .await_nosuspend,
2390 .ret_err_value_code,2396 .ret_err_value_code,
2391 .extended,
2392 .closure_get,2397 .closure_get,
2393 .array_base_ptr,2398 .array_base_ptr,
2394 .field_base_ptr,2399 .field_base_ptr,
2395 .param_type,2400 .param_type,
2401 .ret_ptr,
2402 .ret_type,
2396 => break :b false,2403 => break :b false,
23972404
2405 .extended => switch (gz.astgen.instructions.items(.data)[inst].extended.opcode) {
2406 .breakpoint,
2407 .fence,
2408 .set_align_stack,
2409 .set_float_mode,
2410 => break :b true,
2411 else => break :b false,
2412 },
2413
2398 // ZIR instructions that are always `noreturn`.2414 // ZIR instructions that are always `noreturn`.
2399 .@"break",2415 .@"break",
2400 .break_inline,2416 .break_inline,
...@@ -2415,11 +2431,11 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner...@@ -2415,11 +2431,11 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2415 },2431 },
24162432
2417 // ZIR instructions that are always `void`.2433 // ZIR instructions that are always `void`.
2418 .breakpoint,
2419 .fence,
2420 .dbg_stmt,2434 .dbg_stmt,
2421 .dbg_var_ptr,2435 .dbg_var_ptr,
2422 .dbg_var_val,2436 .dbg_var_val,
2437 .dbg_block_begin,
2438 .dbg_block_end,
2423 .ensure_result_used,2439 .ensure_result_used,
2424 .ensure_result_non_error,2440 .ensure_result_non_error,
2425 .@"export",2441 .@"export",
...@@ -2436,9 +2452,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner...@@ -2436,9 +2452,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2436 .validate_struct_init_comptime,2452 .validate_struct_init_comptime,
2437 .validate_array_init,2453 .validate_array_init,
2438 .validate_array_init_comptime,2454 .validate_array_init_comptime,
2439 .set_align_stack,
2440 .set_cold,2455 .set_cold,
2441 .set_float_mode,
2442 .set_runtime_safety,2456 .set_runtime_safety,
2443 .closure_capture,2457 .closure_capture,
2444 .memcpy,2458 .memcpy,
...@@ -6310,9 +6324,9 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -6310,9 +6324,9 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
6310 }6324 }
63116325
6312 const rl: ResultLoc = if (nodeMayNeedMemoryLocation(tree, operand_node, true)) .{6326 const rl: ResultLoc = if (nodeMayNeedMemoryLocation(tree, operand_node, true)) .{
6313 .ptr = try gz.addNodeExtended(.ret_ptr, node),6327 .ptr = try gz.addNode(.ret_ptr, node),
6314 } else .{6328 } else .{
6315 .ty = try gz.addNodeExtended(.ret_type, node),6329 .ty = try gz.addNode(.ret_type, node),
6316 };6330 };
6317 const prev_anon_name_strategy = gz.anon_name_strategy;6331 const prev_anon_name_strategy = gz.anon_name_strategy;
6318 gz.anon_name_strategy = .func;6332 gz.anon_name_strategy = .func;
...@@ -7183,7 +7197,26 @@ fn builtinCall(...@@ -7183,7 +7197,26 @@ fn builtinCall(
7183 },7197 },
7184 .fence => {7198 .fence => {
7185 const order = try expr(gz, scope, .{ .coerced_ty = .atomic_order_type }, params[0]);7199 const order = try expr(gz, scope, .{ .coerced_ty = .atomic_order_type }, params[0]);
7186 const result = try gz.addUnNode(.fence, order, node);7200 const result = try gz.addExtendedPayload(.fence, Zir.Inst.UnNode{
7201 .node = gz.nodeIndexToRelative(node),
7202 .operand = order,
7203 });
7204 return rvalue(gz, rl, result, node);
7205 },
7206 .set_float_mode => {
7207 const order = try expr(gz, scope, .{ .coerced_ty = .float_mode_type }, params[0]);
7208 const result = try gz.addExtendedPayload(.set_float_mode, Zir.Inst.UnNode{
7209 .node = gz.nodeIndexToRelative(node),
7210 .operand = order,
7211 });
7212 return rvalue(gz, rl, result, node);
7213 },
7214 .set_align_stack => {
7215 const order = try expr(gz, scope, align_rl, params[0]);
7216 const result = try gz.addExtendedPayload(.set_align_stack, Zir.Inst.UnNode{
7217 .node = gz.nodeIndexToRelative(node),
7218 .operand = order,
7219 });
7187 return rvalue(gz, rl, result, node);7220 return rvalue(gz, rl, result, node);
7188 },7221 },
71897222
...@@ -7198,14 +7231,13 @@ fn builtinCall(...@@ -7198,14 +7231,13 @@ fn builtinCall(
7198 return rvalue(gz, rl, result, node);7231 return rvalue(gz, rl, result, node);
7199 },7232 },
72007233
7201 .breakpoint => return simpleNoOpVoid(gz, rl, node, .breakpoint),
7202
7203 // zig fmt: off7234 // zig fmt: off
7204 .This => return rvalue(gz, rl, try gz.addNodeExtended(.this, node), node),7235 .This => return rvalue(gz, rl, try gz.addNodeExtended(.this, node), node),
7205 .return_address => return rvalue(gz, rl, try gz.addNodeExtended(.ret_addr, node), node),7236 .return_address => return rvalue(gz, rl, try gz.addNodeExtended(.ret_addr, node), node),
7206 .error_return_trace => return rvalue(gz, rl, try gz.addNodeExtended(.error_return_trace, node), node),7237 .error_return_trace => return rvalue(gz, rl, try gz.addNodeExtended(.error_return_trace, node), node),
7207 .frame => return rvalue(gz, rl, try gz.addNodeExtended(.frame, node), node),7238 .frame => return rvalue(gz, rl, try gz.addNodeExtended(.frame, node), node),
7208 .frame_address => return rvalue(gz, rl, try gz.addNodeExtended(.frame_address, node), node),7239 .frame_address => return rvalue(gz, rl, try gz.addNodeExtended(.frame_address, node), node),
7240 .breakpoint => return rvalue(gz, rl, try gz.addNodeExtended(.breakpoint, node), node),
72097241
7210 .type_info => return simpleUnOpType(gz, scope, rl, node, params[0], .type_info),7242 .type_info => return simpleUnOpType(gz, scope, rl, node, params[0], .type_info),
7211 .size_of => return simpleUnOpType(gz, scope, rl, node, params[0], .size_of),7243 .size_of => return simpleUnOpType(gz, scope, rl, node, params[0], .size_of),
...@@ -7222,9 +7254,7 @@ fn builtinCall(...@@ -7222,9 +7254,7 @@ fn builtinCall(
7222 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),7254 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
7223 .error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),7255 .error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),
7224 .panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .panic),7256 .panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .panic),
7225 .set_align_stack => return simpleUnOp(gz, scope, rl, node, align_rl, params[0], .set_align_stack),
7226 .set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),7257 .set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),
7227 .set_float_mode => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .float_mode_type }, params[0], .set_float_mode),
7228 .set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),7258 .set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),
7229 .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),7259 .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),
7230 .sin => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sin),7260 .sin => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sin),
...@@ -7252,7 +7282,6 @@ fn builtinCall(...@@ -7252,7 +7282,6 @@ fn builtinCall(
7252 .int_to_enum => return typeCast(gz, scope, rl, node, params[0], params[1], .int_to_enum),7282 .int_to_enum => return typeCast(gz, scope, rl, node, params[0], params[1], .int_to_enum),
7253 .float_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .float_cast),7283 .float_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .float_cast),
7254 .int_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .int_cast),7284 .int_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .int_cast),
7255 .err_set_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .err_set_cast),
7256 .ptr_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .ptr_cast),7285 .ptr_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .ptr_cast),
7257 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),7286 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
7258 // zig fmt: on7287 // zig fmt: on
...@@ -7266,6 +7295,14 @@ fn builtinCall(...@@ -7266,6 +7295,14 @@ fn builtinCall(
7266 });7295 });
7267 return rvalue(gz, rl, result, node);7296 return rvalue(gz, rl, result, node);
7268 },7297 },
7298 .err_set_cast => {
7299 const result = try gz.addExtendedPayload(.err_set_cast, Zir.Inst.BinNode{
7300 .lhs = try typeExpr(gz, scope, params[0]),
7301 .rhs = try expr(gz, scope, .none, params[1]),
7302 .node = gz.nodeIndexToRelative(node),
7303 });
7304 return rvalue(gz, rl, result, node);
7305 },
72697306
7270 // zig fmt: off7307 // zig fmt: off
7271 .has_decl => return hasDeclOrField(gz, scope, rl, node, params[0], params[1], .has_decl),7308 .has_decl => return hasDeclOrField(gz, scope, rl, node, params[0], params[1], .has_decl),
...@@ -8940,7 +8977,8 @@ fn rvalue(...@@ -8940,7 +8977,8 @@ fn rvalue(
8940 const result = r: {8977 const result = r: {
8941 if (refToIndex(raw_result)) |result_index| {8978 if (refToIndex(raw_result)) |result_index| {
8942 const zir_tags = gz.astgen.instructions.items(.tag);8979 const zir_tags = gz.astgen.instructions.items(.tag);
8943 if (zir_tags[result_index].isAlwaysVoid()) {8980 const data = gz.astgen.instructions.items(.data)[result_index];
8981 if (zir_tags[result_index].isAlwaysVoid(data)) {
8944 break :r Zir.Inst.Ref.void_value;8982 break :r Zir.Inst.Ref.void_value;
8945 }8983 }
8946 }8984 }
...@@ -9987,10 +10025,18 @@ const GenZir = struct {...@@ -9987,10 +10025,18 @@ const GenZir = struct {
9987 @boolToInt(args.cc != .none),10025 @boolToInt(args.cc != .none),
9988 );10026 );
9989 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.ExtendedFunc{10027 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.ExtendedFunc{
9990 .src_node = gz.nodeIndexToRelative(args.src_node),
9991 .param_block = args.param_block,10028 .param_block = args.param_block,
9992 .ret_body_len = @intCast(u32, ret_ty.len),10029 .ret_body_len = @intCast(u32, ret_ty.len),
9993 .body_len = @intCast(u32, body.len),10030 .body_len = @intCast(u32, body.len),
10031 .bits = .{
10032 .is_var_args = args.is_var_args,
10033 .is_inferred_error = args.is_inferred_error,
10034 .has_lib_name = args.lib_name != 0,
10035 .has_cc = args.cc != .none,
10036 .has_align = args.align_inst != .none,
10037 .is_test = args.is_test,
10038 .is_extern = args.is_extern,
10039 },
9994 });10040 });
9995 if (args.lib_name != 0) {10041 if (args.lib_name != 0) {
9996 astgen.extra.appendAssumeCapacity(args.lib_name);10042 astgen.extra.appendAssumeCapacity(args.lib_name);
...@@ -10014,19 +10060,10 @@ const GenZir = struct {...@@ -10014,19 +10060,10 @@ const GenZir = struct {
10014 astgen.instructions.items(.data)[args.ret_br].@"break".block_inst = new_index;10060 astgen.instructions.items(.data)[args.ret_br].@"break".block_inst = new_index;
10015 }10061 }
10016 astgen.instructions.appendAssumeCapacity(.{10062 astgen.instructions.appendAssumeCapacity(.{
10017 .tag = .extended,10063 .tag = .func_extended,
10018 .data = .{ .extended = .{10064 .data = .{ .pl_node = .{
10019 .opcode = .func,10065 .src_node = gz.nodeIndexToRelative(args.src_node),
10020 .small = @bitCast(u16, Zir.Inst.ExtendedFunc.Small{10066 .payload_index = payload_index,
10021 .is_var_args = args.is_var_args,
10022 .is_inferred_error = args.is_inferred_error,
10023 .has_lib_name = args.lib_name != 0,
10024 .has_cc = args.cc != .none,
10025 .has_align = args.align_inst != .none,
10026 .is_test = args.is_test,
10027 .is_extern = args.is_extern,
10028 }),
10029 .operand = payload_index,
10030 } },10067 } },
10031 });10068 });
10032 gz.instructions.appendAssumeCapacity(new_index);10069 gz.instructions.appendAssumeCapacity(new_index);
...@@ -10893,9 +10930,7 @@ const GenZir = struct {...@@ -10893,9 +10930,7 @@ const GenZir = struct {
10893 fn addDbgBlockBegin(gz: *GenZir) !void {10930 fn addDbgBlockBegin(gz: *GenZir) !void {
10894 if (gz.force_comptime) return;10931 if (gz.force_comptime) return;
1089510932
10896 _ = try gz.add(.{ .tag = .extended, .data = .{10933 _ = try gz.add(.{ .tag = .dbg_block_begin, .data = undefined });
10897 .extended = .{ .opcode = .dbg_block_begin, .small = undefined, .operand = undefined },
10898 } });
10899 }10934 }
1090010935
10901 fn addDbgBlockEnd(gz: *GenZir) !void {10936 fn addDbgBlockEnd(gz: *GenZir) !void {
...@@ -10903,18 +10938,15 @@ const GenZir = struct {...@@ -10903,18 +10938,15 @@ const GenZir = struct {
10903 const gpa = gz.astgen.gpa;10938 const gpa = gz.astgen.gpa;
1090410939
10905 const tags = gz.astgen.instructions.items(.tag);10940 const tags = gz.astgen.instructions.items(.tag);
10906 const data = gz.astgen.instructions.items(.data);
10907 const last_inst = gz.instructions.items[gz.instructions.items.len - 1];10941 const last_inst = gz.instructions.items[gz.instructions.items.len - 1];
10908 // remove dbg_block_begin immediately followed by dbg_block_end10942 // remove dbg_block_begin immediately followed by dbg_block_end
10909 if (tags[last_inst] == .extended and data[last_inst].extended.opcode == .dbg_block_begin) {10943 if (tags[last_inst] == .dbg_block_begin) {
10910 _ = gz.instructions.pop();10944 _ = gz.instructions.pop();
10911 return;10945 return;
10912 }10946 }
1091310947
10914 const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);10948 const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
10915 try gz.astgen.instructions.append(gpa, .{ .tag = .extended, .data = .{10949 try gz.astgen.instructions.append(gpa, .{ .tag = .dbg_block_end, .data = undefined });
10916 .extended = .{ .opcode = .dbg_block_end, .small = undefined, .operand = undefined },
10917 } });
10918 try gz.instructions.insert(gpa, gz.instructions.items.len - 1, new_index);10950 try gz.instructions.insert(gpa, gz.instructions.items.len - 1, new_index);
10919 }10951 }
1092010952
src/Module.zig+4-4
...@@ -1532,10 +1532,10 @@ pub const Fn = struct {...@@ -1532,10 +1532,10 @@ pub const Fn = struct {
1532 switch (zir_tags[func.zir_body_inst]) {1532 switch (zir_tags[func.zir_body_inst]) {
1533 .func => return false,1533 .func => return false,
1534 .func_inferred => return true,1534 .func_inferred => return true,
1535 .extended => {1535 .func_extended => {
1536 const extended = zir.instructions.items(.data)[func.zir_body_inst].extended;1536 const inst_data = zir.instructions.items(.data)[func.zir_body_inst].pl_node;
1537 const small = @bitCast(Zir.Inst.ExtendedFunc.Small, extended.small);1537 const extra = zir.extraData(Zir.Inst.ExtendedFunc, inst_data.payload_index);
1538 return small.is_inferred_error;1538 return extra.data.bits.is_inferred_error;
1539 },1539 },
1540 else => unreachable,1540 else => unreachable,
1541 }1541 }
src/Sema.zig+89-91
...@@ -745,6 +745,7 @@ fn analyzeBodyInner(...@@ -745,6 +745,7 @@ fn analyzeBodyInner(
745 .field_call_bind => try sema.zirFieldCallBind(block, inst),745 .field_call_bind => try sema.zirFieldCallBind(block, inst),
746 .func => try sema.zirFunc(block, inst, false),746 .func => try sema.zirFunc(block, inst, false),
747 .func_inferred => try sema.zirFunc(block, inst, true),747 .func_inferred => try sema.zirFunc(block, inst, true),
748 .func_extended => try sema.zirFuncExtended(block, inst),
748 .import => try sema.zirImport(block, inst),749 .import => try sema.zirImport(block, inst),
749 .indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst),750 .indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst),
750 .int => try sema.zirInt(block, inst),751 .int => try sema.zirInt(block, inst),
...@@ -819,7 +820,6 @@ fn analyzeBodyInner(...@@ -819,7 +820,6 @@ fn analyzeBodyInner(
819 .int_to_ptr => try sema.zirIntToPtr(block, inst),820 .int_to_ptr => try sema.zirIntToPtr(block, inst),
820 .float_cast => try sema.zirFloatCast(block, inst),821 .float_cast => try sema.zirFloatCast(block, inst),
821 .int_cast => try sema.zirIntCast(block, inst),822 .int_cast => try sema.zirIntCast(block, inst),
822 .err_set_cast => try sema.zirErrSetCast(block, inst),
823 .ptr_cast => try sema.zirPtrCast(block, inst),823 .ptr_cast => try sema.zirPtrCast(block, inst),
824 .truncate => try sema.zirTruncate(block, inst),824 .truncate => try sema.zirTruncate(block, inst),
825 .align_cast => try sema.zirAlignCast(block, inst),825 .align_cast => try sema.zirAlignCast(block, inst),
...@@ -842,8 +842,7 @@ fn analyzeBodyInner(...@@ -842,8 +842,7 @@ fn analyzeBodyInner(
842 .field_parent_ptr => try sema.zirFieldParentPtr(block, inst),842 .field_parent_ptr => try sema.zirFieldParentPtr(block, inst),
843 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),843 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),
844 .@"resume" => try sema.zirResume(block, inst),844 .@"resume" => try sema.zirResume(block, inst),
845 .@"await" => try sema.zirAwait(block, inst, false),845 .@"await" => try sema.zirAwait(block, inst),
846 .await_nosuspend => try sema.zirAwait(block, inst, true),
847 .array_base_ptr => try sema.zirArrayBasePtr(block, inst),846 .array_base_ptr => try sema.zirArrayBasePtr(block, inst),
848 .field_base_ptr => try sema.zirFieldBasePtr(block, inst),847 .field_base_ptr => try sema.zirFieldBasePtr(block, inst),
849848
...@@ -894,6 +893,9 @@ fn analyzeBodyInner(...@@ -894,6 +893,9 @@ fn analyzeBodyInner(
894 .shl_exact => try sema.zirShl(block, inst, .shl_exact),893 .shl_exact => try sema.zirShl(block, inst, .shl_exact),
895 .shl_sat => try sema.zirShl(block, inst, .shl_sat),894 .shl_sat => try sema.zirShl(block, inst, .shl_sat),
896895
896 .ret_ptr => try sema.zirRetPtr(block, inst),
897 .ret_type => try sema.zirRetType(block, inst),
898
897 // Instructions that we know to *always* be noreturn based solely on their tag.899 // Instructions that we know to *always* be noreturn based solely on their tag.
898 // These functions match the return type of analyzeBody so that we can900 // These functions match the return type of analyzeBody so that we can
899 // tail call them here.901 // tail call them here.
...@@ -910,14 +912,11 @@ fn analyzeBodyInner(...@@ -910,14 +912,11 @@ fn analyzeBodyInner(
910 const extended = datas[inst].extended;912 const extended = datas[inst].extended;
911 break :ext switch (extended.opcode) {913 break :ext switch (extended.opcode) {
912 // zig fmt: off914 // zig fmt: off
913 .func => try sema.zirFuncExtended( block, extended, inst),
914 .variable => try sema.zirVarExtended( block, extended),915 .variable => try sema.zirVarExtended( block, extended),
915 .struct_decl => try sema.zirStructDecl( block, extended, inst),916 .struct_decl => try sema.zirStructDecl( block, extended, inst),
916 .enum_decl => try sema.zirEnumDecl( block, extended),917 .enum_decl => try sema.zirEnumDecl( block, extended),
917 .union_decl => try sema.zirUnionDecl( block, extended, inst),918 .union_decl => try sema.zirUnionDecl( block, extended, inst),
918 .opaque_decl => try sema.zirOpaqueDecl( block, extended),919 .opaque_decl => try sema.zirOpaqueDecl( block, extended),
919 .ret_ptr => try sema.zirRetPtr( block, extended),
920 .ret_type => try sema.zirRetType( block, extended),
921 .this => try sema.zirThis( block, extended),920 .this => try sema.zirThis( block, extended),
922 .ret_addr => try sema.zirRetAddr( block, extended),921 .ret_addr => try sema.zirRetAddr( block, extended),
923 .builtin_src => try sema.zirBuiltinSrc( block, extended),922 .builtin_src => try sema.zirBuiltinSrc( block, extended),
...@@ -940,16 +939,28 @@ fn analyzeBodyInner(...@@ -940,16 +939,28 @@ fn analyzeBodyInner(
940 .wasm_memory_grow => try sema.zirWasmMemoryGrow( block, extended),939 .wasm_memory_grow => try sema.zirWasmMemoryGrow( block, extended),
941 .prefetch => try sema.zirPrefetch( block, extended),940 .prefetch => try sema.zirPrefetch( block, extended),
942 .field_call_bind_named => try sema.zirFieldCallBindNamed(block, extended),941 .field_call_bind_named => try sema.zirFieldCallBindNamed(block, extended),
942 .err_set_cast => try sema.zirErrSetCast( block, extended),
943 .await_nosuspend => try sema.zirAwaitNosuspend( block, extended),
943 // zig fmt: on944 // zig fmt: on
944 .dbg_block_begin => {945 .fence => {
945 dbg_block_begins += 1;946 try sema.zirFence(block, extended);
946 try sema.zirDbgBlockBegin(block);947 i += 1;
948 continue;
949 },
950 .set_float_mode => {
951 try sema.zirSetFloatMode(block, extended);
947 i += 1;952 i += 1;
948 continue;953 continue;
949 },954 },
950 .dbg_block_end => {955 .set_align_stack => {
951 dbg_block_begins -= 1;956 try sema.zirSetAlignStack(block, extended);
952 try sema.zirDbgBlockEnd(block);957 i += 1;
958 continue;
959 },
960 .breakpoint => {
961 if (!block.is_comptime) {
962 _ = try block.addNoOp(.breakpoint);
963 }
953 i += 1;964 i += 1;
954 continue;965 continue;
955 },966 },
...@@ -961,18 +972,6 @@ fn analyzeBodyInner(...@@ -961,18 +972,6 @@ fn analyzeBodyInner(
961 // continue the loop.972 // continue the loop.
962 // We also know that they cannot be referenced later, so we avoid973 // We also know that they cannot be referenced later, so we avoid
963 // putting them into the map.974 // putting them into the map.
964 .breakpoint => {
965 if (!block.is_comptime) {
966 _ = try block.addNoOp(.breakpoint);
967 }
968 i += 1;
969 continue;
970 },
971 .fence => {
972 try sema.zirFence(block, inst);
973 i += 1;
974 continue;
975 },
976 .dbg_stmt => {975 .dbg_stmt => {
977 try sema.zirDbgStmt(block, inst);976 try sema.zirDbgStmt(block, inst);
978 i += 1;977 i += 1;
...@@ -988,6 +987,18 @@ fn analyzeBodyInner(...@@ -988,6 +987,18 @@ fn analyzeBodyInner(
988 i += 1;987 i += 1;
989 continue;988 continue;
990 },989 },
990 .dbg_block_begin => {
991 dbg_block_begins += 1;
992 try sema.zirDbgBlockBegin(block);
993 i += 1;
994 continue;
995 },
996 .dbg_block_end => {
997 dbg_block_begins -= 1;
998 try sema.zirDbgBlockEnd(block);
999 i += 1;
1000 continue;
1001 },
991 .ensure_err_payload_void => {1002 .ensure_err_payload_void => {
992 try sema.zirEnsureErrPayloadVoid(block, inst);1003 try sema.zirEnsureErrPayloadVoid(block, inst);
993 i += 1;1004 i += 1;
...@@ -1078,21 +1089,11 @@ fn analyzeBodyInner(...@@ -1078,21 +1089,11 @@ fn analyzeBodyInner(
1078 i += 1;1089 i += 1;
1079 continue;1090 continue;
1080 },1091 },
1081 .set_align_stack => {
1082 try sema.zirSetAlignStack(block, inst);
1083 i += 1;
1084 continue;
1085 },
1086 .set_cold => {1092 .set_cold => {
1087 try sema.zirSetCold(block, inst);1093 try sema.zirSetCold(block, inst);
1088 i += 1;1094 i += 1;
1089 continue;1095 continue;
1090 },1096 },
1091 .set_float_mode => {
1092 try sema.zirSetFloatMode(block, inst);
1093 i += 1;
1094 continue;
1095 },
1096 .set_runtime_safety => {1097 .set_runtime_safety => {
1097 try sema.zirSetRuntimeSafety(block, inst);1098 try sema.zirSetRuntimeSafety(block, inst);
1098 i += 1;1099 i += 1;
...@@ -2452,15 +2453,12 @@ fn zirErrorSetDecl(...@@ -2452,15 +2453,12 @@ fn zirErrorSetDecl(
2452 return sema.analyzeDeclVal(block, src, new_decl_index);2453 return sema.analyzeDeclVal(block, src, new_decl_index);
2453}2454}
24542455
2455fn zirRetPtr(2456fn zirRetPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2456 sema: *Sema,
2457 block: *Block,
2458 extended: Zir.Inst.Extended.InstData,
2459) CompileError!Air.Inst.Ref {
2460 const tracy = trace(@src());2457 const tracy = trace(@src());
2461 defer tracy.end();2458 defer tracy.end();
24622459
2463 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };2460 const inst_data = sema.code.instructions.items(.data)[inst].node;
2461 const src: LazySrcLoc = .{ .node_offset = inst_data };
2464 try sema.requireFunctionBlock(block, src);2462 try sema.requireFunctionBlock(block, src);
24652463
2466 if (block.is_comptime) {2464 if (block.is_comptime) {
...@@ -2493,15 +2491,12 @@ fn zirRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins...@@ -2493,15 +2491,12 @@ fn zirRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
2493 return sema.analyzeRef(block, inst_data.src(), operand);2491 return sema.analyzeRef(block, inst_data.src(), operand);
2494}2492}
24952493
2496fn zirRetType(2494fn zirRetType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2497 sema: *Sema,
2498 block: *Block,
2499 extended: Zir.Inst.Extended.InstData,
2500) CompileError!Air.Inst.Ref {
2501 const tracy = trace(@src());2495 const tracy = trace(@src());
2502 defer tracy.end();2496 defer tracy.end();
25032497
2504 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };2498 const inst_data = sema.code.instructions.items(.data)[inst].node;
2499 const src: LazySrcLoc = .{ .node_offset = inst_data };
2505 try sema.requireFunctionBlock(block, src);2500 try sema.requireFunctionBlock(block, src);
2506 return sema.addType(sema.fn_ret_ty);2501 return sema.addType(sema.fn_ret_ty);
2507}2502}
...@@ -3746,9 +3741,7 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v...@@ -3746,9 +3741,7 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v
3746 sema.fn_ret_ty.zigTypeTag() == .ErrorUnion)3741 sema.fn_ret_ty.zigTypeTag() == .ErrorUnion)
3747 {3742 {
3748 if (Zir.refToIndex(extra.lhs)) |ptr_index| {3743 if (Zir.refToIndex(extra.lhs)) |ptr_index| {
3749 if (zir_tags[ptr_index] == .extended and3744 if (zir_tags[ptr_index] == .ret_ptr) {
3750 zir_datas[ptr_index].extended.opcode == .ret_ptr)
3751 {
3752 try sema.addToInferredErrorSet(operand);3745 try sema.addToInferredErrorSet(operand);
3753 }3746 }
3754 }3747 }
...@@ -4392,11 +4385,11 @@ pub fn analyzeExport(...@@ -4392,11 +4385,11 @@ pub fn analyzeExport(
4392 errdefer de_gop.value_ptr.* = gpa.shrink(de_gop.value_ptr.*, de_gop.value_ptr.len - 1);4385 errdefer de_gop.value_ptr.* = gpa.shrink(de_gop.value_ptr.*, de_gop.value_ptr.len - 1);
4393}4386}
43944387
4395fn zirSetAlignStack(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {4388fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
4396 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4389 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
4397 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };4390 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
4398 const src: LazySrcLoc = inst_data.src();4391 const src: LazySrcLoc = .{ .node_offset = extra.node };
4399 const alignment = try sema.resolveAlign(block, operand_src, inst_data.operand);4392 const alignment = try sema.resolveAlign(block, operand_src, extra.operand);
4400 if (alignment > 256) {4393 if (alignment > 256) {
4401 return sema.fail(block, src, "attempt to @setAlignStack({d}); maximum is 256", .{4394 return sema.fail(block, src, "attempt to @setAlignStack({d}); maximum is 256", .{
4402 alignment,4395 alignment,
...@@ -4433,10 +4426,10 @@ fn zirSetCold(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi...@@ -4433,10 +4426,10 @@ fn zirSetCold(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
4433 func.is_cold = is_cold;4426 func.is_cold = is_cold;
4434}4427}
44354428
4436fn zirSetFloatMode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {4429fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
4437 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4430 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
4438 const src: LazySrcLoc = inst_data.src();4431 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
4439 const float_mode = try sema.resolveBuiltinEnum(block, src, inst_data.operand, "FloatMode");4432 const float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode");
4440 switch (float_mode) {4433 switch (float_mode) {
4441 .Strict => return,4434 .Strict => return,
4442 .Optimized => {4435 .Optimized => {
...@@ -4451,12 +4444,12 @@ fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile...@@ -4451,12 +4444,12 @@ fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile
4451 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand);4444 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand);
4452}4445}
44534446
4454fn zirFence(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {4447fn zirFence(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
4455 if (block.is_comptime) return;4448 if (block.is_comptime) return;
44564449
4457 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4450 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
4458 const order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };4451 const order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
4459 const order = try sema.resolveAtomicOrder(block, order_src, inst_data.operand);4452 const order = try sema.resolveAtomicOrder(block, order_src, extra.operand);
44604453
4461 if (@enumToInt(order) < @enumToInt(std.builtin.AtomicOrder.Acquire)) {4454 if (@enumToInt(order) < @enumToInt(std.builtin.AtomicOrder.Acquire)) {
4462 return sema.fail(block, order_src, "atomic ordering must be Acquire or stricter", .{});4455 return sema.fail(block, order_src, "atomic ordering must be Acquire or stricter", .{});
...@@ -12337,14 +12330,15 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi...@@ -12337,14 +12330,15 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi
12337}12330}
1233812331
12339fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {12332fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
12340 const tracy = trace(@src());
12341 defer tracy.end();
12342
12343 const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable";12333 const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable";
12344 const src = inst_data.src();12334 const src = inst_data.src();
12335
12336 if (block.is_comptime or inst_data.force_comptime) {
12337 return sema.fail(block, src, "reached unreachable code", .{});
12338 }
12345 try sema.requireRuntimeBlock(block, src);12339 try sema.requireRuntimeBlock(block, src);
12346 // TODO Add compile error for @optimizeFor occurring too late in a scope.12340 // TODO Add compile error for @optimizeFor occurring too late in a scope.
12347 try block.addUnreachable(src, inst_data.safety);12341 try block.addUnreachable(src, true);
12348 return always_noreturn;12342 return always_noreturn;
12349}12343}
1235012344
...@@ -14143,12 +14137,11 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -14143,12 +14137,11 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
14143 return block.addBitCast(type_res, operand_coerced);14137 return block.addBitCast(type_res, operand_coerced);
14144}14138}
1414514139
14146fn zirErrSetCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {14140fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
14147 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;14141 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
14148 const src = inst_data.src();14142 const src: LazySrcLoc = .{ .node_offset = extra.node };
14149 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };14143 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
14150 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };14144 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
14151 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
14152 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);14145 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
14153 const operand = sema.resolveInst(extra.rhs);14146 const operand = sema.resolveInst(extra.rhs);
14154 const operand_ty = sema.typeOf(operand);14147 const operand_ty = sema.typeOf(operand);
...@@ -16006,15 +15999,24 @@ fn zirAwait(...@@ -16006,15 +15999,24 @@ fn zirAwait(
16006 sema: *Sema,15999 sema: *Sema,
16007 block: *Block,16000 block: *Block,
16008 inst: Zir.Inst.Index,16001 inst: Zir.Inst.Index,
16009 is_nosuspend: bool,
16010) CompileError!Air.Inst.Ref {16002) CompileError!Air.Inst.Ref {
16011 const inst_data = sema.code.instructions.items(.data)[inst].un_node;16003 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
16012 const src = inst_data.src();16004 const src = inst_data.src();
1601316005
16014 _ = is_nosuspend;
16015 return sema.fail(block, src, "TODO: Sema.zirAwait", .{});16006 return sema.fail(block, src, "TODO: Sema.zirAwait", .{});
16016}16007}
1601716008
16009fn zirAwaitNosuspend(
16010 sema: *Sema,
16011 block: *Block,
16012 extended: Zir.Inst.Extended.InstData,
16013) CompileError!Air.Inst.Ref {
16014 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
16015 const src: LazySrcLoc = .{ .node_offset = extra.node };
16016
16017 return sema.fail(block, src, "TODO: Sema.zirAwaitNosuspend", .{});
16018}
16019
16018fn zirVarExtended(16020fn zirVarExtended(
16019 sema: *Sema,16021 sema: *Sema,
16020 block: *Block,16022 block: *Block,
...@@ -16097,37 +16099,33 @@ fn zirVarExtended(...@@ -16097,37 +16099,33 @@ fn zirVarExtended(
16097 return result;16099 return result;
16098}16100}
1609916101
16100fn zirFuncExtended(16102fn zirFuncExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16101 sema: *Sema,
16102 block: *Block,
16103 extended: Zir.Inst.Extended.InstData,
16104 inst: Zir.Inst.Index,
16105) CompileError!Air.Inst.Ref {
16106 const tracy = trace(@src());16103 const tracy = trace(@src());
16107 defer tracy.end();16104 defer tracy.end();
1610816105
16109 const extra = sema.code.extraData(Zir.Inst.ExtendedFunc, extended.operand);16106 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
16110 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };16107 const src = inst_data.src();
16111 const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = extra.data.src_node };16108 const extra = sema.code.extraData(Zir.Inst.ExtendedFunc, inst_data.payload_index);
16109
16110 const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = inst_data.src_node };
16112 const align_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at align16111 const align_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at align
16113 const small = @bitCast(Zir.Inst.ExtendedFunc.Small, extended.small);
1611416112
16115 var extra_index: usize = extra.end;16113 var extra_index: usize = extra.end;
1611616114
16117 const lib_name: ?[]const u8 = if (small.has_lib_name) blk: {16115 const lib_name: ?[]const u8 = if (extra.data.bits.has_lib_name) blk: {
16118 const lib_name = sema.code.nullTerminatedString(sema.code.extra[extra_index]);16116 const lib_name = sema.code.nullTerminatedString(sema.code.extra[extra_index]);
16119 extra_index += 1;16117 extra_index += 1;
16120 break :blk lib_name;16118 break :blk lib_name;
16121 } else null;16119 } else null;
1612216120
16123 const cc: std.builtin.CallingConvention = if (small.has_cc) blk: {16121 const cc: std.builtin.CallingConvention = if (extra.data.bits.has_cc) blk: {
16124 const cc_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);16122 const cc_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
16125 extra_index += 1;16123 extra_index += 1;
16126 const cc_tv = try sema.resolveInstConst(block, cc_src, cc_ref);16124 const cc_tv = try sema.resolveInstConst(block, cc_src, cc_ref);
16127 break :blk cc_tv.val.toEnum(std.builtin.CallingConvention);16125 break :blk cc_tv.val.toEnum(std.builtin.CallingConvention);
16128 } else .Unspecified;16126 } else .Unspecified;
1612916127
16130 const align_val: Value = if (small.has_align) blk: {16128 const align_val: Value = if (extra.data.bits.has_align) blk: {
16131 const align_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);16129 const align_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
16132 extra_index += 1;16130 extra_index += 1;
16133 const align_tv = try sema.resolveInstConst(block, align_src, align_ref);16131 const align_tv = try sema.resolveInstConst(block, align_src, align_ref);
...@@ -16144,13 +16142,13 @@ fn zirFuncExtended(...@@ -16144,13 +16142,13 @@ fn zirFuncExtended(
16144 src_locs = sema.code.extraData(Zir.Inst.Func.SrcLocs, extra_index).data;16142 src_locs = sema.code.extraData(Zir.Inst.Func.SrcLocs, extra_index).data;
16145 }16143 }
1614616144
16147 const is_var_args = small.is_var_args;16145 const is_var_args = extra.data.bits.is_var_args;
16148 const is_inferred_error = small.is_inferred_error;16146 const is_inferred_error = extra.data.bits.is_inferred_error;
16149 const is_extern = small.is_extern;16147 const is_extern = extra.data.bits.is_extern;
1615016148
16151 return sema.funcCommon(16149 return sema.funcCommon(
16152 block,16150 block,
16153 extra.data.src_node,16151 inst_data.src_node,
16154 inst,16152 inst,
16155 ret_ty_body,16153 ret_ty_body,
16156 cc,16154 cc,
src/Zir.zig+75-79
...@@ -73,6 +73,7 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) struct { data: T, en...@@ -73,6 +73,7 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) struct { data: T, en
73 i32 => @bitCast(i32, code.extra[i]),73 i32 => @bitCast(i32, code.extra[i]),
74 Inst.Call.Flags => @bitCast(Inst.Call.Flags, code.extra[i]),74 Inst.Call.Flags => @bitCast(Inst.Call.Flags, code.extra[i]),
75 Inst.SwitchBlock.Bits => @bitCast(Inst.SwitchBlock.Bits, code.extra[i]),75 Inst.SwitchBlock.Bits => @bitCast(Inst.SwitchBlock.Bits, code.extra[i]),
76 Inst.ExtendedFunc.Bits => @bitCast(Inst.ExtendedFunc.Bits, code.extra[i]),
76 else => @compileError("bad field type"),77 else => @compileError("bad field type"),
77 };78 };
78 i += 1;79 i += 1;
...@@ -278,8 +279,6 @@ pub const Inst = struct {...@@ -278,8 +279,6 @@ pub const Inst = struct {
278 /// break instruction in a block, and the target block is the parent.279 /// break instruction in a block, and the target block is the parent.
279 /// Uses the `break` union field.280 /// Uses the `break` union field.
280 break_inline,281 break_inline,
281 /// Uses the `node` union field.
282 breakpoint,
283 /// Function call.282 /// Function call.
284 /// Uses `pl_node`. AST node is the function call. Payload is `Call`.283 /// Uses `pl_node`. AST node is the function call. Payload is `Call`.
285 call,284 call,
...@@ -331,6 +330,10 @@ pub const Inst = struct {...@@ -331,6 +330,10 @@ pub const Inst = struct {
331 /// Same as `dbg_var_ptr` but the local is always a const and the operand330 /// Same as `dbg_var_ptr` but the local is always a const and the operand
332 /// is the local's value.331 /// is the local's value.
333 dbg_var_val,332 dbg_var_val,
333 /// Marks the beginning of a semantic scope for debug info variables.
334 dbg_block_begin,
335 /// Marks the end of a semantic scope for debug info variables.
336 dbg_block_end,
334 /// Uses a name to identify a Decl and takes a pointer to it.337 /// Uses a name to identify a Decl and takes a pointer to it.
335 /// Uses the `str_tok` union field.338 /// Uses the `str_tok` union field.
336 decl_ref,339 decl_ref,
...@@ -413,6 +416,10 @@ pub const Inst = struct {...@@ -413,6 +416,10 @@ pub const Inst = struct {
413 func,416 func,
414 /// Same as `func` but has an inferred error set.417 /// Same as `func` but has an inferred error set.
415 func_inferred,418 func_inferred,
419 /// Represents a function declaration or function prototype, depending on
420 /// whether body_len is 0.
421 /// Uses the `pl_node` union field. `payload_index` points to a `ExtendedFunc`.
422 func_extended,
416 /// Implements the `@import` builtin.423 /// Implements the `@import` builtin.
417 /// Uses the `str_tok` field.424 /// Uses the `str_tok` field.
418 import,425 import,
...@@ -499,6 +506,12 @@ pub const Inst = struct {...@@ -499,6 +506,12 @@ pub const Inst = struct {
499 /// this instruction; a following 'ret' instruction will do the diversion.506 /// this instruction; a following 'ret' instruction will do the diversion.
500 /// Uses the `str_tok` union field.507 /// Uses the `str_tok` union field.
501 ret_err_value_code,508 ret_err_value_code,
509 /// Obtains a pointer to the return value.
510 /// Uses the `node` union field.
511 ret_ptr,
512 /// Obtains the return type of the in-scope function.
513 /// Uses the `node` union field.
514 ret_type,
502 /// Create a pointer type that does not have a sentinel, alignment, address space, or bit range specified.515 /// Create a pointer type that does not have a sentinel, alignment, address space, or bit range specified.
503 /// Uses the `ptr_type_simple` union field.516 /// Uses the `ptr_type_simple` union field.
504 ptr_type_simple,517 ptr_type_simple,
...@@ -744,8 +757,6 @@ pub const Inst = struct {...@@ -744,8 +757,6 @@ pub const Inst = struct {
744 size_of,757 size_of,
745 /// Implements the `@bitSizeOf` builtin. Uses `un_node`.758 /// Implements the `@bitSizeOf` builtin. Uses `un_node`.
746 bit_size_of,759 bit_size_of,
747 /// Implements the `@fence` builtin. Uses `un_node`.
748 fence,
749760
750 /// Implement builtin `@ptrToInt`. Uses `un_node`.761 /// Implement builtin `@ptrToInt`. Uses `un_node`.
751 /// Convert a pointer to a `usize` integer.762 /// Convert a pointer to a `usize` integer.
...@@ -774,12 +785,8 @@ pub const Inst = struct {...@@ -774,12 +785,8 @@ pub const Inst = struct {
774 error_name,785 error_name,
775 /// Implement builtin `@panic`. Uses `un_node`.786 /// Implement builtin `@panic`. Uses `un_node`.
776 panic,787 panic,
777 /// Implement builtin `@setAlignStack`. Uses `un_node`.
778 set_align_stack,
779 /// Implement builtin `@setCold`. Uses `un_node`.788 /// Implement builtin `@setCold`. Uses `un_node`.
780 set_cold,789 set_cold,
781 /// Implement builtin `@setFloatMode`. Uses `un_node`.
782 set_float_mode,
783 /// Implement builtin `@setRuntimeSafety`. Uses `un_node`.790 /// Implement builtin `@setRuntimeSafety`. Uses `un_node`.
784 set_runtime_safety,791 set_runtime_safety,
785 /// Implement builtin `@sqrt`. Uses `un_node`.792 /// Implement builtin `@sqrt`. Uses `un_node`.
...@@ -843,9 +850,6 @@ pub const Inst = struct {...@@ -843,9 +850,6 @@ pub const Inst = struct {
843 /// Convert an integer value to another integer type, asserting that the destination type850 /// Convert an integer value to another integer type, asserting that the destination type
844 /// can hold the same mathematical value.851 /// can hold the same mathematical value.
845 int_cast,852 int_cast,
846 /// Implements the `@errSetCast` builtin.
847 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
848 err_set_cast,
849 /// Implements the `@ptrCast` builtin.853 /// Implements the `@ptrCast` builtin.
850 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.854 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
851 ptr_cast,855 ptr_cast,
...@@ -972,7 +976,6 @@ pub const Inst = struct {...@@ -972,7 +976,6 @@ pub const Inst = struct {
972 /// Implements `resume` syntax. Uses `un_node` field.976 /// Implements `resume` syntax. Uses `un_node` field.
973 @"resume",977 @"resume",
974 @"await",978 @"await",
975 await_nosuspend,
976979
977 /// When a type or function refers to a comptime value from an outer980 /// When a type or function refers to a comptime value from an outer
978 /// scope, that forms a closure over comptime value. The outer scope981 /// scope, that forms a closure over comptime value. The outer scope
...@@ -1028,8 +1031,6 @@ pub const Inst = struct {...@@ -1028,8 +1031,6 @@ pub const Inst = struct {
1028 .bool_br_and,1031 .bool_br_and,
1029 .bool_br_or,1032 .bool_br_or,
1030 .bool_not,1033 .bool_not,
1031 .breakpoint,
1032 .fence,
1033 .call,1034 .call,
1034 .cmp_lt,1035 .cmp_lt,
1035 .cmp_lte,1036 .cmp_lte,
...@@ -1044,6 +1045,8 @@ pub const Inst = struct {...@@ -1044,6 +1045,8 @@ pub const Inst = struct {
1044 .dbg_stmt,1045 .dbg_stmt,
1045 .dbg_var_ptr,1046 .dbg_var_ptr,
1046 .dbg_var_val,1047 .dbg_var_val,
1048 .dbg_block_begin,
1049 .dbg_block_end,
1047 .decl_ref,1050 .decl_ref,
1048 .decl_val,1051 .decl_val,
1049 .load,1052 .load,
...@@ -1064,6 +1067,7 @@ pub const Inst = struct {...@@ -1064,6 +1067,7 @@ pub const Inst = struct {
1064 .field_val_named,1067 .field_val_named,
1065 .func,1068 .func,
1066 .func_inferred,1069 .func_inferred,
1070 .func_extended,
1067 .has_decl,1071 .has_decl,
1068 .int,1072 .int,
1069 .int_big,1073 .int_big,
...@@ -1164,9 +1168,7 @@ pub const Inst = struct {...@@ -1164,9 +1168,7 @@ pub const Inst = struct {
1164 .bool_to_int,1168 .bool_to_int,
1165 .embed_file,1169 .embed_file,
1166 .error_name,1170 .error_name,
1167 .set_align_stack,
1168 .set_cold,1171 .set_cold,
1169 .set_float_mode,
1170 .set_runtime_safety,1172 .set_runtime_safety,
1171 .sqrt,1173 .sqrt,
1172 .sin,1174 .sin,
...@@ -1192,7 +1194,6 @@ pub const Inst = struct {...@@ -1192,7 +1194,6 @@ pub const Inst = struct {
1192 .int_to_ptr,1194 .int_to_ptr,
1193 .float_cast,1195 .float_cast,
1194 .int_cast,1196 .int_cast,
1195 .err_set_cast,
1196 .ptr_cast,1197 .ptr_cast,
1197 .truncate,1198 .truncate,
1198 .align_cast,1199 .align_cast,
...@@ -1231,11 +1232,12 @@ pub const Inst = struct {...@@ -1231,11 +1232,12 @@ pub const Inst = struct {
1231 .c_import,1232 .c_import,
1232 .@"resume",1233 .@"resume",
1233 .@"await",1234 .@"await",
1234 .await_nosuspend,
1235 .ret_err_value_code,1235 .ret_err_value_code,
1236 .extended,1236 .extended,
1237 .closure_get,1237 .closure_get,
1238 .closure_capture,1238 .closure_capture,
1239 .ret_ptr,
1240 .ret_type,
1239 => false,1241 => false,
12401242
1241 .@"break",1243 .@"break",
...@@ -1258,13 +1260,13 @@ pub const Inst = struct {...@@ -1258,13 +1260,13 @@ pub const Inst = struct {
1258 /// AstGen uses this to find out if `Ref.void_value` should be used in place1260 /// AstGen uses this to find out if `Ref.void_value` should be used in place
1259 /// of the result of a given instruction. This allows Sema to forego adding1261 /// of the result of a given instruction. This allows Sema to forego adding
1260 /// the instruction to the map after analysis.1262 /// the instruction to the map after analysis.
1261 pub fn isAlwaysVoid(tag: Tag) bool {1263 pub fn isAlwaysVoid(tag: Tag, data: Data) bool {
1262 return switch (tag) {1264 return switch (tag) {
1263 .breakpoint,
1264 .fence,
1265 .dbg_stmt,1265 .dbg_stmt,
1266 .dbg_var_ptr,1266 .dbg_var_ptr,
1267 .dbg_var_val,1267 .dbg_var_val,
1268 .dbg_block_begin,
1269 .dbg_block_end,
1268 .ensure_result_used,1270 .ensure_result_used,
1269 .ensure_result_non_error,1271 .ensure_result_non_error,
1270 .ensure_err_payload_void,1272 .ensure_err_payload_void,
...@@ -1283,9 +1285,7 @@ pub const Inst = struct {...@@ -1283,9 +1285,7 @@ pub const Inst = struct {
1283 .validate_array_init_comptime,1285 .validate_array_init_comptime,
1284 .@"export",1286 .@"export",
1285 .export_value,1287 .export_value,
1286 .set_align_stack,
1287 .set_cold,1288 .set_cold,
1288 .set_float_mode,
1289 .set_runtime_safety,1289 .set_runtime_safety,
1290 .memcpy,1290 .memcpy,
1291 .memset,1291 .memset,
...@@ -1353,6 +1353,7 @@ pub const Inst = struct {...@@ -1353,6 +1353,7 @@ pub const Inst = struct {
1353 .field_val_named,1353 .field_val_named,
1354 .func,1354 .func,
1355 .func_inferred,1355 .func_inferred,
1356 .func_extended,
1356 .has_decl,1357 .has_decl,
1357 .int,1358 .int,
1358 .int_big,1359 .int_big,
...@@ -1464,7 +1465,6 @@ pub const Inst = struct {...@@ -1464,7 +1465,6 @@ pub const Inst = struct {
1464 .int_to_ptr,1465 .int_to_ptr,
1465 .float_cast,1466 .float_cast,
1466 .int_cast,1467 .int_cast,
1467 .err_set_cast,
1468 .ptr_cast,1468 .ptr_cast,
1469 .truncate,1469 .truncate,
1470 .align_cast,1470 .align_cast,
...@@ -1500,9 +1500,7 @@ pub const Inst = struct {...@@ -1500,9 +1500,7 @@ pub const Inst = struct {
1500 .c_import,1500 .c_import,
1501 .@"resume",1501 .@"resume",
1502 .@"await",1502 .@"await",
1503 .await_nosuspend,
1504 .ret_err_value_code,1503 .ret_err_value_code,
1505 .extended,
1506 .closure_get,1504 .closure_get,
1507 .closure_capture,1505 .closure_capture,
1508 .@"break",1506 .@"break",
...@@ -1514,11 +1512,18 @@ pub const Inst = struct {...@@ -1514,11 +1512,18 @@ pub const Inst = struct {
1514 .ret_load,1512 .ret_load,
1515 .ret_tok,1513 .ret_tok,
1516 .ret_err_value,1514 .ret_err_value,
1515 .ret_ptr,
1516 .ret_type,
1517 .@"unreachable",1517 .@"unreachable",
1518 .repeat,1518 .repeat,
1519 .repeat_inline,1519 .repeat_inline,
1520 .panic,1520 .panic,
1521 => false,1521 => false,
1522
1523 .extended => switch (data.extended.opcode) {
1524 .breakpoint, .fence => true,
1525 else => false,
1526 },
1522 };1527 };
1523 }1528 }
15241529
...@@ -1563,7 +1568,6 @@ pub const Inst = struct {...@@ -1563,7 +1568,6 @@ pub const Inst = struct {
1563 .bool_br_or = .bool_br,1568 .bool_br_or = .bool_br,
1564 .@"break" = .@"break",1569 .@"break" = .@"break",
1565 .break_inline = .@"break",1570 .break_inline = .@"break",
1566 .breakpoint = .node,
1567 .call = .pl_node,1571 .call = .pl_node,
1568 .cmp_lt = .pl_node,1572 .cmp_lt = .pl_node,
1569 .cmp_lte = .pl_node,1573 .cmp_lte = .pl_node,
...@@ -1580,6 +1584,8 @@ pub const Inst = struct {...@@ -1580,6 +1584,8 @@ pub const Inst = struct {
1580 .dbg_stmt = .dbg_stmt,1584 .dbg_stmt = .dbg_stmt,
1581 .dbg_var_ptr = .str_op,1585 .dbg_var_ptr = .str_op,
1582 .dbg_var_val = .str_op,1586 .dbg_var_val = .str_op,
1587 .dbg_block_begin = .tok,
1588 .dbg_block_end = .tok,
1583 .decl_ref = .str_tok,1589 .decl_ref = .str_tok,
1584 .decl_val = .str_tok,1590 .decl_val = .str_tok,
1585 .load = .un_node,1591 .load = .un_node,
...@@ -1602,6 +1608,7 @@ pub const Inst = struct {...@@ -1602,6 +1608,7 @@ pub const Inst = struct {
1602 .field_call_bind = .pl_node,1608 .field_call_bind = .pl_node,
1603 .func = .pl_node,1609 .func = .pl_node,
1604 .func_inferred = .pl_node,1610 .func_inferred = .pl_node,
1611 .func_extended = .pl_node,
1605 .import = .str_tok,1612 .import = .str_tok,
1606 .int = .int,1613 .int = .int,
1607 .int_big = .str,1614 .int_big = .str,
...@@ -1623,6 +1630,8 @@ pub const Inst = struct {...@@ -1623,6 +1630,8 @@ pub const Inst = struct {
1623 .ret_tok = .un_tok,1630 .ret_tok = .un_tok,
1624 .ret_err_value = .str_tok,1631 .ret_err_value = .str_tok,
1625 .ret_err_value_code = .str_tok,1632 .ret_err_value_code = .str_tok,
1633 .ret_ptr = .node,
1634 .ret_type = .node,
1626 .ptr_type_simple = .ptr_type_simple,1635 .ptr_type_simple = .ptr_type_simple,
1627 .ptr_type = .ptr_type,1636 .ptr_type = .ptr_type,
1628 .slice_start = .pl_node,1637 .slice_start = .pl_node,
...@@ -1685,7 +1694,6 @@ pub const Inst = struct {...@@ -1685,7 +1694,6 @@ pub const Inst = struct {
1685 .type_info = .un_node,1694 .type_info = .un_node,
1686 .size_of = .un_node,1695 .size_of = .un_node,
1687 .bit_size_of = .un_node,1696 .bit_size_of = .un_node,
1688 .fence = .un_node,
16891697
1690 .ptr_to_int = .un_node,1698 .ptr_to_int = .un_node,
1691 .error_to_int = .un_node,1699 .error_to_int = .un_node,
...@@ -1698,9 +1706,7 @@ pub const Inst = struct {...@@ -1698,9 +1706,7 @@ pub const Inst = struct {
1698 .embed_file = .un_node,1706 .embed_file = .un_node,
1699 .error_name = .un_node,1707 .error_name = .un_node,
1700 .panic = .un_node,1708 .panic = .un_node,
1701 .set_align_stack = .un_node,
1702 .set_cold = .un_node,1709 .set_cold = .un_node,
1703 .set_float_mode = .un_node,
1704 .set_runtime_safety = .un_node,1710 .set_runtime_safety = .un_node,
1705 .sqrt = .un_node,1711 .sqrt = .un_node,
1706 .sin = .un_node,1712 .sin = .un_node,
...@@ -1728,7 +1734,6 @@ pub const Inst = struct {...@@ -1728,7 +1734,6 @@ pub const Inst = struct {
1728 .int_to_enum = .pl_node,1734 .int_to_enum = .pl_node,
1729 .float_cast = .pl_node,1735 .float_cast = .pl_node,
1730 .int_cast = .pl_node,1736 .int_cast = .pl_node,
1731 .err_set_cast = .pl_node,
1732 .ptr_cast = .pl_node,1737 .ptr_cast = .pl_node,
1733 .truncate = .pl_node,1738 .truncate = .pl_node,
1734 .align_cast = .pl_node,1739 .align_cast = .pl_node,
...@@ -1788,7 +1793,6 @@ pub const Inst = struct {...@@ -1788,7 +1793,6 @@ pub const Inst = struct {
17881793
1789 .@"resume" = .un_node,1794 .@"resume" = .un_node,
1790 .@"await" = .un_node,1795 .@"await" = .un_node,
1791 .await_nosuspend = .un_node,
17921796
1793 .closure_capture = .un_tok,1797 .closure_capture = .un_tok,
1794 .closure_get = .inst_node,1798 .closure_get = .inst_node,
...@@ -1806,11 +1810,6 @@ pub const Inst = struct {...@@ -1806,11 +1810,6 @@ pub const Inst = struct {
1806 /// Rarer instructions are here; ones that do not fit in the 8-bit `Tag` enum.1810 /// Rarer instructions are here; ones that do not fit in the 8-bit `Tag` enum.
1807 /// `noreturn` instructions may not go here; they must be part of the main `Tag` enum.1811 /// `noreturn` instructions may not go here; they must be part of the main `Tag` enum.
1808 pub const Extended = enum(u16) {1812 pub const Extended = enum(u16) {
1809 /// Represents a function declaration or function prototype, depending on
1810 /// whether body_len is 0.
1811 /// `operand` is payload index to `ExtendedFunc`.
1812 /// `small` is `ExtendedFunc.Small`.
1813 func,
1814 /// Declares a global variable.1813 /// Declares a global variable.
1815 /// `operand` is payload index to `ExtendedVar`.1814 /// `operand` is payload index to `ExtendedVar`.
1816 /// `small` is `ExtendedVar.Small`.1815 /// `small` is `ExtendedVar.Small`.
...@@ -1834,12 +1833,6 @@ pub const Inst = struct {...@@ -1834,12 +1833,6 @@ pub const Inst = struct {
1834 /// `operand` is payload index to `OpaqueDecl`.1833 /// `operand` is payload index to `OpaqueDecl`.
1835 /// `small` is `OpaqueDecl.Small`.1834 /// `small` is `OpaqueDecl.Small`.
1836 opaque_decl,1835 opaque_decl,
1837 /// Obtains a pointer to the return value.
1838 /// `operand` is `src_node: i32`.
1839 ret_ptr,
1840 /// Obtains the return type of the in-scope function.
1841 /// `operand` is `src_node: i32`.
1842 ret_type,
1843 /// Implements the `@This` builtin.1836 /// Implements the `@This` builtin.
1844 /// `operand` is `src_node: i32`.1837 /// `operand` is `src_node: i32`.
1845 this,1838 this,
...@@ -1917,10 +1910,6 @@ pub const Inst = struct {...@@ -1917,10 +1910,6 @@ pub const Inst = struct {
1917 /// The `@prefetch` builtin.1910 /// The `@prefetch` builtin.
1918 /// `operand` is payload index to `BinNode`.1911 /// `operand` is payload index to `BinNode`.
1919 prefetch,1912 prefetch,
1920 /// Marks the beginning of a semantic scope for debug info variables.
1921 dbg_block_begin,
1922 /// Marks the end of a semantic scope for debug info variables.
1923 dbg_block_end,
1924 /// Given a pointer to a struct or object that contains virtual fields, returns the1913 /// Given a pointer to a struct or object that contains virtual fields, returns the
1925 /// named field. If there is no named field, searches in the type for a decl that1914 /// named field. If there is no named field, searches in the type for a decl that
1926 /// matches the field name. The decl is resolved and we ensure that it's a function1915 /// matches the field name. The decl is resolved and we ensure that it's a function
...@@ -1931,6 +1920,22 @@ pub const Inst = struct {...@@ -1931,6 +1920,22 @@ pub const Inst = struct {
1931 /// `builtin_call` instruction. Any other use is invalid zir and may crash the compiler.1920 /// `builtin_call` instruction. Any other use is invalid zir and may crash the compiler.
1932 /// Uses `pl_node` field. The AST node is the `@field` builtin. Payload is FieldNamedNode.1921 /// Uses `pl_node` field. The AST node is the `@field` builtin. Payload is FieldNamedNode.
1933 field_call_bind_named,1922 field_call_bind_named,
1923 /// Implements the `@fence` builtin.
1924 /// `operand` is payload index to `UnNode`.
1925 fence,
1926 /// Implement builtin `@setFloatMode`.
1927 /// `operand` is payload index to `UnNode`.
1928 set_float_mode,
1929 /// Implement builtin `@setAlignStack`.
1930 /// `operand` is payload index to `UnNode`.
1931 set_align_stack,
1932 /// Implements the `@errSetCast` builtin.
1933 /// `operand` is payload index to `BinNode`. `lhs` is dest type, `rhs` is operand.
1934 err_set_cast,
1935 /// `operand` is payload index to `UnNode`.
1936 await_nosuspend,
1937 /// `operand` is `src_node: i32`.
1938 breakpoint,
19341939
1935 pub const InstData = struct {1940 pub const InstData = struct {
1936 opcode: Extended,1941 opcode: Extended,
...@@ -2502,11 +2507,7 @@ pub const Inst = struct {...@@ -2502,11 +2507,7 @@ pub const Inst = struct {
2502 /// Offset from Decl AST node index.2507 /// Offset from Decl AST node index.
2503 /// `Tag` determines which kind of AST node this points to.2508 /// `Tag` determines which kind of AST node this points to.
2504 src_node: i32,2509 src_node: i32,
2505 /// `false`: Not safety checked - the compiler will assume the2510 force_comptime: bool,
2506 /// correctness of this instruction.
2507 /// `true`: In safety-checked modes, this will generate a call
2508 /// to the panic function unless it can be proven unreachable by the compiler.
2509 safety: bool,
25102511
2511 pub fn src(self: @This()) LazySrcLoc {2512 pub fn src(self: @This()) LazySrcLoc {
2512 return .{ .node_offset = self.src_node };2513 return .{ .node_offset = self.src_node };
...@@ -2623,14 +2624,14 @@ pub const Inst = struct {...@@ -2623,14 +2624,14 @@ pub const Inst = struct {
2623 /// 4. body: Index // for each body_len2624 /// 4. body: Index // for each body_len
2624 /// 5. src_locs: Func.SrcLocs // if body_len != 02625 /// 5. src_locs: Func.SrcLocs // if body_len != 0
2625 pub const ExtendedFunc = struct {2626 pub const ExtendedFunc = struct {
2626 src_node: i32,
2627 /// If this is 0 it means a void return type.2627 /// If this is 0 it means a void return type.
2628 ret_body_len: u32,2628 ret_body_len: u32,
2629 /// Points to the block that contains the param instructions for this function.2629 /// Points to the block that contains the param instructions for this function.
2630 param_block: Index,2630 param_block: Index,
2631 body_len: u32,2631 body_len: u32,
2632 bits: Bits,
26322633
2633 pub const Small = packed struct {2634 pub const Bits = packed struct {
2634 is_var_args: bool,2635 is_var_args: bool,
2635 is_inferred_error: bool,2636 is_inferred_error: bool,
2636 has_lib_name: bool,2637 has_lib_name: bool,
...@@ -2638,7 +2639,7 @@ pub const Inst = struct {...@@ -2638,7 +2639,7 @@ pub const Inst = struct {
2638 has_align: bool,2639 has_align: bool,
2639 is_test: bool,2640 is_test: bool,
2640 is_extern: bool,2641 is_extern: bool,
2641 _: u9 = undefined,2642 _: u25 = undefined,
2642 };2643 };
2643 };2644 };
26442645
...@@ -3462,14 +3463,11 @@ pub fn declIterator(zir: Zir, decl_inst: u32) DeclIterator {...@@ -3462,14 +3463,11 @@ pub fn declIterator(zir: Zir, decl_inst: u32) DeclIterator {
3462 switch (tags[decl_inst]) {3463 switch (tags[decl_inst]) {
3463 // Functions are allowed and yield no iterations.3464 // Functions are allowed and yield no iterations.
3464 // There is one case matching this in the extended instruction set below.3465 // There is one case matching this in the extended instruction set below.
3465 .func,3466 .func, .func_inferred, .func_extended => return declIteratorInner(zir, 0, 0),
3466 .func_inferred,
3467 => return declIteratorInner(zir, 0, 0),
34683467
3469 .extended => {3468 .extended => {
3470 const extended = datas[decl_inst].extended;3469 const extended = datas[decl_inst].extended;
3471 switch (extended.opcode) {3470 switch (extended.opcode) {
3472 .func => return declIteratorInner(zir, 0, 0),
3473 .struct_decl => {3471 .struct_decl => {
3474 const small = @bitCast(Inst.StructDecl.Small, extended.small);3472 const small = @bitCast(Inst.StructDecl.Small, extended.small);
3475 var extra_index: usize = extended.operand;3473 var extra_index: usize = extended.operand;
...@@ -3574,21 +3572,21 @@ fn findDeclsInner(...@@ -3574,21 +3572,21 @@ fn findDeclsInner(
3574 const body = zir.extra[extra.end..][0..extra.data.body_len];3572 const body = zir.extra[extra.end..][0..extra.data.body_len];
3575 return zir.findDeclsBody(list, body);3573 return zir.findDeclsBody(list, body);
3576 },3574 },
3575 .func_extended => {
3576 try list.append(inst);
3577
3578 const inst_data = datas[inst].pl_node;
3579 const extra = zir.extraData(Inst.ExtendedFunc, inst_data.payload_index);
3580 var extra_index: usize = extra.end;
3581 extra_index += @boolToInt(extra.data.bits.has_lib_name);
3582 extra_index += @boolToInt(extra.data.bits.has_cc);
3583 extra_index += @boolToInt(extra.data.bits.has_align);
3584 const body = zir.extra[extra_index..][0..extra.data.body_len];
3585 return zir.findDeclsBody(list, body);
3586 },
3577 .extended => {3587 .extended => {
3578 const extended = datas[inst].extended;3588 const extended = datas[inst].extended;
3579 switch (extended.opcode) {3589 switch (extended.opcode) {
3580 .func => {
3581 try list.append(inst);
3582
3583 const extra = zir.extraData(Inst.ExtendedFunc, extended.operand);
3584 const small = @bitCast(Inst.ExtendedFunc.Small, extended.small);
3585 var extra_index: usize = extra.end;
3586 extra_index += @boolToInt(small.has_lib_name);
3587 extra_index += @boolToInt(small.has_cc);
3588 extra_index += @boolToInt(small.has_align);
3589 const body = zir.extra[extra_index..][0..extra.data.body_len];
3590 return zir.findDeclsBody(list, body);
3591 },
35923590
3593 // Decl instructions are interesting but have no body.3591 // Decl instructions are interesting but have no body.
3594 // TODO yes they do have a body actually. recurse over them just like block instructions.3592 // TODO yes they do have a body actually. recurse over them just like block instructions.
...@@ -3735,15 +3733,13 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {...@@ -3735,15 +3733,13 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
3735 .body = body,3733 .body = body,
3736 };3734 };
3737 },3735 },
3738 .extended => blk: {3736 .func_extended => blk: {
3739 const extended = datas[fn_inst].extended;3737 const inst_data = datas[fn_inst].pl_node;
3740 assert(extended.opcode == .func);3738 const extra = zir.extraData(Inst.ExtendedFunc, inst_data.payload_index);
3741 const extra = zir.extraData(Inst.ExtendedFunc, extended.operand);
3742 const small = @bitCast(Inst.ExtendedFunc.Small, extended.small);
3743 var extra_index: usize = extra.end;3739 var extra_index: usize = extra.end;
3744 extra_index += @boolToInt(small.has_lib_name);3740 extra_index += @boolToInt(extra.data.bits.has_lib_name);
3745 extra_index += @boolToInt(small.has_cc);3741 extra_index += @boolToInt(extra.data.bits.has_cc);
3746 extra_index += @boolToInt(small.has_align);3742 extra_index += @boolToInt(extra.data.bits.has_align);
3747 const ret_ty_body = zir.extra[extra_index..][0..extra.data.ret_body_len];3743 const ret_ty_body = zir.extra[extra_index..][0..extra.data.ret_body_len];
3748 extra_index += ret_ty_body.len;3744 extra_index += ret_ty_body.len;
3749 const body = zir.extra[extra_index..][0..extra.data.body_len];3745 const body = zir.extra[extra_index..][0..extra.data.body_len];
src/print_zir.zig+33-28
...@@ -200,9 +200,7 @@ const Writer = struct {...@@ -200,9 +200,7 @@ const Writer = struct {
200 .embed_file,200 .embed_file,
201 .error_name,201 .error_name,
202 .panic,202 .panic,
203 .set_align_stack,
204 .set_cold,203 .set_cold,
205 .set_float_mode,
206 .set_runtime_safety,204 .set_runtime_safety,
207 .sqrt,205 .sqrt,
208 .sin,206 .sin,
...@@ -231,8 +229,6 @@ const Writer = struct {...@@ -231,8 +229,6 @@ const Writer = struct {
231 .elem_type,229 .elem_type,
232 .@"resume",230 .@"resume",
233 .@"await",231 .@"await",
234 .await_nosuspend,
235 .fence,
236 .switch_cond,232 .switch_cond,
237 .switch_cond_ref,233 .switch_cond_ref,
238 .array_base_ptr,234 .array_base_ptr,
...@@ -343,7 +339,6 @@ const Writer = struct {...@@ -343,7 +339,6 @@ const Writer = struct {
343 .int_to_enum,339 .int_to_enum,
344 .float_cast,340 .float_cast,
345 .int_cast,341 .int_cast,
346 .err_set_cast,
347 .ptr_cast,342 .ptr_cast,
348 .truncate,343 .truncate,
349 .align_cast,344 .align_cast,
...@@ -405,13 +400,14 @@ const Writer = struct {...@@ -405,13 +400,14 @@ const Writer = struct {
405400
406 .as_node => try self.writeAs(stream, inst),401 .as_node => try self.writeAs(stream, inst),
407402
408 .breakpoint,
409 .repeat,403 .repeat,
410 .repeat_inline,404 .repeat_inline,
411 .alloc_inferred,405 .alloc_inferred,
412 .alloc_inferred_mut,406 .alloc_inferred_mut,
413 .alloc_inferred_comptime,407 .alloc_inferred_comptime,
414 .alloc_inferred_comptime_mut,408 .alloc_inferred_comptime_mut,
409 .ret_ptr,
410 .ret_type,
415 => try self.writeNode(stream, inst),411 => try self.writeNode(stream, inst),
416412
417 .error_value,413 .error_value,
...@@ -433,6 +429,7 @@ const Writer = struct {...@@ -433,6 +429,7 @@ const Writer = struct {
433429
434 .func => try self.writeFunc(stream, inst, false),430 .func => try self.writeFunc(stream, inst, false),
435 .func_inferred => try self.writeFunc(stream, inst, true),431 .func_inferred => try self.writeFunc(stream, inst, true),
432 .func_extended => try self.writeFuncExtended(stream, inst),
436433
437 .@"unreachable" => try self.writeUnreachable(stream, inst),434 .@"unreachable" => try self.writeUnreachable(stream, inst),
438435
...@@ -444,6 +441,10 @@ const Writer = struct {...@@ -444,6 +441,10 @@ const Writer = struct {
444441
445 .dbg_stmt => try self.writeDbgStmt(stream, inst),442 .dbg_stmt => try self.writeDbgStmt(stream, inst),
446443
444 .dbg_block_begin,
445 .dbg_block_end,
446 => try stream.writeAll("))"),
447
447 .closure_get => try self.writeInstNode(stream, inst),448 .closure_get => try self.writeInstNode(stream, inst),
448449
449 .extended => try self.writeExtended(stream, inst),450 .extended => try self.writeExtended(stream, inst),
...@@ -454,13 +455,12 @@ const Writer = struct {...@@ -454,13 +455,12 @@ const Writer = struct {
454 const extended = self.code.instructions.items(.data)[inst].extended;455 const extended = self.code.instructions.items(.data)[inst].extended;
455 try stream.print("{s}(", .{@tagName(extended.opcode)});456 try stream.print("{s}(", .{@tagName(extended.opcode)});
456 switch (extended.opcode) {457 switch (extended.opcode) {
457 .ret_ptr,
458 .ret_type,
459 .this,458 .this,
460 .ret_addr,459 .ret_addr,
461 .error_return_trace,460 .error_return_trace,
462 .frame,461 .frame,
463 .frame_address,462 .frame_address,
463 .breakpoint,
464 => try self.writeExtNode(stream, extended),464 => try self.writeExtNode(stream, extended),
465465
466 .builtin_src => {466 .builtin_src => {
...@@ -469,12 +469,7 @@ const Writer = struct {...@@ -469,12 +469,7 @@ const Writer = struct {
469 try stream.print(":{d}:{d}", .{ inst_data.line + 1, inst_data.column + 1 });469 try stream.print(":{d}:{d}", .{ inst_data.line + 1, inst_data.column + 1 });
470 },470 },
471471
472 .dbg_block_begin,
473 .dbg_block_end,
474 => try stream.writeAll("))"),
475
476 .@"asm" => try self.writeAsm(stream, extended),472 .@"asm" => try self.writeAsm(stream, extended),
477 .func => try self.writeFuncExtended(stream, extended),
478 .variable => try self.writeVarExtended(stream, extended),473 .variable => try self.writeVarExtended(stream, extended),
479 .alloc => try self.writeAllocExtended(stream, extended),474 .alloc => try self.writeAllocExtended(stream, extended),
480475
...@@ -492,7 +487,14 @@ const Writer = struct {...@@ -492,7 +487,14 @@ const Writer = struct {
492 .enum_decl => try self.writeEnumDecl(stream, extended),487 .enum_decl => try self.writeEnumDecl(stream, extended),
493 .opaque_decl => try self.writeOpaqueDecl(stream, extended),488 .opaque_decl => try self.writeOpaqueDecl(stream, extended),
494489
495 .c_undef, .c_include, .wasm_memory_size => {490 .await_nosuspend,
491 .c_undef,
492 .c_include,
493 .fence,
494 .set_float_mode,
495 .set_align_stack,
496 .wasm_memory_size,
497 => {
496 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;498 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
497 const src: LazySrcLoc = .{ .node_offset = inst_data.node };499 const src: LazySrcLoc = .{ .node_offset = inst_data.node };
498 try self.writeInstRef(stream, inst_data.operand);500 try self.writeInstRef(stream, inst_data.operand);
...@@ -500,7 +502,12 @@ const Writer = struct {...@@ -500,7 +502,12 @@ const Writer = struct {
500 try self.writeSrc(stream, src);502 try self.writeSrc(stream, src);
501 },503 },
502504
503 .builtin_extern, .c_define, .wasm_memory_grow, .prefetch => {505 .builtin_extern,
506 .c_define,
507 .err_set_cast,
508 .wasm_memory_grow,
509 .prefetch,
510 => {
504 const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;511 const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
505 const src: LazySrcLoc = .{ .node_offset = inst_data.node };512 const src: LazySrcLoc = .{ .node_offset = inst_data.node };
506 try self.writeInstRef(stream, inst_data.lhs);513 try self.writeInstRef(stream, inst_data.lhs);
...@@ -1913,24 +1920,24 @@ const Writer = struct {...@@ -1913,24 +1920,24 @@ const Writer = struct {
1913 );1920 );
1914 }1921 }
19151922
1916 fn writeFuncExtended(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {1923 fn writeFuncExtended(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
1917 const extra = self.code.extraData(Zir.Inst.ExtendedFunc, extended.operand);1924 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
1918 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };1925 const extra = self.code.extraData(Zir.Inst.ExtendedFunc, inst_data.payload_index);
1919 const small = @bitCast(Zir.Inst.ExtendedFunc.Small, extended.small);1926 const src = inst_data.src();
19201927
1921 var extra_index: usize = extra.end;1928 var extra_index: usize = extra.end;
1922 if (small.has_lib_name) {1929 if (extra.data.bits.has_lib_name) {
1923 const lib_name = self.code.nullTerminatedString(self.code.extra[extra_index]);1930 const lib_name = self.code.nullTerminatedString(self.code.extra[extra_index]);
1924 extra_index += 1;1931 extra_index += 1;
1925 try stream.print("lib_name=\"{}\", ", .{std.zig.fmtEscapes(lib_name)});1932 try stream.print("lib_name=\"{}\", ", .{std.zig.fmtEscapes(lib_name)});
1926 }1933 }
1927 try self.writeFlag(stream, "test, ", small.is_test);1934 try self.writeFlag(stream, "test, ", extra.data.bits.is_test);
1928 const cc: Zir.Inst.Ref = if (!small.has_cc) .none else blk: {1935 const cc: Zir.Inst.Ref = if (!extra.data.bits.has_cc) .none else blk: {
1929 const cc = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);1936 const cc = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);
1930 extra_index += 1;1937 extra_index += 1;
1931 break :blk cc;1938 break :blk cc;
1932 };1939 };
1933 const align_inst: Zir.Inst.Ref = if (!small.has_align) .none else blk: {1940 const align_inst: Zir.Inst.Ref = if (!extra.data.bits.has_align) .none else blk: {
1934 const align_inst = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);1941 const align_inst = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);
1935 extra_index += 1;1942 extra_index += 1;
1936 break :blk align_inst;1943 break :blk align_inst;
...@@ -1949,9 +1956,9 @@ const Writer = struct {...@@ -1949,9 +1956,9 @@ const Writer = struct {
1949 return self.writeFuncCommon(1956 return self.writeFuncCommon(
1950 stream,1957 stream,
1951 ret_ty_body,1958 ret_ty_body,
1952 small.is_inferred_error,1959 extra.data.bits.is_inferred_error,
1953 small.is_var_args,1960 extra.data.bits.is_var_args,
1954 small.is_extern,1961 extra.data.bits.is_extern,
1955 cc,1962 cc,
1956 align_inst,1963 align_inst,
1957 body,1964 body,
...@@ -2091,8 +2098,6 @@ const Writer = struct {...@@ -2091,8 +2098,6 @@ const Writer = struct {
20912098
2092 fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {2099 fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2093 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";2100 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";
2094 const safety_str = if (inst_data.safety) "safe" else "unsafe";
2095 try stream.print("{s}) ", .{safety_str});
2096 try self.writeSrc(stream, inst_data.src());2101 try self.writeSrc(stream, inst_data.src());
2097 }2102 }
20982103
test/compile_errors/stage2/comptime_unreachable.zig created+7
...@@ -0,0 +1,7 @@
1pub export fn entry() void {
2 comptime unreachable;
3}
4
5// error
6//
7// :2:14: error: reached unreachable code
test/incremental/x86_64-linux/comptime_var.3.zig+1-1
...@@ -7,4 +7,4 @@ pub fn main() void {}...@@ -7,4 +7,4 @@ pub fn main() void {}
77
8// error8// error
9//9//
10// :4:17: error: unable to resolve comptime value10// :4:17: error: reached unreachable code
test/incremental/x86_64-macos/comptime_var.3.zig+1-1
...@@ -7,4 +7,4 @@ pub fn main() void {}...@@ -7,4 +7,4 @@ pub fn main() void {}
77
8// error8// error
9//9//
10// :4:17: error: unable to resolve comptime value10// :4:17: error: reached unreachable code