• 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 class StIdx {  // scope nesting level + symbol table index
38 public:
39     union un {
40         struct {
41             uint32 idx : 24;
42             uint8 scope;  // scope level, with the global scope is at level 1
43         } scopeIdx;
44 
45         uint32 fullIdx;
46     };
47 
StIdx()48     StIdx()
49     {
50         u.fullIdx = 0;
51     }
52 
StIdx(uint32 level,uint32 i)53     StIdx(uint32 level, uint32 i)
54     {
55         u.scopeIdx.scope = level;
56         u.scopeIdx.idx = i;
57     }
58 
StIdx(uint32 fidx)59     StIdx(uint32 fidx)
60     {
61         u.fullIdx = fidx;
62     }
63 
64     ~StIdx() = default;
65 
Idx()66     uint32 Idx() const
67     {
68         return u.scopeIdx.idx;
69     }
70 
SetIdx(uint32 idx)71     void SetIdx(uint32 idx)
72     {
73         u.scopeIdx.idx = idx;
74     }
75 
Scope()76     uint32 Scope() const
77     {
78         return u.scopeIdx.scope;
79     }
80 
SetScope(uint32 scpe)81     void SetScope(uint32 scpe)
82     {
83         u.scopeIdx.scope = static_cast<uint8>(scpe);
84     }
85 
FullIdx()86     uint32 FullIdx() const
87     {
88         return u.fullIdx;
89     }
90 
SetFullIdx(uint32 idx)91     void SetFullIdx(uint32 idx)
92     {
93         u.fullIdx = idx;
94     }
95 
Islocal()96     bool Islocal() const
97     {
98         return u.scopeIdx.scope > 1;
99     }
100 
IsGlobal()101     bool IsGlobal() const
102     {
103         return u.scopeIdx.scope == 1;
104     }
105 
106     bool operator==(const StIdx &x) const
107     {
108         return u.fullIdx == x.u.fullIdx;
109     }
110 
111     bool operator!=(const StIdx &x) const
112     {
113         return !(*this == x);
114     }
115 
116     bool operator<(const StIdx &x) const
117     {
118         return u.fullIdx < x.u.fullIdx;
119     }
120 
121 private:
122     un u;
123 };
124 
125 using LabelIdx = uint32;
126 using phyRegIdx = uint64;
127 using OfstRegIdx = uint64;
128 using LabelIDOrder = uint32;
129 using PUIdx = uint32;
130 using PregIdx = int32;
131 using ExprIdx = int32;
132 using FieldID = int32;
133 
134 class TypeTag;
135 using TyIdx = utils::Index<TypeTag, uint32>;  // global type table index
136 
137 class GStrTag;
138 using GStrIdx = utils::Index<GStrTag, uint32>;  // global string table index
139 
140 class UStrTag;
141 using UStrIdx = utils::Index<UStrTag, uint32>;  // user string table index (from the conststr opcode)
142 
143 class U16StrTag;
144 using U16StrIdx = utils::Index<U16StrTag, uint32>;  // user string table index (from the conststr opcode)
145 
146 const TyIdx kInitTyIdx = TyIdx(0);
147 const TyIdx kNoneTyIdx = TyIdx(UINT32_MAX);
148 
149 enum SSALevel : uint8 {
150     kSSAInvalid = 0x00,
151     kSSATopLevel = 0x01,                        // ssa only for local top-level is valid
152     kSSAAddrTaken = 0x02,                       // ssa only for addr-taken is valid
153     kSSAMemory = kSSATopLevel | kSSAAddrTaken,  // ssa for both top-level and addr-taken is valid
154     kSSAHSSA = 0x04,                            // hssa is valid
155 };
156 
157 constexpr uint8 kOperandNumUnary = 1;
158 constexpr uint8 kOperandNumBinary = 2;
159 constexpr uint8 kOperandNumTernary = 3;
160 }  // namespace maple
161 namespace std {
162 template <>  // function-template-specialization
163 class hash<maple::StIdx> {
164 public:
operator()165     size_t operator()(const maple::StIdx &x) const
166     {
167         std::size_t seed = 0;
168         hash_combine(seed, x.Scope());
169         hash_combine(seed, x.Idx());
170         return seed;
171     }
172 };
173 }  // namespace std
174 #endif  // MAPLE_IR_INCLUDE_TYPES_DEF_H
175