blob: 7990368e661976944739480529d3a5f191d7d654 [file] [log] [blame]
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003//
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the
14// distribution.
15//
16// - Neither the name of Sun Microsystems or the names of contributors may
17// be used to endorse or promote products derived from this software without
18// specific prior written permission.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000019//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000022// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31// OF THE POSSIBILITY OF SUCH DAMAGE.
32
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033// The original source code covered by the above license above has been
34// modified significantly by Google Inc.
35// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37#include "v8.h"
38
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000039#include "arm/assembler-arm-inl.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000040#include "serialize.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
kasperl@chromium.org71affb52009-05-26 05:44:31 +000042namespace v8 {
43namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
ager@chromium.orgc4c92722009-11-18 14:12:51 +000045// Safe default is no features.
46unsigned CpuFeatures::supported_ = 0;
47unsigned CpuFeatures::enabled_ = 0;
48unsigned CpuFeatures::found_by_runtime_probing_ = 0;
49
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000050
51#ifdef __arm__
52static uint64_t CpuFeaturesImpliedByCompiler() {
53 uint64_t answer = 0;
54#ifdef CAN_USE_ARMV7_INSTRUCTIONS
55 answer |= 1u << ARMv7;
56#endif // def CAN_USE_ARMV7_INSTRUCTIONS
57 // If the compiler is allowed to use VFP then we can use VFP too in our code
58 // generation even when generating snapshots. This won't work for cross
59 // compilation.
60#if defined(__VFP_FP__) && !defined(__SOFTFP__)
61 answer |= 1u << VFP3;
62#endif // defined(__VFP_FP__) && !defined(__SOFTFP__)
63#ifdef CAN_USE_VFP_INSTRUCTIONS
64 answer |= 1u << VFP3;
65#endif // def CAN_USE_VFP_INSTRUCTIONS
66 return answer;
67}
68#endif // def __arm__
69
70
ager@chromium.orgc4c92722009-11-18 14:12:51 +000071void CpuFeatures::Probe() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000072#ifndef __arm__
ager@chromium.org5c838252010-02-19 08:53:10 +000073 // For the simulator=arm build, use VFP when FLAG_enable_vfp3 is enabled.
74 if (FLAG_enable_vfp3) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000075 supported_ |= 1u << VFP3;
ager@chromium.org5c838252010-02-19 08:53:10 +000076 }
77 // For the simulator=arm build, use ARMv7 when FLAG_enable_armv7 is enabled
78 if (FLAG_enable_armv7) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000079 supported_ |= 1u << ARMv7;
ager@chromium.org5c838252010-02-19 08:53:10 +000080 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000081#else // def __arm__
ager@chromium.orgc4c92722009-11-18 14:12:51 +000082 if (Serializer::enabled()) {
83 supported_ |= OS::CpuFeaturesImpliedByPlatform();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000084 supported_ |= CpuFeaturesImpliedByCompiler();
ager@chromium.orgc4c92722009-11-18 14:12:51 +000085 return; // No features if we might serialize.
86 }
87
88 if (OS::ArmCpuHasFeature(VFP3)) {
89 // This implementation also sets the VFP flags if
90 // runtime detection of VFP returns true.
91 supported_ |= 1u << VFP3;
92 found_by_runtime_probing_ |= 1u << VFP3;
93 }
ager@chromium.org5c838252010-02-19 08:53:10 +000094
95 if (OS::ArmCpuHasFeature(ARMv7)) {
96 supported_ |= 1u << ARMv7;
97 found_by_runtime_probing_ |= 1u << ARMv7;
98 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +000099#endif
100}
101
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104// Implementation of RelocInfo
105
106const int RelocInfo::kApplyMask = 0;
107
108
iposva@chromium.org245aa852009-02-10 00:49:54 +0000109void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 // Patch the code at the current address with the supplied instructions.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000111 Instr* pc = reinterpret_cast<Instr*>(pc_);
112 Instr* instr = reinterpret_cast<Instr*>(instructions);
113 for (int i = 0; i < instruction_count; i++) {
114 *(pc + i) = *(instr + i);
115 }
116
117 // Indicate that code has changed.
118 CPU::FlushICache(pc_, instruction_count * Assembler::kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119}
120
121
122// Patch the code at the current PC with a call to the target address.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000123// Additional guard instructions can be added if required.
124void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 // Patch the code at the current address with a call to the target.
126 UNIMPLEMENTED();
127}
128
129
130// -----------------------------------------------------------------------------
131// Implementation of Operand and MemOperand
132// See assembler-arm-inl.h for inlined constructors
133
134Operand::Operand(Handle<Object> handle) {
135 rm_ = no_reg;
136 // Verify all Objects referred by code are NOT in new space.
137 Object* obj = *handle;
138 ASSERT(!Heap::InNewSpace(obj));
139 if (obj->IsHeapObject()) {
140 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000141 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 } else {
143 // no relocation needed
144 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000145 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 }
147}
148
149
150Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
151 ASSERT(is_uint5(shift_imm));
152 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
153 rm_ = rm;
154 rs_ = no_reg;
155 shift_op_ = shift_op;
156 shift_imm_ = shift_imm & 31;
157 if (shift_op == RRX) {
158 // encoded as ROR with shift_imm == 0
159 ASSERT(shift_imm == 0);
160 shift_op_ = ROR;
161 shift_imm_ = 0;
162 }
163}
164
165
166Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
167 ASSERT(shift_op != RRX);
168 rm_ = rm;
169 rs_ = no_reg;
170 shift_op_ = shift_op;
171 rs_ = rs;
172}
173
174
175MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
176 rn_ = rn;
177 rm_ = no_reg;
178 offset_ = offset;
179 am_ = am;
180}
181
182MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
183 rn_ = rn;
184 rm_ = rm;
185 shift_op_ = LSL;
186 shift_imm_ = 0;
187 am_ = am;
188}
189
190
191MemOperand::MemOperand(Register rn, Register rm,
192 ShiftOp shift_op, int shift_imm, AddrMode am) {
193 ASSERT(is_uint5(shift_imm));
194 rn_ = rn;
195 rm_ = rm;
196 shift_op_ = shift_op;
197 shift_imm_ = shift_imm & 31;
198 am_ = am;
199}
200
201
202// -----------------------------------------------------------------------------
ager@chromium.org5c838252010-02-19 08:53:10 +0000203// Implementation of Assembler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
ager@chromium.org5c838252010-02-19 08:53:10 +0000205// Instruction encoding bits.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206enum {
207 H = 1 << 5, // halfword (or byte)
208 S6 = 1 << 6, // signed (or unsigned)
209 L = 1 << 20, // load (or store)
210 S = 1 << 20, // set condition code (or leave unchanged)
211 W = 1 << 21, // writeback base register (or leave unchanged)
212 A = 1 << 21, // accumulate in multiply instruction (or not)
213 B = 1 << 22, // unsigned byte (or word)
214 N = 1 << 22, // long (or short)
215 U = 1 << 23, // positive (or negative) offset/index
216 P = 1 << 24, // offset/pre-indexed addressing (or post-indexed addressing)
217 I = 1 << 25, // immediate shifter operand (or not)
218
219 B4 = 1 << 4,
220 B5 = 1 << 5,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000221 B6 = 1 << 6,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 B7 = 1 << 7,
223 B8 = 1 << 8,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000224 B9 = 1 << 9,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 B12 = 1 << 12,
226 B16 = 1 << 16,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000227 B18 = 1 << 18,
228 B19 = 1 << 19,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 B20 = 1 << 20,
230 B21 = 1 << 21,
231 B22 = 1 << 22,
232 B23 = 1 << 23,
233 B24 = 1 << 24,
234 B25 = 1 << 25,
235 B26 = 1 << 26,
236 B27 = 1 << 27,
237
ager@chromium.org5c838252010-02-19 08:53:10 +0000238 // Instruction bit masks.
mads.s.ager31e71382008-08-13 09:32:07 +0000239 RdMask = 15 << 12, // in str instruction
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 CondMask = 15 << 28,
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000241 CoprocessorMask = 15 << 8,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 OpCodeMask = 15 << 21, // in data-processing instructions
243 Imm24Mask = (1 << 24) - 1,
244 Off12Mask = (1 << 12) - 1,
ager@chromium.org5c838252010-02-19 08:53:10 +0000245 // Reserved condition.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 nv = 15 << 28
247};
248
249
mads.s.ager31e71382008-08-13 09:32:07 +0000250// add(sp, sp, 4) instruction (aka Pop())
251static const Instr kPopInstruction =
252 al | 4 * B21 | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
253// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
254// register r is not encoded.
255static const Instr kPushRegPattern =
256 al | B26 | 4 | NegPreIndex | sp.code() * B16;
257// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
258// register r is not encoded.
259static const Instr kPopRegPattern =
260 al | B26 | L | 4 | PostIndex | sp.code() * B16;
ager@chromium.org4af710e2009-09-15 12:20:11 +0000261// mov lr, pc
262const Instr kMovLrPc = al | 13*B21 | pc.code() | lr.code() * B12;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000263// ldr rd, [pc, #offset]
264const Instr kLdrPCMask = CondMask | 15 * B24 | 7 * B20 | 15 * B16;
265const Instr kLdrPCPattern = al | 5 * B24 | L | pc.code() * B16;
266// blxcc rm
267const Instr kBlxRegMask =
268 15 * B24 | 15 * B20 | 15 * B16 | 15 * B12 | 15 * B8 | 15 * B4;
269const Instr kBlxRegPattern =
270 B24 | B21 | 15 * B16 | 15 * B12 | 15 * B8 | 3 * B4;
mads.s.ager31e71382008-08-13 09:32:07 +0000271
ager@chromium.org5c838252010-02-19 08:53:10 +0000272// Spare buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273static const int kMinimalBufferSize = 4*KB;
274static byte* spare_buffer_ = NULL;
275
276Assembler::Assembler(void* buffer, int buffer_size) {
277 if (buffer == NULL) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000278 // Do our own buffer management.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 if (buffer_size <= kMinimalBufferSize) {
280 buffer_size = kMinimalBufferSize;
281
282 if (spare_buffer_ != NULL) {
283 buffer = spare_buffer_;
284 spare_buffer_ = NULL;
285 }
286 }
287 if (buffer == NULL) {
288 buffer_ = NewArray<byte>(buffer_size);
289 } else {
290 buffer_ = static_cast<byte*>(buffer);
291 }
292 buffer_size_ = buffer_size;
293 own_buffer_ = true;
294
295 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 // Use externally provided buffer instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 ASSERT(buffer_size > 0);
298 buffer_ = static_cast<byte*>(buffer);
299 buffer_size_ = buffer_size;
300 own_buffer_ = false;
301 }
302
ager@chromium.org5c838252010-02-19 08:53:10 +0000303 // Setup buffer pointers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 ASSERT(buffer_ != NULL);
305 pc_ = buffer_;
306 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
307 num_prinfo_ = 0;
308 next_buffer_check_ = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000309 const_pool_blocked_nesting_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 no_const_pool_before_ = 0;
311 last_const_pool_end_ = 0;
312 last_bound_pos_ = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000313 current_statement_position_ = RelocInfo::kNoPosition;
314 current_position_ = RelocInfo::kNoPosition;
315 written_statement_position_ = current_statement_position_;
316 written_position_ = current_position_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317}
318
319
320Assembler::~Assembler() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000321 ASSERT(const_pool_blocked_nesting_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 if (own_buffer_) {
323 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
324 spare_buffer_ = buffer_;
325 } else {
326 DeleteArray(buffer_);
327 }
328 }
329}
330
331
332void Assembler::GetCode(CodeDesc* desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000333 // Emit constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 CheckConstPool(true, false);
335 ASSERT(num_prinfo_ == 0);
336
ager@chromium.org5c838252010-02-19 08:53:10 +0000337 // Setup code descriptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 desc->buffer = buffer_;
339 desc->buffer_size = buffer_size_;
340 desc->instr_size = pc_offset();
341 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
342}
343
344
345void Assembler::Align(int m) {
346 ASSERT(m >= 4 && IsPowerOf2(m));
347 while ((pc_offset() & (m - 1)) != 0) {
348 nop();
349 }
350}
351
352
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000353bool Assembler::IsNop(Instr instr, int type) {
354 // Check for mov rx, rx.
355 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
356 return instr == (al | 13*B21 | type*B12 | type);
357}
358
359
360bool Assembler::IsBranch(Instr instr) {
361 return (instr & (B27 | B25)) == (B27 | B25);
362}
363
364
365int Assembler::GetBranchOffset(Instr instr) {
366 ASSERT(IsBranch(instr));
367 // Take the jump offset in the lower 24 bits, sign extend it and multiply it
368 // with 4 to get the offset in bytes.
369 return ((instr & Imm24Mask) << 8) >> 6;
370}
371
372
373bool Assembler::IsLdrRegisterImmediate(Instr instr) {
374 return (instr & (B27 | B26 | B25 | B22 | B20)) == (B26 | B20);
375}
376
377
378int Assembler::GetLdrRegisterImmediateOffset(Instr instr) {
379 ASSERT(IsLdrRegisterImmediate(instr));
380 bool positive = (instr & B23) == B23;
381 int offset = instr & Off12Mask; // Zero extended offset.
382 return positive ? offset : -offset;
383}
384
385
386Instr Assembler::SetLdrRegisterImmediateOffset(Instr instr, int offset) {
387 ASSERT(IsLdrRegisterImmediate(instr));
388 bool positive = offset >= 0;
389 if (!positive) offset = -offset;
390 ASSERT(is_uint12(offset));
391 // Set bit indicating whether the offset should be added.
392 instr = (instr & ~B23) | (positive ? B23 : 0);
393 // Set the actual offset.
394 return (instr & ~Off12Mask) | offset;
395}
396
397
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398// Labels refer to positions in the (to be) generated code.
399// There are bound, linked, and unused labels.
400//
401// Bound labels refer to known positions in the already
402// generated code. pos() is the position the label refers to.
403//
404// Linked labels refer to unknown positions in the code
405// to be generated; pos() is the position of the last
406// instruction using the label.
407
408
409// The link chain is terminated by a negative code position (must be aligned)
410const int kEndOfChain = -4;
411
412
413int Assembler::target_at(int pos) {
414 Instr instr = instr_at(pos);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000415 if ((instr & ~Imm24Mask) == 0) {
416 // Emitted label constant, not part of a branch.
417 return instr - (Code::kHeaderSize - kHeapObjectTag);
418 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
420 int imm26 = ((instr & Imm24Mask) << 8) >> 6;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000421 if ((instr & CondMask) == nv && (instr & B24) != 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 // blx uses bit 24 to encode bit 2 of imm26
423 imm26 += 2;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000424 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000425 return pos + kPcLoadDelta + imm26;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426}
427
428
429void Assembler::target_at_put(int pos, int target_pos) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 Instr instr = instr_at(pos);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000431 if ((instr & ~Imm24Mask) == 0) {
432 ASSERT(target_pos == kEndOfChain || target_pos >= 0);
433 // Emitted label constant, not part of a branch.
434 // Make label relative to Code* of generated Code object.
435 instr_at_put(pos, target_pos + (Code::kHeaderSize - kHeapObjectTag));
436 return;
437 }
438 int imm26 = target_pos - (pos + kPcLoadDelta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
440 if ((instr & CondMask) == nv) {
441 // blx uses bit 24 to encode bit 2 of imm26
442 ASSERT((imm26 & 1) == 0);
443 instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24;
444 } else {
445 ASSERT((imm26 & 3) == 0);
446 instr &= ~Imm24Mask;
447 }
448 int imm24 = imm26 >> 2;
449 ASSERT(is_int24(imm24));
450 instr_at_put(pos, instr | (imm24 & Imm24Mask));
451}
452
453
454void Assembler::print(Label* L) {
455 if (L->is_unused()) {
456 PrintF("unused label\n");
457 } else if (L->is_bound()) {
458 PrintF("bound label to %d\n", L->pos());
459 } else if (L->is_linked()) {
460 Label l = *L;
461 PrintF("unbound label");
462 while (l.is_linked()) {
463 PrintF("@ %d ", l.pos());
464 Instr instr = instr_at(l.pos());
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000465 if ((instr & ~Imm24Mask) == 0) {
466 PrintF("value\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000468 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
469 int cond = instr & CondMask;
470 const char* b;
471 const char* c;
472 if (cond == nv) {
473 b = "blx";
474 c = "";
475 } else {
476 if ((instr & B24) != 0)
477 b = "bl";
478 else
479 b = "b";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000481 switch (cond) {
482 case eq: c = "eq"; break;
483 case ne: c = "ne"; break;
484 case hs: c = "hs"; break;
485 case lo: c = "lo"; break;
486 case mi: c = "mi"; break;
487 case pl: c = "pl"; break;
488 case vs: c = "vs"; break;
489 case vc: c = "vc"; break;
490 case hi: c = "hi"; break;
491 case ls: c = "ls"; break;
492 case ge: c = "ge"; break;
493 case lt: c = "lt"; break;
494 case gt: c = "gt"; break;
495 case le: c = "le"; break;
496 case al: c = ""; break;
497 default:
498 c = "";
499 UNREACHABLE();
500 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000502 PrintF("%s%s\n", b, c);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 next(&l);
505 }
506 } else {
507 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
508 }
509}
510
511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512void Assembler::bind_to(Label* L, int pos) {
513 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
514 while (L->is_linked()) {
515 int fixup_pos = L->pos();
516 next(L); // call next before overwriting link with target at fixup_pos
517 target_at_put(fixup_pos, pos);
518 }
519 L->bind_to(pos);
520
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000521 // Keep track of the last bound label so we don't eliminate any instructions
522 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 if (pos > last_bound_pos_)
524 last_bound_pos_ = pos;
525}
526
527
528void Assembler::link_to(Label* L, Label* appendix) {
529 if (appendix->is_linked()) {
530 if (L->is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000531 // Append appendix to L's list.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 int fixup_pos;
533 int link = L->pos();
534 do {
535 fixup_pos = link;
536 link = target_at(fixup_pos);
537 } while (link > 0);
538 ASSERT(link == kEndOfChain);
539 target_at_put(fixup_pos, appendix->pos());
540 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000541 // L is empty, simply use appendix.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 *L = *appendix;
543 }
544 }
545 appendix->Unuse(); // appendix should not be used anymore
546}
547
548
549void Assembler::bind(Label* L) {
550 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 bind_to(L, pc_offset());
552}
553
554
555void Assembler::next(Label* L) {
556 ASSERT(L->is_linked());
557 int link = target_at(L->pos());
558 if (link > 0) {
559 L->link_to(link);
560 } else {
561 ASSERT(link == kEndOfChain);
562 L->Unuse();
563 }
564}
565
566
ager@chromium.org5c838252010-02-19 08:53:10 +0000567// Low-level code emission routines depending on the addressing mode.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568static bool fits_shifter(uint32_t imm32,
569 uint32_t* rotate_imm,
570 uint32_t* immed_8,
571 Instr* instr) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000572 // imm32 must be unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 for (int rot = 0; rot < 16; rot++) {
574 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
575 if ((imm8 <= 0xff)) {
576 *rotate_imm = rot;
577 *immed_8 = imm8;
578 return true;
579 }
580 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000581 // If the opcode is mov or mvn and if ~imm32 fits, change the opcode.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 if (instr != NULL && (*instr & 0xd*B21) == 0xd*B21) {
583 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
584 *instr ^= 0x2*B21;
585 return true;
586 }
587 }
588 return false;
589}
590
591
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000592// We have to use the temporary register for things that can be relocated even
593// if they can be encoded in the ARM's 12 bits of immediate-offset instruction
594// space. There is no guarantee that the relocated location can be similarly
595// encoded.
596static bool MustUseIp(RelocInfo::Mode rmode) {
597 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000598#ifdef DEBUG
599 if (!Serializer::enabled()) {
600 Serializer::TooLateToEnableNow();
601 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000602#endif // def DEBUG
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000603 return Serializer::enabled();
604 } else if (rmode == RelocInfo::NONE) {
605 return false;
606 }
607 return true;
608}
609
610
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611void Assembler::addrmod1(Instr instr,
612 Register rn,
613 Register rd,
614 const Operand& x) {
615 CheckBuffer();
616 ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0);
617 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000618 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 uint32_t rotate_imm;
620 uint32_t immed_8;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000621 if (MustUseIp(x.rmode_) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
623 // The immediate operand cannot be encoded as a shifter operand, so load
624 // it first to register ip and change the original instruction to use ip.
625 // However, if the original instruction is a 'mov rd, x' (not setting the
ager@chromium.org5c838252010-02-19 08:53:10 +0000626 // condition code), then replace it with a 'ldr rd, [pc]'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 RecordRelocInfo(x.rmode_, x.imm32_);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000628 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 Condition cond = static_cast<Condition>(instr & CondMask);
630 if ((instr & ~CondMask) == 13*B21) { // mov, S not set
631 ldr(rd, MemOperand(pc, 0), cond);
632 } else {
633 ldr(ip, MemOperand(pc, 0), cond);
634 addrmod1(instr, rn, rd, Operand(ip));
635 }
636 return;
637 }
638 instr |= I | rotate_imm*B8 | immed_8;
639 } else if (!x.rs_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 // Immediate shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
642 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000643 // Register shift.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
645 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
646 }
647 emit(instr | rn.code()*B16 | rd.code()*B12);
648 if (rn.is(pc) || x.rm_.is(pc))
ager@chromium.org5c838252010-02-19 08:53:10 +0000649 // Block constant pool emission for one instruction after reading pc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 BlockConstPoolBefore(pc_offset() + kInstrSize);
651}
652
653
654void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
655 ASSERT((instr & ~(CondMask | B | L)) == B26);
656 int am = x.am_;
657 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000658 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 int offset_12 = x.offset_;
660 if (offset_12 < 0) {
661 offset_12 = -offset_12;
662 am ^= U;
663 }
664 if (!is_uint12(offset_12)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000665 // Immediate offset cannot be encoded, load it first to register ip
666 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
668 mov(ip, Operand(x.offset_), LeaveCC,
669 static_cast<Condition>(instr & CondMask));
670 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
671 return;
672 }
673 ASSERT(offset_12 >= 0); // no masking needed
674 instr |= offset_12;
675 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000676 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 // register offset the constructors make sure than both shift_imm_
ager@chromium.org5c838252010-02-19 08:53:10 +0000678 // and shift_op_ are initialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 ASSERT(!x.rm_.is(pc));
680 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
681 }
682 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
683 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
684}
685
686
687void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
688 ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7));
689 ASSERT(x.rn_.is_valid());
690 int am = x.am_;
691 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000692 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 int offset_8 = x.offset_;
694 if (offset_8 < 0) {
695 offset_8 = -offset_8;
696 am ^= U;
697 }
698 if (!is_uint8(offset_8)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000699 // Immediate offset cannot be encoded, load it first to register ip
700 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
702 mov(ip, Operand(x.offset_), LeaveCC,
703 static_cast<Condition>(instr & CondMask));
704 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
705 return;
706 }
707 ASSERT(offset_8 >= 0); // no masking needed
708 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
709 } else if (x.shift_imm_ != 0) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000710 // Scaled register offset not supported, load index first
711 // rn (and rd in a load) should never be ip, or will be trashed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
713 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
714 static_cast<Condition>(instr & CondMask));
715 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
716 return;
717 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000718 // Register offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
720 instr |= x.rm_.code();
721 }
722 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
723 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
724}
725
726
727void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
728 ASSERT((instr & ~(CondMask | P | U | W | L)) == B27);
729 ASSERT(rl != 0);
730 ASSERT(!rn.is(pc));
731 emit(instr | rn.code()*B16 | rl);
732}
733
734
735void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000736 // Unindexed addressing is not encoded by this function.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000737 ASSERT_EQ((B27 | B26),
738 (instr & ~(CondMask | CoprocessorMask | P | U | N | W | L)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
740 int am = x.am_;
741 int offset_8 = x.offset_;
742 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
743 offset_8 >>= 2;
744 if (offset_8 < 0) {
745 offset_8 = -offset_8;
746 am ^= U;
747 }
748 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
749 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
750
ager@chromium.org5c838252010-02-19 08:53:10 +0000751 // Post-indexed addressing requires W == 1; different than in addrmod2/3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 if ((am & P) == 0)
753 am |= W;
754
755 ASSERT(offset_8 >= 0); // no masking needed
756 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
757}
758
759
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000760int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 int target_pos;
762 if (L->is_bound()) {
763 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000765 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000767 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000769 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 L->link_to(pc_offset());
771 }
772
773 // Block the emission of the constant pool, since the branch instruction must
ager@chromium.org5c838252010-02-19 08:53:10 +0000774 // be emitted at the pc offset recorded by the label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 BlockConstPoolBefore(pc_offset() + kInstrSize);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000776 return target_pos - (pc_offset() + kPcLoadDelta);
777}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000779
780void Assembler::label_at_put(Label* L, int at_offset) {
781 int target_pos;
782 if (L->is_bound()) {
783 target_pos = L->pos();
784 } else {
785 if (L->is_linked()) {
786 target_pos = L->pos(); // L's link
787 } else {
788 target_pos = kEndOfChain;
789 }
790 L->link_to(at_offset);
791 instr_at_put(at_offset, target_pos + (Code::kHeaderSize - kHeapObjectTag));
792 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793}
794
795
ager@chromium.org5c838252010-02-19 08:53:10 +0000796// Branch instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797void Assembler::b(int branch_offset, Condition cond) {
798 ASSERT((branch_offset & 3) == 0);
799 int imm24 = branch_offset >> 2;
800 ASSERT(is_int24(imm24));
801 emit(cond | B27 | B25 | (imm24 & Imm24Mask));
802
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000803 if (cond == al) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000804 // Dead code is a good location to emit the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805 CheckConstPool(false, false);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000806 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807}
808
809
810void Assembler::bl(int branch_offset, Condition cond) {
811 ASSERT((branch_offset & 3) == 0);
812 int imm24 = branch_offset >> 2;
813 ASSERT(is_int24(imm24));
814 emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask));
815}
816
817
818void Assembler::blx(int branch_offset) { // v5 and above
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000819 WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820 ASSERT((branch_offset & 1) == 0);
821 int h = ((branch_offset & 2) >> 1)*B24;
822 int imm24 = branch_offset >> 2;
823 ASSERT(is_int24(imm24));
824 emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask));
825}
826
827
828void Assembler::blx(Register target, Condition cond) { // v5 and above
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000829 WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 ASSERT(!target.is(pc));
831 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code());
832}
833
834
835void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000836 WriteRecordedPositions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
838 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code());
839}
840
841
ager@chromium.org5c838252010-02-19 08:53:10 +0000842// Data-processing instructions.
843
844// UBFX <Rd>,<Rn>,#<lsb>,#<width - 1>
845// Instruction details available in ARM DDI 0406A, A8-464.
846// cond(31-28) | 01111(27-23)| 1(22) | 1(21) | widthm1(20-16) |
847// Rd(15-12) | lsb(11-7) | 101(6-4) | Rn(3-0)
848void Assembler::ubfx(Register dst, Register src1, const Operand& src2,
849 const Operand& src3, Condition cond) {
850 ASSERT(!src2.rm_.is_valid() && !src3.rm_.is_valid());
851 ASSERT(static_cast<uint32_t>(src2.imm32_) <= 0x1f);
852 ASSERT(static_cast<uint32_t>(src3.imm32_) <= 0x1f);
853 emit(cond | 0x3F*B21 | src3.imm32_*B16 |
854 dst.code()*B12 | src2.imm32_*B7 | 0x5*B4 | src1.code());
855}
856
857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858void Assembler::and_(Register dst, Register src1, const Operand& src2,
859 SBit s, Condition cond) {
860 addrmod1(cond | 0*B21 | s, src1, dst, src2);
861}
862
863
864void Assembler::eor(Register dst, Register src1, const Operand& src2,
865 SBit s, Condition cond) {
866 addrmod1(cond | 1*B21 | s, src1, dst, src2);
867}
868
869
870void Assembler::sub(Register dst, Register src1, const Operand& src2,
871 SBit s, Condition cond) {
872 addrmod1(cond | 2*B21 | s, src1, dst, src2);
873}
874
875
876void Assembler::rsb(Register dst, Register src1, const Operand& src2,
877 SBit s, Condition cond) {
878 addrmod1(cond | 3*B21 | s, src1, dst, src2);
879}
880
881
882void Assembler::add(Register dst, Register src1, const Operand& src2,
883 SBit s, Condition cond) {
884 addrmod1(cond | 4*B21 | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +0000885
886 // Eliminate pattern: push(r), pop()
887 // str(src, MemOperand(sp, 4, NegPreIndex), al);
888 // add(sp, sp, Operand(kPointerSize));
889 // Both instructions can be eliminated.
890 int pattern_size = 2 * kInstrSize;
891 if (FLAG_push_pop_elimination &&
892 last_bound_pos_ <= (pc_offset() - pattern_size) &&
893 reloc_info_writer.last_pc() <= (pc_ - pattern_size) &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000894 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +0000895 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
896 (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) {
897 pc_ -= 2 * kInstrSize;
898 if (FLAG_print_push_pop_elimination) {
899 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
900 }
901 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902}
903
904
905void Assembler::adc(Register dst, Register src1, const Operand& src2,
906 SBit s, Condition cond) {
907 addrmod1(cond | 5*B21 | s, src1, dst, src2);
908}
909
910
911void Assembler::sbc(Register dst, Register src1, const Operand& src2,
912 SBit s, Condition cond) {
913 addrmod1(cond | 6*B21 | s, src1, dst, src2);
914}
915
916
917void Assembler::rsc(Register dst, Register src1, const Operand& src2,
918 SBit s, Condition cond) {
919 addrmod1(cond | 7*B21 | s, src1, dst, src2);
920}
921
922
923void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
924 addrmod1(cond | 8*B21 | S, src1, r0, src2);
925}
926
927
928void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
929 addrmod1(cond | 9*B21 | S, src1, r0, src2);
930}
931
932
933void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
934 addrmod1(cond | 10*B21 | S, src1, r0, src2);
935}
936
937
938void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
939 addrmod1(cond | 11*B21 | S, src1, r0, src2);
940}
941
942
943void Assembler::orr(Register dst, Register src1, const Operand& src2,
944 SBit s, Condition cond) {
945 addrmod1(cond | 12*B21 | s, src1, dst, src2);
946}
947
948
949void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000950 if (dst.is(pc)) {
951 WriteRecordedPositions();
952 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000953 // Don't allow nop instructions in the form mov rn, rn to be generated using
954 // the mov instruction. They must be generated using nop(int)
955 // pseudo instructions.
956 ASSERT(!(src.is_reg() && src.rm().is(dst) && s == LeaveCC && cond == al));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 addrmod1(cond | 13*B21 | s, r0, dst, src);
958}
959
960
961void Assembler::bic(Register dst, Register src1, const Operand& src2,
962 SBit s, Condition cond) {
963 addrmod1(cond | 14*B21 | s, src1, dst, src2);
964}
965
966
967void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
968 addrmod1(cond | 15*B21 | s, r0, dst, src);
969}
970
971
ager@chromium.org5c838252010-02-19 08:53:10 +0000972// Multiply instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
974 SBit s, Condition cond) {
975 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
977 src2.code()*B8 | B7 | B4 | src1.code());
978}
979
980
981void Assembler::mul(Register dst, Register src1, Register src2,
982 SBit s, Condition cond) {
983 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000984 // dst goes in bits 16-19 for this instruction!
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
986}
987
988
989void Assembler::smlal(Register dstL,
990 Register dstH,
991 Register src1,
992 Register src2,
993 SBit s,
994 Condition cond) {
995 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000996 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
998 src2.code()*B8 | B7 | B4 | src1.code());
999}
1000
1001
1002void Assembler::smull(Register dstL,
1003 Register dstH,
1004 Register src1,
1005 Register src2,
1006 SBit s,
1007 Condition cond) {
1008 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001009 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
1011 src2.code()*B8 | B7 | B4 | src1.code());
1012}
1013
1014
1015void Assembler::umlal(Register dstL,
1016 Register dstH,
1017 Register src1,
1018 Register src2,
1019 SBit s,
1020 Condition cond) {
1021 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001022 ASSERT(!dstL.is(dstH));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
1024 src2.code()*B8 | B7 | B4 | src1.code());
1025}
1026
1027
1028void Assembler::umull(Register dstL,
1029 Register dstH,
1030 Register src1,
1031 Register src2,
1032 SBit s,
1033 Condition cond) {
1034 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001035 ASSERT(!dstL.is(dstH));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001036 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 src2.code()*B8 | B7 | B4 | src1.code());
1038}
1039
1040
ager@chromium.org5c838252010-02-19 08:53:10 +00001041// Miscellaneous arithmetic instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042void Assembler::clz(Register dst, Register src, Condition cond) {
1043 // v5 and above.
1044 ASSERT(!dst.is(pc) && !src.is(pc));
1045 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
1046 15*B8 | B4 | src.code());
1047}
1048
1049
ager@chromium.org5c838252010-02-19 08:53:10 +00001050// Status register access instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051void Assembler::mrs(Register dst, SRegister s, Condition cond) {
1052 ASSERT(!dst.is(pc));
1053 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
1054}
1055
1056
1057void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
1058 Condition cond) {
1059 ASSERT(fields >= B16 && fields < B20); // at least one field set
1060 Instr instr;
1061 if (!src.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001062 // Immediate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 uint32_t rotate_imm;
1064 uint32_t immed_8;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001065 if (MustUseIp(src.rmode_) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001067 // Immediate operand cannot be encoded, load it first to register ip.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 RecordRelocInfo(src.rmode_, src.imm32_);
1069 ldr(ip, MemOperand(pc, 0), cond);
1070 msr(fields, Operand(ip), cond);
1071 return;
1072 }
1073 instr = I | rotate_imm*B8 | immed_8;
1074 } else {
1075 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
1076 instr = src.rm_.code();
1077 }
1078 emit(cond | instr | B24 | B21 | fields | 15*B12);
1079}
1080
1081
ager@chromium.org5c838252010-02-19 08:53:10 +00001082// Load/Store instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001084 if (dst.is(pc)) {
1085 WriteRecordedPositions();
1086 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +00001088
1089 // Eliminate pattern: push(r), pop(r)
1090 // str(r, MemOperand(sp, 4, NegPreIndex), al)
1091 // ldr(r, MemOperand(sp, 4, PostIndex), al)
1092 // Both instructions can be eliminated.
1093 int pattern_size = 2 * kInstrSize;
1094 if (FLAG_push_pop_elimination &&
1095 last_bound_pos_ <= (pc_offset() - pattern_size) &&
1096 reloc_info_writer.last_pc() <= (pc_ - pattern_size) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001097 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001098 instr_at(pc_ - 1 * kInstrSize) == (kPopRegPattern | dst.code() * B12) &&
1099 instr_at(pc_ - 2 * kInstrSize) == (kPushRegPattern | dst.code() * B12)) {
1100 pc_ -= 2 * kInstrSize;
1101 if (FLAG_print_push_pop_elimination) {
1102 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
1103 }
1104 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105}
1106
1107
1108void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
1109 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +00001110
1111 // Eliminate pattern: pop(), push(r)
1112 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
1113 // -> str r, [sp, 0], al
1114 int pattern_size = 2 * kInstrSize;
1115 if (FLAG_push_pop_elimination &&
1116 last_bound_pos_ <= (pc_offset() - pattern_size) &&
1117 reloc_info_writer.last_pc() <= (pc_ - pattern_size) &&
ager@chromium.org5c838252010-02-19 08:53:10 +00001118 // Pattern.
mads.s.ager31e71382008-08-13 09:32:07 +00001119 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1120 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1121 pc_ -= 2 * kInstrSize;
1122 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
1123 if (FLAG_print_push_pop_elimination) {
1124 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1125 }
1126 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127}
1128
1129
1130void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1131 addrmod2(cond | B26 | B | L, dst, src);
1132}
1133
1134
1135void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1136 addrmod2(cond | B26 | B, src, dst);
1137}
1138
1139
1140void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1141 addrmod3(cond | L | B7 | H | B4, dst, src);
1142}
1143
1144
1145void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1146 addrmod3(cond | B7 | H | B4, src, dst);
1147}
1148
1149
1150void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1151 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1152}
1153
1154
1155void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1156 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1157}
1158
1159
ager@chromium.org5c838252010-02-19 08:53:10 +00001160// Load/Store multiple instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161void Assembler::ldm(BlockAddrMode am,
1162 Register base,
1163 RegList dst,
1164 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001165 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1167
1168 addrmod4(cond | B27 | am | L, base, dst);
1169
ager@chromium.org5c838252010-02-19 08:53:10 +00001170 // Emit the constant pool after a function return implemented by ldm ..{..pc}.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 if (cond == al && (dst & pc.bit()) != 0) {
1172 // There is a slight chance that the ldm instruction was actually a call,
1173 // in which case it would be wrong to return into the constant pool; we
1174 // recognize this case by checking if the emission of the pool was blocked
1175 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1176 // the case, we emit a jump over the pool.
1177 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1178 }
1179}
1180
1181
1182void Assembler::stm(BlockAddrMode am,
1183 Register base,
1184 RegList src,
1185 Condition cond) {
1186 addrmod4(cond | B27 | am, base, src);
1187}
1188
1189
ager@chromium.org5c838252010-02-19 08:53:10 +00001190// Semaphore instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191void Assembler::swp(Register dst, Register src, Register base, Condition cond) {
1192 ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc));
1193 ASSERT(!dst.is(base) && !src.is(base));
1194 emit(cond | P | base.code()*B16 | dst.code()*B12 |
1195 B7 | B4 | src.code());
1196}
1197
1198
1199void Assembler::swpb(Register dst,
1200 Register src,
1201 Register base,
1202 Condition cond) {
1203 ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc));
1204 ASSERT(!dst.is(base) && !src.is(base));
1205 emit(cond | P | B | base.code()*B16 | dst.code()*B12 |
1206 B7 | B4 | src.code());
1207}
1208
1209
ager@chromium.org5c838252010-02-19 08:53:10 +00001210// Exception-generating instructions and debugging support.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211void Assembler::stop(const char* msg) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001212#ifndef __arm__
kasper.lund7276f142008-07-30 08:49:36 +00001213 // The simulator handles these special instructions and stops execution.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 emit(15 << 28 | ((intptr_t) msg));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001215#else // def __arm__
1216#ifdef CAN_USE_ARMV5_INSTRUCTIONS
kasper.lund7276f142008-07-30 08:49:36 +00001217 bkpt(0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001218#else // ndef CAN_USE_ARMV5_INSTRUCTIONS
1219 swi(0x9f0001);
1220#endif // ndef CAN_USE_ARMV5_INSTRUCTIONS
1221#endif // def __arm__
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222}
1223
1224
1225void Assembler::bkpt(uint32_t imm16) { // v5 and above
1226 ASSERT(is_uint16(imm16));
1227 emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf));
1228}
1229
1230
1231void Assembler::swi(uint32_t imm24, Condition cond) {
1232 ASSERT(is_uint24(imm24));
1233 emit(cond | 15*B24 | imm24);
1234}
1235
1236
ager@chromium.org5c838252010-02-19 08:53:10 +00001237// Coprocessor instructions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238void Assembler::cdp(Coprocessor coproc,
1239 int opcode_1,
1240 CRegister crd,
1241 CRegister crn,
1242 CRegister crm,
1243 int opcode_2,
1244 Condition cond) {
1245 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1246 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1247 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1248}
1249
1250
1251void Assembler::cdp2(Coprocessor coproc,
1252 int opcode_1,
1253 CRegister crd,
1254 CRegister crn,
1255 CRegister crm,
1256 int opcode_2) { // v5 and above
1257 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv));
1258}
1259
1260
1261void Assembler::mcr(Coprocessor coproc,
1262 int opcode_1,
1263 Register rd,
1264 CRegister crn,
1265 CRegister crm,
1266 int opcode_2,
1267 Condition cond) {
1268 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1269 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1270 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1271}
1272
1273
1274void Assembler::mcr2(Coprocessor coproc,
1275 int opcode_1,
1276 Register rd,
1277 CRegister crn,
1278 CRegister crm,
1279 int opcode_2) { // v5 and above
1280 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1281}
1282
1283
1284void Assembler::mrc(Coprocessor coproc,
1285 int opcode_1,
1286 Register rd,
1287 CRegister crn,
1288 CRegister crm,
1289 int opcode_2,
1290 Condition cond) {
1291 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1292 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1293 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1294}
1295
1296
1297void Assembler::mrc2(Coprocessor coproc,
1298 int opcode_1,
1299 Register rd,
1300 CRegister crn,
1301 CRegister crm,
1302 int opcode_2) { // v5 and above
1303 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1304}
1305
1306
1307void Assembler::ldc(Coprocessor coproc,
1308 CRegister crd,
1309 const MemOperand& src,
1310 LFlag l,
1311 Condition cond) {
1312 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1313}
1314
1315
1316void Assembler::ldc(Coprocessor coproc,
1317 CRegister crd,
1318 Register rn,
1319 int option,
1320 LFlag l,
1321 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001322 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 ASSERT(is_uint8(option));
1324 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1325 coproc*B8 | (option & 255));
1326}
1327
1328
1329void Assembler::ldc2(Coprocessor coproc,
1330 CRegister crd,
1331 const MemOperand& src,
1332 LFlag l) { // v5 and above
1333 ldc(coproc, crd, src, l, static_cast<Condition>(nv));
1334}
1335
1336
1337void Assembler::ldc2(Coprocessor coproc,
1338 CRegister crd,
1339 Register rn,
1340 int option,
1341 LFlag l) { // v5 and above
1342 ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1343}
1344
1345
1346void Assembler::stc(Coprocessor coproc,
1347 CRegister crd,
1348 const MemOperand& dst,
1349 LFlag l,
1350 Condition cond) {
1351 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1352}
1353
1354
1355void Assembler::stc(Coprocessor coproc,
1356 CRegister crd,
1357 Register rn,
1358 int option,
1359 LFlag l,
1360 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001361 // Unindexed addressing.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 ASSERT(is_uint8(option));
1363 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1364 coproc*B8 | (option & 255));
1365}
1366
1367
1368void Assembler::stc2(Coprocessor
1369 coproc, CRegister crd,
1370 const MemOperand& dst,
1371 LFlag l) { // v5 and above
1372 stc(coproc, crd, dst, l, static_cast<Condition>(nv));
1373}
1374
1375
1376void Assembler::stc2(Coprocessor coproc,
1377 CRegister crd,
1378 Register rn,
1379 int option,
1380 LFlag l) { // v5 and above
1381 stc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1382}
1383
1384
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001385// Support for VFP.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386void Assembler::vldr(const DwVfpRegister dst,
1387 const Register base,
1388 int offset,
1389 const Condition cond) {
1390 // Ddst = MEM(Rbase + offset).
1391 // Instruction details available in ARM DDI 0406A, A8-628.
1392 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1393 // Vdst(15-12) | 1011(11-8) | offset
1394 ASSERT(CpuFeatures::IsEnabled(VFP3));
1395 ASSERT(offset % 4 == 0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001396 ASSERT((offset / 4) < 256);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001397 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1398 0xB*B8 | ((offset / 4) & 255));
1399}
1400
1401
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001402void Assembler::vldr(const SwVfpRegister dst,
1403 const Register base,
1404 int offset,
1405 const Condition cond) {
1406 // Sdst = MEM(Rbase + offset).
1407 // Instruction details available in ARM DDI 0406A, A8-628.
1408 // cond(31-28) | 1101(27-24)| 1001(23-20) | Rbase(19-16) |
1409 // Vdst(15-12) | 1010(11-8) | offset
1410 ASSERT(CpuFeatures::IsEnabled(VFP3));
1411 ASSERT(offset % 4 == 0);
1412 ASSERT((offset / 4) < 256);
1413 emit(cond | 0xD9*B20 | base.code()*B16 | dst.code()*B12 |
1414 0xA*B8 | ((offset / 4) & 255));
1415}
1416
1417
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001418void Assembler::vstr(const DwVfpRegister src,
1419 const Register base,
1420 int offset,
1421 const Condition cond) {
1422 // MEM(Rbase + offset) = Dsrc.
1423 // Instruction details available in ARM DDI 0406A, A8-786.
1424 // cond(31-28) | 1101(27-24)| 1000(23-20) | | Rbase(19-16) |
1425 // Vsrc(15-12) | 1011(11-8) | (offset/4)
1426 ASSERT(CpuFeatures::IsEnabled(VFP3));
1427 ASSERT(offset % 4 == 0);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001428 ASSERT((offset / 4) < 256);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001429 emit(cond | 0xD8*B20 | base.code()*B16 | src.code()*B12 |
1430 0xB*B8 | ((offset / 4) & 255));
1431}
1432
1433
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001434void Assembler::vmov(const DwVfpRegister dst,
1435 const Register src1,
1436 const Register src2,
1437 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001438 // Dm = <Rt,Rt2>.
1439 // Instruction details available in ARM DDI 0406A, A8-646.
1440 // cond(31-28) | 1100(27-24)| 010(23-21) | op=0(20) | Rt2(19-16) |
1441 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1442 ASSERT(CpuFeatures::IsEnabled(VFP3));
1443 ASSERT(!src1.is(pc) && !src2.is(pc));
1444 emit(cond | 0xC*B24 | B22 | src2.code()*B16 |
1445 src1.code()*B12 | 0xB*B8 | B4 | dst.code());
1446}
1447
1448
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001449void Assembler::vmov(const Register dst1,
1450 const Register dst2,
1451 const DwVfpRegister src,
1452 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001453 // <Rt,Rt2> = Dm.
1454 // Instruction details available in ARM DDI 0406A, A8-646.
1455 // cond(31-28) | 1100(27-24)| 010(23-21) | op=1(20) | Rt2(19-16) |
1456 // Rt(15-12) | 1011(11-8) | 00(7-6) | M(5) | 1(4) | Vm
1457 ASSERT(CpuFeatures::IsEnabled(VFP3));
1458 ASSERT(!dst1.is(pc) && !dst2.is(pc));
1459 emit(cond | 0xC*B24 | B22 | B20 | dst2.code()*B16 |
1460 dst1.code()*B12 | 0xB*B8 | B4 | src.code());
1461}
1462
1463
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001464void Assembler::vmov(const SwVfpRegister dst,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001465 const Register src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001466 const Condition cond) {
1467 // Sn = Rt.
1468 // Instruction details available in ARM DDI 0406A, A8-642.
1469 // cond(31-28) | 1110(27-24)| 000(23-21) | op=0(20) | Vn(19-16) |
1470 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1471 ASSERT(CpuFeatures::IsEnabled(VFP3));
1472 ASSERT(!src.is(pc));
1473 emit(cond | 0xE*B24 | (dst.code() >> 1)*B16 |
1474 src.code()*B12 | 0xA*B8 | (0x1 & dst.code())*B7 | B4);
1475}
1476
1477
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001478void Assembler::vmov(const Register dst,
1479 const SwVfpRegister src,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001480 const Condition cond) {
1481 // Rt = Sn.
1482 // Instruction details available in ARM DDI 0406A, A8-642.
1483 // cond(31-28) | 1110(27-24)| 000(23-21) | op=1(20) | Vn(19-16) |
1484 // Rt(15-12) | 1010(11-8) | N(7)=0 | 00(6-5) | 1(4) | 0000(3-0)
1485 ASSERT(CpuFeatures::IsEnabled(VFP3));
1486 ASSERT(!dst.is(pc));
1487 emit(cond | 0xE*B24 | B20 | (src.code() >> 1)*B16 |
1488 dst.code()*B12 | 0xA*B8 | (0x1 & src.code())*B7 | B4);
1489}
1490
1491
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001492// Type of data to read from or write to VFP register.
1493// Used as specifier in generic vcvt instruction.
1494enum VFPType { S32, U32, F32, F64 };
1495
1496
1497static bool IsSignedVFPType(VFPType type) {
1498 switch (type) {
1499 case S32:
1500 return true;
1501 case U32:
1502 return false;
1503 default:
1504 UNREACHABLE();
1505 return false;
1506 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001507}
1508
1509
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001510static bool IsIntegerVFPType(VFPType type) {
1511 switch (type) {
1512 case S32:
1513 case U32:
1514 return true;
1515 case F32:
1516 case F64:
1517 return false;
1518 default:
1519 UNREACHABLE();
1520 return false;
1521 }
1522}
1523
1524
1525static bool IsDoubleVFPType(VFPType type) {
1526 switch (type) {
1527 case F32:
1528 return false;
1529 case F64:
1530 return true;
1531 default:
1532 UNREACHABLE();
1533 return false;
1534 }
1535}
1536
1537
1538// Depending on split_last_bit split binary representation of reg_code into Vm:M
1539// or M:Vm form (where M is single bit).
1540static void SplitRegCode(bool split_last_bit,
1541 int reg_code,
1542 int* vm,
1543 int* m) {
1544 if (split_last_bit) {
1545 *m = reg_code & 0x1;
1546 *vm = reg_code >> 1;
1547 } else {
1548 *m = (reg_code & 0x10) >> 4;
1549 *vm = reg_code & 0x0F;
1550 }
1551}
1552
1553
1554// Encode vcvt.src_type.dst_type instruction.
1555static Instr EncodeVCVT(const VFPType dst_type,
1556 const int dst_code,
1557 const VFPType src_type,
1558 const int src_code,
1559 const Condition cond) {
1560 if (IsIntegerVFPType(dst_type) || IsIntegerVFPType(src_type)) {
1561 // Conversion between IEEE floating point and 32-bit integer.
1562 // Instruction details available in ARM DDI 0406B, A8.6.295.
1563 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 1(19) | opc2(18-16) |
1564 // Vd(15-12) | 101(11-9) | sz(8) | op(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
1565 ASSERT(!IsIntegerVFPType(dst_type) || !IsIntegerVFPType(src_type));
1566
1567 int sz, opc2, D, Vd, M, Vm, op;
1568
1569 if (IsIntegerVFPType(dst_type)) {
1570 opc2 = IsSignedVFPType(dst_type) ? 0x5 : 0x4;
1571 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
1572 op = 1; // round towards zero
1573 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
1574 SplitRegCode(true, dst_code, &Vd, &D);
1575 } else {
1576 ASSERT(IsIntegerVFPType(src_type));
1577
1578 opc2 = 0x0;
1579 sz = IsDoubleVFPType(dst_type) ? 0x1 : 0x0;
1580 op = IsSignedVFPType(src_type) ? 0x1 : 0x0;
1581 SplitRegCode(true, src_code, &Vm, &M);
1582 SplitRegCode(!IsDoubleVFPType(dst_type), dst_code, &Vd, &D);
1583 }
1584
1585 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | B19 | opc2*B16 |
1586 Vd*B12 | 0x5*B9 | sz*B8 | op*B7 | B6 | M*B5 | Vm);
1587 } else {
1588 // Conversion between IEEE double and single precision.
1589 // Instruction details available in ARM DDI 0406B, A8.6.298.
1590 // cond(31-28) | 11101(27-23)| D(22) | 11(21-20) | 0111(19-16) |
1591 // Vd(15-12) | 101(11-9) | sz(8) | 1(7) | 1(6) | M(5) | 0(4) | Vm(3-0)
1592 int sz, D, Vd, M, Vm;
1593
1594 ASSERT(IsDoubleVFPType(dst_type) != IsDoubleVFPType(src_type));
1595 sz = IsDoubleVFPType(src_type) ? 0x1 : 0x0;
1596 SplitRegCode(IsDoubleVFPType(src_type), dst_code, &Vd, &D);
1597 SplitRegCode(!IsDoubleVFPType(src_type), src_code, &Vm, &M);
1598
1599 return (cond | 0xE*B24 | B23 | D*B22 | 0x3*B20 | 0x7*B16 |
1600 Vd*B12 | 0x5*B9 | sz*B8 | B7 | B6 | M*B5 | Vm);
1601 }
1602}
1603
1604
1605void Assembler::vcvt_f64_s32(const DwVfpRegister dst,
1606 const SwVfpRegister src,
1607 const Condition cond) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001608 ASSERT(CpuFeatures::IsEnabled(VFP3));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001609 emit(EncodeVCVT(F64, dst.code(), S32, src.code(), cond));
1610}
1611
1612
1613void Assembler::vcvt_f32_s32(const SwVfpRegister dst,
1614 const SwVfpRegister src,
1615 const Condition cond) {
1616 ASSERT(CpuFeatures::IsEnabled(VFP3));
1617 emit(EncodeVCVT(F32, dst.code(), S32, src.code(), cond));
1618}
1619
1620
1621void Assembler::vcvt_f64_u32(const DwVfpRegister dst,
1622 const SwVfpRegister src,
1623 const Condition cond) {
1624 ASSERT(CpuFeatures::IsEnabled(VFP3));
1625 emit(EncodeVCVT(F64, dst.code(), U32, src.code(), cond));
1626}
1627
1628
1629void Assembler::vcvt_s32_f64(const SwVfpRegister dst,
1630 const DwVfpRegister src,
1631 const Condition cond) {
1632 ASSERT(CpuFeatures::IsEnabled(VFP3));
1633 emit(EncodeVCVT(S32, dst.code(), F64, src.code(), cond));
1634}
1635
1636
1637void Assembler::vcvt_u32_f64(const SwVfpRegister dst,
1638 const DwVfpRegister src,
1639 const Condition cond) {
1640 ASSERT(CpuFeatures::IsEnabled(VFP3));
1641 emit(EncodeVCVT(U32, dst.code(), F64, src.code(), cond));
1642}
1643
1644
1645void Assembler::vcvt_f64_f32(const DwVfpRegister dst,
1646 const SwVfpRegister src,
1647 const Condition cond) {
1648 ASSERT(CpuFeatures::IsEnabled(VFP3));
1649 emit(EncodeVCVT(F64, dst.code(), F32, src.code(), cond));
1650}
1651
1652
1653void Assembler::vcvt_f32_f64(const SwVfpRegister dst,
1654 const DwVfpRegister src,
1655 const Condition cond) {
1656 ASSERT(CpuFeatures::IsEnabled(VFP3));
1657 emit(EncodeVCVT(F32, dst.code(), F64, src.code(), cond));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001658}
1659
1660
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001661void Assembler::vadd(const DwVfpRegister dst,
1662 const DwVfpRegister src1,
1663 const DwVfpRegister src2,
1664 const Condition cond) {
1665 // Dd = vadd(Dn, Dm) double precision floating point addition.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001666 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1667 // Instruction details available in ARM DDI 0406A, A8-536.
1668 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
1669 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1670 ASSERT(CpuFeatures::IsEnabled(VFP3));
1671 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
1672 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1673}
1674
1675
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001676void Assembler::vsub(const DwVfpRegister dst,
1677 const DwVfpRegister src1,
1678 const DwVfpRegister src2,
1679 const Condition cond) {
1680 // Dd = vsub(Dn, Dm) double precision floating point subtraction.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001681 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1682 // Instruction details available in ARM DDI 0406A, A8-784.
1683 // cond(31-28) | 11100(27-23)| D=?(22) | 11(21-20) | Vn(19-16) |
1684 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 1(6) | M=?(5) | 0(4) | Vm(3-0)
1685 ASSERT(CpuFeatures::IsEnabled(VFP3));
1686 emit(cond | 0xE*B24 | 0x3*B20 | src1.code()*B16 |
1687 dst.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
1688}
1689
1690
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001691void Assembler::vmul(const DwVfpRegister dst,
1692 const DwVfpRegister src1,
1693 const DwVfpRegister src2,
1694 const Condition cond) {
1695 // Dd = vmul(Dn, Dm) double precision floating point multiplication.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001696 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1697 // Instruction details available in ARM DDI 0406A, A8-784.
1698 // cond(31-28) | 11100(27-23)| D=?(22) | 10(21-20) | Vn(19-16) |
1699 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=0 | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1700 ASSERT(CpuFeatures::IsEnabled(VFP3));
1701 emit(cond | 0xE*B24 | 0x2*B20 | src1.code()*B16 |
1702 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1703}
1704
1705
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001706void Assembler::vdiv(const DwVfpRegister dst,
1707 const DwVfpRegister src1,
1708 const DwVfpRegister src2,
1709 const Condition cond) {
1710 // Dd = vdiv(Dn, Dm) double precision floating point division.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001711 // Dd = D:Vd; Dm=M:Vm; Dn=N:Vm.
1712 // Instruction details available in ARM DDI 0406A, A8-584.
1713 // cond(31-28) | 11101(27-23)| D=?(22) | 00(21-20) | Vn(19-16) |
1714 // Vd(15-12) | 101(11-9) | sz(8)=1 | N(7)=? | 0(6) | M=?(5) | 0(4) | Vm(3-0)
1715 ASSERT(CpuFeatures::IsEnabled(VFP3));
1716 emit(cond | 0xE*B24 | B23 | src1.code()*B16 |
1717 dst.code()*B12 | 0x5*B9 | B8 | src2.code());
1718}
1719
1720
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001721void Assembler::vcmp(const DwVfpRegister src1,
1722 const DwVfpRegister src2,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001723 const SBit s,
1724 const Condition cond) {
1725 // vcmp(Dd, Dm) double precision floating point comparison.
1726 // Instruction details available in ARM DDI 0406A, A8-570.
1727 // cond(31-28) | 11101 (27-23)| D=?(22) | 11 (21-20) | 0100 (19-16) |
1728 // Vd(15-12) | 101(11-9) | sz(8)=1 | E(7)=? | 1(6) | M(5)=? | 0(4) | Vm(3-0)
1729 ASSERT(CpuFeatures::IsEnabled(VFP3));
1730 emit(cond | 0xE*B24 |B23 | 0x3*B20 | B18 |
1731 src1.code()*B12 | 0x5*B9 | B8 | B6 | src2.code());
1732}
1733
1734
1735void Assembler::vmrs(Register dst, Condition cond) {
1736 // Instruction details available in ARM DDI 0406A, A8-652.
1737 // cond(31-28) | 1110 (27-24) | 1111(23-20)| 0001 (19-16) |
1738 // Rt(15-12) | 1010 (11-8) | 0(7) | 00 (6-5) | 1(4) | 0000(3-0)
1739 ASSERT(CpuFeatures::IsEnabled(VFP3));
1740 emit(cond | 0xE*B24 | 0xF*B20 | B16 |
1741 dst.code()*B12 | 0xA*B8 | B4);
1742}
1743
1744
ager@chromium.org5c838252010-02-19 08:53:10 +00001745// Pseudo instructions.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001746void Assembler::nop(int type) {
1747 // This is mov rx, rx.
1748 ASSERT(0 <= type && type <= 14); // mov pc, pc is not a nop.
1749 emit(al | 13*B21 | type*B12 | type);
1750}
1751
1752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753void Assembler::lea(Register dst,
1754 const MemOperand& x,
1755 SBit s,
1756 Condition cond) {
1757 int am = x.am_;
1758 if (!x.rm_.is_valid()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001759 // Immediate offset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 if ((am & P) == 0) // post indexing
1761 mov(dst, Operand(x.rn_), s, cond);
1762 else if ((am & U) == 0) // negative indexing
1763 sub(dst, x.rn_, Operand(x.offset_), s, cond);
1764 else
1765 add(dst, x.rn_, Operand(x.offset_), s, cond);
1766 } else {
1767 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
1768 // register offset the constructors make sure than both shift_imm_
1769 // and shift_op_ are initialized.
1770 ASSERT(!x.rm_.is(pc));
1771 if ((am & P) == 0) // post indexing
1772 mov(dst, Operand(x.rn_), s, cond);
1773 else if ((am & U) == 0) // negative indexing
1774 sub(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond);
1775 else
1776 add(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond);
1777 }
1778}
1779
1780
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001781bool Assembler::ImmediateFitsAddrMode1Instruction(int32_t imm32) {
1782 uint32_t dummy1;
1783 uint32_t dummy2;
1784 return fits_shifter(imm32, &dummy1, &dummy2, NULL);
1785}
1786
1787
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001788void Assembler::BlockConstPoolFor(int instructions) {
1789 BlockConstPoolBefore(pc_offset() + instructions * kInstrSize);
1790}
1791
1792
ager@chromium.org5c838252010-02-19 08:53:10 +00001793// Debugging.
ager@chromium.org4af710e2009-09-15 12:20:11 +00001794void Assembler::RecordJSReturn() {
1795 WriteRecordedPositions();
1796 CheckBuffer();
1797 RecordRelocInfo(RelocInfo::JS_RETURN);
1798}
1799
1800
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801void Assembler::RecordComment(const char* msg) {
1802 if (FLAG_debug_code) {
1803 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00001804 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 }
1806}
1807
1808
1809void Assembler::RecordPosition(int pos) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001810 if (pos == RelocInfo::kNoPosition) return;
1811 ASSERT(pos >= 0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001812 current_position_ = pos;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813}
1814
1815
1816void Assembler::RecordStatementPosition(int pos) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001817 if (pos == RelocInfo::kNoPosition) return;
1818 ASSERT(pos >= 0);
1819 current_statement_position_ = pos;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001820}
1821
1822
1823void Assembler::WriteRecordedPositions() {
1824 // Write the statement position if it is different from what was written last
1825 // time.
1826 if (current_statement_position_ != written_statement_position_) {
1827 CheckBuffer();
1828 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_);
1829 written_statement_position_ = current_statement_position_;
1830 }
1831
1832 // Write the position if it is different from what was written last time and
ager@chromium.org32912102009-01-16 10:38:43 +00001833 // also different from the written statement position.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001834 if (current_position_ != written_position_ &&
1835 current_position_ != written_statement_position_) {
1836 CheckBuffer();
1837 RecordRelocInfo(RelocInfo::POSITION, current_position_);
1838 written_position_ = current_position_;
1839 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840}
1841
1842
1843void Assembler::GrowBuffer() {
1844 if (!own_buffer_) FATAL("external code buffer is too small");
1845
ager@chromium.org5c838252010-02-19 08:53:10 +00001846 // Compute new buffer size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 CodeDesc desc; // the new buffer
1848 if (buffer_size_ < 4*KB) {
1849 desc.buffer_size = 4*KB;
1850 } else if (buffer_size_ < 1*MB) {
1851 desc.buffer_size = 2*buffer_size_;
1852 } else {
1853 desc.buffer_size = buffer_size_ + 1*MB;
1854 }
1855 CHECK_GT(desc.buffer_size, 0); // no overflow
1856
ager@chromium.org5c838252010-02-19 08:53:10 +00001857 // Setup new buffer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 desc.buffer = NewArray<byte>(desc.buffer_size);
1859
1860 desc.instr_size = pc_offset();
1861 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
1862
ager@chromium.org5c838252010-02-19 08:53:10 +00001863 // Copy the data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 int pc_delta = desc.buffer - buffer_;
1865 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
1866 memmove(desc.buffer, buffer_, desc.instr_size);
1867 memmove(reloc_info_writer.pos() + rc_delta,
1868 reloc_info_writer.pos(), desc.reloc_size);
1869
ager@chromium.org5c838252010-02-19 08:53:10 +00001870 // Switch buffers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 DeleteArray(buffer_);
1872 buffer_ = desc.buffer;
1873 buffer_size_ = desc.buffer_size;
1874 pc_ += pc_delta;
1875 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
1876 reloc_info_writer.last_pc() + pc_delta);
1877
ager@chromium.org5c838252010-02-19 08:53:10 +00001878 // None of our relocation types are pc relative pointing outside the code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 // buffer nor pc absolute pointing inside the code buffer, so there is no need
ager@chromium.org5c838252010-02-19 08:53:10 +00001880 // to relocate any emitted relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881
ager@chromium.org5c838252010-02-19 08:53:10 +00001882 // Relocate pending relocation entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883 for (int i = 0; i < num_prinfo_; i++) {
1884 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00001885 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
1886 rinfo.rmode() != RelocInfo::POSITION);
ager@chromium.org4af710e2009-09-15 12:20:11 +00001887 if (rinfo.rmode() != RelocInfo::JS_RETURN) {
1888 rinfo.set_pc(rinfo.pc() + pc_delta);
1889 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 }
1891}
1892
1893
ager@chromium.org236ad962008-09-25 09:45:57 +00001894void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
ager@chromium.org4af710e2009-09-15 12:20:11 +00001896 if (rmode >= RelocInfo::JS_RETURN && rmode <= RelocInfo::STATEMENT_POSITION) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001897 // Adjust code for new modes.
ager@chromium.org4af710e2009-09-15 12:20:11 +00001898 ASSERT(RelocInfo::IsJSReturn(rmode)
1899 || RelocInfo::IsComment(rmode)
1900 || RelocInfo::IsPosition(rmode));
ager@chromium.org5c838252010-02-19 08:53:10 +00001901 // These modes do not need an entry in the constant pool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 } else {
1903 ASSERT(num_prinfo_ < kMaxNumPRInfo);
1904 prinfo_[num_prinfo_++] = rinfo;
1905 // Make sure the constant pool is not emitted in place of the next
ager@chromium.org5c838252010-02-19 08:53:10 +00001906 // instruction for which we just recorded relocation info.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 BlockConstPoolBefore(pc_offset() + kInstrSize);
1908 }
ager@chromium.org236ad962008-09-25 09:45:57 +00001909 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001910 // Don't record external references unless the heap will be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001911 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
1912#ifdef DEBUG
1913 if (!Serializer::enabled()) {
1914 Serializer::TooLateToEnableNow();
1915 }
1916#endif
1917 if (!Serializer::enabled() && !FLAG_debug_code) {
1918 return;
1919 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001920 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
1922 reloc_info_writer.Write(&rinfo);
1923 }
1924}
1925
1926
1927void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
1928 // Calculate the offset of the next check. It will be overwritten
1929 // when a const pool is generated or when const pools are being
1930 // blocked for a specific range.
1931 next_buffer_check_ = pc_offset() + kCheckConstInterval;
1932
ager@chromium.org5c838252010-02-19 08:53:10 +00001933 // There is nothing to do if there are no pending relocation info entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 if (num_prinfo_ == 0) return;
1935
1936 // We emit a constant pool at regular intervals of about kDistBetweenPools
1937 // or when requested by parameter force_emit (e.g. after each function).
1938 // We prefer not to emit a jump unless the max distance is reached or if we
1939 // are running low on slots, which can happen if a lot of constants are being
1940 // emitted (e.g. --debug-code and many static references).
1941 int dist = pc_offset() - last_const_pool_end_;
1942 if (!force_emit && dist < kMaxDistBetweenPools &&
1943 (require_jump || dist < kDistBetweenPools) &&
1944 // TODO(1236125): Cleanup the "magic" number below. We know that
1945 // the code generation will test every kCheckConstIntervalInst.
1946 // Thus we are safe as long as we generate less than 7 constant
1947 // entries per instruction.
1948 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
1949 return;
1950 }
1951
1952 // If we did not return by now, we need to emit the constant pool soon.
1953
1954 // However, some small sequences of instructions must not be broken up by the
1955 // insertion of a constant pool; such sequences are protected by setting
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001956 // either const_pool_blocked_nesting_ or no_const_pool_before_, which are
1957 // both checked here. Also, recursive calls to CheckConstPool are blocked by
1958 // no_const_pool_before_.
1959 if (const_pool_blocked_nesting_ > 0 || pc_offset() < no_const_pool_before_) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001960 // Emission is currently blocked; make sure we try again as soon as
1961 // possible.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001962 if (const_pool_blocked_nesting_ > 0) {
1963 next_buffer_check_ = pc_offset() + kInstrSize;
1964 } else {
1965 next_buffer_check_ = no_const_pool_before_;
1966 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967
ager@chromium.org5c838252010-02-19 08:53:10 +00001968 // Something is wrong if emission is forced and blocked at the same time.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 ASSERT(!force_emit);
1970 return;
1971 }
1972
1973 int jump_instr = require_jump ? kInstrSize : 0;
1974
1975 // Check that the code buffer is large enough before emitting the constant
1976 // pool and relocation information (include the jump over the pool and the
1977 // constant pool marker).
1978 int max_needed_space =
1979 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
1980 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
1981
ager@chromium.org5c838252010-02-19 08:53:10 +00001982 // Block recursive calls to CheckConstPool.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
1984 num_prinfo_*kInstrSize);
1985 // Don't bother to check for the emit calls below.
1986 next_buffer_check_ = no_const_pool_before_;
1987
ager@chromium.org5c838252010-02-19 08:53:10 +00001988 // Emit jump over constant pool if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 Label after_pool;
1990 if (require_jump) b(&after_pool);
1991
1992 RecordComment("[ Constant Pool");
1993
ager@chromium.org5c838252010-02-19 08:53:10 +00001994 // Put down constant pool marker "Undefined instruction" as specified by
1995 // A3.1 Instruction set encoding.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 emit(0x03000000 | num_prinfo_);
1997
ager@chromium.org5c838252010-02-19 08:53:10 +00001998 // Emit constant pool entries.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 for (int i = 0; i < num_prinfo_; i++) {
2000 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00002001 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
2002 rinfo.rmode() != RelocInfo::POSITION &&
2003 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 Instr instr = instr_at(rinfo.pc());
ager@chromium.org4af710e2009-09-15 12:20:11 +00002005
ager@chromium.org5c838252010-02-19 08:53:10 +00002006 // Instruction to patch must be a ldr/str [pc, #offset].
2007 // P and U set, B and W clear, Rn == pc, offset12 still 0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) ==
2009 (2*B25 | P | U | pc.code()*B16));
2010 int delta = pc_ - rinfo.pc() - 8;
2011 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
2012 if (delta < 0) {
2013 instr &= ~U;
2014 delta = -delta;
2015 }
2016 ASSERT(is_uint12(delta));
2017 instr_at_put(rinfo.pc(), instr + delta);
2018 emit(rinfo.data());
2019 }
2020 num_prinfo_ = 0;
2021 last_const_pool_end_ = pc_offset();
2022
2023 RecordComment("]");
2024
2025 if (after_pool.is_linked()) {
2026 bind(&after_pool);
2027 }
2028
2029 // Since a constant pool was just emitted, move the check offset forward by
2030 // the standard interval.
2031 next_buffer_check_ = pc_offset() + kCheckConstInterval;
2032}
2033
2034
2035} } // namespace v8::internal