[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / 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/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   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));
114   }
115 }
116
117
118 Object* RelocInfo::target_object() {
119   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
120   return Memory::Object_at(pc_);
121 }
122
123
124 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
125   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
126   return Memory::Object_Handle_at(pc_);
127 }
128
129
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));
137   }
138   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
139       host() != NULL &&
140       target->IsHeapObject()) {
141     host()->GetHeap()->incremental_marking()->RecordWrite(
142         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
143   }
144 }
145
146
147 Address RelocInfo::target_external_reference() {
148   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
149   return Memory::Address_at(pc_);
150 }
151
152
153 Address RelocInfo::target_internal_reference() {
154   DCHECK(rmode_ == INTERNAL_REFERENCE);
155   return Memory::Address_at(pc_);
156 }
157
158
159 Address RelocInfo::target_internal_reference_address() {
160   DCHECK(rmode_ == INTERNAL_REFERENCE);
161   return reinterpret_cast<Address>(pc_);
162 }
163
164
165 Address RelocInfo::target_runtime_entry(Assembler* origin) {
166   DCHECK(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   DCHECK(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   DCHECK(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   DCHECK(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   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));
203   }
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(
208         host(), NULL, cell);
209   }
210 }
211
212
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);
217 }
218
219
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_));
225 }
226
227
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(),
233                                    icache_flush_mode);
234 }
235
236
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_);
241 }
242
243
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));
252   }
253 }
254
255
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));
263   } else {
264     UNREACHABLE();
265   }
266 }
267
268
269 bool RelocInfo::IsPatchedReturnSequence() {
270   return *pc_ == kCallOpcode;
271 }
272
273
274 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
275   return !Assembler::IsNop(pc());
276 }
277
278
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);
299   }
300 }
301
302
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);
324   }
325 }
326
327
328
329 Immediate::Immediate(int x)  {
330   x_ = x;
331   rmode_ = RelocInfo::NONE32;
332 }
333
334
335 Immediate::Immediate(const ExternalReference& ext) {
336   x_ = reinterpret_cast<int32_t>(ext.address());
337   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
338 }
339
340
341 Immediate::Immediate(Label* internal_offset) {
342   x_ = reinterpret_cast<int32_t>(internal_offset);
343   rmode_ = RelocInfo::INTERNAL_REFERENCE;
344 }
345
346
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;
355   } else {
356     // no relocation needed
357     x_ =  reinterpret_cast<intptr_t>(obj);
358     rmode_ = RelocInfo::NONE32;
359   }
360 }
361
362
363 Immediate::Immediate(Smi* value) {
364   x_ = reinterpret_cast<intptr_t>(value);
365   rmode_ = RelocInfo::NONE32;
366 }
367
368
369 Immediate::Immediate(Address addr) {
370   x_ = reinterpret_cast<int32_t>(addr);
371   rmode_ = RelocInfo::NONE32;
372 }
373
374
375 void Assembler::emit(uint32_t x) {
376   *reinterpret_cast<uint32_t*>(pc_) = x;
377   pc_ += sizeof(uint32_t);
378 }
379
380
381 void Assembler::emit_q(uint64_t x) {
382   *reinterpret_cast<uint64_t*>(pc_) = x;
383   pc_ += sizeof(uint64_t);
384 }
385
386
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);
395   } else {
396     // no relocation needed
397     emit(reinterpret_cast<intptr_t>(obj));
398   }
399 }
400
401
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);
408   }
409   emit(x);
410 }
411
412
413 void Assembler::emit(Handle<Code> code,
414                      RelocInfo::Mode rmode,
415                      TypeFeedbackId id) {
416   AllowDeferredHandleDereference embedding_raw_address;
417   emit(reinterpret_cast<intptr_t>(code.location()), rmode, id);
418 }
419
420
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);
425     return;
426   }
427   if (!RelocInfo::IsNone(x.rmode_)) RecordRelocInfo(x.rmode_);
428   emit(x.x_);
429 }
430
431
432 void Assembler::emit_code_relative_offset(Label* label) {
433   if (label->is_bound()) {
434     int32_t pos;
435     pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
436     emit(pos);
437   } else {
438     emit_disp(label, Displacement::CODE_RELATIVE);
439   }
440 }
441
442
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);
448 }
449
450
451 Address Assembler::target_address_at(Address pc, Address constant_pool) {
452   return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
453 }
454
455
456 void Assembler::set_target_address_at(Address pc, Address constant_pool,
457                                       Address target,
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));
463   }
464 }
465
466
467 Address Assembler::target_address_from_return_address(Address pc) {
468   return pc - kCallTargetAddressOffset;
469 }
470
471
472 Displacement Assembler::disp_at(Label* L) {
473   return Displacement(long_at(L->pos()));
474 }
475
476
477 void Assembler::disp_at_put(Label* L, Displacement disp) {
478   long_at_put(L->pos(), disp.data());
479 }
480
481
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()));
486 }
487
488
489 void Assembler::emit_near_disp(Label* L) {
490   byte disp = 0x00;
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);
495   }
496   L->link_to(pc_offset(), Label::kNear);
497   *pc_++ = disp;
498 }
499
500
501 void Assembler::deserialization_set_target_internal_reference_at(
502     Address pc, Address target, RelocInfo::Mode mode) {
503   Memory::Address_at(pc) = target;
504 }
505
506
507 void Operand::set_modrm(int mod, Register rm) {
508   DCHECK((mod & -4) == 0);
509   buf_[0] = mod << 6 | rm.code();
510   len_ = 1;
511 }
512
513
514 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
515   DCHECK(len_ == 1);
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();
520   len_ = 2;
521 }
522
523
524 void Operand::set_disp8(int8_t disp) {
525   DCHECK(len_ == 1 || len_ == 2);
526   *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
527 }
528
529
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_]);
533   *p = disp;
534   len_ += sizeof(int32_t);
535   rmode_ = rmode;
536 }
537
538 Operand::Operand(Register reg) {
539   // reg
540   set_modrm(3, reg);
541 }
542
543
544 Operand::Operand(XMMRegister xmm_reg) {
545   Register reg = { xmm_reg.code() };
546   set_modrm(3, reg);
547 }
548
549
550 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
551   // [disp/r]
552   set_modrm(0, ebp);
553   set_dispr(disp, rmode);
554 }
555
556
557 Operand::Operand(Immediate imm) {
558   // [disp/r]
559   set_modrm(0, ebp);
560   set_dispr(imm.x_, imm.rmode_);
561 }
562 }  // namespace internal
563 }  // namespace v8
564
565 #endif  // V8_IA32_ASSEMBLER_IA32_INL_H_