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 X64_ASSEMBLER_UTIL_H
17 #define X64_ASSEMBLER_UTIL_H
18
19 namespace assembler {
20 using uint8 = uint8_t;
21 using uint16 = uint16_t;
22 using uint32 = uint32_t;
23 using uint64 = uint64_t;
24 using int8 = int8_t;
25 using int16 = int16_t;
26 using int32 = int32_t;
27 using int64 = int64_t;
28 using uintptr = uintptr_t;
29
30 /* InsnSize is in byte. */
31 enum InsnSize : uint8 {
32 kB = 1,
33 kW = 2,
34 kL = 4,
35 kQ = 8,
36 };
37
38 /* size in bytes */
39 static const uint8 k1Byte = 1;
40 static const uint8 k2Bytes = 2;
41 static const uint8 k4Bytes = 4;
42 static const uint8 k8Bytes = 8;
43
44 /* size in bits */
45 static const uint8 k8Bits = 8;
46 static const uint8 k16Bits = 16;
47 static const uint8 k32Bits = 32;
48 static const uint8 k64Bits = 64;
49 static const uint8 k128Bits = 128;
50
Is8Bits(uint32 val)51 inline bool Is8Bits(uint32 val)
52 {
53 return val >= 0xFFFFFF80 || val <= 0x7F;
54 }
55
Is16Bits(uint32 val)56 inline bool Is16Bits(uint32 val)
57 {
58 return val >= 0xFFFF8000 || val <= 0x7FFF;
59 }
60
Is32Bits(uint64 val)61 inline bool Is32Bits(uint64 val)
62 {
63 return val >= ~uint64(0x7FFFFFFFU) || val <= 0x7FFFFFFFU;
64 }
65
Is64Bits(uint64 val)66 inline bool Is64Bits(uint64 val)
67 {
68 return val >= ~uint64(0x7FFFFFFFFFFFFFFFU) || val <= 0x7FFFFFFFFFFFFFFFU;
69 }
70
CalculateLabelSymIdx(uint32 funcUniqueId,uint32 labelIdx)71 inline int64 CalculateLabelSymIdx(uint32 funcUniqueId, uint32 labelIdx)
72 {
73 /* 32: make sure stIdx is large enough to be unique */
74 const int kLeftShiftBits = 32;
75 /* -1: BBLabel's stIdx is negative */
76 return static_cast<int64>((static_cast<uint64>(funcUniqueId) << kLeftShiftBits) + labelIdx) * (-1);
77 }
78
79 inline int64 CalculateStrLabelSymIdx(uint64 size, int64 labelIdx, size_t strTableSize = 0)
80 {
81 const int kLeftShiftBits = 8;
82 return (static_cast<int64>(size + strTableSize) * kLeftShiftBits + labelIdx);
83 }
84 } /* namespace assembler */
85
86 #endif /* X64_ASSEMBLER_UTIL_H */