Closed SteveLauC closed 1 year ago
@SteveLauC Thanks for doing this, this is awesome!
Is m68k 16 bits or 32 bits?
Considering that the Wiki link says:
The 68000 has a 24-bit external address bus and two byte-select signals "replaced" A0. These 24 lines can therefore address 16 MB of physical memory with byte resolution.
Rust should make usize
32-bits rather than 16, so it should be treated as a 32-bit arch.
Should
asm.js
be included? What is the width of this arch?
You can try compiling a program to asm.js that exports a function which returns the result of core::mem::size_of::<usize>()
and call it from JavaScript, printing to the console to find out. You could treat it as unknown for now, though, if you don't want to go through doing all that.
Rust does not have
i386-xxxxxx
toolchains for the OSes we are going to support
Rust could add support at any time for currently unsupported architectures, so we should try to "support" them even though it can't be tested.
Some variants of
Arch
may never be constructed
I will have to think more about that, it should be fine to leave it for now but some testing with QEMU might be a future task to look at.
Should asm.js be included? What is the width of this arch?
You can try compiling a program to asm.js that exports a function which returns the result of
core::mem::size_of::<usize>()
and call it from JavaScript, printing to the console to find out. You could treat it as unknown for now, though, if you don't want to go through doing all that.
I just gave it a try:
// main.rs
fn main() {
println!("The size of <usize>: {} (in bytes)", core::mem::size_of::<usize>());
}
# env
$ emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.26 (8eaf19f1c6a9a1b0cd0f9a91657366829e34ae5c)
$ node --version
v14.18.2
$ /usr/bin/node --version
v18.1.0
# Chrome Version:
# Version 107.0.5304.110 (Official Build) unknown (64-bit)
# Test through nodejs
$ cargo b --target asmjs-unknown-emscripten
$ node target/asmjs-unknown-emscripten/debug/rust.js
The size of <usize>: 4 (in bytes)
$ /usr/bin/node target/asmjs-unknown-emscripten/debug/rust.js
The size of <usize>: 4 (in bytes)
# Test through Chrome
$ cat rust.html
<html>
<head>
<script src="rust.js"></script>
</head>
</html>
# Open `rust.html` in Chrome
# Open console to see the output
All the platforms (nodejs14/nodejs18/Chrome) report 4 bytes, seems like we should
add a variant Bits16
to Width
.
Some variants of Arch may never be constructed
I will have to think more about that, it should be fine to leave it for now but some testing with QEMU might be a future task to look at.
Yeah, this is also my concern. We should add some tests for this API. Currently,
it is only tested on x86_64-unknown-linux-gnu
, let me try some other UNIX
platforms through the CI of my repo:
All the platforms (nodejs14/nodejs18/Chrome) report 4 bytes, seems like we should add a variant Bits16 to Width.
4 bytes would be 32 bits, so should correspond to Bits32
All the platforms (nodejs14/nodejs18/Chrome) report 4 bytes, seems like we should
add a variant Bits16 to Width.
4 bytes would be 32 bits, so should correspond to
Bits32
God, this is awkward, sorry about this, I am pretty dizzy now🥲
All the platforms (nodejs14/nodejs18/Chrome) report 4 bytes, seems like we should
add a variant Bits16 to Width. 4 bytes would be 32 bits, so should correspond to
Bits32
God, this is awkward, sorry about this, I am pretty dizzy now
It's all good, no worries!
I just got every suggestion applied, but currently, it is still not fine to get this merged. This PR is tested and worked on:
But on FreeBSD, it does not:
use whoami::{arch, Arch};
fn whoami_test() {
println!("whoami_test:");
println!("std::env::consts::ARCH: {}", ARCH);
let arch = arch();
println!("{:?}", arch);
println!("{:?}", arch.width());
println!();
}
fn main() {
whoami_test();
}
$ uname -m
amd64
$ cargo r -q
whoami_test:
std::env::consts::ARCH: x86_64
Unknown("")
Err(Custom { kind: InvalidData, error: "Calling width() on an unknown arch () is invalid" })
I don't have a FreeBSD VM set up and can't configure one currently, so I have to use cirrus CI to get this tested. I will add some debugging commits until I ascertain what is wrong with the FreeBSD implementation, no API changes, just some dbg!()
statements, so you don't need to check it out (if you are satisfied with the current implementation).
Once this is done, I will ping you to get the final review:)
This is kinda weird, it just worked:
// whoami/unix.rs
pub fn arch() -> Arch {
let mut buf = UtsName::default();
let result = unsafe { uname(&mut buf as *mut UtsName) };
if result == -1 {
return Arch::Unknown("uname(2) failed to execute".to_owned());
}
let arch_str = unsafe { CStr::from_ptr(buf.machine.as_ptr()) }
.to_str()
.unwrap();
println!("DBG: arch_str {}", arch_str);
println!("DBG: arch_str bytes {:?}", arch_str.as_bytes());
Arch::from_str(arch_str)
}
// test/main.rs
fn whoami_test() {
println!("whoami_test:");
println!("std::env::consts::ARCH: {}", ARCH);
let arch = arch();
println!("arch: {:?}", arch);
println!("width: {:?}", arch.width());
println!();
}
fn main() {
whoami_test();
}
$ uname -m
amd64
$ cargo r -q
whoami_test:
std::env::consts::ARCH: x86_64
DBG: CStr "amd64"
DBG: CStr bytes [97, 109, 100, 54, 52]
arch: X86_64
width: Ok(Bits64)
I just squashed commits into a single one, I think it is ready for the final review:)
Changes requested are done and testing passed:)
Changes on README.md/lib.rs/CHANGELOG.md/Cargo.toml
are made to make it ready for release!
What this PR does
Arch
to represent the CPU architecture (constructed byarch()
function)Width
to represent the word widthArch::arch_width(&self) -> Width
to get the width of a specific architectureArchitectures supported by each OS and Rust toolchain:
Linux
aarch64
arm
armv4t
armv5te
armv7
thumbv7neon
armeb
i586
i686
x86_64
mips
mipsel
mips64
mips64el
mipsisa32r6
mipsisa32r6el
powerpc
powerpc64
powerpc64le
sparc
sparc64
hexagon
m68k
riscv32gc
riscv64gc
s390x
macOS
aarch64
i686
x86_64
FreeBSD
aarch64
armv6
armv7
i686
x86_64
powerpc
powerpc64
powerpc64le
riscv64gc
DragonflyBSD
NetBSD
aarch64
armv6
armv7
i686
x86_64
powerpc
sparc64
OpenBSD
aarch64
i686
x86_64
powerpc
powerpc64
riscv64gc
sparc64
Windows
i586
i686
x86_64
aarch64
thumbv7a
Wasm
Known Problems
Is
m68k
16 bits or 32 bits?In my impl, I treat it as a 32-bit arch.
This chip is 32 bits internally and 16 bits externally.
Ref:
Should
asm.js
be included? What is the width of this arch?Rust does not have
i386-xxxxxx
toolchains for the OSes we are going to supportBut this arch is still added in my impl as
uname -m
could return this value on LinuxSome variants of
Arch
may never be constructed, for example:I am not sure if
uname -m
will return such detailed architectures, for example, for archMipsIsa32R6
,uname -m
may just returnmips
? If so, thenMipsIsa32R6El
is never used.