aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAlex Rønne Petersen <alex@alexrp.com>2025-08-24 21:23:45 +0200
committerAlex Rønne Petersen <alex@alexrp.com>2025-08-25 16:15:17 +0200
commit5d019abe4ec70373db7a75c2a8a3e2d76939817c (patch)
tree4ca5e04afceab7cc8e363d7adfe0436f54a3e82a /lib/std
parent12686d9b7df8fe4c2663cd8e2136991dc3cf661c (diff)
downloadzig-5d019abe4ec70373db7a75c2a8a3e2d76939817c.tar.gz
zig-5d019abe4ec70373db7a75c2a8a3e2d76939817c.zip
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.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Target.zig56
-rw-r--r--lib/std/atomic.zig2
-rw-r--r--lib/std/builtin.zig2
-rw-r--r--lib/std/builtin/assembly.zig2
-rw-r--r--lib/std/debug/Dwarf/abi.zig2
-rw-r--r--lib/std/pie.zig4
-rw-r--r--lib/std/start.zig4
7 files changed, 54 insertions, 18 deletions
diff --git a/lib/std/Target.zig b/lib/std/Target.zig
index 06a229ebd6..4b65b782b6 100644
--- a/lib/std/Target.zig
+++ b/lib/std/Target.zig
@@ -1042,7 +1042,7 @@ pub fn toElfMachine(target: *const Target) std.elf.EM {
.powerpc, .powerpcle => .PPC,
.powerpc64, .powerpc64le => .PPC64,
.propeller => .PROPELLER,
- .riscv32, .riscv64 => .RISCV,
+ .riscv32, .riscv32be, .riscv64, .riscv64be => .RISCV,
.s390x => .S390,
.sparc => if (target.cpu.has(.sparc, .v9)) .SPARC32PLUS else .SPARC,
.sparc64 => .SPARCV9,
@@ -1099,6 +1099,8 @@ pub fn toCoffMachine(target: *const Target) std.coff.MachineType {
.powerpcle,
.powerpc64,
.powerpc64le,
+ .riscv32be,
+ .riscv64be,
.s390x,
.sparc,
.sparc64,
@@ -1310,7 +1312,9 @@ pub const Cpu = struct {
powerpc64le,
propeller,
riscv32,
+ riscv32be,
riscv64,
+ riscv64be,
s390x,
sparc,
sparc64,
@@ -1340,6 +1344,7 @@ pub const Cpu = struct {
// - sparcel
// - spir
// - spir64
+ // - spirv
// - tce
// - tcele
@@ -1396,7 +1401,7 @@ pub const Cpu = struct {
.nvptx, .nvptx64 => .nvptx,
.powerpc, .powerpcle, .powerpc64, .powerpc64le => .powerpc,
.propeller => .propeller,
- .riscv32, .riscv64 => .riscv,
+ .riscv32, .riscv32be, .riscv64, .riscv64be => .riscv,
.s390x => .s390x,
.sparc, .sparc64 => .sparc,
.spirv32, .spirv64 => .spirv,
@@ -1452,8 +1457,19 @@ pub const Cpu = struct {
}
pub inline fn isRISCV(arch: Arch) bool {
+ return arch.isRiscv32() or arch.isRiscv64();
+ }
+
+ pub inline fn isRiscv32(arch: Arch) bool {
+ return switch (arch) {
+ .riscv32, .riscv32be => true,
+ else => false,
+ };
+ }
+
+ pub inline fn isRiscv64(arch: Arch) bool {
return switch (arch) {
- .riscv32, .riscv64 => true,
+ .riscv64, .riscv64be => true,
else => false,
};
}
@@ -1576,6 +1592,8 @@ pub const Cpu = struct {
.or1k,
.powerpc,
.powerpc64,
+ .riscv32be,
+ .riscv64be,
.thumbeb,
.sparc,
.sparc64,
@@ -1688,12 +1706,12 @@ pub const Cpu = struct {
.riscv64_lp64,
.riscv64_lp64_v,
.riscv64_interrupt,
- => &.{.riscv64},
+ => &.{ .riscv64, .riscv64be },
.riscv32_ilp32,
.riscv32_ilp32_v,
.riscv32_interrupt,
- => &.{.riscv32},
+ => &.{ .riscv32, .riscv32be },
.sparc64_sysv,
=> &.{.sparc64},
@@ -1822,8 +1840,8 @@ pub const Cpu = struct {
.powerpc, .powerpcle => &powerpc.cpu.ppc,
.powerpc64, .powerpc64le => &powerpc.cpu.ppc64,
.propeller => &propeller.cpu.p1,
- .riscv32 => &riscv.cpu.generic_rv32,
- .riscv64 => &riscv.cpu.generic_rv64,
+ .riscv32, .riscv32be => &riscv.cpu.generic_rv32,
+ .riscv64, .riscv64be => &riscv.cpu.generic_rv64,
.sparc64 => &sparc.cpu.v9, // SPARC can only be 64-bit from v9 and up.
.wasm32, .wasm64 => &wasm.cpu.mvp,
.x86 => &x86.cpu.i386,
@@ -1867,8 +1885,8 @@ pub const Cpu = struct {
.msp430 => &msp430.cpu.msp430,
.nvptx, .nvptx64 => &nvptx.cpu.sm_52,
.powerpc64le => &powerpc.cpu.ppc64le,
- .riscv32 => &riscv.cpu.baseline_rv32,
- .riscv64 => &riscv.cpu.baseline_rv64,
+ .riscv32, .riscv32be => &riscv.cpu.baseline_rv32,
+ .riscv64, .riscv64be => &riscv.cpu.baseline_rv64,
.s390x => &s390x.cpu.arch8, // gcc/clang do not have a generic s390x model.
.sparc => &sparc.cpu.v9, // glibc does not work with 'plain' v8.
.x86 => &x86.cpu.pentium4,
@@ -2615,6 +2633,7 @@ pub fn ptrBitWidth_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) u16 {
.powerpc,
.powerpcle,
.riscv32,
+ .riscv32be,
.thumb,
.thumbeb,
.x86,
@@ -2637,6 +2656,7 @@ pub fn ptrBitWidth_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) u16 {
.powerpc64,
.powerpc64le,
.riscv64,
+ .riscv64be,
.x86_64,
.nvptx64,
.wasm64,
@@ -2691,7 +2711,9 @@ pub fn stackAlignment(target: *const Target) u16 {
.powerpc64le,
=> if (target.os.tag == .linux or target.os.tag == .aix) return 16,
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
=> if (!target.cpu.has(.riscv, .e)) return 16,
.x86 => if (target.os.tag != .windows and target.os.tag != .uefi) return 16,
.x86_64 => return 16,
@@ -2724,7 +2746,9 @@ pub fn cCharSignedness(target: *const Target) std.builtin.Signedness {
.powerpc64le,
.s390x,
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
.xcore,
.xtensa,
=> .unsigned,
@@ -2838,7 +2862,9 @@ pub fn cTypeBitSize(target: *const Target, c_type: CType) u16 {
},
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
.aarch64,
.aarch64_be,
.s390x,
@@ -2957,7 +2983,9 @@ pub fn cTypeBitSize(target: *const Target, c_type: CType) u16 {
},
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
.aarch64,
.aarch64_be,
.s390x,
@@ -3169,7 +3197,9 @@ pub fn cTypeAlignment(target: *const Target, c_type: CType) u16 {
.powerpc64,
.powerpc64le,
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
.sparc64,
.spirv32,
.spirv64,
@@ -3261,7 +3291,9 @@ pub fn cTypePreferredAlignment(target: *const Target, c_type: CType) u16 {
.powerpc64,
.powerpc64le,
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
.sparc64,
.spirv32,
.spirv64,
@@ -3300,6 +3332,7 @@ pub fn cMaxIntAlignment(target: *const Target) u16 {
.powerpc,
.powerpcle,
.riscv32,
+ .riscv32be,
.s390x,
=> 8,
@@ -3315,6 +3348,7 @@ pub fn cMaxIntAlignment(target: *const Target) u16 {
.powerpc64,
.powerpc64le,
.riscv64,
+ .riscv64be,
.sparc,
.sparc64,
.wasm32,
@@ -3364,8 +3398,8 @@ pub fn cCallingConvention(target: *const Target) ?std.builtin.CallingConvention
else => .{ .mips64_n64 = .{} },
},
.mips, .mipsel => .{ .mips_o32 = .{} },
- .riscv64 => .{ .riscv64_lp64 = .{} },
- .riscv32 => .{ .riscv32_ilp32 = .{} },
+ .riscv64, .riscv64be => .{ .riscv64_lp64 = .{} },
+ .riscv32, .riscv32be => .{ .riscv32_ilp32 = .{} },
.sparc64 => .{ .sparc64_sysv = .{} },
.sparc => .{ .sparc_sysv = .{} },
.powerpc64 => if (target.abi.isMusl())
diff --git a/lib/std/atomic.zig b/lib/std/atomic.zig
index 194f645975..64926dbc4f 100644
--- a/lib/std/atomic.zig
+++ b/lib/std/atomic.zig
@@ -386,7 +386,9 @@ pub inline fn spinLoopHint() void {
=> asm volatile ("pause(#1)"),
.riscv32,
+ .riscv32be,
.riscv64,
+ .riscv64be,
=> if (comptime builtin.cpu.has(.riscv, .zihintpause)) {
asm volatile ("pause");
},
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig
index f79bc2fe72..7e49c29100 100644
--- a/lib/std/builtin.zig
+++ b/lib/std/builtin.zig
@@ -909,7 +909,7 @@ pub const VaList = switch (builtin.cpu.arch) {
.hexagon => if (builtin.target.abi.isMusl()) VaListHexagon else *u8,
.loongarch32, .loongarch64 => *anyopaque,
.mips, .mipsel, .mips64, .mips64el => *anyopaque,
- .riscv32, .riscv64 => *anyopaque,
+ .riscv32, .riscv32be, .riscv64, .riscv64be => *anyopaque,
.powerpc, .powerpcle => switch (builtin.os.tag) {
.ios, .macos, .tvos, .watchos, .visionos, .aix => *u8,
else => VaListPowerPc,
diff --git a/lib/std/builtin/assembly.zig b/lib/std/builtin/assembly.zig
index 1377e654bd..ba0f7ceb4e 100644
--- a/lib/std/builtin/assembly.zig
+++ b/lib/std/builtin/assembly.zig
@@ -641,7 +641,7 @@ pub const Clobbers = switch (@import("builtin").cpu.arch) {
q14: bool = false,
q15: bool = false,
},
- .riscv32, .riscv64 => packed struct {
+ .riscv32, .riscv32be, .riscv64, .riscv64be => packed struct {
/// Whether the inline assembly code may perform stores to memory
/// addresses other than those derived from input pointer provenance.
memory: bool = false,
diff --git a/lib/std/debug/Dwarf/abi.zig b/lib/std/debug/Dwarf/abi.zig
index b8b644e026..c5e509c7b0 100644
--- a/lib/std/debug/Dwarf/abi.zig
+++ b/lib/std/debug/Dwarf/abi.zig
@@ -20,7 +20,7 @@ pub fn supportsUnwinding(target: *const std.Target) bool {
// Enabling this causes relocation errors such as:
// error: invalid relocation type R_RISCV_SUB32 at offset 0x20
- .riscv64, .riscv32 => false,
+ .riscv64, .riscv64be, .riscv32, .riscv32be => false,
// Conservative guess. Feel free to update this logic with any targets
// that are known to not support Dwarf unwinding.
diff --git a/lib/std/pie.zig b/lib/std/pie.zig
index 09b875ca15..2f730a5bdf 100644
--- a/lib/std/pie.zig
+++ b/lib/std/pie.zig
@@ -30,7 +30,7 @@ const R_RELATIVE = switch (builtin.cpu.arch) {
.m68k => R_68K_RELATIVE,
.mips, .mipsel, .mips64, .mips64el => R_MIPS_RELATIVE,
.powerpc, .powerpcle, .powerpc64, .powerpc64le => R_PPC_RELATIVE,
- .riscv32, .riscv64 => R_RISCV_RELATIVE,
+ .riscv32, .riscv32be, .riscv64, .riscv64be => R_RISCV_RELATIVE,
.s390x => R_390_RELATIVE,
.sparc, .sparc64 => R_SPARC_RELATIVE,
else => @compileError("Missing R_RELATIVE definition for this target"),
@@ -163,7 +163,7 @@ inline fn getDynamicSymbol() [*]const elf.Dyn {
: [ret] "=r" (-> [*]const elf.Dyn),
:
: .{ .lr = true, .r4 = true }),
- .riscv32, .riscv64 => asm volatile (
+ .riscv32, .riscv32be, .riscv64, .riscv64be => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lla %[ret], _DYNAMIC
diff --git a/lib/std/start.zig b/lib/std/start.zig
index c71aee529e..7030616d6d 100644
--- a/lib/std/start.zig
+++ b/lib/std/start.zig
@@ -203,7 +203,7 @@ fn _start() callconv(.naked) noreturn {
.m68k => ".cfi_undefined %%pc",
.mips, .mipsel, .mips64, .mips64el => ".cfi_undefined $ra",
.powerpc, .powerpcle, .powerpc64, .powerpc64le => ".cfi_undefined lr",
- .riscv32, .riscv64 => if (builtin.zig_backend == .stage2_riscv64)
+ .riscv32, .riscv32be, .riscv64, .riscv64be => if (builtin.zig_backend == .stage2_riscv64)
""
else
".cfi_undefined ra",
@@ -305,7 +305,7 @@ fn _start() callconv(.naked) noreturn {
\\ bstrins.d $sp, $zero, 3, 0
\\ b %[posixCallMainAndExit]
,
- .riscv32, .riscv64 =>
+ .riscv32, .riscv32be, .riscv64, .riscv64be =>
\\ li fp, 0
\\ li ra, 0
\\ mv a0, sp