• 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 
FullIdx()83     uint32 FullIdx() const
84     {
85         return u.fullIdx;
86     }
87 
SetFullIdx(uint32 idx)88     void SetFullIdx(uint32 idx)
89     {
90         u.fullIdx = idx;
91     }
92 
Islocal()93     bool Islocal() const
94     {
95         return u.scopeIdx.scope > 1;
96     }
97 
IsGlobal()98     bool IsGlobal() const
99     {
100         return u.scopeIdx.scope == 1;
101     }
102 
103     bool operator==(const StIdx &x) const
104     {
105         return u.fullIdx == x.u.fullIdx;
106     }
107 
108     bool operator!=(const StIdx &x) const
109     {
110         return !(*this == x);
111     }
112 
113     bool operator<(const StIdx &x) const
114     {
115         return u.fullIdx < x.u.fullIdx;
116     }
117 
118 private:
119     un u;
120 };
121 
122 using LabelIdx = uint32;
123 using phyRegIdx = uint64;
124 using OfstRegIdx = uint64;
125 using LabelIDOrder = uint32;
126 using PUIdx = uint32;
127 using PregIdx = int32;
128 using ExprIdx = int32;
129 using FieldID = int32;
130 
131 class TypeTag;
132 using TyIdx = utils::Index<TypeTag, uint32>;  // global type table index
133 
134 class GStrTag;
135 using GStrIdx = utils::Index<GStrTag, uint32>;  // global string table index
136 
137 class UStrTag;
138 using UStrIdx = utils::Index<UStrTag, uint32>;  // user string table index (from the conststr opcode)
139 
140 class U16StrTag;
141 using U16StrIdx = utils::Index<U16StrTag, uint32>;  // user string table index (from the conststr opcode)
142 
143 const TyIdx kInitTyIdx = TyIdx(0);
144 const TyIdx kNoneTyIdx = TyIdx(UINT32_MAX);
145 
146 enum SSALevel : uint8 {
147     kSSAInvalid = 0x00,
148     kSSATopLevel = 0x01,                        // ssa only for local top-level is valid
149     kSSAAddrTaken = 0x02,                       // ssa only for addr-taken is valid
150     kSSAMemory = kSSATopLevel | kSSAAddrTaken,  // ssa for both top-level and addr-taken is valid
151     kSSAHSSA = 0x04,                            // hssa is valid
152 };
153 
154 constexpr uint8 kOperandNumUnary = 1;
155 constexpr uint8 kOperandNumBinary = 2;
156 constexpr uint8 kOperandNumTernary = 3;
157 }  // namespace maple
158 namespace std {
159 template <>  // function-template-specialization
160 class hash<maple::StIdx> {
161 public:
operator()162     size_t operator()(const maple::StIdx &x) const
163     {
164         std::size_t seed = 0;
165         hash_combine(seed, x.Scope());
166         hash_combine(seed, x.Idx());
167         return seed;
168     }
169 };
170 }  // namespace std
171 #endif  // MAPLE_IR_INCLUDE_TYPES_DEF_H
172