From 665eba93c1733f83614c443c19cd9a5f1be910df Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 1 Dec 2022 18:24:37 -0700 Subject: CBE: eliminate zig_void C void is perfectly fine. --- lib/zig.h | 2 -- src/codegen/c.zig | 59 +++++++++++++++++++++++++++++++++-------------------- test/stage2/cbe.zig | 22 ++++++++++---------- 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index 0024bb0c58..acb2255e74 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -189,8 +189,6 @@ #define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T)) -typedef void zig_void; - #if defined(__cplusplus) typedef bool zig_bool; #define zig_false false diff --git a/src/codegen/c.zig b/src/codegen/c.zig index a14154d85f..d7aec355e9 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1802,33 +1802,24 @@ pub const DeclGen = struct { const target = dg.module.getTarget(); switch (t.zigTypeTag()) { - .NoReturn, .Void, .Bool, .Int, .Float, .ErrorSet => |tag| { - const is_named = switch (tag) { - .Int => t.isNamedInt(), - .ErrorSet => false, - else => true, - }; - if (is_named) { + .Void => { + try w.writeAll("void"); + }, + .NoReturn, .Bool, .Float => { + try w.writeAll("zig_"); + try t.print(w, dg.module); + }, + .Int => { + if (t.isNamedInt()) { try w.writeAll("zig_"); try t.print(w, dg.module); } else { - const int_info = t.intInfo(target); - if (toCIntBits(int_info.bits)) |c_bits| - return w.print("zig_{c}{d}", .{ signAbbrev(int_info.signedness), c_bits }) - else if (loweredArrayInfo(t, target)) |array_info| { - assert(array_info.sentinel == null); - var array_pl = Type.Payload.Array{ - .base = .{ .tag = .array }, - .data = .{ .len = array_info.len, .elem_type = array_info.elem_type }, - }; - const array_ty = Type.initPayload(&array_pl.base); - - return dg.renderType(w, array_ty, kind); - } else return dg.fail("C backend: Unable to lower unnamed integer type {}", .{ - t.fmt(dg.module), - }); + return renderTypeUnnamed(dg, w, t, kind); } }, + .ErrorSet => { + return renderTypeUnnamed(dg, w, t, kind); + }, .Pointer => { const ptr_info = t.ptrInfo().data; if (ptr_info.size == .Slice) { @@ -2015,6 +2006,30 @@ pub const DeclGen = struct { } } + fn renderTypeUnnamed( + dg: *DeclGen, + w: anytype, + t: Type, + kind: TypedefKind, + ) error{ OutOfMemory, AnalysisFail }!void { + const target = dg.module.getTarget(); + const int_info = t.intInfo(target); + if (toCIntBits(int_info.bits)) |c_bits| + return w.print("zig_{c}{d}", .{ signAbbrev(int_info.signedness), c_bits }) + else if (loweredArrayInfo(t, target)) |array_info| { + assert(array_info.sentinel == null); + var array_pl = Type.Payload.Array{ + .base = .{ .tag = .array }, + .data = .{ .len = array_info.len, .elem_type = array_info.elem_type }, + }; + const array_ty = Type.initPayload(&array_pl.base); + + return dg.renderType(w, array_ty, kind); + } else return dg.fail("C backend: Unable to lower unnamed integer type {}", .{ + t.fmt(dg.module), + }); + } + /// Renders a type in C typecast format. /// /// This is guaranteed to be valid in a typecast expression, but not diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index c29dafb248..441b8b23d3 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -951,7 +951,7 @@ pub fn addCases(ctx: *TestContext) !void { ctx.h("simple header", linux_x64, \\export fn start() void{} , - \\zig_extern zig_void start(zig_void); + \\zig_extern void start(void); \\ ); ctx.h("header with single param function", linux_x64, @@ -959,7 +959,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ _ = a; \\} , - \\zig_extern zig_void start(zig_u8 const a0); + \\zig_extern void start(zig_u8 const a0); \\ ); ctx.h("header with multiple param function", linux_x64, @@ -967,25 +967,25 @@ pub fn addCases(ctx: *TestContext) !void { \\ _ = a; _ = b; _ = c; \\} , - \\zig_extern zig_void start(zig_u8 const a0, zig_u8 const a1, zig_u8 const a2); + \\zig_extern void start(zig_u8 const a0, zig_u8 const a1, zig_u8 const a2); \\ ); ctx.h("header with u32 param function", linux_x64, \\export fn start(a: u32) void{ _ = a; } , - \\zig_extern zig_void start(zig_u32 const a0); + \\zig_extern void start(zig_u32 const a0); \\ ); ctx.h("header with usize param function", linux_x64, \\export fn start(a: usize) void{ _ = a; } , - \\zig_extern zig_void start(zig_usize const a0); + \\zig_extern void start(zig_usize const a0); \\ ); ctx.h("header with bool param function", linux_x64, \\export fn start(a: bool) void{_ = a;} , - \\zig_extern zig_void start(zig_bool const a0); + \\zig_extern void start(zig_bool const a0); \\ ); ctx.h("header with noreturn function", linux_x64, @@ -993,7 +993,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ unreachable; \\} , - \\zig_extern zig_noreturn start(zig_void); + \\zig_extern zig_noreturn start(void); \\ ); ctx.h("header with multiple functions", linux_x64, @@ -1001,15 +1001,15 @@ pub fn addCases(ctx: *TestContext) !void { \\export fn b() void{} \\export fn c() void{} , - \\zig_extern zig_void a(zig_void); - \\zig_extern zig_void b(zig_void); - \\zig_extern zig_void c(zig_void); + \\zig_extern void a(void); + \\zig_extern void b(void); + \\zig_extern void c(void); \\ ); ctx.h("header with multiple includes", linux_x64, \\export fn start(a: u32, b: usize) void{ _ = a; _ = b; } , - \\zig_extern zig_void start(zig_u32 const a0, zig_usize const a1); + \\zig_extern void start(zig_u32 const a0, zig_usize const a1); \\ ); } -- cgit v1.2.3