P4C
The P4 Compiler
Loading...
Searching...
No Matches
replaceSelectRange.h
1/*
2 * Copyright 2020, MNK Labs & Consulting
3 * http://mnkcg.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18#ifndef MIDEND_REPLACESELECTRANGE_H_
19#define MIDEND_REPLACESELECTRANGE_H_
20
21#include <iomanip>
22#include <iostream>
23#include <utility>
24#include <vector>
25
26#include "../lib/big_int_util.h"
27#include "frontends/p4/typeChecking/typeChecker.h"
28#include "ir/ir.h"
29
30namespace P4 {
31
37 public:
38 // number of new cases generated for ternary operations due to a range
39 // Each case is a key set expression.
40 const uint MAX_CASES;
41 // An index i is in this set if selectExpression->components[i] needs to be
42 // cast from int to bit. This is needed if and only if the expression at
43 // position i is of int type and there is a label that has in the i-th
44 // position a range expression. This is needed since we replace ranges with
45 // masks and masks are only defined for signed types.
46 std::set<size_t> signedIndicesToReplace;
47
48 explicit DoReplaceSelectRange(uint max) : MAX_CASES(max) { setName("DoReplaceSelectRange"); }
49
50 const IR::Node *postorder(IR::SelectExpression *e) override;
51 const IR::Node *postorder(IR::SelectCase *p) override;
52
53 std::vector<const IR::Mask *> *rangeToMasks(const IR::Range *, size_t);
54 std::vector<IR::Vector<IR::Expression>> cartesianAppend(
55 const std::vector<IR::Vector<IR::Expression>> &vecs,
56 const std::vector<const IR::Mask *> &masks);
57 std::vector<IR::Vector<IR::Expression>> cartesianAppend(
58 const std::vector<IR::Vector<IR::Expression>> &vecs, const IR::Expression *e);
59};
60
61class ReplaceSelectRange final : public PassManager {
62 public:
63 ReplaceSelectRange(ReferenceMap *refMap, TypeMap *typeMap) {
64 passes.push_back(new TypeChecking(refMap, typeMap));
65 passes.push_back(new DoReplaceSelectRange(100));
66 setName("ReplaceSelectRange");
67 }
68};
69
70} // namespace P4
71
72#endif /* MIDEND_REPLACESELECTRANGE_H_ */
Definition replaceSelectRange.h:36
Definition node.h:95
Definition vector.h:58
Definition pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition replaceSelectRange.h:61
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24