blob: 025f28e551399d4e56b41c5eb5ee769044d9c22a [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;
Leon Clarkef7060e22010-06-03 12:02:55 +0100282// A mask for the Rd register for push, pop, ldr, str instructions.
283const Instr kRdMask = 0x0000f000;
284static const int kRdShift = 12;
285static const Instr kLdrRegFpOffsetPattern =
286 al | B26 | L | Offset | fp.code() * B16;
287static const Instr kStrRegFpOffsetPattern =
288 al | B26 | Offset | fp.code() * B16;
289static const Instr kLdrRegFpNegOffsetPattern =
290 al | B26 | L | NegOffset | fp.code() * B16;
291static const Instr kStrRegFpNegOffsetPattern =
292 al | B26 | NegOffset | fp.code() * B16;
293static const Instr kLdrStrInstrTypeMask = 0xffff0000;
294static const Instr kLdrStrInstrArgumentMask = 0x0000ffff;
295static const Instr kLdrStrOffsetMask = 0x00000fff;
Steve Blocka7e24c12009-10-30 11:49:00 +0000296
Andrei Popescu31002712010-02-23 13:46:05 +0000297// Spare buffer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000298static const int kMinimalBufferSize = 4*KB;
299static byte* spare_buffer_ = NULL;
300
301Assembler::Assembler(void* buffer, int buffer_size) {
302 if (buffer == NULL) {
Andrei Popescu31002712010-02-23 13:46:05 +0000303 // Do our own buffer management.
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 if (buffer_size <= kMinimalBufferSize) {
305 buffer_size = kMinimalBufferSize;
306
307 if (spare_buffer_ != NULL) {
308 buffer = spare_buffer_;
309 spare_buffer_ = NULL;
310 }
311 }
312 if (buffer == NULL) {
313 buffer_ = NewArray<byte>(buffer_size);
314 } else {
315 buffer_ = static_cast<byte*>(buffer);
316 }
317 buffer_size_ = buffer_size;
318 own_buffer_ = true;
319
320 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000321 // Use externally provided buffer instead.
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 ASSERT(buffer_size > 0);
323 buffer_ = static_cast<byte*>(buffer);
324 buffer_size_ = buffer_size;
325 own_buffer_ = false;
326 }
327
Andrei Popescu31002712010-02-23 13:46:05 +0000328 // Setup buffer pointers.
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 ASSERT(buffer_ != NULL);
330 pc_ = buffer_;
331 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
332 num_prinfo_ = 0;
333 next_buffer_check_ = 0;
Steve Block6ded16b2010-05-10 14:33:55 +0100334 const_pool_blocked_nesting_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 no_const_pool_before_ = 0;
336 last_const_pool_end_ = 0;
337 last_bound_pos_ = 0;
338 current_statement_position_ = RelocInfo::kNoPosition;
339 current_position_ = RelocInfo::kNoPosition;
340 written_statement_position_ = current_statement_position_;
341 written_position_ = current_position_;
342}
343
344
345Assembler::~Assembler() {
Steve Block6ded16b2010-05-10 14:33:55 +0100346 ASSERT(const_pool_blocked_nesting_ == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 if (own_buffer_) {
348 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
349 spare_buffer_ = buffer_;
350 } else {
351 DeleteArray(buffer_);
352 }
353 }
354}
355
356
357void Assembler::GetCode(CodeDesc* desc) {
Andrei Popescu31002712010-02-23 13:46:05 +0000358 // Emit constant pool if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 CheckConstPool(true, false);
360 ASSERT(num_prinfo_ == 0);
361
Andrei Popescu31002712010-02-23 13:46:05 +0000362 // Setup code descriptor.
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 desc->buffer = buffer_;
364 desc->buffer_size = buffer_size_;
365 desc->instr_size = pc_offset();
366 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
367}
368
369
370void Assembler::Align(int m) {
371 ASSERT(m >= 4 && IsPowerOf2(m));
372 while ((pc_offset() & (m - 1)) != 0) {
373 nop();
374 }
375}
376
377
Steve Block6ded16b2010-05-10 14:33:55 +0100378bool Assembler::IsNop(Instr instr, int type) {
379 // Check for mov rx, rx.
380 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
381 return instr == (al | 13*B21 | type*B12 | type);
382}
383
384
385bool Assembler::IsBranch(Instr instr) {
386 return (instr & (B27 | B25)) == (B27 | B25);
387}
388
389
390int Assembler::GetBranchOffset(Instr instr) {
391 ASSERT(IsBranch(instr));
392 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
393 // with 4 to get the offset in bytes.
394 return ((instr & Imm24Mask) << 8) >> 6;
395}
396
397
398bool Assembler::IsLdrRegisterImmediate(Instr instr) {
399 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
400}
401
402
403int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
404 ASSERT(IsLdrRegisterImmediate(instr));
405 bool positive = (instr & B23) == B23;
406 int offset = instr & Off12Mask; // Zero extended offset.
407 return positive ? offset : -offset;
408}
409
410
411Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
412 ASSERT(IsLdrRegisterImmediate(instr));
413 bool positive = offset >= 0;
414 if (!positive) offset = -offset;
415 ASSERT(is_uint12(offset));
416 // Set bit indicating whether the offset should be added.
417 instr = (instr & ~B23) | (positive ? B23 : 0);
418 // Set the actual offset.
419 return (instr & ~Off12Mask) | offset;
420}
421
422
Leon Clarkef7060e22010-06-03 12:02:55 +0100423Register Assembler::GetRd(Instr instr) {
424 Register reg;
425 reg.code_ = ((instr & kRdMask) >> kRdShift);
426 return reg;
427}
428
429
430bool Assembler::IsPush(Instr instr) {
431 return ((instr & ~kRdMask) == kPushRegPattern);
432}
433
434
435bool Assembler::IsPop(Instr instr) {
436 return ((instr & ~kRdMask) == kPopRegPattern);
437}
438
439
440bool Assembler::IsStrRegFpOffset(Instr instr) {
441 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpOffsetPattern);
442}
443
444
445bool Assembler::IsLdrRegFpOffset(Instr instr) {
446 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpOffsetPattern);
447}
448
449
450bool Assembler::IsStrRegFpNegOffset(Instr instr) {
451 return ((instr & kLdrStrInstrTypeMask) == kStrRegFpNegOffsetPattern);
452}
453
454
455bool Assembler::IsLdrRegFpNegOffset(Instr instr) {
456 return ((instr & kLdrStrInstrTypeMask) == kLdrRegFpNegOffsetPattern);
457}
458
459
Steve Blocka7e24c12009-10-30 11:49:00 +0000460// Labels refer to positions in the (to be) generated code.
461// There are bound, linked, and unused labels.
462//
463// Bound labels refer to known positions in the already
464// generated code. pos() is the position the label refers to.
465//
466// Linked labels refer to unknown positions in the code
467// to be generated; pos() is the position of the last
468// instruction using the label.
469
470
471// The link chain is terminated by a negative code position (must be aligned)
472const int kEndOfChain = -4;
473
474
475int Assembler::target_at(int pos) {
476 Instr instr = instr_at(pos);
477 if ((instr & ~Imm24Mask) == 0) {
478 // Emitted label constant, not part of a branch.
479 return instr - (Code::kHeaderSize - kHeapObjectTag);
480 }
481 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
482 int imm26 = ((instr & Imm24Mask) << 8) >> 6;
Steve Block6ded16b2010-05-10 14:33:55 +0100483 if ((instr & CondMask) == nv && (instr & B24) != 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 // blx uses bit 24 to encode bit 2 of imm26
485 imm26 += 2;
Steve Block6ded16b2010-05-10 14:33:55 +0100486 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 return pos + kPcLoadDelta + imm26;
488}
489
490
491void Assembler::target_at_put(int pos, int target_pos) {
492 Instr instr = instr_at(pos);
493 if ((instr & ~Imm24Mask) == 0) {
494 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
495 // Emitted label constant, not part of a branch.
496 // Make label relative to Code* of generated Code object.
497 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
498 return;
499 }
500 int imm26 = target_pos - (pos + kPcLoadDelta);
501 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
502 if ((instr & CondMask) == nv) {
503 // blx uses bit 24 to encode bit 2 of imm26
504 ASSERT((imm26 & 1) == 0);
505 instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24;
506 } else {
507 ASSERT((imm26 & 3) == 0);
508 instr &= ~Imm24Mask;
509 }
510 int imm24 = imm26 >> 2;
511 ASSERT(is_int24(imm24));
512 instr_at_put(pos, instr | (imm24 & Imm24Mask));
513}
514
515
516void Assembler::print(Label* L) {
517 if (L->is_unused()) {
518 PrintF("unused label\n");
519 } else if (L->is_bound()) {
520 PrintF("bound label to %d\n", L->pos());
521 } else if (L->is_linked()) {
522 Label l = *L;
523 PrintF("unbound label");
524 while (l.is_linked()) {
525 PrintF("@ %d ", l.pos());
526 Instr instr = instr_at(l.pos());
527 if ((instr & ~Imm24Mask) == 0) {
528 PrintF("value\n");
529 } else {
530 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
531 int cond = instr & CondMask;
532 const char* b;
533 const char* c;
534 if (cond == nv) {
535 b = "blx";
536 c = "";
537 } else {
538 if ((instr & B24) != 0)
539 b = "bl";
540 else
541 b = "b";
542
543 switch (cond) {
544 case eq: c = "eq"; break;
545 case ne: c = "ne"; break;
546 case hs: c = "hs"; break;
547 case lo: c = "lo"; break;
548 case mi: c = "mi"; break;
549 case pl: c = "pl"; break;
550 case vs: c = "vs"; break;
551 case vc: c = "vc"; break;
552 case hi: c = "hi"; break;
553 case ls: c = "ls"; break;
554 case ge: c = "ge"; break;
555 case lt: c = "lt"; break;
556 case gt: c = "gt"; break;
557 case le: c = "le"; break;
558 case al: c = ""; break;
559 default:
560 c = "";
561 UNREACHABLE();
562 }
563 }
564 PrintF("%s%s\n", b, c);
565 }
566 next(&l);
567 }
568 } else {
569 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
570 }
571}
572
573
574void Assembler::bind_to(Label* L, int pos) {
575 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
576 while (L->is_linked()) {
577 int fixup_pos = L->pos();
578 next(L); // call next before overwriting link with target at fixup_pos
579 target_at_put(fixup_pos, pos);
580 }
581 L->bind_to(pos);
582
583 // Keep track of the last bound label so we don't eliminate any instructions
584 // before a bound label.
585 if (pos > last_bound_pos_)
586 last_bound_pos_ = pos;
587}
588
589
590void Assembler::link_to(Label* L, Label* appendix) {
591 if (appendix->is_linked()) {
592 if (L->is_linked()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000593 // Append appendix to L's list.
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 int fixup_pos;
595 int link = L->pos();
596 do {
597 fixup_pos = link;
598 link = target_at(fixup_pos);
599 } while (link > 0);
600 ASSERT(link == kEndOfChain);
601 target_at_put(fixup_pos, appendix->pos());
602 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000603 // L is empty, simply use appendix.
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 *L = *appendix;
605 }
606 }
607 appendix->Unuse(); // appendix should not be used anymore
608}
609
610
611void Assembler::bind(Label* L) {
612 ASSERT(!L->is_bound()); // label can only be bound once
613 bind_to(L, pc_offset());
614}
615
616
617void Assembler::next(Label* L) {
618 ASSERT(L->is_linked());
619 int link = target_at(L->pos());
620 if (link > 0) {
621 L->link_to(link);
622 } else {
623 ASSERT(link == kEndOfChain);
624 L->Unuse();
625 }
626}
627
628
Andrei Popescu31002712010-02-23 13:46:05 +0000629// Low-level code emission routines depending on the addressing mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000630static bool fits_shifter(uint32_t imm32,
631 uint32_t* rotate_imm,
632 uint32_t* immed_8,
633 Instr* instr) {
Andrei Popescu31002712010-02-23 13:46:05 +0000634 // imm32 must be unsigned.
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 for (int rot = 0; rot < 16; rot++) {
636 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
637 if ((imm8 <= 0xff)) {
638 *rotate_imm = rot;
639 *immed_8 = imm8;
640 return true;
641 }
642 }
Andrei Popescu31002712010-02-23 13:46:05 +0000643 // If the opcode is mov or mvn and if ~imm32 fits, change the opcode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000644 if (instr != NULL && (*instr & 0xd*B21) == 0xd*B21) {
645 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
646 *instr ^= 0x2*B21;
647 return true;
648 }
649 }
650 return false;
651}
652
653
654// We have to use the temporary register for things that can be relocated even
655// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
656// space. There is no guarantee that the relocated location can be similarly
657// encoded.
658static bool MustUseIp(RelocInfo::Mode rmode) {
659 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
Steve Blockd0582a62009-12-15 09:54:21 +0000660#ifdef DEBUG
661 if (!Serializer::enabled()) {
662 Serializer::TooLateToEnableNow();
663 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000664#endif // def DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 return Serializer::enabled();
666 } else if (rmode == RelocInfo::NONE) {
667 return false;
668 }
669 return true;
670}
671
672
673void Assembler::addrmod1(Instr instr,
674 Register rn,
675 Register rd,
676 const Operand& x) {
677 CheckBuffer();
678 ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0);
679 if (!x.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000680 // Immediate.
Steve Blocka7e24c12009-10-30 11:49:00 +0000681 uint32_t rotate_imm;
682 uint32_t immed_8;
683 if (MustUseIp(x.rmode_) ||
684 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
685 // The immediate operand cannot be encoded as a shifter operand, so load
686 // it first to register ip and change the original instruction to use ip.
687 // However, if the original instruction is a 'mov rd, x' (not setting the
Andrei Popescu31002712010-02-23 13:46:05 +0000688 // condition code), then replace it with a 'ldr rd, [pc]'.
Steve Blocka7e24c12009-10-30 11:49:00 +0000689 RecordRelocInfo(x.rmode_, x.imm32_);
690 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
691 Condition cond = static_cast<Condition>(instr & CondMask);
692 if ((instr & ~CondMask) == 13*B21) { // mov, S not set
693 ldr(rd, MemOperand(pc, 0), cond);
694 } else {
695 ldr(ip, MemOperand(pc, 0), cond);
696 addrmod1(instr, rn, rd, Operand(ip));
697 }
698 return;
699 }
700 instr |= I | rotate_imm*B8 | immed_8;
701 } else if (!x.rs_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000702 // Immediate shift.
Steve Blocka7e24c12009-10-30 11:49:00 +0000703 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
704 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000705 // Register shift.
Steve Blocka7e24c12009-10-30 11:49:00 +0000706 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
707 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
708 }
709 emit(instr | rn.code()*B16 | rd.code()*B12);
710 if (rn.is(pc) || x.rm_.is(pc))
Andrei Popescu31002712010-02-23 13:46:05 +0000711 // Block constant pool emission for one instruction after reading pc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 BlockConstPoolBefore(pc_offset() + kInstrSize);
713}
714
715
716void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
717 ASSERT((instr & ~(CondMask | B | L)) == B26);
718 int am = x.am_;
719 if (!x.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000720 // Immediate offset.
Steve Blocka7e24c12009-10-30 11:49:00 +0000721 int offset_12 = x.offset_;
722 if (offset_12 < 0) {
723 offset_12 = -offset_12;
724 am ^= U;
725 }
726 if (!is_uint12(offset_12)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000727 // Immediate offset cannot be encoded, load it first to register ip
728 // rn (and rd in a load) should never be ip, or will be trashed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000729 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
730 mov(ip, Operand(x.offset_), LeaveCC,
731 static_cast<Condition>(instr & CondMask));
732 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
733 return;
734 }
735 ASSERT(offset_12 >= 0); // no masking needed
736 instr |= offset_12;
737 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000738 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 // register offset the constructors make sure than both shift_imm_
Andrei Popescu31002712010-02-23 13:46:05 +0000740 // and shift_op_ are initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000741 ASSERT(!x.rm_.is(pc));
742 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
743 }
744 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
745 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
746}
747
748
749void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
750 ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7));
751 ASSERT(x.rn_.is_valid());
752 int am = x.am_;
753 if (!x.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000754 // Immediate offset.
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 int offset_8 = x.offset_;
756 if (offset_8 < 0) {
757 offset_8 = -offset_8;
758 am ^= U;
759 }
760 if (!is_uint8(offset_8)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000761 // Immediate offset cannot be encoded, load it first to register ip
762 // rn (and rd in a load) should never be ip, or will be trashed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
764 mov(ip, Operand(x.offset_), LeaveCC,
765 static_cast<Condition>(instr & CondMask));
766 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
767 return;
768 }
769 ASSERT(offset_8 >= 0); // no masking needed
770 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
771 } else if (x.shift_imm_ != 0) {
Andrei Popescu31002712010-02-23 13:46:05 +0000772 // Scaled register offset not supported, load index first
773 // rn (and rd in a load) should never be ip, or will be trashed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000774 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
775 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
776 static_cast<Condition>(instr & CondMask));
777 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
778 return;
779 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000780 // Register offset.
Steve Blocka7e24c12009-10-30 11:49:00 +0000781 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
782 instr |= x.rm_.code();
783 }
784 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
785 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
786}
787
788
789void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
790 ASSERT((instr & ~(CondMask | P | U | W | L)) == B27);
791 ASSERT(rl != 0);
792 ASSERT(!rn.is(pc));
793 emit(instr | rn.code()*B16 | rl);
794}
795
796
797void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
Andrei Popescu31002712010-02-23 13:46:05 +0000798 // Unindexed addressing is not encoded by this function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000799 ASSERT_EQ((B27 | B26),
800 (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L)));
801 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
802 int am = x.am_;
803 int offset_8 = x.offset_;
804 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
805 offset_8 >>= 2;
806 if (offset_8 < 0) {
807 offset_8 = -offset_8;
808 am ^= U;
809 }
810 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
811 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
812
Andrei Popescu31002712010-02-23 13:46:05 +0000813 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 if ((am & P) == 0)
815 am |= W;
816
817 ASSERT(offset_8 >= 0); // no masking needed
818 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
819}
820
821
822int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
823 int target_pos;
824 if (L->is_bound()) {
825 target_pos = L->pos();
826 } else {
827 if (L->is_linked()) {
828 target_pos = L->pos(); // L's link
829 } else {
830 target_pos = kEndOfChain;
831 }
832 L->link_to(pc_offset());
833 }
834
835 // Block the emission of the constant pool, since the branch instruction must
Andrei Popescu31002712010-02-23 13:46:05 +0000836 // be emitted at the pc offset recorded by the label.
Steve Blocka7e24c12009-10-30 11:49:00 +0000837 BlockConstPoolBefore(pc_offset() + kInstrSize);
838 return target_pos - (pc_offset() + kPcLoadDelta);
839}
840
841
842void Assembler::label_at_put(Label* L, int at_offset) {
843 int target_pos;
844 if (L->is_bound()) {
845 target_pos = L->pos();
846 } else {
847 if (L->is_linked()) {
848 target_pos = L->pos(); // L's link
849 } else {
850 target_pos = kEndOfChain;
851 }
852 L->link_to(at_offset);
853 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
854 }
855}
856
857
Andrei Popescu31002712010-02-23 13:46:05 +0000858// Branch instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000859void Assembler::b(int branch_offset, Condition cond) {
860 ASSERT((branch_offset & 3) == 0);
861 int imm24 = branch_offset >> 2;
862 ASSERT(is_int24(imm24));
863 emit(cond | B27 | B25 | (imm24 & Imm24Mask));
864
Steve Block6ded16b2010-05-10 14:33:55 +0100865 if (cond == al) {
Andrei Popescu31002712010-02-23 13:46:05 +0000866 // Dead code is a good location to emit the constant pool.
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 CheckConstPool(false, false);
Steve Block6ded16b2010-05-10 14:33:55 +0100868 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000869}
870
871
872void Assembler::bl(int branch_offset, Condition cond) {
873 ASSERT((branch_offset & 3) == 0);
874 int imm24 = branch_offset >> 2;
875 ASSERT(is_int24(imm24));
876 emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask));
877}
878
879
880void Assembler::blx(int branch_offset) { // v5 and above
881 WriteRecordedPositions();
882 ASSERT((branch_offset & 1) == 0);
883 int h = ((branch_offset & 2) >> 1)*B24;
884 int imm24 = branch_offset >> 2;
885 ASSERT(is_int24(imm24));
886 emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask));
887}
888
889
890void Assembler::blx(Register target, Condition cond) { // v5 and above
891 WriteRecordedPositions();
892 ASSERT(!target.is(pc));
893 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code());
894}
895
896
897void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
898 WriteRecordedPositions();
899 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
900 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code());
901}
902
903
Andrei Popescu31002712010-02-23 13:46:05 +0000904// Data-processing instructions.
905
Steve Blocka7e24c12009-10-30 11:49:00 +0000906void Assembler::and_(Register dst, Register src1, const Operand& src2,
907 SBit s, Condition cond) {
908 addrmod1(cond | 0*B21 | s, src1, dst, src2);
909}
910
911
912void Assembler::eor(Register dst, Register src1, const Operand& src2,
913 SBit s, Condition cond) {
914 addrmod1(cond | 1*B21 | s, src1, dst, src2);
915}
916
917
918void Assembler::sub(Register dst, Register src1, const Operand& src2,
919 SBit s, Condition cond) {
920 addrmod1(cond | 2*B21 | s, src1, dst, src2);
921}
922
923
924void Assembler::rsb(Register dst, Register src1, const Operand& src2,
925 SBit s, Condition cond) {
926 addrmod1(cond | 3*B21 | s, src1, dst, src2);
927}
928
929
930void Assembler::add(Register dst, Register src1, const Operand& src2,
931 SBit s, Condition cond) {
932 addrmod1(cond | 4*B21 | s, src1, dst, src2);
933
934 // Eliminate pattern: push(r), pop()
935 // str(src, MemOperand(sp, 4, NegPreIndex), al);
936 // add(sp, sp, Operand(kPointerSize));
937 // Both instructions can be eliminated.
Leon Clarkef7060e22010-06-03 12:02:55 +0100938 if (can_peephole_optimize(2) &&
Andrei Popescu31002712010-02-23 13:46:05 +0000939 // Pattern.
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
941 (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) {
942 pc_ -= 2 * kInstrSize;
Leon Clarkef7060e22010-06-03 12:02:55 +0100943 if (FLAG_print_peephole_optimization) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000944 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
945 }
946 }
947}
948
949
950void Assembler::adc(Register dst, Register src1, const Operand& src2,
951 SBit s, Condition cond) {
952 addrmod1(cond | 5*B21 | s, src1, dst, src2);
953}
954
955
956void Assembler::sbc(Register dst, Register src1, const Operand& src2,
957 SBit s, Condition cond) {
958 addrmod1(cond | 6*B21 | s, src1, dst, src2);
959}
960
961
962void Assembler::rsc(Register dst, Register src1, const Operand& src2,
963 SBit s, Condition cond) {
964 addrmod1(cond | 7*B21 | s, src1, dst, src2);
965}
966
967
968void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
969 addrmod1(cond | 8*B21 | S, src1, r0, src2);
970}
971
972
973void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
974 addrmod1(cond | 9*B21 | S, src1, r0, src2);
975}
976
977
978void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
979 addrmod1(cond | 10*B21 | S, src1, r0, src2);
980}
981
982
983void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
984 addrmod1(cond | 11*B21 | S, src1, r0, src2);
985}
986
987
988void Assembler::orr(Register dst, Register src1, const Operand& src2,
989 SBit s, Condition cond) {
990 addrmod1(cond | 12*B21 | s, src1, dst, src2);
991}
992
993
994void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
995 if (dst.is(pc)) {
996 WriteRecordedPositions();
997 }
Steve Block6ded16b2010-05-10 14:33:55 +0100998 // Don't allow nop instructions in the form mov rn, rn to be generated using
999 // the mov instruction. They must be generated using nop(int)
1000 // pseudo instructions.
1001 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 addrmod1(cond | 13*B21 | s, r0, dst, src);
1003}
1004
1005
1006void Assembler::bic(Register dst, Register src1, const Operand& src2,
1007 SBit s, Condition cond) {
1008 addrmod1(cond | 14*B21 | s, src1, dst, src2);
1009}
1010
1011
1012void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
1013 addrmod1(cond | 15*B21 | s, r0, dst, src);
1014}
1015
1016
Andrei Popescu31002712010-02-23 13:46:05 +00001017// Multiply instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001018void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1019 SBit s, Condition cond) {
1020 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
1021 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1022 src2.code()*B8 | B7 | B4 | src1.code());
1023}
1024
1025
1026void Assembler::mul(Register dst, Register src1, Register src2,
1027 SBit s, Condition cond) {
1028 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
1029 // dst goes in bits 16-19 for this instruction!
1030 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1031}
1032
1033
1034void Assembler::smlal(Register dstL,
1035 Register dstH,
1036 Register src1,
1037 Register src2,
1038 SBit s,
1039 Condition cond) {
1040 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1041 ASSERT(!dstL.is(dstH));
1042 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1043 src2.code()*B8 | B7 | B4 | src1.code());
1044}
1045
1046
1047void Assembler::smull(Register dstL,
1048 Register dstH,
1049 Register src1,
1050 Register src2,
1051 SBit s,
1052 Condition cond) {
1053 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1054 ASSERT(!dstL.is(dstH));
1055 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1056 src2.code()*B8 | B7 | B4 | src1.code());
1057}
1058
1059
1060void Assembler::umlal(Register dstL,
1061 Register dstH,
1062 Register src1,
1063 Register src2,
1064 SBit s,
1065 Condition cond) {
1066 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1067 ASSERT(!dstL.is(dstH));
1068 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1069 src2.code()*B8 | B7 | B4 | src1.code());
1070}
1071
1072
1073void Assembler::umull(Register dstL,
1074 Register dstH,
1075 Register src1,
1076 Register src2,
1077 SBit s,
1078 Condition cond) {
1079 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1080 ASSERT(!dstL.is(dstH));
1081 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
1082 src2.code()*B8 | B7 | B4 | src1.code());
1083}
1084
1085
Andrei Popescu31002712010-02-23 13:46:05 +00001086// Miscellaneous arithmetic instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001087void Assembler::clz(Register dst, Register src, Condition cond) {
1088 // v5 and above.
1089 ASSERT(!dst.is(pc) && !src.is(pc));
1090 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
1091 15*B8 | B4 | src.code());
1092}
1093
1094
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001095// Bitfield manipulation instructions.
1096
1097// Unsigned bit field extract.
1098// Extracts #width adjacent bits from position #lsb in a register, and
1099// writes them to the low bits of a destination register.
1100// ubfx dst, src, #lsb, #width
1101void Assembler::ubfx(Register dst,
1102 Register src,
1103 int lsb,
1104 int width,
1105 Condition cond) {
1106 // v7 and above.
1107 ASSERT(CpuFeatures::IsSupported(ARMv7));
1108 ASSERT(!dst.is(pc) && !src.is(pc));
1109 ASSERT((lsb >= 0) && (lsb <= 31));
1110 ASSERT((width >= 1) && (width <= (32 - lsb)));
1111 emit(cond | 0xf*B23 | B22 | B21 | (width - 1)*B16 | dst.code()*B12 |
1112 lsb*B7 | B6 | B4 | src.code());
1113}
1114
1115
1116// Signed bit field extract.
1117// Extracts #width adjacent bits from position #lsb in a register, and
1118// writes them to the low bits of a destination register. The extracted
1119// value is sign extended to fill the destination register.
1120// sbfx dst, src, #lsb, #width
1121void Assembler::sbfx(Register dst,
1122 Register src,
1123 int lsb,
1124 int width,
1125 Condition cond) {
1126 // v7 and above.
1127 ASSERT(CpuFeatures::IsSupported(ARMv7));
1128 ASSERT(!dst.is(pc) && !src.is(pc));
1129 ASSERT((lsb >= 0) && (lsb <= 31));
1130 ASSERT((width >= 1) && (width <= (32 - lsb)));
1131 emit(cond | 0xf*B23 | B21 | (width - 1)*B16 | dst.code()*B12 |
1132 lsb*B7 | B6 | B4 | src.code());
1133}
1134
1135
1136// Bit field clear.
1137// Sets #width adjacent bits at position #lsb in the destination register
1138// to zero, preserving the value of the other bits.
1139// bfc dst, #lsb, #width
1140void Assembler::bfc(Register dst, int lsb, int width, Condition cond) {
1141 // v7 and above.
1142 ASSERT(CpuFeatures::IsSupported(ARMv7));
1143 ASSERT(!dst.is(pc));
1144 ASSERT((lsb >= 0) && (lsb <= 31));
1145 ASSERT((width >= 1) && (width <= (32 - lsb)));
1146 int msb = lsb + width - 1;
1147 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 | 0xf);
1148}
1149
1150
1151// Bit field insert.
1152// Inserts #width adjacent bits from the low bits of the source register
1153// into position #lsb of the destination register.
1154// bfi dst, src, #lsb, #width
1155void Assembler::bfi(Register dst,
1156 Register src,
1157 int lsb,
1158 int width,
1159 Condition cond) {
1160 // v7 and above.
1161 ASSERT(CpuFeatures::IsSupported(ARMv7));
1162 ASSERT(!dst.is(pc) && !src.is(pc));
1163 ASSERT((lsb >= 0) && (lsb <= 31));
1164 ASSERT((width >= 1) && (width <= (32 - lsb)));
1165 int msb = lsb + width - 1;
1166 emit(cond | 0x1f*B22 | msb*B16 | dst.code()*B12 | lsb*B7 | B4 |
1167 src.code());
1168}
1169
1170
Andrei Popescu31002712010-02-23 13:46:05 +00001171// Status register access instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001172void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1173 ASSERT(!dst.is(pc));
1174 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1175}
1176
1177
1178void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1179 Condition cond) {
1180 ASSERT(fields >= B16 && fields < B20); // at least one field set
1181 Instr instr;
1182 if (!src.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +00001183 // Immediate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001184 uint32_t rotate_imm;
1185 uint32_t immed_8;
1186 if (MustUseIp(src.rmode_) ||
1187 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001188 // Immediate operand cannot be encoded, load it first to register ip.
Steve Blocka7e24c12009-10-30 11:49:00 +00001189 RecordRelocInfo(src.rmode_, src.imm32_);
1190 ldr(ip, MemOperand(pc, 0), cond);
1191 msr(fields, Operand(ip), cond);
1192 return;
1193 }
1194 instr = I | rotate_imm*B8 | immed_8;
1195 } else {
1196 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1197 instr = src.rm_.code();
1198 }
1199 emit(cond | instr | B24 | B21 | fields | 15*B12);
1200}
1201
1202
Andrei Popescu31002712010-02-23 13:46:05 +00001203// Load/Store instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001204void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
1205 if (dst.is(pc)) {
1206 WriteRecordedPositions();
1207 }
1208 addrmod2(cond | B26 | L, dst, src);
1209
Leon Clarkef7060e22010-06-03 12:02:55 +01001210 // Eliminate pattern: push(ry), pop(rx)
1211 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1212 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1213 // Both instructions can be eliminated if ry = rx.
1214 // If ry != rx, a register copy from ry to rx is inserted
1215 // after eliminating the push and the pop instructions.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001216 if (can_peephole_optimize(2)) {
1217 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1218 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
Leon Clarkef7060e22010-06-03 12:02:55 +01001219
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001220 if (IsPush(push_instr) && IsPop(pop_instr)) {
1221 if ((pop_instr & kRdMask) != (push_instr & kRdMask)) {
1222 // For consecutive push and pop on different registers,
1223 // we delete both the push & pop and insert a register move.
1224 // push ry, pop rx --> mov rx, ry
1225 Register reg_pushed, reg_popped;
1226 reg_pushed = GetRd(push_instr);
1227 reg_popped = GetRd(pop_instr);
1228 pc_ -= 2 * kInstrSize;
1229 // Insert a mov instruction, which is better than a pair of push & pop
1230 mov(reg_popped, reg_pushed);
1231 if (FLAG_print_peephole_optimization) {
1232 PrintF("%x push/pop (diff reg) replaced by a reg move\n",
1233 pc_offset());
1234 }
1235 } else {
1236 // For consecutive push and pop on the same register,
1237 // both the push and the pop can be deleted.
1238 pc_ -= 2 * kInstrSize;
1239 if (FLAG_print_peephole_optimization) {
1240 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1241 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001242 }
1243 }
1244 }
1245
1246 if (can_peephole_optimize(2)) {
1247 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1248 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1249
1250 if ((IsStrRegFpOffset(str_instr) &&
1251 IsLdrRegFpOffset(ldr_instr)) ||
1252 (IsStrRegFpNegOffset(str_instr) &&
1253 IsLdrRegFpNegOffset(ldr_instr))) {
1254 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1255 (str_instr & kLdrStrInstrArgumentMask)) {
1256 // Pattern: Ldr/str same fp+offset, same register.
1257 //
1258 // The following:
1259 // str rx, [fp, #-12]
1260 // ldr rx, [fp, #-12]
1261 //
1262 // Becomes:
1263 // str rx, [fp, #-12]
1264
1265 pc_ -= 1 * kInstrSize;
1266 if (FLAG_print_peephole_optimization) {
1267 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1268 }
1269 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1270 (str_instr & kLdrStrOffsetMask)) {
1271 // Pattern: Ldr/str same fp+offset, different register.
1272 //
1273 // The following:
1274 // str rx, [fp, #-12]
1275 // ldr ry, [fp, #-12]
1276 //
1277 // Becomes:
1278 // str rx, [fp, #-12]
1279 // mov ry, rx
1280
1281 Register reg_stored, reg_loaded;
1282 reg_stored = GetRd(str_instr);
1283 reg_loaded = GetRd(ldr_instr);
1284 pc_ -= 1 * kInstrSize;
1285 // Insert a mov instruction, which is better than ldr.
1286 mov(reg_loaded, reg_stored);
1287 if (FLAG_print_peephole_optimization) {
1288 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1289 }
1290 }
1291 }
1292 }
1293
1294 if (can_peephole_optimize(3)) {
1295 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1296 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1297 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1298 if (IsPush(mem_write_instr) &&
1299 IsPop(mem_read_instr)) {
1300 if ((IsLdrRegFpOffset(ldr_instr) ||
1301 IsLdrRegFpNegOffset(ldr_instr))) {
1302 if ((mem_write_instr & kRdMask) ==
1303 (mem_read_instr & kRdMask)) {
1304 // Pattern: push & pop from/to same register,
1305 // with a fp+offset ldr in between
1306 //
1307 // The following:
1308 // str rx, [sp, #-4]!
1309 // ldr rz, [fp, #-24]
1310 // ldr rx, [sp], #+4
1311 //
1312 // Becomes:
1313 // if(rx == rz)
1314 // delete all
1315 // else
1316 // ldr rz, [fp, #-24]
1317
1318 if ((mem_write_instr & kRdMask) == (ldr_instr & kRdMask)) {
1319 pc_ -= 3 * kInstrSize;
1320 } else {
1321 pc_ -= 3 * kInstrSize;
1322 // Reinsert back the ldr rz.
1323 emit(ldr_instr);
1324 }
1325 if (FLAG_print_peephole_optimization) {
1326 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1327 }
1328 } else {
1329 // Pattern: push & pop from/to different registers
1330 // with a fp+offset ldr in between
1331 //
1332 // The following:
1333 // str rx, [sp, #-4]!
1334 // ldr rz, [fp, #-24]
1335 // ldr ry, [sp], #+4
1336 //
1337 // Becomes:
1338 // if(ry == rz)
1339 // mov ry, rx;
1340 // else if(rx != rz)
1341 // ldr rz, [fp, #-24]
1342 // mov ry, rx
1343 // else if((ry != rz) || (rx == rz)) becomes:
1344 // mov ry, rx
1345 // ldr rz, [fp, #-24]
1346
1347 Register reg_pushed, reg_popped;
1348 if ((mem_read_instr & kRdMask) == (ldr_instr & kRdMask)) {
1349 reg_pushed = GetRd(mem_write_instr);
1350 reg_popped = GetRd(mem_read_instr);
1351 pc_ -= 3 * kInstrSize;
1352 mov(reg_popped, reg_pushed);
1353 } else if ((mem_write_instr & kRdMask)
1354 != (ldr_instr & kRdMask)) {
1355 reg_pushed = GetRd(mem_write_instr);
1356 reg_popped = GetRd(mem_read_instr);
1357 pc_ -= 3 * kInstrSize;
1358 emit(ldr_instr);
1359 mov(reg_popped, reg_pushed);
1360 } else if (((mem_read_instr & kRdMask)
1361 != (ldr_instr & kRdMask)) ||
1362 ((mem_write_instr & kRdMask)
1363 == (ldr_instr & kRdMask)) ) {
1364 reg_pushed = GetRd(mem_write_instr);
1365 reg_popped = GetRd(mem_read_instr);
1366 pc_ -= 3 * kInstrSize;
1367 mov(reg_popped, reg_pushed);
1368 emit(ldr_instr);
1369 }
1370 if (FLAG_print_peephole_optimization) {
1371 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1372 }
1373 }
1374 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001375 }
1376 }
1377}
1378
1379
1380void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1381 addrmod2(cond | B26, src, dst);
1382
1383 // Eliminate pattern: pop(), push(r)
1384 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1385 // -> str r, [sp, 0], al
Leon Clarkef7060e22010-06-03 12:02:55 +01001386 if (can_peephole_optimize(2) &&
Andrei Popescu31002712010-02-23 13:46:05 +00001387 // Pattern.
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1389 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1390 pc_ -= 2 * kInstrSize;
1391 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
Leon Clarkef7060e22010-06-03 12:02:55 +01001392 if (FLAG_print_peephole_optimization) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001393 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1394 }
1395 }
1396}
1397
1398
1399void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1400 addrmod2(cond | B26 | B | L, dst, src);
1401}
1402
1403
1404void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1405 addrmod2(cond | B26 | B, src, dst);
1406}
1407
1408
1409void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1410 addrmod3(cond | L | B7 | H | B4, dst, src);
1411}
1412
1413
1414void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1415 addrmod3(cond | B7 | H | B4, src, dst);
1416}
1417
1418
1419void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1420 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1421}
1422
1423
1424void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1425 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1426}
1427
1428
Leon Clarkef7060e22010-06-03 12:02:55 +01001429void Assembler::ldrd(Register dst1, Register dst2,
1430 const MemOperand& src, Condition cond) {
1431 ASSERT(CpuFeatures::IsEnabled(ARMv7));
Kristian Monsen25f61362010-05-21 11:50:48 +01001432 ASSERT(src.rm().is(no_reg));
Leon Clarkef7060e22010-06-03 12:02:55 +01001433 ASSERT(!dst1.is(lr)); // r14.
1434 ASSERT_EQ(0, dst1.code() % 2);
1435 ASSERT_EQ(dst1.code() + 1, dst2.code());
1436 addrmod3(cond | B7 | B6 | B4, dst1, src);
Kristian Monsen25f61362010-05-21 11:50:48 +01001437}
1438
1439
Leon Clarkef7060e22010-06-03 12:02:55 +01001440void Assembler::strd(Register src1, Register src2,
1441 const MemOperand& dst, Condition cond) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001442 ASSERT(dst.rm().is(no_reg));
Leon Clarkef7060e22010-06-03 12:02:55 +01001443 ASSERT(!src1.is(lr)); // r14.
1444 ASSERT_EQ(0, src1.code() % 2);
1445 ASSERT_EQ(src1.code() + 1, src2.code());
1446 ASSERT(CpuFeatures::IsEnabled(ARMv7));
1447 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
Kristian Monsen25f61362010-05-21 11:50:48 +01001448}
1449
Andrei Popescu31002712010-02-23 13:46:05 +00001450// Load/Store multiple instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001451void Assembler::ldm(BlockAddrMode am,
1452 Register base,
1453 RegList dst,
1454 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001455 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
Steve Blocka7e24c12009-10-30 11:49:00 +00001456 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1457
1458 addrmod4(cond | B27 | am | L, base, dst);
1459
Andrei Popescu31002712010-02-23 13:46:05 +00001460 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
Steve Blocka7e24c12009-10-30 11:49:00 +00001461 if (cond == al && (dst & pc.bit()) != 0) {
1462 // There is a slight chance that the ldm instruction was actually a call,
1463 // in which case it would be wrong to return into the constant pool; we
1464 // recognize this case by checking if the emission of the pool was blocked
1465 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1466 // the case, we emit a jump over the pool.
1467 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1468 }
1469}
1470
1471
1472void Assembler::stm(BlockAddrMode am,
1473 Register base,
1474 RegList src,
1475 Condition cond) {
1476 addrmod4(cond | B27 | am, base, src);
1477}
1478
1479
Andrei Popescu31002712010-02-23 13:46:05 +00001480// Exception-generating instructions and debugging support.
Steve Blocka7e24c12009-10-30 11:49:00 +00001481void Assembler::stop(const char* msg) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001482#ifndef __arm__
Steve Blocka7e24c12009-10-30 11:49:00 +00001483 // The simulator handles these special instructions and stops execution.
1484 emit(15 << 28 | ((intptr_t) msg));
Andrei Popescu402d9372010-02-26 13:31:12 +00001485#else // def __arm__
1486#ifdef CAN_USE_ARMV5_INSTRUCTIONS
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 bkpt(0);
Andrei Popescu402d9372010-02-26 13:31:12 +00001488#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
1489 swi(0x9f0001);
1490#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1491#endif // def __arm__
Steve Blocka7e24c12009-10-30 11:49:00 +00001492}
1493
1494
1495void Assembler::bkpt(uint32_t imm16) { // v5 and above
1496 ASSERT(is_uint16(imm16));
1497 emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf));
1498}
1499
1500
1501void Assembler::swi(uint32_t imm24, Condition cond) {
1502 ASSERT(is_uint24(imm24));
1503 emit(cond | 15*B24 | imm24);
1504}
1505
1506
Andrei Popescu31002712010-02-23 13:46:05 +00001507// Coprocessor instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001508void Assembler::cdp(Coprocessor coproc,
1509 int opcode_1,
1510 CRegister crd,
1511 CRegister crn,
1512 CRegister crm,
1513 int opcode_2,
1514 Condition cond) {
1515 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1516 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1517 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1518}
1519
1520
1521void Assembler::cdp2(Coprocessor coproc,
1522 int opcode_1,
1523 CRegister crd,
1524 CRegister crn,
1525 CRegister crm,
1526 int opcode_2) { // v5 and above
1527 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv));
1528}
1529
1530
1531void Assembler::mcr(Coprocessor coproc,
1532 int opcode_1,
1533 Register rd,
1534 CRegister crn,
1535 CRegister crm,
1536 int opcode_2,
1537 Condition cond) {
1538 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1539 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1540 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1541}
1542
1543
1544void Assembler::mcr2(Coprocessor coproc,
1545 int opcode_1,
1546 Register rd,
1547 CRegister crn,
1548 CRegister crm,
1549 int opcode_2) { // v5 and above
1550 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1551}
1552
1553
1554void Assembler::mrc(Coprocessor coproc,
1555 int opcode_1,
1556 Register rd,
1557 CRegister crn,
1558 CRegister crm,
1559 int opcode_2,
1560 Condition cond) {
1561 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1562 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1563 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1564}
1565
1566
1567void Assembler::mrc2(Coprocessor coproc,
1568 int opcode_1,
1569 Register rd,
1570 CRegister crn,
1571 CRegister crm,
1572 int opcode_2) { // v5 and above
1573 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1574}
1575
1576
1577void Assembler::ldc(Coprocessor coproc,
1578 CRegister crd,
1579 const MemOperand& src,
1580 LFlag l,
1581 Condition cond) {
1582 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1583}
1584
1585
1586void Assembler::ldc(Coprocessor coproc,
1587 CRegister crd,
1588 Register rn,
1589 int option,
1590 LFlag l,
1591 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001592 // Unindexed addressing.
Steve Blocka7e24c12009-10-30 11:49:00 +00001593 ASSERT(is_uint8(option));
1594 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1595 coproc*B8 | (option & 255));
1596}
1597
1598
1599void Assembler::ldc2(Coprocessor coproc,
1600 CRegister crd,
1601 const MemOperand& src,
1602 LFlag l) { // v5 and above
1603 ldc(coproc, crd, src, l, static_cast<Condition>(nv));
1604}
1605
1606
1607void Assembler::ldc2(Coprocessor coproc,
1608 CRegister crd,
1609 Register rn,
1610 int option,
1611 LFlag l) { // v5 and above
1612 ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1613}
1614
1615
1616void Assembler::stc(Coprocessor coproc,
1617 CRegister crd,
1618 const MemOperand& dst,
1619 LFlag l,
1620 Condition cond) {
1621 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1622}
1623
1624
1625void Assembler::stc(Coprocessor coproc,
1626 CRegister crd,
1627 Register rn,
1628 int option,
1629 LFlag l,
1630 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001631 // Unindexed addressing.
Steve Blocka7e24c12009-10-30 11:49:00 +00001632 ASSERT(is_uint8(option));
1633 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1634 coproc*B8 | (option & 255));
1635}
1636
1637
1638void Assembler::stc2(Coprocessor
1639 coproc, CRegister crd,
1640 const MemOperand& dst,
1641 LFlag l) { // v5 and above
1642 stc(coproc, crd, dst, l, static_cast<Condition>(nv));
1643}
1644
1645
1646void Assembler::stc2(Coprocessor coproc,
1647 CRegister crd,
1648 Register rn,
1649 int option,
1650 LFlag l) { // v5 and above
1651 stc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1652}
1653
1654
Steve Blockd0582a62009-12-15 09:54:21 +00001655// Support for VFP.
Leon Clarked91b9f72010-01-27 17:25:45 +00001656void Assembler::vldr(const DwVfpRegister dst,
1657 const Register base,
1658 int offset,
1659 const Condition cond) {
1660 // Ddst = MEM(Rbase + offset).
1661 // Instruction details available in ARM DDI 0406A, A8-628.
1662 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1663 // Vdst(15-12) | 1011(11-8) | offset
1664 ASSERT(CpuFeatures::IsEnabled(VFP3));
1665 ASSERT(offset % 4 == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001666 ASSERT((offset / 4) < 256);
Leon Clarked91b9f72010-01-27 17:25:45 +00001667 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1668 0xB*B8 | ((offset / 4) & 255));
1669}
1670
1671
Steve Block6ded16b2010-05-10 14:33:55 +01001672void Assembler::vldr(const SwVfpRegister dst,
1673 const Register base,
1674 int offset,
1675 const Condition cond) {
1676 // Sdst = MEM(Rbase + offset).
1677 // Instruction details available in ARM DDI 0406A, A8-628.
1678 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1679 // Vdst(15-12) | 1010(11-8) | offset
1680 ASSERT(CpuFeatures::IsEnabled(VFP3));
1681 ASSERT(offset % 4 == 0);
1682 ASSERT((offset / 4) < 256);
1683 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1684 0xA*B8 | ((offset / 4) & 255));
1685}
1686
1687
Leon Clarked91b9f72010-01-27 17:25:45 +00001688void Assembler::vstr(const DwVfpRegister src,
1689 const Register base,
1690 int offset,
1691 const Condition cond) {
1692 // MEM(Rbase + offset) = Dsrc.
1693 // Instruction details available in ARM DDI 0406A, A8-786.
1694 // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) |
1695 // Vsrc(15-12) | 1011(11-8) | (offset/4)
1696 ASSERT(CpuFeatures::IsEnabled(VFP3));
1697 ASSERT(offset % 4 == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001698 ASSERT((offset / 4) < 256);
Leon Clarked91b9f72010-01-27 17:25:45 +00001699 emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 |
1700 0xB*B8 | ((offset / 4) & 255));
1701}
1702
1703
Leon Clarkee46be812010-01-19 14:06:41 +00001704void Assembler::vmov(const DwVfpRegister dst,
1705 const Register src1,
1706 const Register src2,
1707 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001708 // Dm = <Rt,Rt2>.
1709 // Instruction details available in ARM DDI 0406A, A8-646.
1710 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
1711 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1712 ASSERT(CpuFeatures::IsEnabled(VFP3));
1713 ASSERT(!src1.is(pc) && !src2.is(pc));
1714 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
1715 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
1716}
1717
1718
Leon Clarkee46be812010-01-19 14:06:41 +00001719void Assembler::vmov(const Register dst1,
1720 const Register dst2,
1721 const DwVfpRegister src,
1722 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001723 // <Rt,Rt2> = Dm.
1724 // Instruction details available in ARM DDI 0406A, A8-646.
1725 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
1726 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1727 ASSERT(CpuFeatures::IsEnabled(VFP3));
1728 ASSERT(!dst1.is(pc) && !dst2.is(pc));
1729 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
1730 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
1731}
1732
1733
Leon Clarkee46be812010-01-19 14:06:41 +00001734void Assembler::vmov(const SwVfpRegister dst,
Steve Blockd0582a62009-12-15 09:54:21 +00001735 const Register src,
Steve Blockd0582a62009-12-15 09:54:21 +00001736 const Condition cond) {
1737 // Sn = Rt.
1738 // Instruction details available in ARM DDI 0406A, A8-642.
1739 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
1740 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1741 ASSERT(CpuFeatures::IsEnabled(VFP3));
1742 ASSERT(!src.is(pc));
1743 emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 |
1744 src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4);
1745}
1746
1747
Leon Clarkee46be812010-01-19 14:06:41 +00001748void Assembler::vmov(const Register dst,
1749 const SwVfpRegister src,
Steve Blockd0582a62009-12-15 09:54:21 +00001750 const Condition cond) {
1751 // Rt = Sn.
1752 // Instruction details available in ARM DDI 0406A, A8-642.
1753 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
1754 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1755 ASSERT(CpuFeatures::IsEnabled(VFP3));
1756 ASSERT(!dst.is(pc));
1757 emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 |
1758 dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4);
1759}
1760
1761
Steve Block6ded16b2010-05-10 14:33:55 +01001762// Type of data to read from or write to VFP register.
1763// Used as specifier in generic vcvt instruction.
1764enum VFPType { S32, U32, F32, F64 };
1765
1766
1767static bool IsSignedVFPType(VFPType type) {
1768 switch (type) {
1769 case S32:
1770 return true;
1771 case U32:
1772 return false;
1773 default:
1774 UNREACHABLE();
1775 return false;
1776 }
Steve Blockd0582a62009-12-15 09:54:21 +00001777}
1778
1779
Steve Block6ded16b2010-05-10 14:33:55 +01001780static bool IsIntegerVFPType(VFPType type) {
1781 switch (type) {
1782 case S32:
1783 case U32:
1784 return true;
1785 case F32:
1786 case F64:
1787 return false;
1788 default:
1789 UNREACHABLE();
1790 return false;
1791 }
1792}
1793
1794
1795static bool IsDoubleVFPType(VFPType type) {
1796 switch (type) {
1797 case F32:
1798 return false;
1799 case F64:
1800 return true;
1801 default:
1802 UNREACHABLE();
1803 return false;
1804 }
1805}
1806
1807
1808// Depending on split_last_bit split binary representation of reg_code into Vm:M
1809// or M:Vm form (where M is single bit).
1810static void SplitRegCode(bool split_last_bit,
1811 int reg_code,
1812 int* vm,
1813 int* m) {
1814 if (split_last_bit) {
1815 *m = reg_code & 0x1;
1816 *vm = reg_code >> 1;
1817 } else {
1818 *m = (reg_code & 0x10) >> 4;
1819 *vm = reg_code & 0x0F;
1820 }
1821}
1822
1823
1824// Encode vcvt.src_type.dst_type instruction.
1825static Instr EncodeVCVT(const VFPType dst_type,
1826 const int dst_code,
1827 const VFPType src_type,
1828 const int src_code,
1829 const Condition cond) {
1830 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
1831 // Conversion between IEEE floating point and 32-bit integer.
1832 // Instruction details available in ARM DDI 0406B, A8.6.295.
1833 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
1834 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
1835 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
1836
1837 int sz, opc2, D, Vd, M, Vm, op;
1838
1839 if (IsIntegerVFPType(dst_type)) {
1840 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
1841 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
1842 op = 1; // round towards zero
1843 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
1844 SplitRegCode(true, dst_code, &Vd, &D);
1845 } else {
1846 ASSERT(IsIntegerVFPType(src_type));
1847
1848 opc2 = 0x0;
1849 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
1850 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
1851 SplitRegCode(true, src_code, &Vm, &M);
1852 SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D);
1853 }
1854
1855 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
1856 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
1857 } else {
1858 // Conversion between IEEE double and single precision.
1859 // Instruction details available in ARM DDI 0406B, A8.6.298.
1860 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
1861 // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
1862 int sz, D, Vd, M, Vm;
1863
1864 ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type));
1865 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
1866 SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D);
1867 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
1868
1869 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
1870 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
1871 }
1872}
1873
1874
1875void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
1876 const SwVfpRegister src,
1877 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001878 ASSERT(CpuFeatures::IsEnabled(VFP3));
Steve Block6ded16b2010-05-10 14:33:55 +01001879 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond));
1880}
1881
1882
1883void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
1884 const SwVfpRegister src,
1885 const Condition cond) {
1886 ASSERT(CpuFeatures::IsEnabled(VFP3));
1887 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond));
1888}
1889
1890
1891void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
1892 const SwVfpRegister src,
1893 const Condition cond) {
1894 ASSERT(CpuFeatures::IsEnabled(VFP3));
1895 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond));
1896}
1897
1898
1899void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
1900 const DwVfpRegister src,
1901 const Condition cond) {
1902 ASSERT(CpuFeatures::IsEnabled(VFP3));
1903 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond));
1904}
1905
1906
1907void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
1908 const DwVfpRegister src,
1909 const Condition cond) {
1910 ASSERT(CpuFeatures::IsEnabled(VFP3));
1911 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond));
1912}
1913
1914
1915void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
1916 const SwVfpRegister src,
1917 const Condition cond) {
1918 ASSERT(CpuFeatures::IsEnabled(VFP3));
1919 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond));
1920}
1921
1922
1923void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
1924 const DwVfpRegister src,
1925 const Condition cond) {
1926 ASSERT(CpuFeatures::IsEnabled(VFP3));
1927 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond));
Steve Blockd0582a62009-12-15 09:54:21 +00001928}
1929
1930
Leon Clarkee46be812010-01-19 14:06:41 +00001931void Assembler::vadd(const DwVfpRegister dst,
1932 const DwVfpRegister src1,
1933 const DwVfpRegister src2,
1934 const Condition cond) {
1935 // Dd = vadd(Dn, Dm) double precision floating point addition.
Steve Blockd0582a62009-12-15 09:54:21 +00001936 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1937 // Instruction details available in ARM DDI 0406A, A8-536.
1938 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
1939 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1940 ASSERT(CpuFeatures::IsEnabled(VFP3));
1941 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
1942 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1943}
1944
1945
Leon Clarkee46be812010-01-19 14:06:41 +00001946void Assembler::vsub(const DwVfpRegister dst,
1947 const DwVfpRegister src1,
1948 const DwVfpRegister src2,
1949 const Condition cond) {
1950 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
Steve Blockd0582a62009-12-15 09:54:21 +00001951 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1952 // Instruction details available in ARM DDI 0406A, A8-784.
1953 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
1954 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
1955 ASSERT(CpuFeatures::IsEnabled(VFP3));
1956 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
1957 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
1958}
1959
1960
Leon Clarkee46be812010-01-19 14:06:41 +00001961void Assembler::vmul(const DwVfpRegister dst,
1962 const DwVfpRegister src1,
1963 const DwVfpRegister src2,
1964 const Condition cond) {
1965 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
Steve Blockd0582a62009-12-15 09:54:21 +00001966 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1967 // Instruction details available in ARM DDI 0406A, A8-784.
1968 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
1969 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1970 ASSERT(CpuFeatures::IsEnabled(VFP3));
1971 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
1972 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1973}
1974
1975
Leon Clarkee46be812010-01-19 14:06:41 +00001976void Assembler::vdiv(const DwVfpRegister dst,
1977 const DwVfpRegister src1,
1978 const DwVfpRegister src2,
1979 const Condition cond) {
1980 // Dd = vdiv(Dn, Dm) double precision floating point division.
Steve Blockd0582a62009-12-15 09:54:21 +00001981 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1982 // Instruction details available in ARM DDI 0406A, A8-584.
1983 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
1984 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1985 ASSERT(CpuFeatures::IsEnabled(VFP3));
1986 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
1987 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1988}
1989
1990
Leon Clarkee46be812010-01-19 14:06:41 +00001991void Assembler::vcmp(const DwVfpRegister src1,
1992 const DwVfpRegister src2,
Steve Blockd0582a62009-12-15 09:54:21 +00001993 const SBit s,
1994 const Condition cond) {
1995 // vcmp(Dd, Dm) double precision floating point comparison.
1996 // Instruction details available in ARM DDI 0406A, A8-570.
1997 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
1998 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0)
1999 ASSERT(CpuFeatures::IsEnabled(VFP3));
2000 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
2001 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
2002}
2003
2004
2005void Assembler::vmrs(Register dst, Condition cond) {
2006 // Instruction details available in ARM DDI 0406A, A8-652.
2007 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
2008 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
2009 ASSERT(CpuFeatures::IsEnabled(VFP3));
2010 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
2011 dst.code()*B12 | 0xA*B8 | B4);
2012}
2013
2014
Andrei Popescu31002712010-02-23 13:46:05 +00002015// Pseudo instructions.
Steve Block6ded16b2010-05-10 14:33:55 +01002016void Assembler::nop(int type) {
2017 // This is mov rx, rx.
2018 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
2019 emit(al | 13*B21 | type*B12 | type);
2020}
2021
2022
Steve Blockd0582a62009-12-15 09:54:21 +00002023bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
2024 uint32_t dummy1;
2025 uint32_t dummy2;
2026 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
2027}
2028
2029
2030void Assembler::BlockConstPoolFor(int instructions) {
2031 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
2032}
2033
2034
Andrei Popescu31002712010-02-23 13:46:05 +00002035// Debugging.
Steve Blocka7e24c12009-10-30 11:49:00 +00002036void Assembler::RecordJSReturn() {
2037 WriteRecordedPositions();
2038 CheckBuffer();
2039 RecordRelocInfo(RelocInfo::JS_RETURN);
2040}
2041
2042
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002043void Assembler::RecordDebugBreakSlot() {
2044 WriteRecordedPositions();
2045 CheckBuffer();
2046 RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2047}
2048
2049
Steve Blocka7e24c12009-10-30 11:49:00 +00002050void Assembler::RecordComment(const char* msg) {
2051 if (FLAG_debug_code) {
2052 CheckBuffer();
2053 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
2054 }
2055}
2056
2057
2058void Assembler::RecordPosition(int pos) {
2059 if (pos == RelocInfo::kNoPosition) return;
2060 ASSERT(pos >= 0);
2061 current_position_ = pos;
2062}
2063
2064
2065void Assembler::RecordStatementPosition(int pos) {
2066 if (pos == RelocInfo::kNoPosition) return;
2067 ASSERT(pos >= 0);
2068 current_statement_position_ = pos;
2069}
2070
2071
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002072bool Assembler::WriteRecordedPositions() {
2073 bool written = false;
2074
Steve Blocka7e24c12009-10-30 11:49:00 +00002075 // Write the statement position if it is different from what was written last
2076 // time.
2077 if (current_statement_position_ != written_statement_position_) {
2078 CheckBuffer();
2079 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_);
2080 written_statement_position_ = current_statement_position_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002081 written = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002082 }
2083
2084 // Write the position if it is different from what was written last time and
2085 // also different from the written statement position.
2086 if (current_position_ != written_position_ &&
2087 current_position_ != written_statement_position_) {
2088 CheckBuffer();
2089 RecordRelocInfo(RelocInfo::POSITION, current_position_);
2090 written_position_ = current_position_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002091 written = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002092 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002093
2094 // Return whether something was written.
2095 return written;
Steve Blocka7e24c12009-10-30 11:49:00 +00002096}
2097
2098
2099void Assembler::GrowBuffer() {
2100 if (!own_buffer_) FATAL("external code buffer is too small");
2101
Andrei Popescu31002712010-02-23 13:46:05 +00002102 // Compute new buffer size.
Steve Blocka7e24c12009-10-30 11:49:00 +00002103 CodeDesc desc; // the new buffer
2104 if (buffer_size_ < 4*KB) {
2105 desc.buffer_size = 4*KB;
2106 } else if (buffer_size_ < 1*MB) {
2107 desc.buffer_size = 2*buffer_size_;
2108 } else {
2109 desc.buffer_size = buffer_size_ + 1*MB;
2110 }
2111 CHECK_GT(desc.buffer_size, 0); // no overflow
2112
Andrei Popescu31002712010-02-23 13:46:05 +00002113 // Setup new buffer.
Steve Blocka7e24c12009-10-30 11:49:00 +00002114 desc.buffer = NewArray<byte>(desc.buffer_size);
2115
2116 desc.instr_size = pc_offset();
2117 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2118
Andrei Popescu31002712010-02-23 13:46:05 +00002119 // Copy the data.
Steve Blocka7e24c12009-10-30 11:49:00 +00002120 int pc_delta = desc.buffer - buffer_;
2121 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2122 memmove(desc.buffer, buffer_, desc.instr_size);
2123 memmove(reloc_info_writer.pos() + rc_delta,
2124 reloc_info_writer.pos(), desc.reloc_size);
2125
Andrei Popescu31002712010-02-23 13:46:05 +00002126 // Switch buffers.
Steve Blocka7e24c12009-10-30 11:49:00 +00002127 DeleteArray(buffer_);
2128 buffer_ = desc.buffer;
2129 buffer_size_ = desc.buffer_size;
2130 pc_ += pc_delta;
2131 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2132 reloc_info_writer.last_pc() + pc_delta);
2133
Andrei Popescu31002712010-02-23 13:46:05 +00002134 // None of our relocation types are pc relative pointing outside the code
Steve Blocka7e24c12009-10-30 11:49:00 +00002135 // buffer nor pc absolute pointing inside the code buffer, so there is no need
Andrei Popescu31002712010-02-23 13:46:05 +00002136 // to relocate any emitted relocation entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002137
Andrei Popescu31002712010-02-23 13:46:05 +00002138 // Relocate pending relocation entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002139 for (int i = 0; i < num_prinfo_; i++) {
2140 RelocInfo& rinfo = prinfo_[i];
2141 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2142 rinfo.rmode() != RelocInfo::POSITION);
2143 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2144 rinfo.set_pc(rinfo.pc() + pc_delta);
2145 }
2146 }
2147}
2148
2149
2150void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2151 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002152 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::DEBUG_BREAK_SLOT) {
Andrei Popescu31002712010-02-23 13:46:05 +00002153 // Adjust code for new modes.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002154 ASSERT(RelocInfo::IsDebugBreakSlot(rmode)
2155 || RelocInfo::IsJSReturn(rmode)
Steve Blocka7e24c12009-10-30 11:49:00 +00002156 || RelocInfo::IsComment(rmode)
2157 || RelocInfo::IsPosition(rmode));
Andrei Popescu31002712010-02-23 13:46:05 +00002158 // These modes do not need an entry in the constant pool.
Steve Blocka7e24c12009-10-30 11:49:00 +00002159 } else {
2160 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2161 prinfo_[num_prinfo_++] = rinfo;
2162 // Make sure the constant pool is not emitted in place of the next
Andrei Popescu31002712010-02-23 13:46:05 +00002163 // instruction for which we just recorded relocation info.
Steve Blocka7e24c12009-10-30 11:49:00 +00002164 BlockConstPoolBefore(pc_offset() + kInstrSize);
2165 }
2166 if (rinfo.rmode() != RelocInfo::NONE) {
2167 // Don't record external references unless the heap will be serialized.
Steve Blockd0582a62009-12-15 09:54:21 +00002168 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2169#ifdef DEBUG
2170 if (!Serializer::enabled()) {
2171 Serializer::TooLateToEnableNow();
2172 }
2173#endif
2174 if (!Serializer::enabled() && !FLAG_debug_code) {
2175 return;
2176 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002177 }
2178 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2179 reloc_info_writer.Write(&rinfo);
2180 }
2181}
2182
2183
2184void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2185 // Calculate the offset of the next check. It will be overwritten
2186 // when a const pool is generated or when const pools are being
2187 // blocked for a specific range.
2188 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2189
Andrei Popescu31002712010-02-23 13:46:05 +00002190 // There is nothing to do if there are no pending relocation info entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002191 if (num_prinfo_ == 0) return;
2192
2193 // We emit a constant pool at regular intervals of about kDistBetweenPools
2194 // or when requested by parameter force_emit (e.g. after each function).
2195 // We prefer not to emit a jump unless the max distance is reached or if we
2196 // are running low on slots, which can happen if a lot of constants are being
2197 // emitted (e.g. --debug-code and many static references).
2198 int dist = pc_offset() - last_const_pool_end_;
2199 if (!force_emit && dist < kMaxDistBetweenPools &&
2200 (require_jump || dist < kDistBetweenPools) &&
2201 // TODO(1236125): Cleanup the "magic" number below. We know that
2202 // the code generation will test every kCheckConstIntervalInst.
2203 // Thus we are safe as long as we generate less than 7 constant
2204 // entries per instruction.
2205 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2206 return;
2207 }
2208
2209 // If we did not return by now, we need to emit the constant pool soon.
2210
2211 // However, some small sequences of instructions must not be broken up by the
2212 // insertion of a constant pool; such sequences are protected by setting
Steve Block6ded16b2010-05-10 14:33:55 +01002213 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2214 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2215 // no_const_pool_before_.
2216 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
Andrei Popescu31002712010-02-23 13:46:05 +00002217 // Emission is currently blocked; make sure we try again as soon as
2218 // possible.
Steve Block6ded16b2010-05-10 14:33:55 +01002219 if (const_pool_blocked_nesting_ > 0) {
2220 next_buffer_check_ = pc_offset() + kInstrSize;
2221 } else {
2222 next_buffer_check_ = no_const_pool_before_;
2223 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002224
Andrei Popescu31002712010-02-23 13:46:05 +00002225 // Something is wrong if emission is forced and blocked at the same time.
Steve Blocka7e24c12009-10-30 11:49:00 +00002226 ASSERT(!force_emit);
2227 return;
2228 }
2229
2230 int jump_instr = require_jump ? kInstrSize : 0;
2231
2232 // Check that the code buffer is large enough before emitting the constant
2233 // pool and relocation information (include the jump over the pool and the
2234 // constant pool marker).
2235 int max_needed_space =
2236 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2237 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2238
Andrei Popescu31002712010-02-23 13:46:05 +00002239 // Block recursive calls to CheckConstPool.
Steve Blocka7e24c12009-10-30 11:49:00 +00002240 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2241 num_prinfo_*kInstrSize);
2242 // Don't bother to check for the emit calls below.
2243 next_buffer_check_ = no_const_pool_before_;
2244
Andrei Popescu31002712010-02-23 13:46:05 +00002245 // Emit jump over constant pool if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002246 Label after_pool;
2247 if (require_jump) b(&after_pool);
2248
2249 RecordComment("[ Constant Pool");
2250
Andrei Popescu31002712010-02-23 13:46:05 +00002251 // Put down constant pool marker "Undefined instruction" as specified by
2252 // A3.1 Instruction set encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +00002253 emit(0x03000000 | num_prinfo_);
2254
Andrei Popescu31002712010-02-23 13:46:05 +00002255 // Emit constant pool entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002256 for (int i = 0; i < num_prinfo_; i++) {
2257 RelocInfo& rinfo = prinfo_[i];
2258 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2259 rinfo.rmode() != RelocInfo::POSITION &&
2260 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
2261 Instr instr = instr_at(rinfo.pc());
2262
Andrei Popescu31002712010-02-23 13:46:05 +00002263 // Instruction to patch must be a ldr/str [pc, #offset].
2264 // P and U set, B and W clear, Rn == pc, offset12 still 0.
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) ==
2266 (2*B25 | P | U | pc.code()*B16));
2267 int delta = pc_ - rinfo.pc() - 8;
2268 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2269 if (delta < 0) {
2270 instr &= ~U;
2271 delta = -delta;
2272 }
2273 ASSERT(is_uint12(delta));
2274 instr_at_put(rinfo.pc(), instr + delta);
2275 emit(rinfo.data());
2276 }
2277 num_prinfo_ = 0;
2278 last_const_pool_end_ = pc_offset();
2279
2280 RecordComment("]");
2281
2282 if (after_pool.is_linked()) {
2283 bind(&after_pool);
2284 }
2285
2286 // Since a constant pool was just emitted, move the check offset forward by
2287 // the standard interval.
2288 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2289}
2290
2291
2292} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002293
2294#endif // V8_TARGET_ARCH_ARM