blob: 4c69510caeacd0ded472833cfe228912661bcffa [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
Kristian Monsen25f61362010-05-21 11:50:48 +010038inline Condition NegateCondition(Condition cc) {
Steve Blocka7e24c12009-10-30 11:49:00 +000039 return static_cast<Condition>(cc ^ 1);
40}
41
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43// -----------------------------------------------------------------------------
44// Implementation of Assembler
45
46
47
48void Assembler::emitl(uint32_t x) {
49 Memory::uint32_at(pc_) = x;
50 pc_ += sizeof(uint32_t);
51}
52
53
54void Assembler::emitq(uint64_t x, RelocInfo::Mode rmode) {
55 Memory::uint64_at(pc_) = x;
56 if (rmode != RelocInfo::NONE) {
57 RecordRelocInfo(rmode, x);
58 }
59 pc_ += sizeof(uint64_t);
60}
61
62
63void Assembler::emitw(uint16_t x) {
64 Memory::uint16_at(pc_) = x;
65 pc_ += sizeof(uint16_t);
66}
67
68
Steve Block3ce2e202009-11-05 08:53:23 +000069void Assembler::emit_code_target(Handle<Code> target, RelocInfo::Mode rmode) {
70 ASSERT(RelocInfo::IsCodeTarget(rmode));
71 RecordRelocInfo(rmode);
72 int current = code_targets_.length();
73 if (current > 0 && code_targets_.last().is_identical_to(target)) {
74 // Optimization if we keep jumping to the same code target.
75 emitl(current - 1);
76 } else {
77 code_targets_.Add(target);
78 emitl(current);
79 }
80}
81
82
Steve Blocka7e24c12009-10-30 11:49:00 +000083void Assembler::emit_rex_64(Register reg, Register rm_reg) {
84 emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit());
85}
86
87
88void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) {
89 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
90}
91
92
Steve Block6ded16b2010-05-10 14:33:55 +010093void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) {
94 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
95}
96
97
Steve Blocka7e24c12009-10-30 11:49:00 +000098void Assembler::emit_rex_64(Register reg, const Operand& op) {
99 emit(0x48 | reg.high_bit() << 2 | op.rex_);
100}
101
102
103void Assembler::emit_rex_64(XMMRegister reg, const Operand& op) {
104 emit(0x48 | (reg.code() & 0x8) >> 1 | op.rex_);
105}
106
107
108void Assembler::emit_rex_64(Register rm_reg) {
109 ASSERT_EQ(rm_reg.code() & 0xf, rm_reg.code());
110 emit(0x48 | rm_reg.high_bit());
111}
112
113
114void Assembler::emit_rex_64(const Operand& op) {
115 emit(0x48 | op.rex_);
116}
117
118
119void Assembler::emit_rex_32(Register reg, Register rm_reg) {
120 emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit());
121}
122
123
124void Assembler::emit_rex_32(Register reg, const Operand& op) {
125 emit(0x40 | reg.high_bit() << 2 | op.rex_);
126}
127
128
129void Assembler::emit_rex_32(Register rm_reg) {
130 emit(0x40 | rm_reg.high_bit());
131}
132
133
134void Assembler::emit_rex_32(const Operand& op) {
135 emit(0x40 | op.rex_);
136}
137
138
139void Assembler::emit_optional_rex_32(Register reg, Register rm_reg) {
140 byte rex_bits = reg.high_bit() << 2 | rm_reg.high_bit();
141 if (rex_bits != 0) emit(0x40 | rex_bits);
142}
143
144
145void Assembler::emit_optional_rex_32(Register reg, const Operand& op) {
146 byte rex_bits = reg.high_bit() << 2 | op.rex_;
147 if (rex_bits != 0) emit(0x40 | rex_bits);
148}
149
150
151void Assembler::emit_optional_rex_32(XMMRegister reg, const Operand& op) {
152 byte rex_bits = (reg.code() & 0x8) >> 1 | op.rex_;
153 if (rex_bits != 0) emit(0x40 | rex_bits);
154}
155
156
157void Assembler::emit_optional_rex_32(XMMRegister reg, XMMRegister base) {
158 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
159 if (rex_bits != 0) emit(0x40 | rex_bits);
160}
161
162
163void Assembler::emit_optional_rex_32(XMMRegister reg, Register base) {
164 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
165 if (rex_bits != 0) emit(0x40 | rex_bits);
166}
167
168
Steve Block6ded16b2010-05-10 14:33:55 +0100169void Assembler::emit_optional_rex_32(Register reg, XMMRegister base) {
170 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
171 if (rex_bits != 0) emit(0x40 | rex_bits);
172}
173
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175void Assembler::emit_optional_rex_32(Register rm_reg) {
176 if (rm_reg.high_bit()) emit(0x41);
177}
178
179
180void Assembler::emit_optional_rex_32(const Operand& op) {
181 if (op.rex_ != 0) emit(0x40 | op.rex_);
182}
183
184
185Address Assembler::target_address_at(Address pc) {
Steve Block3ce2e202009-11-05 08:53:23 +0000186 return Memory::int32_at(pc) + pc + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +0000187}
188
189
190void Assembler::set_target_address_at(Address pc, Address target) {
Steve Blockd0582a62009-12-15 09:54:21 +0000191 Memory::int32_at(pc) = static_cast<int32_t>(target - pc - 4);
Steve Block3ce2e202009-11-05 08:53:23 +0000192 CPU::FlushICache(pc, sizeof(int32_t));
Steve Blocka7e24c12009-10-30 11:49:00 +0000193}
194
Steve Block3ce2e202009-11-05 08:53:23 +0000195Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
196 return code_targets_[Memory::int32_at(pc)];
197}
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199// -----------------------------------------------------------------------------
200// Implementation of RelocInfo
201
202// The modes possibly affected by apply must be in kApplyMask.
203void RelocInfo::apply(intptr_t delta) {
204 if (IsInternalReference(rmode_)) {
205 // absolute code pointer inside code object moves with the code object.
Steve Blockd0582a62009-12-15 09:54:21 +0000206 Memory::Address_at(pc_) += static_cast<int32_t>(delta);
Steve Block3ce2e202009-11-05 08:53:23 +0000207 } else if (IsCodeTarget(rmode_)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000208 Memory::int32_at(pc_) -= static_cast<int32_t>(delta);
Steve Block3ce2e202009-11-05 08:53:23 +0000209 } else if (rmode_ == JS_RETURN && IsPatchedReturnSequence()) {
210 // Special handling of js_return when a break point is set (call
211 // instruction has been inserted).
Steve Blockd0582a62009-12-15 09:54:21 +0000212 Memory::int32_at(pc_ + 1) -= static_cast<int32_t>(delta); // relocate entry
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 }
214}
215
216
217Address RelocInfo::target_address() {
218 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
Steve Block3ce2e202009-11-05 08:53:23 +0000219 if (IsCodeTarget(rmode_)) {
220 return Assembler::target_address_at(pc_);
221 } else {
222 return Memory::Address_at(pc_);
223 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000224}
225
226
227Address RelocInfo::target_address_address() {
228 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
229 return reinterpret_cast<Address>(pc_);
230}
231
232
Leon Clarkef7060e22010-06-03 12:02:55 +0100233int RelocInfo::target_address_size() {
234 if (IsCodedSpecially()) {
235 return Assembler::kCallTargetSize;
236 } else {
237 return Assembler::kExternalTargetSize;
238 }
239}
240
241
Steve Blocka7e24c12009-10-30 11:49:00 +0000242void RelocInfo::set_target_address(Address target) {
243 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
Steve Block3ce2e202009-11-05 08:53:23 +0000244 if (IsCodeTarget(rmode_)) {
245 Assembler::set_target_address_at(pc_, target);
246 } else {
247 Memory::Address_at(pc_) = target;
248 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000249}
250
251
252Object* RelocInfo::target_object() {
253 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Steve Block3ce2e202009-11-05 08:53:23 +0000254 return Memory::Object_at(pc_);
255}
256
257
258Handle<Object> RelocInfo::target_object_handle(Assembler *origin) {
259 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
260 if (rmode_ == EMBEDDED_OBJECT) {
261 return Memory::Object_Handle_at(pc_);
262 } else {
263 return origin->code_target_object_handle_at(pc_);
264 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000265}
266
267
268Object** RelocInfo::target_object_address() {
269 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
270 return reinterpret_cast<Object**>(pc_);
271}
272
273
274Address* RelocInfo::target_reference_address() {
275 ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
276 return reinterpret_cast<Address*>(pc_);
277}
278
279
280void RelocInfo::set_target_object(Object* target) {
281 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
282 *reinterpret_cast<Object**>(pc_) = target;
283}
284
285
Steve Block3ce2e202009-11-05 08:53:23 +0000286bool RelocInfo::IsPatchedReturnSequence() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000287 // The recognized call sequence is:
288 // movq(kScratchRegister, immediate64); call(kScratchRegister);
289 // It only needs to be distinguished from a return sequence
290 // movq(rsp, rbp); pop(rbp); ret(n); int3 *6
291 // The 11th byte is int3 (0xCC) in the return sequence and
292 // REX.WB (0x48+register bit) for the call sequence.
Steve Block3ce2e202009-11-05 08:53:23 +0000293#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 return pc_[10] != 0xCC;
Steve Block3ce2e202009-11-05 08:53:23 +0000295#else
296 return false;
297#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000298}
299
300
301Address RelocInfo::call_address() {
Steve Block3ce2e202009-11-05 08:53:23 +0000302 ASSERT(IsPatchedReturnSequence());
303 return Memory::Address_at(
304 pc_ + Assembler::kRealPatchReturnSequenceAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000305}
306
307
308void RelocInfo::set_call_address(Address target) {
Steve Block3ce2e202009-11-05 08:53:23 +0000309 ASSERT(IsPatchedReturnSequence());
310 Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) =
311 target;
Steve Blocka7e24c12009-10-30 11:49:00 +0000312}
313
314
315Object* RelocInfo::call_object() {
Steve Block3ce2e202009-11-05 08:53:23 +0000316 ASSERT(IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 return *call_object_address();
318}
319
320
321void RelocInfo::set_call_object(Object* target) {
Steve Block3ce2e202009-11-05 08:53:23 +0000322 ASSERT(IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 *call_object_address() = target;
324}
325
326
327Object** RelocInfo::call_object_address() {
Steve Block3ce2e202009-11-05 08:53:23 +0000328 ASSERT(IsPatchedReturnSequence());
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 return reinterpret_cast<Object**>(
330 pc_ + Assembler::kPatchReturnSequenceAddressOffset);
331}
332
Leon Clarkef7060e22010-06-03 12:02:55 +0100333
334void RelocInfo::Visit(ObjectVisitor* visitor) {
335 RelocInfo::Mode mode = rmode();
336 if (mode == RelocInfo::EMBEDDED_OBJECT) {
337 visitor->VisitPointer(target_object_address());
338 } else if (RelocInfo::IsCodeTarget(mode)) {
339 visitor->VisitCodeTarget(this);
340 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
341 visitor->VisitExternalReference(target_reference_address());
342#ifdef ENABLE_DEBUGGER_SUPPORT
343 } else if (Debug::has_break_points() &&
344 RelocInfo::IsJSReturn(mode) &&
345 IsPatchedReturnSequence()) {
346 visitor->VisitDebugTarget(this);
347#endif
348 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
349 visitor->VisitRuntimeEntry(this);
350 }
351}
352
353
Steve Blocka7e24c12009-10-30 11:49:00 +0000354// -----------------------------------------------------------------------------
355// Implementation of Operand
356
357void Operand::set_modrm(int mod, Register rm_reg) {
358 ASSERT(is_uint2(mod));
359 buf_[0] = mod << 6 | rm_reg.low_bits();
360 // Set REX.B to the high bit of rm.code().
361 rex_ |= rm_reg.high_bit();
362}
363
364
365void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
366 ASSERT(len_ == 1);
367 ASSERT(is_uint2(scale));
368 // Use SIB with no index register only for base rsp or r12. Otherwise we
369 // would skip the SIB byte entirely.
370 ASSERT(!index.is(rsp) || base.is(rsp) || base.is(r12));
371 buf_[1] = scale << 6 | index.low_bits() << 3 | base.low_bits();
372 rex_ |= index.high_bit() << 1 | base.high_bit();
373 len_ = 2;
374}
375
376void Operand::set_disp8(int disp) {
377 ASSERT(is_int8(disp));
378 ASSERT(len_ == 1 || len_ == 2);
379 int8_t* p = reinterpret_cast<int8_t*>(&buf_[len_]);
380 *p = disp;
381 len_ += sizeof(int8_t);
382}
383
384void Operand::set_disp32(int disp) {
385 ASSERT(len_ == 1 || len_ == 2);
386 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
387 *p = disp;
388 len_ += sizeof(int32_t);
389}
390
391
392} } // namespace v8::internal
393
394#endif // V8_X64_ASSEMBLER_X64_INL_H_