Skip to content

Commit 2365392

Browse files
authored
Merge pull request #24536 from jacobly0/aarch64
aarch64: add new from scratch self-hosted backend
2 parents 5c57657 + e9b9a27 commit 2365392

File tree

168 files changed

+28242
-3737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+28242
-3737
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ set(ZIG_STAGE2_SOURCES
550550
src/clang_options.zig
551551
src/clang_options_data.zig
552552
src/codegen.zig
553+
src/codegen/aarch64.zig
554+
src/codegen/aarch64/abi.zig
555+
src/codegen/aarch64/Assemble.zig
556+
src/codegen/aarch64/Disassemble.zig
557+
src/codegen/aarch64/encoding.zig
558+
src/codegen/aarch64/instructions.zon
559+
src/codegen/aarch64/Mir.zig
560+
src/codegen/aarch64/Select.zig
553561
src/codegen/c.zig
554562
src/codegen/c/Type.zig
555563
src/codegen/llvm.zig

lib/compiler/test_runner.zig

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var stdin_buffer: [4096]u8 = undefined;
1616
var stdout_buffer: [4096]u8 = undefined;
1717

1818
const crippled = switch (builtin.zig_backend) {
19+
.stage2_aarch64,
1920
.stage2_powerpc,
2021
.stage2_riscv64,
2122
=> true,
@@ -287,13 +288,14 @@ pub fn log(
287288
/// work-in-progress backends can handle it.
288289
pub fn mainSimple() anyerror!void {
289290
@disableInstrumentation();
290-
// is the backend capable of printing to stderr?
291-
const enable_print = switch (builtin.zig_backend) {
291+
// is the backend capable of calling `std.fs.File.writeAll`?
292+
const enable_write = switch (builtin.zig_backend) {
293+
.stage2_aarch64, .stage2_riscv64 => true,
292294
else => false,
293295
};
294-
// is the backend capable of using std.fmt.format to print a summary at the end?
295-
const print_summary = switch (builtin.zig_backend) {
296-
.stage2_riscv64 => true,
296+
// is the backend capable of calling `std.Io.Writer.print`?
297+
const enable_print = switch (builtin.zig_backend) {
298+
.stage2_aarch64, .stage2_riscv64 => true,
297299
else => false,
298300
};
299301

@@ -302,34 +304,31 @@ pub fn mainSimple() anyerror!void {
302304
var failed: u64 = 0;
303305

304306
// we don't want to bring in File and Writer if the backend doesn't support it
305-
const stderr = if (comptime enable_print) std.fs.File.stderr() else {};
307+
const stdout = if (enable_write) std.fs.File.stdout() else {};
306308

307309
for (builtin.test_functions) |test_fn| {
310+
if (enable_write) {
311+
stdout.writeAll(test_fn.name) catch {};
312+
stdout.writeAll("... ") catch {};
313+
}
308314
if (test_fn.func()) |_| {
309-
if (enable_print) {
310-
stderr.writeAll(test_fn.name) catch {};
311-
stderr.writeAll("... ") catch {};
312-
stderr.writeAll("PASS\n") catch {};
313-
}
315+
if (enable_write) stdout.writeAll("PASS\n") catch {};
314316
} else |err| {
315-
if (enable_print) {
316-
stderr.writeAll(test_fn.name) catch {};
317-
stderr.writeAll("... ") catch {};
318-
}
319317
if (err != error.SkipZigTest) {
320-
if (enable_print) stderr.writeAll("FAIL\n") catch {};
318+
if (enable_write) stdout.writeAll("FAIL\n") catch {};
321319
failed += 1;
322-
if (!enable_print) return err;
320+
if (!enable_write) return err;
323321
continue;
324322
}
325-
if (enable_print) stderr.writeAll("SKIP\n") catch {};
323+
if (enable_write) stdout.writeAll("SKIP\n") catch {};
326324
skipped += 1;
327325
continue;
328326
}
329327
passed += 1;
330328
}
331-
if (enable_print and print_summary) {
332-
stderr.deprecatedWriter().print("{} passed, {} skipped, {} failed\n", .{ passed, skipped, failed }) catch {};
329+
if (enable_print) {
330+
var stdout_writer = stdout.writer(&.{});
331+
stdout_writer.interface.print("{} passed, {} skipped, {} failed\n", .{ passed, skipped, failed }) catch {};
333332
}
334333
if (failed != 0) std.process.exit(1);
335334
}

lib/compiler_rt.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ comptime {
240240
_ = @import("compiler_rt/udivmodti4.zig");
241241

242242
// extra
243-
_ = @import("compiler_rt/os_version_check.zig");
243+
if (builtin.zig_backend != .stage2_aarch64) _ = @import("compiler_rt/os_version_check.zig");
244244
_ = @import("compiler_rt/emutls.zig");
245245
_ = @import("compiler_rt/arm.zig");
246246
_ = @import("compiler_rt/aulldiv.zig");
@@ -249,12 +249,12 @@ comptime {
249249
_ = @import("compiler_rt/hexagon.zig");
250250

251251
if (@import("builtin").object_format != .c) {
252-
_ = @import("compiler_rt/atomics.zig");
252+
if (builtin.zig_backend != .stage2_aarch64) _ = @import("compiler_rt/atomics.zig");
253253
_ = @import("compiler_rt/stack_probe.zig");
254254

255255
// macOS has these functions inside libSystem.
256256
if (builtin.cpu.arch.isAARCH64() and !builtin.os.tag.isDarwin()) {
257-
_ = @import("compiler_rt/aarch64_outline_atomics.zig");
257+
if (builtin.zig_backend != .stage2_aarch64) _ = @import("compiler_rt/aarch64_outline_atomics.zig");
258258
}
259259

260260
_ = @import("compiler_rt/memcpy.zig");

lib/compiler_rt/addo.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const std = @import("std");
2-
const builtin = @import("builtin");
3-
const is_test = builtin.is_test;
42
const common = @import("./common.zig");
53
pub const panic = @import("common.zig").panic;
64

@@ -16,7 +14,7 @@ comptime {
1614
// - addoXi4_generic as default
1715

1816
inline fn addoXi4_generic(comptime ST: type, a: ST, b: ST, overflow: *c_int) ST {
19-
@setRuntimeSafety(builtin.is_test);
17+
@setRuntimeSafety(common.test_safety);
2018
overflow.* = 0;
2119
const sum: ST = a +% b;
2220
// Hackers Delight: section Overflow Detection, subsection Signed Add/Subtract

lib/compiler_rt/addoti4_test.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const addv = @import("addo.zig");
2+
const builtin = @import("builtin");
23
const std = @import("std");
34
const testing = std.testing;
45
const math = std.math;
@@ -23,6 +24,8 @@ fn simple_addoti4(a: i128, b: i128, overflow: *c_int) i128 {
2324
}
2425

2526
test "addoti4" {
27+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
28+
2629
const min: i128 = math.minInt(i128);
2730
const max: i128 = math.maxInt(i128);
2831
var i: i128 = 1;

lib/compiler_rt/clear_cache.zig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
9797
.nbytes = end - start,
9898
.whichcache = 3, // ICACHE | DCACHE
9999
};
100-
asm volatile (
101-
\\ syscall
100+
asm volatile ("syscall"
102101
:
103102
: [_] "{$2}" (165), // nr = SYS_sysarch
104103
[_] "{$4}" (0), // op = MIPS_CACHEFLUSH
@@ -116,11 +115,8 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
116115
} else if (arm64 and !apple) {
117116
// Get Cache Type Info.
118117
// TODO memoize this?
119-
var ctr_el0: u64 = 0;
120-
asm volatile (
121-
\\mrs %[x], ctr_el0
122-
\\
123-
: [x] "=r" (ctr_el0),
118+
const ctr_el0 = asm volatile ("mrs %[ctr_el0], ctr_el0"
119+
: [ctr_el0] "=r" (-> u64),
124120
);
125121
// The DC and IC instructions must use 64-bit registers so we don't use
126122
// uintptr_t in case this runs in an IPL32 environment.
@@ -187,9 +183,7 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
187183
exportIt();
188184
} else if (os == .linux and loongarch) {
189185
// See: https://github.com/llvm/llvm-project/blob/cf54cae26b65fc3201eff7200ffb9b0c9e8f9a13/compiler-rt/lib/builtins/clear_cache.c#L94-L95
190-
asm volatile (
191-
\\ ibar 0
192-
);
186+
asm volatile ("ibar 0");
193187
exportIt();
194188
}
195189

lib/compiler_rt/cmp.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
3-
const is_test = builtin.is_test;
43
const common = @import("common.zig");
54

65
pub const panic = common.panic;

lib/compiler_rt/common.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,14 @@ pub const gnu_f16_abi = switch (builtin.cpu.arch) {
102102

103103
pub const want_sparc_abi = builtin.cpu.arch.isSPARC();
104104

105+
pub const test_safety = switch (builtin.zig_backend) {
106+
.stage2_aarch64 => false,
107+
else => builtin.is_test,
108+
};
109+
105110
// Avoid dragging in the runtime safety mechanisms into this .o file, unless
106111
// we're trying to test compiler-rt.
107-
pub const panic = if (builtin.is_test) std.debug.FullPanic(std.debug.defaultPanic) else std.debug.no_panic;
112+
pub const panic = if (test_safety) std.debug.FullPanic(std.debug.defaultPanic) else std.debug.no_panic;
108113

109114
/// This seems to mostly correspond to `clang::TargetInfo::HasFloat16`.
110115
pub fn F16T(comptime OtherType: type) type {

lib/compiler_rt/comparedf2_test.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const std = @import("std");
66
const builtin = @import("builtin");
7-
const is_test = builtin.is_test;
87

98
const __eqdf2 = @import("./cmpdf2.zig").__eqdf2;
109
const __ledf2 = @import("./cmpdf2.zig").__ledf2;

lib/compiler_rt/comparesf2_test.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const std = @import("std");
66
const builtin = @import("builtin");
7-
const is_test = builtin.is_test;
87

98
const __eqsf2 = @import("./cmpsf2.zig").__eqsf2;
109
const __lesf2 = @import("./cmpsf2.zig").__lesf2;

0 commit comments

Comments
 (0)