P4C
The P4 Compiler
Loading...
Searching...
No Matches
convertEnums.h
1/*
2 * Copyright 2016 VMware, Inc.
3 * SPDX-FileCopyrightText: 2016 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef MIDEND_CONVERTENUMS_H_
9#define MIDEND_CONVERTENUMS_H_
10
11#include "frontends/p4/typeChecking/typeChecker.h"
12#include "ir/ir.h"
13
14namespace P4 {
15
21 public:
22 virtual ~ChooseEnumRepresentation() {}
23 // If true this type has to be converted.
24 virtual bool convert(const IR::Type_Enum *type) const = 0;
25 // Returns the value to be assigned to the enum Constant.
26 // Maps to the index of member in the enum by default.
27 virtual unsigned encoding(const IR::Type_Enum *, unsigned n) const { return n; }
28 // enumCount is the number of different enum values.
29 // The returned value is the width of Type_Bits used
30 // to represent the enum. Obviously, we must have
31 // 2^(return) >= enumCount.
32 virtual unsigned enumSize(unsigned enumCount) const = 0;
33};
34
35class EnumRepresentation {
37
38 public:
39 const IR::Type_Bits *type;
40
41 EnumRepresentation(Util::SourceInfo srcInfo, unsigned width) {
42 type = IR::Type_Bits::get(srcInfo, width, false);
43 }
44 void add(cstring decl) { repr.emplace(decl, repr.size()); }
45 unsigned get(cstring decl) const { return ::P4::get(repr, decl); }
46
47 auto begin() { return repr.begin(); }
48 auto begin() const { return repr.begin(); }
49 auto end() { return repr.end(); }
50 auto end() const { return repr.end(); }
51};
52
84class DoConvertEnums : public Transform {
85 friend class ConvertEnums;
86
89 TypeMap *typeMap;
90
91 public:
92 DoConvertEnums(ChooseEnumRepresentation *policy, TypeMap *typeMap)
93 : policy(policy), typeMap(typeMap) {
94 CHECK_NULL(policy);
95 CHECK_NULL(typeMap);
96 setName("DoConvertEnums");
97 }
98 const IR::Node *preorder(IR::Type_Enum *type) override;
99 const IR::Node *postorder(IR::Type_Name *type) override;
100 const IR::Node *postorder(IR::Member *expression) override;
101};
102
103class ConvertEnums : public PassManager {
104 DoConvertEnums *convertEnums{nullptr};
105
106 public:
107 using EnumMapping = decltype(DoConvertEnums::repr);
108 ConvertEnums(TypeMap *typeMap, ChooseEnumRepresentation *policy,
109 TypeChecking *typeChecking = nullptr)
110 : convertEnums(new DoConvertEnums(policy, typeMap)) {
111 if (!typeChecking) typeChecking = new TypeChecking(nullptr, typeMap);
112 passes.push_back(typeChecking);
113 passes.push_back(convertEnums);
114 passes.push_back(new ClearTypeMap(typeMap));
115 setName("ConvertEnums");
116 }
117
118 EnumMapping getEnumMapping() const { return convertEnums->repr; }
119};
120
127 bool convert(const IR::Type_Enum *type) const override {
128 if (type->srcInfo.isValid()) {
129 return true;
130 }
131 return true;
132 }
133 unsigned enumSize(unsigned) const override { return 32; }
134};
135
136} // namespace P4
137
138#endif /* MIDEND_CONVERTENUMS_H_ */
Definition convertEnums.h:20
Definition typeChecker.h:32
Definition convertEnums.h:84
Definition convertEnums.h:126
Definition node.h:53
Definition visitor.h:442
Definition typeChecker.h:55
Definition typeMap.h:32
Definition source_file.h:132
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:13