Skip to content

Commit 462c19e

Browse files
committed
Support SPARC and SPARC64 (experimental)
1 parent 15adb28 commit 462c19e

File tree

10 files changed

+493
-6
lines changed

10 files changed

+493
-6
lines changed

.github/.cspell/project-dictionary.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ldrex
3939
ldrexd
4040
ldsetp
4141
ldxp
42+
leoncasa
4243
lgcc
4344
libunwind
4445
linkall
@@ -49,6 +50,7 @@ lwarx
4950
lwsync
5051
machdep
5152
mcpu
53+
membar
5254
memd
5355
memw
5456
mfcr
@@ -97,6 +99,7 @@ stxp
9799
subarch
98100
swpp
99101
uart
102+
ultrasparc
100103
usart
101104
uwrite
102105
uwriteln

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ jobs:
234234
target: s390x-unknown-linux-gnu
235235
- rust: nightly
236236
target: s390x-unknown-linux-gnu
237+
- rust: nightly
238+
target: sparc-unknown-linux-gnu
239+
flags: -C target-cpu=leon4
240+
- rust: nightly-2024-11-08 # Rust 1.84, LLVM 19 (oldest version we can use asm_experimental_arch on this target)
241+
target: sparc64-unknown-linux-gnu
242+
- rust: nightly
243+
target: sparc64-unknown-linux-gnu
237244
- rust: '1.59' # LLVM 13
238245
target: thumbv7neon-unknown-linux-gnueabihf
239246
- rust: '1.74' # LLVM 17 (oldest version that MaybeUninit register is supported)

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This crate provides a way to soundly perform such operations.
1818

1919
## Platform Support
2020

21-
Currently, x86, x86_64, Arm, AArch64, RISC-V, LoongArch64, MIPS32, MIPS64, PowerPC, s390x, MSP430, Arm64EC, AVR, Hexagon, M68k, and Xtensa are supported.
21+
Currently, x86, x86_64, Arm, AArch64, RISC-V, LoongArch64, MIPS32, MIPS64, PowerPC, s390x, MSP430, Arm64EC, AVR, SPARC, Hexagon, M68k, and Xtensa are supported.
2222

2323
| target_arch | primitives | load/store | swap/CAS |
2424
| -------------------------------- | --------------------------------------------------- |:----------:|:--------:|
@@ -40,6 +40,8 @@ Currently, x86, x86_64, Arm, AArch64, RISC-V, LoongArch64, MIPS32, MIPS64, Power
4040
| arm64ec \[4] | isize,usize,i8,u8,i16,u16,i32,u32,i64,u64,i128,u128 |||
4141
| msp430 \[4] (experimental) | isize,usize,i8,u8,i16,u16 |||
4242
| avr \[4] (experimental) | isize,usize,i8,u8,i16,u16 |||
43+
| sparc \[4] \[7] (experimental) | isize,usize,i8,u8,i16,u16,i32,u32 |||
44+
| sparc64 \[4] (experimental) | isize,usize,i8,u8,i16,u16,i32,u32,i64,u64 |||
4345
| hexagon \[4] (experimental) | isize,usize,i8,u8,i16,u16,i32,u32,i64,u64 |||
4446
| m68k \[4] (experimental) | isize,usize,i8,u8,i16,u16,i32,u32 ||\[1] |
4547
| xtensa \[4] (experimental) | isize,usize,i8,u8,i16,u16,i32,u32 ||\[1] |
@@ -50,6 +52,7 @@ Currently, x86, x86_64, Arm, AArch64, RISC-V, LoongArch64, MIPS32, MIPS64, Power
5052
\[4] Requires nightly due to `#![feature(asm_experimental_arch)]`.<br>
5153
\[5] Requires cmpxchg16b target feature (enabled by default on Apple and Windows (except Windows 7) targets).<br>
5254
\[6] Requires target-cpu pwr8+ (powerpc64le is pwr8 by default).<br>
55+
\[7] Requires CAS instruction support.<br>
5356

5457
Feel free to submit an issue if your target is not supported yet.
5558

build.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() {
3030

3131
if version.minor >= 80 {
3232
println!(
33-
r#"cargo:rustc-check-cfg=cfg(target_feature,values("x87","v8m","fast-serialization","isa-68020"))"#
33+
r#"cargo:rustc-check-cfg=cfg(target_feature,values("x87","v8m","fast-serialization","leoncasa","v9","isa-68020"))"#
3434
);
3535

3636
// Custom cfgs set by build script. Not public API.
@@ -41,7 +41,7 @@ fn main() {
4141
// TODO: handle multi-line target_feature_fallback
4242
// grep -F 'target_feature_fallback("' build.rs | grep -Ev '^ *//' | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/'
4343
println!(
44-
r#"cargo:rustc-check-cfg=cfg(atomic_maybe_uninit_target_feature,values("a","cmpxchg16b","fast-serialization","isa-68020","lse","lse128","lse2","mclass","partword-atomics","quadword-atomics","rcpc","rcpc3","v5te","v6","v7","v8","v8m","x87","zaamo","zabha"))"#
44+
r#"cargo:rustc-check-cfg=cfg(atomic_maybe_uninit_target_feature,values("a","cmpxchg16b","fast-serialization","isa-68020","leoncasa","lse","lse128","lse2","mclass","partword-atomics","quadword-atomics","rcpc","rcpc3","v5te","v6","v7","v8","v8m","v9","x87","zaamo","zabha"))"#
4545
);
4646
}
4747

@@ -107,6 +107,15 @@ fn main() {
107107
println!("cargo:rustc-cfg=atomic_maybe_uninit_unstable_asm_experimental_arch");
108108
}
109109
}
110+
// https://github.com/rust-lang/rust/pull/132472 merged in Rust 1.84 (nightly-2024-11-08).
111+
"sparc" | "sparc64" => {
112+
if version.nightly
113+
&& version.probe(84, 2024, 11, 7)
114+
&& is_allowed_feature("asm_experimental_arch")
115+
{
116+
println!("cargo:rustc-cfg=atomic_maybe_uninit_unstable_asm_experimental_arch");
117+
}
118+
}
110119
_ => {}
111120
}
112121

@@ -351,6 +360,32 @@ fn main() {
351360
// bcr 14,0
352361
target_feature_fallback("fast-serialization", arch9_features);
353362
}
363+
"sparc" => {
364+
let mut leoncasa = false;
365+
let mut v9 = false;
366+
if let Some(cpu) = target_cpu() {
367+
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/Sparc/Sparc.td
368+
match &*cpu {
369+
"myriad2" | "myriad2.1" | "myriad2.2" | "myriad2.3" | "ma2100" | "ma2150"
370+
| "ma2155" | "ma2450" | "ma2455" | "ma2x5x" | "ma2080" | "ma2085"
371+
| "ma2480" | "ma2485" | "ma2x8x" | "gr712rc" | "leon4" | "gr740" => {
372+
leoncasa = true;
373+
}
374+
"v9" | "ultrasparc" | "ultrasparc3" | "niagara" | "niagara2" | "niagara3"
375+
| "niagara4" => v9 = true,
376+
_ => {}
377+
}
378+
} else {
379+
// https://github.com/llvm/llvm-project/pull/109278
380+
// https://github.com/rust-lang/rust/blob/1.82.0/compiler/rustc_target/src/spec/targets/sparc_unknown_linux_gnu.rs#L17
381+
v9 = target_os == "linux" || target_os == "solaris";
382+
}
383+
// As of rustc 1.82, target_feature "leoncasa"/"v9" is not available on rustc side:
384+
// https://github.com/rust-lang/rust/blob/1.82.0/compiler/rustc_target/src/target_features.rs
385+
// (will be added in https://github.com/rust-lang/rust/pull/132552)
386+
target_feature_fallback("leoncasa", leoncasa);
387+
target_feature_fallback("v9", v9);
388+
}
354389
"m68k" => {
355390
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/M68k/M68k.td
356391
// Linux requires M68020+.

src/arch/cfgs/sparc.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
3+
#![allow(missing_docs)]
4+
5+
#[macro_export]
6+
macro_rules! cfg_has_atomic_8 {
7+
($($tt:tt)*) => { $($tt)* };
8+
}
9+
#[macro_export]
10+
macro_rules! cfg_no_atomic_8 {
11+
($($tt:tt)*) => {};
12+
}
13+
#[macro_export]
14+
macro_rules! cfg_has_atomic_16 {
15+
($($tt:tt)*) => { $($tt)* };
16+
}
17+
#[macro_export]
18+
macro_rules! cfg_no_atomic_16 {
19+
($($tt:tt)*) => {};
20+
}
21+
#[macro_export]
22+
macro_rules! cfg_has_atomic_32 {
23+
($($tt:tt)*) => { $($tt)* };
24+
}
25+
#[macro_export]
26+
macro_rules! cfg_no_atomic_32 {
27+
($($tt:tt)*) => {};
28+
}
29+
// TODO: V8+ with 64-bit g/o reg
30+
#[cfg(target_arch = "sparc")]
31+
#[macro_export]
32+
macro_rules! cfg_has_atomic_64 {
33+
($($tt:tt)*) => {};
34+
}
35+
// TODO: V8+ with 64-bit g/o reg
36+
#[cfg(target_arch = "sparc")]
37+
#[macro_export]
38+
macro_rules! cfg_no_atomic_64 {
39+
($($tt:tt)*) => { $($tt)* };
40+
}
41+
#[cfg(target_arch = "sparc64")]
42+
#[macro_export]
43+
macro_rules! cfg_has_atomic_64 {
44+
($($tt:tt)*) => { $($tt)* };
45+
}
46+
#[cfg(target_arch = "sparc64")]
47+
#[macro_export]
48+
macro_rules! cfg_no_atomic_64 {
49+
($($tt:tt)*) => {};
50+
}
51+
#[macro_export]
52+
macro_rules! cfg_has_atomic_128 {
53+
($($tt:tt)*) => {};
54+
}
55+
#[macro_export]
56+
macro_rules! cfg_no_atomic_128 {
57+
($($tt:tt)*) => { $($tt)* };
58+
}
59+
#[macro_export]
60+
macro_rules! cfg_has_atomic_cas {
61+
($($tt:tt)*) => { $($tt)* };
62+
}
63+
#[macro_export]
64+
macro_rules! cfg_no_atomic_cas {
65+
($($tt:tt)*) => {};
66+
}

src/arch/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
target_arch = "powerpc",
4040
target_arch = "powerpc64",
4141
target_arch = "s390x",
42+
all(
43+
target_arch = "sparc",
44+
any(
45+
target_feature = "leoncasa",
46+
atomic_maybe_uninit_target_feature = "leoncasa",
47+
target_feature = "v9",
48+
atomic_maybe_uninit_target_feature = "v9",
49+
),
50+
),
51+
target_arch = "sparc64",
4252
target_arch = "xtensa",
4353
),
4454
atomic_maybe_uninit_unstable_asm_experimental_arch,
@@ -108,6 +118,20 @@ mod riscv;
108118
#[cfg(atomic_maybe_uninit_unstable_asm_experimental_arch)]
109119
#[cfg_attr(atomic_maybe_uninit_s390x_no_reg_addr, path = "s390x_no_reg_addr.rs")]
110120
mod s390x;
121+
#[cfg(any(
122+
all(
123+
target_arch = "sparc",
124+
any(
125+
target_feature = "leoncasa",
126+
atomic_maybe_uninit_target_feature = "leoncasa",
127+
target_feature = "v9",
128+
atomic_maybe_uninit_target_feature = "v9",
129+
),
130+
),
131+
target_arch = "sparc64",
132+
))]
133+
#[cfg(atomic_maybe_uninit_unstable_asm_experimental_arch)]
134+
mod sparc;
111135
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
112136
mod x86;
113137
#[cfg(target_arch = "xtensa")]

0 commit comments

Comments
 (0)