diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-02-19 19:21:49 +0200 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-02-19 20:48:00 +0200 |
| commit | 27c63bf433f27b06bed92111cbc96e8574de8d11 (patch) | |
| tree | 33a3ffae8217196b22d5fbe9786dee7a34bfbe7b /src/codegen/llvm.zig | |
| parent | 89f6ff177178dba4e66680227a02de7b4068123c (diff) | |
| download | zig-27c63bf433f27b06bed92111cbc96e8574de8d11.tar.gz zig-27c63bf433f27b06bed92111cbc96e8574de8d11.zip | |
stage2: implement errunion_payload_ptr_set
Diffstat (limited to 'src/codegen/llvm.zig')
| -rw-r--r-- | src/codegen/llvm.zig | 32 |
1 files changed, 32 insertions, 0 deletions
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; |
