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 MAPLEBE_INCLUDE_BE_COMMON_UTILS_H
17 #define MAPLEBE_INCLUDE_BE_COMMON_UTILS_H
18 #include <cstdint>
19 #include "types_def.h"
20 #include "mpl_logging.h"
21
22 namespace maplebe {
23 using namespace maple;
24 constexpr uint32 kOffsetAlignmentOf8Bit = 0;
25 constexpr uint32 kOffsetAlignmentOf16Bit = 1;
26 constexpr uint32 kOffsetAlignmentOf32Bit = 2;
27 constexpr uint32 kOffsetAlignmentOf64Bit = 3;
28 constexpr uint32 kOffsetAlignmentOf128Bit = 4;
29 constexpr uint32 kBaseOffsetAlignment = 3;
30 /*
31 * The constexpr implementations, without assertions. Suitable for using in
32 * constants.
33 */
34 constexpr uint32 k1FConst = 31;
35 constexpr uint32 k0BitSize = 0;
36 constexpr uint32 k1BitSize = 1;
37 constexpr uint32 k2BitSize = 2;
38 constexpr uint32 k3BitSize = 3;
39 constexpr uint32 k4BitSize = 4;
40 constexpr uint32 k5BitSize = 5;
41 constexpr uint32 k6BitSize = 6;
42 constexpr uint32 k7BitSize = 7;
43 constexpr uint32 k8BitSize = 8;
44 constexpr uint32 k16BitSize = 16;
45 constexpr uint32 k24BitSize = 24;
46 constexpr uint32 k32BitSize = 32;
47 constexpr uint32 k48BitSize = 48;
48 constexpr uint32 k56BitSize = 56;
49 constexpr uint32 k64BitSize = 64;
50 constexpr uint32 k128BitSize = 128;
51 constexpr uint32 k256BitSize = 256;
52 constexpr uint32 k512BitSize = 512;
53 constexpr uint32 k1024BitSize = 1024;
54 constexpr uint32 k2048BitSize = 2048;
55
56 constexpr int32 k1FConstInt = 31;
57 constexpr int32 k0BitSizeInt = 0;
58 constexpr int32 k1BitSizeInt = 1;
59 constexpr int32 k2BitSizeInt = 2;
60 constexpr int32 k3BitSizeInt = 3;
61 constexpr int32 k4BitSizeInt = 4;
62 constexpr int32 k5BitSizeInt = 5;
63 constexpr int32 k6BitSizeInt = 6;
64 constexpr int32 k7BitSizeInt = 7;
65 constexpr int32 k8BitSizeInt = 8;
66 constexpr int32 k16BitSizeInt = 16;
67 constexpr int32 k24BitSizeInt = 24;
68 constexpr int32 k32BitSizeInt = 32;
69 constexpr int32 k48BitSizeInt = 48;
70 constexpr int32 k56BitSizeInt = 56;
71 constexpr int32 k64BitSizeInt = 64;
72 constexpr int32 k128BitSizeInt = 128;
73 constexpr int32 k256BitSizeInt = 256;
74 constexpr int32 k512BitSizeInt = 512;
75 constexpr int32 k1024BitSizeInt = 1024;
76
77 constexpr int32 kNegative8BitSize = -8;
78 constexpr int32 kNegative256BitSize = -256;
79 constexpr int32 kNegative512BitSize = -512;
80 constexpr int32 kNegative1024BitSize = -1024;
81
82 constexpr uint32 k1ByteSize = 1;
83 constexpr uint32 k2ByteSize = 2;
84 constexpr uint32 k3ByteSize = 3;
85 constexpr uint32 k4ByteSize = 4;
86 constexpr uint32 k8ByteSize = 8;
87 constexpr uint32 k9ByteSize = 9;
88 constexpr uint32 k12ByteSize = 12;
89 constexpr uint32 k14ByteSize = 14;
90 constexpr uint32 k15ByteSize = 15;
91 constexpr uint32 k16ByteSize = 16;
92 constexpr uint32 k32ByteSize = 32;
93 constexpr uint32 k64ByteSize = 64;
94
95 constexpr int32 k1ByteSizeInt = 1;
96 constexpr int32 k2ByteSizeInt = 2;
97 constexpr int32 k3ByteSizeInt = 3;
98 constexpr int32 k4ByteSizeInt = 4;
99 constexpr int32 k8ByteSizeInt = 8;
100 constexpr int32 k9ByteSizeInt = 9;
101 constexpr int32 k12ByteSizeInt = 12;
102 constexpr int32 k14ByteSizeInt = 14;
103 constexpr int32 k15ByteSizeInt = 15;
104 constexpr int32 k16ByteSizeInt = 16;
105 constexpr int32 k32ByteSizeInt = 32;
106
107 constexpr uint32 k1EightBytesSize = 8;
108 constexpr uint32 k2EightBytesSize = 16;
109 constexpr uint32 k3EightBytesSize = 24;
110 constexpr uint32 k4EightBytesSize = 32;
111
112 constexpr uint32 k4BitShift = 2; /* 4 is 1 << 2; */
113 constexpr uint32 k8BitShift = 3; /* 8 is 1 << 3; */
114 constexpr uint32 k16BitShift = 4; /* 16 is 1 << 4 */
115
116 constexpr uint32 kDwordSizeTwo = 2;
117
118 constexpr uint32 k4ByteFloatSize = 4;
119 constexpr uint32 k8ByteDoubleSize = 8;
120
121 /* Storage location of operands in one insn */
122 constexpr int32 kInsnFirstOpnd = 0;
123 constexpr int32 kInsnSecondOpnd = 1;
124 constexpr int32 kInsnThirdOpnd = 2;
125 constexpr int32 kInsnFourthOpnd = 3;
126 constexpr int32 kInsnFifthOpnd = 4;
127 constexpr int32 kInsnSixthOpnd = 5;
128 constexpr int32 kInsnSeventhOpnd = 6;
129 constexpr int32 kInsnEighthOpnd = 7;
130 constexpr int32 kInsnMaxOpnd = 8;
131
132 /* Reg of CCLocInfo */
133 constexpr uint32 kFirstReg = 0;
134 constexpr uint32 kSecondReg = 1;
135 constexpr uint32 kThirdReg = 2;
136 constexpr uint32 kFourthReg = 3;
137
138 /* inline asm operand designations */
139 constexpr uint32 kAsmStringOpnd = 0;
140 constexpr uint32 kAsmOutputListOpnd = 1;
141 constexpr uint32 kAsmClobberListOpnd = 2;
142 constexpr uint32 kAsmInputListOpnd = 3;
143 constexpr uint32 kAsmOutputConstraintOpnd = 4;
144 constexpr uint32 kAsmInputConstraintOpnd = 5;
145 constexpr uint32 kAsmOutputRegPrefixOpnd = 6;
146 constexpr uint32 kAsmInputRegPrefixOpnd = 7;
147
148 /* Number of registers */
149 constexpr uint32 kOneRegister = 1;
150 constexpr uint32 kTwoRegister = 2;
151 constexpr uint32 kThreeRegister = 3;
152 constexpr uint32 kFourRegister = 4;
153
154 /* position of an operand within an instruction */
155 constexpr uint32 kOperandPosition0 = 0;
156 constexpr uint32 kOperandPosition1 = 1;
157 constexpr uint32 kOperandPosition2 = 2;
158
159 /* Size of struct for memcpy */
160 constexpr uint32 kParmMemcpySize = 40;
161
162 /* Check whether the value is an even number. */
163 constexpr int32 kDivide2 = 2;
164 constexpr int32 kRegNum2 = 2;
165 constexpr int32 kStepNum2 = 2;
166 constexpr int32 kInsnNum2 = 2;
167 constexpr int32 kOpndNum2 = 2;
168
169 /* alignment in bytes of uint8 */
170 constexpr uint8 kAlignOfU8 = 3;
171
172 /*
173 * if the number of local refvar is less than 12, use stp or str to init local refvar
174 * else call function MCC_InitializeLocalStackRef to init.
175 */
176 constexpr int32 kRefNum12 = 12;
177
178 /* mod function max argument size */
179 constexpr uint32 kMaxModFuncArgSize = 8;
180
181 /* string length of spacial name "__EARetTemp__" */
182 constexpr int32 kEARetTempNameSize = 10;
183
184 /*
185 * Aarch64 data processing instructions have 12 bits of space for values in their instuction word
186 * This is arranged as a four-bit rotate value and an eight-bit immediate value:
187 */
188 constexpr uint32 kMaxImmVal5Bits = 5;
189 constexpr uint32 kMaxImmVal6Bits = 6;
190 constexpr uint32 kMaxImmVal8Bits = 8;
191 constexpr uint32 kMaxImmVal12Bits = 12;
192 constexpr uint32 kMaxImmVal13Bits = 13;
193 constexpr uint32 kMaxImmVal16Bits = 16;
194
195 constexpr int32 kMaxPimm8 = 4095;
196 constexpr int32 kMaxPimm16 = 8190;
197 constexpr int32 kMaxPimm32 = 16380;
198 constexpr int32 kMaxPimm64 = 32760;
199 constexpr int32 kMaxPimm128 = 65520;
200
201 constexpr int32 kMaxPimm[k5BitSize] = {kMaxPimm8, kMaxPimm16, kMaxPimm32, kMaxPimm64, kMaxPimm128};
202 constexpr int32 kMaxPairPimm[k3BitSize] = {k256BitSize, k512BitSize, k512BitSize};
203
204 constexpr int32 kMaxSimm32 = 255;
205 constexpr int32 kMaxSimm32Pair = 252;
206 constexpr int32 kMinSimm32 = kNegative256BitSize;
207 constexpr int32 kMaxSimm64Pair = 504;
208 constexpr int32 kMinSimm64 = kNegative512BitSize;
209
210 constexpr int32 kMax12UnsignedImm = 4096;
211 constexpr int32 kMax13UnsignedImm = 8192;
212 constexpr int32 kMax16UnsignedImm = 65535;
213
214 /* Dedicated for Vector */
215 constexpr int32 kMinImmVal = -128;
216 constexpr int32 kMaxImmVal = 255;
217
218 /* aarch64 assembly takes up to 24-bits */
219 constexpr uint32 kMaxImmVal24Bits = 24;
220
221 constexpr uint32 kDecimalMax = 10;
222
223 constexpr double kMicroSecPerMilliSec = 1000.0;
224
225 constexpr double kPercent = 100.0;
226
227 enum ConditionCode : uint8 {
228 CC_EQ, /* equal */
229 CC_NE, /* not equal */
230 CC_CS, /* carry set (== HS) */
231 CC_HS, /* unsigned higher or same (== CS) */
232 CC_CC, /* carry clear (== LO) */
233 CC_LO, /* Unsigned lower (== CC) */
234 CC_MI, /* Minus or negative result */
235 CC_PL, /* positive or zero result */
236 CC_VS, /* overflow */
237 CC_VC, /* no overflow */
238 CC_HI, /* unsigned higher */
239 CC_LS, /* unsigned lower or same */
240 CC_GE, /* signed greater than or equal */
241 CC_LT, /* signed less than */
242 CC_GT, /* signed greater than */
243 CC_LE, /* signed less than or equal */
244 CC_AL, /* always, this is the default. usually omitted. */
245 kCcLast
246 };
247
GetReverseCC(ConditionCode cc)248 inline ConditionCode GetReverseCC(ConditionCode cc)
249 {
250 switch (cc) {
251 case CC_NE:
252 return CC_EQ;
253 case CC_EQ:
254 return CC_NE;
255 case CC_HS:
256 return CC_LO;
257 case CC_LO:
258 return CC_HS;
259 case CC_MI:
260 return CC_PL;
261 case CC_PL:
262 return CC_MI;
263 case CC_VS:
264 return CC_VC;
265 case CC_VC:
266 return CC_VS;
267 case CC_HI:
268 return CC_LS;
269 case CC_LS:
270 return CC_HI;
271 case CC_LT:
272 return CC_GE;
273 case CC_GE:
274 return CC_LT;
275 case CC_GT:
276 return CC_LE;
277 case CC_LE:
278 return CC_GT;
279 default:
280 CHECK_FATAL(0, "unknown condition code");
281 }
282 return kCcLast;
283 }
GetReverseBasicCC(ConditionCode cc)284 inline ConditionCode GetReverseBasicCC(ConditionCode cc)
285 {
286 switch (cc) {
287 case CC_NE:
288 return CC_EQ;
289 case CC_EQ:
290 return CC_NE;
291 case CC_LT:
292 return CC_GE;
293 case CC_GE:
294 return CC_LT;
295 case CC_GT:
296 return CC_LE;
297 case CC_LE:
298 return CC_GT;
299 default:
300 CHECK_FATAL(false, "Not support yet.");
301 }
302 return kCcLast;
303 }
304
IsPowerOf2Const(uint64 i)305 inline bool IsPowerOf2Const(uint64 i)
306 {
307 return (i & (i - 1)) == 0;
308 }
309
RoundUpConst(uint64 offset,uint64 align)310 inline uint64 RoundUpConst(uint64 offset, uint64 align)
311 {
312 return (-align) & (offset + align - 1);
313 }
314
IsPowerOf2(uint64 i)315 inline bool IsPowerOf2(uint64 i)
316 {
317 return IsPowerOf2Const(i);
318 }
319
320 /* align must be a power of 2 */
RoundUp(uint64 offset,uint64 align)321 inline uint64 RoundUp(uint64 offset, uint64 align)
322 {
323 if (align == 0) {
324 return offset;
325 }
326 DEBUG_ASSERT(IsPowerOf2(align), "align must be power of 2!");
327 return RoundUpConst(offset, align);
328 }
329
RoundDownConst(int64 offset,int64 align)330 inline int64 RoundDownConst(int64 offset, int64 align)
331 {
332 return (-align) & offset;
333 }
334
335 // align must be a power of 2
RoundDown(int64 offset,int64 align)336 inline int64 RoundDown(int64 offset, int64 align)
337 {
338 if (align == 0) {
339 return offset;
340 }
341 DEBUG_ASSERT(IsPowerOf2(align), "align must be power of 2!");
342 return RoundDownConst(offset, align);
343 }
344
IsAlignedTo(uint64 offset,uint64 align)345 inline bool IsAlignedTo(uint64 offset, uint64 align)
346 {
347 DEBUG_ASSERT(IsPowerOf2(align), "align must be power of 2!");
348 return (offset & (align - 1)) == 0;
349 }
350 } /* namespace maplebe */
351
352 #endif /* MAPLEBE_INCLUDE_BE_COMMON_UTILS_H */
353