Skip to content

Commit 7f3f06b

Browse files
committed
impl in device instead
1 parent 5b0d324 commit 7f3f06b

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/generate/block.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
2-
use proc_macro2::{Ident, Literal, Span, TokenStream};
2+
use proc_macro2::TokenStream;
3+
use proc_macro2::{Ident, Span};
34
use quote::quote;
4-
use std::collections::HashMap;
55

66
use crate::ir::*;
77
use crate::util;
@@ -83,17 +83,8 @@ pub fn render(opts: &super::Options, ir: &IR, b: &Block, path: &str) -> Result<T
8383
}
8484

8585
let (_, name) = super::split_path(path);
86-
let unknown = Literal::string(&format!("Unknown {}", name));
8786
let name = Ident::new(name, span);
8887
let doc = util::doc(&b.description);
89-
90-
let addrs = get_addrs(ir, b);
91-
let addrs = addrs.iter().map(|(addr, block_name)| {
92-
let addr = Literal::u32_suffixed(*addr);
93-
let block_name = Literal::string(block_name);
94-
quote!(#addr => f.write_str(#block_name),)
95-
});
96-
9788
let out = quote! {
9889
#doc
9990
#[derive(Copy, Clone, Eq, PartialEq)]
@@ -103,22 +94,7 @@ pub fn render(opts: &super::Options, ir: &IR, b: &Block, path: &str) -> Result<T
10394
impl #name {
10495
#items
10596
}
106-
107-
impl core::fmt::Display for #name {
108-
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
109-
match self.0 {
110-
#(
111-
#addrs
112-
)*
113-
_ => f.write_str(#unknown),
114-
}
115-
}
116-
}
11797
};
11898

11999
Ok(out)
120100
}
121-
122-
fn get_addrs(_ir: &IR, _b: &Block) -> HashMap<u32, String> {
123-
todo!()
124-
}

src/generate/device.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use std::collections::HashMap;
2+
13
use anyhow::Result;
2-
use proc_macro2::{Ident, Span, TokenStream};
4+
use proc_macro2::{Ident, Literal, Span, TokenStream};
35
use quote::quote;
46

57
use crate::ir::*;
@@ -16,6 +18,7 @@ pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result
1618
let mut peripherals = TokenStream::new();
1719
let mut vectors = TokenStream::new();
1820
let mut names = vec![];
21+
let mut addrs: HashMap<_, Vec<(_, _)>> = HashMap::new();
1922

2023
let mut pos = 0;
2124
for i in &interrupts_sorted {
@@ -54,6 +57,12 @@ pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result
5457

5558
if let Some(block_name) = &p.block {
5659
let _b = ir.blocks.get(block_name);
60+
61+
addrs
62+
.entry((block_name, path))
63+
.or_default()
64+
.push((address.clone(), name.clone()));
65+
5766
let path = util::relative_path(block_name, path);
5867

5968
peripherals.extend(quote! {
@@ -68,6 +77,29 @@ pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result
6877
}
6978
}
7079

80+
for ((block_name, path), addrs) in addrs {
81+
let unknown = Literal::string(&format!("Unknown {}", block_name));
82+
83+
let path = util::relative_path(block_name, path);
84+
85+
let addrs = addrs
86+
.iter()
87+
.map(|(addr, block_name)| quote!(#addr => f.write_str(#block_name),));
88+
89+
peripherals.extend(quote! {
90+
impl core::fmt::Display for #path {
91+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
92+
match self.0 {
93+
#(
94+
#addrs
95+
)*
96+
_ => f.write_str(#unknown),
97+
}
98+
}
99+
}
100+
});
101+
}
102+
71103
let n = util::unsuffixed(pos as u64);
72104
out.extend(quote!(
73105
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)