Upstream version 8.37.186.0
[platform/framework/web/crosswalk.git] / src / v8 / src / ia32 / assembler-ia32-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_IA32_ASSEMBLER_IA32_INL_H_
38 #define V8_IA32_ASSEMBLER_IA32_INL_H_
39
40 #include "src/ia32/assembler-ia32.h"
41
42 #include "src/cpu.h"
43 #include "src/debug.h"
44
45 namespace v8 {
46 namespace internal {
47
48 bool CpuFeatures::SupportsCrankshaft() { return true; }
49 bool CpuFeatures::SupportsSIMD128InCrankshaft() { return true; }
50
51
52 static const byte kCallOpcode = 0xE8;
53 static const int kNoCodeAgeSequenceLength = 5;
54
55
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));
68     }
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));
86   }
87 }
88
89
90 Address RelocInfo::target_address() {
91   ASSERT(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
92   return Assembler::target_address_at(pc_, host_);
93 }
94
95
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_);
101 }
102
103
104 Address RelocInfo::constant_pool_entry_address() {
105   UNREACHABLE();
106   return NULL;
107 }
108
109
110 int RelocInfo::target_address_size() {
111   return Assembler::kSpecialTargetSize;
112 }
113
114
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));
125   }
126 }
127
128
129 Object* RelocInfo::target_object() {
130   ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
131   return Memory::Object_at(pc_);
132 }
133
134
135 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
136   ASSERT(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   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));
149   }
150   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
151       host() != NULL &&
152       target->IsHeapObject()) {
153     host()->GetHeap()->incremental_marking()->RecordWrite(
154         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
155   }
156 }
157
158
159 Address RelocInfo::target_reference() {
160   ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
161   return Memory::Address_at(pc_);
162 }
163
164
165 Address RelocInfo::target_runtime_entry(Assembler* origin) {
166   ASSERT(IsRuntimeEntry(rmode_));
167   return reinterpret_cast<Address>(*reinterpret_cast<int32_t*>(pc_));
168 }
169
170
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);
177   }
178 }
179
180
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));
185 }
186
187
188 Cell* RelocInfo::target_cell() {
189   ASSERT(rmode_ == RelocInfo::CELL);
190   return Cell::FromValueAddress(Memory::Address_at(pc_));
191 }
192
193
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));
202   }
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(
207         host(), NULL, cell);
208   }
209 }
210
211
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);
216 }
217
218
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_));
224 }
225
226
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(),
232                                    icache_flush_mode);
233 }
234
235
236 Address RelocInfo::call_address() {
237   ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
238          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
239   return Assembler::target_address_at(pc_ + 1, host_);
240 }
241
242
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));
251   }
252 }
253
254
255 Object* RelocInfo::call_object() {
256   return *call_object_address();
257 }
258
259
260 void RelocInfo::set_call_object(Object* target) {
261   *call_object_address() = target;
262 }
263
264
265 Object** RelocInfo::call_object_address() {
266   ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
267          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
268   return reinterpret_cast<Object**>(pc_ + 1);
269 }
270
271
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));
278   } else {
279     UNREACHABLE();
280   }
281 }
282
283
284 bool RelocInfo::IsPatchedReturnSequence() {
285   return *pc_ == kCallOpcode;
286 }
287
288
289 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
290   return !Assembler::IsNop(pc());
291 }
292
293
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);
316   }
317 }
318
319
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);
343   }
344 }
345
346
347
348 Immediate::Immediate(int x)  {
349   x_ = x;
350   rmode_ = RelocInfo::NONE32;
351 }
352
353
354 Immediate::Immediate(const ExternalReference& ext) {
355   x_ = reinterpret_cast<int32_t>(ext.address());
356   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
357 }
358
359
360 Immediate::Immediate(Label* internal_offset) {
361   x_ = reinterpret_cast<int32_t>(internal_offset);
362   rmode_ = RelocInfo::INTERNAL_REFERENCE;
363 }
364
365
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;
374   } else {
375     // no relocation needed
376     x_ =  reinterpret_cast<intptr_t>(obj);
377     rmode_ = RelocInfo::NONE32;
378   }
379 }
380
381
382 Immediate::Immediate(Smi* value) {
383   x_ = reinterpret_cast<intptr_t>(value);
384   rmode_ = RelocInfo::NONE32;
385 }
386
387
388 Immediate::Immediate(Address addr) {
389   x_ = reinterpret_cast<int32_t>(addr);
390   rmode_ = RelocInfo::NONE32;
391 }
392
393
394 void Assembler::emit(uint32_t x) {
395   *reinterpret_cast<uint32_t*>(pc_) = x;
396   pc_ += sizeof(uint32_t);
397 }
398
399
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);
408   } else {
409     // no relocation needed
410     emit(reinterpret_cast<intptr_t>(obj));
411   }
412 }
413
414
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);
421   }
422   emit(x);
423 }
424
425
426 void Assembler::emit(Handle<Code> code,
427                      RelocInfo::Mode rmode,
428                      TypeFeedbackId id) {
429   AllowDeferredHandleDereference embedding_raw_address;
430   emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
431 }
432
433
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);
438     return;
439   }
440   if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
441   emit(x.x_);
442 }
443
444
445 void Assembler::emit_code_relative_offset(Label* label) {
446   if (label->is_bound()) {
447     int32_t pos;
448     pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
449     emit(pos);
450   } else {
451     emit_disp(label, Displacement::CODE_RELATIVE);
452   }
453 }
454
455
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);
461 }
462
463
464 Address Assembler::target_address_at(Address pc,
465                                      ConstantPoolArray* constant_pool) {
466   return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
467 }
468
469
470 void Assembler::set_target_address_at(Address pc,
471                                       ConstantPoolArray* constant_pool,
472                                       Address target,
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));
478   }
479 }
480
481
482 Address Assembler::target_address_from_return_address(Address pc) {
483   return pc - kCallTargetAddressOffset;
484 }
485
486
487 Displacement Assembler::disp_at(Label* L) {
488   return Displacement(long_at(L->pos()));
489 }
490
491
492 void Assembler::disp_at_put(Label* L, Displacement disp) {
493   long_at_put(L->pos(), disp.data());
494 }
495
496
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()));
501 }
502
503
504 void Assembler::emit_near_disp(Label* L) {
505   byte disp = 0x00;
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);
510   }
511   L->link_to(pc_offset(), Label::kNear);
512   *pc_++ = disp;
513 }
514
515
516 void Operand::set_modrm(int mod, Register rm) {
517   ASSERT((mod & -4) == 0);
518   buf_[0] = mod << 6 | rm.code();
519   len_ = 1;
520 }
521
522
523 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
524   ASSERT(len_ == 1);
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();
529   len_ = 2;
530 }
531
532
533 void Operand::set_disp8(int8_t disp) {
534   ASSERT(len_ == 1 || len_ == 2);
535   *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
536 }
537
538
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_]);
542   *p = disp;
543   len_ += sizeof(int32_t);
544   rmode_ = rmode;
545 }
546
547 Operand::Operand(Register reg) {
548   // reg
549   set_modrm(3, reg);
550 }
551
552
553 Operand::Operand(XMMRegister xmm_reg) {
554   Register reg = { xmm_reg.code() };
555   set_modrm(3, reg);
556 }
557
558
559 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
560   // [disp/r]
561   set_modrm(0, ebp);
562   set_dispr(disp, rmode);
563 }
564
565 } }  // namespace v8::internal
566
567 #endif  // V8_IA32_ASSEMBLER_IA32_INL_H_