45430470802d491a93424cbaad27491c7a402b63
[platform/upstream/v8.git] / src / x87 / assembler-x87-inl.h
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
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.
14 //
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.
18 //
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.
30
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.
34
35 // A light-weight IA32 Assembler.
36
37 #ifndef V8_X87_ASSEMBLER_X87_INL_H_
38 #define V8_X87_ASSEMBLER_X87_INL_H_
39
40 #include "src/x87/assembler-x87.h"
41
42 #include "src/assembler.h"
43 #include "src/debug/debug.h"
44
45 namespace v8 {
46 namespace internal {
47
48 bool CpuFeatures::SupportsCrankshaft() { return true; }
49
50
51 static const byte kCallOpcode = 0xE8;
52 static const int kNoCodeAgeSequenceLength = 5;
53
54
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.
64     }
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.
75   }
76 }
77
78
79 Address RelocInfo::target_address() {
80   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
81   return Assembler::target_address_at(pc_, host_);
82 }
83
84
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_);
90 }
91
92
93 Address RelocInfo::constant_pool_entry_address() {
94   UNREACHABLE();
95   return NULL;
96 }
97
98
99 int RelocInfo::target_address_size() {
100   return Assembler::kSpecialTargetSize;
101 }
102
103
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));
115   }
116 }
117
118
119 Object* RelocInfo::target_object() {
120   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
121   return Memory::Object_at(pc_);
122 }
123
124
125 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
126   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
127   return Memory::Object_Handle_at(pc_);
128 }
129
130
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));
138   }
139   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
140       host() != NULL &&
141       target->IsHeapObject()) {
142     host()->GetHeap()->incremental_marking()->RecordWrite(
143         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
144   }
145 }
146
147
148 Address RelocInfo::target_external_reference() {
149   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
150   return Memory::Address_at(pc_);
151 }
152
153
154 Address RelocInfo::target_internal_reference() {
155   DCHECK(rmode_ == INTERNAL_REFERENCE);
156   return Memory::Address_at(pc_);
157 }
158
159
160 Address RelocInfo::target_internal_reference_address() {
161   DCHECK(rmode_ == INTERNAL_REFERENCE);
162   return reinterpret_cast<Address>(pc_);
163 }
164
165
166 Address RelocInfo::target_runtime_entry(Assembler* origin) {
167   DCHECK(IsRuntimeEntry(rmode_));
168   return reinterpret_cast<Address>(*reinterpret_cast<int32_t*>(pc_));
169 }
170
171
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);
178   }
179 }
180
181
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));
186 }
187
188
189 Cell* RelocInfo::target_cell() {
190   DCHECK(rmode_ == RelocInfo::CELL);
191   return Cell::FromValueAddress(Memory::Address_at(pc_));
192 }
193
194
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));
204   }
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(
209         host(), NULL, cell);
210   }
211 }
212
213
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);
218 }
219
220
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_));
226 }
227
228
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(),
234                                    icache_flush_mode);
235 }
236
237
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_);
242 }
243
244
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));
253   }
254 }
255
256
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));
264   } else {
265     UNREACHABLE();
266   }
267 }
268
269
270 bool RelocInfo::IsPatchedReturnSequence() {
271   return *pc_ == kCallOpcode;
272 }
273
274
275 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
276   return !Assembler::IsNop(pc());
277 }
278
279
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);
300   }
301 }
302
303
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);
325   }
326 }
327
328
329
330 Immediate::Immediate(int x)  {
331   x_ = x;
332   rmode_ = RelocInfo::NONE32;
333 }
334
335
336 Immediate::Immediate(const ExternalReference& ext) {
337   x_ = reinterpret_cast<int32_t>(ext.address());
338   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
339 }
340
341
342 Immediate::Immediate(Label* internal_offset) {
343   x_ = reinterpret_cast<int32_t>(internal_offset);
344   rmode_ = RelocInfo::INTERNAL_REFERENCE;
345 }
346
347
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;
356   } else {
357     // no relocation needed
358     x_ =  reinterpret_cast<intptr_t>(obj);
359     rmode_ = RelocInfo::NONE32;
360   }
361 }
362
363
364 Immediate::Immediate(Smi* value) {
365   x_ = reinterpret_cast<intptr_t>(value);
366   rmode_ = RelocInfo::NONE32;
367 }
368
369
370 Immediate::Immediate(Address addr) {
371   x_ = reinterpret_cast<int32_t>(addr);
372   rmode_ = RelocInfo::NONE32;
373 }
374
375
376 void Assembler::emit(uint32_t x) {
377   *reinterpret_cast<uint32_t*>(pc_) = x;
378   pc_ += sizeof(uint32_t);
379 }
380
381
382 void Assembler::emit_q(uint64_t x) {
383   *reinterpret_cast<uint64_t*>(pc_) = x;
384   pc_ += sizeof(uint64_t);
385 }
386
387
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);
396   } else {
397     // no relocation needed
398     emit(reinterpret_cast<intptr_t>(obj));
399   }
400 }
401
402
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);
409   }
410   emit(x);
411 }
412
413
414 void Assembler::emit(Handle<Code> code,
415                      RelocInfo::Mode rmode,
416                      TypeFeedbackId id) {
417   AllowDeferredHandleDereference embedding_raw_address;
418   emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
419 }
420
421
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);
426     return;
427   }
428   if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
429   emit(x.x_);
430 }
431
432
433 void Assembler::emit_code_relative_offset(Label* label) {
434   if (label->is_bound()) {
435     int32_t pos;
436     pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
437     emit(pos);
438   } else {
439     emit_disp(label, Displacement::CODE_RELATIVE);
440   }
441 }
442
443
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);
449 }
450
451
452 Address Assembler::target_address_at(Address pc, Address constant_pool) {
453   return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
454 }
455
456
457 void Assembler::set_target_address_at(Address pc, Address constant_pool,
458                                       Address target,
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));
464   }
465 }
466
467
468 Address Assembler::target_address_from_return_address(Address pc) {
469   return pc - kCallTargetAddressOffset;
470 }
471
472
473 Displacement Assembler::disp_at(Label* L) {
474   return Displacement(long_at(L->pos()));
475 }
476
477
478 void Assembler::disp_at_put(Label* L, Displacement disp) {
479   long_at_put(L->pos(), disp.data());
480 }
481
482
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()));
487 }
488
489
490 void Assembler::emit_near_disp(Label* L) {
491   byte disp = 0x00;
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);
496   }
497   L->link_to(pc_offset(), Label::kNear);
498   *pc_++ = disp;
499 }
500
501
502 void Assembler::deserialization_set_target_internal_reference_at(
503     Address pc, Address target, RelocInfo::Mode mode) {
504   Memory::Address_at(pc) = target;
505 }
506
507
508 void Operand::set_modrm(int mod, Register rm) {
509   DCHECK((mod & -4) == 0);
510   buf_[0] = mod << 6 | rm.code();
511   len_ = 1;
512 }
513
514
515 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
516   DCHECK(len_ == 1);
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();
521   len_ = 2;
522 }
523
524
525 void Operand::set_disp8(int8_t disp) {
526   DCHECK(len_ == 1 || len_ == 2);
527   *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
528 }
529
530
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_]);
534   *p = disp;
535   len_ += sizeof(int32_t);
536   rmode_ = rmode;
537 }
538
539 Operand::Operand(Register reg) {
540   // reg
541   set_modrm(3, reg);
542 }
543
544
545 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
546   // [disp/r]
547   set_modrm(0, ebp);
548   set_dispr(disp, rmode);
549 }
550
551
552 Operand::Operand(Immediate imm) {
553   // [disp/r]
554   set_modrm(0, ebp);
555   set_dispr(imm.x_, imm.rmode_);
556 }
557 } }  // namespace v8::internal
558
559 #endif  // V8_X87_ASSEMBLER_X87_INL_H_