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 {...@@ -822,19 +822,6 @@ pub const DeclGen = struct {
822 try dg.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset), .Other),822 try dg.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset), .Other),
823 });823 });
824 },824 },
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 },
838 }825 }
839 },826 },
840 .comptime_field, .comptime_alloc => unreachable,827 .comptime_field, .comptime_alloc => unreachable,
...@@ -5453,49 +5440,39 @@ fn fieldLocation(...@@ -5453,49 +5440,39 @@ fn fieldLocation(
5453) union(enum) {5440) union(enum) {
5454 begin: void,5441 begin: void,
5455 field: CValue,5442 field: CValue,
5456 byte_offset: u32,5443 byte_offset: u64,
5457 end: void,
5458} {5444} {
5459 const ip = &zcu.intern_pool;5445 const ip = &zcu.intern_pool;
5460 const container_ty = Type.fromInterned(ip.indexToKey(container_ptr_ty.toIntern()).ptr_type.child);5446 const container_ty = Type.fromInterned(ip.indexToKey(container_ptr_ty.toIntern()).ptr_type.child);
5461 switch (ip.indexToKey(container_ty.toIntern())) {5447 switch (ip.indexToKey(container_ty.toIntern())) {
5462 .struct_type => {5448 .struct_type => {
5463 const loaded_struct = ip.loadStructType(container_ty.toIntern());5449 const loaded_struct = ip.loadStructType(container_ty.toIntern());
5464 switch (loaded_struct.layout) {5450 return switch (loaded_struct.layout) {
5465 .auto, .@"extern" => {5451 .auto, .@"extern" => if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu))
5466 var field_it = loaded_struct.iterateRuntimeOrder(ip);5452 .begin
5467 var before = true;5453 else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu))
5468 while (field_it.next()) |next_field_index| {5454 .{ .byte_offset = loaded_struct.offsets.get(ip)[field_index] }
5469 if (next_field_index == field_index) before = false;5455 else
5470 if (before) continue;5456 .{ .field = if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
5471 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[next_field_index]);5457 .{ .identifier = ip.stringToSlice(field_name) }
5472 if (!field_type.hasRuntimeBitsIgnoreComptime(zcu)) continue;5458 else
5473 return .{ .field = if (loaded_struct.fieldName(ip, next_field_index).unwrap()) |field_name|5459 .{ .field = field_index } },
5474 .{ .identifier = ip.stringToSlice(field_name) }5460 .@"packed" => if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0)
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)
5481 .{ .byte_offset = @divExact(zcu.structPackedFieldBitOffset(loaded_struct, field_index) +5461 .{ .byte_offset = @divExact(zcu.structPackedFieldBitOffset(loaded_struct, field_index) +
5482 container_ptr_ty.ptrInfo(zcu).packed_offset.bit_offset, 8) }5462 container_ptr_ty.ptrInfo(zcu).packed_offset.bit_offset, 8) }
5483 else5463 else
5484 .begin,5464 .begin,
5485 }5465 };
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;
5498 },5466 },
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 } },
5499 .union_type => {5476 .union_type => {
5500 const loaded_union = ip.loadUnionType(container_ty.toIntern());5477 const loaded_union = ip.loadUnionType(container_ty.toIntern());
5501 switch (loaded_union.getLayout(ip)) {5478 switch (loaded_union.getLayout(ip)) {
...@@ -5591,10 +5568,6 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5591,10 +5568,6 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
5591 try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)),5568 try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)),
5592 });5569 });
5593 },5570 },
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 },
5598 }5571 }
55995572
5600 try writer.writeAll(";\n");5573 try writer.writeAll(";\n");
...@@ -5639,11 +5612,6 @@ fn fieldPtr(...@@ -5639,11 +5612,6 @@ fn fieldPtr(
5639 try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)),5612 try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)),
5640 });5613 });
5641 },5614 },
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 },
5647 }5615 }
56485616
5649 try writer.writeAll(";\n");5617 try writer.writeAll(";\n");