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