P4C
The P4 Compiler
Loading...
Searching...
No Matches
programMap.h
1
/*
2
Copyright 2013-present Barefoot Networks, Inc.
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
#ifndef FRONTENDS_COMMON_PROGRAMMAP_H_
18
#define FRONTENDS_COMMON_PROGRAMMAP_H_
19
20
#include "ir/ir.h"
21
#include "lib/log.h"
22
23
namespace
P4
{
24
25
// Base class for various maps.
26
// A map is computed on a certain P4Program.
27
// If the program has not changed, the map is up-to-date.
28
class
ProgramMap
:
public
IHasDbPrint
{
29
protected
:
30
const
IR::P4Program *fake =
new
IR::P4Program();
31
const
IR::P4Program *program =
nullptr
;
32
cstring
mapKind;
33
explicit
ProgramMap
(std::string_view kind) : mapKind(kind) {}
34
virtual
~ProgramMap
() {}
35
36
public
:
37
// Check if map is up-to-date for the specified node; return true if it is
38
bool
checkMap(
const
IR::Node
*node)
const
{
39
if
(node == program) {
40
// program has not changed
41
LOG2(mapKind <<
" is up-to-date"
);
42
return
true
;
43
}
else
{
44
LOG2(
"Program has changed from "
<< dbp(program) <<
" to "
<< dbp(node));
45
}
46
return
false
;
47
}
48
void
validateMap(
const
IR::Node
*node)
const
{
49
if
(node ==
nullptr
|| !node->
is
<IR::P4Program>() || program ==
nullptr
)
return
;
50
if
(program != node)
51
BUG(
"Invalid map %1%: computed for %2%, used for %3%"
, mapKind, dbp(program),
52
dbp(node));
53
}
54
void
updateMap(
const
IR::Node
*node) {
55
if
(node ==
nullptr
|| !node->
is
<IR::P4Program>())
return
;
56
program = node->
to
<IR::P4Program>();
57
LOG2(mapKind <<
" updated to "
<< dbp(node));
58
}
59
void
clear() {
60
// This ensures that a clear map is never up-to-date,
61
// since the 'fake' node cannot appear in a program.
62
program = fake;
63
}
64
};
65
66
}
// namespace P4
67
68
#endif
/* FRONTENDS_COMMON_PROGRAMMAP_H_ */
P4::IHasDbPrint
Definition
stringify.h:33
P4::IR::Node
Definition
node.h:95
P4::ProgramMap
Definition
programMap.h:28
P4::cstring
Definition
cstring.h:85
P4
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition
applyOptionsPragmas.cpp:24
P4::RTTI::Base::to
T * to() noexcept
Definition
rtti.h:226
P4::RTTI::Base::is
bool is() const noexcept
Definition
rtti.h:216
frontends
common
programMap.h
Generated by
1.12.0