blob: 050e15bcc2a46e1454a3c243fc2dc4bfc2653148 [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
906// UBFX <Rd>,<Rn>,#<lsb>,#<width - 1>
907// Instruction details available in ARM DDI 0406A, A8-464.
908// cond(31-28) | 01111(27-23)| 1(22) | 1(21) | widthm1(20-16) |
909// Rd(15-12) | lsb(11-7) | 101(6-4) | Rn(3-0)
910void Assembler::ubfx(Register dst, Register src1, const Operand& src2,
911 const Operand& src3, Condition cond) {
912 ASSERT(!src2.rm_.is_valid() && !src3.rm_.is_valid());
913 ASSERT(static_cast<uint32_t>(src2.imm32_) <= 0x1f);
914 ASSERT(static_cast<uint32_t>(src3.imm32_) <= 0x1f);
915 emit(cond | 0x3F*B21 | src3.imm32_*B16 |
916 dst.code()*B12 | src2.imm32_*B7 | 0x5*B4 | src1.code());
917}
918
919
Steve Blocka7e24c12009-10-30 11:49:00 +0000920void Assembler::and_(Register dst, Register src1, const Operand& src2,
921 SBit s, Condition cond) {
922 addrmod1(cond | 0*B21 | s, src1, dst, src2);
923}
924
925
926void Assembler::eor(Register dst, Register src1, const Operand& src2,
927 SBit s, Condition cond) {
928 addrmod1(cond | 1*B21 | s, src1, dst, src2);
929}
930
931
932void Assembler::sub(Register dst, Register src1, const Operand& src2,
933 SBit s, Condition cond) {
934 addrmod1(cond | 2*B21 | s, src1, dst, src2);
935}
936
937
938void Assembler::rsb(Register dst, Register src1, const Operand& src2,
939 SBit s, Condition cond) {
940 addrmod1(cond | 3*B21 | s, src1, dst, src2);
941}
942
943
944void Assembler::add(Register dst, Register src1, const Operand& src2,
945 SBit s, Condition cond) {
946 addrmod1(cond | 4*B21 | s, src1, dst, src2);
947
948 // Eliminate pattern: push(r), pop()
949 // str(src, MemOperand(sp, 4, NegPreIndex), al);
950 // add(sp, sp, Operand(kPointerSize));
951 // Both instructions can be eliminated.
Leon Clarkef7060e22010-06-03 12:02:55 +0100952 if (can_peephole_optimize(2) &&
Andrei Popescu31002712010-02-23 13:46:05 +0000953 // Pattern.
Steve Blocka7e24c12009-10-30 11:49:00 +0000954 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
955 (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) {
956 pc_ -= 2 * kInstrSize;
Leon Clarkef7060e22010-06-03 12:02:55 +0100957 if (FLAG_print_peephole_optimization) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000958 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
959 }
960 }
961}
962
963
964void Assembler::adc(Register dst, Register src1, const Operand& src2,
965 SBit s, Condition cond) {
966 addrmod1(cond | 5*B21 | s, src1, dst, src2);
967}
968
969
970void Assembler::sbc(Register dst, Register src1, const Operand& src2,
971 SBit s, Condition cond) {
972 addrmod1(cond | 6*B21 | s, src1, dst, src2);
973}
974
975
976void Assembler::rsc(Register dst, Register src1, const Operand& src2,
977 SBit s, Condition cond) {
978 addrmod1(cond | 7*B21 | s, src1, dst, src2);
979}
980
981
982void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
983 addrmod1(cond | 8*B21 | S, src1, r0, src2);
984}
985
986
987void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
988 addrmod1(cond | 9*B21 | S, src1, r0, src2);
989}
990
991
992void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
993 addrmod1(cond | 10*B21 | S, src1, r0, src2);
994}
995
996
997void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
998 addrmod1(cond | 11*B21 | S, src1, r0, src2);
999}
1000
1001
1002void Assembler::orr(Register dst, Register src1, const Operand& src2,
1003 SBit s, Condition cond) {
1004 addrmod1(cond | 12*B21 | s, src1, dst, src2);
1005}
1006
1007
1008void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
1009 if (dst.is(pc)) {
1010 WriteRecordedPositions();
1011 }
Steve Block6ded16b2010-05-10 14:33:55 +01001012 // Don't allow nop instructions in the form mov rn, rn to be generated using
1013 // the mov instruction. They must be generated using nop(int)
1014 // pseudo instructions.
1015 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 addrmod1(cond | 13*B21 | s, r0, dst, src);
1017}
1018
1019
1020void Assembler::bic(Register dst, Register src1, const Operand& src2,
1021 SBit s, Condition cond) {
1022 addrmod1(cond | 14*B21 | s, src1, dst, src2);
1023}
1024
1025
1026void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
1027 addrmod1(cond | 15*B21 | s, r0, dst, src);
1028}
1029
1030
Andrei Popescu31002712010-02-23 13:46:05 +00001031// Multiply instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001032void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
1033 SBit s, Condition cond) {
1034 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
1035 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
1036 src2.code()*B8 | B7 | B4 | src1.code());
1037}
1038
1039
1040void Assembler::mul(Register dst, Register src1, Register src2,
1041 SBit s, Condition cond) {
1042 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
1043 // dst goes in bits 16-19 for this instruction!
1044 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
1045}
1046
1047
1048void Assembler::smlal(Register dstL,
1049 Register dstH,
1050 Register src1,
1051 Register src2,
1052 SBit s,
1053 Condition cond) {
1054 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1055 ASSERT(!dstL.is(dstH));
1056 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1057 src2.code()*B8 | B7 | B4 | src1.code());
1058}
1059
1060
1061void Assembler::smull(Register dstL,
1062 Register dstH,
1063 Register src1,
1064 Register src2,
1065 SBit s,
1066 Condition cond) {
1067 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1068 ASSERT(!dstL.is(dstH));
1069 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1070 src2.code()*B8 | B7 | B4 | src1.code());
1071}
1072
1073
1074void Assembler::umlal(Register dstL,
1075 Register dstH,
1076 Register src1,
1077 Register src2,
1078 SBit s,
1079 Condition cond) {
1080 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1081 ASSERT(!dstL.is(dstH));
1082 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1083 src2.code()*B8 | B7 | B4 | src1.code());
1084}
1085
1086
1087void Assembler::umull(Register dstL,
1088 Register dstH,
1089 Register src1,
1090 Register src2,
1091 SBit s,
1092 Condition cond) {
1093 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
1094 ASSERT(!dstL.is(dstH));
1095 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
1096 src2.code()*B8 | B7 | B4 | src1.code());
1097}
1098
1099
Andrei Popescu31002712010-02-23 13:46:05 +00001100// Miscellaneous arithmetic instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001101void Assembler::clz(Register dst, Register src, Condition cond) {
1102 // v5 and above.
1103 ASSERT(!dst.is(pc) && !src.is(pc));
1104 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
1105 15*B8 | B4 | src.code());
1106}
1107
1108
Andrei Popescu31002712010-02-23 13:46:05 +00001109// Status register access instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001110void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1111 ASSERT(!dst.is(pc));
1112 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1113}
1114
1115
1116void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1117 Condition cond) {
1118 ASSERT(fields >= B16 && fields < B20); // at least one field set
1119 Instr instr;
1120 if (!src.rm_.is_valid()) {
Andrei Popescu31002712010-02-23 13:46:05 +00001121 // Immediate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 uint32_t rotate_imm;
1123 uint32_t immed_8;
1124 if (MustUseIp(src.rmode_) ||
1125 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001126 // Immediate operand cannot be encoded, load it first to register ip.
Steve Blocka7e24c12009-10-30 11:49:00 +00001127 RecordRelocInfo(src.rmode_, src.imm32_);
1128 ldr(ip, MemOperand(pc, 0), cond);
1129 msr(fields, Operand(ip), cond);
1130 return;
1131 }
1132 instr = I | rotate_imm*B8 | immed_8;
1133 } else {
1134 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1135 instr = src.rm_.code();
1136 }
1137 emit(cond | instr | B24 | B21 | fields | 15*B12);
1138}
1139
1140
Andrei Popescu31002712010-02-23 13:46:05 +00001141// Load/Store instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001142void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
1143 if (dst.is(pc)) {
1144 WriteRecordedPositions();
1145 }
1146 addrmod2(cond | B26 | L, dst, src);
1147
Leon Clarkef7060e22010-06-03 12:02:55 +01001148 // Eliminate pattern: push(ry), pop(rx)
1149 // str(ry, MemOperand(sp, 4, NegPreIndex), al)
1150 // ldr(rx, MemOperand(sp, 4, PostIndex), al)
1151 // Both instructions can be eliminated if ry = rx.
1152 // If ry != rx, a register copy from ry to rx is inserted
1153 // after eliminating the push and the pop instructions.
1154 Instr push_instr = instr_at(pc_ - 2 * kInstrSize);
1155 Instr pop_instr = instr_at(pc_ - 1 * kInstrSize);
1156
1157 if (can_peephole_optimize(2) &&
1158 IsPush(push_instr) &&
1159 IsPop(pop_instr)) {
1160 if ((pop_instr & kRdMask) != (push_instr & kRdMask)) {
1161 // For consecutive push and pop on different registers,
1162 // we delete both the push & pop and insert a register move.
1163 // push ry, pop rx --> mov rx, ry
1164 Register reg_pushed, reg_popped;
1165 reg_pushed = GetRd(push_instr);
1166 reg_popped = GetRd(pop_instr);
1167 pc_ -= 2 * kInstrSize;
1168 // Insert a mov instruction, which is better than a pair of push & pop
1169 mov(reg_popped, reg_pushed);
1170 if (FLAG_print_peephole_optimization) {
1171 PrintF("%x push/pop (diff reg) replaced by a reg move\n", pc_offset());
1172 }
1173 } else {
1174 // For consecutive push and pop on the same register,
1175 // both the push and the pop can be deleted.
1176 pc_ -= 2 * kInstrSize;
1177 if (FLAG_print_peephole_optimization) {
1178 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1179 }
1180 }
1181 }
1182
1183 if (can_peephole_optimize(2)) {
1184 Instr str_instr = instr_at(pc_ - 2 * kInstrSize);
1185 Instr ldr_instr = instr_at(pc_ - 1 * kInstrSize);
1186
1187 if ((IsStrRegFpOffset(str_instr) &&
1188 IsLdrRegFpOffset(ldr_instr)) ||
1189 (IsStrRegFpNegOffset(str_instr) &&
1190 IsLdrRegFpNegOffset(ldr_instr))) {
1191 if ((ldr_instr & kLdrStrInstrArgumentMask) ==
1192 (str_instr & kLdrStrInstrArgumentMask)) {
1193 // Pattern: Ldr/str same fp+offset, same register.
1194 //
1195 // The following:
1196 // str rx, [fp, #-12]
1197 // ldr rx, [fp, #-12]
1198 //
1199 // Becomes:
1200 // str rx, [fp, #-12]
1201
1202 pc_ -= 1 * kInstrSize;
1203 if (FLAG_print_peephole_optimization) {
1204 PrintF("%x str/ldr (fp + same offset), same reg\n", pc_offset());
1205 }
1206 } else if ((ldr_instr & kLdrStrOffsetMask) ==
1207 (str_instr & kLdrStrOffsetMask)) {
1208 // Pattern: Ldr/str same fp+offset, different register.
1209 //
1210 // The following:
1211 // str rx, [fp, #-12]
1212 // ldr ry, [fp, #-12]
1213 //
1214 // Becomes:
1215 // str rx, [fp, #-12]
1216 // mov ry, rx
1217
1218 Register reg_stored, reg_loaded;
1219 reg_stored = GetRd(str_instr);
1220 reg_loaded = GetRd(ldr_instr);
1221 pc_ -= 1 * kInstrSize;
1222 // Insert a mov instruction, which is better than ldr.
1223 mov(reg_loaded, reg_stored);
1224 if (FLAG_print_peephole_optimization) {
1225 PrintF("%x str/ldr (fp + same offset), diff reg \n", pc_offset());
1226 }
1227 }
1228 }
1229 }
1230
1231 if (can_peephole_optimize(3)) {
1232 Instr mem_write_instr = instr_at(pc_ - 3 * kInstrSize);
1233 Instr ldr_instr = instr_at(pc_ - 2 * kInstrSize);
1234 Instr mem_read_instr = instr_at(pc_ - 1 * kInstrSize);
1235 if (IsPush(mem_write_instr) &&
1236 IsPop(mem_read_instr)) {
1237 if ((IsLdrRegFpOffset(ldr_instr) ||
1238 IsLdrRegFpNegOffset(ldr_instr))) {
1239 if ((mem_write_instr & kRdMask) ==
1240 (mem_read_instr & kRdMask)) {
1241 // Pattern: push & pop from/to same register,
1242 // with a fp+offset ldr in between
1243 //
1244 // The following:
1245 // str rx, [sp, #-4]!
1246 // ldr rz, [fp, #-24]
1247 // ldr rx, [sp], #+4
1248 //
1249 // Becomes:
1250 // if(rx == rz)
1251 // delete all
1252 // else
1253 // ldr rz, [fp, #-24]
1254
1255 if ((mem_write_instr & kRdMask) == (ldr_instr & kRdMask)) {
1256 pc_ -= 3 * kInstrSize;
1257 } else {
1258 pc_ -= 3 * kInstrSize;
1259 // Reinsert back the ldr rz.
1260 emit(ldr_instr);
1261 }
1262 if (FLAG_print_peephole_optimization) {
1263 PrintF("%x push/pop -dead ldr fp+offset in middle\n", pc_offset());
1264 }
1265 } else {
1266 // Pattern: push & pop from/to different registers
1267 // with a fp+offset ldr in between
1268 //
1269 // The following:
1270 // str rx, [sp, #-4]!
1271 // ldr rz, [fp, #-24]
1272 // ldr ry, [sp], #+4
1273 //
1274 // Becomes:
1275 // if(ry == rz)
1276 // mov ry, rx;
1277 // else if(rx != rz)
1278 // ldr rz, [fp, #-24]
1279 // mov ry, rx
1280 // else if((ry != rz) || (rx == rz)) becomes:
1281 // mov ry, rx
1282 // ldr rz, [fp, #-24]
1283
1284 Register reg_pushed, reg_popped;
1285 if ((mem_read_instr & kRdMask) == (ldr_instr & kRdMask)) {
1286 reg_pushed = GetRd(mem_write_instr);
1287 reg_popped = GetRd(mem_read_instr);
1288 pc_ -= 3 * kInstrSize;
1289 mov(reg_popped, reg_pushed);
1290 } else if ((mem_write_instr & kRdMask)
1291 != (ldr_instr & kRdMask)) {
1292 reg_pushed = GetRd(mem_write_instr);
1293 reg_popped = GetRd(mem_read_instr);
1294 pc_ -= 3 * kInstrSize;
1295 emit(ldr_instr);
1296 mov(reg_popped, reg_pushed);
1297 } else if (((mem_read_instr & kRdMask)
1298 != (ldr_instr & kRdMask)) ||
1299 ((mem_write_instr & kRdMask)
1300 == (ldr_instr & kRdMask)) ) {
1301 reg_pushed = GetRd(mem_write_instr);
1302 reg_popped = GetRd(mem_read_instr);
1303 pc_ -= 3 * kInstrSize;
1304 mov(reg_popped, reg_pushed);
1305 emit(ldr_instr);
1306 }
1307 if (FLAG_print_peephole_optimization) {
1308 PrintF("%x push/pop (ldr fp+off in middle)\n", pc_offset());
1309 }
1310 }
1311 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001312 }
1313 }
1314}
1315
1316
1317void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1318 addrmod2(cond | B26, src, dst);
1319
1320 // Eliminate pattern: pop(), push(r)
1321 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1322 // -> str r, [sp, 0], al
Leon Clarkef7060e22010-06-03 12:02:55 +01001323 if (can_peephole_optimize(2) &&
Andrei Popescu31002712010-02-23 13:46:05 +00001324 // Pattern.
Steve Blocka7e24c12009-10-30 11:49:00 +00001325 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1326 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1327 pc_ -= 2 * kInstrSize;
1328 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
Leon Clarkef7060e22010-06-03 12:02:55 +01001329 if (FLAG_print_peephole_optimization) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1331 }
1332 }
1333}
1334
1335
1336void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1337 addrmod2(cond | B26 | B | L, dst, src);
1338}
1339
1340
1341void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1342 addrmod2(cond | B26 | B, src, dst);
1343}
1344
1345
1346void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1347 addrmod3(cond | L | B7 | H | B4, dst, src);
1348}
1349
1350
1351void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1352 addrmod3(cond | B7 | H | B4, src, dst);
1353}
1354
1355
1356void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1357 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1358}
1359
1360
1361void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1362 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1363}
1364
1365
Leon Clarkef7060e22010-06-03 12:02:55 +01001366void Assembler::ldrd(Register dst1, Register dst2,
1367 const MemOperand& src, Condition cond) {
1368 ASSERT(CpuFeatures::IsEnabled(ARMv7));
Kristian Monsen25f61362010-05-21 11:50:48 +01001369 ASSERT(src.rm().is(no_reg));
Leon Clarkef7060e22010-06-03 12:02:55 +01001370 ASSERT(!dst1.is(lr)); // r14.
1371 ASSERT_EQ(0, dst1.code() % 2);
1372 ASSERT_EQ(dst1.code() + 1, dst2.code());
1373 addrmod3(cond | B7 | B6 | B4, dst1, src);
Kristian Monsen25f61362010-05-21 11:50:48 +01001374}
1375
1376
Leon Clarkef7060e22010-06-03 12:02:55 +01001377void Assembler::strd(Register src1, Register src2,
1378 const MemOperand& dst, Condition cond) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001379 ASSERT(dst.rm().is(no_reg));
Leon Clarkef7060e22010-06-03 12:02:55 +01001380 ASSERT(!src1.is(lr)); // r14.
1381 ASSERT_EQ(0, src1.code() % 2);
1382 ASSERT_EQ(src1.code() + 1, src2.code());
1383 ASSERT(CpuFeatures::IsEnabled(ARMv7));
1384 addrmod3(cond | B7 | B6 | B5 | B4, src1, dst);
Kristian Monsen25f61362010-05-21 11:50:48 +01001385}
1386
Andrei Popescu31002712010-02-23 13:46:05 +00001387// Load/Store multiple instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001388void Assembler::ldm(BlockAddrMode am,
1389 Register base,
1390 RegList dst,
1391 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001392 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
Steve Blocka7e24c12009-10-30 11:49:00 +00001393 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1394
1395 addrmod4(cond | B27 | am | L, base, dst);
1396
Andrei Popescu31002712010-02-23 13:46:05 +00001397 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
Steve Blocka7e24c12009-10-30 11:49:00 +00001398 if (cond == al && (dst & pc.bit()) != 0) {
1399 // There is a slight chance that the ldm instruction was actually a call,
1400 // in which case it would be wrong to return into the constant pool; we
1401 // recognize this case by checking if the emission of the pool was blocked
1402 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1403 // the case, we emit a jump over the pool.
1404 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1405 }
1406}
1407
1408
1409void Assembler::stm(BlockAddrMode am,
1410 Register base,
1411 RegList src,
1412 Condition cond) {
1413 addrmod4(cond | B27 | am, base, src);
1414}
1415
1416
Andrei Popescu31002712010-02-23 13:46:05 +00001417// Exception-generating instructions and debugging support.
Steve Blocka7e24c12009-10-30 11:49:00 +00001418void Assembler::stop(const char* msg) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001419#ifndef __arm__
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 // The simulator handles these special instructions and stops execution.
1421 emit(15 << 28 | ((intptr_t) msg));
Andrei Popescu402d9372010-02-26 13:31:12 +00001422#else // def __arm__
1423#ifdef CAN_USE_ARMV5_INSTRUCTIONS
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 bkpt(0);
Andrei Popescu402d9372010-02-26 13:31:12 +00001425#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
1426 swi(0x9f0001);
1427#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1428#endif // def __arm__
Steve Blocka7e24c12009-10-30 11:49:00 +00001429}
1430
1431
1432void Assembler::bkpt(uint32_t imm16) { // v5 and above
1433 ASSERT(is_uint16(imm16));
1434 emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf));
1435}
1436
1437
1438void Assembler::swi(uint32_t imm24, Condition cond) {
1439 ASSERT(is_uint24(imm24));
1440 emit(cond | 15*B24 | imm24);
1441}
1442
1443
Andrei Popescu31002712010-02-23 13:46:05 +00001444// Coprocessor instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001445void Assembler::cdp(Coprocessor coproc,
1446 int opcode_1,
1447 CRegister crd,
1448 CRegister crn,
1449 CRegister crm,
1450 int opcode_2,
1451 Condition cond) {
1452 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1453 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1454 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1455}
1456
1457
1458void Assembler::cdp2(Coprocessor coproc,
1459 int opcode_1,
1460 CRegister crd,
1461 CRegister crn,
1462 CRegister crm,
1463 int opcode_2) { // v5 and above
1464 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv));
1465}
1466
1467
1468void Assembler::mcr(Coprocessor coproc,
1469 int opcode_1,
1470 Register rd,
1471 CRegister crn,
1472 CRegister crm,
1473 int opcode_2,
1474 Condition cond) {
1475 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1476 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1477 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1478}
1479
1480
1481void Assembler::mcr2(Coprocessor coproc,
1482 int opcode_1,
1483 Register rd,
1484 CRegister crn,
1485 CRegister crm,
1486 int opcode_2) { // v5 and above
1487 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1488}
1489
1490
1491void Assembler::mrc(Coprocessor coproc,
1492 int opcode_1,
1493 Register rd,
1494 CRegister crn,
1495 CRegister crm,
1496 int opcode_2,
1497 Condition cond) {
1498 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1499 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1500 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1501}
1502
1503
1504void Assembler::mrc2(Coprocessor coproc,
1505 int opcode_1,
1506 Register rd,
1507 CRegister crn,
1508 CRegister crm,
1509 int opcode_2) { // v5 and above
1510 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1511}
1512
1513
1514void Assembler::ldc(Coprocessor coproc,
1515 CRegister crd,
1516 const MemOperand& src,
1517 LFlag l,
1518 Condition cond) {
1519 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1520}
1521
1522
1523void Assembler::ldc(Coprocessor coproc,
1524 CRegister crd,
1525 Register rn,
1526 int option,
1527 LFlag l,
1528 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001529 // Unindexed addressing.
Steve Blocka7e24c12009-10-30 11:49:00 +00001530 ASSERT(is_uint8(option));
1531 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1532 coproc*B8 | (option & 255));
1533}
1534
1535
1536void Assembler::ldc2(Coprocessor coproc,
1537 CRegister crd,
1538 const MemOperand& src,
1539 LFlag l) { // v5 and above
1540 ldc(coproc, crd, src, l, static_cast<Condition>(nv));
1541}
1542
1543
1544void Assembler::ldc2(Coprocessor coproc,
1545 CRegister crd,
1546 Register rn,
1547 int option,
1548 LFlag l) { // v5 and above
1549 ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1550}
1551
1552
1553void Assembler::stc(Coprocessor coproc,
1554 CRegister crd,
1555 const MemOperand& dst,
1556 LFlag l,
1557 Condition cond) {
1558 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1559}
1560
1561
1562void Assembler::stc(Coprocessor coproc,
1563 CRegister crd,
1564 Register rn,
1565 int option,
1566 LFlag l,
1567 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +00001568 // Unindexed addressing.
Steve Blocka7e24c12009-10-30 11:49:00 +00001569 ASSERT(is_uint8(option));
1570 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1571 coproc*B8 | (option & 255));
1572}
1573
1574
1575void Assembler::stc2(Coprocessor
1576 coproc, CRegister crd,
1577 const MemOperand& dst,
1578 LFlag l) { // v5 and above
1579 stc(coproc, crd, dst, l, static_cast<Condition>(nv));
1580}
1581
1582
1583void Assembler::stc2(Coprocessor coproc,
1584 CRegister crd,
1585 Register rn,
1586 int option,
1587 LFlag l) { // v5 and above
1588 stc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1589}
1590
1591
Steve Blockd0582a62009-12-15 09:54:21 +00001592// Support for VFP.
Leon Clarked91b9f72010-01-27 17:25:45 +00001593void Assembler::vldr(const DwVfpRegister dst,
1594 const Register base,
1595 int offset,
1596 const Condition cond) {
1597 // Ddst = MEM(Rbase + offset).
1598 // Instruction details available in ARM DDI 0406A, A8-628.
1599 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1600 // Vdst(15-12) | 1011(11-8) | offset
1601 ASSERT(CpuFeatures::IsEnabled(VFP3));
1602 ASSERT(offset % 4 == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001603 ASSERT((offset / 4) < 256);
Leon Clarked91b9f72010-01-27 17:25:45 +00001604 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1605 0xB*B8 | ((offset / 4) & 255));
1606}
1607
1608
Steve Block6ded16b2010-05-10 14:33:55 +01001609void Assembler::vldr(const SwVfpRegister dst,
1610 const Register base,
1611 int offset,
1612 const Condition cond) {
1613 // Sdst = MEM(Rbase + offset).
1614 // Instruction details available in ARM DDI 0406A, A8-628.
1615 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1616 // Vdst(15-12) | 1010(11-8) | offset
1617 ASSERT(CpuFeatures::IsEnabled(VFP3));
1618 ASSERT(offset % 4 == 0);
1619 ASSERT((offset / 4) < 256);
1620 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1621 0xA*B8 | ((offset / 4) & 255));
1622}
1623
1624
Leon Clarked91b9f72010-01-27 17:25:45 +00001625void Assembler::vstr(const DwVfpRegister src,
1626 const Register base,
1627 int offset,
1628 const Condition cond) {
1629 // MEM(Rbase + offset) = Dsrc.
1630 // Instruction details available in ARM DDI 0406A, A8-786.
1631 // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) |
1632 // Vsrc(15-12) | 1011(11-8) | (offset/4)
1633 ASSERT(CpuFeatures::IsEnabled(VFP3));
1634 ASSERT(offset % 4 == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001635 ASSERT((offset / 4) < 256);
Leon Clarked91b9f72010-01-27 17:25:45 +00001636 emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 |
1637 0xB*B8 | ((offset / 4) & 255));
1638}
1639
1640
Leon Clarkee46be812010-01-19 14:06:41 +00001641void Assembler::vmov(const DwVfpRegister dst,
1642 const Register src1,
1643 const Register src2,
1644 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001645 // Dm = <Rt,Rt2>.
1646 // Instruction details available in ARM DDI 0406A, A8-646.
1647 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
1648 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1649 ASSERT(CpuFeatures::IsEnabled(VFP3));
1650 ASSERT(!src1.is(pc) && !src2.is(pc));
1651 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
1652 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
1653}
1654
1655
Leon Clarkee46be812010-01-19 14:06:41 +00001656void Assembler::vmov(const Register dst1,
1657 const Register dst2,
1658 const DwVfpRegister src,
1659 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001660 // <Rt,Rt2> = Dm.
1661 // Instruction details available in ARM DDI 0406A, A8-646.
1662 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
1663 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1664 ASSERT(CpuFeatures::IsEnabled(VFP3));
1665 ASSERT(!dst1.is(pc) && !dst2.is(pc));
1666 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
1667 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
1668}
1669
1670
Leon Clarkee46be812010-01-19 14:06:41 +00001671void Assembler::vmov(const SwVfpRegister dst,
Steve Blockd0582a62009-12-15 09:54:21 +00001672 const Register src,
Steve Blockd0582a62009-12-15 09:54:21 +00001673 const Condition cond) {
1674 // Sn = Rt.
1675 // Instruction details available in ARM DDI 0406A, A8-642.
1676 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
1677 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1678 ASSERT(CpuFeatures::IsEnabled(VFP3));
1679 ASSERT(!src.is(pc));
1680 emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 |
1681 src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4);
1682}
1683
1684
Leon Clarkee46be812010-01-19 14:06:41 +00001685void Assembler::vmov(const Register dst,
1686 const SwVfpRegister src,
Steve Blockd0582a62009-12-15 09:54:21 +00001687 const Condition cond) {
1688 // Rt = Sn.
1689 // Instruction details available in ARM DDI 0406A, A8-642.
1690 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
1691 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1692 ASSERT(CpuFeatures::IsEnabled(VFP3));
1693 ASSERT(!dst.is(pc));
1694 emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 |
1695 dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4);
1696}
1697
1698
Steve Block6ded16b2010-05-10 14:33:55 +01001699// Type of data to read from or write to VFP register.
1700// Used as specifier in generic vcvt instruction.
1701enum VFPType { S32, U32, F32, F64 };
1702
1703
1704static bool IsSignedVFPType(VFPType type) {
1705 switch (type) {
1706 case S32:
1707 return true;
1708 case U32:
1709 return false;
1710 default:
1711 UNREACHABLE();
1712 return false;
1713 }
Steve Blockd0582a62009-12-15 09:54:21 +00001714}
1715
1716
Steve Block6ded16b2010-05-10 14:33:55 +01001717static bool IsIntegerVFPType(VFPType type) {
1718 switch (type) {
1719 case S32:
1720 case U32:
1721 return true;
1722 case F32:
1723 case F64:
1724 return false;
1725 default:
1726 UNREACHABLE();
1727 return false;
1728 }
1729}
1730
1731
1732static bool IsDoubleVFPType(VFPType type) {
1733 switch (type) {
1734 case F32:
1735 return false;
1736 case F64:
1737 return true;
1738 default:
1739 UNREACHABLE();
1740 return false;
1741 }
1742}
1743
1744
1745// Depending on split_last_bit split binary representation of reg_code into Vm:M
1746// or M:Vm form (where M is single bit).
1747static void SplitRegCode(bool split_last_bit,
1748 int reg_code,
1749 int* vm,
1750 int* m) {
1751 if (split_last_bit) {
1752 *m = reg_code & 0x1;
1753 *vm = reg_code >> 1;
1754 } else {
1755 *m = (reg_code & 0x10) >> 4;
1756 *vm = reg_code & 0x0F;
1757 }
1758}
1759
1760
1761// Encode vcvt.src_type.dst_type instruction.
1762static Instr EncodeVCVT(const VFPType dst_type,
1763 const int dst_code,
1764 const VFPType src_type,
1765 const int src_code,
1766 const Condition cond) {
1767 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
1768 // Conversion between IEEE floating point and 32-bit integer.
1769 // Instruction details available in ARM DDI 0406B, A8.6.295.
1770 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
1771 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
1772 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
1773
1774 int sz, opc2, D, Vd, M, Vm, op;
1775
1776 if (IsIntegerVFPType(dst_type)) {
1777 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
1778 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
1779 op = 1; // round towards zero
1780 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
1781 SplitRegCode(true, dst_code, &Vd, &D);
1782 } else {
1783 ASSERT(IsIntegerVFPType(src_type));
1784
1785 opc2 = 0x0;
1786 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
1787 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
1788 SplitRegCode(true, src_code, &Vm, &M);
1789 SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D);
1790 }
1791
1792 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
1793 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
1794 } else {
1795 // Conversion between IEEE double and single precision.
1796 // Instruction details available in ARM DDI 0406B, A8.6.298.
1797 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
1798 // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
1799 int sz, D, Vd, M, Vm;
1800
1801 ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type));
1802 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
1803 SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D);
1804 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
1805
1806 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
1807 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
1808 }
1809}
1810
1811
1812void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
1813 const SwVfpRegister src,
1814 const Condition cond) {
Steve Blockd0582a62009-12-15 09:54:21 +00001815 ASSERT(CpuFeatures::IsEnabled(VFP3));
Steve Block6ded16b2010-05-10 14:33:55 +01001816 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond));
1817}
1818
1819
1820void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
1821 const SwVfpRegister src,
1822 const Condition cond) {
1823 ASSERT(CpuFeatures::IsEnabled(VFP3));
1824 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond));
1825}
1826
1827
1828void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
1829 const SwVfpRegister src,
1830 const Condition cond) {
1831 ASSERT(CpuFeatures::IsEnabled(VFP3));
1832 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond));
1833}
1834
1835
1836void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
1837 const DwVfpRegister src,
1838 const Condition cond) {
1839 ASSERT(CpuFeatures::IsEnabled(VFP3));
1840 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond));
1841}
1842
1843
1844void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
1845 const DwVfpRegister src,
1846 const Condition cond) {
1847 ASSERT(CpuFeatures::IsEnabled(VFP3));
1848 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond));
1849}
1850
1851
1852void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
1853 const SwVfpRegister src,
1854 const Condition cond) {
1855 ASSERT(CpuFeatures::IsEnabled(VFP3));
1856 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond));
1857}
1858
1859
1860void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
1861 const DwVfpRegister src,
1862 const Condition cond) {
1863 ASSERT(CpuFeatures::IsEnabled(VFP3));
1864 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond));
Steve Blockd0582a62009-12-15 09:54:21 +00001865}
1866
1867
Leon Clarkee46be812010-01-19 14:06:41 +00001868void Assembler::vadd(const DwVfpRegister dst,
1869 const DwVfpRegister src1,
1870 const DwVfpRegister src2,
1871 const Condition cond) {
1872 // Dd = vadd(Dn, Dm) double precision floating point addition.
Steve Blockd0582a62009-12-15 09:54:21 +00001873 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1874 // Instruction details available in ARM DDI 0406A, A8-536.
1875 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
1876 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1877 ASSERT(CpuFeatures::IsEnabled(VFP3));
1878 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
1879 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1880}
1881
1882
Leon Clarkee46be812010-01-19 14:06:41 +00001883void Assembler::vsub(const DwVfpRegister dst,
1884 const DwVfpRegister src1,
1885 const DwVfpRegister src2,
1886 const Condition cond) {
1887 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
Steve Blockd0582a62009-12-15 09:54:21 +00001888 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1889 // Instruction details available in ARM DDI 0406A, A8-784.
1890 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
1891 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
1892 ASSERT(CpuFeatures::IsEnabled(VFP3));
1893 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
1894 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
1895}
1896
1897
Leon Clarkee46be812010-01-19 14:06:41 +00001898void Assembler::vmul(const DwVfpRegister dst,
1899 const DwVfpRegister src1,
1900 const DwVfpRegister src2,
1901 const Condition cond) {
1902 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
Steve Blockd0582a62009-12-15 09:54:21 +00001903 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1904 // Instruction details available in ARM DDI 0406A, A8-784.
1905 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
1906 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1907 ASSERT(CpuFeatures::IsEnabled(VFP3));
1908 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
1909 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1910}
1911
1912
Leon Clarkee46be812010-01-19 14:06:41 +00001913void Assembler::vdiv(const DwVfpRegister dst,
1914 const DwVfpRegister src1,
1915 const DwVfpRegister src2,
1916 const Condition cond) {
1917 // Dd = vdiv(Dn, Dm) double precision floating point division.
Steve Blockd0582a62009-12-15 09:54:21 +00001918 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1919 // Instruction details available in ARM DDI 0406A, A8-584.
1920 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
1921 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1922 ASSERT(CpuFeatures::IsEnabled(VFP3));
1923 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
1924 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1925}
1926
1927
Leon Clarkee46be812010-01-19 14:06:41 +00001928void Assembler::vcmp(const DwVfpRegister src1,
1929 const DwVfpRegister src2,
Steve Blockd0582a62009-12-15 09:54:21 +00001930 const SBit s,
1931 const Condition cond) {
1932 // vcmp(Dd, Dm) double precision floating point comparison.
1933 // Instruction details available in ARM DDI 0406A, A8-570.
1934 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
1935 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0)
1936 ASSERT(CpuFeatures::IsEnabled(VFP3));
1937 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
1938 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
1939}
1940
1941
1942void Assembler::vmrs(Register dst, Condition cond) {
1943 // Instruction details available in ARM DDI 0406A, A8-652.
1944 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
1945 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
1946 ASSERT(CpuFeatures::IsEnabled(VFP3));
1947 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
1948 dst.code()*B12 | 0xA*B8 | B4);
1949}
1950
1951
Andrei Popescu31002712010-02-23 13:46:05 +00001952// Pseudo instructions.
Steve Block6ded16b2010-05-10 14:33:55 +01001953void Assembler::nop(int type) {
1954 // This is mov rx, rx.
1955 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
1956 emit(al | 13*B21 | type*B12 | type);
1957}
1958
1959
Steve Blockd0582a62009-12-15 09:54:21 +00001960bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
1961 uint32_t dummy1;
1962 uint32_t dummy2;
1963 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
1964}
1965
1966
1967void Assembler::BlockConstPoolFor(int instructions) {
1968 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
1969}
1970
1971
Andrei Popescu31002712010-02-23 13:46:05 +00001972// Debugging.
Steve Blocka7e24c12009-10-30 11:49:00 +00001973void Assembler::RecordJSReturn() {
1974 WriteRecordedPositions();
1975 CheckBuffer();
1976 RecordRelocInfo(RelocInfo::JS_RETURN);
1977}
1978
1979
1980void Assembler::RecordComment(const char* msg) {
1981 if (FLAG_debug_code) {
1982 CheckBuffer();
1983 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
1984 }
1985}
1986
1987
1988void Assembler::RecordPosition(int pos) {
1989 if (pos == RelocInfo::kNoPosition) return;
1990 ASSERT(pos >= 0);
1991 current_position_ = pos;
1992}
1993
1994
1995void Assembler::RecordStatementPosition(int pos) {
1996 if (pos == RelocInfo::kNoPosition) return;
1997 ASSERT(pos >= 0);
1998 current_statement_position_ = pos;
1999}
2000
2001
2002void Assembler::WriteRecordedPositions() {
2003 // Write the statement position if it is different from what was written last
2004 // time.
2005 if (current_statement_position_ != written_statement_position_) {
2006 CheckBuffer();
2007 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_);
2008 written_statement_position_ = current_statement_position_;
2009 }
2010
2011 // Write the position if it is different from what was written last time and
2012 // also different from the written statement position.
2013 if (current_position_ != written_position_ &&
2014 current_position_ != written_statement_position_) {
2015 CheckBuffer();
2016 RecordRelocInfo(RelocInfo::POSITION, current_position_);
2017 written_position_ = current_position_;
2018 }
2019}
2020
2021
2022void Assembler::GrowBuffer() {
2023 if (!own_buffer_) FATAL("external code buffer is too small");
2024
Andrei Popescu31002712010-02-23 13:46:05 +00002025 // Compute new buffer size.
Steve Blocka7e24c12009-10-30 11:49:00 +00002026 CodeDesc desc; // the new buffer
2027 if (buffer_size_ < 4*KB) {
2028 desc.buffer_size = 4*KB;
2029 } else if (buffer_size_ < 1*MB) {
2030 desc.buffer_size = 2*buffer_size_;
2031 } else {
2032 desc.buffer_size = buffer_size_ + 1*MB;
2033 }
2034 CHECK_GT(desc.buffer_size, 0); // no overflow
2035
Andrei Popescu31002712010-02-23 13:46:05 +00002036 // Setup new buffer.
Steve Blocka7e24c12009-10-30 11:49:00 +00002037 desc.buffer = NewArray<byte>(desc.buffer_size);
2038
2039 desc.instr_size = pc_offset();
2040 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
2041
Andrei Popescu31002712010-02-23 13:46:05 +00002042 // Copy the data.
Steve Blocka7e24c12009-10-30 11:49:00 +00002043 int pc_delta = desc.buffer - buffer_;
2044 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2045 memmove(desc.buffer, buffer_, desc.instr_size);
2046 memmove(reloc_info_writer.pos() + rc_delta,
2047 reloc_info_writer.pos(), desc.reloc_size);
2048
Andrei Popescu31002712010-02-23 13:46:05 +00002049 // Switch buffers.
Steve Blocka7e24c12009-10-30 11:49:00 +00002050 DeleteArray(buffer_);
2051 buffer_ = desc.buffer;
2052 buffer_size_ = desc.buffer_size;
2053 pc_ += pc_delta;
2054 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2055 reloc_info_writer.last_pc() + pc_delta);
2056
Andrei Popescu31002712010-02-23 13:46:05 +00002057 // None of our relocation types are pc relative pointing outside the code
Steve Blocka7e24c12009-10-30 11:49:00 +00002058 // buffer nor pc absolute pointing inside the code buffer, so there is no need
Andrei Popescu31002712010-02-23 13:46:05 +00002059 // to relocate any emitted relocation entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002060
Andrei Popescu31002712010-02-23 13:46:05 +00002061 // Relocate pending relocation entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002062 for (int i = 0; i < num_prinfo_; i++) {
2063 RelocInfo& rinfo = prinfo_[i];
2064 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2065 rinfo.rmode() != RelocInfo::POSITION);
2066 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
2067 rinfo.set_pc(rinfo.pc() + pc_delta);
2068 }
2069 }
2070}
2071
2072
2073void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2074 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
2075 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::STATEMENT_POSITION) {
Andrei Popescu31002712010-02-23 13:46:05 +00002076 // Adjust code for new modes.
Steve Blocka7e24c12009-10-30 11:49:00 +00002077 ASSERT(RelocInfo::IsJSReturn(rmode)
2078 || RelocInfo::IsComment(rmode)
2079 || RelocInfo::IsPosition(rmode));
Andrei Popescu31002712010-02-23 13:46:05 +00002080 // These modes do not need an entry in the constant pool.
Steve Blocka7e24c12009-10-30 11:49:00 +00002081 } else {
2082 ASSERT(num_prinfo_ < kMaxNumPRInfo);
2083 prinfo_[num_prinfo_++] = rinfo;
2084 // Make sure the constant pool is not emitted in place of the next
Andrei Popescu31002712010-02-23 13:46:05 +00002085 // instruction for which we just recorded relocation info.
Steve Blocka7e24c12009-10-30 11:49:00 +00002086 BlockConstPoolBefore(pc_offset() + kInstrSize);
2087 }
2088 if (rinfo.rmode() != RelocInfo::NONE) {
2089 // Don't record external references unless the heap will be serialized.
Steve Blockd0582a62009-12-15 09:54:21 +00002090 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
2091#ifdef DEBUG
2092 if (!Serializer::enabled()) {
2093 Serializer::TooLateToEnableNow();
2094 }
2095#endif
2096 if (!Serializer::enabled() && !FLAG_debug_code) {
2097 return;
2098 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002099 }
2100 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2101 reloc_info_writer.Write(&rinfo);
2102 }
2103}
2104
2105
2106void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2107 // Calculate the offset of the next check. It will be overwritten
2108 // when a const pool is generated or when const pools are being
2109 // blocked for a specific range.
2110 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2111
Andrei Popescu31002712010-02-23 13:46:05 +00002112 // There is nothing to do if there are no pending relocation info entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002113 if (num_prinfo_ == 0) return;
2114
2115 // We emit a constant pool at regular intervals of about kDistBetweenPools
2116 // or when requested by parameter force_emit (e.g. after each function).
2117 // We prefer not to emit a jump unless the max distance is reached or if we
2118 // are running low on slots, which can happen if a lot of constants are being
2119 // emitted (e.g. --debug-code and many static references).
2120 int dist = pc_offset() - last_const_pool_end_;
2121 if (!force_emit && dist < kMaxDistBetweenPools &&
2122 (require_jump || dist < kDistBetweenPools) &&
2123 // TODO(1236125): Cleanup the "magic" number below. We know that
2124 // the code generation will test every kCheckConstIntervalInst.
2125 // Thus we are safe as long as we generate less than 7 constant
2126 // entries per instruction.
2127 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
2128 return;
2129 }
2130
2131 // If we did not return by now, we need to emit the constant pool soon.
2132
2133 // However, some small sequences of instructions must not be broken up by the
2134 // insertion of a constant pool; such sequences are protected by setting
Steve Block6ded16b2010-05-10 14:33:55 +01002135 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
2136 // both checked here. Also, recursive calls to CheckConstPool are blocked by
2137 // no_const_pool_before_.
2138 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
Andrei Popescu31002712010-02-23 13:46:05 +00002139 // Emission is currently blocked; make sure we try again as soon as
2140 // possible.
Steve Block6ded16b2010-05-10 14:33:55 +01002141 if (const_pool_blocked_nesting_ > 0) {
2142 next_buffer_check_ = pc_offset() + kInstrSize;
2143 } else {
2144 next_buffer_check_ = no_const_pool_before_;
2145 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002146
Andrei Popescu31002712010-02-23 13:46:05 +00002147 // Something is wrong if emission is forced and blocked at the same time.
Steve Blocka7e24c12009-10-30 11:49:00 +00002148 ASSERT(!force_emit);
2149 return;
2150 }
2151
2152 int jump_instr = require_jump ? kInstrSize : 0;
2153
2154 // Check that the code buffer is large enough before emitting the constant
2155 // pool and relocation information (include the jump over the pool and the
2156 // constant pool marker).
2157 int max_needed_space =
2158 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
2159 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
2160
Andrei Popescu31002712010-02-23 13:46:05 +00002161 // Block recursive calls to CheckConstPool.
Steve Blocka7e24c12009-10-30 11:49:00 +00002162 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
2163 num_prinfo_*kInstrSize);
2164 // Don't bother to check for the emit calls below.
2165 next_buffer_check_ = no_const_pool_before_;
2166
Andrei Popescu31002712010-02-23 13:46:05 +00002167 // Emit jump over constant pool if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002168 Label after_pool;
2169 if (require_jump) b(&after_pool);
2170
2171 RecordComment("[ Constant Pool");
2172
Andrei Popescu31002712010-02-23 13:46:05 +00002173 // Put down constant pool marker "Undefined instruction" as specified by
2174 // A3.1 Instruction set encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +00002175 emit(0x03000000 | num_prinfo_);
2176
Andrei Popescu31002712010-02-23 13:46:05 +00002177 // Emit constant pool entries.
Steve Blocka7e24c12009-10-30 11:49:00 +00002178 for (int i = 0; i < num_prinfo_; i++) {
2179 RelocInfo& rinfo = prinfo_[i];
2180 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2181 rinfo.rmode() != RelocInfo::POSITION &&
2182 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
2183 Instr instr = instr_at(rinfo.pc());
2184
Andrei Popescu31002712010-02-23 13:46:05 +00002185 // Instruction to patch must be a ldr/str [pc, #offset].
2186 // P and U set, B and W clear, Rn == pc, offset12 still 0.
Steve Blocka7e24c12009-10-30 11:49:00 +00002187 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) ==
2188 (2*B25 | P | U | pc.code()*B16));
2189 int delta = pc_ - rinfo.pc() - 8;
2190 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2191 if (delta < 0) {
2192 instr &= ~U;
2193 delta = -delta;
2194 }
2195 ASSERT(is_uint12(delta));
2196 instr_at_put(rinfo.pc(), instr + delta);
2197 emit(rinfo.data());
2198 }
2199 num_prinfo_ = 0;
2200 last_const_pool_end_ = pc_offset();
2201
2202 RecordComment("]");
2203
2204 if (after_pool.is_linked()) {
2205 bind(&after_pool);
2206 }
2207
2208 // Since a constant pool was just emitted, move the check offset forward by
2209 // the standard interval.
2210 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2211}
2212
2213
2214} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002215
2216#endif // V8_TARGET_ARCH_ARM