From 27c63bf433f27b06bed92111cbc96e8574de8d11 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 19 Feb 2022 19:21:49 +0200 Subject: stage2: implement errunion_payload_ptr_set --- src/codegen/c.zig | 7 +++++++ src/codegen/llvm.zig | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index f6e2d467a7..fe0184849d 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1753,6 +1753,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .unwrap_errunion_err_ptr => try airUnwrapErrUnionErr(f, inst), .wrap_errunion_payload => try airWrapErrUnionPay(f, inst), .wrap_errunion_err => try airWrapErrUnionErr(f, inst), + .errunion_payload_ptr_set => try airErrUnionPayloadPtrSet(f, inst), // zig fmt: on }; switch (result_value) { @@ -3164,6 +3165,7 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { try writer.writeAll("};\n"); return local; } + fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { if (f.liveness.isUnused(inst)) return CValue.none; @@ -3183,6 +3185,11 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { return local; } +fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { + _ = inst; + return f.fail("TODO: C backend: implement airErrUnionPayloadPtrSet", .{}); +} + fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { if (f.liveness.isUnused(inst)) return CValue.none; diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 8af74fce64..d8c19fba71 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -2238,6 +2238,7 @@ pub const FuncGen = struct { .unwrap_errunion_payload_ptr => try self.airErrUnionPayload(inst, true), .unwrap_errunion_err => try self.airErrUnionErr(inst, false), .unwrap_errunion_err_ptr => try self.airErrUnionErr(inst, true), + .errunion_payload_ptr_set => try self.airErrUnionPayloadPtrSet(inst), .wrap_optional => try self.airWrapOptional(inst), .wrap_errunion_payload => try self.airWrapErrUnionPayload(inst), @@ -3202,6 +3203,37 @@ pub const FuncGen = struct { return self.builder.buildExtractValue(operand, 0, ""); } + fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value { + const ty_op = self.air.instructions.items(.data)[inst].ty_op; + const operand = try self.resolveInst(ty_op.operand); + const error_set_ty = self.air.typeOf(ty_op.operand).childType(); + + const error_ty = error_set_ty.errorUnionSet(); + const payload_ty = error_set_ty.errorUnionPayload(); + const non_error_val = try self.dg.genTypedValue(.{ .ty = error_ty, .val = Value.zero }); + if (!payload_ty.hasRuntimeBits()) { + // We have a pointer to a i1. We need to set it to 1 and then return the same pointer. + _ = self.builder.buildStore(non_error_val, operand); + return operand; + } + const index_type = self.context.intType(32); + { + // First set the non-error value. + const indices: [2]*const llvm.Value = .{ + index_type.constNull(), // dereference the pointer + index_type.constNull(), // first field is the payload + }; + const non_null_ptr = self.builder.buildInBoundsGEP(operand, &indices, indices.len, ""); + _ = self.builder.buildStore(non_error_val, non_null_ptr); + } + // Then return the payload pointer. + const indices: [2]*const llvm.Value = .{ + index_type.constNull(), // dereference the pointer + index_type.constInt(1, .False), // second field is the payload + }; + return self.builder.buildInBoundsGEP(operand, &indices, indices.len, ""); + } + fn airWrapOptional(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value { if (self.liveness.isUnused(inst)) return null; -- cgit v1.2.3