@@ -11,7 +11,6 @@ use syn::{
1111} ;
1212
1313/// Struct to represent a function parameter.
14- #[ cfg( feature = "rt" ) ]
1514struct FunctionParam {
1615 /// Name of the parameter.
1716 param_name : TokenStream2 ,
@@ -21,7 +20,6 @@ struct FunctionParam {
2120
2221/// Configuration parameters of a trap. It is useful to abstract the
2322/// differences between exception handlers and core interrupt handlers.
24- #[ cfg( feature = "rt" ) ]
2523struct TrapConfig {
2624 /// Name of the default handler (e.g., `DefaultHandler` for core interrupts).
2725 default_handler : TokenStream2 ,
@@ -33,7 +31,6 @@ struct TrapConfig {
3331 handlers_array_name : TokenStream2 ,
3432}
3533
36- #[ cfg( feature = "rt" ) ]
3734impl TrapConfig {
3835 /// Vector with all the input parameters expected when declaring extern handler functions
3936 fn extern_signature ( & self ) -> Vec < TokenStream2 > {
@@ -110,7 +107,6 @@ impl PacTrait {
110107 }
111108
112109 /// For Exception or an Interrupt enums, it returns the trap configuration details.
113- #[ cfg( feature = "rt" ) ]
114110 fn trap_config ( & self ) -> Option < TrapConfig > {
115111 match self {
116112 Self :: Exception => Some ( TrapConfig {
@@ -167,7 +163,6 @@ impl InterruptType {
167163 }
168164
169165 /// Returns a token stream representing the name of the array of interrupt service routines
170- #[ cfg( feature = "rt" ) ]
171166 fn isr_array_name ( & self ) -> TokenStream2 {
172167 match self {
173168 Self :: Core => quote ! ( __CORE_INTERRUPTS) ,
@@ -176,7 +171,6 @@ impl InterruptType {
176171 }
177172
178173 /// Returns a token stream representing the name of the interrupt dispatch function
179- #[ cfg( feature = "rt" ) ]
180174 fn dispatch_fn_name ( & self ) -> TokenStream2 {
181175 match self {
182176 Self :: Core => quote ! ( _dispatch_core_interrupt) ,
@@ -245,7 +239,6 @@ impl PacEnumItem {
245239 }
246240
247241 /// Returns a vector of token streams representing the interrupt handler functions
248- #[ cfg( feature = "rt" ) ]
249242 fn handlers ( & self , trap_config : & TrapConfig ) -> Vec < TokenStream2 > {
250243 let signature = trap_config. extern_signature ( ) ;
251244 self . numbers
@@ -259,7 +252,6 @@ impl PacEnumItem {
259252 /// Returns a sorted vector of token streams representing all the elements of the interrupt array.
260253 /// If an interrupt number is not present in the enum, the corresponding element is `None`.
261254 /// Otherwise, it is `Some(<interrupt_handler>)`.
262- #[ cfg( feature = "rt" ) ]
263255 fn handlers_array ( & self ) -> Vec < TokenStream2 > {
264256 let mut vectors = vec ! [ ] ;
265257 for i in 0 ..=self . max_number {
@@ -272,7 +264,6 @@ impl PacEnumItem {
272264 vectors
273265 }
274266
275- #[ cfg( feature = "rt-v-trap" ) ]
276267 fn vector_table ( & self ) -> TokenStream2 {
277268 let align = match std:: env:: var ( "RISCV_MTVEC_ALIGN" ) {
278269 Ok ( x) => x. parse :: < u32 > ( ) . ok ( ) ,
@@ -289,7 +280,7 @@ impl PacEnumItem {
289280 } ;
290281 let mut asm = format ! (
291282 r#"
292- #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
283+ #[cfg(all(feature = "v-trap", any(target_arch = "riscv32", target_arch = "riscv64") ))]
293284core::arch::global_asm!("
294285 .section .trap.vector, \"ax\"
295286 .global _vector_table
@@ -337,6 +328,8 @@ core::arch::global_asm!("
337328 let max_discriminant = self . max_number ;
338329 let valid_matches = self . valid_matches ( ) ;
339330
331+ let is_core_interrupt = matches ! ( attr, PacTrait :: Interrupt ( InterruptType :: Core ) ) ;
332+
340333 // Push the trait implementation
341334 res. push ( quote ! {
342335 unsafe impl riscv:: #trait_name for #name {
@@ -361,51 +354,54 @@ core::arch::global_asm!("
361354 res. push ( quote ! { unsafe impl riscv:: #marker_trait_name for #name { } } ) ;
362355 }
363356
364- #[ cfg( feature = "rt" ) ]
365357 if let Some ( trap_config) = attr. trap_config ( ) {
366- match attr {
367- #[ cfg( feature = "rt-v-trap" ) ]
368- PacTrait :: Interrupt ( InterruptType :: Core ) => {
369- res. push ( self . vector_table ( ) ) ;
358+ let default_handler = & trap_config. default_handler ;
359+ let extern_signature = trap_config. extern_signature ( ) ;
360+ let handler_input = trap_config. handler_input ( ) ;
361+ let array_signature = trap_config. array_signature ( ) ;
362+ let dispatch_fn_name = & trap_config. dispatch_fn_name ;
363+ let dispatch_fn_args = & trap_config. dispatch_fn_signature ( ) ;
364+ let vector_table = & trap_config. handlers_array_name ;
365+
366+ let handlers = self . handlers ( & trap_config) ;
367+ let interrupt_array = self . handlers_array ( ) ;
368+ let cfg_v_trap = match is_core_interrupt {
369+ true => Some ( quote ! ( #[ cfg( not( feature = "v-trap" ) ) ] ) ) ,
370+ false => None ,
371+ } ;
372+
373+ // Push the interrupt handler functions and the interrupt array
374+ res. push ( quote ! {
375+ #cfg_v_trap
376+ extern "C" {
377+ #( #handlers; ) *
370378 }
371- _ => {
372- let default_handler = & trap_config. default_handler ;
373- let extern_signature = trap_config. extern_signature ( ) ;
374- let handler_input = trap_config. handler_input ( ) ;
375- let array_signature = trap_config. array_signature ( ) ;
376- let dispatch_fn_name = & trap_config. dispatch_fn_name ;
377- let dispatch_fn_args = & trap_config. dispatch_fn_signature ( ) ;
378- let vector_table = & trap_config. handlers_array_name ;
379-
380- let handlers = self . handlers ( & trap_config) ;
381- let interrupt_array = self . handlers_array ( ) ;
382-
383- res. push ( quote ! {
384- extern "C" {
385- #( #handlers; ) *
386- }
387-
388- #[ doc( hidden) ]
389- #[ no_mangle]
390- pub static #vector_table: [ Option <unsafe extern "C" fn ( #( #array_signature) , * ) >; #max_discriminant + 1 ] = [
391- #( #interrupt_array) , *
392- ] ;
393-
394- #[ inline]
395- #[ no_mangle]
396- unsafe extern "C" fn #dispatch_fn_name( #( #dispatch_fn_args) , * ) {
397- extern "C" {
398- fn #default_handler( #( #extern_signature) , * ) ;
399- }
400-
401- match #vector_table. get( code) {
402- Some ( Some ( handler) ) => handler( #( #handler_input) , * ) ,
403- _ => #default_handler( #( #handler_input) , * ) ,
404- }
405- }
406- } ) ;
379+
380+ #cfg_v_trap
381+ #[ doc( hidden) ]
382+ #[ no_mangle]
383+ pub static #vector_table: [ Option <unsafe extern "C" fn ( #( #array_signature) , * ) >; #max_discriminant + 1 ] = [
384+ #( #interrupt_array) , *
385+ ] ;
386+
387+ #cfg_v_trap
388+ #[ inline]
389+ #[ no_mangle]
390+ unsafe extern "C" fn #dispatch_fn_name( #( #dispatch_fn_args) , * ) {
391+ extern "C" {
392+ fn #default_handler( #( #extern_signature) , * ) ;
393+ }
394+
395+ match #vector_table. get( code) {
396+ Some ( Some ( handler) ) => handler( #( #handler_input) , * ) ,
397+ _ => #default_handler( #( #handler_input) , * ) ,
398+ }
407399 }
408- }
400+ } ) ;
401+ }
402+
403+ if is_core_interrupt {
404+ res. push ( self . vector_table ( ) ) ;
409405 }
410406
411407 res
@@ -417,8 +413,8 @@ core::arch::global_asm!("
417413/// As these traits are unsafe, the macro must be called with the `unsafe` keyword followed by the trait name.
418414/// In this way, we warn callers that they must comply with the requirements of the trait.
419415///
420- /// The trait name must be one of `ExceptionNumber`, `CoreInterruptNumber `, `ExternalInterruptNumber`,
421- /// `PriorityNumber`, or `HartIdNumber` .
416+ /// The trait name must be one of `ExceptionNumber`, `InterruptNumber `, `PriorityNumber`, or `HartIdNumber`.
417+ /// Marker traits `CoreInterruptNumber` and `ExternalInterruptNumber` cannot be implemented using this macro .
422418///
423419/// # Safety
424420///
0 commit comments