aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/llvm.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-02-21 14:18:17 -0500
committerGitHub <noreply@github.com>2022-02-21 14:18:17 -0500
commit74303a3d9591f188fb4dda96bf689c00ebbd24ca (patch)
tree936ea6f3afe985b9cc3110eca1d4b1750a4fca2f /src/codegen/llvm.zig
parent628e9e6d040979bd0a2cba05e854014dee5a7d55 (diff)
parenta5ac06268972bd7279a1bb928a40d70cc7d515ed (diff)
downloadzig-74303a3d9591f188fb4dda96bf689c00ebbd24ca.tar.gz
zig-74303a3d9591f188fb4dda96bf689c00ebbd24ca.zip
Merge pull request #10925 from Vexu/stage2
stage2: support anon init through error unions and optionals
Diffstat (limited to 'src/codegen/llvm.zig')
-rw-r--r--src/codegen/llvm.zig42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index f40dbf41de..23fa19d9cb 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -2248,6 +2248,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),
@@ -3175,8 +3176,9 @@ pub const FuncGen = struct {
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
const operand = try self.resolveInst(ty_op.operand);
- const err_union_ty = self.air.typeOf(ty_op.operand);
- const payload_ty = err_union_ty.errorUnionPayload();
+ const result_ty = self.air.getRefType(ty_op.ty);
+ const payload_ty = if (operand_is_ptr) result_ty.childType() else result_ty;
+
if (!payload_ty.hasRuntimeBits()) return null;
if (operand_is_ptr or isByRef(payload_ty)) {
return self.builder.buildStructGEP(operand, 1, "");
@@ -3195,14 +3197,15 @@ pub const FuncGen = struct {
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
const operand = try self.resolveInst(ty_op.operand);
const operand_ty = self.air.typeOf(ty_op.operand);
+ const err_set_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
- const payload_ty = operand_ty.errorUnionPayload();
+ const payload_ty = err_set_ty.errorUnionPayload();
if (!payload_ty.hasRuntimeBits()) {
if (!operand_is_ptr) return operand;
return self.builder.buildLoad(operand, "");
}
- if (operand_is_ptr or isByRef(payload_ty)) {
+ if (operand_is_ptr or isByRef(err_set_ty)) {
const err_field_ptr = self.builder.buildStructGEP(operand, 0, "");
return self.builder.buildLoad(err_field_ptr, "");
}
@@ -3210,6 +3213,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;