P4C
The P4 Compiler
Loading...
Searching...
No Matches
field_packing.h
1
19#ifndef BF_P4C_PARDE_FIELD_PACKING_H_
20#define BF_P4C_PARDE_FIELD_PACKING_H_
21
22#include <vector>
23
24#include "backends/tofino/bf-p4c/ir/bitrange.h"
25#include "backends/tofino/bf-p4c/ir/gress.h"
26#include "lib/cstring.h"
27
28namespace P4 {
29namespace IR {
30namespace BFN {
31class ParserState;
32} // namespace BFN
33class Expression;
34} // namespace IR
35} // namespace P4
36
37namespace BFN {
38
39using namespace P4;
40
46 struct PackedItem {
48 const IR::Expression *field;
49 gress_t gress;
50
54
56 unsigned width;
57
58 bool isPadding() const { return field == nullptr; }
59
60 explicit PackedItem(unsigned width) : field(nullptr), width(width) {}
61 PackedItem(const IR::Expression *field, gress_t gress, cstring source, unsigned width)
62 : field(field), gress(gress), source(source), width(width) {}
63
64 static PackedItem makePadding(unsigned width) { return PackedItem(width); }
65 };
66
67 typedef std::vector<PackedItem>::iterator iterator;
68 typedef std::vector<PackedItem>::const_iterator const_iterator;
69
70 iterator begin() { return fields.begin(); }
71 iterator end() { return fields.end(); }
72 const_iterator begin() const { return fields.begin(); }
73 const_iterator end() const { return fields.end(); }
74
79 template <Endian Order, typename Func>
80 void forEachField(Func func) const {
81 int posBits = 0;
82 for (auto &field : fields) {
83 if (field.isPadding()) {
84 posBits += field.width;
85 continue;
86 }
87 const auto fieldRange =
88 nw_bitrange(StartLen(posBits, field.width)).toOrder<Order>(totalWidth);
89 posBits += field.width;
90 func(fieldRange, field.field, field.source);
91 }
92 }
93
108 void appendField(const IR::Expression *field, cstring source, unsigned width,
109 gress_t gress = INGRESS);
110 void appendField(const IR::Expression *field, unsigned width, gress_t gress = INGRESS);
111
114 void appendPadding(unsigned width);
115
117 void append(const FieldPacking &packing);
118
129 void padToAlignment(unsigned alignment, unsigned phase = 0);
130
132 void clear();
133
136 bool containsFields() const;
137
141 bool isAlignedTo(unsigned alignment, unsigned phase = 0) const;
142
154 IR::BFN::ParserState *createExtractionState(gress_t gress, cstring stateName,
155 const IR::BFN::ParserState *finalState) const;
156
158 std::vector<PackedItem> fields;
159
161 unsigned totalWidth = 0;
162};
163
164} // namespace BFN
165
166std::ostream &operator<<(std::ostream &out, const BFN::FieldPacking *packing);
167
168#endif /* BF_P4C_PARDE_FIELD_PACKING_H_ */
Definition cstring.h:85
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
ClosedRange< RangeUnit::Bit, Endian::Network > nw_bitrange
Convenience typedefs for closed ranges in bits.
Definition lib/bitrange.h:755
Definition field_packing.h:46
unsigned width
This item's width in bits.
Definition field_packing.h:56
cstring source
Definition field_packing.h:53
const IR::Expression * field
The packed field, or null if this is a padding item.
Definition field_packing.h:48
Definition field_packing.h:45
bool isAlignedTo(unsigned alignment, unsigned phase=0) const
Definition parde/field_packing.cpp:79
void append(const FieldPacking &packing)
Appends another sequence of packed items to this one.
Definition parde/field_packing.cpp:50
bool containsFields() const
Definition parde/field_packing.cpp:73
void appendField(const IR::Expression *field, cstring source, unsigned width, gress_t gress=INGRESS)
Definition parde/field_packing.cpp:33
void padToAlignment(unsigned alignment, unsigned phase=0)
Definition parde/field_packing.cpp:59
IR::BFN::ParserState * createExtractionState(gress_t gress, cstring stateName, const IR::BFN::ParserState *finalState) const
Definition parde/field_packing.cpp:84
std::vector< PackedItem > fields
The sequence of packed items (fields and padding).
Definition field_packing.h:158
void appendPadding(unsigned width)
Definition parde/field_packing.cpp:40
void clear()
Removes all packed items from this sequence.
Definition parde/field_packing.cpp:68
void forEachField(Func func) const
Definition field_packing.h:80
unsigned totalWidth
The total width of all packed items in the sequence.
Definition field_packing.h:161
Definition lib/bitrange.h:158
ClosedRange< Unit, DestOrder > toOrder(int spaceSize) const
Definition lib/bitrange.h:636