P4C
The P4 Compiler
Loading...
Searching...
No Matches
convertEnums.h
1/*
2Copyright 2016 VMware, 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 MIDEND_CONVERTENUMS_H_
18#define MIDEND_CONVERTENUMS_H_
19
20#include "frontends/p4/typeChecking/typeChecker.h"
21#include "ir/ir.h"
22
23namespace P4 {
24
30 public:
31 virtual ~ChooseEnumRepresentation() {}
32 // If true this type has to be converted.
33 virtual bool convert(const IR::Type_Enum *type) const = 0;
34 // Returns the value to be assigned to the enum Constant.
35 // Maps to the index of member in the enum by default.
36 virtual unsigned encoding(const IR::Type_Enum *, unsigned n) const { return n; }
37 // enumCount is the number of different enum values.
38 // The returned value is the width of Type_Bits used
39 // to represent the enum. Obviously, we must have
40 // 2^(return) >= enumCount.
41 virtual unsigned enumSize(unsigned enumCount) const = 0;
42};
43
45 std::map<cstring, unsigned> repr;
46
47 public:
48 const IR::Type_Bits *type;
49
50 EnumRepresentation(Util::SourceInfo srcInfo, unsigned width) {
51 type = IR::Type_Bits::get(srcInfo, width, false);
52 }
53 void add(cstring decl) { repr.emplace(decl, repr.size()); }
54 unsigned get(cstring decl) const { return ::P4::get(repr, decl); }
55
56 using iterator = decltype(repr)::iterator;
57 using const_iterator = decltype(repr)::const_iterator;
58 iterator begin() { return repr.begin(); }
59 const_iterator begin() const { return repr.begin(); }
60 iterator end() { return repr.end(); }
61 const_iterator end() const { return repr.end(); }
62};
63
95class DoConvertEnums : public Transform {
96 friend class ConvertEnums;
97
100 TypeMap *typeMap;
101
102 public:
104 : policy(policy), typeMap(typeMap) {
105 CHECK_NULL(policy);
106 CHECK_NULL(typeMap);
107 setName("DoConvertEnums");
108 }
109 const IR::Node *preorder(IR::Type_Enum *type) override;
110 const IR::Node *postorder(IR::Type_Name *type) override;
111 const IR::Node *postorder(IR::Member *expression) override;
112};
113
114class ConvertEnums : public PassManager {
115 DoConvertEnums *convertEnums{nullptr};
116
117 public:
118 using EnumMapping = decltype(DoConvertEnums::repr);
120 TypeChecking *typeChecking = nullptr)
121 : convertEnums(new DoConvertEnums(policy, typeMap)) {
122 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap);
123 passes.push_back(typeChecking);
124 passes.push_back(convertEnums);
125 passes.push_back(new ClearTypeMap(typeMap));
126 setName("ConvertEnums");
127 }
128
129 EnumMapping getEnumMapping() const { return convertEnums->repr; }
130};
131
138 bool convert(const IR::Type_Enum *type) const override {
139 if (type->srcInfo.isValid()) {
140 return true;
141 }
142 return true;
143 }
144 unsigned enumSize(unsigned) const override { return 32; }
145};
146
147} // namespace P4
148
149#endif /* MIDEND_CONVERTENUMS_H_ */
Definition convertEnums.h:29
Definition typeChecker.h:32
Definition convertEnums.h:114
Definition convertEnums.h:95
Definition convertEnums.h:137
Definition convertEnums.h:44
Definition node.h:95
Definition pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition source_file.h:124
Definition cstring.h:85
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24