aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
authorGravatar bors[bot] <bors[bot]@users.noreply.github.com> 2019-02-26 22:26:52 +0000
committerGravatar bors[bot] <bors[bot]@users.noreply.github.com> 2019-02-26 22:26:52 +0000
commit6d1d84980a1f3212164dda5a890a90efd3c8583d (patch)
treeee9aa60d3c109b4583299cf6a71277507a4be241 /macros/src
parentbbdc3221f6a76da5784ca0017a0f5ac1ca875597 (diff)
parent8eccef7d9cda8a60594b86d31b656a3680d1ca16 (diff)
downloadrtic-6d1d84980a1f3212164dda5a890a90efd3c8583d.tar.gz
rtic-6d1d84980a1f3212164dda5a890a90efd3c8583d.tar.zst
rtic-6d1d84980a1f3212164dda5a890a90efd3c8583d.zip
Merge #158
158: implement RFC #128: #[interrupt(binds = ..)] r=korken89 a=japaric closes #128 r? @korken89 or @TeXitoi suggestions for tests are welcome! (2 of the 3 tests I added hit bugs in my implementation) Co-authored-by: Jorge Aparicio <jorge@japaric.io>
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/check.rs8
-rw-r--r--macros/src/codegen.rs24
-rw-r--r--macros/src/syntax.rs105
3 files changed, 100 insertions, 37 deletions
diff --git a/macros/src/check.rs b/macros/src/check.rs
index 464e280a..4adc2c17 100644
--- a/macros/src/check.rs
+++ b/macros/src/check.rs
@@ -106,10 +106,12 @@ pub fn app(app: &App) -> parse::Result<()> {
}
// Check that free interrupts are not being used
- for int in app.interrupts.keys() {
- if app.free_interrupts.contains_key(int) {
+ for (handler, interrupt) in &app.interrupts {
+ let name = interrupt.args.binds(handler);
+
+ if app.free_interrupts.contains_key(name) {
return Err(parse::Error::new(
- int.span(),
+ name.span(),
"free interrupts (`extern { .. }`) can't be used as interrupt handlers",
));
}
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs
index 83e50266..1d201c08 100644
--- a/macros/src/codegen.rs
+++ b/macros/src/codegen.rs
@@ -468,7 +468,8 @@ fn post_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::Tok
// the device into compile errors
let device = &app.args.device;
let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
- for (name, exception) in &app.exceptions {
+ for (handler, exception) in &app.exceptions {
+ let name = exception.args.binds(handler);
let priority = exception.args.priority;
exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits))));
exprs.push(quote!(p.SCB.set_priority(
@@ -1082,9 +1083,10 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec<proc_ma
let attrs = &exception.attrs;
let stmts = &exception.stmts;
+ let kind = Kind::Exception(ident.clone());
let prelude = prelude(
ctxt,
- Kind::Exception(ident.clone()),
+ kind.clone(),
&exception.args.resources,
&exception.args.spawn,
&exception.args.schedule,
@@ -1095,7 +1097,7 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec<proc_ma
let module = module(
ctxt,
- Kind::Exception(ident.clone()),
+ kind,
!exception.args.schedule.is_empty(),
!exception.args.spawn.is_empty(),
app,
@@ -1122,7 +1124,7 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec<proc_ma
};
let locals = mk_locals(&exception.statics, false);
- let symbol = ident.to_string();
+ let symbol = exception.args.binds(ident).to_string();
let alias = ctxt.ident_gen.mk_ident(None, false);
let unsafety = &exception.unsafety;
quote!(
@@ -1162,14 +1164,14 @@ fn interrupts(
let mut root = vec![];
let mut scoped = vec![];
- let device = &app.args.device;
for (ident, interrupt) in &app.interrupts {
let attrs = &interrupt.attrs;
let stmts = &interrupt.stmts;
+ let kind = Kind::Interrupt(ident.clone());
let prelude = prelude(
ctxt,
- Kind::Interrupt(ident.clone()),
+ kind.clone(),
&interrupt.args.resources,
&interrupt.args.spawn,
&interrupt.args.schedule,
@@ -1180,7 +1182,7 @@ fn interrupts(
root.push(module(
ctxt,
- Kind::Interrupt(ident.clone()),
+ kind,
!interrupt.args.schedule.is_empty(),
!interrupt.args.spawn.is_empty(),
app,
@@ -1208,7 +1210,7 @@ fn interrupts(
let locals = mk_locals(&interrupt.statics, false);
let alias = ctxt.ident_gen.mk_ident(None, false);
- let symbol = ident.to_string();
+ let symbol = interrupt.args.binds(ident).to_string();
let unsafety = &interrupt.unsafety;
scoped.push(quote!(
// unsafe trampoline to deter end-users from calling this non-reentrant function
@@ -1217,9 +1219,6 @@ fn interrupts(
unsafe fn #alias() {
#[inline(always)]
#unsafety fn interrupt() {
- // check that this interrupt exists
- let _ = #device::interrupt::#ident;
-
#(#locals)*
#baseline_let
@@ -1994,7 +1993,8 @@ fn pre_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::Toke
// the device into compile errors
let device = &app.args.device;
let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
- for (name, interrupt) in &app.interrupts {
+ for (handler, interrupt) in &app.interrupts {
+ let name = interrupt.args.binds(handler);
let priority = interrupt.args.priority;
exprs.push(quote!(p.NVIC.enable(#device::Interrupt::#name);));
exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits));));
diff --git a/macros/src/syntax.rs b/macros/src/syntax.rs
index 9771ea92..7f87f633 100644
--- a/macros/src/syntax.rs
+++ b/macros/src/syntax.rs
@@ -699,6 +699,29 @@ impl Init {
}
}
+/// Union of `TaskArgs`, `ExceptionArgs` and `InterruptArgs`
+pub struct Args {
+ pub binds: Option<Ident>,
+ pub capacity: Option<u8>,
+ pub priority: u8,
+ pub resources: Idents,
+ pub schedule: Idents,
+ pub spawn: Idents,
+}
+
+impl Default for Args {
+ fn default() -> Self {
+ Args {
+ binds: None,
+ capacity: None,
+ priority: 1,
+ resources: Idents::new(),
+ schedule: Idents::new(),
+ spawn: Idents::new(),
+ }
+ }
+}
+
pub struct Exception {
pub args: ExceptionArgs,
pub attrs: Vec<Attribute>,
@@ -708,16 +731,25 @@ pub struct Exception {
}
pub struct ExceptionArgs {
+ binds: Option<Ident>,
pub priority: u8,
pub resources: Idents,
pub schedule: Idents,
pub spawn: Idents,
}
+impl ExceptionArgs {
+ /// Returns the name of the exception / interrupt this handler binds to
+ pub fn binds<'a>(&'a self, handler: &'a Ident) -> &'a Ident {
+ self.binds.as_ref().unwrap_or(handler)
+ }
+}
+
impl Parse for ExceptionArgs {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
- parse_args(input, false).map(
- |TaskArgs {
+ parse_args(input, /* binds */ true, /* capacity */ false).map(
+ |Args {
+ binds,
priority,
resources,
schedule,
@@ -725,6 +757,7 @@ impl Parse for ExceptionArgs {
..
}| {
ExceptionArgs {
+ binds,
priority,
resources,
schedule,
@@ -755,7 +788,7 @@ impl Exception {
}
let span = item.ident.span();
- match &*item.ident.to_string() {
+ match &*args.binds.as_ref().unwrap_or(&item.ident).to_string() {
"MemoryManagement" | "BusFault" | "UsageFault" | "SecureFault" | "SVCall"
| "DebugMonitor" | "PendSV" => {} // OK
"SysTick" => {
@@ -893,30 +926,40 @@ pub struct TaskArgs {
pub schedule: Idents,
}
-impl Default for TaskArgs {
- fn default() -> Self {
- TaskArgs {
- capacity: None,
- priority: 1,
- resources: Idents::new(),
- schedule: Idents::new(),
- spawn: Idents::new(),
- }
- }
-}
-
impl Parse for TaskArgs {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
- parse_args(input, true)
+ parse_args(input, /* binds */ false, /* capacity */ true).map(
+ |Args {
+ capacity,
+ priority,
+ resources,
+ schedule,
+ spawn,
+ ..
+ }| {
+ TaskArgs {
+ capacity,
+ priority,
+ resources,
+ schedule,
+ spawn,
+ }
+ },
+ )
}
}
-// Parser shared by TaskArgs and ExceptionArgs / InterruptArgs
-fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result<TaskArgs> {
+// Parser shared by ExceptionArgs, InterruptArgs and TaskArgs
+fn parse_args(
+ input: ParseStream<'_>,
+ accepts_binds: bool,
+ accepts_capacity: bool,
+) -> parse::Result<Args> {
if input.is_empty() {
- return Ok(TaskArgs::default());
+ return Ok(Args::default());
}
+ let mut binds = None;
let mut capacity = None;
let mut priority = None;
let mut resources = None;
@@ -936,7 +979,20 @@ fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result<Ta
let ident_s = ident.to_string();
match &*ident_s {
- "capacity" if accept_capacity => {
+ "binds" if accepts_binds => {
+ if binds.is_some() {
+ return Err(parse::Error::new(
+ ident.span(),
+ "argument appears more than once",
+ ));
+ }
+
+ // #ident
+ let ident = content.parse()?;
+
+ binds = Some(ident);
+ }
+ "capacity" if accepts_capacity => {
if capacity.is_some() {
return Err(parse::Error::new(
ident.span(),
@@ -1052,7 +1108,11 @@ fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result<Ta
_ => {
return Err(parse::Error::new(
ident.span(),
- "expected one of: priority, resources, schedule or spawn",
+ format!(
+ "expected one of: {}{}priority, resources, schedule or spawn",
+ if accepts_binds { "binds, " } else { "" },
+ if accepts_capacity { "capacity, " } else { "" },
+ ),
));
}
}
@@ -1065,7 +1125,8 @@ fn parse_args(input: ParseStream<'_>, accept_capacity: bool) -> parse::Result<Ta
let _: Token![,] = content.parse()?;
}
- Ok(TaskArgs {
+ Ok(Args {
+ binds,
capacity,
priority: priority.unwrap_or(1),
resources: resources.unwrap_or(Idents::new()),