Commit e018e64a53

Andrew Kelley <andrew@ziglang.org>
2021-04-25 02:31:15
stage2: move overflow builtin ZIR instructions to Extended
make some more room in our ZIR enum tag space
1 parent 15e8918
Changed files (3)
src/AstGen.zig
@@ -1946,10 +1946,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
             .type_info,
             .size_of,
             .bit_size_of,
-            .add_with_overflow,
-            .sub_with_overflow,
-            .mul_with_overflow,
-            .shl_with_overflow,
             .log2_int_type,
             .typeof_log2_int_type,
             .ptr_to_int,
@@ -6374,7 +6370,8 @@ fn builtinCall(
             const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
             const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, params[2]);
             const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);
-            const result = try gz.addPlNode(.shl_with_overflow, node, Zir.Inst.OverflowArithmetic{
+            const result = try gz.addExtendedPayload(.shl_with_overflow, Zir.Inst.OverflowArithmetic{
+                .node = gz.nodeIndexToRelative(node),
                 .lhs = lhs,
                 .rhs = rhs,
                 .ptr = ptr,
@@ -6734,7 +6731,7 @@ fn overflowArithmetic(
     rl: ResultLoc,
     node: ast.Node.Index,
     params: []const ast.Node.Index,
-    tag: Zir.Inst.Tag,
+    tag: Zir.Inst.Extended,
 ) InnerError!Zir.Inst.Ref {
     const int_type = try typeExpr(gz, scope, params[0]);
     const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
@@ -6749,7 +6746,8 @@ fn overflowArithmetic(
     const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
     const rhs = try expr(gz, scope, .{ .ty = int_type }, params[2]);
     const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);
-    const result = try gz.addPlNode(tag, node, Zir.Inst.OverflowArithmetic{
+    const result = try gz.addExtendedPayload(tag, Zir.Inst.OverflowArithmetic{
+        .node = gz.nodeIndexToRelative(node),
         .lhs = lhs,
         .rhs = rhs,
         .ptr = ptr,
src/Sema.zig
@@ -356,11 +356,6 @@ pub fn analyzeBody(
             .sub     => try sema.zirArithmetic(block, inst),
             .subwrap => try sema.zirArithmetic(block, inst),
 
-            .add_with_overflow => try sema.zirOverflowArithmetic(block, inst),
-            .sub_with_overflow => try sema.zirOverflowArithmetic(block, inst),
-            .mul_with_overflow => try sema.zirOverflowArithmetic(block, inst),
-            .shl_with_overflow => try sema.zirOverflowArithmetic(block, inst),
-
             // Instructions that we know to *always* be noreturn based solely on their tag.
             // These functions match the return type of analyzeBody so that we can
             // tail call them here.
@@ -504,25 +499,29 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErro
     const extended = sema.code.instructions.items(.data)[inst].extended;
     switch (extended.opcode) {
         // zig fmt: off
-        .func               => return sema.zirFuncExtended(    block, extended),
-        .ret_ptr            => return sema.zirRetPtr(          block, extended),
-        .ret_type           => return sema.zirRetType(         block, extended),
-        .this               => return sema.zirThis(            block, extended),
-        .ret_addr           => return sema.zirRetAddr(         block, extended),
-        .builtin_src        => return sema.zirBuiltinSrc(      block, extended),
-        .error_return_trace => return sema.zirErrorReturnTrace(block, extended),
-        .frame              => return sema.zirFrame(           block, extended),
-        .frame_address      => return sema.zirFrameAddress(    block, extended),
-        .alloc              => return sema.zirAllocExtended(   block, extended),
-        .builtin_extern     => return sema.zirBuiltinExtern(   block, extended),
-        .@"asm"             => return sema.zirAsm(             block, extended),
-        .typeof_peer        => return sema.zirTypeofPeer(      block, extended),
-        .compile_log        => return sema.zirCompileLog(      block, extended),
-        .c_undef            => return sema.zirCUndef(          block, extended),
-        .c_include          => return sema.zirCInclude(        block, extended),
-        .c_define           => return sema.zirCDefine(         block, extended),
-        .wasm_memory_size   => return sema.zirWasmMemorySize(  block, extended),
-        .wasm_memory_grow   => return sema.zirWasmMemoryGrow(  block, extended),
+        .func               => return sema.zirFuncExtended(      block, extended),
+        .ret_ptr            => return sema.zirRetPtr(            block, extended),
+        .ret_type           => return sema.zirRetType(           block, extended),
+        .this               => return sema.zirThis(              block, extended),
+        .ret_addr           => return sema.zirRetAddr(           block, extended),
+        .builtin_src        => return sema.zirBuiltinSrc(        block, extended),
+        .error_return_trace => return sema.zirErrorReturnTrace(  block, extended),
+        .frame              => return sema.zirFrame(             block, extended),
+        .frame_address      => return sema.zirFrameAddress(      block, extended),
+        .alloc              => return sema.zirAllocExtended(     block, extended),
+        .builtin_extern     => return sema.zirBuiltinExtern(     block, extended),
+        .@"asm"             => return sema.zirAsm(               block, extended),
+        .typeof_peer        => return sema.zirTypeofPeer(        block, extended),
+        .compile_log        => return sema.zirCompileLog(        block, extended),
+        .add_with_overflow  => return sema.zirOverflowArithmetic(block, extended),
+        .sub_with_overflow  => return sema.zirOverflowArithmetic(block, extended),
+        .mul_with_overflow  => return sema.zirOverflowArithmetic(block, extended),
+        .shl_with_overflow  => return sema.zirOverflowArithmetic(block, extended),
+        .c_undef            => return sema.zirCUndef(            block, extended),
+        .c_include          => return sema.zirCInclude(          block, extended),
+        .c_define           => return sema.zirCDefine(           block, extended),
+        .wasm_memory_size   => return sema.zirWasmMemorySize(    block, extended),
+        .wasm_memory_grow   => return sema.zirWasmMemoryGrow(    block, extended),
         // zig fmt: on
     }
 }
@@ -4272,12 +4271,16 @@ fn zirArithmetic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr
     return sema.analyzeArithmetic(block, tag_override, lhs, rhs, src, lhs_src, rhs_src);
 }
 
-fn zirOverflowArithmetic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
+fn zirOverflowArithmetic(
+    sema: *Sema,
+    block: *Scope.Block,
+    extended: Zir.Inst.Extended.InstData,
+) InnerError!*Inst {
     const tracy = trace(@src());
     defer tracy.end();
 
-    const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
-    const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
+    const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
+    const src: LazySrcLoc = .{ .node_offset = extra.node };
 
     return sema.mod.fail(&block.base, src, "TODO implement Sema.zirOverflowArithmetic", .{});
 }
src/Zir.zig
@@ -693,14 +693,6 @@ pub const Inst = struct {
         bit_size_of,
         /// Implements the `@fence` builtin. Uses `node`.
         fence,
-        /// Implements the `@addWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
-        add_with_overflow,
-        /// Implements the `@subWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
-        sub_with_overflow,
-        /// Implements the `@mulWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
-        mul_with_overflow,
-        /// Implements the `@shlWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
-        shl_with_overflow,
 
         /// Implement builtin `@ptrToInt`. Uses `un_node`.
         /// Convert a pointer to a `usize` integer.
@@ -1118,10 +1110,6 @@ pub const Inst = struct {
                 .type_info,
                 .size_of,
                 .bit_size_of,
-                .add_with_overflow,
-                .sub_with_overflow,
-                .mul_with_overflow,
-                .shl_with_overflow,
                 .ptr_to_int,
                 .align_of,
                 .bool_to_int,
@@ -1273,6 +1261,22 @@ pub const Inst = struct {
         /// `small` is `operands_len`.
         /// The AST node is the builtin call.
         typeof_peer,
+        /// Implements the `@addWithOverflow` builtin.
+        /// `operand` is payload index to `OverflowArithmetic`.
+        /// `small` is unused.
+        add_with_overflow,
+        /// Implements the `@subWithOverflow` builtin.
+        /// `operand` is payload index to `OverflowArithmetic`.
+        /// `small` is unused.
+        sub_with_overflow,
+        /// Implements the `@mulWithOverflow` builtin.
+        /// `operand` is payload index to `OverflowArithmetic`.
+        /// `small` is unused.
+        mul_with_overflow,
+        /// Implements the `@shlWithOverflow` builtin.
+        /// `operand` is payload index to `OverflowArithmetic`.
+        /// `small` is unused.
+        shl_with_overflow,
         /// `operand` is payload index to `UnNode`.
         c_undef,
         /// `operand` is payload index to `UnNode`.
@@ -2201,6 +2205,7 @@ pub const Inst = struct {
     };
 
     pub const OverflowArithmetic = struct {
+        node: i32,
         lhs: Ref,
         rhs: Ref,
         ptr: Ref,
@@ -2485,12 +2490,6 @@ const Writer = struct {
 
             .error_set_decl => try self.writePlNodeErrorSetDecl(stream, inst),
 
-            .add_with_overflow,
-            .sub_with_overflow,
-            .mul_with_overflow,
-            .shl_with_overflow,
-            => try self.writePlNodeOverflowArithmetic(stream, inst),
-
             .add,
             .addwrap,
             .array_cat,
@@ -2658,6 +2657,12 @@ const Writer = struct {
             .typeof_peer,
             => try self.writeNodeMultiOp(stream, extended),
 
+            .add_with_overflow,
+            .sub_with_overflow,
+            .mul_with_overflow,
+            .shl_with_overflow,
+            => try self.writeOverflowArithmetic(stream, extended),
+
             .alloc,
             .builtin_extern,
             .c_undef,
@@ -2931,16 +2936,17 @@ const Writer = struct {
         try self.writeSrc(stream, src);
     }
 
-    fn writePlNodeOverflowArithmetic(self: *Writer, stream: anytype, inst: Inst.Index) !void {
-        const inst_data = self.code.instructions.items(.data)[inst].pl_node;
-        const extra = self.code.extraData(Inst.OverflowArithmetic, inst_data.payload_index).data;
+    fn writeOverflowArithmetic(self: *Writer, stream: anytype, extended: Inst.Extended.InstData) !void {
+        const extra = self.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
+        const src: LazySrcLoc = .{ .node_offset = extra.node };
+
         try self.writeInstRef(stream, extra.lhs);
         try stream.writeAll(", ");
         try self.writeInstRef(stream, extra.rhs);
         try stream.writeAll(", ");
         try self.writeInstRef(stream, extra.ptr);
-        try stream.writeAll(") ");
-        try self.writeSrc(stream, inst_data.src());
+        try stream.writeAll(")) ");
+        try self.writeSrc(stream, src);
     }
 
     fn writePlNodeCall(self: *Writer, stream: anytype, inst: Inst.Index) !void {