Skip to content

Commit 56eff97

Browse files
committed
lots of changes
1 parent d3d1564 commit 56eff97

10 files changed

Lines changed: 572 additions & 80 deletions

File tree

macros/Cargo.lock

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

macros/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ edition = "2024"
77
proc-macro2 = "1.0.95"
88
quote = "1.0.40"
99
syn = { version = "2", features = ["full"] }
10+
probe-plotter-common = { path = "../probe-plotter-common" }
11+
serde = { version = "1.0.219", features = ["derive"] }
12+
serde_json = "1.0.141"
1013

1114
[lib]
12-
proc-macro = true
15+
proc-macro = true

macros/src/lib.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ pub fn make_metric(args: TokenStream) -> TokenStream {
6464
.into()
6565
}
6666

67-
pub fn make_inplace_thing(args: TokenStream) -> TokenStream {
67+
/*pub fn make_inplace_thing(args: TokenStream) -> TokenStream {
6868
69-
}
69+
}*/
7070

7171
/// Create a Setting instance that will be shown as a slider in the probe-plotter utility
7272
///
@@ -139,13 +139,13 @@ fn hash(string: &str) -> u64 {
139139
/// struct Foo {
140140
/// #[metric("bar / 33.1")]
141141
/// bar: u8,
142-
///
142+
///
143143
/// baz: Baz,
144-
///
144+
///
145145
/// #[setting(0..=3, 1)]
146146
/// quix: f32
147147
/// }
148-
///
148+
///
149149
/// #[derive(Plottable)]
150150
/// struct Baz {
151151
/// #[metric("a * 3.0")]
@@ -159,16 +159,32 @@ pub fn impl_metricable(input: TokenStream) -> TokenStream {
159159
let ast: syn::ItemStruct = syn::parse(input).unwrap();
160160
let fields = ast.fields;
161161
let name = &ast.ident;
162-
let fields = fields.iter().fold(String::new(), |acc, f| acc + &quote!([#f.ident, #f.ty]).to_string() + ",");
162+
163+
let fields: Vec<_> = fields
164+
.iter()
165+
.map(|f| {
166+
let ty = &f.ty;
167+
probe_plotter_common::symbol::Member {
168+
name: f.ident.as_ref().unwrap().to_string(),
169+
ty: quote!(#ty).to_string(),
170+
offset: None,
171+
}})
172+
.collect();
173+
163174
// TODO: move the static into some linker section so it does not occupy flash space
164175
// TODO: handle module paths and name spaces
165-
let sym_name = format!(r#"{{"type":"Type",""ty":"{name}", fields: [{fields}]}}"#);
176+
let sym_name = serde_json::to_string(&probe_plotter_common::symbol::Symbol::Type {
177+
name: name.to_string(),
178+
fields,
179+
})
180+
.unwrap();
181+
166182
quote! {
167183
unsafe impl ::probe_plotter::metric::Metricable for #name {}
168184

169185
#[allow(non_upper_case_globals)]
170186
#[unsafe(export_name = #sym_name)]
171187
static #name: u8 = 0;
172-
173-
}.into()
174-
}
188+
}
189+
.into()
190+
}

probe-plotter-common/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "probe-plotter-common"
3+
version = "0.1.0"
4+
edition = "2024"
5+
publish = ["gitea"]
6+
7+
[dependencies]
8+
serde = { version = "1.0.219", features = ["derive"] }
9+
serde_json = "1.0.141"
10+
shunting = { git = "https://github.com/usbalbin/tox", branch = "add-ln" }

probe-plotter-common/src/lib.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use std::{collections::HashMap, fmt::Display};
2+
3+
pub mod symbol;
4+
5+
#[allow(non_camel_case_types)]
6+
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq, Hash, Eq)]
7+
pub enum PrimitiveType {
8+
u8,
9+
u16,
10+
u32,
11+
i8,
12+
i16,
13+
i32,
14+
f32,
15+
}
16+
17+
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Clone, Hash, Eq)]
18+
pub struct Atype(String);
19+
20+
impl Display for Atype {
21+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22+
self.0.fmt(f)
23+
}
24+
}
25+
26+
pub type Types = HashMap<Atype, TypeDef>;
27+
28+
pub enum TypeDef {
29+
Struct {
30+
name: Atype,
31+
fields: Vec<symbol::Member>,
32+
},
33+
Enum {
34+
name: Atype,
35+
discriminator_type: PrimitiveType,
36+
variants: Vec<EnumVariant>,
37+
},
38+
}
39+
40+
impl TypeDef {
41+
fn size_of(&self, types: &Types) -> u64 {
42+
match self {
43+
TypeDef::Struct { name, fields } => fields
44+
.last()
45+
.map(|x| {
46+
x.offset.unwrap().next_multiple_of(x.ty.align_of(&types)) + x.ty.size_of(&types)
47+
})
48+
.unwrap_or(0),
49+
TypeDef::Enum {
50+
name,
51+
discriminator_type,
52+
variants,
53+
} => todo!(),
54+
}
55+
}
56+
57+
fn align_of(&self, _types: &Types) -> u64 {
58+
todo!()
59+
}
60+
}
61+
62+
pub struct EnumVariant {
63+
pub name: String,
64+
pub ty: PrimitiveType,
65+
pub expr: shunting::RPNExpr,
66+
}
67+
68+
impl Atype {
69+
pub fn is_primitive(&self) -> bool {
70+
["u8", "u16", "u32", "i8", "i16", "i32", "f32"].contains(&self.0.as_ref())
71+
}
72+
73+
pub fn size_of(&self, types: &Types) -> u64 {
74+
match PrimitiveType::try_from(self) {
75+
Ok(p) => p.size_of(),
76+
Err(()) => types.get(self).unwrap().size_of(types),
77+
}
78+
}
79+
80+
pub fn align_of(&self, types: &Types) -> u64 {
81+
match PrimitiveType::try_from(self) {
82+
Ok(p) => p.align_of(),
83+
Err(()) => types.get(self).unwrap().size_of(types),
84+
}
85+
}
86+
}
87+
88+
impl TryFrom<&Atype> for PrimitiveType {
89+
type Error = ();
90+
91+
fn try_from(t: &Atype) -> Result<PrimitiveType, Self::Error> {
92+
match t.0.as_str() {
93+
"u8" => Ok(PrimitiveType::u8),
94+
"u16" => Ok(PrimitiveType::u16),
95+
"u32" => Ok(PrimitiveType::u32),
96+
"i8" => Ok(PrimitiveType::i8),
97+
"i16" => Ok(PrimitiveType::i16),
98+
"i32" => Ok(PrimitiveType::i32),
99+
"f32" => Ok(PrimitiveType::f32),
100+
_ => Err(()),
101+
}
102+
}
103+
}
104+
105+
impl PrimitiveType {
106+
pub fn size_of(&self) -> u64 {
107+
match self {
108+
PrimitiveType::u8 | PrimitiveType::i8 => 1,
109+
PrimitiveType::u16 | PrimitiveType::i16 => 2,
110+
PrimitiveType::u32 | PrimitiveType::i32 | PrimitiveType::f32 => 4,
111+
}
112+
}
113+
114+
pub fn align_of(&self) -> u64 {
115+
match self {
116+
PrimitiveType::u8 | PrimitiveType::i8 => 1,
117+
PrimitiveType::u16 | PrimitiveType::i16 => 2,
118+
PrimitiveType::u32 | PrimitiveType::i32 | PrimitiveType::f32 => 4,
119+
}
120+
}
121+
}
122+
123+
impl Into<Atype> for &PrimitiveType {
124+
fn into(self) -> Atype {
125+
match self {
126+
PrimitiveType::u8 => Atype("u8".to_string()),
127+
PrimitiveType::u16 => Atype("u16".to_string()),
128+
PrimitiveType::u32 => Atype("u32".to_string()),
129+
PrimitiveType::i8 => Atype("i8".to_string()),
130+
PrimitiveType::i16 => Atype("i16".to_string()),
131+
PrimitiveType::i32 => Atype("i32".to_string()),
132+
PrimitiveType::f32 => Atype("f32".to_string()),
133+
}
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use serde::Deserialize;
21
use std::ops::RangeInclusive;
32

4-
use crate::Type;
3+
use crate::{Atype, PrimitiveType};
54

6-
#[derive(Deserialize, PartialEq)]
5+
#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
76
#[serde(tag = "type")]
87
pub enum Symbol {
98
Metric {
@@ -13,33 +12,41 @@ pub enum Symbol {
1312
expr: String,
1413

1514
/// Type of value, i32, u8 etc.
16-
ty: Type,
15+
ty: Atype,
1716
},
1817
Setting {
1918
name: String,
2019

2120
/// Type of value, i32, u8 etc.
22-
ty: Type,
21+
ty: PrimitiveType,
2322

2423
/// Range of valid values
2524
range: RangeInclusive<f64>,
2625

2726
/// Step size
2827
step_size: f64,
2928
},
29+
30+
// {"type":"Type",""ty":"{name}", fields: [{fields}]}
31+
Type {
32+
name: Atype,
33+
fields: Vec<Member>,
34+
},
3035
}
3136

3237
impl Symbol {
3338
pub fn name(&self) -> &str {
3439
match self {
3540
Symbol::Metric { name, .. } => name,
3641
Symbol::Setting { name, .. } => name,
42+
_ => todo!(),
3743
}
3844
}
39-
pub fn ty(&self) -> Type {
45+
pub fn ty(&self) -> Atype {
4046
match self {
41-
Symbol::Metric { ty, .. } => *ty,
42-
Symbol::Setting { ty, .. } => *ty,
47+
Symbol::Metric { ty, .. } => ty.clone(),
48+
Symbol::Setting { ty, .. } => ty.into(),
49+
Symbol::Type { name, .. } => name.clone(),
4350
}
4451
}
4552
}
@@ -52,3 +59,10 @@ impl Symbol {
5259
serde_json::from_str(raw).map_err(|_| InvalidSymbolError)
5360
}
5461
}
62+
63+
#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
64+
pub struct Member {
65+
pub name: String,
66+
pub ty: Atype,
67+
pub offset: Option<u64>,
68+
}

probe-plotter-tools/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ mimalloc = "0.1.43"
2222
shunting = { git = "https://github.com/usbalbin/tox", branch = "add-ln" }
2323
defmt-decoder = "1.0.0"
2424
defmt-parser = "1.0.0"
25+
probe-plotter-common = { path = "../probe-plotter-common" }

0 commit comments

Comments
 (0)