Commit 252388eb28

Veikka Tuominen <git@vexu.eu>
2022-07-07 09:43:57
AstGen: move error_to_int, int_to_error and select to extended
1 parent c9006d9
src/AstGen.zig
@@ -2332,8 +2332,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
             .error_union_type,
             .bit_not,
             .error_value,
-            .error_to_int,
-            .int_to_error,
             .slice_start,
             .slice_end,
             .slice_sentinel,
@@ -2420,7 +2418,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
             .splat,
             .reduce,
             .shuffle,
-            .select,
             .atomic_load,
             .atomic_rmw,
             .mul_add,
@@ -7355,8 +7352,6 @@ fn builtinCall(
         .align_of    => return simpleUnOpType(gz, scope, rl, node, params[0], .align_of),
 
         .ptr_to_int            => return simpleUnOp(gz, scope, rl, node, .none,                               params[0], .ptr_to_int),
-        .error_to_int          => return simpleUnOp(gz, scope, rl, node, .none,                               params[0], .error_to_int),
-        .int_to_error          => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u16_type },        params[0], .int_to_error),
         .compile_error         => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type },     params[0], .compile_error),
         .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type },        params[0], .set_eval_branch_quota),
         .enum_to_int           => return simpleUnOp(gz, scope, rl, node, .none,                               params[0], .enum_to_int),
@@ -7396,6 +7391,22 @@ fn builtinCall(
         .truncate     => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
         // zig fmt: on
 
+        .error_to_int => {
+            const operand = try expr(gz, scope, .none, params[0]);
+            const result = try gz.addExtendedPayload(.error_to_int, Zir.Inst.UnNode{
+                .node = gz.nodeIndexToRelative(node),
+                .operand = operand,
+            });
+            return rvalue(gz, rl, result, node);
+        },
+        .int_to_error => {
+            const operand = try expr(gz, scope, .{ .coerced_ty = .u16_type }, params[0]);
+            const result = try gz.addExtendedPayload(.int_to_error, Zir.Inst.UnNode{
+                .node = gz.nodeIndexToRelative(node),
+                .operand = operand,
+            });
+            return rvalue(gz, rl, result, node);
+        },
         .align_cast => {
             const dest_align = try comptimeExpr(gz, scope, align_rl, params[0]);
             const rhs = try expr(gz, scope, .none, params[1]);
@@ -7635,7 +7646,8 @@ fn builtinCall(
             return rvalue(gz, rl, result, node);
         },
         .select => {
-            const result = try gz.addPlNode(.select, node, Zir.Inst.Select{
+            const result = try gz.addExtendedPayload(.select, Zir.Inst.Select{
+                .node = gz.nodeIndexToRelative(node),
                 .elem_type = try typeExpr(gz, scope, params[0]),
                 .pred = try expr(gz, scope, .none, params[1]),
                 .a = try expr(gz, scope, .none, params[2]),
src/print_zir.zig
@@ -189,8 +189,6 @@ const Writer = struct {
             .typeof_log2_int_type,
             .log2_int_type,
             .ptr_to_int,
-            .error_to_int,
-            .int_to_error,
             .compile_error,
             .set_eval_branch_quota,
             .enum_to_int,
@@ -284,7 +282,6 @@ const Writer = struct {
             .memcpy => try self.writeMemcpy(stream, inst),
             .memset => try self.writeMemset(stream, inst),
             .shuffle => try self.writeShuffle(stream, inst),
-            .select => try self.writeSelect(stream, inst),
             .mul_add => try self.writeMulAdd(stream, inst),
             .field_parent_ptr => try self.writeFieldParentPtr(stream, inst),
             .builtin_call => try self.writeBuiltinCall(stream, inst),
@@ -478,6 +475,8 @@ const Writer = struct {
             .compile_log => try self.writeNodeMultiOp(stream, extended),
             .typeof_peer => try self.writeTypeofPeer(stream, extended),
 
+            .select => try self.writeSelect(stream, extended),
+
             .add_with_overflow,
             .sub_with_overflow,
             .mul_with_overflow,
@@ -496,6 +495,8 @@ const Writer = struct {
             .set_float_mode,
             .set_align_stack,
             .wasm_memory_size,
+            .error_to_int,
+            .int_to_error,
             => {
                 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
                 const src = LazySrcLoc.nodeOffset(inst_data.node);
@@ -772,9 +773,8 @@ const Writer = struct {
         try self.writeSrc(stream, inst_data.src());
     }
 
-    fn writeSelect(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
-        const inst_data = self.code.instructions.items(.data)[inst].pl_node;
-        const extra = self.code.extraData(Zir.Inst.Select, inst_data.payload_index).data;
+    fn writeSelect(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
+        const extra = self.code.extraData(Zir.Inst.Select, extended.operand).data;
         try self.writeInstRef(stream, extra.elem_type);
         try stream.writeAll(", ");
         try self.writeInstRef(stream, extra.pred);
@@ -783,7 +783,7 @@ const Writer = struct {
         try stream.writeAll(", ");
         try self.writeInstRef(stream, extra.b);
         try stream.writeAll(") ");
-        try self.writeSrc(stream, inst_data.src());
+        try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
     }
 
     fn writeMulAdd(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
src/Sema.zig
@@ -739,8 +739,6 @@ fn analyzeBodyInner(
             .err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false),
             .error_union_type             => try sema.zirErrorUnionType(block, inst),
             .error_value                  => try sema.zirErrorValue(block, inst),
-            .error_to_int                 => try sema.zirErrorToInt(block, inst),
-            .int_to_error                 => try sema.zirIntToError(block, inst),
             .field_ptr                    => try sema.zirFieldPtr(block, inst),
             .field_ptr_named              => try sema.zirFieldPtrNamed(block, inst),
             .field_val                    => try sema.zirFieldVal(block, inst),
@@ -835,7 +833,6 @@ fn analyzeBodyInner(
             .splat                        => try sema.zirSplat(block, inst),
             .reduce                       => try sema.zirReduce(block, inst),
             .shuffle                      => try sema.zirShuffle(block, inst),
-            .select                       => try sema.zirSelect(block, inst),
             .atomic_load                  => try sema.zirAtomicLoad(block, inst),
             .atomic_rmw                   => try sema.zirAtomicRmw(block, inst),
             .mul_add                      => try sema.zirMulAdd(block, inst),
@@ -942,6 +939,9 @@ fn analyzeBodyInner(
                     .field_call_bind_named => try sema.zirFieldCallBindNamed(block, extended),
                     .err_set_cast          => try sema.zirErrSetCast(        block, extended),
                     .await_nosuspend       => try sema.zirAwaitNosuspend(    block, extended),
+                    .select                => try sema.zirSelect(            block, extended),
+                    .error_to_int          => try sema.zirErrorToInt(        block, extended),
+                    .int_to_error          => try sema.zirIntToError(        block, extended),
                     // zig fmt: on
                     .fence => {
                         try sema.zirFence(block, extended);
@@ -6240,18 +6240,18 @@ fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
     );
 }
 
-fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
+fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
     const tracy = trace(@src());
     defer tracy.end();
 
-    const inst_data = sema.code.instructions.items(.data)[inst].un_node;
-    const src = inst_data.src();
-    const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
-    const op = try sema.resolveInst(inst_data.operand);
-    const op_coerced = try sema.coerce(block, Type.anyerror, op, operand_src);
+    const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
+    const src = LazySrcLoc.nodeOffset(extra.node);
+    const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
+    const uncasted_operand = try sema.resolveInst(extra.operand);
+    const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
     const result_ty = Type.u16;
 
-    if (try sema.resolveMaybeUndefVal(block, src, op_coerced)) |val| {
+    if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
         if (val.isUndef()) {
             return sema.addConstUndef(result_ty);
         }
@@ -6273,7 +6273,7 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
         }
     }
 
-    const op_ty = sema.typeOf(op);
+    const op_ty = sema.typeOf(uncasted_operand);
     try sema.resolveInferredErrorSetTy(block, src, op_ty);
     if (!op_ty.isAnyError()) {
         const names = op_ty.errorSetNames();
@@ -6285,17 +6285,17 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
     }
 
     try sema.requireRuntimeBlock(block, src);
-    return block.addBitCast(result_ty, op_coerced);
+    return block.addBitCast(result_ty, operand);
 }
 
-fn zirIntToError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
+fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
     const tracy = trace(@src());
     defer tracy.end();
 
-    const inst_data = sema.code.instructions.items(.data)[inst].un_node;
-    const src = inst_data.src();
-    const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
-    const uncasted_operand = try sema.resolveInst(inst_data.operand);
+    const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
+    const src = LazySrcLoc.nodeOffset(extra.node);
+    const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
+    const uncasted_operand = try sema.resolveInst(extra.operand);
     const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
     const target = sema.mod.getTarget();
 
@@ -16703,14 +16703,13 @@ fn analyzeShuffle(
     });
 }
 
-fn zirSelect(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
-    const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
-    const extra = sema.code.extraData(Zir.Inst.Select, inst_data.payload_index).data;
+fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
+    const extra = sema.code.extraData(Zir.Inst.Select, extended.operand).data;
 
-    const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
-    const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
-    const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
-    const b_src: LazySrcLoc = .{ .node_offset_builtin_call_arg3 = inst_data.src_node };
+    const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
+    const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
+    const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = extra.node };
+    const b_src: LazySrcLoc = .{ .node_offset_builtin_call_arg3 = extra.node };
 
     const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
     try sema.checkVectorElemType(block, elem_ty_src, elem_ty);
src/Zir.zig
@@ -778,10 +778,6 @@ pub const Inst = struct {
         /// Implement builtin `@ptrToInt`. Uses `un_node`.
         /// Convert a pointer to a `usize` integer.
         ptr_to_int,
-        /// Implement builtin `@errToInt`. Uses `un_node`.
-        error_to_int,
-        /// Implement builtin `@intToError`. Uses `un_node`.
-        int_to_error,
         /// Emit an error message and fail compilation.
         /// Uses the `un_node` field.
         compile_error,
@@ -916,9 +912,6 @@ pub const Inst = struct {
         /// Implements the `@shuffle` builtin.
         /// Uses the `pl_node` union field with payload `Shuffle`.
         shuffle,
-        /// Implements the `@select` builtin.
-        /// Uses the `pl_node` union field with payload `Select`.
-        select,
         /// Implements the `@atomicLoad` builtin.
         /// Uses the `pl_node` union field with payload `AtomicLoad`.
         atomic_load,
@@ -1125,8 +1118,6 @@ pub const Inst = struct {
                 .err_union_payload_unsafe_ptr,
                 .err_union_code,
                 .err_union_code_ptr,
-                .error_to_int,
-                .int_to_error,
                 .ptr_type,
                 .ptr_type_simple,
                 .ensure_err_payload_void,
@@ -1230,7 +1221,6 @@ pub const Inst = struct {
                 .splat,
                 .reduce,
                 .shuffle,
-                .select,
                 .atomic_load,
                 .atomic_rmw,
                 .atomic_store,
@@ -1423,8 +1413,6 @@ pub const Inst = struct {
                 .err_union_payload_unsafe_ptr,
                 .err_union_code,
                 .err_union_code_ptr,
-                .error_to_int,
-                .int_to_error,
                 .ptr_type,
                 .ptr_type_simple,
                 .enum_literal,
@@ -1516,7 +1504,6 @@ pub const Inst = struct {
                 .splat,
                 .reduce,
                 .shuffle,
-                .select,
                 .atomic_load,
                 .atomic_rmw,
                 .mul_add,
@@ -1731,8 +1718,6 @@ pub const Inst = struct {
                 .bit_size_of = .un_node,
 
                 .ptr_to_int = .un_node,
-                .error_to_int = .un_node,
-                .int_to_error = .un_node,
                 .compile_error = .un_node,
                 .set_eval_branch_quota = .un_node,
                 .enum_to_int = .un_node,
@@ -1802,7 +1787,6 @@ pub const Inst = struct {
                 .splat = .pl_node,
                 .reduce = .pl_node,
                 .shuffle = .pl_node,
-                .select = .pl_node,
                 .atomic_load = .pl_node,
                 .atomic_rmw = .pl_node,
                 .atomic_store = .pl_node,
@@ -1971,6 +1955,15 @@ pub const Inst = struct {
         await_nosuspend,
         /// `operand` is `src_node: i32`.
         breakpoint,
+        /// Implements the `@select` builtin.
+        /// operand` is payload index to `Select`.
+        select,
+        /// Implement builtin `@errToInt`.
+        /// `operand` is payload index to `UnNode`.
+        error_to_int,
+        /// Implement builtin `@intToError`.
+        /// `operand` is payload index to `UnNode`.
+        int_to_error,
 
         pub const InstData = struct {
             opcode: Extended,
@@ -3448,6 +3441,7 @@ pub const Inst = struct {
     };
 
     pub const Select = struct {
+        node: i32,
         elem_type: Ref,
         pred: Ref,
         a: Ref,