Skip to content

Commit

Permalink
fix: avoid using naked_asm!
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Oct 20, 2024
1 parent 4d4608a commit 12de573
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ documentation = "https://docs.rs/qingke"
homepage = "https://github.com/ch32-rs/qingke"
categories = ["embedded", "no-std", "hardware-support"]
license = "MIT/Apache-2.0"
version = "0.3.0" # for rt and macros
version = "0.4.0" # for rt and macros
edition = "2021"

[package]
Expand All @@ -27,8 +27,8 @@ readme = "README.md"

[dependencies]
bit_field = "0.10.2"
riscv = "0.11.1"
critical-section = { version = "1.1.3", features = [
riscv = "0.12.0"
critical-section = { version = "1.2.0", features = [
"restore-state-bool",
], optional = true }
defmt = { version = "0.3.8", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

set -ex

cargo publish --target riscv32imac-unknown-none-elf
cargo publish --target riscv32imac-unknown-none-elf --allow-dirty

(cd qingke-rt/macros && cargo publish --target riscv32imac-unknown-none-elf)
(cd qingke-rt/macros && cargo publish --target riscv32imac-unknown-none-elf --allow-dirty)

(cd qingke-rt && cargo publish --target riscv32imac-unknown-none-elf)
(cd qingke-rt && cargo publish --target riscv32imac-unknown-none-elf --allow-dirty)
4 changes: 2 additions & 2 deletions qingke-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ v4 = []
highcode = []

[dependencies]
qingke-rt-macros = { path = "./macros", version = "0.3" }
qingke = { path = "../", version = "0.3", features = ["critical-section-impl"] }
qingke-rt-macros = { path = "./macros", version = "0.4" }
qingke = { path = "../", version = "0.4", features = ["critical-section-impl"] }

[package.metadata.docs.rs]
targets = ["riscv32imac-unknown-none-elf"]
42 changes: 26 additions & 16 deletions qingke-rt/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
}

let ident = f.sig.ident.clone();
let ident_s = ident.to_string();
let interrupt = ident.to_string();

let valid_signature = f.sig.constness.is_none()
&& f.vis == Visibility::Inherited
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
let ident = f.sig.ident.clone();

// This will be overwritten by `export_name` in linking process, i.e. name of the interrupt
let wrapper_ident = Ident::new(&format!("{}_naked_wrapper", f.sig.ident), Span::call_site());
// let wrapper_ident = Ident::new(&format!("{}_naked_wrapper", f.sig.ident), Span::call_site());

f.sig.ident = Ident::new(&format!("__qingke_rt_{}", f.sig.ident), Span::call_site());

Expand Down Expand Up @@ -221,22 +221,32 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
*/
}

let wrapped_name = wrapped_ident.to_string();

let start_interrupt = format!(
r#"
core::arch::global_asm!(
".section .trap, \"ax\"
.align 2
.global {interrupt}
{interrupt}:
addi sp, sp, -4
sw ra, 0(sp)
jal {wrapped_name}
lw ra, 0(sp)
addi sp, sp, 4
mret
");"#
);

let start_interrupt_asm: proc_macro2::TokenStream = start_interrupt.parse().unwrap();

quote!(
#[doc(hidden)]
#[export_name = #ident_s]
#[naked]
unsafe extern "C" fn #wrapper_ident() {
core::arch::naked_asm!("
addi sp, sp, -4
sw ra, 0(sp)
jal {irq_impl}
lw ra, 0(sp)
addi sp, sp, 4
mret",
irq_impl = sym #wrapped_ident
);
}
#start_interrupt_asm

#[allow(non_snake_case)]
#[no_mangle]
#[link_section = ".trap.rust"]
#f
)
.into()
Expand Down

0 comments on commit 12de573

Please sign in to comment.