P4C
The P4 Compiler
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
match_source.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_MATCH_SOURCE_H_
19#define BACKENDS_TOFINO_BF_ASM_MATCH_SOURCE_H_
20
21#include <sstream>
22#include <string>
23
24#include "backends/tofino/bf-asm/asm-types.h"
25#include "lib/stringify.h"
26
32class MatchSource : public IHasDbPrint {
33 public:
34 virtual int fieldlobit() const = 0;
35 virtual int fieldhibit() const = 0;
36 virtual unsigned size() const = 0;
37 virtual int slicelobit() const = 0;
38 virtual int slicehibit() const = 0;
39 virtual const char *name() const = 0;
40 virtual int get_lineno() const = 0;
41 virtual std::string toString() const = 0;
42 virtual void dbprint(std::ostream &out) const = 0;
43};
44
48class HashMatchSource : public MatchSource {
49 int lo = 0;
50 int hi = 0;
51
52 public:
53 int lineno = 0;
54 HashMatchSource(int line, int l, int h) : lo(l), hi(h), lineno(line) {}
55 explicit HashMatchSource(value_t value) {
56 if (CHECKTYPE(value, tCMD)) {
57 lineno = value.lineno;
58 if (value != "hash_group")
59 error(value.lineno, "Hash Match source must come from a hash group");
60 if (value.vec.size != 2) error(value.lineno, "Hash Match source requires a range");
61 if (CHECKTYPE(value.vec[1], tRANGE)) {
62 lo = value.vec[1].range.lo;
63 hi = value.vec[1].range.hi;
64 }
65 }
66 }
67
68 int get_lineno() const override { return lineno; }
69 int fieldlobit() const override { return lo < 0 ? 0 : lo; }
70 int fieldhibit() const override { return hi < 0 ? 0 : hi; }
71 unsigned size() const override { return hi >= lo && lo >= 0 ? hi - lo + 1 : 0; }
72 int slicelobit() const override { return fieldlobit(); }
73 int slicehibit() const override { return fieldhibit(); }
74 const char *name() const override { return "hash_group"; }
75 std::string toString() const override {
76 std::stringstream str;
77 str << *this;
78 return str.str();
79 }
80
81 void dbprint(std::ostream &out) const override {
82 out << name() << "(" << lo << ".." << hi << ")";
83 }
84};
85
86#endif /* BACKENDS_TOFINO_BF_ASM_MATCH_SOURCE_H_ */
Definition match_source.h:32
Definition stringify.h:33
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58
Definition asm-types.h:114