authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-05-30 12:56:52-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-05-31 18:54:28-04:00
log77e6513030a6258a893c2f11ad9708c9612b7715
treed6821410ab84c13ba0a440bc499422d430b03f7a
parent6198f7afb76b7a5a6d359bfd24f8fbdabc77939b

cbe: implement `stdbool.h` reserved identifiers

Also remove the legalize pass from zig1.

13 files changed, 55 insertions(+), 28 deletions(-)

src/Air/Legalize.zig+3-1
...@@ -164,13 +164,14 @@ pub const Features = std.enums.EnumSet(Feature);...@@ -164,13 +164,14 @@ pub const Features = std.enums.EnumSet(Feature);
164pub const Error = std.mem.Allocator.Error;164pub const Error = std.mem.Allocator.Error;
165165
166pub fn legalize(air: *Air, pt: Zcu.PerThread, features: *const Features) Error!void {166pub 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
167 var l: Legalize = .{169 var l: Legalize = .{
168 .pt = pt,170 .pt = pt,
169 .air_instructions = air.instructions.toMultiArrayList(),171 .air_instructions = air.instructions.toMultiArrayList(),
170 .air_extra = air.extra,172 .air_extra = air.extra,
171 .features = features,173 .features = features,
172 };174 };
173 if (l.features.bits.eql(.initEmpty())) return;
174 defer air.* = l.getTmpAir();175 defer air.* = l.getTmpAir();
175 const main_extra = l.extraData(Air.Block, l.air_extra.items[@intFromEnum(Air.ExtraIndex.main_block)]);176 const main_extra = l.extraData(Air.Block, l.air_extra.items[@intFromEnum(Air.ExtraIndex.main_block)]);
176 try l.legalizeBody(main_extra.end, main_extra.data.body_len);177 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...@@ -845,6 +846,7 @@ inline fn replaceInst(l: *Legalize, inst: Air.Inst.Index, tag: Air.Inst.Tag, dat
845846
846const Air = @import("../Air.zig");847const Air = @import("../Air.zig");
847const assert = std.debug.assert;848const assert = std.debug.assert;
849const dev = @import("../dev.zig");
848const Legalize = @This();850const Legalize = @This();
849const std = @import("std");851const std = @import("std");
850const Type = @import("../Type.zig");852const Type = @import("../Type.zig");
src/Zcu/PerThread.zig+3-1
...@@ -1741,7 +1741,9 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: *A...@@ -1741,7 +1741,9 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: *A
1741 return;1741 return;
1742 }1742 }
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
1746 var liveness = try Air.Liveness.analyze(gpa, air.*, ip);1748 var liveness = try Air.Liveness.analyze(gpa, air.*, ip);
1747 defer liveness.deinit(gpa);1749 defer liveness.deinit(gpa);
src/arch/aarch64/CodeGen.zig+2-2
...@@ -40,8 +40,8 @@ const gp = abi.RegisterClass.gp;...@@ -40,8 +40,8 @@ const gp = abi.RegisterClass.gp;
4040
41const InnerError = CodeGenError || error{OutOfRegisters};41const InnerError = CodeGenError || error{OutOfRegisters};
4242
43pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {43pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
44 return comptime &.initEmpty();44 return null;
45}45}
4646
47gpa: Allocator,47gpa: Allocator,
src/arch/arm/CodeGen.zig+2-2
...@@ -41,8 +41,8 @@ const gp = abi.RegisterClass.gp;...@@ -41,8 +41,8 @@ const gp = abi.RegisterClass.gp;
4141
42const InnerError = CodeGenError || error{OutOfRegisters};42const InnerError = CodeGenError || error{OutOfRegisters};
4343
44pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {44pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
45 return comptime &.initEmpty();45 return null;
46}46}
4747
48gpa: Allocator,48gpa: Allocator,
src/arch/powerpc/CodeGen.zig+2-2
...@@ -10,8 +10,8 @@ const Zcu = @import("../../Zcu.zig");...@@ -10,8 +10,8 @@ const Zcu = @import("../../Zcu.zig");
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const log = std.log.scoped(.codegen);11const log = std.log.scoped(.codegen);
1212
13pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {13pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
14 return comptime &.initEmpty();14 return null;
15}15}
1616
17pub fn generate(17pub fn generate(
src/arch/riscv64/CodeGen.zig+2-2
...@@ -51,8 +51,8 @@ const Instruction = encoding.Instruction;...@@ -51,8 +51,8 @@ const Instruction = encoding.Instruction;
5151
52const InnerError = CodeGenError || error{OutOfRegisters};52const InnerError = CodeGenError || error{OutOfRegisters};
5353
54pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {54pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
55 return comptime &.initEmpty();55 return null;
56}56}
5757
58pt: Zcu.PerThread,58pt: Zcu.PerThread,
src/arch/sparc64/CodeGen.zig+2-2
...@@ -41,8 +41,8 @@ const Self = @This();...@@ -41,8 +41,8 @@ const Self = @This();
4141
42const InnerError = CodeGenError || error{OutOfRegisters};42const InnerError = CodeGenError || error{OutOfRegisters};
4343
44pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {44pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
45 return comptime &.initEmpty();45 return null;
46}46}
4747
48const RegisterView = enum(u1) {48const RegisterView = enum(u1) {
src/arch/wasm/CodeGen.zig+2-2
...@@ -31,8 +31,8 @@ const libcFloatSuffix = target_util.libcFloatSuffix;...@@ -31,8 +31,8 @@ const libcFloatSuffix = target_util.libcFloatSuffix;
31const compilerRtFloatAbbrev = target_util.compilerRtFloatAbbrev;31const compilerRtFloatAbbrev = target_util.compilerRtFloatAbbrev;
32const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev;32const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev;
3333
34pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {34pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
35 return comptime &.initEmpty();35 return null;
36}36}
3737
38/// Reference to the function declaration the code38/// Reference to the function declaration the code
src/codegen.zig+23-7
...@@ -27,9 +27,22 @@ pub const CodeGenError = GenerateSymbolError || error{...@@ -27,9 +27,22 @@ pub const CodeGenError = GenerateSymbolError || error{
27 CodegenFail,27 CodegenFail,
28};28};
2929
30fn devFeatureForBackend(comptime backend: std.builtin.CompilerBackend) dev.Feature {30fn devFeatureForBackend(backend: std.builtin.CompilerBackend) dev.Feature {
31 comptime assert(mem.startsWith(u8, @tagName(backend), "stage2_"));31 return switch (backend) {
32 return @field(dev.Feature, @tagName(backend)["stage2_".len..] ++ "_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 };
33}46}
3447
35fn importBackend(comptime backend: std.builtin.CompilerBackend) type {48fn importBackend(comptime backend: std.builtin.CompilerBackend) type {
...@@ -49,10 +62,10 @@ fn importBackend(comptime backend: std.builtin.CompilerBackend) type {...@@ -49,10 +62,10 @@ fn importBackend(comptime backend: std.builtin.CompilerBackend) type {
49 };62 };
50}63}
5164
52pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) *const Air.Legalize.Features {65pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) ?*const Air.Legalize.Features {
53 const zcu = pt.zcu;66 const zcu = pt.zcu;
54 const target = &zcu.navFileScope(nav_index).mod.?.resolved_target.result;67 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)) {
56 else => unreachable,69 else => unreachable,
57 inline .stage2_llvm,70 inline .stage2_llvm,
58 .stage2_c,71 .stage2_c,
...@@ -65,8 +78,11 @@ pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) *con...@@ -65,8 +78,11 @@ pub fn legalizeFeatures(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) *con
65 .stage2_sparc64,78 .stage2_sparc64,
66 .stage2_spirv64,79 .stage2_spirv64,
67 .stage2_powerpc,80 .stage2_powerpc,
68 => |backend| importBackend(backend).legalizeFeatures(target),81 => |backend| {
69 };82 dev.check(devFeatureForBackend(backend));
83 return importBackend(backend).legalizeFeatures(target);
84 },
85 }
70}86}
7187
72pub fn generateFunction(88pub fn generateFunction(
src/codegen/c.zig+7-3
...@@ -20,8 +20,8 @@ const Alignment = InternPool.Alignment;...@@ -20,8 +20,8 @@ const Alignment = InternPool.Alignment;
20const BigIntLimb = std.math.big.Limb;20const BigIntLimb = std.math.big.Limb;
21const BigInt = std.math.big.int;21const BigInt = std.math.big.int;
2222
23pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {23pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
24 return comptime &.initEmpty();24 return null;
25}25}
2626
27pub const CType = @import("c/Type.zig");27pub const CType = @import("c/Type.zig");
...@@ -210,7 +210,6 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{...@@ -210,7 +210,6 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{
210 .{ "atomic_ushort", {} },210 .{ "atomic_ushort", {} },
211 .{ "atomic_wchar_t", {} },211 .{ "atomic_wchar_t", {} },
212 .{ "auto", {} },212 .{ "auto", {} },
213 .{ "bool", {} },
214 .{ "break", {} },213 .{ "break", {} },
215 .{ "case", {} },214 .{ "case", {} },
216 .{ "char", {} },215 .{ "char", {} },
...@@ -270,6 +269,11 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{...@@ -270,6 +269,11 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{
270 .{ "va_end", {} },269 .{ "va_end", {} },
271 .{ "va_copy", {} },270 .{ "va_copy", {} },
272271
272 // stdbool.h
273 .{ "bool", {} },
274 .{ "false", {} },
275 .{ "true", {} },
276
273 // stddef.h277 // stddef.h
274 .{ "offsetof", {} },278 .{ "offsetof", {} },
275279
src/codegen/llvm.zig+2-2
...@@ -36,8 +36,8 @@ const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev;...@@ -36,8 +36,8 @@ const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev;
3636
37const Error = error{ OutOfMemory, CodegenFail };37const Error = error{ OutOfMemory, CodegenFail };
3838
39pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {39pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
40 return comptime &.initEmpty();40 return null;
41}41}
4242
43fn subArchName(features: std.Target.Cpu.Feature.Set, arch: anytype, mappings: anytype) ?[]const u8 {43fn subArchName(features: std.Target.Cpu.Feature.Set, arch: anytype, mappings: anytype) ?[]const u8 {
src/codegen/spirv.zig+2-2
...@@ -28,8 +28,8 @@ const SpvAssembler = @import("spirv/Assembler.zig");...@@ -28,8 +28,8 @@ const SpvAssembler = @import("spirv/Assembler.zig");
2828
29const InstMap = std.AutoHashMapUnmanaged(Air.Inst.Index, IdRef);29const InstMap = std.AutoHashMapUnmanaged(Air.Inst.Index, IdRef);
3030
31pub inline fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features {31pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features {
32 return comptime &.initEmpty();32 return null;
33}33}
3434
35pub const zig_call_abi_ver = 3;35pub const zig_call_abi_ver = 3;
src/dev.zig+3
...@@ -67,6 +67,7 @@ pub const Env = enum {...@@ -67,6 +67,7 @@ pub const Env = enum {
67 .incremental,67 .incremental,
68 .ast_gen,68 .ast_gen,
69 .sema,69 .sema,
70 .legalize,
70 .llvm_backend,71 .llvm_backend,
71 .c_backend,72 .c_backend,
72 .wasm_backend,73 .wasm_backend,
...@@ -144,6 +145,7 @@ pub const Env = enum {...@@ -144,6 +145,7 @@ pub const Env = enum {
144 .build_command,145 .build_command,
145 .stdio_listen,146 .stdio_listen,
146 .incremental,147 .incremental,
148 .legalize,
147 .x86_64_backend,149 .x86_64_backend,
148 .elf_linker,150 .elf_linker,
149 => true,151 => true,
...@@ -222,6 +224,7 @@ pub const Feature = enum {...@@ -222,6 +224,7 @@ pub const Feature = enum {
222 incremental,224 incremental,
223 ast_gen,225 ast_gen,
224 sema,226 sema,
227 legalize,
225228
226 llvm_backend,229 llvm_backend,
227 c_backend,230 c_backend,