authorgravatar for matthew@quickbeam.me.ukMatthew Hall <matthew@quickbeam.me.uk> 2021-12-23 18:40:27+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-27 14:42:25-08:00
log4266795743d86efc763ecadbc155d068ca1ec45a
treebec0277bd6f1e847bbbb2437d1bf64ac968e160c
parent17046674a745faaa34cf2646b9cf43455b12aba9

stage2: make tests/behaviour/void.zig work with c backend

* fix initialisation of void* fields of structs (initialises to 0xaa.. rather than {}) * don't generate struct fields when the field type does not have codegen bits * in airAlloc generate a void* literal if the element type does not have codegen bits

2 files changed, 18 insertions(+), 3 deletions(-)

src/codegen/c.zig+17-2
......@@ -302,7 +302,11 @@ pub const DeclGen = struct {
302302 else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}),
303303 }
304304 },
305
305 .Pointer => switch (dg.module.getTarget().cpu.arch.ptrBitWidth()) {
306 32 => return writer.writeAll("(void *)0xaaaaaaaa"),
307 64 => return writer.writeAll("(void *)0xaaaaaaaaaaaaaaaa"),
308 else => unreachable,
309 },
306310 else => {
307311 // This should lower to 0xaa bytes in safe modes, and for unsafe modes should
308312 // lower to leaving variables uninitialized (that might need to be implemented
......@@ -685,6 +689,7 @@ pub const DeclGen = struct {
685689 var it = struct_obj.fields.iterator();
686690 while (it.next()) |entry| {
687691 const field_ty = entry.value_ptr.ty;
692 if (!field_ty.hasCodeGenBits()) continue;
688693 const name: CValue = .{ .bytes = entry.key_ptr.* };
689694 try buffer.append(' ');
690695 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut);
......@@ -1400,9 +1405,19 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
14001405 const writer = f.object.writer();
14011406 const inst_ty = f.air.typeOfIndex(inst);
14021407
1403 // First line: the variable used as data storage.
14041408 const elem_type = inst_ty.elemType();
14051409 const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut;
1410 if (!elem_type.hasCodeGenBits()) {
1411 const target = f.object.dg.module.getTarget();
1412 const literal = switch (target.cpu.arch.ptrBitWidth()) {
1413 32 => "(void *)0xaaaaaaaa",
1414 64 => "(void *)0xaaaaaaaaaaaaaaaa",
1415 else => unreachable,
1416 };
1417 return CValue{ .bytes = literal };
1418 }
1419
1420 // First line: the variable used as data storage.
14061421 const local = try f.allocLocal(elem_type, mutability);
14071422 try writer.writeAll(";\n");
14081423
test/behavior.zig+1-1
......@@ -55,6 +55,7 @@ test {
5555 _ = @import("behavior/translate_c_macros.zig");
5656 _ = @import("behavior/underscore.zig");
5757 _ = @import("behavior/while.zig");
58 _ = @import("behavior/void.zig");
5859
5960 if (builtin.object_format != .c) {
6061 // Tests that pass for stage1 and stage2 but not the C backend and wasm backend.
......@@ -94,7 +95,6 @@ test {
9495 _ = @import("behavior/switch.zig");
9596 _ = @import("behavior/undefined.zig");
9697 _ = @import("behavior/union.zig");
97 _ = @import("behavior/void.zig");
9898 _ = @import("behavior/widening.zig");
9999
100100 if (builtin.zig_is_stage2) {