P4C
The P4 Compiler
Loading...
Searching...
No Matches
register_reference.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_REGISTER_REFERENCE_H_
19#define BACKENDS_TOFINO_BF_ASM_REGISTER_REFERENCE_H_
20
21#include <functional>
22#include <iostream>
23
24#include "lib/log.h"
25
26/* used by `dump_unread` methods to hold a concatenation of string literals for printing.
27 * Allocated on the stack, the `pfx` chain prints the calling context */
28struct prefix {
29 const prefix *pfx;
30 const char *str; // should always be a string literal
31 prefix(const prefix *p, const char *s) : pfx(p), str(s) {}
32};
33
34inline std::ostream &operator<<(std::ostream &out, const prefix *p) {
35 if (p) {
36 if (p->pfx) out << p->pfx << '.';
37 out << p->str;
38 }
39 return out;
40}
41
42/* Class to link register trees together into a larger dag that will expand into a tree
43 * when dumped as binary (so trees that appear in mulitple places will be duplicated)
44 * 'name' is the json file name to use when dumping as cfg.json, and the name for logging
45 * 'tree' is the subtree to dump as binary at the appropriate offset
46 */
47template <class REG>
48class register_reference {
49 REG *tree = nullptr;
50 std::string name;
51
52 public:
53 mutable bool read = false, write = false, disabled_ = false;
54 register_reference() {}
55 register_reference(const register_reference &) = default;
56 register_reference(register_reference &&) = default;
57 register_reference &operator=(const register_reference &) & = default;
58 register_reference &operator=(register_reference &&) & = default;
59 ~register_reference() {}
60
61 register_reference &set(const char *a, REG *r) {
62 if (disabled_) LOG1("ERROR: Writing disabled register value in " << this);
63 if (write) LOG1("WARNING: Overwriting \"" << name << "\" with \"" << a << "\" in " << this);
64 name = a;
65 tree = r;
66 log();
67 write = true;
68 return *this;
69 }
70 const char *c_str() const { return name.c_str(); }
71 REG *operator->() const {
72 read = true;
73 return tree;
74 }
75 explicit operator bool() const { return tree != nullptr; }
76 bool modified() const { return write; }
77 void set_modified(bool v = true) { write = v; }
78 void rewrite() { write = false; }
79 // friend std::ostream &operator<<(std::ostream &out, const register_reference<REG> &u);
80 void enable() { disabled_ = false; }
81 bool disabled() const { return disabled_; }
82 bool disable_if_unmodified() { return false; }
83 bool disable_if_zero() { return false; }
84 bool disable_if_reset_value() { return false; }
85 bool disable() {
86 if (!name.empty()) {
87 LOG1("ERROR: Disabling modified register in " << this);
88 return false;
89 }
90 tree = nullptr;
91 disabled_ = true;
92 return true;
93 }
94 void log() const { LOG1(this << " = \"" << name << "\""); }
95};
96
97template <class REG>
98inline std::ostream &operator<<(std::ostream &out, const register_reference<REG> *u) {
99 print_regname(out, u, u + 1);
100 return out;
101}
102template <class REG>
103inline std::ostream &operator<<(std::ostream &out, const register_reference<REG> &u) {
104 if (!*u.c_str())
105 out << 0;
106 else
107 out << '"' << u.c_str() << '"';
108 return out;
109}
110
111#endif /* BACKENDS_TOFINO_BF_ASM_REGISTER_REFERENCE_H_ */
Definition register_reference.h:48
Definition register_reference.h:28