aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index bc3e357827..2084b1e1ce 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -909,6 +909,12 @@ fn genBody(o: *Object, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfM
.switch_br => try airSwitchBr(o, inst),
.wrap_optional => try airWrapOptional(o, inst),
.struct_field_ptr => try airStructFieldPtr(o, inst),
+
+ .struct_field_ptr_index_0 => try airStructFieldPtrIndex(o, inst, 0),
+ .struct_field_ptr_index_1 => try airStructFieldPtrIndex(o, inst, 1),
+ .struct_field_ptr_index_2 => try airStructFieldPtrIndex(o, inst, 2),
+ .struct_field_ptr_index_3 => try airStructFieldPtrIndex(o, inst, 3),
+
.struct_field_val => try airStructFieldVal(o, inst),
.slice_ptr => try airSliceField(o, inst, ".ptr;\n"),
.slice_len => try airSliceField(o, inst, ".len;\n"),
@@ -1651,15 +1657,31 @@ fn airOptionalPayload(o: *Object, inst: Air.Inst.Index) !CValue {
fn airStructFieldPtr(o: *Object, inst: Air.Inst.Index) !CValue {
if (o.liveness.isUnused(inst))
- return CValue.none;
+ // TODO this @as is needed because of a stage1 bug
+ return @as(CValue, CValue.none);
const ty_pl = o.air.instructions.items(.data)[inst].ty_pl;
const extra = o.air.extraData(Air.StructField, ty_pl.payload).data;
- const writer = o.writer();
const struct_ptr = try o.resolveInst(extra.struct_operand);
const struct_ptr_ty = o.air.typeOf(extra.struct_operand);
+ return structFieldPtr(o, inst, struct_ptr_ty, struct_ptr, extra.field_index);
+}
+
+fn airStructFieldPtrIndex(o: *Object, inst: Air.Inst.Index, index: u8) !CValue {
+ if (o.liveness.isUnused(inst))
+ // TODO this @as is needed because of a stage1 bug
+ return @as(CValue, CValue.none);
+
+ const ty_op = o.air.instructions.items(.data)[inst].ty_op;
+ const struct_ptr = try o.resolveInst(ty_op.operand);
+ const struct_ptr_ty = o.air.typeOf(ty_op.operand);
+ return structFieldPtr(o, inst, struct_ptr_ty, struct_ptr, index);
+}
+
+fn structFieldPtr(o: *Object, inst: Air.Inst.Index, struct_ptr_ty: Type, struct_ptr: CValue, index: u32) !CValue {
+ const writer = o.writer();
const struct_obj = struct_ptr_ty.elemType().castTag(.@"struct").?.data;
- const field_name = struct_obj.fields.keys()[extra.field_index];
+ const field_name = struct_obj.fields.keys()[index];
const inst_ty = o.air.typeOfIndex(inst);
const local = try o.allocLocal(inst_ty, .Const);