forked from oxidecomputer/p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.rs
220 lines (207 loc) · 7.73 KB
/
expression.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2022 Oxide Computer Company
use p4::ast::{BinOp, DeclarationInfo, Expression, ExpressionKind, Lvalue};
use p4::hlir::Hlir;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
pub(crate) struct ExpressionGenerator<'a> {
hlir: &'a Hlir,
}
impl<'a> ExpressionGenerator<'a> {
pub fn new(hlir: &'a Hlir) -> Self {
Self { hlir }
}
pub(crate) fn generate_expression(&self, xpr: &Expression) -> TokenStream {
match &xpr.kind {
ExpressionKind::BoolLit(v) => {
quote! { #v }
}
ExpressionKind::IntegerLit(v) => {
quote! { #v }
}
ExpressionKind::BitLit(width, v) => {
self.generate_bit_literal(*width, *v)
}
ExpressionKind::SignedLit(_width, _v) => {
todo!("generate expression signed lit");
}
ExpressionKind::Lvalue(v) => self.generate_lvalue(v),
ExpressionKind::Binary(lhs, op, rhs) => {
let lhs_tks = self.generate_expression(lhs.as_ref());
let op_tks = self.generate_binop(*op);
let rhs_tks = self.generate_expression(rhs.as_ref());
let mut ts = TokenStream::new();
match op {
BinOp::Add => {
ts.extend(quote!{
p4rs::bitmath::add_le(#lhs_tks.clone(), #rhs_tks.clone())
});
}
BinOp::Mod => {
ts.extend(quote!{
p4rs::bitmath::mod_le(#lhs_tks.clone(), #rhs_tks.clone())
});
}
BinOp::Eq | BinOp::NotEq => {
let lhs_tks_ = match &lhs.as_ref().kind {
ExpressionKind::Lvalue(lval) => {
let name_info = self
.hlir
.lvalue_decls
.get(lval)
.unwrap_or_else(|| {
panic!(
"declaration info for {:#?}",
lval
)
});
match name_info.decl {
DeclarationInfo::ActionParameter(_) => {
quote! {
&#lhs_tks
}
}
_ => lhs_tks,
}
}
_ => lhs_tks,
};
let rhs_tks_ = match &rhs.as_ref().kind {
ExpressionKind::Lvalue(lval) => {
let name_info = self
.hlir
.lvalue_decls
.get(lval)
.unwrap_or_else(|| {
panic!(
"declaration info for {:#?}",
lval
)
});
match name_info.decl {
DeclarationInfo::ActionParameter(_) => {
quote! {
&#rhs_tks
}
}
_ => rhs_tks,
}
}
_ => rhs_tks,
};
ts.extend(lhs_tks_);
ts.extend(op_tks);
ts.extend(rhs_tks_);
}
_ => {
ts.extend(lhs_tks);
ts.extend(op_tks);
ts.extend(rhs_tks);
}
}
ts
}
ExpressionKind::Index(lval, xpr) => {
let mut ts = self.generate_lvalue(lval);
ts.extend(self.generate_expression(xpr.as_ref()));
ts
}
ExpressionKind::Slice(begin, end) => {
let l = match &begin.kind {
ExpressionKind::IntegerLit(v) => *v as usize,
_ => panic!("slice ranges can only be integer literals"),
};
let l = l + 1;
let r = match &end.kind {
ExpressionKind::IntegerLit(v) => *v as usize,
_ => panic!("slice ranges can only be integer literals"),
};
quote! {
[#r..#l]
}
}
ExpressionKind::Call(call) => {
let lv: Vec<TokenStream> = call
.lval
.name
.split('.')
.map(|x| format_ident!("{}", x))
.map(|x| quote! { #x })
.collect();
let lvalue = quote! { #(#lv).* };
let mut args = Vec::new();
for arg in &call.args {
args.push(self.generate_expression(arg));
}
quote! {
#lvalue(#(#args),*)
}
}
ExpressionKind::List(elements) => {
let mut parts = Vec::new();
for e in elements {
parts.push(self.generate_expression(e));
}
quote! {
&[ #(&#parts),* ]
}
}
}
}
pub(crate) fn generate_bit_literal(
&self,
width: u16,
value: u128,
) -> TokenStream {
assert!(width <= 128);
let width = width as usize;
quote! {
{
let mut x = bitvec![mut u8, Msb0; 0; #width];
x.store_le(#value);
x
}
}
}
pub(crate) fn generate_binop(&self, op: BinOp) -> TokenStream {
match op {
BinOp::Add => quote! { + },
BinOp::Subtract => quote! { - },
BinOp::Mod => quote! { % },
BinOp::Geq => quote! { >= },
BinOp::Gt => quote! { > },
BinOp::Leq => quote! { <= },
BinOp::Lt => quote! { < },
BinOp::Eq => quote! { == },
BinOp::NotEq => quote! { != },
BinOp::Mask => quote! { & },
BinOp::BitAnd => quote! { & },
BinOp::BitOr => quote! { | },
BinOp::Xor => quote! { ^ },
}
}
pub(crate) fn generate_lvalue(&self, lval: &Lvalue) -> TokenStream {
let lv: Vec<TokenStream> = lval
.name
.split('.')
.map(|x| format_ident!("{}", x))
.map(|x| quote! { #x })
.collect();
let lvalue = quote! { #(#lv).* };
let name_info = self
.hlir
.lvalue_decls
.get(lval)
.unwrap_or_else(|| panic!("declaration info for {:#?}", lval));
match name_info.decl {
DeclarationInfo::HeaderMember => quote! {
#lvalue
},
/*
DeclarationInfo::ActionParameter(_) => quote! {
&#lvalue
},
*/
_ => lvalue,
}
}
}