• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MAPLE_IR_INCLUDE_TYPES_DEF_H
17 #define MAPLE_IR_INCLUDE_TYPES_DEF_H
18 
19 // NOTE: Since we already committed to -std=c++0x, we should eventually use the
20 // standard definitions in the <cstdint> and <limits> headers rather than
21 // reinventing our own primitive types.
22 #include <cstdint>
23 #include <cstddef>
24 #include <functional>
25 #include "mpl_number.h"
26 
27 namespace maple {
28 // Let's keep the following definitions so that existing code will continue to work.
29 using int8 = std::int8_t;
30 using int16 = std::int16_t;
31 using int32 = std::int32_t;
32 using int64 = std::int64_t;
33 using uint8 = std::uint8_t;
34 using uint16 = std::uint16_t;
35 using uint32 = std::uint32_t;
36 using uint64 = std::uint64_t;
37 typedef unsigned int uint;
38 
39 class StIdx {  // scope nesting level + symbol table index
40 public:
41     union un {
42         struct {
43             uint32 idx : 24;
44             uint8 scope;  // scope level, with the global scope is at level 1
45         } scopeIdx;
46 
47         uint32 fullIdx;
48     };
49 
StIdx()50     StIdx()
51     {
52         u.fullIdx = 0;
53     }
54 
StIdx(uint32 level,uint32 i)55     StIdx(uint32 level, uint32 i)
56     {
57         u.scopeIdx.scope = level;
58         u.scopeIdx.idx = i;
59     }
60 
StIdx(uint32 fidx)61     StIdx(uint32 fidx)
62     {
63         u.fullIdx = fidx;
64     }
65 
66     ~StIdx() = default;
67 
Idx()68     uint32 Idx() const
69     {
70         return u.scopeIdx.idx;
71     }
72 
SetIdx(uint32 idx)73     void SetIdx(uint32 idx)
74     {
75         u.scopeIdx.idx = idx;
76     }
77 
Scope()78     uint32 Scope() const
79     {
80         return u.scopeIdx.scope;
81     }
82 
SetScope(uint32 scpe)83     void SetScope(uint32 scpe)
84     {
85         u.scopeIdx.scope = static_cast<uint8>(scpe);
86     }
87 
FullIdx()88     uint32 FullIdx() const
89     {
90         return u.fullIdx;
91     }
92 
SetFullIdx(uint32 idx)93     void SetFullIdx(uint32 idx)
94     {
95         u.fullIdx = idx;
96     }
97 
Islocal()98     bool Islocal() const
99     {
100         return u.scopeIdx.scope > 1;
101     }
102 
IsGlobal()103     bool IsGlobal() const
104     {
105         return u.scopeIdx.scope == 1;
106     }
107 
108     bool operator==(const StIdx &x) const
109     {
110         return u.fullIdx == x.u.fullIdx;
111     }
112 
113     bool operator!=(const StIdx &x) const
114     {
115         return !(*this == x);
116     }
117 
118     bool operator<(const StIdx &x) const
119     {
120         return u.fullIdx < x.u.fullIdx;
121     }
122 
123 private:
124     un u;
125 };
126 
127 using LabelIdx = uint32;
128 using phyRegIdx = uint64;
129 using OfstRegIdx = uint64;
130 using LabelIDOrder = uint32;
131 using PUIdx = uint32;
132 using PregIdx = int32;
133 using ExprIdx = int32;
134 using FieldID = int32;
135 
136 class TypeTag;
137 using TyIdx = utils::Index<TypeTag, uint32>;  // global type table index
138 
139 class GStrTag;
140 using GStrIdx = utils::Index<GStrTag, uint32>;  // global string table index
141 
142 class UStrTag;
143 using UStrIdx = utils::Index<UStrTag, uint32>;  // user string table index (from the conststr opcode)
144 
145 class U16StrTag;
146 using U16StrIdx = utils::Index<U16StrTag, uint32>;  // user string table index (from the conststr opcode)
147 
148 const TyIdx kInitTyIdx = TyIdx(0);
149 const TyIdx kNoneTyIdx = TyIdx(UINT32_MAX);
150 
151 enum SSALevel : uint8 {
152     kSSAInvalid = 0x00,
153     kSSATopLevel = 0x01,                        // ssa only for local top-level is valid
154     kSSAAddrTaken = 0x02,                       // ssa only for addr-taken is valid
155     kSSAMemory = kSSATopLevel | kSSAAddrTaken,  // ssa for both top-level and addr-taken is valid
156     kSSAHSSA = 0x04,                            // hssa is valid
157 };
158 
159 constexpr uint8 kOperandNumUnary = 1;
160 constexpr uint8 kOperandNumBinary = 2;
161 constexpr uint8 kOperandNumTernary = 3;
162 }  // namespace maple
163 namespace std {
164 template <>  // function-template-specialization
165 class hash<maple::StIdx> {
166 public:
operator()167     size_t operator()(const maple::StIdx &x) const
168     {
169         std::size_t seed = 0;
170         hash_combine(seed, x.Scope());
171         hash_combine(seed, x.Idx());
172         return seed;
173     }
174 };
175 }  // namespace std
176 #endif  // MAPLE_IR_INCLUDE_TYPES_DEF_H
177