P4C
The P4 Compiler
Loading...
Searching...
No Matches
programMap.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_COMMON_PROGRAMMAP_H_
9#define FRONTENDS_COMMON_PROGRAMMAP_H_
10
11#include "ir/ir.h"
12#include "lib/log.h"
13
14namespace P4 {
15
16// Base class for various maps.
17// A map is computed on a certain P4Program.
18// If the program has not changed, the map is up-to-date.
19class ProgramMap : public IHasDbPrint {
20 protected:
21 const IR::P4Program *fake = new IR::P4Program();
22 const IR::P4Program *program = nullptr;
23 cstring mapKind;
24 explicit ProgramMap(std::string_view kind) : mapKind(kind) {}
25 virtual ~ProgramMap() {}
26
27 public:
28 // Check if map is up-to-date for the specified node; return true if it is
29 bool checkMap(const IR::Node *node) const {
30 if (node == program) {
31 // program has not changed
32 LOG2(mapKind << " is up-to-date");
33 return true;
34 } else {
35 LOG2("Program has changed from " << dbp(program) << " to " << dbp(node));
36 }
37 return false;
38 }
39 void validateMap(const IR::Node *node) const {
40 if (node == nullptr || !node->is<IR::P4Program>() || program == nullptr) return;
41 if (program != node)
42 BUG("Invalid map %1%: computed for %2%, used for %3%", mapKind, dbp(program),
43 dbp(node));
44 }
45 void updateMap(const IR::Node *node) {
46 if (node == nullptr || !node->is<IR::P4Program>()) return;
47 program = node->to<IR::P4Program>();
48 LOG2(mapKind << " updated to " << dbp(node));
49 }
50 void clear() {
51 // This ensures that a clear map is never up-to-date,
52 // since the 'fake' node cannot appear in a program.
53 program = fake;
54 }
55};
56
57} // namespace P4
58
59#endif /* FRONTENDS_COMMON_PROGRAMMAP_H_ */
Definition stringify.h:24
Definition node.h:44
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
T * to() noexcept
Definition rtti.h:226
bool is() const noexcept
Definition rtti.h:216