Skip to content

Commit 0bc8229

Browse files
authored
Rollup merge of #161161 - xobs:proc_macro-support-16-bit-targets, r=petrochenkov
proc_macro: add support for 16-bit targets When building Rust for 16-bit targets, the `proc_macro` crate cannot currently be used. There are two places that have guards in place for 32- and 64-bit targets, but no support for 16-bit targets. Add gates to the `proc_macro` crate to support 16-bit targets. This was tested on a MOS6502 target running on a Commodore 64. Disclosure: This issue was found with the assistance of an LLM as part of porting a project to a Commodore 64.
2 parents 98da550 + e4d8f07 commit 0bc8229

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

library/proc_macro/src/bridge/arena.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ use std::mem::MaybeUninit;
99
use std::ops::Range;
1010
use std::{cmp, ptr, slice};
1111

12-
// The arenas start with PAGE-sized chunks, and then each new chunk is twice as
13-
// big as its predecessor, up until we reach HUGE_PAGE-sized chunks, whereupon
14-
// we stop growing. This scales well, from arenas that are barely used up to
15-
// arenas that are used for 100s of MiBs. Note also that the chosen sizes match
16-
// the usual sizes of pages and huge pages on Linux.
17-
const PAGE: usize = 4096;
18-
const HUGE_PAGE: usize = 2 * 1024 * 1024;
19-
2012
/// A minimal arena allocator inspired by `rustc_arena::DroplessArena`.
2113
///
2214
/// This is unfortunately a complete re-implementation rather than a dependency
@@ -44,6 +36,18 @@ impl Arena {
4436
#[inline(never)]
4537
#[cold]
4638
fn grow(&self, additional: usize) {
39+
// The arenas start with PAGE-sized chunks, and then each new chunk is twice as
40+
// big as its predecessor, up until we reach HUGE_PAGE-sized chunks, whereupon
41+
// we stop growing. This scales well, from arenas that are barely used up to
42+
// arenas that are used for 100s of MiBs. Note also that the chosen sizes match
43+
// the usual sizes of pages and huge pages on Linux.
44+
const PAGE: usize = 4096;
45+
const HUGE_PAGE: usize =
46+
cfg_select! {
47+
any(target_pointer_width = "64", target_pointer_width = "32") => 2 * 1024 * 1024,
48+
_ => 8192, // just make it compile for -Zbuild-std
49+
};
50+
4751
let mut chunks = self.chunks.borrow_mut();
4852
let mut new_cap;
4953
if let Some(last_chunk) = chunks.last_mut() {

library/proc_macro/src/bridge/fxhash.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,25 @@ pub(super) struct FxHasher {
2727
hash: usize,
2828
}
2929

30-
#[cfg(target_pointer_width = "32")]
31-
const K: usize = 0x9e3779b9;
32-
#[cfg(target_pointer_width = "64")]
33-
const K: usize = 0x517cc1b727220a95;
34-
3530
impl FxHasher {
3631
#[inline]
3732
fn add_to_hash(&mut self, i: usize) {
33+
const K: usize = cfg_select! {
34+
target_pointer_width = "64" => 0x517cc1b727220a95,
35+
target_pointer_width = "32" => 0x9e3779b9,
36+
_ => 0, // just make it compile for -Zbuild-std
37+
};
3838
self.hash = self.hash.rotate_left(5).bitxor(i).wrapping_mul(K);
3939
}
4040
}
4141

4242
impl Hasher for FxHasher {
4343
#[inline]
4444
fn write(&mut self, mut bytes: &[u8]) {
45-
#[cfg(target_pointer_width = "32")]
46-
let read_usize = |bytes: &[u8]| u32::from_ne_bytes(bytes[..4].try_into().unwrap());
47-
#[cfg(target_pointer_width = "64")]
48-
let read_usize = |bytes: &[u8]| u64::from_ne_bytes(bytes[..8].try_into().unwrap());
49-
5045
let mut hash = FxHasher { hash: self.hash };
5146
assert!(size_of::<usize>() <= 8);
5247
while bytes.len() >= size_of::<usize>() {
53-
hash.add_to_hash(read_usize(bytes) as usize);
48+
hash.add_to_hash(usize::from_ne_bytes(bytes[..size_of::<usize>()].try_into().unwrap()));
5449
bytes = &bytes[size_of::<usize>()..];
5550
}
5651
if (size_of::<usize>() > 4) && (bytes.len() >= 4) {

0 commit comments

Comments
 (0)