blob: 21cd22527fd9f33f5f58d459d0bfaab7d68169dc [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
33// The original source code covered by the above license above has been modified
34// significantly by Google Inc.
ager@chromium.org9258b6b2008-09-11 09:11:10 +000035// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37#include "v8.h"
38
39#include "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
42namespace v8 { namespace internal {
43
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044// -----------------------------------------------------------------------------
45// Implementation of Register and CRegister
46
47Register no_reg = { -1 };
48
49Register r0 = { 0 };
50Register r1 = { 1 };
51Register r2 = { 2 };
52Register r3 = { 3 };
53Register r4 = { 4 };
54Register r5 = { 5 };
55Register r6 = { 6 };
56Register r7 = { 7 };
57Register r8 = { 8 };
58Register r9 = { 9 };
59Register r10 = { 10 };
60Register fp = { 11 };
61Register ip = { 12 };
62Register sp = { 13 };
63Register lr = { 14 };
64Register pc = { 15 };
65
66
67CRegister no_creg = { -1 };
68
69CRegister cr0 = { 0 };
70CRegister cr1 = { 1 };
71CRegister cr2 = { 2 };
72CRegister cr3 = { 3 };
73CRegister cr4 = { 4 };
74CRegister cr5 = { 5 };
75CRegister cr6 = { 6 };
76CRegister cr7 = { 7 };
77CRegister cr8 = { 8 };
78CRegister cr9 = { 9 };
79CRegister cr10 = { 10 };
80CRegister cr11 = { 11 };
81CRegister cr12 = { 12 };
82CRegister cr13 = { 13 };
83CRegister cr14 = { 14 };
84CRegister cr15 = { 15 };
85
86
87// In order to determine the pc store offset, we execute a small code sequence.
88// See ARM Architecture Reference Manual section A-2.4.3
89// Note that 'str pc, [sp]' and 'stmia sp, {pc}' were using different offsets
90// under the QEMU emulator (now fixed), so we are careful to test the actual
91// instruction we are interested in (stmia).
92int PcStoreOffset() {
93#if !defined(__arm__)
94 // Building an ARM emulator based target. The emulator is wired for 8 byte
95 // pc offsets as is the default in the spec.
96 static int pc_store_offset = 8;
97#elif defined(__arm__) && !defined(__thumb__)
98 // __arm__ may be defined in thumb mode.
99 static int pc_store_offset = -1;
100 asm volatile(
101 "sub sp, sp, #4 \n\t"
102 "sub r1, pc, #4 \n\t"
103 "stmia sp, {pc} \n\t"
104 "ldr r0, [sp] \n\t"
105 "add sp, sp, #4 \n\t"
106 "sub %0, r0, r1 \n\t"
107 : "=r" (pc_store_offset) : : "r0", "r1", "memory");
108#elif defined(__thumb__)
109 static int pc_store_offset = -1;
110 asm volatile(
111 "@ Enter ARM Mode \n\t"
112 "adr r2, 1f \n\t"
113 "bx r2 \n\t"
114 ".ALIGN 4 \n\t"
115 ".ARM \n"
116 "1: sub sp, sp, #4 \n\t"
117 "sub r1, pc, #4 \n\t"
118 "stmia sp, {pc} \n\t"
119 "ldr r0, [sp] \n\t"
120 "add sp, sp, #4 \n\t"
121 "sub %0, r0, r1 \n"
122 "@ Enter THUMB Mode\n\t"
123 "adr r2, 2f+1 \n\t"
124 "bx r2 \n\t"
125 ".THUMB \n"
126 "2: \n\t"
127 : "=r" (pc_store_offset) : : "r0", "r1", "r2", "memory");
128#else
129#error unsupported architecture
130#endif
131 ASSERT(pc_store_offset == 8 || pc_store_offset == 12);
132 return pc_store_offset;
133}
134
135
136// -----------------------------------------------------------------------------
137// Implementation of RelocInfo
138
139const int RelocInfo::kApplyMask = 0;
140
141
142void RelocInfo::patch_code(byte* instructions, int instruction_count) {
143 // Patch the code at the current address with the supplied instructions.
144 UNIMPLEMENTED();
145}
146
147
148// Patch the code at the current PC with a call to the target address.
149// Additional guard int3 instructions can be added if required.
150void RelocInfo::patch_code_with_call(Address target, int guard_bytes) {
151 // Patch the code at the current address with a call to the target.
152 UNIMPLEMENTED();
153}
154
155
156// -----------------------------------------------------------------------------
157// Implementation of Operand and MemOperand
158// See assembler-arm-inl.h for inlined constructors
159
160Operand::Operand(Handle<Object> handle) {
161 rm_ = no_reg;
162 // Verify all Objects referred by code are NOT in new space.
163 Object* obj = *handle;
164 ASSERT(!Heap::InNewSpace(obj));
165 if (obj->IsHeapObject()) {
166 imm32_ = reinterpret_cast<intptr_t>(handle.location());
ager@chromium.org236ad962008-09-25 09:45:57 +0000167 rmode_ = RelocInfo::EMBEDDED_OBJECT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 } else {
169 // no relocation needed
170 imm32_ = reinterpret_cast<intptr_t>(obj);
ager@chromium.org236ad962008-09-25 09:45:57 +0000171 rmode_ = RelocInfo::NONE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 }
173}
174
175
176Operand::Operand(Register rm, ShiftOp shift_op, int shift_imm) {
177 ASSERT(is_uint5(shift_imm));
178 ASSERT(shift_op != ROR || shift_imm != 0); // use RRX if you mean it
179 rm_ = rm;
180 rs_ = no_reg;
181 shift_op_ = shift_op;
182 shift_imm_ = shift_imm & 31;
183 if (shift_op == RRX) {
184 // encoded as ROR with shift_imm == 0
185 ASSERT(shift_imm == 0);
186 shift_op_ = ROR;
187 shift_imm_ = 0;
188 }
189}
190
191
192Operand::Operand(Register rm, ShiftOp shift_op, Register rs) {
193 ASSERT(shift_op != RRX);
194 rm_ = rm;
195 rs_ = no_reg;
196 shift_op_ = shift_op;
197 rs_ = rs;
198}
199
200
201MemOperand::MemOperand(Register rn, int32_t offset, AddrMode am) {
202 rn_ = rn;
203 rm_ = no_reg;
204 offset_ = offset;
205 am_ = am;
206}
207
208MemOperand::MemOperand(Register rn, Register rm, AddrMode am) {
209 rn_ = rn;
210 rm_ = rm;
211 shift_op_ = LSL;
212 shift_imm_ = 0;
213 am_ = am;
214}
215
216
217MemOperand::MemOperand(Register rn, Register rm,
218 ShiftOp shift_op, int shift_imm, AddrMode am) {
219 ASSERT(is_uint5(shift_imm));
220 rn_ = rn;
221 rm_ = rm;
222 shift_op_ = shift_op;
223 shift_imm_ = shift_imm & 31;
224 am_ = am;
225}
226
227
228// -----------------------------------------------------------------------------
229// Implementation of Assembler
230
231// Instruction encoding bits
232enum {
233 H = 1 << 5, // halfword (or byte)
234 S6 = 1 << 6, // signed (or unsigned)
235 L = 1 << 20, // load (or store)
236 S = 1 << 20, // set condition code (or leave unchanged)
237 W = 1 << 21, // writeback base register (or leave unchanged)
238 A = 1 << 21, // accumulate in multiply instruction (or not)
239 B = 1 << 22, // unsigned byte (or word)
240 N = 1 << 22, // long (or short)
241 U = 1 << 23, // positive (or negative) offset/index
242 P = 1 << 24, // offset/pre-indexed addressing (or post-indexed addressing)
243 I = 1 << 25, // immediate shifter operand (or not)
244
245 B4 = 1 << 4,
246 B5 = 1 << 5,
247 B7 = 1 << 7,
248 B8 = 1 << 8,
249 B12 = 1 << 12,
250 B16 = 1 << 16,
251 B20 = 1 << 20,
252 B21 = 1 << 21,
253 B22 = 1 << 22,
254 B23 = 1 << 23,
255 B24 = 1 << 24,
256 B25 = 1 << 25,
257 B26 = 1 << 26,
258 B27 = 1 << 27,
259
260 // Instruction bit masks
mads.s.ager31e71382008-08-13 09:32:07 +0000261 RdMask = 15 << 12, // in str instruction
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 CondMask = 15 << 28,
263 OpCodeMask = 15 << 21, // in data-processing instructions
264 Imm24Mask = (1 << 24) - 1,
265 Off12Mask = (1 << 12) - 1,
266 // Reserved condition
267 nv = 15 << 28
268};
269
270
mads.s.ager31e71382008-08-13 09:32:07 +0000271// add(sp, sp, 4) instruction (aka Pop())
272static const Instr kPopInstruction =
273 al | 4 * B21 | 4 | LeaveCC | I | sp.code() * B16 | sp.code() * B12;
274// str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
275// register r is not encoded.
276static const Instr kPushRegPattern =
277 al | B26 | 4 | NegPreIndex | sp.code() * B16;
278// ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
279// register r is not encoded.
280static const Instr kPopRegPattern =
281 al | B26 | L | 4 | PostIndex | sp.code() * B16;
282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283// spare_buffer_
284static const int kMinimalBufferSize = 4*KB;
285static byte* spare_buffer_ = NULL;
286
287Assembler::Assembler(void* buffer, int buffer_size) {
288 if (buffer == NULL) {
289 // do our own buffer management
290 if (buffer_size <= kMinimalBufferSize) {
291 buffer_size = kMinimalBufferSize;
292
293 if (spare_buffer_ != NULL) {
294 buffer = spare_buffer_;
295 spare_buffer_ = NULL;
296 }
297 }
298 if (buffer == NULL) {
299 buffer_ = NewArray<byte>(buffer_size);
300 } else {
301 buffer_ = static_cast<byte*>(buffer);
302 }
303 buffer_size_ = buffer_size;
304 own_buffer_ = true;
305
306 } else {
307 // use externally provided buffer instead
308 ASSERT(buffer_size > 0);
309 buffer_ = static_cast<byte*>(buffer);
310 buffer_size_ = buffer_size;
311 own_buffer_ = false;
312 }
313
314 // setup buffer pointers
315 ASSERT(buffer_ != NULL);
316 pc_ = buffer_;
317 reloc_info_writer.Reposition(buffer_ + buffer_size, pc_);
318 num_prinfo_ = 0;
319 next_buffer_check_ = 0;
320 no_const_pool_before_ = 0;
321 last_const_pool_end_ = 0;
322 last_bound_pos_ = 0;
ager@chromium.org236ad962008-09-25 09:45:57 +0000323 last_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 last_position_is_statement_ = false;
325}
326
327
328Assembler::~Assembler() {
329 if (own_buffer_) {
330 if (spare_buffer_ == NULL && buffer_size_ == kMinimalBufferSize) {
331 spare_buffer_ = buffer_;
332 } else {
333 DeleteArray(buffer_);
334 }
335 }
336}
337
338
339void Assembler::GetCode(CodeDesc* desc) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 // emit constant pool if necessary
341 CheckConstPool(true, false);
342 ASSERT(num_prinfo_ == 0);
343
344 // setup desc
345 desc->buffer = buffer_;
346 desc->buffer_size = buffer_size_;
347 desc->instr_size = pc_offset();
348 desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
349}
350
351
352void Assembler::Align(int m) {
353 ASSERT(m >= 4 && IsPowerOf2(m));
354 while ((pc_offset() & (m - 1)) != 0) {
355 nop();
356 }
357}
358
359
360// Labels refer to positions in the (to be) generated code.
361// There are bound, linked, and unused labels.
362//
363// Bound labels refer to known positions in the already
364// generated code. pos() is the position the label refers to.
365//
366// Linked labels refer to unknown positions in the code
367// to be generated; pos() is the position of the last
368// instruction using the label.
369
370
371// The link chain is terminated by a negative code position (must be aligned)
372const int kEndOfChain = -4;
373
374
375int Assembler::target_at(int pos) {
376 Instr instr = instr_at(pos);
377 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
378 int imm26 = ((instr & Imm24Mask) << 8) >> 6;
379 if ((instr & CondMask) == nv && (instr & B24) != 0)
380 // blx uses bit 24 to encode bit 2 of imm26
381 imm26 += 2;
382
383 return pos + 8 + imm26;
384}
385
386
387void Assembler::target_at_put(int pos, int target_pos) {
388 int imm26 = target_pos - pos - 8;
389 Instr instr = instr_at(pos);
390 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx imm24
391 if ((instr & CondMask) == nv) {
392 // blx uses bit 24 to encode bit 2 of imm26
393 ASSERT((imm26 & 1) == 0);
394 instr = (instr & ~(B24 | Imm24Mask)) | ((imm26 & 2) >> 1)*B24;
395 } else {
396 ASSERT((imm26 & 3) == 0);
397 instr &= ~Imm24Mask;
398 }
399 int imm24 = imm26 >> 2;
400 ASSERT(is_int24(imm24));
401 instr_at_put(pos, instr | (imm24 & Imm24Mask));
402}
403
404
405void Assembler::print(Label* L) {
406 if (L->is_unused()) {
407 PrintF("unused label\n");
408 } else if (L->is_bound()) {
409 PrintF("bound label to %d\n", L->pos());
410 } else if (L->is_linked()) {
411 Label l = *L;
412 PrintF("unbound label");
413 while (l.is_linked()) {
414 PrintF("@ %d ", l.pos());
415 Instr instr = instr_at(l.pos());
416 ASSERT((instr & 7*B25) == 5*B25); // b, bl, or blx
417 int cond = instr & CondMask;
418 const char* b;
419 const char* c;
420 if (cond == nv) {
421 b = "blx";
422 c = "";
423 } else {
424 if ((instr & B24) != 0)
425 b = "bl";
426 else
427 b = "b";
428
429 switch (cond) {
430 case eq: c = "eq"; break;
431 case ne: c = "ne"; break;
432 case hs: c = "hs"; break;
433 case lo: c = "lo"; break;
434 case mi: c = "mi"; break;
435 case pl: c = "pl"; break;
436 case vs: c = "vs"; break;
437 case vc: c = "vc"; break;
438 case hi: c = "hi"; break;
439 case ls: c = "ls"; break;
440 case ge: c = "ge"; break;
441 case lt: c = "lt"; break;
442 case gt: c = "gt"; break;
443 case le: c = "le"; break;
444 case al: c = ""; break;
445 default:
446 c = "";
447 UNREACHABLE();
448 }
449 }
450 PrintF("%s%s\n", b, c);
451 next(&l);
452 }
453 } else {
454 PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
455 }
456}
457
458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459void Assembler::bind_to(Label* L, int pos) {
460 ASSERT(0 <= pos && pos <= pc_offset()); // must have a valid binding position
461 while (L->is_linked()) {
462 int fixup_pos = L->pos();
463 next(L); // call next before overwriting link with target at fixup_pos
464 target_at_put(fixup_pos, pos);
465 }
466 L->bind_to(pos);
467
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000468 // Keep track of the last bound label so we don't eliminate any instructions
469 // before a bound label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 if (pos > last_bound_pos_)
471 last_bound_pos_ = pos;
472}
473
474
475void Assembler::link_to(Label* L, Label* appendix) {
476 if (appendix->is_linked()) {
477 if (L->is_linked()) {
478 // append appendix to L's list
479 int fixup_pos;
480 int link = L->pos();
481 do {
482 fixup_pos = link;
483 link = target_at(fixup_pos);
484 } while (link > 0);
485 ASSERT(link == kEndOfChain);
486 target_at_put(fixup_pos, appendix->pos());
487 } else {
488 // L is empty, simply use appendix
489 *L = *appendix;
490 }
491 }
492 appendix->Unuse(); // appendix should not be used anymore
493}
494
495
496void Assembler::bind(Label* L) {
497 ASSERT(!L->is_bound()); // label can only be bound once
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 bind_to(L, pc_offset());
499}
500
501
502void Assembler::next(Label* L) {
503 ASSERT(L->is_linked());
504 int link = target_at(L->pos());
505 if (link > 0) {
506 L->link_to(link);
507 } else {
508 ASSERT(link == kEndOfChain);
509 L->Unuse();
510 }
511}
512
513
514// Low-level code emission routines depending on the addressing mode
515static bool fits_shifter(uint32_t imm32,
516 uint32_t* rotate_imm,
517 uint32_t* immed_8,
518 Instr* instr) {
519 // imm32 must be unsigned
520 for (int rot = 0; rot < 16; rot++) {
521 uint32_t imm8 = (imm32 << 2*rot) | (imm32 >> (32 - 2*rot));
522 if ((imm8 <= 0xff)) {
523 *rotate_imm = rot;
524 *immed_8 = imm8;
525 return true;
526 }
527 }
528 // if the opcode is mov or mvn and if ~imm32 fits, change the opcode
529 if (instr != NULL && (*instr & 0xd*B21) == 0xd*B21) {
530 if (fits_shifter(~imm32, rotate_imm, immed_8, NULL)) {
531 *instr ^= 0x2*B21;
532 return true;
533 }
534 }
535 return false;
536}
537
538
539void Assembler::addrmod1(Instr instr,
540 Register rn,
541 Register rd,
542 const Operand& x) {
543 CheckBuffer();
544 ASSERT((instr & ~(CondMask | OpCodeMask | S)) == 0);
545 if (!x.rm_.is_valid()) {
546 // immediate
547 uint32_t rotate_imm;
548 uint32_t immed_8;
ager@chromium.org236ad962008-09-25 09:45:57 +0000549 if ((x.rmode_ != RelocInfo::NONE &&
550 x.rmode_ != RelocInfo::EXTERNAL_REFERENCE) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 !fits_shifter(x.imm32_, &rotate_imm, &immed_8, &instr)) {
552 // The immediate operand cannot be encoded as a shifter operand, so load
553 // it first to register ip and change the original instruction to use ip.
554 // However, if the original instruction is a 'mov rd, x' (not setting the
555 // condition code), then replace it with a 'ldr rd, [pc]'
556 RecordRelocInfo(x.rmode_, x.imm32_);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000557 CHECK(!rn.is(ip)); // rn should never be ip, or will be trashed
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 Condition cond = static_cast<Condition>(instr & CondMask);
559 if ((instr & ~CondMask) == 13*B21) { // mov, S not set
560 ldr(rd, MemOperand(pc, 0), cond);
561 } else {
562 ldr(ip, MemOperand(pc, 0), cond);
563 addrmod1(instr, rn, rd, Operand(ip));
564 }
565 return;
566 }
567 instr |= I | rotate_imm*B8 | immed_8;
568 } else if (!x.rs_.is_valid()) {
569 // immediate shift
570 instr |= x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
571 } else {
572 // register shift
573 ASSERT(!rn.is(pc) && !rd.is(pc) && !x.rm_.is(pc) && !x.rs_.is(pc));
574 instr |= x.rs_.code()*B8 | x.shift_op_ | B4 | x.rm_.code();
575 }
576 emit(instr | rn.code()*B16 | rd.code()*B12);
577 if (rn.is(pc) || x.rm_.is(pc))
578 // block constant pool emission for one instruction after reading pc
579 BlockConstPoolBefore(pc_offset() + kInstrSize);
580}
581
582
583void Assembler::addrmod2(Instr instr, Register rd, const MemOperand& x) {
584 ASSERT((instr & ~(CondMask | B | L)) == B26);
585 int am = x.am_;
586 if (!x.rm_.is_valid()) {
587 // immediate offset
588 int offset_12 = x.offset_;
589 if (offset_12 < 0) {
590 offset_12 = -offset_12;
591 am ^= U;
592 }
593 if (!is_uint12(offset_12)) {
594 // immediate offset cannot be encoded, load it first to register ip
595 // rn (and rd in a load) should never be ip, or will be trashed
596 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
597 mov(ip, Operand(x.offset_), LeaveCC,
598 static_cast<Condition>(instr & CondMask));
599 addrmod2(instr, rd, MemOperand(x.rn_, ip, x.am_));
600 return;
601 }
602 ASSERT(offset_12 >= 0); // no masking needed
603 instr |= offset_12;
604 } else {
605 // register offset (shift_imm_ and shift_op_ are 0) or scaled
606 // register offset the constructors make sure than both shift_imm_
607 // and shift_op_ are initialized
608 ASSERT(!x.rm_.is(pc));
609 instr |= B25 | x.shift_imm_*B7 | x.shift_op_ | x.rm_.code();
610 }
611 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
612 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
613}
614
615
616void Assembler::addrmod3(Instr instr, Register rd, const MemOperand& x) {
617 ASSERT((instr & ~(CondMask | L | S6 | H)) == (B4 | B7));
618 ASSERT(x.rn_.is_valid());
619 int am = x.am_;
620 if (!x.rm_.is_valid()) {
621 // immediate offset
622 int offset_8 = x.offset_;
623 if (offset_8 < 0) {
624 offset_8 = -offset_8;
625 am ^= U;
626 }
627 if (!is_uint8(offset_8)) {
628 // immediate offset cannot be encoded, load it first to register ip
629 // rn (and rd in a load) should never be ip, or will be trashed
630 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
631 mov(ip, Operand(x.offset_), LeaveCC,
632 static_cast<Condition>(instr & CondMask));
633 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
634 return;
635 }
636 ASSERT(offset_8 >= 0); // no masking needed
637 instr |= B | (offset_8 >> 4)*B8 | (offset_8 & 0xf);
638 } else if (x.shift_imm_ != 0) {
639 // scaled register offset not supported, load index first
640 // rn (and rd in a load) should never be ip, or will be trashed
641 ASSERT(!x.rn_.is(ip) && ((instr & L) == L || !rd.is(ip)));
642 mov(ip, Operand(x.rm_, x.shift_op_, x.shift_imm_), LeaveCC,
643 static_cast<Condition>(instr & CondMask));
644 addrmod3(instr, rd, MemOperand(x.rn_, ip, x.am_));
645 return;
646 } else {
647 // register offset
648 ASSERT((am & (P|W)) == P || !x.rm_.is(pc)); // no pc index with writeback
649 instr |= x.rm_.code();
650 }
651 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
652 emit(instr | am | x.rn_.code()*B16 | rd.code()*B12);
653}
654
655
656void Assembler::addrmod4(Instr instr, Register rn, RegList rl) {
657 ASSERT((instr & ~(CondMask | P | U | W | L)) == B27);
658 ASSERT(rl != 0);
659 ASSERT(!rn.is(pc));
660 emit(instr | rn.code()*B16 | rl);
661}
662
663
664void Assembler::addrmod5(Instr instr, CRegister crd, const MemOperand& x) {
665 // unindexed addressing is not encoded by this function
666 ASSERT((instr & ~(CondMask | P | U | N | W | L)) == (B27 | B26));
667 ASSERT(x.rn_.is_valid() && !x.rm_.is_valid());
668 int am = x.am_;
669 int offset_8 = x.offset_;
670 ASSERT((offset_8 & 3) == 0); // offset must be an aligned word offset
671 offset_8 >>= 2;
672 if (offset_8 < 0) {
673 offset_8 = -offset_8;
674 am ^= U;
675 }
676 ASSERT(is_uint8(offset_8)); // unsigned word offset must fit in a byte
677 ASSERT((am & (P|W)) == P || !x.rn_.is(pc)); // no pc base with writeback
678
679 // post-indexed addressing requires W == 1; different than in addrmod2/3
680 if ((am & P) == 0)
681 am |= W;
682
683 ASSERT(offset_8 >= 0); // no masking needed
684 emit(instr | am | x.rn_.code()*B16 | crd.code()*B12 | offset_8);
685}
686
687
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000688int Assembler::branch_offset(Label* L, bool jump_elimination_allowed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 int target_pos;
690 if (L->is_bound()) {
691 target_pos = L->pos();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 } else {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000693 if (L->is_linked()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 target_pos = L->pos(); // L's link
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000695 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 target_pos = kEndOfChain;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 L->link_to(pc_offset());
699 }
700
701 // Block the emission of the constant pool, since the branch instruction must
702 // be emitted at the pc offset recorded by the label
703 BlockConstPoolBefore(pc_offset() + kInstrSize);
704
705 return target_pos - pc_offset() - 8;
706}
707
708
709// Branch instructions
710void Assembler::b(int branch_offset, Condition cond) {
711 ASSERT((branch_offset & 3) == 0);
712 int imm24 = branch_offset >> 2;
713 ASSERT(is_int24(imm24));
714 emit(cond | B27 | B25 | (imm24 & Imm24Mask));
715
716 if (cond == al)
717 // dead code is a good location to emit the constant pool
718 CheckConstPool(false, false);
719}
720
721
722void Assembler::bl(int branch_offset, Condition cond) {
723 ASSERT((branch_offset & 3) == 0);
724 int imm24 = branch_offset >> 2;
725 ASSERT(is_int24(imm24));
726 emit(cond | B27 | B25 | B24 | (imm24 & Imm24Mask));
727}
728
729
730void Assembler::blx(int branch_offset) { // v5 and above
731 ASSERT((branch_offset & 1) == 0);
732 int h = ((branch_offset & 2) >> 1)*B24;
733 int imm24 = branch_offset >> 2;
734 ASSERT(is_int24(imm24));
735 emit(15 << 28 | B27 | B25 | h | (imm24 & Imm24Mask));
736}
737
738
739void Assembler::blx(Register target, Condition cond) { // v5 and above
740 ASSERT(!target.is(pc));
741 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | 3*B4 | target.code());
742}
743
744
745void Assembler::bx(Register target, Condition cond) { // v5 and above, plus v4t
746 ASSERT(!target.is(pc)); // use of pc is actually allowed, but discouraged
747 emit(cond | B24 | B21 | 15*B16 | 15*B12 | 15*B8 | B4 | target.code());
748}
749
750
751// Data-processing instructions
752void Assembler::and_(Register dst, Register src1, const Operand& src2,
753 SBit s, Condition cond) {
754 addrmod1(cond | 0*B21 | s, src1, dst, src2);
755}
756
757
758void Assembler::eor(Register dst, Register src1, const Operand& src2,
759 SBit s, Condition cond) {
760 addrmod1(cond | 1*B21 | s, src1, dst, src2);
761}
762
763
764void Assembler::sub(Register dst, Register src1, const Operand& src2,
765 SBit s, Condition cond) {
766 addrmod1(cond | 2*B21 | s, src1, dst, src2);
767}
768
769
770void Assembler::rsb(Register dst, Register src1, const Operand& src2,
771 SBit s, Condition cond) {
772 addrmod1(cond | 3*B21 | s, src1, dst, src2);
773}
774
775
776void Assembler::add(Register dst, Register src1, const Operand& src2,
777 SBit s, Condition cond) {
778 addrmod1(cond | 4*B21 | s, src1, dst, src2);
mads.s.ager31e71382008-08-13 09:32:07 +0000779
780 // Eliminate pattern: push(r), pop()
781 // str(src, MemOperand(sp, 4, NegPreIndex), al);
782 // add(sp, sp, Operand(kPointerSize));
783 // Both instructions can be eliminated.
784 int pattern_size = 2 * kInstrSize;
785 if (FLAG_push_pop_elimination &&
786 last_bound_pos_ <= (pc_offset() - pattern_size) &&
787 reloc_info_writer.last_pc() <= (pc_ - pattern_size) &&
788 // pattern
789 instr_at(pc_ - 1 * kInstrSize) == kPopInstruction &&
790 (instr_at(pc_ - 2 * kInstrSize) & ~RdMask) == kPushRegPattern) {
791 pc_ -= 2 * kInstrSize;
792 if (FLAG_print_push_pop_elimination) {
793 PrintF("%x push(reg)/pop() eliminated\n", pc_offset());
794 }
795 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796}
797
798
799void Assembler::adc(Register dst, Register src1, const Operand& src2,
800 SBit s, Condition cond) {
801 addrmod1(cond | 5*B21 | s, src1, dst, src2);
802}
803
804
805void Assembler::sbc(Register dst, Register src1, const Operand& src2,
806 SBit s, Condition cond) {
807 addrmod1(cond | 6*B21 | s, src1, dst, src2);
808}
809
810
811void Assembler::rsc(Register dst, Register src1, const Operand& src2,
812 SBit s, Condition cond) {
813 addrmod1(cond | 7*B21 | s, src1, dst, src2);
814}
815
816
817void Assembler::tst(Register src1, const Operand& src2, Condition cond) {
818 addrmod1(cond | 8*B21 | S, src1, r0, src2);
819}
820
821
822void Assembler::teq(Register src1, const Operand& src2, Condition cond) {
823 addrmod1(cond | 9*B21 | S, src1, r0, src2);
824}
825
826
827void Assembler::cmp(Register src1, const Operand& src2, Condition cond) {
828 addrmod1(cond | 10*B21 | S, src1, r0, src2);
829}
830
831
832void Assembler::cmn(Register src1, const Operand& src2, Condition cond) {
833 addrmod1(cond | 11*B21 | S, src1, r0, src2);
834}
835
836
837void Assembler::orr(Register dst, Register src1, const Operand& src2,
838 SBit s, Condition cond) {
839 addrmod1(cond | 12*B21 | s, src1, dst, src2);
840}
841
842
843void Assembler::mov(Register dst, const Operand& src, SBit s, Condition cond) {
844 addrmod1(cond | 13*B21 | s, r0, dst, src);
845}
846
847
848void Assembler::bic(Register dst, Register src1, const Operand& src2,
849 SBit s, Condition cond) {
850 addrmod1(cond | 14*B21 | s, src1, dst, src2);
851}
852
853
854void Assembler::mvn(Register dst, const Operand& src, SBit s, Condition cond) {
855 addrmod1(cond | 15*B21 | s, r0, dst, src);
856}
857
858
859// Multiply instructions
860void Assembler::mla(Register dst, Register src1, Register src2, Register srcA,
861 SBit s, Condition cond) {
862 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc) && !srcA.is(pc));
863 ASSERT(!dst.is(src1));
864 emit(cond | A | s | dst.code()*B16 | srcA.code()*B12 |
865 src2.code()*B8 | B7 | B4 | src1.code());
866}
867
868
869void Assembler::mul(Register dst, Register src1, Register src2,
870 SBit s, Condition cond) {
871 ASSERT(!dst.is(pc) && !src1.is(pc) && !src2.is(pc));
872 ASSERT(!dst.is(src1));
873 emit(cond | s | dst.code()*B16 | src2.code()*B8 | B7 | B4 | src1.code());
874}
875
876
877void Assembler::smlal(Register dstL,
878 Register dstH,
879 Register src1,
880 Register src2,
881 SBit s,
882 Condition cond) {
883 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
884 ASSERT(!dstL.is(dstH) && !dstH.is(src1) && !src1.is(dstL));
885 emit(cond | B23 | B22 | A | s | dstH.code()*B16 | dstL.code()*B12 |
886 src2.code()*B8 | B7 | B4 | src1.code());
887}
888
889
890void Assembler::smull(Register dstL,
891 Register dstH,
892 Register src1,
893 Register src2,
894 SBit s,
895 Condition cond) {
896 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
897 ASSERT(!dstL.is(dstH) && !dstH.is(src1) && !src1.is(dstL));
898 emit(cond | B23 | B22 | s | dstH.code()*B16 | dstL.code()*B12 |
899 src2.code()*B8 | B7 | B4 | src1.code());
900}
901
902
903void Assembler::umlal(Register dstL,
904 Register dstH,
905 Register src1,
906 Register src2,
907 SBit s,
908 Condition cond) {
909 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
910 ASSERT(!dstL.is(dstH) && !dstH.is(src1) && !src1.is(dstL));
911 emit(cond | B23 | A | s | dstH.code()*B16 | dstL.code()*B12 |
912 src2.code()*B8 | B7 | B4 | src1.code());
913}
914
915
916void Assembler::umull(Register dstL,
917 Register dstH,
918 Register src1,
919 Register src2,
920 SBit s,
921 Condition cond) {
922 ASSERT(!dstL.is(pc) && !dstH.is(pc) && !src1.is(pc) && !src2.is(pc));
923 ASSERT(!dstL.is(dstH) && !dstH.is(src1) && !src1.is(dstL));
924 emit(cond | B23 | s | dstH.code()*B16 | dstL.code()*B12 |
925 src2.code()*B8 | B7 | B4 | src1.code());
926}
927
928
929// Miscellaneous arithmetic instructions
930void Assembler::clz(Register dst, Register src, Condition cond) {
931 // v5 and above.
932 ASSERT(!dst.is(pc) && !src.is(pc));
933 emit(cond | B24 | B22 | B21 | 15*B16 | dst.code()*B12 |
934 15*B8 | B4 | src.code());
935}
936
937
938// Status register access instructions
939void Assembler::mrs(Register dst, SRegister s, Condition cond) {
940 ASSERT(!dst.is(pc));
941 emit(cond | B24 | s | 15*B16 | dst.code()*B12);
942}
943
944
945void Assembler::msr(SRegisterFieldMask fields, const Operand& src,
946 Condition cond) {
947 ASSERT(fields >= B16 && fields < B20); // at least one field set
948 Instr instr;
949 if (!src.rm_.is_valid()) {
950 // immediate
951 uint32_t rotate_imm;
952 uint32_t immed_8;
ager@chromium.org236ad962008-09-25 09:45:57 +0000953 if ((src.rmode_ != RelocInfo::NONE &&
954 src.rmode_ != RelocInfo::EXTERNAL_REFERENCE)||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 !fits_shifter(src.imm32_, &rotate_imm, &immed_8, NULL)) {
956 // immediate operand cannot be encoded, load it first to register ip
957 RecordRelocInfo(src.rmode_, src.imm32_);
958 ldr(ip, MemOperand(pc, 0), cond);
959 msr(fields, Operand(ip), cond);
960 return;
961 }
962 instr = I | rotate_imm*B8 | immed_8;
963 } else {
964 ASSERT(!src.rs_.is_valid() && src.shift_imm_ == 0); // only rm allowed
965 instr = src.rm_.code();
966 }
967 emit(cond | instr | B24 | B21 | fields | 15*B12);
968}
969
970
971// Load/Store instructions
972void Assembler::ldr(Register dst, const MemOperand& src, Condition cond) {
973 addrmod2(cond | B26 | L, dst, src);
mads.s.ager31e71382008-08-13 09:32:07 +0000974
975 // Eliminate pattern: push(r), pop(r)
976 // str(r, MemOperand(sp, 4, NegPreIndex), al)
977 // ldr(r, MemOperand(sp, 4, PostIndex), al)
978 // Both instructions can be eliminated.
979 int pattern_size = 2 * kInstrSize;
980 if (FLAG_push_pop_elimination &&
981 last_bound_pos_ <= (pc_offset() - pattern_size) &&
982 reloc_info_writer.last_pc() <= (pc_ - pattern_size) &&
983 // pattern
984 instr_at(pc_ - 1 * kInstrSize) == (kPopRegPattern | dst.code() * B12) &&
985 instr_at(pc_ - 2 * kInstrSize) == (kPushRegPattern | dst.code() * B12)) {
986 pc_ -= 2 * kInstrSize;
987 if (FLAG_print_push_pop_elimination) {
988 PrintF("%x push/pop (same reg) eliminated\n", pc_offset());
989 }
990 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991}
992
993
994void Assembler::str(Register src, const MemOperand& dst, Condition cond) {
995 addrmod2(cond | B26, src, dst);
mads.s.ager31e71382008-08-13 09:32:07 +0000996
997 // Eliminate pattern: pop(), push(r)
998 // add sp, sp, #4 LeaveCC, al; str r, [sp, #-4], al
999 // -> str r, [sp, 0], al
1000 int pattern_size = 2 * kInstrSize;
1001 if (FLAG_push_pop_elimination &&
1002 last_bound_pos_ <= (pc_offset() - pattern_size) &&
1003 reloc_info_writer.last_pc() <= (pc_ - pattern_size) &&
1004 instr_at(pc_ - 1 * kInstrSize) == (kPushRegPattern | src.code() * B12) &&
1005 instr_at(pc_ - 2 * kInstrSize) == kPopInstruction) {
1006 pc_ -= 2 * kInstrSize;
1007 emit(al | B26 | 0 | Offset | sp.code() * B16 | src.code() * B12);
1008 if (FLAG_print_push_pop_elimination) {
1009 PrintF("%x pop()/push(reg) eliminated\n", pc_offset());
1010 }
1011 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012}
1013
1014
1015void Assembler::ldrb(Register dst, const MemOperand& src, Condition cond) {
1016 addrmod2(cond | B26 | B | L, dst, src);
1017}
1018
1019
1020void Assembler::strb(Register src, const MemOperand& dst, Condition cond) {
1021 addrmod2(cond | B26 | B, src, dst);
1022}
1023
1024
1025void Assembler::ldrh(Register dst, const MemOperand& src, Condition cond) {
1026 addrmod3(cond | L | B7 | H | B4, dst, src);
1027}
1028
1029
1030void Assembler::strh(Register src, const MemOperand& dst, Condition cond) {
1031 addrmod3(cond | B7 | H | B4, src, dst);
1032}
1033
1034
1035void Assembler::ldrsb(Register dst, const MemOperand& src, Condition cond) {
1036 addrmod3(cond | L | B7 | S6 | B4, dst, src);
1037}
1038
1039
1040void Assembler::ldrsh(Register dst, const MemOperand& src, Condition cond) {
1041 addrmod3(cond | L | B7 | S6 | H | B4, dst, src);
1042}
1043
1044
1045// Load/Store multiple instructions
1046void Assembler::ldm(BlockAddrMode am,
1047 Register base,
1048 RegList dst,
1049 Condition cond) {
1050 // ABI stack constraint: ldmxx base, {..sp..} base != sp is not restartable
1051 ASSERT(base.is(sp) || (dst & sp.bit()) == 0);
1052
1053 addrmod4(cond | B27 | am | L, base, dst);
1054
1055 // emit the constant pool after a function return implemented by ldm ..{..pc}
1056 if (cond == al && (dst & pc.bit()) != 0) {
1057 // There is a slight chance that the ldm instruction was actually a call,
1058 // in which case it would be wrong to return into the constant pool; we
1059 // recognize this case by checking if the emission of the pool was blocked
1060 // at the pc of the ldm instruction by a mov lr, pc instruction; if this is
1061 // the case, we emit a jump over the pool.
1062 CheckConstPool(true, no_const_pool_before_ == pc_offset() - kInstrSize);
1063 }
1064}
1065
1066
1067void Assembler::stm(BlockAddrMode am,
1068 Register base,
1069 RegList src,
1070 Condition cond) {
1071 addrmod4(cond | B27 | am, base, src);
1072}
1073
1074
1075// Semaphore instructions
1076void Assembler::swp(Register dst, Register src, Register base, Condition cond) {
1077 ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc));
1078 ASSERT(!dst.is(base) && !src.is(base));
1079 emit(cond | P | base.code()*B16 | dst.code()*B12 |
1080 B7 | B4 | src.code());
1081}
1082
1083
1084void Assembler::swpb(Register dst,
1085 Register src,
1086 Register base,
1087 Condition cond) {
1088 ASSERT(!dst.is(pc) && !src.is(pc) && !base.is(pc));
1089 ASSERT(!dst.is(base) && !src.is(base));
1090 emit(cond | P | B | base.code()*B16 | dst.code()*B12 |
1091 B7 | B4 | src.code());
1092}
1093
1094
1095// Exception-generating instructions and debugging support
1096void Assembler::stop(const char* msg) {
kasper.lund7276f142008-07-30 08:49:36 +00001097#if !defined(__arm__)
1098 // The simulator handles these special instructions and stops execution.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 emit(15 << 28 | ((intptr_t) msg));
kasper.lund7276f142008-07-30 08:49:36 +00001100#else
1101 // Just issue a simple break instruction for now. Alternatively we could use
1102 // the swi(0x9f0001) instruction on Linux.
1103 bkpt(0);
1104#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105}
1106
1107
1108void Assembler::bkpt(uint32_t imm16) { // v5 and above
1109 ASSERT(is_uint16(imm16));
1110 emit(al | B24 | B21 | (imm16 >> 4)*B8 | 7*B4 | (imm16 & 0xf));
1111}
1112
1113
1114void Assembler::swi(uint32_t imm24, Condition cond) {
1115 ASSERT(is_uint24(imm24));
1116 emit(cond | 15*B24 | imm24);
1117}
1118
1119
1120// Coprocessor instructions
1121void Assembler::cdp(Coprocessor coproc,
1122 int opcode_1,
1123 CRegister crd,
1124 CRegister crn,
1125 CRegister crm,
1126 int opcode_2,
1127 Condition cond) {
1128 ASSERT(is_uint4(opcode_1) && is_uint3(opcode_2));
1129 emit(cond | B27 | B26 | B25 | (opcode_1 & 15)*B20 | crn.code()*B16 |
1130 crd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | crm.code());
1131}
1132
1133
1134void Assembler::cdp2(Coprocessor coproc,
1135 int opcode_1,
1136 CRegister crd,
1137 CRegister crn,
1138 CRegister crm,
1139 int opcode_2) { // v5 and above
1140 cdp(coproc, opcode_1, crd, crn, crm, opcode_2, static_cast<Condition>(nv));
1141}
1142
1143
1144void Assembler::mcr(Coprocessor coproc,
1145 int opcode_1,
1146 Register rd,
1147 CRegister crn,
1148 CRegister crm,
1149 int opcode_2,
1150 Condition cond) {
1151 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1152 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | crn.code()*B16 |
1153 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1154}
1155
1156
1157void Assembler::mcr2(Coprocessor coproc,
1158 int opcode_1,
1159 Register rd,
1160 CRegister crn,
1161 CRegister crm,
1162 int opcode_2) { // v5 and above
1163 mcr(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1164}
1165
1166
1167void Assembler::mrc(Coprocessor coproc,
1168 int opcode_1,
1169 Register rd,
1170 CRegister crn,
1171 CRegister crm,
1172 int opcode_2,
1173 Condition cond) {
1174 ASSERT(is_uint3(opcode_1) && is_uint3(opcode_2));
1175 emit(cond | B27 | B26 | B25 | (opcode_1 & 7)*B21 | L | crn.code()*B16 |
1176 rd.code()*B12 | coproc*B8 | (opcode_2 & 7)*B5 | B4 | crm.code());
1177}
1178
1179
1180void Assembler::mrc2(Coprocessor coproc,
1181 int opcode_1,
1182 Register rd,
1183 CRegister crn,
1184 CRegister crm,
1185 int opcode_2) { // v5 and above
1186 mrc(coproc, opcode_1, rd, crn, crm, opcode_2, static_cast<Condition>(nv));
1187}
1188
1189
1190void Assembler::ldc(Coprocessor coproc,
1191 CRegister crd,
1192 const MemOperand& src,
1193 LFlag l,
1194 Condition cond) {
1195 addrmod5(cond | B27 | B26 | l | L | coproc*B8, crd, src);
1196}
1197
1198
1199void Assembler::ldc(Coprocessor coproc,
1200 CRegister crd,
1201 Register rn,
1202 int option,
1203 LFlag l,
1204 Condition cond) {
1205 // unindexed addressing
1206 ASSERT(is_uint8(option));
1207 emit(cond | B27 | B26 | U | l | L | rn.code()*B16 | crd.code()*B12 |
1208 coproc*B8 | (option & 255));
1209}
1210
1211
1212void Assembler::ldc2(Coprocessor coproc,
1213 CRegister crd,
1214 const MemOperand& src,
1215 LFlag l) { // v5 and above
1216 ldc(coproc, crd, src, l, static_cast<Condition>(nv));
1217}
1218
1219
1220void Assembler::ldc2(Coprocessor coproc,
1221 CRegister crd,
1222 Register rn,
1223 int option,
1224 LFlag l) { // v5 and above
1225 ldc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1226}
1227
1228
1229void Assembler::stc(Coprocessor coproc,
1230 CRegister crd,
1231 const MemOperand& dst,
1232 LFlag l,
1233 Condition cond) {
1234 addrmod5(cond | B27 | B26 | l | coproc*B8, crd, dst);
1235}
1236
1237
1238void Assembler::stc(Coprocessor coproc,
1239 CRegister crd,
1240 Register rn,
1241 int option,
1242 LFlag l,
1243 Condition cond) {
1244 // unindexed addressing
1245 ASSERT(is_uint8(option));
1246 emit(cond | B27 | B26 | U | l | rn.code()*B16 | crd.code()*B12 |
1247 coproc*B8 | (option & 255));
1248}
1249
1250
1251void Assembler::stc2(Coprocessor
1252 coproc, CRegister crd,
1253 const MemOperand& dst,
1254 LFlag l) { // v5 and above
1255 stc(coproc, crd, dst, l, static_cast<Condition>(nv));
1256}
1257
1258
1259void Assembler::stc2(Coprocessor coproc,
1260 CRegister crd,
1261 Register rn,
1262 int option,
1263 LFlag l) { // v5 and above
1264 stc(coproc, crd, rn, option, l, static_cast<Condition>(nv));
1265}
1266
1267
1268// Pseudo instructions
1269void Assembler::lea(Register dst,
1270 const MemOperand& x,
1271 SBit s,
1272 Condition cond) {
1273 int am = x.am_;
1274 if (!x.rm_.is_valid()) {
1275 // immediate offset
1276 if ((am & P) == 0) // post indexing
1277 mov(dst, Operand(x.rn_), s, cond);
1278 else if ((am & U) == 0) // negative indexing
1279 sub(dst, x.rn_, Operand(x.offset_), s, cond);
1280 else
1281 add(dst, x.rn_, Operand(x.offset_), s, cond);
1282 } else {
1283 // Register offset (shift_imm_ and shift_op_ are 0) or scaled
1284 // register offset the constructors make sure than both shift_imm_
1285 // and shift_op_ are initialized.
1286 ASSERT(!x.rm_.is(pc));
1287 if ((am & P) == 0) // post indexing
1288 mov(dst, Operand(x.rn_), s, cond);
1289 else if ((am & U) == 0) // negative indexing
1290 sub(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond);
1291 else
1292 add(dst, x.rn_, Operand(x.rm_, x.shift_op_, x.shift_imm_), s, cond);
1293 }
1294}
1295
1296
1297// Debugging
1298void Assembler::RecordComment(const char* msg) {
1299 if (FLAG_debug_code) {
1300 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00001301 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 }
1303}
1304
1305
1306void Assembler::RecordPosition(int pos) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001307 if (pos == RelocInfo::kNoPosition) return;
1308 ASSERT(pos >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 if (pos == last_position_) return;
1310 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00001311 RecordRelocInfo(RelocInfo::POSITION, pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 last_position_ = pos;
1313 last_position_is_statement_ = false;
1314}
1315
1316
1317void Assembler::RecordStatementPosition(int pos) {
1318 if (pos == last_position_) return;
1319 CheckBuffer();
ager@chromium.org236ad962008-09-25 09:45:57 +00001320 RecordRelocInfo(RelocInfo::STATEMENT_POSITION, pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 last_position_ = pos;
1322 last_position_is_statement_ = true;
1323}
1324
1325
1326void Assembler::GrowBuffer() {
1327 if (!own_buffer_) FATAL("external code buffer is too small");
1328
1329 // compute new buffer size
1330 CodeDesc desc; // the new buffer
1331 if (buffer_size_ < 4*KB) {
1332 desc.buffer_size = 4*KB;
1333 } else if (buffer_size_ < 1*MB) {
1334 desc.buffer_size = 2*buffer_size_;
1335 } else {
1336 desc.buffer_size = buffer_size_ + 1*MB;
1337 }
1338 CHECK_GT(desc.buffer_size, 0); // no overflow
1339
1340 // setup new buffer
1341 desc.buffer = NewArray<byte>(desc.buffer_size);
1342
1343 desc.instr_size = pc_offset();
1344 desc.reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
1345
1346 // copy the data
1347 int pc_delta = desc.buffer - buffer_;
1348 int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
1349 memmove(desc.buffer, buffer_, desc.instr_size);
1350 memmove(reloc_info_writer.pos() + rc_delta,
1351 reloc_info_writer.pos(), desc.reloc_size);
1352
1353 // switch buffers
1354 DeleteArray(buffer_);
1355 buffer_ = desc.buffer;
1356 buffer_size_ = desc.buffer_size;
1357 pc_ += pc_delta;
1358 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
1359 reloc_info_writer.last_pc() + pc_delta);
1360
1361 // none of our relocation types are pc relative pointing outside the code
1362 // buffer nor pc absolute pointing inside the code buffer, so there is no need
1363 // to relocate any emitted relocation entries
1364
1365 // relocate pending relocation entries
1366 for (int i = 0; i < num_prinfo_; i++) {
1367 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00001368 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
1369 rinfo.rmode() != RelocInfo::POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 rinfo.set_pc(rinfo.pc() + pc_delta);
1371 }
1372}
1373
1374
ager@chromium.org236ad962008-09-25 09:45:57 +00001375void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 RelocInfo rinfo(pc_, rmode, data); // we do not try to reuse pool constants
ager@chromium.org236ad962008-09-25 09:45:57 +00001377 if (rmode >= RelocInfo::COMMENT && rmode <= RelocInfo::STATEMENT_POSITION) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 // adjust code for new modes
ager@chromium.org236ad962008-09-25 09:45:57 +00001379 ASSERT(RelocInfo::IsComment(rmode) || RelocInfo::IsPosition(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 // these modes do not need an entry in the constant pool
1381 } else {
1382 ASSERT(num_prinfo_ < kMaxNumPRInfo);
1383 prinfo_[num_prinfo_++] = rinfo;
1384 // Make sure the constant pool is not emitted in place of the next
1385 // instruction for which we just recorded relocation info
1386 BlockConstPoolBefore(pc_offset() + kInstrSize);
1387 }
ager@chromium.org236ad962008-09-25 09:45:57 +00001388 if (rinfo.rmode() != RelocInfo::NONE) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001389 // Don't record external references unless the heap will be serialized.
ager@chromium.org236ad962008-09-25 09:45:57 +00001390 if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001391 !Serializer::enabled() &&
1392 !FLAG_debug_code) {
1393 return;
1394 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 ASSERT(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
1396 reloc_info_writer.Write(&rinfo);
1397 }
1398}
1399
1400
1401void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
1402 // Calculate the offset of the next check. It will be overwritten
1403 // when a const pool is generated or when const pools are being
1404 // blocked for a specific range.
1405 next_buffer_check_ = pc_offset() + kCheckConstInterval;
1406
1407 // There is nothing to do if there are no pending relocation info entries
1408 if (num_prinfo_ == 0) return;
1409
1410 // We emit a constant pool at regular intervals of about kDistBetweenPools
1411 // or when requested by parameter force_emit (e.g. after each function).
1412 // We prefer not to emit a jump unless the max distance is reached or if we
1413 // are running low on slots, which can happen if a lot of constants are being
1414 // emitted (e.g. --debug-code and many static references).
1415 int dist = pc_offset() - last_const_pool_end_;
1416 if (!force_emit && dist < kMaxDistBetweenPools &&
1417 (require_jump || dist < kDistBetweenPools) &&
1418 // TODO(1236125): Cleanup the "magic" number below. We know that
1419 // the code generation will test every kCheckConstIntervalInst.
1420 // Thus we are safe as long as we generate less than 7 constant
1421 // entries per instruction.
1422 (num_prinfo_ < (kMaxNumPRInfo - (7 * kCheckConstIntervalInst)))) {
1423 return;
1424 }
1425
1426 // If we did not return by now, we need to emit the constant pool soon.
1427
1428 // However, some small sequences of instructions must not be broken up by the
1429 // insertion of a constant pool; such sequences are protected by setting
1430 // no_const_pool_before_, which is checked here. Also, recursive calls to
1431 // CheckConstPool are blocked by no_const_pool_before_.
1432 if (pc_offset() < no_const_pool_before_) {
1433 // Emission is currently blocked; make sure we try again as soon as possible
1434 next_buffer_check_ = no_const_pool_before_;
1435
1436 // Something is wrong if emission is forced and blocked at the same time
1437 ASSERT(!force_emit);
1438 return;
1439 }
1440
1441 int jump_instr = require_jump ? kInstrSize : 0;
1442
1443 // Check that the code buffer is large enough before emitting the constant
1444 // pool and relocation information (include the jump over the pool and the
1445 // constant pool marker).
1446 int max_needed_space =
1447 jump_instr + kInstrSize + num_prinfo_*(kInstrSize + kMaxRelocSize);
1448 while (buffer_space() <= (max_needed_space + kGap)) GrowBuffer();
1449
1450 // Block recursive calls to CheckConstPool
1451 BlockConstPoolBefore(pc_offset() + jump_instr + kInstrSize +
1452 num_prinfo_*kInstrSize);
1453 // Don't bother to check for the emit calls below.
1454 next_buffer_check_ = no_const_pool_before_;
1455
1456 // Emit jump over constant pool if necessary
1457 Label after_pool;
1458 if (require_jump) b(&after_pool);
1459
1460 RecordComment("[ Constant Pool");
1461
1462 // Put down constant pool marker
1463 // "Undefined instruction" as specified by A3.1 Instruction set encoding
1464 emit(0x03000000 | num_prinfo_);
1465
1466 // Emit constant pool entries
1467 for (int i = 0; i < num_prinfo_; i++) {
1468 RelocInfo& rinfo = prinfo_[i];
ager@chromium.org236ad962008-09-25 09:45:57 +00001469 ASSERT(rinfo.rmode() != RelocInfo::COMMENT &&
1470 rinfo.rmode() != RelocInfo::POSITION &&
1471 rinfo.rmode() != RelocInfo::STATEMENT_POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 Instr instr = instr_at(rinfo.pc());
1473 // Instruction to patch must be a ldr/str [pc, #offset]
1474 // P and U set, B and W clear, Rn == pc, offset12 still 0
1475 ASSERT((instr & (7*B25 | P | U | B | W | 15*B16 | Off12Mask)) ==
1476 (2*B25 | P | U | pc.code()*B16));
1477 int delta = pc_ - rinfo.pc() - 8;
1478 ASSERT(delta >= -4); // instr could be ldr pc, [pc, #-4] followed by targ32
1479 if (delta < 0) {
1480 instr &= ~U;
1481 delta = -delta;
1482 }
1483 ASSERT(is_uint12(delta));
1484 instr_at_put(rinfo.pc(), instr + delta);
1485 emit(rinfo.data());
1486 }
1487 num_prinfo_ = 0;
1488 last_const_pool_end_ = pc_offset();
1489
1490 RecordComment("]");
1491
1492 if (after_pool.is_linked()) {
1493 bind(&after_pool);
1494 }
1495
1496 // Since a constant pool was just emitted, move the check offset forward by
1497 // the standard interval.
1498 next_buffer_check_ = pc_offset() + kCheckConstInterval;
1499}
1500
1501
1502} } // namespace v8::internal