@@ -11,6 +11,7 @@ use syn::{
11
11
} ;
12
12
13
13
/// Struct to represent a function parameter.
14
+ #[ cfg( feature = "rt" ) ]
14
15
struct FunctionParam {
15
16
/// Name of the parameter.
16
17
param_name : TokenStream2 ,
@@ -20,6 +21,7 @@ struct FunctionParam {
20
21
21
22
/// Configuration parameters of a trap. It is useful to abstract the
22
23
/// differences between exception handlers and core interrupt handlers.
24
+ #[ cfg( feature = "rt" ) ]
23
25
struct TrapConfig {
24
26
/// Name of the default handler (e.g., `DefaultHandler` for core interrupts).
25
27
default_handler : TokenStream2 ,
@@ -31,6 +33,7 @@ struct TrapConfig {
31
33
handlers_array_name : TokenStream2 ,
32
34
}
33
35
36
+ #[ cfg( feature = "rt" ) ]
34
37
impl TrapConfig {
35
38
/// Vector with all the input parameters expected when declaring extern handler functions
36
39
fn extern_signature ( & self ) -> Vec < TokenStream2 > {
@@ -107,6 +110,7 @@ impl PacTrait {
107
110
}
108
111
109
112
/// For Exception or an Interrupt enums, it returns the trap configuration details.
113
+ #[ cfg( feature = "rt" ) ]
110
114
fn trap_config ( & self ) -> Option < TrapConfig > {
111
115
match self {
112
116
Self :: Exception => Some ( TrapConfig {
@@ -163,6 +167,7 @@ impl InterruptType {
163
167
}
164
168
165
169
/// Returns a token stream representing the name of the array of interrupt service routines
170
+ #[ cfg( feature = "rt" ) ]
166
171
fn isr_array_name ( & self ) -> TokenStream2 {
167
172
match self {
168
173
Self :: Core => quote ! ( __CORE_INTERRUPTS) ,
@@ -171,6 +176,7 @@ impl InterruptType {
171
176
}
172
177
173
178
/// Returns a token stream representing the name of the interrupt dispatch function
179
+ #[ cfg( feature = "rt" ) ]
174
180
fn dispatch_fn_name ( & self ) -> TokenStream2 {
175
181
match self {
176
182
Self :: Core => quote ! ( _dispatch_core_interrupt) ,
@@ -239,6 +245,7 @@ impl PacEnumItem {
239
245
}
240
246
241
247
/// Returns a vector of token streams representing the interrupt handler functions
248
+ #[ cfg( feature = "rt" ) ]
242
249
fn handlers ( & self , trap_config : & TrapConfig ) -> Vec < TokenStream2 > {
243
250
let signature = trap_config. extern_signature ( ) ;
244
251
self . numbers
@@ -252,6 +259,7 @@ impl PacEnumItem {
252
259
/// Returns a sorted vector of token streams representing all the elements of the interrupt array.
253
260
/// If an interrupt number is not present in the enum, the corresponding element is `None`.
254
261
/// Otherwise, it is `Some(<interrupt_handler>)`.
262
+ #[ cfg( feature = "rt" ) ]
255
263
fn handlers_array ( & self ) -> Vec < TokenStream2 > {
256
264
let mut vectors = vec ! [ ] ;
257
265
for i in 0 ..=self . max_number {
@@ -264,6 +272,7 @@ impl PacEnumItem {
264
272
vectors
265
273
}
266
274
275
+ #[ cfg( feature = "rt-v-trap" ) ]
267
276
fn vector_table ( & self ) -> TokenStream2 {
268
277
let align = match std:: env:: var ( "RISCV_MTVEC_ALIGN" ) {
269
278
Ok ( x) => x. parse :: < u32 > ( ) . ok ( ) ,
@@ -280,7 +289,7 @@ impl PacEnumItem {
280
289
} ;
281
290
let mut asm = format ! (
282
291
r#"
283
- #[cfg(all(feature = "v-trap", any(target_arch = "riscv32", target_arch = "riscv64") ))]
292
+ #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
284
293
core::arch::global_asm!("
285
294
.section .trap.vector, \"ax\"
286
295
.global _vector_table
@@ -328,8 +337,6 @@ core::arch::global_asm!("
328
337
let max_discriminant = self . max_number ;
329
338
let valid_matches = self . valid_matches ( ) ;
330
339
331
- let is_core_interrupt = matches ! ( attr, PacTrait :: Interrupt ( InterruptType :: Core ) ) ;
332
-
333
340
// Push the trait implementation
334
341
res. push ( quote ! {
335
342
unsafe impl riscv:: #trait_name for #name {
@@ -354,54 +361,51 @@ core::arch::global_asm!("
354
361
res. push ( quote ! { unsafe impl riscv:: #marker_trait_name for #name { } } ) ;
355
362
}
356
363
364
+ #[ cfg( feature = "rt" ) ]
357
365
if let Some ( trap_config) = attr. trap_config ( ) {
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; ) *
366
+ match attr {
367
+ #[ cfg( feature = "rt-v-trap" ) ]
368
+ PacTrait :: Interrupt ( InterruptType :: Core ) => {
369
+ res. push ( self . vector_table ( ) ) ;
378
370
}
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
- }
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
+ } ) ;
399
407
}
400
- } ) ;
401
- }
402
-
403
- if is_core_interrupt {
404
- res. push ( self . vector_table ( ) ) ;
408
+ }
405
409
}
406
410
407
411
res
@@ -413,8 +417,8 @@ core::arch::global_asm!("
413
417
/// As these traits are unsafe, the macro must be called with the `unsafe` keyword followed by the trait name.
414
418
/// In this way, we warn callers that they must comply with the requirements of the trait.
415
419
///
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 .
420
+ /// The trait name must be one of `ExceptionNumber`, `CoreInterruptNumber `, `ExternalInterruptNumber`,
421
+ /// `PriorityNumber`, or `HartIdNumber` .
418
422
///
419
423
/// # Safety
420
424
///
0 commit comments