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 {...@@ -302,7 +302,11 @@ pub const DeclGen = struct {
302 else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}),302 else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}),
303 }303 }
304 },304 },
305305 .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 },
306 else => {310 else => {
307 // This should lower to 0xaa bytes in safe modes, and for unsafe modes should311 // This should lower to 0xaa bytes in safe modes, and for unsafe modes should
308 // lower to leaving variables uninitialized (that might need to be implemented312 // lower to leaving variables uninitialized (that might need to be implemented
...@@ -685,6 +689,7 @@ pub const DeclGen = struct {...@@ -685,6 +689,7 @@ pub const DeclGen = struct {
685 var it = struct_obj.fields.iterator();689 var it = struct_obj.fields.iterator();
686 while (it.next()) |entry| {690 while (it.next()) |entry| {
687 const field_ty = entry.value_ptr.ty;691 const field_ty = entry.value_ptr.ty;
692 if (!field_ty.hasCodeGenBits()) continue;
688 const name: CValue = .{ .bytes = entry.key_ptr.* };693 const name: CValue = .{ .bytes = entry.key_ptr.* };
689 try buffer.append(' ');694 try buffer.append(' ');
690 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut);695 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut);
...@@ -1400,9 +1405,19 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -1400,9 +1405,19 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
1400 const writer = f.object.writer();1405 const writer = f.object.writer();
1401 const inst_ty = f.air.typeOfIndex(inst);1406 const inst_ty = f.air.typeOfIndex(inst);
14021407
1403 // First line: the variable used as data storage.
1404 const elem_type = inst_ty.elemType();1408 const elem_type = inst_ty.elemType();
1405 const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut;1409 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.
1406 const local = try f.allocLocal(elem_type, mutability);1421 const local = try f.allocLocal(elem_type, mutability);
1407 try writer.writeAll(";\n");1422 try writer.writeAll(";\n");
14081423
test/behavior.zig+1-1
...@@ -55,6 +55,7 @@ test {...@@ -55,6 +55,7 @@ test {
55 _ = @import("behavior/translate_c_macros.zig");55 _ = @import("behavior/translate_c_macros.zig");
56 _ = @import("behavior/underscore.zig");56 _ = @import("behavior/underscore.zig");
57 _ = @import("behavior/while.zig");57 _ = @import("behavior/while.zig");
58 _ = @import("behavior/void.zig");
5859
59 if (builtin.object_format != .c) {60 if (builtin.object_format != .c) {
60 // Tests that pass for stage1 and stage2 but not the C backend and wasm backend.61 // Tests that pass for stage1 and stage2 but not the C backend and wasm backend.
...@@ -94,7 +95,6 @@ test {...@@ -94,7 +95,6 @@ test {
94 _ = @import("behavior/switch.zig");95 _ = @import("behavior/switch.zig");
95 _ = @import("behavior/undefined.zig");96 _ = @import("behavior/undefined.zig");
96 _ = @import("behavior/union.zig");97 _ = @import("behavior/union.zig");
97 _ = @import("behavior/void.zig");
98 _ = @import("behavior/widening.zig");98 _ = @import("behavior/widening.zig");
9999
100 if (builtin.zig_is_stage2) {100 if (builtin.zig_is_stage2) {