blob: 3533dfd6df95ab017c1c794d02883d6185c4ce30 [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.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000035// Copyright 2011 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
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000047#ifdef DEBUG
48bool CpuFeatures::initialized_ = false;
49#endif
50unsigned CpuFeatures::supported_ = 0;
51unsigned CpuFeatures::found_by_runtime_probing_ = 0;
52
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000053
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000054// Get the CPU features enabled by the build. For cross compilation the
55// preprocessor symbols CAN_USE_ARMV7_INSTRUCTIONS and CAN_USE_VFP_INSTRUCTIONS
56// can be defined to enable ARMv7 and VFPv3 instructions when building the
57// snapshot.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000058static uint64_t CpuFeaturesImpliedByCompiler() {
59 uint64_t answer = 0;
60#ifdef CAN_USE_ARMV7_INSTRUCTIONS
61 answer |= 1u << ARMv7;
62#endif // def CAN_USE_ARMV7_INSTRUCTIONS
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000063#ifdef CAN_USE_VFP_INSTRUCTIONS
64 answer |= 1u << VFP3 | 1u << ARMv7;
65#endif // def CAN_USE_VFP_INSTRUCTIONS
66
67#ifdef __arm__
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000068 // If the compiler is allowed to use VFP then we can use VFP too in our code
69 // generation even when generating snapshots. This won't work for cross
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000070 // compilation. VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000071#if defined(__VFP_FP__) && !defined(__SOFTFP__)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000072 answer |= 1u << VFP3 | 1u << ARMv7;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000073#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000074#endif // def __arm__
75
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000076 return answer;
77}
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000078
79
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000080void CpuFeatures::Probe() {
81 ASSERT(!initialized_);
82#ifdef DEBUG
83 initialized_ = true;
84#endif
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000085
86 // Get the features implied by the OS and the compiler settings. This is the
87 // minimal set of features which is also alowed for generated code in the
88 // snapshot.
89 supported_ |= OS::CpuFeaturesImpliedByPlatform();
90 supported_ |= CpuFeaturesImpliedByCompiler();
91
92 if (Serializer::enabled()) {
93 // No probing for features if we might serialize (generate snapshot).
94 return;
95 }
96
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000097#ifndef __arm__
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000098 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is
99 // enabled. VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
ager@chromium.org5c838252010-02-19 08:53:10 +0000100 if (FLAG_enable_vfp3) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000101 supported_ |= 1u << VFP3 | 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +0000102 }
103 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
104 if (FLAG_enable_armv7) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000105 supported_ |= 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +0000106 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000107#else // def __arm__
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000108 // Probe for additional features not already known to be available.
109 if (!IsSupported(VFP3) && OS::ArmCpuHasFeature(VFP3)) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000110 // This implementation also sets the VFP flags if runtime
111 // detection of VFP returns true. VFPv3 implies ARMv7, see ARM DDI
112 // 0406B, page A1-6.
113 supported_ |= 1u << VFP3 | 1u << ARMv7;
114 found_by_runtime_probing_ |= 1u << VFP3 | 1u << ARMv7;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000116
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000117 if (!IsSupported(ARMv7) && OS::ArmCpuHasFeature(ARMv7)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000118 supported_ |= 1u << ARMv7;
119 found_by_runtime_probing_ |= 1u << ARMv7;
120 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121#endif
122}
123
124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126// Implementation of RelocInfo
127
128const int RelocInfo::kApplyMask = 0;
129
130
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000131bool RelocInfo::IsCodedSpecially() {
132 // The deserializer needs to know whether a pointer is specially coded. Being
133 // specially coded on ARM means that it is a movw/movt instruction. We don't
134 // generate those yet.
135 return false;
136}
137
138
139
iposva@chromium.org245aa852009-02-10 00:49:54 +0000140void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000142 Instr* pc = reinterpret_cast<Instr*>(pc_);
143 Instr* instr = reinterpret_cast<Instr*>(instructions);
144 for (int i = 0; i < instruction_count; i++) {
145 *(pc + i) = *(instr + i);
146 }
147
148 // Indicate that code has changed.
149 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150}
151
152
153// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000154// Additional guard instructions can be added if required.
155void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 // Patch the code at the current address with a call to the target.
157 UNIMPLEMENTED();
158}
159
160
161// -----------------------------------------------------------------------------
162// Implementation of Operand and MemOperand
163// See assembler-arm-inl.h for inlined constructors
164
165Operand::Operand(Handle<Object> handle) {
166 rm_ = no_reg;
167 // Verify all Objects referred by code are NOT in new space.
168 Object* obj = *handle;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000169 ASSERT(!HEAP->InNewSpace(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 if (obj->IsHeapObject()) {
171 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000172 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 } else {
174 // no relocation needed
175 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000176 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 }
178}
179
180
181Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
182 ASSERT(is_uint5(shift_imm));
183 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
184 rm_ = rm;
185 rs_ = no_reg;
186 shift_op_ = shift_op;
187 shift_imm_ = shift_imm & 31;
188 if (shift_op == RRX) {
189 // encoded as ROR with shift_imm == 0
190 ASSERT(shift_imm == 0);
191 shift_op_ = ROR;
192 shift_imm_ = 0;
193 }
194}
195
196
197Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
198 ASSERT(shift_op != RRX);
199 rm_ = rm;
200 rs_ = no_reg;
201 shift_op_ = shift_op;
202 rs_ = rs;
203}
204
205
206MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
207 rn_ = rn;
208 rm_ = no_reg;
209 offset_ = offset;
210 am_ = am;
211}
212
213MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
214 rn_ = rn;
215 rm_ = rm;
216 shift_op_ = LSL;
217 shift_imm_ = 0;
218 am_ = am;
219}
220
221
222MemOperand::MemOperand(Register rn, Register rm,
223 ShiftOp shift_op, int shift_imm, AddrMode am) {
224 ASSERT(is_uint5(shift_imm));
225 rn_ = rn;
226 rm_ = rm;
227 shift_op_ = shift_op;
228 shift_imm_ = shift_imm & 31;
229 am_ = am;
230}
231
232
233// -----------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000234// Specific instructions, constants, and masks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
mads.s.ager31e71382008-08-13 09:32:07 +0000236// add(sp, sp, 4) instruction (aka Pop())
ager@chromium.org378b34e2011-01-28 08:04:38 +0000237const Instr kPopInstruction =
238 al | PostIndex | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
mads.s.ager31e71382008-08-13 09:32:07 +0000239// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
240// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000241const Instr kPushRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000242 al | B26 | 4 | NegPreIndex | sp.code() * B16;
243// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
244// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000245const Instr kPopRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000246 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000247// mov lr, pc
ager@chromium.org378b34e2011-01-28 08:04:38 +0000248const Instr kMovLrPc = al | MOV | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000249// ldr rd, [pc, #offset]
ager@chromium.org378b34e2011-01-28 08:04:38 +0000250const Instr kLdrPCMask = kCondMask | 15 * B24 | 7 * B20 | 15 * B16;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000251const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
252// blxcc rm
253const Instr kBlxRegMask =
254 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
255const Instr kBlxRegPattern =
ager@chromium.org378b34e2011-01-28 08:04:38 +0000256 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | BLX;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000257const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
258const Instr kMovMvnPattern = 0xd * B21;
259const Instr kMovMvnFlip = B22;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000260const Instr kMovLeaveCCMask = 0xdff * B16;
261const Instr kMovLeaveCCPattern = 0x1a0 * B16;
262const Instr kMovwMask = 0xff * B20;
263const Instr kMovwPattern = 0x30 * B20;
264const Instr kMovwLeaveCCFlip = 0x5 * B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000265const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
266const Instr kCmpCmnPattern = 0x15 * B20;
267const Instr kCmpCmnFlip = B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000268const Instr kAddSubFlip = 0x6 * B21;
269const Instr kAndBicFlip = 0xe * B21;
270
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000271// A mask for the Rd register for push, pop, ldr, str instructions.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000272const Instr kLdrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000273 al | B26 | L | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000274const Instr kStrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000275 al | B26 | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000276const Instr kLdrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000277 al | B26 | L | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000278const Instr kStrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000279 al | B26 | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000280const Instr kLdrStrInstrTypeMask = 0xffff0000;
281const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
282const Instr kLdrStrOffsetMask = 0x00000fff;
283
mads.s.ager31e71382008-08-13 09:32:07 +0000284
ager@chromium.org5c838252010-02-19 08:53:10 +0000285// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286static const int kMinimalBufferSize = 4*KB;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287
ager@chromium.org378b34e2011-01-28 08:04:38 +0000288
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000289Assembler::Assembler(Isolate* arg_isolate, void* buffer, int buffer_size)
290 : AssemblerBase(arg_isolate),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291 positions_recorder_(this),
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000292 allow_peephole_optimization_(false),
293 emit_debug_code_(FLAG_debug_code) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000294 allow_peephole_optimization_ = FLAG_peephole_optimization;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 if (buffer_size <= kMinimalBufferSize) {
298 buffer_size = kMinimalBufferSize;
299
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 if (isolate()->assembler_spare_buffer() != NULL) {
301 buffer = isolate()->assembler_spare_buffer();
302 isolate()->set_assembler_spare_buffer(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 }
304 }
305 if (buffer == NULL) {
306 buffer_ = NewArray<byte>(buffer_size);
307 } else {
308 buffer_ = static_cast<byte*>(buffer);
309 }
310 buffer_size_ = buffer_size;
311 own_buffer_ = true;
312
313 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000314 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 ASSERT(buffer_size > 0);
316 buffer_ = static_cast<byte*>(buffer);
317 buffer_size_ = buffer_size;
318 own_buffer_ = false;
319 }
320
ager@chromium.org5c838252010-02-19 08:53:10 +0000321 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 ASSERT(buffer_ != NULL);
323 pc_ = buffer_;
324 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
325 num_prinfo_ = 0;
326 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000327 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 no_const_pool_before_ = 0;
329 last_const_pool_end_ = 0;
330 last_bound_pos_ = 0;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000331 ast_id_for_reloc_info_ = kNoASTId;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332}
333
334
335Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000336 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 if (own_buffer_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000338 if (isolate()->assembler_spare_buffer() == NULL &&
339 buffer_size_ == kMinimalBufferSize) {
340 isolate()->set_assembler_spare_buffer(buffer_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 } else {
342 DeleteArray(buffer_);
343 }
344 }
345}
346
347
348void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000349 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 CheckConstPool(true, false);
351 ASSERT(num_prinfo_ == 0);
352
ager@chromium.org5c838252010-02-19 08:53:10 +0000353 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 desc->buffer = buffer_;
355 desc->buffer_size = buffer_size_;
356 desc->instr_size = pc_offset();
357 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
358}
359
360
361void Assembler::Align(int m) {
362 ASSERT(m >= 4 && IsPowerOf2(m));
363 while ((pc_offset() & (m - 1)) != 0) {
364 nop();
365 }
366}
367
368
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000369void Assembler::CodeTargetAlign() {
370 // Preferred alignment of jump targets on some ARM chips.
371 Align(8);
372}
373
374
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000375Condition Assembler::GetCondition(Instr instr) {
376 return Instruction::ConditionField(instr);
377}
378
379
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000380bool Assembler::IsBranch(Instr instr) {
381 return (instr & (B27 | B25)) == (B27 | B25);
382}
383
384
385int Assembler::GetBranchOffset(Instr instr) {
386 ASSERT(IsBranch(instr));
387 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
388 // with 4 to get the offset in bytes.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000389 return ((instr & kImm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000390}
391
392
393bool Assembler::IsLdrRegisterImmediate(Instr instr) {
394 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
395}
396
397
398int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
399 ASSERT(IsLdrRegisterImmediate(instr));
400 bool positive = (instr & B23) == B23;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000401 int offset = instr & kOff12Mask; // Zero extended offset.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000402 return positive ? offset : -offset;
403}
404
405
406Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
407 ASSERT(IsLdrRegisterImmediate(instr));
408 bool positive = offset >= 0;
409 if (!positive) offset = -offset;
410 ASSERT(is_uint12(offset));
411 // Set bit indicating whether the offset should be added.
412 instr = (instr & ~B23) | (positive ? B23 : 0);
413 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000414 return (instr & ~kOff12Mask) | offset;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000415}
416
417
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000418bool Assembler::IsStrRegisterImmediate(Instr instr) {
419 return (instr & (B27 | B26 | B25 | B22 | B20)) == B26;
420}
421
422
423Instr Assembler::SetStrRegisterImmediateOffset(Instr instr, int offset) {
424 ASSERT(IsStrRegisterImmediate(instr));
425 bool positive = offset >= 0;
426 if (!positive) offset = -offset;
427 ASSERT(is_uint12(offset));
428 // Set bit indicating whether the offset should be added.
429 instr = (instr & ~B23) | (positive ? B23 : 0);
430 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000431 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000432}
433
434
435bool Assembler::IsAddRegisterImmediate(Instr instr) {
436 return (instr & (B27 | B26 | B25 | B24 | B23 | B22 | B21)) == (B25 | B23);
437}
438
439
440Instr Assembler::SetAddRegisterImmediateOffset(Instr instr, int offset) {
441 ASSERT(IsAddRegisterImmediate(instr));
442 ASSERT(offset >= 0);
443 ASSERT(is_uint12(offset));
444 // Set the offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000445 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000446}
447
448
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000449Register Assembler::GetRd(Instr instr) {
450 Register reg;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000451 reg.code_ = Instruction::RdValue(instr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000452 return reg;
453}
454
455
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000456Register Assembler::GetRn(Instr instr) {
457 Register reg;
458 reg.code_ = Instruction::RnValue(instr);
459 return reg;
460}
461
462
463Register Assembler::GetRm(Instr instr) {
464 Register reg;
465 reg.code_ = Instruction::RmValue(instr);
466 return reg;
467}
468
469
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000470bool Assembler::IsPush(Instr instr) {
471 return ((instr & ~kRdMask) == kPushRegPattern);
472}
473
474
475bool Assembler::IsPop(Instr instr) {
476 return ((instr & ~kRdMask) == kPopRegPattern);
477}
478
479
480bool Assembler::IsStrRegFpOffset(Instr instr) {
481 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
482}
483
484
485bool Assembler::IsLdrRegFpOffset(Instr instr) {
486 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
487}
488
489
490bool Assembler::IsStrRegFpNegOffset(Instr instr) {
491 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
492}
493
494
495bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
496 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
497}
498
499
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000500bool Assembler::IsLdrPcImmediateOffset(Instr instr) {
501 // Check the instruction is indeed a
502 // ldr<cond> <Rd>, [pc +/- offset_12].
ager@chromium.org378b34e2011-01-28 08:04:38 +0000503 return (instr & (kLdrPCMask & ~kCondMask)) == 0x051f0000;
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000504}
505
506
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000507bool Assembler::IsTstImmediate(Instr instr) {
508 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
509 (I | TST | S);
510}
511
512
513bool Assembler::IsCmpRegister(Instr instr) {
514 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask | B4)) ==
515 (CMP | S);
516}
517
518
519bool Assembler::IsCmpImmediate(Instr instr) {
520 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
521 (I | CMP | S);
522}
523
524
525Register Assembler::GetCmpImmediateRegister(Instr instr) {
526 ASSERT(IsCmpImmediate(instr));
527 return GetRn(instr);
528}
529
530
531int Assembler::GetCmpImmediateRawImmediate(Instr instr) {
532 ASSERT(IsCmpImmediate(instr));
533 return instr & kOff12Mask;
534}
535
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536// Labels refer to positions in the (to be) generated code.
537// There are bound, linked, and unused labels.
538//
539// Bound labels refer to known positions in the already
540// generated code. pos() is the position the label refers to.
541//
542// Linked labels refer to unknown positions in the code
543// to be generated; pos() is the position of the last
544// instruction using the label.
545
546
547// The link chain is terminated by a negative code position (must be aligned)
548const int kEndOfChain = -4;
549
550
551int Assembler::target_at(int pos) {
552 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000553 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000554 // Emitted label constant, not part of a branch.
555 return instr - (Code::kHeaderSize - kHeapObjectTag);
556 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000558 int imm26 = ((instr & kImm24Mask) << 8) >> 6;
559 if ((Instruction::ConditionField(instr) == kSpecialCondition) &&
560 ((instr & B24) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 // blx uses bit 24 to encode bit 2 of imm26
562 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000563 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000564 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565}
566
567
568void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000570 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000571 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
572 // Emitted label constant, not part of a branch.
573 // Make label relative to Code* of generated Code object.
574 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
575 return;
576 }
577 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000579 if (Instruction::ConditionField(instr) == kSpecialCondition) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 // blx uses bit 24 to encode bit 2 of imm26
581 ASSERT((imm26 & 1) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000582 instr = (instr & ~(B24 | kImm24Mask)) | ((imm26 & 2) >> 1)*B24;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 } else {
584 ASSERT((imm26 & 3) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000585 instr &= ~kImm24Mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 }
587 int imm24 = imm26 >> 2;
588 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000589 instr_at_put(pos, instr | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590}
591
592
593void Assembler::print(Label* L) {
594 if (L->is_unused()) {
595 PrintF("unused label\n");
596 } else if (L->is_bound()) {
597 PrintF("bound label to %d\n", L->pos());
598 } else if (L->is_linked()) {
599 Label l = *L;
600 PrintF("unbound label");
601 while (l.is_linked()) {
602 PrintF("@ %d ", l.pos());
603 Instr instr = instr_at(l.pos());
ager@chromium.org378b34e2011-01-28 08:04:38 +0000604 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000605 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000607 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
ager@chromium.org378b34e2011-01-28 08:04:38 +0000608 Condition cond = Instruction::ConditionField(instr);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000609 const char* b;
610 const char* c;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000611 if (cond == kSpecialCondition) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000612 b = "blx";
613 c = "";
614 } else {
615 if ((instr & B24) != 0)
616 b = "bl";
617 else
618 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000620 switch (cond) {
621 case eq: c = "eq"; break;
622 case ne: c = "ne"; break;
623 case hs: c = "hs"; break;
624 case lo: c = "lo"; break;
625 case mi: c = "mi"; break;
626 case pl: c = "pl"; break;
627 case vs: c = "vs"; break;
628 case vc: c = "vc"; break;
629 case hi: c = "hi"; break;
630 case ls: c = "ls"; break;
631 case ge: c = "ge"; break;
632 case lt: c = "lt"; break;
633 case gt: c = "gt"; break;
634 case le: c = "le"; break;
635 case al: c = ""; break;
636 default:
637 c = "";
638 UNREACHABLE();
639 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000641 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 next(&l);
644 }
645 } else {
646 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
647 }
648}
649
650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651void Assembler::bind_to(Label* L, int pos) {
652 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
653 while (L->is_linked()) {
654 int fixup_pos = L->pos();
655 next(L); // call next before overwriting link with target at fixup_pos
656 target_at_put(fixup_pos, pos);
657 }
658 L->bind_to(pos);
659
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000660 // Keep track of the last bound label so we don't eliminate any instructions
661 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 if (pos > last_bound_pos_)
663 last_bound_pos_ = pos;
664}
665
666
667void Assembler::link_to(Label* L, Label* appendix) {
668 if (appendix->is_linked()) {
669 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000670 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 int fixup_pos;
672 int link = L->pos();
673 do {
674 fixup_pos = link;
675 link = target_at(fixup_pos);
676 } while (link > 0);
677 ASSERT(link == kEndOfChain);
678 target_at_put(fixup_pos, appendix->pos());
679 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000680 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 *L = *appendix;
682 }
683 }
684 appendix->Unuse(); // appendix should not be used anymore
685}
686
687
688void Assembler::bind(Label* L) {
689 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 bind_to(L, pc_offset());
691}
692
693
694void Assembler::next(Label* L) {
695 ASSERT(L->is_linked());
696 int link = target_at(L->pos());
697 if (link > 0) {
698 L->link_to(link);
699 } else {
700 ASSERT(link == kEndOfChain);
701 L->Unuse();
702 }
703}
704
705
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000706static Instr EncodeMovwImmediate(uint32_t immediate) {
707 ASSERT(immediate < 0x10000);
708 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
709}
710
711
ager@chromium.org5c838252010-02-19 08:53:10 +0000712// Low-level code emission routines depending on the addressing mode.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000713// If this returns true then you have to use the rotate_imm and immed_8
714// that it returns, because it may have already changed the instruction
715// to match them!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716static bool fits_shifter(uint32_t imm32,
717 uint32_t* rotate_imm,
718 uint32_t* immed_8,
719 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000720 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 for (int rot = 0; rot < 16; rot++) {
722 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
723 if ((imm8 <= 0xff)) {
724 *rotate_imm = rot;
725 *immed_8 = imm8;
726 return true;
727 }
728 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000729 // If the opcode is one with a complementary version and the complementary
730 // immediate fits, change the opcode.
731 if (instr != NULL) {
732 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
733 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
734 *instr ^= kMovMvnFlip;
735 return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000736 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000737 if (CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000738 if (imm32 < 0x10000) {
739 *instr ^= kMovwLeaveCCFlip;
740 *instr |= EncodeMovwImmediate(imm32);
741 *rotate_imm = *immed_8 = 0; // Not used for movw.
742 return true;
743 }
744 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000745 }
746 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
747 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
748 *instr ^= kCmpCmnFlip;
749 return true;
750 }
751 } else {
752 Instr alu_insn = (*instr & kALUMask);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000753 if (alu_insn == ADD ||
754 alu_insn == SUB) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000755 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
756 *instr ^= kAddSubFlip;
757 return true;
758 }
ager@chromium.org378b34e2011-01-28 08:04:38 +0000759 } else if (alu_insn == AND ||
760 alu_insn == BIC) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000761 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
762 *instr ^= kAndBicFlip;
763 return true;
764 }
765 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 }
767 }
768 return false;
769}
770
771
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000772// We have to use the temporary register for things that can be relocated even
773// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
774// space. There is no guarantee that the relocated location can be similarly
775// encoded.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000776bool Operand::must_use_constant_pool() const {
777 if (rmode_ == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000778#ifdef DEBUG
779 if (!Serializer::enabled()) {
780 Serializer::TooLateToEnableNow();
781 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000782#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000783 return Serializer::enabled();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000784 } else if (rmode_ == RelocInfo::NONE) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000785 return false;
786 }
787 return true;
788}
789
790
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000791bool Operand::is_single_instruction(Instr instr) const {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000792 if (rm_.is_valid()) return true;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000793 uint32_t dummy1, dummy2;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000794 if (must_use_constant_pool() ||
795 !fits_shifter(imm32_, &dummy1, &dummy2, &instr)) {
796 // The immediate operand cannot be encoded as a shifter operand, or use of
797 // constant pool is required. For a mov instruction not setting the
798 // condition code additional instruction conventions can be used.
799 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000800 if (must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000801 !CpuFeatures::IsSupported(ARMv7)) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000802 // mov instruction will be an ldr from constant pool (one instruction).
803 return true;
804 } else {
805 // mov instruction will be a mov or movw followed by movt (two
806 // instructions).
807 return false;
808 }
809 } else {
810 // If this is not a mov or mvn instruction there will always an additional
811 // instructions - either mov or ldr. The mov might actually be two
812 // instructions mov or movw followed by movt so including the actual
813 // instruction two or three instructions will be generated.
814 return false;
815 }
816 } else {
817 // No use of constant pool and the immediate operand can be encoded as a
818 // shifter operand.
819 return true;
820 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000821}
822
823
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824void Assembler::addrmod1(Instr instr,
825 Register rn,
826 Register rd,
827 const Operand& x) {
828 CheckBuffer();
ager@chromium.org378b34e2011-01-28 08:04:38 +0000829 ASSERT((instr & ~(kCondMask | kOpCodeMask | S)) == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000831 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 uint32_t rotate_imm;
833 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000834 if (x.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
836 // The immediate operand cannot be encoded as a shifter operand, so load
837 // it first to register ip and change the original instruction to use ip.
838 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000839 // condition code), then replace it with a 'ldr rd, [pc]'.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000840 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
ager@chromium.org378b34e2011-01-28 08:04:38 +0000841 Condition cond = Instruction::ConditionField(instr);
842 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000843 if (x.must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000844 !CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000845 RecordRelocInfo(x.rmode_, x.imm32_);
846 ldr(rd, MemOperand(pc, 0), cond);
847 } else {
848 // Will probably use movw, will certainly not use constant pool.
849 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
850 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
851 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000853 // If this is not a mov or mvn instruction we may still be able to avoid
854 // a constant pool entry by using mvn or movw.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000855 if (!x.must_use_constant_pool() &&
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000856 (instr & kMovMvnMask) != kMovMvnPattern) {
857 mov(ip, x, LeaveCC, cond);
858 } else {
859 RecordRelocInfo(x.rmode_, x.imm32_);
860 ldr(ip, MemOperand(pc, 0), cond);
861 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 addrmod1(instr, rn, rd, Operand(ip));
863 }
864 return;
865 }
866 instr |= I | rotate_imm*B8 | immed_8;
867 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000868 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
870 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000871 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
873 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
874 }
875 emit(instr | rn.code()*B16 | rd.code()*B12);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000876 if (rn.is(pc) || x.rm_.is(pc)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000877 // Block constant pool emission for one instruction after reading pc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 BlockConstPoolBefore(pc_offset() + kInstrSize);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000879 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880}
881
882
883void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000884 ASSERT((instr & ~(kCondMask | B | L)) == B26);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 int am = x.am_;
886 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000887 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 int offset_12 = x.offset_;
889 if (offset_12 < 0) {
890 offset_12 = -offset_12;
891 am ^= U;
892 }
893 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000894 // Immediate offset cannot be encoded, load it first to register ip
895 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000897 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
899 return;
900 }
901 ASSERT(offset_12 >= 0); // no masking needed
902 instr |= offset_12;
903 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000904 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000906 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 ASSERT(!x.rm_.is(pc));
908 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
909 }
910 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
911 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
912}
913
914
915void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000916 ASSERT((instr & ~(kCondMask | L | S6 | H)) == (B4 | B7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 ASSERT(x.rn_.is_valid());
918 int am = x.am_;
919 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000920 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 int offset_8 = x.offset_;
922 if (offset_8 < 0) {
923 offset_8 = -offset_8;
924 am ^= U;
925 }
926 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000927 // Immediate offset cannot be encoded, load it first to register ip
928 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000930 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
932 return;
933 }
934 ASSERT(offset_8 >= 0); // no masking needed
935 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
936 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000937 // Scaled register offset not supported, load index first
938 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
940 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
ager@chromium.org378b34e2011-01-28 08:04:38 +0000941 Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
943 return;
944 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000945 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
947 instr |= x.rm_.code();
948 }
949 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
950 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
951}
952
953
954void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000955 ASSERT((instr & ~(kCondMask | P | U | W | L)) == B27);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 ASSERT(rl != 0);
957 ASSERT(!rn.is(pc));
958 emit(instr | rn.code()*B16 | rl);
959}
960
961
962void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000963 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000964 ASSERT_EQ((B27 | B26),
ager@chromium.org378b34e2011-01-28 08:04:38 +0000965 (instr & ~(kCondMask | kCoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
967 int am = x.am_;
968 int offset_8 = x.offset_;
969 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
970 offset_8 >>= 2;
971 if (offset_8 < 0) {
972 offset_8 = -offset_8;
973 am ^= U;
974 }
975 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
976 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
977
ager@chromium.org5c838252010-02-19 08:53:10 +0000978 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 if ((am & P) == 0)
980 am |= W;
981
982 ASSERT(offset_8 >= 0); // no masking needed
983 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
984}
985
986
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000987int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 int target_pos;
989 if (L->is_bound()) {
990 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000992 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000994 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000996 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 L->link_to(pc_offset());
998 }
999
1000 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +00001001 // be emitted at the pc offset recorded by the label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 BlockConstPoolBefore(pc_offset() + kInstrSize);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001003 return target_pos - (pc_offset() + kPcLoadDelta);
1004}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001006
1007void Assembler::label_at_put(Label* L, int at_offset) {
1008 int target_pos;
1009 if (L->is_bound()) {
1010 target_pos = L->pos();
1011 } else {
1012 if (L->is_linked()) {
1013 target_pos = L->pos(); // L's link
1014 } else {
1015 target_pos = kEndOfChain;
1016 }
1017 L->link_to(at_offset);
1018 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
1019 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020}
1021
1022
ager@chromium.org5c838252010-02-19 08:53:10 +00001023// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024void Assembler::b(int branch_offset, Condition cond) {
1025 ASSERT((branch_offset & 3) == 0);
1026 int imm24 = branch_offset >> 2;
1027 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001028 emit(cond | B27 | B25 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001030 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001031 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001033 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034}
1035
1036
1037void Assembler::bl(int branch_offset, Condition cond) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001038 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 ASSERT((branch_offset & 3) == 0);
1040 int imm24 = branch_offset >> 2;
1041 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001042 emit(cond | B27 | B25 | B24 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043}
1044
1045
1046void Assembler::blx(int branch_offset) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001047 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 ASSERT((branch_offset & 1) == 0);
1049 int h = ((branch_offset & 2) >> 1)*B24;
1050 int imm24 = branch_offset >> 2;
1051 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001052 emit(kSpecialCondition | B27 | B25 | h | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053}
1054
1055
1056void Assembler::blx(Register target, Condition cond) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001057 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 ASSERT(!target.is(pc));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001059 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BLX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060}
1061
1062
1063void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001064 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
ager@chromium.org378b34e2011-01-28 08:04:38 +00001066 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067}
1068
1069
ager@chromium.org5c838252010-02-19 08:53:10 +00001070// Data-processing instructions.
1071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072void Assembler::and_(Register dst, Register src1, const Operand& src2,
1073 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001074 addrmod1(cond | AND | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075}
1076
1077
1078void Assembler::eor(Register dst, Register src1, const Operand& src2,
1079 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001080 addrmod1(cond | EOR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081}
1082
1083
1084void Assembler::sub(Register dst, Register src1, const Operand& src2,
1085 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001086 addrmod1(cond | SUB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087}
1088
1089
1090void Assembler::rsb(Register dst, Register src1, const Operand& src2,
1091 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001092 addrmod1(cond | RSB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093}
1094
1095
1096void Assembler::add(Register dst, Register src1, const Operand& src2,
1097 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001098 addrmod1(cond | ADD | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +00001099
1100 // Eliminate pattern: push(r), pop()
1101 // str(src, MemOperand(sp, 4, NegPreIndex), al);
1102 // add(sp, sp, Operand(kPointerSize));
1103 // Both instructions can be eliminated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001104 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001105 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001106 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
ager@chromium.org378b34e2011-01-28 08:04:38 +00001107 (instr_at(pc_ - 2 * kInstrSize) & ~kRdMask) == kPushRegPattern) {
mads.s.ager31e71382008-08-13 09:32:07 +00001108 pc_ -= 2 * kInstrSize;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001109 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001110 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
1111 }
1112 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113}
1114
1115
1116void Assembler::adc(Register dst, Register src1, const Operand& src2,
1117 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001118 addrmod1(cond | ADC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119}
1120
1121
1122void Assembler::sbc(Register dst, Register src1, const Operand& src2,
1123 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001124 addrmod1(cond | SBC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125}
1126
1127
1128void Assembler::rsc(Register dst, Register src1, const Operand& src2,
1129 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001130 addrmod1(cond | RSC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131}
1132
1133
1134void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001135 addrmod1(cond | TST | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136}
1137
1138
1139void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001140 addrmod1(cond | TEQ | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141}
1142
1143
1144void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001145 addrmod1(cond | CMP | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146}
1147
1148
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001149void Assembler::cmp_raw_immediate(
1150 Register src, int raw_immediate, Condition cond) {
1151 ASSERT(is_uint12(raw_immediate));
1152 emit(cond | I | CMP | S | src.code() << 16 | raw_immediate);
1153}
1154
1155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001157 addrmod1(cond | CMN | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158}
1159
1160
1161void Assembler::orr(Register dst, Register src1, const Operand& src2,
1162 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001163 addrmod1(cond | ORR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164}
1165
1166
1167void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001168 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001169 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001170 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001171 // Don't allow nop instructions in the form mov rn, rn to be generated using
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001172 // the mov instruction. They must be generated using nop(int/NopMarkerTypes)
1173 // or MarkCode(int/NopMarkerTypes) pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001174 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001175 addrmod1(cond | MOV | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176}
1177
1178
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001179void Assembler::movw(Register reg, uint32_t immediate, Condition cond) {
1180 ASSERT(immediate < 0x10000);
1181 mov(reg, Operand(immediate), LeaveCC, cond);
1182}
1183
1184
1185void Assembler::movt(Register reg, uint32_t immediate, Condition cond) {
1186 emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate));
1187}
1188
1189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190void Assembler::bic(Register dst, Register src1, const Operand& src2,
1191 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001192 addrmod1(cond | BIC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193}
1194
1195
1196void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001197 addrmod1(cond | MVN | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198}
1199
1200
ager@chromium.org5c838252010-02-19 08:53:10 +00001201// Multiply instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1203 SBit s, Condition cond) {
1204 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1206 src2.code()*B8 | B7 | B4 | src1.code());
1207}
1208
1209
1210void Assembler::mul(Register dst, Register src1, Register src2,
1211 SBit s, Condition cond) {
1212 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001213 // dst goes in bits 16-19 for this instruction!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1215}
1216
1217
1218void Assembler::smlal(Register dstL,
1219 Register dstH,
1220 Register src1,
1221 Register src2,
1222 SBit s,
1223 Condition cond) {
1224 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001225 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1227 src2.code()*B8 | B7 | B4 | src1.code());
1228}
1229
1230
1231void Assembler::smull(Register dstL,
1232 Register dstH,
1233 Register src1,
1234 Register src2,
1235 SBit s,
1236 Condition cond) {
1237 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001238 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1240 src2.code()*B8 | B7 | B4 | src1.code());
1241}
1242
1243
1244void Assembler::umlal(Register dstL,
1245 Register dstH,
1246 Register src1,
1247 Register src2,
1248 SBit s,
1249 Condition cond) {
1250 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001251 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1253 src2.code()*B8 | B7 | B4 | src1.code());
1254}
1255
1256
1257void Assembler::umull(Register dstL,
1258 Register dstH,
1259 Register src1,
1260 Register src2,
1261 SBit s,
1262 Condition cond) {
1263 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001264 ASSERT(!dstL.is(dstH));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001265 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 src2.code()*B8 | B7 | B4 | src1.code());
1267}
1268
1269
ager@chromium.org5c838252010-02-19 08:53:10 +00001270// Miscellaneous arithmetic instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271void Assembler::clz(Register dst, Register src, Condition cond) {
1272 // v5 and above.
1273 ASSERT(!dst.is(pc) && !src.is(pc));
1274 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
ager@chromium.org378b34e2011-01-28 08:04:38 +00001275 15*B8 | CLZ | src.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276}
1277
1278
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001279// Saturating instructions.
1280
1281// Unsigned saturate.
1282void Assembler::usat(Register dst,
1283 int satpos,
1284 const Operand& src,
1285 Condition cond) {
1286 // v6 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001287 ASSERT(CpuFeatures::IsSupported(ARMv7));
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001288 ASSERT(!dst.is(pc) && !src.rm_.is(pc));
1289 ASSERT((satpos >= 0) && (satpos <= 31));
1290 ASSERT((src.shift_op_ == ASR) || (src.shift_op_ == LSL));
1291 ASSERT(src.rs_.is(no_reg));
1292
1293 int sh = 0;
1294 if (src.shift_op_ == ASR) {
1295 sh = 1;
1296 }
1297
1298 emit(cond | 0x6*B24 | 0xe*B20 | satpos*B16 | dst.code()*B12 |
1299 src.shift_imm_*B7 | sh*B6 | 0x1*B4 | src.rm_.code());
1300}
1301
1302
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001303// Bitfield manipulation instructions.
1304
1305// Unsigned bit field extract.
1306// Extracts #width adjacent bits from position #lsb in a register, and
1307// writes them to the low bits of a destination register.
1308// ubfx dst, src, #lsb, #width
1309void Assembler::ubfx(Register dst,
1310 Register src,
1311 int lsb,
1312 int width,
1313 Condition cond) {
1314 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001315 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001316 ASSERT(!dst.is(pc) && !src.is(pc));
1317 ASSERT((lsb >= 0) && (lsb <= 31));
1318 ASSERT((width >= 1) && (width <= (32 - lsb)));
1319 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1320 lsb*B7 | B6 | B4 | src.code());
1321}
1322
1323
1324// Signed bit field extract.
1325// Extracts #width adjacent bits from position #lsb in a register, and
1326// writes them to the low bits of a destination register. The extracted
1327// value is sign extended to fill the destination register.
1328// sbfx dst, src, #lsb, #width
1329void Assembler::sbfx(Register dst,
1330 Register src,
1331 int lsb,
1332 int width,
1333 Condition cond) {
1334 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001335 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001336 ASSERT(!dst.is(pc) && !src.is(pc));
1337 ASSERT((lsb >= 0) && (lsb <= 31));
1338 ASSERT((width >= 1) && (width <= (32 - lsb)));
1339 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1340 lsb*B7 | B6 | B4 | src.code());
1341}
1342
1343
1344// Bit field clear.
1345// Sets #width adjacent bits at position #lsb in the destination register
1346// to zero, preserving the value of the other bits.
1347// bfc dst, #lsb, #width
1348void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1349 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001350 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001351 ASSERT(!dst.is(pc));
1352 ASSERT((lsb >= 0) && (lsb <= 31));
1353 ASSERT((width >= 1) && (width <= (32 - lsb)));
1354 int msb = lsb + width - 1;
1355 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1356}
1357
1358
1359// Bit field insert.
1360// Inserts #width adjacent bits from the low bits of the source register
1361// into position #lsb of the destination register.
1362// bfi dst, src, #lsb, #width
1363void Assembler::bfi(Register dst,
1364 Register src,
1365 int lsb,
1366 int width,
1367 Condition cond) {
1368 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001369 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001370 ASSERT(!dst.is(pc) && !src.is(pc));
1371 ASSERT((lsb >= 0) && (lsb <= 31));
1372 ASSERT((width >= 1) && (width <= (32 - lsb)));
1373 int msb = lsb + width - 1;
1374 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1375 src.code());
1376}
1377
1378
ager@chromium.org5c838252010-02-19 08:53:10 +00001379// Status register access instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1381 ASSERT(!dst.is(pc));
1382 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1383}
1384
1385
1386void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1387 Condition cond) {
1388 ASSERT(fields >= B16 && fields < B20); // at least one field set
1389 Instr instr;
1390 if (!src.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001391 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 uint32_t rotate_imm;
1393 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001394 if (src.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001396 // Immediate operand cannot be encoded, load it first to register ip.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 RecordRelocInfo(src.rmode_, src.imm32_);
1398 ldr(ip, MemOperand(pc, 0), cond);
1399 msr(fields, Operand(ip), cond);
1400 return;
1401 }
1402 instr = I | rotate_imm*B8 | immed_8;
1403 } else {
1404 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1405 instr = src.rm_.code();
1406 }
1407 emit(cond | instr | B24 | B21 | fields | 15*B12);
1408}
1409
1410
ager@chromium.org5c838252010-02-19 08:53:10 +00001411// Load/Store instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001413 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001414 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001415 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +00001417
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001418 // Eliminate pattern: push(ry), pop(rx)
1419 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1420 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1421 // Both instructions can be eliminated if ry = rx.
1422 // If ry != rx, a register copy from ry to rx is inserted
1423 // after eliminating the push and the pop instructions.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001424 if (can_peephole_optimize(2)) {
1425 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1426 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001427
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001428 if (IsPush(push_instr) && IsPop(pop_instr)) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001429 if (Instruction::RdValue(pop_instr) != Instruction::RdValue(push_instr)) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001430 // For consecutive push and pop on different registers,
1431 // we delete both the push & pop and insert a register move.
1432 // push ry, pop rx --> mov rx, ry
1433 Register reg_pushed, reg_popped;
1434 reg_pushed = GetRd(push_instr);
1435 reg_popped = GetRd(pop_instr);
1436 pc_ -= 2 * kInstrSize;
1437 // Insert a mov instruction, which is better than a pair of push & pop
1438 mov(reg_popped, reg_pushed);
1439 if (FLAG_print_peephole_optimization) {
1440 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1441 pc_offset());
1442 }
1443 } else {
1444 // For consecutive push and pop on the same register,
1445 // both the push and the pop can be deleted.
1446 pc_ -= 2 * kInstrSize;
1447 if (FLAG_print_peephole_optimization) {
1448 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1449 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001450 }
1451 }
1452 }
1453
1454 if (can_peephole_optimize(2)) {
1455 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1456 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1457
1458 if ((IsStrRegFpOffset(str_instr) &&
1459 IsLdrRegFpOffset(ldr_instr)) ||
1460 (IsStrRegFpNegOffset(str_instr) &&
1461 IsLdrRegFpNegOffset(ldr_instr))) {
1462 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1463 (str_instr & kLdrStrInstrArgumentMask)) {
1464 // Pattern: Ldr/str same fp+offset, same register.
1465 //
1466 // The following:
1467 // str rx, [fp, #-12]
1468 // ldr rx, [fp, #-12]
1469 //
1470 // Becomes:
1471 // str rx, [fp, #-12]
1472
1473 pc_ -= 1 * kInstrSize;
1474 if (FLAG_print_peephole_optimization) {
1475 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1476 }
1477 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1478 (str_instr & kLdrStrOffsetMask)) {
1479 // Pattern: Ldr/str same fp+offset, different register.
1480 //
1481 // The following:
1482 // str rx, [fp, #-12]
1483 // ldr ry, [fp, #-12]
1484 //
1485 // Becomes:
1486 // str rx, [fp, #-12]
1487 // mov ry, rx
1488
1489 Register reg_stored, reg_loaded;
1490 reg_stored = GetRd(str_instr);
1491 reg_loaded = GetRd(ldr_instr);
1492 pc_ -= 1 * kInstrSize;
1493 // Insert a mov instruction, which is better than ldr.
1494 mov(reg_loaded, reg_stored);
1495 if (FLAG_print_peephole_optimization) {
1496 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1497 }
1498 }
1499 }
1500 }
1501
1502 if (can_peephole_optimize(3)) {
1503 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1504 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1505 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1506 if (IsPush(mem_write_instr) &&
1507 IsPop(mem_read_instr)) {
1508 if ((IsLdrRegFpOffset(ldr_instr) ||
1509 IsLdrRegFpNegOffset(ldr_instr))) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001510 if (Instruction::RdValue(mem_write_instr) ==
1511 Instruction::RdValue(mem_read_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001512 // Pattern: push & pop from/to same register,
1513 // with a fp+offset ldr in between
1514 //
1515 // The following:
1516 // str rx, [sp, #-4]!
1517 // ldr rz, [fp, #-24]
1518 // ldr rx, [sp], #+4
1519 //
1520 // Becomes:
1521 // if(rx == rz)
1522 // delete all
1523 // else
1524 // ldr rz, [fp, #-24]
1525
ager@chromium.org378b34e2011-01-28 08:04:38 +00001526 if (Instruction::RdValue(mem_write_instr) ==
1527 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001528 pc_ -= 3 * kInstrSize;
1529 } else {
1530 pc_ -= 3 * kInstrSize;
1531 // Reinsert back the ldr rz.
1532 emit(ldr_instr);
1533 }
1534 if (FLAG_print_peephole_optimization) {
1535 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1536 }
1537 } else {
1538 // Pattern: push & pop from/to different registers
1539 // with a fp+offset ldr in between
1540 //
1541 // The following:
1542 // str rx, [sp, #-4]!
1543 // ldr rz, [fp, #-24]
1544 // ldr ry, [sp], #+4
1545 //
1546 // Becomes:
1547 // if(ry == rz)
1548 // mov ry, rx;
1549 // else if(rx != rz)
1550 // ldr rz, [fp, #-24]
1551 // mov ry, rx
1552 // else if((ry != rz) || (rx == rz)) becomes:
1553 // mov ry, rx
1554 // ldr rz, [fp, #-24]
1555
1556 Register reg_pushed, reg_popped;
ager@chromium.org378b34e2011-01-28 08:04:38 +00001557 if (Instruction::RdValue(mem_read_instr) ==
1558 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001559 reg_pushed = GetRd(mem_write_instr);
1560 reg_popped = GetRd(mem_read_instr);
1561 pc_ -= 3 * kInstrSize;
1562 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001563 } else if (Instruction::RdValue(mem_write_instr) !=
1564 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001565 reg_pushed = GetRd(mem_write_instr);
1566 reg_popped = GetRd(mem_read_instr);
1567 pc_ -= 3 * kInstrSize;
1568 emit(ldr_instr);
1569 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001570 } else if ((Instruction::RdValue(mem_read_instr) !=
1571 Instruction::RdValue(ldr_instr)) ||
1572 (Instruction::RdValue(mem_write_instr) ==
1573 Instruction::RdValue(ldr_instr))) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001574 reg_pushed = GetRd(mem_write_instr);
1575 reg_popped = GetRd(mem_read_instr);
1576 pc_ -= 3 * kInstrSize;
1577 mov(reg_popped, reg_pushed);
1578 emit(ldr_instr);
1579 }
1580 if (FLAG_print_peephole_optimization) {
1581 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1582 }
1583 }
1584 }
mads.s.ager31e71382008-08-13 09:32:07 +00001585 }
1586 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587}
1588
1589
1590void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1591 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +00001592
1593 // Eliminate pattern: pop(), push(r)
1594 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1595 // -> str r, [sp, 0], al
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001596 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001597 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001598 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1599 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1600 pc_ -= 2 * kInstrSize;
1601 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001602 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001603 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1604 }
1605 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606}
1607
1608
1609void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1610 addrmod2(cond | B26 | B | L, dst, src);
1611}
1612
1613
1614void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1615 addrmod2(cond | B26 | B, src, dst);
1616}
1617
1618
1619void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1620 addrmod3(cond | L | B7 | H | B4, dst, src);
1621}
1622
1623
1624void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1625 addrmod3(cond | B7 | H | B4, src, dst);
1626}
1627
1628
1629void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1630 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1631}
1632
1633
1634void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1635 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1636}
1637
1638
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001639void Assembler::ldrd(Register dst1, Register dst2,
1640 const MemOperand& src, Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001641 ASSERT(CpuFeatures::IsEnabled(ARMv7));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001642 ASSERT(src.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001643 ASSERT(!dst1.is(lr)); // r14.
1644 ASSERT_EQ(0, dst1.code() % 2);
1645 ASSERT_EQ(dst1.code() + 1, dst2.code());
1646 addrmod3(cond | B7 | B6 | B4, dst1, src);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001647}
1648
1649
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001650void Assembler::strd(Register src1, Register src2,
1651 const MemOperand& dst, Condition cond) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001652 ASSERT(dst.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001653 ASSERT(!src1.is(lr)); // r14.
1654 ASSERT_EQ(0, src1.code() % 2);
1655 ASSERT_EQ(src1.code() + 1, src2.code());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001656 ASSERT(CpuFeatures::IsEnabled(ARMv7));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001657 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001658}
1659
ager@chromium.org5c838252010-02-19 08:53:10 +00001660// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661void Assembler::ldm(BlockAddrMode am,
1662 Register base,
1663 RegList dst,
1664 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001665 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1667
1668 addrmod4(cond | B27 | am | L, base, dst);
1669
ager@chromium.org5c838252010-02-19 08:53:10 +00001670 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 if (cond == al && (dst & pc.bit()) != 0) {
1672 // There is a slight chance that the ldm instruction was actually a call,
1673 // in which case it would be wrong to return into the constant pool; we
1674 // recognize this case by checking if the emission of the pool was blocked
1675 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1676 // the case, we emit a jump over the pool.
1677 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1678 }
1679}
1680
1681
1682void Assembler::stm(BlockAddrMode am,
1683 Register base,
1684 RegList src,
1685 Condition cond) {
1686 addrmod4(cond | B27 | am, base, src);
1687}
1688
1689
ager@chromium.org5c838252010-02-19 08:53:10 +00001690// Exception-generating instructions and debugging support.
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001691// Stops with a non-negative code less than kNumOfWatchedStops support
1692// enabling/disabling and a counter feature. See simulator-arm.h .
1693void Assembler::stop(const char* msg, Condition cond, int32_t code) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001694#ifndef __arm__
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001695 ASSERT(code >= kDefaultStopCode);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001696 // The Simulator will handle the stop instruction and get the message address.
1697 // It expects to find the address just after the svc instruction.
1698 BlockConstPoolFor(2);
1699 if (code >= 0) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001700 svc(kStopCode + code, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001701 } else {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001702 svc(kStopCode + kMaxStopCode, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001703 }
1704 emit(reinterpret_cast<Instr>(msg));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001705#else // def __arm__
1706#ifdef CAN_USE_ARMV5_INSTRUCTIONS
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001707 if (cond != al) {
1708 Label skip;
1709 b(&skip, NegateCondition(cond));
1710 bkpt(0);
1711 bind(&skip);
1712 } else {
1713 bkpt(0);
1714 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001715#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001716 svc(0x9f0001, cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001717#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1718#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719}
1720
1721
1722void Assembler::bkpt(uint32_t imm16) { // v5 and above
1723 ASSERT(is_uint16(imm16));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001724 emit(al | B24 | B21 | (imm16 >> 4)*B8 | BKPT | (imm16 & 0xf));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725}
1726
1727
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001728void Assembler::svc(uint32_t imm24, Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 ASSERT(is_uint24(imm24));
1730 emit(cond | 15*B24 | imm24);
1731}
1732
1733
ager@chromium.org5c838252010-02-19 08:53:10 +00001734// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735void Assembler::cdp(Coprocessor coproc,
1736 int opcode_1,
1737 CRegister crd,
1738 CRegister crn,
1739 CRegister crm,
1740 int opcode_2,
1741 Condition cond) {
1742 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1743 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1744 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1745}
1746
1747
1748void Assembler::cdp2(Coprocessor coproc,
1749 int opcode_1,
1750 CRegister crd,
1751 CRegister crn,
1752 CRegister crm,
1753 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001754 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755}
1756
1757
1758void Assembler::mcr(Coprocessor coproc,
1759 int opcode_1,
1760 Register rd,
1761 CRegister crn,
1762 CRegister crm,
1763 int opcode_2,
1764 Condition cond) {
1765 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1766 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1767 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1768}
1769
1770
1771void Assembler::mcr2(Coprocessor coproc,
1772 int opcode_1,
1773 Register rd,
1774 CRegister crn,
1775 CRegister crm,
1776 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001777 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778}
1779
1780
1781void Assembler::mrc(Coprocessor coproc,
1782 int opcode_1,
1783 Register rd,
1784 CRegister crn,
1785 CRegister crm,
1786 int opcode_2,
1787 Condition cond) {
1788 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1789 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1790 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1791}
1792
1793
1794void Assembler::mrc2(Coprocessor coproc,
1795 int opcode_1,
1796 Register rd,
1797 CRegister crn,
1798 CRegister crm,
1799 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001800 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801}
1802
1803
1804void Assembler::ldc(Coprocessor coproc,
1805 CRegister crd,
1806 const MemOperand& src,
1807 LFlag l,
1808 Condition cond) {
1809 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1810}
1811
1812
1813void Assembler::ldc(Coprocessor coproc,
1814 CRegister crd,
1815 Register rn,
1816 int option,
1817 LFlag l,
1818 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001819 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 ASSERT(is_uint8(option));
1821 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1822 coproc*B8 | (option & 255));
1823}
1824
1825
1826void Assembler::ldc2(Coprocessor coproc,
1827 CRegister crd,
1828 const MemOperand& src,
1829 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001830 ldc(coproc, crd, src, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831}
1832
1833
1834void Assembler::ldc2(Coprocessor coproc,
1835 CRegister crd,
1836 Register rn,
1837 int option,
1838 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001839 ldc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840}
1841
1842
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001843// Support for VFP.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001844
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001845void Assembler::vldr(const DwVfpRegister dst,
1846 const Register base,
1847 int offset,
1848 const Condition cond) {
1849 // Ddst = MEM(Rbase + offset).
1850 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001851 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001852 // Vdst(15-12) | 1011(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001853 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001854 int u = 1;
1855 if (offset < 0) {
1856 offset = -offset;
1857 u = 0;
1858 }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001859
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001860 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001861 if ((offset % 4) == 0 && (offset / 4) < 256) {
1862 emit(cond | u*B23 | 0xD1*B20 | base.code()*B16 | dst.code()*B12 |
1863 0xB*B8 | ((offset / 4) & 255));
1864 } else {
1865 // Larger offsets must be handled by computing the correct address
1866 // in the ip register.
1867 ASSERT(!base.is(ip));
1868 if (u == 1) {
1869 add(ip, base, Operand(offset));
1870 } else {
1871 sub(ip, base, Operand(offset));
1872 }
1873 emit(cond | 0xD1*B20 | ip.code()*B16 | dst.code()*B12 | 0xB*B8);
1874 }
1875}
1876
1877
1878void Assembler::vldr(const DwVfpRegister dst,
1879 const MemOperand& operand,
1880 const Condition cond) {
1881 ASSERT(!operand.rm().is_valid());
1882 ASSERT(operand.am_ == Offset);
1883 vldr(dst, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001884}
1885
1886
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001887void Assembler::vldr(const SwVfpRegister dst,
1888 const Register base,
1889 int offset,
1890 const Condition cond) {
1891 // Sdst = MEM(Rbase + offset).
1892 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001893 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001894 // Vdst(15-12) | 1010(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001895 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001896 int u = 1;
1897 if (offset < 0) {
1898 offset = -offset;
1899 u = 0;
1900 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001901 int sd, d;
1902 dst.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001903 ASSERT(offset >= 0);
1904
1905 if ((offset % 4) == 0 && (offset / 4) < 256) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001906 emit(cond | u*B23 | d*B22 | 0xD1*B20 | base.code()*B16 | sd*B12 |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001907 0xA*B8 | ((offset / 4) & 255));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001908 } else {
1909 // Larger offsets must be handled by computing the correct address
1910 // in the ip register.
1911 ASSERT(!base.is(ip));
1912 if (u == 1) {
1913 add(ip, base, Operand(offset));
1914 } else {
1915 sub(ip, base, Operand(offset));
1916 }
1917 emit(cond | d*B22 | 0xD1*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
1918 }
1919}
1920
1921
1922void Assembler::vldr(const SwVfpRegister dst,
1923 const MemOperand& operand,
1924 const Condition cond) {
1925 ASSERT(!operand.rm().is_valid());
1926 ASSERT(operand.am_ == Offset);
1927 vldr(dst, operand.rn(), operand.offset(), cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001928}
1929
1930
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001931void Assembler::vstr(const DwVfpRegister src,
1932 const Register base,
1933 int offset,
1934 const Condition cond) {
1935 // MEM(Rbase + offset) = Dsrc.
1936 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001937 // cond(31-28) | 1101(27-24)| U000(23-20) | | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001938 // Vsrc(15-12) | 1011(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001939 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001940 int u = 1;
1941 if (offset < 0) {
1942 offset = -offset;
1943 u = 0;
1944 }
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001945 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001946 if ((offset % 4) == 0 && (offset / 4) < 256) {
1947 emit(cond | u*B23 | 0xD0*B20 | base.code()*B16 | src.code()*B12 |
1948 0xB*B8 | ((offset / 4) & 255));
1949 } else {
1950 // Larger offsets must be handled by computing the correct address
1951 // in the ip register.
1952 ASSERT(!base.is(ip));
1953 if (u == 1) {
1954 add(ip, base, Operand(offset));
1955 } else {
1956 sub(ip, base, Operand(offset));
1957 }
1958 emit(cond | 0xD0*B20 | ip.code()*B16 | src.code()*B12 | 0xB*B8);
1959 }
1960}
1961
1962
1963void Assembler::vstr(const DwVfpRegister src,
1964 const MemOperand& operand,
1965 const Condition cond) {
1966 ASSERT(!operand.rm().is_valid());
1967 ASSERT(operand.am_ == Offset);
1968 vstr(src, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001969}
1970
1971
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001972void Assembler::vstr(const SwVfpRegister src,
1973 const Register base,
1974 int offset,
1975 const Condition cond) {
1976 // MEM(Rbase + offset) = SSrc.
1977 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001978 // cond(31-28) | 1101(27-24)| U000(23-20) | Rbase(19-16) |
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001979 // Vdst(15-12) | 1010(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001980 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001981 int u = 1;
1982 if (offset < 0) {
1983 offset = -offset;
1984 u = 0;
1985 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001986 int sd, d;
1987 src.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001988 ASSERT(offset >= 0);
1989 if ((offset % 4) == 0 && (offset / 4) < 256) {
1990 emit(cond | u*B23 | d*B22 | 0xD0*B20 | base.code()*B16 | sd*B12 |
1991 0xA*B8 | ((offset / 4) & 255));
1992 } else {
1993 // Larger offsets must be handled by computing the correct address
1994 // in the ip register.
1995 ASSERT(!base.is(ip));
1996 if (u == 1) {
1997 add(ip, base, Operand(offset));
1998 } else {
1999 sub(ip, base, Operand(offset));
2000 }
2001 emit(cond | d*B22 | 0xD0*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
2002 }
2003}
2004
2005
2006void Assembler::vstr(const SwVfpRegister src,
2007 const MemOperand& operand,
2008 const Condition cond) {
2009 ASSERT(!operand.rm().is_valid());
2010 ASSERT(operand.am_ == Offset);
2011 vldr(src, operand.rn(), operand.offset(), cond);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002012}
2013
2014
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002015void Assembler::vldm(BlockAddrMode am,
2016 Register base,
2017 DwVfpRegister first,
2018 DwVfpRegister last,
2019 Condition cond) {
2020 // Instruction details available in ARM DDI 0406A, A8-626.
2021 // cond(31-28) | 110(27-25)| PUDW1(24-20) | Rbase(19-16) |
2022 // first(15-12) | 1010(11-8) | (count * 2)
2023 ASSERT(CpuFeatures::IsEnabled(VFP3));
2024 ASSERT_LE(first.code(), last.code());
2025 ASSERT(am == ia || am == ia_w || am == db_w);
2026 ASSERT(!base.is(pc));
2027
2028 int sd, d;
2029 first.split_code(&sd, &d);
2030 int count = last.code() - first.code() + 1;
2031 emit(cond | B27 | B26 | am | d*B22 | B20 | base.code()*B16 | sd*B12 |
2032 0xB*B8 | count*2);
2033}
2034
2035
2036void Assembler::vstm(BlockAddrMode am,
2037 Register base,
2038 DwVfpRegister first,
2039 DwVfpRegister last,
2040 Condition cond) {
2041 // Instruction details available in ARM DDI 0406A, A8-784.
2042 // cond(31-28) | 110(27-25)| PUDW0(24-20) | Rbase(19-16) |
2043 // first(15-12) | 1011(11-8) | (count * 2)
2044 ASSERT(CpuFeatures::IsEnabled(VFP3));
2045 ASSERT_LE(first.code(), last.code());
2046 ASSERT(am == ia || am == ia_w || am == db_w);
2047 ASSERT(!base.is(pc));
2048
2049 int sd, d;
2050 first.split_code(&sd, &d);
2051 int count = last.code() - first.code() + 1;
2052 emit(cond | B27 | B26 | am | d*B22 | base.code()*B16 | sd*B12 |
2053 0xB*B8 | count*2);
2054}
2055
2056void Assembler::vldm(BlockAddrMode am,
2057 Register base,
2058 SwVfpRegister first,
2059 SwVfpRegister last,
2060 Condition cond) {
2061 // Instruction details available in ARM DDI 0406A, A8-626.
2062 // cond(31-28) | 110(27-25)| PUDW1(24-20) | Rbase(19-16) |
2063 // first(15-12) | 1010(11-8) | (count/2)
2064 ASSERT(CpuFeatures::IsEnabled(VFP3));
2065 ASSERT_LE(first.code(), last.code());
2066 ASSERT(am == ia || am == ia_w || am == db_w);
2067 ASSERT(!base.is(pc));
2068
2069 int sd, d;
2070 first.split_code(&sd, &d);
2071 int count = last.code() - first.code() + 1;
2072 emit(cond | B27 | B26 | am | d*B22 | B20 | base.code()*B16 | sd*B12 |
2073 0xA*B8 | count);
2074}
2075
2076
2077void Assembler::vstm(BlockAddrMode am,
2078 Register base,
2079 SwVfpRegister first,
2080 SwVfpRegister last,
2081 Condition cond) {
2082 // Instruction details available in ARM DDI 0406A, A8-784.
2083 // cond(31-28) | 110(27-25)| PUDW0(24-20) | Rbase(19-16) |
2084 // first(15-12) | 1011(11-8) | (count/2)
2085 ASSERT(CpuFeatures::IsEnabled(VFP3));
2086 ASSERT_LE(first.code(), last.code());
2087 ASSERT(am == ia || am == ia_w || am == db_w);
2088 ASSERT(!base.is(pc));
2089
2090 int sd, d;
2091 first.split_code(&sd, &d);
2092 int count = last.code() - first.code() + 1;
2093 emit(cond | B27 | B26 | am | d*B22 | base.code()*B16 | sd*B12 |
2094 0xA*B8 | count);
2095}
2096
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002097static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
2098 uint64_t i;
2099 memcpy(&i, &d, 8);
2100
2101 *lo = i & 0xffffffff;
2102 *hi = i >> 32;
2103}
2104
2105// Only works for little endian floating point formats.
2106// We don't support VFP on the mixed endian floating point platform.
2107static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002108 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002109
2110 // VMOV can accept an immediate of the form:
2111 //
2112 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
2113 //
2114 // The immediate is encoded using an 8-bit quantity, comprised of two
2115 // 4-bit fields. For an 8-bit immediate of the form:
2116 //
2117 // [abcdefgh]
2118 //
2119 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
2120 // created of the form:
2121 //
2122 // [aBbbbbbb,bbcdefgh,00000000,00000000,
2123 // 00000000,00000000,00000000,00000000]
2124 //
2125 // where B = ~b.
2126 //
2127
2128 uint32_t lo, hi;
2129 DoubleAsTwoUInt32(d, &lo, &hi);
2130
2131 // The most obvious constraint is the long block of zeroes.
2132 if ((lo != 0) || ((hi & 0xffff) != 0)) {
2133 return false;
2134 }
2135
2136 // Bits 62:55 must be all clear or all set.
2137 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
2138 return false;
2139 }
2140
2141 // Bit 63 must be NOT bit 62.
2142 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
2143 return false;
2144 }
2145
2146 // Create the encoded immediate in the form:
2147 // [00000000,0000abcd,00000000,0000efgh]
2148 *encoding = (hi >> 16) & 0xf; // Low nybble.
2149 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
2150 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
2151
2152 return true;
2153}
2154
2155
2156void Assembler::vmov(const DwVfpRegister dst,
2157 double imm,
2158 const Condition cond) {
2159 // Dd = immediate
2160 // Instruction details available in ARM DDI 0406B, A8-640.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002161 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002162
2163 uint32_t enc;
2164 if (FitsVMOVDoubleImmediate(imm, &enc)) {
2165 // The double can be encoded in the instruction.
2166 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
2167 } else {
2168 // Synthesise the double from ARM immediates. This could be implemented
2169 // using vldr from a constant pool.
2170 uint32_t lo, hi;
2171 DoubleAsTwoUInt32(imm, &lo, &hi);
2172
2173 if (lo == hi) {
2174 // If the lo and hi parts of the double are equal, the literal is easier
2175 // to create. This is the case with 0.0.
2176 mov(ip, Operand(lo));
2177 vmov(dst, ip, ip);
2178 } else {
2179 // Move the low part of the double into the lower of the corresponsing S
2180 // registers of D register dst.
2181 mov(ip, Operand(lo));
2182 vmov(dst.low(), ip, cond);
2183
2184 // Move the high part of the double into the higher of the corresponsing S
2185 // registers of D register dst.
2186 mov(ip, Operand(hi));
2187 vmov(dst.high(), ip, cond);
2188 }
2189 }
2190}
2191
2192
2193void Assembler::vmov(const SwVfpRegister dst,
2194 const SwVfpRegister src,
2195 const Condition cond) {
2196 // Sd = Sm
2197 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002198 ASSERT(CpuFeatures::IsEnabled(VFP3));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002199 int sd, d, sm, m;
2200 dst.split_code(&sd, &d);
2201 src.split_code(&sm, &m);
2202 emit(cond | 0xE*B24 | d*B22 | 0xB*B20 | sd*B12 | 0xA*B8 | B6 | m*B5 | sm);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002203}
2204
2205
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002206void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002207 const DwVfpRegister src,
2208 const Condition cond) {
2209 // Dd = Dm
2210 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002211 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002212 emit(cond | 0xE*B24 | 0xB*B20 |
2213 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
2214}
2215
2216
2217void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002218 const Register src1,
2219 const Register src2,
2220 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002221 // Dm = <Rt,Rt2>.
2222 // Instruction details available in ARM DDI 0406A, A8-646.
2223 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
2224 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002225 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002226 ASSERT(!src1.is(pc) && !src2.is(pc));
2227 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
2228 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
2229}
2230
2231
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002232void Assembler::vmov(const Register dst1,
2233 const Register dst2,
2234 const DwVfpRegister src,
2235 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002236 // <Rt,Rt2> = Dm.
2237 // Instruction details available in ARM DDI 0406A, A8-646.
2238 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
2239 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002240 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002241 ASSERT(!dst1.is(pc) && !dst2.is(pc));
2242 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
2243 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
2244}
2245
2246
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002247void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002248 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002249 const Condition cond) {
2250 // Sn = Rt.
2251 // Instruction details available in ARM DDI 0406A, A8-642.
2252 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
2253 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002254 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002255 ASSERT(!src.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002256 int sn, n;
2257 dst.split_code(&sn, &n);
2258 emit(cond | 0xE*B24 | sn*B16 | src.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002259}
2260
2261
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002262void Assembler::vmov(const Register dst,
2263 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002264 const Condition cond) {
2265 // Rt = Sn.
2266 // Instruction details available in ARM DDI 0406A, A8-642.
2267 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
2268 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002269 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002270 ASSERT(!dst.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002271 int sn, n;
2272 src.split_code(&sn, &n);
2273 emit(cond | 0xE*B24 | B20 | sn*B16 | dst.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002274}
2275
2276
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002277// Type of data to read from or write to VFP register.
2278// Used as specifier in generic vcvt instruction.
2279enum VFPType { S32, U32, F32, F64 };
2280
2281
2282static bool IsSignedVFPType(VFPType type) {
2283 switch (type) {
2284 case S32:
2285 return true;
2286 case U32:
2287 return false;
2288 default:
2289 UNREACHABLE();
2290 return false;
2291 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002292}
2293
2294
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002295static bool IsIntegerVFPType(VFPType type) {
2296 switch (type) {
2297 case S32:
2298 case U32:
2299 return true;
2300 case F32:
2301 case F64:
2302 return false;
2303 default:
2304 UNREACHABLE();
2305 return false;
2306 }
2307}
2308
2309
2310static bool IsDoubleVFPType(VFPType type) {
2311 switch (type) {
2312 case F32:
2313 return false;
2314 case F64:
2315 return true;
2316 default:
2317 UNREACHABLE();
2318 return false;
2319 }
2320}
2321
2322
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002323// Split five bit reg_code based on size of reg_type.
2324// 32-bit register codes are Vm:M
2325// 64-bit register codes are M:Vm
2326// where Vm is four bits, and M is a single bit.
2327static void SplitRegCode(VFPType reg_type,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002328 int reg_code,
2329 int* vm,
2330 int* m) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002331 ASSERT((reg_code >= 0) && (reg_code <= 31));
2332 if (IsIntegerVFPType(reg_type) || !IsDoubleVFPType(reg_type)) {
2333 // 32 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002334 *m = reg_code & 0x1;
2335 *vm = reg_code >> 1;
2336 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002337 // 64 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002338 *m = (reg_code & 0x10) >> 4;
2339 *vm = reg_code & 0x0F;
2340 }
2341}
2342
2343
2344// Encode vcvt.src_type.dst_type instruction.
2345static Instr EncodeVCVT(const VFPType dst_type,
2346 const int dst_code,
2347 const VFPType src_type,
2348 const int src_code,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002349 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002350 const Condition cond) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002351 ASSERT(src_type != dst_type);
2352 int D, Vd, M, Vm;
2353 SplitRegCode(src_type, src_code, &Vm, &M);
2354 SplitRegCode(dst_type, dst_code, &Vd, &D);
2355
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002356 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2357 // Conversion between IEEE floating point and 32-bit integer.
2358 // Instruction details available in ARM DDI 0406B, A8.6.295.
2359 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2360 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2361 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2362
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002363 int sz, opc2, op;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002364
2365 if (IsIntegerVFPType(dst_type)) {
2366 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2367 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002368 op = mode;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002369 } else {
2370 ASSERT(IsIntegerVFPType(src_type));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002371 opc2 = 0x0;
2372 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2373 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002374 }
2375
2376 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2377 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2378 } else {
2379 // Conversion between IEEE double and single precision.
2380 // Instruction details available in ARM DDI 0406B, A8.6.298.
2381 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2382 // 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 +00002383 int sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002384 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2385 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2386 }
2387}
2388
2389
2390void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2391 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002392 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002393 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002394 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002395 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002396}
2397
2398
2399void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2400 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002401 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002402 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002403 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002404 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002405}
2406
2407
2408void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2409 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002410 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002411 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002412 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002413 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002414}
2415
2416
2417void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2418 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002419 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002420 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002421 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002422 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002423}
2424
2425
2426void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2427 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002428 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002429 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002430 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002431 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002432}
2433
2434
2435void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2436 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002437 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002438 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002439 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002440 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002441}
2442
2443
2444void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2445 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002446 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002447 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002448 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002449 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), mode, cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002450}
2451
2452
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002453void Assembler::vneg(const DwVfpRegister dst,
2454 const DwVfpRegister src,
2455 const Condition cond) {
2456 emit(cond | 0xE*B24 | 0xB*B20 | B16 | dst.code()*B12 |
2457 0x5*B9 | B8 | B6 | src.code());
2458}
2459
2460
whesse@chromium.org7a392b32011-01-31 11:30:36 +00002461void Assembler::vabs(const DwVfpRegister dst,
2462 const DwVfpRegister src,
2463 const Condition cond) {
2464 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 |
2465 0x5*B9 | B8 | 0x3*B6 | src.code());
2466}
2467
2468
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002469void Assembler::vadd(const DwVfpRegister dst,
2470 const DwVfpRegister src1,
2471 const DwVfpRegister src2,
2472 const Condition cond) {
2473 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002474 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2475 // Instruction details available in ARM DDI 0406A, A8-536.
2476 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2477 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002478 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002479 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2480 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2481}
2482
2483
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002484void Assembler::vsub(const DwVfpRegister dst,
2485 const DwVfpRegister src1,
2486 const DwVfpRegister src2,
2487 const Condition cond) {
2488 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002489 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2490 // Instruction details available in ARM DDI 0406A, A8-784.
2491 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2492 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002493 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002494 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2495 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2496}
2497
2498
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002499void Assembler::vmul(const DwVfpRegister dst,
2500 const DwVfpRegister src1,
2501 const DwVfpRegister src2,
2502 const Condition cond) {
2503 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002504 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2505 // Instruction details available in ARM DDI 0406A, A8-784.
2506 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2507 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002508 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002509 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2510 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2511}
2512
2513
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002514void Assembler::vdiv(const DwVfpRegister dst,
2515 const DwVfpRegister src1,
2516 const DwVfpRegister src2,
2517 const Condition cond) {
2518 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002519 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2520 // Instruction details available in ARM DDI 0406A, A8-584.
2521 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2522 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002523 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002524 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2525 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2526}
2527
2528
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002529void Assembler::vcmp(const DwVfpRegister src1,
2530 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002531 const Condition cond) {
2532 // vcmp(Dd, Dm) double precision floating point comparison.
2533 // Instruction details available in ARM DDI 0406A, A8-570.
2534 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002535 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=0 | 1(6) | M(5)=? | 0(4) | Vm(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002536 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002537 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002538 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002539}
2540
2541
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002542void Assembler::vcmp(const DwVfpRegister src1,
2543 const double src2,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002544 const Condition cond) {
2545 // vcmp(Dd, Dm) double precision floating point comparison.
2546 // Instruction details available in ARM DDI 0406A, A8-570.
2547 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0101 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002548 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=0 | 1(6) | M(5)=? | 0(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002549 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002550 ASSERT(src2 == 0.0);
2551 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | B16 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002552 src1.code()*B12 | 0x5*B9 | B8 | B6);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002553}
2554
2555
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002556void Assembler::vmsr(Register dst, Condition cond) {
2557 // Instruction details available in ARM DDI 0406A, A8-652.
2558 // cond(31-28) | 1110 (27-24) | 1110(23-20)| 0001 (19-16) |
2559 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002560 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002561 emit(cond | 0xE*B24 | 0xE*B20 | B16 |
2562 dst.code()*B12 | 0xA*B8 | B4);
2563}
2564
2565
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002566void Assembler::vmrs(Register dst, Condition cond) {
2567 // Instruction details available in ARM DDI 0406A, A8-652.
2568 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2569 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002570 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002571 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2572 dst.code()*B12 | 0xA*B8 | B4);
2573}
2574
2575
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002576void Assembler::vsqrt(const DwVfpRegister dst,
2577 const DwVfpRegister src,
2578 const Condition cond) {
2579 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2580 // Vd(15-12) | 101(11-9) | sz(8)=1 | 11 (7-6) | M(5)=? | 0(4) | Vm(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002581 ASSERT(CpuFeatures::IsEnabled(VFP3));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002582 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2583 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2584}
2585
2586
ager@chromium.org5c838252010-02-19 08:53:10 +00002587// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002588void Assembler::nop(int type) {
2589 // This is mov rx, rx.
2590 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2591 emit(al | 13*B21 | type*B12 | type);
2592}
2593
2594
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002595bool Assembler::IsNop(Instr instr, int type) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002596 // Check for mov rx, rx where x = type.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002597 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2598 return instr == (al | 13*B21 | type*B12 | type);
2599}
2600
2601
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002602bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2603 uint32_t dummy1;
2604 uint32_t dummy2;
2605 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2606}
2607
2608
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002609void Assembler::BlockConstPoolFor(int instructions) {
2610 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2611}
2612
2613
ager@chromium.org5c838252010-02-19 08:53:10 +00002614// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002615void Assembler::RecordJSReturn() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002616 positions_recorder()->WriteRecordedPositions();
ager@chromium.org4af710e2009-09-15 12:20:11 +00002617 CheckBuffer();
2618 RecordRelocInfo(RelocInfo::JS_RETURN);
2619}
2620
2621
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002622void Assembler::RecordDebugBreakSlot() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002623 positions_recorder()->WriteRecordedPositions();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002624 CheckBuffer();
2625 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2626}
2627
2628
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629void Assembler::RecordComment(const char* msg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002630 if (FLAG_code_comments) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00002632 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 }
2634}
2635
2636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637void Assembler::GrowBuffer() {
2638 if (!own_buffer_) FATAL("external code buffer is too small");
2639
ager@chromium.org5c838252010-02-19 08:53:10 +00002640 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 CodeDesc desc; // the new buffer
2642 if (buffer_size_ < 4*KB) {
2643 desc.buffer_size = 4*KB;
2644 } else if (buffer_size_ < 1*MB) {
2645 desc.buffer_size = 2*buffer_size_;
2646 } else {
2647 desc.buffer_size = buffer_size_ + 1*MB;
2648 }
2649 CHECK_GT(desc.buffer_size, 0); // no overflow
2650
ager@chromium.org5c838252010-02-19 08:53:10 +00002651 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 desc.buffer = NewArray<byte>(desc.buffer_size);
2653
2654 desc.instr_size = pc_offset();
2655 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2656
ager@chromium.org5c838252010-02-19 08:53:10 +00002657 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 int pc_delta = desc.buffer - buffer_;
2659 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2660 memmove(desc.buffer, buffer_, desc.instr_size);
2661 memmove(reloc_info_writer.pos() + rc_delta,
2662 reloc_info_writer.pos(), desc.reloc_size);
2663
ager@chromium.org5c838252010-02-19 08:53:10 +00002664 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 DeleteArray(buffer_);
2666 buffer_ = desc.buffer;
2667 buffer_size_ = desc.buffer_size;
2668 pc_ += pc_delta;
2669 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2670 reloc_info_writer.last_pc() + pc_delta);
2671
ager@chromium.org5c838252010-02-19 08:53:10 +00002672 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00002674 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675
ager@chromium.org5c838252010-02-19 08:53:10 +00002676 // Relocate pending relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 for (int i = 0; i < num_prinfo_; i++) {
2678 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002679 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2680 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00002681 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2682 rinfo.set_pc(rinfo.pc() + pc_delta);
2683 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 }
2685}
2686
2687
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002688void Assembler::db(uint8_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002689 // No relocation info should be pending while using db. db is used
2690 // to write pure data with no pointers and the constant pool should
2691 // be emitted before using db.
2692 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002693 CheckBuffer();
2694 *reinterpret_cast<uint8_t*>(pc_) = data;
2695 pc_ += sizeof(uint8_t);
2696}
2697
2698
2699void Assembler::dd(uint32_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002700 // No relocation info should be pending while using dd. dd is used
2701 // to write pure data with no pointers and the constant pool should
2702 // be emitted before using dd.
2703 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002704 CheckBuffer();
2705 *reinterpret_cast<uint32_t*>(pc_) = data;
2706 pc_ += sizeof(uint32_t);
2707}
2708
2709
ager@chromium.org236ad962008-09-25 09:45:57 +00002710void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002712 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002713 // Adjust code for new modes.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002714 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2715 || RelocInfo::IsJSReturn(rmode)
ager@chromium.org4af710e2009-09-15 12:20:11 +00002716 || RelocInfo::IsComment(rmode)
2717 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00002718 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719 } else {
2720 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2721 prinfo_[num_prinfo_++] = rinfo;
2722 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00002723 // instruction for which we just recorded relocation info.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 BlockConstPoolBefore(pc_offset() + kInstrSize);
2725 }
ager@chromium.org236ad962008-09-25 09:45:57 +00002726 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002727 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002728 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2729#ifdef DEBUG
2730 if (!Serializer::enabled()) {
2731 Serializer::TooLateToEnableNow();
2732 }
2733#endif
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002734 if (!Serializer::enabled() && !emit_debug_code()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002735 return;
2736 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002737 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002739 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
2740 ASSERT(ast_id_for_reloc_info_ != kNoASTId);
2741 RelocInfo reloc_info_with_ast_id(pc_, rmode, ast_id_for_reloc_info_);
2742 ast_id_for_reloc_info_ = kNoASTId;
2743 reloc_info_writer.Write(&reloc_info_with_ast_id);
2744 } else {
2745 reloc_info_writer.Write(&rinfo);
2746 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 }
2748}
2749
2750
2751void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2752 // Calculate the offset of the next check. It will be overwritten
2753 // when a const pool is generated or when const pools are being
2754 // blocked for a specific range.
2755 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2756
ager@chromium.org5c838252010-02-19 08:53:10 +00002757 // There is nothing to do if there are no pending relocation info entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758 if (num_prinfo_ == 0) return;
2759
2760 // We emit a constant pool at regular intervals of about kDistBetweenPools
2761 // or when requested by parameter force_emit (e.g. after each function).
2762 // We prefer not to emit a jump unless the max distance is reached or if we
2763 // are running low on slots, which can happen if a lot of constants are being
2764 // emitted (e.g. --debug-code and many static references).
2765 int dist = pc_offset() - last_const_pool_end_;
2766 if (!force_emit && dist < kMaxDistBetweenPools &&
2767 (require_jump || dist < kDistBetweenPools) &&
2768 // TODO(1236125): Cleanup the "magic" number below. We know that
2769 // the code generation will test every kCheckConstIntervalInst.
2770 // Thus we are safe as long as we generate less than 7 constant
2771 // entries per instruction.
2772 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2773 return;
2774 }
2775
2776 // If we did not return by now, we need to emit the constant pool soon.
2777
2778 // However, some small sequences of instructions must not be broken up by the
2779 // insertion of a constant pool; such sequences are protected by setting
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002780 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2781 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2782 // no_const_pool_before_.
2783 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002784 // Emission is currently blocked; make sure we try again as soon as
2785 // possible.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002786 if (const_pool_blocked_nesting_ > 0) {
2787 next_buffer_check_ = pc_offset() + kInstrSize;
2788 } else {
2789 next_buffer_check_ = no_const_pool_before_;
2790 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791
ager@chromium.org5c838252010-02-19 08:53:10 +00002792 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 ASSERT(!force_emit);
2794 return;
2795 }
2796
2797 int jump_instr = require_jump ? kInstrSize : 0;
2798
2799 // Check that the code buffer is large enough before emitting the constant
2800 // pool and relocation information (include the jump over the pool and the
2801 // constant pool marker).
2802 int max_needed_space =
2803 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2804 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2805
ager@chromium.org5c838252010-02-19 08:53:10 +00002806 // Block recursive calls to CheckConstPool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2808 num_prinfo_*kInstrSize);
2809 // Don't bother to check for the emit calls below.
2810 next_buffer_check_ = no_const_pool_before_;
2811
ager@chromium.org5c838252010-02-19 08:53:10 +00002812 // Emit jump over constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813 Label after_pool;
2814 if (require_jump) b(&after_pool);
2815
2816 RecordComment("[ Constant Pool");
2817
ager@chromium.org5c838252010-02-19 08:53:10 +00002818 // Put down constant pool marker "Undefined instruction" as specified by
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002819 // A5.6 (ARMv7) Instruction set encoding.
2820 emit(kConstantPoolMarker | num_prinfo_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821
ager@chromium.org5c838252010-02-19 08:53:10 +00002822 // Emit constant pool entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002823 for (int i = 0; i < num_prinfo_; i++) {
2824 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002825 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2826 rinfo.rmode() != RelocInfo::POSITION &&
2827 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828 Instr instr = instr_at(rinfo.pc());
ager@chromium.org4af710e2009-09-15 12:20:11 +00002829
ager@chromium.org5c838252010-02-19 08:53:10 +00002830 // Instruction to patch must be a ldr/str [pc, #offset].
2831 // P and U set, B and W clear, Rn == pc, offset12 still 0.
ager@chromium.org378b34e2011-01-28 08:04:38 +00002832 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | kOff12Mask)) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 (2*B25 | P | U | pc.code()*B16));
2834 int delta = pc_ - rinfo.pc() - 8;
2835 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2836 if (delta < 0) {
2837 instr &= ~U;
2838 delta = -delta;
2839 }
2840 ASSERT(is_uint12(delta));
2841 instr_at_put(rinfo.pc(), instr + delta);
2842 emit(rinfo.data());
2843 }
2844 num_prinfo_ = 0;
2845 last_const_pool_end_ = pc_offset();
2846
2847 RecordComment("]");
2848
2849 if (after_pool.is_linked()) {
2850 bind(&after_pool);
2851 }
2852
2853 // Since a constant pool was just emitted, move the check offset forward by
2854 // the standard interval.
2855 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2856}
2857
2858
2859} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002860
2861#endif // V8_TARGET_ARCH_ARM