1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_X64_ASSEMBLER_X64_INL_H_
6 #define V8_X64_ASSEMBLER_X64_INL_H_
7
8 #include "src/x64/assembler-x64.h"
9
10 #include "src/base/cpu.h"
11 #include "src/debug/debug.h"
12 #include "src/objects-inl.h"
13 #include "src/v8memory.h"
14
15 namespace v8 {
16 namespace internal {
17
SupportsOptimizer()18 bool CpuFeatures::SupportsOptimizer() { return true; }
19
SupportsWasmSimd128()20 bool CpuFeatures::SupportsWasmSimd128() { return IsSupported(SSE4_1); }
21
22 // -----------------------------------------------------------------------------
23 // Implementation of Assembler
24
25
emitl(uint32_t x)26 void Assembler::emitl(uint32_t x) {
27 Memory<uint32_t>(pc_) = x;
28 pc_ += sizeof(uint32_t);
29 }
30
emitp(Address x,RelocInfo::Mode rmode)31 void Assembler::emitp(Address x, RelocInfo::Mode rmode) {
32 Memory<uintptr_t>(pc_) = x;
33 if (!RelocInfo::IsNone(rmode)) {
34 RecordRelocInfo(rmode, x);
35 }
36 pc_ += sizeof(uintptr_t);
37 }
38
39
emitq(uint64_t x)40 void Assembler::emitq(uint64_t x) {
41 Memory<uint64_t>(pc_) = x;
42 pc_ += sizeof(uint64_t);
43 }
44
45
emitw(uint16_t x)46 void Assembler::emitw(uint16_t x) {
47 Memory<uint16_t>(pc_) = x;
48 pc_ += sizeof(uint16_t);
49 }
50
emit_runtime_entry(Address entry,RelocInfo::Mode rmode)51 void Assembler::emit_runtime_entry(Address entry, RelocInfo::Mode rmode) {
52 DCHECK(RelocInfo::IsRuntimeEntry(rmode));
53 RecordRelocInfo(rmode);
54 emitl(static_cast<uint32_t>(entry - options().code_range_start));
55 }
56
emit(Immediate x)57 void Assembler::emit(Immediate x) {
58 if (!RelocInfo::IsNone(x.rmode_)) {
59 RecordRelocInfo(x.rmode_);
60 }
61 emitl(x.value_);
62 }
63
emit_rex_64(Register reg,Register rm_reg)64 void Assembler::emit_rex_64(Register reg, Register rm_reg) {
65 emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit());
66 }
67
68
emit_rex_64(XMMRegister reg,Register rm_reg)69 void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) {
70 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
71 }
72
73
emit_rex_64(Register reg,XMMRegister rm_reg)74 void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) {
75 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
76 }
77
emit_rex_64(XMMRegister reg,XMMRegister rm_reg)78 void Assembler::emit_rex_64(XMMRegister reg, XMMRegister rm_reg) {
79 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
80 }
81
emit_rex_64(Register reg,Operand op)82 void Assembler::emit_rex_64(Register reg, Operand op) {
83 emit(0x48 | reg.high_bit() << 2 | op.data().rex);
84 }
85
emit_rex_64(XMMRegister reg,Operand op)86 void Assembler::emit_rex_64(XMMRegister reg, Operand op) {
87 emit(0x48 | (reg.code() & 0x8) >> 1 | op.data().rex);
88 }
89
90
emit_rex_64(Register rm_reg)91 void Assembler::emit_rex_64(Register rm_reg) {
92 DCHECK_EQ(rm_reg.code() & 0xf, rm_reg.code());
93 emit(0x48 | rm_reg.high_bit());
94 }
95
emit_rex_64(Operand op)96 void Assembler::emit_rex_64(Operand op) { emit(0x48 | op.data().rex); }
97
emit_rex_32(Register reg,Register rm_reg)98 void Assembler::emit_rex_32(Register reg, Register rm_reg) {
99 emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit());
100 }
101
emit_rex_32(Register reg,Operand op)102 void Assembler::emit_rex_32(Register reg, Operand op) {
103 emit(0x40 | reg.high_bit() << 2 | op.data().rex);
104 }
105
106
emit_rex_32(Register rm_reg)107 void Assembler::emit_rex_32(Register rm_reg) {
108 emit(0x40 | rm_reg.high_bit());
109 }
110
emit_rex_32(Operand op)111 void Assembler::emit_rex_32(Operand op) { emit(0x40 | op.data().rex); }
112
emit_optional_rex_32(Register reg,Register rm_reg)113 void Assembler::emit_optional_rex_32(Register reg, Register rm_reg) {
114 byte rex_bits = reg.high_bit() << 2 | rm_reg.high_bit();
115 if (rex_bits != 0) emit(0x40 | rex_bits);
116 }
117
emit_optional_rex_32(Register reg,Operand op)118 void Assembler::emit_optional_rex_32(Register reg, Operand op) {
119 byte rex_bits = reg.high_bit() << 2 | op.data().rex;
120 if (rex_bits != 0) emit(0x40 | rex_bits);
121 }
122
emit_optional_rex_32(XMMRegister reg,Operand op)123 void Assembler::emit_optional_rex_32(XMMRegister reg, Operand op) {
124 byte rex_bits = (reg.code() & 0x8) >> 1 | op.data().rex;
125 if (rex_bits != 0) emit(0x40 | rex_bits);
126 }
127
128
emit_optional_rex_32(XMMRegister reg,XMMRegister base)129 void Assembler::emit_optional_rex_32(XMMRegister reg, XMMRegister base) {
130 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
131 if (rex_bits != 0) emit(0x40 | rex_bits);
132 }
133
134
emit_optional_rex_32(XMMRegister reg,Register base)135 void Assembler::emit_optional_rex_32(XMMRegister reg, Register base) {
136 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
137 if (rex_bits != 0) emit(0x40 | rex_bits);
138 }
139
140
emit_optional_rex_32(Register reg,XMMRegister base)141 void Assembler::emit_optional_rex_32(Register reg, XMMRegister base) {
142 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
143 if (rex_bits != 0) emit(0x40 | rex_bits);
144 }
145
146
emit_optional_rex_32(Register rm_reg)147 void Assembler::emit_optional_rex_32(Register rm_reg) {
148 if (rm_reg.high_bit()) emit(0x41);
149 }
150
emit_optional_rex_32(XMMRegister rm_reg)151 void Assembler::emit_optional_rex_32(XMMRegister rm_reg) {
152 if (rm_reg.high_bit()) emit(0x41);
153 }
154
emit_optional_rex_32(Operand op)155 void Assembler::emit_optional_rex_32(Operand op) {
156 if (op.data().rex != 0) emit(0x40 | op.data().rex);
157 }
158
159
160 // byte 1 of 3-byte VEX
emit_vex3_byte1(XMMRegister reg,XMMRegister rm,LeadingOpcode m)161 void Assembler::emit_vex3_byte1(XMMRegister reg, XMMRegister rm,
162 LeadingOpcode m) {
163 byte rxb = ~((reg.high_bit() << 2) | rm.high_bit()) << 5;
164 emit(rxb | m);
165 }
166
167
168 // byte 1 of 3-byte VEX
emit_vex3_byte1(XMMRegister reg,Operand rm,LeadingOpcode m)169 void Assembler::emit_vex3_byte1(XMMRegister reg, Operand rm, LeadingOpcode m) {
170 byte rxb = ~((reg.high_bit() << 2) | rm.data().rex) << 5;
171 emit(rxb | m);
172 }
173
174
175 // byte 1 of 2-byte VEX
emit_vex2_byte1(XMMRegister reg,XMMRegister v,VectorLength l,SIMDPrefix pp)176 void Assembler::emit_vex2_byte1(XMMRegister reg, XMMRegister v, VectorLength l,
177 SIMDPrefix pp) {
178 byte rv = ~((reg.high_bit() << 4) | v.code()) << 3;
179 emit(rv | l | pp);
180 }
181
182
183 // byte 2 of 3-byte VEX
emit_vex3_byte2(VexW w,XMMRegister v,VectorLength l,SIMDPrefix pp)184 void Assembler::emit_vex3_byte2(VexW w, XMMRegister v, VectorLength l,
185 SIMDPrefix pp) {
186 emit(w | ((~v.code() & 0xf) << 3) | l | pp);
187 }
188
189
emit_vex_prefix(XMMRegister reg,XMMRegister vreg,XMMRegister rm,VectorLength l,SIMDPrefix pp,LeadingOpcode mm,VexW w)190 void Assembler::emit_vex_prefix(XMMRegister reg, XMMRegister vreg,
191 XMMRegister rm, VectorLength l, SIMDPrefix pp,
192 LeadingOpcode mm, VexW w) {
193 if (rm.high_bit() || mm != k0F || w != kW0) {
194 emit_vex3_byte0();
195 emit_vex3_byte1(reg, rm, mm);
196 emit_vex3_byte2(w, vreg, l, pp);
197 } else {
198 emit_vex2_byte0();
199 emit_vex2_byte1(reg, vreg, l, pp);
200 }
201 }
202
203
emit_vex_prefix(Register reg,Register vreg,Register rm,VectorLength l,SIMDPrefix pp,LeadingOpcode mm,VexW w)204 void Assembler::emit_vex_prefix(Register reg, Register vreg, Register rm,
205 VectorLength l, SIMDPrefix pp, LeadingOpcode mm,
206 VexW w) {
207 XMMRegister ireg = XMMRegister::from_code(reg.code());
208 XMMRegister ivreg = XMMRegister::from_code(vreg.code());
209 XMMRegister irm = XMMRegister::from_code(rm.code());
210 emit_vex_prefix(ireg, ivreg, irm, l, pp, mm, w);
211 }
212
emit_vex_prefix(XMMRegister reg,XMMRegister vreg,Operand rm,VectorLength l,SIMDPrefix pp,LeadingOpcode mm,VexW w)213 void Assembler::emit_vex_prefix(XMMRegister reg, XMMRegister vreg, Operand rm,
214 VectorLength l, SIMDPrefix pp, LeadingOpcode mm,
215 VexW w) {
216 if (rm.data().rex || mm != k0F || w != kW0) {
217 emit_vex3_byte0();
218 emit_vex3_byte1(reg, rm, mm);
219 emit_vex3_byte2(w, vreg, l, pp);
220 } else {
221 emit_vex2_byte0();
222 emit_vex2_byte1(reg, vreg, l, pp);
223 }
224 }
225
emit_vex_prefix(Register reg,Register vreg,Operand rm,VectorLength l,SIMDPrefix pp,LeadingOpcode mm,VexW w)226 void Assembler::emit_vex_prefix(Register reg, Register vreg, Operand rm,
227 VectorLength l, SIMDPrefix pp, LeadingOpcode mm,
228 VexW w) {
229 XMMRegister ireg = XMMRegister::from_code(reg.code());
230 XMMRegister ivreg = XMMRegister::from_code(vreg.code());
231 emit_vex_prefix(ireg, ivreg, rm, l, pp, mm, w);
232 }
233
234
target_address_at(Address pc,Address constant_pool)235 Address Assembler::target_address_at(Address pc, Address constant_pool) {
236 return Memory<int32_t>(pc) + pc + 4;
237 }
238
set_target_address_at(Address pc,Address constant_pool,Address target,ICacheFlushMode icache_flush_mode)239 void Assembler::set_target_address_at(Address pc, Address constant_pool,
240 Address target,
241 ICacheFlushMode icache_flush_mode) {
242 Memory<int32_t>(pc) = static_cast<int32_t>(target - pc - 4);
243 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
244 Assembler::FlushICache(pc, sizeof(int32_t));
245 }
246 }
247
deserialization_set_target_internal_reference_at(Address pc,Address target,RelocInfo::Mode mode)248 void Assembler::deserialization_set_target_internal_reference_at(
249 Address pc, Address target, RelocInfo::Mode mode) {
250 Memory<Address>(pc) = target;
251 }
252
253
target_address_from_return_address(Address pc)254 Address Assembler::target_address_from_return_address(Address pc) {
255 return pc - kCallTargetAddressOffset;
256 }
257
deserialization_set_special_target_at(Address instruction_payload,Code * code,Address target)258 void Assembler::deserialization_set_special_target_at(
259 Address instruction_payload, Code* code, Address target) {
260 set_target_address_at(instruction_payload,
261 code ? code->constant_pool() : kNullAddress, target);
262 }
263
deserialization_special_target_size(Address instruction_payload)264 int Assembler::deserialization_special_target_size(
265 Address instruction_payload) {
266 return kSpecialTargetSize;
267 }
268
code_target_object_handle_at(Address pc)269 Handle<Code> Assembler::code_target_object_handle_at(Address pc) {
270 return GetCodeTarget(Memory<int32_t>(pc));
271 }
272
runtime_entry_at(Address pc)273 Address Assembler::runtime_entry_at(Address pc) {
274 return Memory<int32_t>(pc) + options().code_range_start;
275 }
276
277 // -----------------------------------------------------------------------------
278 // Implementation of RelocInfo
279
280 // The modes possibly affected by apply must be in kApplyMask.
apply(intptr_t delta)281 void RelocInfo::apply(intptr_t delta) {
282 if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
283 Memory<int32_t>(pc_) -= static_cast<int32_t>(delta);
284 } else if (IsInternalReference(rmode_)) {
285 // absolute code pointer inside code object moves with the code object.
286 Memory<Address>(pc_) += delta;
287 }
288 }
289
290
target_address()291 Address RelocInfo::target_address() {
292 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) || IsWasmCall(rmode_));
293 return Assembler::target_address_at(pc_, constant_pool_);
294 }
295
target_address_address()296 Address RelocInfo::target_address_address() {
297 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) || IsWasmCall(rmode_) ||
298 IsWasmStubCall(rmode_) || IsEmbeddedObject(rmode_) ||
299 IsExternalReference(rmode_) || IsOffHeapTarget(rmode_));
300 return pc_;
301 }
302
303
constant_pool_entry_address()304 Address RelocInfo::constant_pool_entry_address() {
305 UNREACHABLE();
306 }
307
308
target_address_size()309 int RelocInfo::target_address_size() {
310 if (IsCodedSpecially()) {
311 return Assembler::kSpecialTargetSize;
312 } else {
313 return kPointerSize;
314 }
315 }
316
target_object()317 HeapObject* RelocInfo::target_object() {
318 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
319 return HeapObject::cast(Memory<Object*>(pc_));
320 }
321
target_object_handle(Assembler * origin)322 Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
323 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
324 if (rmode_ == EMBEDDED_OBJECT) {
325 return Handle<HeapObject>::cast(Memory<Handle<Object>>(pc_));
326 } else {
327 return origin->code_target_object_handle_at(pc_);
328 }
329 }
330
target_external_reference()331 Address RelocInfo::target_external_reference() {
332 DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
333 return Memory<Address>(pc_);
334 }
335
set_target_external_reference(Address target,ICacheFlushMode icache_flush_mode)336 void RelocInfo::set_target_external_reference(
337 Address target, ICacheFlushMode icache_flush_mode) {
338 DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
339 Memory<Address>(pc_) = target;
340 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
341 Assembler::FlushICache(pc_, sizeof(Address));
342 }
343 }
344
target_internal_reference()345 Address RelocInfo::target_internal_reference() {
346 DCHECK(rmode_ == INTERNAL_REFERENCE);
347 return Memory<Address>(pc_);
348 }
349
350
target_internal_reference_address()351 Address RelocInfo::target_internal_reference_address() {
352 DCHECK(rmode_ == INTERNAL_REFERENCE);
353 return pc_;
354 }
355
set_target_object(Heap * heap,HeapObject * target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)356 void RelocInfo::set_target_object(Heap* heap, HeapObject* target,
357 WriteBarrierMode write_barrier_mode,
358 ICacheFlushMode icache_flush_mode) {
359 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
360 Memory<Object*>(pc_) = target;
361 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
362 Assembler::FlushICache(pc_, sizeof(Address));
363 }
364 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != nullptr) {
365 WriteBarrierForCode(host(), this, target);
366 }
367 }
368
369
target_runtime_entry(Assembler * origin)370 Address RelocInfo::target_runtime_entry(Assembler* origin) {
371 DCHECK(IsRuntimeEntry(rmode_));
372 return origin->runtime_entry_at(pc_);
373 }
374
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)375 void RelocInfo::set_target_runtime_entry(Address target,
376 WriteBarrierMode write_barrier_mode,
377 ICacheFlushMode icache_flush_mode) {
378 DCHECK(IsRuntimeEntry(rmode_));
379 if (target_address() != target) {
380 set_target_address(target, write_barrier_mode, icache_flush_mode);
381 }
382 }
383
target_off_heap_target()384 Address RelocInfo::target_off_heap_target() {
385 DCHECK(IsOffHeapTarget(rmode_));
386 return Memory<Address>(pc_);
387 }
388
WipeOut()389 void RelocInfo::WipeOut() {
390 if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_) ||
391 IsInternalReference(rmode_) || IsOffHeapTarget(rmode_)) {
392 Memory<Address>(pc_) = kNullAddress;
393 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
394 // Effectively write zero into the relocation.
395 Assembler::set_target_address_at(pc_, constant_pool_,
396 pc_ + sizeof(int32_t));
397 } else {
398 UNREACHABLE();
399 }
400 }
401
402 template <typename ObjectVisitor>
Visit(ObjectVisitor * visitor)403 void RelocInfo::Visit(ObjectVisitor* visitor) {
404 RelocInfo::Mode mode = rmode();
405 if (mode == RelocInfo::EMBEDDED_OBJECT) {
406 visitor->VisitEmbeddedPointer(host(), this);
407 Assembler::FlushICache(pc_, sizeof(Address));
408 } else if (RelocInfo::IsCodeTargetMode(mode)) {
409 visitor->VisitCodeTarget(host(), this);
410 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
411 visitor->VisitExternalReference(host(), this);
412 } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
413 visitor->VisitInternalReference(host(), this);
414 } else if (RelocInfo::IsRuntimeEntry(mode)) {
415 visitor->VisitRuntimeEntry(host(), this);
416 } else if (RelocInfo::IsOffHeapTarget(mode)) {
417 visitor->VisitOffHeapTarget(host(), this);
418 }
419 }
420
421 } // namespace internal
422 } // namespace v8
423
424 #endif // V8_X64_ASSEMBLER_X64_INL_H_
425