authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-07 10:43:57+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-07 10:45:45+03:00
log252388eb28fde40f085e14cd341efdc701093fe1
treec14f6b026b1b193b9e1034e3c705e3b9c94ab7cd
parentc9006d9479c619d9ed555164831e11a04d88d382

AstGen: move error_to_int, int_to_error and select to extended


4 files changed, 58 insertions(+), 53 deletions(-)

src/AstGen.zig+18-6
......@@ -2332,8 +2332,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
23322332 .error_union_type,
23332333 .bit_not,
23342334 .error_value,
2335 .error_to_int,
2336 .int_to_error,
23372335 .slice_start,
23382336 .slice_end,
23392337 .slice_sentinel,
......@@ -2420,7 +2418,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
24202418 .splat,
24212419 .reduce,
24222420 .shuffle,
2423 .select,
24242421 .atomic_load,
24252422 .atomic_rmw,
24262423 .mul_add,
......@@ -7355,8 +7352,6 @@ fn builtinCall(
73557352 .align_of => return simpleUnOpType(gz, scope, rl, node, params[0], .align_of),
73567353
73577354 .ptr_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .ptr_to_int),
7358 .error_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .error_to_int),
7359 .int_to_error => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u16_type }, params[0], .int_to_error),
73607355 .compile_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .compile_error),
73617356 .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type }, params[0], .set_eval_branch_quota),
73627357 .enum_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .enum_to_int),
......@@ -7396,6 +7391,22 @@ fn builtinCall(
73967391 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
73977392 // zig fmt: on
73987393
7394 .error_to_int => {
7395 const operand = try expr(gz, scope, .none, params[0]);
7396 const result = try gz.addExtendedPayload(.error_to_int, Zir.Inst.UnNode{
7397 .node = gz.nodeIndexToRelative(node),
7398 .operand = operand,
7399 });
7400 return rvalue(gz, rl, result, node);
7401 },
7402 .int_to_error => {
7403 const operand = try expr(gz, scope, .{ .coerced_ty = .u16_type }, params[0]);
7404 const result = try gz.addExtendedPayload(.int_to_error, Zir.Inst.UnNode{
7405 .node = gz.nodeIndexToRelative(node),
7406 .operand = operand,
7407 });
7408 return rvalue(gz, rl, result, node);
7409 },
73997410 .align_cast => {
74007411 const dest_align = try comptimeExpr(gz, scope, align_rl, params[0]);
74017412 const rhs = try expr(gz, scope, .none, params[1]);
......@@ -7635,7 +7646,8 @@ fn builtinCall(
76357646 return rvalue(gz, rl, result, node);
76367647 },
76377648 .select => {
7638 const result = try gz.addPlNode(.select, node, Zir.Inst.Select{
7649 const result = try gz.addExtendedPayload(.select, Zir.Inst.Select{
7650 .node = gz.nodeIndexToRelative(node),
76397651 .elem_type = try typeExpr(gz, scope, params[0]),
76407652 .pred = try expr(gz, scope, .none, params[1]),
76417653 .a = try expr(gz, scope, .none, params[2]),
src/Sema.zig+23-24
......@@ -739,8 +739,6 @@ fn analyzeBodyInner(
739739 .err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false),
740740 .error_union_type => try sema.zirErrorUnionType(block, inst),
741741 .error_value => try sema.zirErrorValue(block, inst),
742 .error_to_int => try sema.zirErrorToInt(block, inst),
743 .int_to_error => try sema.zirIntToError(block, inst),
744742 .field_ptr => try sema.zirFieldPtr(block, inst),
745743 .field_ptr_named => try sema.zirFieldPtrNamed(block, inst),
746744 .field_val => try sema.zirFieldVal(block, inst),
......@@ -835,7 +833,6 @@ fn analyzeBodyInner(
835833 .splat => try sema.zirSplat(block, inst),
836834 .reduce => try sema.zirReduce(block, inst),
837835 .shuffle => try sema.zirShuffle(block, inst),
838 .select => try sema.zirSelect(block, inst),
839836 .atomic_load => try sema.zirAtomicLoad(block, inst),
840837 .atomic_rmw => try sema.zirAtomicRmw(block, inst),
841838 .mul_add => try sema.zirMulAdd(block, inst),
......@@ -942,6 +939,9 @@ fn analyzeBodyInner(
942939 .field_call_bind_named => try sema.zirFieldCallBindNamed(block, extended),
943940 .err_set_cast => try sema.zirErrSetCast( block, extended),
944941 .await_nosuspend => try sema.zirAwaitNosuspend( block, extended),
942 .select => try sema.zirSelect( block, extended),
943 .error_to_int => try sema.zirErrorToInt( block, extended),
944 .int_to_error => try sema.zirIntToError( block, extended),
945945 // zig fmt: on
946946 .fence => {
947947 try sema.zirFence(block, extended);
......@@ -6240,18 +6240,18 @@ fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
62406240 );
62416241}
62426242
6243fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6243fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
62446244 const tracy = trace(@src());
62456245 defer tracy.end();
62466246
6247 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6248 const src = inst_data.src();
6249 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
6250 const op = try sema.resolveInst(inst_data.operand);
6251 const op_coerced = try sema.coerce(block, Type.anyerror, op, operand_src);
6247 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
6248 const src = LazySrcLoc.nodeOffset(extra.node);
6249 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
6250 const uncasted_operand = try sema.resolveInst(extra.operand);
6251 const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
62526252 const result_ty = Type.u16;
62536253
6254 if (try sema.resolveMaybeUndefVal(block, src, op_coerced)) |val| {
6254 if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
62556255 if (val.isUndef()) {
62566256 return sema.addConstUndef(result_ty);
62576257 }
......@@ -6273,7 +6273,7 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
62736273 }
62746274 }
62756275
6276 const op_ty = sema.typeOf(op);
6276 const op_ty = sema.typeOf(uncasted_operand);
62776277 try sema.resolveInferredErrorSetTy(block, src, op_ty);
62786278 if (!op_ty.isAnyError()) {
62796279 const names = op_ty.errorSetNames();
......@@ -6285,17 +6285,17 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
62856285 }
62866286
62876287 try sema.requireRuntimeBlock(block, src);
6288 return block.addBitCast(result_ty, op_coerced);
6288 return block.addBitCast(result_ty, operand);
62896289}
62906290
6291fn zirIntToError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6291fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
62926292 const tracy = trace(@src());
62936293 defer tracy.end();
62946294
6295 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6296 const src = inst_data.src();
6297 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
6298 const uncasted_operand = try sema.resolveInst(inst_data.operand);
6295 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
6296 const src = LazySrcLoc.nodeOffset(extra.node);
6297 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
6298 const uncasted_operand = try sema.resolveInst(extra.operand);
62996299 const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
63006300 const target = sema.mod.getTarget();
63016301
......@@ -16703,14 +16703,13 @@ fn analyzeShuffle(
1670316703 });
1670416704}
1670516705
16706fn zirSelect(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16707 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
16708 const extra = sema.code.extraData(Zir.Inst.Select, inst_data.payload_index).data;
16706fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
16707 const extra = sema.code.extraData(Zir.Inst.Select, extended.operand).data;
1670916708
16710 const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
16711 const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
16712 const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
16713 const b_src: LazySrcLoc = .{ .node_offset_builtin_call_arg3 = inst_data.src_node };
16709 const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
16710 const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
16711 const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = extra.node };
16712 const b_src: LazySrcLoc = .{ .node_offset_builtin_call_arg3 = extra.node };
1671416713
1671516714 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
1671616715 try sema.checkVectorElemType(block, elem_ty_src, elem_ty);
src/Zir.zig+10-16
......@@ -778,10 +778,6 @@ pub const Inst = struct {
778778 /// Implement builtin `@ptrToInt`. Uses `un_node`.
779779 /// Convert a pointer to a `usize` integer.
780780 ptr_to_int,
781 /// Implement builtin `@errToInt`. Uses `un_node`.
782 error_to_int,
783 /// Implement builtin `@intToError`. Uses `un_node`.
784 int_to_error,
785781 /// Emit an error message and fail compilation.
786782 /// Uses the `un_node` field.
787783 compile_error,
......@@ -916,9 +912,6 @@ pub const Inst = struct {
916912 /// Implements the `@shuffle` builtin.
917913 /// Uses the `pl_node` union field with payload `Shuffle`.
918914 shuffle,
919 /// Implements the `@select` builtin.
920 /// Uses the `pl_node` union field with payload `Select`.
921 select,
922915 /// Implements the `@atomicLoad` builtin.
923916 /// Uses the `pl_node` union field with payload `AtomicLoad`.
924917 atomic_load,
......@@ -1125,8 +1118,6 @@ pub const Inst = struct {
11251118 .err_union_payload_unsafe_ptr,
11261119 .err_union_code,
11271120 .err_union_code_ptr,
1128 .error_to_int,
1129 .int_to_error,
11301121 .ptr_type,
11311122 .ptr_type_simple,
11321123 .ensure_err_payload_void,
......@@ -1230,7 +1221,6 @@ pub const Inst = struct {
12301221 .splat,
12311222 .reduce,
12321223 .shuffle,
1233 .select,
12341224 .atomic_load,
12351225 .atomic_rmw,
12361226 .atomic_store,
......@@ -1423,8 +1413,6 @@ pub const Inst = struct {
14231413 .err_union_payload_unsafe_ptr,
14241414 .err_union_code,
14251415 .err_union_code_ptr,
1426 .error_to_int,
1427 .int_to_error,
14281416 .ptr_type,
14291417 .ptr_type_simple,
14301418 .enum_literal,
......@@ -1516,7 +1504,6 @@ pub const Inst = struct {
15161504 .splat,
15171505 .reduce,
15181506 .shuffle,
1519 .select,
15201507 .atomic_load,
15211508 .atomic_rmw,
15221509 .mul_add,
......@@ -1731,8 +1718,6 @@ pub const Inst = struct {
17311718 .bit_size_of = .un_node,
17321719
17331720 .ptr_to_int = .un_node,
1734 .error_to_int = .un_node,
1735 .int_to_error = .un_node,
17361721 .compile_error = .un_node,
17371722 .set_eval_branch_quota = .un_node,
17381723 .enum_to_int = .un_node,
......@@ -1802,7 +1787,6 @@ pub const Inst = struct {
18021787 .splat = .pl_node,
18031788 .reduce = .pl_node,
18041789 .shuffle = .pl_node,
1805 .select = .pl_node,
18061790 .atomic_load = .pl_node,
18071791 .atomic_rmw = .pl_node,
18081792 .atomic_store = .pl_node,
......@@ -1971,6 +1955,15 @@ pub const Inst = struct {
19711955 await_nosuspend,
19721956 /// `operand` is `src_node: i32`.
19731957 breakpoint,
1958 /// Implements the `@select` builtin.
1959 /// operand` is payload index to `Select`.
1960 select,
1961 /// Implement builtin `@errToInt`.
1962 /// `operand` is payload index to `UnNode`.
1963 error_to_int,
1964 /// Implement builtin `@intToError`.
1965 /// `operand` is payload index to `UnNode`.
1966 int_to_error,
19741967
19751968 pub const InstData = struct {
19761969 opcode: Extended,
......@@ -3448,6 +3441,7 @@ pub const Inst = struct {
34483441 };
34493442
34503443 pub const Select = struct {
3444 node: i32,
34513445 elem_type: Ref,
34523446 pred: Ref,
34533447 a: Ref,
src/print_zir.zig+7-7
......@@ -189,8 +189,6 @@ const Writer = struct {
189189 .typeof_log2_int_type,
190190 .log2_int_type,
191191 .ptr_to_int,
192 .error_to_int,
193 .int_to_error,
194192 .compile_error,
195193 .set_eval_branch_quota,
196194 .enum_to_int,
......@@ -284,7 +282,6 @@ const Writer = struct {
284282 .memcpy => try self.writeMemcpy(stream, inst),
285283 .memset => try self.writeMemset(stream, inst),
286284 .shuffle => try self.writeShuffle(stream, inst),
287 .select => try self.writeSelect(stream, inst),
288285 .mul_add => try self.writeMulAdd(stream, inst),
289286 .field_parent_ptr => try self.writeFieldParentPtr(stream, inst),
290287 .builtin_call => try self.writeBuiltinCall(stream, inst),
......@@ -478,6 +475,8 @@ const Writer = struct {
478475 .compile_log => try self.writeNodeMultiOp(stream, extended),
479476 .typeof_peer => try self.writeTypeofPeer(stream, extended),
480477
478 .select => try self.writeSelect(stream, extended),
479
481480 .add_with_overflow,
482481 .sub_with_overflow,
483482 .mul_with_overflow,
......@@ -496,6 +495,8 @@ const Writer = struct {
496495 .set_float_mode,
497496 .set_align_stack,
498497 .wasm_memory_size,
498 .error_to_int,
499 .int_to_error,
499500 => {
500501 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
501502 const src = LazySrcLoc.nodeOffset(inst_data.node);
......@@ -772,9 +773,8 @@ const Writer = struct {
772773 try self.writeSrc(stream, inst_data.src());
773774 }
774775
775 fn writeSelect(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
776 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
777 const extra = self.code.extraData(Zir.Inst.Select, inst_data.payload_index).data;
776 fn writeSelect(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
777 const extra = self.code.extraData(Zir.Inst.Select, extended.operand).data;
778778 try self.writeInstRef(stream, extra.elem_type);
779779 try stream.writeAll(", ");
780780 try self.writeInstRef(stream, extra.pred);
......@@ -783,7 +783,7 @@ const Writer = struct {
783783 try stream.writeAll(", ");
784784 try self.writeInstRef(stream, extra.b);
785785 try stream.writeAll(") ");
786 try self.writeSrc(stream, inst_data.src());
786 try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
787787 }
788788
789789 fn writeMulAdd(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {