authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-24 17:31:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-24 17:31:15-07:00
loge018e64a53eb7fdffedb3efadb862f400f9e9f70
treed7b9fb223c21cb882a7c5a48a2d744822d4271a4
parent15e891823e34ad57ccc5f1636323c5b7e718d44f

stage2: move overflow builtin ZIR instructions to Extended

make some more room in our ZIR enum tag space

3 files changed, 64 insertions(+), 57 deletions(-)

src/AstGen.zig+5-7
......@@ -1946,10 +1946,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
19461946 .type_info,
19471947 .size_of,
19481948 .bit_size_of,
1949 .add_with_overflow,
1950 .sub_with_overflow,
1951 .mul_with_overflow,
1952 .shl_with_overflow,
19531949 .log2_int_type,
19541950 .typeof_log2_int_type,
19551951 .ptr_to_int,
......@@ -6374,7 +6370,8 @@ fn builtinCall(
63746370 const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
63756371 const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, params[2]);
63766372 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);
6377 const result = try gz.addPlNode(.shl_with_overflow, node, Zir.Inst.OverflowArithmetic{
6373 const result = try gz.addExtendedPayload(.shl_with_overflow, Zir.Inst.OverflowArithmetic{
6374 .node = gz.nodeIndexToRelative(node),
63786375 .lhs = lhs,
63796376 .rhs = rhs,
63806377 .ptr = ptr,
......@@ -6734,7 +6731,7 @@ fn overflowArithmetic(
67346731 rl: ResultLoc,
67356732 node: ast.Node.Index,
67366733 params: []const ast.Node.Index,
6737 tag: Zir.Inst.Tag,
6734 tag: Zir.Inst.Extended,
67386735) InnerError!Zir.Inst.Ref {
67396736 const int_type = try typeExpr(gz, scope, params[0]);
67406737 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
......@@ -6749,7 +6746,8 @@ fn overflowArithmetic(
67496746 const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
67506747 const rhs = try expr(gz, scope, .{ .ty = int_type }, params[2]);
67516748 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);
6752 const result = try gz.addPlNode(tag, node, Zir.Inst.OverflowArithmetic{
6749 const result = try gz.addExtendedPayload(tag, Zir.Inst.OverflowArithmetic{
6750 .node = gz.nodeIndexToRelative(node),
67536751 .lhs = lhs,
67546752 .rhs = rhs,
67556753 .ptr = ptr,
src/Sema.zig+30-27
......@@ -356,11 +356,6 @@ pub fn analyzeBody(
356356 .sub => try sema.zirArithmetic(block, inst),
357357 .subwrap => try sema.zirArithmetic(block, inst),
358358
359 .add_with_overflow => try sema.zirOverflowArithmetic(block, inst),
360 .sub_with_overflow => try sema.zirOverflowArithmetic(block, inst),
361 .mul_with_overflow => try sema.zirOverflowArithmetic(block, inst),
362 .shl_with_overflow => try sema.zirOverflowArithmetic(block, inst),
363
364359 // Instructions that we know to *always* be noreturn based solely on their tag.
365360 // These functions match the return type of analyzeBody so that we can
366361 // tail call them here.
......@@ -504,25 +499,29 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErro
504499 const extended = sema.code.instructions.items(.data)[inst].extended;
505500 switch (extended.opcode) {
506501 // zig fmt: off
507 .func => return sema.zirFuncExtended( block, extended),
508 .ret_ptr => return sema.zirRetPtr( block, extended),
509 .ret_type => return sema.zirRetType( block, extended),
510 .this => return sema.zirThis( block, extended),
511 .ret_addr => return sema.zirRetAddr( block, extended),
512 .builtin_src => return sema.zirBuiltinSrc( block, extended),
513 .error_return_trace => return sema.zirErrorReturnTrace(block, extended),
514 .frame => return sema.zirFrame( block, extended),
515 .frame_address => return sema.zirFrameAddress( block, extended),
516 .alloc => return sema.zirAllocExtended( block, extended),
517 .builtin_extern => return sema.zirBuiltinExtern( block, extended),
518 .@"asm" => return sema.zirAsm( block, extended),
519 .typeof_peer => return sema.zirTypeofPeer( block, extended),
520 .compile_log => return sema.zirCompileLog( block, extended),
521 .c_undef => return sema.zirCUndef( block, extended),
522 .c_include => return sema.zirCInclude( block, extended),
523 .c_define => return sema.zirCDefine( block, extended),
524 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
525 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
502 .func => return sema.zirFuncExtended( block, extended),
503 .ret_ptr => return sema.zirRetPtr( block, extended),
504 .ret_type => return sema.zirRetType( block, extended),
505 .this => return sema.zirThis( block, extended),
506 .ret_addr => return sema.zirRetAddr( block, extended),
507 .builtin_src => return sema.zirBuiltinSrc( block, extended),
508 .error_return_trace => return sema.zirErrorReturnTrace( block, extended),
509 .frame => return sema.zirFrame( block, extended),
510 .frame_address => return sema.zirFrameAddress( block, extended),
511 .alloc => return sema.zirAllocExtended( block, extended),
512 .builtin_extern => return sema.zirBuiltinExtern( block, extended),
513 .@"asm" => return sema.zirAsm( block, extended),
514 .typeof_peer => return sema.zirTypeofPeer( block, extended),
515 .compile_log => return sema.zirCompileLog( block, extended),
516 .add_with_overflow => return sema.zirOverflowArithmetic(block, extended),
517 .sub_with_overflow => return sema.zirOverflowArithmetic(block, extended),
518 .mul_with_overflow => return sema.zirOverflowArithmetic(block, extended),
519 .shl_with_overflow => return sema.zirOverflowArithmetic(block, extended),
520 .c_undef => return sema.zirCUndef( block, extended),
521 .c_include => return sema.zirCInclude( block, extended),
522 .c_define => return sema.zirCDefine( block, extended),
523 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
524 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
526525 // zig fmt: on
527526 }
528527}
......@@ -4272,12 +4271,16 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr
42724271 return sema.analyzeArithmetic(block, tag_override, lhs, rhs, src, lhs_src, rhs_src);
42734272}
42744273
4275fn zirOverflowArithmetic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4274fn zirOverflowArithmetic(
4275 sema: *Sema,
4276 block: *Scope.Block,
4277 extended: Zir.Inst.Extended.InstData,
4278) InnerError!*Inst {
42764279 const tracy = trace(@src());
42774280 defer tracy.end();
42784281
4279 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
4280 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
4282 const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
4283 const src: LazySrcLoc = .{ .node_offset = extra.node };
42814284
42824285 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirOverflowArithmetic", .{});
42834286}
src/Zir.zig+29-23
......@@ -693,14 +693,6 @@ pub const Inst = struct {
693693 bit_size_of,
694694 /// Implements the `@fence` builtin. Uses `node`.
695695 fence,
696 /// Implements the `@addWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
697 add_with_overflow,
698 /// Implements the `@subWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
699 sub_with_overflow,
700 /// Implements the `@mulWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
701 mul_with_overflow,
702 /// Implements the `@shlWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
703 shl_with_overflow,
704696
705697 /// Implement builtin `@ptrToInt`. Uses `un_node`.
706698 /// Convert a pointer to a `usize` integer.
......@@ -1118,10 +1110,6 @@ pub const Inst = struct {
11181110 .type_info,
11191111 .size_of,
11201112 .bit_size_of,
1121 .add_with_overflow,
1122 .sub_with_overflow,
1123 .mul_with_overflow,
1124 .shl_with_overflow,
11251113 .ptr_to_int,
11261114 .align_of,
11271115 .bool_to_int,
......@@ -1273,6 +1261,22 @@ pub const Inst = struct {
12731261 /// `small` is `operands_len`.
12741262 /// The AST node is the builtin call.
12751263 typeof_peer,
1264 /// Implements the `@addWithOverflow` builtin.
1265 /// `operand` is payload index to `OverflowArithmetic`.
1266 /// `small` is unused.
1267 add_with_overflow,
1268 /// Implements the `@subWithOverflow` builtin.
1269 /// `operand` is payload index to `OverflowArithmetic`.
1270 /// `small` is unused.
1271 sub_with_overflow,
1272 /// Implements the `@mulWithOverflow` builtin.
1273 /// `operand` is payload index to `OverflowArithmetic`.
1274 /// `small` is unused.
1275 mul_with_overflow,
1276 /// Implements the `@shlWithOverflow` builtin.
1277 /// `operand` is payload index to `OverflowArithmetic`.
1278 /// `small` is unused.
1279 shl_with_overflow,
12761280 /// `operand` is payload index to `UnNode`.
12771281 c_undef,
12781282 /// `operand` is payload index to `UnNode`.
......@@ -2201,6 +2205,7 @@ pub const Inst = struct {
22012205 };
22022206
22032207 pub const OverflowArithmetic = struct {
2208 node: i32,
22042209 lhs: Ref,
22052210 rhs: Ref,
22062211 ptr: Ref,
......@@ -2485,12 +2490,6 @@ const Writer = struct {
24852490
24862491 .error_set_decl => try self.writePlNodeErrorSetDecl(stream, inst),
24872492
2488 .add_with_overflow,
2489 .sub_with_overflow,
2490 .mul_with_overflow,
2491 .shl_with_overflow,
2492 => try self.writePlNodeOverflowArithmetic(stream, inst),
2493
24942493 .add,
24952494 .addwrap,
24962495 .array_cat,
......@@ -2658,6 +2657,12 @@ const Writer = struct {
26582657 .typeof_peer,
26592658 => try self.writeNodeMultiOp(stream, extended),
26602659
2660 .add_with_overflow,
2661 .sub_with_overflow,
2662 .mul_with_overflow,
2663 .shl_with_overflow,
2664 => try self.writeOverflowArithmetic(stream, extended),
2665
26612666 .alloc,
26622667 .builtin_extern,
26632668 .c_undef,
......@@ -2931,16 +2936,17 @@ const Writer = struct {
29312936 try self.writeSrc(stream, src);
29322937 }
29332938
2934 fn writePlNodeOverflowArithmetic(self: *Writer, stream: anytype, inst: Inst.Index) !void {
2935 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
2936 const extra = self.code.extraData(Inst.OverflowArithmetic, inst_data.payload_index).data;
2939 fn writeOverflowArithmetic(self: *Writer, stream: anytype, extended: Inst.Extended.InstData) !void {
2940 const extra = self.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
2941 const src: LazySrcLoc = .{ .node_offset = extra.node };
2942
29372943 try self.writeInstRef(stream, extra.lhs);
29382944 try stream.writeAll(", ");
29392945 try self.writeInstRef(stream, extra.rhs);
29402946 try stream.writeAll(", ");
29412947 try self.writeInstRef(stream, extra.ptr);
2942 try stream.writeAll(") ");
2943 try self.writeSrc(stream, inst_data.src());
2948 try stream.writeAll(")) ");
2949 try self.writeSrc(stream, src);
29442950 }
29452951
29462952 fn writePlNodeCall(self: *Writer, stream: anytype, inst: Inst.Index) !void {