blob: b5ffe7391b462d0be199191cfdc93ee5a3e71269 [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 Murdoch257744e2011-11-30 15:57:28 +000033// Copyright 2011 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"
40#include "cpu.h"
Steve Block44f0eee2011-05-26 01:26:41 +010041#include "debug.h"
Andrei Popescu31002712010-02-23 13:46:05 +000042
43
44namespace v8 {
45namespace internal {
46
47// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +000048// Operand and MemOperand.
Andrei Popescu31002712010-02-23 13:46:05 +000049
50Operand::Operand(int32_t immediate, RelocInfo::Mode rmode) {
51 rm_ = no_reg;
52 imm32_ = immediate;
53 rmode_ = rmode;
54}
55
Steve Block44f0eee2011-05-26 01:26:41 +010056
Andrei Popescu31002712010-02-23 13:46:05 +000057Operand::Operand(const ExternalReference& f) {
58 rm_ = no_reg;
59 imm32_ = reinterpret_cast<int32_t>(f.address());
60 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
61}
62
Andrei Popescu31002712010-02-23 13:46:05 +000063
64Operand::Operand(Smi* value) {
65 rm_ = no_reg;
66 imm32_ = reinterpret_cast<intptr_t>(value);
67 rmode_ = RelocInfo::NONE;
68}
69
Steve Block44f0eee2011-05-26 01:26:41 +010070
Andrei Popescu31002712010-02-23 13:46:05 +000071Operand::Operand(Register rm) {
72 rm_ = rm;
73}
74
Steve Block44f0eee2011-05-26 01:26:41 +010075
Andrei Popescu31002712010-02-23 13:46:05 +000076bool Operand::is_reg() const {
77 return rm_.is_valid();
78}
79
80
81
82// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +000083// RelocInfo.
Andrei Popescu31002712010-02-23 13:46:05 +000084
85void RelocInfo::apply(intptr_t delta) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000086 if (IsInternalReference(rmode_)) {
87 // Absolute code pointer inside code object moves with the code object.
88 byte* p = reinterpret_cast<byte*>(pc_);
89 int count = Assembler::RelocateInternalReference(p, delta);
90 CPU::FlushICache(p, count * sizeof(uint32_t));
91 }
Andrei Popescu31002712010-02-23 13:46:05 +000092}
93
94
95Address RelocInfo::target_address() {
96 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
97 return Assembler::target_address_at(pc_);
98}
99
100
101Address RelocInfo::target_address_address() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000102 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
103 return reinterpret_cast<Address>(pc_);
Steve Block44f0eee2011-05-26 01:26:41 +0100104}
105
106
107int RelocInfo::target_address_size() {
108 return Assembler::kExternalTargetSize;
Andrei Popescu31002712010-02-23 13:46:05 +0000109}
110
111
112void RelocInfo::set_target_address(Address target) {
113 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
114 Assembler::set_target_address_at(pc_, target);
115}
116
117
118Object* RelocInfo::target_object() {
119 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
120 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_));
121}
122
123
124Handle<Object> RelocInfo::target_object_handle(Assembler *origin) {
125 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
126 return Handle<Object>(reinterpret_cast<Object**>(
127 Assembler::target_address_at(pc_)));
128}
129
130
131Object** RelocInfo::target_object_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100132 // Provide a "natural pointer" to the embedded object,
133 // which can be de-referenced during heap iteration.
Andrei Popescu31002712010-02-23 13:46:05 +0000134 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000135 reconstructed_obj_ptr_ =
136 reinterpret_cast<Object*>(Assembler::target_address_at(pc_));
137 return &reconstructed_obj_ptr_;
Andrei Popescu31002712010-02-23 13:46:05 +0000138}
139
140
141void RelocInfo::set_target_object(Object* target) {
142 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
143 Assembler::set_target_address_at(pc_, reinterpret_cast<Address>(target));
144}
145
146
147Address* RelocInfo::target_reference_address() {
148 ASSERT(rmode_ == EXTERNAL_REFERENCE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000149 reconstructed_adr_ptr_ = Assembler::target_address_at(pc_);
150 return &reconstructed_adr_ptr_;
Steve Block44f0eee2011-05-26 01:26:41 +0100151}
152
153
154Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
155 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
156 Address address = Memory::Address_at(pc_);
157 return Handle<JSGlobalPropertyCell>(
158 reinterpret_cast<JSGlobalPropertyCell**>(address));
159}
160
161
162JSGlobalPropertyCell* RelocInfo::target_cell() {
163 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
164 Address address = Memory::Address_at(pc_);
165 Object* object = HeapObject::FromAddress(
166 address - JSGlobalPropertyCell::kValueOffset);
167 return reinterpret_cast<JSGlobalPropertyCell*>(object);
168}
169
170
171void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell) {
172 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
173 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
174 Memory::Address_at(pc_) = address;
Andrei Popescu31002712010-02-23 13:46:05 +0000175}
176
177
178Address RelocInfo::call_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100179 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
180 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
181 // The pc_ offset of 0 assumes mips patched return sequence per
182 // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
183 // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
184 return Assembler::target_address_at(pc_);
Andrei Popescu31002712010-02-23 13:46:05 +0000185}
186
187
188void RelocInfo::set_call_address(Address target) {
Steve Block44f0eee2011-05-26 01:26:41 +0100189 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
190 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
191 // The pc_ offset of 0 assumes mips patched return sequence per
192 // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
193 // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
194 Assembler::set_target_address_at(pc_, target);
Andrei Popescu31002712010-02-23 13:46:05 +0000195}
196
197
198Object* RelocInfo::call_object() {
199 return *call_object_address();
200}
201
202
203Object** RelocInfo::call_object_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100204 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
205 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Andrei Popescu31002712010-02-23 13:46:05 +0000206 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
207}
208
209
210void RelocInfo::set_call_object(Object* target) {
211 *call_object_address() = target;
212}
213
214
215bool RelocInfo::IsPatchedReturnSequence() {
Steve Block44f0eee2011-05-26 01:26:41 +0100216 Instr instr0 = Assembler::instr_at(pc_);
217 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
218 Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize);
219 bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
220 (instr1 & kOpcodeMask) == ORI &&
221 (instr2 & kOpcodeMask) == SPECIAL &&
222 (instr2 & kFunctionFieldMask) == JALR);
223 return patched_return;
224}
225
226
227bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
228 Instr current_instr = Assembler::instr_at(pc_);
229 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
230}
231
232
233void RelocInfo::Visit(ObjectVisitor* visitor) {
234 RelocInfo::Mode mode = rmode();
235 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000236 Object** p = target_object_address();
237 Object* orig = *p;
238 visitor->VisitPointer(p);
239 if (*p != orig) {
240 set_target_object(*p);
241 }
Steve Block44f0eee2011-05-26 01:26:41 +0100242 } else if (RelocInfo::IsCodeTarget(mode)) {
243 visitor->VisitCodeTarget(this);
Ben Murdoch257744e2011-11-30 15:57:28 +0000244 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
245 visitor->VisitGlobalPropertyCell(this);
Steve Block44f0eee2011-05-26 01:26:41 +0100246 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000247 visitor->VisitExternalReference(target_reference_address());
Steve Block44f0eee2011-05-26 01:26:41 +0100248#ifdef ENABLE_DEBUGGER_SUPPORT
249 // TODO(isolates): Get a cached isolate below.
250 } else if (((RelocInfo::IsJSReturn(mode) &&
251 IsPatchedReturnSequence()) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000252 (RelocInfo::IsDebugBreakSlot(mode) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100253 IsPatchedDebugBreakSlotSequence())) &&
254 Isolate::Current()->debug()->has_break_points()) {
255 visitor->VisitDebugTarget(this);
Andrei Popescu31002712010-02-23 13:46:05 +0000256#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100257 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
258 visitor->VisitRuntimeEntry(this);
259 }
260}
261
262
263template<typename StaticVisitor>
264void RelocInfo::Visit(Heap* heap) {
265 RelocInfo::Mode mode = rmode();
266 if (mode == RelocInfo::EMBEDDED_OBJECT) {
267 StaticVisitor::VisitPointer(heap, target_object_address());
268 } else if (RelocInfo::IsCodeTarget(mode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000269 StaticVisitor::VisitCodeTarget(heap, this);
270 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
271 StaticVisitor::VisitGlobalPropertyCell(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100272 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
273 StaticVisitor::VisitExternalReference(target_reference_address());
274#ifdef ENABLE_DEBUGGER_SUPPORT
275 } else if (heap->isolate()->debug()->has_break_points() &&
276 ((RelocInfo::IsJSReturn(mode) &&
277 IsPatchedReturnSequence()) ||
278 (RelocInfo::IsDebugBreakSlot(mode) &&
279 IsPatchedDebugBreakSlotSequence()))) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000280 StaticVisitor::VisitDebugTarget(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100281#endif
282 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
283 StaticVisitor::VisitRuntimeEntry(this);
284 }
Andrei Popescu31002712010-02-23 13:46:05 +0000285}
286
287
288// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000289// Assembler.
Andrei Popescu31002712010-02-23 13:46:05 +0000290
291
292void Assembler::CheckBuffer() {
293 if (buffer_space() <= kGap) {
294 GrowBuffer();
295 }
296}
297
298
Steve Block44f0eee2011-05-26 01:26:41 +0100299void Assembler::CheckTrampolinePoolQuick() {
300 if (pc_offset() >= next_buffer_check_) {
301 CheckTrampolinePool();
302 }
303}
304
305
Andrei Popescu31002712010-02-23 13:46:05 +0000306void Assembler::emit(Instr x) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000307 if (!is_buffer_growth_blocked()) {
308 CheckBuffer();
309 }
Andrei Popescu31002712010-02-23 13:46:05 +0000310 *reinterpret_cast<Instr*>(pc_) = x;
311 pc_ += kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +0100312 CheckTrampolinePoolQuick();
Andrei Popescu31002712010-02-23 13:46:05 +0000313}
314
315
316} } // namespace v8::internal
317
318#endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_