blob: 1fe9eed4daca02dbefac7538a10c2af1c6578a29 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_X64_ASSEMBLER_X64_INL_H_
29#define V8_X64_ASSEMBLER_X64_INL_H_
30
31#include "cpu.h"
Leon Clarkef7060e22010-06-03 12:02:55 +010032#include "debug.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033#include "memory.h"
34
35namespace v8 {
36namespace internal {
37
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39// -----------------------------------------------------------------------------
40// Implementation of Assembler
41
42
Steve Blocka7e24c12009-10-30 11:49:00 +000043void Assembler::emitl(uint32_t x) {
44 Memory::uint32_at(pc_) = x;
45 pc_ += sizeof(uint32_t);
46}
47
48
49void Assembler::emitq(uint64_t x, RelocInfo::Mode rmode) {
50 Memory::uint64_at(pc_) = x;
51 if (rmode != RelocInfo::NONE) {
52 RecordRelocInfo(rmode, x);
53 }
54 pc_ += sizeof(uint64_t);
55}
56
57
58void Assembler::emitw(uint16_t x) {
59 Memory::uint16_at(pc_) = x;
60 pc_ += sizeof(uint16_t);
61}
62
63
Steve Block3ce2e202009-11-05 08:53:23 +000064void Assembler::emit_code_target(Handle<Code> target, RelocInfo::Mode rmode) {
65 ASSERT(RelocInfo::IsCodeTarget(rmode));
66 RecordRelocInfo(rmode);
67 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
Steve Blocka7e24c12009-10-30 11:49:00 +000078void Assembler::emit_rex_64(Register reg, Register rm_reg) {
79 emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit());
80}
81
82
83void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) {
84 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
85}
86
87
Steve Block6ded16b2010-05-10 14:33:55 +010088void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) {
89 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
90}
91
92
Steve Blocka7e24c12009-10-30 11:49:00 +000093void Assembler::emit_rex_64(Register reg, const Operand& op) {
94 emit(0x48 | reg.high_bit() << 2 | op.rex_);
95}
96
97
98void Assembler::emit_rex_64(XMMRegister reg, const Operand& op) {
99 emit(0x48 | (reg.code() & 0x8) >> 1 | op.rex_);
100}
101
102
103void Assembler::emit_rex_64(Register rm_reg) {
104 ASSERT_EQ(rm_reg.code() & 0xf, rm_reg.code());
105 emit(0x48 | rm_reg.high_bit());
106}
107
108
109void Assembler::emit_rex_64(const Operand& op) {
110 emit(0x48 | op.rex_);
111}
112
113
114void Assembler::emit_rex_32(Register reg, Register rm_reg) {
115 emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit());
116}
117
118
119void Assembler::emit_rex_32(Register reg, const Operand& op) {
120 emit(0x40 | reg.high_bit() << 2 | op.rex_);
121}
122
123
124void Assembler::emit_rex_32(Register rm_reg) {
125 emit(0x40 | rm_reg.high_bit());
126}
127
128
129void Assembler::emit_rex_32(const Operand& op) {
130 emit(0x40 | op.rex_);
131}
132
133
134void Assembler::emit_optional_rex_32(Register reg, Register rm_reg) {
135 byte rex_bits = reg.high_bit() << 2 | rm_reg.high_bit();
136 if (rex_bits != 0) emit(0x40 | rex_bits);
137}
138
139
140void Assembler::emit_optional_rex_32(Register reg, const Operand& op) {
141 byte rex_bits = reg.high_bit() << 2 | op.rex_;
142 if (rex_bits != 0) emit(0x40 | rex_bits);
143}
144
145
146void Assembler::emit_optional_rex_32(XMMRegister reg, const Operand& op) {
147 byte rex_bits = (reg.code() & 0x8) >> 1 | op.rex_;
148 if (rex_bits != 0) emit(0x40 | rex_bits);
149}
150
151
152void Assembler::emit_optional_rex_32(XMMRegister reg, XMMRegister base) {
153 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
154 if (rex_bits != 0) emit(0x40 | rex_bits);
155}
156
157
158void Assembler::emit_optional_rex_32(XMMRegister reg, Register base) {
159 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
160 if (rex_bits != 0) emit(0x40 | rex_bits);
161}
162
163
Steve Block6ded16b2010-05-10 14:33:55 +0100164void Assembler::emit_optional_rex_32(Register reg, XMMRegister base) {
165 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
166 if (rex_bits != 0) emit(0x40 | rex_bits);
167}
168
169
Steve Blocka7e24c12009-10-30 11:49:00 +0000170void Assembler::emit_optional_rex_32(Register rm_reg) {
171 if (rm_reg.high_bit()) emit(0x41);
172}
173
174
175void Assembler::emit_optional_rex_32(const Operand& op) {
176 if (op.rex_ != 0) emit(0x40 | op.rex_);
177}
178
179
180Address Assembler::target_address_at(Address pc) {
Steve Block3ce2e202009-11-05 08:53:23 +0000181 return Memory::int32_at(pc) + pc + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +0000182}
183
184
185void Assembler::set_target_address_at(Address pc, Address target) {
Steve Blockd0582a62009-12-15 09:54:21 +0000186 Memory::int32_at(pc) = static_cast<int32_t>(target - pc - 4);
Steve Block3ce2e202009-11-05 08:53:23 +0000187 CPU::FlushICache(pc, sizeof(int32_t));
Steve Blocka7e24c12009-10-30 11:49:00 +0000188}
189
Steve Block3ce2e202009-11-05 08:53:23 +0000190Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
191 return code_targets_[Memory::int32_at(pc)];
192}
Steve Blocka7e24c12009-10-30 11:49:00 +0000193
194// -----------------------------------------------------------------------------
195// Implementation of RelocInfo
196
197// The modes possibly affected by apply must be in kApplyMask.
198void RelocInfo::apply(intptr_t delta) {
199 if (IsInternalReference(rmode_)) {
200 // absolute code pointer inside code object moves with the code object.
Steve Blockd0582a62009-12-15 09:54:21 +0000201 Memory::Address_at(pc_) += static_cast<int32_t>(delta);
Steve Block3ce2e202009-11-05 08:53:23 +0000202 } else if (IsCodeTarget(rmode_)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000203 Memory::int32_at(pc_) -= static_cast<int32_t>(delta);
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 }
205}
206
207
208Address RelocInfo::target_address() {
209 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
Steve Block3ce2e202009-11-05 08:53:23 +0000210 if (IsCodeTarget(rmode_)) {
211 return Assembler::target_address_at(pc_);
212 } else {
213 return Memory::Address_at(pc_);
214 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000215}
216
217
218Address RelocInfo::target_address_address() {
219 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
220 return reinterpret_cast<Address>(pc_);
221}
222
223
Leon Clarkef7060e22010-06-03 12:02:55 +0100224int RelocInfo::target_address_size() {
225 if (IsCodedSpecially()) {
226 return Assembler::kCallTargetSize;
227 } else {
228 return Assembler::kExternalTargetSize;
229 }
230}
231
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233void RelocInfo::set_target_address(Address target) {
234 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
Steve Block3ce2e202009-11-05 08:53:23 +0000235 if (IsCodeTarget(rmode_)) {
236 Assembler::set_target_address_at(pc_, target);
237 } else {
238 Memory::Address_at(pc_) = target;
239 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000240}
241
242
243Object* RelocInfo::target_object() {
244 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Steve Block3ce2e202009-11-05 08:53:23 +0000245 return Memory::Object_at(pc_);
246}
247
248
249Handle<Object> RelocInfo::target_object_handle(Assembler *origin) {
250 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
251 if (rmode_ == EMBEDDED_OBJECT) {
252 return Memory::Object_Handle_at(pc_);
253 } else {
254 return origin->code_target_object_handle_at(pc_);
255 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000256}
257
258
259Object** RelocInfo::target_object_address() {
260 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
261 return reinterpret_cast<Object**>(pc_);
262}
263
264
265Address* RelocInfo::target_reference_address() {
266 ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
267 return reinterpret_cast<Address*>(pc_);
268}
269
270
271void RelocInfo::set_target_object(Object* target) {
272 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
273 *reinterpret_cast<Object**>(pc_) = target;
274}
275
276
Ben Murdochb0fe1622011-05-05 13:52:32 +0100277Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
278 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
279 Address address = Memory::Address_at(pc_);
280 return Handle<JSGlobalPropertyCell>(
281 reinterpret_cast<JSGlobalPropertyCell**>(address));
282}
283
284
285JSGlobalPropertyCell* RelocInfo::target_cell() {
286 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
287 Address address = Memory::Address_at(pc_);
288 Object* object = HeapObject::FromAddress(
289 address - JSGlobalPropertyCell::kValueOffset);
290 return reinterpret_cast<JSGlobalPropertyCell*>(object);
291}
292
293
294void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell) {
295 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
296 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
297 Memory::Address_at(pc_) = address;
298}
299
300
Steve Block3ce2e202009-11-05 08:53:23 +0000301bool RelocInfo::IsPatchedReturnSequence() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 // The recognized call sequence is:
303 // movq(kScratchRegister, immediate64); call(kScratchRegister);
304 // It only needs to be distinguished from a return sequence
305 // movq(rsp, rbp); pop(rbp); ret(n); int3 *6
306 // The 11th byte is int3 (0xCC) in the return sequence and
307 // REX.WB (0x48+register bit) for the call sequence.
Steve Block3ce2e202009-11-05 08:53:23 +0000308#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 return pc_[10] != 0xCC;
Steve Block3ce2e202009-11-05 08:53:23 +0000310#else
311 return false;
312#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000313}
314
315
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100316bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
317 return !Assembler::IsNop(pc());
318}
319
320
Steve Blocka7e24c12009-10-30 11:49:00 +0000321Address RelocInfo::call_address() {
Ben Murdochbb769b22010-08-11 14:56:33 +0100322 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
323 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Block3ce2e202009-11-05 08:53:23 +0000324 return Memory::Address_at(
325 pc_ + Assembler::kRealPatchReturnSequenceAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326}
327
328
329void RelocInfo::set_call_address(Address target) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100330 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
331 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Block3ce2e202009-11-05 08:53:23 +0000332 Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) =
333 target;
Steve Blocka7e24c12009-10-30 11:49:00 +0000334}
335
336
337Object* RelocInfo::call_object() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 return *call_object_address();
339}
340
341
342void RelocInfo::set_call_object(Object* target) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 *call_object_address() = target;
344}
345
346
347Object** RelocInfo::call_object_address() {
Ben Murdochbb769b22010-08-11 14:56:33 +0100348 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
349 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 return reinterpret_cast<Object**>(
351 pc_ + Assembler::kPatchReturnSequenceAddressOffset);
352}
353
Leon Clarkef7060e22010-06-03 12:02:55 +0100354
355void RelocInfo::Visit(ObjectVisitor* visitor) {
356 RelocInfo::Mode mode = rmode();
357 if (mode == RelocInfo::EMBEDDED_OBJECT) {
358 visitor->VisitPointer(target_object_address());
359 } else if (RelocInfo::IsCodeTarget(mode)) {
360 visitor->VisitCodeTarget(this);
361 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
362 visitor->VisitExternalReference(target_reference_address());
363#ifdef ENABLE_DEBUGGER_SUPPORT
364 } else if (Debug::has_break_points() &&
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100365 ((RelocInfo::IsJSReturn(mode) &&
366 IsPatchedReturnSequence()) ||
367 (RelocInfo::IsDebugBreakSlot(mode) &&
368 IsPatchedDebugBreakSlotSequence()))) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100369 visitor->VisitDebugTarget(this);
370#endif
371 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
372 visitor->VisitRuntimeEntry(this);
373 }
374}
375
376
Iain Merrick75681382010-08-19 15:07:18 +0100377template<typename StaticVisitor>
378void RelocInfo::Visit() {
379 RelocInfo::Mode mode = rmode();
380 if (mode == RelocInfo::EMBEDDED_OBJECT) {
381 StaticVisitor::VisitPointer(target_object_address());
382 } else if (RelocInfo::IsCodeTarget(mode)) {
383 StaticVisitor::VisitCodeTarget(this);
384 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
385 StaticVisitor::VisitExternalReference(target_reference_address());
386#ifdef ENABLE_DEBUGGER_SUPPORT
387 } else if (Debug::has_break_points() &&
388 ((RelocInfo::IsJSReturn(mode) &&
389 IsPatchedReturnSequence()) ||
390 (RelocInfo::IsDebugBreakSlot(mode) &&
391 IsPatchedDebugBreakSlotSequence()))) {
392 StaticVisitor::VisitDebugTarget(this);
393#endif
394 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
395 StaticVisitor::VisitRuntimeEntry(this);
396 }
397}
398
399
Steve Blocka7e24c12009-10-30 11:49:00 +0000400// -----------------------------------------------------------------------------
401// Implementation of Operand
402
403void Operand::set_modrm(int mod, Register rm_reg) {
404 ASSERT(is_uint2(mod));
405 buf_[0] = mod << 6 | rm_reg.low_bits();
406 // Set REX.B to the high bit of rm.code().
407 rex_ |= rm_reg.high_bit();
408}
409
410
411void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
412 ASSERT(len_ == 1);
413 ASSERT(is_uint2(scale));
414 // Use SIB with no index register only for base rsp or r12. Otherwise we
415 // would skip the SIB byte entirely.
416 ASSERT(!index.is(rsp) || base.is(rsp) || base.is(r12));
417 buf_[1] = scale << 6 | index.low_bits() << 3 | base.low_bits();
418 rex_ |= index.high_bit() << 1 | base.high_bit();
419 len_ = 2;
420}
421
422void Operand::set_disp8(int disp) {
423 ASSERT(is_int8(disp));
424 ASSERT(len_ == 1 || len_ == 2);
425 int8_t* p = reinterpret_cast<int8_t*>(&buf_[len_]);
426 *p = disp;
427 len_ += sizeof(int8_t);
428}
429
430void Operand::set_disp32(int disp) {
431 ASSERT(len_ == 1 || len_ == 2);
432 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
433 *p = disp;
434 len_ += sizeof(int32_t);
435}
436
437
438} } // namespace v8::internal
439
440#endif // V8_X64_ASSEMBLER_X64_INL_H_