aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorMatthew Hall <matthew@quickbeam.me.uk>2021-12-23 18:40:27 +0000
committerAndrew Kelley <andrew@ziglang.org>2021-12-27 14:42:25 -0800
commit4266795743d86efc763ecadbc155d068ca1ec45a (patch)
treebec0277bd6f1e847bbbb2437d1bf64ac968e160c /src/codegen/c.zig
parent17046674a745faaa34cf2646b9cf43455b12aba9 (diff)
downloadzig-4266795743d86efc763ecadbc155d068ca1ec45a.tar.gz
zig-4266795743d86efc763ecadbc155d068ca1ec45a.zip
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
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 56df9a86cb..b23b937e05 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -302,7 +302,11 @@ pub const DeclGen = struct {
else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}),
}
},
-
+ .Pointer => switch (dg.module.getTarget().cpu.arch.ptrBitWidth()) {
+ 32 => return writer.writeAll("(void *)0xaaaaaaaa"),
+ 64 => return writer.writeAll("(void *)0xaaaaaaaaaaaaaaaa"),
+ else => unreachable,
+ },
else => {
// This should lower to 0xaa bytes in safe modes, and for unsafe modes should
// lower to leaving variables uninitialized (that might need to be implemented
@@ -685,6 +689,7 @@ pub const DeclGen = struct {
var it = struct_obj.fields.iterator();
while (it.next()) |entry| {
const field_ty = entry.value_ptr.ty;
+ if (!field_ty.hasCodeGenBits()) continue;
const name: CValue = .{ .bytes = entry.key_ptr.* };
try buffer.append(' ');
try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut);
@@ -1400,9 +1405,19 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
const writer = f.object.writer();
const inst_ty = f.air.typeOfIndex(inst);
- // First line: the variable used as data storage.
const elem_type = inst_ty.elemType();
const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut;
+ if (!elem_type.hasCodeGenBits()) {
+ const target = f.object.dg.module.getTarget();
+ const literal = switch (target.cpu.arch.ptrBitWidth()) {
+ 32 => "(void *)0xaaaaaaaa",
+ 64 => "(void *)0xaaaaaaaaaaaaaaaa",
+ else => unreachable,
+ };
+ return CValue{ .bytes = literal };
+ }
+
+ // First line: the variable used as data storage.
const local = try f.allocLocal(elem_type, mutability);
try writer.writeAll(";\n");