blob: ebf040d904dfaab6156b0d047dbb9a2a4f1a3ab7 [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
kasperl@chromium.orga5551262010-12-07 12:49:48 +000073void CpuFeatures::Probe(bool portable) {
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__
kasperl@chromium.orga5551262010-12-07 12:49:48 +000084 if (portable && Serializer::enabled()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000085 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 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000101
102 if (!portable) found_by_runtime_probing_ = 0;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000103#endif
104}
105
106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108// Implementation of RelocInfo
109
110const int RelocInfo::kApplyMask = 0;
111
112
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000113bool RelocInfo::IsCodedSpecially() {
114 // The deserializer needs to know whether a pointer is specially coded. Being
115 // specially coded on ARM means that it is a movw/movt instruction. We don't
116 // generate those yet.
117 return false;
118}
119
120
121
iposva@chromium.org245aa852009-02-10 00:49:54 +0000122void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000124 Instr* pc = reinterpret_cast<Instr*>(pc_);
125 Instr* instr = reinterpret_cast<Instr*>(instructions);
126 for (int i = 0; i < instruction_count; i++) {
127 *(pc + i) = *(instr + i);
128 }
129
130 // Indicate that code has changed.
131 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132}
133
134
135// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000136// Additional guard instructions can be added if required.
137void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 // Patch the code at the current address with a call to the target.
139 UNIMPLEMENTED();
140}
141
142
143// -----------------------------------------------------------------------------
144// Implementation of Operand and MemOperand
145// See assembler-arm-inl.h for inlined constructors
146
147Operand::Operand(Handle<Object> handle) {
148 rm_ = no_reg;
149 // Verify all Objects referred by code are NOT in new space.
150 Object* obj = *handle;
151 ASSERT(!Heap::InNewSpace(obj));
152 if (obj->IsHeapObject()) {
153 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000154 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 } else {
156 // no relocation needed
157 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000158 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 }
160}
161
162
163Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
164 ASSERT(is_uint5(shift_imm));
165 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
166 rm_ = rm;
167 rs_ = no_reg;
168 shift_op_ = shift_op;
169 shift_imm_ = shift_imm & 31;
170 if (shift_op == RRX) {
171 // encoded as ROR with shift_imm == 0
172 ASSERT(shift_imm == 0);
173 shift_op_ = ROR;
174 shift_imm_ = 0;
175 }
176}
177
178
179Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
180 ASSERT(shift_op != RRX);
181 rm_ = rm;
182 rs_ = no_reg;
183 shift_op_ = shift_op;
184 rs_ = rs;
185}
186
187
188MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
189 rn_ = rn;
190 rm_ = no_reg;
191 offset_ = offset;
192 am_ = am;
193}
194
195MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
196 rn_ = rn;
197 rm_ = rm;
198 shift_op_ = LSL;
199 shift_imm_ = 0;
200 am_ = am;
201}
202
203
204MemOperand::MemOperand(Register rn, Register rm,
205 ShiftOp shift_op, int shift_imm, AddrMode am) {
206 ASSERT(is_uint5(shift_imm));
207 rn_ = rn;
208 rm_ = rm;
209 shift_op_ = shift_op;
210 shift_imm_ = shift_imm & 31;
211 am_ = am;
212}
213
214
215// -----------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000216// Specific instructions, constants, and masks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
mads.s.ager31e71382008-08-13 09:32:07 +0000218// add(sp, sp, 4) instruction (aka Pop())
ager@chromium.org378b34e2011-01-28 08:04:38 +0000219const Instr kPopInstruction =
220 al | PostIndex | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
mads.s.ager31e71382008-08-13 09:32:07 +0000221// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
222// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000223const Instr kPushRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000224 al | B26 | 4 | NegPreIndex | sp.code() * B16;
225// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
226// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000227const Instr kPopRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000228 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000229// mov lr, pc
ager@chromium.org378b34e2011-01-28 08:04:38 +0000230const Instr kMovLrPc = al | MOV | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000231// ldr rd, [pc, #offset]
ager@chromium.org378b34e2011-01-28 08:04:38 +0000232const Instr kLdrPCMask = kCondMask | 15 * B24 | 7 * B20 | 15 * B16;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000233const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
234// blxcc rm
235const Instr kBlxRegMask =
236 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
237const Instr kBlxRegPattern =
ager@chromium.org378b34e2011-01-28 08:04:38 +0000238 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | BLX;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000239const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
240const Instr kMovMvnPattern = 0xd * B21;
241const Instr kMovMvnFlip = B22;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000242const Instr kMovLeaveCCMask = 0xdff * B16;
243const Instr kMovLeaveCCPattern = 0x1a0 * B16;
244const Instr kMovwMask = 0xff * B20;
245const Instr kMovwPattern = 0x30 * B20;
246const Instr kMovwLeaveCCFlip = 0x5 * B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000247const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
248const Instr kCmpCmnPattern = 0x15 * B20;
249const Instr kCmpCmnFlip = B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000250const Instr kAddSubFlip = 0x6 * B21;
251const Instr kAndBicFlip = 0xe * B21;
252
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000253// A mask for the Rd register for push, pop, ldr, str instructions.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000254const Instr kLdrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000255 al | B26 | L | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000256const Instr kStrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000257 al | B26 | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000258const Instr kLdrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000259 al | B26 | L | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000260const Instr kStrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000261 al | B26 | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000262const Instr kLdrStrInstrTypeMask = 0xffff0000;
263const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
264const Instr kLdrStrOffsetMask = 0x00000fff;
265
mads.s.ager31e71382008-08-13 09:32:07 +0000266
ager@chromium.org5c838252010-02-19 08:53:10 +0000267// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268static const int kMinimalBufferSize = 4*KB;
269static byte* spare_buffer_ = NULL;
270
ager@chromium.org378b34e2011-01-28 08:04:38 +0000271
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000272Assembler::Assembler(void* buffer, int buffer_size)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000273 : positions_recorder_(this),
274 allow_peephole_optimization_(false) {
275 // BUG(3245989): disable peephole optimization if crankshaft is enabled.
276 allow_peephole_optimization_ = FLAG_peephole_optimization;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000278 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 if (buffer_size <= kMinimalBufferSize) {
280 buffer_size = kMinimalBufferSize;
281
282 if (spare_buffer_ != NULL) {
283 buffer = spare_buffer_;
284 spare_buffer_ = NULL;
285 }
286 }
287 if (buffer == NULL) {
288 buffer_ = NewArray<byte>(buffer_size);
289 } else {
290 buffer_ = static_cast<byte*>(buffer);
291 }
292 buffer_size_ = buffer_size;
293 own_buffer_ = true;
294
295 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 ASSERT(buffer_size > 0);
298 buffer_ = static_cast<byte*>(buffer);
299 buffer_size_ = buffer_size;
300 own_buffer_ = false;
301 }
302
ager@chromium.org5c838252010-02-19 08:53:10 +0000303 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 ASSERT(buffer_ != NULL);
305 pc_ = buffer_;
306 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
307 num_prinfo_ = 0;
308 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000309 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 no_const_pool_before_ = 0;
311 last_const_pool_end_ = 0;
312 last_bound_pos_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313}
314
315
316Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000317 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 if (own_buffer_) {
319 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
320 spare_buffer_ = buffer_;
321 } else {
322 DeleteArray(buffer_);
323 }
324 }
325}
326
327
328void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000329 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 CheckConstPool(true, false);
331 ASSERT(num_prinfo_ == 0);
332
ager@chromium.org5c838252010-02-19 08:53:10 +0000333 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 desc->buffer = buffer_;
335 desc->buffer_size = buffer_size_;
336 desc->instr_size = pc_offset();
337 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
338}
339
340
341void Assembler::Align(int m) {
342 ASSERT(m >= 4 && IsPowerOf2(m));
343 while ((pc_offset() & (m - 1)) != 0) {
344 nop();
345 }
346}
347
348
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000349void Assembler::CodeTargetAlign() {
350 // Preferred alignment of jump targets on some ARM chips.
351 Align(8);
352}
353
354
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000355bool Assembler::IsBranch(Instr instr) {
356 return (instr & (B27 | B25)) == (B27 | B25);
357}
358
359
360int Assembler::GetBranchOffset(Instr instr) {
361 ASSERT(IsBranch(instr));
362 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
363 // with 4 to get the offset in bytes.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000364 return ((instr & kImm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000365}
366
367
368bool Assembler::IsLdrRegisterImmediate(Instr instr) {
369 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
370}
371
372
373int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
374 ASSERT(IsLdrRegisterImmediate(instr));
375 bool positive = (instr & B23) == B23;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000376 int offset = instr & kOff12Mask; // Zero extended offset.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000377 return positive ? offset : -offset;
378}
379
380
381Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
382 ASSERT(IsLdrRegisterImmediate(instr));
383 bool positive = offset >= 0;
384 if (!positive) offset = -offset;
385 ASSERT(is_uint12(offset));
386 // Set bit indicating whether the offset should be added.
387 instr = (instr & ~B23) | (positive ? B23 : 0);
388 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000389 return (instr & ~kOff12Mask) | offset;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000390}
391
392
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000393bool Assembler::IsStrRegisterImmediate(Instr instr) {
394 return (instr & (B27 | B26 | B25 | B22 | B20)) == B26;
395}
396
397
398Instr Assembler::SetStrRegisterImmediateOffset(Instr instr, int offset) {
399 ASSERT(IsStrRegisterImmediate(instr));
400 bool positive = offset >= 0;
401 if (!positive) offset = -offset;
402 ASSERT(is_uint12(offset));
403 // Set bit indicating whether the offset should be added.
404 instr = (instr & ~B23) | (positive ? B23 : 0);
405 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000406 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000407}
408
409
410bool Assembler::IsAddRegisterImmediate(Instr instr) {
411 return (instr & (B27 | B26 | B25 | B24 | B23 | B22 | B21)) == (B25 | B23);
412}
413
414
415Instr Assembler::SetAddRegisterImmediateOffset(Instr instr, int offset) {
416 ASSERT(IsAddRegisterImmediate(instr));
417 ASSERT(offset >= 0);
418 ASSERT(is_uint12(offset));
419 // Set the offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000420 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000421}
422
423
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000424Register Assembler::GetRd(Instr instr) {
425 Register reg;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000426 reg.code_ = Instruction::RdValue(instr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000427 return reg;
428}
429
430
431bool Assembler::IsPush(Instr instr) {
432 return ((instr & ~kRdMask) == kPushRegPattern);
433}
434
435
436bool Assembler::IsPop(Instr instr) {
437 return ((instr & ~kRdMask) == kPopRegPattern);
438}
439
440
441bool Assembler::IsStrRegFpOffset(Instr instr) {
442 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
443}
444
445
446bool Assembler::IsLdrRegFpOffset(Instr instr) {
447 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
448}
449
450
451bool Assembler::IsStrRegFpNegOffset(Instr instr) {
452 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
453}
454
455
456bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
457 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
458}
459
460
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000461bool Assembler::IsLdrPcImmediateOffset(Instr instr) {
462 // Check the instruction is indeed a
463 // ldr<cond> <Rd>, [pc +/- offset_12].
ager@chromium.org378b34e2011-01-28 08:04:38 +0000464 return (instr & (kLdrPCMask & ~kCondMask)) == 0x051f0000;
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000465}
466
467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468// Labels refer to positions in the (to be) generated code.
469// There are bound, linked, and unused labels.
470//
471// Bound labels refer to known positions in the already
472// generated code. pos() is the position the label refers to.
473//
474// Linked labels refer to unknown positions in the code
475// to be generated; pos() is the position of the last
476// instruction using the label.
477
478
479// The link chain is terminated by a negative code position (must be aligned)
480const int kEndOfChain = -4;
481
482
483int Assembler::target_at(int pos) {
484 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000485 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000486 // Emitted label constant, not part of a branch.
487 return instr - (Code::kHeaderSize - kHeapObjectTag);
488 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000490 int imm26 = ((instr & kImm24Mask) << 8) >> 6;
491 if ((Instruction::ConditionField(instr) == kSpecialCondition) &&
492 ((instr & B24) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 // blx uses bit 24 to encode bit 2 of imm26
494 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000495 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000496 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497}
498
499
500void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000502 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000503 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
504 // Emitted label constant, not part of a branch.
505 // Make label relative to Code* of generated Code object.
506 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
507 return;
508 }
509 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000511 if (Instruction::ConditionField(instr) == kSpecialCondition) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 // blx uses bit 24 to encode bit 2 of imm26
513 ASSERT((imm26 & 1) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000514 instr = (instr & ~(B24 | kImm24Mask)) | ((imm26 & 2) >> 1)*B24;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 } else {
516 ASSERT((imm26 & 3) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000517 instr &= ~kImm24Mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
519 int imm24 = imm26 >> 2;
520 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000521 instr_at_put(pos, instr | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522}
523
524
525void Assembler::print(Label* L) {
526 if (L->is_unused()) {
527 PrintF("unused label\n");
528 } else if (L->is_bound()) {
529 PrintF("bound label to %d\n", L->pos());
530 } else if (L->is_linked()) {
531 Label l = *L;
532 PrintF("unbound label");
533 while (l.is_linked()) {
534 PrintF("@ %d ", l.pos());
535 Instr instr = instr_at(l.pos());
ager@chromium.org378b34e2011-01-28 08:04:38 +0000536 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000537 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000539 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
ager@chromium.org378b34e2011-01-28 08:04:38 +0000540 Condition cond = Instruction::ConditionField(instr);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000541 const char* b;
542 const char* c;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000543 if (cond == kSpecialCondition) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000544 b = "blx";
545 c = "";
546 } else {
547 if ((instr & B24) != 0)
548 b = "bl";
549 else
550 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000552 switch (cond) {
553 case eq: c = "eq"; break;
554 case ne: c = "ne"; break;
555 case hs: c = "hs"; break;
556 case lo: c = "lo"; break;
557 case mi: c = "mi"; break;
558 case pl: c = "pl"; break;
559 case vs: c = "vs"; break;
560 case vc: c = "vc"; break;
561 case hi: c = "hi"; break;
562 case ls: c = "ls"; break;
563 case ge: c = "ge"; break;
564 case lt: c = "lt"; break;
565 case gt: c = "gt"; break;
566 case le: c = "le"; break;
567 case al: c = ""; break;
568 default:
569 c = "";
570 UNREACHABLE();
571 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000573 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 next(&l);
576 }
577 } else {
578 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
579 }
580}
581
582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583void Assembler::bind_to(Label* L, int pos) {
584 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
585 while (L->is_linked()) {
586 int fixup_pos = L->pos();
587 next(L); // call next before overwriting link with target at fixup_pos
588 target_at_put(fixup_pos, pos);
589 }
590 L->bind_to(pos);
591
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000592 // Keep track of the last bound label so we don't eliminate any instructions
593 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 if (pos > last_bound_pos_)
595 last_bound_pos_ = pos;
596}
597
598
599void Assembler::link_to(Label* L, Label* appendix) {
600 if (appendix->is_linked()) {
601 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 int fixup_pos;
604 int link = L->pos();
605 do {
606 fixup_pos = link;
607 link = target_at(fixup_pos);
608 } while (link > 0);
609 ASSERT(link == kEndOfChain);
610 target_at_put(fixup_pos, appendix->pos());
611 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000612 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 *L = *appendix;
614 }
615 }
616 appendix->Unuse(); // appendix should not be used anymore
617}
618
619
620void Assembler::bind(Label* L) {
621 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 bind_to(L, pc_offset());
623}
624
625
626void Assembler::next(Label* L) {
627 ASSERT(L->is_linked());
628 int link = target_at(L->pos());
629 if (link > 0) {
630 L->link_to(link);
631 } else {
632 ASSERT(link == kEndOfChain);
633 L->Unuse();
634 }
635}
636
637
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000638static Instr EncodeMovwImmediate(uint32_t immediate) {
639 ASSERT(immediate < 0x10000);
640 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
641}
642
643
ager@chromium.org5c838252010-02-19 08:53:10 +0000644// Low-level code emission routines depending on the addressing mode.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000645// If this returns true then you have to use the rotate_imm and immed_8
646// that it returns, because it may have already changed the instruction
647// to match them!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648static bool fits_shifter(uint32_t imm32,
649 uint32_t* rotate_imm,
650 uint32_t* immed_8,
651 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000652 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 for (int rot = 0; rot < 16; rot++) {
654 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
655 if ((imm8 <= 0xff)) {
656 *rotate_imm = rot;
657 *immed_8 = imm8;
658 return true;
659 }
660 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000661 // If the opcode is one with a complementary version and the complementary
662 // immediate fits, change the opcode.
663 if (instr != NULL) {
664 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
665 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
666 *instr ^= kMovMvnFlip;
667 return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000668 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
669 if (CpuFeatures::IsSupported(ARMv7)) {
670 if (imm32 < 0x10000) {
671 *instr ^= kMovwLeaveCCFlip;
672 *instr |= EncodeMovwImmediate(imm32);
673 *rotate_imm = *immed_8 = 0; // Not used for movw.
674 return true;
675 }
676 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000677 }
678 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
679 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
680 *instr ^= kCmpCmnFlip;
681 return true;
682 }
683 } else {
684 Instr alu_insn = (*instr & kALUMask);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000685 if (alu_insn == ADD ||
686 alu_insn == SUB) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000687 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
688 *instr ^= kAddSubFlip;
689 return true;
690 }
ager@chromium.org378b34e2011-01-28 08:04:38 +0000691 } else if (alu_insn == AND ||
692 alu_insn == BIC) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000693 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
694 *instr ^= kAndBicFlip;
695 return true;
696 }
697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 }
699 }
700 return false;
701}
702
703
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000704// We have to use the temporary register for things that can be relocated even
705// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
706// space. There is no guarantee that the relocated location can be similarly
707// encoded.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000708bool Operand::must_use_constant_pool() const {
709 if (rmode_ == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000710#ifdef DEBUG
711 if (!Serializer::enabled()) {
712 Serializer::TooLateToEnableNow();
713 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000714#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000715 return Serializer::enabled();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000716 } else if (rmode_ == RelocInfo::NONE) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000717 return false;
718 }
719 return true;
720}
721
722
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000723bool Operand::is_single_instruction() const {
724 if (rm_.is_valid()) return true;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000725 if (must_use_constant_pool()) return false;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000726 uint32_t dummy1, dummy2;
727 return fits_shifter(imm32_, &dummy1, &dummy2, NULL);
728}
729
730
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731void Assembler::addrmod1(Instr instr,
732 Register rn,
733 Register rd,
734 const Operand& x) {
735 CheckBuffer();
ager@chromium.org378b34e2011-01-28 08:04:38 +0000736 ASSERT((instr & ~(kCondMask | kOpCodeMask | S)) == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000738 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 uint32_t rotate_imm;
740 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000741 if (x.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
743 // The immediate operand cannot be encoded as a shifter operand, so load
744 // it first to register ip and change the original instruction to use ip.
745 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000746 // condition code), then replace it with a 'ldr rd, [pc]'.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000747 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
ager@chromium.org378b34e2011-01-28 08:04:38 +0000748 Condition cond = Instruction::ConditionField(instr);
749 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000750 if (x.must_use_constant_pool() || !CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000751 RecordRelocInfo(x.rmode_, x.imm32_);
752 ldr(rd, MemOperand(pc, 0), cond);
753 } else {
754 // Will probably use movw, will certainly not use constant pool.
755 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
756 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
757 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000759 // If this is not a mov or mvn instruction we may still be able to avoid
760 // a constant pool entry by using mvn or movw.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000761 if (!x.must_use_constant_pool() &&
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000762 (instr & kMovMvnMask) != kMovMvnPattern) {
763 mov(ip, x, LeaveCC, cond);
764 } else {
765 RecordRelocInfo(x.rmode_, x.imm32_);
766 ldr(ip, MemOperand(pc, 0), cond);
767 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768 addrmod1(instr, rn, rd, Operand(ip));
769 }
770 return;
771 }
772 instr |= I | rotate_imm*B8 | immed_8;
773 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000774 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
776 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000777 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
779 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
780 }
781 emit(instr | rn.code()*B16 | rd.code()*B12);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000782 if (rn.is(pc) || x.rm_.is(pc)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000783 // Block constant pool emission for one instruction after reading pc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 BlockConstPoolBefore(pc_offset() + kInstrSize);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000785 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786}
787
788
789void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000790 ASSERT((instr & ~(kCondMask | B | L)) == B26);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791 int am = x.am_;
792 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000793 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 int offset_12 = x.offset_;
795 if (offset_12 < 0) {
796 offset_12 = -offset_12;
797 am ^= U;
798 }
799 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000800 // Immediate offset cannot be encoded, load it first to register ip
801 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000803 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
805 return;
806 }
807 ASSERT(offset_12 >= 0); // no masking needed
808 instr |= offset_12;
809 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000810 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000812 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 ASSERT(!x.rm_.is(pc));
814 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
815 }
816 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
817 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
818}
819
820
821void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000822 ASSERT((instr & ~(kCondMask | L | S6 | H)) == (B4 | B7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 ASSERT(x.rn_.is_valid());
824 int am = x.am_;
825 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000826 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 int offset_8 = x.offset_;
828 if (offset_8 < 0) {
829 offset_8 = -offset_8;
830 am ^= U;
831 }
832 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000833 // Immediate offset cannot be encoded, load it first to register ip
834 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000836 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
838 return;
839 }
840 ASSERT(offset_8 >= 0); // no masking needed
841 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
842 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000843 // Scaled register offset not supported, load index first
844 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
846 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
ager@chromium.org378b34e2011-01-28 08:04:38 +0000847 Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
849 return;
850 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000851 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
853 instr |= x.rm_.code();
854 }
855 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
856 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
857}
858
859
860void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000861 ASSERT((instr & ~(kCondMask | P | U | W | L)) == B27);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 ASSERT(rl != 0);
863 ASSERT(!rn.is(pc));
864 emit(instr | rn.code()*B16 | rl);
865}
866
867
868void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000869 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000870 ASSERT_EQ((B27 | B26),
ager@chromium.org378b34e2011-01-28 08:04:38 +0000871 (instr & ~(kCondMask | kCoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
873 int am = x.am_;
874 int offset_8 = x.offset_;
875 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
876 offset_8 >>= 2;
877 if (offset_8 < 0) {
878 offset_8 = -offset_8;
879 am ^= U;
880 }
881 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
882 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
883
ager@chromium.org5c838252010-02-19 08:53:10 +0000884 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 if ((am & P) == 0)
886 am |= W;
887
888 ASSERT(offset_8 >= 0); // no masking needed
889 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
890}
891
892
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000893int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 int target_pos;
895 if (L->is_bound()) {
896 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000898 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000900 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000902 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 L->link_to(pc_offset());
904 }
905
906 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +0000907 // be emitted at the pc offset recorded by the label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 BlockConstPoolBefore(pc_offset() + kInstrSize);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000909 return target_pos - (pc_offset() + kPcLoadDelta);
910}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000912
913void Assembler::label_at_put(Label* L, int at_offset) {
914 int target_pos;
915 if (L->is_bound()) {
916 target_pos = L->pos();
917 } else {
918 if (L->is_linked()) {
919 target_pos = L->pos(); // L's link
920 } else {
921 target_pos = kEndOfChain;
922 }
923 L->link_to(at_offset);
924 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
925 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926}
927
928
ager@chromium.org5c838252010-02-19 08:53:10 +0000929// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930void Assembler::b(int branch_offset, Condition cond) {
931 ASSERT((branch_offset & 3) == 0);
932 int imm24 = branch_offset >> 2;
933 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000934 emit(cond | B27 | B25 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000936 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000937 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000939 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940}
941
942
943void Assembler::bl(int branch_offset, Condition cond) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000944 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 ASSERT((branch_offset & 3) == 0);
946 int imm24 = branch_offset >> 2;
947 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000948 emit(cond | B27 | B25 | B24 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949}
950
951
952void Assembler::blx(int branch_offset) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000953 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 ASSERT((branch_offset & 1) == 0);
955 int h = ((branch_offset & 2) >> 1)*B24;
956 int imm24 = branch_offset >> 2;
957 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000958 emit(kSpecialCondition | B27 | B25 | h | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959}
960
961
962void Assembler::blx(Register target, Condition cond) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000963 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 ASSERT(!target.is(pc));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000965 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BLX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966}
967
968
969void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000970 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
ager@chromium.org378b34e2011-01-28 08:04:38 +0000972 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973}
974
975
ager@chromium.org5c838252010-02-19 08:53:10 +0000976// Data-processing instructions.
977
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978void Assembler::and_(Register dst, Register src1, const Operand& src2,
979 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000980 addrmod1(cond | AND | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981}
982
983
984void Assembler::eor(Register dst, Register src1, const Operand& src2,
985 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000986 addrmod1(cond | EOR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987}
988
989
990void Assembler::sub(Register dst, Register src1, const Operand& src2,
991 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000992 addrmod1(cond | SUB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993}
994
995
996void Assembler::rsb(Register dst, Register src1, const Operand& src2,
997 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000998 addrmod1(cond | RSB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999}
1000
1001
1002void Assembler::add(Register dst, Register src1, const Operand& src2,
1003 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001004 addrmod1(cond | ADD | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +00001005
1006 // Eliminate pattern: push(r), pop()
1007 // str(src, MemOperand(sp, 4, NegPreIndex), al);
1008 // add(sp, sp, Operand(kPointerSize));
1009 // Both instructions can be eliminated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001010 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001011 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001012 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
ager@chromium.org378b34e2011-01-28 08:04:38 +00001013 (instr_at(pc_ - 2 * kInstrSize) & ~kRdMask) == kPushRegPattern) {
mads.s.ager31e71382008-08-13 09:32:07 +00001014 pc_ -= 2 * kInstrSize;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001015 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001016 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
1017 }
1018 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019}
1020
1021
1022void Assembler::adc(Register dst, Register src1, const Operand& src2,
1023 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001024 addrmod1(cond | ADC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025}
1026
1027
1028void Assembler::sbc(Register dst, Register src1, const Operand& src2,
1029 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001030 addrmod1(cond | SBC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031}
1032
1033
1034void Assembler::rsc(Register dst, Register src1, const Operand& src2,
1035 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001036 addrmod1(cond | RSC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037}
1038
1039
1040void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001041 addrmod1(cond | TST | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042}
1043
1044
1045void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001046 addrmod1(cond | TEQ | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047}
1048
1049
1050void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001051 addrmod1(cond | CMP | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052}
1053
1054
1055void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001056 addrmod1(cond | CMN | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057}
1058
1059
1060void Assembler::orr(Register dst, Register src1, const Operand& src2,
1061 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001062 addrmod1(cond | ORR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063}
1064
1065
1066void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001067 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001068 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001069 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001070 // Don't allow nop instructions in the form mov rn, rn to be generated using
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001071 // the mov instruction. They must be generated using nop(int/NopMarkerTypes)
1072 // or MarkCode(int/NopMarkerTypes) pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001073 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001074 addrmod1(cond | MOV | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075}
1076
1077
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001078void Assembler::movw(Register reg, uint32_t immediate, Condition cond) {
1079 ASSERT(immediate < 0x10000);
1080 mov(reg, Operand(immediate), LeaveCC, cond);
1081}
1082
1083
1084void Assembler::movt(Register reg, uint32_t immediate, Condition cond) {
1085 emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate));
1086}
1087
1088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089void Assembler::bic(Register dst, Register src1, const Operand& src2,
1090 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001091 addrmod1(cond | BIC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092}
1093
1094
1095void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001096 addrmod1(cond | MVN | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097}
1098
1099
ager@chromium.org5c838252010-02-19 08:53:10 +00001100// Multiply instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1102 SBit s, Condition cond) {
1103 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1105 src2.code()*B8 | B7 | B4 | src1.code());
1106}
1107
1108
1109void Assembler::mul(Register dst, Register src1, Register src2,
1110 SBit s, Condition cond) {
1111 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001112 // dst goes in bits 16-19 for this instruction!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1114}
1115
1116
1117void Assembler::smlal(Register dstL,
1118 Register dstH,
1119 Register src1,
1120 Register src2,
1121 SBit s,
1122 Condition cond) {
1123 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001124 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1126 src2.code()*B8 | B7 | B4 | src1.code());
1127}
1128
1129
1130void Assembler::smull(Register dstL,
1131 Register dstH,
1132 Register src1,
1133 Register src2,
1134 SBit s,
1135 Condition cond) {
1136 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001137 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1139 src2.code()*B8 | B7 | B4 | src1.code());
1140}
1141
1142
1143void Assembler::umlal(Register dstL,
1144 Register dstH,
1145 Register src1,
1146 Register src2,
1147 SBit s,
1148 Condition cond) {
1149 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001150 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1152 src2.code()*B8 | B7 | B4 | src1.code());
1153}
1154
1155
1156void Assembler::umull(Register dstL,
1157 Register dstH,
1158 Register src1,
1159 Register src2,
1160 SBit s,
1161 Condition cond) {
1162 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001163 ASSERT(!dstL.is(dstH));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001164 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 src2.code()*B8 | B7 | B4 | src1.code());
1166}
1167
1168
ager@chromium.org5c838252010-02-19 08:53:10 +00001169// Miscellaneous arithmetic instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170void Assembler::clz(Register dst, Register src, Condition cond) {
1171 // v5 and above.
1172 ASSERT(!dst.is(pc) && !src.is(pc));
1173 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
ager@chromium.org378b34e2011-01-28 08:04:38 +00001174 15*B8 | CLZ | src.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175}
1176
1177
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001178// Saturating instructions.
1179
1180// Unsigned saturate.
1181void Assembler::usat(Register dst,
1182 int satpos,
1183 const Operand& src,
1184 Condition cond) {
1185 // v6 and above.
1186 ASSERT(CpuFeatures::IsSupported(ARMv7));
1187 ASSERT(!dst.is(pc) && !src.rm_.is(pc));
1188 ASSERT((satpos >= 0) && (satpos <= 31));
1189 ASSERT((src.shift_op_ == ASR) || (src.shift_op_ == LSL));
1190 ASSERT(src.rs_.is(no_reg));
1191
1192 int sh = 0;
1193 if (src.shift_op_ == ASR) {
1194 sh = 1;
1195 }
1196
1197 emit(cond | 0x6*B24 | 0xe*B20 | satpos*B16 | dst.code()*B12 |
1198 src.shift_imm_*B7 | sh*B6 | 0x1*B4 | src.rm_.code());
1199}
1200
1201
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001202// Bitfield manipulation instructions.
1203
1204// Unsigned bit field extract.
1205// Extracts #width adjacent bits from position #lsb in a register, and
1206// writes them to the low bits of a destination register.
1207// ubfx dst, src, #lsb, #width
1208void Assembler::ubfx(Register dst,
1209 Register src,
1210 int lsb,
1211 int width,
1212 Condition cond) {
1213 // v7 and above.
1214 ASSERT(CpuFeatures::IsSupported(ARMv7));
1215 ASSERT(!dst.is(pc) && !src.is(pc));
1216 ASSERT((lsb >= 0) && (lsb <= 31));
1217 ASSERT((width >= 1) && (width <= (32 - lsb)));
1218 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1219 lsb*B7 | B6 | B4 | src.code());
1220}
1221
1222
1223// Signed bit field extract.
1224// Extracts #width adjacent bits from position #lsb in a register, and
1225// writes them to the low bits of a destination register. The extracted
1226// value is sign extended to fill the destination register.
1227// sbfx dst, src, #lsb, #width
1228void Assembler::sbfx(Register dst,
1229 Register src,
1230 int lsb,
1231 int width,
1232 Condition cond) {
1233 // v7 and above.
1234 ASSERT(CpuFeatures::IsSupported(ARMv7));
1235 ASSERT(!dst.is(pc) && !src.is(pc));
1236 ASSERT((lsb >= 0) && (lsb <= 31));
1237 ASSERT((width >= 1) && (width <= (32 - lsb)));
1238 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1239 lsb*B7 | B6 | B4 | src.code());
1240}
1241
1242
1243// Bit field clear.
1244// Sets #width adjacent bits at position #lsb in the destination register
1245// to zero, preserving the value of the other bits.
1246// bfc dst, #lsb, #width
1247void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1248 // v7 and above.
1249 ASSERT(CpuFeatures::IsSupported(ARMv7));
1250 ASSERT(!dst.is(pc));
1251 ASSERT((lsb >= 0) && (lsb <= 31));
1252 ASSERT((width >= 1) && (width <= (32 - lsb)));
1253 int msb = lsb + width - 1;
1254 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1255}
1256
1257
1258// Bit field insert.
1259// Inserts #width adjacent bits from the low bits of the source register
1260// into position #lsb of the destination register.
1261// bfi dst, src, #lsb, #width
1262void Assembler::bfi(Register dst,
1263 Register src,
1264 int lsb,
1265 int width,
1266 Condition cond) {
1267 // v7 and above.
1268 ASSERT(CpuFeatures::IsSupported(ARMv7));
1269 ASSERT(!dst.is(pc) && !src.is(pc));
1270 ASSERT((lsb >= 0) && (lsb <= 31));
1271 ASSERT((width >= 1) && (width <= (32 - lsb)));
1272 int msb = lsb + width - 1;
1273 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1274 src.code());
1275}
1276
1277
ager@chromium.org5c838252010-02-19 08:53:10 +00001278// Status register access instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1280 ASSERT(!dst.is(pc));
1281 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1282}
1283
1284
1285void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1286 Condition cond) {
1287 ASSERT(fields >= B16 && fields < B20); // at least one field set
1288 Instr instr;
1289 if (!src.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001290 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 uint32_t rotate_imm;
1292 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001293 if (src.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001295 // Immediate operand cannot be encoded, load it first to register ip.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 RecordRelocInfo(src.rmode_, src.imm32_);
1297 ldr(ip, MemOperand(pc, 0), cond);
1298 msr(fields, Operand(ip), cond);
1299 return;
1300 }
1301 instr = I | rotate_imm*B8 | immed_8;
1302 } else {
1303 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1304 instr = src.rm_.code();
1305 }
1306 emit(cond | instr | B24 | B21 | fields | 15*B12);
1307}
1308
1309
ager@chromium.org5c838252010-02-19 08:53:10 +00001310// Load/Store instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001312 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001313 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001314 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +00001316
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001317 // Eliminate pattern: push(ry), pop(rx)
1318 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1319 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1320 // Both instructions can be eliminated if ry = rx.
1321 // If ry != rx, a register copy from ry to rx is inserted
1322 // after eliminating the push and the pop instructions.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001323 if (can_peephole_optimize(2)) {
1324 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1325 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001326
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001327 if (IsPush(push_instr) && IsPop(pop_instr)) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001328 if (Instruction::RdValue(pop_instr) != Instruction::RdValue(push_instr)) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001329 // For consecutive push and pop on different registers,
1330 // we delete both the push & pop and insert a register move.
1331 // push ry, pop rx --> mov rx, ry
1332 Register reg_pushed, reg_popped;
1333 reg_pushed = GetRd(push_instr);
1334 reg_popped = GetRd(pop_instr);
1335 pc_ -= 2 * kInstrSize;
1336 // Insert a mov instruction, which is better than a pair of push & pop
1337 mov(reg_popped, reg_pushed);
1338 if (FLAG_print_peephole_optimization) {
1339 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1340 pc_offset());
1341 }
1342 } else {
1343 // For consecutive push and pop on the same register,
1344 // both the push and the pop can be deleted.
1345 pc_ -= 2 * kInstrSize;
1346 if (FLAG_print_peephole_optimization) {
1347 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1348 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001349 }
1350 }
1351 }
1352
1353 if (can_peephole_optimize(2)) {
1354 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1355 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1356
1357 if ((IsStrRegFpOffset(str_instr) &&
1358 IsLdrRegFpOffset(ldr_instr)) ||
1359 (IsStrRegFpNegOffset(str_instr) &&
1360 IsLdrRegFpNegOffset(ldr_instr))) {
1361 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1362 (str_instr & kLdrStrInstrArgumentMask)) {
1363 // Pattern: Ldr/str same fp+offset, same register.
1364 //
1365 // The following:
1366 // str rx, [fp, #-12]
1367 // ldr rx, [fp, #-12]
1368 //
1369 // Becomes:
1370 // str rx, [fp, #-12]
1371
1372 pc_ -= 1 * kInstrSize;
1373 if (FLAG_print_peephole_optimization) {
1374 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1375 }
1376 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1377 (str_instr & kLdrStrOffsetMask)) {
1378 // Pattern: Ldr/str same fp+offset, different register.
1379 //
1380 // The following:
1381 // str rx, [fp, #-12]
1382 // ldr ry, [fp, #-12]
1383 //
1384 // Becomes:
1385 // str rx, [fp, #-12]
1386 // mov ry, rx
1387
1388 Register reg_stored, reg_loaded;
1389 reg_stored = GetRd(str_instr);
1390 reg_loaded = GetRd(ldr_instr);
1391 pc_ -= 1 * kInstrSize;
1392 // Insert a mov instruction, which is better than ldr.
1393 mov(reg_loaded, reg_stored);
1394 if (FLAG_print_peephole_optimization) {
1395 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1396 }
1397 }
1398 }
1399 }
1400
1401 if (can_peephole_optimize(3)) {
1402 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1403 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1404 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1405 if (IsPush(mem_write_instr) &&
1406 IsPop(mem_read_instr)) {
1407 if ((IsLdrRegFpOffset(ldr_instr) ||
1408 IsLdrRegFpNegOffset(ldr_instr))) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001409 if (Instruction::RdValue(mem_write_instr) ==
1410 Instruction::RdValue(mem_read_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001411 // Pattern: push & pop from/to same register,
1412 // with a fp+offset ldr in between
1413 //
1414 // The following:
1415 // str rx, [sp, #-4]!
1416 // ldr rz, [fp, #-24]
1417 // ldr rx, [sp], #+4
1418 //
1419 // Becomes:
1420 // if(rx == rz)
1421 // delete all
1422 // else
1423 // ldr rz, [fp, #-24]
1424
ager@chromium.org378b34e2011-01-28 08:04:38 +00001425 if (Instruction::RdValue(mem_write_instr) ==
1426 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001427 pc_ -= 3 * kInstrSize;
1428 } else {
1429 pc_ -= 3 * kInstrSize;
1430 // Reinsert back the ldr rz.
1431 emit(ldr_instr);
1432 }
1433 if (FLAG_print_peephole_optimization) {
1434 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1435 }
1436 } else {
1437 // Pattern: push & pop from/to different registers
1438 // with a fp+offset ldr in between
1439 //
1440 // The following:
1441 // str rx, [sp, #-4]!
1442 // ldr rz, [fp, #-24]
1443 // ldr ry, [sp], #+4
1444 //
1445 // Becomes:
1446 // if(ry == rz)
1447 // mov ry, rx;
1448 // else if(rx != rz)
1449 // ldr rz, [fp, #-24]
1450 // mov ry, rx
1451 // else if((ry != rz) || (rx == rz)) becomes:
1452 // mov ry, rx
1453 // ldr rz, [fp, #-24]
1454
1455 Register reg_pushed, reg_popped;
ager@chromium.org378b34e2011-01-28 08:04:38 +00001456 if (Instruction::RdValue(mem_read_instr) ==
1457 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001458 reg_pushed = GetRd(mem_write_instr);
1459 reg_popped = GetRd(mem_read_instr);
1460 pc_ -= 3 * kInstrSize;
1461 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001462 } else if (Instruction::RdValue(mem_write_instr) !=
1463 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001464 reg_pushed = GetRd(mem_write_instr);
1465 reg_popped = GetRd(mem_read_instr);
1466 pc_ -= 3 * kInstrSize;
1467 emit(ldr_instr);
1468 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001469 } else if ((Instruction::RdValue(mem_read_instr) !=
1470 Instruction::RdValue(ldr_instr)) ||
1471 (Instruction::RdValue(mem_write_instr) ==
1472 Instruction::RdValue(ldr_instr))) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001473 reg_pushed = GetRd(mem_write_instr);
1474 reg_popped = GetRd(mem_read_instr);
1475 pc_ -= 3 * kInstrSize;
1476 mov(reg_popped, reg_pushed);
1477 emit(ldr_instr);
1478 }
1479 if (FLAG_print_peephole_optimization) {
1480 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1481 }
1482 }
1483 }
mads.s.ager31e71382008-08-13 09:32:07 +00001484 }
1485 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486}
1487
1488
1489void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1490 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +00001491
1492 // Eliminate pattern: pop(), push(r)
1493 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1494 // -> str r, [sp, 0], al
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001495 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001496 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001497 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1498 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1499 pc_ -= 2 * kInstrSize;
1500 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001501 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001502 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1503 }
1504 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505}
1506
1507
1508void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1509 addrmod2(cond | B26 | B | L, dst, src);
1510}
1511
1512
1513void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1514 addrmod2(cond | B26 | B, src, dst);
1515}
1516
1517
1518void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1519 addrmod3(cond | L | B7 | H | B4, dst, src);
1520}
1521
1522
1523void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1524 addrmod3(cond | B7 | H | B4, src, dst);
1525}
1526
1527
1528void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1529 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1530}
1531
1532
1533void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1534 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1535}
1536
1537
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001538void Assembler::ldrd(Register dst1, Register dst2,
1539 const MemOperand& src, Condition cond) {
1540 ASSERT(CpuFeatures::IsEnabled(ARMv7));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001541 ASSERT(src.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001542 ASSERT(!dst1.is(lr)); // r14.
1543 ASSERT_EQ(0, dst1.code() % 2);
1544 ASSERT_EQ(dst1.code() + 1, dst2.code());
1545 addrmod3(cond | B7 | B6 | B4, dst1, src);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001546}
1547
1548
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001549void Assembler::strd(Register src1, Register src2,
1550 const MemOperand& dst, Condition cond) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001551 ASSERT(dst.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001552 ASSERT(!src1.is(lr)); // r14.
1553 ASSERT_EQ(0, src1.code() % 2);
1554 ASSERT_EQ(src1.code() + 1, src2.code());
1555 ASSERT(CpuFeatures::IsEnabled(ARMv7));
1556 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001557}
1558
ager@chromium.org5c838252010-02-19 08:53:10 +00001559// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560void Assembler::ldm(BlockAddrMode am,
1561 Register base,
1562 RegList dst,
1563 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001564 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1566
1567 addrmod4(cond | B27 | am | L, base, dst);
1568
ager@chromium.org5c838252010-02-19 08:53:10 +00001569 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 if (cond == al && (dst & pc.bit()) != 0) {
1571 // There is a slight chance that the ldm instruction was actually a call,
1572 // in which case it would be wrong to return into the constant pool; we
1573 // recognize this case by checking if the emission of the pool was blocked
1574 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1575 // the case, we emit a jump over the pool.
1576 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1577 }
1578}
1579
1580
1581void Assembler::stm(BlockAddrMode am,
1582 Register base,
1583 RegList src,
1584 Condition cond) {
1585 addrmod4(cond | B27 | am, base, src);
1586}
1587
1588
ager@chromium.org5c838252010-02-19 08:53:10 +00001589// Exception-generating instructions and debugging support.
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001590// Stops with a non-negative code less than kNumOfWatchedStops support
1591// enabling/disabling and a counter feature. See simulator-arm.h .
1592void Assembler::stop(const char* msg, Condition cond, int32_t code) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001593#ifndef __arm__
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001594 ASSERT(code >= kDefaultStopCode);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001595 // The Simulator will handle the stop instruction and get the message address.
1596 // It expects to find the address just after the svc instruction.
1597 BlockConstPoolFor(2);
1598 if (code >= 0) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001599 svc(kStopCode + code, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001600 } else {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001601 svc(kStopCode + kMaxStopCode, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001602 }
1603 emit(reinterpret_cast<Instr>(msg));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001604#else // def __arm__
1605#ifdef CAN_USE_ARMV5_INSTRUCTIONS
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001606 if (cond != al) {
1607 Label skip;
1608 b(&skip, NegateCondition(cond));
1609 bkpt(0);
1610 bind(&skip);
1611 } else {
1612 bkpt(0);
1613 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001614#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001615 svc(0x9f0001, cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001616#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1617#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618}
1619
1620
1621void Assembler::bkpt(uint32_t imm16) { // v5 and above
1622 ASSERT(is_uint16(imm16));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001623 emit(al | B24 | B21 | (imm16 >> 4)*B8 | BKPT | (imm16 & 0xf));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624}
1625
1626
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001627void Assembler::svc(uint32_t imm24, Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628 ASSERT(is_uint24(imm24));
1629 emit(cond | 15*B24 | imm24);
1630}
1631
1632
ager@chromium.org5c838252010-02-19 08:53:10 +00001633// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634void Assembler::cdp(Coprocessor coproc,
1635 int opcode_1,
1636 CRegister crd,
1637 CRegister crn,
1638 CRegister crm,
1639 int opcode_2,
1640 Condition cond) {
1641 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1642 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1643 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1644}
1645
1646
1647void Assembler::cdp2(Coprocessor coproc,
1648 int opcode_1,
1649 CRegister crd,
1650 CRegister crn,
1651 CRegister crm,
1652 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001653 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654}
1655
1656
1657void Assembler::mcr(Coprocessor coproc,
1658 int opcode_1,
1659 Register rd,
1660 CRegister crn,
1661 CRegister crm,
1662 int opcode_2,
1663 Condition cond) {
1664 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1665 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1666 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1667}
1668
1669
1670void Assembler::mcr2(Coprocessor coproc,
1671 int opcode_1,
1672 Register rd,
1673 CRegister crn,
1674 CRegister crm,
1675 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001676 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677}
1678
1679
1680void Assembler::mrc(Coprocessor coproc,
1681 int opcode_1,
1682 Register rd,
1683 CRegister crn,
1684 CRegister crm,
1685 int opcode_2,
1686 Condition cond) {
1687 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1688 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1689 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1690}
1691
1692
1693void Assembler::mrc2(Coprocessor coproc,
1694 int opcode_1,
1695 Register rd,
1696 CRegister crn,
1697 CRegister crm,
1698 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001699 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700}
1701
1702
1703void Assembler::ldc(Coprocessor coproc,
1704 CRegister crd,
1705 const MemOperand& src,
1706 LFlag l,
1707 Condition cond) {
1708 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1709}
1710
1711
1712void Assembler::ldc(Coprocessor coproc,
1713 CRegister crd,
1714 Register rn,
1715 int option,
1716 LFlag l,
1717 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001718 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 ASSERT(is_uint8(option));
1720 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1721 coproc*B8 | (option & 255));
1722}
1723
1724
1725void Assembler::ldc2(Coprocessor coproc,
1726 CRegister crd,
1727 const MemOperand& src,
1728 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001729 ldc(coproc, crd, src, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730}
1731
1732
1733void Assembler::ldc2(Coprocessor coproc,
1734 CRegister crd,
1735 Register rn,
1736 int option,
1737 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001738 ldc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739}
1740
1741
1742void Assembler::stc(Coprocessor coproc,
1743 CRegister crd,
1744 const MemOperand& dst,
1745 LFlag l,
1746 Condition cond) {
1747 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1748}
1749
1750
1751void Assembler::stc(Coprocessor coproc,
1752 CRegister crd,
1753 Register rn,
1754 int option,
1755 LFlag l,
1756 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001757 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 ASSERT(is_uint8(option));
1759 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1760 coproc*B8 | (option & 255));
1761}
1762
1763
1764void Assembler::stc2(Coprocessor
1765 coproc, CRegister crd,
1766 const MemOperand& dst,
1767 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001768 stc(coproc, crd, dst, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769}
1770
1771
1772void Assembler::stc2(Coprocessor coproc,
1773 CRegister crd,
1774 Register rn,
1775 int option,
1776 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001777 stc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778}
1779
1780
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001781// Support for VFP.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001782
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001783void Assembler::vldr(const DwVfpRegister dst,
1784 const Register base,
1785 int offset,
1786 const Condition cond) {
1787 // Ddst = MEM(Rbase + offset).
1788 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001789 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001790 // Vdst(15-12) | 1011(11-8) | offset
1791 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001792 int u = 1;
1793 if (offset < 0) {
1794 offset = -offset;
1795 u = 0;
1796 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001797 ASSERT(offset % 4 == 0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001798 ASSERT((offset / 4) < 256);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001799 ASSERT(offset >= 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001800 emit(cond | u*B23 | 0xD1*B20 | base.code()*B16 | dst.code()*B12 |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001801 0xB*B8 | ((offset / 4) & 255));
1802}
1803
1804
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001805void Assembler::vldr(const SwVfpRegister dst,
1806 const Register base,
1807 int offset,
1808 const Condition cond) {
1809 // Sdst = MEM(Rbase + offset).
1810 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001811 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001812 // Vdst(15-12) | 1010(11-8) | offset
1813 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001814 int u = 1;
1815 if (offset < 0) {
1816 offset = -offset;
1817 u = 0;
1818 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001819 ASSERT(offset % 4 == 0);
1820 ASSERT((offset / 4) < 256);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001821 ASSERT(offset >= 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001822 int sd, d;
1823 dst.split_code(&sd, &d);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001824 emit(cond | u*B23 | d*B22 | 0xD1*B20 | base.code()*B16 | sd*B12 |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001825 0xA*B8 | ((offset / 4) & 255));
1826}
1827
1828
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001829void Assembler::vstr(const DwVfpRegister src,
1830 const Register base,
1831 int offset,
1832 const Condition cond) {
1833 // MEM(Rbase + offset) = Dsrc.
1834 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001835 // cond(31-28) | 1101(27-24)| U000(23-20) | | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001836 // Vsrc(15-12) | 1011(11-8) | (offset/4)
1837 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001838 int u = 1;
1839 if (offset < 0) {
1840 offset = -offset;
1841 u = 0;
1842 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001843 ASSERT(offset % 4 == 0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001844 ASSERT((offset / 4) < 256);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001845 ASSERT(offset >= 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001846 emit(cond | u*B23 | 0xD0*B20 | base.code()*B16 | src.code()*B12 |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001847 0xB*B8 | ((offset / 4) & 255));
1848}
1849
1850
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001851void Assembler::vstr(const SwVfpRegister src,
1852 const Register base,
1853 int offset,
1854 const Condition cond) {
1855 // MEM(Rbase + offset) = SSrc.
1856 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001857 // cond(31-28) | 1101(27-24)| U000(23-20) | Rbase(19-16) |
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001858 // Vdst(15-12) | 1010(11-8) | (offset/4)
1859 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001860 int u = 1;
1861 if (offset < 0) {
1862 offset = -offset;
1863 u = 0;
1864 }
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001865 ASSERT(offset % 4 == 0);
1866 ASSERT((offset / 4) < 256);
1867 ASSERT(offset >= 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001868 int sd, d;
1869 src.split_code(&sd, &d);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001870 emit(cond | u*B23 | d*B22 | 0xD0*B20 | base.code()*B16 | sd*B12 |
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001871 0xA*B8 | ((offset / 4) & 255));
1872}
1873
1874
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001875static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
1876 uint64_t i;
1877 memcpy(&i, &d, 8);
1878
1879 *lo = i & 0xffffffff;
1880 *hi = i >> 32;
1881}
1882
1883// Only works for little endian floating point formats.
1884// We don't support VFP on the mixed endian floating point platform.
1885static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
1886 ASSERT(CpuFeatures::IsEnabled(VFP3));
1887
1888 // VMOV can accept an immediate of the form:
1889 //
1890 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
1891 //
1892 // The immediate is encoded using an 8-bit quantity, comprised of two
1893 // 4-bit fields. For an 8-bit immediate of the form:
1894 //
1895 // [abcdefgh]
1896 //
1897 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
1898 // created of the form:
1899 //
1900 // [aBbbbbbb,bbcdefgh,00000000,00000000,
1901 // 00000000,00000000,00000000,00000000]
1902 //
1903 // where B = ~b.
1904 //
1905
1906 uint32_t lo, hi;
1907 DoubleAsTwoUInt32(d, &lo, &hi);
1908
1909 // The most obvious constraint is the long block of zeroes.
1910 if ((lo != 0) || ((hi & 0xffff) != 0)) {
1911 return false;
1912 }
1913
1914 // Bits 62:55 must be all clear or all set.
1915 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
1916 return false;
1917 }
1918
1919 // Bit 63 must be NOT bit 62.
1920 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
1921 return false;
1922 }
1923
1924 // Create the encoded immediate in the form:
1925 // [00000000,0000abcd,00000000,0000efgh]
1926 *encoding = (hi >> 16) & 0xf; // Low nybble.
1927 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
1928 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
1929
1930 return true;
1931}
1932
1933
1934void Assembler::vmov(const DwVfpRegister dst,
1935 double imm,
1936 const Condition cond) {
1937 // Dd = immediate
1938 // Instruction details available in ARM DDI 0406B, A8-640.
1939 ASSERT(CpuFeatures::IsEnabled(VFP3));
1940
1941 uint32_t enc;
1942 if (FitsVMOVDoubleImmediate(imm, &enc)) {
1943 // The double can be encoded in the instruction.
1944 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
1945 } else {
1946 // Synthesise the double from ARM immediates. This could be implemented
1947 // using vldr from a constant pool.
1948 uint32_t lo, hi;
1949 DoubleAsTwoUInt32(imm, &lo, &hi);
1950
1951 if (lo == hi) {
1952 // If the lo and hi parts of the double are equal, the literal is easier
1953 // to create. This is the case with 0.0.
1954 mov(ip, Operand(lo));
1955 vmov(dst, ip, ip);
1956 } else {
1957 // Move the low part of the double into the lower of the corresponsing S
1958 // registers of D register dst.
1959 mov(ip, Operand(lo));
1960 vmov(dst.low(), ip, cond);
1961
1962 // Move the high part of the double into the higher of the corresponsing S
1963 // registers of D register dst.
1964 mov(ip, Operand(hi));
1965 vmov(dst.high(), ip, cond);
1966 }
1967 }
1968}
1969
1970
1971void Assembler::vmov(const SwVfpRegister dst,
1972 const SwVfpRegister src,
1973 const Condition cond) {
1974 // Sd = Sm
1975 // Instruction details available in ARM DDI 0406B, A8-642.
1976 ASSERT(CpuFeatures::IsEnabled(VFP3));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001977 int sd, d, sm, m;
1978 dst.split_code(&sd, &d);
1979 src.split_code(&sm, &m);
1980 emit(cond | 0xE*B24 | d*B22 | 0xB*B20 | sd*B12 | 0xA*B8 | B6 | m*B5 | sm);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001981}
1982
1983
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001984void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001985 const DwVfpRegister src,
1986 const Condition cond) {
1987 // Dd = Dm
1988 // Instruction details available in ARM DDI 0406B, A8-642.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001989 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001990 emit(cond | 0xE*B24 | 0xB*B20 |
1991 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
1992}
1993
1994
1995void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001996 const Register src1,
1997 const Register src2,
1998 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001999 // Dm = <Rt,Rt2>.
2000 // Instruction details available in ARM DDI 0406A, A8-646.
2001 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
2002 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
2003 ASSERT(CpuFeatures::IsEnabled(VFP3));
2004 ASSERT(!src1.is(pc) && !src2.is(pc));
2005 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
2006 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
2007}
2008
2009
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002010void Assembler::vmov(const Register dst1,
2011 const Register dst2,
2012 const DwVfpRegister src,
2013 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002014 // <Rt,Rt2> = Dm.
2015 // Instruction details available in ARM DDI 0406A, A8-646.
2016 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
2017 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
2018 ASSERT(CpuFeatures::IsEnabled(VFP3));
2019 ASSERT(!dst1.is(pc) && !dst2.is(pc));
2020 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
2021 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
2022}
2023
2024
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002025void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002026 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002027 const Condition cond) {
2028 // Sn = Rt.
2029 // Instruction details available in ARM DDI 0406A, A8-642.
2030 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
2031 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
2032 ASSERT(CpuFeatures::IsEnabled(VFP3));
2033 ASSERT(!src.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002034 int sn, n;
2035 dst.split_code(&sn, &n);
2036 emit(cond | 0xE*B24 | sn*B16 | src.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002037}
2038
2039
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002040void Assembler::vmov(const Register dst,
2041 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002042 const Condition cond) {
2043 // Rt = Sn.
2044 // Instruction details available in ARM DDI 0406A, A8-642.
2045 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
2046 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
2047 ASSERT(CpuFeatures::IsEnabled(VFP3));
2048 ASSERT(!dst.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002049 int sn, n;
2050 src.split_code(&sn, &n);
2051 emit(cond | 0xE*B24 | B20 | sn*B16 | dst.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002052}
2053
2054
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002055// Type of data to read from or write to VFP register.
2056// Used as specifier in generic vcvt instruction.
2057enum VFPType { S32, U32, F32, F64 };
2058
2059
2060static bool IsSignedVFPType(VFPType type) {
2061 switch (type) {
2062 case S32:
2063 return true;
2064 case U32:
2065 return false;
2066 default:
2067 UNREACHABLE();
2068 return false;
2069 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002070}
2071
2072
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002073static bool IsIntegerVFPType(VFPType type) {
2074 switch (type) {
2075 case S32:
2076 case U32:
2077 return true;
2078 case F32:
2079 case F64:
2080 return false;
2081 default:
2082 UNREACHABLE();
2083 return false;
2084 }
2085}
2086
2087
2088static bool IsDoubleVFPType(VFPType type) {
2089 switch (type) {
2090 case F32:
2091 return false;
2092 case F64:
2093 return true;
2094 default:
2095 UNREACHABLE();
2096 return false;
2097 }
2098}
2099
2100
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002101// Split five bit reg_code based on size of reg_type.
2102// 32-bit register codes are Vm:M
2103// 64-bit register codes are M:Vm
2104// where Vm is four bits, and M is a single bit.
2105static void SplitRegCode(VFPType reg_type,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002106 int reg_code,
2107 int* vm,
2108 int* m) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002109 ASSERT((reg_code >= 0) && (reg_code <= 31));
2110 if (IsIntegerVFPType(reg_type) || !IsDoubleVFPType(reg_type)) {
2111 // 32 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002112 *m = reg_code & 0x1;
2113 *vm = reg_code >> 1;
2114 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002115 // 64 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002116 *m = (reg_code & 0x10) >> 4;
2117 *vm = reg_code & 0x0F;
2118 }
2119}
2120
2121
2122// Encode vcvt.src_type.dst_type instruction.
2123static Instr EncodeVCVT(const VFPType dst_type,
2124 const int dst_code,
2125 const VFPType src_type,
2126 const int src_code,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002127 Assembler::ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002128 const Condition cond) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002129 ASSERT(src_type != dst_type);
2130 int D, Vd, M, Vm;
2131 SplitRegCode(src_type, src_code, &Vm, &M);
2132 SplitRegCode(dst_type, dst_code, &Vd, &D);
2133
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002134 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2135 // Conversion between IEEE floating point and 32-bit integer.
2136 // Instruction details available in ARM DDI 0406B, A8.6.295.
2137 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2138 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2139 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2140
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002141 int sz, opc2, op;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002142
2143 if (IsIntegerVFPType(dst_type)) {
2144 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2145 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002146 op = mode;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002147 } else {
2148 ASSERT(IsIntegerVFPType(src_type));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002149 opc2 = 0x0;
2150 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2151 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002152 }
2153
2154 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2155 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2156 } else {
2157 // Conversion between IEEE double and single precision.
2158 // Instruction details available in ARM DDI 0406B, A8.6.298.
2159 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2160 // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002161 int sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002162 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2163 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2164 }
2165}
2166
2167
2168void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2169 const SwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002170 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002171 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002172 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002173 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002174}
2175
2176
2177void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2178 const SwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002179 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002180 const Condition cond) {
2181 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002182 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002183}
2184
2185
2186void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2187 const SwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002188 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002189 const Condition cond) {
2190 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002191 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002192}
2193
2194
2195void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2196 const DwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002197 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002198 const Condition cond) {
2199 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002200 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002201}
2202
2203
2204void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2205 const DwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002206 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002207 const Condition cond) {
2208 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002209 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002210}
2211
2212
2213void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2214 const SwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002215 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002216 const Condition cond) {
2217 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002218 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002219}
2220
2221
2222void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2223 const DwVfpRegister src,
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002224 ConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002225 const Condition cond) {
2226 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002227 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), mode, cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002228}
2229
2230
whesse@chromium.org7a392b32011-01-31 11:30:36 +00002231void Assembler::vabs(const DwVfpRegister dst,
2232 const DwVfpRegister src,
2233 const Condition cond) {
2234 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 |
2235 0x5*B9 | B8 | 0x3*B6 | src.code());
2236}
2237
2238
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002239void Assembler::vadd(const DwVfpRegister dst,
2240 const DwVfpRegister src1,
2241 const DwVfpRegister src2,
2242 const Condition cond) {
2243 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002244 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2245 // Instruction details available in ARM DDI 0406A, A8-536.
2246 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2247 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2248 ASSERT(CpuFeatures::IsEnabled(VFP3));
2249 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2250 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2251}
2252
2253
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002254void Assembler::vsub(const DwVfpRegister dst,
2255 const DwVfpRegister src1,
2256 const DwVfpRegister src2,
2257 const Condition cond) {
2258 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002259 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2260 // Instruction details available in ARM DDI 0406A, A8-784.
2261 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2262 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
2263 ASSERT(CpuFeatures::IsEnabled(VFP3));
2264 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2265 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2266}
2267
2268
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002269void Assembler::vmul(const DwVfpRegister dst,
2270 const DwVfpRegister src1,
2271 const DwVfpRegister src2,
2272 const Condition cond) {
2273 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002274 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2275 // Instruction details available in ARM DDI 0406A, A8-784.
2276 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2277 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2278 ASSERT(CpuFeatures::IsEnabled(VFP3));
2279 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2280 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2281}
2282
2283
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002284void Assembler::vdiv(const DwVfpRegister dst,
2285 const DwVfpRegister src1,
2286 const DwVfpRegister src2,
2287 const Condition cond) {
2288 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002289 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2290 // Instruction details available in ARM DDI 0406A, A8-584.
2291 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2292 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2293 ASSERT(CpuFeatures::IsEnabled(VFP3));
2294 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2295 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2296}
2297
2298
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002299void Assembler::vcmp(const DwVfpRegister src1,
2300 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002301 const Condition cond) {
2302 // vcmp(Dd, Dm) double precision floating point comparison.
2303 // Instruction details available in ARM DDI 0406A, A8-570.
2304 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002305 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=0 | 1(6) | M(5)=? | 0(4) | Vm(3-0)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002306 ASSERT(CpuFeatures::IsEnabled(VFP3));
2307 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002308 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002309}
2310
2311
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002312void Assembler::vcmp(const DwVfpRegister src1,
2313 const double src2,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002314 const Condition cond) {
2315 // vcmp(Dd, Dm) double precision floating point comparison.
2316 // Instruction details available in ARM DDI 0406A, A8-570.
2317 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0101 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002318 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=0 | 1(6) | M(5)=? | 0(4) | 0000(3-0)
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002319 ASSERT(CpuFeatures::IsEnabled(VFP3));
2320 ASSERT(src2 == 0.0);
2321 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | B16 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002322 src1.code()*B12 | 0x5*B9 | B8 | B6);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002323}
2324
2325
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002326void Assembler::vmsr(Register dst, Condition cond) {
2327 // Instruction details available in ARM DDI 0406A, A8-652.
2328 // cond(31-28) | 1110 (27-24) | 1110(23-20)| 0001 (19-16) |
2329 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
2330 ASSERT(CpuFeatures::IsEnabled(VFP3));
2331 emit(cond | 0xE*B24 | 0xE*B20 | B16 |
2332 dst.code()*B12 | 0xA*B8 | B4);
2333}
2334
2335
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002336void Assembler::vmrs(Register dst, Condition cond) {
2337 // Instruction details available in ARM DDI 0406A, A8-652.
2338 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2339 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
2340 ASSERT(CpuFeatures::IsEnabled(VFP3));
2341 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2342 dst.code()*B12 | 0xA*B8 | B4);
2343}
2344
2345
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002346void Assembler::vsqrt(const DwVfpRegister dst,
2347 const DwVfpRegister src,
2348 const Condition cond) {
2349 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2350 // Vd(15-12) | 101(11-9) | sz(8)=1 | 11 (7-6) | M(5)=? | 0(4) | Vm(3-0)
2351 ASSERT(CpuFeatures::IsEnabled(VFP3));
2352 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2353 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2354}
2355
2356
ager@chromium.org5c838252010-02-19 08:53:10 +00002357// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002358void Assembler::nop(int type) {
2359 // This is mov rx, rx.
2360 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2361 emit(al | 13*B21 | type*B12 | type);
2362}
2363
2364
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002365bool Assembler::IsNop(Instr instr, int type) {
2366 // Check for mov rx, rx.
2367 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2368 return instr == (al | 13*B21 | type*B12 | type);
2369}
2370
2371
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002372bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2373 uint32_t dummy1;
2374 uint32_t dummy2;
2375 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2376}
2377
2378
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002379void Assembler::BlockConstPoolFor(int instructions) {
2380 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2381}
2382
2383
ager@chromium.org5c838252010-02-19 08:53:10 +00002384// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002385void Assembler::RecordJSReturn() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002386 positions_recorder()->WriteRecordedPositions();
ager@chromium.org4af710e2009-09-15 12:20:11 +00002387 CheckBuffer();
2388 RecordRelocInfo(RelocInfo::JS_RETURN);
2389}
2390
2391
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002392void Assembler::RecordDebugBreakSlot() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002393 positions_recorder()->WriteRecordedPositions();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002394 CheckBuffer();
2395 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2396}
2397
2398
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399void Assembler::RecordComment(const char* msg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002400 if (FLAG_code_comments) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00002402 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 }
2404}
2405
2406
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407void Assembler::GrowBuffer() {
2408 if (!own_buffer_) FATAL("external code buffer is too small");
2409
ager@chromium.org5c838252010-02-19 08:53:10 +00002410 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411 CodeDesc desc; // the new buffer
2412 if (buffer_size_ < 4*KB) {
2413 desc.buffer_size = 4*KB;
2414 } else if (buffer_size_ < 1*MB) {
2415 desc.buffer_size = 2*buffer_size_;
2416 } else {
2417 desc.buffer_size = buffer_size_ + 1*MB;
2418 }
2419 CHECK_GT(desc.buffer_size, 0); // no overflow
2420
ager@chromium.org5c838252010-02-19 08:53:10 +00002421 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 desc.buffer = NewArray<byte>(desc.buffer_size);
2423
2424 desc.instr_size = pc_offset();
2425 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2426
ager@chromium.org5c838252010-02-19 08:53:10 +00002427 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 int pc_delta = desc.buffer - buffer_;
2429 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2430 memmove(desc.buffer, buffer_, desc.instr_size);
2431 memmove(reloc_info_writer.pos() + rc_delta,
2432 reloc_info_writer.pos(), desc.reloc_size);
2433
ager@chromium.org5c838252010-02-19 08:53:10 +00002434 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435 DeleteArray(buffer_);
2436 buffer_ = desc.buffer;
2437 buffer_size_ = desc.buffer_size;
2438 pc_ += pc_delta;
2439 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2440 reloc_info_writer.last_pc() + pc_delta);
2441
ager@chromium.org5c838252010-02-19 08:53:10 +00002442 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00002444 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445
ager@chromium.org5c838252010-02-19 08:53:10 +00002446 // Relocate pending relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 for (int i = 0; i < num_prinfo_; i++) {
2448 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002449 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2450 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00002451 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2452 rinfo.set_pc(rinfo.pc() + pc_delta);
2453 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454 }
2455}
2456
2457
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002458void Assembler::db(uint8_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002459 // No relocation info should be pending while using db. db is used
2460 // to write pure data with no pointers and the constant pool should
2461 // be emitted before using db.
2462 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002463 CheckBuffer();
2464 *reinterpret_cast<uint8_t*>(pc_) = data;
2465 pc_ += sizeof(uint8_t);
2466}
2467
2468
2469void Assembler::dd(uint32_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002470 // No relocation info should be pending while using dd. dd is used
2471 // to write pure data with no pointers and the constant pool should
2472 // be emitted before using dd.
2473 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002474 CheckBuffer();
2475 *reinterpret_cast<uint32_t*>(pc_) = data;
2476 pc_ += sizeof(uint32_t);
2477}
2478
2479
ager@chromium.org236ad962008-09-25 09:45:57 +00002480void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002482 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002483 // Adjust code for new modes.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002484 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2485 || RelocInfo::IsJSReturn(rmode)
ager@chromium.org4af710e2009-09-15 12:20:11 +00002486 || RelocInfo::IsComment(rmode)
2487 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00002488 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489 } else {
2490 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2491 prinfo_[num_prinfo_++] = rinfo;
2492 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00002493 // instruction for which we just recorded relocation info.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 BlockConstPoolBefore(pc_offset() + kInstrSize);
2495 }
ager@chromium.org236ad962008-09-25 09:45:57 +00002496 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002497 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002498 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2499#ifdef DEBUG
2500 if (!Serializer::enabled()) {
2501 Serializer::TooLateToEnableNow();
2502 }
2503#endif
2504 if (!Serializer::enabled() && !FLAG_debug_code) {
2505 return;
2506 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002507 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2509 reloc_info_writer.Write(&rinfo);
2510 }
2511}
2512
2513
2514void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2515 // Calculate the offset of the next check. It will be overwritten
2516 // when a const pool is generated or when const pools are being
2517 // blocked for a specific range.
2518 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2519
ager@chromium.org5c838252010-02-19 08:53:10 +00002520 // There is nothing to do if there are no pending relocation info entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521 if (num_prinfo_ == 0) return;
2522
2523 // We emit a constant pool at regular intervals of about kDistBetweenPools
2524 // or when requested by parameter force_emit (e.g. after each function).
2525 // We prefer not to emit a jump unless the max distance is reached or if we
2526 // are running low on slots, which can happen if a lot of constants are being
2527 // emitted (e.g. --debug-code and many static references).
2528 int dist = pc_offset() - last_const_pool_end_;
2529 if (!force_emit && dist < kMaxDistBetweenPools &&
2530 (require_jump || dist < kDistBetweenPools) &&
2531 // TODO(1236125): Cleanup the "magic" number below. We know that
2532 // the code generation will test every kCheckConstIntervalInst.
2533 // Thus we are safe as long as we generate less than 7 constant
2534 // entries per instruction.
2535 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2536 return;
2537 }
2538
2539 // If we did not return by now, we need to emit the constant pool soon.
2540
2541 // However, some small sequences of instructions must not be broken up by the
2542 // insertion of a constant pool; such sequences are protected by setting
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002543 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2544 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2545 // no_const_pool_before_.
2546 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002547 // Emission is currently blocked; make sure we try again as soon as
2548 // possible.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002549 if (const_pool_blocked_nesting_ > 0) {
2550 next_buffer_check_ = pc_offset() + kInstrSize;
2551 } else {
2552 next_buffer_check_ = no_const_pool_before_;
2553 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554
ager@chromium.org5c838252010-02-19 08:53:10 +00002555 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 ASSERT(!force_emit);
2557 return;
2558 }
2559
2560 int jump_instr = require_jump ? kInstrSize : 0;
2561
2562 // Check that the code buffer is large enough before emitting the constant
2563 // pool and relocation information (include the jump over the pool and the
2564 // constant pool marker).
2565 int max_needed_space =
2566 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2567 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2568
ager@chromium.org5c838252010-02-19 08:53:10 +00002569 // Block recursive calls to CheckConstPool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2571 num_prinfo_*kInstrSize);
2572 // Don't bother to check for the emit calls below.
2573 next_buffer_check_ = no_const_pool_before_;
2574
ager@chromium.org5c838252010-02-19 08:53:10 +00002575 // Emit jump over constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576 Label after_pool;
2577 if (require_jump) b(&after_pool);
2578
2579 RecordComment("[ Constant Pool");
2580
ager@chromium.org5c838252010-02-19 08:53:10 +00002581 // Put down constant pool marker "Undefined instruction" as specified by
2582 // A3.1 Instruction set encoding.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 emit(0x03000000 | num_prinfo_);
2584
ager@chromium.org5c838252010-02-19 08:53:10 +00002585 // Emit constant pool entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586 for (int i = 0; i < num_prinfo_; i++) {
2587 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002588 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2589 rinfo.rmode() != RelocInfo::POSITION &&
2590 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 Instr instr = instr_at(rinfo.pc());
ager@chromium.org4af710e2009-09-15 12:20:11 +00002592
ager@chromium.org5c838252010-02-19 08:53:10 +00002593 // Instruction to patch must be a ldr/str [pc, #offset].
2594 // P and U set, B and W clear, Rn == pc, offset12 still 0.
ager@chromium.org378b34e2011-01-28 08:04:38 +00002595 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | kOff12Mask)) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 (2*B25 | P | U | pc.code()*B16));
2597 int delta = pc_ - rinfo.pc() - 8;
2598 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2599 if (delta < 0) {
2600 instr &= ~U;
2601 delta = -delta;
2602 }
2603 ASSERT(is_uint12(delta));
2604 instr_at_put(rinfo.pc(), instr + delta);
2605 emit(rinfo.data());
2606 }
2607 num_prinfo_ = 0;
2608 last_const_pool_end_ = pc_offset();
2609
2610 RecordComment("]");
2611
2612 if (after_pool.is_linked()) {
2613 bind(&after_pool);
2614 }
2615
2616 // Since a constant pool was just emitted, move the check offset forward by
2617 // the standard interval.
2618 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2619}
2620
2621
2622} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002623
2624#endif // V8_TARGET_ARCH_ARM