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_IA32_ASSEMBLER_IA32_INL_H_
38 #define V8_IA32_ASSEMBLER_IA32_INL_H_
40 #include "src/ia32/assembler-ia32.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 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
109 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
110 IsCodeTarget(rmode_)) {
111 Object* target_code = Code::GetCodeFromTargetAddress(target);
112 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
113 host(), this, HeapObject::cast(target_code));
118 Object* RelocInfo::target_object() {
119 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
120 return Memory::Object_at(pc_);
124 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
125 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
126 return Memory::Object_Handle_at(pc_);
130 void RelocInfo::set_target_object(Object* target,
131 WriteBarrierMode write_barrier_mode,
132 ICacheFlushMode icache_flush_mode) {
133 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
134 Memory::Object_at(pc_) = target;
135 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
136 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
138 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
140 target->IsHeapObject()) {
141 host()->GetHeap()->incremental_marking()->RecordWrite(
142 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
147 Address RelocInfo::target_external_reference() {
148 DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
149 return Memory::Address_at(pc_);
153 Address RelocInfo::target_internal_reference() {
154 DCHECK(rmode_ == INTERNAL_REFERENCE);
155 return Memory::Address_at(pc_);
159 Address RelocInfo::target_internal_reference_address() {
160 DCHECK(rmode_ == INTERNAL_REFERENCE);
161 return reinterpret_cast<Address>(pc_);
165 Address RelocInfo::target_runtime_entry(Assembler* origin) {
166 DCHECK(IsRuntimeEntry(rmode_));
167 return reinterpret_cast<Address>(*reinterpret_cast<int32_t*>(pc_));
171 void RelocInfo::set_target_runtime_entry(Address target,
172 WriteBarrierMode write_barrier_mode,
173 ICacheFlushMode icache_flush_mode) {
174 DCHECK(IsRuntimeEntry(rmode_));
175 if (target_address() != target) {
176 set_target_address(target, write_barrier_mode, icache_flush_mode);
181 Handle<Cell> RelocInfo::target_cell_handle() {
182 DCHECK(rmode_ == RelocInfo::CELL);
183 Address address = Memory::Address_at(pc_);
184 return Handle<Cell>(reinterpret_cast<Cell**>(address));
188 Cell* RelocInfo::target_cell() {
189 DCHECK(rmode_ == RelocInfo::CELL);
190 return Cell::FromValueAddress(Memory::Address_at(pc_));
194 void RelocInfo::set_target_cell(Cell* cell,
195 WriteBarrierMode write_barrier_mode,
196 ICacheFlushMode icache_flush_mode) {
197 DCHECK(cell->IsCell());
198 DCHECK(rmode_ == RelocInfo::CELL);
199 Address address = cell->address() + Cell::kValueOffset;
200 Memory::Address_at(pc_) = address;
201 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
202 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
204 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
205 // TODO(1550) We are passing NULL as a slot because cell can never be on
206 // evacuation candidate.
207 host()->GetHeap()->incremental_marking()->RecordWrite(
213 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
214 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
215 DCHECK(*pc_ == kCallOpcode);
216 return Memory::Object_Handle_at(pc_ + 1);
220 Code* RelocInfo::code_age_stub() {
221 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
222 DCHECK(*pc_ == kCallOpcode);
223 return Code::GetCodeFromTargetAddress(
224 Assembler::target_address_at(pc_ + 1, host_));
228 void RelocInfo::set_code_age_stub(Code* stub,
229 ICacheFlushMode icache_flush_mode) {
230 DCHECK(*pc_ == kCallOpcode);
231 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
232 Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
237 Address RelocInfo::debug_call_address() {
238 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
239 Address location = pc_ + Assembler::kPatchDebugBreakSlotAddressOffset;
240 return Assembler::target_address_at(location, host_);
244 void RelocInfo::set_debug_call_address(Address target) {
245 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
246 Address location = pc_ + Assembler::kPatchDebugBreakSlotAddressOffset;
247 Assembler::set_target_address_at(location, host_, target);
248 if (host() != NULL) {
249 Object* target_code = Code::GetCodeFromTargetAddress(target);
250 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
251 host(), this, HeapObject::cast(target_code));
256 void RelocInfo::WipeOut() {
257 if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_) ||
258 IsInternalReference(rmode_)) {
259 Memory::Address_at(pc_) = NULL;
260 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
261 // Effectively write zero into the relocation.
262 Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
269 bool RelocInfo::IsPatchedReturnSequence() {
270 return *pc_ == kCallOpcode;
274 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
275 return !Assembler::IsNop(pc());
279 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
280 RelocInfo::Mode mode = rmode();
281 if (mode == RelocInfo::EMBEDDED_OBJECT) {
282 visitor->VisitEmbeddedPointer(this);
283 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
284 } else if (RelocInfo::IsCodeTarget(mode)) {
285 visitor->VisitCodeTarget(this);
286 } else if (mode == RelocInfo::CELL) {
287 visitor->VisitCell(this);
288 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
289 visitor->VisitExternalReference(this);
290 } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
291 visitor->VisitInternalReference(this);
292 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
293 visitor->VisitCodeAgeSequence(this);
294 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
295 IsPatchedDebugBreakSlotSequence()) {
296 visitor->VisitDebugTarget(this);
297 } else if (IsRuntimeEntry(mode)) {
298 visitor->VisitRuntimeEntry(this);
303 template<typename StaticVisitor>
304 void RelocInfo::Visit(Heap* heap) {
305 RelocInfo::Mode mode = rmode();
306 if (mode == RelocInfo::EMBEDDED_OBJECT) {
307 StaticVisitor::VisitEmbeddedPointer(heap, this);
308 Assembler::FlushICacheWithoutIsolate(pc_, sizeof(Address));
309 } else if (RelocInfo::IsCodeTarget(mode)) {
310 StaticVisitor::VisitCodeTarget(heap, this);
311 } else if (mode == RelocInfo::CELL) {
312 StaticVisitor::VisitCell(heap, this);
313 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
314 StaticVisitor::VisitExternalReference(this);
315 } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
316 StaticVisitor::VisitInternalReference(this);
317 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
318 StaticVisitor::VisitCodeAgeSequence(heap, this);
319 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
320 IsPatchedDebugBreakSlotSequence()) {
321 StaticVisitor::VisitDebugTarget(heap, this);
322 } else if (IsRuntimeEntry(mode)) {
323 StaticVisitor::VisitRuntimeEntry(this);
329 Immediate::Immediate(int x) {
331 rmode_ = RelocInfo::NONE32;
335 Immediate::Immediate(const ExternalReference& ext) {
336 x_ = reinterpret_cast<int32_t>(ext.address());
337 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
341 Immediate::Immediate(Label* internal_offset) {
342 x_ = reinterpret_cast<int32_t>(internal_offset);
343 rmode_ = RelocInfo::INTERNAL_REFERENCE;
347 Immediate::Immediate(Handle<Object> handle) {
348 AllowDeferredHandleDereference using_raw_address;
349 // Verify all Objects referred by code are NOT in new space.
350 Object* obj = *handle;
351 if (obj->IsHeapObject()) {
352 DCHECK(!HeapObject::cast(obj)->GetHeap()->InNewSpace(obj));
353 x_ = reinterpret_cast<intptr_t>(handle.location());
354 rmode_ = RelocInfo::EMBEDDED_OBJECT;
356 // no relocation needed
357 x_ = reinterpret_cast<intptr_t>(obj);
358 rmode_ = RelocInfo::NONE32;
363 Immediate::Immediate(Smi* value) {
364 x_ = reinterpret_cast<intptr_t>(value);
365 rmode_ = RelocInfo::NONE32;
369 Immediate::Immediate(Address addr) {
370 x_ = reinterpret_cast<int32_t>(addr);
371 rmode_ = RelocInfo::NONE32;
375 void Assembler::emit(uint32_t x) {
376 *reinterpret_cast<uint32_t*>(pc_) = x;
377 pc_ += sizeof(uint32_t);
381 void Assembler::emit_q(uint64_t x) {
382 *reinterpret_cast<uint64_t*>(pc_) = x;
383 pc_ += sizeof(uint64_t);
387 void Assembler::emit(Handle<Object> handle) {
388 AllowDeferredHandleDereference heap_object_check;
389 // Verify all Objects referred by code are NOT in new space.
390 Object* obj = *handle;
391 DCHECK(!isolate()->heap()->InNewSpace(obj));
392 if (obj->IsHeapObject()) {
393 emit(reinterpret_cast<intptr_t>(handle.location()),
394 RelocInfo::EMBEDDED_OBJECT);
396 // no relocation needed
397 emit(reinterpret_cast<intptr_t>(obj));
402 void Assembler::emit(uint32_t x, RelocInfo::Mode rmode, TypeFeedbackId id) {
403 if (rmode == RelocInfo::CODE_TARGET && !id.IsNone()) {
404 RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, id.ToInt());
405 } else if (!RelocInfo::IsNone(rmode)
406 && rmode != RelocInfo::CODE_AGE_SEQUENCE) {
407 RecordRelocInfo(rmode);
413 void Assembler::emit(Handle<Code> code,
414 RelocInfo::Mode rmode,
416 AllowDeferredHandleDereference embedding_raw_address;
417 emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
421 void Assembler::emit(const Immediate& x) {
422 if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
423 Label* label = reinterpret_cast<Label*>(x.x_);
424 emit_code_relative_offset(label);
427 if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
432 void Assembler::emit_code_relative_offset(Label* label) {
433 if (label->is_bound()) {
435 pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
438 emit_disp(label, Displacement::CODE_RELATIVE);
443 void Assembler::emit_w(const Immediate& x) {
444 DCHECK(RelocInfo::IsNone(x.rmode_));
445 uint16_t value = static_cast<uint16_t>(x.x_);
446 reinterpret_cast<uint16_t*>(pc_)[0] = value;
447 pc_ += sizeof(uint16_t);
451 Address Assembler::target_address_at(Address pc, Address constant_pool) {
452 return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
456 void Assembler::set_target_address_at(Address pc, Address constant_pool,
458 ICacheFlushMode icache_flush_mode) {
459 int32_t* p = reinterpret_cast<int32_t*>(pc);
460 *p = target - (pc + sizeof(int32_t));
461 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
462 Assembler::FlushICacheWithoutIsolate(p, sizeof(int32_t));
467 Address Assembler::target_address_from_return_address(Address pc) {
468 return pc - kCallTargetAddressOffset;
472 Displacement Assembler::disp_at(Label* L) {
473 return Displacement(long_at(L->pos()));
477 void Assembler::disp_at_put(Label* L, Displacement disp) {
478 long_at_put(L->pos(), disp.data());
482 void Assembler::emit_disp(Label* L, Displacement::Type type) {
483 Displacement disp(L, type);
484 L->link_to(pc_offset());
485 emit(static_cast<int>(disp.data()));
489 void Assembler::emit_near_disp(Label* L) {
491 if (L->is_near_linked()) {
492 int offset = L->near_link_pos() - pc_offset();
493 DCHECK(is_int8(offset));
494 disp = static_cast<byte>(offset & 0xFF);
496 L->link_to(pc_offset(), Label::kNear);
501 void Assembler::deserialization_set_target_internal_reference_at(
502 Address pc, Address target, RelocInfo::Mode mode) {
503 Memory::Address_at(pc) = target;
507 void Operand::set_modrm(int mod, Register rm) {
508 DCHECK((mod & -4) == 0);
509 buf_[0] = mod << 6 | rm.code();
514 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
516 DCHECK((scale & -4) == 0);
517 // Use SIB with no index register only for base esp.
518 DCHECK(!index.is(esp) || base.is(esp));
519 buf_[1] = scale << 6 | index.code() << 3 | base.code();
524 void Operand::set_disp8(int8_t disp) {
525 DCHECK(len_ == 1 || len_ == 2);
526 *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
530 void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) {
531 DCHECK(len_ == 1 || len_ == 2);
532 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
534 len_ += sizeof(int32_t);
538 Operand::Operand(Register reg) {
544 Operand::Operand(XMMRegister xmm_reg) {
545 Register reg = { xmm_reg.code() };
550 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
553 set_dispr(disp, rmode);
557 Operand::Operand(Immediate imm) {
560 set_dispr(imm.x_, imm.rmode_);
562 } // namespace internal
565 #endif // V8_IA32_ASSEMBLER_IA32_INL_H_