Debian Codegraph @

js/src/jit/x86-shared/Constants-x86-shared.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef jit_x86_shared_Constants_x86_shared_h
8 #define jit_x86_shared_Constants_x86_shared_h
9
10 #include "mozilla/Assertions.h"
11
12 #include <iterator>
13 #include <stddef.h>
14 #include <stdint.h>
15
16 namespace js {
17 namespace jit {
18
19 namespace X86Encoding {
20
21 enum RegisterID : uint8_t {
22 rax,
23 rcx,
24 rdx,
25 rbx,
26 rsp,
27 rbp,
28 rsi,
29 rdi
30 #ifdef JS_CODEGEN_X64
31 ,
32 r8,
33 r9,
34 r10,
35 r11,
36 r12,
37 r13,
38 r14,
39 r15
40 #endif
41 ,
42 invalid_reg
43 };
44
45 enum HRegisterID { ah = rsp, ch = rbp, dh = rsi, bh = rdi };
46
47 enum XMMRegisterID
48 // GCC < 8.0 has a bug with bitfields of enums with an underlying type.
49 #if defined(__clang__) || __GNUC__ > 7
50 : uint8_t
51 #endif
52 {
53 xmm0 = 0,
54 xmm1,
55 xmm2,
56 xmm3,
57 xmm4,
58 xmm5,
59 xmm6,
60 xmm7
61 #ifdef JS_CODEGEN_X64
62 ,
63 xmm8,
64 xmm9,
65 xmm10,
66 xmm11,
67 xmm12,
68 xmm13,
69 xmm14,
70 xmm15
71 #endif
72 ,
73 invalid_xmm
74 };
75
76 inline const char* XMMRegName(XMMRegisterID reg) {
77 static const char* const names[] = {"%xmm0",
78 "%xmm1",
79 "%xmm2",
80 "%xmm3",
81 "%xmm4",
82 "%xmm5",
83 "%xmm6",
84 "%xmm7"
85 #ifdef JS_CODEGEN_X64
86 ,
87 "%xmm8",
88 "%xmm9",
89 "%xmm10",
90 "%xmm11",
91 "%xmm12",
92 "%xmm13",
93 "%xmm14",
94 "%xmm15"
95 #endif
96 };
97 MOZ_ASSERT(size_t(reg) < std::size(names));
98 return names[reg];
99 }
100
101 #ifdef JS_CODEGEN_X64
102 inline const char* GPReg64Name(RegisterID reg) {
103 static const char* const names[] = {"%rax",
104 "%rcx",
105 "%rdx",
106 "%rbx",
107 "%rsp",
108 "%rbp",
109 "%rsi",
110 "%rdi"
111 # ifdef JS_CODEGEN_X64
112 ,
113 "%r8",
114 "%r9",
115 "%r10",
116 "%r11",
117 "%r12",
118 "%r13",
119 "%r14",
120 "%r15"
121 # endif
122 };
123 MOZ_ASSERT(size_t(reg) < std::size(names));
124 return names[reg];
125 }
126 #endif
127
128 inline const char* GPReg32Name(RegisterID reg) {
129 static const char* const names[] = {"%eax",
130 "%ecx",
131 "%edx",
132 "%ebx",
133 "%esp",
134 "%ebp",
135 "%esi",
136 "%edi"
137 #ifdef JS_CODEGEN_X64
138 ,
139 "%r8d",
140 "%r9d",
141 "%r10d",
142 "%r11d",
143 "%r12d",
144 "%r13d",
145 "%r14d",
146 "%r15d"
147 #endif
148 };
149 MOZ_ASSERT(size_t(reg) < std::size(names));
150 return names[reg];
151 }
152
153 inline const char* GPReg16Name(RegisterID reg) {
154 static const char* const names[] = {"%ax",
155 "%cx",
156 "%dx",
157 "%bx",
158 "%sp",
159 "%bp",
160 "%si",
161 "%di"
162 #ifdef JS_CODEGEN_X64
163 ,
164 "%r8w",
165 "%r9w",
166 "%r10w",
167 "%r11w",
168 "%r12w",
169 "%r13w",
170 "%r14w",
171 "%r15w"
172 #endif
173 };
174 MOZ_ASSERT(size_t(reg) < std::size(names));
175 return names[reg];
176 }
177
178 inline const char* GPReg8Name(RegisterID reg) {
179 static const char* const names[] = {"%al",
180 "%cl",
181 "%dl",
182 "%bl"
183 #ifdef JS_CODEGEN_X64
184 ,
185 "%spl",
186 "%bpl",
187 "%sil",
188 "%dil",
189 "%r8b",
190 "%r9b",
191 "%r10b",
192 "%r11b",
193 "%r12b",
194 "%r13b",
195 "%r14b",
196 "%r15b"
197 #endif
198 };
199 MOZ_ASSERT(size_t(reg) < std::size(names));
200 return names[reg];
201 }
202
203 inline const char* GPRegName(RegisterID reg) {
204 #ifdef JS_CODEGEN_X64
205 return GPReg64Name(reg);
206 #else
207 return GPReg32Name(reg);
208 #endif
209 }
210
211 inline bool HasSubregL(RegisterID reg) {
212 #ifdef JS_CODEGEN_X64
213 // In 64-bit mode, all registers have an 8-bit lo subreg.
214 return true;
215 #else
216 // In 32-bit mode, only the first four registers do.
217 return reg <= rbx;
218 #endif
219 }
220
221 inline bool HasSubregH(RegisterID reg) {
222 // The first four registers always have h registers. However, note that
223 // on x64, h registers may not be used in instructions using REX
224 // prefixes. Also note that this may depend on what other registers are
225 // used!
226 return reg <= rbx;
227 }
228
229 inline HRegisterID GetSubregH(RegisterID reg) {
230 MOZ_ASSERT(HasSubregH(reg));
231 return HRegisterID(reg + 4);
232 }
233
234 inline const char* HRegName8(HRegisterID reg) {
235 static const char* const names[] = {"%ah", "%ch", "%dh", "%bh"};
236 size_t index = reg - GetSubregH(rax);
237 MOZ_ASSERT(index < std::size(names));
238 return names[index];
239 }
240
241 enum Condition {
242 ConditionO,
243 ConditionNO,
244 ConditionB,
245 ConditionAE,
246 ConditionE,
247 ConditionNE,
248 ConditionBE,
249 ConditionA,
250 ConditionS,
251 ConditionNS,
252 ConditionP,
253 ConditionNP,
254 ConditionL,
255 ConditionGE,
256 ConditionLE,
257 ConditionG,
258
259 ConditionC = ConditionB,
260 ConditionNC = ConditionAE
261 };
262
263 inline const char* CCName(Condition cc) {
264 static const char* const names[] = {"o ", "no", "b ", "ae", "e ", "ne",
265 "be", "a ", "s ", "ns", "p ", "np",
266 "l ", "ge", "le", "g "};
267 MOZ_ASSERT(size_t(cc) < std::size(names));
268 return names[cc];
269 }
270
271 // Conditions for CMP instructions (CMPSS, CMPSD, CMPPS, CMPPD, etc).
272 enum ConditionCmp {
273 ConditionCmp_EQ = 0x0,
274 ConditionCmp_LT = 0x1,
275 ConditionCmp_LE = 0x2,
276 ConditionCmp_UNORD = 0x3,
277 ConditionCmp_NEQ = 0x4,
278 ConditionCmp_NLT = 0x5,
279 ConditionCmp_NLE = 0x6,
280 ConditionCmp_ORD = 0x7,
281 ConditionCmp_AVX_Enabled = 0x8,
282 ConditionCmp_GE = 0xD,
283 };
284
285 // Rounding modes for ROUNDSS / ROUNDSD / VCVTPS2PH.
286 enum RoundingMode {
287 RoundToNearest = 0x0,
288 RoundDown = 0x1,
289 RoundUp = 0x2,
290 RoundToZero = 0x3
291 };
292
293 // Rounding modes for ROUNDPS / ROUNDPD. Note these are the same as for
294 // RoundingMode above but incorporate the 'inexact' bit which says not to signal
295 // exceptions for lost precision. It's not obvious that this bit is needed; it
296 // was however suggested in the wasm SIMD proposal that led to these encodings.
297 enum class SSERoundingMode {
298 RoundToNearest = 0x08,
299 RoundDown = 0x09,
300 RoundUp = 0x0A,
301 RoundToZero = 0x0B
302 };
303
304 // Test whether the given address will fit in an address immediate field.
305 // This is always true on x86, but on x64 it's only true for addreses which
306 // fit in the 32-bit immediate field.
307 inline bool IsAddressImmediate(const void* address) {
308 intptr_t value = reinterpret_cast<intptr_t>(address);
309 int32_t immediate = static_cast<int32_t>(value);
310 return value == immediate;
311 }
312
313 // Convert the given address to a 32-bit immediate field value. This is a
314 // no-op on x86, but on x64 it asserts that the address is actually a valid
315 // address immediate.
316 inline int32_t AddressImmediate(const void* address) {
317 MOZ_ASSERT(IsAddressImmediate(address));
318 return static_cast<int32_t>(reinterpret_cast<intptr_t>(address));
319 }
320
321 } // namespace X86Encoding
322
323 } // namespace jit
324 } // namespace js
325
326 #endif /* jit_x86_shared_Constants_x86_shared_h */