forked from oxidecomputer/p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4struct.rs
147 lines (129 loc) · 4.7 KB
/
p4struct.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2022 Oxide Computer Company
use crate::Context;
use p4::ast::{Struct, Type, AST};
use quote::{format_ident, quote};
pub(crate) struct StructGenerator<'a> {
ast: &'a AST,
ctx: &'a mut Context,
}
impl<'a> StructGenerator<'a> {
pub(crate) fn new(ast: &'a AST, ctx: &'a mut Context) -> Self {
Self { ast, ctx }
}
pub(crate) fn generate(&mut self) {
for s in &self.ast.structs {
self.generate_struct(s);
}
}
fn generate_struct(&mut self, s: &Struct) {
let mut members = Vec::new();
let mut valid_member_size = Vec::new();
let mut to_bitvec_stmts = Vec::new();
let mut dump_statements = Vec::new();
let fmt = "{}: {}\n".repeat(s.members.len());
let fmt = fmt.trim();
for member in &s.members {
let name = format_ident!("{}", member.name);
let name_s = &member.name;
match &member.ty {
Type::UserDefined(ref typename) => {
if self.ast.get_header(typename).is_some() {
let ty = format_ident!("{}", typename);
// member generation
members.push(quote! { pub #name: #ty });
// valid header size statements
valid_member_size.push(quote! {
if self.#name.valid {
x += #ty::size();
}
});
// to bitvec statements
to_bitvec_stmts.push(quote!{
if self.#name.valid {
x[off..off+#ty::size()] |= self.#name.to_bitvec();
off += #ty::size();
}
});
dump_statements.push(quote! {
#name_s.blue(),
self.#name.dump()
});
} else {
panic!(
"Struct member {:#?} undefined in {:#?}",
member, s
);
}
}
Type::Bit(size) => {
members.push(quote! { pub #name: BitVec::<u8, Msb0> });
dump_statements.push(quote! {
#name_s.blue(),
p4rs::dump_bv(&self.#name)
});
valid_member_size.push(quote! {
x += #size;
});
to_bitvec_stmts.push(quote! {
x[off..off+#size] |= self.#name.to_bitvec();
off += #size;
});
}
Type::Bool => {
members.push(quote! { pub #name: bool });
dump_statements.push(quote! {
#name_s.blue(),
self.#name
});
}
x => {
todo!("struct member {}", x)
}
}
}
let dump = quote! {
format!(#fmt, #(#dump_statements),*)
};
let name = format_ident!("{}", s.name);
let mut structure = quote! {
#[derive(Debug, Default, Clone)]
pub struct #name {
#(#members),*
}
};
if !valid_member_size.is_empty() {
structure.extend(quote! {
impl #name {
fn valid_header_size(&self) -> usize {
let mut x: usize = 0;
#(#valid_member_size)*
x
}
fn to_bitvec(&self) -> BitVec<u8, Msb0> {
let mut x =
bitvec![u8, Msb0; 0; self.valid_header_size()];
let mut off = 0;
#(#to_bitvec_stmts)*
x
}
fn dump(&self) -> String {
#dump
}
}
})
} else {
structure.extend(quote! {
impl #name {
fn valid_header_size(&self) -> usize { 0 }
fn to_bitvec(&self) -> BitVec<u8, Msb0> {
bitvec![u8, Msb0; 0; 0]
}
fn dump(&self) -> String {
std::string::String::new()
}
}
})
}
self.ctx.structs.insert(s.name.clone(), structure);
}
}