blob: 5ad7b5abaec01cbe59cf3d9f8d4b170c8f4c974f [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +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
6// are 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
14// 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
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31// OF THE POSSIBILITY OF SUCH DAMAGE.
32
33// The original source code covered by the above license above has been modified
34// significantly by Google Inc.
Ben Murdoch692be652012-01-10 18:47:50 +000035// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37#ifndef V8_ARM_ASSEMBLER_ARM_INL_H_
38#define V8_ARM_ASSEMBLER_ARM_INL_H_
39
40#include "arm/assembler-arm.h"
41#include "cpu.h"
Leon Clarkef7060e22010-06-03 12:02:55 +010042#include "debug.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44
45namespace v8 {
46namespace internal {
47
Steve Blocka7e24c12009-10-30 11:49:00 +000048
Ben Murdoch692be652012-01-10 18:47:50 +000049int DwVfpRegister::ToAllocationIndex(DwVfpRegister reg) {
50 ASSERT(!reg.is(kDoubleRegZero));
51 ASSERT(!reg.is(kScratchDoubleReg));
52 return reg.code();
53}
54
55
Steve Blocka7e24c12009-10-30 11:49:00 +000056void RelocInfo::apply(intptr_t delta) {
57 if (RelocInfo::IsInternalReference(rmode_)) {
58 // absolute code pointer inside code object moves with the code object.
59 int32_t* p = reinterpret_cast<int32_t*>(pc_);
60 *p += delta; // relocate entry
61 }
62 // We do not use pc relative addressing on ARM, so there is
63 // nothing else to do.
64}
65
66
67Address RelocInfo::target_address() {
68 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
69 return Assembler::target_address_at(pc_);
70}
71
72
73Address RelocInfo::target_address_address() {
74 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
75 return reinterpret_cast<Address>(Assembler::target_address_address_at(pc_));
76}
77
78
Leon Clarkef7060e22010-06-03 12:02:55 +010079int RelocInfo::target_address_size() {
80 return Assembler::kExternalTargetSize;
81}
82
83
Steve Blocka7e24c12009-10-30 11:49:00 +000084void RelocInfo::set_target_address(Address target) {
85 ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
86 Assembler::set_target_address_at(pc_, target);
87}
88
89
90Object* RelocInfo::target_object() {
91 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Steve Block3ce2e202009-11-05 08:53:23 +000092 return Memory::Object_at(Assembler::target_address_address_at(pc_));
93}
94
95
Steve Blockd0582a62009-12-15 09:54:21 +000096Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
Steve Block3ce2e202009-11-05 08:53:23 +000097 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
98 return Memory::Object_Handle_at(Assembler::target_address_address_at(pc_));
Steve Blocka7e24c12009-10-30 11:49:00 +000099}
100
101
102Object** RelocInfo::target_object_address() {
103 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
104 return reinterpret_cast<Object**>(Assembler::target_address_address_at(pc_));
105}
106
107
108void RelocInfo::set_target_object(Object* target) {
109 ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
110 Assembler::set_target_address_at(pc_, reinterpret_cast<Address>(target));
111}
112
113
114Address* RelocInfo::target_reference_address() {
115 ASSERT(rmode_ == EXTERNAL_REFERENCE);
116 return reinterpret_cast<Address*>(Assembler::target_address_address_at(pc_));
117}
118
119
Ben Murdochb0fe1622011-05-05 13:52:32 +0100120Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
121 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
122 Address address = Memory::Address_at(pc_);
123 return Handle<JSGlobalPropertyCell>(
124 reinterpret_cast<JSGlobalPropertyCell**>(address));
125}
126
127
128JSGlobalPropertyCell* RelocInfo::target_cell() {
129 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
130 Address address = Memory::Address_at(pc_);
131 Object* object = HeapObject::FromAddress(
132 address - JSGlobalPropertyCell::kValueOffset);
133 return reinterpret_cast<JSGlobalPropertyCell*>(object);
134}
135
136
137void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell) {
138 ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
139 Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
140 Memory::Address_at(pc_) = address;
141}
142
143
Steve Blocka7e24c12009-10-30 11:49:00 +0000144Address RelocInfo::call_address() {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100145 // The 2 instructions offset assumes patched debug break slot or return
146 // sequence.
147 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
148 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 return Memory::Address_at(pc_ + 2 * Assembler::kInstrSize);
150}
151
152
153void RelocInfo::set_call_address(Address target) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100154 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
155 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 Memory::Address_at(pc_ + 2 * Assembler::kInstrSize) = target;
157}
158
159
160Object* RelocInfo::call_object() {
161 return *call_object_address();
162}
163
164
Ben Murdochbb769b22010-08-11 14:56:33 +0100165void RelocInfo::set_call_object(Object* target) {
166 *call_object_address() = target;
Steve Blocka7e24c12009-10-30 11:49:00 +0000167}
168
169
Ben Murdochbb769b22010-08-11 14:56:33 +0100170Object** RelocInfo::call_object_address() {
171 ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
172 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
173 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000174}
175
176
Steve Block3ce2e202009-11-05 08:53:23 +0000177bool RelocInfo::IsPatchedReturnSequence() {
Steve Block6ded16b2010-05-10 14:33:55 +0100178 Instr current_instr = Assembler::instr_at(pc_);
179 Instr next_instr = Assembler::instr_at(pc_ + Assembler::kInstrSize);
180#ifdef USE_BLX
181 // A patched return sequence is:
182 // ldr ip, [pc, #0]
183 // blx ip
184 return ((current_instr & kLdrPCMask) == kLdrPCPattern)
185 && ((next_instr & kBlxRegMask) == kBlxRegPattern);
186#else
187 // A patched return sequence is:
188 // mov lr, pc
189 // ldr pc, [pc, #-4]
190 return (current_instr == kMovLrPc)
191 && ((next_instr & kLdrPCMask) == kLdrPCPattern);
192#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000193}
194
195
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
197 Instr current_instr = Assembler::instr_at(pc_);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800198 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100199}
200
201
Leon Clarkef7060e22010-06-03 12:02:55 +0100202void RelocInfo::Visit(ObjectVisitor* visitor) {
203 RelocInfo::Mode mode = rmode();
204 if (mode == RelocInfo::EMBEDDED_OBJECT) {
205 visitor->VisitPointer(target_object_address());
206 } else if (RelocInfo::IsCodeTarget(mode)) {
207 visitor->VisitCodeTarget(this);
Steve Block1e0659c2011-05-24 12:43:12 +0100208 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
209 visitor->VisitGlobalPropertyCell(this);
Leon Clarkef7060e22010-06-03 12:02:55 +0100210 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
211 visitor->VisitExternalReference(target_reference_address());
212#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100213 // TODO(isolates): Get a cached isolate below.
214 } else if (((RelocInfo::IsJSReturn(mode) &&
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100215 IsPatchedReturnSequence()) ||
216 (RelocInfo::IsDebugBreakSlot(mode) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100217 IsPatchedDebugBreakSlotSequence())) &&
218 Isolate::Current()->debug()->has_break_points()) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100219 visitor->VisitDebugTarget(this);
220#endif
221 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
222 visitor->VisitRuntimeEntry(this);
223 }
224}
225
226
Iain Merrick75681382010-08-19 15:07:18 +0100227template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +0100228void RelocInfo::Visit(Heap* heap) {
Iain Merrick75681382010-08-19 15:07:18 +0100229 RelocInfo::Mode mode = rmode();
230 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Steve Block44f0eee2011-05-26 01:26:41 +0100231 StaticVisitor::VisitPointer(heap, target_object_address());
Iain Merrick75681382010-08-19 15:07:18 +0100232 } else if (RelocInfo::IsCodeTarget(mode)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100233 StaticVisitor::VisitCodeTarget(heap, this);
Steve Block1e0659c2011-05-24 12:43:12 +0100234 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100235 StaticVisitor::VisitGlobalPropertyCell(heap, this);
Iain Merrick75681382010-08-19 15:07:18 +0100236 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
237 StaticVisitor::VisitExternalReference(target_reference_address());
238#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100239 } else if (heap->isolate()->debug()->has_break_points() &&
Iain Merrick75681382010-08-19 15:07:18 +0100240 ((RelocInfo::IsJSReturn(mode) &&
241 IsPatchedReturnSequence()) ||
242 (RelocInfo::IsDebugBreakSlot(mode) &&
243 IsPatchedDebugBreakSlotSequence()))) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100244 StaticVisitor::VisitDebugTarget(heap, this);
Iain Merrick75681382010-08-19 15:07:18 +0100245#endif
246 } else if (mode == RelocInfo::RUNTIME_ENTRY) {
247 StaticVisitor::VisitRuntimeEntry(this);
248 }
249}
250
251
Steve Blocka7e24c12009-10-30 11:49:00 +0000252Operand::Operand(int32_t immediate, RelocInfo::Mode rmode) {
253 rm_ = no_reg;
254 imm32_ = immediate;
255 rmode_ = rmode;
256}
257
258
Steve Blocka7e24c12009-10-30 11:49:00 +0000259Operand::Operand(const ExternalReference& f) {
260 rm_ = no_reg;
261 imm32_ = reinterpret_cast<int32_t>(f.address());
262 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
263}
264
265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266Operand::Operand(Smi* value) {
267 rm_ = no_reg;
268 imm32_ = reinterpret_cast<intptr_t>(value);
269 rmode_ = RelocInfo::NONE;
270}
271
272
273Operand::Operand(Register rm) {
274 rm_ = rm;
275 rs_ = no_reg;
276 shift_op_ = LSL;
277 shift_imm_ = 0;
278}
279
280
281bool Operand::is_reg() const {
282 return rm_.is_valid() &&
283 rs_.is(no_reg) &&
284 shift_op_ == LSL &&
285 shift_imm_ == 0;
286}
287
288
289void Assembler::CheckBuffer() {
290 if (buffer_space() <= kGap) {
291 GrowBuffer();
292 }
293 if (pc_offset() >= next_buffer_check_) {
294 CheckConstPool(false, true);
295 }
296}
297
298
299void Assembler::emit(Instr x) {
300 CheckBuffer();
301 *reinterpret_cast<Instr*>(pc_) = x;
302 pc_ += kInstrSize;
303}
304
305
306Address Assembler::target_address_address_at(Address pc) {
Leon Clarkee46be812010-01-19 14:06:41 +0000307 Address target_pc = pc;
308 Instr instr = Memory::int32_at(target_pc);
309 // If we have a bx instruction, the instruction before the bx is
310 // what we need to patch.
311 static const int32_t kBxInstMask = 0x0ffffff0;
312 static const int32_t kBxInstPattern = 0x012fff10;
313 if ((instr & kBxInstMask) == kBxInstPattern) {
314 target_pc -= kInstrSize;
315 instr = Memory::int32_at(target_pc);
316 }
Steve Block6ded16b2010-05-10 14:33:55 +0100317
318#ifdef USE_BLX
319 // If we have a blx instruction, the instruction before it is
320 // what needs to be patched.
321 if ((instr & kBlxRegMask) == kBlxRegPattern) {
322 target_pc -= kInstrSize;
323 instr = Memory::int32_at(target_pc);
324 }
325#endif
326
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800327 ASSERT(IsLdrPcImmediateOffset(instr));
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 int offset = instr & 0xfff; // offset_12 is unsigned
329 if ((instr & (1 << 23)) == 0) offset = -offset; // U bit defines offset sign
330 // Verify that the constant pool comes after the instruction referencing it.
331 ASSERT(offset >= -4);
Leon Clarkee46be812010-01-19 14:06:41 +0000332 return target_pc + offset + 8;
Steve Blocka7e24c12009-10-30 11:49:00 +0000333}
334
335
336Address Assembler::target_address_at(Address pc) {
337 return Memory::Address_at(target_address_address_at(pc));
338}
339
340
Steve Blockd0582a62009-12-15 09:54:21 +0000341void Assembler::set_target_at(Address constant_pool_entry,
342 Address target) {
343 Memory::Address_at(constant_pool_entry) = target;
344}
345
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347void Assembler::set_target_address_at(Address pc, Address target) {
348 Memory::Address_at(target_address_address_at(pc)) = target;
349 // Intuitively, we would think it is necessary to flush the instruction cache
350 // after patching a target address in the code as follows:
351 // CPU::FlushICache(pc, sizeof(target));
352 // However, on ARM, no instruction was actually patched by the assignment
353 // above; the target address is not part of an instruction, it is patched in
354 // the constant pool and is read via a data access; the instruction accessing
355 // this address in the constant pool remains unchanged.
356}
357
358} } // namespace v8::internal
359
360#endif // V8_ARM_ASSEMBLER_ARM_INL_H_