blob: fa97a3b3217ce506ccd8a070a831e82a47d606ae [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
54#ifdef __arm__
55static uint64_t CpuFeaturesImpliedByCompiler() {
56 uint64_t answer = 0;
57#ifdef CAN_USE_ARMV7_INSTRUCTIONS
58 answer |= 1u << ARMv7;
59#endif // def CAN_USE_ARMV7_INSTRUCTIONS
60 // If the compiler is allowed to use VFP then we can use VFP too in our code
61 // generation even when generating snapshots. This won't work for cross
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000062 // compilation. VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000063#if defined(__VFP_FP__) && !defined(__SOFTFP__)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000064 answer |= 1u << VFP3 | 1u << ARMv7;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000065#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
66#ifdef CAN_USE_VFP_INSTRUCTIONS
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000067 answer |= 1u << VFP3 | 1u << ARMv7;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000068#endif // def CAN_USE_VFP_INSTRUCTIONS
69 return answer;
70}
71#endif // def __arm__
72
73
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000074void CpuFeatures::Probe() {
75 ASSERT(!initialized_);
76#ifdef DEBUG
77 initialized_ = true;
78#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000079#ifndef __arm__
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000080 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is
81 // enabled. VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
ager@chromium.org5c838252010-02-19 08:53:10 +000082 if (FLAG_enable_vfp3) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000083 supported_ |= 1u << VFP3 | 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +000084 }
85 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
86 if (FLAG_enable_armv7) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000087 supported_ |= 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +000088 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000089#else // def __arm__
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000090 if (Serializer::enabled()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000091 supported_ |= OS::CpuFeaturesImpliedByPlatform();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000092 supported_ |= CpuFeaturesImpliedByCompiler();
ager@chromium.orgc4c92722009-11-18 14:12:51 +000093 return; // No features if we might serialize.
94 }
95
96 if (OS::ArmCpuHasFeature(VFP3)) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000097 // This implementation also sets the VFP flags if runtime
98 // detection of VFP returns true. VFPv3 implies ARMv7, see ARM DDI
99 // 0406B, page A1-6.
100 supported_ |= 1u << VFP3 | 1u << ARMv7;
101 found_by_runtime_probing_ |= 1u << VFP3 | 1u << ARMv7;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000102 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000103
104 if (OS::ArmCpuHasFeature(ARMv7)) {
105 supported_ |= 1u << ARMv7;
106 found_by_runtime_probing_ |= 1u << ARMv7;
107 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000108#endif
109}
110
111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113// Implementation of RelocInfo
114
115const int RelocInfo::kApplyMask = 0;
116
117
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000118bool RelocInfo::IsCodedSpecially() {
119 // The deserializer needs to know whether a pointer is specially coded. Being
120 // specially coded on ARM means that it is a movw/movt instruction. We don't
121 // generate those yet.
122 return false;
123}
124
125
126
iposva@chromium.org245aa852009-02-10 00:49:54 +0000127void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000129 Instr* pc = reinterpret_cast<Instr*>(pc_);
130 Instr* instr = reinterpret_cast<Instr*>(instructions);
131 for (int i = 0; i < instruction_count; i++) {
132 *(pc + i) = *(instr + i);
133 }
134
135 // Indicate that code has changed.
136 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000141// Additional guard instructions can be added if required.
142void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 // Patch the code at the current address with a call to the target.
144 UNIMPLEMENTED();
145}
146
147
148// -----------------------------------------------------------------------------
149// Implementation of Operand and MemOperand
150// See assembler-arm-inl.h for inlined constructors
151
152Operand::Operand(Handle<Object> handle) {
153 rm_ = no_reg;
154 // Verify all Objects referred by code are NOT in new space.
155 Object* obj = *handle;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000156 ASSERT(!HEAP->InNewSpace(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 if (obj->IsHeapObject()) {
158 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000159 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 } else {
161 // no relocation needed
162 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000163 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 }
165}
166
167
168Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
169 ASSERT(is_uint5(shift_imm));
170 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
171 rm_ = rm;
172 rs_ = no_reg;
173 shift_op_ = shift_op;
174 shift_imm_ = shift_imm & 31;
175 if (shift_op == RRX) {
176 // encoded as ROR with shift_imm == 0
177 ASSERT(shift_imm == 0);
178 shift_op_ = ROR;
179 shift_imm_ = 0;
180 }
181}
182
183
184Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
185 ASSERT(shift_op != RRX);
186 rm_ = rm;
187 rs_ = no_reg;
188 shift_op_ = shift_op;
189 rs_ = rs;
190}
191
192
193MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
194 rn_ = rn;
195 rm_ = no_reg;
196 offset_ = offset;
197 am_ = am;
198}
199
200MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
201 rn_ = rn;
202 rm_ = rm;
203 shift_op_ = LSL;
204 shift_imm_ = 0;
205 am_ = am;
206}
207
208
209MemOperand::MemOperand(Register rn, Register rm,
210 ShiftOp shift_op, int shift_imm, AddrMode am) {
211 ASSERT(is_uint5(shift_imm));
212 rn_ = rn;
213 rm_ = rm;
214 shift_op_ = shift_op;
215 shift_imm_ = shift_imm & 31;
216 am_ = am;
217}
218
219
220// -----------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000221// Specific instructions, constants, and masks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222
mads.s.ager31e71382008-08-13 09:32:07 +0000223// add(sp, sp, 4) instruction (aka Pop())
ager@chromium.org378b34e2011-01-28 08:04:38 +0000224const Instr kPopInstruction =
225 al | PostIndex | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
mads.s.ager31e71382008-08-13 09:32:07 +0000226// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
227// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000228const Instr kPushRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000229 al | B26 | 4 | NegPreIndex | sp.code() * B16;
230// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
231// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000232const Instr kPopRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000233 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000234// mov lr, pc
ager@chromium.org378b34e2011-01-28 08:04:38 +0000235const Instr kMovLrPc = al | MOV | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000236// ldr rd, [pc, #offset]
ager@chromium.org378b34e2011-01-28 08:04:38 +0000237const Instr kLdrPCMask = kCondMask | 15 * B24 | 7 * B20 | 15 * B16;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000238const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
239// blxcc rm
240const Instr kBlxRegMask =
241 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
242const Instr kBlxRegPattern =
ager@chromium.org378b34e2011-01-28 08:04:38 +0000243 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | BLX;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000244const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
245const Instr kMovMvnPattern = 0xd * B21;
246const Instr kMovMvnFlip = B22;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000247const Instr kMovLeaveCCMask = 0xdff * B16;
248const Instr kMovLeaveCCPattern = 0x1a0 * B16;
249const Instr kMovwMask = 0xff * B20;
250const Instr kMovwPattern = 0x30 * B20;
251const Instr kMovwLeaveCCFlip = 0x5 * B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000252const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
253const Instr kCmpCmnPattern = 0x15 * B20;
254const Instr kCmpCmnFlip = B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000255const Instr kAddSubFlip = 0x6 * B21;
256const Instr kAndBicFlip = 0xe * B21;
257
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000258// A mask for the Rd register for push, pop, ldr, str instructions.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000259const Instr kLdrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000260 al | B26 | L | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000261const Instr kStrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000262 al | B26 | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000263const Instr kLdrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000264 al | B26 | L | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000265const Instr kStrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000266 al | B26 | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000267const Instr kLdrStrInstrTypeMask = 0xffff0000;
268const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
269const Instr kLdrStrOffsetMask = 0x00000fff;
270
mads.s.ager31e71382008-08-13 09:32:07 +0000271
ager@chromium.org5c838252010-02-19 08:53:10 +0000272// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273static const int kMinimalBufferSize = 4*KB;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
ager@chromium.org378b34e2011-01-28 08:04:38 +0000275
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000276Assembler::Assembler(Isolate* arg_isolate, void* buffer, int buffer_size)
277 : AssemblerBase(arg_isolate),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 positions_recorder_(this),
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000279 allow_peephole_optimization_(false),
280 emit_debug_code_(FLAG_debug_code) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000281 allow_peephole_optimization_ = FLAG_peephole_optimization;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000283 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 if (buffer_size <= kMinimalBufferSize) {
285 buffer_size = kMinimalBufferSize;
286
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 if (isolate()->assembler_spare_buffer() != NULL) {
288 buffer = isolate()->assembler_spare_buffer();
289 isolate()->set_assembler_spare_buffer(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 }
291 }
292 if (buffer == NULL) {
293 buffer_ = NewArray<byte>(buffer_size);
294 } else {
295 buffer_ = static_cast<byte*>(buffer);
296 }
297 buffer_size_ = buffer_size;
298 own_buffer_ = true;
299
300 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000301 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 ASSERT(buffer_size > 0);
303 buffer_ = static_cast<byte*>(buffer);
304 buffer_size_ = buffer_size;
305 own_buffer_ = false;
306 }
307
ager@chromium.org5c838252010-02-19 08:53:10 +0000308 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 ASSERT(buffer_ != NULL);
310 pc_ = buffer_;
311 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
312 num_prinfo_ = 0;
313 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000314 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 no_const_pool_before_ = 0;
316 last_const_pool_end_ = 0;
317 last_bound_pos_ = 0;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000318 ast_id_for_reloc_info_ = kNoASTId;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319}
320
321
322Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000323 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 if (own_buffer_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000325 if (isolate()->assembler_spare_buffer() == NULL &&
326 buffer_size_ == kMinimalBufferSize) {
327 isolate()->set_assembler_spare_buffer(buffer_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 } else {
329 DeleteArray(buffer_);
330 }
331 }
332}
333
334
335void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000336 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 CheckConstPool(true, false);
338 ASSERT(num_prinfo_ == 0);
339
ager@chromium.org5c838252010-02-19 08:53:10 +0000340 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 desc->buffer = buffer_;
342 desc->buffer_size = buffer_size_;
343 desc->instr_size = pc_offset();
344 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
345}
346
347
348void Assembler::Align(int m) {
349 ASSERT(m >= 4 && IsPowerOf2(m));
350 while ((pc_offset() & (m - 1)) != 0) {
351 nop();
352 }
353}
354
355
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000356void Assembler::CodeTargetAlign() {
357 // Preferred alignment of jump targets on some ARM chips.
358 Align(8);
359}
360
361
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000362Condition Assembler::GetCondition(Instr instr) {
363 return Instruction::ConditionField(instr);
364}
365
366
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000367bool Assembler::IsBranch(Instr instr) {
368 return (instr & (B27 | B25)) == (B27 | B25);
369}
370
371
372int Assembler::GetBranchOffset(Instr instr) {
373 ASSERT(IsBranch(instr));
374 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
375 // with 4 to get the offset in bytes.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000376 return ((instr & kImm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000377}
378
379
380bool Assembler::IsLdrRegisterImmediate(Instr instr) {
381 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
382}
383
384
385int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
386 ASSERT(IsLdrRegisterImmediate(instr));
387 bool positive = (instr & B23) == B23;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000388 int offset = instr & kOff12Mask; // Zero extended offset.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000389 return positive ? offset : -offset;
390}
391
392
393Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
394 ASSERT(IsLdrRegisterImmediate(instr));
395 bool positive = offset >= 0;
396 if (!positive) offset = -offset;
397 ASSERT(is_uint12(offset));
398 // Set bit indicating whether the offset should be added.
399 instr = (instr & ~B23) | (positive ? B23 : 0);
400 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000401 return (instr & ~kOff12Mask) | offset;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000402}
403
404
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000405bool Assembler::IsStrRegisterImmediate(Instr instr) {
406 return (instr & (B27 | B26 | B25 | B22 | B20)) == B26;
407}
408
409
410Instr Assembler::SetStrRegisterImmediateOffset(Instr instr, int offset) {
411 ASSERT(IsStrRegisterImmediate(instr));
412 bool positive = offset >= 0;
413 if (!positive) offset = -offset;
414 ASSERT(is_uint12(offset));
415 // Set bit indicating whether the offset should be added.
416 instr = (instr & ~B23) | (positive ? B23 : 0);
417 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000418 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000419}
420
421
422bool Assembler::IsAddRegisterImmediate(Instr instr) {
423 return (instr & (B27 | B26 | B25 | B24 | B23 | B22 | B21)) == (B25 | B23);
424}
425
426
427Instr Assembler::SetAddRegisterImmediateOffset(Instr instr, int offset) {
428 ASSERT(IsAddRegisterImmediate(instr));
429 ASSERT(offset >= 0);
430 ASSERT(is_uint12(offset));
431 // Set the offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000432 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000433}
434
435
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000436Register Assembler::GetRd(Instr instr) {
437 Register reg;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000438 reg.code_ = Instruction::RdValue(instr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000439 return reg;
440}
441
442
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000443Register Assembler::GetRn(Instr instr) {
444 Register reg;
445 reg.code_ = Instruction::RnValue(instr);
446 return reg;
447}
448
449
450Register Assembler::GetRm(Instr instr) {
451 Register reg;
452 reg.code_ = Instruction::RmValue(instr);
453 return reg;
454}
455
456
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000457bool Assembler::IsPush(Instr instr) {
458 return ((instr & ~kRdMask) == kPushRegPattern);
459}
460
461
462bool Assembler::IsPop(Instr instr) {
463 return ((instr & ~kRdMask) == kPopRegPattern);
464}
465
466
467bool Assembler::IsStrRegFpOffset(Instr instr) {
468 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
469}
470
471
472bool Assembler::IsLdrRegFpOffset(Instr instr) {
473 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
474}
475
476
477bool Assembler::IsStrRegFpNegOffset(Instr instr) {
478 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
479}
480
481
482bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
483 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
484}
485
486
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000487bool Assembler::IsLdrPcImmediateOffset(Instr instr) {
488 // Check the instruction is indeed a
489 // ldr<cond> <Rd>, [pc +/- offset_12].
ager@chromium.org378b34e2011-01-28 08:04:38 +0000490 return (instr & (kLdrPCMask & ~kCondMask)) == 0x051f0000;
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000491}
492
493
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000494bool Assembler::IsTstImmediate(Instr instr) {
495 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
496 (I | TST | S);
497}
498
499
500bool Assembler::IsCmpRegister(Instr instr) {
501 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask | B4)) ==
502 (CMP | S);
503}
504
505
506bool Assembler::IsCmpImmediate(Instr instr) {
507 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
508 (I | CMP | S);
509}
510
511
512Register Assembler::GetCmpImmediateRegister(Instr instr) {
513 ASSERT(IsCmpImmediate(instr));
514 return GetRn(instr);
515}
516
517
518int Assembler::GetCmpImmediateRawImmediate(Instr instr) {
519 ASSERT(IsCmpImmediate(instr));
520 return instr & kOff12Mask;
521}
522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523// Labels refer to positions in the (to be) generated code.
524// There are bound, linked, and unused labels.
525//
526// Bound labels refer to known positions in the already
527// generated code. pos() is the position the label refers to.
528//
529// Linked labels refer to unknown positions in the code
530// to be generated; pos() is the position of the last
531// instruction using the label.
532
533
534// The link chain is terminated by a negative code position (must be aligned)
535const int kEndOfChain = -4;
536
537
538int Assembler::target_at(int pos) {
539 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000540 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000541 // Emitted label constant, not part of a branch.
542 return instr - (Code::kHeaderSize - kHeapObjectTag);
543 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000545 int imm26 = ((instr & kImm24Mask) << 8) >> 6;
546 if ((Instruction::ConditionField(instr) == kSpecialCondition) &&
547 ((instr & B24) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 // blx uses bit 24 to encode bit 2 of imm26
549 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000550 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000551 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552}
553
554
555void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000557 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000558 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
559 // Emitted label constant, not part of a branch.
560 // Make label relative to Code* of generated Code object.
561 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
562 return;
563 }
564 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000566 if (Instruction::ConditionField(instr) == kSpecialCondition) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 // blx uses bit 24 to encode bit 2 of imm26
568 ASSERT((imm26 & 1) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000569 instr = (instr & ~(B24 | kImm24Mask)) | ((imm26 & 2) >> 1)*B24;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 } else {
571 ASSERT((imm26 & 3) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000572 instr &= ~kImm24Mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 }
574 int imm24 = imm26 >> 2;
575 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000576 instr_at_put(pos, instr | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577}
578
579
580void Assembler::print(Label* L) {
581 if (L->is_unused()) {
582 PrintF("unused label\n");
583 } else if (L->is_bound()) {
584 PrintF("bound label to %d\n", L->pos());
585 } else if (L->is_linked()) {
586 Label l = *L;
587 PrintF("unbound label");
588 while (l.is_linked()) {
589 PrintF("@ %d ", l.pos());
590 Instr instr = instr_at(l.pos());
ager@chromium.org378b34e2011-01-28 08:04:38 +0000591 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000592 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000594 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
ager@chromium.org378b34e2011-01-28 08:04:38 +0000595 Condition cond = Instruction::ConditionField(instr);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000596 const char* b;
597 const char* c;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000598 if (cond == kSpecialCondition) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000599 b = "blx";
600 c = "";
601 } else {
602 if ((instr & B24) != 0)
603 b = "bl";
604 else
605 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000607 switch (cond) {
608 case eq: c = "eq"; break;
609 case ne: c = "ne"; break;
610 case hs: c = "hs"; break;
611 case lo: c = "lo"; break;
612 case mi: c = "mi"; break;
613 case pl: c = "pl"; break;
614 case vs: c = "vs"; break;
615 case vc: c = "vc"; break;
616 case hi: c = "hi"; break;
617 case ls: c = "ls"; break;
618 case ge: c = "ge"; break;
619 case lt: c = "lt"; break;
620 case gt: c = "gt"; break;
621 case le: c = "le"; break;
622 case al: c = ""; break;
623 default:
624 c = "";
625 UNREACHABLE();
626 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000628 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 next(&l);
631 }
632 } else {
633 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
634 }
635}
636
637
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638void Assembler::bind_to(Label* L, int pos) {
639 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
640 while (L->is_linked()) {
641 int fixup_pos = L->pos();
642 next(L); // call next before overwriting link with target at fixup_pos
643 target_at_put(fixup_pos, pos);
644 }
645 L->bind_to(pos);
646
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000647 // Keep track of the last bound label so we don't eliminate any instructions
648 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 if (pos > last_bound_pos_)
650 last_bound_pos_ = pos;
651}
652
653
654void Assembler::link_to(Label* L, Label* appendix) {
655 if (appendix->is_linked()) {
656 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000657 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 int fixup_pos;
659 int link = L->pos();
660 do {
661 fixup_pos = link;
662 link = target_at(fixup_pos);
663 } while (link > 0);
664 ASSERT(link == kEndOfChain);
665 target_at_put(fixup_pos, appendix->pos());
666 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000667 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 *L = *appendix;
669 }
670 }
671 appendix->Unuse(); // appendix should not be used anymore
672}
673
674
675void Assembler::bind(Label* L) {
676 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 bind_to(L, pc_offset());
678}
679
680
681void Assembler::next(Label* L) {
682 ASSERT(L->is_linked());
683 int link = target_at(L->pos());
684 if (link > 0) {
685 L->link_to(link);
686 } else {
687 ASSERT(link == kEndOfChain);
688 L->Unuse();
689 }
690}
691
692
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000693static Instr EncodeMovwImmediate(uint32_t immediate) {
694 ASSERT(immediate < 0x10000);
695 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
696}
697
698
ager@chromium.org5c838252010-02-19 08:53:10 +0000699// Low-level code emission routines depending on the addressing mode.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000700// If this returns true then you have to use the rotate_imm and immed_8
701// that it returns, because it may have already changed the instruction
702// to match them!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703static bool fits_shifter(uint32_t imm32,
704 uint32_t* rotate_imm,
705 uint32_t* immed_8,
706 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000707 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 for (int rot = 0; rot < 16; rot++) {
709 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
710 if ((imm8 <= 0xff)) {
711 *rotate_imm = rot;
712 *immed_8 = imm8;
713 return true;
714 }
715 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000716 // If the opcode is one with a complementary version and the complementary
717 // immediate fits, change the opcode.
718 if (instr != NULL) {
719 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
720 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
721 *instr ^= kMovMvnFlip;
722 return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000723 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000724 if (CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000725 if (imm32 < 0x10000) {
726 *instr ^= kMovwLeaveCCFlip;
727 *instr |= EncodeMovwImmediate(imm32);
728 *rotate_imm = *immed_8 = 0; // Not used for movw.
729 return true;
730 }
731 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000732 }
733 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
734 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
735 *instr ^= kCmpCmnFlip;
736 return true;
737 }
738 } else {
739 Instr alu_insn = (*instr & kALUMask);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000740 if (alu_insn == ADD ||
741 alu_insn == SUB) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000742 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
743 *instr ^= kAddSubFlip;
744 return true;
745 }
ager@chromium.org378b34e2011-01-28 08:04:38 +0000746 } else if (alu_insn == AND ||
747 alu_insn == BIC) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000748 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
749 *instr ^= kAndBicFlip;
750 return true;
751 }
752 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 }
754 }
755 return false;
756}
757
758
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000759// We have to use the temporary register for things that can be relocated even
760// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
761// space. There is no guarantee that the relocated location can be similarly
762// encoded.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000763bool Operand::must_use_constant_pool() const {
764 if (rmode_ == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000765#ifdef DEBUG
766 if (!Serializer::enabled()) {
767 Serializer::TooLateToEnableNow();
768 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000769#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000770 return Serializer::enabled();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000771 } else if (rmode_ == RelocInfo::NONE) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000772 return false;
773 }
774 return true;
775}
776
777
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000778bool Operand::is_single_instruction(Instr instr) const {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000779 if (rm_.is_valid()) return true;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000780 uint32_t dummy1, dummy2;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000781 if (must_use_constant_pool() ||
782 !fits_shifter(imm32_, &dummy1, &dummy2, &instr)) {
783 // The immediate operand cannot be encoded as a shifter operand, or use of
784 // constant pool is required. For a mov instruction not setting the
785 // condition code additional instruction conventions can be used.
786 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000787 if (must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000788 !CpuFeatures::IsSupported(ARMv7)) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000789 // mov instruction will be an ldr from constant pool (one instruction).
790 return true;
791 } else {
792 // mov instruction will be a mov or movw followed by movt (two
793 // instructions).
794 return false;
795 }
796 } else {
797 // If this is not a mov or mvn instruction there will always an additional
798 // instructions - either mov or ldr. The mov might actually be two
799 // instructions mov or movw followed by movt so including the actual
800 // instruction two or three instructions will be generated.
801 return false;
802 }
803 } else {
804 // No use of constant pool and the immediate operand can be encoded as a
805 // shifter operand.
806 return true;
807 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000808}
809
810
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811void Assembler::addrmod1(Instr instr,
812 Register rn,
813 Register rd,
814 const Operand& x) {
815 CheckBuffer();
ager@chromium.org378b34e2011-01-28 08:04:38 +0000816 ASSERT((instr & ~(kCondMask | kOpCodeMask | S)) == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000818 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 uint32_t rotate_imm;
820 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000821 if (x.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
823 // The immediate operand cannot be encoded as a shifter operand, so load
824 // it first to register ip and change the original instruction to use ip.
825 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000826 // condition code), then replace it with a 'ldr rd, [pc]'.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000827 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
ager@chromium.org378b34e2011-01-28 08:04:38 +0000828 Condition cond = Instruction::ConditionField(instr);
829 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000830 if (x.must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000831 !CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000832 RecordRelocInfo(x.rmode_, x.imm32_);
833 ldr(rd, MemOperand(pc, 0), cond);
834 } else {
835 // Will probably use movw, will certainly not use constant pool.
836 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
837 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
838 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000840 // If this is not a mov or mvn instruction we may still be able to avoid
841 // a constant pool entry by using mvn or movw.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000842 if (!x.must_use_constant_pool() &&
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000843 (instr & kMovMvnMask) != kMovMvnPattern) {
844 mov(ip, x, LeaveCC, cond);
845 } else {
846 RecordRelocInfo(x.rmode_, x.imm32_);
847 ldr(ip, MemOperand(pc, 0), cond);
848 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 addrmod1(instr, rn, rd, Operand(ip));
850 }
851 return;
852 }
853 instr |= I | rotate_imm*B8 | immed_8;
854 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000855 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
857 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000858 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
860 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
861 }
862 emit(instr | rn.code()*B16 | rd.code()*B12);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000863 if (rn.is(pc) || x.rm_.is(pc)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000864 // Block constant pool emission for one instruction after reading pc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 BlockConstPoolBefore(pc_offset() + kInstrSize);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000866 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867}
868
869
870void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000871 ASSERT((instr & ~(kCondMask | B | L)) == B26);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 int am = x.am_;
873 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000874 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 int offset_12 = x.offset_;
876 if (offset_12 < 0) {
877 offset_12 = -offset_12;
878 am ^= U;
879 }
880 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000881 // Immediate offset cannot be encoded, load it first to register ip
882 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000884 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
886 return;
887 }
888 ASSERT(offset_12 >= 0); // no masking needed
889 instr |= offset_12;
890 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000891 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000893 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 ASSERT(!x.rm_.is(pc));
895 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
896 }
897 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
898 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
899}
900
901
902void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000903 ASSERT((instr & ~(kCondMask | L | S6 | H)) == (B4 | B7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 ASSERT(x.rn_.is_valid());
905 int am = x.am_;
906 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000907 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 int offset_8 = x.offset_;
909 if (offset_8 < 0) {
910 offset_8 = -offset_8;
911 am ^= U;
912 }
913 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000914 // Immediate offset cannot be encoded, load it first to register ip
915 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000917 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
919 return;
920 }
921 ASSERT(offset_8 >= 0); // no masking needed
922 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
923 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000924 // Scaled register offset not supported, load index first
925 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
927 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
ager@chromium.org378b34e2011-01-28 08:04:38 +0000928 Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
930 return;
931 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000932 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
934 instr |= x.rm_.code();
935 }
936 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
937 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
938}
939
940
941void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000942 ASSERT((instr & ~(kCondMask | P | U | W | L)) == B27);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 ASSERT(rl != 0);
944 ASSERT(!rn.is(pc));
945 emit(instr | rn.code()*B16 | rl);
946}
947
948
949void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000950 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000951 ASSERT_EQ((B27 | B26),
ager@chromium.org378b34e2011-01-28 08:04:38 +0000952 (instr & ~(kCondMask | kCoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
954 int am = x.am_;
955 int offset_8 = x.offset_;
956 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
957 offset_8 >>= 2;
958 if (offset_8 < 0) {
959 offset_8 = -offset_8;
960 am ^= U;
961 }
962 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
963 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
964
ager@chromium.org5c838252010-02-19 08:53:10 +0000965 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 if ((am & P) == 0)
967 am |= W;
968
969 ASSERT(offset_8 >= 0); // no masking needed
970 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
971}
972
973
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000974int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 int target_pos;
976 if (L->is_bound()) {
977 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000979 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000981 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000983 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 L->link_to(pc_offset());
985 }
986
987 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +0000988 // be emitted at the pc offset recorded by the label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 BlockConstPoolBefore(pc_offset() + kInstrSize);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000990 return target_pos - (pc_offset() + kPcLoadDelta);
991}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000993
994void Assembler::label_at_put(Label* L, int at_offset) {
995 int target_pos;
996 if (L->is_bound()) {
997 target_pos = L->pos();
998 } else {
999 if (L->is_linked()) {
1000 target_pos = L->pos(); // L's link
1001 } else {
1002 target_pos = kEndOfChain;
1003 }
1004 L->link_to(at_offset);
1005 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
1006 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007}
1008
1009
ager@chromium.org5c838252010-02-19 08:53:10 +00001010// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011void Assembler::b(int branch_offset, Condition cond) {
1012 ASSERT((branch_offset & 3) == 0);
1013 int imm24 = branch_offset >> 2;
1014 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001015 emit(cond | B27 | B25 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001017 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001018 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001020 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021}
1022
1023
1024void Assembler::bl(int branch_offset, Condition cond) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001025 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026 ASSERT((branch_offset & 3) == 0);
1027 int imm24 = branch_offset >> 2;
1028 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001029 emit(cond | B27 | B25 | B24 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030}
1031
1032
1033void Assembler::blx(int branch_offset) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001034 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035 ASSERT((branch_offset & 1) == 0);
1036 int h = ((branch_offset & 2) >> 1)*B24;
1037 int imm24 = branch_offset >> 2;
1038 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001039 emit(kSpecialCondition | B27 | B25 | h | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040}
1041
1042
1043void Assembler::blx(Register target, Condition cond) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001044 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 ASSERT(!target.is(pc));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001046 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BLX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047}
1048
1049
1050void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001051 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
ager@chromium.org378b34e2011-01-28 08:04:38 +00001053 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054}
1055
1056
ager@chromium.org5c838252010-02-19 08:53:10 +00001057// Data-processing instructions.
1058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059void Assembler::and_(Register dst, Register src1, const Operand& src2,
1060 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001061 addrmod1(cond | AND | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062}
1063
1064
1065void Assembler::eor(Register dst, Register src1, const Operand& src2,
1066 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001067 addrmod1(cond | EOR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068}
1069
1070
1071void Assembler::sub(Register dst, Register src1, const Operand& src2,
1072 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001073 addrmod1(cond | SUB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074}
1075
1076
1077void Assembler::rsb(Register dst, Register src1, const Operand& src2,
1078 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001079 addrmod1(cond | RSB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080}
1081
1082
1083void Assembler::add(Register dst, Register src1, const Operand& src2,
1084 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001085 addrmod1(cond | ADD | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +00001086
1087 // Eliminate pattern: push(r), pop()
1088 // str(src, MemOperand(sp, 4, NegPreIndex), al);
1089 // add(sp, sp, Operand(kPointerSize));
1090 // Both instructions can be eliminated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001091 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001092 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001093 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
ager@chromium.org378b34e2011-01-28 08:04:38 +00001094 (instr_at(pc_ - 2 * kInstrSize) & ~kRdMask) == kPushRegPattern) {
mads.s.ager31e71382008-08-13 09:32:07 +00001095 pc_ -= 2 * kInstrSize;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001096 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001097 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
1098 }
1099 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100}
1101
1102
1103void Assembler::adc(Register dst, Register src1, const Operand& src2,
1104 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001105 addrmod1(cond | ADC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106}
1107
1108
1109void Assembler::sbc(Register dst, Register src1, const Operand& src2,
1110 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001111 addrmod1(cond | SBC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112}
1113
1114
1115void Assembler::rsc(Register dst, Register src1, const Operand& src2,
1116 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001117 addrmod1(cond | RSC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118}
1119
1120
1121void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001122 addrmod1(cond | TST | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123}
1124
1125
1126void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001127 addrmod1(cond | TEQ | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128}
1129
1130
1131void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001132 addrmod1(cond | CMP | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133}
1134
1135
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001136void Assembler::cmp_raw_immediate(
1137 Register src, int raw_immediate, Condition cond) {
1138 ASSERT(is_uint12(raw_immediate));
1139 emit(cond | I | CMP | S | src.code() << 16 | raw_immediate);
1140}
1141
1142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001144 addrmod1(cond | CMN | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145}
1146
1147
1148void Assembler::orr(Register dst, Register src1, const Operand& src2,
1149 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001150 addrmod1(cond | ORR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151}
1152
1153
1154void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001155 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001156 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001157 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001158 // Don't allow nop instructions in the form mov rn, rn to be generated using
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001159 // the mov instruction. They must be generated using nop(int/NopMarkerTypes)
1160 // or MarkCode(int/NopMarkerTypes) pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001161 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001162 addrmod1(cond | MOV | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163}
1164
1165
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001166void Assembler::movw(Register reg, uint32_t immediate, Condition cond) {
1167 ASSERT(immediate < 0x10000);
1168 mov(reg, Operand(immediate), LeaveCC, cond);
1169}
1170
1171
1172void Assembler::movt(Register reg, uint32_t immediate, Condition cond) {
1173 emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate));
1174}
1175
1176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177void Assembler::bic(Register dst, Register src1, const Operand& src2,
1178 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001179 addrmod1(cond | BIC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180}
1181
1182
1183void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001184 addrmod1(cond | MVN | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185}
1186
1187
ager@chromium.org5c838252010-02-19 08:53:10 +00001188// Multiply instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1190 SBit s, Condition cond) {
1191 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1193 src2.code()*B8 | B7 | B4 | src1.code());
1194}
1195
1196
1197void Assembler::mul(Register dst, Register src1, Register src2,
1198 SBit s, Condition cond) {
1199 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001200 // dst goes in bits 16-19 for this instruction!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1202}
1203
1204
1205void Assembler::smlal(Register dstL,
1206 Register dstH,
1207 Register src1,
1208 Register src2,
1209 SBit s,
1210 Condition cond) {
1211 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001212 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1214 src2.code()*B8 | B7 | B4 | src1.code());
1215}
1216
1217
1218void Assembler::smull(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 | s | dstH.code()*B16 | dstL.code()*B12 |
1227 src2.code()*B8 | B7 | B4 | src1.code());
1228}
1229
1230
1231void Assembler::umlal(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 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1240 src2.code()*B8 | B7 | B4 | src1.code());
1241}
1242
1243
1244void Assembler::umull(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));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001252 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 src2.code()*B8 | B7 | B4 | src1.code());
1254}
1255
1256
ager@chromium.org5c838252010-02-19 08:53:10 +00001257// Miscellaneous arithmetic instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258void Assembler::clz(Register dst, Register src, Condition cond) {
1259 // v5 and above.
1260 ASSERT(!dst.is(pc) && !src.is(pc));
1261 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
ager@chromium.org378b34e2011-01-28 08:04:38 +00001262 15*B8 | CLZ | src.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263}
1264
1265
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001266// Saturating instructions.
1267
1268// Unsigned saturate.
1269void Assembler::usat(Register dst,
1270 int satpos,
1271 const Operand& src,
1272 Condition cond) {
1273 // v6 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001274 ASSERT(CpuFeatures::IsSupported(ARMv7));
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001275 ASSERT(!dst.is(pc) && !src.rm_.is(pc));
1276 ASSERT((satpos >= 0) && (satpos <= 31));
1277 ASSERT((src.shift_op_ == ASR) || (src.shift_op_ == LSL));
1278 ASSERT(src.rs_.is(no_reg));
1279
1280 int sh = 0;
1281 if (src.shift_op_ == ASR) {
1282 sh = 1;
1283 }
1284
1285 emit(cond | 0x6*B24 | 0xe*B20 | satpos*B16 | dst.code()*B12 |
1286 src.shift_imm_*B7 | sh*B6 | 0x1*B4 | src.rm_.code());
1287}
1288
1289
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001290// Bitfield manipulation instructions.
1291
1292// Unsigned bit field extract.
1293// Extracts #width adjacent bits from position #lsb in a register, and
1294// writes them to the low bits of a destination register.
1295// ubfx dst, src, #lsb, #width
1296void Assembler::ubfx(Register dst,
1297 Register src,
1298 int lsb,
1299 int width,
1300 Condition cond) {
1301 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001302 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001303 ASSERT(!dst.is(pc) && !src.is(pc));
1304 ASSERT((lsb >= 0) && (lsb <= 31));
1305 ASSERT((width >= 1) && (width <= (32 - lsb)));
1306 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1307 lsb*B7 | B6 | B4 | src.code());
1308}
1309
1310
1311// Signed bit field extract.
1312// Extracts #width adjacent bits from position #lsb in a register, and
1313// writes them to the low bits of a destination register. The extracted
1314// value is sign extended to fill the destination register.
1315// sbfx dst, src, #lsb, #width
1316void Assembler::sbfx(Register dst,
1317 Register src,
1318 int lsb,
1319 int width,
1320 Condition cond) {
1321 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001322 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001323 ASSERT(!dst.is(pc) && !src.is(pc));
1324 ASSERT((lsb >= 0) && (lsb <= 31));
1325 ASSERT((width >= 1) && (width <= (32 - lsb)));
1326 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1327 lsb*B7 | B6 | B4 | src.code());
1328}
1329
1330
1331// Bit field clear.
1332// Sets #width adjacent bits at position #lsb in the destination register
1333// to zero, preserving the value of the other bits.
1334// bfc dst, #lsb, #width
1335void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1336 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001337 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001338 ASSERT(!dst.is(pc));
1339 ASSERT((lsb >= 0) && (lsb <= 31));
1340 ASSERT((width >= 1) && (width <= (32 - lsb)));
1341 int msb = lsb + width - 1;
1342 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1343}
1344
1345
1346// Bit field insert.
1347// Inserts #width adjacent bits from the low bits of the source register
1348// into position #lsb of the destination register.
1349// bfi dst, src, #lsb, #width
1350void Assembler::bfi(Register dst,
1351 Register src,
1352 int lsb,
1353 int width,
1354 Condition cond) {
1355 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001356 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001357 ASSERT(!dst.is(pc) && !src.is(pc));
1358 ASSERT((lsb >= 0) && (lsb <= 31));
1359 ASSERT((width >= 1) && (width <= (32 - lsb)));
1360 int msb = lsb + width - 1;
1361 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1362 src.code());
1363}
1364
1365
ager@chromium.org5c838252010-02-19 08:53:10 +00001366// Status register access instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1368 ASSERT(!dst.is(pc));
1369 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1370}
1371
1372
1373void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1374 Condition cond) {
1375 ASSERT(fields >= B16 && fields < B20); // at least one field set
1376 Instr instr;
1377 if (!src.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001378 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 uint32_t rotate_imm;
1380 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001381 if (src.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001383 // Immediate operand cannot be encoded, load it first to register ip.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 RecordRelocInfo(src.rmode_, src.imm32_);
1385 ldr(ip, MemOperand(pc, 0), cond);
1386 msr(fields, Operand(ip), cond);
1387 return;
1388 }
1389 instr = I | rotate_imm*B8 | immed_8;
1390 } else {
1391 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1392 instr = src.rm_.code();
1393 }
1394 emit(cond | instr | B24 | B21 | fields | 15*B12);
1395}
1396
1397
ager@chromium.org5c838252010-02-19 08:53:10 +00001398// Load/Store instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001400 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001401 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001402 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +00001404
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001405 // Eliminate pattern: push(ry), pop(rx)
1406 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1407 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1408 // Both instructions can be eliminated if ry = rx.
1409 // If ry != rx, a register copy from ry to rx is inserted
1410 // after eliminating the push and the pop instructions.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001411 if (can_peephole_optimize(2)) {
1412 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1413 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001414
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001415 if (IsPush(push_instr) && IsPop(pop_instr)) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001416 if (Instruction::RdValue(pop_instr) != Instruction::RdValue(push_instr)) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001417 // For consecutive push and pop on different registers,
1418 // we delete both the push & pop and insert a register move.
1419 // push ry, pop rx --> mov rx, ry
1420 Register reg_pushed, reg_popped;
1421 reg_pushed = GetRd(push_instr);
1422 reg_popped = GetRd(pop_instr);
1423 pc_ -= 2 * kInstrSize;
1424 // Insert a mov instruction, which is better than a pair of push & pop
1425 mov(reg_popped, reg_pushed);
1426 if (FLAG_print_peephole_optimization) {
1427 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1428 pc_offset());
1429 }
1430 } else {
1431 // For consecutive push and pop on the same register,
1432 // both the push and the pop can be deleted.
1433 pc_ -= 2 * kInstrSize;
1434 if (FLAG_print_peephole_optimization) {
1435 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1436 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001437 }
1438 }
1439 }
1440
1441 if (can_peephole_optimize(2)) {
1442 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1443 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1444
1445 if ((IsStrRegFpOffset(str_instr) &&
1446 IsLdrRegFpOffset(ldr_instr)) ||
1447 (IsStrRegFpNegOffset(str_instr) &&
1448 IsLdrRegFpNegOffset(ldr_instr))) {
1449 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1450 (str_instr & kLdrStrInstrArgumentMask)) {
1451 // Pattern: Ldr/str same fp+offset, same register.
1452 //
1453 // The following:
1454 // str rx, [fp, #-12]
1455 // ldr rx, [fp, #-12]
1456 //
1457 // Becomes:
1458 // str rx, [fp, #-12]
1459
1460 pc_ -= 1 * kInstrSize;
1461 if (FLAG_print_peephole_optimization) {
1462 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1463 }
1464 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1465 (str_instr & kLdrStrOffsetMask)) {
1466 // Pattern: Ldr/str same fp+offset, different register.
1467 //
1468 // The following:
1469 // str rx, [fp, #-12]
1470 // ldr ry, [fp, #-12]
1471 //
1472 // Becomes:
1473 // str rx, [fp, #-12]
1474 // mov ry, rx
1475
1476 Register reg_stored, reg_loaded;
1477 reg_stored = GetRd(str_instr);
1478 reg_loaded = GetRd(ldr_instr);
1479 pc_ -= 1 * kInstrSize;
1480 // Insert a mov instruction, which is better than ldr.
1481 mov(reg_loaded, reg_stored);
1482 if (FLAG_print_peephole_optimization) {
1483 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1484 }
1485 }
1486 }
1487 }
1488
1489 if (can_peephole_optimize(3)) {
1490 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1491 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1492 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1493 if (IsPush(mem_write_instr) &&
1494 IsPop(mem_read_instr)) {
1495 if ((IsLdrRegFpOffset(ldr_instr) ||
1496 IsLdrRegFpNegOffset(ldr_instr))) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001497 if (Instruction::RdValue(mem_write_instr) ==
1498 Instruction::RdValue(mem_read_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001499 // Pattern: push & pop from/to same register,
1500 // with a fp+offset ldr in between
1501 //
1502 // The following:
1503 // str rx, [sp, #-4]!
1504 // ldr rz, [fp, #-24]
1505 // ldr rx, [sp], #+4
1506 //
1507 // Becomes:
1508 // if(rx == rz)
1509 // delete all
1510 // else
1511 // ldr rz, [fp, #-24]
1512
ager@chromium.org378b34e2011-01-28 08:04:38 +00001513 if (Instruction::RdValue(mem_write_instr) ==
1514 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001515 pc_ -= 3 * kInstrSize;
1516 } else {
1517 pc_ -= 3 * kInstrSize;
1518 // Reinsert back the ldr rz.
1519 emit(ldr_instr);
1520 }
1521 if (FLAG_print_peephole_optimization) {
1522 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1523 }
1524 } else {
1525 // Pattern: push & pop from/to different registers
1526 // with a fp+offset ldr in between
1527 //
1528 // The following:
1529 // str rx, [sp, #-4]!
1530 // ldr rz, [fp, #-24]
1531 // ldr ry, [sp], #+4
1532 //
1533 // Becomes:
1534 // if(ry == rz)
1535 // mov ry, rx;
1536 // else if(rx != rz)
1537 // ldr rz, [fp, #-24]
1538 // mov ry, rx
1539 // else if((ry != rz) || (rx == rz)) becomes:
1540 // mov ry, rx
1541 // ldr rz, [fp, #-24]
1542
1543 Register reg_pushed, reg_popped;
ager@chromium.org378b34e2011-01-28 08:04:38 +00001544 if (Instruction::RdValue(mem_read_instr) ==
1545 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001546 reg_pushed = GetRd(mem_write_instr);
1547 reg_popped = GetRd(mem_read_instr);
1548 pc_ -= 3 * kInstrSize;
1549 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001550 } else if (Instruction::RdValue(mem_write_instr) !=
1551 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001552 reg_pushed = GetRd(mem_write_instr);
1553 reg_popped = GetRd(mem_read_instr);
1554 pc_ -= 3 * kInstrSize;
1555 emit(ldr_instr);
1556 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001557 } else if ((Instruction::RdValue(mem_read_instr) !=
1558 Instruction::RdValue(ldr_instr)) ||
1559 (Instruction::RdValue(mem_write_instr) ==
1560 Instruction::RdValue(ldr_instr))) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001561 reg_pushed = GetRd(mem_write_instr);
1562 reg_popped = GetRd(mem_read_instr);
1563 pc_ -= 3 * kInstrSize;
1564 mov(reg_popped, reg_pushed);
1565 emit(ldr_instr);
1566 }
1567 if (FLAG_print_peephole_optimization) {
1568 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1569 }
1570 }
1571 }
mads.s.ager31e71382008-08-13 09:32:07 +00001572 }
1573 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574}
1575
1576
1577void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1578 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +00001579
1580 // Eliminate pattern: pop(), push(r)
1581 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1582 // -> str r, [sp, 0], al
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001583 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001584 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001585 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1586 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1587 pc_ -= 2 * kInstrSize;
1588 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001589 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001590 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1591 }
1592 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593}
1594
1595
1596void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1597 addrmod2(cond | B26 | B | L, dst, src);
1598}
1599
1600
1601void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1602 addrmod2(cond | B26 | B, src, dst);
1603}
1604
1605
1606void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1607 addrmod3(cond | L | B7 | H | B4, dst, src);
1608}
1609
1610
1611void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1612 addrmod3(cond | B7 | H | B4, src, dst);
1613}
1614
1615
1616void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1617 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1618}
1619
1620
1621void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1622 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1623}
1624
1625
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001626void Assembler::ldrd(Register dst1, Register dst2,
1627 const MemOperand& src, Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001628 ASSERT(CpuFeatures::IsEnabled(ARMv7));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001629 ASSERT(src.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001630 ASSERT(!dst1.is(lr)); // r14.
1631 ASSERT_EQ(0, dst1.code() % 2);
1632 ASSERT_EQ(dst1.code() + 1, dst2.code());
1633 addrmod3(cond | B7 | B6 | B4, dst1, src);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001634}
1635
1636
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001637void Assembler::strd(Register src1, Register src2,
1638 const MemOperand& dst, Condition cond) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001639 ASSERT(dst.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001640 ASSERT(!src1.is(lr)); // r14.
1641 ASSERT_EQ(0, src1.code() % 2);
1642 ASSERT_EQ(src1.code() + 1, src2.code());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001643 ASSERT(CpuFeatures::IsEnabled(ARMv7));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001644 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001645}
1646
ager@chromium.org5c838252010-02-19 08:53:10 +00001647// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648void Assembler::ldm(BlockAddrMode am,
1649 Register base,
1650 RegList dst,
1651 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001652 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1654
1655 addrmod4(cond | B27 | am | L, base, dst);
1656
ager@chromium.org5c838252010-02-19 08:53:10 +00001657 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 if (cond == al && (dst & pc.bit()) != 0) {
1659 // There is a slight chance that the ldm instruction was actually a call,
1660 // in which case it would be wrong to return into the constant pool; we
1661 // recognize this case by checking if the emission of the pool was blocked
1662 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1663 // the case, we emit a jump over the pool.
1664 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1665 }
1666}
1667
1668
1669void Assembler::stm(BlockAddrMode am,
1670 Register base,
1671 RegList src,
1672 Condition cond) {
1673 addrmod4(cond | B27 | am, base, src);
1674}
1675
1676
ager@chromium.org5c838252010-02-19 08:53:10 +00001677// Exception-generating instructions and debugging support.
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001678// Stops with a non-negative code less than kNumOfWatchedStops support
1679// enabling/disabling and a counter feature. See simulator-arm.h .
1680void Assembler::stop(const char* msg, Condition cond, int32_t code) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001681#ifndef __arm__
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001682 ASSERT(code >= kDefaultStopCode);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001683 // The Simulator will handle the stop instruction and get the message address.
1684 // It expects to find the address just after the svc instruction.
1685 BlockConstPoolFor(2);
1686 if (code >= 0) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001687 svc(kStopCode + code, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001688 } else {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001689 svc(kStopCode + kMaxStopCode, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001690 }
1691 emit(reinterpret_cast<Instr>(msg));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001692#else // def __arm__
1693#ifdef CAN_USE_ARMV5_INSTRUCTIONS
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001694 if (cond != al) {
1695 Label skip;
1696 b(&skip, NegateCondition(cond));
1697 bkpt(0);
1698 bind(&skip);
1699 } else {
1700 bkpt(0);
1701 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001702#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001703 svc(0x9f0001, cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001704#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1705#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706}
1707
1708
1709void Assembler::bkpt(uint32_t imm16) { // v5 and above
1710 ASSERT(is_uint16(imm16));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001711 emit(al | B24 | B21 | (imm16 >> 4)*B8 | BKPT | (imm16 & 0xf));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712}
1713
1714
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001715void Assembler::svc(uint32_t imm24, Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 ASSERT(is_uint24(imm24));
1717 emit(cond | 15*B24 | imm24);
1718}
1719
1720
ager@chromium.org5c838252010-02-19 08:53:10 +00001721// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001722void Assembler::cdp(Coprocessor coproc,
1723 int opcode_1,
1724 CRegister crd,
1725 CRegister crn,
1726 CRegister crm,
1727 int opcode_2,
1728 Condition cond) {
1729 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1730 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1731 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1732}
1733
1734
1735void Assembler::cdp2(Coprocessor coproc,
1736 int opcode_1,
1737 CRegister crd,
1738 CRegister crn,
1739 CRegister crm,
1740 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001741 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742}
1743
1744
1745void Assembler::mcr(Coprocessor coproc,
1746 int opcode_1,
1747 Register rd,
1748 CRegister crn,
1749 CRegister crm,
1750 int opcode_2,
1751 Condition cond) {
1752 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1753 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1754 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1755}
1756
1757
1758void Assembler::mcr2(Coprocessor coproc,
1759 int opcode_1,
1760 Register rd,
1761 CRegister crn,
1762 CRegister crm,
1763 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001764 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765}
1766
1767
1768void Assembler::mrc(Coprocessor coproc,
1769 int opcode_1,
1770 Register rd,
1771 CRegister crn,
1772 CRegister crm,
1773 int opcode_2,
1774 Condition cond) {
1775 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1776 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1777 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1778}
1779
1780
1781void Assembler::mrc2(Coprocessor coproc,
1782 int opcode_1,
1783 Register rd,
1784 CRegister crn,
1785 CRegister crm,
1786 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001787 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788}
1789
1790
1791void Assembler::ldc(Coprocessor coproc,
1792 CRegister crd,
1793 const MemOperand& src,
1794 LFlag l,
1795 Condition cond) {
1796 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1797}
1798
1799
1800void Assembler::ldc(Coprocessor coproc,
1801 CRegister crd,
1802 Register rn,
1803 int option,
1804 LFlag l,
1805 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001806 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807 ASSERT(is_uint8(option));
1808 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1809 coproc*B8 | (option & 255));
1810}
1811
1812
1813void Assembler::ldc2(Coprocessor coproc,
1814 CRegister crd,
1815 const MemOperand& src,
1816 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001817 ldc(coproc, crd, src, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818}
1819
1820
1821void Assembler::ldc2(Coprocessor coproc,
1822 CRegister crd,
1823 Register rn,
1824 int option,
1825 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001826 ldc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827}
1828
1829
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001830// Support for VFP.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001831
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001832void Assembler::vldr(const DwVfpRegister dst,
1833 const Register base,
1834 int offset,
1835 const Condition cond) {
1836 // Ddst = MEM(Rbase + offset).
1837 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001838 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001839 // Vdst(15-12) | 1011(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001840 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001841 int u = 1;
1842 if (offset < 0) {
1843 offset = -offset;
1844 u = 0;
1845 }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001846
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001847 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001848 if ((offset % 4) == 0 && (offset / 4) < 256) {
1849 emit(cond | u*B23 | 0xD1*B20 | base.code()*B16 | dst.code()*B12 |
1850 0xB*B8 | ((offset / 4) & 255));
1851 } else {
1852 // Larger offsets must be handled by computing the correct address
1853 // in the ip register.
1854 ASSERT(!base.is(ip));
1855 if (u == 1) {
1856 add(ip, base, Operand(offset));
1857 } else {
1858 sub(ip, base, Operand(offset));
1859 }
1860 emit(cond | 0xD1*B20 | ip.code()*B16 | dst.code()*B12 | 0xB*B8);
1861 }
1862}
1863
1864
1865void Assembler::vldr(const DwVfpRegister dst,
1866 const MemOperand& operand,
1867 const Condition cond) {
1868 ASSERT(!operand.rm().is_valid());
1869 ASSERT(operand.am_ == Offset);
1870 vldr(dst, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001871}
1872
1873
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001874void Assembler::vldr(const SwVfpRegister dst,
1875 const Register base,
1876 int offset,
1877 const Condition cond) {
1878 // Sdst = MEM(Rbase + offset).
1879 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001880 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001881 // Vdst(15-12) | 1010(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001882 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001883 int u = 1;
1884 if (offset < 0) {
1885 offset = -offset;
1886 u = 0;
1887 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001888 int sd, d;
1889 dst.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001890 ASSERT(offset >= 0);
1891
1892 if ((offset % 4) == 0 && (offset / 4) < 256) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001893 emit(cond | u*B23 | d*B22 | 0xD1*B20 | base.code()*B16 | sd*B12 |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001894 0xA*B8 | ((offset / 4) & 255));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001895 } else {
1896 // Larger offsets must be handled by computing the correct address
1897 // in the ip register.
1898 ASSERT(!base.is(ip));
1899 if (u == 1) {
1900 add(ip, base, Operand(offset));
1901 } else {
1902 sub(ip, base, Operand(offset));
1903 }
1904 emit(cond | d*B22 | 0xD1*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
1905 }
1906}
1907
1908
1909void Assembler::vldr(const SwVfpRegister dst,
1910 const MemOperand& operand,
1911 const Condition cond) {
1912 ASSERT(!operand.rm().is_valid());
1913 ASSERT(operand.am_ == Offset);
1914 vldr(dst, operand.rn(), operand.offset(), cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001915}
1916
1917
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001918void Assembler::vstr(const DwVfpRegister src,
1919 const Register base,
1920 int offset,
1921 const Condition cond) {
1922 // MEM(Rbase + offset) = Dsrc.
1923 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001924 // cond(31-28) | 1101(27-24)| U000(23-20) | | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001925 // Vsrc(15-12) | 1011(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001926 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001927 int u = 1;
1928 if (offset < 0) {
1929 offset = -offset;
1930 u = 0;
1931 }
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001932 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001933 if ((offset % 4) == 0 && (offset / 4) < 256) {
1934 emit(cond | u*B23 | 0xD0*B20 | base.code()*B16 | src.code()*B12 |
1935 0xB*B8 | ((offset / 4) & 255));
1936 } else {
1937 // Larger offsets must be handled by computing the correct address
1938 // in the ip register.
1939 ASSERT(!base.is(ip));
1940 if (u == 1) {
1941 add(ip, base, Operand(offset));
1942 } else {
1943 sub(ip, base, Operand(offset));
1944 }
1945 emit(cond | 0xD0*B20 | ip.code()*B16 | src.code()*B12 | 0xB*B8);
1946 }
1947}
1948
1949
1950void Assembler::vstr(const DwVfpRegister src,
1951 const MemOperand& operand,
1952 const Condition cond) {
1953 ASSERT(!operand.rm().is_valid());
1954 ASSERT(operand.am_ == Offset);
1955 vstr(src, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001956}
1957
1958
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001959void Assembler::vstr(const SwVfpRegister src,
1960 const Register base,
1961 int offset,
1962 const Condition cond) {
1963 // MEM(Rbase + offset) = SSrc.
1964 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001965 // cond(31-28) | 1101(27-24)| U000(23-20) | Rbase(19-16) |
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001966 // Vdst(15-12) | 1010(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001967 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001968 int u = 1;
1969 if (offset < 0) {
1970 offset = -offset;
1971 u = 0;
1972 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001973 int sd, d;
1974 src.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001975 ASSERT(offset >= 0);
1976 if ((offset % 4) == 0 && (offset / 4) < 256) {
1977 emit(cond | u*B23 | d*B22 | 0xD0*B20 | base.code()*B16 | sd*B12 |
1978 0xA*B8 | ((offset / 4) & 255));
1979 } else {
1980 // Larger offsets must be handled by computing the correct address
1981 // in the ip register.
1982 ASSERT(!base.is(ip));
1983 if (u == 1) {
1984 add(ip, base, Operand(offset));
1985 } else {
1986 sub(ip, base, Operand(offset));
1987 }
1988 emit(cond | d*B22 | 0xD0*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
1989 }
1990}
1991
1992
1993void Assembler::vstr(const SwVfpRegister src,
1994 const MemOperand& operand,
1995 const Condition cond) {
1996 ASSERT(!operand.rm().is_valid());
1997 ASSERT(operand.am_ == Offset);
1998 vldr(src, operand.rn(), operand.offset(), cond);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001999}
2000
2001
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002002void Assembler::vldm(BlockAddrMode am,
2003 Register base,
2004 DwVfpRegister first,
2005 DwVfpRegister last,
2006 Condition cond) {
2007 // Instruction details available in ARM DDI 0406A, A8-626.
2008 // cond(31-28) | 110(27-25)| PUDW1(24-20) | Rbase(19-16) |
2009 // first(15-12) | 1010(11-8) | (count * 2)
2010 ASSERT(CpuFeatures::IsEnabled(VFP3));
2011 ASSERT_LE(first.code(), last.code());
2012 ASSERT(am == ia || am == ia_w || am == db_w);
2013 ASSERT(!base.is(pc));
2014
2015 int sd, d;
2016 first.split_code(&sd, &d);
2017 int count = last.code() - first.code() + 1;
2018 emit(cond | B27 | B26 | am | d*B22 | B20 | base.code()*B16 | sd*B12 |
2019 0xB*B8 | count*2);
2020}
2021
2022
2023void Assembler::vstm(BlockAddrMode am,
2024 Register base,
2025 DwVfpRegister first,
2026 DwVfpRegister last,
2027 Condition cond) {
2028 // Instruction details available in ARM DDI 0406A, A8-784.
2029 // cond(31-28) | 110(27-25)| PUDW0(24-20) | Rbase(19-16) |
2030 // first(15-12) | 1011(11-8) | (count * 2)
2031 ASSERT(CpuFeatures::IsEnabled(VFP3));
2032 ASSERT_LE(first.code(), last.code());
2033 ASSERT(am == ia || am == ia_w || am == db_w);
2034 ASSERT(!base.is(pc));
2035
2036 int sd, d;
2037 first.split_code(&sd, &d);
2038 int count = last.code() - first.code() + 1;
2039 emit(cond | B27 | B26 | am | d*B22 | base.code()*B16 | sd*B12 |
2040 0xB*B8 | count*2);
2041}
2042
2043void Assembler::vldm(BlockAddrMode am,
2044 Register base,
2045 SwVfpRegister first,
2046 SwVfpRegister last,
2047 Condition cond) {
2048 // Instruction details available in ARM DDI 0406A, A8-626.
2049 // cond(31-28) | 110(27-25)| PUDW1(24-20) | Rbase(19-16) |
2050 // first(15-12) | 1010(11-8) | (count/2)
2051 ASSERT(CpuFeatures::IsEnabled(VFP3));
2052 ASSERT_LE(first.code(), last.code());
2053 ASSERT(am == ia || am == ia_w || am == db_w);
2054 ASSERT(!base.is(pc));
2055
2056 int sd, d;
2057 first.split_code(&sd, &d);
2058 int count = last.code() - first.code() + 1;
2059 emit(cond | B27 | B26 | am | d*B22 | B20 | base.code()*B16 | sd*B12 |
2060 0xA*B8 | count);
2061}
2062
2063
2064void Assembler::vstm(BlockAddrMode am,
2065 Register base,
2066 SwVfpRegister first,
2067 SwVfpRegister last,
2068 Condition cond) {
2069 // Instruction details available in ARM DDI 0406A, A8-784.
2070 // cond(31-28) | 110(27-25)| PUDW0(24-20) | Rbase(19-16) |
2071 // first(15-12) | 1011(11-8) | (count/2)
2072 ASSERT(CpuFeatures::IsEnabled(VFP3));
2073 ASSERT_LE(first.code(), last.code());
2074 ASSERT(am == ia || am == ia_w || am == db_w);
2075 ASSERT(!base.is(pc));
2076
2077 int sd, d;
2078 first.split_code(&sd, &d);
2079 int count = last.code() - first.code() + 1;
2080 emit(cond | B27 | B26 | am | d*B22 | base.code()*B16 | sd*B12 |
2081 0xA*B8 | count);
2082}
2083
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002084static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
2085 uint64_t i;
2086 memcpy(&i, &d, 8);
2087
2088 *lo = i & 0xffffffff;
2089 *hi = i >> 32;
2090}
2091
2092// Only works for little endian floating point formats.
2093// We don't support VFP on the mixed endian floating point platform.
2094static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002095 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002096
2097 // VMOV can accept an immediate of the form:
2098 //
2099 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
2100 //
2101 // The immediate is encoded using an 8-bit quantity, comprised of two
2102 // 4-bit fields. For an 8-bit immediate of the form:
2103 //
2104 // [abcdefgh]
2105 //
2106 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
2107 // created of the form:
2108 //
2109 // [aBbbbbbb,bbcdefgh,00000000,00000000,
2110 // 00000000,00000000,00000000,00000000]
2111 //
2112 // where B = ~b.
2113 //
2114
2115 uint32_t lo, hi;
2116 DoubleAsTwoUInt32(d, &lo, &hi);
2117
2118 // The most obvious constraint is the long block of zeroes.
2119 if ((lo != 0) || ((hi & 0xffff) != 0)) {
2120 return false;
2121 }
2122
2123 // Bits 62:55 must be all clear or all set.
2124 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
2125 return false;
2126 }
2127
2128 // Bit 63 must be NOT bit 62.
2129 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
2130 return false;
2131 }
2132
2133 // Create the encoded immediate in the form:
2134 // [00000000,0000abcd,00000000,0000efgh]
2135 *encoding = (hi >> 16) & 0xf; // Low nybble.
2136 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
2137 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
2138
2139 return true;
2140}
2141
2142
2143void Assembler::vmov(const DwVfpRegister dst,
2144 double imm,
2145 const Condition cond) {
2146 // Dd = immediate
2147 // Instruction details available in ARM DDI 0406B, A8-640.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002148 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002149
2150 uint32_t enc;
2151 if (FitsVMOVDoubleImmediate(imm, &enc)) {
2152 // The double can be encoded in the instruction.
2153 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
2154 } else {
2155 // Synthesise the double from ARM immediates. This could be implemented
2156 // using vldr from a constant pool.
2157 uint32_t lo, hi;
2158 DoubleAsTwoUInt32(imm, &lo, &hi);
2159
2160 if (lo == hi) {
2161 // If the lo and hi parts of the double are equal, the literal is easier
2162 // to create. This is the case with 0.0.
2163 mov(ip, Operand(lo));
2164 vmov(dst, ip, ip);
2165 } else {
2166 // Move the low part of the double into the lower of the corresponsing S
2167 // registers of D register dst.
2168 mov(ip, Operand(lo));
2169 vmov(dst.low(), ip, cond);
2170
2171 // Move the high part of the double into the higher of the corresponsing S
2172 // registers of D register dst.
2173 mov(ip, Operand(hi));
2174 vmov(dst.high(), ip, cond);
2175 }
2176 }
2177}
2178
2179
2180void Assembler::vmov(const SwVfpRegister dst,
2181 const SwVfpRegister src,
2182 const Condition cond) {
2183 // Sd = Sm
2184 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002185 ASSERT(CpuFeatures::IsEnabled(VFP3));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002186 int sd, d, sm, m;
2187 dst.split_code(&sd, &d);
2188 src.split_code(&sm, &m);
2189 emit(cond | 0xE*B24 | d*B22 | 0xB*B20 | sd*B12 | 0xA*B8 | B6 | m*B5 | sm);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002190}
2191
2192
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002193void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002194 const DwVfpRegister src,
2195 const Condition cond) {
2196 // Dd = Dm
2197 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002198 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002199 emit(cond | 0xE*B24 | 0xB*B20 |
2200 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
2201}
2202
2203
2204void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002205 const Register src1,
2206 const Register src2,
2207 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002208 // Dm = <Rt,Rt2>.
2209 // Instruction details available in ARM DDI 0406A, A8-646.
2210 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
2211 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002212 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002213 ASSERT(!src1.is(pc) && !src2.is(pc));
2214 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
2215 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
2216}
2217
2218
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002219void Assembler::vmov(const Register dst1,
2220 const Register dst2,
2221 const DwVfpRegister src,
2222 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002223 // <Rt,Rt2> = Dm.
2224 // Instruction details available in ARM DDI 0406A, A8-646.
2225 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
2226 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002227 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002228 ASSERT(!dst1.is(pc) && !dst2.is(pc));
2229 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
2230 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
2231}
2232
2233
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002234void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002235 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002236 const Condition cond) {
2237 // Sn = Rt.
2238 // Instruction details available in ARM DDI 0406A, A8-642.
2239 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
2240 // 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 +00002241 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002242 ASSERT(!src.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002243 int sn, n;
2244 dst.split_code(&sn, &n);
2245 emit(cond | 0xE*B24 | sn*B16 | src.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002246}
2247
2248
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002249void Assembler::vmov(const Register dst,
2250 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002251 const Condition cond) {
2252 // Rt = Sn.
2253 // Instruction details available in ARM DDI 0406A, A8-642.
2254 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
2255 // 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 +00002256 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002257 ASSERT(!dst.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002258 int sn, n;
2259 src.split_code(&sn, &n);
2260 emit(cond | 0xE*B24 | B20 | sn*B16 | dst.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002261}
2262
2263
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002264// Type of data to read from or write to VFP register.
2265// Used as specifier in generic vcvt instruction.
2266enum VFPType { S32, U32, F32, F64 };
2267
2268
2269static bool IsSignedVFPType(VFPType type) {
2270 switch (type) {
2271 case S32:
2272 return true;
2273 case U32:
2274 return false;
2275 default:
2276 UNREACHABLE();
2277 return false;
2278 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002279}
2280
2281
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002282static bool IsIntegerVFPType(VFPType type) {
2283 switch (type) {
2284 case S32:
2285 case U32:
2286 return true;
2287 case F32:
2288 case F64:
2289 return false;
2290 default:
2291 UNREACHABLE();
2292 return false;
2293 }
2294}
2295
2296
2297static bool IsDoubleVFPType(VFPType type) {
2298 switch (type) {
2299 case F32:
2300 return false;
2301 case F64:
2302 return true;
2303 default:
2304 UNREACHABLE();
2305 return false;
2306 }
2307}
2308
2309
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002310// Split five bit reg_code based on size of reg_type.
2311// 32-bit register codes are Vm:M
2312// 64-bit register codes are M:Vm
2313// where Vm is four bits, and M is a single bit.
2314static void SplitRegCode(VFPType reg_type,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002315 int reg_code,
2316 int* vm,
2317 int* m) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002318 ASSERT((reg_code >= 0) && (reg_code <= 31));
2319 if (IsIntegerVFPType(reg_type) || !IsDoubleVFPType(reg_type)) {
2320 // 32 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002321 *m = reg_code & 0x1;
2322 *vm = reg_code >> 1;
2323 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002324 // 64 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002325 *m = (reg_code & 0x10) >> 4;
2326 *vm = reg_code & 0x0F;
2327 }
2328}
2329
2330
2331// Encode vcvt.src_type.dst_type instruction.
2332static Instr EncodeVCVT(const VFPType dst_type,
2333 const int dst_code,
2334 const VFPType src_type,
2335 const int src_code,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002336 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002337 const Condition cond) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002338 ASSERT(src_type != dst_type);
2339 int D, Vd, M, Vm;
2340 SplitRegCode(src_type, src_code, &Vm, &M);
2341 SplitRegCode(dst_type, dst_code, &Vd, &D);
2342
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002343 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2344 // Conversion between IEEE floating point and 32-bit integer.
2345 // Instruction details available in ARM DDI 0406B, A8.6.295.
2346 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2347 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2348 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2349
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002350 int sz, opc2, op;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002351
2352 if (IsIntegerVFPType(dst_type)) {
2353 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2354 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002355 op = mode;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002356 } else {
2357 ASSERT(IsIntegerVFPType(src_type));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002358 opc2 = 0x0;
2359 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2360 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002361 }
2362
2363 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2364 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2365 } else {
2366 // Conversion between IEEE double and single precision.
2367 // Instruction details available in ARM DDI 0406B, A8.6.298.
2368 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2369 // 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 +00002370 int sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002371 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2372 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2373 }
2374}
2375
2376
2377void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2378 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002379 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002380 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002381 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002382 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002383}
2384
2385
2386void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2387 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002388 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002389 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002390 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002391 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002392}
2393
2394
2395void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2396 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002397 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002398 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002399 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002400 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002401}
2402
2403
2404void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2405 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002406 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002407 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002408 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002409 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002410}
2411
2412
2413void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2414 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002415 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002416 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002417 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002418 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002419}
2420
2421
2422void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2423 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002424 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002425 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002426 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002427 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002428}
2429
2430
2431void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2432 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002433 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002434 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002435 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002436 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), mode, cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002437}
2438
2439
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002440void Assembler::vneg(const DwVfpRegister dst,
2441 const DwVfpRegister src,
2442 const Condition cond) {
2443 emit(cond | 0xE*B24 | 0xB*B20 | B16 | dst.code()*B12 |
2444 0x5*B9 | B8 | B6 | src.code());
2445}
2446
2447
whesse@chromium.org7a392b32011-01-31 11:30:36 +00002448void Assembler::vabs(const DwVfpRegister dst,
2449 const DwVfpRegister src,
2450 const Condition cond) {
2451 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 |
2452 0x5*B9 | B8 | 0x3*B6 | src.code());
2453}
2454
2455
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002456void Assembler::vadd(const DwVfpRegister dst,
2457 const DwVfpRegister src1,
2458 const DwVfpRegister src2,
2459 const Condition cond) {
2460 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002461 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2462 // Instruction details available in ARM DDI 0406A, A8-536.
2463 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2464 // 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 +00002465 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002466 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2467 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2468}
2469
2470
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002471void Assembler::vsub(const DwVfpRegister dst,
2472 const DwVfpRegister src1,
2473 const DwVfpRegister src2,
2474 const Condition cond) {
2475 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002476 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2477 // Instruction details available in ARM DDI 0406A, A8-784.
2478 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2479 // 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 +00002480 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002481 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2482 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2483}
2484
2485
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002486void Assembler::vmul(const DwVfpRegister dst,
2487 const DwVfpRegister src1,
2488 const DwVfpRegister src2,
2489 const Condition cond) {
2490 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002491 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2492 // Instruction details available in ARM DDI 0406A, A8-784.
2493 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2494 // 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 +00002495 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002496 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2497 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2498}
2499
2500
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002501void Assembler::vdiv(const DwVfpRegister dst,
2502 const DwVfpRegister src1,
2503 const DwVfpRegister src2,
2504 const Condition cond) {
2505 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002506 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2507 // Instruction details available in ARM DDI 0406A, A8-584.
2508 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2509 // 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 +00002510 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002511 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2512 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2513}
2514
2515
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002516void Assembler::vcmp(const DwVfpRegister src1,
2517 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002518 const Condition cond) {
2519 // vcmp(Dd, Dm) double precision floating point comparison.
2520 // Instruction details available in ARM DDI 0406A, A8-570.
2521 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002522 // 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 +00002523 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002524 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002525 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002526}
2527
2528
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002529void Assembler::vcmp(const DwVfpRegister src1,
2530 const double src2,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +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) | 0101 (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) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002536 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002537 ASSERT(src2 == 0.0);
2538 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | B16 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002539 src1.code()*B12 | 0x5*B9 | B8 | B6);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002540}
2541
2542
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002543void Assembler::vmsr(Register dst, Condition cond) {
2544 // Instruction details available in ARM DDI 0406A, A8-652.
2545 // cond(31-28) | 1110 (27-24) | 1110(23-20)| 0001 (19-16) |
2546 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002547 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002548 emit(cond | 0xE*B24 | 0xE*B20 | B16 |
2549 dst.code()*B12 | 0xA*B8 | B4);
2550}
2551
2552
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002553void Assembler::vmrs(Register dst, Condition cond) {
2554 // Instruction details available in ARM DDI 0406A, A8-652.
2555 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2556 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002557 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002558 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2559 dst.code()*B12 | 0xA*B8 | B4);
2560}
2561
2562
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002563void Assembler::vsqrt(const DwVfpRegister dst,
2564 const DwVfpRegister src,
2565 const Condition cond) {
2566 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2567 // 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 +00002568 ASSERT(CpuFeatures::IsEnabled(VFP3));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002569 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2570 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2571}
2572
2573
ager@chromium.org5c838252010-02-19 08:53:10 +00002574// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002575void Assembler::nop(int type) {
2576 // This is mov rx, rx.
2577 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2578 emit(al | 13*B21 | type*B12 | type);
2579}
2580
2581
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002582bool Assembler::IsNop(Instr instr, int type) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002583 // Check for mov rx, rx where x = type.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002584 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2585 return instr == (al | 13*B21 | type*B12 | type);
2586}
2587
2588
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002589bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2590 uint32_t dummy1;
2591 uint32_t dummy2;
2592 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2593}
2594
2595
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002596void Assembler::BlockConstPoolFor(int instructions) {
2597 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2598}
2599
2600
ager@chromium.org5c838252010-02-19 08:53:10 +00002601// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002602void Assembler::RecordJSReturn() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002603 positions_recorder()->WriteRecordedPositions();
ager@chromium.org4af710e2009-09-15 12:20:11 +00002604 CheckBuffer();
2605 RecordRelocInfo(RelocInfo::JS_RETURN);
2606}
2607
2608
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002609void Assembler::RecordDebugBreakSlot() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002610 positions_recorder()->WriteRecordedPositions();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002611 CheckBuffer();
2612 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2613}
2614
2615
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616void Assembler::RecordComment(const char* msg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002617 if (FLAG_code_comments) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00002619 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620 }
2621}
2622
2623
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624void Assembler::GrowBuffer() {
2625 if (!own_buffer_) FATAL("external code buffer is too small");
2626
ager@chromium.org5c838252010-02-19 08:53:10 +00002627 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 CodeDesc desc; // the new buffer
2629 if (buffer_size_ < 4*KB) {
2630 desc.buffer_size = 4*KB;
2631 } else if (buffer_size_ < 1*MB) {
2632 desc.buffer_size = 2*buffer_size_;
2633 } else {
2634 desc.buffer_size = buffer_size_ + 1*MB;
2635 }
2636 CHECK_GT(desc.buffer_size, 0); // no overflow
2637
ager@chromium.org5c838252010-02-19 08:53:10 +00002638 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 desc.buffer = NewArray<byte>(desc.buffer_size);
2640
2641 desc.instr_size = pc_offset();
2642 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2643
ager@chromium.org5c838252010-02-19 08:53:10 +00002644 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 int pc_delta = desc.buffer - buffer_;
2646 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2647 memmove(desc.buffer, buffer_, desc.instr_size);
2648 memmove(reloc_info_writer.pos() + rc_delta,
2649 reloc_info_writer.pos(), desc.reloc_size);
2650
ager@chromium.org5c838252010-02-19 08:53:10 +00002651 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 DeleteArray(buffer_);
2653 buffer_ = desc.buffer;
2654 buffer_size_ = desc.buffer_size;
2655 pc_ += pc_delta;
2656 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2657 reloc_info_writer.last_pc() + pc_delta);
2658
ager@chromium.org5c838252010-02-19 08:53:10 +00002659 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00002661 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662
ager@chromium.org5c838252010-02-19 08:53:10 +00002663 // Relocate pending relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 for (int i = 0; i < num_prinfo_; i++) {
2665 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002666 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2667 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00002668 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2669 rinfo.set_pc(rinfo.pc() + pc_delta);
2670 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 }
2672}
2673
2674
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002675void Assembler::db(uint8_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002676 // No relocation info should be pending while using db. db is used
2677 // to write pure data with no pointers and the constant pool should
2678 // be emitted before using db.
2679 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002680 CheckBuffer();
2681 *reinterpret_cast<uint8_t*>(pc_) = data;
2682 pc_ += sizeof(uint8_t);
2683}
2684
2685
2686void Assembler::dd(uint32_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002687 // No relocation info should be pending while using dd. dd is used
2688 // to write pure data with no pointers and the constant pool should
2689 // be emitted before using dd.
2690 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002691 CheckBuffer();
2692 *reinterpret_cast<uint32_t*>(pc_) = data;
2693 pc_ += sizeof(uint32_t);
2694}
2695
2696
ager@chromium.org236ad962008-09-25 09:45:57 +00002697void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002699 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002700 // Adjust code for new modes.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002701 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2702 || RelocInfo::IsJSReturn(rmode)
ager@chromium.org4af710e2009-09-15 12:20:11 +00002703 || RelocInfo::IsComment(rmode)
2704 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00002705 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 } else {
2707 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2708 prinfo_[num_prinfo_++] = rinfo;
2709 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00002710 // instruction for which we just recorded relocation info.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 BlockConstPoolBefore(pc_offset() + kInstrSize);
2712 }
ager@chromium.org236ad962008-09-25 09:45:57 +00002713 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002714 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002715 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2716#ifdef DEBUG
2717 if (!Serializer::enabled()) {
2718 Serializer::TooLateToEnableNow();
2719 }
2720#endif
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002721 if (!Serializer::enabled() && !emit_debug_code()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002722 return;
2723 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002724 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002726 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
2727 ASSERT(ast_id_for_reloc_info_ != kNoASTId);
2728 RelocInfo reloc_info_with_ast_id(pc_, rmode, ast_id_for_reloc_info_);
2729 ast_id_for_reloc_info_ = kNoASTId;
2730 reloc_info_writer.Write(&reloc_info_with_ast_id);
2731 } else {
2732 reloc_info_writer.Write(&rinfo);
2733 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734 }
2735}
2736
2737
2738void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2739 // Calculate the offset of the next check. It will be overwritten
2740 // when a const pool is generated or when const pools are being
2741 // blocked for a specific range.
2742 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2743
ager@chromium.org5c838252010-02-19 08:53:10 +00002744 // There is nothing to do if there are no pending relocation info entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002745 if (num_prinfo_ == 0) return;
2746
2747 // We emit a constant pool at regular intervals of about kDistBetweenPools
2748 // or when requested by parameter force_emit (e.g. after each function).
2749 // We prefer not to emit a jump unless the max distance is reached or if we
2750 // are running low on slots, which can happen if a lot of constants are being
2751 // emitted (e.g. --debug-code and many static references).
2752 int dist = pc_offset() - last_const_pool_end_;
2753 if (!force_emit && dist < kMaxDistBetweenPools &&
2754 (require_jump || dist < kDistBetweenPools) &&
2755 // TODO(1236125): Cleanup the "magic" number below. We know that
2756 // the code generation will test every kCheckConstIntervalInst.
2757 // Thus we are safe as long as we generate less than 7 constant
2758 // entries per instruction.
2759 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2760 return;
2761 }
2762
2763 // If we did not return by now, we need to emit the constant pool soon.
2764
2765 // However, some small sequences of instructions must not be broken up by the
2766 // insertion of a constant pool; such sequences are protected by setting
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002767 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2768 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2769 // no_const_pool_before_.
2770 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002771 // Emission is currently blocked; make sure we try again as soon as
2772 // possible.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002773 if (const_pool_blocked_nesting_ > 0) {
2774 next_buffer_check_ = pc_offset() + kInstrSize;
2775 } else {
2776 next_buffer_check_ = no_const_pool_before_;
2777 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778
ager@chromium.org5c838252010-02-19 08:53:10 +00002779 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 ASSERT(!force_emit);
2781 return;
2782 }
2783
2784 int jump_instr = require_jump ? kInstrSize : 0;
2785
2786 // Check that the code buffer is large enough before emitting the constant
2787 // pool and relocation information (include the jump over the pool and the
2788 // constant pool marker).
2789 int max_needed_space =
2790 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2791 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2792
ager@chromium.org5c838252010-02-19 08:53:10 +00002793 // Block recursive calls to CheckConstPool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002794 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2795 num_prinfo_*kInstrSize);
2796 // Don't bother to check for the emit calls below.
2797 next_buffer_check_ = no_const_pool_before_;
2798
ager@chromium.org5c838252010-02-19 08:53:10 +00002799 // Emit jump over constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 Label after_pool;
2801 if (require_jump) b(&after_pool);
2802
2803 RecordComment("[ Constant Pool");
2804
ager@chromium.org5c838252010-02-19 08:53:10 +00002805 // Put down constant pool marker "Undefined instruction" as specified by
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002806 // A5.6 (ARMv7) Instruction set encoding.
2807 emit(kConstantPoolMarker | num_prinfo_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002808
ager@chromium.org5c838252010-02-19 08:53:10 +00002809 // Emit constant pool entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810 for (int i = 0; i < num_prinfo_; i++) {
2811 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002812 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2813 rinfo.rmode() != RelocInfo::POSITION &&
2814 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815 Instr instr = instr_at(rinfo.pc());
ager@chromium.org4af710e2009-09-15 12:20:11 +00002816
ager@chromium.org5c838252010-02-19 08:53:10 +00002817 // Instruction to patch must be a ldr/str [pc, #offset].
2818 // P and U set, B and W clear, Rn == pc, offset12 still 0.
ager@chromium.org378b34e2011-01-28 08:04:38 +00002819 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | kOff12Mask)) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820 (2*B25 | P | U | pc.code()*B16));
2821 int delta = pc_ - rinfo.pc() - 8;
2822 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2823 if (delta < 0) {
2824 instr &= ~U;
2825 delta = -delta;
2826 }
2827 ASSERT(is_uint12(delta));
2828 instr_at_put(rinfo.pc(), instr + delta);
2829 emit(rinfo.data());
2830 }
2831 num_prinfo_ = 0;
2832 last_const_pool_end_ = pc_offset();
2833
2834 RecordComment("]");
2835
2836 if (after_pool.is_linked()) {
2837 bind(&after_pool);
2838 }
2839
2840 // Since a constant pool was just emitted, move the check offset forward by
2841 // the standard interval.
2842 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2843}
2844
2845
2846} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002847
2848#endif // V8_TARGET_ARCH_ARM