Skip to content

Commit 77e6513

Browse files
committed
cbe: implement stdbool.h reserved identifiers
Also remove the legalize pass from zig1.
1 parent 6198f7a commit 77e6513

File tree

13 files changed

+55
-28
lines changed

13 files changed

+55
-28
lines changed

src/Air/Legalize.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,14 @@ pub const Features = std.enums.EnumSet(Feature);
164164
pub const Error = std.mem.Allocator.Error;
165165

166166
pub fn legalize(air: *Air, pt: Zcu.PerThread, features: *const Features) Error!void {
167+
dev.check(.legalize);
168+
assert(!features.bits.eql(.initEmpty())); // backend asked to run legalize, but no features were enabled
167169
var l: Legalize = .{
168170
.pt = pt,
169171
.air_instructions = air.instructions.toMultiArrayList(),
170172
.air_extra = air.extra,
171173
.features = features,
172174
};
173-
if (l.features.bits.eql(.initEmpty())) return;
174175
defer air.* = l.getTmpAir();
175176
const main_extra = l.extraData(Air.Block, l.air_extra.items[@intFromEnum(Air.ExtraIndex.main_block)]);
176177
try l.legalizeBody(main_extra.end, main_extra.data.body_len);
@@ -845,6 +846,7 @@ inline fn replaceInst(l: *Legalize, inst: Air.Inst.Index, tag: Air.Inst.Tag, dat
845846

846847
const Air = @import("../Air.zig");
847848
const assert = std.debug.assert;
849+
const dev = @import("../dev.zig");
848850
const Legalize = @This();
849851
const std = @import("std");
850852
const Type = @import("../Type.zig");

src/Zcu/PerThread.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,9 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: *A
17411741
return;
17421742
}
17431743

1744-
try air.legalize(pt, @import("../codegen.zig").legalizeFeatures(pt, nav_index));
1744+
legalize: {
1745+
try air.legalize(pt, @import("../codegen.zig").legalizeFeatures(pt, nav_index) orelse break :legalize);
1746+
}
17451747

17461748
var liveness = try Air.Liveness.analyze(gpa, air.*, ip);
17471749
defer liveness.deinit(gpa);

src/arch/aarch64/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const gp = abi.RegisterClass.gp;
4040

4141
const InnerError = CodeGenError || error{OutOfRegisters};
4242

43-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
44-
return comptime &.initEmpty();
43+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
44+
return null;
4545
}
4646

4747
gpa: Allocator,

src/arch/arm/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const gp = abi.RegisterClass.gp;
4141

4242
const InnerError = CodeGenError || error{OutOfRegisters};
4343

44-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
45-
return comptime &.initEmpty();
44+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
45+
return null;
4646
}
4747

4848
gpa: Allocator,

src/arch/powerpc/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const Zcu = @import("../../Zcu.zig");
1010
const assert = std.debug.assert;
1111
const log = std.log.scoped(.codegen);
1212

13-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
14-
return comptime &.initEmpty();
13+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
14+
return null;
1515
}
1616

1717
pub fn generate(

src/arch/riscv64/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ const Instruction = encoding.Instruction;
5151

5252
const InnerError = CodeGenError || error{OutOfRegisters};
5353

54-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
55-
return comptime &.initEmpty();
54+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
55+
return null;
5656
}
5757

5858
pt: Zcu.PerThread,

src/arch/sparc64/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const Self = @This();
4141

4242
const InnerError = CodeGenError || error{OutOfRegisters};
4343

44-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
45-
return comptime &.initEmpty();
44+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
45+
return null;
4646
}
4747

4848
const RegisterView = enum(u1) {

src/arch/wasm/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const libcFloatSuffix = target_util.libcFloatSuffix;
3131
const compilerRtFloatAbbrev = target_util.compilerRtFloatAbbrev;
3232
const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev;
3333

34-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
35-
return comptime &.initEmpty();
34+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
35+
return null;
3636
}
3737

3838
/// Reference to the function declaration the code

src/codegen.zig

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,22 @@ pub const CodeGenError = GenerateSymbolError || error{
2727
CodegenFail,
2828
};
2929

30-
fn devFeatureForBackend(comptime backend: std.builtin.CompilerBackend) dev.Feature {
31-
comptime assert(mem.startsWith(u8, @tagName(backend), "stage2_"));
32-
return @field(dev.Feature, @tagName(backend)["stage2_".len..] ++ "_backend");
30+
fn devFeatureForBackend(backend: std.builtin.CompilerBackend) dev.Feature {
31+
return switch (backend) {
32+
.other, .stage1 => unreachable,
33+
.stage2_aarch64 => .aarch64_backend,
34+
.stage2_arm => .arm_backend,
35+
.stage2_c => .c_backend,
36+
.stage2_llvm => .llvm_backend,
37+
.stage2_powerpc => .powerpc_backend,
38+
.stage2_riscv64 => .riscv64_backend,
39+
.stage2_sparc64 => .sparc64_backend,
40+
.stage2_spirv64 => .spirv64_backend,
41+
.stage2_wasm => .wasm_backend,
42+
.stage2_x86 => .x86_backend,
43+
.stage2_x86_64 => .x86_64_backend,
44+
_ => unreachable,
45+
};
3346
}
3447

3548
fn importBackend(comptime backend: std.builtin.CompilerBackend) type {
@@ -49,10 +62,10 @@ fn importBackend(comptime backend: std.builtin.CompilerBackend) type {
4962
};
5063
}
5164

52-
pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) *const Air.Legalize.Features {
65+
pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) ?*const Air.Legalize.Features {
5366
const zcu = pt.zcu;
5467
const target = &zcu.navFileScope(nav_index).mod.?.resolved_target.result;
55-
return switch (target_util.zigBackend(target.*, zcu.comp.config.use_llvm)) {
68+
switch (target_util.zigBackend(target.*, zcu.comp.config.use_llvm)) {
5669
else => unreachable,
5770
inline .stage2_llvm,
5871
.stage2_c,
@@ -65,8 +78,11 @@ pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) *con
6578
.stage2_sparc64,
6679
.stage2_spirv64,
6780
.stage2_powerpc,
68-
=> |backend| importBackend(backend).legalizeFeatures(target),
69-
};
81+
=> |backend| {
82+
dev.check(devFeatureForBackend(backend));
83+
return importBackend(backend).legalizeFeatures(target);
84+
},
85+
}
7086
}
7187

7288
pub fn generateFunction(

src/codegen/c.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const Alignment = InternPool.Alignment;
2020
const BigIntLimb = std.math.big.Limb;
2121
const BigInt = std.math.big.int;
2222

23-
pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {
24-
return comptime &.initEmpty();
23+
pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
24+
return null;
2525
}
2626

2727
pub const CType = @import("c/Type.zig");
@@ -210,7 +210,6 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{
210210
.{ "atomic_ushort", {} },
211211
.{ "atomic_wchar_t", {} },
212212
.{ "auto", {} },
213-
.{ "bool", {} },
214213
.{ "break", {} },
215214
.{ "case", {} },
216215
.{ "char", {} },
@@ -270,6 +269,11 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{
270269
.{ "va_end", {} },
271270
.{ "va_copy", {} },
272271

272+
// stdbool.h
273+
.{ "bool", {} },
274+
.{ "false", {} },
275+
.{ "true", {} },
276+
273277
// stddef.h
274278
.{ "offsetof", {} },
275279

0 commit comments

Comments
 (0)