P4C
The P4 Compiler
Loading...
Searching...
No Matches
rename_keys.h
1/*******************************************************************************
2 * Copyright (C) 2024 Intel Corporation
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,
11 * software 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
14 * and limitations under the License.
15 *
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 ******************************************************************************/
19
20#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_TOFINO_RENAME_KEYS_H_
21#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_TOFINO_RENAME_KEYS_H_
22
23#include <ir/pass_manager.h>
24
25#include <regex> // NOLINT
26
27#include "ir/ir.h"
28#include "lib/cstring.h"
29
30#include "backends/p4tools/modules/testgen/options.h"
31
32namespace P4::P4Tools::P4Testgen::Tofino {
33
55class ProcessKeyElems : public Transform {
56 public:
57 ProcessKeyElems() { setName("ProcessKeyElems"); }
58
63 IR::Node *postorder(IR::KeyElement *keyElem) override {
64 const auto *nameAnnot = keyElem->getAnnotation(IR::Annotation::nameAnnotation);
65 CHECK_NULL(nameAnnot);
66 cstring keyName = nameAnnot->getName();
67
68 auto isReplaced = false;
69
70 const auto *foundValid = keyName.find(".$valid");
71 if (foundValid != nullptr) {
72 // Remove the trailing '$'.
73 keyName = keyName.exceptLast(1);
74 isReplaced = true;
75 }
76
77 // Replace header stack indices hdr[<index>] with hdr$<index>. This
78 // is output in the context.json
79 std::regex hdrStackRegex(R"(\[([0-9]+)\])");
80 auto indexName = std::regex_replace(keyName.c_str(), hdrStackRegex, "$$$1");
81 if (indexName != keyName.c_str()) {
82 keyName = cstring::to_cstring(indexName);
83 isReplaced = true;
84 }
85
86 if (isReplaced) {
87 ::warning("Replacing invalid key %1% with %2% for key %3%. ", keyName, keyName,
88 keyElem);
89 // Update the key's annotation with the new fieldName.
90 keyElem->addOrReplaceAnnotation(IR::Annotation::nameAnnotation,
91 new IR::StringLiteral(keyName));
92 return keyElem;
93 }
94
95 return keyElem;
96 }
97};
98
101class ProcessAnnotatedTables : public Transform {
102 class ReplaceExactKeyMatches : public Transform {
103 public:
104 ReplaceExactKeyMatches() { setName("ReplaceExactKeyMatches"); }
105 IR::KeyElement *preorder(IR::KeyElement *keyElem) override {
106 const auto *keyElemMatchType = keyElem->matchType;
107 if (keyElemMatchType->path->name == "exact") {
108 auto *newMatchType = keyElemMatchType->clone();
109 newMatchType->path = new IR::Path("ternary");
110 keyElem->matchType = newMatchType;
111 }
112 return keyElem;
113 }
114 };
115
116 public:
117 ProcessAnnotatedTables() { setName("ProcessAnnotatedTables"); }
118 const IR::Node *preorder(IR::P4Table *table) override {
119 if (table->getAnnotation("ternary"_cs) != nullptr) {
120 return table->apply(ReplaceExactKeyMatches());
121 }
122 return table;
123 }
124};
125
126class RenameKeys : public PassManager {
127 public:
128 RenameKeys() {
129 setName("RenameKeys");
130 passes.emplace_back(new ProcessKeyElems());
131 if (TestgenOptions::get().testBackend == "STF") {
132 passes.emplace_back(new ProcessAnnotatedTables());
133 }
134 }
135};
136
137} // namespace P4::P4Tools::P4Testgen::Tofino
138
139#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_TOFINO_RENAME_KEYS_H_ */
Definition node.h:94
static TestgenOptions & get()
Definition backends/p4tools/modules/testgen/options.cpp:23
IR::Node * postorder(IR::KeyElement *keyElem) override
Definition rename_keys.h:63
Definition cstring.h:85
void warning(const char *format, Args &&...args)
Report a warning with the given message.
Definition lib/error.h:122