P4C
The P4 Compiler
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
sections.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_SECTIONS_H_
19#define BACKENDS_TOFINO_BF_ASM_SECTIONS_H_
20
21#include <stdarg.h>
22
23#include <string>
24
25#include "asm-types.h"
26#include "backends/tofino/bf-asm/json.h"
27#include "bfas.h"
28#include "map.h"
29
33class Section : virtual public Parsable, virtual public Contextable {
34 static std::map<std::string, Section *> *sections;
35 std::string name;
36 bool isInput = false;
37 static Section *get(const char *name) { return ::get(sections, name); }
38
39 protected:
40 explicit Section(const char *name_) : name(name_) {
41 if (!sections) sections = new std::map<std::string, Section *>();
42 if (get(name_)) {
43 fprintf(stderr, "Duplicate section handler for %s\n", name_);
44 exit(1);
45 }
46 (*sections)[name] = this;
47 }
48 virtual ~Section() {
49 sections->erase(name);
50 if (sections->empty()) {
51 delete sections;
52 sections = 0;
53 }
54 }
56 virtual void start(int lineno, VECTOR(value_t) args) {}
58 virtual void process() {}
59
60 public:
61 static int start_section(int lineno, char *name, VECTOR(value_t) args) {
62 if (Section *sec = get(name)) {
63 int prev_error_count = error_count;
64 sec->isInput = true;
65 sec->start(lineno, args);
66 return error_count > prev_error_count;
67 } else {
68 warning(lineno, "Unknown section %s, ignoring\n", name);
69 return 1;
70 }
71 }
72 static void asm_section(char *name, VECTOR(value_t) args, value_t data) {
73 if (Section *sec = get(name)) sec->input(args, data);
74 }
75 static void process_all() {
76 if (sections)
77 for (auto &it : *sections) it.second->process();
78 }
79 static void output_all(json::map &ctxtJson) {
80 if (sections) {
81 for (auto &it : *sections) {
82 // Skip primitives to be called last
83 if (it.first == "primitives") continue;
84 it.second->output(ctxtJson);
85 }
86 auto &s = *sections;
87 if (s.count("primitives")) s["primitives"]->output(ctxtJson);
88 }
89 }
90 static bool no_sections_in_assembly() {
91 if (sections) {
92 for (auto &it : *sections) {
93 if (it.second->isInput) return false;
94 }
95 }
96 return true;
97 }
98 static bool section_in_assembly(const char *name) { return get(name)->isInput; }
99
100 public: // for gtest
101 static Section *test_get(const char *name) { return get(name); }
102};
103
104#endif /* BACKENDS_TOFINO_BF_ASM_SECTIONS_H_ */
Definition sections.h:33
virtual void process()
optionally process the data if not done during parsing
Definition sections.h:58
virtual void start(int lineno, VECTOR(value_t) args)
process the arguments on the same line as the heading
Definition sections.h:56
void warning(const char *format, Args &&...args)
Report a warning with the given message.
Definition lib/error.h:122
Definition asm-types.h:114