blob: 0dc6b7710652258b451538adfb5eccf538eb861f [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
3//
4// 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.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// 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
Leon Clarked91b9f72010-01-27 17:25:45 +000033// The original source code covered by the above license above has been
34// modified significantly by Google Inc.
35// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37#include "v8.h"
38
Leon Clarkef7060e22010-06-03 12:02:55 +010039#if defined(V8_TARGET_ARCH_ARM)
40
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "arm/assembler-arm-inl.h"
42#include "serialize.h"
43
44namespace v8 {
45namespace internal {
46
Steve Blockd0582a62009-12-15 09:54:21 +000047// Safe default is no features.
48unsigned CpuFeatures::supported_ = 0;
49unsigned CpuFeatures::enabled_ = 0;
50unsigned CpuFeatures::found_by_runtime_probing_ = 0;
51
Andrei Popescu402d9372010-02-26 13:31:12 +000052
53#ifdef __arm__
54static uint64_t CpuFeaturesImpliedByCompiler() {
55 uint64_t answer = 0;
56#ifdef CAN_USE_ARMV7_INSTRUCTIONS
57 answer |= 1u << ARMv7;
58#endif // def CAN_USE_ARMV7_INSTRUCTIONS
59 // If the compiler is allowed to use VFP then we can use VFP too in our code
60 // generation even when generating snapshots. This won't work for cross
61 // compilation.
62#if defined(__VFP_FP__) && !defined(__SOFTFP__)
63 answer |= 1u << VFP3;
64#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
65#ifdef CAN_USE_VFP_INSTRUCTIONS
66 answer |= 1u << VFP3;
67#endif // def CAN_USE_VFP_INSTRUCTIONS
68 return answer;
69}
70#endif // def __arm__
71
72
Steve Blockd0582a62009-12-15 09:54:21 +000073void CpuFeatures::Probe() {
Andrei Popescu402d9372010-02-26 13:31:12 +000074#ifndef __arm__
Andrei Popescu31002712010-02-23 13:46:05 +000075 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled.
76 if (FLAG_enable_vfp3) {
Steve Block6ded16b2010-05-10 14:33:55 +010077 supported_ |= 1u << VFP3;
Andrei Popescu31002712010-02-23 13:46:05 +000078 }
79 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
80 if (FLAG_enable_armv7) {
Steve Block6ded16b2010-05-10 14:33:55 +010081 supported_ |= 1u << ARMv7;
Andrei Popescu31002712010-02-23 13:46:05 +000082 }
Andrei Popescu402d9372010-02-26 13:31:12 +000083#else // def __arm__
Steve Blockd0582a62009-12-15 09:54:21 +000084 if (Serializer::enabled()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000085 supported_ |= OS::CpuFeaturesImpliedByPlatform();
86 supported_ |= CpuFeaturesImpliedByCompiler();
Steve Blockd0582a62009-12-15 09:54:21 +000087 return; // No features if we might serialize.
88 }
89
90 if (OS::ArmCpuHasFeature(VFP3)) {
91 // This implementation also sets the VFP flags if
92 // runtime detection of VFP returns true.
93 supported_ |= 1u << VFP3;
94 found_by_runtime_probing_ |= 1u << VFP3;
95 }
Andrei Popescu31002712010-02-23 13:46:05 +000096
97 if (OS::ArmCpuHasFeature(ARMv7)) {
98 supported_ |= 1u << ARMv7;
99 found_by_runtime_probing_ |= 1u << ARMv7;
100 }
Steve Block6ded16b2010-05-10 14:33:55 +0100101#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000102}
103
104
Steve Blocka7e24c12009-10-30 11:49:00 +0000105// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000106// Implementation of RelocInfo
107
108const int RelocInfo::kApplyMask = 0;
109
110
Leon Clarkef7060e22010-06-03 12:02:55 +0100111bool RelocInfo::IsCodedSpecially() {
112 // The deserializer needs to know whether a pointer is specially coded. Being
113 // specially coded on ARM means that it is a movw/movt instruction. We don't
114 // generate those yet.
115 return false;
116}
117
118
119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
121 // Patch the code at the current address with the supplied instructions.
122 Instr* pc = reinterpret_cast<Instr*>(pc_);
123 Instr* instr = reinterpret_cast<Instr*>(instructions);
124 for (int i = 0; i < instruction_count; i++) {
125 *(pc + i) = *(instr + i);
126 }
127
128 // Indicate that code has changed.
129 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
130}
131
132
133// Patch the code at the current PC with a call to the target address.
134// Additional guard instructions can be added if required.
135void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
136 // Patch the code at the current address with a call to the target.
137 UNIMPLEMENTED();
138}
139
140
141// -----------------------------------------------------------------------------
142// Implementation of Operand and MemOperand
143// See assembler-arm-inl.h for inlined constructors
144
145Operand::Operand(Handle<Object> handle) {
146 rm_ = no_reg;
147 // Verify all Objects referred by code are NOT in new space.
148 Object* obj = *handle;
149 ASSERT(!Heap::InNewSpace(obj));
150 if (obj->IsHeapObject()) {
151 imm32_ = reinterpret_cast<intptr_t>(handle.location());
152 rmode_ = RelocInfo::EMBEDDED_OBJECT;
153 } else {
154 // no relocation needed
155 imm32_ = reinterpret_cast<intptr_t>(obj);
156 rmode_ = RelocInfo::NONE;
157 }
158}
159
160
161Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
162 ASSERT(is_uint5(shift_imm));
163 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
164 rm_ = rm;
165 rs_ = no_reg;
166 shift_op_ = shift_op;
167 shift_imm_ = shift_imm & 31;
168 if (shift_op == RRX) {
169 // encoded as ROR with shift_imm == 0
170 ASSERT(shift_imm == 0);
171 shift_op_ = ROR;
172 shift_imm_ = 0;
173 }
174}
175
176
177Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
178 ASSERT(shift_op != RRX);
179 rm_ = rm;
180 rs_ = no_reg;
181 shift_op_ = shift_op;
182 rs_ = rs;
183}
184
185
186MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
187 rn_ = rn;
188 rm_ = no_reg;
189 offset_ = offset;
190 am_ = am;
191}
192
193MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
194 rn_ = rn;
195 rm_ = rm;
196 shift_op_ = LSL;
197 shift_imm_ = 0;
198 am_ = am;
199}
200
201
202MemOperand::MemOperand(Register rn, Register rm,
203 ShiftOp shift_op, int shift_imm, AddrMode am) {
204 ASSERT(is_uint5(shift_imm));
205 rn_ = rn;
206 rm_ = rm;
207 shift_op_ = shift_op;
208 shift_imm_ = shift_imm & 31;
209 am_ = am;
210}
211
212
213// -----------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000214// Implementation of Assembler.
Steve Blocka7e24c12009-10-30 11:49:00 +0000215
Andrei Popescu31002712010-02-23 13:46:05 +0000216// Instruction encoding bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000217enum {
218 H = 1 << 5, // halfword (or byte)
219 S6 = 1 << 6, // signed (or unsigned)
220 L = 1 << 20, // load (or store)
221 S = 1 << 20, // set condition code (or leave unchanged)
222 W = 1 << 21, // writeback base register (or leave unchanged)
223 A = 1 << 21, // accumulate in multiply instruction (or not)
224 B = 1 << 22, // unsigned byte (or word)
225 N = 1 << 22, // long (or short)
226 U = 1 << 23, // positive (or negative) offset/index
227 P = 1 << 24, // offset/pre-indexed addressing (or post-indexed addressing)
228 I = 1 << 25, // immediate shifter operand (or not)
229
230 B4 = 1 << 4,
231 B5 = 1 << 5,
Steve Blockd0582a62009-12-15 09:54:21 +0000232 B6 = 1 << 6,
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 B7 = 1 << 7,
234 B8 = 1 << 8,
Steve Blockd0582a62009-12-15 09:54:21 +0000235 B9 = 1 << 9,
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 B12 = 1 << 12,
237 B16 = 1 << 16,
Steve Blockd0582a62009-12-15 09:54:21 +0000238 B18 = 1 << 18,
239 B19 = 1 << 19,
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 B20 = 1 << 20,
241 B21 = 1 << 21,
242 B22 = 1 << 22,
243 B23 = 1 << 23,
244 B24 = 1 << 24,
245 B25 = 1 << 25,
246 B26 = 1 << 26,
247 B27 = 1 << 27,
248
Andrei Popescu31002712010-02-23 13:46:05 +0000249 // Instruction bit masks.
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 RdMask = 15 << 12, // in str instruction
251 CondMask = 15 << 28,
252 CoprocessorMask = 15 << 8,
253 OpCodeMask = 15 << 21, // in data-processing instructions
254 Imm24Mask = (1 << 24) - 1,
255 Off12Mask = (1 << 12) - 1,
Andrei Popescu31002712010-02-23 13:46:05 +0000256 // Reserved condition.
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 nv = 15 << 28
258};
259
260
261// add(sp, sp, 4) instruction (aka Pop())
262static const Instr kPopInstruction =
263 al | 4 * B21 | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
264// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
265// register r is not encoded.
266static const Instr kPushRegPattern =
267 al | B26 | 4 | NegPreIndex | sp.code() * B16;
268// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
269// register r is not encoded.
270static const Instr kPopRegPattern =
271 al | B26 | L | 4 | PostIndex | sp.code() * B16;
272// mov lr, pc
273const Instr kMovLrPc = al | 13*B21 | pc.code() | lr.code() * B12;
Steve Block6ded16b2010-05-10 14:33:55 +0100274// ldr rd, [pc, #offset]
275const Instr kLdrPCMask = CondMask | 15 * B24 | 7 * B20 | 15 * B16;
276const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
277// blxcc rm
278const Instr kBlxRegMask =
279 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
280const Instr kBlxRegPattern =
281 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | 3 * B4;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100282const Instr kMovMvnMask = 0x6d * B21 | 0xf * B16;
283const Instr kMovMvnPattern = 0xd * B21;
284const Instr kMovMvnFlip = B22;
285const Instr kMovLeaveCCMask = 0xdff * B16;
286const Instr kMovLeaveCCPattern = 0x1a0 * B16;
287const Instr kMovwMask = 0xff * B20;
288const Instr kMovwPattern = 0x30 * B20;
289const Instr kMovwLeaveCCFlip = 0x5 * B21;
290const Instr kCmpCmnMask = 0xdd * B20 | 0xf * B12;
291const Instr kCmpCmnPattern = 0x15 * B20;
292const Instr kCmpCmnFlip = B21;
293const Instr kALUMask = 0x6f * B21;
294const Instr kAddPattern = 0x4 * B21;
295const Instr kSubPattern = 0x2 * B21;
296const Instr kBicPattern = 0xe * B21;
297const Instr kAndPattern = 0x0 * B21;
298const Instr kAddSubFlip = 0x6 * B21;
299const Instr kAndBicFlip = 0xe * B21;
300
Leon Clarkef7060e22010-06-03 12:02:55 +0100301// A mask for the Rd register for push, pop, ldr, str instructions.
302const Instr kRdMask = 0x0000f000;
303static const int kRdShift = 12;
304static const Instr kLdrRegFpOffsetPattern =
305 al | B26 | L | Offset | fp.code() * B16;
306static const Instr kStrRegFpOffsetPattern =
307 al | B26 | Offset | fp.code() * B16;
308static const Instr kLdrRegFpNegOffsetPattern =
309 al | B26 | L | NegOffset | fp.code() * B16;
310static const Instr kStrRegFpNegOffsetPattern =
311 al | B26 | NegOffset | fp.code() * B16;
312static const Instr kLdrStrInstrTypeMask = 0xffff0000;
313static const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
314static const Instr kLdrStrOffsetMask = 0x00000fff;
Steve Blocka7e24c12009-10-30 11:49:00 +0000315
Andrei Popescu31002712010-02-23 13:46:05 +0000316// Spare buffer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000317static const int kMinimalBufferSize = 4*KB;
318static byte* spare_buffer_ = NULL;
319
320Assembler::Assembler(void* buffer, int buffer_size) {
321 if (buffer == NULL) {
Andrei Popescu31002712010-02-23 13:46:05 +0000322 // Do our own buffer management.
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 if (buffer_size <= kMinimalBufferSize) {
324 buffer_size = kMinimalBufferSize;
325
326 if (spare_buffer_ != NULL) {
327 buffer = spare_buffer_;
328 spare_buffer_ = NULL;
329 }
330 }
331 if (buffer == NULL) {
332 buffer_ = NewArray<byte>(buffer_size);
333 } else {
334 buffer_ = static_cast<byte*>(buffer);
335 }
336 buffer_size_ = buffer_size;
337 own_buffer_ = true;
338
339 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000340 // Use externally provided buffer instead.
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 ASSERT(buffer_size > 0);
342 buffer_ = static_cast<byte*>(buffer);
343 buffer_size_ = buffer_size;
344 own_buffer_ = false;
345 }
346
Andrei Popescu31002712010-02-23 13:46:05 +0000347 // Setup buffer pointers.
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 ASSERT(buffer_ != NULL);
349 pc_ = buffer_;
350 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
351 num_prinfo_ = 0;
352 next_buffer_check_ = 0;
Steve Block6ded16b2010-05-10 14:33:55 +0100353 const_pool_blocked_nesting_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 no_const_pool_before_ = 0;
355 last_const_pool_end_ = 0;
356 last_bound_pos_ = 0;
357 current_statement_position_ = RelocInfo::kNoPosition;
358 current_position_ = RelocInfo::kNoPosition;
359 written_statement_position_ = current_statement_position_;
360 written_position_ = current_position_;
361}
362
363
364Assembler::~Assembler() {
Steve Block6ded16b2010-05-10 14:33:55 +0100365 ASSERT(const_pool_blocked_nesting_ == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 if (own_buffer_) {
367 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
368 spare_buffer_ = buffer_;
369 } else {
370 DeleteArray(buffer_);
371 }
372 }
373}
374
375
376void Assembler::GetCode(CodeDesc* desc) {
Andrei Popescu31002712010-02-23 13:46:05 +0000377 // Emit constant pool if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 CheckConstPool(true, false);
379 ASSERT(num_prinfo_ == 0);
380
Andrei Popescu31002712010-02-23 13:46:05 +0000381 // Setup code descriptor.
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 desc->buffer = buffer_;
383 desc->buffer_size = buffer_size_;
384 desc->instr_size = pc_offset();
385 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
386}
387
388
389void Assembler::Align(int m) {
390 ASSERT(m >= 4 && IsPowerOf2(m));
391 while ((pc_offset() & (m - 1)) != 0) {
392 nop();
393 }
394}
395
396
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100397void Assembler::CodeTargetAlign() {
398 // Preferred alignment of jump targets on some ARM chips.
399 Align(8);
400}
401
402
Steve Block6ded16b2010-05-10 14:33:55 +0100403bool Assembler::IsNop(Instr instr, int type) {
404 // Check for mov rx, rx.
405 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
406 return instr == (al | 13*B21 | type*B12 | type);
407}
408
409
410bool Assembler::IsBranch(Instr instr) {
411 return (instr & (B27 | B25)) == (B27 | B25);
412}
413
414
415int Assembler::GetBranchOffset(Instr instr) {
416 ASSERT(IsBranch(instr));
417 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
418 // with 4 to get the offset in bytes.
419 return ((instr & Imm24Mask) << 8) >> 6;
420}
421
422
423bool Assembler::IsLdrRegisterImmediate(Instr instr) {
424 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
425}
426
427
428int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
429 ASSERT(IsLdrRegisterImmediate(instr));
430 bool positive = (instr & B23) == B23;
431 int offset = instr & Off12Mask; // Zero extended offset.
432 return positive ? offset : -offset;
433}
434
435
436Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
437 ASSERT(IsLdrRegisterImmediate(instr));
438 bool positive = offset >= 0;
439 if (!positive) offset = -offset;
440 ASSERT(is_uint12(offset));
441 // Set bit indicating whether the offset should be added.
442 instr = (instr & ~B23) | (positive ? B23 : 0);
443 // Set the actual offset.
444 return (instr & ~Off12Mask) | offset;
445}
446
447
Leon Clarkef7060e22010-06-03 12:02:55 +0100448Register Assembler::GetRd(Instr instr) {
449 Register reg;
450 reg.code_ = ((instr & kRdMask) >> kRdShift);
451 return reg;
452}
453
454
455bool Assembler::IsPush(Instr instr) {
456 return ((instr & ~kRdMask) == kPushRegPattern);
457}
458
459
460bool Assembler::IsPop(Instr instr) {
461 return ((instr & ~kRdMask) == kPopRegPattern);
462}
463
464
465bool Assembler::IsStrRegFpOffset(Instr instr) {
466 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
467}
468
469
470bool Assembler::IsLdrRegFpOffset(Instr instr) {
471 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
472}
473
474
475bool Assembler::IsStrRegFpNegOffset(Instr instr) {
476 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
477}
478
479
480bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
481 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
482}
483
484
Steve Blocka7e24c12009-10-30 11:49:00 +0000485// Labels refer to positions in the (to be) generated code.
486// There are bound, linked, and unused labels.
487//
488// Bound labels refer to known positions in the already
489// generated code. pos() is the position the label refers to.
490//
491// Linked labels refer to unknown positions in the code
492// to be generated; pos() is the position of the last
493// instruction using the label.
494
495
496// The link chain is terminated by a negative code position (must be aligned)
497const int kEndOfChain = -4;
498
499
500int Assembler::target_at(int pos) {
501 Instr instr = instr_at(pos);
502 if ((instr & ~Imm24Mask) == 0) {
503 // Emitted label constant, not part of a branch.
504 return instr - (Code::kHeaderSize - kHeapObjectTag);
505 }
506 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
507 int imm26 = ((instr & Imm24Mask) << 8) >> 6;
Steve Block6ded16b2010-05-10 14:33:55 +0100508 if ((instr & CondMask) == nv && (instr & B24) != 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000509 // blx uses bit 24 to encode bit 2 of imm26
510 imm26 += 2;
Steve Block6ded16b2010-05-10 14:33:55 +0100511 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 return pos + kPcLoadDelta + imm26;
513}
514
515
516void Assembler::target_at_put(int pos, int target_pos) {
517 Instr instr = instr_at(pos);
518 if ((instr & ~Imm24Mask) == 0) {
519 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
520 // Emitted label constant, not part of a branch.
521 // Make label relative to Code* of generated Code object.
522 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
523 return;
524 }
525 int imm26 = target_pos - (pos + kPcLoadDelta);
526 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
527 if ((instr & CondMask) == nv) {
528 // blx uses bit 24 to encode bit 2 of imm26
529 ASSERT((imm26 & 1) == 0);
530 instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24;
531 } else {
532 ASSERT((imm26 & 3) == 0);
533 instr &= ~Imm24Mask;
534 }
535 int imm24 = imm26 >> 2;
536 ASSERT(is_int24(imm24));
537 instr_at_put(pos, instr | (imm24 & Imm24Mask));
538}
539
540
541void Assembler::print(Label* L) {
542 if (L->is_unused()) {
543 PrintF("unused label\n");
544 } else if (L->is_bound()) {
545 PrintF("bound label to %d\n", L->pos());
546 } else if (L->is_linked()) {
547 Label l = *L;
548 PrintF("unbound label");
549 while (l.is_linked()) {
550 PrintF("@ %d ", l.pos());
551 Instr instr = instr_at(l.pos());
552 if ((instr & ~Imm24Mask) == 0) {
553 PrintF("value\n");
554 } else {
555 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
556 int cond = instr & CondMask;
557 const char* b;
558 const char* c;
559 if (cond == nv) {
560 b = "blx";
561 c = "";
562 } else {
563 if ((instr & B24) != 0)
564 b = "bl";
565 else
566 b = "b";
567
568 switch (cond) {
569 case eq: c = "eq"; break;
570 case ne: c = "ne"; break;
571 case hs: c = "hs"; break;
572 case lo: c = "lo"; break;
573 case mi: c = "mi"; break;
574 case pl: c = "pl"; break;
575 case vs: c = "vs"; break;
576 case vc: c = "vc"; break;
577 case hi: c = "hi"; break;
578 case ls: c = "ls"; break;
579 case ge: c = "ge"; break;
580 case lt: c = "lt"; break;
581 case gt: c = "gt"; break;
582 case le: c = "le"; break;
583 case al: c = ""; break;
584 default:
585 c = "";
586 UNREACHABLE();
587 }
588 }
589 PrintF("%s%s\n", b, c);
590 }
591 next(&l);
592 }
593 } else {
594 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
595 }
596}
597
598
599void Assembler::bind_to(Label* L, int pos) {
600 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
601 while (L->is_linked()) {
602 int fixup_pos = L->pos();
603 next(L); // call next before overwriting link with target at fixup_pos
604 target_at_put(fixup_pos, pos);
605 }
606 L->bind_to(pos);
607
608 // Keep track of the last bound label so we don't eliminate any instructions
609 // before a bound label.
610 if (pos > last_bound_pos_)
611 last_bound_pos_ = pos;
612}
613
614
615void Assembler::link_to(Label* L, Label* appendix) {
616 if (appendix->is_linked()) {
617 if (L->is_linked()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000618 // Append appendix to L's list.
Steve Blocka7e24c12009-10-30 11:49:00 +0000619 int fixup_pos;
620 int link = L->pos();
621 do {
622 fixup_pos = link;
623 link = target_at(fixup_pos);
624 } while (link > 0);
625 ASSERT(link == kEndOfChain);
626 target_at_put(fixup_pos, appendix->pos());
627 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000628 // L is empty, simply use appendix.
Steve Blocka7e24c12009-10-30 11:49:00 +0000629 *L = *appendix;
630 }
631 }
632 appendix->Unuse(); // appendix should not be used anymore
633}
634
635
636void Assembler::bind(Label* L) {
637 ASSERT(!L->is_bound()); // label can only be bound once
638 bind_to(L, pc_offset());
639}
640
641
642void Assembler::next(Label* L) {
643 ASSERT(L->is_linked());
644 int link = target_at(L->pos());
645 if (link > 0) {
646 L->link_to(link);
647 } else {
648 ASSERT(link == kEndOfChain);
649 L->Unuse();
650 }
651}
652
653
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100654static Instr EncodeMovwImmediate(uint32_t immediate) {
655 ASSERT(immediate < 0x10000);
656 return ((immediate & 0xf000) << 4) | (immediate & 0xfff);
657}
658
659
Andrei Popescu31002712010-02-23 13:46:05 +0000660// Low-level code emission routines depending on the addressing mode.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100661// If this returns true then you have to use the rotate_imm and immed_8
662// that it returns, because it may have already changed the instruction
663// to match them!
Steve Blocka7e24c12009-10-30 11:49:00 +0000664static bool fits_shifter(uint32_t imm32,
665 uint32_t* rotate_imm,
666 uint32_t* immed_8,
667 Instr* instr) {
Andrei Popescu31002712010-02-23 13:46:05 +0000668 // imm32 must be unsigned.
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 for (int rot = 0; rot < 16; rot++) {
670 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
671 if ((imm8 <= 0xff)) {
672 *rotate_imm = rot;
673 *immed_8 = imm8;
674 return true;
675 }
676 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100677 // If the opcode is one with a complementary version and the complementary
678 // immediate fits, change the opcode.
679 if (instr != NULL) {
680 if ((*instr & kMovMvnMask) == kMovMvnPattern) {
681 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
682 *instr ^= kMovMvnFlip;
683 return true;
684 } else if ((*instr & kMovLeaveCCMask) == kMovLeaveCCPattern) {
685 if (CpuFeatures::IsSupported(ARMv7)) {
686 if (imm32 < 0x10000) {
687 *instr ^= kMovwLeaveCCFlip;
688 *instr |= EncodeMovwImmediate(imm32);
689 *rotate_imm = *immed_8 = 0; // Not used for movw.
690 return true;
691 }
692 }
693 }
694 } else if ((*instr & kCmpCmnMask) == kCmpCmnPattern) {
695 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
696 *instr ^= kCmpCmnFlip;
697 return true;
698 }
699 } else {
700 Instr alu_insn = (*instr & kALUMask);
701 if (alu_insn == kAddPattern ||
702 alu_insn == kSubPattern) {
703 if (fits_shifter(-imm32, rotate_imm, immed_8, NULL)) {
704 *instr ^= kAddSubFlip;
705 return true;
706 }
707 } else if (alu_insn == kAndPattern ||
708 alu_insn == kBicPattern) {
709 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
710 *instr ^= kAndBicFlip;
711 return true;
712 }
713 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000714 }
715 }
716 return false;
717}
718
719
720// We have to use the temporary register for things that can be relocated even
721// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
722// space. There is no guarantee that the relocated location can be similarly
723// encoded.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100724static bool MustUseConstantPool(RelocInfo::Mode rmode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000725 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
Steve Blockd0582a62009-12-15 09:54:21 +0000726#ifdef DEBUG
727 if (!Serializer::enabled()) {
728 Serializer::TooLateToEnableNow();
729 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000730#endif // def DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000731 return Serializer::enabled();
732 } else if (rmode == RelocInfo::NONE) {
733 return false;
734 }
735 return true;
736}
737
738
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100739bool Operand::is_single_instruction() const {
740 if (rm_.is_valid()) return true;
741 if (MustUseConstantPool(rmode_)) return false;
742 uint32_t dummy1, dummy2;
743 return fits_shifter(imm32_, &dummy1, &dummy2, NULL);
744}
745
746
Steve Blocka7e24c12009-10-30 11:49:00 +0000747void Assembler::addrmod1(Instr instr,
748 Register rn,
749 Register rd,
750 const Operand& x) {
751 CheckBuffer();
752 ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0);
753 if (!x.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000754 // Immediate.
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 uint32_t rotate_imm;
756 uint32_t immed_8;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100757 if (MustUseConstantPool(x.rmode_) ||
Steve Blocka7e24c12009-10-30 11:49:00 +0000758 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
759 // The immediate operand cannot be encoded as a shifter operand, so load
760 // it first to register ip and change the original instruction to use ip.
761 // However, if the original instruction is a 'mov rd, x' (not setting the
Andrei Popescu31002712010-02-23 13:46:05 +0000762 // condition code), then replace it with a 'ldr rd, [pc]'.
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
764 Condition cond = static_cast<Condition>(instr & CondMask);
765 if ((instr & ~CondMask) == 13*B21) { // mov, S not set
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100766 if (MustUseConstantPool(x.rmode_) ||
767 !CpuFeatures::IsSupported(ARMv7)) {
768 RecordRelocInfo(x.rmode_, x.imm32_);
769 ldr(rd, MemOperand(pc, 0), cond);
770 } else {
771 // Will probably use movw, will certainly not use constant pool.
772 mov(rd, Operand(x.imm32_ & 0xffff), LeaveCC, cond);
773 movt(rd, static_cast<uint32_t>(x.imm32_) >> 16, cond);
774 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000775 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100776 // If this is not a mov or mvn instruction we may still be able to avoid
777 // a constant pool entry by using mvn or movw.
778 if (!MustUseConstantPool(x.rmode_) &&
779 (instr & kMovMvnMask) != kMovMvnPattern) {
780 mov(ip, x, LeaveCC, cond);
781 } else {
782 RecordRelocInfo(x.rmode_, x.imm32_);
783 ldr(ip, MemOperand(pc, 0), cond);
784 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 addrmod1(instr, rn, rd, Operand(ip));
786 }
787 return;
788 }
789 instr |= I | rotate_imm*B8 | immed_8;
790 } else if (!x.rs_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000791 // Immediate shift.
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
793 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000794 // Register shift.
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
796 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
797 }
798 emit(instr | rn.code()*B16 | rd.code()*B12);
799 if (rn.is(pc) || x.rm_.is(pc))
Andrei Popescu31002712010-02-23 13:46:05 +0000800 // Block constant pool emission for one instruction after reading pc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 BlockConstPoolBefore(pc_offset() + kInstrSize);
802}
803
804
805void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
806 ASSERT((instr & ~(CondMask | B | L)) == B26);
807 int am = x.am_;
808 if (!x.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000809 // Immediate offset.
Steve Blocka7e24c12009-10-30 11:49:00 +0000810 int offset_12 = x.offset_;
811 if (offset_12 < 0) {
812 offset_12 = -offset_12;
813 am ^= U;
814 }
815 if (!is_uint12(offset_12)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000816 // Immediate offset cannot be encoded, load it first to register ip
817 // rn (and rd in a load) should never be ip, or will be trashed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000818 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
819 mov(ip, Operand(x.offset_), LeaveCC,
820 static_cast<Condition>(instr & CondMask));
821 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
822 return;
823 }
824 ASSERT(offset_12 >= 0); // no masking needed
825 instr |= offset_12;
826 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000827 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 // register offset the constructors make sure than both shift_imm_
Andrei Popescu31002712010-02-23 13:46:05 +0000829 // and shift_op_ are initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000830 ASSERT(!x.rm_.is(pc));
831 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
832 }
833 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
834 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
835}
836
837
838void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
839 ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7));
840 ASSERT(x.rn_.is_valid());
841 int am = x.am_;
842 if (!x.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000843 // Immediate offset.
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 int offset_8 = x.offset_;
845 if (offset_8 < 0) {
846 offset_8 = -offset_8;
847 am ^= U;
848 }
849 if (!is_uint8(offset_8)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000850 // Immediate offset cannot be encoded, load it first to register ip
851 // rn (and rd in a load) should never be ip, or will be trashed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000852 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
853 mov(ip, Operand(x.offset_), LeaveCC,
854 static_cast<Condition>(instr & CondMask));
855 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
856 return;
857 }
858 ASSERT(offset_8 >= 0); // no masking needed
859 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
860 } else if (x.shift_imm_ != 0) {
Andrei Popescu31002712010-02-23 13:46:05 +0000861 // Scaled register offset not supported, load index first
862 // rn (and rd in a load) should never be ip, or will be trashed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000863 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
864 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
865 static_cast<Condition>(instr & CondMask));
866 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
867 return;
868 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000869 // Register offset.
Steve Blocka7e24c12009-10-30 11:49:00 +0000870 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
871 instr |= x.rm_.code();
872 }
873 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
874 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
875}
876
877
878void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
879 ASSERT((instr & ~(CondMask | P | U | W | L)) == B27);
880 ASSERT(rl != 0);
881 ASSERT(!rn.is(pc));
882 emit(instr | rn.code()*B16 | rl);
883}
884
885
886void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
Andrei Popescu31002712010-02-23 13:46:05 +0000887 // Unindexed addressing is not encoded by this function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000888 ASSERT_EQ((B27 | B26),
889 (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L)));
890 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
891 int am = x.am_;
892 int offset_8 = x.offset_;
893 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
894 offset_8 >>= 2;
895 if (offset_8 < 0) {
896 offset_8 = -offset_8;
897 am ^= U;
898 }
899 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
900 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
901
Andrei Popescu31002712010-02-23 13:46:05 +0000902 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
Steve Blocka7e24c12009-10-30 11:49:00 +0000903 if ((am & P) == 0)
904 am |= W;
905
906 ASSERT(offset_8 >= 0); // no masking needed
907 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
908}
909
910
911int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
912 int target_pos;
913 if (L->is_bound()) {
914 target_pos = L->pos();
915 } else {
916 if (L->is_linked()) {
917 target_pos = L->pos(); // L's link
918 } else {
919 target_pos = kEndOfChain;
920 }
921 L->link_to(pc_offset());
922 }
923
924 // Block the emission of the constant pool, since the branch instruction must
Andrei Popescu31002712010-02-23 13:46:05 +0000925 // be emitted at the pc offset recorded by the label.
Steve Blocka7e24c12009-10-30 11:49:00 +0000926 BlockConstPoolBefore(pc_offset() + kInstrSize);
927 return target_pos - (pc_offset() + kPcLoadDelta);
928}
929
930
931void Assembler::label_at_put(Label* L, int at_offset) {
932 int target_pos;
933 if (L->is_bound()) {
934 target_pos = L->pos();
935 } else {
936 if (L->is_linked()) {
937 target_pos = L->pos(); // L's link
938 } else {
939 target_pos = kEndOfChain;
940 }
941 L->link_to(at_offset);
942 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
943 }
944}
945
946
Andrei Popescu31002712010-02-23 13:46:05 +0000947// Branch instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000948void Assembler::b(int branch_offset, Condition cond) {
949 ASSERT((branch_offset & 3) == 0);
950 int imm24 = branch_offset >> 2;
951 ASSERT(is_int24(imm24));
952 emit(cond | B27 | B25 | (imm24 & Imm24Mask));
953
Steve Block6ded16b2010-05-10 14:33:55 +0100954 if (cond == al) {
Andrei Popescu31002712010-02-23 13:46:05 +0000955 // Dead code is a good location to emit the constant pool.
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 CheckConstPool(false, false);
Steve Block6ded16b2010-05-10 14:33:55 +0100957 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000958}
959
960
961void Assembler::bl(int branch_offset, Condition cond) {
962 ASSERT((branch_offset & 3) == 0);
963 int imm24 = branch_offset >> 2;
964 ASSERT(is_int24(imm24));
965 emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask));
966}
967
968
969void Assembler::blx(int branch_offset) { // v5 and above
970 WriteRecordedPositions();
971 ASSERT((branch_offset & 1) == 0);
972 int h = ((branch_offset & 2) >> 1)*B24;
973 int imm24 = branch_offset >> 2;
974 ASSERT(is_int24(imm24));
975 emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask));
976}
977
978
979void Assembler::blx(Register target, Condition cond) { // v5 and above
980 WriteRecordedPositions();
981 ASSERT(!target.is(pc));
982 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code());
983}
984
985
986void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
987 WriteRecordedPositions();
988 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
989 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code());
990}
991
992
Andrei Popescu31002712010-02-23 13:46:05 +0000993// Data-processing instructions.
994
Steve Blocka7e24c12009-10-30 11:49:00 +0000995void Assembler::and_(Register dst, Register src1, const Operand& src2,
996 SBit s, Condition cond) {
997 addrmod1(cond | 0*B21 | s, src1, dst, src2);
998}
999
1000
1001void Assembler::eor(Register dst, Register src1, const Operand& src2,
1002 SBit s, Condition cond) {
1003 addrmod1(cond | 1*B21 | s, src1, dst, src2);
1004}
1005
1006
1007void Assembler::sub(Register dst, Register src1, const Operand& src2,
1008 SBit s, Condition cond) {
1009 addrmod1(cond | 2*B21 | s, src1, dst, src2);
1010}
1011
1012
1013void Assembler::rsb(Register dst, Register src1, const Operand& src2,
1014 SBit s, Condition cond) {
1015 addrmod1(cond | 3*B21 | s, src1, dst, src2);
1016}
1017
1018
1019void Assembler::add(Register dst, Register src1, const Operand& src2,
1020 SBit s, Condition cond) {
1021 addrmod1(cond | 4*B21 | s, src1, dst, src2);
1022
1023 // Eliminate pattern: push(r), pop()
1024 // str(src, MemOperand(sp, 4, NegPreIndex), al);
1025 // add(sp, sp, Operand(kPointerSize));
1026 // Both instructions can be eliminated.
Leon Clarkef7060e22010-06-03 12:02:55 +01001027 if (can_peephole_optimize(2) &&
Andrei Popescu31002712010-02-23 13:46:05 +00001028 // Pattern.
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
1030 (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) {
1031 pc_ -= 2 * kInstrSize;
Leon Clarkef7060e22010-06-03 12:02:55 +01001032 if (FLAG_print_peephole_optimization) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001033 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
1034 }
1035 }
1036}
1037
1038
1039void Assembler::adc(Register dst, Register src1, const Operand& src2,
1040 SBit s, Condition cond) {
1041 addrmod1(cond | 5*B21 | s, src1, dst, src2);
1042}
1043
1044
1045void Assembler::sbc(Register dst, Register src1, const Operand& src2,
1046 SBit s, Condition cond) {
1047 addrmod1(cond | 6*B21 | s, src1, dst, src2);
1048}
1049
1050
1051void Assembler::rsc(Register dst, Register src1, const Operand& src2,
1052 SBit s, Condition cond) {
1053 addrmod1(cond | 7*B21 | s, src1, dst, src2);
1054}
1055
1056
1057void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
1058 addrmod1(cond | 8*B21 | S, src1, r0, src2);
1059}
1060
1061
1062void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
1063 addrmod1(cond | 9*B21 | S, src1, r0, src2);
1064}
1065
1066
1067void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
1068 addrmod1(cond | 10*B21 | S, src1, r0, src2);
1069}
1070
1071
1072void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
1073 addrmod1(cond | 11*B21 | S, src1, r0, src2);
1074}
1075
1076
1077void Assembler::orr(Register dst, Register src1, const Operand& src2,
1078 SBit s, Condition cond) {
1079 addrmod1(cond | 12*B21 | s, src1, dst, src2);
1080}
1081
1082
1083void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
1084 if (dst.is(pc)) {
1085 WriteRecordedPositions();
1086 }
Steve Block6ded16b2010-05-10 14:33:55 +01001087 // Don't allow nop instructions in the form mov rn, rn to be generated using
1088 // the mov instruction. They must be generated using nop(int)
1089 // pseudo instructions.
1090 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 addrmod1(cond | 13*B21 | s, r0, dst, src);
1092}
1093
1094
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001095void Assembler::movw(Register reg, uint32_t immediate, Condition cond) {
1096 ASSERT(immediate < 0x10000);
1097 mov(reg, Operand(immediate), LeaveCC, cond);
1098}
1099
1100
1101void Assembler::movt(Register reg, uint32_t immediate, Condition cond) {
1102 emit(cond | 0x34*B20 | reg.code()*B12 | EncodeMovwImmediate(immediate));
1103}
1104
1105
Steve Blocka7e24c12009-10-30 11:49:00 +00001106void Assembler::bic(Register dst, Register src1, const Operand& src2,
1107 SBit s, Condition cond) {
1108 addrmod1(cond | 14*B21 | s, src1, dst, src2);
1109}
1110
1111
1112void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
1113 addrmod1(cond | 15*B21 | s, r0, dst, src);
1114}
1115
1116
Andrei Popescu31002712010-02-23 13:46:05 +00001117// Multiply instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001118void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1119 SBit s, Condition cond) {
1120 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
1121 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1122 src2.code()*B8 | B7 | B4 | src1.code());
1123}
1124
1125
1126void Assembler::mul(Register dst, Register src1, Register src2,
1127 SBit s, Condition cond) {
1128 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
1129 // dst goes in bits 16-19 for this instruction!
1130 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1131}
1132
1133
1134void Assembler::smlal(Register dstL,
1135 Register dstH,
1136 Register src1,
1137 Register src2,
1138 SBit s,
1139 Condition cond) {
1140 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1141 ASSERT(!dstL.is(dstH));
1142 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1143 src2.code()*B8 | B7 | B4 | src1.code());
1144}
1145
1146
1147void Assembler::smull(Register dstL,
1148 Register dstH,
1149 Register src1,
1150 Register src2,
1151 SBit s,
1152 Condition cond) {
1153 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1154 ASSERT(!dstL.is(dstH));
1155 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1156 src2.code()*B8 | B7 | B4 | src1.code());
1157}
1158
1159
1160void Assembler::umlal(Register dstL,
1161 Register dstH,
1162 Register src1,
1163 Register src2,
1164 SBit s,
1165 Condition cond) {
1166 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1167 ASSERT(!dstL.is(dstH));
1168 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1169 src2.code()*B8 | B7 | B4 | src1.code());
1170}
1171
1172
1173void Assembler::umull(Register dstL,
1174 Register dstH,
1175 Register src1,
1176 Register src2,
1177 SBit s,
1178 Condition cond) {
1179 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1180 ASSERT(!dstL.is(dstH));
1181 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
1182 src2.code()*B8 | B7 | B4 | src1.code());
1183}
1184
1185
Andrei Popescu31002712010-02-23 13:46:05 +00001186// Miscellaneous arithmetic instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001187void Assembler::clz(Register dst, Register src, Condition cond) {
1188 // v5 and above.
1189 ASSERT(!dst.is(pc) && !src.is(pc));
1190 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
1191 15*B8 | B4 | src.code());
1192}
1193
1194
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001195// Bitfield manipulation instructions.
1196
1197// Unsigned bit field extract.
1198// Extracts #width adjacent bits from position #lsb in a register, and
1199// writes them to the low bits of a destination register.
1200// ubfx dst, src, #lsb, #width
1201void Assembler::ubfx(Register dst,
1202 Register src,
1203 int lsb,
1204 int width,
1205 Condition cond) {
1206 // v7 and above.
1207 ASSERT(CpuFeatures::IsSupported(ARMv7));
1208 ASSERT(!dst.is(pc) && !src.is(pc));
1209 ASSERT((lsb >= 0) && (lsb <= 31));
1210 ASSERT((width >= 1) && (width <= (32 - lsb)));
1211 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1212 lsb*B7 | B6 | B4 | src.code());
1213}
1214
1215
1216// Signed bit field extract.
1217// Extracts #width adjacent bits from position #lsb in a register, and
1218// writes them to the low bits of a destination register. The extracted
1219// value is sign extended to fill the destination register.
1220// sbfx dst, src, #lsb, #width
1221void Assembler::sbfx(Register dst,
1222 Register src,
1223 int lsb,
1224 int width,
1225 Condition cond) {
1226 // v7 and above.
1227 ASSERT(CpuFeatures::IsSupported(ARMv7));
1228 ASSERT(!dst.is(pc) && !src.is(pc));
1229 ASSERT((lsb >= 0) && (lsb <= 31));
1230 ASSERT((width >= 1) && (width <= (32 - lsb)));
1231 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1232 lsb*B7 | B6 | B4 | src.code());
1233}
1234
1235
1236// Bit field clear.
1237// Sets #width adjacent bits at position #lsb in the destination register
1238// to zero, preserving the value of the other bits.
1239// bfc dst, #lsb, #width
1240void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1241 // v7 and above.
1242 ASSERT(CpuFeatures::IsSupported(ARMv7));
1243 ASSERT(!dst.is(pc));
1244 ASSERT((lsb >= 0) && (lsb <= 31));
1245 ASSERT((width >= 1) && (width <= (32 - lsb)));
1246 int msb = lsb + width - 1;
1247 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1248}
1249
1250
1251// Bit field insert.
1252// Inserts #width adjacent bits from the low bits of the source register
1253// into position #lsb of the destination register.
1254// bfi dst, src, #lsb, #width
1255void Assembler::bfi(Register dst,
1256 Register src,
1257 int lsb,
1258 int width,
1259 Condition cond) {
1260 // v7 and above.
1261 ASSERT(CpuFeatures::IsSupported(ARMv7));
1262 ASSERT(!dst.is(pc) && !src.is(pc));
1263 ASSERT((lsb >= 0) && (lsb <= 31));
1264 ASSERT((width >= 1) && (width <= (32 - lsb)));
1265 int msb = lsb + width - 1;
1266 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1267 src.code());
1268}
1269
1270
Andrei Popescu31002712010-02-23 13:46:05 +00001271// Status register access instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001272void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1273 ASSERT(!dst.is(pc));
1274 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1275}
1276
1277
1278void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1279 Condition cond) {
1280 ASSERT(fields >= B16 && fields < B20); // at least one field set
1281 Instr instr;
1282 if (!src.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +00001283 // Immediate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001284 uint32_t rotate_imm;
1285 uint32_t immed_8;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001286 if (MustUseConstantPool(src.rmode_) ||
Steve Blocka7e24c12009-10-30 11:49:00 +00001287 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001288 // Immediate operand cannot be encoded, load it first to register ip.
Steve Blocka7e24c12009-10-30 11:49:00 +00001289 RecordRelocInfo(src.rmode_, src.imm32_);
1290 ldr(ip, MemOperand(pc, 0), cond);
1291 msr(fields, Operand(ip), cond);
1292 return;
1293 }
1294 instr = I | rotate_imm*B8 | immed_8;
1295 } else {
1296 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1297 instr = src.rm_.code();
1298 }
1299 emit(cond | instr | B24 | B21 | fields | 15*B12);
1300}
1301
1302
Andrei Popescu31002712010-02-23 13:46:05 +00001303// Load/Store instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001304void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
1305 if (dst.is(pc)) {
1306 WriteRecordedPositions();
1307 }
1308 addrmod2(cond | B26 | L, dst, src);
1309
Leon Clarkef7060e22010-06-03 12:02:55 +01001310 // Eliminate pattern: push(ry), pop(rx)
1311 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1312 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1313 // Both instructions can be eliminated if ry = rx.
1314 // If ry != rx, a register copy from ry to rx is inserted
1315 // after eliminating the push and the pop instructions.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001316 if (can_peephole_optimize(2)) {
1317 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1318 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
Leon Clarkef7060e22010-06-03 12:02:55 +01001319
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001320 if (IsPush(push_instr) && IsPop(pop_instr)) {
1321 if ((pop_instr & kRdMask) != (push_instr & kRdMask)) {
1322 // For consecutive push and pop on different registers,
1323 // we delete both the push & pop and insert a register move.
1324 // push ry, pop rx --> mov rx, ry
1325 Register reg_pushed, reg_popped;
1326 reg_pushed = GetRd(push_instr);
1327 reg_popped = GetRd(pop_instr);
1328 pc_ -= 2 * kInstrSize;
1329 // Insert a mov instruction, which is better than a pair of push & pop
1330 mov(reg_popped, reg_pushed);
1331 if (FLAG_print_peephole_optimization) {
1332 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1333 pc_offset());
1334 }
1335 } else {
1336 // For consecutive push and pop on the same register,
1337 // both the push and the pop can be deleted.
1338 pc_ -= 2 * kInstrSize;
1339 if (FLAG_print_peephole_optimization) {
1340 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1341 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001342 }
1343 }
1344 }
1345
1346 if (can_peephole_optimize(2)) {
1347 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1348 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1349
1350 if ((IsStrRegFpOffset(str_instr) &&
1351 IsLdrRegFpOffset(ldr_instr)) ||
1352 (IsStrRegFpNegOffset(str_instr) &&
1353 IsLdrRegFpNegOffset(ldr_instr))) {
1354 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1355 (str_instr & kLdrStrInstrArgumentMask)) {
1356 // Pattern: Ldr/str same fp+offset, same register.
1357 //
1358 // The following:
1359 // str rx, [fp, #-12]
1360 // ldr rx, [fp, #-12]
1361 //
1362 // Becomes:
1363 // str rx, [fp, #-12]
1364
1365 pc_ -= 1 * kInstrSize;
1366 if (FLAG_print_peephole_optimization) {
1367 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1368 }
1369 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1370 (str_instr & kLdrStrOffsetMask)) {
1371 // Pattern: Ldr/str same fp+offset, different register.
1372 //
1373 // The following:
1374 // str rx, [fp, #-12]
1375 // ldr ry, [fp, #-12]
1376 //
1377 // Becomes:
1378 // str rx, [fp, #-12]
1379 // mov ry, rx
1380
1381 Register reg_stored, reg_loaded;
1382 reg_stored = GetRd(str_instr);
1383 reg_loaded = GetRd(ldr_instr);
1384 pc_ -= 1 * kInstrSize;
1385 // Insert a mov instruction, which is better than ldr.
1386 mov(reg_loaded, reg_stored);
1387 if (FLAG_print_peephole_optimization) {
1388 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1389 }
1390 }
1391 }
1392 }
1393
1394 if (can_peephole_optimize(3)) {
1395 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1396 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1397 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1398 if (IsPush(mem_write_instr) &&
1399 IsPop(mem_read_instr)) {
1400 if ((IsLdrRegFpOffset(ldr_instr) ||
1401 IsLdrRegFpNegOffset(ldr_instr))) {
1402 if ((mem_write_instr & kRdMask) ==
1403 (mem_read_instr & kRdMask)) {
1404 // Pattern: push & pop from/to same register,
1405 // with a fp+offset ldr in between
1406 //
1407 // The following:
1408 // str rx, [sp, #-4]!
1409 // ldr rz, [fp, #-24]
1410 // ldr rx, [sp], #+4
1411 //
1412 // Becomes:
1413 // if(rx == rz)
1414 // delete all
1415 // else
1416 // ldr rz, [fp, #-24]
1417
1418 if ((mem_write_instr & kRdMask) == (ldr_instr & kRdMask)) {
1419 pc_ -= 3 * kInstrSize;
1420 } else {
1421 pc_ -= 3 * kInstrSize;
1422 // Reinsert back the ldr rz.
1423 emit(ldr_instr);
1424 }
1425 if (FLAG_print_peephole_optimization) {
1426 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1427 }
1428 } else {
1429 // Pattern: push & pop from/to different registers
1430 // with a fp+offset ldr in between
1431 //
1432 // The following:
1433 // str rx, [sp, #-4]!
1434 // ldr rz, [fp, #-24]
1435 // ldr ry, [sp], #+4
1436 //
1437 // Becomes:
1438 // if(ry == rz)
1439 // mov ry, rx;
1440 // else if(rx != rz)
1441 // ldr rz, [fp, #-24]
1442 // mov ry, rx
1443 // else if((ry != rz) || (rx == rz)) becomes:
1444 // mov ry, rx
1445 // ldr rz, [fp, #-24]
1446
1447 Register reg_pushed, reg_popped;
1448 if ((mem_read_instr & kRdMask) == (ldr_instr & kRdMask)) {
1449 reg_pushed = GetRd(mem_write_instr);
1450 reg_popped = GetRd(mem_read_instr);
1451 pc_ -= 3 * kInstrSize;
1452 mov(reg_popped, reg_pushed);
1453 } else if ((mem_write_instr & kRdMask)
1454 != (ldr_instr & kRdMask)) {
1455 reg_pushed = GetRd(mem_write_instr);
1456 reg_popped = GetRd(mem_read_instr);
1457 pc_ -= 3 * kInstrSize;
1458 emit(ldr_instr);
1459 mov(reg_popped, reg_pushed);
1460 } else if (((mem_read_instr & kRdMask)
1461 != (ldr_instr & kRdMask)) ||
1462 ((mem_write_instr & kRdMask)
1463 == (ldr_instr & kRdMask)) ) {
1464 reg_pushed = GetRd(mem_write_instr);
1465 reg_popped = GetRd(mem_read_instr);
1466 pc_ -= 3 * kInstrSize;
1467 mov(reg_popped, reg_pushed);
1468 emit(ldr_instr);
1469 }
1470 if (FLAG_print_peephole_optimization) {
1471 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1472 }
1473 }
1474 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001475 }
1476 }
1477}
1478
1479
1480void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1481 addrmod2(cond | B26, src, dst);
1482
1483 // Eliminate pattern: pop(), push(r)
1484 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1485 // -> str r, [sp, 0], al
Leon Clarkef7060e22010-06-03 12:02:55 +01001486 if (can_peephole_optimize(2) &&
Andrei Popescu31002712010-02-23 13:46:05 +00001487 // Pattern.
Steve Blocka7e24c12009-10-30 11:49:00 +00001488 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1489 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1490 pc_ -= 2 * kInstrSize;
1491 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
Leon Clarkef7060e22010-06-03 12:02:55 +01001492 if (FLAG_print_peephole_optimization) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001493 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1494 }
1495 }
1496}
1497
1498
1499void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1500 addrmod2(cond | B26 | B | L, dst, src);
1501}
1502
1503
1504void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1505 addrmod2(cond | B26 | B, src, dst);
1506}
1507
1508
1509void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1510 addrmod3(cond | L | B7 | H | B4, dst, src);
1511}
1512
1513
1514void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1515 addrmod3(cond | B7 | H | B4, src, dst);
1516}
1517
1518
1519void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1520 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1521}
1522
1523
1524void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1525 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1526}
1527
1528
Leon Clarkef7060e22010-06-03 12:02:55 +01001529void Assembler::ldrd(Register dst1, Register dst2,
1530 const MemOperand& src, Condition cond) {
1531 ASSERT(CpuFeatures::IsEnabled(ARMv7));
Kristian Monsen25f61362010-05-21 11:50:48 +01001532 ASSERT(src.rm().is(no_reg));
Leon Clarkef7060e22010-06-03 12:02:55 +01001533 ASSERT(!dst1.is(lr)); // r14.
1534 ASSERT_EQ(0, dst1.code() % 2);
1535 ASSERT_EQ(dst1.code() + 1, dst2.code());
1536 addrmod3(cond | B7 | B6 | B4, dst1, src);
Kristian Monsen25f61362010-05-21 11:50:48 +01001537}
1538
1539
Leon Clarkef7060e22010-06-03 12:02:55 +01001540void Assembler::strd(Register src1, Register src2,
1541 const MemOperand& dst, Condition cond) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001542 ASSERT(dst.rm().is(no_reg));
Leon Clarkef7060e22010-06-03 12:02:55 +01001543 ASSERT(!src1.is(lr)); // r14.
1544 ASSERT_EQ(0, src1.code() % 2);
1545 ASSERT_EQ(src1.code() + 1, src2.code());
1546 ASSERT(CpuFeatures::IsEnabled(ARMv7));
1547 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
Kristian Monsen25f61362010-05-21 11:50:48 +01001548}
1549
Andrei Popescu31002712010-02-23 13:46:05 +00001550// Load/Store multiple instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001551void Assembler::ldm(BlockAddrMode am,
1552 Register base,
1553 RegList dst,
1554 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001555 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
Steve Blocka7e24c12009-10-30 11:49:00 +00001556 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1557
1558 addrmod4(cond | B27 | am | L, base, dst);
1559
Andrei Popescu31002712010-02-23 13:46:05 +00001560 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
Steve Blocka7e24c12009-10-30 11:49:00 +00001561 if (cond == al && (dst & pc.bit()) != 0) {
1562 // There is a slight chance that the ldm instruction was actually a call,
1563 // in which case it would be wrong to return into the constant pool; we
1564 // recognize this case by checking if the emission of the pool was blocked
1565 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1566 // the case, we emit a jump over the pool.
1567 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1568 }
1569}
1570
1571
1572void Assembler::stm(BlockAddrMode am,
1573 Register base,
1574 RegList src,
1575 Condition cond) {
1576 addrmod4(cond | B27 | am, base, src);
1577}
1578
1579
Andrei Popescu31002712010-02-23 13:46:05 +00001580// Exception-generating instructions and debugging support.
Steve Blocka7e24c12009-10-30 11:49:00 +00001581void Assembler::stop(const char* msg) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001582#ifndef __arm__
Steve Blocka7e24c12009-10-30 11:49:00 +00001583 // The simulator handles these special instructions and stops execution.
1584 emit(15 << 28 | ((intptr_t) msg));
Andrei Popescu402d9372010-02-26 13:31:12 +00001585#else // def __arm__
1586#ifdef CAN_USE_ARMV5_INSTRUCTIONS
Steve Blocka7e24c12009-10-30 11:49:00 +00001587 bkpt(0);
Andrei Popescu402d9372010-02-26 13:31:12 +00001588#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
1589 swi(0x9f0001);
1590#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1591#endif // def __arm__
Steve Blocka7e24c12009-10-30 11:49:00 +00001592}
1593
1594
1595void Assembler::bkpt(uint32_t imm16) { // v5 and above
1596 ASSERT(is_uint16(imm16));
1597 emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf));
1598}
1599
1600
1601void Assembler::swi(uint32_t imm24, Condition cond) {
1602 ASSERT(is_uint24(imm24));
1603 emit(cond | 15*B24 | imm24);
1604}
1605
1606
Andrei Popescu31002712010-02-23 13:46:05 +00001607// Coprocessor instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001608void Assembler::cdp(Coprocessor coproc,
1609 int opcode_1,
1610 CRegister crd,
1611 CRegister crn,
1612 CRegister crm,
1613 int opcode_2,
1614 Condition cond) {
1615 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1616 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1617 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1618}
1619
1620
1621void Assembler::cdp2(Coprocessor coproc,
1622 int opcode_1,
1623 CRegister crd,
1624 CRegister crn,
1625 CRegister crm,
1626 int opcode_2) { // v5 and above
1627 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv));
1628}
1629
1630
1631void Assembler::mcr(Coprocessor coproc,
1632 int opcode_1,
1633 Register rd,
1634 CRegister crn,
1635 CRegister crm,
1636 int opcode_2,
1637 Condition cond) {
1638 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1639 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1640 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1641}
1642
1643
1644void Assembler::mcr2(Coprocessor coproc,
1645 int opcode_1,
1646 Register rd,
1647 CRegister crn,
1648 CRegister crm,
1649 int opcode_2) { // v5 and above
1650 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1651}
1652
1653
1654void Assembler::mrc(Coprocessor coproc,
1655 int opcode_1,
1656 Register rd,
1657 CRegister crn,
1658 CRegister crm,
1659 int opcode_2,
1660 Condition cond) {
1661 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1662 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1663 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1664}
1665
1666
1667void Assembler::mrc2(Coprocessor coproc,
1668 int opcode_1,
1669 Register rd,
1670 CRegister crn,
1671 CRegister crm,
1672 int opcode_2) { // v5 and above
1673 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1674}
1675
1676
1677void Assembler::ldc(Coprocessor coproc,
1678 CRegister crd,
1679 const MemOperand& src,
1680 LFlag l,
1681 Condition cond) {
1682 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1683}
1684
1685
1686void Assembler::ldc(Coprocessor coproc,
1687 CRegister crd,
1688 Register rn,
1689 int option,
1690 LFlag l,
1691 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001692 // Unindexed addressing.
Steve Blocka7e24c12009-10-30 11:49:00 +00001693 ASSERT(is_uint8(option));
1694 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1695 coproc*B8 | (option & 255));
1696}
1697
1698
1699void Assembler::ldc2(Coprocessor coproc,
1700 CRegister crd,
1701 const MemOperand& src,
1702 LFlag l) { // v5 and above
1703 ldc(coproc, crd, src, l, static_cast<Condition>(nv));
1704}
1705
1706
1707void Assembler::ldc2(Coprocessor coproc,
1708 CRegister crd,
1709 Register rn,
1710 int option,
1711 LFlag l) { // v5 and above
1712 ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1713}
1714
1715
1716void Assembler::stc(Coprocessor coproc,
1717 CRegister crd,
1718 const MemOperand& dst,
1719 LFlag l,
1720 Condition cond) {
1721 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1722}
1723
1724
1725void Assembler::stc(Coprocessor coproc,
1726 CRegister crd,
1727 Register rn,
1728 int option,
1729 LFlag l,
1730 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001731 // Unindexed addressing.
Steve Blocka7e24c12009-10-30 11:49:00 +00001732 ASSERT(is_uint8(option));
1733 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1734 coproc*B8 | (option & 255));
1735}
1736
1737
1738void Assembler::stc2(Coprocessor
1739 coproc, CRegister crd,
1740 const MemOperand& dst,
1741 LFlag l) { // v5 and above
1742 stc(coproc, crd, dst, l, static_cast<Condition>(nv));
1743}
1744
1745
1746void Assembler::stc2(Coprocessor coproc,
1747 CRegister crd,
1748 Register rn,
1749 int option,
1750 LFlag l) { // v5 and above
1751 stc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1752}
1753
1754
Steve Blockd0582a62009-12-15 09:54:21 +00001755// Support for VFP.
Leon Clarked91b9f72010-01-27 17:25:45 +00001756void Assembler::vldr(const DwVfpRegister dst,
1757 const Register base,
1758 int offset,
1759 const Condition cond) {
1760 // Ddst = MEM(Rbase + offset).
1761 // Instruction details available in ARM DDI 0406A, A8-628.
1762 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1763 // Vdst(15-12) | 1011(11-8) | offset
1764 ASSERT(CpuFeatures::IsEnabled(VFP3));
1765 ASSERT(offset % 4 == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001766 ASSERT((offset / 4) < 256);
Leon Clarked91b9f72010-01-27 17:25:45 +00001767 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1768 0xB*B8 | ((offset / 4) & 255));
1769}
1770
1771
Steve Block6ded16b2010-05-10 14:33:55 +01001772void Assembler::vldr(const SwVfpRegister dst,
1773 const Register base,
1774 int offset,
1775 const Condition cond) {
1776 // Sdst = MEM(Rbase + offset).
1777 // Instruction details available in ARM DDI 0406A, A8-628.
1778 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1779 // Vdst(15-12) | 1010(11-8) | offset
1780 ASSERT(CpuFeatures::IsEnabled(VFP3));
1781 ASSERT(offset % 4 == 0);
1782 ASSERT((offset / 4) < 256);
1783 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1784 0xA*B8 | ((offset / 4) & 255));
1785}
1786
1787
Leon Clarked91b9f72010-01-27 17:25:45 +00001788void Assembler::vstr(const DwVfpRegister src,
1789 const Register base,
1790 int offset,
1791 const Condition cond) {
1792 // MEM(Rbase + offset) = Dsrc.
1793 // Instruction details available in ARM DDI 0406A, A8-786.
1794 // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) |
1795 // Vsrc(15-12) | 1011(11-8) | (offset/4)
1796 ASSERT(CpuFeatures::IsEnabled(VFP3));
1797 ASSERT(offset % 4 == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001798 ASSERT((offset / 4) < 256);
Leon Clarked91b9f72010-01-27 17:25:45 +00001799 emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 |
1800 0xB*B8 | ((offset / 4) & 255));
1801}
1802
1803
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001804static void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi) {
1805 uint64_t i;
1806 memcpy(&i, &d, 8);
1807
1808 *lo = i & 0xffffffff;
1809 *hi = i >> 32;
1810}
1811
1812// Only works for little endian floating point formats.
1813// We don't support VFP on the mixed endian floating point platform.
1814static bool FitsVMOVDoubleImmediate(double d, uint32_t *encoding) {
1815 ASSERT(CpuFeatures::IsEnabled(VFP3));
1816
1817 // VMOV can accept an immediate of the form:
1818 //
1819 // +/- m * 2^(-n) where 16 <= m <= 31 and 0 <= n <= 7
1820 //
1821 // The immediate is encoded using an 8-bit quantity, comprised of two
1822 // 4-bit fields. For an 8-bit immediate of the form:
1823 //
1824 // [abcdefgh]
1825 //
1826 // where a is the MSB and h is the LSB, an immediate 64-bit double can be
1827 // created of the form:
1828 //
1829 // [aBbbbbbb,bbcdefgh,00000000,00000000,
1830 // 00000000,00000000,00000000,00000000]
1831 //
1832 // where B = ~b.
1833 //
1834
1835 uint32_t lo, hi;
1836 DoubleAsTwoUInt32(d, &lo, &hi);
1837
1838 // The most obvious constraint is the long block of zeroes.
1839 if ((lo != 0) || ((hi & 0xffff) != 0)) {
1840 return false;
1841 }
1842
1843 // Bits 62:55 must be all clear or all set.
1844 if (((hi & 0x3fc00000) != 0) && ((hi & 0x3fc00000) != 0x3fc00000)) {
1845 return false;
1846 }
1847
1848 // Bit 63 must be NOT bit 62.
1849 if (((hi ^ (hi << 1)) & (0x40000000)) == 0) {
1850 return false;
1851 }
1852
1853 // Create the encoded immediate in the form:
1854 // [00000000,0000abcd,00000000,0000efgh]
1855 *encoding = (hi >> 16) & 0xf; // Low nybble.
1856 *encoding |= (hi >> 4) & 0x70000; // Low three bits of the high nybble.
1857 *encoding |= (hi >> 12) & 0x80000; // Top bit of the high nybble.
1858
1859 return true;
1860}
1861
1862
1863void Assembler::vmov(const DwVfpRegister dst,
1864 double imm,
1865 const Condition cond) {
1866 // Dd = immediate
1867 // Instruction details available in ARM DDI 0406B, A8-640.
1868 ASSERT(CpuFeatures::IsEnabled(VFP3));
1869
1870 uint32_t enc;
1871 if (FitsVMOVDoubleImmediate(imm, &enc)) {
1872 // The double can be encoded in the instruction.
1873 emit(cond | 0xE*B24 | 0xB*B20 | dst.code()*B12 | 0xB*B8 | enc);
1874 } else {
1875 // Synthesise the double from ARM immediates. This could be implemented
1876 // using vldr from a constant pool.
1877 uint32_t lo, hi;
1878 DoubleAsTwoUInt32(imm, &lo, &hi);
1879
1880 if (lo == hi) {
1881 // If the lo and hi parts of the double are equal, the literal is easier
1882 // to create. This is the case with 0.0.
1883 mov(ip, Operand(lo));
1884 vmov(dst, ip, ip);
1885 } else {
1886 // Move the low part of the double into the lower of the corresponsing S
1887 // registers of D register dst.
1888 mov(ip, Operand(lo));
1889 vmov(dst.low(), ip, cond);
1890
1891 // Move the high part of the double into the higher of the corresponsing S
1892 // registers of D register dst.
1893 mov(ip, Operand(hi));
1894 vmov(dst.high(), ip, cond);
1895 }
1896 }
1897}
1898
1899
1900void Assembler::vmov(const SwVfpRegister dst,
1901 const SwVfpRegister src,
1902 const Condition cond) {
1903 // Sd = Sm
1904 // Instruction details available in ARM DDI 0406B, A8-642.
1905 ASSERT(CpuFeatures::IsEnabled(VFP3));
1906 emit(cond | 0xE*B24 | 0xB*B20 |
1907 dst.code()*B12 | 0x5*B9 | B6 | src.code());
1908}
1909
1910
Leon Clarkee46be812010-01-19 14:06:41 +00001911void Assembler::vmov(const DwVfpRegister dst,
Steve Block8defd9f2010-07-08 12:39:36 +01001912 const DwVfpRegister src,
1913 const Condition cond) {
1914 // Dd = Dm
1915 // Instruction details available in ARM DDI 0406B, A8-642.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001916 ASSERT(CpuFeatures::IsEnabled(VFP3));
Steve Block8defd9f2010-07-08 12:39:36 +01001917 emit(cond | 0xE*B24 | 0xB*B20 |
1918 dst.code()*B12 | 0x5*B9 | B8 | B6 | src.code());
1919}
1920
1921
1922void Assembler::vmov(const DwVfpRegister dst,
Leon Clarkee46be812010-01-19 14:06:41 +00001923 const Register src1,
1924 const Register src2,
1925 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001926 // Dm = <Rt,Rt2>.
1927 // Instruction details available in ARM DDI 0406A, A8-646.
1928 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
1929 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1930 ASSERT(CpuFeatures::IsEnabled(VFP3));
1931 ASSERT(!src1.is(pc) && !src2.is(pc));
1932 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
1933 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
1934}
1935
1936
Leon Clarkee46be812010-01-19 14:06:41 +00001937void Assembler::vmov(const Register dst1,
1938 const Register dst2,
1939 const DwVfpRegister src,
1940 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001941 // <Rt,Rt2> = Dm.
1942 // Instruction details available in ARM DDI 0406A, A8-646.
1943 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
1944 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1945 ASSERT(CpuFeatures::IsEnabled(VFP3));
1946 ASSERT(!dst1.is(pc) && !dst2.is(pc));
1947 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
1948 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
1949}
1950
1951
Leon Clarkee46be812010-01-19 14:06:41 +00001952void Assembler::vmov(const SwVfpRegister dst,
Steve Blockd0582a62009-12-15 09:54:21 +00001953 const Register src,
Steve Blockd0582a62009-12-15 09:54:21 +00001954 const Condition cond) {
1955 // Sn = Rt.
1956 // Instruction details available in ARM DDI 0406A, A8-642.
1957 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
1958 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1959 ASSERT(CpuFeatures::IsEnabled(VFP3));
1960 ASSERT(!src.is(pc));
1961 emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 |
1962 src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4);
1963}
1964
1965
Leon Clarkee46be812010-01-19 14:06:41 +00001966void Assembler::vmov(const Register dst,
1967 const SwVfpRegister src,
Steve Blockd0582a62009-12-15 09:54:21 +00001968 const Condition cond) {
1969 // Rt = Sn.
1970 // Instruction details available in ARM DDI 0406A, A8-642.
1971 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
1972 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1973 ASSERT(CpuFeatures::IsEnabled(VFP3));
1974 ASSERT(!dst.is(pc));
1975 emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 |
1976 dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4);
1977}
1978
1979
Steve Block6ded16b2010-05-10 14:33:55 +01001980// Type of data to read from or write to VFP register.
1981// Used as specifier in generic vcvt instruction.
1982enum VFPType { S32, U32, F32, F64 };
1983
1984
1985static bool IsSignedVFPType(VFPType type) {
1986 switch (type) {
1987 case S32:
1988 return true;
1989 case U32:
1990 return false;
1991 default:
1992 UNREACHABLE();
1993 return false;
1994 }
Steve Blockd0582a62009-12-15 09:54:21 +00001995}
1996
1997
Steve Block6ded16b2010-05-10 14:33:55 +01001998static bool IsIntegerVFPType(VFPType type) {
1999 switch (type) {
2000 case S32:
2001 case U32:
2002 return true;
2003 case F32:
2004 case F64:
2005 return false;
2006 default:
2007 UNREACHABLE();
2008 return false;
2009 }
2010}
2011
2012
2013static bool IsDoubleVFPType(VFPType type) {
2014 switch (type) {
2015 case F32:
2016 return false;
2017 case F64:
2018 return true;
2019 default:
2020 UNREACHABLE();
2021 return false;
2022 }
2023}
2024
2025
2026// Depending on split_last_bit split binary representation of reg_code into Vm:M
2027// or M:Vm form (where M is single bit).
2028static void SplitRegCode(bool split_last_bit,
2029 int reg_code,
2030 int* vm,
2031 int* m) {
2032 if (split_last_bit) {
2033 *m = reg_code & 0x1;
2034 *vm = reg_code >> 1;
2035 } else {
2036 *m = (reg_code & 0x10) >> 4;
2037 *vm = reg_code & 0x0F;
2038 }
2039}
2040
2041
2042// Encode vcvt.src_type.dst_type instruction.
2043static Instr EncodeVCVT(const VFPType dst_type,
2044 const int dst_code,
2045 const VFPType src_type,
2046 const int src_code,
2047 const Condition cond) {
2048 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
2049 // Conversion between IEEE floating point and 32-bit integer.
2050 // Instruction details available in ARM DDI 0406B, A8.6.295.
2051 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
2052 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2053 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
2054
2055 int sz, opc2, D, Vd, M, Vm, op;
2056
2057 if (IsIntegerVFPType(dst_type)) {
2058 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
2059 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
2060 op = 1; // round towards zero
2061 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
2062 SplitRegCode(true, dst_code, &Vd, &D);
2063 } else {
2064 ASSERT(IsIntegerVFPType(src_type));
2065
2066 opc2 = 0x0;
2067 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
2068 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
2069 SplitRegCode(true, src_code, &Vm, &M);
2070 SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D);
2071 }
2072
2073 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
2074 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
2075 } else {
2076 // Conversion between IEEE double and single precision.
2077 // Instruction details available in ARM DDI 0406B, A8.6.298.
2078 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
2079 // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
2080 int sz, D, Vd, M, Vm;
2081
2082 ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type));
2083 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
2084 SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D);
2085 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
2086
2087 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
2088 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
2089 }
2090}
2091
2092
2093void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
2094 const SwVfpRegister src,
2095 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00002096 ASSERT(CpuFeatures::IsEnabled(VFP3));
Steve Block6ded16b2010-05-10 14:33:55 +01002097 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond));
2098}
2099
2100
2101void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
2102 const SwVfpRegister src,
2103 const Condition cond) {
2104 ASSERT(CpuFeatures::IsEnabled(VFP3));
2105 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond));
2106}
2107
2108
2109void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
2110 const SwVfpRegister src,
2111 const Condition cond) {
2112 ASSERT(CpuFeatures::IsEnabled(VFP3));
2113 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond));
2114}
2115
2116
2117void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
2118 const DwVfpRegister src,
2119 const Condition cond) {
2120 ASSERT(CpuFeatures::IsEnabled(VFP3));
2121 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond));
2122}
2123
2124
2125void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
2126 const DwVfpRegister src,
2127 const Condition cond) {
2128 ASSERT(CpuFeatures::IsEnabled(VFP3));
2129 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond));
2130}
2131
2132
2133void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
2134 const SwVfpRegister src,
2135 const Condition cond) {
2136 ASSERT(CpuFeatures::IsEnabled(VFP3));
2137 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond));
2138}
2139
2140
2141void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
2142 const DwVfpRegister src,
2143 const Condition cond) {
2144 ASSERT(CpuFeatures::IsEnabled(VFP3));
2145 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond));
Steve Blockd0582a62009-12-15 09:54:21 +00002146}
2147
2148
Leon Clarkee46be812010-01-19 14:06:41 +00002149void Assembler::vadd(const DwVfpRegister dst,
2150 const DwVfpRegister src1,
2151 const DwVfpRegister src2,
2152 const Condition cond) {
2153 // Dd = vadd(Dn, Dm) double precision floating point addition.
Steve Blockd0582a62009-12-15 09:54:21 +00002154 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2155 // Instruction details available in ARM DDI 0406A, A8-536.
2156 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2157 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2158 ASSERT(CpuFeatures::IsEnabled(VFP3));
2159 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2160 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2161}
2162
2163
Leon Clarkee46be812010-01-19 14:06:41 +00002164void Assembler::vsub(const DwVfpRegister dst,
2165 const DwVfpRegister src1,
2166 const DwVfpRegister src2,
2167 const Condition cond) {
2168 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
Steve Blockd0582a62009-12-15 09:54:21 +00002169 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2170 // Instruction details available in ARM DDI 0406A, A8-784.
2171 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
2172 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
2173 ASSERT(CpuFeatures::IsEnabled(VFP3));
2174 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
2175 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2176}
2177
2178
Leon Clarkee46be812010-01-19 14:06:41 +00002179void Assembler::vmul(const DwVfpRegister dst,
2180 const DwVfpRegister src1,
2181 const DwVfpRegister src2,
2182 const Condition cond) {
2183 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
Steve Blockd0582a62009-12-15 09:54:21 +00002184 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2185 // Instruction details available in ARM DDI 0406A, A8-784.
2186 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
2187 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2188 ASSERT(CpuFeatures::IsEnabled(VFP3));
2189 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
2190 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2191}
2192
2193
Leon Clarkee46be812010-01-19 14:06:41 +00002194void Assembler::vdiv(const DwVfpRegister dst,
2195 const DwVfpRegister src1,
2196 const DwVfpRegister src2,
2197 const Condition cond) {
2198 // Dd = vdiv(Dn, Dm) double precision floating point division.
Steve Blockd0582a62009-12-15 09:54:21 +00002199 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
2200 // Instruction details available in ARM DDI 0406A, A8-584.
2201 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
2202 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
2203 ASSERT(CpuFeatures::IsEnabled(VFP3));
2204 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
2205 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
2206}
2207
2208
Leon Clarkee46be812010-01-19 14:06:41 +00002209void Assembler::vcmp(const DwVfpRegister src1,
2210 const DwVfpRegister src2,
Steve Blockd0582a62009-12-15 09:54:21 +00002211 const SBit s,
2212 const Condition cond) {
2213 // vcmp(Dd, Dm) double precision floating point comparison.
2214 // Instruction details available in ARM DDI 0406A, A8-570.
2215 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
2216 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0)
2217 ASSERT(CpuFeatures::IsEnabled(VFP3));
2218 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
2219 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2220}
2221
2222
2223void Assembler::vmrs(Register dst, Condition cond) {
2224 // Instruction details available in ARM DDI 0406A, A8-652.
2225 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2226 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
2227 ASSERT(CpuFeatures::IsEnabled(VFP3));
2228 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2229 dst.code()*B12 | 0xA*B8 | B4);
2230}
2231
2232
Steve Block8defd9f2010-07-08 12:39:36 +01002233
2234void Assembler::vsqrt(const DwVfpRegister dst,
2235 const DwVfpRegister src,
2236 const Condition cond) {
2237 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0001 (19-16) |
2238 // Vd(15-12) | 101(11-9) | sz(8)=1 | 11 (7-6) | M(5)=? | 0(4) | Vm(3-0)
2239 ASSERT(CpuFeatures::IsEnabled(VFP3));
2240 emit(cond | 0xE*B24 | B23 | 0x3*B20 | B16 |
2241 dst.code()*B12 | 0x5*B9 | B8 | 3*B6 | src.code());
2242}
2243
2244
Andrei Popescu31002712010-02-23 13:46:05 +00002245// Pseudo instructions.
Steve Block6ded16b2010-05-10 14:33:55 +01002246void Assembler::nop(int type) {
2247 // This is mov rx, rx.
2248 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2249 emit(al | 13*B21 | type*B12 | type);
2250}
2251
2252
Steve Blockd0582a62009-12-15 09:54:21 +00002253bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2254 uint32_t dummy1;
2255 uint32_t dummy2;
2256 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2257}
2258
2259
2260void Assembler::BlockConstPoolFor(int instructions) {
2261 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2262}
2263
2264
Andrei Popescu31002712010-02-23 13:46:05 +00002265// Debugging.
Steve Blocka7e24c12009-10-30 11:49:00 +00002266void Assembler::RecordJSReturn() {
2267 WriteRecordedPositions();
2268 CheckBuffer();
2269 RecordRelocInfo(RelocInfo::JS_RETURN);
2270}
2271
2272
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002273void Assembler::RecordDebugBreakSlot() {
2274 WriteRecordedPositions();
2275 CheckBuffer();
2276 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2277}
2278
2279
Steve Blocka7e24c12009-10-30 11:49:00 +00002280void Assembler::RecordComment(const char* msg) {
2281 if (FLAG_debug_code) {
2282 CheckBuffer();
2283 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
2284 }
2285}
2286
2287
2288void Assembler::RecordPosition(int pos) {
2289 if (pos == RelocInfo::kNoPosition) return;
2290 ASSERT(pos >= 0);
2291 current_position_ = pos;
2292}
2293
2294
2295void Assembler::RecordStatementPosition(int pos) {
2296 if (pos == RelocInfo::kNoPosition) return;
2297 ASSERT(pos >= 0);
2298 current_statement_position_ = pos;
2299}
2300
2301
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002302bool Assembler::WriteRecordedPositions() {
2303 bool written = false;
2304
Steve Blocka7e24c12009-10-30 11:49:00 +00002305 // Write the statement position if it is different from what was written last
2306 // time.
2307 if (current_statement_position_ != written_statement_position_) {
2308 CheckBuffer();
2309 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_);
2310 written_statement_position_ = current_statement_position_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002311 written = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002312 }
2313
2314 // Write the position if it is different from what was written last time and
2315 // also different from the written statement position.
2316 if (current_position_ != written_position_ &&
2317 current_position_ != written_statement_position_) {
2318 CheckBuffer();
2319 RecordRelocInfo(RelocInfo::POSITION, current_position_);
2320 written_position_ = current_position_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002321 written = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002322 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002323
2324 // Return whether something was written.
2325 return written;
Steve Blocka7e24c12009-10-30 11:49:00 +00002326}
2327
2328
2329void Assembler::GrowBuffer() {
2330 if (!own_buffer_) FATAL("external code buffer is too small");
2331
Andrei Popescu31002712010-02-23 13:46:05 +00002332 // Compute new buffer size.
Steve Blocka7e24c12009-10-30 11:49:00 +00002333 CodeDesc desc; // the new buffer
2334 if (buffer_size_ < 4*KB) {
2335 desc.buffer_size = 4*KB;
2336 } else if (buffer_size_ < 1*MB) {
2337 desc.buffer_size = 2*buffer_size_;
2338 } else {
2339 desc.buffer_size = buffer_size_ + 1*MB;
2340 }
2341 CHECK_GT(desc.buffer_size, 0); // no overflow
2342
Andrei Popescu31002712010-02-23 13:46:05 +00002343 // Setup new buffer.
Steve Blocka7e24c12009-10-30 11:49:00 +00002344 desc.buffer = NewArray<byte>(desc.buffer_size);
2345
2346 desc.instr_size = pc_offset();
2347 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2348
Andrei Popescu31002712010-02-23 13:46:05 +00002349 // Copy the data.
Steve Blocka7e24c12009-10-30 11:49:00 +00002350 int pc_delta = desc.buffer - buffer_;
2351 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2352 memmove(desc.buffer, buffer_, desc.instr_size);
2353 memmove(reloc_info_writer.pos() + rc_delta,
2354 reloc_info_writer.pos(), desc.reloc_size);
2355
Andrei Popescu31002712010-02-23 13:46:05 +00002356 // Switch buffers.
Steve Blocka7e24c12009-10-30 11:49:00 +00002357 DeleteArray(buffer_);
2358 buffer_ = desc.buffer;
2359 buffer_size_ = desc.buffer_size;
2360 pc_ += pc_delta;
2361 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2362 reloc_info_writer.last_pc() + pc_delta);
2363
Andrei Popescu31002712010-02-23 13:46:05 +00002364 // None of our relocation types are pc relative pointing outside the code
Steve Blocka7e24c12009-10-30 11:49:00 +00002365 // buffer nor pc absolute pointing inside the code buffer, so there is no need
Andrei Popescu31002712010-02-23 13:46:05 +00002366 // to relocate any emitted relocation entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002367
Andrei Popescu31002712010-02-23 13:46:05 +00002368 // Relocate pending relocation entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002369 for (int i = 0; i < num_prinfo_; i++) {
2370 RelocInfo& rinfo = prinfo_[i];
2371 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2372 rinfo.rmode() != RelocInfo::POSITION);
2373 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2374 rinfo.set_pc(rinfo.pc() + pc_delta);
2375 }
2376 }
2377}
2378
2379
2380void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2381 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002382 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
Andrei Popescu31002712010-02-23 13:46:05 +00002383 // Adjust code for new modes.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002384 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2385 || RelocInfo::IsJSReturn(rmode)
Steve Blocka7e24c12009-10-30 11:49:00 +00002386 || RelocInfo::IsComment(rmode)
2387 || RelocInfo::IsPosition(rmode));
Andrei Popescu31002712010-02-23 13:46:05 +00002388 // These modes do not need an entry in the constant pool.
Steve Blocka7e24c12009-10-30 11:49:00 +00002389 } else {
2390 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2391 prinfo_[num_prinfo_++] = rinfo;
2392 // Make sure the constant pool is not emitted in place of the next
Andrei Popescu31002712010-02-23 13:46:05 +00002393 // instruction for which we just recorded relocation info.
Steve Blocka7e24c12009-10-30 11:49:00 +00002394 BlockConstPoolBefore(pc_offset() + kInstrSize);
2395 }
2396 if (rinfo.rmode() != RelocInfo::NONE) {
2397 // Don't record external references unless the heap will be serialized.
Steve Blockd0582a62009-12-15 09:54:21 +00002398 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2399#ifdef DEBUG
2400 if (!Serializer::enabled()) {
2401 Serializer::TooLateToEnableNow();
2402 }
2403#endif
2404 if (!Serializer::enabled() && !FLAG_debug_code) {
2405 return;
2406 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002407 }
2408 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2409 reloc_info_writer.Write(&rinfo);
2410 }
2411}
2412
2413
2414void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2415 // Calculate the offset of the next check. It will be overwritten
2416 // when a const pool is generated or when const pools are being
2417 // blocked for a specific range.
2418 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2419
Andrei Popescu31002712010-02-23 13:46:05 +00002420 // There is nothing to do if there are no pending relocation info entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002421 if (num_prinfo_ == 0) return;
2422
2423 // We emit a constant pool at regular intervals of about kDistBetweenPools
2424 // or when requested by parameter force_emit (e.g. after each function).
2425 // We prefer not to emit a jump unless the max distance is reached or if we
2426 // are running low on slots, which can happen if a lot of constants are being
2427 // emitted (e.g. --debug-code and many static references).
2428 int dist = pc_offset() - last_const_pool_end_;
2429 if (!force_emit && dist < kMaxDistBetweenPools &&
2430 (require_jump || dist < kDistBetweenPools) &&
2431 // TODO(1236125): Cleanup the "magic" number below. We know that
2432 // the code generation will test every kCheckConstIntervalInst.
2433 // Thus we are safe as long as we generate less than 7 constant
2434 // entries per instruction.
2435 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2436 return;
2437 }
2438
2439 // If we did not return by now, we need to emit the constant pool soon.
2440
2441 // However, some small sequences of instructions must not be broken up by the
2442 // insertion of a constant pool; such sequences are protected by setting
Steve Block6ded16b2010-05-10 14:33:55 +01002443 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2444 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2445 // no_const_pool_before_.
2446 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
Andrei Popescu31002712010-02-23 13:46:05 +00002447 // Emission is currently blocked; make sure we try again as soon as
2448 // possible.
Steve Block6ded16b2010-05-10 14:33:55 +01002449 if (const_pool_blocked_nesting_ > 0) {
2450 next_buffer_check_ = pc_offset() + kInstrSize;
2451 } else {
2452 next_buffer_check_ = no_const_pool_before_;
2453 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002454
Andrei Popescu31002712010-02-23 13:46:05 +00002455 // Something is wrong if emission is forced and blocked at the same time.
Steve Blocka7e24c12009-10-30 11:49:00 +00002456 ASSERT(!force_emit);
2457 return;
2458 }
2459
2460 int jump_instr = require_jump ? kInstrSize : 0;
2461
2462 // Check that the code buffer is large enough before emitting the constant
2463 // pool and relocation information (include the jump over the pool and the
2464 // constant pool marker).
2465 int max_needed_space =
2466 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2467 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2468
Andrei Popescu31002712010-02-23 13:46:05 +00002469 // Block recursive calls to CheckConstPool.
Steve Blocka7e24c12009-10-30 11:49:00 +00002470 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2471 num_prinfo_*kInstrSize);
2472 // Don't bother to check for the emit calls below.
2473 next_buffer_check_ = no_const_pool_before_;
2474
Andrei Popescu31002712010-02-23 13:46:05 +00002475 // Emit jump over constant pool if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002476 Label after_pool;
2477 if (require_jump) b(&after_pool);
2478
2479 RecordComment("[ Constant Pool");
2480
Andrei Popescu31002712010-02-23 13:46:05 +00002481 // Put down constant pool marker "Undefined instruction" as specified by
2482 // A3.1 Instruction set encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +00002483 emit(0x03000000 | num_prinfo_);
2484
Andrei Popescu31002712010-02-23 13:46:05 +00002485 // Emit constant pool entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002486 for (int i = 0; i < num_prinfo_; i++) {
2487 RelocInfo& rinfo = prinfo_[i];
2488 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2489 rinfo.rmode() != RelocInfo::POSITION &&
2490 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
2491 Instr instr = instr_at(rinfo.pc());
2492
Andrei Popescu31002712010-02-23 13:46:05 +00002493 // Instruction to patch must be a ldr/str [pc, #offset].
2494 // P and U set, B and W clear, Rn == pc, offset12 still 0.
Steve Blocka7e24c12009-10-30 11:49:00 +00002495 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) ==
2496 (2*B25 | P | U | pc.code()*B16));
2497 int delta = pc_ - rinfo.pc() - 8;
2498 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2499 if (delta < 0) {
2500 instr &= ~U;
2501 delta = -delta;
2502 }
2503 ASSERT(is_uint12(delta));
2504 instr_at_put(rinfo.pc(), instr + delta);
2505 emit(rinfo.data());
2506 }
2507 num_prinfo_ = 0;
2508 last_const_pool_end_ = pc_offset();
2509
2510 RecordComment("]");
2511
2512 if (after_pool.is_linked()) {
2513 bind(&after_pool);
2514 }
2515
2516 // Since a constant pool was just emitted, move the check offset forward by
2517 // the standard interval.
2518 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2519}
2520
2521
2522} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002523
2524#endif // V8_TARGET_ARCH_ARM