-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexpression.rs
More file actions
501 lines (460 loc) · 17.7 KB
/
expression.rs
File metadata and controls
501 lines (460 loc) · 17.7 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright 2026 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::Subtract => {
ts.extend(quote!{
p4rs::bitmath::sub_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_);
}
BinOp::BitOr | BinOp::BitAnd | BinOp::Xor | BinOp::Mask => {
ts.extend(quote! {
{
let __lhs = #lhs_tks.clone();
let __rhs = #rhs_tks.clone();
__lhs #op_tks __rhs
}
});
}
BinOp::Shl => {
ts.extend(quote!{
p4rs::bitmath::shl_le(#lhs_tks.clone(), #rhs_tks.clone())
});
}
BinOp::Shr => {
ts.extend(quote!{
p4rs::bitmath::shr_le(#lhs_tks.clone(), #rhs_tks.clone())
});
}
_ => {
ts.extend(lhs_tks);
ts.extend(op_tks);
ts.extend(rhs_tks);
}
}
ts
}
ExpressionKind::Index(lval, xpr) => {
let mut ts = self.generate_lvalue(lval);
// For slices, look up the parent field's bit width
// so generate_slice can adjust for header.rs byte
// reversal.
if let ExpressionKind::Slice(begin, end) = &xpr.kind {
let ni =
self.hlir.lvalue_decls.get(lval).unwrap_or_else(|| {
panic!("unresolved lvalue {:#?} in slice", lval)
});
let field_width = match &ni.ty {
p4::ast::Type::Bit(w)
| p4::ast::Type::Varbit(w)
| p4::ast::Type::Int(w) => *w,
ty => panic!(
"slice on non-bit type {:?} reached codegen",
ty,
),
};
let (hi, lo) = Self::slice_bounds(begin, end);
if Self::slice_is_contiguous(hi, lo, field_width) {
ts.extend(self.generate_slice(begin, end, field_width));
} else {
// Non-contiguous after byte reversal;
// replace the lvalue suffix with arithmetic.
return Self::generate_slice_read_arith(&ts, hi, lo);
}
} else {
ts.extend(self.generate_expression(xpr.as_ref()));
}
ts
}
ExpressionKind::Slice(_begin, _end) => {
// The HLIR rejects bare slices outside an Index
// expression, so this is unreachable for well-typed
// programs.
unreachable!("bare Slice reached codegen");
}
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),* ]
}
}
}
}
/// Extract compile-time hi and lo from slice bound expressions.
pub(crate) fn slice_bounds(
begin: &Expression,
end: &Expression,
) -> (P4Bit, P4Bit) {
let hi: P4Bit = match &begin.kind {
ExpressionKind::IntegerLit(v) => *v as usize,
_ => panic!("slice ranges can only be integer literals"),
};
let lo: P4Bit = match &end.kind {
ExpressionKind::IntegerLit(v) => *v as usize,
_ => panic!("slice ranges can only be integer literals"),
};
(hi, lo)
}
/// Whether `[hi:lo]` on a field of `field_width` bits can be
/// expressed as a contiguous bitvec range after byte reversal.
pub(crate) fn slice_is_contiguous(
hi: P4Bit,
lo: P4Bit,
field_width: FieldWidth,
) -> bool {
if field_width <= 8 {
return true;
}
// Non-byte-multiple widths have an additional bit-shift in
// header.rs storage that reversed_slice_range does not model.
if !field_width.is_multiple_of(8) {
return false;
}
reversed_slice_range(hi, lo, field_width).is_some()
}
pub(crate) fn generate_slice(
&self,
begin: &Expression,
end: &Expression,
field_width: FieldWidth,
) -> TokenStream {
let (hi, lo) = Self::slice_bounds(begin, end);
if field_width > 8 {
let (r, l) = reversed_slice_range(hi, lo, field_width).expect(
"non-contiguous slice reads must be handled \
by the caller via generate_slice_read_arith",
);
quote! { [#r..#l] }
} else {
// Fields <= 8 bits are not byte-reversed by header.rs,
// so the naive P4-to-bitvec mapping is correct.
let l = hi + 1;
let r = lo;
quote! { [#r..#l] }
}
}
/// Emit an arithmetic slice read for non-contiguous slices.
/// Loads the field as an integer, shifts and masks to extract
/// the requested bits, then packs into a new bitvec.
pub(crate) fn generate_slice_read_arith(
lhs: &TokenStream,
hi: P4Bit,
lo: P4Bit,
) -> TokenStream {
let slice_width = hi - lo + 1;
let mask_val = (1u128 << slice_width) - 1;
quote! {
{
let __v: u128 = #lhs.load_le();
let __extracted = (__v >> #lo) & #mask_val;
let mut __out = bitvec![u8, Msb0; 0; #slice_width];
__out.store_le(__extracted);
__out
}
}
}
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! { ^ },
BinOp::Shl => quote! { << },
BinOp::Shr => 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,
}
}
}
/// P4 bit position (MSB-first index within a field).
type P4Bit = usize;
/// Width of a P4 header field in bits.
type FieldWidth = usize;
/// Half-open bitvec range `(start, end)` into the storage representation.
type BitvecRange = (usize, usize);
/// Map a P4 slice `[hi:lo]` to a bitvec range in byte-reversed storage.
///
/// header.rs reverses byte order for fields wider than 8 bits. Bit
/// positions within each byte are preserved (Msb0). The mapping from
/// P4 bit positions to storage indices:
///
/// ```text
/// wire_idx = W - 1 - b
/// wire_byte = wire_idx / 8
/// bit_in_byte = wire_idx % 8
/// storage_byte = W/8 - 1 - wire_byte
/// bitvec_idx = storage_byte * 8 + bit_in_byte
/// ```
///
/// # Returns
///
/// `Some(range)` when the slice maps to a contiguous bitvec range
/// (single-byte slices or byte-aligned multi-byte slices), `None`
/// for non-byte-aligned multi-byte slices where byte reversal makes
/// the bits non-contiguous.
pub(crate) fn reversed_slice_range(
hi: P4Bit,
lo: P4Bit,
field_width: FieldWidth,
) -> Option<BitvecRange> {
// Wire byte indices for the slice endpoints. P4 bit W-1 is in wire
// byte 0 (MSB-first), so higher bit numbers map to lower byte indices.
let wire_byte_hi = (field_width - 1 - hi) / 8;
let wire_byte_lo = (field_width - 1 - lo) / 8;
if wire_byte_hi == wire_byte_lo {
// Single-byte slice: map each endpoint individually.
let map_bit = |bit_pos: usize| -> usize {
let wire_idx = field_width - 1 - bit_pos;
let wire_byte = wire_idx / 8;
let bit_in_byte = wire_idx % 8;
let storage_byte = field_width / 8 - 1 - wire_byte;
storage_byte * 8 + bit_in_byte
};
let mapped_hi = map_bit(hi);
let mapped_lo = map_bit(lo);
Some((mapped_hi.min(mapped_lo), mapped_hi.max(mapped_lo) + 1))
} else if (hi + 1).is_multiple_of(8) && lo.is_multiple_of(8) {
// Multi-byte byte-aligned slice: reversed bytes form a
// contiguous block.
let storage_byte_start = field_width / 8 - 1 - wire_byte_lo;
let storage_byte_end = field_width / 8 - 1 - wire_byte_hi;
Some((storage_byte_start * 8, (storage_byte_end + 1) * 8))
} else {
// Non-byte-aligned multi-byte slice: byte reversal makes the
// bits non-contiguous, so there is no single bitvec range.
None
}
}
#[cfg(test)]
mod tests {
use super::*;
// Verify the reversed slice range mapping against the byte reversal
// in header.rs. For each case we check that the bitvec range lands
// on the correct bits in the reversed storage layout.
// Sub-byte slices within a single wire byte.
#[test]
fn slice_32bit_top_nibble() {
// P4 [31:28] on 32-bit: top nibble of wire byte 0.
// Storage: wire byte 0 -> storage byte 3.
// High nibble of storage byte 3 = bitvec [24..28].
assert_eq!(reversed_slice_range(31, 28, 32), Some((24, 28)));
}
#[test]
fn slice_32bit_bottom_nibble() {
// P4 [3:0] on 32-bit: bottom nibble of wire byte 3.
// Storage: wire byte 3 -> storage byte 0.
// Low nibble (Msb0) of storage byte 0 = bitvec [4..8].
assert_eq!(reversed_slice_range(3, 0, 32), Some((4, 8)));
}
#[test]
fn slice_16bit_top_nibble() {
// P4 [15:12] on 16-bit: top nibble of wire byte 0.
// Storage: wire byte 0 -> storage byte 1.
// High nibble of storage byte 1 = bitvec [8..12].
assert_eq!(reversed_slice_range(15, 12, 16), Some((8, 12)));
}
// Full-byte slices (single byte).
#[test]
fn slice_128bit_top_byte() {
// P4 [127:120] on 128-bit: wire byte 0 -> storage byte 15.
// bitvec [120..128].
assert_eq!(reversed_slice_range(127, 120, 128), Some((120, 128)));
}
#[test]
fn slice_16bit_low_byte() {
// P4 [7:0] on 16-bit: wire byte 1 -> storage byte 0.
// bitvec [0..8].
assert_eq!(reversed_slice_range(7, 0, 16), Some((0, 8)));
}
#[test]
fn slice_32bit_middle_byte() {
// P4 [23:16] on 32-bit: wire byte 1 -> storage byte 2.
// bitvec [16..24].
assert_eq!(reversed_slice_range(23, 16, 32), Some((16, 24)));
}
// Multi-byte byte-aligned slices.
#[test]
fn slice_128bit_top_two_bytes() {
// P4 [127:112] on 128-bit: wire bytes 0-1 -> storage bytes 14-15.
// bitvec [112..128].
assert_eq!(reversed_slice_range(127, 112, 128), Some((112, 128)));
}
#[test]
fn slice_32bit_top_three_bytes() {
// P4 [31:8] on 32-bit: wire bytes 0-2 -> storage bytes 1-3.
// bitvec [8..32].
assert_eq!(reversed_slice_range(31, 8, 32), Some((8, 32)));
}
#[test]
fn slice_32bit_bottom_two_bytes() {
// P4 [15:0] on 32-bit: wire bytes 2-3 -> storage bytes 0-1.
// bitvec [0..16].
assert_eq!(reversed_slice_range(15, 0, 32), Some((0, 16)));
}
#[test]
fn slice_48bit_upper_24() {
assert_eq!(reversed_slice_range(47, 24, 48), Some((24, 48)));
}
#[test]
fn slice_non_contiguous_returns_none() {
assert_eq!(reversed_slice_range(11, 4, 32), None);
assert_eq!(reversed_slice_range(22, 0, 32), None);
}
}