P4C
The P4 Compiler
Loading...
Searching...
No Matches
action.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_BMV2_COMMON_ACTION_H_
18#define BACKENDS_BMV2_COMMON_ACTION_H_
19
20#include "helpers.h"
21#include "ir/ir.h"
22
23namespace P4::BMV2 {
24
29 // The number of jump labels that have been created so far, for
30 // the current action where we are generating its primitives.
31 // Jump labels are allocated starting from 0 and incrementing from
32 // there.
33 int numLabels;
34 // The jump label that is positioned at the end of the action
35 // body. This is needed if we wish to implement a 'return' or
36 // 'exit' statement within a conditionally executed part of an
37 // action body, to jump to the end of the action.
38 int labelIdEndOfAction;
39 // Let F be the set of offsets in the action that contain a
40 // primitive "_jump" or _jump_if_zero". For each offset f in F,
41 // offsetToTargetLabelId[f] is the label ID to which the primitive
42 // should jump.
43 std::map<unsigned int, unsigned int> offsetToTargetLabelId;
44 // For each offset f in F, offsetToJumpParams[f] is the array
45 // of parameters that should be used as operands to the primitive,
46 // except for the offset, which will only be added at the end of
47 // method convertActionBodyTop.
48 std::map<unsigned int, Util::JsonArray *> offsetToJumpParams;
49 // labelIdToJumpOffset[labelId] equal to offset means that the
50 // position of that labelId within the array of primitives for an
51 // action is 'offset'. _jump and _jump_if_zero primitive actions
52 // must use 'offset' as their offset parameter in order to jump to
53 // that primitive.
54 std::map<unsigned int, unsigned int> labelIdToJumpOffset;
55};
56
57class ActionConverter : public Inspector {
59
60 void convertActionBodyTop(const IR::Vector<IR::StatOrDecl> *body, Util::JsonArray *result);
61 void convertActionBody(const IR::Vector<IR::StatOrDecl> *body, Util::JsonArray *result,
62 bool inConditional, JumpLabelInfo *info);
63 void convertActionParams(const IR::ParameterList *parameters, Util::JsonArray *params);
64 cstring jsonAssignment(const IR::Type *type);
65 void postorder(const IR::P4Action *action) override;
66
67 public:
68 const bool emitExterns;
69 explicit ActionConverter(ConversionContext *ctxt, const bool &emitExterns_)
70 : ctxt(ctxt), emitExterns(emitExterns_) {
71 setName("ConvertActions");
72 }
73};
74
75} // namespace P4::BMV2
76
77#endif /* BACKENDS_BMV2_COMMON_ACTION_H_ */
Definition action.h:57
Definition vector.h:59
Definition visitor.h:400
Definition json.h:115
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
Definition action.h:28
void info(const int kind, const char *format, const T *node, Args &&...args)
Report info messages of type kind. Requires that the node argument have source info.
Definition lib/error.h:148
Definition bmv2/common/helpers.h:297