P4C
The P4 Compiler
Loading...
Searching...
No Matches
misc.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_MISC_H_
19#define BACKENDS_TOFINO_BF_ASM_MISC_H_
20
21#include <memory>
22#include <sstream>
23#include <string>
24#include <type_traits>
25#include <vector>
26
27#include "asm-types.h"
28#include "backends/tofino/bf-asm/json.h"
29
30template <class T>
31auto setup_muxctl(T &reg, int val) -> decltype((void)reg.enabled_2bit_muxctl_enable) {
32 reg.enabled_2bit_muxctl_select = val;
33 reg.enabled_2bit_muxctl_enable = 1;
34}
35template <class T>
36auto setup_muxctl(T &reg, int val) -> decltype((void)reg.enabled_3bit_muxctl_enable) {
37 reg.enabled_3bit_muxctl_select = val;
38 reg.enabled_3bit_muxctl_enable = 1;
39}
40template <class T>
41auto setup_muxctl(T &reg, int val) -> decltype((void)reg.enabled_4bit_muxctl_enable) {
42 reg.enabled_4bit_muxctl_select = val;
43 reg.enabled_4bit_muxctl_enable = 1;
44}
45template <class T>
46auto setup_muxctl(T &reg, int val) -> decltype((void)reg.enabled_5bit_muxctl_enable) {
47 reg.enabled_5bit_muxctl_select = val;
48 reg.enabled_5bit_muxctl_enable = 1;
49}
50template <class T>
51auto setup_muxctl(T &reg, int val) -> decltype((void)reg.exactmatch_row_vh_xbar_enable) {
52 reg.exactmatch_row_vh_xbar_select = val;
53 reg.exactmatch_row_vh_xbar_enable = 1;
54}
55
56template <class T, class Alloc>
57void append(std::vector<T, Alloc> &a, const std::vector<T, Alloc> &b) {
58 for (auto &e : b) a.push_back(e);
59}
60
61template <class T, class U>
62T join(const std::vector<T> &vec, U sep) {
63 T rv;
64 bool first = true;
65 for (auto &el : vec) {
66 if (first)
67 first = false;
68 else
69 rv += sep;
70 rv += el;
71 }
72 return rv;
73}
74
75extern int remove_name_tail_range(std::string &, int *size = nullptr);
76
77// Convert an integer to hex string of specified width (in bytes)
78std::string int_to_hex_string(unsigned val, unsigned width);
79
80// Add a reg to CJSON Configuration Cache
81void add_cfg_reg(json::vector &cfg_cache, std::string full_name, std::string name, std::string val);
82
83bool check_zero_string(const std::string &s);
84
85// Get filename
86std::string get_filename(const char *s);
87std::string get_directory(const char *s);
88
93void gen_instfield_name(const std::string &fullname, std::string &instname, std::string &fieldname);
94
97template <class T>
98struct ptrless {
99 bool operator()(const T *a, const T *b) const { return b ? a ? *a < *b : true : false; }
100 bool operator()(const std::unique_ptr<T> &a, const std::unique_ptr<T> &b) const {
101 return b ? a ? *a < *b : true : false;
102 }
103};
104
105/* word with size (lowest) bits set */
106uint64_t bitMask(unsigned size);
107/* word with range of bits from lo to hi (inclusive) set */
108uint64_t bitRange(unsigned lo, unsigned hi);
109
110int parity(uint32_t v);
111int parity_2b(uint32_t v); // two-bit parity (parity of pairs in the word)
112
113inline bool check_value(const value_t value, const decltype(value_t::i) expected) {
114 if (!CHECKTYPE(value, tINT)) return false;
115 if (value.i != expected) {
116 error(value.lineno, "unexpected value %ld; expected %ld", value.i, expected);
117 return false;
118 }
119 return true;
120}
121
136template <typename IntType,
137 typename = typename std::enable_if<std::is_integral<IntType>::value>::type>
138bool check_range_strict(value_t value, IntType lo, IntType hi) {
139 auto format_error_message([](value_t value, IntType lo, IntType hi) {
140 /* -- As we don't know actual type of the IntType, we cannot use the printf-like
141 * formatting. */
142 std::ostringstream oss;
143 oss << "value " << value.i << " is out of allowed range <" << +lo << "; " << +hi << ">";
144 error(value.lineno, "%s", oss.str().c_str());
145 });
146
147 if (!CHECKTYPE(value, tINT)) return false;
148
149 /* -- Handle different ranges (signed, unsigned, different size) of the value_t::i
150 * and IntType. */
151
152 if (!std::in_range<IntType>(value.i)) {
153 format_error_message(value, lo, hi);
154 return false;
155 }
156
157 /* -- Now check requested limits */
158 IntType converted(static_cast<IntType>(value.i));
159 if (converted < lo || converted > hi) {
160 format_error_message(value, lo, hi);
161 return false;
162 }
163 return true;
164}
165
166inline bool check_range(const value_t value, const decltype(value_t::i) lo,
167 const decltype(value_t::i) hi) {
168 return check_range_strict<decltype(value_t::i)>(value, lo, hi);
169}
170
171inline bool check_range_match(const value_t &match, const decltype(match_t::word0) mask,
172 int width) {
173 if (!CHECKTYPE(match, tMATCH)) return false;
174 if ((match.m.word0 | match.m.word1) != mask) {
175 error(match.lineno, "invalid match width; expected %i bits", width);
176 return false;
177 }
178 return true;
179}
180
181template <typename IntType>
182void convert_i2m(IntType i, match_t &m) {
183 static_assert(sizeof(IntType) == sizeof(match_t::word0));
184 static_assert(std::is_integral<IntType>::value);
185
186 m.word0 = ~static_cast<decltype(match_t::word0)>(i);
187 m.word1 = static_cast<decltype(match_t::word0)>(i);
188}
189
190bool check_bigint_unsigned(value_t value, uint32_t byte_width);
191
194inline void fix_match_star(match_t &match, const decltype(match_t::word0) mask) {
195 if (match.word0 == 0 && match.word1 == 0) match.word0 = match.word1 = mask;
196}
197
205bool input_int_match(const value_t value, match_t &match, int width);
206
212bool require_keys(const value_t &data, std::set<const char *> keys);
213
214#endif /* BACKENDS_TOFINO_BF_ASM_MISC_H_ */
Definition backends/tofino/bf-asm/json.h:222
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58
Definition match.h:36
Definition misc.h:98
Definition asm-types.h:114