P4C
The P4 Compiler
Loading...
Searching...
No Matches
checked_array.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_CHECKED_ARRAY_H_
19#define BACKENDS_TOFINO_BF_ASM_CHECKED_ARRAY_H_
20
21#include <stdlib.h>
22
23#include "bfas.h" // to get at the options
24#include "lib/log.h"
25
26void print_regname(std::ostream &out, const void *addr, const void *end);
27
28template <size_t S, typename T>
29class checked_array;
30template <size_t S, typename T>
31std::ostream &operator<<(std::ostream &out, checked_array<S, T> *arr);
32
33template <typename T>
35 public:
36 virtual T &operator[](size_t) = 0;
37 virtual const T &operator[](size_t) const = 0;
38 virtual size_t size() const = 0;
39 virtual T *begin() = 0;
40 virtual T *end() = 0;
41 virtual bool modified() const = 0;
42 virtual void set_modified(bool v = true) = 0;
43 virtual bool disabled() const = 0;
44 virtual bool disable() = 0;
45 virtual bool disable_if_zero() = 0;
46 virtual void enable() = 0;
47};
48
49template <size_t S, typename T>
50class checked_array : public checked_array_base<T> {
51 bool disabled_;
52 T data[S];
53
54 public:
55 checked_array() : disabled_(false) {}
56 template <class U>
57 explicit checked_array(U v) : disabled_(false) {
58 for (auto &e : data) new (&e) T(v);
59 }
60 template <class U>
61 checked_array(const std::initializer_list<U> &v) : disabled_(false) {
62 auto it = v.begin();
63 for (auto &e : data) {
64 if (it == v.end()) break;
65 new (&e) T(*it++);
66 }
67 }
68 T &operator[](size_t idx) {
69 if (idx >= S) {
70 LOG1("ERROR: array index " << idx << " out of bounds " << this);
71 BUG("array index %zu out of bounds (%zu)", idx, S);
72 }
73 return data[idx];
74 }
75 const T &operator[](size_t idx) const {
76 if (idx >= S) {
77 LOG1("ERROR: array index " << idx << " out of bounds " << this);
78 BUG("array index %zu out of bounds (%zu)", idx, S);
79 }
80 return data[idx];
81 }
82 size_t size() const { return S; }
83 T *begin() { return data; }
84 T *end() { return data + S; }
85 bool modified() const {
86 for (size_t i = 0; i < S; i++)
87 if (data[i].modified()) return true;
88 return false;
89 }
90 void set_modified(bool v = true) {
91 for (size_t i = 0; i < S; i++) data[i].set_modified(v);
92 }
93 bool disabled() const { return disabled_; }
94 bool disable() {
95 bool rv = true;
96 for (size_t i = 0; i < S; i++)
97 if (!data[i].disable()) rv = false;
98 if (rv) disabled_ = true;
99 return rv;
100 }
101 void enable() {
102 disabled_ = false;
103 for (size_t i = 0; i < S; i++) data[i].enable();
104 }
105 bool disable_if_unmodified() {
106 bool rv = true;
107 for (size_t i = 0; i < S; i++)
108 if (!data[i].disable_if_unmodified()) rv = false;
109 if (rv && !options.gen_json) {
110 /* Can't actually disable arrays when generating json, as walle doesn't like it,
111 * but allow containing object to be disabled */
112 disabled_ = true;
113 }
114 return rv;
115 }
116 bool disable_if_zero() {
117 bool rv = true;
118 for (size_t i = 0; i < S; i++)
119 if (!data[i].disable_if_zero()) rv = false;
120 if (rv && !options.gen_json) {
121 /* Can't actually disable arrays when generating json, as walle doesn't like it,
122 * but allow containing object to be disabled */
123 disabled_ = true;
124 }
125 return rv;
126 }
127 bool disable_if_reset_value() {
128 bool rv = true;
129 for (size_t i = 0; i < S; i++)
130 if (!data[i].disable_if_reset_value()) rv = false;
131 if (rv && !options.gen_json) {
132 /* Can't actually disable arrays when generating json, as walle doesn't like it,
133 * but allow containing object to be disabled */
134 disabled_ = true;
135 }
136 return rv;
137 }
138};
139
140template <size_t S, typename T>
141inline std::ostream &operator<<(std::ostream &out, checked_array<S, T> *arr) {
142 print_regname(out, arr, arr + 1);
143 return out;
144}
145
146#endif /* BACKENDS_TOFINO_BF_ASM_CHECKED_ARRAY_H_ */
Definition checked_array.h:34
Definition checked_array.h:50