aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-12-02 16:06:28 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-12-02 16:12:49 -0700
commit29e8e67a7ec767aed0b9b689bdcad7355c032751 (patch)
treee8781d33e0a2a1d6510c19d31cdc5ee15ff71f3a /src/codegen
parentd171279d7965137ac835f2ed6d48517478508eae (diff)
downloadzig-29e8e67a7ec767aed0b9b689bdcad7355c032751.tar.gz
zig-29e8e67a7ec767aed0b9b689bdcad7355c032751.zip
CBE: use bool, true, false, instead of `zig_` prefixes
In general the C backend should lower to human-maintainable C code whenever possible. Directly using C types that one would use when writing C code is one part of the strategy. The concern with including stdint.h is C89 compatibility. Well, we can just check the C std lib version before deciding to include that header.
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index e38aedb125..f4b54cd3aa 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -717,10 +717,21 @@ pub const DeclGen = struct {
val = rt.data;
}
const target = dg.module.getTarget();
+
+ const safety_on = switch (dg.module.optimizeMode()) {
+ .Debug, .ReleaseSafe => true,
+ .ReleaseFast, .ReleaseSmall => false,
+ };
+
if (val.isUndefDeep()) {
switch (ty.zigTypeTag()) {
- // bool b = 0xaa; evals to true, but memcpy(&b, 0xaa, 1); evals to false.
- .Bool => return dg.renderValue(writer, ty, Value.false, location),
+ .Bool => {
+ if (safety_on) {
+ return writer.writeAll("0xaa");
+ } else {
+ return writer.writeAll("false");
+ }
+ },
.Int, .Enum, .ErrorSet => return writer.print("{x}", .{try dg.fmtIntLiteral(ty, val)}),
.Float => {
const bits = ty.floatBits(target);
@@ -1099,7 +1110,13 @@ pub const DeclGen = struct {
},
}
},
- .Bool => return writer.print("zig_{}", .{val.toBool()}),
+ .Bool => {
+ if (val.toBool()) {
+ return writer.writeAll("true");
+ } else {
+ return writer.writeAll("false");
+ }
+ },
.Optional => {
var opt_buf: Type.Payload.ElemType = undefined;
const payload_ty = ty.optionalChild(&opt_buf);
@@ -1804,10 +1821,9 @@ pub const DeclGen = struct {
const target = dg.module.getTarget();
switch (t.zigTypeTag()) {
- .Void => {
- try w.writeAll("void");
- },
- .NoReturn, .Bool, .Float => {
+ .Void => try w.writeAll("void"),
+ .Bool => try w.writeAll("bool"),
+ .NoReturn, .Float => {
try w.writeAll("zig_");
try t.print(w, dg.module);
},