blob: f9e75face8a69c0670218890db6357d4748a8360 [file] [log] [blame]
Andrei Popescu31002712010-02-23 13:46:05 +00001// 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.
Ben Murdochc7cc0282012-03-05 14:35:55 +000033// Copyright 2012 the V8 project authors. All rights reserved.
Andrei Popescu31002712010-02-23 13:46:05 +000034
35
36#ifndef V8_MIPS_ASSEMBLER_MIPS_INL_H_
37#define V8_MIPS_ASSEMBLER_MIPS_INL_H_
38
39#include "mips/assembler-mips.h"
Ben Murdochc7cc0282012-03-05 14:35:55 +000040
Andrei Popescu31002712010-02-23 13:46:05 +000041#include "cpu.h"
Steve Block44f0eee2011-05-26 01:26:41 +010042#include "debug.h"
Andrei Popescu31002712010-02-23 13:46:05 +000043
44
45namespace v8 {
46namespace internal {
47
48// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +000049// Operand and MemOperand.
Andrei Popescu31002712010-02-23 13:46:05 +000050
51Operand::Operand(int32_t immediate, RelocInfo::Mode rmode) {
52 rm_ = no_reg;
53 imm32_ = immediate;
54 rmode_ = rmode;
55}
56
Steve Block44f0eee2011-05-26 01:26:41 +010057
Andrei Popescu31002712010-02-23 13:46:05 +000058Operand::Operand(const ExternalReference& f) {
59 rm_ = no_reg;
60 imm32_ = reinterpret_cast<int32_t>(f.address());
61 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
62}
63
Andrei Popescu31002712010-02-23 13:46:05 +000064
65Operand::Operand(Smi* value) {
66 rm_ = no_reg;
67 imm32_ = reinterpret_cast<intptr_t>(value);
68 rmode_ = RelocInfo::NONE;
69}
70
Steve Block44f0eee2011-05-26 01:26:41 +010071
Andrei Popescu31002712010-02-23 13:46:05 +000072Operand::Operand(Register rm) {
73 rm_ = rm;
74}
75
Steve Block44f0eee2011-05-26 01:26:41 +010076
Andrei Popescu31002712010-02-23 13:46:05 +000077bool Operand::is_reg() const {
78 return rm_.is_valid();
79}
80
81
Ben Murdochc7cc0282012-03-05 14:35:55 +000082int FPURegister::ToAllocationIndex(FPURegister reg) {
83 ASSERT(reg.code() % 2 == 0);
84 ASSERT(reg.code() / 2 < kNumAllocatableRegisters);
85 ASSERT(reg.is_valid());
86 ASSERT(!reg.is(kDoubleRegZero));
87 ASSERT(!reg.is(kLithiumScratchDouble));
88 return (reg.code() / 2);
89}
90
91
Andrei Popescu31002712010-02-23 13:46:05 +000092// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +000093// RelocInfo.
Andrei Popescu31002712010-02-23 13:46:05 +000094
95void RelocInfo::apply(intptr_t delta) {
Ben Murdoch589d6972011-11-30 16:04:58 +000096 if (IsCodeTarget(rmode_)) {
97 uint32_t scope1 = (uint32_t) target_address() & ~kImm28Mask;
98 uint32_t scope2 = reinterpret_cast<uint32_t>(pc_) & ~kImm28Mask;
99
100 if (scope1 != scope2) {
101 Assembler::JumpLabelToJumpRegister(pc_);
102 }
103 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000104 if (IsInternalReference(rmode_)) {
105 // Absolute code pointer inside code object moves with the code object.
106 byte* p = reinterpret_cast<byte*>(pc_);
107 int count = Assembler::RelocateInternalReference(p, delta);
108 CPU::FlushICache(p, count * sizeof(uint32_t));
109 }
Andrei Popescu31002712010-02-23 13:46:05 +0000110}
111
112
113Address RelocInfo::target_address() {
114 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
115 return Assembler::target_address_at(pc_);
116}
117
118
119Address RelocInfo::target_address_address() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000120 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
121 return reinterpret_cast<Address>(pc_);
Steve Block44f0eee2011-05-26 01:26:41 +0100122}
123
124
125int RelocInfo::target_address_size() {
126 return Assembler::kExternalTargetSize;
Andrei Popescu31002712010-02-23 13:46:05 +0000127}
128
129
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000130void RelocInfo::set_target_address(Address target, WriteBarrierMode mode) {
Andrei Popescu31002712010-02-23 13:46:05 +0000131 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
132 Assembler::set_target_address_at(pc_, target);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000133 if (mode == UPDATE_WRITE_BARRIER && host() != NULL && IsCodeTarget(rmode_)) {
134 Object* target_code = Code::GetCodeFromTargetAddress(target);
135 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
136 host(), this, HeapObject::cast(target_code));
137 }
Andrei Popescu31002712010-02-23 13:46:05 +0000138}
139
140
141Object* RelocInfo::target_object() {
142 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
143 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_));
144}
145
146
Ben Murdochc7cc0282012-03-05 14:35:55 +0000147Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
Andrei Popescu31002712010-02-23 13:46:05 +0000148 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
149 return Handle<Object>(reinterpret_cast<Object**>(
150 Assembler::target_address_at(pc_)));
151}
152
153
154Object** RelocInfo::target_object_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100155 // Provide a "natural pointer" to the embedded object,
156 // which can be de-referenced during heap iteration.
Andrei Popescu31002712010-02-23 13:46:05 +0000157 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000158 reconstructed_obj_ptr_ =
159 reinterpret_cast<Object*>(Assembler::target_address_at(pc_));
160 return &reconstructed_obj_ptr_;
Andrei Popescu31002712010-02-23 13:46:05 +0000161}
162
163
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000164void RelocInfo::set_target_object(Object* target, WriteBarrierMode mode) {
Andrei Popescu31002712010-02-23 13:46:05 +0000165 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
166 Assembler::set_target_address_at(pc_, reinterpret_cast<Address>(target));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000167 if (mode == UPDATE_WRITE_BARRIER &&
168 host() != NULL &&
169 target->IsHeapObject()) {
170 host()->GetHeap()->incremental_marking()->RecordWrite(
171 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
172 }
Andrei Popescu31002712010-02-23 13:46:05 +0000173}
174
175
176Address* RelocInfo::target_reference_address() {
177 ASSERT(rmode_ == EXTERNAL_REFERENCE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000178 reconstructed_adr_ptr_ = Assembler::target_address_at(pc_);
179 return &reconstructed_adr_ptr_;
Steve Block44f0eee2011-05-26 01:26:41 +0100180}
181
182
183Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
184 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
185 Address address = Memory::Address_at(pc_);
186 return Handle<JSGlobalPropertyCell>(
187 reinterpret_cast<JSGlobalPropertyCell**>(address));
188}
189
190
191JSGlobalPropertyCell* RelocInfo::target_cell() {
192 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
193 Address address = Memory::Address_at(pc_);
194 Object* object = HeapObject::FromAddress(
195 address - JSGlobalPropertyCell::kValueOffset);
196 return reinterpret_cast<JSGlobalPropertyCell*>(object);
197}
198
199
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000200void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell,
201 WriteBarrierMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100202 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
203 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
204 Memory::Address_at(pc_) = address;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000205 if (mode == UPDATE_WRITE_BARRIER && host() != NULL) {
206 // TODO(1550) We are passing NULL as a slot because cell can never be on
207 // evacuation candidate.
208 host()->GetHeap()->incremental_marking()->RecordWrite(
209 host(), NULL, cell);
210 }
Andrei Popescu31002712010-02-23 13:46:05 +0000211}
212
213
214Address RelocInfo::call_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100215 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
216 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
217 // The pc_ offset of 0 assumes mips patched return sequence per
218 // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
219 // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
220 return Assembler::target_address_at(pc_);
Andrei Popescu31002712010-02-23 13:46:05 +0000221}
222
223
224void RelocInfo::set_call_address(Address target) {
Steve Block44f0eee2011-05-26 01:26:41 +0100225 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
226 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
227 // The pc_ offset of 0 assumes mips patched return sequence per
228 // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
229 // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
230 Assembler::set_target_address_at(pc_, target);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000231 if (host() != NULL) {
232 Object* target_code = Code::GetCodeFromTargetAddress(target);
233 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
234 host(), this, HeapObject::cast(target_code));
235 }
Andrei Popescu31002712010-02-23 13:46:05 +0000236}
237
238
239Object* RelocInfo::call_object() {
240 return *call_object_address();
241}
242
243
244Object** RelocInfo::call_object_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100245 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
246 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Andrei Popescu31002712010-02-23 13:46:05 +0000247 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
248}
249
250
251void RelocInfo::set_call_object(Object* target) {
252 *call_object_address() = target;
253}
254
255
256bool RelocInfo::IsPatchedReturnSequence() {
Steve Block44f0eee2011-05-26 01:26:41 +0100257 Instr instr0 = Assembler::instr_at(pc_);
258 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
259 Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize);
260 bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
261 (instr1 & kOpcodeMask) == ORI &&
Ben Murdoch589d6972011-11-30 16:04:58 +0000262 ((instr2 & kOpcodeMask) == JAL ||
263 ((instr2 & kOpcodeMask) == SPECIAL &&
264 (instr2 & kFunctionFieldMask) == JALR)));
Steve Block44f0eee2011-05-26 01:26:41 +0100265 return patched_return;
266}
267
268
269bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
270 Instr current_instr = Assembler::instr_at(pc_);
271 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
272}
273
274
275void RelocInfo::Visit(ObjectVisitor* visitor) {
276 RelocInfo::Mode mode = rmode();
277 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000278 visitor->VisitEmbeddedPointer(this);
Steve Block44f0eee2011-05-26 01:26:41 +0100279 } else if (RelocInfo::IsCodeTarget(mode)) {
280 visitor->VisitCodeTarget(this);
Ben Murdoch257744e2011-11-30 15:57:28 +0000281 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
282 visitor->VisitGlobalPropertyCell(this);
Steve Block44f0eee2011-05-26 01:26:41 +0100283 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000284 visitor->VisitExternalReference(target_reference_address());
Steve Block44f0eee2011-05-26 01:26:41 +0100285#ifdef ENABLE_DEBUGGER_SUPPORT
286 // TODO(isolates): Get a cached isolate below.
287 } else if (((RelocInfo::IsJSReturn(mode) &&
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000288 IsPatchedReturnSequence()) ||
289 (RelocInfo::IsDebugBreakSlot(mode) &&
290 IsPatchedDebugBreakSlotSequence())) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100291 Isolate::Current()->debug()->has_break_points()) {
292 visitor->VisitDebugTarget(this);
Andrei Popescu31002712010-02-23 13:46:05 +0000293#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100294 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
295 visitor->VisitRuntimeEntry(this);
296 }
297}
298
299
300template<typename StaticVisitor>
301void RelocInfo::Visit(Heap* heap) {
302 RelocInfo::Mode mode = rmode();
303 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000304 StaticVisitor::VisitEmbeddedPointer(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100305 } else if (RelocInfo::IsCodeTarget(mode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000306 StaticVisitor::VisitCodeTarget(heap, this);
307 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
308 StaticVisitor::VisitGlobalPropertyCell(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100309 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
310 StaticVisitor::VisitExternalReference(target_reference_address());
311#ifdef ENABLE_DEBUGGER_SUPPORT
312 } else if (heap->isolate()->debug()->has_break_points() &&
313 ((RelocInfo::IsJSReturn(mode) &&
314 IsPatchedReturnSequence()) ||
315 (RelocInfo::IsDebugBreakSlot(mode) &&
316 IsPatchedDebugBreakSlotSequence()))) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000317 StaticVisitor::VisitDebugTarget(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100318#endif
319 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
320 StaticVisitor::VisitRuntimeEntry(this);
321 }
Andrei Popescu31002712010-02-23 13:46:05 +0000322}
323
324
325// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000326// Assembler.
Andrei Popescu31002712010-02-23 13:46:05 +0000327
328
329void Assembler::CheckBuffer() {
330 if (buffer_space() <= kGap) {
331 GrowBuffer();
332 }
333}
334
335
Steve Block44f0eee2011-05-26 01:26:41 +0100336void Assembler::CheckTrampolinePoolQuick() {
337 if (pc_offset() >= next_buffer_check_) {
338 CheckTrampolinePool();
339 }
340}
341
342
Andrei Popescu31002712010-02-23 13:46:05 +0000343void Assembler::emit(Instr x) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000344 if (!is_buffer_growth_blocked()) {
345 CheckBuffer();
346 }
Andrei Popescu31002712010-02-23 13:46:05 +0000347 *reinterpret_cast<Instr*>(pc_) = x;
348 pc_ += kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +0100349 CheckTrampolinePoolQuick();
Andrei Popescu31002712010-02-23 13:46:05 +0000350}
351
352
353} } // namespace v8::internal
354
355#endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_