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...@@ -1946,10 +1946,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
1946 .type_info,1946 .type_info,
1947 .size_of,1947 .size_of,
1948 .bit_size_of,1948 .bit_size_of,
1949 .add_with_overflow,
1950 .sub_with_overflow,
1951 .mul_with_overflow,
1952 .shl_with_overflow,
1953 .log2_int_type,1949 .log2_int_type,
1954 .typeof_log2_int_type,1950 .typeof_log2_int_type,
1955 .ptr_to_int,1951 .ptr_to_int,
...@@ -6374,7 +6370,8 @@ fn builtinCall(...@@ -6374,7 +6370,8 @@ fn builtinCall(
6374 const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);6370 const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
6375 const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, params[2]);6371 const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, params[2]);
6376 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);6372 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),
6378 .lhs = lhs,6375 .lhs = lhs,
6379 .rhs = rhs,6376 .rhs = rhs,
6380 .ptr = ptr,6377 .ptr = ptr,
...@@ -6734,7 +6731,7 @@ fn overflowArithmetic(...@@ -6734,7 +6731,7 @@ fn overflowArithmetic(
6734 rl: ResultLoc,6731 rl: ResultLoc,
6735 node: ast.Node.Index,6732 node: ast.Node.Index,
6736 params: []const ast.Node.Index,6733 params: []const ast.Node.Index,
6737 tag: Zir.Inst.Tag,6734 tag: Zir.Inst.Extended,
6738) InnerError!Zir.Inst.Ref {6735) InnerError!Zir.Inst.Ref {
6739 const int_type = try typeExpr(gz, scope, params[0]);6736 const int_type = try typeExpr(gz, scope, params[0]);
6740 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{6737 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
...@@ -6749,7 +6746,8 @@ fn overflowArithmetic(...@@ -6749,7 +6746,8 @@ fn overflowArithmetic(
6749 const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);6746 const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
6750 const rhs = try expr(gz, scope, .{ .ty = int_type }, params[2]);6747 const rhs = try expr(gz, scope, .{ .ty = int_type }, params[2]);
6751 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);6748 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),
6753 .lhs = lhs,6751 .lhs = lhs,
6754 .rhs = rhs,6752 .rhs = rhs,
6755 .ptr = ptr,6753 .ptr = ptr,
src/Sema.zig+30-27
...@@ -356,11 +356,6 @@ pub fn analyzeBody(...@@ -356,11 +356,6 @@ pub fn analyzeBody(
356 .sub => try sema.zirArithmetic(block, inst),356 .sub => try sema.zirArithmetic(block, inst),
357 .subwrap => try sema.zirArithmetic(block, inst),357 .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
364 // Instructions that we know to *always* be noreturn based solely on their tag.359 // Instructions that we know to *always* be noreturn based solely on their tag.
365 // These functions match the return type of analyzeBody so that we can360 // These functions match the return type of analyzeBody so that we can
366 // tail call them here.361 // tail call them here.
...@@ -504,25 +499,29 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErro...@@ -504,25 +499,29 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErro
504 const extended = sema.code.instructions.items(.data)[inst].extended;499 const extended = sema.code.instructions.items(.data)[inst].extended;
505 switch (extended.opcode) {500 switch (extended.opcode) {
506 // zig fmt: off501 // zig fmt: off
507 .func => return sema.zirFuncExtended( block, extended),502 .func => return sema.zirFuncExtended( block, extended),
508 .ret_ptr => return sema.zirRetPtr( block, extended),503 .ret_ptr => return sema.zirRetPtr( block, extended),
509 .ret_type => return sema.zirRetType( block, extended),504 .ret_type => return sema.zirRetType( block, extended),
510 .this => return sema.zirThis( block, extended),505 .this => return sema.zirThis( block, extended),
511 .ret_addr => return sema.zirRetAddr( block, extended),506 .ret_addr => return sema.zirRetAddr( block, extended),
512 .builtin_src => return sema.zirBuiltinSrc( block, extended),507 .builtin_src => return sema.zirBuiltinSrc( block, extended),
513 .error_return_trace => return sema.zirErrorReturnTrace(block, extended),508 .error_return_trace => return sema.zirErrorReturnTrace( block, extended),
514 .frame => return sema.zirFrame( block, extended),509 .frame => return sema.zirFrame( block, extended),
515 .frame_address => return sema.zirFrameAddress( block, extended),510 .frame_address => return sema.zirFrameAddress( block, extended),
516 .alloc => return sema.zirAllocExtended( block, extended),511 .alloc => return sema.zirAllocExtended( block, extended),
517 .builtin_extern => return sema.zirBuiltinExtern( block, extended),512 .builtin_extern => return sema.zirBuiltinExtern( block, extended),
518 .@"asm" => return sema.zirAsm( block, extended),513 .@"asm" => return sema.zirAsm( block, extended),
519 .typeof_peer => return sema.zirTypeofPeer( block, extended),514 .typeof_peer => return sema.zirTypeofPeer( block, extended),
520 .compile_log => return sema.zirCompileLog( block, extended),515 .compile_log => return sema.zirCompileLog( block, extended),
521 .c_undef => return sema.zirCUndef( block, extended),516 .add_with_overflow => return sema.zirOverflowArithmetic(block, extended),
522 .c_include => return sema.zirCInclude( block, extended),517 .sub_with_overflow => return sema.zirOverflowArithmetic(block, extended),
523 .c_define => return sema.zirCDefine( block, extended),518 .mul_with_overflow => return sema.zirOverflowArithmetic(block, extended),
524 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),519 .shl_with_overflow => return sema.zirOverflowArithmetic(block, extended),
525 .wasm_memory_grow => return sema.zirWasmMemoryGrow( 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),
526 // zig fmt: on525 // zig fmt: on
527 }526 }
528}527}
...@@ -4272,12 +4271,16 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr...@@ -4272,12 +4271,16 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr
4272 return sema.analyzeArithmetic(block, tag_override, lhs, rhs, src, lhs_src, rhs_src);4271 return sema.analyzeArithmetic(block, tag_override, lhs, rhs, src, lhs_src, rhs_src);
4273}4272}
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 {
4276 const tracy = trace(@src());4279 const tracy = trace(@src());
4277 defer tracy.end();4280 defer tracy.end();
42784281
4279 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;4282 const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
4280 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };4283 const src: LazySrcLoc = .{ .node_offset = extra.node };
42814284
4282 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirOverflowArithmetic", .{});4285 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirOverflowArithmetic", .{});
4283}4286}
src/Zir.zig+29-23
...@@ -693,14 +693,6 @@ pub const Inst = struct {...@@ -693,14 +693,6 @@ pub const Inst = struct {
693 bit_size_of,693 bit_size_of,
694 /// Implements the `@fence` builtin. Uses `node`.694 /// Implements the `@fence` builtin. Uses `node`.
695 fence,695 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
705 /// Implement builtin `@ptrToInt`. Uses `un_node`.697 /// Implement builtin `@ptrToInt`. Uses `un_node`.
706 /// Convert a pointer to a `usize` integer.698 /// Convert a pointer to a `usize` integer.
...@@ -1118,10 +1110,6 @@ pub const Inst = struct {...@@ -1118,10 +1110,6 @@ pub const Inst = struct {
1118 .type_info,1110 .type_info,
1119 .size_of,1111 .size_of,
1120 .bit_size_of,1112 .bit_size_of,
1121 .add_with_overflow,
1122 .sub_with_overflow,
1123 .mul_with_overflow,
1124 .shl_with_overflow,
1125 .ptr_to_int,1113 .ptr_to_int,
1126 .align_of,1114 .align_of,
1127 .bool_to_int,1115 .bool_to_int,
...@@ -1273,6 +1261,22 @@ pub const Inst = struct {...@@ -1273,6 +1261,22 @@ pub const Inst = struct {
1273 /// `small` is `operands_len`.1261 /// `small` is `operands_len`.
1274 /// The AST node is the builtin call.1262 /// The AST node is the builtin call.
1275 typeof_peer,1263 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,
1276 /// `operand` is payload index to `UnNode`.1280 /// `operand` is payload index to `UnNode`.
1277 c_undef,1281 c_undef,
1278 /// `operand` is payload index to `UnNode`.1282 /// `operand` is payload index to `UnNode`.
...@@ -2201,6 +2205,7 @@ pub const Inst = struct {...@@ -2201,6 +2205,7 @@ pub const Inst = struct {
2201 };2205 };
22022206
2203 pub const OverflowArithmetic = struct {2207 pub const OverflowArithmetic = struct {
2208 node: i32,
2204 lhs: Ref,2209 lhs: Ref,
2205 rhs: Ref,2210 rhs: Ref,
2206 ptr: Ref,2211 ptr: Ref,
...@@ -2485,12 +2490,6 @@ const Writer = struct {...@@ -2485,12 +2490,6 @@ const Writer = struct {
24852490
2486 .error_set_decl => try self.writePlNodeErrorSetDecl(stream, inst),2491 .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
2494 .add,2493 .add,
2495 .addwrap,2494 .addwrap,
2496 .array_cat,2495 .array_cat,
...@@ -2658,6 +2657,12 @@ const Writer = struct {...@@ -2658,6 +2657,12 @@ const Writer = struct {
2658 .typeof_peer,2657 .typeof_peer,
2659 => try self.writeNodeMultiOp(stream, extended),2658 => 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
2661 .alloc,2666 .alloc,
2662 .builtin_extern,2667 .builtin_extern,
2663 .c_undef,2668 .c_undef,
...@@ -2931,16 +2936,17 @@ const Writer = struct {...@@ -2931,16 +2936,17 @@ const Writer = struct {
2931 try self.writeSrc(stream, src);2936 try self.writeSrc(stream, src);
2932 }2937 }
29332938
2934 fn writePlNodeOverflowArithmetic(self: *Writer, stream: anytype, inst: Inst.Index) !void {2939 fn writeOverflowArithmetic(self: *Writer, stream: anytype, extended: Inst.Extended.InstData) !void {
2935 const inst_data = self.code.instructions.items(.data)[inst].pl_node;2940 const extra = self.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
2936 const extra = self.code.extraData(Inst.OverflowArithmetic, inst_data.payload_index).data;2941 const src: LazySrcLoc = .{ .node_offset = extra.node };
2942
2937 try self.writeInstRef(stream, extra.lhs);2943 try self.writeInstRef(stream, extra.lhs);
2938 try stream.writeAll(", ");2944 try stream.writeAll(", ");
2939 try self.writeInstRef(stream, extra.rhs);2945 try self.writeInstRef(stream, extra.rhs);
2940 try stream.writeAll(", ");2946 try stream.writeAll(", ");
2941 try self.writeInstRef(stream, extra.ptr);2947 try self.writeInstRef(stream, extra.ptr);
2942 try stream.writeAll(") ");2948 try stream.writeAll(")) ");
2943 try self.writeSrc(stream, inst_data.src());2949 try self.writeSrc(stream, src);
2944 }2950 }
29452951
2946 fn writePlNodeCall(self: *Writer, stream: anytype, inst: Inst.Index) !void {2952 fn writePlNodeCall(self: *Writer, stream: anytype, inst: Inst.Index) !void {