blob: a16f2002f994876a22990a066ad6dd83896ddb83 [file] [log] [blame]
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003//
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004// 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.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000019//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000022// 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
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033// The original source code covered by the above license above has been
34// modified significantly by Google Inc.
35// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37#include "v8.h"
38
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000039#if defined(V8_TARGET_ARCH_ARM)
40
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000041#include "arm/assembler-arm-inl.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000042#include "serialize.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
ager@chromium.orgc4c92722009-11-18 14:12:51 +000047// Safe default is no features.
48unsigned CpuFeatures::supported_ = 0;
49unsigned CpuFeatures::enabled_ = 0;
50unsigned CpuFeatures::found_by_runtime_probing_ = 0;
51
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000052
53#ifdef __arm__
54static uint64_t CpuFeaturesImpliedByCompiler() {
55 uint64_t answer = 0;
56#ifdef CAN_USE_ARMV7_INSTRUCTIONS
57 answer |= 1u << ARMv7;
58#endif // def CAN_USE_ARMV7_INSTRUCTIONS
59 // If the compiler is allowed to use VFP then we can use VFP too in our code
60 // generation even when generating snapshots. This won't work for cross
61 // compilation.
62#if defined(__VFP_FP__) && !defined(__SOFTFP__)
63 answer |= 1u << VFP3;
64#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
65#ifdef CAN_USE_VFP_INSTRUCTIONS
66 answer |= 1u << VFP3;
67#endif // def CAN_USE_VFP_INSTRUCTIONS
68 return answer;
69}
70#endif // def __arm__
71
72
ager@chromium.orgc4c92722009-11-18 14:12:51 +000073void CpuFeatures::Probe() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000074#ifndef __arm__
ager@chromium.org5c838252010-02-19 08:53:10 +000075 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled.
76 if (FLAG_enable_vfp3) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000077 supported_ |= 1u << VFP3;
ager@chromium.org5c838252010-02-19 08:53:10 +000078 }
79 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
80 if (FLAG_enable_armv7) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000081 supported_ |= 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +000082 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000083#else // def __arm__
ager@chromium.orgc4c92722009-11-18 14:12:51 +000084 if (Serializer::enabled()) {
85 supported_ |= OS::CpuFeaturesImpliedByPlatform();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000086 supported_ |= CpuFeaturesImpliedByCompiler();
ager@chromium.orgc4c92722009-11-18 14:12:51 +000087 return; // No features if we might serialize.
88 }
89
90 if (OS::ArmCpuHasFeature(VFP3)) {
91 // This implementation also sets the VFP flags if
92 // runtime detection of VFP returns true.
93 supported_ |= 1u << VFP3;
94 found_by_runtime_probing_ |= 1u << VFP3;
95 }
ager@chromium.org5c838252010-02-19 08:53:10 +000096
97 if (OS::ArmCpuHasFeature(ARMv7)) {
98 supported_ |= 1u << ARMv7;
99 found_by_runtime_probing_ |= 1u << ARMv7;
100 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000101#endif
102}
103
104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106// Implementation of RelocInfo
107
108const int RelocInfo::kApplyMask = 0;
109
110
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000111bool RelocInfo::IsCodedSpecially() {
112 // The deserializer needs to know whether a pointer is specially coded. Being
113 // specially coded on ARM means that it is a movw/movt instruction. We don't
114 // generate those yet.
115 return false;
116}
117
118
119
iposva@chromium.org245aa852009-02-10 00:49:54 +0000120void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000122 Instr* pc = reinterpret_cast<Instr*>(pc_);
123 Instr* instr = reinterpret_cast<Instr*>(instructions);
124 for (int i = 0; i < instruction_count; i++) {
125 *(pc + i) = *(instr + i);
126 }
127
128 // Indicate that code has changed.
129 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130}
131
132
133// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000134// Additional guard instructions can be added if required.
135void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 // Patch the code at the current address with a call to the target.
137 UNIMPLEMENTED();
138}
139
140
141// -----------------------------------------------------------------------------
142// Implementation of Operand and MemOperand
143// See assembler-arm-inl.h for inlined constructors
144
145Operand::Operand(Handle<Object> handle) {
146 rm_ = no_reg;
147 // Verify all Objects referred by code are NOT in new space.
148 Object* obj = *handle;
149 ASSERT(!Heap::InNewSpace(obj));
150 if (obj->IsHeapObject()) {
151 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000152 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 } else {
154 // no relocation needed
155 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000156 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 }
158}
159
160
161Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
162 ASSERT(is_uint5(shift_imm));
163 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
164 rm_ = rm;
165 rs_ = no_reg;
166 shift_op_ = shift_op;
167 shift_imm_ = shift_imm & 31;
168 if (shift_op == RRX) {
169 // encoded as ROR with shift_imm == 0
170 ASSERT(shift_imm == 0);
171 shift_op_ = ROR;
172 shift_imm_ = 0;
173 }
174}
175
176
177Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
178 ASSERT(shift_op != RRX);
179 rm_ = rm;
180 rs_ = no_reg;
181 shift_op_ = shift_op;
182 rs_ = rs;
183}
184
185
186MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
187 rn_ = rn;
188 rm_ = no_reg;
189 offset_ = offset;
190 am_ = am;
191}
192
193MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
194 rn_ = rn;
195 rm_ = rm;
196 shift_op_ = LSL;
197 shift_imm_ = 0;
198 am_ = am;
199}
200
201
202MemOperand::MemOperand(Register rn, Register rm,
203 ShiftOp shift_op, int shift_imm, AddrMode am) {
204 ASSERT(is_uint5(shift_imm));
205 rn_ = rn;
206 rm_ = rm;
207 shift_op_ = shift_op;
208 shift_imm_ = shift_imm & 31;
209 am_ = am;
210}
211
212
213// -----------------------------------------------------------------------------
ager@chromium.org5c838252010-02-19 08:53:10 +0000214// Implementation of Assembler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
ager@chromium.org5c838252010-02-19 08:53:10 +0000216// Instruction encoding bits.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217enum {
218 H = 1 << 5, // halfword (or byte)
219 S6 = 1 << 6, // signed (or unsigned)
220 L = 1 << 20, // load (or store)
221 S = 1 << 20, // set condition code (or leave unchanged)
222 W = 1 << 21, // writeback base register (or leave unchanged)
223 A = 1 << 21, // accumulate in multiply instruction (or not)
224 B = 1 << 22, // unsigned byte (or word)
225 N = 1 << 22, // long (or short)
226 U = 1 << 23, // positive (or negative) offset/index
227 P = 1 << 24, // offset/pre-indexed addressing (or post-indexed addressing)
228 I = 1 << 25, // immediate shifter operand (or not)
229
230 B4 = 1 << 4,
231 B5 = 1 << 5,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000232 B6 = 1 << 6,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 B7 = 1 << 7,
234 B8 = 1 << 8,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000235 B9 = 1 << 9,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 B12 = 1 << 12,
237 B16 = 1 << 16,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000238 B18 = 1 << 18,
239 B19 = 1 << 19,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 B20 = 1 << 20,
241 B21 = 1 << 21,
242 B22 = 1 << 22,
243 B23 = 1 << 23,
244 B24 = 1 << 24,
245 B25 = 1 << 25,
246 B26 = 1 << 26,
247 B27 = 1 << 27,
248
ager@chromium.org5c838252010-02-19 08:53:10 +0000249 // Instruction bit masks.
mads.s.ager31e71382008-08-13 09:32:07 +0000250 RdMask = 15 << 12, // in str instruction
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 CondMask = 15 << 28,
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000252 CoprocessorMask = 15 << 8,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 OpCodeMask = 15 << 21, // in data-processing instructions
254 Imm24Mask = (1 << 24) - 1,
255 Off12Mask = (1 << 12) - 1,
ager@chromium.org5c838252010-02-19 08:53:10 +0000256 // Reserved condition.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 nv = 15 << 28
258};
259
260
mads.s.ager31e71382008-08-13 09:32:07 +0000261// add(sp, sp, 4) instruction (aka Pop())
262static const Instr kPopInstruction =
263 al | 4 * B21 | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
264// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
265// register r is not encoded.
266static const Instr kPushRegPattern =
267 al | B26 | 4 | NegPreIndex | sp.code() * B16;
268// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
269// register r is not encoded.
270static const Instr kPopRegPattern =
271 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000272// mov lr, pc
273const Instr kMovLrPc = al | 13*B21 | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000274// ldr rd, [pc, #offset]
275const Instr kLdrPCMask = CondMask | 15 * B24 | 7 * B20 | 15 * B16;
276const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
277// blxcc rm
278const Instr kBlxRegMask =
279 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
280const Instr kBlxRegPattern =
281 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | 3 * B4;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000282const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
283const Instr kMovMvnPattern = 0xd * B21;
284const Instr kMovMvnFlip = B22;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000285const Instr kMovLeaveCCMask = 0xdff * B16;
286const Instr kMovLeaveCCPattern = 0x1a0 * B16;
287const Instr kMovwMask = 0xff * B20;
288const Instr kMovwPattern = 0x30 * B20;
289const Instr kMovwLeaveCCFlip = 0x5 * B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000290const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
291const Instr kCmpCmnPattern = 0x15 * B20;
292const Instr kCmpCmnFlip = B21;
293const Instr kALUMask = 0x6f * B21;
294const Instr kAddPattern = 0x4 * B21;
295const Instr kSubPattern = 0x2 * B21;
296const Instr kBicPattern = 0xe * B21;
297const Instr kAndPattern = 0x0 * B21;
298const Instr kAddSubFlip = 0x6 * B21;
299const Instr kAndBicFlip = 0xe * B21;
300
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000301// A mask for the Rd register for push, pop, ldr, str instructions.
302const Instr kRdMask = 0x0000f000;
303static const int kRdShift = 12;
304static const Instr kLdrRegFpOffsetPattern =
305 al | B26 | L | Offset | fp.code() * B16;
306static const Instr kStrRegFpOffsetPattern =
307 al | B26 | Offset | fp.code() * B16;
308static const Instr kLdrRegFpNegOffsetPattern =
309 al | B26 | L | NegOffset | fp.code() * B16;
310static const Instr kStrRegFpNegOffsetPattern =
311 al | B26 | NegOffset | fp.code() * B16;
312static const Instr kLdrStrInstrTypeMask = 0xffff0000;
313static const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
314static const Instr kLdrStrOffsetMask = 0x00000fff;
mads.s.ager31e71382008-08-13 09:32:07 +0000315
ager@chromium.org5c838252010-02-19 08:53:10 +0000316// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317static const int kMinimalBufferSize = 4*KB;
318static byte* spare_buffer_ = NULL;
319
320Assembler::Assembler(void* buffer, int buffer_size) {
321 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000322 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 if (buffer_size <= kMinimalBufferSize) {
324 buffer_size = kMinimalBufferSize;
325
326 if (spare_buffer_ != NULL) {
327 buffer = spare_buffer_;
328 spare_buffer_ = NULL;
329 }
330 }
331 if (buffer == NULL) {
332 buffer_ = NewArray<byte>(buffer_size);
333 } else {
334 buffer_ = static_cast<byte*>(buffer);
335 }
336 buffer_size_ = buffer_size;
337 own_buffer_ = true;
338
339 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000340 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 ASSERT(buffer_size > 0);
342 buffer_ = static_cast<byte*>(buffer);
343 buffer_size_ = buffer_size;
344 own_buffer_ = false;
345 }
346
ager@chromium.org5c838252010-02-19 08:53:10 +0000347 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 ASSERT(buffer_ != NULL);
349 pc_ = buffer_;
350 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
351 num_prinfo_ = 0;
352 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000353 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 no_const_pool_before_ = 0;
355 last_const_pool_end_ = 0;
356 last_bound_pos_ = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000357 current_statement_position_ = RelocInfo::kNoPosition;
358 current_position_ = RelocInfo::kNoPosition;
359 written_statement_position_ = current_statement_position_;
360 written_position_ = current_position_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361}
362
363
364Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000365 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 if (own_buffer_) {
367 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
368 spare_buffer_ = buffer_;
369 } else {
370 DeleteArray(buffer_);
371 }
372 }
373}
374
375
376void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000377 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 CheckConstPool(true, false);
379 ASSERT(num_prinfo_ == 0);
380
ager@chromium.org5c838252010-02-19 08:53:10 +0000381 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 desc->buffer = buffer_;
383 desc->buffer_size = buffer_size_;
384 desc->instr_size = pc_offset();
385 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
386}
387
388
389void Assembler::Align(int m) {
390 ASSERT(m >= 4 && IsPowerOf2(m));
391 while ((pc_offset() & (m - 1)) != 0) {
392 nop();
393 }
394}
395
396
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000397void Assembler::CodeTargetAlign() {
398 // Preferred alignment of jump targets on some ARM chips.
399 Align(8);
400}
401
402
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000403bool Assembler::IsNop(Instr instr, int type) {
404 // Check for mov rx, rx.
405 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
406 return instr == (al | 13*B21 | type*B12 | type);
407}
408
409
410bool Assembler::IsBranch(Instr instr) {
411 return (instr & (B27 | B25)) == (B27 | B25);
412}
413
414
415int Assembler::GetBranchOffset(Instr instr) {
416 ASSERT(IsBranch(instr));
417 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
418 // with 4 to get the offset in bytes.
419 return ((instr & Imm24Mask) << 8) >> 6;
420}
421
422
423bool Assembler::IsLdrRegisterImmediate(Instr instr) {
424 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
425}
426
427
428int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
429 ASSERT(IsLdrRegisterImmediate(instr));
430 bool positive = (instr & B23) == B23;
431 int offset = instr & Off12Mask; // Zero extended offset.
432 return positive ? offset : -offset;
433}
434
435
436Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
437 ASSERT(IsLdrRegisterImmediate(instr));
438 bool positive = offset >= 0;
439 if (!positive) offset = -offset;
440 ASSERT(is_uint12(offset));
441 // Set bit indicating whether the offset should be added.
442 instr = (instr & ~B23) | (positive ? B23 : 0);
443 // Set the actual offset.
444 return (instr & ~Off12Mask) | offset;
445}
446
447
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000448Register Assembler::GetRd(Instr instr) {
449 Register reg;
450 reg.code_ = ((instr & kRdMask) >> kRdShift);
451 return reg;
452}
453
454
455bool Assembler::IsPush(Instr instr) {
456 return ((instr & ~kRdMask) == kPushRegPattern);
457}
458
459
460bool Assembler::IsPop(Instr instr) {
461 return ((instr & ~kRdMask) == kPopRegPattern);
462}
463
464
465bool Assembler::IsStrRegFpOffset(Instr instr) {
466 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
467}
468
469
470bool Assembler::IsLdrRegFpOffset(Instr instr) {
471 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
472}
473
474
475bool Assembler::IsStrRegFpNegOffset(Instr instr) {
476 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
477}
478
479
480bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
481 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
482}
483
484
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485// Labels refer to positions in the (to be) generated code.
486// There are bound, linked, and unused labels.
487//
488// Bound labels refer to known positions in the already
489// generated code. pos() is the position the label refers to.
490//
491// Linked labels refer to unknown positions in the code
492// to be generated; pos() is the position of the last
493// instruction using the label.
494
495
496// The link chain is terminated by a negative code position (must be aligned)
497const int kEndOfChain = -4;
498
499
500int Assembler::target_at(int pos) {
501 Instr instr = instr_at(pos);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000502 if ((instr & ~Imm24Mask) == 0) {
503 // Emitted label constant, not part of a branch.
504 return instr - (Code::kHeaderSize - kHeapObjectTag);
505 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
507 int imm26 = ((instr & Imm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000508 if ((instr & CondMask) == nv && (instr & B24) != 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 // blx uses bit 24 to encode bit 2 of imm26
510 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000511 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000512 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513}
514
515
516void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 Instr instr = instr_at(pos);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000518 if ((instr & ~Imm24Mask) == 0) {
519 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
520 // Emitted label constant, not part of a branch.
521 // Make label relative to Code* of generated Code object.
522 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
523 return;
524 }
525 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
527 if ((instr & CondMask) == nv) {
528 // blx uses bit 24 to encode bit 2 of imm26
529 ASSERT((imm26 & 1) == 0);
530 instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24;
531 } else {
532 ASSERT((imm26 & 3) == 0);
533 instr &= ~Imm24Mask;
534 }
535 int imm24 = imm26 >> 2;
536 ASSERT(is_int24(imm24));
537 instr_at_put(pos, instr | (imm24 & Imm24Mask));
538}
539
540
541void Assembler::print(Label* L) {
542 if (L->is_unused()) {
543 PrintF("unused label\n");
544 } else if (L->is_bound()) {
545 PrintF("bound label to %d\n", L->pos());
546 } else if (L->is_linked()) {
547 Label l = *L;
548 PrintF("unbound label");
549 while (l.is_linked()) {
550 PrintF("@ %d ", l.pos());
551 Instr instr = instr_at(l.pos());
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000552 if ((instr & ~Imm24Mask) == 0) {
553 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000555 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
556 int cond = instr & CondMask;
557 const char* b;
558 const char* c;
559 if (cond == nv) {
560 b = "blx";
561 c = "";
562 } else {
563 if ((instr & B24) != 0)
564 b = "bl";
565 else
566 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000568 switch (cond) {
569 case eq: c = "eq"; break;
570 case ne: c = "ne"; break;
571 case hs: c = "hs"; break;
572 case lo: c = "lo"; break;
573 case mi: c = "mi"; break;
574 case pl: c = "pl"; break;
575 case vs: c = "vs"; break;
576 case vc: c = "vc"; break;
577 case hi: c = "hi"; break;
578 case ls: c = "ls"; break;
579 case ge: c = "ge"; break;
580 case lt: c = "lt"; break;
581 case gt: c = "gt"; break;
582 case le: c = "le"; break;
583 case al: c = ""; break;
584 default:
585 c = "";
586 UNREACHABLE();
587 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000589 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 next(&l);
592 }
593 } else {
594 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
595 }
596}
597
598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599void Assembler::bind_to(Label* L, int pos) {
600 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
601 while (L->is_linked()) {
602 int fixup_pos = L->pos();
603 next(L); // call next before overwriting link with target at fixup_pos
604 target_at_put(fixup_pos, pos);
605 }
606 L->bind_to(pos);
607
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000608 // Keep track of the last bound label so we don't eliminate any instructions
609 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 if (pos > last_bound_pos_)
611 last_bound_pos_ = pos;
612}
613
614
615void Assembler::link_to(Label* L, Label* appendix) {
616 if (appendix->is_linked()) {
617 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000618 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 int fixup_pos;
620 int link = L->pos();
621 do {
622 fixup_pos = link;
623 link = target_at(fixup_pos);
624 } while (link > 0);
625 ASSERT(link == kEndOfChain);
626 target_at_put(fixup_pos, appendix->pos());
627 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 *L = *appendix;
630 }
631 }
632 appendix->Unuse(); // appendix should not be used anymore
633}
634
635
636void Assembler::bind(Label* L) {
637 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 bind_to(L, pc_offset());
639}
640
641
642void Assembler::next(Label* L) {
643 ASSERT(L->is_linked());
644 int link = target_at(L->pos());
645 if (link > 0) {
646 L->link_to(link);
647 } else {
648 ASSERT(link == kEndOfChain);
649 L->Unuse();
650 }
651}
652
653
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000654static Instr EncodeMovwImmediate(uint32_t immediate) {
655 ASSERT(immediate < 0x10000);
656 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
657}
658
659
ager@chromium.org5c838252010-02-19 08:53:10 +0000660// Low-level code emission routines depending on the addressing mode.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000661// If this returns true then you have to use the rotate_imm and immed_8
662// that it returns, because it may have already changed the instruction
663// to match them!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664static bool fits_shifter(uint32_t imm32,
665 uint32_t* rotate_imm,
666 uint32_t* immed_8,
667 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000668 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 for (int rot = 0; rot < 16; rot++) {
670 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
671 if ((imm8 <= 0xff)) {
672 *rotate_imm = rot;
673 *immed_8 = imm8;
674 return true;
675 }
676 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000677 // If the opcode is one with a complementary version and the complementary
678 // immediate fits, change the opcode.
679 if (instr != NULL) {
680 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
681 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
682 *instr ^= kMovMvnFlip;
683 return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000684 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
685 if (CpuFeatures::IsSupported(ARMv7)) {
686 if (imm32 < 0x10000) {
687 *instr ^= kMovwLeaveCCFlip;
688 *instr |= EncodeMovwImmediate(imm32);
689 *rotate_imm = *immed_8 = 0; // Not used for movw.
690 return true;
691 }
692 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000693 }
694 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
695 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
696 *instr ^= kCmpCmnFlip;
697 return true;
698 }
699 } else {
700 Instr alu_insn = (*instr & kALUMask);
701 if (alu_insn == kAddPattern ||
702 alu_insn == kSubPattern) {
703 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
704 *instr ^= kAddSubFlip;
705 return true;
706 }
707 } else if (alu_insn == kAndPattern ||
708 alu_insn == kBicPattern) {
709 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
710 *instr ^= kAndBicFlip;
711 return true;
712 }
713 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 }
715 }
716 return false;
717}
718
719
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000720// We have to use the temporary register for things that can be relocated even
721// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
722// space. There is no guarantee that the relocated location can be similarly
723// encoded.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000724static bool MustUseConstantPool(RelocInfo::Mode rmode) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000725 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000726#ifdef DEBUG
727 if (!Serializer::enabled()) {
728 Serializer::TooLateToEnableNow();
729 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000730#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000731 return Serializer::enabled();
732 } else if (rmode == RelocInfo::NONE) {
733 return false;
734 }
735 return true;
736}
737
738
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000739bool Operand::is_single_instruction() const {
740 if (rm_.is_valid()) return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000741 if (MustUseConstantPool(rmode_)) return false;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000742 uint32_t dummy1, dummy2;
743 return fits_shifter(imm32_, &dummy1, &dummy2, NULL);
744}
745
746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747void Assembler::addrmod1(Instr instr,
748 Register rn,
749 Register rd,
750 const Operand& x) {
751 CheckBuffer();
752 ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0);
753 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000754 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 uint32_t rotate_imm;
756 uint32_t immed_8;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000757 if (MustUseConstantPool(x.rmode_) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
759 // The immediate operand cannot be encoded as a shifter operand, so load
760 // it first to register ip and change the original instruction to use ip.
761 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000762 // condition code), then replace it with a 'ldr rd, [pc]'.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000763 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 Condition cond = static_cast<Condition>(instr & CondMask);
765 if ((instr & ~CondMask) == 13*B21) { // mov, S not set
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000766 if (MustUseConstantPool(x.rmode_) ||
767 !CpuFeatures::IsSupported(ARMv7)) {
768 RecordRelocInfo(x.rmode_, x.imm32_);
769 ldr(rd, MemOperand(pc, 0), cond);
770 } else {
771 // Will probably use movw, will certainly not use constant pool.
772 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
773 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
774 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000776 // If this is not a mov or mvn instruction we may still be able to avoid
777 // a constant pool entry by using mvn or movw.
778 if (!MustUseConstantPool(x.rmode_) &&
779 (instr & kMovMvnMask) != kMovMvnPattern) {
780 mov(ip, x, LeaveCC, cond);
781 } else {
782 RecordRelocInfo(x.rmode_, x.imm32_);
783 ldr(ip, MemOperand(pc, 0), cond);
784 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 addrmod1(instr, rn, rd, Operand(ip));
786 }
787 return;
788 }
789 instr |= I | rotate_imm*B8 | immed_8;
790 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000791 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
793 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000794 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
796 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
797 }
798 emit(instr | rn.code()*B16 | rd.code()*B12);
799 if (rn.is(pc) || x.rm_.is(pc))
ager@chromium.org5c838252010-02-19 08:53:10 +0000800 // Block constant pool emission for one instruction after reading pc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 BlockConstPoolBefore(pc_offset() + kInstrSize);
802}
803
804
805void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
806 ASSERT((instr & ~(CondMask | B | L)) == B26);
807 int am = x.am_;
808 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000809 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 int offset_12 = x.offset_;
811 if (offset_12 < 0) {
812 offset_12 = -offset_12;
813 am ^= U;
814 }
815 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000816 // Immediate offset cannot be encoded, load it first to register ip
817 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
819 mov(ip, Operand(x.offset_), LeaveCC,
820 static_cast<Condition>(instr & CondMask));
821 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
822 return;
823 }
824 ASSERT(offset_12 >= 0); // no masking needed
825 instr |= offset_12;
826 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000827 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000829 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 ASSERT(!x.rm_.is(pc));
831 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
832 }
833 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
834 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
835}
836
837
838void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
839 ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7));
840 ASSERT(x.rn_.is_valid());
841 int am = x.am_;
842 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000843 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844 int offset_8 = x.offset_;
845 if (offset_8 < 0) {
846 offset_8 = -offset_8;
847 am ^= U;
848 }
849 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000850 // Immediate offset cannot be encoded, load it first to register ip
851 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
853 mov(ip, Operand(x.offset_), LeaveCC,
854 static_cast<Condition>(instr & CondMask));
855 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
856 return;
857 }
858 ASSERT(offset_8 >= 0); // no masking needed
859 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
860 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000861 // Scaled register offset not supported, load index first
862 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
864 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
865 static_cast<Condition>(instr & CondMask));
866 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
867 return;
868 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000869 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
871 instr |= x.rm_.code();
872 }
873 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
874 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
875}
876
877
878void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
879 ASSERT((instr & ~(CondMask | P | U | W | L)) == B27);
880 ASSERT(rl != 0);
881 ASSERT(!rn.is(pc));
882 emit(instr | rn.code()*B16 | rl);
883}
884
885
886void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000887 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000888 ASSERT_EQ((B27 | B26),
889 (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
891 int am = x.am_;
892 int offset_8 = x.offset_;
893 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
894 offset_8 >>= 2;
895 if (offset_8 < 0) {
896 offset_8 = -offset_8;
897 am ^= U;
898 }
899 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
900 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
901
ager@chromium.org5c838252010-02-19 08:53:10 +0000902 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 if ((am & P) == 0)
904 am |= W;
905
906 ASSERT(offset_8 >= 0); // no masking needed
907 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
908}
909
910
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000911int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 int target_pos;
913 if (L->is_bound()) {
914 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000916 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000918 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000920 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 L->link_to(pc_offset());
922 }
923
924 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +0000925 // be emitted at the pc offset recorded by the label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 BlockConstPoolBefore(pc_offset() + kInstrSize);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000927 return target_pos - (pc_offset() + kPcLoadDelta);
928}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000930
931void Assembler::label_at_put(Label* L, int at_offset) {
932 int target_pos;
933 if (L->is_bound()) {
934 target_pos = L->pos();
935 } else {
936 if (L->is_linked()) {
937 target_pos = L->pos(); // L's link
938 } else {
939 target_pos = kEndOfChain;
940 }
941 L->link_to(at_offset);
942 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
943 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944}
945
946
ager@chromium.org5c838252010-02-19 08:53:10 +0000947// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948void Assembler::b(int branch_offset, Condition cond) {
949 ASSERT((branch_offset & 3) == 0);
950 int imm24 = branch_offset >> 2;
951 ASSERT(is_int24(imm24));
952 emit(cond | B27 | B25 | (imm24 & Imm24Mask));
953
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000954 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000955 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958}
959
960
961void Assembler::bl(int branch_offset, Condition cond) {
962 ASSERT((branch_offset & 3) == 0);
963 int imm24 = branch_offset >> 2;
964 ASSERT(is_int24(imm24));
965 emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask));
966}
967
968
969void Assembler::blx(int branch_offset) { // v5 and above
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000970 WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 ASSERT((branch_offset & 1) == 0);
972 int h = ((branch_offset & 2) >> 1)*B24;
973 int imm24 = branch_offset >> 2;
974 ASSERT(is_int24(imm24));
975 emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask));
976}
977
978
979void Assembler::blx(Register target, Condition cond) { // v5 and above
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000980 WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 ASSERT(!target.is(pc));
982 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code());
983}
984
985
986void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000987 WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
989 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code());
990}
991
992
ager@chromium.org5c838252010-02-19 08:53:10 +0000993// Data-processing instructions.
994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995void Assembler::and_(Register dst, Register src1, const Operand& src2,
996 SBit s, Condition cond) {
997 addrmod1(cond | 0*B21 | s, src1, dst, src2);
998}
999
1000
1001void Assembler::eor(Register dst, Register src1, const Operand& src2,
1002 SBit s, Condition cond) {
1003 addrmod1(cond | 1*B21 | s, src1, dst, src2);
1004}
1005
1006
1007void Assembler::sub(Register dst, Register src1, const Operand& src2,
1008 SBit s, Condition cond) {
1009 addrmod1(cond | 2*B21 | s, src1, dst, src2);
1010}
1011
1012
1013void Assembler::rsb(Register dst, Register src1, const Operand& src2,
1014 SBit s, Condition cond) {
1015 addrmod1(cond | 3*B21 | s, src1, dst, src2);
1016}
1017
1018
1019void Assembler::add(Register dst, Register src1, const Operand& src2,
1020 SBit s, Condition cond) {
1021 addrmod1(cond | 4*B21 | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +00001022
1023 // Eliminate pattern: push(r), pop()
1024 // str(src, MemOperand(sp, 4, NegPreIndex), al);
1025 // add(sp, sp, Operand(kPointerSize));
1026 // Both instructions can be eliminated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001027 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001028 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001029 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
1030 (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) {
1031 pc_ -= 2 * kInstrSize;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001032 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001033 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
1034 }
1035 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036}
1037
1038
1039void Assembler::adc(Register dst, Register src1, const Operand& src2,
1040 SBit s, Condition cond) {
1041 addrmod1(cond | 5*B21 | s, src1, dst, src2);
1042}
1043
1044
1045void Assembler::sbc(Register dst, Register src1, const Operand& src2,
1046 SBit s, Condition cond) {
1047 addrmod1(cond | 6*B21 | s, src1, dst, src2);
1048}
1049
1050
1051void Assembler::rsc(Register dst, Register src1, const Operand& src2,
1052 SBit s, Condition cond) {
1053 addrmod1(cond | 7*B21 | s, src1, dst, src2);
1054}
1055
1056
1057void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
1058 addrmod1(cond | 8*B21 | S, src1, r0, src2);
1059}
1060
1061
1062void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
1063 addrmod1(cond | 9*B21 | S, src1, r0, src2);
1064}
1065
1066
1067void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
1068 addrmod1(cond | 10*B21 | S, src1, r0, src2);
1069}
1070
1071
1072void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
1073 addrmod1(cond | 11*B21 | S, src1, r0, src2);
1074}
1075
1076
1077void Assembler::orr(Register dst, Register src1, const Operand& src2,
1078 SBit s, Condition cond) {
1079 addrmod1(cond | 12*B21 | s, src1, dst, src2);
1080}
1081
1082
1083void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001084 if (dst.is(pc)) {
1085 WriteRecordedPositions();
1086 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001087 // Don't allow nop instructions in the form mov rn, rn to be generated using
1088 // the mov instruction. They must be generated using nop(int)
1089 // pseudo instructions.
1090 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 addrmod1(cond | 13*B21 | s, r0, dst, src);
1092}
1093
1094
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001095void Assembler::movw(Register reg, uint32_t immediate, Condition cond) {
1096 ASSERT(immediate < 0x10000);
1097 mov(reg, Operand(immediate), LeaveCC, cond);
1098}
1099
1100
1101void Assembler::movt(Register reg, uint32_t immediate, Condition cond) {
1102 emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate));
1103}
1104
1105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106void Assembler::bic(Register dst, Register src1, const Operand& src2,
1107 SBit s, Condition cond) {
1108 addrmod1(cond | 14*B21 | s, src1, dst, src2);
1109}
1110
1111
1112void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
1113 addrmod1(cond | 15*B21 | s, r0, dst, src);
1114}
1115
1116
ager@chromium.org5c838252010-02-19 08:53:10 +00001117// Multiply instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1119 SBit s, Condition cond) {
1120 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1122 src2.code()*B8 | B7 | B4 | src1.code());
1123}
1124
1125
1126void Assembler::mul(Register dst, Register src1, Register src2,
1127 SBit s, Condition cond) {
1128 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001129 // dst goes in bits 16-19 for this instruction!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1131}
1132
1133
1134void Assembler::smlal(Register dstL,
1135 Register dstH,
1136 Register src1,
1137 Register src2,
1138 SBit s,
1139 Condition cond) {
1140 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001141 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1143 src2.code()*B8 | B7 | B4 | src1.code());
1144}
1145
1146
1147void Assembler::smull(Register dstL,
1148 Register dstH,
1149 Register src1,
1150 Register src2,
1151 SBit s,
1152 Condition cond) {
1153 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001154 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1156 src2.code()*B8 | B7 | B4 | src1.code());
1157}
1158
1159
1160void Assembler::umlal(Register dstL,
1161 Register dstH,
1162 Register src1,
1163 Register src2,
1164 SBit s,
1165 Condition cond) {
1166 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001167 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1169 src2.code()*B8 | B7 | B4 | src1.code());
1170}
1171
1172
1173void Assembler::umull(Register dstL,
1174 Register dstH,
1175 Register src1,
1176 Register src2,
1177 SBit s,
1178 Condition cond) {
1179 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001180 ASSERT(!dstL.is(dstH));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001181 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 src2.code()*B8 | B7 | B4 | src1.code());
1183}
1184
1185
ager@chromium.org5c838252010-02-19 08:53:10 +00001186// Miscellaneous arithmetic instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187void Assembler::clz(Register dst, Register src, Condition cond) {
1188 // v5 and above.
1189 ASSERT(!dst.is(pc) && !src.is(pc));
1190 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
1191 15*B8 | B4 | src.code());
1192}
1193
1194
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001195// Saturating instructions.
1196
1197// Unsigned saturate.
1198void Assembler::usat(Register dst,
1199 int satpos,
1200 const Operand& src,
1201 Condition cond) {
1202 // v6 and above.
1203 ASSERT(CpuFeatures::IsSupported(ARMv7));
1204 ASSERT(!dst.is(pc) && !src.rm_.is(pc));
1205 ASSERT((satpos >= 0) && (satpos <= 31));
1206 ASSERT((src.shift_op_ == ASR) || (src.shift_op_ == LSL));
1207 ASSERT(src.rs_.is(no_reg));
1208
1209 int sh = 0;
1210 if (src.shift_op_ == ASR) {
1211 sh = 1;
1212 }
1213
1214 emit(cond | 0x6*B24 | 0xe*B20 | satpos*B16 | dst.code()*B12 |
1215 src.shift_imm_*B7 | sh*B6 | 0x1*B4 | src.rm_.code());
1216}
1217
1218
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001219// Bitfield manipulation instructions.
1220
1221// Unsigned bit field extract.
1222// Extracts #width adjacent bits from position #lsb in a register, and
1223// writes them to the low bits of a destination register.
1224// ubfx dst, src, #lsb, #width
1225void Assembler::ubfx(Register dst,
1226 Register src,
1227 int lsb,
1228 int width,
1229 Condition cond) {
1230 // v7 and above.
1231 ASSERT(CpuFeatures::IsSupported(ARMv7));
1232 ASSERT(!dst.is(pc) && !src.is(pc));
1233 ASSERT((lsb >= 0) && (lsb <= 31));
1234 ASSERT((width >= 1) && (width <= (32 - lsb)));
1235 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1236 lsb*B7 | B6 | B4 | src.code());
1237}
1238
1239
1240// Signed bit field extract.
1241// Extracts #width adjacent bits from position #lsb in a register, and
1242// writes them to the low bits of a destination register. The extracted
1243// value is sign extended to fill the destination register.
1244// sbfx dst, src, #lsb, #width
1245void Assembler::sbfx(Register dst,
1246 Register src,
1247 int lsb,
1248 int width,
1249 Condition cond) {
1250 // v7 and above.
1251 ASSERT(CpuFeatures::IsSupported(ARMv7));
1252 ASSERT(!dst.is(pc) && !src.is(pc));
1253 ASSERT((lsb >= 0) && (lsb <= 31));
1254 ASSERT((width >= 1) && (width <= (32 - lsb)));
1255 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1256 lsb*B7 | B6 | B4 | src.code());
1257}
1258
1259
1260// Bit field clear.
1261// Sets #width adjacent bits at position #lsb in the destination register
1262// to zero, preserving the value of the other bits.
1263// bfc dst, #lsb, #width
1264void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1265 // v7 and above.
1266 ASSERT(CpuFeatures::IsSupported(ARMv7));
1267 ASSERT(!dst.is(pc));
1268 ASSERT((lsb >= 0) && (lsb <= 31));
1269 ASSERT((width >= 1) && (width <= (32 - lsb)));
1270 int msb = lsb + width - 1;
1271 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1272}
1273
1274
1275// Bit field insert.
1276// Inserts #width adjacent bits from the low bits of the source register
1277// into position #lsb of the destination register.
1278// bfi dst, src, #lsb, #width
1279void Assembler::bfi(Register dst,
1280 Register src,
1281 int lsb,
1282 int width,
1283 Condition cond) {
1284 // v7 and above.
1285 ASSERT(CpuFeatures::IsSupported(ARMv7));
1286 ASSERT(!dst.is(pc) && !src.is(pc));
1287 ASSERT((lsb >= 0) && (lsb <= 31));
1288 ASSERT((width >= 1) && (width <= (32 - lsb)));
1289 int msb = lsb + width - 1;
1290 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1291 src.code());
1292}
1293
1294
ager@chromium.org5c838252010-02-19 08:53:10 +00001295// Status register access instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1297 ASSERT(!dst.is(pc));
1298 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1299}
1300
1301
1302void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1303 Condition cond) {
1304 ASSERT(fields >= B16 && fields < B20); // at least one field set
1305 Instr instr;
1306 if (!src.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001307 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 uint32_t rotate_imm;
1309 uint32_t immed_8;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001310 if (MustUseConstantPool(src.rmode_) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001312 // Immediate operand cannot be encoded, load it first to register ip.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 RecordRelocInfo(src.rmode_, src.imm32_);
1314 ldr(ip, MemOperand(pc, 0), cond);
1315 msr(fields, Operand(ip), cond);
1316 return;
1317 }
1318 instr = I | rotate_imm*B8 | immed_8;
1319 } else {
1320 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1321 instr = src.rm_.code();
1322 }
1323 emit(cond | instr | B24 | B21 | fields | 15*B12);
1324}
1325
1326
ager@chromium.org5c838252010-02-19 08:53:10 +00001327// Load/Store instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001329 if (dst.is(pc)) {
1330 WriteRecordedPositions();
1331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +00001333
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001334 // Eliminate pattern: push(ry), pop(rx)
1335 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1336 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1337 // Both instructions can be eliminated if ry = rx.
1338 // If ry != rx, a register copy from ry to rx is inserted
1339 // after eliminating the push and the pop instructions.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001340 if (can_peephole_optimize(2)) {
1341 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1342 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001343
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001344 if (IsPush(push_instr) && IsPop(pop_instr)) {
1345 if ((pop_instr & kRdMask) != (push_instr & kRdMask)) {
1346 // For consecutive push and pop on different registers,
1347 // we delete both the push & pop and insert a register move.
1348 // push ry, pop rx --> mov rx, ry
1349 Register reg_pushed, reg_popped;
1350 reg_pushed = GetRd(push_instr);
1351 reg_popped = GetRd(pop_instr);
1352 pc_ -= 2 * kInstrSize;
1353 // Insert a mov instruction, which is better than a pair of push & pop
1354 mov(reg_popped, reg_pushed);
1355 if (FLAG_print_peephole_optimization) {
1356 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1357 pc_offset());
1358 }
1359 } else {
1360 // For consecutive push and pop on the same register,
1361 // both the push and the pop can be deleted.
1362 pc_ -= 2 * kInstrSize;
1363 if (FLAG_print_peephole_optimization) {
1364 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1365 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001366 }
1367 }
1368 }
1369
1370 if (can_peephole_optimize(2)) {
1371 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1372 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1373
1374 if ((IsStrRegFpOffset(str_instr) &&
1375 IsLdrRegFpOffset(ldr_instr)) ||
1376 (IsStrRegFpNegOffset(str_instr) &&
1377 IsLdrRegFpNegOffset(ldr_instr))) {
1378 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1379 (str_instr & kLdrStrInstrArgumentMask)) {
1380 // Pattern: Ldr/str same fp+offset, same register.
1381 //
1382 // The following:
1383 // str rx, [fp, #-12]
1384 // ldr rx, [fp, #-12]
1385 //
1386 // Becomes:
1387 // str rx, [fp, #-12]
1388
1389 pc_ -= 1 * kInstrSize;
1390 if (FLAG_print_peephole_optimization) {
1391 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1392 }
1393 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1394 (str_instr & kLdrStrOffsetMask)) {
1395 // Pattern: Ldr/str same fp+offset, different register.
1396 //
1397 // The following:
1398 // str rx, [fp, #-12]
1399 // ldr ry, [fp, #-12]
1400 //
1401 // Becomes:
1402 // str rx, [fp, #-12]
1403 // mov ry, rx
1404
1405 Register reg_stored, reg_loaded;
1406 reg_stored = GetRd(str_instr);
1407 reg_loaded = GetRd(ldr_instr);
1408 pc_ -= 1 * kInstrSize;
1409 // Insert a mov instruction, which is better than ldr.
1410 mov(reg_loaded, reg_stored);
1411 if (FLAG_print_peephole_optimization) {
1412 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1413 }
1414 }
1415 }
1416 }
1417
1418 if (can_peephole_optimize(3)) {
1419 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1420 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1421 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1422 if (IsPush(mem_write_instr) &&
1423 IsPop(mem_read_instr)) {
1424 if ((IsLdrRegFpOffset(ldr_instr) ||
1425 IsLdrRegFpNegOffset(ldr_instr))) {
1426 if ((mem_write_instr & kRdMask) ==
1427 (mem_read_instr & kRdMask)) {
1428 // Pattern: push & pop from/to same register,
1429 // with a fp+offset ldr in between
1430 //
1431 // The following:
1432 // str rx, [sp, #-4]!
1433 // ldr rz, [fp, #-24]
1434 // ldr rx, [sp], #+4
1435 //
1436 // Becomes:
1437 // if(rx == rz)
1438 // delete all
1439 // else
1440 // ldr rz, [fp, #-24]
1441
1442 if ((mem_write_instr & kRdMask) == (ldr_instr & kRdMask)) {
1443 pc_ -= 3 * kInstrSize;
1444 } else {
1445 pc_ -= 3 * kInstrSize;
1446 // Reinsert back the ldr rz.
1447 emit(ldr_instr);
1448 }
1449 if (FLAG_print_peephole_optimization) {
1450 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1451 }
1452 } else {
1453 // Pattern: push & pop from/to different registers
1454 // with a fp+offset ldr in between
1455 //
1456 // The following:
1457 // str rx, [sp, #-4]!
1458 // ldr rz, [fp, #-24]
1459 // ldr ry, [sp], #+4
1460 //
1461 // Becomes:
1462 // if(ry == rz)
1463 // mov ry, rx;
1464 // else if(rx != rz)
1465 // ldr rz, [fp, #-24]
1466 // mov ry, rx
1467 // else if((ry != rz) || (rx == rz)) becomes:
1468 // mov ry, rx
1469 // ldr rz, [fp, #-24]
1470
1471 Register reg_pushed, reg_popped;
1472 if ((mem_read_instr & kRdMask) == (ldr_instr & kRdMask)) {
1473 reg_pushed = GetRd(mem_write_instr);
1474 reg_popped = GetRd(mem_read_instr);
1475 pc_ -= 3 * kInstrSize;
1476 mov(reg_popped, reg_pushed);
1477 } else if ((mem_write_instr & kRdMask)
1478 != (ldr_instr & kRdMask)) {
1479 reg_pushed = GetRd(mem_write_instr);
1480 reg_popped = GetRd(mem_read_instr);
1481 pc_ -= 3 * kInstrSize;
1482 emit(ldr_instr);
1483 mov(reg_popped, reg_pushed);
1484 } else if (((mem_read_instr & kRdMask)
1485 != (ldr_instr & kRdMask)) ||
1486 ((mem_write_instr & kRdMask)
1487 == (ldr_instr & kRdMask)) ) {
1488 reg_pushed = GetRd(mem_write_instr);
1489 reg_popped = GetRd(mem_read_instr);
1490 pc_ -= 3 * kInstrSize;
1491 mov(reg_popped, reg_pushed);
1492 emit(ldr_instr);
1493 }
1494 if (FLAG_print_peephole_optimization) {
1495 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1496 }
1497 }
1498 }
mads.s.ager31e71382008-08-13 09:32:07 +00001499 }
1500 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501}
1502
1503
1504void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1505 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +00001506
1507 // Eliminate pattern: pop(), push(r)
1508 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1509 // -> str r, [sp, 0], al
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001510 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001511 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001512 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1513 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1514 pc_ -= 2 * kInstrSize;
1515 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001516 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001517 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1518 }
1519 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520}
1521
1522
1523void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1524 addrmod2(cond | B26 | B | L, dst, src);
1525}
1526
1527
1528void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1529 addrmod2(cond | B26 | B, src, dst);
1530}
1531
1532
1533void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1534 addrmod3(cond | L | B7 | H | B4, dst, src);
1535}
1536
1537
1538void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1539 addrmod3(cond | B7 | H | B4, src, dst);
1540}
1541
1542
1543void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1544 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1545}
1546
1547
1548void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1549 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1550}
1551
1552
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001553void Assembler::ldrd(Register dst1, Register dst2,
1554 const MemOperand& src, Condition cond) {
1555 ASSERT(CpuFeatures::IsEnabled(ARMv7));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001556 ASSERT(src.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001557 ASSERT(!dst1.is(lr)); // r14.
1558 ASSERT_EQ(0, dst1.code() % 2);
1559 ASSERT_EQ(dst1.code() + 1, dst2.code());
1560 addrmod3(cond | B7 | B6 | B4, dst1, src);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001561}
1562
1563
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001564void Assembler::strd(Register src1, Register src2,
1565 const MemOperand& dst, Condition cond) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001566 ASSERT(dst.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001567 ASSERT(!src1.is(lr)); // r14.
1568 ASSERT_EQ(0, src1.code() % 2);
1569 ASSERT_EQ(src1.code() + 1, src2.code());
1570 ASSERT(CpuFeatures::IsEnabled(ARMv7));
1571 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001572}
1573
ager@chromium.org5c838252010-02-19 08:53:10 +00001574// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575void Assembler::ldm(BlockAddrMode am,
1576 Register base,
1577 RegList dst,
1578 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001579 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1581
1582 addrmod4(cond | B27 | am | L, base, dst);
1583
ager@chromium.org5c838252010-02-19 08:53:10 +00001584 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 if (cond == al && (dst & pc.bit()) != 0) {
1586 // There is a slight chance that the ldm instruction was actually a call,
1587 // in which case it would be wrong to return into the constant pool; we
1588 // recognize this case by checking if the emission of the pool was blocked
1589 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1590 // the case, we emit a jump over the pool.
1591 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1592 }
1593}
1594
1595
1596void Assembler::stm(BlockAddrMode am,
1597 Register base,
1598 RegList src,
1599 Condition cond) {
1600 addrmod4(cond | B27 | am, base, src);
1601}
1602
1603
ager@chromium.org5c838252010-02-19 08:53:10 +00001604// Exception-generating instructions and debugging support.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605void Assembler::stop(const char* msg) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001606#ifndef __arm__
kasper.lund7276f142008-07-30 08:49:36 +00001607 // The simulator handles these special instructions and stops execution.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 emit(15 << 28 | ((intptr_t) msg));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001609#else // def __arm__
1610#ifdef CAN_USE_ARMV5_INSTRUCTIONS
kasper.lund7276f142008-07-30 08:49:36 +00001611 bkpt(0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001612#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
1613 swi(0x9f0001);
1614#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1615#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616}
1617
1618
1619void Assembler::bkpt(uint32_t imm16) { // v5 and above
1620 ASSERT(is_uint16(imm16));
1621 emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf));
1622}
1623
1624
1625void Assembler::swi(uint32_t imm24, Condition cond) {
1626 ASSERT(is_uint24(imm24));
1627 emit(cond | 15*B24 | imm24);
1628}
1629
1630
ager@chromium.org5c838252010-02-19 08:53:10 +00001631// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632void Assembler::cdp(Coprocessor coproc,
1633 int opcode_1,
1634 CRegister crd,
1635 CRegister crn,
1636 CRegister crm,
1637 int opcode_2,
1638 Condition cond) {
1639 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1640 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1641 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1642}
1643
1644
1645void Assembler::cdp2(Coprocessor coproc,
1646 int opcode_1,
1647 CRegister crd,
1648 CRegister crn,
1649 CRegister crm,
1650 int opcode_2) { // v5 and above
1651 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv));
1652}
1653
1654
1655void Assembler::mcr(Coprocessor coproc,
1656 int opcode_1,
1657 Register rd,
1658 CRegister crn,
1659 CRegister crm,
1660 int opcode_2,
1661 Condition cond) {
1662 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1663 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1664 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1665}
1666
1667
1668void Assembler::mcr2(Coprocessor coproc,
1669 int opcode_1,
1670 Register rd,
1671 CRegister crn,
1672 CRegister crm,
1673 int opcode_2) { // v5 and above
1674 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1675}
1676
1677
1678void Assembler::mrc(Coprocessor coproc,
1679 int opcode_1,
1680 Register rd,
1681 CRegister crn,
1682 CRegister crm,
1683 int opcode_2,
1684 Condition cond) {
1685 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1686 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1687 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1688}
1689
1690
1691void Assembler::mrc2(Coprocessor coproc,
1692 int opcode_1,
1693 Register rd,
1694 CRegister crn,
1695 CRegister crm,
1696 int opcode_2) { // v5 and above
1697 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1698}
1699
1700
1701void Assembler::ldc(Coprocessor coproc,
1702 CRegister crd,
1703 const MemOperand& src,
1704 LFlag l,
1705 Condition cond) {
1706 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1707}
1708
1709
1710void Assembler::ldc(Coprocessor coproc,
1711 CRegister crd,
1712 Register rn,
1713 int option,
1714 LFlag l,
1715 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001716 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 ASSERT(is_uint8(option));
1718 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1719 coproc*B8 | (option & 255));
1720}
1721
1722
1723void Assembler::ldc2(Coprocessor coproc,
1724 CRegister crd,
1725 const MemOperand& src,
1726 LFlag l) { // v5 and above
1727 ldc(coproc, crd, src, l, static_cast<Condition>(nv));
1728}
1729
1730
1731void Assembler::ldc2(Coprocessor coproc,
1732 CRegister crd,
1733 Register rn,
1734 int option,
1735 LFlag l) { // v5 and above
1736 ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1737}
1738
1739
1740void Assembler::stc(Coprocessor coproc,
1741 CRegister crd,
1742 const MemOperand& dst,
1743 LFlag l,
1744 Condition cond) {
1745 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1746}
1747
1748
1749void Assembler::stc(Coprocessor coproc,
1750 CRegister crd,
1751 Register rn,
1752 int option,
1753 LFlag l,
1754 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001755 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 ASSERT(is_uint8(option));
1757 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1758 coproc*B8 | (option & 255));
1759}
1760
1761
1762void Assembler::stc2(Coprocessor
1763 coproc, CRegister crd,
1764 const MemOperand& dst,
1765 LFlag l) { // v5 and above
1766 stc(coproc, crd, dst, l, static_cast<Condition>(nv));
1767}
1768
1769
1770void Assembler::stc2(Coprocessor coproc,
1771 CRegister crd,
1772 Register rn,
1773 int option,
1774 LFlag l) { // v5 and above
1775 stc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1776}
1777
1778
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001779// Support for VFP.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001780void Assembler::vldr(const DwVfpRegister dst,
1781 const Register base,
1782 int offset,
1783 const Condition cond) {
1784 // Ddst = MEM(Rbase + offset).
1785 // Instruction details available in ARM DDI 0406A, A8-628.
1786 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1787 // Vdst(15-12) | 1011(11-8) | offset
1788 ASSERT(CpuFeatures::IsEnabled(VFP3));
1789 ASSERT(offset % 4 == 0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001790 ASSERT((offset / 4) < 256);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1792 0xB*B8 | ((offset / 4) & 255));
1793}
1794
1795
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001796void Assembler::vldr(const SwVfpRegister dst,
1797 const Register base,
1798 int offset,
1799 const Condition cond) {
1800 // Sdst = MEM(Rbase + offset).
1801 // Instruction details available in ARM DDI 0406A, A8-628.
1802 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1803 // Vdst(15-12) | 1010(11-8) | offset
1804 ASSERT(CpuFeatures::IsEnabled(VFP3));
1805 ASSERT(offset % 4 == 0);
1806 ASSERT((offset / 4) < 256);
1807 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1808 0xA*B8 | ((offset / 4) & 255));
1809}
1810
1811
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001812void Assembler::vstr(const DwVfpRegister src,
1813 const Register base,
1814 int offset,
1815 const Condition cond) {
1816 // MEM(Rbase + offset) = Dsrc.
1817 // Instruction details available in ARM DDI 0406A, A8-786.
1818 // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) |
1819 // Vsrc(15-12) | 1011(11-8) | (offset/4)
1820 ASSERT(CpuFeatures::IsEnabled(VFP3));
1821 ASSERT(offset % 4 == 0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001822 ASSERT((offset / 4) < 256);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001823 emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 |
1824 0xB*B8 | ((offset / 4) & 255));
1825}
1826
1827
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001828static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
1829 uint64_t i;
1830 memcpy(&i, &d, 8);
1831
1832 *lo = i & 0xffffffff;
1833 *hi = i >> 32;
1834}
1835
1836// Only works for little endian floating point formats.
1837// We don't support VFP on the mixed endian floating point platform.
1838static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
1839 ASSERT(CpuFeatures::IsEnabled(VFP3));
1840
1841 // VMOV can accept an immediate of the form:
1842 //
1843 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
1844 //
1845 // The immediate is encoded using an 8-bit quantity, comprised of two
1846 // 4-bit fields. For an 8-bit immediate of the form:
1847 //
1848 // [abcdefgh]
1849 //
1850 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
1851 // created of the form:
1852 //
1853 // [aBbbbbbb,bbcdefgh,00000000,00000000,
1854 // 00000000,00000000,00000000,00000000]
1855 //
1856 // where B = ~b.
1857 //
1858
1859 uint32_t lo, hi;
1860 DoubleAsTwoUInt32(d, &lo, &hi);
1861
1862 // The most obvious constraint is the long block of zeroes.
1863 if ((lo != 0) || ((hi & 0xffff) != 0)) {
1864 return false;
1865 }
1866
1867 // Bits 62:55 must be all clear or all set.
1868 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
1869 return false;
1870 }
1871
1872 // Bit 63 must be NOT bit 62.
1873 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
1874 return false;
1875 }
1876
1877 // Create the encoded immediate in the form:
1878 // [00000000,0000abcd,00000000,0000efgh]
1879 *encoding = (hi >> 16) & 0xf; // Low nybble.
1880 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
1881 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
1882
1883 return true;
1884}
1885
1886
1887void Assembler::vmov(const DwVfpRegister dst,
1888 double imm,
1889 const Condition cond) {
1890 // Dd = immediate
1891 // Instruction details available in ARM DDI 0406B, A8-640.
1892 ASSERT(CpuFeatures::IsEnabled(VFP3));
1893
1894 uint32_t enc;
1895 if (FitsVMOVDoubleImmediate(imm, &enc)) {
1896 // The double can be encoded in the instruction.
1897 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
1898 } else {
1899 // Synthesise the double from ARM immediates. This could be implemented
1900 // using vldr from a constant pool.
1901 uint32_t lo, hi;
1902 DoubleAsTwoUInt32(imm, &lo, &hi);
1903
1904 if (lo == hi) {
1905 // If the lo and hi parts of the double are equal, the literal is easier
1906 // to create. This is the case with 0.0.
1907 mov(ip, Operand(lo));
1908 vmov(dst, ip, ip);
1909 } else {
1910 // Move the low part of the double into the lower of the corresponsing S
1911 // registers of D register dst.
1912 mov(ip, Operand(lo));
1913 vmov(dst.low(), ip, cond);
1914
1915 // Move the high part of the double into the higher of the corresponsing S
1916 // registers of D register dst.
1917 mov(ip, Operand(hi));
1918 vmov(dst.high(), ip, cond);
1919 }
1920 }
1921}
1922
1923
1924void Assembler::vmov(const SwVfpRegister dst,
1925 const SwVfpRegister src,
1926 const Condition cond) {
1927 // Sd = Sm
1928 // Instruction details available in ARM DDI 0406B, A8-642.
1929 ASSERT(CpuFeatures::IsEnabled(VFP3));
1930 emit(cond | 0xE*B24 | 0xB*B20 |
1931 dst.code()*B12 | 0x5*B9 | B6 | src.code());
1932}
1933
1934
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001935void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001936 const DwVfpRegister src,
1937 const Condition cond) {
1938 // Dd = Dm
1939 // Instruction details available in ARM DDI 0406B, A8-642.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001940 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001941 emit(cond | 0xE*B24 | 0xB*B20 |
1942 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
1943}
1944
1945
1946void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001947 const Register src1,
1948 const Register src2,
1949 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001950 // Dm = <Rt,Rt2>.
1951 // Instruction details available in ARM DDI 0406A, A8-646.
1952 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
1953 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1954 ASSERT(CpuFeatures::IsEnabled(VFP3));
1955 ASSERT(!src1.is(pc) && !src2.is(pc));
1956 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
1957 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
1958}
1959
1960
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001961void Assembler::vmov(const Register dst1,
1962 const Register dst2,
1963 const DwVfpRegister src,
1964 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001965 // <Rt,Rt2> = Dm.
1966 // Instruction details available in ARM DDI 0406A, A8-646.
1967 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
1968 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1969 ASSERT(CpuFeatures::IsEnabled(VFP3));
1970 ASSERT(!dst1.is(pc) && !dst2.is(pc));
1971 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
1972 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
1973}
1974
1975
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001976void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001977 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001978 const Condition cond) {
1979 // Sn = Rt.
1980 // Instruction details available in ARM DDI 0406A, A8-642.
1981 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
1982 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1983 ASSERT(CpuFeatures::IsEnabled(VFP3));
1984 ASSERT(!src.is(pc));
1985 emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 |
1986 src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4);
1987}
1988
1989
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001990void Assembler::vmov(const Register dst,
1991 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001992 const Condition cond) {
1993 // Rt = Sn.
1994 // Instruction details available in ARM DDI 0406A, A8-642.
1995 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
1996 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1997 ASSERT(CpuFeatures::IsEnabled(VFP3));
1998 ASSERT(!dst.is(pc));
1999 emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 |
2000 dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4);
2001}
2002
2003
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002004// Type of data to read from or write to VFP register.
2005// Used as specifier in generic vcvt instruction.
2006enum VFPType { S32, U32, F32, F64 };
2007
2008
2009static bool IsSignedVFPType(VFPType type) {
2010 switch (type) {
2011 case S32:
2012 return true;
2013 case U32:
2014 return false;
2015 default:
2016 UNREACHABLE();
2017 return false;
2018 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002019}
2020
2021
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002022static bool IsIntegerVFPType(VFPType type) {
2023 switch (type) {
2024 case S32:
2025 case U32:
2026 return true;
2027 case F32:
2028 case F64:
2029 return false;
2030 default:
2031 UNREACHABLE();
2032 return false;
2033 }
2034}
2035
2036
2037static bool IsDoubleVFPType(VFPType type) {
2038 switch (type) {
2039 case F32:
2040 return false;
2041 case F64:
2042 return true;
2043 default:
2044 UNREACHABLE();
2045 return false;
2046 }
2047}
2048
2049
2050// Depending on split_last_bit split binary representation of reg_code into Vm:M
2051// or M:Vm form (where M is single bit).
2052static void SplitRegCode(bool split_last_bit,
2053 int reg_code,
2054 int* vm,
2055 int* m) {
2056 if (split_last_bit) {
2057 *m = reg_code & 0x1;
2058 *vm = reg_code >> 1;
2059 } else {
2060 *m = (reg_code & 0x10) >> 4;
2061 *vm = reg_code & 0x0F;
2062 }
2063}
2064
2065
2066// Encode vcvt.src_type.dst_type instruction.
2067static Instr EncodeVCVT(const VFPType dst_type,
2068 const int dst_code,
2069 const VFPType src_type,
2070 const int src_code,
2071 const Condition cond) {
2072 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2073 // Conversion between IEEE floating point and 32-bit integer.
2074 // Instruction details available in ARM DDI 0406B, A8.6.295.
2075 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2076 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2077 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2078
2079 int sz, opc2, D, Vd, M, Vm, op;
2080
2081 if (IsIntegerVFPType(dst_type)) {
2082 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2083 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
2084 op = 1; // round towards zero
2085 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
2086 SplitRegCode(true, dst_code, &Vd, &D);
2087 } else {
2088 ASSERT(IsIntegerVFPType(src_type));
2089
2090 opc2 = 0x0;
2091 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2092 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
2093 SplitRegCode(true, src_code, &Vm, &M);
2094 SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D);
2095 }
2096
2097 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2098 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2099 } else {
2100 // Conversion between IEEE double and single precision.
2101 // Instruction details available in ARM DDI 0406B, A8.6.298.
2102 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2103 // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2104 int sz, D, Vd, M, Vm;
2105
2106 ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type));
2107 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
2108 SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D);
2109 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
2110
2111 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2112 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2113 }
2114}
2115
2116
2117void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2118 const SwVfpRegister src,
2119 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002120 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002121 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond));
2122}
2123
2124
2125void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2126 const SwVfpRegister src,
2127 const Condition cond) {
2128 ASSERT(CpuFeatures::IsEnabled(VFP3));
2129 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond));
2130}
2131
2132
2133void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2134 const SwVfpRegister src,
2135 const Condition cond) {
2136 ASSERT(CpuFeatures::IsEnabled(VFP3));
2137 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond));
2138}
2139
2140
2141void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2142 const DwVfpRegister src,
2143 const Condition cond) {
2144 ASSERT(CpuFeatures::IsEnabled(VFP3));
2145 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond));
2146}
2147
2148
2149void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2150 const DwVfpRegister src,
2151 const Condition cond) {
2152 ASSERT(CpuFeatures::IsEnabled(VFP3));
2153 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond));
2154}
2155
2156
2157void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2158 const SwVfpRegister src,
2159 const Condition cond) {
2160 ASSERT(CpuFeatures::IsEnabled(VFP3));
2161 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond));
2162}
2163
2164
2165void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2166 const DwVfpRegister src,
2167 const Condition cond) {
2168 ASSERT(CpuFeatures::IsEnabled(VFP3));
2169 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002170}
2171
2172
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002173void Assembler::vadd(const DwVfpRegister dst,
2174 const DwVfpRegister src1,
2175 const DwVfpRegister src2,
2176 const Condition cond) {
2177 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002178 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2179 // Instruction details available in ARM DDI 0406A, A8-536.
2180 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2181 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2182 ASSERT(CpuFeatures::IsEnabled(VFP3));
2183 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2184 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2185}
2186
2187
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002188void Assembler::vsub(const DwVfpRegister dst,
2189 const DwVfpRegister src1,
2190 const DwVfpRegister src2,
2191 const Condition cond) {
2192 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002193 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2194 // Instruction details available in ARM DDI 0406A, A8-784.
2195 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2196 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
2197 ASSERT(CpuFeatures::IsEnabled(VFP3));
2198 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2199 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2200}
2201
2202
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002203void Assembler::vmul(const DwVfpRegister dst,
2204 const DwVfpRegister src1,
2205 const DwVfpRegister src2,
2206 const Condition cond) {
2207 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002208 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2209 // Instruction details available in ARM DDI 0406A, A8-784.
2210 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2211 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2212 ASSERT(CpuFeatures::IsEnabled(VFP3));
2213 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2214 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2215}
2216
2217
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002218void Assembler::vdiv(const DwVfpRegister dst,
2219 const DwVfpRegister src1,
2220 const DwVfpRegister src2,
2221 const Condition cond) {
2222 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002223 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2224 // Instruction details available in ARM DDI 0406A, A8-584.
2225 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2226 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2227 ASSERT(CpuFeatures::IsEnabled(VFP3));
2228 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2229 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2230}
2231
2232
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002233void Assembler::vcmp(const DwVfpRegister src1,
2234 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002235 const SBit s,
2236 const Condition cond) {
2237 // vcmp(Dd, Dm) double precision floating point comparison.
2238 // Instruction details available in ARM DDI 0406A, A8-570.
2239 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
2240 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0)
2241 ASSERT(CpuFeatures::IsEnabled(VFP3));
2242 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
2243 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2244}
2245
2246
2247void Assembler::vmrs(Register dst, Condition cond) {
2248 // Instruction details available in ARM DDI 0406A, A8-652.
2249 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2250 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
2251 ASSERT(CpuFeatures::IsEnabled(VFP3));
2252 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2253 dst.code()*B12 | 0xA*B8 | B4);
2254}
2255
2256
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002257
2258void Assembler::vsqrt(const DwVfpRegister dst,
2259 const DwVfpRegister src,
2260 const Condition cond) {
2261 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2262 // Vd(15-12) | 101(11-9) | sz(8)=1 | 11 (7-6) | M(5)=? | 0(4) | Vm(3-0)
2263 ASSERT(CpuFeatures::IsEnabled(VFP3));
2264 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2265 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2266}
2267
2268
ager@chromium.org5c838252010-02-19 08:53:10 +00002269// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002270void Assembler::nop(int type) {
2271 // This is mov rx, rx.
2272 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2273 emit(al | 13*B21 | type*B12 | type);
2274}
2275
2276
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002277bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2278 uint32_t dummy1;
2279 uint32_t dummy2;
2280 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2281}
2282
2283
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002284void Assembler::BlockConstPoolFor(int instructions) {
2285 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2286}
2287
2288
ager@chromium.org5c838252010-02-19 08:53:10 +00002289// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002290void Assembler::RecordJSReturn() {
2291 WriteRecordedPositions();
2292 CheckBuffer();
2293 RecordRelocInfo(RelocInfo::JS_RETURN);
2294}
2295
2296
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002297void Assembler::RecordDebugBreakSlot() {
2298 WriteRecordedPositions();
2299 CheckBuffer();
2300 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2301}
2302
2303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304void Assembler::RecordComment(const char* msg) {
2305 if (FLAG_debug_code) {
2306 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00002307 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 }
2309}
2310
2311
2312void Assembler::RecordPosition(int pos) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002313 if (pos == RelocInfo::kNoPosition) return;
2314 ASSERT(pos >= 0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002315 current_position_ = pos;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316}
2317
2318
2319void Assembler::RecordStatementPosition(int pos) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002320 if (pos == RelocInfo::kNoPosition) return;
2321 ASSERT(pos >= 0);
2322 current_statement_position_ = pos;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002323}
2324
2325
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002326bool Assembler::WriteRecordedPositions() {
2327 bool written = false;
2328
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002329 // Write the statement position if it is different from what was written last
2330 // time.
2331 if (current_statement_position_ != written_statement_position_) {
2332 CheckBuffer();
2333 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_);
2334 written_statement_position_ = current_statement_position_;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002335 written = true;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002336 }
2337
2338 // Write the position if it is different from what was written last time and
ager@chromium.org32912102009-01-16 10:38:43 +00002339 // also different from the written statement position.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002340 if (current_position_ != written_position_ &&
2341 current_position_ != written_statement_position_) {
2342 CheckBuffer();
2343 RecordRelocInfo(RelocInfo::POSITION, current_position_);
2344 written_position_ = current_position_;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002345 written = true;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002346 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002347
2348 // Return whether something was written.
2349 return written;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350}
2351
2352
2353void Assembler::GrowBuffer() {
2354 if (!own_buffer_) FATAL("external code buffer is too small");
2355
ager@chromium.org5c838252010-02-19 08:53:10 +00002356 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 CodeDesc desc; // the new buffer
2358 if (buffer_size_ < 4*KB) {
2359 desc.buffer_size = 4*KB;
2360 } else if (buffer_size_ < 1*MB) {
2361 desc.buffer_size = 2*buffer_size_;
2362 } else {
2363 desc.buffer_size = buffer_size_ + 1*MB;
2364 }
2365 CHECK_GT(desc.buffer_size, 0); // no overflow
2366
ager@chromium.org5c838252010-02-19 08:53:10 +00002367 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 desc.buffer = NewArray<byte>(desc.buffer_size);
2369
2370 desc.instr_size = pc_offset();
2371 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2372
ager@chromium.org5c838252010-02-19 08:53:10 +00002373 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 int pc_delta = desc.buffer - buffer_;
2375 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2376 memmove(desc.buffer, buffer_, desc.instr_size);
2377 memmove(reloc_info_writer.pos() + rc_delta,
2378 reloc_info_writer.pos(), desc.reloc_size);
2379
ager@chromium.org5c838252010-02-19 08:53:10 +00002380 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 DeleteArray(buffer_);
2382 buffer_ = desc.buffer;
2383 buffer_size_ = desc.buffer_size;
2384 pc_ += pc_delta;
2385 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2386 reloc_info_writer.last_pc() + pc_delta);
2387
ager@chromium.org5c838252010-02-19 08:53:10 +00002388 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00002390 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
ager@chromium.org5c838252010-02-19 08:53:10 +00002392 // Relocate pending relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 for (int i = 0; i < num_prinfo_; i++) {
2394 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002395 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2396 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00002397 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2398 rinfo.set_pc(rinfo.pc() + pc_delta);
2399 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 }
2401}
2402
2403
ager@chromium.org236ad962008-09-25 09:45:57 +00002404void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002406 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002407 // Adjust code for new modes.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002408 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2409 || RelocInfo::IsJSReturn(rmode)
ager@chromium.org4af710e2009-09-15 12:20:11 +00002410 || RelocInfo::IsComment(rmode)
2411 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00002412 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 } else {
2414 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2415 prinfo_[num_prinfo_++] = rinfo;
2416 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00002417 // instruction for which we just recorded relocation info.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 BlockConstPoolBefore(pc_offset() + kInstrSize);
2419 }
ager@chromium.org236ad962008-09-25 09:45:57 +00002420 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002421 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002422 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2423#ifdef DEBUG
2424 if (!Serializer::enabled()) {
2425 Serializer::TooLateToEnableNow();
2426 }
2427#endif
2428 if (!Serializer::enabled() && !FLAG_debug_code) {
2429 return;
2430 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002431 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2433 reloc_info_writer.Write(&rinfo);
2434 }
2435}
2436
2437
2438void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2439 // Calculate the offset of the next check. It will be overwritten
2440 // when a const pool is generated or when const pools are being
2441 // blocked for a specific range.
2442 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2443
ager@chromium.org5c838252010-02-19 08:53:10 +00002444 // There is nothing to do if there are no pending relocation info entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445 if (num_prinfo_ == 0) return;
2446
2447 // We emit a constant pool at regular intervals of about kDistBetweenPools
2448 // or when requested by parameter force_emit (e.g. after each function).
2449 // We prefer not to emit a jump unless the max distance is reached or if we
2450 // are running low on slots, which can happen if a lot of constants are being
2451 // emitted (e.g. --debug-code and many static references).
2452 int dist = pc_offset() - last_const_pool_end_;
2453 if (!force_emit && dist < kMaxDistBetweenPools &&
2454 (require_jump || dist < kDistBetweenPools) &&
2455 // TODO(1236125): Cleanup the "magic" number below. We know that
2456 // the code generation will test every kCheckConstIntervalInst.
2457 // Thus we are safe as long as we generate less than 7 constant
2458 // entries per instruction.
2459 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2460 return;
2461 }
2462
2463 // If we did not return by now, we need to emit the constant pool soon.
2464
2465 // However, some small sequences of instructions must not be broken up by the
2466 // insertion of a constant pool; such sequences are protected by setting
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002467 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2468 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2469 // no_const_pool_before_.
2470 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002471 // Emission is currently blocked; make sure we try again as soon as
2472 // possible.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002473 if (const_pool_blocked_nesting_ > 0) {
2474 next_buffer_check_ = pc_offset() + kInstrSize;
2475 } else {
2476 next_buffer_check_ = no_const_pool_before_;
2477 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478
ager@chromium.org5c838252010-02-19 08:53:10 +00002479 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 ASSERT(!force_emit);
2481 return;
2482 }
2483
2484 int jump_instr = require_jump ? kInstrSize : 0;
2485
2486 // Check that the code buffer is large enough before emitting the constant
2487 // pool and relocation information (include the jump over the pool and the
2488 // constant pool marker).
2489 int max_needed_space =
2490 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2491 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2492
ager@chromium.org5c838252010-02-19 08:53:10 +00002493 // Block recursive calls to CheckConstPool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2495 num_prinfo_*kInstrSize);
2496 // Don't bother to check for the emit calls below.
2497 next_buffer_check_ = no_const_pool_before_;
2498
ager@chromium.org5c838252010-02-19 08:53:10 +00002499 // Emit jump over constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500 Label after_pool;
2501 if (require_jump) b(&after_pool);
2502
2503 RecordComment("[ Constant Pool");
2504
ager@chromium.org5c838252010-02-19 08:53:10 +00002505 // Put down constant pool marker "Undefined instruction" as specified by
2506 // A3.1 Instruction set encoding.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 emit(0x03000000 | num_prinfo_);
2508
ager@chromium.org5c838252010-02-19 08:53:10 +00002509 // Emit constant pool entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 for (int i = 0; i < num_prinfo_; i++) {
2511 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002512 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2513 rinfo.rmode() != RelocInfo::POSITION &&
2514 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 Instr instr = instr_at(rinfo.pc());
ager@chromium.org4af710e2009-09-15 12:20:11 +00002516
ager@chromium.org5c838252010-02-19 08:53:10 +00002517 // Instruction to patch must be a ldr/str [pc, #offset].
2518 // P and U set, B and W clear, Rn == pc, offset12 still 0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) ==
2520 (2*B25 | P | U | pc.code()*B16));
2521 int delta = pc_ - rinfo.pc() - 8;
2522 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2523 if (delta < 0) {
2524 instr &= ~U;
2525 delta = -delta;
2526 }
2527 ASSERT(is_uint12(delta));
2528 instr_at_put(rinfo.pc(), instr + delta);
2529 emit(rinfo.data());
2530 }
2531 num_prinfo_ = 0;
2532 last_const_pool_end_ = pc_offset();
2533
2534 RecordComment("]");
2535
2536 if (after_pool.is_linked()) {
2537 bind(&after_pool);
2538 }
2539
2540 // Since a constant pool was just emitted, move the check offset forward by
2541 // the standard interval.
2542 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2543}
2544
2545
2546} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002547
2548#endif // V8_TARGET_ARCH_ARM