-
Notifications
You must be signed in to change notification settings - Fork 8
Add support for RISC V #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
831d419
7e5505e
cd05eae
75d2e9d
ecfef80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include <setjmp.h> | ||
|
|
||
| #if defined __riscv_float_abi_double | ||
| CEE_SCAPE_FLOAT_ABI_DOUBLE | ||
| #endif | ||
|
|
||
| #if defined __riscv_float_abi_soft | ||
| CEE_SCAPE_FLOAT_ABI_SOFT | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| use std::{fs::File, io::Write, path::PathBuf}; | ||
|
|
||
| fn main() { | ||
| if std::env::var("CARGO_FEATURE_TEST_C_INTEGRATION") | ||
| .ok() | ||
| .is_some() | ||
| { | ||
| cc::Build::new() | ||
| .file("src/test_c_integration.c") | ||
| .compile("test_c_integration"); | ||
| } else { | ||
| panic!("did not see it"); | ||
| } | ||
|
|
||
| if std::env::var("CARGO_FEATURE_USE_C_TO_INTERFACE_WITH_SETJMP") | ||
| .ok() | ||
| .is_some() | ||
| { | ||
| cc::Build::new() | ||
| .file("src/interop_via_c.c") | ||
| .compile("interop_via_c"); | ||
| } else { | ||
| } | ||
|
|
||
| if cfg!(target_arch = "riscv64") { | ||
| generate_riscv64_consts(); | ||
| } | ||
| } | ||
|
|
||
| fn generate_riscv64_consts() { | ||
| println!("cargo:rerun-if-changed=build/get_riscv64_consts.c"); | ||
|
|
||
| let expanded = cc::Build::new().file("build/get_riscv64_consts.c").expand(); | ||
| let expanded = String::from_utf8(expanded).unwrap(); | ||
|
|
||
| let mut float_abi_double = false; | ||
| let mut float_abi_soft = false; | ||
| for line in expanded.lines() { | ||
| match line.trim() { | ||
| "CEE_SCAPE_FLOAT_ABI_DOUBLE" => float_abi_double = true, | ||
| "CEE_SCAPE_FLOAT_ABI_SOFT" => float_abi_soft = true, | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| let out_dir: PathBuf = std::env::var("OUT_DIR") | ||
| .expect("OUT_DIR env variable should be available") | ||
| .into(); | ||
| println!("cargo::rustc-env=OUT_DIR={}", out_dir.display()); | ||
|
|
||
| let mut riscv64_consts_file = File::create(out_dir.join("riscv64_consts.rs")) | ||
| .expect("unable to create riscv64_consts.rs"); | ||
| writeln!( | ||
| riscv64_consts_file, | ||
| "const FLOAT_ABI_DOUBLE: bool = {float_abi_double};\n\ | ||
| const FLOAT_ABI_SOFT: bool = {float_abi_soft};" | ||
| ) | ||
| .expect("unable to write to riscv64_consts.rs"); | ||
| riscv64_consts_file | ||
| .flush() | ||
| .expect("unable to write to riscv64_consts.rs"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| use core::marker::PhantomData; | ||
|
|
||
| #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] | ||
| const JMP_BUF_SIZE: usize = 8; | ||
|
|
||
| #[cfg(target_arch = "riscv64")] | ||
| const JMP_BUF_SIZE: usize = 14 /* pc + regs + sp */ + crate::riscv64::floating_point_registers(); | ||
|
|
||
| /// `JmpBufFields` are the accessible fields when viewed via a JmpBuf pointer. | ||
| /// But also: You shouldn't be poking at these! | ||
| #[repr(C)] | ||
| pub struct JmpBufFields { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to encode the fields at this level of detail here? I.e. the other
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I was surprised to see that the glibc code was so different between different architectures (I expected to find an array of Said that, the "similarity" with the C code was the only reason behind this. There is no issue whatsoever with alignment or padding (because all the fields perfectly align to 8 bytes), and type punning is totally fine in this case (correct me if I am wrong, but I am pretty confident in this case). Therefore I can use an array of
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I agree with you that just ensuring 8-byte alignment seems like it will handle all possible issues here. and I think it will be easier to maintain over time if we just use an array of so if you can make that change, I'll r+ after that.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, now I only made the |
||
| _buf: [u64; 8], | ||
| _buf: [u64; JMP_BUF_SIZE], | ||
| _neither_send_nor_sync: PhantomData<*const u8>, | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| include!(concat!(env!("OUT_DIR"), "/riscv64_consts.rs")); | ||
|
|
||
| pub const fn floating_point_registers() -> usize { | ||
| if FLOAT_ABI_DOUBLE { | ||
| 12 | ||
| } else if !FLOAT_ABI_SOFT { | ||
| panic!("unsupported number of floating point registers"); | ||
| } else { | ||
| 0 | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just as a heads up, I think this might be inheriting a known bug in other asm code in cee-scape, namely #14
but I'll be satisfied landing this and then fixing all the instances of #14 in one fell swoop (or filing follow-up issues for the cases that I don't initially fix on my own).