blob: e787fed739048e24e888afbec02246a20273b0f9 [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) {
86 // On MIPS we do not use pc relative addressing, so we don't need to patch the
87 // code here.
88}
89
90
91Address RelocInfo::target_address() {
92 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
93 return Assembler::target_address_at(pc_);
94}
95
96
97Address RelocInfo::target_address_address() {
Ben Murdoch257744e2011-11-30 15:57:28 +000098 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
99 return reinterpret_cast<Address>(pc_);
Steve Block44f0eee2011-05-26 01:26:41 +0100100}
101
102
103int RelocInfo::target_address_size() {
104 return Assembler::kExternalTargetSize;
Andrei Popescu31002712010-02-23 13:46:05 +0000105}
106
107
108void RelocInfo::set_target_address(Address target) {
109 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
110 Assembler::set_target_address_at(pc_, target);
111}
112
113
114Object* RelocInfo::target_object() {
115 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
116 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_));
117}
118
119
120Handle<Object> RelocInfo::target_object_handle(Assembler *origin) {
121 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
122 return Handle<Object>(reinterpret_cast<Object**>(
123 Assembler::target_address_at(pc_)));
124}
125
126
127Object** RelocInfo::target_object_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100128 // Provide a "natural pointer" to the embedded object,
129 // which can be de-referenced during heap iteration.
Andrei Popescu31002712010-02-23 13:46:05 +0000130 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000131 reconstructed_obj_ptr_ =
132 reinterpret_cast<Object*>(Assembler::target_address_at(pc_));
133 return &reconstructed_obj_ptr_;
Andrei Popescu31002712010-02-23 13:46:05 +0000134}
135
136
137void RelocInfo::set_target_object(Object* target) {
138 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
139 Assembler::set_target_address_at(pc_, reinterpret_cast<Address>(target));
140}
141
142
143Address* RelocInfo::target_reference_address() {
144 ASSERT(rmode_ == EXTERNAL_REFERENCE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000145 reconstructed_adr_ptr_ = Assembler::target_address_at(pc_);
146 return &reconstructed_adr_ptr_;
Steve Block44f0eee2011-05-26 01:26:41 +0100147}
148
149
150Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
151 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
152 Address address = Memory::Address_at(pc_);
153 return Handle<JSGlobalPropertyCell>(
154 reinterpret_cast<JSGlobalPropertyCell**>(address));
155}
156
157
158JSGlobalPropertyCell* RelocInfo::target_cell() {
159 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
160 Address address = Memory::Address_at(pc_);
161 Object* object = HeapObject::FromAddress(
162 address - JSGlobalPropertyCell::kValueOffset);
163 return reinterpret_cast<JSGlobalPropertyCell*>(object);
164}
165
166
167void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell) {
168 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
169 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
170 Memory::Address_at(pc_) = address;
Andrei Popescu31002712010-02-23 13:46:05 +0000171}
172
173
174Address RelocInfo::call_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100175 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
176 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
177 // The pc_ offset of 0 assumes mips patched return sequence per
178 // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
179 // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
180 return Assembler::target_address_at(pc_);
Andrei Popescu31002712010-02-23 13:46:05 +0000181}
182
183
184void RelocInfo::set_call_address(Address target) {
Steve Block44f0eee2011-05-26 01:26:41 +0100185 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
186 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
187 // The pc_ offset of 0 assumes mips patched return sequence per
188 // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or
189 // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot().
190 Assembler::set_target_address_at(pc_, target);
Andrei Popescu31002712010-02-23 13:46:05 +0000191}
192
193
194Object* RelocInfo::call_object() {
195 return *call_object_address();
196}
197
198
199Object** RelocInfo::call_object_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100200 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
201 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Andrei Popescu31002712010-02-23 13:46:05 +0000202 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
203}
204
205
206void RelocInfo::set_call_object(Object* target) {
207 *call_object_address() = target;
208}
209
210
211bool RelocInfo::IsPatchedReturnSequence() {
Steve Block44f0eee2011-05-26 01:26:41 +0100212 Instr instr0 = Assembler::instr_at(pc_);
213 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
214 Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize);
215 bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
216 (instr1 & kOpcodeMask) == ORI &&
217 (instr2 & kOpcodeMask) == SPECIAL &&
218 (instr2 & kFunctionFieldMask) == JALR);
219 return patched_return;
220}
221
222
223bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
224 Instr current_instr = Assembler::instr_at(pc_);
225 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
226}
227
228
229void RelocInfo::Visit(ObjectVisitor* visitor) {
230 RelocInfo::Mode mode = rmode();
231 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000232 Object** p = target_object_address();
233 Object* orig = *p;
234 visitor->VisitPointer(p);
235 if (*p != orig) {
236 set_target_object(*p);
237 }
Steve Block44f0eee2011-05-26 01:26:41 +0100238 } else if (RelocInfo::IsCodeTarget(mode)) {
239 visitor->VisitCodeTarget(this);
Ben Murdoch257744e2011-11-30 15:57:28 +0000240 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
241 visitor->VisitGlobalPropertyCell(this);
Steve Block44f0eee2011-05-26 01:26:41 +0100242 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000243 visitor->VisitExternalReference(target_reference_address());
Steve Block44f0eee2011-05-26 01:26:41 +0100244#ifdef ENABLE_DEBUGGER_SUPPORT
245 // TODO(isolates): Get a cached isolate below.
246 } else if (((RelocInfo::IsJSReturn(mode) &&
247 IsPatchedReturnSequence()) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000248 (RelocInfo::IsDebugBreakSlot(mode) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100249 IsPatchedDebugBreakSlotSequence())) &&
250 Isolate::Current()->debug()->has_break_points()) {
251 visitor->VisitDebugTarget(this);
Andrei Popescu31002712010-02-23 13:46:05 +0000252#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100253 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
254 visitor->VisitRuntimeEntry(this);
255 }
256}
257
258
259template<typename StaticVisitor>
260void RelocInfo::Visit(Heap* heap) {
261 RelocInfo::Mode mode = rmode();
262 if (mode == RelocInfo::EMBEDDED_OBJECT) {
263 StaticVisitor::VisitPointer(heap, target_object_address());
264 } else if (RelocInfo::IsCodeTarget(mode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000265 StaticVisitor::VisitCodeTarget(heap, this);
266 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
267 StaticVisitor::VisitGlobalPropertyCell(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100268 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
269 StaticVisitor::VisitExternalReference(target_reference_address());
270#ifdef ENABLE_DEBUGGER_SUPPORT
271 } else if (heap->isolate()->debug()->has_break_points() &&
272 ((RelocInfo::IsJSReturn(mode) &&
273 IsPatchedReturnSequence()) ||
274 (RelocInfo::IsDebugBreakSlot(mode) &&
275 IsPatchedDebugBreakSlotSequence()))) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000276 StaticVisitor::VisitDebugTarget(heap, this);
Steve Block44f0eee2011-05-26 01:26:41 +0100277#endif
278 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
279 StaticVisitor::VisitRuntimeEntry(this);
280 }
Andrei Popescu31002712010-02-23 13:46:05 +0000281}
282
283
284// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000285// Assembler.
Andrei Popescu31002712010-02-23 13:46:05 +0000286
287
288void Assembler::CheckBuffer() {
289 if (buffer_space() <= kGap) {
290 GrowBuffer();
291 }
292}
293
294
Steve Block44f0eee2011-05-26 01:26:41 +0100295void Assembler::CheckTrampolinePoolQuick() {
296 if (pc_offset() >= next_buffer_check_) {
297 CheckTrampolinePool();
298 }
299}
300
301
Andrei Popescu31002712010-02-23 13:46:05 +0000302void Assembler::emit(Instr x) {
303 CheckBuffer();
304 *reinterpret_cast<Instr*>(pc_) = x;
305 pc_ += kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +0100306 CheckTrampolinePoolQuick();
Andrei Popescu31002712010-02-23 13:46:05 +0000307}
308
309
310} } // namespace v8::internal
311
312#endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_