authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-08-24 21:23:45+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-08-25 16:15:17+02:00
log5d019abe4ec70373db7a75c2a8a3e2d76939817c
tree4ca5e04afceab7cc8e363d7adfe0436f54a3e82a
parent12686d9b7df8fe4c2663cd8e2136991dc3cf661c

start adding big endian RISC-V support

The big endian RISC-V effort is mostly driven by MIPS (the company) which is pivoting to RISC-V, and presumably needs a big endian variant to fill the niche that big endian MIPS (the ISA) did. GCC already supports these targets, but LLVM support will only appear in 22; this commit just adds the necessary target knowledge and checks on our end.

23 files changed, 138 insertions(+), 64 deletions(-)

lib/compiler/aro/aro/Compilation.zig+1-1
......@@ -927,7 +927,7 @@ fn generateVaListType(comp: *Compilation) !Type {
927927 .ios, .macos, .tvos, .watchos => .char_ptr,
928928 else => .aarch64_va_list,
929929 },
930 .sparc, .wasm32, .wasm64, .bpfel, .bpfeb, .riscv32, .riscv64, .avr, .spirv32, .spirv64 => .void_ptr,
930 .sparc, .wasm32, .wasm64, .bpfel, .bpfeb, .riscv32, .riscv32be, .riscv64, .riscv64be, .avr, .spirv32, .spirv64 => .void_ptr,
931931 .powerpc => switch (comp.target.os.tag) {
932932 .ios, .macos, .tvos, .watchos, .aix => @as(Kind, .char_ptr),
933933 else => return Type{ .specifier = .void }, // unknown
lib/compiler/aro/aro/target.zig+14-1
......@@ -15,6 +15,7 @@ pub fn intMaxType(target: std.Target) Type {
1515 .bpfeb,
1616 .loongarch64,
1717 .riscv64,
18 .riscv64be,
1819 .powerpc64,
1920 .powerpc64le,
2021 .ve,
......@@ -47,6 +48,7 @@ pub fn intPtrType(target: std.Target) Type {
4748 .csky,
4849 .loongarch32,
4950 .riscv32,
51 .riscv32be,
5052 .xcore,
5153 .hexagon,
5254 .m68k,
......@@ -109,6 +111,7 @@ pub fn int64Type(target: std.Target) Type {
109111 .loongarch64,
110112 .ve,
111113 .riscv64,
114 .riscv64be,
112115 .powerpc64,
113116 .powerpc64le,
114117 .bpfel,
......@@ -138,7 +141,7 @@ pub fn defaultFunctionAlignment(target: std.Target) u8 {
138141 .arm, .armeb => 4,
139142 .aarch64, .aarch64_be => 4,
140143 .sparc, .sparc64 => 4,
141 .riscv64 => 2,
144 .riscv64, .riscv64be => 2,
142145 else => 1,
143146 };
144147}
......@@ -330,7 +333,9 @@ pub const FPSemantics = enum {
330333 .armeb,
331334 .hexagon,
332335 .riscv32,
336 .riscv32be,
333337 .riscv64,
338 .riscv64be,
334339 .spirv32,
335340 .spirv64,
336341 => return .IEEEHalf,
......@@ -429,7 +434,9 @@ pub fn ldEmulationOption(target: std.Target, arm_endianness: ?std.builtin.Endian
429434 .powerpc64 => "elf64ppc",
430435 .powerpc64le => "elf64lppc",
431436 .riscv32 => "elf32lriscv",
437 .riscv32be => "elf32briscv",
432438 .riscv64 => "elf64lriscv",
439 .riscv64be => "elf64briscv",
433440 .sparc => "elf32_sparc",
434441 .sparc64 => "elf64_sparc",
435442 .loongarch32 => "elf32loongarch",
......@@ -477,6 +484,7 @@ pub fn get32BitArchVariant(target: std.Target) ?std.Target {
477484 .powerpc,
478485 .powerpcle,
479486 .riscv32,
487 .riscv32be,
480488 .sparc,
481489 .thumb,
482490 .thumbeb,
......@@ -502,6 +510,7 @@ pub fn get32BitArchVariant(target: std.Target) ?std.Target {
502510 .powerpc64 => copy.cpu.arch = .powerpc,
503511 .powerpc64le => copy.cpu.arch = .powerpcle,
504512 .riscv64 => copy.cpu.arch = .riscv32,
513 .riscv64be => copy.cpu.arch = .riscv32be,
505514 .sparc64 => copy.cpu.arch = .sparc,
506515 .x86_64 => copy.cpu.arch = .x86,
507516 }
......@@ -537,6 +546,7 @@ pub fn get64BitArchVariant(target: std.Target) ?std.Target {
537546 .powerpc64,
538547 .powerpc64le,
539548 .riscv64,
549 .riscv64be,
540550 .s390x,
541551 .sparc64,
542552 .ve,
......@@ -552,6 +562,7 @@ pub fn get64BitArchVariant(target: std.Target) ?std.Target {
552562 .powerpc => copy.cpu.arch = .powerpc64,
553563 .powerpcle => copy.cpu.arch = .powerpc64le,
554564 .riscv32 => copy.cpu.arch = .riscv64,
565 .riscv32be => copy.cpu.arch = .riscv64be,
555566 .sparc => copy.cpu.arch = .sparc64,
556567 .spirv32 => copy.cpu.arch = .spirv64,
557568 .thumb => copy.cpu.arch = .aarch64,
......@@ -595,7 +606,9 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
595606 .powerpc64le => "powerpc64le",
596607 .amdgcn => "amdgcn",
597608 .riscv32 => "riscv32",
609 .riscv32be => "riscv32be",
598610 .riscv64 => "riscv64",
611 .riscv64be => "riscv64be",
599612 .sparc => "sparc",
600613 .sparc64 => "sparc64",
601614 .s390x => "s390x",
lib/compiler_rt/clear_cache.zig+1-4
......@@ -39,10 +39,7 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
3939 .mips, .mipsel, .mips64, .mips64el => true,
4040 else => false,
4141 };
42 const riscv = switch (arch) {
43 .riscv32, .riscv64 => true,
44 else => false,
45 };
42 const riscv = arch.isRISCV();
4643 const powerpc64 = switch (arch) {
4744 .powerpc64, .powerpc64le => true,
4845 else => false,
lib/compiler_rt/common.zig+4
......@@ -87,7 +87,9 @@ pub const gnu_f16_abi = switch (builtin.cpu.arch) {
8787 .wasm32,
8888 .wasm64,
8989 .riscv64,
90 .riscv64be,
9091 .riscv32,
92 .riscv32be,
9193 => false,
9294
9395 .x86, .x86_64 => true,
......@@ -124,7 +126,9 @@ pub fn F16T(comptime OtherType: type) type {
124126 .nvptx,
125127 .nvptx64,
126128 .riscv32,
129 .riscv32be,
127130 .riscv64,
131 .riscv64be,
128132 .spirv32,
129133 .spirv64,
130134 => f16,
lib/std/Target.zig+45-11
......@@ -1042,7 +1042,7 @@ pub fn toElfMachine(target: *const Target) std.elf.EM {
10421042 .powerpc, .powerpcle => .PPC,
10431043 .powerpc64, .powerpc64le => .PPC64,
10441044 .propeller => .PROPELLER,
1045 .riscv32, .riscv64 => .RISCV,
1045 .riscv32, .riscv32be, .riscv64, .riscv64be => .RISCV,
10461046 .s390x => .S390,
10471047 .sparc => if (target.cpu.has(.sparc, .v9)) .SPARC32PLUS else .SPARC,
10481048 .sparc64 => .SPARCV9,
......@@ -1099,6 +1099,8 @@ pub fn toCoffMachine(target: *const Target) std.coff.MachineType {
10991099 .powerpcle,
11001100 .powerpc64,
11011101 .powerpc64le,
1102 .riscv32be,
1103 .riscv64be,
11021104 .s390x,
11031105 .sparc,
11041106 .sparc64,
......@@ -1310,7 +1312,9 @@ pub const Cpu = struct {
13101312 powerpc64le,
13111313 propeller,
13121314 riscv32,
1315 riscv32be,
13131316 riscv64,
1317 riscv64be,
13141318 s390x,
13151319 sparc,
13161320 sparc64,
......@@ -1340,6 +1344,7 @@ pub const Cpu = struct {
13401344 // - sparcel
13411345 // - spir
13421346 // - spir64
1347 // - spirv
13431348 // - tce
13441349 // - tcele
13451350
......@@ -1396,7 +1401,7 @@ pub const Cpu = struct {
13961401 .nvptx, .nvptx64 => .nvptx,
13971402 .powerpc, .powerpcle, .powerpc64, .powerpc64le => .powerpc,
13981403 .propeller => .propeller,
1399 .riscv32, .riscv64 => .riscv,
1404 .riscv32, .riscv32be, .riscv64, .riscv64be => .riscv,
14001405 .s390x => .s390x,
14011406 .sparc, .sparc64 => .sparc,
14021407 .spirv32, .spirv64 => .spirv,
......@@ -1452,8 +1457,19 @@ pub const Cpu = struct {
14521457 }
14531458
14541459 pub inline fn isRISCV(arch: Arch) bool {
1460 return arch.isRiscv32() or arch.isRiscv64();
1461 }
1462
1463 pub inline fn isRiscv32(arch: Arch) bool {
1464 return switch (arch) {
1465 .riscv32, .riscv32be => true,
1466 else => false,
1467 };
1468 }
1469
1470 pub inline fn isRiscv64(arch: Arch) bool {
14551471 return switch (arch) {
1456 .riscv32, .riscv64 => true,
1472 .riscv64, .riscv64be => true,
14571473 else => false,
14581474 };
14591475 }
......@@ -1576,6 +1592,8 @@ pub const Cpu = struct {
15761592 .or1k,
15771593 .powerpc,
15781594 .powerpc64,
1595 .riscv32be,
1596 .riscv64be,
15791597 .thumbeb,
15801598 .sparc,
15811599 .sparc64,
......@@ -1688,12 +1706,12 @@ pub const Cpu = struct {
16881706 .riscv64_lp64,
16891707 .riscv64_lp64_v,
16901708 .riscv64_interrupt,
1691 => &.{.riscv64},
1709 => &.{ .riscv64, .riscv64be },
16921710
16931711 .riscv32_ilp32,
16941712 .riscv32_ilp32_v,
16951713 .riscv32_interrupt,
1696 => &.{.riscv32},
1714 => &.{ .riscv32, .riscv32be },
16971715
16981716 .sparc64_sysv,
16991717 => &.{.sparc64},
......@@ -1822,8 +1840,8 @@ pub const Cpu = struct {
18221840 .powerpc, .powerpcle => &powerpc.cpu.ppc,
18231841 .powerpc64, .powerpc64le => &powerpc.cpu.ppc64,
18241842 .propeller => &propeller.cpu.p1,
1825 .riscv32 => &riscv.cpu.generic_rv32,
1826 .riscv64 => &riscv.cpu.generic_rv64,
1843 .riscv32, .riscv32be => &riscv.cpu.generic_rv32,
1844 .riscv64, .riscv64be => &riscv.cpu.generic_rv64,
18271845 .sparc64 => &sparc.cpu.v9, // SPARC can only be 64-bit from v9 and up.
18281846 .wasm32, .wasm64 => &wasm.cpu.mvp,
18291847 .x86 => &x86.cpu.i386,
......@@ -1867,8 +1885,8 @@ pub const Cpu = struct {
18671885 .msp430 => &msp430.cpu.msp430,
18681886 .nvptx, .nvptx64 => &nvptx.cpu.sm_52,
18691887 .powerpc64le => &powerpc.cpu.ppc64le,
1870 .riscv32 => &riscv.cpu.baseline_rv32,
1871 .riscv64 => &riscv.cpu.baseline_rv64,
1888 .riscv32, .riscv32be => &riscv.cpu.baseline_rv32,
1889 .riscv64, .riscv64be => &riscv.cpu.baseline_rv64,
18721890 .s390x => &s390x.cpu.arch8, // gcc/clang do not have a generic s390x model.
18731891 .sparc => &sparc.cpu.v9, // glibc does not work with 'plain' v8.
18741892 .x86 => &x86.cpu.pentium4,
......@@ -2615,6 +2633,7 @@ pub fn ptrBitWidth_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) u16 {
26152633 .powerpc,
26162634 .powerpcle,
26172635 .riscv32,
2636 .riscv32be,
26182637 .thumb,
26192638 .thumbeb,
26202639 .x86,
......@@ -2637,6 +2656,7 @@ pub fn ptrBitWidth_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) u16 {
26372656 .powerpc64,
26382657 .powerpc64le,
26392658 .riscv64,
2659 .riscv64be,
26402660 .x86_64,
26412661 .nvptx64,
26422662 .wasm64,
......@@ -2691,7 +2711,9 @@ pub fn stackAlignment(target: *const Target) u16 {
26912711 .powerpc64le,
26922712 => if (target.os.tag == .linux or target.os.tag == .aix) return 16,
26932713 .riscv32,
2714 .riscv32be,
26942715 .riscv64,
2716 .riscv64be,
26952717 => if (!target.cpu.has(.riscv, .e)) return 16,
26962718 .x86 => if (target.os.tag != .windows and target.os.tag != .uefi) return 16,
26972719 .x86_64 => return 16,
......@@ -2724,7 +2746,9 @@ pub fn cCharSignedness(target: *const Target) std.builtin.Signedness {
27242746 .powerpc64le,
27252747 .s390x,
27262748 .riscv32,
2749 .riscv32be,
27272750 .riscv64,
2751 .riscv64be,
27282752 .xcore,
27292753 .xtensa,
27302754 => .unsigned,
......@@ -2838,7 +2862,9 @@ pub fn cTypeBitSize(target: *const Target, c_type: CType) u16 {
28382862 },
28392863
28402864 .riscv32,
2865 .riscv32be,
28412866 .riscv64,
2867 .riscv64be,
28422868 .aarch64,
28432869 .aarch64_be,
28442870 .s390x,
......@@ -2957,7 +2983,9 @@ pub fn cTypeBitSize(target: *const Target, c_type: CType) u16 {
29572983 },
29582984
29592985 .riscv32,
2986 .riscv32be,
29602987 .riscv64,
2988 .riscv64be,
29612989 .aarch64,
29622990 .aarch64_be,
29632991 .s390x,
......@@ -3169,7 +3197,9 @@ pub fn cTypeAlignment(target: *const Target, c_type: CType) u16 {
31693197 .powerpc64,
31703198 .powerpc64le,
31713199 .riscv32,
3200 .riscv32be,
31723201 .riscv64,
3202 .riscv64be,
31733203 .sparc64,
31743204 .spirv32,
31753205 .spirv64,
......@@ -3261,7 +3291,9 @@ pub fn cTypePreferredAlignment(target: *const Target, c_type: CType) u16 {
32613291 .powerpc64,
32623292 .powerpc64le,
32633293 .riscv32,
3294 .riscv32be,
32643295 .riscv64,
3296 .riscv64be,
32653297 .sparc64,
32663298 .spirv32,
32673299 .spirv64,
......@@ -3300,6 +3332,7 @@ pub fn cMaxIntAlignment(target: *const Target) u16 {
33003332 .powerpc,
33013333 .powerpcle,
33023334 .riscv32,
3335 .riscv32be,
33033336 .s390x,
33043337 => 8,
33053338
......@@ -3315,6 +3348,7 @@ pub fn cMaxIntAlignment(target: *const Target) u16 {
33153348 .powerpc64,
33163349 .powerpc64le,
33173350 .riscv64,
3351 .riscv64be,
33183352 .sparc,
33193353 .sparc64,
33203354 .wasm32,
......@@ -3364,8 +3398,8 @@ pub fn cCallingConvention(target: *const Target) ?std.builtin.CallingConvention
33643398 else => .{ .mips64_n64 = .{} },
33653399 },
33663400 .mips, .mipsel => .{ .mips_o32 = .{} },
3367 .riscv64 => .{ .riscv64_lp64 = .{} },
3368 .riscv32 => .{ .riscv32_ilp32 = .{} },
3401 .riscv64, .riscv64be => .{ .riscv64_lp64 = .{} },
3402 .riscv32, .riscv32be => .{ .riscv32_ilp32 = .{} },
33693403 .sparc64 => .{ .sparc64_sysv = .{} },
33703404 .sparc => .{ .sparc_sysv = .{} },
33713405 .powerpc64 => if (target.abi.isMusl())
lib/std/atomic.zig+2
......@@ -386,7 +386,9 @@ pub inline fn spinLoopHint() void {
386386 => asm volatile ("pause(#1)"),
387387
388388 .riscv32,
389 .riscv32be,
389390 .riscv64,
391 .riscv64be,
390392 => if (comptime builtin.cpu.has(.riscv, .zihintpause)) {
391393 asm volatile ("pause");
392394 },
lib/std/builtin.zig+1-1
......@@ -909,7 +909,7 @@ pub const VaList = switch (builtin.cpu.arch) {
909909 .hexagon => if (builtin.target.abi.isMusl()) VaListHexagon else *u8,
910910 .loongarch32, .loongarch64 => *anyopaque,
911911 .mips, .mipsel, .mips64, .mips64el => *anyopaque,
912 .riscv32, .riscv64 => *anyopaque,
912 .riscv32, .riscv32be, .riscv64, .riscv64be => *anyopaque,
913913 .powerpc, .powerpcle => switch (builtin.os.tag) {
914914 .ios, .macos, .tvos, .watchos, .visionos, .aix => *u8,
915915 else => VaListPowerPc,
lib/std/builtin/assembly.zig+1-1
......@@ -641,7 +641,7 @@ pub const Clobbers = switch (@import("builtin").cpu.arch) {
641641 q14: bool = false,
642642 q15: bool = false,
643643 },
644 .riscv32, .riscv64 => packed struct {
644 .riscv32, .riscv32be, .riscv64, .riscv64be => packed struct {
645645 /// Whether the inline assembly code may perform stores to memory
646646 /// addresses other than those derived from input pointer provenance.
647647 memory: bool = false,
lib/std/debug/Dwarf/abi.zig+1-1
......@@ -20,7 +20,7 @@ pub fn supportsUnwinding(target: *const std.Target) bool {
2020
2121 // Enabling this causes relocation errors such as:
2222 // error: invalid relocation type R_RISCV_SUB32 at offset 0x20
23 .riscv64, .riscv32 => false,
23 .riscv64, .riscv64be, .riscv32, .riscv32be => false,
2424
2525 // Conservative guess. Feel free to update this logic with any targets
2626 // that are known to not support Dwarf unwinding.
lib/std/pie.zig+2-2
......@@ -30,7 +30,7 @@ const R_RELATIVE = switch (builtin.cpu.arch) {
3030 .m68k => R_68K_RELATIVE,
3131 .mips, .mipsel, .mips64, .mips64el => R_MIPS_RELATIVE,
3232 .powerpc, .powerpcle, .powerpc64, .powerpc64le => R_PPC_RELATIVE,
33 .riscv32, .riscv64 => R_RISCV_RELATIVE,
33 .riscv32, .riscv32be, .riscv64, .riscv64be => R_RISCV_RELATIVE,
3434 .s390x => R_390_RELATIVE,
3535 .sparc, .sparc64 => R_SPARC_RELATIVE,
3636 else => @compileError("Missing R_RELATIVE definition for this target"),
......@@ -163,7 +163,7 @@ inline fn getDynamicSymbol() [*]const elf.Dyn {
163163 : [ret] "=r" (-> [*]const elf.Dyn),
164164 :
165165 : .{ .lr = true, .r4 = true }),
166 .riscv32, .riscv64 => asm volatile (
166 .riscv32, .riscv32be, .riscv64, .riscv64be => asm volatile (
167167 \\ .weak _DYNAMIC
168168 \\ .hidden _DYNAMIC
169169 \\ lla %[ret], _DYNAMIC
lib/std/start.zig+2-2
......@@ -203,7 +203,7 @@ fn _start() callconv(.naked) noreturn {
203203 .m68k => ".cfi_undefined %%pc",
204204 .mips, .mipsel, .mips64, .mips64el => ".cfi_undefined $ra",
205205 .powerpc, .powerpcle, .powerpc64, .powerpc64le => ".cfi_undefined lr",
206 .riscv32, .riscv64 => if (builtin.zig_backend == .stage2_riscv64)
206 .riscv32, .riscv32be, .riscv64, .riscv64be => if (builtin.zig_backend == .stage2_riscv64)
207207 ""
208208 else
209209 ".cfi_undefined ra",
......@@ -305,7 +305,7 @@ fn _start() callconv(.naked) noreturn {
305305 \\ bstrins.d $sp, $zero, 3, 0
306306 \\ b %[posixCallMainAndExit]
307307 ,
308 .riscv32, .riscv64 =>
308 .riscv32, .riscv32be, .riscv64, .riscv64be =>
309309 \\ li fp, 0
310310 \\ li ra, 0
311311 \\ mv a0, sp
src/Compilation.zig+1-1
......@@ -7051,7 +7051,7 @@ pub fn addCCArgs(
70517051 // compiler frontend does. Therefore we must hard-code the -m flags for
70527052 // all CPU features here.
70537053 switch (target.cpu.arch) {
7054 .riscv32, .riscv64 => {
7054 .riscv32, .riscv32be, .riscv64, .riscv64be => {
70557055 const RvArchFeat = struct { char: u8, feat: std.Target.riscv.Feature };
70567056 const letters = [_]RvArchFeat{
70577057 .{ .char = 'm', .feat = .m },
src/Zcu.zig+2
......@@ -3850,6 +3850,7 @@ pub fn atomicPtrAlignment(
38503850 .powerpc,
38513851 .powerpcle,
38523852 .riscv32,
3853 .riscv32be,
38533854 .sparc,
38543855 .thumb,
38553856 .thumbeb,
......@@ -3874,6 +3875,7 @@ pub fn atomicPtrAlignment(
38743875 .powerpc64,
38753876 .powerpc64le,
38763877 .riscv64,
3878 .riscv64be,
38773879 .sparc64,
38783880 .s390x,
38793881 .wasm64,
src/codegen/llvm.zig+14-4
......@@ -82,7 +82,9 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8
8282 .powerpc64le => "powerpc64le",
8383 .amdgcn => "amdgcn",
8484 .riscv32 => "riscv32",
85 .riscv32be => "riscv32be",
8586 .riscv64 => "riscv64",
87 .riscv64be => "riscv64be",
8688 .sparc => "sparc",
8789 .sparc64 => "sparc64",
8890 .s390x => "s390x",
......@@ -397,10 +399,18 @@ pub fn dataLayout(target: *const std.Target) []const u8 {
397399 "e-m:e-p:32:32-i64:64-n32-S32"
398400 else
399401 "e-m:e-p:32:32-i64:64-n32-S128",
402 .riscv32be => if (target.cpu.has(.riscv, .e))
403 "E-m:e-p:32:32-i64:64-n32-S32"
404 else
405 "E-m:e-p:32:32-i64:64-n32-S128",
400406 .riscv64 => if (target.cpu.has(.riscv, .e))
401407 "e-m:e-p:64:64-i64:64-i128:128-n32:64-S64"
402408 else
403409 "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128",
410 .riscv64be => if (target.cpu.has(.riscv, .e))
411 "E-m:e-p:64:64-i64:64-i128:128-n32:64-S64"
412 else
413 "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128",
404414 .sparc => "E-m:e-p:32:32-i64:64-i128:128-f128:64-n32-S64",
405415 .sparc64 => "E-m:e-i64:64-i128:128-n32:64-S128",
406416 .s390x => if (target.os.tag == .zos)
......@@ -12224,8 +12234,8 @@ fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType)
1222412234 .integer => return o.builder.intType(@intCast(return_type.bitSize(zcu))),
1222512235 .double_integer => {
1222612236 const integer: Builder.Type = switch (zcu.getTarget().cpu.arch) {
12227 .riscv64 => .i64,
12228 .riscv32 => .i32,
12237 .riscv64, .riscv64be => .i64,
12238 .riscv32, .riscv32be => .i32,
1222912239 else => unreachable,
1223012240 };
1223112241 return o.builder.structType(.normal, &.{ integer, integer });
......@@ -12685,7 +12695,7 @@ fn ccAbiPromoteInt(
1268512695 else => null,
1268612696 },
1268712697 else => switch (target.cpu.arch) {
12688 .loongarch64, .riscv64 => switch (int_info.bits) {
12698 .loongarch64, .riscv64, .riscv64be => switch (int_info.bits) {
1268912699 0...16 => int_info.signedness,
1269012700 32 => .signed, // LLVM always signextends 32 bit ints, unsure if bug.
1269112701 17...31, 33...63 => int_info.signedness,
......@@ -13079,7 +13089,7 @@ pub fn initializeLLVMTarget(arch: std.Target.Cpu.Arch) void {
1307913089 llvm.LLVMInitializePowerPCAsmPrinter();
1308013090 llvm.LLVMInitializePowerPCAsmParser();
1308113091 },
13082 .riscv32, .riscv64 => {
13092 .riscv32, .riscv32be, .riscv64, .riscv64be => {
1308313093 llvm.LLVMInitializeRISCVTarget();
1308413094 llvm.LLVMInitializeRISCVTargetInfo();
1308513095 llvm.LLVMInitializeRISCVTargetMC();
src/link/Elf.zig+8-8
......@@ -3722,8 +3722,8 @@ pub fn tpAddress(self: *Elf) i64 {
37223722 const phdr = self.phdrs.items[index];
37233723 const addr = switch (self.getTarget().cpu.arch) {
37243724 .x86_64 => mem.alignForward(u64, phdr.p_vaddr + phdr.p_memsz, phdr.p_align),
3725 .aarch64 => mem.alignBackward(u64, phdr.p_vaddr - 16, phdr.p_align),
3726 .riscv64 => phdr.p_vaddr,
3725 .aarch64, .aarch64_be => mem.alignBackward(u64, phdr.p_vaddr - 16, phdr.p_align),
3726 .riscv64, .riscv64be => phdr.p_vaddr,
37273727 else => |arch| std.debug.panic("TODO implement getTpAddress for {s}", .{@tagName(arch)}),
37283728 };
37293729 return @intCast(addr);
......@@ -4099,8 +4099,8 @@ pub fn getTarget(self: *const Elf) *const std.Target {
40994099
41004100fn requiresThunks(self: Elf) bool {
41014101 return switch (self.getTarget().cpu.arch) {
4102 .aarch64 => true,
4103 .x86_64, .riscv64 => false,
4102 .aarch64, .aarch64_be => true,
4103 .x86_64, .riscv64, .riscv64be => false,
41044104 else => @panic("TODO unimplemented architecture"),
41054105 };
41064106}
......@@ -4345,8 +4345,8 @@ fn createThunks(elf_file: *Elf, atom_list: *AtomList) !void {
43454345 // A branch will need an extender if its target is larger than
43464346 // `2^(jump_bits - 1) - margin` where margin is some arbitrary number.
43474347 const max_distance = switch (cpu_arch) {
4348 .aarch64 => 0x500_000,
4349 .x86_64, .riscv64 => unreachable,
4348 .aarch64, .aarch64_be => 0x500_000,
4349 .x86_64, .riscv64, .riscv64be => unreachable,
43504350 else => @panic("unhandled arch"),
43514351 };
43524352
......@@ -4392,7 +4392,7 @@ fn createThunks(elf_file: *Elf, atom_list: *AtomList) !void {
43924392 log.debug("atom({f}) {s}", .{ ref, atom_ptr.name(elf_file) });
43934393 for (atom_ptr.relocs(elf_file)) |rel| {
43944394 const is_reachable = switch (cpu_arch) {
4395 .aarch64 => r: {
4395 .aarch64, .aarch64_be => r: {
43964396 const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type());
43974397 if (r_type != .CALL26 and r_type != .JUMP26) break :r true;
43984398 const target_ref = file_ptr.resolveSymbol(rel.r_sym(), elf_file);
......@@ -4406,7 +4406,7 @@ fn createThunks(elf_file: *Elf, atom_list: *AtomList) !void {
44064406 _ = math.cast(i28, taddr + rel.r_addend - saddr) orelse break :r false;
44074407 break :r true;
44084408 },
4409 .x86_64, .riscv64 => unreachable,
4409 .x86_64, .riscv64, .riscv64be => unreachable,
44104410 else => @panic("unsupported arch"),
44114411 };
44124412 if (is_reachable) continue;
src/link/Elf/Atom.zig+6-6
......@@ -346,11 +346,11 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype
346346 error.RelocFailure => has_reloc_errors = true,
347347 else => |e| return e,
348348 },
349 .aarch64 => aarch64.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) {
349 .aarch64, .aarch64_be => aarch64.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) {
350350 error.RelocFailure => has_reloc_errors = true,
351351 else => |e| return e,
352352 },
353 .riscv64 => riscv.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) {
353 .riscv64, .riscv64be => riscv.scanReloc(self, elf_file, rel, symbol, code, &it) catch |err| switch (err) {
354354 error.RelocFailure => has_reloc_errors = true,
355355 else => |e| return e,
356356 },
......@@ -674,7 +674,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
674674 => has_reloc_errors = true,
675675 else => |e| return e,
676676 },
677 .aarch64 => aarch64.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
677 .aarch64, .aarch64_be => aarch64.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
678678 error.RelocFailure,
679679 error.RelaxFailure,
680680 error.UnexpectedRemainder,
......@@ -682,7 +682,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
682682 => has_reloc_errors = true,
683683 else => |e| return e,
684684 },
685 .riscv64 => riscv.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
685 .riscv64, .riscv64be => riscv.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
686686 error.RelocFailure,
687687 error.RelaxFailure,
688688 => has_reloc_errors = true,
......@@ -870,11 +870,11 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
870870 error.RelocFailure => has_reloc_errors = true,
871871 else => |e| return e,
872872 },
873 .aarch64 => aarch64.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
873 .aarch64, .aarch64_be => aarch64.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
874874 error.RelocFailure => has_reloc_errors = true,
875875 else => |e| return e,
876876 },
877 .riscv64 => riscv.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
877 .riscv64, .riscv64be => riscv.resolveRelocNonAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
878878 error.RelocFailure => has_reloc_errors = true,
879879 else => |e| return e,
880880 },
src/link/Elf/Object.zig+3-3
......@@ -189,7 +189,7 @@ pub fn validateEFlags(
189189 e_flags: elf.Word,
190190) !void {
191191 switch (target.cpu.arch) {
192 .riscv64 => {
192 .riscv64, .riscv64be => {
193193 const flags: riscv.Eflags = @bitCast(e_flags);
194194 var any_errors: bool = false;
195195
......@@ -366,7 +366,7 @@ fn initAtoms(
366366 const rel_count: u32 = @intCast(relocs.len);
367367 self.setAtomFields(atom_ptr, .{ .rel_index = rel_index, .rel_count = rel_count });
368368 try self.relocs.appendUnalignedSlice(gpa, relocs);
369 if (target.cpu.arch == .riscv64) {
369 if (target.cpu.arch.isRiscv64()) {
370370 sortRelocs(self.relocs.items[rel_index..][0..rel_count]);
371371 }
372372 }
......@@ -445,7 +445,7 @@ fn parseEhFrame(
445445 // We expect relocations to be sorted by r_offset as per this comment in mold linker:
446446 // https://github.com/rui314/mold/blob/8e4f7b53832d8af4f48a633a8385cbc932d1944e/src/input-files.cc#L653
447447 // Except for RISCV and Loongarch which do not seem to be uphold this convention.
448 if (target.cpu.arch == .riscv64) {
448 if (target.cpu.arch.isRiscv64()) {
449449 sortRelocs(self.relocs.items[rel_start..][0..relocs.len]);
450450 }
451451 const fdes_start = self.fdes.items.len;
src/link/Elf/Thunk.zig+4-4
......@@ -24,8 +24,8 @@ pub fn targetAddress(thunk: Thunk, ref: Elf.Ref, elf_file: *Elf) i64 {
2424
2525pub fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
2626 switch (elf_file.getTarget().cpu.arch) {
27 .aarch64 => try aarch64.write(thunk, elf_file, writer),
28 .x86_64, .riscv64 => unreachable,
27 .aarch64, .aarch64_be => try aarch64.write(thunk, elf_file, writer),
28 .x86_64, .riscv64, .riscv64be => unreachable,
2929 else => @panic("unhandled arch"),
3030 }
3131}
......@@ -59,8 +59,8 @@ pub fn writeSymtab(thunk: Thunk, elf_file: *Elf) void {
5959
6060fn trampolineSize(cpu_arch: std.Target.Cpu.Arch) usize {
6161 return switch (cpu_arch) {
62 .aarch64 => aarch64.trampoline_size,
63 .x86_64, .riscv64 => unreachable,
62 .aarch64, .aarch64_be => aarch64.trampoline_size,
63 .x86_64, .riscv64, .riscv64be => unreachable,
6464 else => @panic("unhandled arch"),
6565 };
6666}
src/link/Elf/eh_frame.zig+2-2
......@@ -286,8 +286,8 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:
286286
287287 switch (cpu_arch) {
288288 .x86_64 => try x86_64.resolveReloc(rec, elf_file, rel, P, S + A, contents[offset..]),
289 .aarch64 => try aarch64.resolveReloc(rec, elf_file, rel, P, S + A, contents[offset..]),
290 .riscv64 => try riscv.resolveReloc(rec, elf_file, rel, P, S + A, contents[offset..]),
289 .aarch64, .aarch64_be => try aarch64.resolveReloc(rec, elf_file, rel, P, S + A, contents[offset..]),
290 .riscv64, .riscv64be => try riscv.resolveReloc(rec, elf_file, rel, P, S + A, contents[offset..]),
291291 else => return error.UnsupportedCpuArch,
292292 }
293293}
src/link/Elf/relocation.zig+10-10
......@@ -76,8 +76,8 @@ const riscv64_relocs = Table(11, elf.R_RISCV, .{
7676pub fn decode(r_type: u32, cpu_arch: std.Target.Cpu.Arch) ?Kind {
7777 return switch (cpu_arch) {
7878 .x86_64 => x86_64_relocs.decode(r_type),
79 .aarch64 => aarch64_relocs.decode(r_type),
80 .riscv64 => riscv64_relocs.decode(r_type),
79 .aarch64, .aarch64_be => aarch64_relocs.decode(r_type),
80 .riscv64, .riscv64be => riscv64_relocs.decode(r_type),
8181 else => @panic("TODO unhandled cpu arch"),
8282 };
8383}
......@@ -85,8 +85,8 @@ pub fn decode(r_type: u32, cpu_arch: std.Target.Cpu.Arch) ?Kind {
8585pub fn encode(comptime kind: Kind, cpu_arch: std.Target.Cpu.Arch) u32 {
8686 return switch (cpu_arch) {
8787 .x86_64 => x86_64_relocs.encode(kind),
88 .aarch64 => aarch64_relocs.encode(kind),
89 .riscv64 => riscv64_relocs.encode(kind),
88 .aarch64, .aarch64_be => aarch64_relocs.encode(kind),
89 .riscv64, .riscv64be => riscv64_relocs.encode(kind),
9090 else => @panic("TODO unhandled cpu arch"),
9191 };
9292}
......@@ -98,11 +98,11 @@ pub const dwarf = struct {
9898 .@"32" => .@"32",
9999 .@"64" => .@"64",
100100 })),
101 .aarch64 => @intFromEnum(@as(elf.R_AARCH64, switch (format) {
101 .aarch64, .aarch64_be => @intFromEnum(@as(elf.R_AARCH64, switch (format) {
102102 .@"32" => .ABS32,
103103 .@"64" => .ABS64,
104104 })),
105 .riscv64 => @intFromEnum(@as(elf.R_RISCV, switch (format) {
105 .riscv64, .riscv64be => @intFromEnum(@as(elf.R_RISCV, switch (format) {
106106 .@"32" => .@"32",
107107 .@"64" => .@"64",
108108 })),
......@@ -125,7 +125,7 @@ pub const dwarf = struct {
125125 },
126126 .debug_frame => .PC32,
127127 })),
128 .aarch64 => @intFromEnum(@as(elf.R_AARCH64, switch (source_section) {
128 .aarch64, .aarch64_be => @intFromEnum(@as(elf.R_AARCH64, switch (source_section) {
129129 else => switch (address_size) {
130130 .@"32" => .ABS32,
131131 .@"64" => .ABS64,
......@@ -133,7 +133,7 @@ pub const dwarf = struct {
133133 },
134134 .debug_frame => .PREL32,
135135 })),
136 .riscv64 => @intFromEnum(@as(elf.R_RISCV, switch (source_section) {
136 .riscv64, .riscv64be => @intFromEnum(@as(elf.R_RISCV, switch (source_section) {
137137 else => switch (address_size) {
138138 .@"32" => .@"32",
139139 .@"64" => .@"64",
......@@ -164,8 +164,8 @@ fn formatRelocType(ctx: FormatRelocTypeCtx, writer: *std.io.Writer) std.io.Write
164164 const r_type = ctx.r_type;
165165 switch (ctx.cpu_arch) {
166166 .x86_64 => try writer.print("R_X86_64_{s}", .{@tagName(@as(elf.R_X86_64, @enumFromInt(r_type)))}),
167 .aarch64 => try writer.print("R_AARCH64_{s}", .{@tagName(@as(elf.R_AARCH64, @enumFromInt(r_type)))}),
168 .riscv64 => try writer.print("R_RISCV_{s}", .{@tagName(@as(elf.R_RISCV, @enumFromInt(r_type)))}),
167 .aarch64, .aarch64_be => try writer.print("R_AARCH64_{s}", .{@tagName(@as(elf.R_AARCH64, @enumFromInt(r_type)))}),
168 .riscv64, .riscv64be => try writer.print("R_RISCV_{s}", .{@tagName(@as(elf.R_RISCV, @enumFromInt(r_type)))}),
169169 else => unreachable,
170170 }
171171}
src/link/Lld.zig+2
......@@ -1342,7 +1342,9 @@ fn getLDMOption(target: *const std.Target) ?[]const u8 {
13421342 .powerpc64 => "elf64ppc",
13431343 .powerpc64le => "elf64lppc",
13441344 .riscv32 => "elf32lriscv",
1345 .riscv32be => "elf32briscv",
13451346 .riscv64 => "elf64lriscv",
1347 .riscv64be => "elf64briscv",
13461348 .s390x => "elf64_s390",
13471349 .sparc64 => "elf64_sparc",
13481350 .x86 => switch (target.os.tag) {
src/target.zig+8-2
......@@ -189,7 +189,9 @@ pub fn hasLlvmSupport(target: *const std.Target, ofmt: std.Target.ObjectFormat)
189189 .powerpc64le,
190190 .amdgcn,
191191 .riscv32,
192 .riscv32be,
192193 .riscv64,
194 .riscv64be,
193195 .sparc,
194196 .sparc64,
195197 .spirv32,
......@@ -486,7 +488,9 @@ pub fn clangSupportsNoImplicitFloatArg(target: *const std.Target) bool {
486488 .thumb,
487489 .thumbeb,
488490 .riscv32,
491 .riscv32be,
489492 .riscv64,
493 .riscv64be,
490494 .x86,
491495 .x86_64,
492496 => true,
......@@ -655,7 +659,7 @@ pub fn llvmMachineAbi(target: *const std.Target) ?[:0]const u8 {
655659 else => if (target.abi.isMusl()) "elfv2" else "elfv1",
656660 },
657661 .powerpc64le => "elfv2",
658 .riscv64 => if (target.cpu.has(.riscv, .e))
662 .riscv64, .riscv64be => if (target.cpu.has(.riscv, .e))
659663 "lp64e"
660664 else if (target.cpu.has(.riscv, .d))
661665 "lp64d"
......@@ -663,7 +667,7 @@ pub fn llvmMachineAbi(target: *const std.Target) ?[:0]const u8 {
663667 "lp64f"
664668 else
665669 "lp64",
666 .riscv32 => if (target.cpu.has(.riscv, .e))
670 .riscv32, .riscv32be => if (target.cpu.has(.riscv, .e))
667671 "ilp32e"
668672 else if (target.cpu.has(.riscv, .d))
669673 "ilp32d"
......@@ -707,7 +711,9 @@ pub fn defaultFunctionAlignment(target: *const std.Target) Alignment {
707711pub fn minFunctionAlignment(target: *const std.Target) Alignment {
708712 return switch (target.cpu.arch) {
709713 .riscv32,
714 .riscv32be,
710715 .riscv64,
716 .riscv64be,
711717 => if (target.cpu.hasAny(.riscv, &.{ .c, .zca })) .@"2" else .@"4",
712718 .thumb,
713719 .thumbeb,
test/llvm_targets.zig+4
......@@ -217,6 +217,8 @@ const targets = [_]std.Target.Query{
217217 .{ .cpu_arch = .riscv32, .os_tag = .rtems, .abi = .none },
218218 // .{ .cpu_arch = .riscv32, .os_tag = .uefi, .abi = .none },
219219
220 // .{ .cpu_arch = .riscv32be, .os_tag = .freestanding, .abi = .none },
221
220222 .{ .cpu_arch = .riscv64, .os_tag = .freebsd, .abi = .none },
221223 .{ .cpu_arch = .riscv64, .os_tag = .freestanding, .abi = .none },
222224 .{ .cpu_arch = .riscv64, .os_tag = .fuchsia, .abi = .none },
......@@ -231,6 +233,8 @@ const targets = [_]std.Target.Query{
231233 .{ .cpu_arch = .riscv64, .os_tag = .serenity, .abi = .none },
232234 // .{ .cpu_arch = .riscv64, .os_tag = .uefi, .abi = .none },
233235
236 // .{ .cpu_arch = .riscv64, .os_tag = .freestanding, .abi = .none },
237
234238 .{ .cpu_arch = .s390x, .os_tag = .freestanding, .abi = .none },
235239 .{ .cpu_arch = .s390x, .os_tag = .linux, .abi = .gnu },
236240 .{ .cpu_arch = .s390x, .os_tag = .linux, .abi = .none },