1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
15 // - Neither the name of Sun Microsystems or the names of contributors may
16 // be used to endorse or promote products derived from this software without
17 // specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // The original source code covered by the above license above has been
32 // modified significantly by Google Inc.
33 // Copyright 2012 the V8 project authors. All rights reserved.
35 // A light-weight IA32 Assembler.
37 #ifndef V8_X87_ASSEMBLER_X87_INL_H_
38 #define V8_X87_ASSEMBLER_X87_INL_H_
40 #include "src/x87/assembler-x87.h"
42 #include "src/assembler.h"
43 #include "src/debug/debug.h"
48 bool CpuFeatures::SupportsCrankshaft() { return true; }
51 static const byte kCallOpcode = 0xE8;
52 static const int kNoCodeAgeSequenceLength = 5;
55 // The modes possibly affected by apply must be in kApplyMask.
56 void RelocInfo::apply(intptr_t delta) {
57 if (IsRuntimeEntry(rmode_) || IsCodeTarget(rmode_)) {
58 int32_t* p = reinterpret_cast<int32_t*>(pc_);
59 *p -= delta; // Relocate entry.
60 } else if (IsCodeAgeSequence(rmode_)) {
61 if (*pc_ == kCallOpcode) {
62 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
63 *p -= delta; // Relocate entry.
65 } else if (IsDebugBreakSlot(rmode_) && IsPatchedDebugBreakSlotSequence()) {
66 // Special handling of a debug break slot when a break point is set (call
67 // instruction has been inserted).
68 int32_t* p = reinterpret_cast<int32_t*>(
69 pc_ + Assembler::kPatchDebugBreakSlotAddressOffset);
70 *p -= delta; // Relocate entry.
71 } else if (IsInternalReference(rmode_)) {
72 // absolute code pointer inside code object moves with the code object.
73 int32_t* p = reinterpret_cast<int32_t*>(pc_);
74 *p += delta; // Relocate entry.
79 Address RelocInfo::target_address() {
80 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
81 return Assembler::target_address_at(pc_, host_);
85 Address RelocInfo::target_address_address() {
86 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
87 || rmode_ == EMBEDDED_OBJECT
88 || rmode_ == EXTERNAL_REFERENCE);
89 return reinterpret_cast<Address>(pc_);
93 Address RelocInfo::constant_pool_entry_address() {
99 int RelocInfo::target_address_size() {
100 return Assembler::kSpecialTargetSize;
104 void RelocInfo::set_target_address(Address target,
105 WriteBarrierMode write_barrier_mode,
106 ICacheFlushMode icache_flush_mode) {
107 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
108 Assembler::set_target_address_at(pc_, host_, target);
109 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
110 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
111 IsCodeTarget(rmode_)) {
112 Object* target_code = Code::GetCodeFromTargetAddress(target);
113 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
114 host(), this, HeapObject::cast(target_code));
119 Object* RelocInfo::target_object() {
120 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
121 return Memory::Object_at(pc_);
125 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
126 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
127 return Memory::Object_Handle_at(pc_);
131 void RelocInfo::set_target_object(Object* target,
132 WriteBarrierMode write_barrier_mode,
133 ICacheFlushMode icache_flush_mode) {
134 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
135 Memory::Object_at(pc_) = target;
136 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
137 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
139 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
141 target->IsHeapObject()) {
142 host()->GetHeap()->incremental_marking()->RecordWrite(
143 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
148 Address RelocInfo::target_external_reference() {
149 DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
150 return Memory::Address_at(pc_);
154 Address RelocInfo::target_internal_reference() {
155 DCHECK(rmode_ == INTERNAL_REFERENCE);
156 return Memory::Address_at(pc_);
160 Address RelocInfo::target_internal_reference_address() {
161 DCHECK(rmode_ == INTERNAL_REFERENCE);
162 return reinterpret_cast<Address>(pc_);
166 Address RelocInfo::target_runtime_entry(Assembler* origin) {
167 DCHECK(IsRuntimeEntry(rmode_));
168 return reinterpret_cast<Address>(*reinterpret_cast<int32_t*>(pc_));
172 void RelocInfo::set_target_runtime_entry(Address target,
173 WriteBarrierMode write_barrier_mode,
174 ICacheFlushMode icache_flush_mode) {
175 DCHECK(IsRuntimeEntry(rmode_));
176 if (target_address() != target) {
177 set_target_address(target, write_barrier_mode, icache_flush_mode);
182 Handle<Cell> RelocInfo::target_cell_handle() {
183 DCHECK(rmode_ == RelocInfo::CELL);
184 Address address = Memory::Address_at(pc_);
185 return Handle<Cell>(reinterpret_cast<Cell**>(address));
189 Cell* RelocInfo::target_cell() {
190 DCHECK(rmode_ == RelocInfo::CELL);
191 return Cell::FromValueAddress(Memory::Address_at(pc_));
195 void RelocInfo::set_target_cell(Cell* cell,
196 WriteBarrierMode write_barrier_mode,
197 ICacheFlushMode icache_flush_mode) {
198 DCHECK(cell->IsCell());
199 DCHECK(rmode_ == RelocInfo::CELL);
200 Address address = cell->address() + Cell::kValueOffset;
201 Memory::Address_at(pc_) = address;
202 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
203 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
205 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
206 // TODO(1550) We are passing NULL as a slot because cell can never be on
207 // evacuation candidate.
208 host()->GetHeap()->incremental_marking()->RecordWrite(
214 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
215 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
216 DCHECK(*pc_ == kCallOpcode);
217 return Memory::Object_Handle_at(pc_ + 1);
221 Code* RelocInfo::code_age_stub() {
222 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
223 DCHECK(*pc_ == kCallOpcode);
224 return Code::GetCodeFromTargetAddress(
225 Assembler::target_address_at(pc_ + 1, host_));
229 void RelocInfo::set_code_age_stub(Code* stub,
230 ICacheFlushMode icache_flush_mode) {
231 DCHECK(*pc_ == kCallOpcode);
232 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
233 Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
238 Address RelocInfo::debug_call_address() {
239 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
240 Address location = pc_ + Assembler::kPatchDebugBreakSlotAddressOffset;
241 return Assembler::target_address_at(location, host_);
245 void RelocInfo::set_debug_call_address(Address target) {
246 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
247 Address location = pc_ + Assembler::kPatchDebugBreakSlotAddressOffset;
248 Assembler::set_target_address_at(location, host_, target);
249 if (host() != NULL) {
250 Object* target_code = Code::GetCodeFromTargetAddress(target);
251 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
252 host(), this, HeapObject::cast(target_code));
257 void RelocInfo::WipeOut() {
258 if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_) ||
259 IsInternalReference(rmode_)) {
260 Memory::Address_at(pc_) = NULL;
261 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
262 // Effectively write zero into the relocation.
263 Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
270 bool RelocInfo::IsPatchedReturnSequence() {
271 return *pc_ == kCallOpcode;
275 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
276 return !Assembler::IsNop(pc());
280 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
281 RelocInfo::Mode mode = rmode();
282 if (mode == RelocInfo::EMBEDDED_OBJECT) {
283 visitor->VisitEmbeddedPointer(this);
284 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
285 } else if (RelocInfo::IsCodeTarget(mode)) {
286 visitor->VisitCodeTarget(this);
287 } else if (mode == RelocInfo::CELL) {
288 visitor->VisitCell(this);
289 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
290 visitor->VisitExternalReference(this);
291 } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
292 visitor->VisitInternalReference(this);
293 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
294 visitor->VisitCodeAgeSequence(this);
295 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
296 IsPatchedDebugBreakSlotSequence()) {
297 visitor->VisitDebugTarget(this);
298 } else if (IsRuntimeEntry(mode)) {
299 visitor->VisitRuntimeEntry(this);
304 template<typename StaticVisitor>
305 void RelocInfo::Visit(Heap* heap) {
306 RelocInfo::Mode mode = rmode();
307 if (mode == RelocInfo::EMBEDDED_OBJECT) {
308 StaticVisitor::VisitEmbeddedPointer(heap, this);
309 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
310 } else if (RelocInfo::IsCodeTarget(mode)) {
311 StaticVisitor::VisitCodeTarget(heap, this);
312 } else if (mode == RelocInfo::CELL) {
313 StaticVisitor::VisitCell(heap, this);
314 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
315 StaticVisitor::VisitExternalReference(this);
316 } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
317 StaticVisitor::VisitInternalReference(this);
318 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
319 StaticVisitor::VisitCodeAgeSequence(heap, this);
320 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
321 IsPatchedDebugBreakSlotSequence()) {
322 StaticVisitor::VisitDebugTarget(heap, this);
323 } else if (IsRuntimeEntry(mode)) {
324 StaticVisitor::VisitRuntimeEntry(this);
330 Immediate::Immediate(int x) {
332 rmode_ = RelocInfo::NONE32;
336 Immediate::Immediate(const ExternalReference& ext) {
337 x_ = reinterpret_cast<int32_t>(ext.address());
338 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
342 Immediate::Immediate(Label* internal_offset) {
343 x_ = reinterpret_cast<int32_t>(internal_offset);
344 rmode_ = RelocInfo::INTERNAL_REFERENCE;
348 Immediate::Immediate(Handle<Object> handle) {
349 AllowDeferredHandleDereference using_raw_address;
350 // Verify all Objects referred by code are NOT in new space.
351 Object* obj = *handle;
352 if (obj->IsHeapObject()) {
353 DCHECK(!HeapObject::cast(obj)->GetHeap()->InNewSpace(obj));
354 x_ = reinterpret_cast<intptr_t>(handle.location());
355 rmode_ = RelocInfo::EMBEDDED_OBJECT;
357 // no relocation needed
358 x_ = reinterpret_cast<intptr_t>(obj);
359 rmode_ = RelocInfo::NONE32;
364 Immediate::Immediate(Smi* value) {
365 x_ = reinterpret_cast<intptr_t>(value);
366 rmode_ = RelocInfo::NONE32;
370 Immediate::Immediate(Address addr) {
371 x_ = reinterpret_cast<int32_t>(addr);
372 rmode_ = RelocInfo::NONE32;
376 void Assembler::emit(uint32_t x) {
377 *reinterpret_cast<uint32_t*>(pc_) = x;
378 pc_ += sizeof(uint32_t);
382 void Assembler::emit_q(uint64_t x) {
383 *reinterpret_cast<uint64_t*>(pc_) = x;
384 pc_ += sizeof(uint64_t);
388 void Assembler::emit(Handle<Object> handle) {
389 AllowDeferredHandleDereference heap_object_check;
390 // Verify all Objects referred by code are NOT in new space.
391 Object* obj = *handle;
392 DCHECK(!isolate()->heap()->InNewSpace(obj));
393 if (obj->IsHeapObject()) {
394 emit(reinterpret_cast<intptr_t>(handle.location()),
395 RelocInfo::EMBEDDED_OBJECT);
397 // no relocation needed
398 emit(reinterpret_cast<intptr_t>(obj));
403 void Assembler::emit(uint32_t x, RelocInfo::Mode rmode, TypeFeedbackId id) {
404 if (rmode == RelocInfo::CODE_TARGET && !id.IsNone()) {
405 RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, id.ToInt());
406 } else if (!RelocInfo::IsNone(rmode)
407 && rmode != RelocInfo::CODE_AGE_SEQUENCE) {
408 RecordRelocInfo(rmode);
414 void Assembler::emit(Handle<Code> code,
415 RelocInfo::Mode rmode,
417 AllowDeferredHandleDereference embedding_raw_address;
418 emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
422 void Assembler::emit(const Immediate& x) {
423 if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
424 Label* label = reinterpret_cast<Label*>(x.x_);
425 emit_code_relative_offset(label);
428 if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
433 void Assembler::emit_code_relative_offset(Label* label) {
434 if (label->is_bound()) {
436 pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
439 emit_disp(label, Displacement::CODE_RELATIVE);
444 void Assembler::emit_w(const Immediate& x) {
445 DCHECK(RelocInfo::IsNone(x.rmode_));
446 uint16_t value = static_cast<uint16_t>(x.x_);
447 reinterpret_cast<uint16_t*>(pc_)[0] = value;
448 pc_ += sizeof(uint16_t);
452 Address Assembler::target_address_at(Address pc, Address constant_pool) {
453 return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
457 void Assembler::set_target_address_at(Address pc, Address constant_pool,
459 ICacheFlushMode icache_flush_mode) {
460 int32_t* p = reinterpret_cast<int32_t*>(pc);
461 *p = target - (pc + sizeof(int32_t));
462 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
463 Assembler::FlushICacheWithoutIsolate(p, sizeof(int32_t));
468 Address Assembler::target_address_from_return_address(Address pc) {
469 return pc - kCallTargetAddressOffset;
473 Displacement Assembler::disp_at(Label* L) {
474 return Displacement(long_at(L->pos()));
478 void Assembler::disp_at_put(Label* L, Displacement disp) {
479 long_at_put(L->pos(), disp.data());
483 void Assembler::emit_disp(Label* L, Displacement::Type type) {
484 Displacement disp(L, type);
485 L->link_to(pc_offset());
486 emit(static_cast<int>(disp.data()));
490 void Assembler::emit_near_disp(Label* L) {
492 if (L->is_near_linked()) {
493 int offset = L->near_link_pos() - pc_offset();
494 DCHECK(is_int8(offset));
495 disp = static_cast<byte>(offset & 0xFF);
497 L->link_to(pc_offset(), Label::kNear);
502 void Assembler::deserialization_set_target_internal_reference_at(
503 Address pc, Address target, RelocInfo::Mode mode) {
504 Memory::Address_at(pc) = target;
508 void Operand::set_modrm(int mod, Register rm) {
509 DCHECK((mod & -4) == 0);
510 buf_[0] = mod << 6 | rm.code();
515 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
517 DCHECK((scale & -4) == 0);
518 // Use SIB with no index register only for base esp.
519 DCHECK(!index.is(esp) || base.is(esp));
520 buf_[1] = scale << 6 | index.code() << 3 | base.code();
525 void Operand::set_disp8(int8_t disp) {
526 DCHECK(len_ == 1 || len_ == 2);
527 *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
531 void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) {
532 DCHECK(len_ == 1 || len_ == 2);
533 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
535 len_ += sizeof(int32_t);
539 Operand::Operand(Register reg) {
545 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
548 set_dispr(disp, rmode);
552 Operand::Operand(Immediate imm) {
555 set_dispr(imm.x_, imm.rmode_);
557 } } // namespace v8::internal
559 #endif // V8_X87_ASSEMBLER_X87_INL_H_