deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / 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
52
53 // -----------------------------------------------------------------------------
54 // Operand and MemOperand.
55
56 Operand::Operand(int32_t immediate, RelocInfo::Mode rmode)  {
57   rm_ = no_reg;
58   imm32_ = immediate;
59   rmode_ = rmode;
60 }
61
62
63 Operand::Operand(const ExternalReference& f)  {
64   rm_ = no_reg;
65   imm32_ = reinterpret_cast<int32_t>(f.address());
66   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
67 }
68
69
70 Operand::Operand(Smi* value) {
71   rm_ = no_reg;
72   imm32_ =  reinterpret_cast<intptr_t>(value);
73   rmode_ = RelocInfo::NONE32;
74 }
75
76
77 Operand::Operand(Register rm) {
78   rm_ = rm;
79 }
80
81
82 bool Operand::is_reg() const {
83   return rm_.is_valid();
84 }
85
86
87 int Register::NumAllocatableRegisters() {
88     return kMaxNumAllocatableRegisters;
89 }
90
91
92 int DoubleRegister::NumRegisters() {
93     return FPURegister::kMaxNumRegisters;
94 }
95
96
97 int DoubleRegister::NumAllocatableRegisters() {
98     return FPURegister::kMaxNumAllocatableRegisters;
99 }
100
101
102 int DoubleRegister::NumAllocatableAliasedRegisters() {
103   return NumAllocatableRegisters();
104 }
105
106
107 int FPURegister::ToAllocationIndex(FPURegister reg) {
108   DCHECK(reg.code() % 2 == 0);
109   DCHECK(reg.code() / 2 < kMaxNumAllocatableRegisters);
110   DCHECK(reg.is_valid());
111   DCHECK(!reg.is(kDoubleRegZero));
112   DCHECK(!reg.is(kLithiumScratchDouble));
113   return (reg.code() / 2);
114 }
115
116
117 // -----------------------------------------------------------------------------
118 // RelocInfo.
119
120 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
121   if (IsCodeTarget(rmode_)) {
122     uint32_t scope1 = (uint32_t) target_address() & ~kImm28Mask;
123     uint32_t scope2 = reinterpret_cast<uint32_t>(pc_) & ~kImm28Mask;
124
125     if (scope1 != scope2) {
126       Assembler::JumpLabelToJumpRegister(pc_);
127     }
128   }
129   if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
130     // Absolute code pointer inside code object moves with the code object.
131     byte* p = reinterpret_cast<byte*>(pc_);
132     int count = Assembler::RelocateInternalReference(rmode_, p, delta);
133     CpuFeatures::FlushICache(p, count * sizeof(uint32_t));
134   }
135 }
136
137
138 Address RelocInfo::target_address() {
139   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
140   return Assembler::target_address_at(pc_, host_);
141 }
142
143
144 Address RelocInfo::target_address_address() {
145   DCHECK(IsCodeTarget(rmode_) ||
146          IsRuntimeEntry(rmode_) ||
147          rmode_ == EMBEDDED_OBJECT ||
148          rmode_ == EXTERNAL_REFERENCE);
149   // Read the address of the word containing the target_address in an
150   // instruction stream.
151   // The only architecture-independent user of this function is the serializer.
152   // The serializer uses it to find out how many raw bytes of instruction to
153   // output before the next target.
154   // For an instruction like LUI/ORI where the target bits are mixed into the
155   // instruction bits, the size of the target will be zero, indicating that the
156   // serializer should not step forward in memory after a target is resolved
157   // and written. In this case the target_address_address function should
158   // return the end of the instructions to be patched, allowing the
159   // deserializer to deserialize the instructions as raw bytes and put them in
160   // place, ready to be patched with the target. After jump optimization,
161   // that is the address of the instruction that follows J/JAL/JR/JALR
162   // instruction.
163   return reinterpret_cast<Address>(
164     pc_ + Assembler::kInstructionsFor32BitConstant * Assembler::kInstrSize);
165 }
166
167
168 Address RelocInfo::constant_pool_entry_address() {
169   UNREACHABLE();
170   return NULL;
171 }
172
173
174 int RelocInfo::target_address_size() {
175   return Assembler::kSpecialTargetSize;
176 }
177
178
179 void RelocInfo::set_target_address(Address target,
180                                    WriteBarrierMode write_barrier_mode,
181                                    ICacheFlushMode icache_flush_mode) {
182   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
183   Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
184   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
185       host() != NULL && IsCodeTarget(rmode_)) {
186     Object* target_code = Code::GetCodeFromTargetAddress(target);
187     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
188         host(), this, HeapObject::cast(target_code));
189   }
190 }
191
192
193 Address Assembler::target_address_from_return_address(Address pc) {
194   return pc - kCallTargetAddressOffset;
195 }
196
197
198 Address Assembler::break_address_from_return_address(Address pc) {
199   return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
200 }
201
202
203 void Assembler::set_target_internal_reference_encoded_at(Address pc,
204                                                          Address target) {
205   // Encoded internal references are lui/ori load of 32-bit abolute address.
206   Instr instr_lui = Assembler::instr_at(pc + 0 * Assembler::kInstrSize);
207   Instr instr_ori = Assembler::instr_at(pc + 1 * Assembler::kInstrSize);
208   DCHECK(Assembler::IsLui(instr_lui));
209   DCHECK(Assembler::IsOri(instr_ori));
210   instr_lui &= ~kImm16Mask;
211   instr_ori &= ~kImm16Mask;
212   int32_t imm = reinterpret_cast<int32_t>(target);
213   DCHECK((imm & 3) == 0);
214   Assembler::instr_at_put(pc + 0 * Assembler::kInstrSize,
215                           instr_lui | ((imm >> kLuiShift) & kImm16Mask));
216   Assembler::instr_at_put(pc + 1 * Assembler::kInstrSize,
217                           instr_ori | (imm & kImm16Mask));
218
219   // Currently used only by deserializer, and all code will be flushed
220   // after complete deserialization, no need to flush on each reference.
221 }
222
223
224 void Assembler::deserialization_set_target_internal_reference_at(
225     Address pc, Address target, RelocInfo::Mode mode) {
226   if (mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
227     DCHECK(IsLui(instr_at(pc)));
228     set_target_internal_reference_encoded_at(pc, target);
229   } else {
230     DCHECK(mode == RelocInfo::INTERNAL_REFERENCE);
231     Memory::Address_at(pc) = target;
232   }
233 }
234
235
236 Object* RelocInfo::target_object() {
237   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
238   return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
239 }
240
241
242 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
243   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
244   return Handle<Object>(reinterpret_cast<Object**>(
245       Assembler::target_address_at(pc_, host_)));
246 }
247
248
249 void RelocInfo::set_target_object(Object* target,
250                                   WriteBarrierMode write_barrier_mode,
251                                   ICacheFlushMode icache_flush_mode) {
252   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
253   Assembler::set_target_address_at(pc_, host_,
254                                    reinterpret_cast<Address>(target),
255                                    icache_flush_mode);
256   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
257       host() != NULL &&
258       target->IsHeapObject()) {
259     host()->GetHeap()->incremental_marking()->RecordWrite(
260         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
261   }
262 }
263
264
265 Address RelocInfo::target_external_reference() {
266   DCHECK(rmode_ == EXTERNAL_REFERENCE);
267   return Assembler::target_address_at(pc_, host_);
268 }
269
270
271 Address RelocInfo::target_internal_reference() {
272   if (rmode_ == INTERNAL_REFERENCE) {
273     return Memory::Address_at(pc_);
274   } else {
275     // Encoded internal references are lui/ori load of 32-bit abolute address.
276     DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
277     Instr instr_lui = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
278     Instr instr_ori = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
279     DCHECK(Assembler::IsLui(instr_lui));
280     DCHECK(Assembler::IsOri(instr_ori));
281     int32_t imm = (instr_lui & static_cast<int32_t>(kImm16Mask)) << kLuiShift;
282     imm |= (instr_ori & static_cast<int32_t>(kImm16Mask));
283     return reinterpret_cast<Address>(imm);
284   }
285 }
286
287
288 Address RelocInfo::target_internal_reference_address() {
289   DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
290   return reinterpret_cast<Address>(pc_);
291 }
292
293
294 Address RelocInfo::target_runtime_entry(Assembler* origin) {
295   DCHECK(IsRuntimeEntry(rmode_));
296   return target_address();
297 }
298
299
300 void RelocInfo::set_target_runtime_entry(Address target,
301                                          WriteBarrierMode write_barrier_mode,
302                                          ICacheFlushMode icache_flush_mode) {
303   DCHECK(IsRuntimeEntry(rmode_));
304   if (target_address() != target)
305     set_target_address(target, write_barrier_mode, icache_flush_mode);
306 }
307
308
309 Handle<Cell> RelocInfo::target_cell_handle() {
310   DCHECK(rmode_ == RelocInfo::CELL);
311   Address address = Memory::Address_at(pc_);
312   return Handle<Cell>(reinterpret_cast<Cell**>(address));
313 }
314
315
316 Cell* RelocInfo::target_cell() {
317   DCHECK(rmode_ == RelocInfo::CELL);
318   return Cell::FromValueAddress(Memory::Address_at(pc_));
319 }
320
321
322 void RelocInfo::set_target_cell(Cell* cell,
323                                 WriteBarrierMode write_barrier_mode,
324                                 ICacheFlushMode icache_flush_mode) {
325   DCHECK(rmode_ == RelocInfo::CELL);
326   Address address = cell->address() + Cell::kValueOffset;
327   Memory::Address_at(pc_) = address;
328   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
329     // TODO(1550) We are passing NULL as a slot because cell can never be on
330     // evacuation candidate.
331     host()->GetHeap()->incremental_marking()->RecordWrite(
332         host(), NULL, cell);
333   }
334 }
335
336
337 static const int kNoCodeAgeSequenceLength = 7 * Assembler::kInstrSize;
338
339
340 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
341   UNREACHABLE();  // This should never be reached on Arm.
342   return Handle<Object>();
343 }
344
345
346 Code* RelocInfo::code_age_stub() {
347   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
348   return Code::GetCodeFromTargetAddress(
349       Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
350 }
351
352
353 void RelocInfo::set_code_age_stub(Code* stub,
354                                   ICacheFlushMode icache_flush_mode) {
355   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
356   Assembler::set_target_address_at(pc_ + Assembler::kInstrSize,
357                                    host_,
358                                    stub->instruction_start());
359 }
360
361
362 Address RelocInfo::call_address() {
363   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
364          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
365   // The pc_ offset of 0 assumes mips patched return sequence per
366   // debug-mips.cc BreakLocation::SetDebugBreakAtReturn(), or
367   // debug break slot per BreakLocation::SetDebugBreakAtSlot().
368   return Assembler::target_address_at(pc_, host_);
369 }
370
371
372 void RelocInfo::set_call_address(Address target) {
373   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
374          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
375   // The pc_ offset of 0 assumes mips patched return sequence per
376   // debug-mips.cc BreakLocation::SetDebugBreakAtReturn(), or
377   // debug break slot per BreakLocation::SetDebugBreakAtSlot().
378   Assembler::set_target_address_at(pc_, host_, target);
379   if (host() != NULL) {
380     Object* target_code = Code::GetCodeFromTargetAddress(target);
381     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
382         host(), this, HeapObject::cast(target_code));
383   }
384 }
385
386
387 Object* RelocInfo::call_object() {
388   return *call_object_address();
389 }
390
391
392 Object** RelocInfo::call_object_address() {
393   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
394          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
395   return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
396 }
397
398
399 void RelocInfo::set_call_object(Object* target) {
400   *call_object_address() = target;
401 }
402
403
404 void RelocInfo::WipeOut() {
405   DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
406          IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
407          IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
408   if (IsInternalReference(rmode_)) {
409     Memory::Address_at(pc_) = NULL;
410   } else if (IsInternalReferenceEncoded(rmode_)) {
411     Assembler::set_target_internal_reference_encoded_at(pc_, nullptr);
412   } else {
413     Assembler::set_target_address_at(pc_, host_, NULL);
414   }
415 }
416
417
418 bool RelocInfo::IsPatchedReturnSequence() {
419   Instr instr0 = Assembler::instr_at(pc_);
420   Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
421   Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize);
422   bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
423                          (instr1 & kOpcodeMask) == ORI &&
424                          ((instr2 & kOpcodeMask) == JAL ||
425                           ((instr2 & kOpcodeMask) == SPECIAL &&
426                            (instr2 & kFunctionFieldMask) == JALR)));
427   return patched_return;
428 }
429
430
431 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
432   Instr current_instr = Assembler::instr_at(pc_);
433   return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
434 }
435
436
437 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
438   RelocInfo::Mode mode = rmode();
439   if (mode == RelocInfo::EMBEDDED_OBJECT) {
440     visitor->VisitEmbeddedPointer(this);
441   } else if (RelocInfo::IsCodeTarget(mode)) {
442     visitor->VisitCodeTarget(this);
443   } else if (mode == RelocInfo::CELL) {
444     visitor->VisitCell(this);
445   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
446     visitor->VisitExternalReference(this);
447   } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
448              mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
449     visitor->VisitInternalReference(this);
450   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
451     visitor->VisitCodeAgeSequence(this);
452   } else if (((RelocInfo::IsJSReturn(mode) &&
453               IsPatchedReturnSequence()) ||
454              (RelocInfo::IsDebugBreakSlot(mode) &&
455              IsPatchedDebugBreakSlotSequence())) &&
456              isolate->debug()->has_break_points()) {
457     visitor->VisitDebugTarget(this);
458   } else if (RelocInfo::IsRuntimeEntry(mode)) {
459     visitor->VisitRuntimeEntry(this);
460   }
461 }
462
463
464 template<typename StaticVisitor>
465 void RelocInfo::Visit(Heap* heap) {
466   RelocInfo::Mode mode = rmode();
467   if (mode == RelocInfo::EMBEDDED_OBJECT) {
468     StaticVisitor::VisitEmbeddedPointer(heap, this);
469   } else if (RelocInfo::IsCodeTarget(mode)) {
470     StaticVisitor::VisitCodeTarget(heap, this);
471   } else if (mode == RelocInfo::CELL) {
472     StaticVisitor::VisitCell(heap, this);
473   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
474     StaticVisitor::VisitExternalReference(this);
475   } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
476              mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
477     StaticVisitor::VisitInternalReference(this);
478   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
479     StaticVisitor::VisitCodeAgeSequence(heap, this);
480   } else if (heap->isolate()->debug()->has_break_points() &&
481              ((RelocInfo::IsJSReturn(mode) &&
482               IsPatchedReturnSequence()) ||
483              (RelocInfo::IsDebugBreakSlot(mode) &&
484               IsPatchedDebugBreakSlotSequence()))) {
485     StaticVisitor::VisitDebugTarget(heap, this);
486   } else if (RelocInfo::IsRuntimeEntry(mode)) {
487     StaticVisitor::VisitRuntimeEntry(this);
488   }
489 }
490
491
492 // -----------------------------------------------------------------------------
493 // Assembler.
494
495
496 void Assembler::CheckBuffer() {
497   if (buffer_space() <= kGap) {
498     GrowBuffer();
499   }
500 }
501
502
503 void Assembler::CheckTrampolinePoolQuick() {
504   if (pc_offset() >= next_buffer_check_) {
505     CheckTrampolinePool();
506   }
507 }
508
509
510 void Assembler::emit(Instr x) {
511   if (!is_buffer_growth_blocked()) {
512     CheckBuffer();
513   }
514   *reinterpret_cast<Instr*>(pc_) = x;
515   pc_ += kInstrSize;
516   CheckTrampolinePoolQuick();
517 }
518
519
520 } }  // namespace v8::internal
521
522 #endif  // V8_MIPS_ASSEMBLER_MIPS_INL_H_