authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-19 15:24:22+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-19 20:21:48+02:00
log89f6ff177178dba4e66680227a02de7b4068123c
tree8363dfe4f78e21c681b3c59b504813bbf3734dd8
parente02749224357a33cffcaf9970d14c199adc3d892

stage2: correct use of .unwrap_err_union_* in LLVM and C backend


2 files changed, 21 insertions(+), 15 deletions(-)

src/codegen/c.zig+15-11
...@@ -3090,17 +3090,18 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3090,17 +3090,18 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
3090 const operand = try f.resolveInst(ty_op.operand);3090 const operand = try f.resolveInst(ty_op.operand);
3091 const operand_ty = f.air.typeOf(ty_op.operand);3091 const operand_ty = f.air.typeOf(ty_op.operand);
30923092
3093 const payload_ty = operand_ty.errorUnionPayload();3093 if (operand_ty.zigTypeTag() == .Pointer) {
3094 if (!payload_ty.hasRuntimeBits()) {3094 if (!operand_ty.childType().errorUnionPayload().hasRuntimeBits()) {
3095 if (operand_ty.zigTypeTag() == .Pointer) {
3096 const local = try f.allocLocal(inst_ty, .Const);
3097 try writer.writeAll(" = *");
3098 try f.writeCValue(writer, operand);
3099 try writer.writeAll(";\n");
3100 return local;
3101 } else {
3102 return operand;3095 return operand;
3103 }3096 }
3097 const local = try f.allocLocal(inst_ty, .Const);
3098 try writer.writeAll(" = *");
3099 try f.writeCValue(writer, operand);
3100 try writer.writeAll(";\n");
3101 return local;
3102 }
3103 if (!operand_ty.errorUnionPayload().hasRuntimeBits()) {
3104 return operand;
3104 }3105 }
31053106
3106 const local = try f.allocLocal(inst_ty, .Const);3107 const local = try f.allocLocal(inst_ty, .Const);
...@@ -3123,8 +3124,11 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: []cons...@@ -3123,8 +3124,11 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: []cons
3123 const operand = try f.resolveInst(ty_op.operand);3124 const operand = try f.resolveInst(ty_op.operand);
3124 const operand_ty = f.air.typeOf(ty_op.operand);3125 const operand_ty = f.air.typeOf(ty_op.operand);
31253126
3126 const payload_ty = operand_ty.errorUnionPayload();3127 const error_union_ty = if (operand_ty.zigTypeTag() == .Pointer)
3127 if (!payload_ty.hasRuntimeBits()) {3128 operand_ty.childType()
3129 else
3130 operand_ty;
3131 if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) {
3128 return CValue.none;3132 return CValue.none;
3129 }3133 }
31303134
src/codegen/llvm.zig+6-4
...@@ -3165,8 +3165,9 @@ pub const FuncGen = struct {...@@ -3165,8 +3165,9 @@ pub const FuncGen = struct {
31653165
3166 const ty_op = self.air.instructions.items(.data)[inst].ty_op;3166 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3167 const operand = try self.resolveInst(ty_op.operand);3167 const operand = try self.resolveInst(ty_op.operand);
3168 const err_union_ty = self.air.typeOf(ty_op.operand);3168 const result_ty = self.air.getRefType(ty_op.ty);
3169 const payload_ty = err_union_ty.errorUnionPayload();3169 const payload_ty = if (operand_is_ptr) result_ty.childType() else result_ty;
3170
3170 if (!payload_ty.hasRuntimeBits()) return null;3171 if (!payload_ty.hasRuntimeBits()) return null;
3171 if (operand_is_ptr or isByRef(payload_ty)) {3172 if (operand_is_ptr or isByRef(payload_ty)) {
3172 return self.builder.buildStructGEP(operand, 1, "");3173 return self.builder.buildStructGEP(operand, 1, "");
...@@ -3185,14 +3186,15 @@ pub const FuncGen = struct {...@@ -3185,14 +3186,15 @@ pub const FuncGen = struct {
3185 const ty_op = self.air.instructions.items(.data)[inst].ty_op;3186 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3186 const operand = try self.resolveInst(ty_op.operand);3187 const operand = try self.resolveInst(ty_op.operand);
3187 const operand_ty = self.air.typeOf(ty_op.operand);3188 const operand_ty = self.air.typeOf(ty_op.operand);
3189 const err_set_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
31883190
3189 const payload_ty = operand_ty.errorUnionPayload();3191 const payload_ty = err_set_ty.errorUnionPayload();
3190 if (!payload_ty.hasRuntimeBits()) {3192 if (!payload_ty.hasRuntimeBits()) {
3191 if (!operand_is_ptr) return operand;3193 if (!operand_is_ptr) return operand;
3192 return self.builder.buildLoad(operand, "");3194 return self.builder.buildLoad(operand, "");
3193 }3195 }
31943196
3195 if (operand_is_ptr or isByRef(payload_ty)) {3197 if (operand_is_ptr or isByRef(err_set_ty)) {
3196 const err_field_ptr = self.builder.buildStructGEP(operand, 0, "");3198 const err_field_ptr = self.builder.buildStructGEP(operand, 0, "");
3197 return self.builder.buildLoad(err_field_ptr, "");3199 return self.builder.buildLoad(err_field_ptr, "");
3198 }3200 }