forked from oxidecomputer/p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.rs
183 lines (167 loc) · 5.85 KB
/
header.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2022 Oxide Computer Company
use crate::{rust_type, type_size, Context};
use p4::ast::{Header, AST};
use quote::{format_ident, quote};
pub(crate) struct HeaderGenerator<'a> {
ast: &'a AST,
ctx: &'a mut Context,
}
impl<'a> HeaderGenerator<'a> {
pub(crate) fn new(ast: &'a AST, ctx: &'a mut Context) -> Self {
Self { ast, ctx }
}
pub(crate) fn generate(&mut self) {
for h in &self.ast.headers {
self.generate_header(h);
}
}
fn generate_header(&mut self, h: &Header) {
let name = format_ident!("{}", h.name);
//
// genrate a rust struct for the header
//
// generate struct members
let mut members = Vec::new();
for member in &h.members {
let name = format_ident!("{}", member.name);
let ty = rust_type(&member.ty);
members.push(quote! { pub #name: #ty });
}
let mut generated = quote! {
#[derive(Debug, Default, Clone)]
pub struct #name {
pub valid: bool,
#(#members),*
}
};
//
// generate a constructor that maps the header onto a byte slice
//
// generate member assignments
let mut member_values = Vec::new();
let mut set_statements = Vec::new();
let mut to_bitvec_statements = Vec::new();
let mut checksum_statements = Vec::new();
let mut dump_statements = Vec::new();
let fmt = "{} ".repeat(h.members.len() * 2);
let fmt = fmt.trim();
let mut offset = 0;
for member in &h.members {
let name = format_ident!("{}", member.name);
let name_s = &member.name;
let size = type_size(&member.ty, self.ast);
member_values.push(quote! {
#name: BitVec::<u8, Msb0>::default()
});
let end = offset + size;
set_statements.push(quote! {
self.#name = {
let mut b = buf.view_bits::<Msb0>()[#offset..#end].to_owned();
// NOTE this barfing and then unbarfing a vec is to handle
// the p4 confused-endian data model.
let mut v = b.into_vec();
v.reverse();
let mut b = BitVec::<u8, Msb0>::from_vec(v);
if (#end-#offset) < 8 {
b.shift_left(#offset % 8);
}
b.resize(#end-#offset, false);
b
}
});
to_bitvec_statements.push(quote! {
// NOTE this barfing and then unbarfing a vec is to handle
// the p4 confused-endian data model.
let mut v = self.#name.clone().into_vec();
v.reverse();
let n = (#end-#offset);
let m = n%8;
if (n > 8) && m != 0 {
let mut b = BitVec::<u8, Msb0>::from_vec(v);
if b.len() > m {
x[#offset..#end] |= &b[m..];
} else {
x[#offset..#end] |= &b;
}
} else {
let mut b = BitVec::<u8, Msb0>::from_vec(v);
b.resize(#end-#offset, false);
x[#offset..#end] |= &b;
}
});
checksum_statements.push(quote! {
csum = p4rs::bitmath::add_le(csum.clone(), self.#name.csum())
});
dump_statements.push(quote! {
#name_s.cyan(),
p4rs::dump_bv(&self.#name)
});
offset += size;
}
let dump = quote! {
format!(#fmt, #(#dump_statements),*)
};
//TODO perhaps we should just keep the whole header as one bitvec so we
//don't need to construct a consolidated bitvec like to_bitvec does?
generated.extend(quote! {
impl Header for #name {
fn new() -> Self {
Self {
valid: false,
#(#member_values),*
}
}
fn set(
&mut self,
buf: &[u8]
) -> Result<(), TryFromSliceError> {
#(#set_statements);*;
Ok(())
}
fn size() -> usize {
#offset
}
fn set_valid(&mut self) {
self.valid = true;
}
fn set_invalid(&mut self) {
self.valid = false;
}
fn is_valid(&self) -> bool {
self.valid
}
fn to_bitvec(&self) -> BitVec<u8, Msb0> {
let mut x = bitvec![u8, Msb0; 0u8; Self::size()];
#(#to_bitvec_statements);*;
x
}
}
impl Checksum for #name {
fn csum(&self) -> BitVec::<u8, Msb0> {
let mut csum = BitVec::new();
#(#checksum_statements);*;
csum
}
}
impl #name {
fn setValid(&mut self) {
self.valid = true;
}
fn setInvalid(&mut self) {
self.valid = false;
}
fn isValid(&self) -> bool {
self.valid
}
fn dump(&self) -> String {
if self.isValid() {
#dump
} else {
"∅".to_owned()
}
}
}
});
self.ctx.structs.insert(h.name.clone(), generated);
}
}