authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-04-06 19:10:36-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-04-08 13:20:13-04:00
log23ee39116c783908420d7d9e58a9344042c823fa
tree12bef7e52cca2d847148b8b73d0994d85b2ce03a
parentd979df585d05de8d7385495fe6aee2b1d4e1380f

cbe: fix struct field location computation


1 files changed, 22 insertions(+), 54 deletions(-)

src/codegen/c.zig+22-54
......@@ -822,19 +822,6 @@ pub const DeclGen = struct {
822822 try dg.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset), .Other),
823823 });
824824 },
825 .end => {
826 const ptr_base_ctype = try dg.ctypeFromType(ptr_base_ty, .complete);
827 if (!ptr_ctype.eql(ptr_base_ctype)) {
828 try writer.writeByte('(');
829 try dg.renderCType(writer, ptr_ctype);
830 try writer.writeByte(')');
831 }
832 try writer.writeAll("((");
833 try dg.renderParentPtr(writer, field.base, location);
834 try writer.print(") + {})", .{
835 try dg.fmtIntLiteral(try zcu.intValue(Type.usize, 1), .Other),
836 });
837 },
838825 }
839826 },
840827 .comptime_field, .comptime_alloc => unreachable,
......@@ -5453,49 +5440,39 @@ fn fieldLocation(
54535440) union(enum) {
54545441 begin: void,
54555442 field: CValue,
5456 byte_offset: u32,
5457 end: void,
5443 byte_offset: u64,
54585444} {
54595445 const ip = &zcu.intern_pool;
54605446 const container_ty = Type.fromInterned(ip.indexToKey(container_ptr_ty.toIntern()).ptr_type.child);
54615447 switch (ip.indexToKey(container_ty.toIntern())) {
54625448 .struct_type => {
54635449 const loaded_struct = ip.loadStructType(container_ty.toIntern());
5464 switch (loaded_struct.layout) {
5465 .auto, .@"extern" => {
5466 var field_it = loaded_struct.iterateRuntimeOrder(ip);
5467 var before = true;
5468 while (field_it.next()) |next_field_index| {
5469 if (next_field_index == field_index) before = false;
5470 if (before) continue;
5471 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[next_field_index]);
5472 if (!field_type.hasRuntimeBitsIgnoreComptime(zcu)) continue;
5473 return .{ .field = if (loaded_struct.fieldName(ip, next_field_index).unwrap()) |field_name|
5474 .{ .identifier = ip.stringToSlice(field_name) }
5475 else
5476 .{ .field = next_field_index } };
5477 }
5478 return if (container_ty.hasRuntimeBitsIgnoreComptime(zcu)) .end else .begin;
5479 },
5480 .@"packed" => return if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0)
5450 return switch (loaded_struct.layout) {
5451 .auto, .@"extern" => if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu))
5452 .begin
5453 else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu))
5454 .{ .byte_offset = loaded_struct.offsets.get(ip)[field_index] }
5455 else
5456 .{ .field = if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
5457 .{ .identifier = ip.stringToSlice(field_name) }
5458 else
5459 .{ .field = field_index } },
5460 .@"packed" => if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0)
54815461 .{ .byte_offset = @divExact(zcu.structPackedFieldBitOffset(loaded_struct, field_index) +
54825462 container_ptr_ty.ptrInfo(zcu).packed_offset.bit_offset, 8) }
54835463 else
54845464 .begin,
5485 }
5486 },
5487 .anon_struct_type => |anon_struct_info| {
5488 for (field_index..anon_struct_info.types.len) |next_field_index| {
5489 if (anon_struct_info.values.get(ip)[next_field_index] != .none) continue;
5490 const field_type = Type.fromInterned(anon_struct_info.types.get(ip)[next_field_index]);
5491 if (!field_type.hasRuntimeBitsIgnoreComptime(zcu)) continue;
5492 return .{ .field = if (anon_struct_info.fieldName(ip, next_field_index).unwrap()) |field_name|
5493 .{ .identifier = ip.stringToSlice(field_name) }
5494 else
5495 .{ .field = next_field_index } };
5496 }
5497 return if (container_ty.hasRuntimeBitsIgnoreComptime(zcu)) .end else .begin;
5465 };
54985466 },
5467 .anon_struct_type => |anon_struct_info| return if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu))
5468 .begin
5469 else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu))
5470 .{ .byte_offset = container_ty.structFieldOffset(field_index, zcu) }
5471 else
5472 .{ .field = if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name|
5473 .{ .identifier = ip.stringToSlice(field_name) }
5474 else
5475 .{ .field = field_index } },
54995476 .union_type => {
55005477 const loaded_union = ip.loadUnionType(container_ty.toIntern());
55015478 switch (loaded_union.getLayout(ip)) {
......@@ -5591,10 +5568,6 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
55915568 try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)),
55925569 });
55935570 },
5594 .end => {
5595 try f.writeCValue(writer, field_ptr_val, .Other);
5596 try writer.print(" - {}", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))});
5597 },
55985571 }
55995572
56005573 try writer.writeAll(";\n");
......@@ -5639,11 +5612,6 @@ fn fieldPtr(
56395612 try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)),
56405613 });
56415614 },
5642 .end => {
5643 try writer.writeByte('(');
5644 try f.writeCValue(writer, container_ptr_val, .Other);
5645 try writer.print(" + {})", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))});
5646 },
56475615 }
56485616
56495617 try writer.writeAll(";\n");