1a4a6c50908db35b49a01de6287420f2d961df82
[platform/framework/web/crosswalk.git] / src / v8 / src / mips / assembler-mips-inl.h
1
2 // Copyright (c) 1994-2006 Sun Microsystems Inc.
3 // All Rights Reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // - Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 //
12 // - Redistribution in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // - Neither the name of Sun Microsystems or the names of contributors may
17 // be used to endorse or promote products derived from this software without
18 // specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 // The original source code covered by the above license above has been
33 // modified significantly by Google Inc.
34 // Copyright 2012 the V8 project authors. All rights reserved.
35
36
37 #ifndef V8_MIPS_ASSEMBLER_MIPS_INL_H_
38 #define V8_MIPS_ASSEMBLER_MIPS_INL_H_
39
40 #include "src/mips/assembler-mips.h"
41
42 #include "src/assembler.h"
43 #include "src/debug.h"
44
45
46 namespace v8 {
47 namespace internal {
48
49
50 bool CpuFeatures::SupportsCrankshaft() { return IsSupported(FPU); }
51 bool CpuFeatures::SupportsSIMD128InCrankshaft() { return false; }
52
53
54 // -----------------------------------------------------------------------------
55 // Operand and MemOperand.
56
57 Operand::Operand(int32_t immediate, RelocInfo::Mode rmode)  {
58   rm_ = no_reg;
59   imm32_ = immediate;
60   rmode_ = rmode;
61 }
62
63
64 Operand::Operand(const ExternalReference& f)  {
65   rm_ = no_reg;
66   imm32_ = reinterpret_cast<int32_t>(f.address());
67   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
68 }
69
70
71 Operand::Operand(Smi* value) {
72   rm_ = no_reg;
73   imm32_ =  reinterpret_cast<intptr_t>(value);
74   rmode_ = RelocInfo::NONE32;
75 }
76
77
78 Operand::Operand(Register rm) {
79   rm_ = rm;
80 }
81
82
83 bool Operand::is_reg() const {
84   return rm_.is_valid();
85 }
86
87
88 int Register::NumAllocatableRegisters() {
89     return kMaxNumAllocatableRegisters;
90 }
91
92
93 int DoubleRegister::NumRegisters() {
94     return FPURegister::kMaxNumRegisters;
95 }
96
97
98 int DoubleRegister::NumAllocatableRegisters() {
99     return FPURegister::kMaxNumAllocatableRegisters;
100 }
101
102
103 int FPURegister::ToAllocationIndex(FPURegister reg) {
104   DCHECK(reg.code() % 2 == 0);
105   DCHECK(reg.code() / 2 < kMaxNumAllocatableRegisters);
106   DCHECK(reg.is_valid());
107   DCHECK(!reg.is(kDoubleRegZero));
108   DCHECK(!reg.is(kLithiumScratchDouble));
109   return (reg.code() / 2);
110 }
111
112
113 // -----------------------------------------------------------------------------
114 // RelocInfo.
115
116 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
117   if (IsCodeTarget(rmode_)) {
118     uint32_t scope1 = (uint32_t) target_address() & ~kImm28Mask;
119     uint32_t scope2 = reinterpret_cast<uint32_t>(pc_) & ~kImm28Mask;
120
121     if (scope1 != scope2) {
122       Assembler::JumpLabelToJumpRegister(pc_);
123     }
124   }
125   if (IsInternalReference(rmode_)) {
126     // Absolute code pointer inside code object moves with the code object.
127     byte* p = reinterpret_cast<byte*>(pc_);
128     int count = Assembler::RelocateInternalReference(p, delta);
129     CpuFeatures::FlushICache(p, count * sizeof(uint32_t));
130   }
131 }
132
133
134 Address RelocInfo::target_address() {
135   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
136   return Assembler::target_address_at(pc_, host_);
137 }
138
139
140 Address RelocInfo::target_address_address() {
141   DCHECK(IsCodeTarget(rmode_) ||
142          IsRuntimeEntry(rmode_) ||
143          rmode_ == EMBEDDED_OBJECT ||
144          rmode_ == EXTERNAL_REFERENCE);
145   // Read the address of the word containing the target_address in an
146   // instruction stream.
147   // The only architecture-independent user of this function is the serializer.
148   // The serializer uses it to find out how many raw bytes of instruction to
149   // output before the next target.
150   // For an instruction like LUI/ORI where the target bits are mixed into the
151   // instruction bits, the size of the target will be zero, indicating that the
152   // serializer should not step forward in memory after a target is resolved
153   // and written. In this case the target_address_address function should
154   // return the end of the instructions to be patched, allowing the
155   // deserializer to deserialize the instructions as raw bytes and put them in
156   // place, ready to be patched with the target. After jump optimization,
157   // that is the address of the instruction that follows J/JAL/JR/JALR
158   // instruction.
159   return reinterpret_cast<Address>(
160     pc_ + Assembler::kInstructionsFor32BitConstant * Assembler::kInstrSize);
161 }
162
163
164 Address RelocInfo::constant_pool_entry_address() {
165   UNREACHABLE();
166   return NULL;
167 }
168
169
170 int RelocInfo::target_address_size() {
171   return Assembler::kSpecialTargetSize;
172 }
173
174
175 void RelocInfo::set_target_address(Address target,
176                                    WriteBarrierMode write_barrier_mode,
177                                    ICacheFlushMode icache_flush_mode) {
178   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
179   Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
180   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
181       host() != NULL && IsCodeTarget(rmode_)) {
182     Object* target_code = Code::GetCodeFromTargetAddress(target);
183     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
184         host(), this, HeapObject::cast(target_code));
185   }
186 }
187
188
189 Address Assembler::target_address_from_return_address(Address pc) {
190   return pc - kCallTargetAddressOffset;
191 }
192
193
194 Address Assembler::break_address_from_return_address(Address pc) {
195   return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
196 }
197
198
199 Object* RelocInfo::target_object() {
200   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
201   return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
202 }
203
204
205 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
206   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
207   return Handle<Object>(reinterpret_cast<Object**>(
208       Assembler::target_address_at(pc_, host_)));
209 }
210
211
212 void RelocInfo::set_target_object(Object* target,
213                                   WriteBarrierMode write_barrier_mode,
214                                   ICacheFlushMode icache_flush_mode) {
215   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
216   Assembler::set_target_address_at(pc_, host_,
217                                    reinterpret_cast<Address>(target),
218                                    icache_flush_mode);
219   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
220       host() != NULL &&
221       target->IsHeapObject()) {
222     host()->GetHeap()->incremental_marking()->RecordWrite(
223         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
224   }
225 }
226
227
228 Address RelocInfo::target_reference() {
229   DCHECK(rmode_ == EXTERNAL_REFERENCE);
230   return Assembler::target_address_at(pc_, host_);
231 }
232
233
234 Address RelocInfo::target_runtime_entry(Assembler* origin) {
235   DCHECK(IsRuntimeEntry(rmode_));
236   return target_address();
237 }
238
239
240 void RelocInfo::set_target_runtime_entry(Address target,
241                                          WriteBarrierMode write_barrier_mode,
242                                          ICacheFlushMode icache_flush_mode) {
243   DCHECK(IsRuntimeEntry(rmode_));
244   if (target_address() != target)
245     set_target_address(target, write_barrier_mode, icache_flush_mode);
246 }
247
248
249 Handle<Cell> RelocInfo::target_cell_handle() {
250   DCHECK(rmode_ == RelocInfo::CELL);
251   Address address = Memory::Address_at(pc_);
252   return Handle<Cell>(reinterpret_cast<Cell**>(address));
253 }
254
255
256 Cell* RelocInfo::target_cell() {
257   DCHECK(rmode_ == RelocInfo::CELL);
258   return Cell::FromValueAddress(Memory::Address_at(pc_));
259 }
260
261
262 void RelocInfo::set_target_cell(Cell* cell,
263                                 WriteBarrierMode write_barrier_mode,
264                                 ICacheFlushMode icache_flush_mode) {
265   DCHECK(rmode_ == RelocInfo::CELL);
266   Address address = cell->address() + Cell::kValueOffset;
267   Memory::Address_at(pc_) = address;
268   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
269     // TODO(1550) We are passing NULL as a slot because cell can never be on
270     // evacuation candidate.
271     host()->GetHeap()->incremental_marking()->RecordWrite(
272         host(), NULL, cell);
273   }
274 }
275
276
277 static const int kNoCodeAgeSequenceLength = 7 * Assembler::kInstrSize;
278
279
280 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
281   UNREACHABLE();  // This should never be reached on Arm.
282   return Handle<Object>();
283 }
284
285
286 Code* RelocInfo::code_age_stub() {
287   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
288   return Code::GetCodeFromTargetAddress(
289       Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
290 }
291
292
293 void RelocInfo::set_code_age_stub(Code* stub,
294                                   ICacheFlushMode icache_flush_mode) {
295   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
296   Assembler::set_target_address_at(pc_ + Assembler::kInstrSize,
297                                    host_,
298                                    stub->instruction_start());
299 }
300
301
302 Address RelocInfo::call_address() {
303   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
304          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
305   // The pc_ offset of 0 assumes mips patched return sequence per
306   // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
307   // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
308   return Assembler::target_address_at(pc_, host_);
309 }
310
311
312 void RelocInfo::set_call_address(Address target) {
313   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
314          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
315   // The pc_ offset of 0 assumes mips patched return sequence per
316   // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
317   // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
318   Assembler::set_target_address_at(pc_, host_, target);
319   if (host() != NULL) {
320     Object* target_code = Code::GetCodeFromTargetAddress(target);
321     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
322         host(), this, HeapObject::cast(target_code));
323   }
324 }
325
326
327 Object* RelocInfo::call_object() {
328   return *call_object_address();
329 }
330
331
332 Object** RelocInfo::call_object_address() {
333   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
334          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
335   return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
336 }
337
338
339 void RelocInfo::set_call_object(Object* target) {
340   *call_object_address() = target;
341 }
342
343
344 void RelocInfo::WipeOut() {
345   DCHECK(IsEmbeddedObject(rmode_) ||
346          IsCodeTarget(rmode_) ||
347          IsRuntimeEntry(rmode_) ||
348          IsExternalReference(rmode_));
349   Assembler::set_target_address_at(pc_, host_, NULL);
350 }
351
352
353 bool RelocInfo::IsPatchedReturnSequence() {
354   Instr instr0 = Assembler::instr_at(pc_);
355   Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
356   Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize);
357   bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
358                          (instr1 & kOpcodeMask) == ORI &&
359                          ((instr2 & kOpcodeMask) == JAL ||
360                           ((instr2 & kOpcodeMask) == SPECIAL &&
361                            (instr2 & kFunctionFieldMask) == JALR)));
362   return patched_return;
363 }
364
365
366 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
367   Instr current_instr = Assembler::instr_at(pc_);
368   return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
369 }
370
371
372 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
373   RelocInfo::Mode mode = rmode();
374   if (mode == RelocInfo::EMBEDDED_OBJECT) {
375     visitor->VisitEmbeddedPointer(this);
376   } else if (RelocInfo::IsCodeTarget(mode)) {
377     visitor->VisitCodeTarget(this);
378   } else if (mode == RelocInfo::CELL) {
379     visitor->VisitCell(this);
380   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
381     visitor->VisitExternalReference(this);
382   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
383     visitor->VisitCodeAgeSequence(this);
384   } else if (((RelocInfo::IsJSReturn(mode) &&
385               IsPatchedReturnSequence()) ||
386              (RelocInfo::IsDebugBreakSlot(mode) &&
387              IsPatchedDebugBreakSlotSequence())) &&
388              isolate->debug()->has_break_points()) {
389     visitor->VisitDebugTarget(this);
390   } else if (RelocInfo::IsRuntimeEntry(mode)) {
391     visitor->VisitRuntimeEntry(this);
392   }
393 }
394
395
396 template<typename StaticVisitor>
397 void RelocInfo::Visit(Heap* heap) {
398   RelocInfo::Mode mode = rmode();
399   if (mode == RelocInfo::EMBEDDED_OBJECT) {
400     StaticVisitor::VisitEmbeddedPointer(heap, this);
401   } else if (RelocInfo::IsCodeTarget(mode)) {
402     StaticVisitor::VisitCodeTarget(heap, this);
403   } else if (mode == RelocInfo::CELL) {
404     StaticVisitor::VisitCell(heap, this);
405   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
406     StaticVisitor::VisitExternalReference(this);
407   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
408     StaticVisitor::VisitCodeAgeSequence(heap, this);
409   } else if (heap->isolate()->debug()->has_break_points() &&
410              ((RelocInfo::IsJSReturn(mode) &&
411               IsPatchedReturnSequence()) ||
412              (RelocInfo::IsDebugBreakSlot(mode) &&
413               IsPatchedDebugBreakSlotSequence()))) {
414     StaticVisitor::VisitDebugTarget(heap, this);
415   } else if (RelocInfo::IsRuntimeEntry(mode)) {
416     StaticVisitor::VisitRuntimeEntry(this);
417   }
418 }
419
420
421 // -----------------------------------------------------------------------------
422 // Assembler.
423
424
425 void Assembler::CheckBuffer() {
426   if (buffer_space() <= kGap) {
427     GrowBuffer();
428   }
429 }
430
431
432 void Assembler::CheckTrampolinePoolQuick() {
433   if (pc_offset() >= next_buffer_check_) {
434     CheckTrampolinePool();
435   }
436 }
437
438
439 void Assembler::emit(Instr x) {
440   if (!is_buffer_growth_blocked()) {
441     CheckBuffer();
442   }
443   *reinterpret_cast<Instr*>(pc_) = x;
444   pc_ += kInstrSize;
445   CheckTrampolinePoolQuick();
446 }
447
448
449 } }  // namespace v8::internal
450
451 #endif  // V8_MIPS_ASSEMBLER_MIPS_INL_H_