authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-20 02:44:52+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-20 10:23:16+02:00
log999777e73aa1bd06b05cbe53f8c2488877e18874
tree2a12be70cf488ace84478c9f9f583dc50bfa9717
parentd000e53b291afc40da7d676553f1c9685a38f169

compiler: Scaffold stage2_powerpc backend.

Nothing interesting here; literally just the bare minimum so I can work on this on and off in a branch without worrying about merge conflicts in the non-backend code.

19 files changed, 172 insertions(+), 41 deletions(-)

CMakeLists.txt+1
......@@ -543,6 +543,7 @@ set(ZIG_STAGE2_SOURCES
543543 src/arch/arm/Mir.zig
544544 src/arch/arm/abi.zig
545545 src/arch/arm/bits.zig
546 src/arch/powerpc/CodeGen.zig
546547 src/arch/riscv64/abi.zig
547548 src/arch/riscv64/bits.zig
548549 src/arch/riscv64/CodeGen.zig
lib/compiler/test_runner.zig+3-1
......@@ -15,7 +15,9 @@ var fba_buffer: [8192]u8 = undefined;
1515var fba = std.heap.FixedBufferAllocator.init(&fba_buffer);
1616
1717const crippled = switch (builtin.zig_backend) {
18 .stage2_riscv64 => true,
18 .stage2_powerpc,
19 .stage2_riscv64,
20 => true,
1921 else => false,
2022};
2123
lib/std/builtin.zig+9-4
......@@ -1110,6 +1110,9 @@ pub const CompilerBackend = enum(u64) {
11101110 /// The reference implementation self-hosted compiler of Zig, using the
11111111 /// spirv backend.
11121112 stage2_spirv64 = 11,
1113 /// The reference implementation self-hosted compiler of Zig, using the
1114 /// powerpc backend.
1115 stage2_powerpc = 12,
11131116
11141117 _,
11151118};
......@@ -1143,10 +1146,12 @@ pub const panic: type = p: {
11431146 if (@hasDecl(root, "Panic")) {
11441147 break :p root.Panic; // Deprecated; use `panic` instead.
11451148 }
1146 if (builtin.zig_backend == .stage2_riscv64) {
1147 break :p std.debug.simple_panic;
1148 }
1149 break :p std.debug.FullPanic(std.debug.defaultPanic);
1149 break :p switch (builtin.zig_backend) {
1150 .stage2_powerpc,
1151 .stage2_riscv64,
1152 => std.debug.simple_panic,
1153 else => std.debug.FullPanic(std.debug.defaultPanic),
1154 };
11501155};
11511156
11521157pub noinline fn returnError() void {
lib/std/debug.zig+14-9
......@@ -608,15 +608,20 @@ pub fn defaultPanic(
608608
609609 // For backends that cannot handle the language features depended on by the
610610 // default panic handler, we have a simpler panic handler:
611 if (builtin.zig_backend == .stage2_wasm or
612 builtin.zig_backend == .stage2_arm or
613 builtin.zig_backend == .stage2_aarch64 or
614 builtin.zig_backend == .stage2_x86 or
615 (builtin.zig_backend == .stage2_x86_64 and (builtin.target.ofmt != .elf and builtin.target.ofmt != .macho)) or
616 builtin.zig_backend == .stage2_sparc64 or
617 builtin.zig_backend == .stage2_spirv64)
618 {
619 @trap();
611 switch (builtin.zig_backend) {
612 .stage2_aarch64,
613 .stage2_arm,
614 .stage2_powerpc,
615 .stage2_riscv64,
616 .stage2_spirv64,
617 .stage2_wasm,
618 .stage2_x86,
619 => @trap(),
620 .stage2_x86_64 => switch (builtin.target.ofmt) {
621 .elf, .macho => {},
622 else => @trap(),
623 },
624 else => {},
620625 }
621626
622627 switch (builtin.os.tag) {
lib/std/mem.zig+4-2
......@@ -675,10 +675,12 @@ test lessThan {
675675}
676676
677677const eqlBytes_allowed = switch (builtin.zig_backend) {
678 // These backends don't support vectors yet.
679 .stage2_powerpc,
680 .stage2_riscv64,
681 => false,
678682 // The SPIR-V backend does not support the optimized path yet.
679683 .stage2_spirv64 => false,
680 // The RISC-V does not support vectors.
681 .stage2_riscv64 => false,
682684 // The naive memory comparison implementation is more useful for fuzzers to
683685 // find interesting inputs.
684686 else => !builtin.fuzz,
lib/std/os/linux.zig+6-1
......@@ -505,7 +505,12 @@ pub var elf_aux_maybe: ?[*]std.elf.Auxv = null;
505505/// Whether an external or internal getauxval implementation is used.
506506const extern_getauxval = switch (builtin.zig_backend) {
507507 // Calling extern functions is not yet supported with these backends
508 .stage2_aarch64, .stage2_arm, .stage2_riscv64, .stage2_sparc64 => false,
508 .stage2_aarch64,
509 .stage2_arm,
510 .stage2_powerpc,
511 .stage2_riscv64,
512 .stage2_sparc64,
513 => false,
509514 else => !builtin.link_libc,
510515};
511516
lib/std/start.zig+18-9
......@@ -14,12 +14,16 @@ const start_sym_name = if (native_arch.isMIPS()) "__start" else "_start";
1414// The self-hosted compiler is not fully capable of handling all of this start.zig file.
1515// Until then, we have simplified logic here for self-hosted. TODO remove this once
1616// self-hosted is capable enough to handle all of the real start.zig logic.
17pub const simplified_logic =
18 builtin.zig_backend == .stage2_x86 or
19 builtin.zig_backend == .stage2_aarch64 or
20 builtin.zig_backend == .stage2_arm or
21 builtin.zig_backend == .stage2_sparc64 or
22 builtin.zig_backend == .stage2_spirv64;
17pub const simplified_logic = switch (builtin.zig_backend) {
18 .stage2_aarch64,
19 .stage2_arm,
20 .stage2_powerpc,
21 .stage2_sparc64,
22 .stage2_spirv64,
23 .stage2_x86,
24 => true,
25 else => false,
26};
2327
2428comptime {
2529 // No matter what, we import the root file, so that any export, test, comptime
......@@ -669,9 +673,14 @@ pub inline fn callMain() u8 {
669673 if (@typeInfo(ReturnType) != .error_union) @compileError(bad_main_ret);
670674
671675 const result = root.main() catch |err| {
672 if (builtin.zig_backend == .stage2_riscv64) {
673 std.debug.print("error: failed with error\n", .{});
674 return 1;
676 switch (builtin.zig_backend) {
677 .stage2_powerpc,
678 .stage2_riscv64,
679 => {
680 std.debug.print("error: failed with error\n", .{});
681 return 1;
682 },
683 else => {},
675684 }
676685 std.log.err("{s}", .{@errorName(err)});
677686 if (@errorReturnTrace()) |trace| {
lib/std/testing.zig+7-1
......@@ -32,7 +32,13 @@ pub var allocator_instance: std.heap.GeneralPurposeAllocator(.{
3232pub var log_level = std.log.Level.warn;
3333
3434// Disable printing in tests for simple backends.
35pub const backend_can_print = !(builtin.zig_backend == .stage2_spirv64 or builtin.zig_backend == .stage2_riscv64);
35pub const backend_can_print = switch (builtin.zig_backend) {
36 .stage2_powerpc,
37 .stage2_riscv64,
38 .stage2_spirv64,
39 => false,
40 else => true,
41};
3642
3743fn print(comptime fmt: []const u8, args: anytype) void {
3844 if (@inComptime()) {
lib/ubsan_rt.zig+3-1
......@@ -671,7 +671,9 @@ fn exportHandlerWithAbort(
671671}
672672
673673const can_build_ubsan = switch (builtin.zig_backend) {
674 .stage2_riscv64 => false,
674 .stage2_powerpc,
675 .stage2_riscv64,
676 => false,
675677 else => true,
676678};
677679
src/Zcu.zig+20
......@@ -4500,6 +4500,26 @@ pub fn callconvSupported(zcu: *Zcu, cc: std.builtin.CallingConvention) union(enu
45004500 .naked => true,
45014501 else => false,
45024502 },
4503 .stage2_powerpc => switch (target.cpu.arch) {
4504 .powerpc, .powerpcle => switch (cc) {
4505 .powerpc_sysv,
4506 .powerpc_sysv_altivec,
4507 .powerpc_aix,
4508 .powerpc_aix_altivec,
4509 .naked,
4510 => true,
4511 else => false,
4512 },
4513 .powerpc64, .powerpc64le => switch (cc) {
4514 .powerpc64_elf,
4515 .powerpc64_elf_altivec,
4516 .powerpc64_elf_v2,
4517 .naked,
4518 => true,
4519 else => false,
4520 },
4521 else => unreachable,
4522 },
45034523 .stage2_riscv64 => switch (cc) {
45044524 .riscv64_lp64 => |opts| opts.incoming_stack_alignment == null,
45054525 .naked => true,
src/arch/powerpc/CodeGen.zig created+52
......@@ -0,0 +1,52 @@
1const builtin = @import("builtin");
2const std = @import("std");
3
4const Air = @import("../../Air.zig");
5const codegen = @import("../../codegen.zig");
6const InternPool = @import("../../InternPool.zig");
7const link = @import("../../link.zig");
8const Liveness = @import("../../Liveness.zig");
9const Zcu = @import("../../Zcu.zig");
10
11const assert = std.debug.assert;
12const log = std.log.scoped(.codegen);
13
14pub fn generate(
15 bin_file: *link.File,
16 pt: Zcu.PerThread,
17 src_loc: Zcu.LazySrcLoc,
18 func_index: InternPool.Index,
19 air: Air,
20 liveness: Liveness,
21 code: *std.ArrayListUnmanaged(u8),
22 debug_output: link.File.DebugInfoOutput,
23) codegen.CodeGenError!void {
24 _ = bin_file;
25 _ = pt;
26 _ = src_loc;
27 _ = func_index;
28 _ = air;
29 _ = liveness;
30 _ = code;
31 _ = debug_output;
32
33 unreachable;
34}
35
36pub fn generateLazy(
37 bin_file: *link.File,
38 pt: Zcu.PerThread,
39 src_loc: Zcu.LazySrcLoc,
40 lazy_sym: link.File.LazySymbol,
41 code: *std.ArrayListUnmanaged(u8),
42 debug_output: link.File.DebugInfoOutput,
43) codegen.CodeGenError!void {
44 _ = bin_file;
45 _ = pt;
46 _ = src_loc;
47 _ = lazy_sym;
48 _ = code;
49 _ = debug_output;
50
51 unreachable;
52}
src/codegen.zig+6-1
......@@ -37,6 +37,7 @@ fn importBackend(comptime backend: std.builtin.CompilerBackend) type {
3737 return switch (backend) {
3838 .stage2_aarch64 => @import("arch/aarch64/CodeGen.zig"),
3939 .stage2_arm => @import("arch/arm/CodeGen.zig"),
40 .stage2_powerpc => @import("arch/powerpc/CodeGen.zig"),
4041 .stage2_riscv64 => @import("arch/riscv64/CodeGen.zig"),
4142 .stage2_sparc64 => @import("arch/sparc64/CodeGen.zig"),
4243 .stage2_x86_64 => @import("arch/x86_64/CodeGen.zig"),
......@@ -61,6 +62,7 @@ pub fn generateFunction(
6162 else => unreachable,
6263 inline .stage2_aarch64,
6364 .stage2_arm,
65 .stage2_powerpc,
6466 .stage2_riscv64,
6567 .stage2_sparc64,
6668 .stage2_x86_64,
......@@ -86,7 +88,10 @@ pub fn generateLazyFunction(
8688 zcu.getTarget();
8789 switch (target_util.zigBackend(target, false)) {
8890 else => unreachable,
89 inline .stage2_x86_64, .stage2_riscv64 => |backend| {
91 inline .stage2_powerpc,
92 .stage2_riscv64,
93 .stage2_x86_64,
94 => |backend| {
9095 dev.check(devFeatureForBackend(backend));
9196 return importBackend(backend).generateLazy(lf, pt, src_loc, lazy_sym, code, debug_output);
9297 },
src/dev.zig+15
......@@ -26,6 +26,10 @@ pub const Env = enum {
2626 /// - `zig build-* -fincremental -fno-llvm -fno-lld -target x86_64-linux --listen=-`
2727 @"x86_64-linux",
2828
29 /// - sema
30 /// - `zig build-* -fincremental -fno-llvm -fno-lld -target powerpc(64)(le)-linux --listen=-`
31 @"powerpc-linux",
32
2933 /// - sema
3034 /// - `zig build-* -fno-llvm -fno-lld -target riscv64-linux`
3135 @"riscv64-linux",
......@@ -70,6 +74,7 @@ pub const Env = enum {
7074 .x86_64_backend,
7175 .aarch64_backend,
7276 .x86_backend,
77 .powerpc_backend,
7378 .riscv64_backend,
7479 .sparc64_backend,
7580 .spirv64_backend,
......@@ -144,6 +149,15 @@ pub const Env = enum {
144149 => true,
145150 else => Env.sema.supports(feature),
146151 },
152 .@"powerpc-linux" => switch (feature) {
153 .build_command,
154 .stdio_listen,
155 .incremental,
156 .x86_64_backend,
157 .elf_linker,
158 => true,
159 else => Env.sema.supports(feature),
160 },
147161 .@"riscv64-linux" => switch (feature) {
148162 .riscv64_backend,
149163 .elf_linker,
......@@ -216,6 +230,7 @@ pub const Feature = enum {
216230 x86_64_backend,
217231 aarch64_backend,
218232 x86_backend,
233 powerpc_backend,
219234 riscv64_backend,
220235 sparc64_backend,
221236 spirv64_backend,
src/target.zig+6-4
......@@ -736,6 +736,7 @@ pub fn supportsTailCall(target: std.Target, backend: std.builtin.CompilerBackend
736736
737737pub fn supportsThreads(target: std.Target, backend: std.builtin.CompilerBackend) bool {
738738 return switch (backend) {
739 .stage2_powerpc => true,
739740 .stage2_x86_64 => target.ofmt == .macho or target.ofmt == .elf,
740741 else => true,
741742 };
......@@ -796,14 +797,15 @@ pub fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBacken
796797 if (use_llvm) return .stage2_llvm;
797798 if (target.ofmt == .c) return .stage2_c;
798799 return switch (target.cpu.arch) {
799 .wasm32, .wasm64 => .stage2_wasm,
800 .arm, .armeb, .thumb, .thumbeb => .stage2_arm,
801 .x86_64 => .stage2_x86_64,
802 .x86 => .stage2_x86,
803800 .aarch64, .aarch64_be => .stage2_aarch64,
801 .arm, .armeb, .thumb, .thumbeb => .stage2_arm,
802 .powerpc, .powerpcle, .powerpc64, .powerpc64le => .stage2_powerpc,
804803 .riscv64 => .stage2_riscv64,
805804 .sparc64 => .stage2_sparc64,
806805 .spirv64 => .stage2_spirv64,
806 .wasm32, .wasm64 => .stage2_wasm,
807 .x86 => .stage2_x86,
808 .x86_64 => .stage2_x86_64,
807809 else => .other,
808810 };
809811}
test/cases/compile_errors/@import_zon_bad_type.zig+3-3
......@@ -117,9 +117,9 @@ export fn testMutablePointer() void {
117117// tmp.zig:37:38: note: imported here
118118// neg_inf.zon:1:1: error: expected type '?u8'
119119// tmp.zig:57:28: note: imported here
120// neg_inf.zon:1:1: error: expected type 'tmp.testNonExhaustiveEnum__enum_496'
120// neg_inf.zon:1:1: error: expected type 'tmp.testNonExhaustiveEnum__enum_499'
121121// tmp.zig:62:39: note: imported here
122// neg_inf.zon:1:1: error: expected type 'tmp.testUntaggedUnion__union_498'
122// neg_inf.zon:1:1: error: expected type 'tmp.testUntaggedUnion__union_501'
123123// tmp.zig:67:44: note: imported here
124// neg_inf.zon:1:1: error: expected type 'tmp.testTaggedUnionVoid__union_501'
124// neg_inf.zon:1:1: error: expected type 'tmp.testTaggedUnionVoid__union_504'
125125// tmp.zig:72:50: note: imported here
test/cases/compile_errors/anytype_param_requires_comptime.zig+1-1
......@@ -15,6 +15,6 @@ pub export fn entry() void {
1515// error
1616//
1717// :7:25: error: unable to resolve comptime value
18// :7:25: note: initializer of comptime-only struct 'tmp.S.foo__anon_470.C' must be comptime-known
18// :7:25: note: initializer of comptime-only struct 'tmp.S.foo__anon_473.C' must be comptime-known
1919// :4:16: note: struct requires comptime because of this field
2020// :4:16: note: types are not available at runtime
test/cases/compile_errors/bogus_method_call_on_slice.zig+1-1
......@@ -16,5 +16,5 @@ pub export fn entry2() void {
1616//
1717// :3:6: error: no field or member function named 'copy' in '[]const u8'
1818// :9:8: error: no field or member function named 'bar' in '@TypeOf(.{})'
19// :12:18: error: no field or member function named 'bar' in 'tmp.entry2__struct_474'
19// :12:18: error: no field or member function named 'bar' in 'tmp.entry2__struct_477'
2020// :12:6: note: struct declared here
test/cases/compile_errors/coerce_anon_struct.zig+1-1
......@@ -6,6 +6,6 @@ export fn foo() void {
66
77// error
88//
9// :4:16: error: expected type 'tmp.T', found 'tmp.foo__struct_463'
9// :4:16: error: expected type 'tmp.T', found 'tmp.foo__struct_466'
1010// :3:16: note: struct declared here
1111// :1:11: note: struct declared here
test/cases/compile_errors/redundant_try.zig+2-2
......@@ -44,9 +44,9 @@ comptime {
4444//
4545// :5:23: error: expected error union type, found 'comptime_int'
4646// :10:23: error: expected error union type, found '@TypeOf(.{})'
47// :15:23: error: expected error union type, found 'tmp.test2__struct_500'
47// :15:23: error: expected error union type, found 'tmp.test2__struct_503'
4848// :15:23: note: struct declared here
49// :20:27: error: expected error union type, found 'tmp.test3__struct_502'
49// :20:27: error: expected error union type, found 'tmp.test3__struct_505'
5050// :20:27: note: struct declared here
5151// :25:23: error: expected error union type, found 'struct { comptime *const [5:0]u8 = "hello" }'
5252// :31:13: error: expected error union type, found 'u32'