authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-03-29 07:51:56-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-05-11 02:17:11-07:00
log4ce85f930e82c987c0759f528e4876fb45286389
treef706bf95584b4cdc24f921b5d8923ac8aca5183a
parentece70e08a09e24ac27f354579ee70446563cc4bf

riscv: implement `structFieldPtr` and `retLoad`


1 files changed, 38 insertions(+), 19 deletions(-)

src/arch/riscv64/CodeGen.zig+38-19
...@@ -2114,23 +2114,39 @@ fn store(self: *Self, pointer: MCValue, value: MCValue, ptr_ty: Type, value_ty:...@@ -2114,23 +2114,39 @@ fn store(self: *Self, pointer: MCValue, value: MCValue, ptr_ty: Type, value_ty:
2114fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) !void {2114fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) !void {
2115 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;2115 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2116 const extra = self.air.extraData(Air.StructField, ty_pl.payload).data;2116 const extra = self.air.extraData(Air.StructField, ty_pl.payload).data;
2117 const result = try self.structFieldPtr(inst, extra.struct_operand, ty_pl.ty, extra.field_index);2117 const result = try self.structFieldPtr(inst, extra.struct_operand, extra.field_index);
2118 return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none });2118 return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none });
2119}2119}
21202120
2121fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void {2121fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void {
2122 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;2122 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2123 const result = try self.structFieldPtr(inst, ty_op.operand, ty_op.ty, index);2123 const result = try self.structFieldPtr(inst, ty_op.operand, index);
2124 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });2124 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2125}2125}
21262126
2127fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Air.Inst.Ref, index: u32) !MCValue {2127fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue {
2128 _ = inst;2128 const mod = self.bin_file.comp.module.?;
2129 _ = operand;2129 const ptr_field_ty = self.typeOfIndex(inst);
2130 _ = ty;2130 const ptr_container_ty = self.typeOf(operand);
2131 _ = index;2131 const ptr_container_ty_info = ptr_container_ty.ptrInfo(mod);
2132 const container_ty = ptr_container_ty.childType(mod);
2133
2134 const field_offset: i32 = if (mod.typeToPackedStruct(container_ty)) |struct_obj|
2135 if (ptr_field_ty.ptrInfo(mod).packed_offset.host_size == 0)
2136 @divExact(mod.structPackedFieldBitOffset(struct_obj, index) +
2137 ptr_container_ty_info.packed_offset.bit_offset, 8)
2138 else
2139 0
2140 else
2141 @intCast(container_ty.structFieldOffset(index, mod));
21322142
2133 return self.fail("TODO: structFieldPtr", .{});2143 const src_mcv = try self.resolveInst(operand);
2144 const dst_mcv = if (switch (src_mcv) {
2145 .immediate, .ptr_stack_offset => true,
2146 .register, .register_offset => self.reuseOperand(inst, operand, 0, src_mcv),
2147 else => false,
2148 }) src_mcv else try self.copyToNewRegister(inst, src_mcv);
2149 return dst_mcv.offset(field_offset);
2134}2150}
21352151
2136fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {2152fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
...@@ -2402,6 +2418,8 @@ fn genCall(...@@ -2402,6 +2418,8 @@ fn genCall(
2402}2418}
24032419
2404fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {2420fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
2421 const mod = self.bin_file.comp.module.?;
2422
2405 if (safety) {2423 if (safety) {
2406 // safe2424 // safe
2407 } else {2425 } else {
...@@ -2416,17 +2434,15 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {...@@ -2416,17 +2434,15 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
2416 .data = .{ .nop = {} },2434 .data = .{ .nop = {} },
2417 });2435 });
24182436
2419 try self.ret(operand);2437 const ret_ty = self.fn_type.fnReturnType(mod);
2438 try self.genCopy(ret_ty, self.ret_mcv, operand);
2439
2440 try self.ret();
24202441
2421 return self.finishAir(inst, .dead, .{ un_op, .none, .none });2442 return self.finishAir(inst, .dead, .{ un_op, .none, .none });
2422}2443}
24232444
2424fn ret(self: *Self, mcv: MCValue) !void {2445fn ret(self: *Self) !void {
2425 const mod = self.bin_file.comp.module.?;
2426
2427 const ret_ty = self.fn_type.fnReturnType(mod);
2428 try self.genCopy(ret_ty, self.ret_mcv, mcv);
2429
2430 _ = try self.addInst(.{2446 _ = try self.addInst(.{
2431 .tag = .psuedo_epilogue,2447 .tag = .psuedo_epilogue,
2432 .data = .{ .nop = {} },2448 .data = .{ .nop = {} },
...@@ -2444,9 +2460,13 @@ fn ret(self: *Self, mcv: MCValue) !void {...@@ -2444,9 +2460,13 @@ fn ret(self: *Self, mcv: MCValue) !void {
2444fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {2460fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
2445 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;2461 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
2446 const ptr = try self.resolveInst(un_op);2462 const ptr = try self.resolveInst(un_op);
2447 _ = ptr;2463 const ptr_ty = self.typeOf(un_op);
2448 return self.fail("TODO implement airRetLoad for {}", .{self.target.cpu.arch});2464
2449 //return self.finishAir(inst, .dead, .{ un_op, .none, .none });2465 try self.load(self.ret_mcv, ptr, ptr_ty);
2466
2467 try self.ret();
2468
2469 return self.finishAir(inst, .dead, .{ un_op, .none, .none });
2450}2470}
24512471
2452fn airCmp(self: *Self, inst: Air.Inst.Index) !void {2472fn airCmp(self: *Self, inst: Air.Inst.Index) !void {
...@@ -3790,7 +3810,6 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {...@@ -3790,7 +3810,6 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
3790 return MCValue{ .none = {} };3810 return MCValue{ .none = {} };
37913811
3792 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);3812 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
3793
3794 return self.getResolvedInstValue(inst_index);3813 return self.getResolvedInstValue(inst_index);
3795}3814}
37963815