blob: caf7af6ec94368b5140b485fc7caa06469ad2c2c [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_X64_ASSEMBLER_X64_INL_H_
6#define V8_X64_ASSEMBLER_X64_INL_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/x64/assembler-x64.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/base/cpu.h"
11#include "src/debug.h"
12#include "src/v8memory.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000013
14namespace v8 {
15namespace internal {
16
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017bool CpuFeatures::SupportsCrankshaft() { return true; }
18
Steve Blocka7e24c12009-10-30 11:49:00 +000019
20// -----------------------------------------------------------------------------
21// Implementation of Assembler
22
23
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024static const byte kCallOpcode = 0xE8;
25// The length of pushq(rbp), movp(rbp, rsp), Push(rsi) and Push(rdi).
26static const int kNoCodeAgeSequenceLength = kPointerSize == kInt64Size ? 6 : 17;
27
28
Steve Blocka7e24c12009-10-30 11:49:00 +000029void Assembler::emitl(uint32_t x) {
30 Memory::uint32_at(pc_) = x;
31 pc_ += sizeof(uint32_t);
32}
33
34
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035void Assembler::emitp(void* x, RelocInfo::Mode rmode) {
36 uintptr_t value = reinterpret_cast<uintptr_t>(x);
37 Memory::uintptr_at(pc_) = value;
38 if (!RelocInfo::IsNone(rmode)) {
39 RecordRelocInfo(rmode, value);
Steve Blocka7e24c12009-10-30 11:49:00 +000040 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 pc_ += sizeof(uintptr_t);
42}
43
44
45void Assembler::emitq(uint64_t x) {
46 Memory::uint64_at(pc_) = x;
Steve Blocka7e24c12009-10-30 11:49:00 +000047 pc_ += sizeof(uint64_t);
48}
49
50
51void Assembler::emitw(uint16_t x) {
52 Memory::uint16_at(pc_) = x;
53 pc_ += sizeof(uint16_t);
54}
55
56
Ben Murdoch257744e2011-11-30 15:57:28 +000057void Assembler::emit_code_target(Handle<Code> target,
58 RelocInfo::Mode rmode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 TypeFeedbackId ast_id) {
60 DCHECK(RelocInfo::IsCodeTarget(rmode) ||
61 rmode == RelocInfo::CODE_AGE_SEQUENCE);
62 if (rmode == RelocInfo::CODE_TARGET && !ast_id.IsNone()) {
63 RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, ast_id.ToInt());
Ben Murdoch257744e2011-11-30 15:57:28 +000064 } else {
65 RecordRelocInfo(rmode);
66 }
Steve Block3ce2e202009-11-05 08:53:23 +000067 int current = code_targets_.length();
68 if (current > 0 && code_targets_.last().is_identical_to(target)) {
69 // Optimization if we keep jumping to the same code target.
70 emitl(current - 1);
71 } else {
72 code_targets_.Add(target);
73 emitl(current);
74 }
75}
76
77
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078void Assembler::emit_runtime_entry(Address entry, RelocInfo::Mode rmode) {
79 DCHECK(RelocInfo::IsRuntimeEntry(rmode));
80 RecordRelocInfo(rmode);
81 emitl(static_cast<uint32_t>(entry - isolate()->code_range()->start()));
82}
83
84
Steve Blocka7e24c12009-10-30 11:49:00 +000085void Assembler::emit_rex_64(Register reg, Register rm_reg) {
86 emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit());
87}
88
89
90void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) {
91 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
92}
93
94
Steve Block6ded16b2010-05-10 14:33:55 +010095void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) {
96 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
97}
98
99
Steve Blocka7e24c12009-10-30 11:49:00 +0000100void Assembler::emit_rex_64(Register reg, const Operand& op) {
101 emit(0x48 | reg.high_bit() << 2 | op.rex_);
102}
103
104
105void Assembler::emit_rex_64(XMMRegister reg, const Operand& op) {
106 emit(0x48 | (reg.code() & 0x8) >> 1 | op.rex_);
107}
108
109
110void Assembler::emit_rex_64(Register rm_reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111 DCHECK_EQ(rm_reg.code() & 0xf, rm_reg.code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 emit(0x48 | rm_reg.high_bit());
113}
114
115
116void Assembler::emit_rex_64(const Operand& op) {
117 emit(0x48 | op.rex_);
118}
119
120
121void Assembler::emit_rex_32(Register reg, Register rm_reg) {
122 emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit());
123}
124
125
126void Assembler::emit_rex_32(Register reg, const Operand& op) {
127 emit(0x40 | reg.high_bit() << 2 | op.rex_);
128}
129
130
131void Assembler::emit_rex_32(Register rm_reg) {
132 emit(0x40 | rm_reg.high_bit());
133}
134
135
136void Assembler::emit_rex_32(const Operand& op) {
137 emit(0x40 | op.rex_);
138}
139
140
141void Assembler::emit_optional_rex_32(Register reg, Register rm_reg) {
142 byte rex_bits = reg.high_bit() << 2 | rm_reg.high_bit();
143 if (rex_bits != 0) emit(0x40 | rex_bits);
144}
145
146
147void Assembler::emit_optional_rex_32(Register reg, const Operand& op) {
148 byte rex_bits = reg.high_bit() << 2 | op.rex_;
149 if (rex_bits != 0) emit(0x40 | rex_bits);
150}
151
152
153void Assembler::emit_optional_rex_32(XMMRegister reg, const Operand& op) {
154 byte rex_bits = (reg.code() & 0x8) >> 1 | op.rex_;
155 if (rex_bits != 0) emit(0x40 | rex_bits);
156}
157
158
159void Assembler::emit_optional_rex_32(XMMRegister reg, XMMRegister base) {
160 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
161 if (rex_bits != 0) emit(0x40 | rex_bits);
162}
163
164
165void Assembler::emit_optional_rex_32(XMMRegister reg, Register base) {
166 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
167 if (rex_bits != 0) emit(0x40 | rex_bits);
168}
169
170
Steve Block6ded16b2010-05-10 14:33:55 +0100171void Assembler::emit_optional_rex_32(Register reg, XMMRegister base) {
172 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
173 if (rex_bits != 0) emit(0x40 | rex_bits);
174}
175
176
Steve Blocka7e24c12009-10-30 11:49:00 +0000177void Assembler::emit_optional_rex_32(Register rm_reg) {
178 if (rm_reg.high_bit()) emit(0x41);
179}
180
181
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400182void Assembler::emit_optional_rex_32(XMMRegister rm_reg) {
183 if (rm_reg.high_bit()) emit(0x41);
184}
185
186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187void Assembler::emit_optional_rex_32(const Operand& op) {
188 if (op.rex_ != 0) emit(0x40 | op.rex_);
189}
190
191
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400192// byte 1 of 3-byte VEX
193void Assembler::emit_vex3_byte1(XMMRegister reg, XMMRegister rm,
194 LeadingOpcode m) {
195 byte rxb = ~((reg.high_bit() << 2) | rm.high_bit()) << 5;
196 emit(rxb | m);
197}
198
199
200// byte 1 of 3-byte VEX
201void Assembler::emit_vex3_byte1(XMMRegister reg, const Operand& rm,
202 LeadingOpcode m) {
203 byte rxb = ~((reg.high_bit() << 2) | rm.rex_) << 5;
204 emit(rxb | m);
205}
206
207
208// byte 1 of 2-byte VEX
209void Assembler::emit_vex2_byte1(XMMRegister reg, XMMRegister v, VectorLength l,
210 SIMDPrefix pp) {
211 byte rv = ~((reg.high_bit() << 4) | v.code()) << 3;
212 emit(rv | l | pp);
213}
214
215
216// byte 2 of 3-byte VEX
217void Assembler::emit_vex3_byte2(VexW w, XMMRegister v, VectorLength l,
218 SIMDPrefix pp) {
219 emit(w | ((~v.code() & 0xf) << 3) | l | pp);
220}
221
222
223void Assembler::emit_vex_prefix(XMMRegister reg, XMMRegister vreg,
224 XMMRegister rm, VectorLength l, SIMDPrefix pp,
225 LeadingOpcode mm, VexW w) {
226 if (rm.high_bit() || mm != k0F || w != kW0) {
227 emit_vex3_byte0();
228 emit_vex3_byte1(reg, rm, mm);
229 emit_vex3_byte2(w, vreg, l, pp);
230 } else {
231 emit_vex2_byte0();
232 emit_vex2_byte1(reg, vreg, l, pp);
233 }
234}
235
236
237void Assembler::emit_vex_prefix(XMMRegister reg, XMMRegister vreg,
238 const Operand& rm, VectorLength l,
239 SIMDPrefix pp, LeadingOpcode mm, VexW w) {
240 if (rm.rex_ || mm != k0F || w != kW0) {
241 emit_vex3_byte0();
242 emit_vex3_byte1(reg, rm, mm);
243 emit_vex3_byte2(w, vreg, l, pp);
244 } else {
245 emit_vex2_byte0();
246 emit_vex2_byte1(reg, vreg, l, pp);
247 }
248}
249
250
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251Address Assembler::target_address_at(Address pc,
252 ConstantPoolArray* constant_pool) {
Steve Block3ce2e202009-11-05 08:53:23 +0000253 return Memory::int32_at(pc) + pc + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +0000254}
255
256
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257void Assembler::set_target_address_at(Address pc,
258 ConstantPoolArray* constant_pool,
259 Address target,
260 ICacheFlushMode icache_flush_mode) {
Steve Blockd0582a62009-12-15 09:54:21 +0000261 Memory::int32_at(pc) = static_cast<int32_t>(target - pc - 4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
263 CpuFeatures::FlushICache(pc, sizeof(int32_t));
264 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000265}
266
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267
268Address Assembler::target_address_from_return_address(Address pc) {
269 return pc - kCallTargetAddressOffset;
270}
271
272
273Address Assembler::break_address_from_return_address(Address pc) {
274 return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
275}
276
277
Steve Block3ce2e202009-11-05 08:53:23 +0000278Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
279 return code_targets_[Memory::int32_at(pc)];
280}
Steve Blocka7e24c12009-10-30 11:49:00 +0000281
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282
283Address Assembler::runtime_entry_at(Address pc) {
284 return Memory::int32_at(pc) + isolate()->code_range()->start();
285}
286
Steve Blocka7e24c12009-10-30 11:49:00 +0000287// -----------------------------------------------------------------------------
288// Implementation of RelocInfo
289
290// The modes possibly affected by apply must be in kApplyMask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
292 bool flush_icache = icache_flush_mode != SKIP_ICACHE_FLUSH;
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 if (IsInternalReference(rmode_)) {
294 // absolute code pointer inside code object moves with the code object.
Steve Blockd0582a62009-12-15 09:54:21 +0000295 Memory::Address_at(pc_) += static_cast<int32_t>(delta);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(Address));
297 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000298 Memory::int32_at(pc_) -= static_cast<int32_t>(delta);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(int32_t));
300 } else if (rmode_ == CODE_AGE_SEQUENCE) {
301 if (*pc_ == kCallOpcode) {
302 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
303 *p -= static_cast<int32_t>(delta); // Relocate entry.
304 if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
305 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 }
307}
308
309
310Address RelocInfo::target_address() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
312 return Assembler::target_address_at(pc_, host_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000313}
314
315
316Address RelocInfo::target_address_address() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100318 || rmode_ == EMBEDDED_OBJECT
319 || rmode_ == EXTERNAL_REFERENCE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 return reinterpret_cast<Address>(pc_);
321}
322
323
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324Address RelocInfo::constant_pool_entry_address() {
325 UNREACHABLE();
326 return NULL;
327}
328
329
Leon Clarkef7060e22010-06-03 12:02:55 +0100330int RelocInfo::target_address_size() {
331 if (IsCodedSpecially()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100332 return Assembler::kSpecialTargetSize;
Leon Clarkef7060e22010-06-03 12:02:55 +0100333 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100334 return kPointerSize;
Leon Clarkef7060e22010-06-03 12:02:55 +0100335 }
336}
337
338
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339void RelocInfo::set_target_address(Address target,
340 WriteBarrierMode write_barrier_mode,
341 ICacheFlushMode icache_flush_mode) {
342 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
343 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
344 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
345 IsCodeTarget(rmode_)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100346 Object* target_code = Code::GetCodeFromTargetAddress(target);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
348 host(), this, HeapObject::cast(target_code));
Steve Block3ce2e202009-11-05 08:53:23 +0000349 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000350}
351
352
353Object* RelocInfo::target_object() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Steve Block3ce2e202009-11-05 08:53:23 +0000355 return Memory::Object_at(pc_);
356}
357
358
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100359Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Steve Block3ce2e202009-11-05 08:53:23 +0000361 if (rmode_ == EMBEDDED_OBJECT) {
362 return Memory::Object_Handle_at(pc_);
363 } else {
364 return origin->code_target_object_handle_at(pc_);
365 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000366}
367
368
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369Address RelocInfo::target_reference() {
370 DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
371 return Memory::Address_at(pc_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000372}
373
374
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375void RelocInfo::set_target_object(Object* target,
376 WriteBarrierMode write_barrier_mode,
377 ICacheFlushMode icache_flush_mode) {
378 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100379 Memory::Object_at(pc_) = target;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
381 CpuFeatures::FlushICache(pc_, sizeof(Address));
382 }
383 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100384 host() != NULL &&
385 target->IsHeapObject()) {
386 host()->GetHeap()->incremental_marking()->RecordWrite(
387 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
388 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000389}
390
391
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392Address RelocInfo::target_runtime_entry(Assembler* origin) {
393 DCHECK(IsRuntimeEntry(rmode_));
394 return origin->runtime_entry_at(pc_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100395}
396
397
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398void RelocInfo::set_target_runtime_entry(Address target,
399 WriteBarrierMode write_barrier_mode,
400 ICacheFlushMode icache_flush_mode) {
401 DCHECK(IsRuntimeEntry(rmode_));
402 if (target_address() != target) {
403 set_target_address(target, write_barrier_mode, icache_flush_mode);
404 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100405}
406
407
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408Handle<Cell> RelocInfo::target_cell_handle() {
409 DCHECK(rmode_ == RelocInfo::CELL);
410 Address address = Memory::Address_at(pc_);
411 return Handle<Cell>(reinterpret_cast<Cell**>(address));
412}
413
414
415Cell* RelocInfo::target_cell() {
416 DCHECK(rmode_ == RelocInfo::CELL);
417 return Cell::FromValueAddress(Memory::Address_at(pc_));
418}
419
420
421void RelocInfo::set_target_cell(Cell* cell,
422 WriteBarrierMode write_barrier_mode,
423 ICacheFlushMode icache_flush_mode) {
424 DCHECK(rmode_ == RelocInfo::CELL);
425 Address address = cell->address() + Cell::kValueOffset;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100426 Memory::Address_at(pc_) = address;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
428 CpuFeatures::FlushICache(pc_, sizeof(Address));
429 }
430 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100431 host() != NULL) {
432 // TODO(1550) We are passing NULL as a slot because cell can never be on
433 // evacuation candidate.
434 host()->GetHeap()->incremental_marking()->RecordWrite(
435 host(), NULL, cell);
436 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100437}
438
439
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440void RelocInfo::WipeOut() {
441 if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_)) {
442 Memory::Address_at(pc_) = NULL;
443 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
444 // Effectively write zero into the relocation.
445 Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
446 } else {
447 UNREACHABLE();
448 }
449}
450
451
Steve Block3ce2e202009-11-05 08:53:23 +0000452bool RelocInfo::IsPatchedReturnSequence() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000453 // The recognized call sequence is:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 // movq(kScratchRegister, address); call(kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 // It only needs to be distinguished from a return sequence
456 // movq(rsp, rbp); pop(rbp); ret(n); int3 *6
457 // The 11th byte is int3 (0xCC) in the return sequence and
458 // REX.WB (0x48+register bit) for the call sequence.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 return pc_[Assembler::kMoveAddressIntoScratchRegisterInstructionLength] !=
460 0xCC;
Steve Blocka7e24c12009-10-30 11:49:00 +0000461}
462
463
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100464bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
465 return !Assembler::IsNop(pc());
466}
467
468
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
470 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
471 DCHECK(*pc_ == kCallOpcode);
472 return origin->code_target_object_handle_at(pc_ + 1);
473}
474
475
476Code* RelocInfo::code_age_stub() {
477 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
478 DCHECK(*pc_ == kCallOpcode);
479 return Code::GetCodeFromTargetAddress(
480 Assembler::target_address_at(pc_ + 1, host_));
481}
482
483
484void RelocInfo::set_code_age_stub(Code* stub,
485 ICacheFlushMode icache_flush_mode) {
486 DCHECK(*pc_ == kCallOpcode);
487 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
488 Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
489 icache_flush_mode);
490}
491
492
Steve Blocka7e24c12009-10-30 11:49:00 +0000493Address RelocInfo::call_address() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
Ben Murdochbb769b22010-08-11 14:56:33 +0100495 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Block3ce2e202009-11-05 08:53:23 +0000496 return Memory::Address_at(
497 pc_ + Assembler::kRealPatchReturnSequenceAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000498}
499
500
501void RelocInfo::set_call_address(Address target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
Ben Murdochbb769b22010-08-11 14:56:33 +0100503 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Block3ce2e202009-11-05 08:53:23 +0000504 Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) =
505 target;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 CpuFeatures::FlushICache(
507 pc_ + Assembler::kRealPatchReturnSequenceAddressOffset, sizeof(Address));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100508 if (host() != NULL) {
509 Object* target_code = Code::GetCodeFromTargetAddress(target);
510 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
511 host(), this, HeapObject::cast(target_code));
512 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000513}
514
515
516Object* RelocInfo::call_object() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 return *call_object_address();
518}
519
520
521void RelocInfo::set_call_object(Object* target) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 *call_object_address() = target;
523}
524
525
526Object** RelocInfo::call_object_address() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000527 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
Ben Murdochbb769b22010-08-11 14:56:33 +0100528 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 return reinterpret_cast<Object**>(
530 pc_ + Assembler::kPatchReturnSequenceAddressOffset);
531}
532
Leon Clarkef7060e22010-06-03 12:02:55 +0100533
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100535 RelocInfo::Mode mode = rmode();
536 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100537 visitor->VisitEmbeddedPointer(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 CpuFeatures::FlushICache(pc_, sizeof(Address));
Leon Clarkef7060e22010-06-03 12:02:55 +0100539 } else if (RelocInfo::IsCodeTarget(mode)) {
540 visitor->VisitCodeTarget(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 } else if (mode == RelocInfo::CELL) {
542 visitor->VisitCell(this);
Leon Clarkef7060e22010-06-03 12:02:55 +0100543 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100544 visitor->VisitExternalReference(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 CpuFeatures::FlushICache(pc_, sizeof(Address));
546 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
547 visitor->VisitCodeAgeSequence(this);
Steve Block44f0eee2011-05-26 01:26:41 +0100548 } else if (((RelocInfo::IsJSReturn(mode) &&
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100549 IsPatchedReturnSequence()) ||
550 (RelocInfo::IsDebugBreakSlot(mode) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100551 IsPatchedDebugBreakSlotSequence())) &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552 isolate->debug()->has_break_points()) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100553 visitor->VisitDebugTarget(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 } else if (RelocInfo::IsRuntimeEntry(mode)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100555 visitor->VisitRuntimeEntry(this);
556 }
557}
558
559
Iain Merrick75681382010-08-19 15:07:18 +0100560template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +0100561void RelocInfo::Visit(Heap* heap) {
Iain Merrick75681382010-08-19 15:07:18 +0100562 RelocInfo::Mode mode = rmode();
563 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100564 StaticVisitor::VisitEmbeddedPointer(heap, this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 CpuFeatures::FlushICache(pc_, sizeof(Address));
Iain Merrick75681382010-08-19 15:07:18 +0100566 } else if (RelocInfo::IsCodeTarget(mode)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100567 StaticVisitor::VisitCodeTarget(heap, this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568 } else if (mode == RelocInfo::CELL) {
569 StaticVisitor::VisitCell(heap, this);
Iain Merrick75681382010-08-19 15:07:18 +0100570 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100571 StaticVisitor::VisitExternalReference(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 CpuFeatures::FlushICache(pc_, sizeof(Address));
573 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
574 StaticVisitor::VisitCodeAgeSequence(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100575 } else if (heap->isolate()->debug()->has_break_points() &&
Iain Merrick75681382010-08-19 15:07:18 +0100576 ((RelocInfo::IsJSReturn(mode) &&
577 IsPatchedReturnSequence()) ||
578 (RelocInfo::IsDebugBreakSlot(mode) &&
579 IsPatchedDebugBreakSlotSequence()))) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100580 StaticVisitor::VisitDebugTarget(heap, this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 } else if (RelocInfo::IsRuntimeEntry(mode)) {
Iain Merrick75681382010-08-19 15:07:18 +0100582 StaticVisitor::VisitRuntimeEntry(this);
583 }
584}
585
586
Steve Blocka7e24c12009-10-30 11:49:00 +0000587// -----------------------------------------------------------------------------
588// Implementation of Operand
589
590void Operand::set_modrm(int mod, Register rm_reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 DCHECK(is_uint2(mod));
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 buf_[0] = mod << 6 | rm_reg.low_bits();
593 // Set REX.B to the high bit of rm.code().
594 rex_ |= rm_reg.high_bit();
595}
596
597
598void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000599 DCHECK(len_ == 1);
600 DCHECK(is_uint2(scale));
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 // Use SIB with no index register only for base rsp or r12. Otherwise we
602 // would skip the SIB byte entirely.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000603 DCHECK(!index.is(rsp) || base.is(rsp) || base.is(r12));
Steve Block1e0659c2011-05-24 12:43:12 +0100604 buf_[1] = (scale << 6) | (index.low_bits() << 3) | base.low_bits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 rex_ |= index.high_bit() << 1 | base.high_bit();
606 len_ = 2;
607}
608
609void Operand::set_disp8(int disp) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 DCHECK(is_int8(disp));
611 DCHECK(len_ == 1 || len_ == 2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000612 int8_t* p = reinterpret_cast<int8_t*>(&buf_[len_]);
613 *p = disp;
614 len_ += sizeof(int8_t);
615}
616
617void Operand::set_disp32(int disp) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 DCHECK(len_ == 1 || len_ == 2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000619 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
620 *p = disp;
621 len_ += sizeof(int32_t);
622}
623
624
625} } // namespace v8::internal
626
627#endif // V8_X64_ASSEMBLER_X64_INL_H_