18#ifndef BACKENDS_TOFINO_BF_ASM_MISC_H_
19#define BACKENDS_TOFINO_BF_ASM_MISC_H_
28#include "backends/tofino/bf-asm/json.h"
31auto setup_muxctl(T ®,
int val) ->
decltype((void)reg.enabled_2bit_muxctl_enable) {
32 reg.enabled_2bit_muxctl_select = val;
33 reg.enabled_2bit_muxctl_enable = 1;
36auto setup_muxctl(T ®,
int val) ->
decltype((void)reg.enabled_3bit_muxctl_enable) {
37 reg.enabled_3bit_muxctl_select = val;
38 reg.enabled_3bit_muxctl_enable = 1;
41auto setup_muxctl(T ®,
int val) ->
decltype((void)reg.enabled_4bit_muxctl_enable) {
42 reg.enabled_4bit_muxctl_select = val;
43 reg.enabled_4bit_muxctl_enable = 1;
46auto setup_muxctl(T ®,
int val) ->
decltype((void)reg.enabled_5bit_muxctl_enable) {
47 reg.enabled_5bit_muxctl_select = val;
48 reg.enabled_5bit_muxctl_enable = 1;
51auto setup_muxctl(T ®,
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;
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);
61template <
class T,
class U>
62T join(
const std::vector<T> &vec, U sep) {
65 for (
auto &el : vec) {
75extern int remove_name_tail_range(std::string &,
int *size =
nullptr);
78std::string int_to_hex_string(
unsigned val,
unsigned width);
81void add_cfg_reg(
json::vector &cfg_cache, std::string full_name, std::string name, std::string val);
83bool check_zero_string(
const std::string &s);
86std::string get_filename(
const char *s);
87std::string get_directory(
const char *s);
93void gen_instfield_name(
const std::string &fullname, std::string &instname, std::string &fieldname);
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;
106uint64_t bitMask(
unsigned size);
108uint64_t bitRange(
unsigned lo,
unsigned hi);
110int parity(uint32_t v);
111int parity_2b(uint32_t v);
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);
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) {
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());
147 if (!CHECKTYPE(value, tINT))
return false;
152 if (!std::in_range<IntType>(value.i)) {
153 format_error_message(value, lo, hi);
158 IntType converted(
static_cast<IntType
>(value.i));
159 if (converted < lo || converted > hi) {
160 format_error_message(value, lo, hi);
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);
171inline bool check_range_match(
const value_t &match,
const decltype(match_t::word0) mask,
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);
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);
186 m.word0 = ~static_cast<decltype(match_t::word0)>(i);
187 m.word1 =
static_cast<decltype(match_t::word0)
>(i);
190bool check_bigint_unsigned(
value_t value, uint32_t byte_width);
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;
205bool input_int_match(
const value_t value,
match_t &match,
int width);
212bool require_keys(
const value_t &data, std::set<const char *> keys);
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 asm-types.h:114