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"
43 #include "src/debug.h"
48 bool CpuFeatures::SupportsCrankshaft() { return true; }
49 bool CpuFeatures::SupportsSIMD128InCrankshaft() { return true; }
52 static const byte kCallOpcode = 0xE8;
53 static const int kNoCodeAgeSequenceLength = 5;
56 // The modes possibly affected by apply must be in kApplyMask.
57 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
58 bool flush_icache = icache_flush_mode != SKIP_ICACHE_FLUSH;
59 if (IsRuntimeEntry(rmode_) || IsCodeTarget(rmode_)) {
60 int32_t* p = reinterpret_cast<int32_t*>(pc_);
61 *p -= delta; // Relocate entry.
62 if (flush_icache) CPU::FlushICache(p, sizeof(uint32_t));
63 } else if (rmode_ == CODE_AGE_SEQUENCE) {
64 if (*pc_ == kCallOpcode) {
65 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
66 *p -= delta; // Relocate entry.
67 if (flush_icache) CPU::FlushICache(p, sizeof(uint32_t));
69 } else if (rmode_ == JS_RETURN && IsPatchedReturnSequence()) {
70 // Special handling of js_return when a break point is set (call
71 // instruction has been inserted).
72 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
73 *p -= delta; // Relocate entry.
74 if (flush_icache) CPU::FlushICache(p, sizeof(uint32_t));
75 } else if (rmode_ == DEBUG_BREAK_SLOT && IsPatchedDebugBreakSlotSequence()) {
76 // Special handling of a debug break slot when a break point is set (call
77 // instruction has been inserted).
78 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
79 *p -= delta; // Relocate entry.
80 if (flush_icache) CPU::FlushICache(p, sizeof(uint32_t));
81 } else if (IsInternalReference(rmode_)) {
82 // absolute code pointer inside code object moves with the code object.
83 int32_t* p = reinterpret_cast<int32_t*>(pc_);
84 *p += delta; // Relocate entry.
85 if (flush_icache) CPU::FlushICache(p, sizeof(uint32_t));
90 Address RelocInfo::target_address() {
91 ASSERT(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
92 return Assembler::target_address_at(pc_, host_);
96 Address RelocInfo::target_address_address() {
97 ASSERT(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
98 || rmode_ == EMBEDDED_OBJECT
99 || rmode_ == EXTERNAL_REFERENCE);
100 return reinterpret_cast<Address>(pc_);
104 Address RelocInfo::constant_pool_entry_address() {
110 int RelocInfo::target_address_size() {
111 return Assembler::kSpecialTargetSize;
115 void RelocInfo::set_target_address(Address target,
116 WriteBarrierMode write_barrier_mode,
117 ICacheFlushMode icache_flush_mode) {
118 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
119 ASSERT(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
120 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
121 IsCodeTarget(rmode_)) {
122 Object* target_code = Code::GetCodeFromTargetAddress(target);
123 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
124 host(), this, HeapObject::cast(target_code));
129 Object* RelocInfo::target_object() {
130 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
131 return Memory::Object_at(pc_);
135 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
136 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
137 return Memory::Object_Handle_at(pc_);
141 void RelocInfo::set_target_object(Object* target,
142 WriteBarrierMode write_barrier_mode,
143 ICacheFlushMode icache_flush_mode) {
144 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
145 ASSERT(!target->IsConsString());
146 Memory::Object_at(pc_) = target;
147 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
148 CPU::FlushICache(pc_, sizeof(Address));
150 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
152 target->IsHeapObject()) {
153 host()->GetHeap()->incremental_marking()->RecordWrite(
154 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
159 Address RelocInfo::target_reference() {
160 ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
161 return Memory::Address_at(pc_);
165 Address RelocInfo::target_runtime_entry(Assembler* origin) {
166 ASSERT(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 ASSERT(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 ASSERT(rmode_ == RelocInfo::CELL);
183 Address address = Memory::Address_at(pc_);
184 return Handle<Cell>(reinterpret_cast<Cell**>(address));
188 Cell* RelocInfo::target_cell() {
189 ASSERT(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 ASSERT(rmode_ == RelocInfo::CELL);
198 Address address = cell->address() + Cell::kValueOffset;
199 Memory::Address_at(pc_) = address;
200 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
201 CPU::FlushICache(pc_, sizeof(Address));
203 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
204 // TODO(1550) We are passing NULL as a slot because cell can never be on
205 // evacuation candidate.
206 host()->GetHeap()->incremental_marking()->RecordWrite(
212 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
213 ASSERT(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
214 ASSERT(*pc_ == kCallOpcode);
215 return Memory::Object_Handle_at(pc_ + 1);
219 Code* RelocInfo::code_age_stub() {
220 ASSERT(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
221 ASSERT(*pc_ == kCallOpcode);
222 return Code::GetCodeFromTargetAddress(
223 Assembler::target_address_at(pc_ + 1, host_));
227 void RelocInfo::set_code_age_stub(Code* stub,
228 ICacheFlushMode icache_flush_mode) {
229 ASSERT(*pc_ == kCallOpcode);
230 ASSERT(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
231 Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
236 Address RelocInfo::call_address() {
237 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
238 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
239 return Assembler::target_address_at(pc_ + 1, host_);
243 void RelocInfo::set_call_address(Address target) {
244 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
245 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
246 Assembler::set_target_address_at(pc_ + 1, host_, target);
247 if (host() != NULL) {
248 Object* target_code = Code::GetCodeFromTargetAddress(target);
249 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
250 host(), this, HeapObject::cast(target_code));
255 Object* RelocInfo::call_object() {
256 return *call_object_address();
260 void RelocInfo::set_call_object(Object* target) {
261 *call_object_address() = target;
265 Object** RelocInfo::call_object_address() {
266 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
267 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
268 return reinterpret_cast<Object**>(pc_ + 1);
272 void RelocInfo::WipeOut() {
273 if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_)) {
274 Memory::Address_at(pc_) = NULL;
275 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
276 // Effectively write zero into the relocation.
277 Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
284 bool RelocInfo::IsPatchedReturnSequence() {
285 return *pc_ == kCallOpcode;
289 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
290 return !Assembler::IsNop(pc());
294 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
295 RelocInfo::Mode mode = rmode();
296 if (mode == RelocInfo::EMBEDDED_OBJECT) {
297 visitor->VisitEmbeddedPointer(this);
298 CPU::FlushICache(pc_, sizeof(Address));
299 } else if (RelocInfo::IsCodeTarget(mode)) {
300 visitor->VisitCodeTarget(this);
301 } else if (mode == RelocInfo::CELL) {
302 visitor->VisitCell(this);
303 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
304 visitor->VisitExternalReference(this);
305 CPU::FlushICache(pc_, sizeof(Address));
306 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
307 visitor->VisitCodeAgeSequence(this);
308 } else if (((RelocInfo::IsJSReturn(mode) &&
309 IsPatchedReturnSequence()) ||
310 (RelocInfo::IsDebugBreakSlot(mode) &&
311 IsPatchedDebugBreakSlotSequence())) &&
312 isolate->debug()->has_break_points()) {
313 visitor->VisitDebugTarget(this);
314 } else if (IsRuntimeEntry(mode)) {
315 visitor->VisitRuntimeEntry(this);
320 template<typename StaticVisitor>
321 void RelocInfo::Visit(Heap* heap) {
322 RelocInfo::Mode mode = rmode();
323 if (mode == RelocInfo::EMBEDDED_OBJECT) {
324 StaticVisitor::VisitEmbeddedPointer(heap, this);
325 CPU::FlushICache(pc_, sizeof(Address));
326 } else if (RelocInfo::IsCodeTarget(mode)) {
327 StaticVisitor::VisitCodeTarget(heap, this);
328 } else if (mode == RelocInfo::CELL) {
329 StaticVisitor::VisitCell(heap, this);
330 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
331 StaticVisitor::VisitExternalReference(this);
332 CPU::FlushICache(pc_, sizeof(Address));
333 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
334 StaticVisitor::VisitCodeAgeSequence(heap, this);
335 } else if (heap->isolate()->debug()->has_break_points() &&
336 ((RelocInfo::IsJSReturn(mode) &&
337 IsPatchedReturnSequence()) ||
338 (RelocInfo::IsDebugBreakSlot(mode) &&
339 IsPatchedDebugBreakSlotSequence()))) {
340 StaticVisitor::VisitDebugTarget(heap, this);
341 } else if (IsRuntimeEntry(mode)) {
342 StaticVisitor::VisitRuntimeEntry(this);
348 Immediate::Immediate(int x) {
350 rmode_ = RelocInfo::NONE32;
354 Immediate::Immediate(const ExternalReference& ext) {
355 x_ = reinterpret_cast<int32_t>(ext.address());
356 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
360 Immediate::Immediate(Label* internal_offset) {
361 x_ = reinterpret_cast<int32_t>(internal_offset);
362 rmode_ = RelocInfo::INTERNAL_REFERENCE;
366 Immediate::Immediate(Handle<Object> handle) {
367 AllowDeferredHandleDereference using_raw_address;
368 // Verify all Objects referred by code are NOT in new space.
369 Object* obj = *handle;
370 if (obj->IsHeapObject()) {
371 ASSERT(!HeapObject::cast(obj)->GetHeap()->InNewSpace(obj));
372 x_ = reinterpret_cast<intptr_t>(handle.location());
373 rmode_ = RelocInfo::EMBEDDED_OBJECT;
375 // no relocation needed
376 x_ = reinterpret_cast<intptr_t>(obj);
377 rmode_ = RelocInfo::NONE32;
382 Immediate::Immediate(Smi* value) {
383 x_ = reinterpret_cast<intptr_t>(value);
384 rmode_ = RelocInfo::NONE32;
388 Immediate::Immediate(Address addr) {
389 x_ = reinterpret_cast<int32_t>(addr);
390 rmode_ = RelocInfo::NONE32;
394 void Assembler::emit(uint32_t x) {
395 *reinterpret_cast<uint32_t*>(pc_) = x;
396 pc_ += sizeof(uint32_t);
400 void Assembler::emit(Handle<Object> handle) {
401 AllowDeferredHandleDereference heap_object_check;
402 // Verify all Objects referred by code are NOT in new space.
403 Object* obj = *handle;
404 ASSERT(!isolate()->heap()->InNewSpace(obj));
405 if (obj->IsHeapObject()) {
406 emit(reinterpret_cast<intptr_t>(handle.location()),
407 RelocInfo::EMBEDDED_OBJECT);
409 // no relocation needed
410 emit(reinterpret_cast<intptr_t>(obj));
415 void Assembler::emit(uint32_t x, RelocInfo::Mode rmode, TypeFeedbackId id) {
416 if (rmode == RelocInfo::CODE_TARGET && !id.IsNone()) {
417 RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, id.ToInt());
418 } else if (!RelocInfo::IsNone(rmode)
419 && rmode != RelocInfo::CODE_AGE_SEQUENCE) {
420 RecordRelocInfo(rmode);
426 void Assembler::emit(Handle<Code> code,
427 RelocInfo::Mode rmode,
429 AllowDeferredHandleDereference embedding_raw_address;
430 emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
434 void Assembler::emit(const Immediate& x) {
435 if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
436 Label* label = reinterpret_cast<Label*>(x.x_);
437 emit_code_relative_offset(label);
440 if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
445 void Assembler::emit_code_relative_offset(Label* label) {
446 if (label->is_bound()) {
448 pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
451 emit_disp(label, Displacement::CODE_RELATIVE);
456 void Assembler::emit_w(const Immediate& x) {
457 ASSERT(RelocInfo::IsNone(x.rmode_));
458 uint16_t value = static_cast<uint16_t>(x.x_);
459 reinterpret_cast<uint16_t*>(pc_)[0] = value;
460 pc_ += sizeof(uint16_t);
464 Address Assembler::target_address_at(Address pc,
465 ConstantPoolArray* constant_pool) {
466 return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
470 void Assembler::set_target_address_at(Address pc,
471 ConstantPoolArray* constant_pool,
473 ICacheFlushMode icache_flush_mode) {
474 int32_t* p = reinterpret_cast<int32_t*>(pc);
475 *p = target - (pc + sizeof(int32_t));
476 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
477 CPU::FlushICache(p, sizeof(int32_t));
482 Address Assembler::target_address_from_return_address(Address pc) {
483 return pc - kCallTargetAddressOffset;
487 Displacement Assembler::disp_at(Label* L) {
488 return Displacement(long_at(L->pos()));
492 void Assembler::disp_at_put(Label* L, Displacement disp) {
493 long_at_put(L->pos(), disp.data());
497 void Assembler::emit_disp(Label* L, Displacement::Type type) {
498 Displacement disp(L, type);
499 L->link_to(pc_offset());
500 emit(static_cast<int>(disp.data()));
504 void Assembler::emit_near_disp(Label* L) {
506 if (L->is_near_linked()) {
507 int offset = L->near_link_pos() - pc_offset();
508 ASSERT(is_int8(offset));
509 disp = static_cast<byte>(offset & 0xFF);
511 L->link_to(pc_offset(), Label::kNear);
516 void Operand::set_modrm(int mod, Register rm) {
517 ASSERT((mod & -4) == 0);
518 buf_[0] = mod << 6 | rm.code();
523 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
525 ASSERT((scale & -4) == 0);
526 // Use SIB with no index register only for base esp.
527 ASSERT(!index.is(esp) || base.is(esp));
528 buf_[1] = scale << 6 | index.code() << 3 | base.code();
533 void Operand::set_disp8(int8_t disp) {
534 ASSERT(len_ == 1 || len_ == 2);
535 *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
539 void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) {
540 ASSERT(len_ == 1 || len_ == 2);
541 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
543 len_ += sizeof(int32_t);
547 Operand::Operand(Register reg) {
553 Operand::Operand(XMMRegister xmm_reg) {
554 Register reg = { xmm_reg.code() };
559 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
562 set_dispr(disp, rmode);
565 } } // namespace v8::internal
567 #endif // V8_IA32_ASSEMBLER_IA32_INL_H_