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
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 auto begin() { return repr.begin(); }
57 auto begin() const { return repr.begin(); }
58 auto end() { return repr.end(); }
59 auto end() const { return repr.end(); }
60};
61
93class DoConvertEnums : public Transform {
94 friend class ConvertEnums;
95
98 TypeMap *typeMap;
99
100 public:
102 : policy(policy), typeMap(typeMap) {
103 CHECK_NULL(policy);
104 CHECK_NULL(typeMap);
105 setName("DoConvertEnums");
106 }
107 const IR::Node *preorder(IR::Type_Enum *type) override;
108 const IR::Node *postorder(IR::Type_Name *type) override;
109 const IR::Node *postorder(IR::Member *expression) override;
110};
111
112class ConvertEnums : public PassManager {
113 DoConvertEnums *convertEnums{nullptr};
114
115 public:
116 using EnumMapping = decltype(DoConvertEnums::repr);
118 TypeChecking *typeChecking = nullptr)
119 : convertEnums(new DoConvertEnums(policy, typeMap)) {
120 if (!typeChecking) typeChecking = new TypeChecking(nullptr, typeMap);
121 passes.push_back(typeChecking);
122 passes.push_back(convertEnums);
123 passes.push_back(new ClearTypeMap(typeMap));
124 setName("ConvertEnums");
125 }
126
127 EnumMapping getEnumMapping() const { return convertEnums->repr; }
128};
129
136 bool convert(const IR::Type_Enum *type) const override {
137 if (type->srcInfo.isValid()) {
138 return true;
139 }
140 return true;
141 }
142 unsigned enumSize(unsigned) const override { return 32; }
143};
144
145} // namespace P4
146
147#endif /* MIDEND_CONVERTENUMS_H_ */
Definition convertEnums.h:29
Definition typeChecker.h:32
Definition convertEnums.h:112
Definition convertEnums.h:93
Definition convertEnums.h:135
Definition convertEnums.h:44
Definition node.h:94
Definition ir/pass_manager.h:40
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition source_file.h:131
Definition cstring.h:85
Definition ordered_map.h:32
Definition string_map.h:41
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24