deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / 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.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, ICacheFlushMode icache_flush_mode) {
57   bool flush_icache = icache_flush_mode != SKIP_ICACHE_FLUSH;
58   if (IsRuntimeEntry(rmode_) || IsCodeTarget(rmode_)) {
59     int32_t* p = reinterpret_cast<int32_t*>(pc_);
60     *p -= delta;  // Relocate entry.
61     if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
62   } else if (rmode_ == CODE_AGE_SEQUENCE) {
63     if (*pc_ == kCallOpcode) {
64       int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
65       *p -= delta;  // Relocate entry.
66       if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
67     }
68   } else if (rmode_ == JS_RETURN && IsPatchedReturnSequence()) {
69     // Special handling of js_return when a break point is set (call
70     // instruction has been inserted).
71     int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
72     *p -= delta;  // Relocate entry.
73     if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
74   } else if (rmode_ == DEBUG_BREAK_SLOT && IsPatchedDebugBreakSlotSequence()) {
75     // Special handling of a debug break slot when a break point is set (call
76     // instruction has been inserted).
77     int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
78     *p -= delta;  // Relocate entry.
79     if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
80   } else if (IsInternalReference(rmode_)) {
81     // absolute code pointer inside code object moves with the code object.
82     int32_t* p = reinterpret_cast<int32_t*>(pc_);
83     *p += delta;  // Relocate entry.
84     if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
85   }
86 }
87
88
89 Address RelocInfo::target_address() {
90   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
91   return Assembler::target_address_at(pc_, host_);
92 }
93
94
95 Address RelocInfo::target_address_address() {
96   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
97                               || rmode_ == EMBEDDED_OBJECT
98                               || rmode_ == EXTERNAL_REFERENCE);
99   return reinterpret_cast<Address>(pc_);
100 }
101
102
103 Address RelocInfo::constant_pool_entry_address() {
104   UNREACHABLE();
105   return NULL;
106 }
107
108
109 int RelocInfo::target_address_size() {
110   return Assembler::kSpecialTargetSize;
111 }
112
113
114 void RelocInfo::set_target_address(Address target,
115                                    WriteBarrierMode write_barrier_mode,
116                                    ICacheFlushMode icache_flush_mode) {
117   Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
118   Assembler::set_target_address_at(pc_, host_, target);
119   DCHECK(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));
125   }
126 }
127
128
129 Object* RelocInfo::target_object() {
130   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
131   return Memory::Object_at(pc_);
132 }
133
134
135 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
136   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
137   return Memory::Object_Handle_at(pc_);
138 }
139
140
141 void RelocInfo::set_target_object(Object* target,
142                                   WriteBarrierMode write_barrier_mode,
143                                   ICacheFlushMode icache_flush_mode) {
144   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
145   Memory::Object_at(pc_) = target;
146   if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
147     CpuFeatures::FlushICache(pc_, sizeof(Address));
148   }
149   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
150       host() != NULL &&
151       target->IsHeapObject()) {
152     host()->GetHeap()->incremental_marking()->RecordWrite(
153         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
154   }
155 }
156
157
158 Address RelocInfo::target_external_reference() {
159   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
160   return Memory::Address_at(pc_);
161 }
162
163
164 Address RelocInfo::target_internal_reference() {
165   DCHECK(rmode_ == INTERNAL_REFERENCE);
166   return Memory::Address_at(pc_);
167 }
168
169
170 Address RelocInfo::target_internal_reference_address() {
171   DCHECK(rmode_ == INTERNAL_REFERENCE);
172   return reinterpret_cast<Address>(pc_);
173 }
174
175
176 Address RelocInfo::target_runtime_entry(Assembler* origin) {
177   DCHECK(IsRuntimeEntry(rmode_));
178   return reinterpret_cast<Address>(*reinterpret_cast<int32_t*>(pc_));
179 }
180
181
182 void RelocInfo::set_target_runtime_entry(Address target,
183                                          WriteBarrierMode write_barrier_mode,
184                                          ICacheFlushMode icache_flush_mode) {
185   DCHECK(IsRuntimeEntry(rmode_));
186   if (target_address() != target) {
187     set_target_address(target, write_barrier_mode, icache_flush_mode);
188   }
189 }
190
191
192 Handle<Cell> RelocInfo::target_cell_handle() {
193   DCHECK(rmode_ == RelocInfo::CELL);
194   Address address = Memory::Address_at(pc_);
195   return Handle<Cell>(reinterpret_cast<Cell**>(address));
196 }
197
198
199 Cell* RelocInfo::target_cell() {
200   DCHECK(rmode_ == RelocInfo::CELL);
201   return Cell::FromValueAddress(Memory::Address_at(pc_));
202 }
203
204
205 void RelocInfo::set_target_cell(Cell* cell,
206                                 WriteBarrierMode write_barrier_mode,
207                                 ICacheFlushMode icache_flush_mode) {
208   DCHECK(rmode_ == RelocInfo::CELL);
209   Address address = cell->address() + Cell::kValueOffset;
210   Memory::Address_at(pc_) = address;
211   if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
212     CpuFeatures::FlushICache(pc_, sizeof(Address));
213   }
214   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
215     // TODO(1550) We are passing NULL as a slot because cell can never be on
216     // evacuation candidate.
217     host()->GetHeap()->incremental_marking()->RecordWrite(
218         host(), NULL, cell);
219   }
220 }
221
222
223 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
224   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
225   DCHECK(*pc_ == kCallOpcode);
226   return Memory::Object_Handle_at(pc_ + 1);
227 }
228
229
230 Code* RelocInfo::code_age_stub() {
231   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
232   DCHECK(*pc_ == kCallOpcode);
233   return Code::GetCodeFromTargetAddress(
234       Assembler::target_address_at(pc_ + 1, host_));
235 }
236
237
238 void RelocInfo::set_code_age_stub(Code* stub,
239                                   ICacheFlushMode icache_flush_mode) {
240   DCHECK(*pc_ == kCallOpcode);
241   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
242   Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
243                                    icache_flush_mode);
244 }
245
246
247 Address RelocInfo::call_address() {
248   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
249          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
250   return Assembler::target_address_at(pc_ + 1, host_);
251 }
252
253
254 void RelocInfo::set_call_address(Address target) {
255   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
256          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
257   Assembler::set_target_address_at(pc_ + 1, host_, target);
258   if (host() != NULL) {
259     Object* target_code = Code::GetCodeFromTargetAddress(target);
260     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
261         host(), this, HeapObject::cast(target_code));
262   }
263 }
264
265
266 Object* RelocInfo::call_object() {
267   return *call_object_address();
268 }
269
270
271 void RelocInfo::set_call_object(Object* target) {
272   *call_object_address() = target;
273 }
274
275
276 Object** RelocInfo::call_object_address() {
277   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
278          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
279   return reinterpret_cast<Object**>(pc_ + 1);
280 }
281
282
283 void RelocInfo::WipeOut() {
284   if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_) ||
285       IsInternalReference(rmode_)) {
286     Memory::Address_at(pc_) = NULL;
287   } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
288     // Effectively write zero into the relocation.
289     Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
290   } else {
291     UNREACHABLE();
292   }
293 }
294
295
296 bool RelocInfo::IsPatchedReturnSequence() {
297   return *pc_ == kCallOpcode;
298 }
299
300
301 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
302   return !Assembler::IsNop(pc());
303 }
304
305
306 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
307   RelocInfo::Mode mode = rmode();
308   if (mode == RelocInfo::EMBEDDED_OBJECT) {
309     visitor->VisitEmbeddedPointer(this);
310     CpuFeatures::FlushICache(pc_, sizeof(Address));
311   } else if (RelocInfo::IsCodeTarget(mode)) {
312     visitor->VisitCodeTarget(this);
313   } else if (mode == RelocInfo::CELL) {
314     visitor->VisitCell(this);
315   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
316     visitor->VisitExternalReference(this);
317   } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
318     visitor->VisitInternalReference(this);
319   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
320     visitor->VisitCodeAgeSequence(this);
321   } else if (((RelocInfo::IsJSReturn(mode) &&
322               IsPatchedReturnSequence()) ||
323              (RelocInfo::IsDebugBreakSlot(mode) &&
324               IsPatchedDebugBreakSlotSequence())) &&
325              isolate->debug()->has_break_points()) {
326     visitor->VisitDebugTarget(this);
327   } else if (IsRuntimeEntry(mode)) {
328     visitor->VisitRuntimeEntry(this);
329   }
330 }
331
332
333 template<typename StaticVisitor>
334 void RelocInfo::Visit(Heap* heap) {
335   RelocInfo::Mode mode = rmode();
336   if (mode == RelocInfo::EMBEDDED_OBJECT) {
337     StaticVisitor::VisitEmbeddedPointer(heap, this);
338     CpuFeatures::FlushICache(pc_, sizeof(Address));
339   } else if (RelocInfo::IsCodeTarget(mode)) {
340     StaticVisitor::VisitCodeTarget(heap, this);
341   } else if (mode == RelocInfo::CELL) {
342     StaticVisitor::VisitCell(heap, this);
343   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
344     StaticVisitor::VisitExternalReference(this);
345   } else if (mode == RelocInfo::INTERNAL_REFERENCE) {
346     StaticVisitor::VisitInternalReference(this);
347   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
348     StaticVisitor::VisitCodeAgeSequence(heap, this);
349   } else if (heap->isolate()->debug()->has_break_points() &&
350              ((RelocInfo::IsJSReturn(mode) &&
351               IsPatchedReturnSequence()) ||
352              (RelocInfo::IsDebugBreakSlot(mode) &&
353               IsPatchedDebugBreakSlotSequence()))) {
354     StaticVisitor::VisitDebugTarget(heap, this);
355   } else if (IsRuntimeEntry(mode)) {
356     StaticVisitor::VisitRuntimeEntry(this);
357   }
358 }
359
360
361
362 Immediate::Immediate(int x)  {
363   x_ = x;
364   rmode_ = RelocInfo::NONE32;
365 }
366
367
368 Immediate::Immediate(const ExternalReference& ext) {
369   x_ = reinterpret_cast<int32_t>(ext.address());
370   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
371 }
372
373
374 Immediate::Immediate(Label* internal_offset) {
375   x_ = reinterpret_cast<int32_t>(internal_offset);
376   rmode_ = RelocInfo::INTERNAL_REFERENCE;
377 }
378
379
380 Immediate::Immediate(Handle<Object> handle) {
381   AllowDeferredHandleDereference using_raw_address;
382   // Verify all Objects referred by code are NOT in new space.
383   Object* obj = *handle;
384   if (obj->IsHeapObject()) {
385     DCHECK(!HeapObject::cast(obj)->GetHeap()->InNewSpace(obj));
386     x_ = reinterpret_cast<intptr_t>(handle.location());
387     rmode_ = RelocInfo::EMBEDDED_OBJECT;
388   } else {
389     // no relocation needed
390     x_ =  reinterpret_cast<intptr_t>(obj);
391     rmode_ = RelocInfo::NONE32;
392   }
393 }
394
395
396 Immediate::Immediate(Smi* value) {
397   x_ = reinterpret_cast<intptr_t>(value);
398   rmode_ = RelocInfo::NONE32;
399 }
400
401
402 Immediate::Immediate(Address addr) {
403   x_ = reinterpret_cast<int32_t>(addr);
404   rmode_ = RelocInfo::NONE32;
405 }
406
407
408 void Assembler::emit(uint32_t x) {
409   *reinterpret_cast<uint32_t*>(pc_) = x;
410   pc_ += sizeof(uint32_t);
411 }
412
413
414 void Assembler::emit(Handle<Object> handle) {
415   AllowDeferredHandleDereference heap_object_check;
416   // Verify all Objects referred by code are NOT in new space.
417   Object* obj = *handle;
418   DCHECK(!isolate()->heap()->InNewSpace(obj));
419   if (obj->IsHeapObject()) {
420     emit(reinterpret_cast<intptr_t>(handle.location()),
421          RelocInfo::EMBEDDED_OBJECT);
422   } else {
423     // no relocation needed
424     emit(reinterpret_cast<intptr_t>(obj));
425   }
426 }
427
428
429 void Assembler::emit(uint32_t x, RelocInfo::Mode rmode, TypeFeedbackId id) {
430   if (rmode == RelocInfo::CODE_TARGET && !id.IsNone()) {
431     RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, id.ToInt());
432   } else if (!RelocInfo::IsNone(rmode)
433       && rmode != RelocInfo::CODE_AGE_SEQUENCE) {
434     RecordRelocInfo(rmode);
435   }
436   emit(x);
437 }
438
439
440 void Assembler::emit(Handle<Code> code,
441                      RelocInfo::Mode rmode,
442                      TypeFeedbackId id) {
443   AllowDeferredHandleDereference embedding_raw_address;
444   emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
445 }
446
447
448 void Assembler::emit(const Immediate& x) {
449   if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
450     Label* label = reinterpret_cast<Label*>(x.x_);
451     emit_code_relative_offset(label);
452     return;
453   }
454   if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
455   emit(x.x_);
456 }
457
458
459 void Assembler::emit_code_relative_offset(Label* label) {
460   if (label->is_bound()) {
461     int32_t pos;
462     pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
463     emit(pos);
464   } else {
465     emit_disp(label, Displacement::CODE_RELATIVE);
466   }
467 }
468
469
470 void Assembler::emit_w(const Immediate& x) {
471   DCHECK(RelocInfo::IsNone(x.rmode_));
472   uint16_t value = static_cast<uint16_t>(x.x_);
473   reinterpret_cast<uint16_t*>(pc_)[0] = value;
474   pc_ += sizeof(uint16_t);
475 }
476
477
478 Address Assembler::target_address_at(Address pc,
479                                      ConstantPoolArray* constant_pool) {
480   return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
481 }
482
483
484 void Assembler::set_target_address_at(Address pc,
485                                       ConstantPoolArray* constant_pool,
486                                       Address target,
487                                       ICacheFlushMode icache_flush_mode) {
488   int32_t* p = reinterpret_cast<int32_t*>(pc);
489   *p = target - (pc + sizeof(int32_t));
490   if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
491     CpuFeatures::FlushICache(p, sizeof(int32_t));
492   }
493 }
494
495
496 Address Assembler::target_address_from_return_address(Address pc) {
497   return pc - kCallTargetAddressOffset;
498 }
499
500
501 Address Assembler::break_address_from_return_address(Address pc) {
502   return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
503 }
504
505
506 Displacement Assembler::disp_at(Label* L) {
507   return Displacement(long_at(L->pos()));
508 }
509
510
511 void Assembler::disp_at_put(Label* L, Displacement disp) {
512   long_at_put(L->pos(), disp.data());
513 }
514
515
516 void Assembler::emit_disp(Label* L, Displacement::Type type) {
517   Displacement disp(L, type);
518   L->link_to(pc_offset());
519   emit(static_cast<int>(disp.data()));
520 }
521
522
523 void Assembler::emit_near_disp(Label* L) {
524   byte disp = 0x00;
525   if (L->is_near_linked()) {
526     int offset = L->near_link_pos() - pc_offset();
527     DCHECK(is_int8(offset));
528     disp = static_cast<byte>(offset & 0xFF);
529   }
530   L->link_to(pc_offset(), Label::kNear);
531   *pc_++ = disp;
532 }
533
534
535 void Assembler::deserialization_set_target_internal_reference_at(
536     Address pc, Address target, RelocInfo::Mode mode) {
537   Memory::Address_at(pc) = target;
538 }
539
540
541 void Operand::set_modrm(int mod, Register rm) {
542   DCHECK((mod & -4) == 0);
543   buf_[0] = mod << 6 | rm.code();
544   len_ = 1;
545 }
546
547
548 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
549   DCHECK(len_ == 1);
550   DCHECK((scale & -4) == 0);
551   // Use SIB with no index register only for base esp.
552   DCHECK(!index.is(esp) || base.is(esp));
553   buf_[1] = scale << 6 | index.code() << 3 | base.code();
554   len_ = 2;
555 }
556
557
558 void Operand::set_disp8(int8_t disp) {
559   DCHECK(len_ == 1 || len_ == 2);
560   *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
561 }
562
563
564 void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) {
565   DCHECK(len_ == 1 || len_ == 2);
566   int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
567   *p = disp;
568   len_ += sizeof(int32_t);
569   rmode_ = rmode;
570 }
571
572 Operand::Operand(Register reg) {
573   // reg
574   set_modrm(3, reg);
575 }
576
577
578 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
579   // [disp/r]
580   set_modrm(0, ebp);
581   set_dispr(disp, rmode);
582 }
583
584
585 Operand::Operand(Immediate imm) {
586   // [disp/r]
587   set_modrm(0, ebp);
588   set_dispr(imm.x_, imm.rmode_);
589 }
590 } }  // namespace v8::internal
591
592 #endif  // V8_X87_ASSEMBLER_X87_INL_H_