18#ifndef BACKENDS_TOFINO_BF_ASM_DEPARSER_H_
19#define BACKENDS_TOFINO_BF_ASM_DEPARSER_H_
23#include <boost/optional.hpp>
26#include "lib/bitops.h"
27#include "lib/ordered_set.h"
33 MAX_DEPARSER_CHECKSUM_UNITS = 8,
34 DEPARSER_STAGE = INT_MAX,
40class Deparser :
public Section {
41 static Deparser singleton_object;
49 std::reference_wrapper<int> lineno = val.lineno;
51 Val(
const Val &) =
default;
52 Val(Val &&) =
default;
53 Val &operator=(Val &&) =
default;
55 Val(gress_t gr,
const value_t &v) : val(gr, DEPARSER_STAGE, v) {}
56 Val(gress_t gr,
const value_t &v,
const value_t &p) : val(gr, DEPARSER_STAGE, v) {
57 pov.emplace(gr, DEPARSER_STAGE, p);
59 Val(gress_t gr,
int tag,
const value_t &p) : tag(tag) {
60 pov.emplace(gr, DEPARSER_STAGE, p);
62 Val &operator=(
const Val &a) {
68 explicit operator bool()
const {
return is_phv() || is_clot(); }
71 bool is_phv()
const {
return bool(val); }
72 bool is_clot()
const {
return tag >= 0; }
73 virtual bool check()
const {
74 if (is_phv() && is_clot()) {
75 error(lineno,
"Reference cannot be phv and clot at the same time");
80 }
else if (is_clot()) {
82 error(lineno,
"Clot requires a pov bit");
86 error(lineno,
"Unknown val");
93 struct ChecksumVal :
public Val {
96 ChecksumVal(gress_t gr,
const value_t &v,
const value_t &m) : Val(gr, v) {
97 if ((val->lo % 8 != 0) || (val->hi % 8 != 7))
98 error(lineno,
"Can only do checksums on byte-aligned container slices");
99 mask = ((1 << (val->hi + 1) / 8) - 1) ^ ((1 << val->lo / 8) - 1);
101 if (CHECKTYPE(m, tMAP)) {
102 for (
auto &kv : m.map) {
103 if (kv.key ==
"pov") {
104 if (!pov.empty())
error(kv.value.lineno,
"Duplicate POV");
105 pov.emplace_back(gr, DEPARSER_STAGE, kv.value);
106 }
else if (kv.key ==
"swap" && CHECKTYPE(kv.value, tINT)) {
109 error(m.lineno,
"Unknown key for checksum: %s", value_desc(kv.key));
114 ChecksumVal(gress_t gr,
int tag,
const value_t &p) : Val(gr, tag, p) {}
115 ChecksumVal &operator=(
const ChecksumVal &a) {
121 ChecksumVal(
const ChecksumVal &a) : Val(a) {
125 ChecksumVal() : Val() {}
126 ChecksumVal(ChecksumVal &&) =
default;
127 ChecksumVal &operator=(ChecksumVal &&) =
default;
128 bool check()
const override {
130 if (mask == 0)
error(lineno,
"mask is 0 for phv checkum value?");
131 if (swap < 0 || swap > 3)
error(lineno,
"Invalid swap for phv checksum");
138 std::map<int, std::vector<ChecksumVal>> entries;
139 std::map<int, Phv::Ref> pov;
140 std::set<int> checksum_unit_invert;
141 std::set<int> clot_tag_invert;
142 std::vector<ChecksumVal> clot_entries;
143 bool zeros_as_ones_en =
false;
147 std::vector<ChecksumVal> checksum_entries[2][MAX_DEPARSER_CHECKSUM_UNITS];
150 std::vector<FDEntry> dictionary[2];
151 std::vector<Phv::Ref> pov_order[2];
154 std::set<int> constants[2];
160 std::vector<Val> vals;
161 Intrinsic(
Type *t,
int l) : type(t), lineno(l) {}
163 std::vector<Intrinsic> intrinsics;
170 bool can_shift =
false;
171 static std::map<std::string, Type *> all[TARGET_INDEX_LIMIT][2];
174 Type(target_t t, gress_t gr,
const char *n,
int cnt)
175 : target(t), gress(gr), name(n), count(cnt) {
176 BUG_CHECK(!all[target][gress].count(name),
"Duplicate name %s", name.c_str());
177 all[target][gress][name] =
this;
179 ~Type() { all[target][gress].erase(name); }
182#define VIRTUAL_TARGET_METHODS(TARGET) \
183 virtual void setregs(Target::TARGET::deparser_regs ®s, Deparser &deparser, \
184 Deparser::Digest &data) { \
185 BUG("target mismatch"); \
187 FOR_ALL_REGISTER_SETS(VIRTUAL_TARGET_METHODS)
188#undef VIRTUAL_TARGET_METHODS
195 std::map<int, std::vector<Phv::Ref>> layout;
196 std::unique_ptr<json::map> context_json;
197 Digest(
Type *t,
int lineno, VECTOR(
pair_t) & data);
199 std::vector<Digest> digests;
205 std::vector<ChecksumVal> merge_csum_entries(
const std::vector<ChecksumVal> &,
int);
206 template <
class TARGET>
209 template <
class REGS>
211 template <
class REGS>
212 void write_config(REGS &);
214 static const bitvec &PhvUse(gress_t gr) {
return singleton_object.phv_use[gr]; }
216 static bool add_constant(gress_t gr,
int c) {
217 if (!singleton_object.constants[gr].count(c)) {
218 singleton_object.constants[gr].insert(c);
219 if (
int(singleton_object.constants[gr].size()) > Target::DEPARSER_CONSTANTS())
225 static int constant_idx(gress_t gr,
int c) {
226 if (singleton_object.constants[gr].count(c))
227 return std::distance(singleton_object.constants[gr].begin(),
228 singleton_object.constants[gr].find(c));
233 static int get_constant(gress_t gr,
int phv_idx) {
235 for (
auto constant : singleton_object.constants[gr]) {
236 if ((phv_idx - 224) == i) {
247 static void write_pov_in_json(json::map &fd, json::map &fd_entry,
const Phv::Register *phv,
248 int bit,
int offset) {
249 auto povName = Phv::get_pov_name(phv->uid, offset);
251 fd[
"POV PHV"] = phv->uid;
252 fd[
"POV Field bit"] = bit;
253 fd[
"POV Field Name"] = povName;
255 fd_entry[
"pov_bit"] = bit;
256 fd_entry[
"pov_name"] = povName;
268 static unsigned next_handle() {
269 return unique_table_offset << PIPE_ID_SHIFT | FIELD_HANDLE_START |
Definition ordered_map.h:32
Definition ordered_set.h:32
Definition bf-asm/phv.h:186
Definition bf-asm/phv.h:83
Definition backends/tofino/bf-asm/json.h:300
Definition backends/tofino/bf-asm/json.h:222
static Deparser * gtest_get_deparser()
Get the singleton object for use in gtest.
Definition tofino/bf-asm/deparser.h:276
void process()
optionally process the data if not done during parsing
Definition tofino/bf-asm/deparser.cpp:654
void gtest_clear()
Clear/reset the deparser object.
Definition tofino/bf-asm/deparser.cpp:810
void input(VECTOR(value_t) args, value_t data)
Definition tofino/bf-asm/deparser.cpp:295
static unsigned unique_field_list_handle
Definition tofino/bf-asm/deparser.h:267
void start(int lineno, VECTOR(value_t) args)
process the arguments on the same line as the heading
Definition tofino/bf-asm/deparser.cpp:284
Definition tofino/bf-asm/deparser.h:137
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58
Definition tofino/bf-asm/deparser.h:165
Definition tofino/bf-asm/deparser.cpp:33
Definition tofino/bf-asm/deparser.cpp:205
Definition tofino/bf-asm/deparser.h:44
Definition asm-types.h:150
Definition asm-types.h:114