blob: efa252dbaccc73b58f7ff5af34923d90bdabd55a [file] [log] [blame]
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003//
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the
14// distribution.
15//
16// - Neither the name of Sun Microsystems or the names of contributors may
17// be used to endorse or promote products derived from this software without
18// specific prior written permission.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000019//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000022// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31// OF THE POSSIBILITY OF SUCH DAMAGE.
32
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033// The original source code covered by the above license above has been
34// modified significantly by Google Inc.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000035// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37#include "v8.h"
38
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000039#if defined(V8_TARGET_ARCH_ARM)
40
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000041#include "arm/assembler-arm-inl.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000042#include "serialize.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000047#ifdef DEBUG
48bool CpuFeatures::initialized_ = false;
49#endif
50unsigned CpuFeatures::supported_ = 0;
51unsigned CpuFeatures::found_by_runtime_probing_ = 0;
52
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000053
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000054// Get the CPU features enabled by the build. For cross compilation the
55// preprocessor symbols CAN_USE_ARMV7_INSTRUCTIONS and CAN_USE_VFP_INSTRUCTIONS
56// can be defined to enable ARMv7 and VFPv3 instructions when building the
57// snapshot.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000058static uint64_t CpuFeaturesImpliedByCompiler() {
59 uint64_t answer = 0;
60#ifdef CAN_USE_ARMV7_INSTRUCTIONS
61 answer |= 1u << ARMv7;
62#endif // def CAN_USE_ARMV7_INSTRUCTIONS
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000063#ifdef CAN_USE_VFP_INSTRUCTIONS
64 answer |= 1u << VFP3 | 1u << ARMv7;
65#endif // def CAN_USE_VFP_INSTRUCTIONS
66
67#ifdef __arm__
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000068 // If the compiler is allowed to use VFP then we can use VFP too in our code
69 // generation even when generating snapshots. This won't work for cross
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000070 // compilation. VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000071#if defined(__VFP_FP__) && !defined(__SOFTFP__)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000072 answer |= 1u << VFP3 | 1u << ARMv7;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000073#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000074#endif // def __arm__
75
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000076 return answer;
77}
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000078
79
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000080void CpuFeatures::Probe() {
81 ASSERT(!initialized_);
82#ifdef DEBUG
83 initialized_ = true;
84#endif
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000085
86 // Get the features implied by the OS and the compiler settings. This is the
87 // minimal set of features which is also alowed for generated code in the
88 // snapshot.
89 supported_ |= OS::CpuFeaturesImpliedByPlatform();
90 supported_ |= CpuFeaturesImpliedByCompiler();
91
92 if (Serializer::enabled()) {
93 // No probing for features if we might serialize (generate snapshot).
94 return;
95 }
96
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000097#ifndef __arm__
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000098 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is
99 // enabled. VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
ager@chromium.org5c838252010-02-19 08:53:10 +0000100 if (FLAG_enable_vfp3) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000101 supported_ |= 1u << VFP3 | 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +0000102 }
103 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
104 if (FLAG_enable_armv7) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000105 supported_ |= 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +0000106 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000107#else // def __arm__
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000108 // Probe for additional features not already known to be available.
109 if (!IsSupported(VFP3) && OS::ArmCpuHasFeature(VFP3)) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000110 // This implementation also sets the VFP flags if runtime
111 // detection of VFP returns true. VFPv3 implies ARMv7, see ARM DDI
112 // 0406B, page A1-6.
113 supported_ |= 1u << VFP3 | 1u << ARMv7;
114 found_by_runtime_probing_ |= 1u << VFP3 | 1u << ARMv7;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000116
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000117 if (!IsSupported(ARMv7) && OS::ArmCpuHasFeature(ARMv7)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000118 supported_ |= 1u << ARMv7;
119 found_by_runtime_probing_ |= 1u << ARMv7;
120 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121#endif
122}
123
124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126// Implementation of RelocInfo
127
128const int RelocInfo::kApplyMask = 0;
129
130
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000131bool RelocInfo::IsCodedSpecially() {
132 // The deserializer needs to know whether a pointer is specially coded. Being
133 // specially coded on ARM means that it is a movw/movt instruction. We don't
134 // generate those yet.
135 return false;
136}
137
138
139
iposva@chromium.org245aa852009-02-10 00:49:54 +0000140void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000142 Instr* pc = reinterpret_cast<Instr*>(pc_);
143 Instr* instr = reinterpret_cast<Instr*>(instructions);
144 for (int i = 0; i < instruction_count; i++) {
145 *(pc + i) = *(instr + i);
146 }
147
148 // Indicate that code has changed.
149 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150}
151
152
153// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000154// Additional guard instructions can be added if required.
155void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 // Patch the code at the current address with a call to the target.
157 UNIMPLEMENTED();
158}
159
160
161// -----------------------------------------------------------------------------
162// Implementation of Operand and MemOperand
163// See assembler-arm-inl.h for inlined constructors
164
165Operand::Operand(Handle<Object> handle) {
166 rm_ = no_reg;
167 // Verify all Objects referred by code are NOT in new space.
168 Object* obj = *handle;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000169 ASSERT(!HEAP->InNewSpace(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 if (obj->IsHeapObject()) {
171 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000172 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 } else {
174 // no relocation needed
175 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000176 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 }
178}
179
180
181Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
182 ASSERT(is_uint5(shift_imm));
183 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
184 rm_ = rm;
185 rs_ = no_reg;
186 shift_op_ = shift_op;
187 shift_imm_ = shift_imm & 31;
188 if (shift_op == RRX) {
189 // encoded as ROR with shift_imm == 0
190 ASSERT(shift_imm == 0);
191 shift_op_ = ROR;
192 shift_imm_ = 0;
193 }
194}
195
196
197Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
198 ASSERT(shift_op != RRX);
199 rm_ = rm;
200 rs_ = no_reg;
201 shift_op_ = shift_op;
202 rs_ = rs;
203}
204
205
206MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
207 rn_ = rn;
208 rm_ = no_reg;
209 offset_ = offset;
210 am_ = am;
211}
212
213MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
214 rn_ = rn;
215 rm_ = rm;
216 shift_op_ = LSL;
217 shift_imm_ = 0;
218 am_ = am;
219}
220
221
222MemOperand::MemOperand(Register rn, Register rm,
223 ShiftOp shift_op, int shift_imm, AddrMode am) {
224 ASSERT(is_uint5(shift_imm));
225 rn_ = rn;
226 rm_ = rm;
227 shift_op_ = shift_op;
228 shift_imm_ = shift_imm & 31;
229 am_ = am;
230}
231
232
233// -----------------------------------------------------------------------------
ager@chromium.org378b34e2011-01-28 08:04:38 +0000234// Specific instructions, constants, and masks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
mads.s.ager31e71382008-08-13 09:32:07 +0000236// add(sp, sp, 4) instruction (aka Pop())
ager@chromium.org378b34e2011-01-28 08:04:38 +0000237const Instr kPopInstruction =
238 al | PostIndex | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
mads.s.ager31e71382008-08-13 09:32:07 +0000239// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
240// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000241const Instr kPushRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000242 al | B26 | 4 | NegPreIndex | sp.code() * B16;
243// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
244// register r is not encoded.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000245const Instr kPopRegPattern =
mads.s.ager31e71382008-08-13 09:32:07 +0000246 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000247// mov lr, pc
ager@chromium.org378b34e2011-01-28 08:04:38 +0000248const Instr kMovLrPc = al | MOV | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000249// ldr rd, [pc, #offset]
ager@chromium.org378b34e2011-01-28 08:04:38 +0000250const Instr kLdrPCMask = kCondMask | 15 * B24 | 7 * B20 | 15 * B16;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000251const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
252// blxcc rm
253const Instr kBlxRegMask =
254 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
255const Instr kBlxRegPattern =
ager@chromium.org378b34e2011-01-28 08:04:38 +0000256 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | BLX;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000257const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
258const Instr kMovMvnPattern = 0xd * B21;
259const Instr kMovMvnFlip = B22;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000260const Instr kMovLeaveCCMask = 0xdff * B16;
261const Instr kMovLeaveCCPattern = 0x1a0 * B16;
262const Instr kMovwMask = 0xff * B20;
263const Instr kMovwPattern = 0x30 * B20;
264const Instr kMovwLeaveCCFlip = 0x5 * B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000265const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
266const Instr kCmpCmnPattern = 0x15 * B20;
267const Instr kCmpCmnFlip = B21;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000268const Instr kAddSubFlip = 0x6 * B21;
269const Instr kAndBicFlip = 0xe * B21;
270
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000271// A mask for the Rd register for push, pop, ldr, str instructions.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000272const Instr kLdrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000273 al | B26 | L | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000274const Instr kStrRegFpOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000275 al | B26 | Offset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000276const Instr kLdrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000277 al | B26 | L | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000278const Instr kStrRegFpNegOffsetPattern =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000279 al | B26 | NegOffset | fp.code() * B16;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000280const Instr kLdrStrInstrTypeMask = 0xffff0000;
281const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
282const Instr kLdrStrOffsetMask = 0x00000fff;
283
mads.s.ager31e71382008-08-13 09:32:07 +0000284
ager@chromium.org5c838252010-02-19 08:53:10 +0000285// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286static const int kMinimalBufferSize = 4*KB;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287
ager@chromium.org378b34e2011-01-28 08:04:38 +0000288
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000289Assembler::Assembler(Isolate* arg_isolate, void* buffer, int buffer_size)
290 : AssemblerBase(arg_isolate),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291 positions_recorder_(this),
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000292 emit_debug_code_(FLAG_debug_code) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000294 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 if (buffer_size <= kMinimalBufferSize) {
296 buffer_size = kMinimalBufferSize;
297
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298 if (isolate()->assembler_spare_buffer() != NULL) {
299 buffer = isolate()->assembler_spare_buffer();
300 isolate()->set_assembler_spare_buffer(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 }
302 }
303 if (buffer == NULL) {
304 buffer_ = NewArray<byte>(buffer_size);
305 } else {
306 buffer_ = static_cast<byte*>(buffer);
307 }
308 buffer_size_ = buffer_size;
309 own_buffer_ = true;
310
311 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000312 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 ASSERT(buffer_size > 0);
314 buffer_ = static_cast<byte*>(buffer);
315 buffer_size_ = buffer_size;
316 own_buffer_ = false;
317 }
318
ager@chromium.org5c838252010-02-19 08:53:10 +0000319 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 ASSERT(buffer_ != NULL);
321 pc_ = buffer_;
322 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000323 num_pending_reloc_info_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000325 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 no_const_pool_before_ = 0;
whesse@chromium.org7b260152011-06-20 15:33:18 +0000327 first_const_pool_use_ = -1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 last_bound_pos_ = 0;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000329 ast_id_for_reloc_info_ = kNoASTId;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330}
331
332
333Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000334 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 if (own_buffer_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000336 if (isolate()->assembler_spare_buffer() == NULL &&
337 buffer_size_ == kMinimalBufferSize) {
338 isolate()->set_assembler_spare_buffer(buffer_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 } else {
340 DeleteArray(buffer_);
341 }
342 }
343}
344
345
346void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000347 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 CheckConstPool(true, false);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000349 ASSERT(num_pending_reloc_info_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350
ager@chromium.org5c838252010-02-19 08:53:10 +0000351 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 desc->buffer = buffer_;
353 desc->buffer_size = buffer_size_;
354 desc->instr_size = pc_offset();
355 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
356}
357
358
359void Assembler::Align(int m) {
360 ASSERT(m >= 4 && IsPowerOf2(m));
361 while ((pc_offset() & (m - 1)) != 0) {
362 nop();
363 }
364}
365
366
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000367void Assembler::CodeTargetAlign() {
368 // Preferred alignment of jump targets on some ARM chips.
369 Align(8);
370}
371
372
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000373Condition Assembler::GetCondition(Instr instr) {
374 return Instruction::ConditionField(instr);
375}
376
377
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000378bool Assembler::IsBranch(Instr instr) {
379 return (instr & (B27 | B25)) == (B27 | B25);
380}
381
382
383int Assembler::GetBranchOffset(Instr instr) {
384 ASSERT(IsBranch(instr));
385 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
386 // with 4 to get the offset in bytes.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000387 return ((instr & kImm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000388}
389
390
391bool Assembler::IsLdrRegisterImmediate(Instr instr) {
392 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
393}
394
395
396int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
397 ASSERT(IsLdrRegisterImmediate(instr));
398 bool positive = (instr & B23) == B23;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000399 int offset = instr & kOff12Mask; // Zero extended offset.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000400 return positive ? offset : -offset;
401}
402
403
404Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
405 ASSERT(IsLdrRegisterImmediate(instr));
406 bool positive = offset >= 0;
407 if (!positive) offset = -offset;
408 ASSERT(is_uint12(offset));
409 // Set bit indicating whether the offset should be added.
410 instr = (instr & ~B23) | (positive ? B23 : 0);
411 // Set the actual offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000412 return (instr & ~kOff12Mask) | offset;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000413}
414
415
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000416bool Assembler::IsStrRegisterImmediate(Instr instr) {
417 return (instr & (B27 | B26 | B25 | B22 | B20)) == B26;
418}
419
420
421Instr Assembler::SetStrRegisterImmediateOffset(Instr instr, int offset) {
422 ASSERT(IsStrRegisterImmediate(instr));
423 bool positive = offset >= 0;
424 if (!positive) offset = -offset;
425 ASSERT(is_uint12(offset));
426 // Set bit indicating whether the offset should be added.
427 instr = (instr & ~B23) | (positive ? B23 : 0);
428 // Set the actual 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
433bool Assembler::IsAddRegisterImmediate(Instr instr) {
434 return (instr & (B27 | B26 | B25 | B24 | B23 | B22 | B21)) == (B25 | B23);
435}
436
437
438Instr Assembler::SetAddRegisterImmediateOffset(Instr instr, int offset) {
439 ASSERT(IsAddRegisterImmediate(instr));
440 ASSERT(offset >= 0);
441 ASSERT(is_uint12(offset));
442 // Set the offset.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000443 return (instr & ~kOff12Mask) | offset;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000444}
445
446
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000447Register Assembler::GetRd(Instr instr) {
448 Register reg;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000449 reg.code_ = Instruction::RdValue(instr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000450 return reg;
451}
452
453
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000454Register Assembler::GetRn(Instr instr) {
455 Register reg;
456 reg.code_ = Instruction::RnValue(instr);
457 return reg;
458}
459
460
461Register Assembler::GetRm(Instr instr) {
462 Register reg;
463 reg.code_ = Instruction::RmValue(instr);
464 return reg;
465}
466
467
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000468bool Assembler::IsPush(Instr instr) {
469 return ((instr & ~kRdMask) == kPushRegPattern);
470}
471
472
473bool Assembler::IsPop(Instr instr) {
474 return ((instr & ~kRdMask) == kPopRegPattern);
475}
476
477
478bool Assembler::IsStrRegFpOffset(Instr instr) {
479 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
480}
481
482
483bool Assembler::IsLdrRegFpOffset(Instr instr) {
484 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
485}
486
487
488bool Assembler::IsStrRegFpNegOffset(Instr instr) {
489 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
490}
491
492
493bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
494 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
495}
496
497
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000498bool Assembler::IsLdrPcImmediateOffset(Instr instr) {
499 // Check the instruction is indeed a
500 // ldr<cond> <Rd>, [pc +/- offset_12].
ager@chromium.org378b34e2011-01-28 08:04:38 +0000501 return (instr & (kLdrPCMask & ~kCondMask)) == 0x051f0000;
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000502}
503
504
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000505bool Assembler::IsTstImmediate(Instr instr) {
506 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
507 (I | TST | S);
508}
509
510
511bool Assembler::IsCmpRegister(Instr instr) {
512 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask | B4)) ==
513 (CMP | S);
514}
515
516
517bool Assembler::IsCmpImmediate(Instr instr) {
518 return (instr & (B27 | B26 | I | kOpCodeMask | S | kRdMask)) ==
519 (I | CMP | S);
520}
521
522
523Register Assembler::GetCmpImmediateRegister(Instr instr) {
524 ASSERT(IsCmpImmediate(instr));
525 return GetRn(instr);
526}
527
528
529int Assembler::GetCmpImmediateRawImmediate(Instr instr) {
530 ASSERT(IsCmpImmediate(instr));
531 return instr & kOff12Mask;
532}
533
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534// Labels refer to positions in the (to be) generated code.
535// There are bound, linked, and unused labels.
536//
537// Bound labels refer to known positions in the already
538// generated code. pos() is the position the label refers to.
539//
540// Linked labels refer to unknown positions in the code
541// to be generated; pos() is the position of the last
542// instruction using the label.
543
544
545// The link chain is terminated by a negative code position (must be aligned)
546const int kEndOfChain = -4;
547
548
549int Assembler::target_at(int pos) {
550 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000551 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000552 // Emitted label constant, not part of a branch.
553 return instr - (Code::kHeaderSize - kHeapObjectTag);
554 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000556 int imm26 = ((instr & kImm24Mask) << 8) >> 6;
557 if ((Instruction::ConditionField(instr) == kSpecialCondition) &&
558 ((instr & B24) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 // blx uses bit 24 to encode bit 2 of imm26
560 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000561 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000562 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563}
564
565
566void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 Instr instr = instr_at(pos);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000568 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000569 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
570 // Emitted label constant, not part of a branch.
571 // Make label relative to Code* of generated Code object.
572 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
573 return;
574 }
575 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
ager@chromium.org378b34e2011-01-28 08:04:38 +0000577 if (Instruction::ConditionField(instr) == kSpecialCondition) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 // blx uses bit 24 to encode bit 2 of imm26
579 ASSERT((imm26 & 1) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000580 instr = (instr & ~(B24 | kImm24Mask)) | ((imm26 & 2) >> 1)*B24;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 } else {
582 ASSERT((imm26 & 3) == 0);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000583 instr &= ~kImm24Mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 }
585 int imm24 = imm26 >> 2;
586 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000587 instr_at_put(pos, instr | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588}
589
590
591void Assembler::print(Label* L) {
592 if (L->is_unused()) {
593 PrintF("unused label\n");
594 } else if (L->is_bound()) {
595 PrintF("bound label to %d\n", L->pos());
596 } else if (L->is_linked()) {
597 Label l = *L;
598 PrintF("unbound label");
599 while (l.is_linked()) {
600 PrintF("@ %d ", l.pos());
601 Instr instr = instr_at(l.pos());
ager@chromium.org378b34e2011-01-28 08:04:38 +0000602 if ((instr & ~kImm24Mask) == 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000603 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000605 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
ager@chromium.org378b34e2011-01-28 08:04:38 +0000606 Condition cond = Instruction::ConditionField(instr);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000607 const char* b;
608 const char* c;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000609 if (cond == kSpecialCondition) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000610 b = "blx";
611 c = "";
612 } else {
613 if ((instr & B24) != 0)
614 b = "bl";
615 else
616 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000618 switch (cond) {
619 case eq: c = "eq"; break;
620 case ne: c = "ne"; break;
621 case hs: c = "hs"; break;
622 case lo: c = "lo"; break;
623 case mi: c = "mi"; break;
624 case pl: c = "pl"; break;
625 case vs: c = "vs"; break;
626 case vc: c = "vc"; break;
627 case hi: c = "hi"; break;
628 case ls: c = "ls"; break;
629 case ge: c = "ge"; break;
630 case lt: c = "lt"; break;
631 case gt: c = "gt"; break;
632 case le: c = "le"; break;
633 case al: c = ""; break;
634 default:
635 c = "";
636 UNREACHABLE();
637 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000639 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 next(&l);
642 }
643 } else {
644 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
645 }
646}
647
648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649void Assembler::bind_to(Label* L, int pos) {
650 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
651 while (L->is_linked()) {
652 int fixup_pos = L->pos();
653 next(L); // call next before overwriting link with target at fixup_pos
654 target_at_put(fixup_pos, pos);
655 }
656 L->bind_to(pos);
657
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000658 // Keep track of the last bound label so we don't eliminate any instructions
659 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 if (pos > last_bound_pos_)
661 last_bound_pos_ = pos;
662}
663
664
665void Assembler::link_to(Label* L, Label* appendix) {
666 if (appendix->is_linked()) {
667 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000668 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 int fixup_pos;
670 int link = L->pos();
671 do {
672 fixup_pos = link;
673 link = target_at(fixup_pos);
674 } while (link > 0);
675 ASSERT(link == kEndOfChain);
676 target_at_put(fixup_pos, appendix->pos());
677 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000678 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 *L = *appendix;
680 }
681 }
682 appendix->Unuse(); // appendix should not be used anymore
683}
684
685
686void Assembler::bind(Label* L) {
687 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 bind_to(L, pc_offset());
689}
690
691
692void Assembler::next(Label* L) {
693 ASSERT(L->is_linked());
694 int link = target_at(L->pos());
695 if (link > 0) {
696 L->link_to(link);
697 } else {
698 ASSERT(link == kEndOfChain);
699 L->Unuse();
700 }
701}
702
703
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000704static Instr EncodeMovwImmediate(uint32_t immediate) {
705 ASSERT(immediate < 0x10000);
706 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
707}
708
709
ager@chromium.org5c838252010-02-19 08:53:10 +0000710// Low-level code emission routines depending on the addressing mode.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000711// If this returns true then you have to use the rotate_imm and immed_8
712// that it returns, because it may have already changed the instruction
713// to match them!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714static bool fits_shifter(uint32_t imm32,
715 uint32_t* rotate_imm,
716 uint32_t* immed_8,
717 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000718 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 for (int rot = 0; rot < 16; rot++) {
720 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
721 if ((imm8 <= 0xff)) {
722 *rotate_imm = rot;
723 *immed_8 = imm8;
724 return true;
725 }
726 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000727 // If the opcode is one with a complementary version and the complementary
728 // immediate fits, change the opcode.
729 if (instr != NULL) {
730 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
731 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
732 *instr ^= kMovMvnFlip;
733 return true;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000734 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000735 if (CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000736 if (imm32 < 0x10000) {
737 *instr ^= kMovwLeaveCCFlip;
738 *instr |= EncodeMovwImmediate(imm32);
739 *rotate_imm = *immed_8 = 0; // Not used for movw.
740 return true;
741 }
742 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000743 }
744 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
745 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
746 *instr ^= kCmpCmnFlip;
747 return true;
748 }
749 } else {
750 Instr alu_insn = (*instr & kALUMask);
ager@chromium.org378b34e2011-01-28 08:04:38 +0000751 if (alu_insn == ADD ||
752 alu_insn == SUB) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000753 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
754 *instr ^= kAddSubFlip;
755 return true;
756 }
ager@chromium.org378b34e2011-01-28 08:04:38 +0000757 } else if (alu_insn == AND ||
758 alu_insn == BIC) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000759 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
760 *instr ^= kAndBicFlip;
761 return true;
762 }
763 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 }
765 }
766 return false;
767}
768
769
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000770// We have to use the temporary register for things that can be relocated even
771// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
772// space. There is no guarantee that the relocated location can be similarly
773// encoded.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000774bool Operand::must_use_constant_pool() const {
775 if (rmode_ == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000776#ifdef DEBUG
777 if (!Serializer::enabled()) {
778 Serializer::TooLateToEnableNow();
779 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000780#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000781 return Serializer::enabled();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000782 } else if (rmode_ == RelocInfo::NONE) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000783 return false;
784 }
785 return true;
786}
787
788
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000789bool Operand::is_single_instruction(Instr instr) const {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000790 if (rm_.is_valid()) return true;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000791 uint32_t dummy1, dummy2;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000792 if (must_use_constant_pool() ||
793 !fits_shifter(imm32_, &dummy1, &dummy2, &instr)) {
794 // The immediate operand cannot be encoded as a shifter operand, or use of
795 // constant pool is required. For a mov instruction not setting the
796 // condition code additional instruction conventions can be used.
797 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000798 if (must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000799 !CpuFeatures::IsSupported(ARMv7)) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000800 // mov instruction will be an ldr from constant pool (one instruction).
801 return true;
802 } else {
803 // mov instruction will be a mov or movw followed by movt (two
804 // instructions).
805 return false;
806 }
807 } else {
808 // If this is not a mov or mvn instruction there will always an additional
809 // instructions - either mov or ldr. The mov might actually be two
810 // instructions mov or movw followed by movt so including the actual
811 // instruction two or three instructions will be generated.
812 return false;
813 }
814 } else {
815 // No use of constant pool and the immediate operand can be encoded as a
816 // shifter operand.
817 return true;
818 }
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000819}
820
821
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822void Assembler::addrmod1(Instr instr,
823 Register rn,
824 Register rd,
825 const Operand& x) {
826 CheckBuffer();
ager@chromium.org378b34e2011-01-28 08:04:38 +0000827 ASSERT((instr & ~(kCondMask | kOpCodeMask | S)) == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000829 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 uint32_t rotate_imm;
831 uint32_t immed_8;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000832 if (x.must_use_constant_pool() ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
834 // The immediate operand cannot be encoded as a shifter operand, so load
835 // it first to register ip and change the original instruction to use ip.
836 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000837 // condition code), then replace it with a 'ldr rd, [pc]'.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000838 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
ager@chromium.org378b34e2011-01-28 08:04:38 +0000839 Condition cond = Instruction::ConditionField(instr);
840 if ((instr & ~kCondMask) == 13*B21) { // mov, S not set
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000841 if (x.must_use_constant_pool() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000842 !CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000843 RecordRelocInfo(x.rmode_, x.imm32_);
844 ldr(rd, MemOperand(pc, 0), cond);
845 } else {
846 // Will probably use movw, will certainly not use constant pool.
847 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
848 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
849 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000851 // If this is not a mov or mvn instruction we may still be able to avoid
852 // a constant pool entry by using mvn or movw.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000853 if (!x.must_use_constant_pool() &&
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000854 (instr & kMovMvnMask) != kMovMvnPattern) {
855 mov(ip, x, LeaveCC, cond);
856 } else {
857 RecordRelocInfo(x.rmode_, x.imm32_);
858 ldr(ip, MemOperand(pc, 0), cond);
859 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 addrmod1(instr, rn, rd, Operand(ip));
861 }
862 return;
863 }
864 instr |= I | rotate_imm*B8 | immed_8;
865 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000866 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
868 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000869 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
871 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
872 }
873 emit(instr | rn.code()*B16 | rd.code()*B12);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000874 if (rn.is(pc) || x.rm_.is(pc)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000875 // Block constant pool emission for one instruction after reading pc.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000876 BlockConstPoolFor(1);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000877 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878}
879
880
881void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000882 ASSERT((instr & ~(kCondMask | B | L)) == B26);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 int am = x.am_;
884 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000885 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 int offset_12 = x.offset_;
887 if (offset_12 < 0) {
888 offset_12 = -offset_12;
889 am ^= U;
890 }
891 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000892 // Immediate offset cannot be encoded, load it first to register ip
893 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000895 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
897 return;
898 }
899 ASSERT(offset_12 >= 0); // no masking needed
900 instr |= offset_12;
901 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000902 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000904 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 ASSERT(!x.rm_.is(pc));
906 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
907 }
908 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
909 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
910}
911
912
913void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000914 ASSERT((instr & ~(kCondMask | L | S6 | H)) == (B4 | B7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 ASSERT(x.rn_.is_valid());
916 int am = x.am_;
917 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000918 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 int offset_8 = x.offset_;
920 if (offset_8 < 0) {
921 offset_8 = -offset_8;
922 am ^= U;
923 }
924 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000925 // Immediate offset cannot be encoded, load it first to register ip
926 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000928 mov(ip, Operand(x.offset_), LeaveCC, Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
930 return;
931 }
932 ASSERT(offset_8 >= 0); // no masking needed
933 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
934 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000935 // Scaled register offset not supported, load index first
936 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
938 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
ager@chromium.org378b34e2011-01-28 08:04:38 +0000939 Instruction::ConditionField(instr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
941 return;
942 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000943 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
945 instr |= x.rm_.code();
946 }
947 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
948 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
949}
950
951
952void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
ager@chromium.org378b34e2011-01-28 08:04:38 +0000953 ASSERT((instr & ~(kCondMask | P | U | W | L)) == B27);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 ASSERT(rl != 0);
955 ASSERT(!rn.is(pc));
956 emit(instr | rn.code()*B16 | rl);
957}
958
959
960void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000961 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000962 ASSERT_EQ((B27 | B26),
ager@chromium.org378b34e2011-01-28 08:04:38 +0000963 (instr & ~(kCondMask | kCoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
965 int am = x.am_;
966 int offset_8 = x.offset_;
967 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
968 offset_8 >>= 2;
969 if (offset_8 < 0) {
970 offset_8 = -offset_8;
971 am ^= U;
972 }
973 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
974 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
975
ager@chromium.org5c838252010-02-19 08:53:10 +0000976 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 if ((am & P) == 0)
978 am |= W;
979
980 ASSERT(offset_8 >= 0); // no masking needed
981 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
982}
983
984
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000985int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 int target_pos;
987 if (L->is_bound()) {
988 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000990 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000992 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000994 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 L->link_to(pc_offset());
996 }
997
998 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +0000999 // be emitted at the pc offset recorded by the label.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001000 BlockConstPoolFor(1);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001001 return target_pos - (pc_offset() + kPcLoadDelta);
1002}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001004
1005void Assembler::label_at_put(Label* L, int at_offset) {
1006 int target_pos;
1007 if (L->is_bound()) {
1008 target_pos = L->pos();
1009 } else {
1010 if (L->is_linked()) {
1011 target_pos = L->pos(); // L's link
1012 } else {
1013 target_pos = kEndOfChain;
1014 }
1015 L->link_to(at_offset);
1016 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
1017 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018}
1019
1020
ager@chromium.org5c838252010-02-19 08:53:10 +00001021// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022void Assembler::b(int branch_offset, Condition cond) {
1023 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 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001028 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001029 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001031 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032}
1033
1034
1035void Assembler::bl(int branch_offset, Condition cond) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001036 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 ASSERT((branch_offset & 3) == 0);
1038 int imm24 = branch_offset >> 2;
1039 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001040 emit(cond | B27 | B25 | B24 | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041}
1042
1043
1044void Assembler::blx(int branch_offset) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001045 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 ASSERT((branch_offset & 1) == 0);
1047 int h = ((branch_offset & 2) >> 1)*B24;
1048 int imm24 = branch_offset >> 2;
1049 ASSERT(is_int24(imm24));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001050 emit(kSpecialCondition | B27 | B25 | h | (imm24 & kImm24Mask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051}
1052
1053
1054void Assembler::blx(Register target, Condition cond) { // v5 and above
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001055 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 ASSERT(!target.is(pc));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001057 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BLX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058}
1059
1060
1061void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001062 positions_recorder()->WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
ager@chromium.org378b34e2011-01-28 08:04:38 +00001064 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | BX | target.code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065}
1066
1067
ager@chromium.org5c838252010-02-19 08:53:10 +00001068// Data-processing instructions.
1069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070void Assembler::and_(Register dst, Register src1, const Operand& src2,
1071 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001072 addrmod1(cond | AND | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073}
1074
1075
1076void Assembler::eor(Register dst, Register src1, const Operand& src2,
1077 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001078 addrmod1(cond | EOR | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079}
1080
1081
1082void Assembler::sub(Register dst, Register src1, const Operand& src2,
1083 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001084 addrmod1(cond | SUB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085}
1086
1087
1088void Assembler::rsb(Register dst, Register src1, const Operand& src2,
1089 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001090 addrmod1(cond | RSB | s, src1, dst, src2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091}
1092
1093
1094void Assembler::add(Register dst, Register src1, const Operand& src2,
1095 SBit s, Condition cond) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001096 addrmod1(cond | ADD | s, src1, dst, src2);
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);
1401}
1402
1403
1404void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1405 addrmod2(cond | B26, src, dst);
1406}
1407
1408
1409void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1410 addrmod2(cond | B26 | B | L, dst, src);
1411}
1412
1413
1414void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1415 addrmod2(cond | B26 | B, src, dst);
1416}
1417
1418
1419void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1420 addrmod3(cond | L | B7 | H | B4, dst, src);
1421}
1422
1423
1424void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1425 addrmod3(cond | B7 | H | B4, src, dst);
1426}
1427
1428
1429void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1430 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1431}
1432
1433
1434void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1435 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1436}
1437
1438
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001439void Assembler::ldrd(Register dst1, Register dst2,
1440 const MemOperand& src, Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001441 ASSERT(CpuFeatures::IsEnabled(ARMv7));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001442 ASSERT(src.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001443 ASSERT(!dst1.is(lr)); // r14.
1444 ASSERT_EQ(0, dst1.code() % 2);
1445 ASSERT_EQ(dst1.code() + 1, dst2.code());
1446 addrmod3(cond | B7 | B6 | B4, dst1, src);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001447}
1448
1449
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001450void Assembler::strd(Register src1, Register src2,
1451 const MemOperand& dst, Condition cond) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001452 ASSERT(dst.rm().is(no_reg));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001453 ASSERT(!src1.is(lr)); // r14.
1454 ASSERT_EQ(0, src1.code() % 2);
1455 ASSERT_EQ(src1.code() + 1, src2.code());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001456 ASSERT(CpuFeatures::IsEnabled(ARMv7));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001457 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001458}
1459
ager@chromium.org5c838252010-02-19 08:53:10 +00001460// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461void Assembler::ldm(BlockAddrMode am,
1462 Register base,
1463 RegList dst,
1464 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001465 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1467
1468 addrmod4(cond | B27 | am | L, base, dst);
1469
ager@chromium.org5c838252010-02-19 08:53:10 +00001470 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 if (cond == al && (dst & pc.bit()) != 0) {
1472 // There is a slight chance that the ldm instruction was actually a call,
1473 // in which case it would be wrong to return into the constant pool; we
1474 // recognize this case by checking if the emission of the pool was blocked
1475 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1476 // the case, we emit a jump over the pool.
1477 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1478 }
1479}
1480
1481
1482void Assembler::stm(BlockAddrMode am,
1483 Register base,
1484 RegList src,
1485 Condition cond) {
1486 addrmod4(cond | B27 | am, base, src);
1487}
1488
1489
ager@chromium.org5c838252010-02-19 08:53:10 +00001490// Exception-generating instructions and debugging support.
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001491// Stops with a non-negative code less than kNumOfWatchedStops support
1492// enabling/disabling and a counter feature. See simulator-arm.h .
1493void Assembler::stop(const char* msg, Condition cond, int32_t code) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001494#ifndef __arm__
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001495 ASSERT(code >= kDefaultStopCode);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001496 {
1497 // The Simulator will handle the stop instruction and get the message
1498 // address. It expects to find the address just after the svc instruction.
1499 BlockConstPoolScope block_const_pool(this);
1500 if (code >= 0) {
1501 svc(kStopCode + code, cond);
1502 } else {
1503 svc(kStopCode + kMaxStopCode, cond);
1504 }
1505 emit(reinterpret_cast<Instr>(msg));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001506 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001507#else // def __arm__
1508#ifdef CAN_USE_ARMV5_INSTRUCTIONS
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001509 if (cond != al) {
1510 Label skip;
1511 b(&skip, NegateCondition(cond));
1512 bkpt(0);
1513 bind(&skip);
1514 } else {
1515 bkpt(0);
1516 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001517#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001518 svc(0x9f0001, cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001519#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1520#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521}
1522
1523
1524void Assembler::bkpt(uint32_t imm16) { // v5 and above
1525 ASSERT(is_uint16(imm16));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001526 emit(al | B24 | B21 | (imm16 >> 4)*B8 | BKPT | (imm16 & 0xf));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527}
1528
1529
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001530void Assembler::svc(uint32_t imm24, Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 ASSERT(is_uint24(imm24));
1532 emit(cond | 15*B24 | imm24);
1533}
1534
1535
ager@chromium.org5c838252010-02-19 08:53:10 +00001536// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537void Assembler::cdp(Coprocessor coproc,
1538 int opcode_1,
1539 CRegister crd,
1540 CRegister crn,
1541 CRegister crm,
1542 int opcode_2,
1543 Condition cond) {
1544 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1545 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1546 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1547}
1548
1549
1550void Assembler::cdp2(Coprocessor coproc,
1551 int opcode_1,
1552 CRegister crd,
1553 CRegister crn,
1554 CRegister crm,
1555 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001556 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557}
1558
1559
1560void Assembler::mcr(Coprocessor coproc,
1561 int opcode_1,
1562 Register rd,
1563 CRegister crn,
1564 CRegister crm,
1565 int opcode_2,
1566 Condition cond) {
1567 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1568 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1569 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1570}
1571
1572
1573void Assembler::mcr2(Coprocessor coproc,
1574 int opcode_1,
1575 Register rd,
1576 CRegister crn,
1577 CRegister crm,
1578 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001579 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580}
1581
1582
1583void Assembler::mrc(Coprocessor coproc,
1584 int opcode_1,
1585 Register rd,
1586 CRegister crn,
1587 CRegister crm,
1588 int opcode_2,
1589 Condition cond) {
1590 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1591 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1592 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1593}
1594
1595
1596void Assembler::mrc2(Coprocessor coproc,
1597 int opcode_1,
1598 Register rd,
1599 CRegister crn,
1600 CRegister crm,
1601 int opcode_2) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001602 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603}
1604
1605
1606void Assembler::ldc(Coprocessor coproc,
1607 CRegister crd,
1608 const MemOperand& src,
1609 LFlag l,
1610 Condition cond) {
1611 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1612}
1613
1614
1615void Assembler::ldc(Coprocessor coproc,
1616 CRegister crd,
1617 Register rn,
1618 int option,
1619 LFlag l,
1620 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001621 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 ASSERT(is_uint8(option));
1623 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1624 coproc*B8 | (option & 255));
1625}
1626
1627
1628void Assembler::ldc2(Coprocessor coproc,
1629 CRegister crd,
1630 const MemOperand& src,
1631 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001632 ldc(coproc, crd, src, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633}
1634
1635
1636void Assembler::ldc2(Coprocessor coproc,
1637 CRegister crd,
1638 Register rn,
1639 int option,
1640 LFlag l) { // v5 and above
ager@chromium.org378b34e2011-01-28 08:04:38 +00001641 ldc(coproc, crd, rn, option, l, kSpecialCondition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642}
1643
1644
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001645// Support for VFP.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001646
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001647void Assembler::vldr(const DwVfpRegister dst,
1648 const Register base,
1649 int offset,
1650 const Condition cond) {
1651 // Ddst = MEM(Rbase + offset).
1652 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001653 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001654 // Vdst(15-12) | 1011(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001655 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001656 int u = 1;
1657 if (offset < 0) {
1658 offset = -offset;
1659 u = 0;
1660 }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001661
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001662 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001663 if ((offset % 4) == 0 && (offset / 4) < 256) {
1664 emit(cond | u*B23 | 0xD1*B20 | base.code()*B16 | dst.code()*B12 |
1665 0xB*B8 | ((offset / 4) & 255));
1666 } else {
1667 // Larger offsets must be handled by computing the correct address
1668 // in the ip register.
1669 ASSERT(!base.is(ip));
1670 if (u == 1) {
1671 add(ip, base, Operand(offset));
1672 } else {
1673 sub(ip, base, Operand(offset));
1674 }
1675 emit(cond | 0xD1*B20 | ip.code()*B16 | dst.code()*B12 | 0xB*B8);
1676 }
1677}
1678
1679
1680void Assembler::vldr(const DwVfpRegister dst,
1681 const MemOperand& operand,
1682 const Condition cond) {
1683 ASSERT(!operand.rm().is_valid());
1684 ASSERT(operand.am_ == Offset);
1685 vldr(dst, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001686}
1687
1688
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001689void Assembler::vldr(const SwVfpRegister dst,
1690 const Register base,
1691 int offset,
1692 const Condition cond) {
1693 // Sdst = MEM(Rbase + offset).
1694 // Instruction details available in ARM DDI 0406A, A8-628.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001695 // cond(31-28) | 1101(27-24)| U001(23-20) | Rbase(19-16) |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001696 // Vdst(15-12) | 1010(11-8) | offset
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001697 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001698 int u = 1;
1699 if (offset < 0) {
1700 offset = -offset;
1701 u = 0;
1702 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001703 int sd, d;
1704 dst.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001705 ASSERT(offset >= 0);
1706
1707 if ((offset % 4) == 0 && (offset / 4) < 256) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001708 emit(cond | u*B23 | d*B22 | 0xD1*B20 | base.code()*B16 | sd*B12 |
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001709 0xA*B8 | ((offset / 4) & 255));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001710 } else {
1711 // Larger offsets must be handled by computing the correct address
1712 // in the ip register.
1713 ASSERT(!base.is(ip));
1714 if (u == 1) {
1715 add(ip, base, Operand(offset));
1716 } else {
1717 sub(ip, base, Operand(offset));
1718 }
1719 emit(cond | d*B22 | 0xD1*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
1720 }
1721}
1722
1723
1724void Assembler::vldr(const SwVfpRegister dst,
1725 const MemOperand& operand,
1726 const Condition cond) {
1727 ASSERT(!operand.rm().is_valid());
1728 ASSERT(operand.am_ == Offset);
1729 vldr(dst, operand.rn(), operand.offset(), cond);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001730}
1731
1732
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001733void Assembler::vstr(const DwVfpRegister src,
1734 const Register base,
1735 int offset,
1736 const Condition cond) {
1737 // MEM(Rbase + offset) = Dsrc.
1738 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001739 // cond(31-28) | 1101(27-24)| U000(23-20) | | Rbase(19-16) |
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001740 // Vsrc(15-12) | 1011(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001741 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001742 int u = 1;
1743 if (offset < 0) {
1744 offset = -offset;
1745 u = 0;
1746 }
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001747 ASSERT(offset >= 0);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001748 if ((offset % 4) == 0 && (offset / 4) < 256) {
1749 emit(cond | u*B23 | 0xD0*B20 | base.code()*B16 | src.code()*B12 |
1750 0xB*B8 | ((offset / 4) & 255));
1751 } else {
1752 // Larger offsets must be handled by computing the correct address
1753 // in the ip register.
1754 ASSERT(!base.is(ip));
1755 if (u == 1) {
1756 add(ip, base, Operand(offset));
1757 } else {
1758 sub(ip, base, Operand(offset));
1759 }
1760 emit(cond | 0xD0*B20 | ip.code()*B16 | src.code()*B12 | 0xB*B8);
1761 }
1762}
1763
1764
1765void Assembler::vstr(const DwVfpRegister src,
1766 const MemOperand& operand,
1767 const Condition cond) {
1768 ASSERT(!operand.rm().is_valid());
1769 ASSERT(operand.am_ == Offset);
1770 vstr(src, operand.rn(), operand.offset(), cond);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001771}
1772
1773
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001774void Assembler::vstr(const SwVfpRegister src,
1775 const Register base,
1776 int offset,
1777 const Condition cond) {
1778 // MEM(Rbase + offset) = SSrc.
1779 // Instruction details available in ARM DDI 0406A, A8-786.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001780 // cond(31-28) | 1101(27-24)| U000(23-20) | Rbase(19-16) |
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001781 // Vdst(15-12) | 1010(11-8) | (offset/4)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001782 ASSERT(CpuFeatures::IsEnabled(VFP3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001783 int u = 1;
1784 if (offset < 0) {
1785 offset = -offset;
1786 u = 0;
1787 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001788 int sd, d;
1789 src.split_code(&sd, &d);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001790 ASSERT(offset >= 0);
1791 if ((offset % 4) == 0 && (offset / 4) < 256) {
1792 emit(cond | u*B23 | d*B22 | 0xD0*B20 | base.code()*B16 | sd*B12 |
1793 0xA*B8 | ((offset / 4) & 255));
1794 } else {
1795 // Larger offsets must be handled by computing the correct address
1796 // in the ip register.
1797 ASSERT(!base.is(ip));
1798 if (u == 1) {
1799 add(ip, base, Operand(offset));
1800 } else {
1801 sub(ip, base, Operand(offset));
1802 }
1803 emit(cond | d*B22 | 0xD0*B20 | ip.code()*B16 | sd*B12 | 0xA*B8);
1804 }
1805}
1806
1807
1808void Assembler::vstr(const SwVfpRegister src,
1809 const MemOperand& operand,
1810 const Condition cond) {
1811 ASSERT(!operand.rm().is_valid());
1812 ASSERT(operand.am_ == Offset);
1813 vldr(src, operand.rn(), operand.offset(), cond);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001814}
1815
1816
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001817void Assembler::vldm(BlockAddrMode am,
1818 Register base,
1819 DwVfpRegister first,
1820 DwVfpRegister last,
1821 Condition cond) {
1822 // Instruction details available in ARM DDI 0406A, A8-626.
1823 // cond(31-28) | 110(27-25)| PUDW1(24-20) | Rbase(19-16) |
1824 // first(15-12) | 1010(11-8) | (count * 2)
1825 ASSERT(CpuFeatures::IsEnabled(VFP3));
1826 ASSERT_LE(first.code(), last.code());
1827 ASSERT(am == ia || am == ia_w || am == db_w);
1828 ASSERT(!base.is(pc));
1829
1830 int sd, d;
1831 first.split_code(&sd, &d);
1832 int count = last.code() - first.code() + 1;
1833 emit(cond | B27 | B26 | am | d*B22 | B20 | base.code()*B16 | sd*B12 |
1834 0xB*B8 | count*2);
1835}
1836
1837
1838void Assembler::vstm(BlockAddrMode am,
1839 Register base,
1840 DwVfpRegister first,
1841 DwVfpRegister last,
1842 Condition cond) {
1843 // Instruction details available in ARM DDI 0406A, A8-784.
1844 // cond(31-28) | 110(27-25)| PUDW0(24-20) | Rbase(19-16) |
1845 // first(15-12) | 1011(11-8) | (count * 2)
1846 ASSERT(CpuFeatures::IsEnabled(VFP3));
1847 ASSERT_LE(first.code(), last.code());
1848 ASSERT(am == ia || am == ia_w || am == db_w);
1849 ASSERT(!base.is(pc));
1850
1851 int sd, d;
1852 first.split_code(&sd, &d);
1853 int count = last.code() - first.code() + 1;
1854 emit(cond | B27 | B26 | am | d*B22 | base.code()*B16 | sd*B12 |
1855 0xB*B8 | count*2);
1856}
1857
1858void Assembler::vldm(BlockAddrMode am,
1859 Register base,
1860 SwVfpRegister first,
1861 SwVfpRegister last,
1862 Condition cond) {
1863 // Instruction details available in ARM DDI 0406A, A8-626.
1864 // cond(31-28) | 110(27-25)| PUDW1(24-20) | Rbase(19-16) |
1865 // first(15-12) | 1010(11-8) | (count/2)
1866 ASSERT(CpuFeatures::IsEnabled(VFP3));
1867 ASSERT_LE(first.code(), last.code());
1868 ASSERT(am == ia || am == ia_w || am == db_w);
1869 ASSERT(!base.is(pc));
1870
1871 int sd, d;
1872 first.split_code(&sd, &d);
1873 int count = last.code() - first.code() + 1;
1874 emit(cond | B27 | B26 | am | d*B22 | B20 | base.code()*B16 | sd*B12 |
1875 0xA*B8 | count);
1876}
1877
1878
1879void Assembler::vstm(BlockAddrMode am,
1880 Register base,
1881 SwVfpRegister first,
1882 SwVfpRegister last,
1883 Condition cond) {
1884 // Instruction details available in ARM DDI 0406A, A8-784.
1885 // cond(31-28) | 110(27-25)| PUDW0(24-20) | Rbase(19-16) |
1886 // first(15-12) | 1011(11-8) | (count/2)
1887 ASSERT(CpuFeatures::IsEnabled(VFP3));
1888 ASSERT_LE(first.code(), last.code());
1889 ASSERT(am == ia || am == ia_w || am == db_w);
1890 ASSERT(!base.is(pc));
1891
1892 int sd, d;
1893 first.split_code(&sd, &d);
1894 int count = last.code() - first.code() + 1;
1895 emit(cond | B27 | B26 | am | d*B22 | base.code()*B16 | sd*B12 |
1896 0xA*B8 | count);
1897}
1898
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001899static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
1900 uint64_t i;
1901 memcpy(&i, &d, 8);
1902
1903 *lo = i & 0xffffffff;
1904 *hi = i >> 32;
1905}
1906
1907// Only works for little endian floating point formats.
1908// We don't support VFP on the mixed endian floating point platform.
1909static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001910 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001911
1912 // VMOV can accept an immediate of the form:
1913 //
1914 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
1915 //
1916 // The immediate is encoded using an 8-bit quantity, comprised of two
1917 // 4-bit fields. For an 8-bit immediate of the form:
1918 //
1919 // [abcdefgh]
1920 //
1921 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
1922 // created of the form:
1923 //
1924 // [aBbbbbbb,bbcdefgh,00000000,00000000,
1925 // 00000000,00000000,00000000,00000000]
1926 //
1927 // where B = ~b.
1928 //
1929
1930 uint32_t lo, hi;
1931 DoubleAsTwoUInt32(d, &lo, &hi);
1932
1933 // The most obvious constraint is the long block of zeroes.
1934 if ((lo != 0) || ((hi & 0xffff) != 0)) {
1935 return false;
1936 }
1937
1938 // Bits 62:55 must be all clear or all set.
1939 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
1940 return false;
1941 }
1942
1943 // Bit 63 must be NOT bit 62.
1944 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
1945 return false;
1946 }
1947
1948 // Create the encoded immediate in the form:
1949 // [00000000,0000abcd,00000000,0000efgh]
1950 *encoding = (hi >> 16) & 0xf; // Low nybble.
1951 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
1952 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
1953
1954 return true;
1955}
1956
1957
1958void Assembler::vmov(const DwVfpRegister dst,
1959 double imm,
1960 const Condition cond) {
1961 // Dd = immediate
1962 // Instruction details available in ARM DDI 0406B, A8-640.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001963 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001964
1965 uint32_t enc;
1966 if (FitsVMOVDoubleImmediate(imm, &enc)) {
1967 // The double can be encoded in the instruction.
1968 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
1969 } else {
1970 // Synthesise the double from ARM immediates. This could be implemented
1971 // using vldr from a constant pool.
1972 uint32_t lo, hi;
1973 DoubleAsTwoUInt32(imm, &lo, &hi);
1974
1975 if (lo == hi) {
1976 // If the lo and hi parts of the double are equal, the literal is easier
1977 // to create. This is the case with 0.0.
1978 mov(ip, Operand(lo));
1979 vmov(dst, ip, ip);
1980 } else {
1981 // Move the low part of the double into the lower of the corresponsing S
1982 // registers of D register dst.
1983 mov(ip, Operand(lo));
1984 vmov(dst.low(), ip, cond);
1985
1986 // Move the high part of the double into the higher of the corresponsing S
1987 // registers of D register dst.
1988 mov(ip, Operand(hi));
1989 vmov(dst.high(), ip, cond);
1990 }
1991 }
1992}
1993
1994
1995void Assembler::vmov(const SwVfpRegister dst,
1996 const SwVfpRegister src,
1997 const Condition cond) {
1998 // Sd = Sm
1999 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002000 ASSERT(CpuFeatures::IsEnabled(VFP3));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002001 int sd, d, sm, m;
2002 dst.split_code(&sd, &d);
2003 src.split_code(&sm, &m);
2004 emit(cond | 0xE*B24 | d*B22 | 0xB*B20 | sd*B12 | 0xA*B8 | B6 | m*B5 | sm);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002005}
2006
2007
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002008void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002009 const DwVfpRegister src,
2010 const Condition cond) {
2011 // Dd = Dm
2012 // Instruction details available in ARM DDI 0406B, A8-642.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002013 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002014 emit(cond | 0xE*B24 | 0xB*B20 |
2015 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
2016}
2017
2018
2019void Assembler::vmov(const DwVfpRegister dst,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002020 const Register src1,
2021 const Register src2,
2022 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002023 // Dm = <Rt,Rt2>.
2024 // Instruction details available in ARM DDI 0406A, A8-646.
2025 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
2026 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002027 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002028 ASSERT(!src1.is(pc) && !src2.is(pc));
2029 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
2030 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
2031}
2032
2033
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002034void Assembler::vmov(const Register dst1,
2035 const Register dst2,
2036 const DwVfpRegister src,
2037 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002038 // <Rt,Rt2> = Dm.
2039 // Instruction details available in ARM DDI 0406A, A8-646.
2040 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
2041 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002042 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002043 ASSERT(!dst1.is(pc) && !dst2.is(pc));
2044 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
2045 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
2046}
2047
2048
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002049void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002050 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002051 const Condition cond) {
2052 // Sn = Rt.
2053 // Instruction details available in ARM DDI 0406A, A8-642.
2054 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
2055 // 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 +00002056 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002057 ASSERT(!src.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002058 int sn, n;
2059 dst.split_code(&sn, &n);
2060 emit(cond | 0xE*B24 | sn*B16 | src.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002061}
2062
2063
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002064void Assembler::vmov(const Register dst,
2065 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002066 const Condition cond) {
2067 // Rt = Sn.
2068 // Instruction details available in ARM DDI 0406A, A8-642.
2069 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
2070 // 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 +00002071 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002072 ASSERT(!dst.is(pc));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002073 int sn, n;
2074 src.split_code(&sn, &n);
2075 emit(cond | 0xE*B24 | B20 | sn*B16 | dst.code()*B12 | 0xA*B8 | n*B7 | B4);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002076}
2077
2078
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002079// Type of data to read from or write to VFP register.
2080// Used as specifier in generic vcvt instruction.
2081enum VFPType { S32, U32, F32, F64 };
2082
2083
2084static bool IsSignedVFPType(VFPType type) {
2085 switch (type) {
2086 case S32:
2087 return true;
2088 case U32:
2089 return false;
2090 default:
2091 UNREACHABLE();
2092 return false;
2093 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002094}
2095
2096
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002097static bool IsIntegerVFPType(VFPType type) {
2098 switch (type) {
2099 case S32:
2100 case U32:
2101 return true;
2102 case F32:
2103 case F64:
2104 return false;
2105 default:
2106 UNREACHABLE();
2107 return false;
2108 }
2109}
2110
2111
2112static bool IsDoubleVFPType(VFPType type) {
2113 switch (type) {
2114 case F32:
2115 return false;
2116 case F64:
2117 return true;
2118 default:
2119 UNREACHABLE();
2120 return false;
2121 }
2122}
2123
2124
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002125// Split five bit reg_code based on size of reg_type.
2126// 32-bit register codes are Vm:M
2127// 64-bit register codes are M:Vm
2128// where Vm is four bits, and M is a single bit.
2129static void SplitRegCode(VFPType reg_type,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002130 int reg_code,
2131 int* vm,
2132 int* m) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002133 ASSERT((reg_code >= 0) && (reg_code <= 31));
2134 if (IsIntegerVFPType(reg_type) || !IsDoubleVFPType(reg_type)) {
2135 // 32 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002136 *m = reg_code & 0x1;
2137 *vm = reg_code >> 1;
2138 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002139 // 64 bit type.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002140 *m = (reg_code & 0x10) >> 4;
2141 *vm = reg_code & 0x0F;
2142 }
2143}
2144
2145
2146// Encode vcvt.src_type.dst_type instruction.
2147static Instr EncodeVCVT(const VFPType dst_type,
2148 const int dst_code,
2149 const VFPType src_type,
2150 const int src_code,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002151 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002152 const Condition cond) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002153 ASSERT(src_type != dst_type);
2154 int D, Vd, M, Vm;
2155 SplitRegCode(src_type, src_code, &Vm, &M);
2156 SplitRegCode(dst_type, dst_code, &Vd, &D);
2157
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002158 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2159 // Conversion between IEEE floating point and 32-bit integer.
2160 // Instruction details available in ARM DDI 0406B, A8.6.295.
2161 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2162 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2163 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2164
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002165 int sz, opc2, op;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002166
2167 if (IsIntegerVFPType(dst_type)) {
2168 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2169 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002170 op = mode;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002171 } else {
2172 ASSERT(IsIntegerVFPType(src_type));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002173 opc2 = 0x0;
2174 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2175 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002176 }
2177
2178 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2179 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2180 } else {
2181 // Conversion between IEEE double and single precision.
2182 // Instruction details available in ARM DDI 0406B, A8.6.298.
2183 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2184 // 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 +00002185 int sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002186 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2187 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2188 }
2189}
2190
2191
2192void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2193 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002194 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002195 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002196 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002197 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002198}
2199
2200
2201void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2202 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002203 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002204 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002205 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002206 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002207}
2208
2209
2210void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2211 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002212 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002213 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002214 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002215 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002216}
2217
2218
2219void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2220 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002221 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002222 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002223 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002224 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002225}
2226
2227
2228void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2229 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002230 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002231 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002232 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002233 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002234}
2235
2236
2237void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2238 const SwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002239 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002240 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002241 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002242 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), mode, cond));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002243}
2244
2245
2246void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2247 const DwVfpRegister src,
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002248 VFPConversionMode mode,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002249 const Condition cond) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002250 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002251 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), mode, cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002252}
2253
2254
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002255void Assembler::vneg(const DwVfpRegister dst,
2256 const DwVfpRegister src,
2257 const Condition cond) {
2258 emit(cond | 0xE*B24 | 0xB*B20 | B16 | dst.code()*B12 |
2259 0x5*B9 | B8 | B6 | src.code());
2260}
2261
2262
whesse@chromium.org7a392b32011-01-31 11:30:36 +00002263void Assembler::vabs(const DwVfpRegister dst,
2264 const DwVfpRegister src,
2265 const Condition cond) {
2266 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 |
2267 0x5*B9 | B8 | 0x3*B6 | src.code());
2268}
2269
2270
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002271void Assembler::vadd(const DwVfpRegister dst,
2272 const DwVfpRegister src1,
2273 const DwVfpRegister src2,
2274 const Condition cond) {
2275 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002276 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2277 // Instruction details available in ARM DDI 0406A, A8-536.
2278 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2279 // 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 +00002280 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002281 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2282 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2283}
2284
2285
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002286void Assembler::vsub(const DwVfpRegister dst,
2287 const DwVfpRegister src1,
2288 const DwVfpRegister src2,
2289 const Condition cond) {
2290 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002291 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2292 // Instruction details available in ARM DDI 0406A, A8-784.
2293 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2294 // 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 +00002295 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002296 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2297 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2298}
2299
2300
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002301void Assembler::vmul(const DwVfpRegister dst,
2302 const DwVfpRegister src1,
2303 const DwVfpRegister src2,
2304 const Condition cond) {
2305 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002306 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2307 // Instruction details available in ARM DDI 0406A, A8-784.
2308 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2309 // 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 +00002310 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002311 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2312 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2313}
2314
2315
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002316void Assembler::vdiv(const DwVfpRegister dst,
2317 const DwVfpRegister src1,
2318 const DwVfpRegister src2,
2319 const Condition cond) {
2320 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002321 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2322 // Instruction details available in ARM DDI 0406A, A8-584.
2323 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2324 // 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 +00002325 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002326 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2327 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2328}
2329
2330
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002331void Assembler::vcmp(const DwVfpRegister src1,
2332 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002333 const Condition cond) {
2334 // vcmp(Dd, Dm) double precision floating point comparison.
2335 // Instruction details available in ARM DDI 0406A, A8-570.
2336 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002337 // 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 +00002338 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002339 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002340 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002341}
2342
2343
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002344void Assembler::vcmp(const DwVfpRegister src1,
2345 const double src2,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002346 const Condition cond) {
2347 // vcmp(Dd, Dm) double precision floating point comparison.
2348 // Instruction details available in ARM DDI 0406A, A8-570.
2349 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0101 (19-16) |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002350 // 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 +00002351 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002352 ASSERT(src2 == 0.0);
2353 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 | B16 |
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002354 src1.code()*B12 | 0x5*B9 | B8 | B6);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002355}
2356
2357
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002358void Assembler::vmsr(Register dst, Condition cond) {
2359 // Instruction details available in ARM DDI 0406A, A8-652.
2360 // cond(31-28) | 1110 (27-24) | 1110(23-20)| 0001 (19-16) |
2361 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002362 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002363 emit(cond | 0xE*B24 | 0xE*B20 | B16 |
2364 dst.code()*B12 | 0xA*B8 | B4);
2365}
2366
2367
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002368void Assembler::vmrs(Register dst, Condition cond) {
2369 // Instruction details available in ARM DDI 0406A, A8-652.
2370 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2371 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002372 ASSERT(CpuFeatures::IsEnabled(VFP3));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002373 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2374 dst.code()*B12 | 0xA*B8 | B4);
2375}
2376
2377
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002378void Assembler::vsqrt(const DwVfpRegister dst,
2379 const DwVfpRegister src,
2380 const Condition cond) {
2381 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2382 // 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 +00002383 ASSERT(CpuFeatures::IsEnabled(VFP3));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00002384 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2385 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2386}
2387
2388
ager@chromium.org5c838252010-02-19 08:53:10 +00002389// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002390void Assembler::nop(int type) {
2391 // This is mov rx, rx.
2392 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2393 emit(al | 13*B21 | type*B12 | type);
2394}
2395
2396
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002397bool Assembler::IsNop(Instr instr, int type) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002398 // Check for mov rx, rx where x = type.
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002399 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2400 return instr == (al | 13*B21 | type*B12 | type);
2401}
2402
2403
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002404bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2405 uint32_t dummy1;
2406 uint32_t dummy2;
2407 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2408}
2409
2410
ager@chromium.org5c838252010-02-19 08:53:10 +00002411// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002412void Assembler::RecordJSReturn() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002413 positions_recorder()->WriteRecordedPositions();
ager@chromium.org4af710e2009-09-15 12:20:11 +00002414 CheckBuffer();
2415 RecordRelocInfo(RelocInfo::JS_RETURN);
2416}
2417
2418
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002419void Assembler::RecordDebugBreakSlot() {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002420 positions_recorder()->WriteRecordedPositions();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002421 CheckBuffer();
2422 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2423}
2424
2425
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426void Assembler::RecordComment(const char* msg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002427 if (FLAG_code_comments) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00002429 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430 }
2431}
2432
2433
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434void Assembler::GrowBuffer() {
2435 if (!own_buffer_) FATAL("external code buffer is too small");
2436
ager@chromium.org5c838252010-02-19 08:53:10 +00002437 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 CodeDesc desc; // the new buffer
2439 if (buffer_size_ < 4*KB) {
2440 desc.buffer_size = 4*KB;
2441 } else if (buffer_size_ < 1*MB) {
2442 desc.buffer_size = 2*buffer_size_;
2443 } else {
2444 desc.buffer_size = buffer_size_ + 1*MB;
2445 }
2446 CHECK_GT(desc.buffer_size, 0); // no overflow
2447
ager@chromium.org5c838252010-02-19 08:53:10 +00002448 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 desc.buffer = NewArray<byte>(desc.buffer_size);
2450
2451 desc.instr_size = pc_offset();
2452 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2453
ager@chromium.org5c838252010-02-19 08:53:10 +00002454 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 int pc_delta = desc.buffer - buffer_;
2456 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2457 memmove(desc.buffer, buffer_, desc.instr_size);
2458 memmove(reloc_info_writer.pos() + rc_delta,
2459 reloc_info_writer.pos(), desc.reloc_size);
2460
ager@chromium.org5c838252010-02-19 08:53:10 +00002461 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 DeleteArray(buffer_);
2463 buffer_ = desc.buffer;
2464 buffer_size_ = desc.buffer_size;
2465 pc_ += pc_delta;
2466 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2467 reloc_info_writer.last_pc() + pc_delta);
2468
ager@chromium.org5c838252010-02-19 08:53:10 +00002469 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00002471 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472
ager@chromium.org5c838252010-02-19 08:53:10 +00002473 // Relocate pending relocation entries.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002474 for (int i = 0; i < num_pending_reloc_info_; i++) {
2475 RelocInfo& rinfo = pending_reloc_info_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002476 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2477 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00002478 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2479 rinfo.set_pc(rinfo.pc() + pc_delta);
2480 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 }
2482}
2483
2484
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002485void Assembler::db(uint8_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002486 // No relocation info should be pending while using db. db is used
2487 // to write pure data with no pointers and the constant pool should
2488 // be emitted before using db.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002489 ASSERT(num_pending_reloc_info_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002490 CheckBuffer();
2491 *reinterpret_cast<uint8_t*>(pc_) = data;
2492 pc_ += sizeof(uint8_t);
2493}
2494
2495
2496void Assembler::dd(uint32_t data) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002497 // No relocation info should be pending while using dd. dd is used
2498 // to write pure data with no pointers and the constant pool should
2499 // be emitted before using dd.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002500 ASSERT(num_pending_reloc_info_ == 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002501 CheckBuffer();
2502 *reinterpret_cast<uint32_t*>(pc_) = data;
2503 pc_ += sizeof(uint32_t);
2504}
2505
2506
ager@chromium.org236ad962008-09-25 09:45:57 +00002507void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002509 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002510 // Adjust code for new modes.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002511 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2512 || RelocInfo::IsJSReturn(rmode)
ager@chromium.org4af710e2009-09-15 12:20:11 +00002513 || RelocInfo::IsComment(rmode)
2514 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00002515 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 } else {
whesse@chromium.org7b260152011-06-20 15:33:18 +00002517 ASSERT(num_pending_reloc_info_ < kMaxNumPendingRelocInfo);
2518 if (num_pending_reloc_info_ == 0) {
2519 first_const_pool_use_ = pc_offset();
2520 }
2521 pending_reloc_info_[num_pending_reloc_info_++] = rinfo;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00002523 // instruction for which we just recorded relocation info.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002524 BlockConstPoolFor(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 }
ager@chromium.org236ad962008-09-25 09:45:57 +00002526 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002527 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002528 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2529#ifdef DEBUG
2530 if (!Serializer::enabled()) {
2531 Serializer::TooLateToEnableNow();
2532 }
2533#endif
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002534 if (!Serializer::enabled() && !emit_debug_code()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002535 return;
2536 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002537 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002539 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
2540 ASSERT(ast_id_for_reloc_info_ != kNoASTId);
2541 RelocInfo reloc_info_with_ast_id(pc_, rmode, ast_id_for_reloc_info_);
2542 ast_id_for_reloc_info_ = kNoASTId;
2543 reloc_info_writer.Write(&reloc_info_with_ast_id);
2544 } else {
2545 reloc_info_writer.Write(&rinfo);
2546 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547 }
2548}
2549
2550
whesse@chromium.org7b260152011-06-20 15:33:18 +00002551void Assembler::BlockConstPoolFor(int instructions) {
2552 int pc_limit = pc_offset() + instructions * kInstrSize;
2553 if (no_const_pool_before_ < pc_limit) {
2554 // If there are some pending entries, the constant pool cannot be blocked
2555 // further than first_const_pool_use_ + kMaxDistToPool
2556 ASSERT((num_pending_reloc_info_ == 0) ||
2557 (pc_limit < (first_const_pool_use_ + kMaxDistToPool)));
2558 no_const_pool_before_ = pc_limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 }
2560
whesse@chromium.org7b260152011-06-20 15:33:18 +00002561 if (next_buffer_check_ < no_const_pool_before_) {
2562 next_buffer_check_ = no_const_pool_before_;
2563 }
2564}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566
whesse@chromium.org7b260152011-06-20 15:33:18 +00002567void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2568 // Some short sequence of instruction mustn't be broken up by constant pool
2569 // emission, such sequences are protected by calls to BlockConstPoolFor and
2570 // BlockConstPoolScope.
2571 if (is_const_pool_blocked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002572 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 ASSERT(!force_emit);
2574 return;
2575 }
2576
whesse@chromium.org7b260152011-06-20 15:33:18 +00002577 // There is nothing to do if there are no pending constant pool entries.
2578 if (num_pending_reloc_info_ == 0) {
2579 // Calculate the offset of the next check.
2580 next_buffer_check_ = pc_offset() + kCheckPoolInterval;
2581 return;
2582 }
2583
2584 // We emit a constant pool when:
2585 // * requested to do so by parameter force_emit (e.g. after each function).
2586 // * the distance to the first instruction accessing the constant pool is
2587 // kAvgDistToPool or more.
2588 // * no jump is required and the distance to the first instruction accessing
2589 // the constant pool is at least kMaxDistToPool / 2.
2590 ASSERT(first_const_pool_use_ >= 0);
2591 int dist = pc_offset() - first_const_pool_use_;
2592 if (!force_emit && dist < kAvgDistToPool &&
2593 (require_jump || (dist < (kMaxDistToPool / 2)))) {
2594 return;
2595 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596
2597 // Check that the code buffer is large enough before emitting the constant
whesse@chromium.org7b260152011-06-20 15:33:18 +00002598 // pool (include the jump over the pool and the constant pool marker and
2599 // the gap to the relocation information).
2600 int jump_instr = require_jump ? kInstrSize : 0;
2601 int needed_space = jump_instr + kInstrSize +
2602 num_pending_reloc_info_ * kInstrSize + kGap;
2603 while (buffer_space() <= needed_space) GrowBuffer();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604
whesse@chromium.org7b260152011-06-20 15:33:18 +00002605 {
2606 // Block recursive calls to CheckConstPool.
2607 BlockConstPoolScope block_const_pool(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608
whesse@chromium.org7b260152011-06-20 15:33:18 +00002609 // Emit jump over constant pool if necessary.
2610 Label after_pool;
2611 if (require_jump) {
2612 b(&after_pool);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614
whesse@chromium.org7b260152011-06-20 15:33:18 +00002615 RecordComment("[ Constant Pool");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616
whesse@chromium.org7b260152011-06-20 15:33:18 +00002617 // Put down constant pool marker "Undefined instruction" as specified by
2618 // A5.6 (ARMv7) Instruction set encoding.
2619 emit(kConstantPoolMarker | num_pending_reloc_info_);
2620
2621 // Emit constant pool entries.
2622 for (int i = 0; i < num_pending_reloc_info_; i++) {
2623 RelocInfo& rinfo = pending_reloc_info_[i];
2624 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2625 rinfo.rmode() != RelocInfo::POSITION &&
2626 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
2627
2628 Instr instr = instr_at(rinfo.pc());
2629 // Instruction to patch must be 'ldr rd, [pc, #offset]' with offset == 0.
2630 ASSERT(IsLdrPcImmediateOffset(instr) &&
2631 GetLdrRegisterImmediateOffset(instr) == 0);
2632
2633 int delta = pc_ - rinfo.pc() - kPcLoadDelta;
2634 // 0 is the smallest delta:
2635 // ldr rd, [pc, #0]
2636 // constant pool marker
2637 // data
2638 ASSERT(is_uint12(delta));
2639
2640 instr_at_put(rinfo.pc(), SetLdrRegisterImmediateOffset(instr, delta));
2641 emit(rinfo.data());
2642 }
2643
2644 num_pending_reloc_info_ = 0;
2645 first_const_pool_use_ = -1;
2646
2647 RecordComment("]");
2648
2649 if (after_pool.is_linked()) {
2650 bind(&after_pool);
2651 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 }
2653
2654 // Since a constant pool was just emitted, move the check offset forward by
2655 // the standard interval.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002656 next_buffer_check_ = pc_offset() + kCheckPoolInterval;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657}
2658
2659
2660} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002661
2662#endif // V8_TARGET_ARCH_ARM