Merge pull request #10925 from Vexu/stage2
stage2: support anon init through error unions and optionals
This commit is contained in:
@@ -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) {
|
||||
@@ -3090,17 +3091,18 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
|
||||
const operand = try f.resolveInst(ty_op.operand);
|
||||
const operand_ty = f.air.typeOf(ty_op.operand);
|
||||
|
||||
const payload_ty = operand_ty.errorUnionPayload();
|
||||
if (!payload_ty.hasRuntimeBits()) {
|
||||
if (operand_ty.zigTypeTag() == .Pointer) {
|
||||
const local = try f.allocLocal(inst_ty, .Const);
|
||||
try writer.writeAll(" = *");
|
||||
try f.writeCValue(writer, operand);
|
||||
try writer.writeAll(";\n");
|
||||
return local;
|
||||
} else {
|
||||
if (operand_ty.zigTypeTag() == .Pointer) {
|
||||
if (!operand_ty.childType().errorUnionPayload().hasRuntimeBits()) {
|
||||
return operand;
|
||||
}
|
||||
const local = try f.allocLocal(inst_ty, .Const);
|
||||
try writer.writeAll(" = *");
|
||||
try f.writeCValue(writer, operand);
|
||||
try writer.writeAll(";\n");
|
||||
return local;
|
||||
}
|
||||
if (!operand_ty.errorUnionPayload().hasRuntimeBits()) {
|
||||
return operand;
|
||||
}
|
||||
|
||||
const local = try f.allocLocal(inst_ty, .Const);
|
||||
@@ -3123,8 +3125,11 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: []cons
|
||||
const operand = try f.resolveInst(ty_op.operand);
|
||||
const operand_ty = f.air.typeOf(ty_op.operand);
|
||||
|
||||
const payload_ty = operand_ty.errorUnionPayload();
|
||||
if (!payload_ty.hasRuntimeBits()) {
|
||||
const error_union_ty = if (operand_ty.zigTypeTag() == .Pointer)
|
||||
operand_ty.childType()
|
||||
else
|
||||
operand_ty;
|
||||
if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) {
|
||||
return CValue.none;
|
||||
}
|
||||
|
||||
@@ -3160,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;
|
||||
|
||||
@@ -3179,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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user