blob: 49b1975f6b73325262d1535e7cc21efb34e54ee8 [file] [log] [blame]
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003//
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the
14// distribution.
15//
16// - Neither the name of Sun Microsystems or the names of contributors may
17// be used to endorse or promote products derived from this software without
18// specific prior written permission.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000019//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000022// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31// OF THE POSSIBILITY OF SUCH DAMAGE.
32
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033// The original source code covered by the above license above has been
34// modified significantly by Google Inc.
35// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37#include "v8.h"
38
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000039#if defined(V8_TARGET_ARCH_ARM)
40
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000041#include "arm/assembler-arm-inl.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000042#include "serialize.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
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
62 // compilation.
63#if defined(__VFP_FP__) && !defined(__SOFTFP__)
64 answer |= 1u << VFP3;
65#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
66#ifdef CAN_USE_VFP_INSTRUCTIONS
67 answer |= 1u << VFP3;
68#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.org5c838252010-02-19 08:53:10 +000080 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled.
81 if (FLAG_enable_vfp3) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000082 supported_ |= 1u << VFP3;
ager@chromium.org5c838252010-02-19 08:53:10 +000083 }
84 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
85 if (FLAG_enable_armv7) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000086 supported_ |= 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +000087 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000088#else // def __arm__
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000089 if (Serializer::enabled()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000090 supported_ |= OS::CpuFeaturesImpliedByPlatform();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000091 supported_ |= CpuFeaturesImpliedByCompiler();
ager@chromium.orgc4c92722009-11-18 14:12:51 +000092 return; // No features if we might serialize.
93 }
94
95 if (OS::ArmCpuHasFeature(VFP3)) {
96 // This implementation also sets the VFP flags if
97 // runtime detection of VFP returns true.
98 supported_ |= 1u << VFP3;
99 found_by_runtime_probing_ |= 1u << VFP3;
100 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000101
102 if (OS::ArmCpuHasFeature(ARMv7)) {
103 supported_ |= 1u << ARMv7;
104 found_by_runtime_probing_ |= 1u << ARMv7;
105 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000106#endif
107}
108
109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111// Implementation of RelocInfo
112
113const int RelocInfo::kApplyMask = 0;
114
115
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000116bool RelocInfo::IsCodedSpecially() {
117 // The deserializer needs to know whether a pointer is specially coded. Being
118 // specially coded on ARM means that it is a movw/movt instruction. We don't
119 // generate those yet.
120 return false;
121}
122
123
124
iposva@chromium.org245aa852009-02-10 00:49:54 +0000125void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000127 Instr* pc = reinterpret_cast<Instr*>(pc_);
128 Instr* instr = reinterpret_cast<Instr*>(instructions);
129 for (int i = 0; i < instruction_count; i++) {
130 *(pc + i) = *(instr + i);
131 }
132
133 // Indicate that code has changed.
134 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135}
136
137
138// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000139// Additional guard instructions can be added if required.
140void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 // Patch the code at the current address with a call to the target.
142 UNIMPLEMENTED();
143}
144
145
146// -----------------------------------------------------------------------------
147// Implementation of Operand and MemOperand
148// See assembler-arm-inl.h for inlined constructors
149
150Operand::Operand(Handle<Object> handle) {
151 rm_ = no_reg;
152 // Verify all Objects referred by code are NOT in new space.
153 Object* obj = *handle;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000154 ASSERT(!HEAP->InNewSpace(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 if (obj->IsHeapObject()) {
156 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000157 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 } else {
159 // no relocation needed
160 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000161 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 }
163}
164
165
166Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
167 ASSERT(is_uint5(shift_imm));
168 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
169 rm_ = rm;
170 rs_ = no_reg;
171 shift_op_ = shift_op;
172 shift_imm_ = shift_imm & 31;
173 if (shift_op == RRX) {
174 // encoded as ROR with shift_imm == 0
175 ASSERT(shift_imm == 0);
176 shift_op_ = ROR;
177 shift_imm_ = 0;
178 }
179}
180
181
182Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
183 ASSERT(shift_op != RRX);
184 rm_ = rm;
185 rs_ = no_reg;
186 shift_op_ = shift_op;
187 rs_ = rs;
188}
189
190
191MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
192 rn_ = rn;
193 rm_ = no_reg;
194 offset_ = offset;
195 am_ = am;
196}
197
198MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
199 rn_ = rn;
200 rm_ = rm;
201 shift_op_ = LSL;
202 shift_imm_ = 0;
203 am_ = am;
204}
205
206
207MemOperand::MemOperand(Register rn, Register rm,
208 ShiftOp shift_op, int shift_imm, AddrMode am) {
209 ASSERT(is_uint5(shift_imm));
210 rn_ = rn;
211 rm_ = rm;
212 shift_op_ = shift_op;
213 shift_imm_ = shift_imm & 31;
214 am_ = am;
215}
216
217
218// -----------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000219// Specific instructions, constants, and masks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220
mads.s.ager31e71382008-08-13 09:32:07 +0000221// add(sp, sp, 4) instruction (aka Pop())
ager@chromium.org378b34e2011-01-28 08:04:38 +0000222const Instr kPopInstruction =
223 al | PostIndex | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
mads.s.ager31e71382008-08-13 09:32:07 +0000224// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
225// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000226const Instr kPushRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000227 al | B26 | 4 | NegPreIndex | sp.code() * B16;
228// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
229// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000230const Instr kPopRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000231 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000232// mov lr, pc
ager@chromium.org378b34e2011-01-28 08:04:38 +0000233const Instr kMovLrPc = al | MOV | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000234// ldr rd, [pc, #offset]
ager@chromium.org378b34e2011-01-28 08:04:38 +0000235const Instr kLdrPCMask = kCondMask | 15 * B24 | 7 * B20 | 15 * B16;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000236const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
237// blxcc rm
238const Instr kBlxRegMask =
239 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
240const Instr kBlxRegPattern =
ager@chromium.org378b34e2011-01-28 08:04:38 +0000241 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | BLX;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000242const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
243const Instr kMovMvnPattern = 0xd * B21;
244const Instr kMovMvnFlip = B22;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000245const Instr kMovLeaveCCMask = 0xdff * B16;
246const Instr kMovLeaveCCPattern = 0x1a0 * B16;
247const Instr kMovwMask = 0xff * B20;
248const Instr kMovwPattern = 0x30 * B20;
249const Instr kMovwLeaveCCFlip = 0x5 * B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000250const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
251const Instr kCmpCmnPattern = 0x15 * B20;
252const Instr kCmpCmnFlip = B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000253const Instr kAddSubFlip = 0x6 * B21;
254const Instr kAndBicFlip = 0xe * B21;
255
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000256// A mask for the Rd register for push, pop, ldr, str instructions.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000257const Instr kLdrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000258 al | B26 | L | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000259const Instr kStrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000260 al | B26 | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000261const Instr kLdrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000262 al | B26 | L | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000263const Instr kStrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000264 al | B26 | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000265const Instr kLdrStrInstrTypeMask = 0xffff0000;
266const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
267const Instr kLdrStrOffsetMask = 0x00000fff;
268
mads.s.ager31e71382008-08-13 09:32:07 +0000269
ager@chromium.org5c838252010-02-19 08:53:10 +0000270// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271static const int kMinimalBufferSize = 4*KB;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272
ager@chromium.org378b34e2011-01-28 08:04:38 +0000273
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000274Assembler::Assembler(Isolate* arg_isolate, void* buffer, int buffer_size)
275 : AssemblerBase(arg_isolate),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000276 positions_recorder_(this),
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000277 allow_peephole_optimization_(false),
278 emit_debug_code_(FLAG_debug_code) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000279 allow_peephole_optimization_ = FLAG_peephole_optimization;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000281 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 if (buffer_size <= kMinimalBufferSize) {
283 buffer_size = kMinimalBufferSize;
284
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000285 if (isolate()->assembler_spare_buffer() != NULL) {
286 buffer = isolate()->assembler_spare_buffer();
287 isolate()->set_assembler_spare_buffer(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 }
289 }
290 if (buffer == NULL) {
291 buffer_ = NewArray<byte>(buffer_size);
292 } else {
293 buffer_ = static_cast<byte*>(buffer);
294 }
295 buffer_size_ = buffer_size;
296 own_buffer_ = true;
297
298 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000299 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 ASSERT(buffer_size > 0);
301 buffer_ = static_cast<byte*>(buffer);
302 buffer_size_ = buffer_size;
303 own_buffer_ = false;
304 }
305
ager@chromium.org5c838252010-02-19 08:53:10 +0000306 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 ASSERT(buffer_ != NULL);
308 pc_ = buffer_;
309 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
310 num_prinfo_ = 0;
311 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000312 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 no_const_pool_before_ = 0;
314 last_const_pool_end_ = 0;
315 last_bound_pos_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316}
317
318
319Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000320 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 if (own_buffer_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 if (isolate()->assembler_spare_buffer() == NULL &&
323 buffer_size_ == kMinimalBufferSize) {
324 isolate()->set_assembler_spare_buffer(buffer_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 } else {
326 DeleteArray(buffer_);
327 }
328 }
329}
330
331
332void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000333 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 CheckConstPool(true, false);
335 ASSERT(num_prinfo_ == 0);
336
ager@chromium.org5c838252010-02-19 08:53:10 +0000337 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 desc->buffer = buffer_;
339 desc->buffer_size = buffer_size_;
340 desc->instr_size = pc_offset();
341 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
342}
343
344
345void Assembler::Align(int m) {
346 ASSERT(m >= 4 && IsPowerOf2(m));
347 while ((pc_offset() & (m - 1)) != 0) {
348 nop();
349 }
350}
351
352
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000353void Assembler::CodeTargetAlign() {
354 // Preferred alignment of jump targets on some ARM chips.
355 Align(8);
356}
357
358
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000359Condition Assembler::GetCondition(Instr instr) {
360 return Instruction::ConditionField(instr);
361}
362
363
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000364bool Assembler::IsBranch(Instr instr) {
365 return (instr & (B27 | B25)) == (B27 | B25);
366}
367
368
369int Assembler::GetBranchOffset(Instr instr) {
370 ASSERT(IsBranch(instr));
371 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
372 // with 4 to get the offset in bytes.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000373 return ((instr & kImm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000374}
375
376
377bool Assembler::IsLdrRegisterImmediate(Instr instr) {
378 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
379}
380
381
382int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
383 ASSERT(IsLdrRegisterImmediate(instr));
384 bool positive = (instr & B23) == B23;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000385 int offset = instr & kOff12Mask; // Zero extended offset.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000386 return positive ? offset : -offset;
387}
388
389
390Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
391 ASSERT(IsLdrRegisterImmediate(instr));
392 bool positive = offset >= 0;
393 if (!positive) offset = -offset;
394 ASSERT(is_uint12(offset));
395 // Set bit indicating whether the offset should be added.
396 instr = (instr & ~B23) | (positive ? B23 : 0);
397 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000398 return (instr & ~kOff12Mask) | offset;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000399}
400
401
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000402bool Assembler::IsStrRegisterImmediate(Instr instr) {
403 return (instr & (B27 | B26 | B25 | B22 | B20)) == B26;
404}
405
406
407Instr Assembler::SetStrRegisterImmediateOffset(Instr instr, int offset) {
408 ASSERT(IsStrRegisterImmediate(instr));
409 bool positive = offset >= 0;
410 if (!positive) offset = -offset;
411 ASSERT(is_uint12(offset));
412 // Set bit indicating whether the offset should be added.
413 instr = (instr & ~B23) | (positive ? B23 : 0);
414 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000415 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000416}
417
418
419bool Assembler::IsAddRegisterImmediate(Instr instr) {
420 return (instr & (B27 | B26 | B25 | B24 | B23 | B22 | B21)) == (B25 | B23);
421}
422
423
424Instr Assembler::SetAddRegisterImmediateOffset(Instr instr, int offset) {
425 ASSERT(IsAddRegisterImmediate(instr));
426 ASSERT(offset >= 0);
427 ASSERT(is_uint12(offset));
428 // Set the offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000429 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000430}
431
432
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000433Register Assembler::GetRd(Instr instr) {
434 Register reg;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000435 reg.code_ = Instruction::RdValue(instr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000436 return reg;
437}
438
439
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000440Register Assembler::GetRn(Instr instr) {
441 Register reg;
442 reg.code_ = Instruction::RnValue(instr);
443 return reg;
444}
445
446
447Register Assembler::GetRm(Instr instr) {
448 Register reg;
449 reg.code_ = Instruction::RmValue(instr);
450 return reg;
451}
452
453
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000454bool Assembler::IsPush(Instr instr) {
455 return ((instr & ~kRdMask) == kPushRegPattern);
456}
457
458
459bool Assembler::IsPop(Instr instr) {
460 return ((instr & ~kRdMask) == kPopRegPattern);
461}
462
463
464bool Assembler::IsStrRegFpOffset(Instr instr) {
465 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
466}
467
468
469bool Assembler::IsLdrRegFpOffset(Instr instr) {
470 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
471}
472
473
474bool Assembler::IsStrRegFpNegOffset(Instr instr) {
475 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
476}
477
478
479bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
480 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
481}
482
483
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000484bool Assembler::IsLdrPcImmediateOffset(Instr instr) {
485 // Check the instruction is indeed a
486 // ldr<cond> <Rd>, [pc +/- offset_12].
ager@chromium.org378b34e2011-01-28 08:04:38 +0000487 return (instr & (kLdrPCMask & ~kCondMask)) == 0x051f0000;
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000488}
489
490
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000491bool Assembler::IsTstImmediate(Instr instr) {
492 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
493 (I | TST | S);
494}
495
496
497bool Assembler::IsCmpRegister(Instr instr) {
498 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask | B4)) ==
499 (CMP | S);
500}
501
502
503bool Assembler::IsCmpImmediate(Instr instr) {
504 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
505 (I | CMP | S);
506}
507
508
509Register Assembler::GetCmpImmediateRegister(Instr instr) {
510 ASSERT(IsCmpImmediate(instr));
511 return GetRn(instr);
512}
513
514
515int Assembler::GetCmpImmediateRawImmediate(Instr instr) {
516 ASSERT(IsCmpImmediate(instr));
517 return instr & kOff12Mask;
518}
519
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520// Labels refer to positions in the (to be) generated code.
521// There are bound, linked, and unused labels.
522//
523// Bound labels refer to known positions in the already
524// generated code. pos() is the position the label refers to.
525//
526// Linked labels refer to unknown positions in the code
527// to be generated; pos() is the position of the last
528// instruction using the label.
529
530
531// The link chain is terminated by a negative code position (must be aligned)
532const int kEndOfChain = -4;
533
534
535int Assembler::target_at(int pos) {
536 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000537 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000538 // Emitted label constant, not part of a branch.
539 return instr - (Code::kHeaderSize - kHeapObjectTag);
540 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000542 int imm26 = ((instr & kImm24Mask) << 8) >> 6;
543 if ((Instruction::ConditionField(instr) == kSpecialCondition) &&
544 ((instr & B24) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 // blx uses bit 24 to encode bit 2 of imm26
546 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000547 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000548 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549}
550
551
552void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000554 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000555 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
556 // Emitted label constant, not part of a branch.
557 // Make label relative to Code* of generated Code object.
558 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
559 return;
560 }
561 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000563 if (Instruction::ConditionField(instr) == kSpecialCondition) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 // blx uses bit 24 to encode bit 2 of imm26
565 ASSERT((imm26 & 1) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000566 instr = (instr & ~(B24 | kImm24Mask)) | ((imm26 & 2) >> 1)*B24;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 } else {
568 ASSERT((imm26 & 3) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000569 instr &= ~kImm24Mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 }
571 int imm24 = imm26 >> 2;
572 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000573 instr_at_put(pos, instr | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574}
575
576
577void Assembler::print(Label* L) {
578 if (L->is_unused()) {
579 PrintF("unused label\n");
580 } else if (L->is_bound()) {
581 PrintF("bound label to %d\n", L->pos());
582 } else if (L->is_linked()) {
583 Label l = *L;
584 PrintF("unbound label");
585 while (l.is_linked()) {
586 PrintF("@ %d ", l.pos());
587 Instr instr = instr_at(l.pos());
ager@chromium.org378b34e2011-01-28 08:04:38 +0000588 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000589 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000591 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
ager@chromium.org378b34e2011-01-28 08:04:38 +0000592 Condition cond = Instruction::ConditionField(instr);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000593 const char* b;
594 const char* c;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000595 if (cond == kSpecialCondition) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000596 b = "blx";
597 c = "";
598 } else {
599 if ((instr & B24) != 0)
600 b = "bl";
601 else
602 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000604 switch (cond) {
605 case eq: c = "eq"; break;
606 case ne: c = "ne"; break;
607 case hs: c = "hs"; break;
608 case lo: c = "lo"; break;
609 case mi: c = "mi"; break;
610 case pl: c = "pl"; break;
611 case vs: c = "vs"; break;
612 case vc: c = "vc"; break;
613 case hi: c = "hi"; break;
614 case ls: c = "ls"; break;
615 case ge: c = "ge"; break;
616 case lt: c = "lt"; break;
617 case gt: c = "gt"; break;
618 case le: c = "le"; break;
619 case al: c = ""; break;
620 default:
621 c = "";
622 UNREACHABLE();
623 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000625 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 next(&l);
628 }
629 } else {
630 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
631 }
632}
633
634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635void Assembler::bind_to(Label* L, int pos) {
636 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
637 while (L->is_linked()) {
638 int fixup_pos = L->pos();
639 next(L); // call next before overwriting link with target at fixup_pos
640 target_at_put(fixup_pos, pos);
641 }
642 L->bind_to(pos);
643
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000644 // Keep track of the last bound label so we don't eliminate any instructions
645 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 if (pos > last_bound_pos_)
647 last_bound_pos_ = pos;
648}
649
650
651void Assembler::link_to(Label* L, Label* appendix) {
652 if (appendix->is_linked()) {
653 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000654 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 int fixup_pos;
656 int link = L->pos();
657 do {
658 fixup_pos = link;
659 link = target_at(fixup_pos);
660 } while (link > 0);
661 ASSERT(link == kEndOfChain);
662 target_at_put(fixup_pos, appendix->pos());
663 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000664 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 *L = *appendix;
666 }
667 }
668 appendix->Unuse(); // appendix should not be used anymore
669}
670
671
672void Assembler::bind(Label* L) {
673 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 bind_to(L, pc_offset());
675}
676
677
678void Assembler::next(Label* L) {
679 ASSERT(L->is_linked());
680 int link = target_at(L->pos());
681 if (link > 0) {
682 L->link_to(link);
683 } else {
684 ASSERT(link == kEndOfChain);
685 L->Unuse();
686 }
687}
688
689
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000690static Instr EncodeMovwImmediate(uint32_t immediate) {
691 ASSERT(immediate < 0x10000);
692 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
693}
694
695
ager@chromium.org5c838252010-02-19 08:53:10 +0000696// Low-level code emission routines depending on the addressing mode.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000697// If this returns true then you have to use the rotate_imm and immed_8
698// that it returns, because it may have already changed the instruction
699// to match them!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700static bool fits_shifter(uint32_t imm32,
701 uint32_t* rotate_imm,
702 uint32_t* immed_8,
703 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000704 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 for (int rot = 0; rot < 16; rot++) {
706 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
707 if ((imm8 <= 0xff)) {
708 *rotate_imm = rot;
709 *immed_8 = imm8;
710 return true;
711 }
712 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000713 // If the opcode is one with a complementary version and the complementary
714 // immediate fits, change the opcode.
715 if (instr != NULL) {
716 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
717 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
718 *instr ^= kMovMvnFlip;
719 return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000720 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000721 if (CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000722 if (imm32 < 0x10000) {
723 *instr ^= kMovwLeaveCCFlip;
724 *instr |= EncodeMovwImmediate(imm32);
725 *rotate_imm = *immed_8 = 0; // Not used for movw.
726 return true;
727 }
728 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000729 }
730 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
731 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
732 *instr ^= kCmpCmnFlip;
733 return true;
734 }
735 } else {
736 Instr alu_insn = (*instr & kALUMask);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000737 if (alu_insn == ADD ||
738 alu_insn == SUB) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000739 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
740 *instr ^= kAddSubFlip;
741 return true;
742 }
ager@chromium.org378b34e2011-01-28 08:04:38 +0000743 } else if (alu_insn == AND ||
744 alu_insn == BIC) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000745 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
746 *instr ^= kAndBicFlip;
747 return true;
748 }
749 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 }
751 }
752 return false;
753}
754
755
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000756// We have to use the temporary register for things that can be relocated even
757// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
758// space. There is no guarantee that the relocated location can be similarly
759// encoded.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000760bool Operand::must_use_constant_pool() const {
761 if (rmode_ == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000762#ifdef DEBUG
763 if (!Serializer::enabled()) {
764 Serializer::TooLateToEnableNow();
765 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000766#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000767 return Serializer::enabled();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000768 } else if (rmode_ == RelocInfo::NONE) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000769 return false;
770 }
771 return true;
772}
773
774
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000775bool Operand::is_single_instruction(Instr instr) const {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000776 if (rm_.is_valid()) return true;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000777 uint32_t dummy1, dummy2;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000778 if (must_use_constant_pool() ||
779 !fits_shifter(imm32_, &dummy1, &dummy2, &instr)) {
780 // The immediate operand cannot be encoded as a shifter operand, or use of
781 // constant pool is required. For a mov instruction not setting the
782 // condition code additional instruction conventions can be used.
783 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000784 if (must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000785 !CpuFeatures::IsSupported(ARMv7)) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000786 // mov instruction will be an ldr from constant pool (one instruction).
787 return true;
788 } else {
789 // mov instruction will be a mov or movw followed by movt (two
790 // instructions).
791 return false;
792 }
793 } else {
794 // If this is not a mov or mvn instruction there will always an additional
795 // instructions - either mov or ldr. The mov might actually be two
796 // instructions mov or movw followed by movt so including the actual
797 // instruction two or three instructions will be generated.
798 return false;
799 }
800 } else {
801 // No use of constant pool and the immediate operand can be encoded as a
802 // shifter operand.
803 return true;
804 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000805}
806
807
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808void Assembler::addrmod1(Instr instr,
809 Register rn,
810 Register rd,
811 const Operand& x) {
812 CheckBuffer();
ager@chromium.org378b34e2011-01-28 08:04:38 +0000813 ASSERT((instr & ~(kCondMask | kOpCodeMask | S)) == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000815 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 uint32_t rotate_imm;
817 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000818 if (x.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
820 // The immediate operand cannot be encoded as a shifter operand, so load
821 // it first to register ip and change the original instruction to use ip.
822 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000823 // condition code), then replace it with a 'ldr rd, [pc]'.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000824 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
ager@chromium.org378b34e2011-01-28 08:04:38 +0000825 Condition cond = Instruction::ConditionField(instr);
826 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000827 if (x.must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000828 !CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000829 RecordRelocInfo(x.rmode_, x.imm32_);
830 ldr(rd, MemOperand(pc, 0), cond);
831 } else {
832 // Will probably use movw, will certainly not use constant pool.
833 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
834 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
835 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000837 // If this is not a mov or mvn instruction we may still be able to avoid
838 // a constant pool entry by using mvn or movw.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000839 if (!x.must_use_constant_pool() &&
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000840 (instr & kMovMvnMask) != kMovMvnPattern) {
841 mov(ip, x, LeaveCC, cond);
842 } else {
843 RecordRelocInfo(x.rmode_, x.imm32_);
844 ldr(ip, MemOperand(pc, 0), cond);
845 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 addrmod1(instr, rn, rd, Operand(ip));
847 }
848 return;
849 }
850 instr |= I | rotate_imm*B8 | immed_8;
851 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000852 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
854 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000855 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
857 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
858 }
859 emit(instr | rn.code()*B16 | rd.code()*B12);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000860 if (rn.is(pc) || x.rm_.is(pc)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000861 // Block constant pool emission for one instruction after reading pc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 BlockConstPoolBefore(pc_offset() + kInstrSize);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000863 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864}
865
866
867void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000868 ASSERT((instr & ~(kCondMask | B | L)) == B26);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 int am = x.am_;
870 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000871 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 int offset_12 = x.offset_;
873 if (offset_12 < 0) {
874 offset_12 = -offset_12;
875 am ^= U;
876 }
877 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000878 // Immediate offset cannot be encoded, load it first to register ip
879 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000881 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
883 return;
884 }
885 ASSERT(offset_12 >= 0); // no masking needed
886 instr |= offset_12;
887 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000888 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000890 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 ASSERT(!x.rm_.is(pc));
892 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
893 }
894 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
895 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
896}
897
898
899void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000900 ASSERT((instr & ~(kCondMask | L | S6 | H)) == (B4 | B7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 ASSERT(x.rn_.is_valid());
902 int am = x.am_;
903 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000904 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 int offset_8 = x.offset_;
906 if (offset_8 < 0) {
907 offset_8 = -offset_8;
908 am ^= U;
909 }
910 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000911 // Immediate offset cannot be encoded, load it first to register ip
912 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000914 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
916 return;
917 }
918 ASSERT(offset_8 >= 0); // no masking needed
919 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
920 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000921 // Scaled register offset not supported, load index first
922 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
924 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
ager@chromium.org378b34e2011-01-28 08:04:38 +0000925 Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
927 return;
928 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000929 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
931 instr |= x.rm_.code();
932 }
933 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
934 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
935}
936
937
938void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000939 ASSERT((instr & ~(kCondMask | P | U | W | L)) == B27);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 ASSERT(rl != 0);
941 ASSERT(!rn.is(pc));
942 emit(instr | rn.code()*B16 | rl);
943}
944
945
946void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000947 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000948 ASSERT_EQ((B27 | B26),
ager@chromium.org378b34e2011-01-28 08:04:38 +0000949 (instr & ~(kCondMask | kCoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
951 int am = x.am_;
952 int offset_8 = x.offset_;
953 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
954 offset_8 >>= 2;
955 if (offset_8 < 0) {
956 offset_8 = -offset_8;
957 am ^= U;
958 }
959 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
960 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
961
ager@chromium.org5c838252010-02-19 08:53:10 +0000962 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963 if ((am & P) == 0)
964 am |= W;
965
966 ASSERT(offset_8 >= 0); // no masking needed
967 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
968}
969
970
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000971int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 int target_pos;
973 if (L->is_bound()) {
974 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000976 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000978 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000980 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 L->link_to(pc_offset());
982 }
983
984 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +0000985 // be emitted at the pc offset recorded by the label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 BlockConstPoolBefore(pc_offset() + kInstrSize);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000987 return target_pos - (pc_offset() + kPcLoadDelta);
988}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000990
991void Assembler::label_at_put(Label* L, int at_offset) {
992 int target_pos;
993 if (L->is_bound()) {
994 target_pos = L->pos();
995 } else {
996 if (L->is_linked()) {
997 target_pos = L->pos(); // L's link
998 } else {
999 target_pos = kEndOfChain;
1000 }
1001 L->link_to(at_offset);
1002 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
1003 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004}
1005
1006
ager@chromium.org5c838252010-02-19 08:53:10 +00001007// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008void Assembler::b(int branch_offset, Condition cond) {
1009 ASSERT((branch_offset & 3) == 0);
1010 int imm24 = branch_offset >> 2;
1011 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001012 emit(cond | B27 | B25 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001014 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001015 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001017 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018}
1019
1020
1021void Assembler::bl(int branch_offset, Condition cond) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001022 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 ASSERT((branch_offset & 3) == 0);
1024 int imm24 = branch_offset >> 2;
1025 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001026 emit(cond | B27 | B25 | B24 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027}
1028
1029
1030void Assembler::blx(int branch_offset) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001031 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 ASSERT((branch_offset & 1) == 0);
1033 int h = ((branch_offset & 2) >> 1)*B24;
1034 int imm24 = branch_offset >> 2;
1035 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001036 emit(kSpecialCondition | B27 | B25 | h | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037}
1038
1039
1040void Assembler::blx(Register target, Condition cond) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001041 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 ASSERT(!target.is(pc));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001043 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BLX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044}
1045
1046
1047void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001048 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
ager@chromium.org378b34e2011-01-28 08:04:38 +00001050 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051}
1052
1053
ager@chromium.org5c838252010-02-19 08:53:10 +00001054// Data-processing instructions.
1055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056void Assembler::and_(Register dst, Register src1, const Operand& src2,
1057 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001058 addrmod1(cond | AND | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059}
1060
1061
1062void Assembler::eor(Register dst, Register src1, const Operand& src2,
1063 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001064 addrmod1(cond | EOR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065}
1066
1067
1068void Assembler::sub(Register dst, Register src1, const Operand& src2,
1069 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001070 addrmod1(cond | SUB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071}
1072
1073
1074void Assembler::rsb(Register dst, Register src1, const Operand& src2,
1075 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001076 addrmod1(cond | RSB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077}
1078
1079
1080void Assembler::add(Register dst, Register src1, const Operand& src2,
1081 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001082 addrmod1(cond | ADD | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +00001083
1084 // Eliminate pattern: push(r), pop()
1085 // str(src, MemOperand(sp, 4, NegPreIndex), al);
1086 // add(sp, sp, Operand(kPointerSize));
1087 // Both instructions can be eliminated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001088 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001089 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001090 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
ager@chromium.org378b34e2011-01-28 08:04:38 +00001091 (instr_at(pc_ - 2 * kInstrSize) & ~kRdMask) == kPushRegPattern) {
mads.s.ager31e71382008-08-13 09:32:07 +00001092 pc_ -= 2 * kInstrSize;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001093 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001094 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
1095 }
1096 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097}
1098
1099
1100void Assembler::adc(Register dst, Register src1, const Operand& src2,
1101 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001102 addrmod1(cond | ADC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103}
1104
1105
1106void Assembler::sbc(Register dst, Register src1, const Operand& src2,
1107 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001108 addrmod1(cond | SBC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109}
1110
1111
1112void Assembler::rsc(Register dst, Register src1, const Operand& src2,
1113 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001114 addrmod1(cond | RSC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115}
1116
1117
1118void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001119 addrmod1(cond | TST | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120}
1121
1122
1123void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001124 addrmod1(cond | TEQ | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125}
1126
1127
1128void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001129 addrmod1(cond | CMP | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130}
1131
1132
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001133void Assembler::cmp_raw_immediate(
1134 Register src, int raw_immediate, Condition cond) {
1135 ASSERT(is_uint12(raw_immediate));
1136 emit(cond | I | CMP | S | src.code() << 16 | raw_immediate);
1137}
1138
1139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001141 addrmod1(cond | CMN | S, src1, r0, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142}
1143
1144
1145void Assembler::orr(Register dst, Register src1, const Operand& src2,
1146 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001147 addrmod1(cond | ORR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148}
1149
1150
1151void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001152 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001153 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001154 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001155 // Don't allow nop instructions in the form mov rn, rn to be generated using
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001156 // the mov instruction. They must be generated using nop(int/NopMarkerTypes)
1157 // or MarkCode(int/NopMarkerTypes) pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001158 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001159 addrmod1(cond | MOV | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160}
1161
1162
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001163void Assembler::movw(Register reg, uint32_t immediate, Condition cond) {
1164 ASSERT(immediate < 0x10000);
1165 mov(reg, Operand(immediate), LeaveCC, cond);
1166}
1167
1168
1169void Assembler::movt(Register reg, uint32_t immediate, Condition cond) {
1170 emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate));
1171}
1172
1173
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174void Assembler::bic(Register dst, Register src1, const Operand& src2,
1175 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001176 addrmod1(cond | BIC | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177}
1178
1179
1180void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001181 addrmod1(cond | MVN | s, r0, dst, src);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182}
1183
1184
ager@chromium.org5c838252010-02-19 08:53:10 +00001185// Multiply instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1187 SBit s, Condition cond) {
1188 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1190 src2.code()*B8 | B7 | B4 | src1.code());
1191}
1192
1193
1194void Assembler::mul(Register dst, Register src1, Register src2,
1195 SBit s, Condition cond) {
1196 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001197 // dst goes in bits 16-19 for this instruction!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1199}
1200
1201
1202void Assembler::smlal(Register dstL,
1203 Register dstH,
1204 Register src1,
1205 Register src2,
1206 SBit s,
1207 Condition cond) {
1208 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001209 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1211 src2.code()*B8 | B7 | B4 | src1.code());
1212}
1213
1214
1215void Assembler::smull(Register dstL,
1216 Register dstH,
1217 Register src1,
1218 Register src2,
1219 SBit s,
1220 Condition cond) {
1221 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001222 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1224 src2.code()*B8 | B7 | B4 | src1.code());
1225}
1226
1227
1228void Assembler::umlal(Register dstL,
1229 Register dstH,
1230 Register src1,
1231 Register src2,
1232 SBit s,
1233 Condition cond) {
1234 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001235 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1237 src2.code()*B8 | B7 | B4 | src1.code());
1238}
1239
1240
1241void Assembler::umull(Register dstL,
1242 Register dstH,
1243 Register src1,
1244 Register src2,
1245 SBit s,
1246 Condition cond) {
1247 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001248 ASSERT(!dstL.is(dstH));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001249 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 src2.code()*B8 | B7 | B4 | src1.code());
1251}
1252
1253
ager@chromium.org5c838252010-02-19 08:53:10 +00001254// Miscellaneous arithmetic instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255void Assembler::clz(Register dst, Register src, Condition cond) {
1256 // v5 and above.
1257 ASSERT(!dst.is(pc) && !src.is(pc));
1258 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
ager@chromium.org378b34e2011-01-28 08:04:38 +00001259 15*B8 | CLZ | src.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260}
1261
1262
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001263// Saturating instructions.
1264
1265// Unsigned saturate.
1266void Assembler::usat(Register dst,
1267 int satpos,
1268 const Operand& src,
1269 Condition cond) {
1270 // v6 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001271 ASSERT(CpuFeatures::IsSupported(ARMv7));
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001272 ASSERT(!dst.is(pc) && !src.rm_.is(pc));
1273 ASSERT((satpos >= 0) && (satpos <= 31));
1274 ASSERT((src.shift_op_ == ASR) || (src.shift_op_ == LSL));
1275 ASSERT(src.rs_.is(no_reg));
1276
1277 int sh = 0;
1278 if (src.shift_op_ == ASR) {
1279 sh = 1;
1280 }
1281
1282 emit(cond | 0x6*B24 | 0xe*B20 | satpos*B16 | dst.code()*B12 |
1283 src.shift_imm_*B7 | sh*B6 | 0x1*B4 | src.rm_.code());
1284}
1285
1286
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001287// Bitfield manipulation instructions.
1288
1289// Unsigned bit field extract.
1290// Extracts #width adjacent bits from position #lsb in a register, and
1291// writes them to the low bits of a destination register.
1292// ubfx dst, src, #lsb, #width
1293void Assembler::ubfx(Register dst,
1294 Register src,
1295 int lsb,
1296 int width,
1297 Condition cond) {
1298 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001299 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001300 ASSERT(!dst.is(pc) && !src.is(pc));
1301 ASSERT((lsb >= 0) && (lsb <= 31));
1302 ASSERT((width >= 1) && (width <= (32 - lsb)));
1303 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1304 lsb*B7 | B6 | B4 | src.code());
1305}
1306
1307
1308// Signed bit field extract.
1309// Extracts #width adjacent bits from position #lsb in a register, and
1310// writes them to the low bits of a destination register. The extracted
1311// value is sign extended to fill the destination register.
1312// sbfx dst, src, #lsb, #width
1313void Assembler::sbfx(Register dst,
1314 Register src,
1315 int lsb,
1316 int width,
1317 Condition cond) {
1318 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001319 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001320 ASSERT(!dst.is(pc) && !src.is(pc));
1321 ASSERT((lsb >= 0) && (lsb <= 31));
1322 ASSERT((width >= 1) && (width <= (32 - lsb)));
1323 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1324 lsb*B7 | B6 | B4 | src.code());
1325}
1326
1327
1328// Bit field clear.
1329// Sets #width adjacent bits at position #lsb in the destination register
1330// to zero, preserving the value of the other bits.
1331// bfc dst, #lsb, #width
1332void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1333 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001334 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001335 ASSERT(!dst.is(pc));
1336 ASSERT((lsb >= 0) && (lsb <= 31));
1337 ASSERT((width >= 1) && (width <= (32 - lsb)));
1338 int msb = lsb + width - 1;
1339 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1340}
1341
1342
1343// Bit field insert.
1344// Inserts #width adjacent bits from the low bits of the source register
1345// into position #lsb of the destination register.
1346// bfi dst, src, #lsb, #width
1347void Assembler::bfi(Register dst,
1348 Register src,
1349 int lsb,
1350 int width,
1351 Condition cond) {
1352 // v7 and above.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001353 ASSERT(CpuFeatures::IsSupported(ARMv7));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001354 ASSERT(!dst.is(pc) && !src.is(pc));
1355 ASSERT((lsb >= 0) && (lsb <= 31));
1356 ASSERT((width >= 1) && (width <= (32 - lsb)));
1357 int msb = lsb + width - 1;
1358 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1359 src.code());
1360}
1361
1362
ager@chromium.org5c838252010-02-19 08:53:10 +00001363// Status register access instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1365 ASSERT(!dst.is(pc));
1366 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1367}
1368
1369
1370void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1371 Condition cond) {
1372 ASSERT(fields >= B16 && fields < B20); // at least one field set
1373 Instr instr;
1374 if (!src.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001375 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 uint32_t rotate_imm;
1377 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001378 if (src.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001380 // Immediate operand cannot be encoded, load it first to register ip.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 RecordRelocInfo(src.rmode_, src.imm32_);
1382 ldr(ip, MemOperand(pc, 0), cond);
1383 msr(fields, Operand(ip), cond);
1384 return;
1385 }
1386 instr = I | rotate_imm*B8 | immed_8;
1387 } else {
1388 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1389 instr = src.rm_.code();
1390 }
1391 emit(cond | instr | B24 | B21 | fields | 15*B12);
1392}
1393
1394
ager@chromium.org5c838252010-02-19 08:53:10 +00001395// Load/Store instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001397 if (dst.is(pc)) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001398 positions_recorder()->WriteRecordedPositions();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001399 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +00001401
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001402 // Eliminate pattern: push(ry), pop(rx)
1403 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1404 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1405 // Both instructions can be eliminated if ry = rx.
1406 // If ry != rx, a register copy from ry to rx is inserted
1407 // after eliminating the push and the pop instructions.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001408 if (can_peephole_optimize(2)) {
1409 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1410 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001411
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001412 if (IsPush(push_instr) && IsPop(pop_instr)) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001413 if (Instruction::RdValue(pop_instr) != Instruction::RdValue(push_instr)) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00001414 // For consecutive push and pop on different registers,
1415 // we delete both the push & pop and insert a register move.
1416 // push ry, pop rx --> mov rx, ry
1417 Register reg_pushed, reg_popped;
1418 reg_pushed = GetRd(push_instr);
1419 reg_popped = GetRd(pop_instr);
1420 pc_ -= 2 * kInstrSize;
1421 // Insert a mov instruction, which is better than a pair of push & pop
1422 mov(reg_popped, reg_pushed);
1423 if (FLAG_print_peephole_optimization) {
1424 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1425 pc_offset());
1426 }
1427 } else {
1428 // For consecutive push and pop on the same register,
1429 // both the push and the pop can be deleted.
1430 pc_ -= 2 * kInstrSize;
1431 if (FLAG_print_peephole_optimization) {
1432 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1433 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001434 }
1435 }
1436 }
1437
1438 if (can_peephole_optimize(2)) {
1439 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1440 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1441
1442 if ((IsStrRegFpOffset(str_instr) &&
1443 IsLdrRegFpOffset(ldr_instr)) ||
1444 (IsStrRegFpNegOffset(str_instr) &&
1445 IsLdrRegFpNegOffset(ldr_instr))) {
1446 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1447 (str_instr & kLdrStrInstrArgumentMask)) {
1448 // Pattern: Ldr/str same fp+offset, same register.
1449 //
1450 // The following:
1451 // str rx, [fp, #-12]
1452 // ldr rx, [fp, #-12]
1453 //
1454 // Becomes:
1455 // str rx, [fp, #-12]
1456
1457 pc_ -= 1 * kInstrSize;
1458 if (FLAG_print_peephole_optimization) {
1459 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1460 }
1461 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1462 (str_instr & kLdrStrOffsetMask)) {
1463 // Pattern: Ldr/str same fp+offset, different register.
1464 //
1465 // The following:
1466 // str rx, [fp, #-12]
1467 // ldr ry, [fp, #-12]
1468 //
1469 // Becomes:
1470 // str rx, [fp, #-12]
1471 // mov ry, rx
1472
1473 Register reg_stored, reg_loaded;
1474 reg_stored = GetRd(str_instr);
1475 reg_loaded = GetRd(ldr_instr);
1476 pc_ -= 1 * kInstrSize;
1477 // Insert a mov instruction, which is better than ldr.
1478 mov(reg_loaded, reg_stored);
1479 if (FLAG_print_peephole_optimization) {
1480 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1481 }
1482 }
1483 }
1484 }
1485
1486 if (can_peephole_optimize(3)) {
1487 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1488 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1489 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1490 if (IsPush(mem_write_instr) &&
1491 IsPop(mem_read_instr)) {
1492 if ((IsLdrRegFpOffset(ldr_instr) ||
1493 IsLdrRegFpNegOffset(ldr_instr))) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001494 if (Instruction::RdValue(mem_write_instr) ==
1495 Instruction::RdValue(mem_read_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001496 // Pattern: push & pop from/to same register,
1497 // with a fp+offset ldr in between
1498 //
1499 // The following:
1500 // str rx, [sp, #-4]!
1501 // ldr rz, [fp, #-24]
1502 // ldr rx, [sp], #+4
1503 //
1504 // Becomes:
1505 // if(rx == rz)
1506 // delete all
1507 // else
1508 // ldr rz, [fp, #-24]
1509
ager@chromium.org378b34e2011-01-28 08:04:38 +00001510 if (Instruction::RdValue(mem_write_instr) ==
1511 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001512 pc_ -= 3 * kInstrSize;
1513 } else {
1514 pc_ -= 3 * kInstrSize;
1515 // Reinsert back the ldr rz.
1516 emit(ldr_instr);
1517 }
1518 if (FLAG_print_peephole_optimization) {
1519 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1520 }
1521 } else {
1522 // Pattern: push & pop from/to different registers
1523 // with a fp+offset ldr in between
1524 //
1525 // The following:
1526 // str rx, [sp, #-4]!
1527 // ldr rz, [fp, #-24]
1528 // ldr ry, [sp], #+4
1529 //
1530 // Becomes:
1531 // if(ry == rz)
1532 // mov ry, rx;
1533 // else if(rx != rz)
1534 // ldr rz, [fp, #-24]
1535 // mov ry, rx
1536 // else if((ry != rz) || (rx == rz)) becomes:
1537 // mov ry, rx
1538 // ldr rz, [fp, #-24]
1539
1540 Register reg_pushed, reg_popped;
ager@chromium.org378b34e2011-01-28 08:04:38 +00001541 if (Instruction::RdValue(mem_read_instr) ==
1542 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001543 reg_pushed = GetRd(mem_write_instr);
1544 reg_popped = GetRd(mem_read_instr);
1545 pc_ -= 3 * kInstrSize;
1546 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001547 } else if (Instruction::RdValue(mem_write_instr) !=
1548 Instruction::RdValue(ldr_instr)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001549 reg_pushed = GetRd(mem_write_instr);
1550 reg_popped = GetRd(mem_read_instr);
1551 pc_ -= 3 * kInstrSize;
1552 emit(ldr_instr);
1553 mov(reg_popped, reg_pushed);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001554 } else if ((Instruction::RdValue(mem_read_instr) !=
1555 Instruction::RdValue(ldr_instr)) ||
1556 (Instruction::RdValue(mem_write_instr) ==
1557 Instruction::RdValue(ldr_instr))) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001558 reg_pushed = GetRd(mem_write_instr);
1559 reg_popped = GetRd(mem_read_instr);
1560 pc_ -= 3 * kInstrSize;
1561 mov(reg_popped, reg_pushed);
1562 emit(ldr_instr);
1563 }
1564 if (FLAG_print_peephole_optimization) {
1565 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1566 }
1567 }
1568 }
mads.s.ager31e71382008-08-13 09:32:07 +00001569 }
1570 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571}
1572
1573
1574void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1575 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +00001576
1577 // Eliminate pattern: pop(), push(r)
1578 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1579 // -> str r, [sp, 0], al
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001580 if (can_peephole_optimize(2) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001581 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001582 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1583 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1584 pc_ -= 2 * kInstrSize;
1585 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001586 if (FLAG_print_peephole_optimization) {
mads.s.ager31e71382008-08-13 09:32:07 +00001587 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1588 }
1589 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590}
1591
1592
1593void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1594 addrmod2(cond | B26 | B | L, dst, src);
1595}
1596
1597
1598void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1599 addrmod2(cond | B26 | B, src, dst);
1600}
1601
1602
1603void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1604 addrmod3(cond | L | B7 | H | B4, dst, src);
1605}
1606
1607
1608void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1609 addrmod3(cond | B7 | H | B4, src, dst);
1610}
1611
1612
1613void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1614 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1615}
1616
1617
1618void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1619 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1620}
1621
1622
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001623void Assembler::ldrd(Register dst1, Register dst2,
1624 const MemOperand& src, Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001625 ASSERT(CpuFeatures::IsEnabled(ARMv7));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001626 ASSERT(src.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001627 ASSERT(!dst1.is(lr)); // r14.
1628 ASSERT_EQ(0, dst1.code() % 2);
1629 ASSERT_EQ(dst1.code() + 1, dst2.code());
1630 addrmod3(cond | B7 | B6 | B4, dst1, src);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001631}
1632
1633
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001634void Assembler::strd(Register src1, Register src2,
1635 const MemOperand& dst, Condition cond) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001636 ASSERT(dst.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001637 ASSERT(!src1.is(lr)); // r14.
1638 ASSERT_EQ(0, src1.code() % 2);
1639 ASSERT_EQ(src1.code() + 1, src2.code());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001640 ASSERT(CpuFeatures::IsEnabled(ARMv7));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001641 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001642}
1643
ager@chromium.org5c838252010-02-19 08:53:10 +00001644// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645void Assembler::ldm(BlockAddrMode am,
1646 Register base,
1647 RegList dst,
1648 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001649 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1651
1652 addrmod4(cond | B27 | am | L, base, dst);
1653
ager@chromium.org5c838252010-02-19 08:53:10 +00001654 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 if (cond == al && (dst & pc.bit()) != 0) {
1656 // There is a slight chance that the ldm instruction was actually a call,
1657 // in which case it would be wrong to return into the constant pool; we
1658 // recognize this case by checking if the emission of the pool was blocked
1659 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1660 // the case, we emit a jump over the pool.
1661 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1662 }
1663}
1664
1665
1666void Assembler::stm(BlockAddrMode am,
1667 Register base,
1668 RegList src,
1669 Condition cond) {
1670 addrmod4(cond | B27 | am, base, src);
1671}
1672
1673
ager@chromium.org5c838252010-02-19 08:53:10 +00001674// Exception-generating instructions and debugging support.
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001675// Stops with a non-negative code less than kNumOfWatchedStops support
1676// enabling/disabling and a counter feature. See simulator-arm.h .
1677void Assembler::stop(const char* msg, Condition cond, int32_t code) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001678#ifndef __arm__
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001679 ASSERT(code >= kDefaultStopCode);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001680 // The Simulator will handle the stop instruction and get the message address.
1681 // It expects to find the address just after the svc instruction.
1682 BlockConstPoolFor(2);
1683 if (code >= 0) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001684 svc(kStopCode + code, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001685 } else {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001686 svc(kStopCode + kMaxStopCode, cond);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001687 }
1688 emit(reinterpret_cast<Instr>(msg));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001689#else // def __arm__
1690#ifdef CAN_USE_ARMV5_INSTRUCTIONS
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001691 if (cond != al) {
1692 Label skip;
1693 b(&skip, NegateCondition(cond));
1694 bkpt(0);
1695 bind(&skip);
1696 } else {
1697 bkpt(0);
1698 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001699#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001700 svc(0x9f0001, cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001701#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1702#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703}
1704
1705
1706void Assembler::bkpt(uint32_t imm16) { // v5 and above
1707 ASSERT(is_uint16(imm16));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001708 emit(al | B24 | B21 | (imm16 >> 4)*B8 | BKPT | (imm16 & 0xf));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709}
1710
1711
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001712void Assembler::svc(uint32_t imm24, Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 ASSERT(is_uint24(imm24));
1714 emit(cond | 15*B24 | imm24);
1715}
1716
1717
ager@chromium.org5c838252010-02-19 08:53:10 +00001718// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719void Assembler::cdp(Coprocessor coproc,
1720 int opcode_1,
1721 CRegister crd,
1722 CRegister crn,
1723 CRegister crm,
1724 int opcode_2,
1725 Condition cond) {
1726 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1727 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1728 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1729}
1730
1731
1732void Assembler::cdp2(Coprocessor coproc,
1733 int opcode_1,
1734 CRegister crd,
1735 CRegister crn,
1736 CRegister crm,
1737 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001738 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739}
1740
1741
1742void Assembler::mcr(Coprocessor coproc,
1743 int opcode_1,
1744 Register rd,
1745 CRegister crn,
1746 CRegister crm,
1747 int opcode_2,
1748 Condition cond) {
1749 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1750 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1751 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1752}
1753
1754
1755void Assembler::mcr2(Coprocessor coproc,
1756 int opcode_1,
1757 Register rd,
1758 CRegister crn,
1759 CRegister crm,
1760 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001761 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762}
1763
1764
1765void Assembler::mrc(Coprocessor coproc,
1766 int opcode_1,
1767 Register rd,
1768 CRegister crn,
1769 CRegister crm,
1770 int opcode_2,
1771 Condition cond) {
1772 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1773 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1774 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1775}
1776
1777
1778void Assembler::mrc2(Coprocessor coproc,
1779 int opcode_1,
1780 Register rd,
1781 CRegister crn,
1782 CRegister crm,
1783 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001784 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785}
1786
1787
1788void Assembler::ldc(Coprocessor coproc,
1789 CRegister crd,
1790 const MemOperand& src,
1791 LFlag l,
1792 Condition cond) {
1793 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1794}
1795
1796
1797void Assembler::ldc(Coprocessor coproc,
1798 CRegister crd,
1799 Register rn,
1800 int option,
1801 LFlag l,
1802 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001803 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 ASSERT(is_uint8(option));
1805 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1806 coproc*B8 | (option & 255));
1807}
1808
1809
1810void Assembler::ldc2(Coprocessor coproc,
1811 CRegister crd,
1812 const MemOperand& src,
1813 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001814 ldc(coproc, crd, src, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815}
1816
1817
1818void Assembler::ldc2(Coprocessor coproc,
1819 CRegister crd,
1820 Register rn,
1821 int option,
1822 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001823 ldc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824}
1825
1826
1827void Assembler::stc(Coprocessor coproc,
1828 CRegister crd,
1829 const MemOperand& dst,
1830 LFlag l,
1831 Condition cond) {
1832 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1833}
1834
1835
1836void Assembler::stc(Coprocessor coproc,
1837 CRegister crd,
1838 Register rn,
1839 int option,
1840 LFlag l,
1841 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001842 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 ASSERT(is_uint8(option));
1844 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1845 coproc*B8 | (option & 255));
1846}
1847
1848
1849void Assembler::stc2(Coprocessor
1850 coproc, CRegister crd,
1851 const MemOperand& dst,
1852 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001853 stc(coproc, crd, dst, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854}
1855
1856
1857void Assembler::stc2(Coprocessor coproc,
1858 CRegister crd,
1859 Register rn,
1860 int option,
1861 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001862 stc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863}
1864
1865
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001866// Support for VFP.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001867
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001868void Assembler::vldr(const DwVfpRegister dst,
1869 const Register base,
1870 int offset,
1871 const Condition cond) {
1872 // Ddst = MEM(Rbase + offset).
1873 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001874 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001875 // Vdst(15-12) | 1011(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001876 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001877 int u = 1;
1878 if (offset < 0) {
1879 offset = -offset;
1880 u = 0;
1881 }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001882
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001883 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001884 if ((offset % 4) == 0 && (offset / 4) < 256) {
1885 emit(cond | u*B23 | 0xD1*B20 | base.code()*B16 | dst.code()*B12 |
1886 0xB*B8 | ((offset / 4) & 255));
1887 } else {
1888 // Larger offsets must be handled by computing the correct address
1889 // in the ip register.
1890 ASSERT(!base.is(ip));
1891 if (u == 1) {
1892 add(ip, base, Operand(offset));
1893 } else {
1894 sub(ip, base, Operand(offset));
1895 }
1896 emit(cond | 0xD1*B20 | ip.code()*B16 | dst.code()*B12 | 0xB*B8);
1897 }
1898}
1899
1900
1901void Assembler::vldr(const DwVfpRegister dst,
1902 const MemOperand& operand,
1903 const Condition cond) {
1904 ASSERT(!operand.rm().is_valid());
1905 ASSERT(operand.am_ == Offset);
1906 vldr(dst, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001907}
1908
1909
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001910void Assembler::vldr(const SwVfpRegister dst,
1911 const Register base,
1912 int offset,
1913 const Condition cond) {
1914 // Sdst = MEM(Rbase + offset).
1915 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001916 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001917 // Vdst(15-12) | 1010(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001918 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001919 int u = 1;
1920 if (offset < 0) {
1921 offset = -offset;
1922 u = 0;
1923 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001924 int sd, d;
1925 dst.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001926 ASSERT(offset >= 0);
1927
1928 if ((offset % 4) == 0 && (offset / 4) < 256) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001929 emit(cond | u*B23 | d*B22 | 0xD1*B20 | base.code()*B16 | sd*B12 |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001930 0xA*B8 | ((offset / 4) & 255));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001931 } else {
1932 // Larger offsets must be handled by computing the correct address
1933 // in the ip register.
1934 ASSERT(!base.is(ip));
1935 if (u == 1) {
1936 add(ip, base, Operand(offset));
1937 } else {
1938 sub(ip, base, Operand(offset));
1939 }
1940 emit(cond | d*B22 | 0xD1*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
1941 }
1942}
1943
1944
1945void Assembler::vldr(const SwVfpRegister dst,
1946 const MemOperand& operand,
1947 const Condition cond) {
1948 ASSERT(!operand.rm().is_valid());
1949 ASSERT(operand.am_ == Offset);
1950 vldr(dst, operand.rn(), operand.offset(), cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001951}
1952
1953
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001954void Assembler::vstr(const DwVfpRegister src,
1955 const Register base,
1956 int offset,
1957 const Condition cond) {
1958 // MEM(Rbase + offset) = Dsrc.
1959 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001960 // cond(31-28) | 1101(27-24)| U000(23-20) | | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001961 // Vsrc(15-12) | 1011(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001962 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001963 int u = 1;
1964 if (offset < 0) {
1965 offset = -offset;
1966 u = 0;
1967 }
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001968 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001969 if ((offset % 4) == 0 && (offset / 4) < 256) {
1970 emit(cond | u*B23 | 0xD0*B20 | base.code()*B16 | src.code()*B12 |
1971 0xB*B8 | ((offset / 4) & 255));
1972 } else {
1973 // Larger offsets must be handled by computing the correct address
1974 // in the ip register.
1975 ASSERT(!base.is(ip));
1976 if (u == 1) {
1977 add(ip, base, Operand(offset));
1978 } else {
1979 sub(ip, base, Operand(offset));
1980 }
1981 emit(cond | 0xD0*B20 | ip.code()*B16 | src.code()*B12 | 0xB*B8);
1982 }
1983}
1984
1985
1986void Assembler::vstr(const DwVfpRegister src,
1987 const MemOperand& operand,
1988 const Condition cond) {
1989 ASSERT(!operand.rm().is_valid());
1990 ASSERT(operand.am_ == Offset);
1991 vstr(src, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001992}
1993
1994
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001995void Assembler::vstr(const SwVfpRegister src,
1996 const Register base,
1997 int offset,
1998 const Condition cond) {
1999 // MEM(Rbase + offset) = SSrc.
2000 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002001 // cond(31-28) | 1101(27-24)| U000(23-20) | Rbase(19-16) |
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002002 // Vdst(15-12) | 1010(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002003 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002004 int u = 1;
2005 if (offset < 0) {
2006 offset = -offset;
2007 u = 0;
2008 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002009 int sd, d;
2010 src.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002011 ASSERT(offset >= 0);
2012 if ((offset % 4) == 0 && (offset / 4) < 256) {
2013 emit(cond | u*B23 | d*B22 | 0xD0*B20 | base.code()*B16 | sd*B12 |
2014 0xA*B8 | ((offset / 4) & 255));
2015 } else {
2016 // Larger offsets must be handled by computing the correct address
2017 // in the ip register.
2018 ASSERT(!base.is(ip));
2019 if (u == 1) {
2020 add(ip, base, Operand(offset));
2021 } else {
2022 sub(ip, base, Operand(offset));
2023 }
2024 emit(cond | d*B22 | 0xD0*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
2025 }
2026}
2027
2028
2029void Assembler::vstr(const SwVfpRegister src,
2030 const MemOperand& operand,
2031 const Condition cond) {
2032 ASSERT(!operand.rm().is_valid());
2033 ASSERT(operand.am_ == Offset);
2034 vldr(src, operand.rn(), operand.offset(), cond);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002035}
2036
2037
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002038static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
2039 uint64_t i;
2040 memcpy(&i, &d, 8);
2041
2042 *lo = i & 0xffffffff;
2043 *hi = i >> 32;
2044}
2045
2046// Only works for little endian floating point formats.
2047// We don't support VFP on the mixed endian floating point platform.
2048static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002049 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002050
2051 // VMOV can accept an immediate of the form:
2052 //
2053 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
2054 //
2055 // The immediate is encoded using an 8-bit quantity, comprised of two
2056 // 4-bit fields. For an 8-bit immediate of the form:
2057 //
2058 // [abcdefgh]
2059 //
2060 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
2061 // created of the form:
2062 //
2063 // [aBbbbbbb,bbcdefgh,00000000,00000000,
2064 // 00000000,00000000,00000000,00000000]
2065 //
2066 // where B = ~b.
2067 //
2068
2069 uint32_t lo, hi;
2070 DoubleAsTwoUInt32(d, &lo, &hi);
2071
2072 // The most obvious constraint is the long block of zeroes.
2073 if ((lo != 0) || ((hi & 0xffff) != 0)) {
2074 return false;
2075 }
2076
2077 // Bits 62:55 must be all clear or all set.
2078 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
2079 return false;
2080 }
2081
2082 // Bit 63 must be NOT bit 62.
2083 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
2084 return false;
2085 }
2086
2087 // Create the encoded immediate in the form:
2088 // [00000000,0000abcd,00000000,0000efgh]
2089 *encoding = (hi >> 16) & 0xf; // Low nybble.
2090 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
2091 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
2092
2093 return true;
2094}
2095
2096
2097void Assembler::vmov(const DwVfpRegister dst,
2098 double imm,
2099 const Condition cond) {
2100 // Dd = immediate
2101 // Instruction details available in ARM DDI 0406B, A8-640.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002102 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002103
2104 uint32_t enc;
2105 if (FitsVMOVDoubleImmediate(imm, &enc)) {
2106 // The double can be encoded in the instruction.
2107 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
2108 } else {
2109 // Synthesise the double from ARM immediates. This could be implemented
2110 // using vldr from a constant pool.
2111 uint32_t lo, hi;
2112 DoubleAsTwoUInt32(imm, &lo, &hi);
2113
2114 if (lo == hi) {
2115 // If the lo and hi parts of the double are equal, the literal is easier
2116 // to create. This is the case with 0.0.
2117 mov(ip, Operand(lo));
2118 vmov(dst, ip, ip);
2119 } else {
2120 // Move the low part of the double into the lower of the corresponsing S
2121 // registers of D register dst.
2122 mov(ip, Operand(lo));
2123 vmov(dst.low(), ip, cond);
2124
2125 // Move the high part of the double into the higher of the corresponsing S
2126 // registers of D register dst.
2127 mov(ip, Operand(hi));
2128 vmov(dst.high(), ip, cond);
2129 }
2130 }
2131}
2132
2133
2134void Assembler::vmov(const SwVfpRegister dst,
2135 const SwVfpRegister src,
2136 const Condition cond) {
2137 // Sd = Sm
2138 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002139 ASSERT(CpuFeatures::IsEnabled(VFP3));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002140 int sd, d, sm, m;
2141 dst.split_code(&sd, &d);
2142 src.split_code(&sm, &m);
2143 emit(cond | 0xE*B24 | d*B22 | 0xB*B20 | sd*B12 | 0xA*B8 | B6 | m*B5 | sm);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002144}
2145
2146
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002147void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002148 const DwVfpRegister src,
2149 const Condition cond) {
2150 // Dd = Dm
2151 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002152 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002153 emit(cond | 0xE*B24 | 0xB*B20 |
2154 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
2155}
2156
2157
2158void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002159 const Register src1,
2160 const Register src2,
2161 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002162 // Dm = <Rt,Rt2>.
2163 // Instruction details available in ARM DDI 0406A, A8-646.
2164 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
2165 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002166 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002167 ASSERT(!src1.is(pc) && !src2.is(pc));
2168 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
2169 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
2170}
2171
2172
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002173void Assembler::vmov(const Register dst1,
2174 const Register dst2,
2175 const DwVfpRegister src,
2176 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002177 // <Rt,Rt2> = Dm.
2178 // Instruction details available in ARM DDI 0406A, A8-646.
2179 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
2180 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002181 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002182 ASSERT(!dst1.is(pc) && !dst2.is(pc));
2183 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
2184 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
2185}
2186
2187
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002188void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002189 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002190 const Condition cond) {
2191 // Sn = Rt.
2192 // Instruction details available in ARM DDI 0406A, A8-642.
2193 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
2194 // 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 +00002195 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002196 ASSERT(!src.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002197 int sn, n;
2198 dst.split_code(&sn, &n);
2199 emit(cond | 0xE*B24 | sn*B16 | src.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002200}
2201
2202
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002203void Assembler::vmov(const Register dst,
2204 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002205 const Condition cond) {
2206 // Rt = Sn.
2207 // Instruction details available in ARM DDI 0406A, A8-642.
2208 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
2209 // 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 +00002210 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002211 ASSERT(!dst.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002212 int sn, n;
2213 src.split_code(&sn, &n);
2214 emit(cond | 0xE*B24 | B20 | sn*B16 | dst.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002215}
2216
2217
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002218// Type of data to read from or write to VFP register.
2219// Used as specifier in generic vcvt instruction.
2220enum VFPType { S32, U32, F32, F64 };
2221
2222
2223static bool IsSignedVFPType(VFPType type) {
2224 switch (type) {
2225 case S32:
2226 return true;
2227 case U32:
2228 return false;
2229 default:
2230 UNREACHABLE();
2231 return false;
2232 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002233}
2234
2235
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002236static bool IsIntegerVFPType(VFPType type) {
2237 switch (type) {
2238 case S32:
2239 case U32:
2240 return true;
2241 case F32:
2242 case F64:
2243 return false;
2244 default:
2245 UNREACHABLE();
2246 return false;
2247 }
2248}
2249
2250
2251static bool IsDoubleVFPType(VFPType type) {
2252 switch (type) {
2253 case F32:
2254 return false;
2255 case F64:
2256 return true;
2257 default:
2258 UNREACHABLE();
2259 return false;
2260 }
2261}
2262
2263
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002264// Split five bit reg_code based on size of reg_type.
2265// 32-bit register codes are Vm:M
2266// 64-bit register codes are M:Vm
2267// where Vm is four bits, and M is a single bit.
2268static void SplitRegCode(VFPType reg_type,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002269 int reg_code,
2270 int* vm,
2271 int* m) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002272 ASSERT((reg_code >= 0) && (reg_code <= 31));
2273 if (IsIntegerVFPType(reg_type) || !IsDoubleVFPType(reg_type)) {
2274 // 32 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002275 *m = reg_code & 0x1;
2276 *vm = reg_code >> 1;
2277 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002278 // 64 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002279 *m = (reg_code & 0x10) >> 4;
2280 *vm = reg_code & 0x0F;
2281 }
2282}
2283
2284
2285// Encode vcvt.src_type.dst_type instruction.
2286static Instr EncodeVCVT(const VFPType dst_type,
2287 const int dst_code,
2288 const VFPType src_type,
2289 const int src_code,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002290 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002291 const Condition cond) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002292 ASSERT(src_type != dst_type);
2293 int D, Vd, M, Vm;
2294 SplitRegCode(src_type, src_code, &Vm, &M);
2295 SplitRegCode(dst_type, dst_code, &Vd, &D);
2296
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002297 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2298 // Conversion between IEEE floating point and 32-bit integer.
2299 // Instruction details available in ARM DDI 0406B, A8.6.295.
2300 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2301 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2302 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2303
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002304 int sz, opc2, op;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002305
2306 if (IsIntegerVFPType(dst_type)) {
2307 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2308 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002309 op = mode;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002310 } else {
2311 ASSERT(IsIntegerVFPType(src_type));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002312 opc2 = 0x0;
2313 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2314 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002315 }
2316
2317 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2318 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2319 } else {
2320 // Conversion between IEEE double and single precision.
2321 // Instruction details available in ARM DDI 0406B, A8.6.298.
2322 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2323 // 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 +00002324 int sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002325 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2326 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2327 }
2328}
2329
2330
2331void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2332 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002333 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002334 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002335 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002336 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002337}
2338
2339
2340void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2341 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002342 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002343 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002344 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002345 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002346}
2347
2348
2349void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2350 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002351 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002352 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002353 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002354 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002355}
2356
2357
2358void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2359 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002360 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002361 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002362 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002363 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002364}
2365
2366
2367void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2368 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002369 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002370 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002371 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002372 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002373}
2374
2375
2376void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2377 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002378 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002379 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002380 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002381 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002382}
2383
2384
2385void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2386 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002387 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002388 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002389 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002390 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), mode, cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002391}
2392
2393
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002394void Assembler::vneg(const DwVfpRegister dst,
2395 const DwVfpRegister src,
2396 const Condition cond) {
2397 emit(cond | 0xE*B24 | 0xB*B20 | B16 | dst.code()*B12 |
2398 0x5*B9 | B8 | B6 | src.code());
2399}
2400
2401
whesse@chromium.org7a392b32011-01-31 11:30:36 +00002402void Assembler::vabs(const DwVfpRegister dst,
2403 const DwVfpRegister src,
2404 const Condition cond) {
2405 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 |
2406 0x5*B9 | B8 | 0x3*B6 | src.code());
2407}
2408
2409
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002410void Assembler::vadd(const DwVfpRegister dst,
2411 const DwVfpRegister src1,
2412 const DwVfpRegister src2,
2413 const Condition cond) {
2414 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002415 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2416 // Instruction details available in ARM DDI 0406A, A8-536.
2417 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2418 // 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 +00002419 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002420 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2421 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2422}
2423
2424
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002425void Assembler::vsub(const DwVfpRegister dst,
2426 const DwVfpRegister src1,
2427 const DwVfpRegister src2,
2428 const Condition cond) {
2429 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002430 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2431 // Instruction details available in ARM DDI 0406A, A8-784.
2432 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2433 // 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 +00002434 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002435 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2436 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2437}
2438
2439
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002440void Assembler::vmul(const DwVfpRegister dst,
2441 const DwVfpRegister src1,
2442 const DwVfpRegister src2,
2443 const Condition cond) {
2444 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002445 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2446 // Instruction details available in ARM DDI 0406A, A8-784.
2447 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2448 // 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 +00002449 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002450 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2451 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2452}
2453
2454
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002455void Assembler::vdiv(const DwVfpRegister dst,
2456 const DwVfpRegister src1,
2457 const DwVfpRegister src2,
2458 const Condition cond) {
2459 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002460 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2461 // Instruction details available in ARM DDI 0406A, A8-584.
2462 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2463 // 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 +00002464 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002465 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2466 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2467}
2468
2469
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002470void Assembler::vcmp(const DwVfpRegister src1,
2471 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002472 const Condition cond) {
2473 // vcmp(Dd, Dm) double precision floating point comparison.
2474 // Instruction details available in ARM DDI 0406A, A8-570.
2475 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002476 // 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 +00002477 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002478 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002479 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002480}
2481
2482
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002483void Assembler::vcmp(const DwVfpRegister src1,
2484 const double src2,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002485 const Condition cond) {
2486 // vcmp(Dd, Dm) double precision floating point comparison.
2487 // Instruction details available in ARM DDI 0406A, A8-570.
2488 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0101 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002489 // 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 +00002490 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002491 ASSERT(src2 == 0.0);
2492 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | B16 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002493 src1.code()*B12 | 0x5*B9 | B8 | B6);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002494}
2495
2496
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002497void Assembler::vmsr(Register dst, Condition cond) {
2498 // Instruction details available in ARM DDI 0406A, A8-652.
2499 // cond(31-28) | 1110 (27-24) | 1110(23-20)| 0001 (19-16) |
2500 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002501 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002502 emit(cond | 0xE*B24 | 0xE*B20 | B16 |
2503 dst.code()*B12 | 0xA*B8 | B4);
2504}
2505
2506
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002507void Assembler::vmrs(Register dst, Condition cond) {
2508 // Instruction details available in ARM DDI 0406A, A8-652.
2509 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2510 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002511 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002512 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2513 dst.code()*B12 | 0xA*B8 | B4);
2514}
2515
2516
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002517void Assembler::vsqrt(const DwVfpRegister dst,
2518 const DwVfpRegister src,
2519 const Condition cond) {
2520 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2521 // 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 +00002522 ASSERT(CpuFeatures::IsEnabled(VFP3));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002523 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2524 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2525}
2526
2527
ager@chromium.org5c838252010-02-19 08:53:10 +00002528// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002529void Assembler::nop(int type) {
2530 // This is mov rx, rx.
2531 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2532 emit(al | 13*B21 | type*B12 | type);
2533}
2534
2535
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002536bool Assembler::IsNop(Instr instr, int type) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002537 // Check for mov rx, rx where x = type.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002538 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2539 return instr == (al | 13*B21 | type*B12 | type);
2540}
2541
2542
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002543bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2544 uint32_t dummy1;
2545 uint32_t dummy2;
2546 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2547}
2548
2549
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002550void Assembler::BlockConstPoolFor(int instructions) {
2551 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2552}
2553
2554
ager@chromium.org5c838252010-02-19 08:53:10 +00002555// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002556void Assembler::RecordJSReturn() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002557 positions_recorder()->WriteRecordedPositions();
ager@chromium.org4af710e2009-09-15 12:20:11 +00002558 CheckBuffer();
2559 RecordRelocInfo(RelocInfo::JS_RETURN);
2560}
2561
2562
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002563void Assembler::RecordDebugBreakSlot() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002564 positions_recorder()->WriteRecordedPositions();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002565 CheckBuffer();
2566 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2567}
2568
2569
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570void Assembler::RecordComment(const char* msg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002571 if (FLAG_code_comments) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00002573 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 }
2575}
2576
2577
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002578void Assembler::GrowBuffer() {
2579 if (!own_buffer_) FATAL("external code buffer is too small");
2580
ager@chromium.org5c838252010-02-19 08:53:10 +00002581 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 CodeDesc desc; // the new buffer
2583 if (buffer_size_ < 4*KB) {
2584 desc.buffer_size = 4*KB;
2585 } else if (buffer_size_ < 1*MB) {
2586 desc.buffer_size = 2*buffer_size_;
2587 } else {
2588 desc.buffer_size = buffer_size_ + 1*MB;
2589 }
2590 CHECK_GT(desc.buffer_size, 0); // no overflow
2591
ager@chromium.org5c838252010-02-19 08:53:10 +00002592 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 desc.buffer = NewArray<byte>(desc.buffer_size);
2594
2595 desc.instr_size = pc_offset();
2596 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2597
ager@chromium.org5c838252010-02-19 08:53:10 +00002598 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599 int pc_delta = desc.buffer - buffer_;
2600 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2601 memmove(desc.buffer, buffer_, desc.instr_size);
2602 memmove(reloc_info_writer.pos() + rc_delta,
2603 reloc_info_writer.pos(), desc.reloc_size);
2604
ager@chromium.org5c838252010-02-19 08:53:10 +00002605 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606 DeleteArray(buffer_);
2607 buffer_ = desc.buffer;
2608 buffer_size_ = desc.buffer_size;
2609 pc_ += pc_delta;
2610 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2611 reloc_info_writer.last_pc() + pc_delta);
2612
ager@chromium.org5c838252010-02-19 08:53:10 +00002613 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00002615 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616
ager@chromium.org5c838252010-02-19 08:53:10 +00002617 // Relocate pending relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 for (int i = 0; i < num_prinfo_; i++) {
2619 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002620 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2621 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00002622 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2623 rinfo.set_pc(rinfo.pc() + pc_delta);
2624 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 }
2626}
2627
2628
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002629void Assembler::db(uint8_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002630 // No relocation info should be pending while using db. db is used
2631 // to write pure data with no pointers and the constant pool should
2632 // be emitted before using db.
2633 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002634 CheckBuffer();
2635 *reinterpret_cast<uint8_t*>(pc_) = data;
2636 pc_ += sizeof(uint8_t);
2637}
2638
2639
2640void Assembler::dd(uint32_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002641 // No relocation info should be pending while using dd. dd is used
2642 // to write pure data with no pointers and the constant pool should
2643 // be emitted before using dd.
2644 ASSERT(num_prinfo_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002645 CheckBuffer();
2646 *reinterpret_cast<uint32_t*>(pc_) = data;
2647 pc_ += sizeof(uint32_t);
2648}
2649
2650
ager@chromium.org236ad962008-09-25 09:45:57 +00002651void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002653 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002654 // Adjust code for new modes.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002655 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2656 || RelocInfo::IsJSReturn(rmode)
ager@chromium.org4af710e2009-09-15 12:20:11 +00002657 || RelocInfo::IsComment(rmode)
2658 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00002659 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 } else {
2661 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2662 prinfo_[num_prinfo_++] = rinfo;
2663 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00002664 // instruction for which we just recorded relocation info.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 BlockConstPoolBefore(pc_offset() + kInstrSize);
2666 }
ager@chromium.org236ad962008-09-25 09:45:57 +00002667 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002668 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002669 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2670#ifdef DEBUG
2671 if (!Serializer::enabled()) {
2672 Serializer::TooLateToEnableNow();
2673 }
2674#endif
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002675 if (!Serializer::enabled() && !emit_debug_code()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002676 return;
2677 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002678 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2680 reloc_info_writer.Write(&rinfo);
2681 }
2682}
2683
2684
2685void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2686 // Calculate the offset of the next check. It will be overwritten
2687 // when a const pool is generated or when const pools are being
2688 // blocked for a specific range.
2689 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2690
ager@chromium.org5c838252010-02-19 08:53:10 +00002691 // There is nothing to do if there are no pending relocation info entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 if (num_prinfo_ == 0) return;
2693
2694 // We emit a constant pool at regular intervals of about kDistBetweenPools
2695 // or when requested by parameter force_emit (e.g. after each function).
2696 // We prefer not to emit a jump unless the max distance is reached or if we
2697 // are running low on slots, which can happen if a lot of constants are being
2698 // emitted (e.g. --debug-code and many static references).
2699 int dist = pc_offset() - last_const_pool_end_;
2700 if (!force_emit && dist < kMaxDistBetweenPools &&
2701 (require_jump || dist < kDistBetweenPools) &&
2702 // TODO(1236125): Cleanup the "magic" number below. We know that
2703 // the code generation will test every kCheckConstIntervalInst.
2704 // Thus we are safe as long as we generate less than 7 constant
2705 // entries per instruction.
2706 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2707 return;
2708 }
2709
2710 // If we did not return by now, we need to emit the constant pool soon.
2711
2712 // However, some small sequences of instructions must not be broken up by the
2713 // insertion of a constant pool; such sequences are protected by setting
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002714 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2715 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2716 // no_const_pool_before_.
2717 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002718 // Emission is currently blocked; make sure we try again as soon as
2719 // possible.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002720 if (const_pool_blocked_nesting_ > 0) {
2721 next_buffer_check_ = pc_offset() + kInstrSize;
2722 } else {
2723 next_buffer_check_ = no_const_pool_before_;
2724 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725
ager@chromium.org5c838252010-02-19 08:53:10 +00002726 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 ASSERT(!force_emit);
2728 return;
2729 }
2730
2731 int jump_instr = require_jump ? kInstrSize : 0;
2732
2733 // Check that the code buffer is large enough before emitting the constant
2734 // pool and relocation information (include the jump over the pool and the
2735 // constant pool marker).
2736 int max_needed_space =
2737 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2738 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2739
ager@chromium.org5c838252010-02-19 08:53:10 +00002740 // Block recursive calls to CheckConstPool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2742 num_prinfo_*kInstrSize);
2743 // Don't bother to check for the emit calls below.
2744 next_buffer_check_ = no_const_pool_before_;
2745
ager@chromium.org5c838252010-02-19 08:53:10 +00002746 // Emit jump over constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 Label after_pool;
2748 if (require_jump) b(&after_pool);
2749
2750 RecordComment("[ Constant Pool");
2751
ager@chromium.org5c838252010-02-19 08:53:10 +00002752 // Put down constant pool marker "Undefined instruction" as specified by
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002753 // A5.6 (ARMv7) Instruction set encoding.
2754 emit(kConstantPoolMarker | num_prinfo_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755
ager@chromium.org5c838252010-02-19 08:53:10 +00002756 // Emit constant pool entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757 for (int i = 0; i < num_prinfo_; i++) {
2758 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002759 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2760 rinfo.rmode() != RelocInfo::POSITION &&
2761 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 Instr instr = instr_at(rinfo.pc());
ager@chromium.org4af710e2009-09-15 12:20:11 +00002763
ager@chromium.org5c838252010-02-19 08:53:10 +00002764 // Instruction to patch must be a ldr/str [pc, #offset].
2765 // P and U set, B and W clear, Rn == pc, offset12 still 0.
ager@chromium.org378b34e2011-01-28 08:04:38 +00002766 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | kOff12Mask)) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 (2*B25 | P | U | pc.code()*B16));
2768 int delta = pc_ - rinfo.pc() - 8;
2769 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2770 if (delta < 0) {
2771 instr &= ~U;
2772 delta = -delta;
2773 }
2774 ASSERT(is_uint12(delta));
2775 instr_at_put(rinfo.pc(), instr + delta);
2776 emit(rinfo.data());
2777 }
2778 num_prinfo_ = 0;
2779 last_const_pool_end_ = pc_offset();
2780
2781 RecordComment("]");
2782
2783 if (after_pool.is_linked()) {
2784 bind(&after_pool);
2785 }
2786
2787 // Since a constant pool was just emitted, move the check offset forward by
2788 // the standard interval.
2789 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2790}
2791
2792
2793} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002794
2795#endif // V8_TARGET_ARCH_ARM