blob: a1b98f673dbbde56134819cd49e3e46ef6f35da1 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// All Rights Reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the
14// distribution.
15//
16// - Neither the name of Sun Microsystems or the names of contributors may
17// be used to endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31// OF THE POSSIBILITY OF SUCH DAMAGE.
32
Leon Clarked91b9f72010-01-27 17:25:45 +000033// The original source code covered by the above license above has been
34// modified significantly by Google Inc.
35// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37// A light-weight ARM Assembler
38// Generates user mode instructions for the ARM architecture up to version 5
39
40#ifndef V8_ARM_ASSEMBLER_ARM_H_
41#define V8_ARM_ASSEMBLER_ARM_H_
42#include <stdio.h>
43#include "assembler.h"
Steve Blockd0582a62009-12-15 09:54:21 +000044#include "serialize.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000045
46namespace v8 {
47namespace internal {
48
49// CPU Registers.
50//
51// 1) We would prefer to use an enum, but enum values are assignment-
52// compatible with int, which has caused code-generation bugs.
53//
54// 2) We would prefer to use a class instead of a struct but we don't like
55// the register initialization to depend on the particular initialization
56// order (which appears to be different on OS X, Linux, and Windows for the
57// installed versions of C++ we tried). Using a struct permits C-style
58// "initialization". Also, the Register objects cannot be const as this
59// forces initialization stubs in MSVC, making us dependent on initialization
60// order.
61//
62// 3) By not using an enum, we are possibly preventing the compiler from
63// doing certain constant folds, which may significantly reduce the
64// code generated for some assembly instructions (because they boil down
65// to a few constants). If this is a problem, we could change the code
66// such that we use an enum in optimized mode, and the struct in debug
67// mode. This way we get the compile-time error checking in debug mode
68// and best performance in optimized code.
69//
70// Core register
71struct Register {
72 bool is_valid() const { return 0 <= code_ && code_ < 16; }
73 bool is(Register reg) const { return code_ == reg.code_; }
74 int code() const {
75 ASSERT(is_valid());
76 return code_;
77 }
78 int bit() const {
79 ASSERT(is_valid());
80 return 1 << code_;
81 }
82
Leon Clarkef7060e22010-06-03 12:02:55 +010083 void set_code(int code) {
84 code_ = code;
85 ASSERT(is_valid());
86 }
87
Andrei Popescu31002712010-02-23 13:46:05 +000088 // Unfortunately we can't make this private in a struct.
Steve Blocka7e24c12009-10-30 11:49:00 +000089 int code_;
90};
91
Steve Block6ded16b2010-05-10 14:33:55 +010092const Register no_reg = { -1 };
Steve Blocka7e24c12009-10-30 11:49:00 +000093
Steve Block6ded16b2010-05-10 14:33:55 +010094const Register r0 = { 0 };
95const Register r1 = { 1 };
96const Register r2 = { 2 };
97const Register r3 = { 3 };
98const Register r4 = { 4 };
99const Register r5 = { 5 };
100const Register r6 = { 6 };
101const Register r7 = { 7 };
102const Register r8 = { 8 }; // Used as context register.
103const Register r9 = { 9 };
104const Register r10 = { 10 }; // Used as roots register.
105const Register fp = { 11 };
106const Register ip = { 12 };
107const Register sp = { 13 };
108const Register lr = { 14 };
109const Register pc = { 15 };
Steve Blockd0582a62009-12-15 09:54:21 +0000110
Leon Clarkee46be812010-01-19 14:06:41 +0000111// Single word VFP register.
112struct SwVfpRegister {
113 bool is_valid() const { return 0 <= code_ && code_ < 32; }
114 bool is(SwVfpRegister reg) const { return code_ == reg.code_; }
115 int code() const {
116 ASSERT(is_valid());
117 return code_;
118 }
119 int bit() const {
120 ASSERT(is_valid());
121 return 1 << code_;
122 }
123
124 int code_;
125};
126
127
128// Double word VFP register.
129struct DwVfpRegister {
130 // Supporting d0 to d15, can be later extended to d31.
131 bool is_valid() const { return 0 <= code_ && code_ < 16; }
132 bool is(DwVfpRegister reg) const { return code_ == reg.code_; }
133 int code() const {
134 ASSERT(is_valid());
135 return code_;
136 }
137 int bit() const {
138 ASSERT(is_valid());
139 return 1 << code_;
140 }
141
142 int code_;
143};
144
145
Steve Block6ded16b2010-05-10 14:33:55 +0100146// Support for the VFP registers s0 to s31 (d0 to d15).
Leon Clarkee46be812010-01-19 14:06:41 +0000147// Note that "s(N):s(N+1)" is the same as "d(N/2)".
Steve Block6ded16b2010-05-10 14:33:55 +0100148const SwVfpRegister s0 = { 0 };
149const SwVfpRegister s1 = { 1 };
150const SwVfpRegister s2 = { 2 };
151const SwVfpRegister s3 = { 3 };
152const SwVfpRegister s4 = { 4 };
153const SwVfpRegister s5 = { 5 };
154const SwVfpRegister s6 = { 6 };
155const SwVfpRegister s7 = { 7 };
156const SwVfpRegister s8 = { 8 };
157const SwVfpRegister s9 = { 9 };
158const SwVfpRegister s10 = { 10 };
159const SwVfpRegister s11 = { 11 };
160const SwVfpRegister s12 = { 12 };
161const SwVfpRegister s13 = { 13 };
162const SwVfpRegister s14 = { 14 };
163const SwVfpRegister s15 = { 15 };
164const SwVfpRegister s16 = { 16 };
165const SwVfpRegister s17 = { 17 };
166const SwVfpRegister s18 = { 18 };
167const SwVfpRegister s19 = { 19 };
168const SwVfpRegister s20 = { 20 };
169const SwVfpRegister s21 = { 21 };
170const SwVfpRegister s22 = { 22 };
171const SwVfpRegister s23 = { 23 };
172const SwVfpRegister s24 = { 24 };
173const SwVfpRegister s25 = { 25 };
174const SwVfpRegister s26 = { 26 };
175const SwVfpRegister s27 = { 27 };
176const SwVfpRegister s28 = { 28 };
177const SwVfpRegister s29 = { 29 };
178const SwVfpRegister s30 = { 30 };
179const SwVfpRegister s31 = { 31 };
Leon Clarkee46be812010-01-19 14:06:41 +0000180
Steve Block6ded16b2010-05-10 14:33:55 +0100181const DwVfpRegister d0 = { 0 };
182const DwVfpRegister d1 = { 1 };
183const DwVfpRegister d2 = { 2 };
184const DwVfpRegister d3 = { 3 };
185const DwVfpRegister d4 = { 4 };
186const DwVfpRegister d5 = { 5 };
187const DwVfpRegister d6 = { 6 };
188const DwVfpRegister d7 = { 7 };
189const DwVfpRegister d8 = { 8 };
190const DwVfpRegister d9 = { 9 };
191const DwVfpRegister d10 = { 10 };
192const DwVfpRegister d11 = { 11 };
193const DwVfpRegister d12 = { 12 };
194const DwVfpRegister d13 = { 13 };
195const DwVfpRegister d14 = { 14 };
196const DwVfpRegister d15 = { 15 };
Leon Clarkee46be812010-01-19 14:06:41 +0000197
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199// Coprocessor register
200struct CRegister {
201 bool is_valid() const { return 0 <= code_ && code_ < 16; }
202 bool is(CRegister creg) const { return code_ == creg.code_; }
203 int code() const {
204 ASSERT(is_valid());
205 return code_;
206 }
207 int bit() const {
208 ASSERT(is_valid());
209 return 1 << code_;
210 }
211
Andrei Popescu31002712010-02-23 13:46:05 +0000212 // Unfortunately we can't make this private in a struct.
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 int code_;
214};
215
216
Steve Block6ded16b2010-05-10 14:33:55 +0100217const CRegister no_creg = { -1 };
218
219const CRegister cr0 = { 0 };
220const CRegister cr1 = { 1 };
221const CRegister cr2 = { 2 };
222const CRegister cr3 = { 3 };
223const CRegister cr4 = { 4 };
224const CRegister cr5 = { 5 };
225const CRegister cr6 = { 6 };
226const CRegister cr7 = { 7 };
227const CRegister cr8 = { 8 };
228const CRegister cr9 = { 9 };
229const CRegister cr10 = { 10 };
230const CRegister cr11 = { 11 };
231const CRegister cr12 = { 12 };
232const CRegister cr13 = { 13 };
233const CRegister cr14 = { 14 };
234const CRegister cr15 = { 15 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000235
236
237// Coprocessor number
238enum Coprocessor {
239 p0 = 0,
240 p1 = 1,
241 p2 = 2,
242 p3 = 3,
243 p4 = 4,
244 p5 = 5,
245 p6 = 6,
246 p7 = 7,
247 p8 = 8,
248 p9 = 9,
249 p10 = 10,
250 p11 = 11,
251 p12 = 12,
252 p13 = 13,
253 p14 = 14,
254 p15 = 15
255};
256
257
Andrei Popescu31002712010-02-23 13:46:05 +0000258// Condition field in instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000259enum Condition {
260 eq = 0 << 28, // Z set equal.
261 ne = 1 << 28, // Z clear not equal.
262 nz = 1 << 28, // Z clear not zero.
263 cs = 2 << 28, // C set carry set.
264 hs = 2 << 28, // C set unsigned higher or same.
265 cc = 3 << 28, // C clear carry clear.
266 lo = 3 << 28, // C clear unsigned lower.
267 mi = 4 << 28, // N set negative.
268 pl = 5 << 28, // N clear positive or zero.
269 vs = 6 << 28, // V set overflow.
270 vc = 7 << 28, // V clear no overflow.
271 hi = 8 << 28, // C set, Z clear unsigned higher.
272 ls = 9 << 28, // C clear or Z set unsigned lower or same.
273 ge = 10 << 28, // N == V greater or equal.
274 lt = 11 << 28, // N != V less than.
275 gt = 12 << 28, // Z clear, N == V greater than.
276 le = 13 << 28, // Z set or N != V less then or equal
277 al = 14 << 28 // always.
278};
279
280
281// Returns the equivalent of !cc.
282INLINE(Condition NegateCondition(Condition cc));
283
284
285// Corresponds to transposing the operands of a comparison.
286inline Condition ReverseCondition(Condition cc) {
287 switch (cc) {
288 case lo:
289 return hi;
290 case hi:
291 return lo;
292 case hs:
293 return ls;
294 case ls:
295 return hs;
296 case lt:
297 return gt;
298 case gt:
299 return lt;
300 case ge:
301 return le;
302 case le:
303 return ge;
304 default:
305 return cc;
306 };
307}
308
309
310// Branch hints are not used on the ARM. They are defined so that they can
311// appear in shared function signatures, but will be ignored in ARM
312// implementations.
313enum Hint { no_hint };
314
315// Hints are not used on the arm. Negating is trivial.
316inline Hint NegateHint(Hint ignored) { return no_hint; }
317
318
319// -----------------------------------------------------------------------------
320// Addressing modes and instruction variants
321
322// Shifter operand shift operation
323enum ShiftOp {
324 LSL = 0 << 5,
325 LSR = 1 << 5,
326 ASR = 2 << 5,
327 ROR = 3 << 5,
328 RRX = -1
329};
330
331
332// Condition code updating mode
333enum SBit {
334 SetCC = 1 << 20, // set condition code
335 LeaveCC = 0 << 20 // leave condition code unchanged
336};
337
338
339// Status register selection
340enum SRegister {
341 CPSR = 0 << 22,
342 SPSR = 1 << 22
343};
344
345
346// Status register fields
347enum SRegisterField {
348 CPSR_c = CPSR | 1 << 16,
349 CPSR_x = CPSR | 1 << 17,
350 CPSR_s = CPSR | 1 << 18,
351 CPSR_f = CPSR | 1 << 19,
352 SPSR_c = SPSR | 1 << 16,
353 SPSR_x = SPSR | 1 << 17,
354 SPSR_s = SPSR | 1 << 18,
355 SPSR_f = SPSR | 1 << 19
356};
357
358// Status register field mask (or'ed SRegisterField enum values)
359typedef uint32_t SRegisterFieldMask;
360
361
362// Memory operand addressing mode
363enum AddrMode {
364 // bit encoding P U W
365 Offset = (8|4|0) << 21, // offset (without writeback to base)
366 PreIndex = (8|4|1) << 21, // pre-indexed addressing with writeback
367 PostIndex = (0|4|0) << 21, // post-indexed addressing with writeback
368 NegOffset = (8|0|0) << 21, // negative offset (without writeback to base)
369 NegPreIndex = (8|0|1) << 21, // negative pre-indexed with writeback
370 NegPostIndex = (0|0|0) << 21 // negative post-indexed with writeback
371};
372
373
374// Load/store multiple addressing mode
375enum BlockAddrMode {
376 // bit encoding P U W
377 da = (0|0|0) << 21, // decrement after
378 ia = (0|4|0) << 21, // increment after
379 db = (8|0|0) << 21, // decrement before
380 ib = (8|4|0) << 21, // increment before
381 da_w = (0|0|1) << 21, // decrement after with writeback to base
382 ia_w = (0|4|1) << 21, // increment after with writeback to base
383 db_w = (8|0|1) << 21, // decrement before with writeback to base
384 ib_w = (8|4|1) << 21 // increment before with writeback to base
385};
386
387
388// Coprocessor load/store operand size
389enum LFlag {
390 Long = 1 << 22, // long load/store coprocessor
391 Short = 0 << 22 // short load/store coprocessor
392};
393
394
395// -----------------------------------------------------------------------------
396// Machine instruction Operands
397
398// Class Operand represents a shifter operand in data processing instructions
399class Operand BASE_EMBEDDED {
400 public:
401 // immediate
402 INLINE(explicit Operand(int32_t immediate,
403 RelocInfo::Mode rmode = RelocInfo::NONE));
404 INLINE(explicit Operand(const ExternalReference& f));
405 INLINE(explicit Operand(const char* s));
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 explicit Operand(Handle<Object> handle);
407 INLINE(explicit Operand(Smi* value));
408
409 // rm
410 INLINE(explicit Operand(Register rm));
411
412 // rm <shift_op> shift_imm
413 explicit Operand(Register rm, ShiftOp shift_op, int shift_imm);
414
415 // rm <shift_op> rs
416 explicit Operand(Register rm, ShiftOp shift_op, Register rs);
417
418 // Return true if this is a register operand.
419 INLINE(bool is_reg() const);
420
421 Register rm() const { return rm_; }
422
423 private:
424 Register rm_;
425 Register rs_;
426 ShiftOp shift_op_;
427 int shift_imm_; // valid if rm_ != no_reg && rs_ == no_reg
428 int32_t imm32_; // valid if rm_ == no_reg
429 RelocInfo::Mode rmode_;
430
431 friend class Assembler;
432};
433
434
435// Class MemOperand represents a memory operand in load and store instructions
436class MemOperand BASE_EMBEDDED {
437 public:
438 // [rn +/- offset] Offset/NegOffset
439 // [rn +/- offset]! PreIndex/NegPreIndex
440 // [rn], +/- offset PostIndex/NegPostIndex
441 // offset is any signed 32-bit value; offset is first loaded to register ip if
442 // it does not fit the addressing mode (12-bit unsigned and sign bit)
443 explicit MemOperand(Register rn, int32_t offset = 0, AddrMode am = Offset);
444
445 // [rn +/- rm] Offset/NegOffset
446 // [rn +/- rm]! PreIndex/NegPreIndex
447 // [rn], +/- rm PostIndex/NegPostIndex
448 explicit MemOperand(Register rn, Register rm, AddrMode am = Offset);
449
450 // [rn +/- rm <shift_op> shift_imm] Offset/NegOffset
451 // [rn +/- rm <shift_op> shift_imm]! PreIndex/NegPreIndex
452 // [rn], +/- rm <shift_op> shift_imm PostIndex/NegPostIndex
453 explicit MemOperand(Register rn, Register rm,
454 ShiftOp shift_op, int shift_imm, AddrMode am = Offset);
455
Kristian Monsen25f61362010-05-21 11:50:48 +0100456 void set_offset(int32_t offset) {
457 ASSERT(rm_.is(no_reg));
458 offset_ = offset;
459 }
460
461 uint32_t offset() {
462 ASSERT(rm_.is(no_reg));
463 return offset_;
464 }
465
Leon Clarkef7060e22010-06-03 12:02:55 +0100466 Register rn() const { return rn_; }
467 Register rm() const { return rm_; }
Kristian Monsen25f61362010-05-21 11:50:48 +0100468
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 private:
470 Register rn_; // base
471 Register rm_; // register offset
472 int32_t offset_; // valid if rm_ == no_reg
473 ShiftOp shift_op_;
474 int shift_imm_; // valid if rm_ != no_reg && rs_ == no_reg
475 AddrMode am_; // bits P, U, and W
476
477 friend class Assembler;
478};
479
Steve Blockd0582a62009-12-15 09:54:21 +0000480// CpuFeatures keeps track of which features are supported by the target CPU.
481// Supported features must be enabled by a Scope before use.
482class CpuFeatures : public AllStatic {
483 public:
484 // Detect features of the target CPU. Set safe defaults if the serializer
485 // is enabled (snapshots must be portable).
486 static void Probe();
487
488 // Check whether a feature is supported by the target CPU.
489 static bool IsSupported(CpuFeature f) {
490 if (f == VFP3 && !FLAG_enable_vfp3) return false;
491 return (supported_ & (1u << f)) != 0;
492 }
493
494 // Check whether a feature is currently enabled.
495 static bool IsEnabled(CpuFeature f) {
496 return (enabled_ & (1u << f)) != 0;
497 }
498
499 // Enable a specified feature within a scope.
500 class Scope BASE_EMBEDDED {
501#ifdef DEBUG
502 public:
503 explicit Scope(CpuFeature f) {
504 ASSERT(CpuFeatures::IsSupported(f));
505 ASSERT(!Serializer::enabled() ||
506 (found_by_runtime_probing_ & (1u << f)) == 0);
507 old_enabled_ = CpuFeatures::enabled_;
508 CpuFeatures::enabled_ |= 1u << f;
509 }
510 ~Scope() { CpuFeatures::enabled_ = old_enabled_; }
511 private:
512 unsigned old_enabled_;
513#else
514 public:
515 explicit Scope(CpuFeature f) {}
516#endif
517 };
518
519 private:
520 static unsigned supported_;
521 static unsigned enabled_;
522 static unsigned found_by_runtime_probing_;
523};
524
Steve Blocka7e24c12009-10-30 11:49:00 +0000525
526typedef int32_t Instr;
527
528
529extern const Instr kMovLrPc;
Steve Block6ded16b2010-05-10 14:33:55 +0100530extern const Instr kLdrPCMask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000531extern const Instr kLdrPCPattern;
Steve Block6ded16b2010-05-10 14:33:55 +0100532extern const Instr kBlxRegMask;
533extern const Instr kBlxRegPattern;
Steve Blocka7e24c12009-10-30 11:49:00 +0000534
535
536class Assembler : public Malloced {
537 public:
538 // Create an assembler. Instructions and relocation information are emitted
539 // into a buffer, with the instructions starting from the beginning and the
540 // relocation information starting from the end of the buffer. See CodeDesc
541 // for a detailed comment on the layout (globals.h).
542 //
543 // If the provided buffer is NULL, the assembler allocates and grows its own
544 // buffer, and buffer_size determines the initial buffer size. The buffer is
545 // owned by the assembler and deallocated upon destruction of the assembler.
546 //
547 // If the provided buffer is not NULL, the assembler uses the provided buffer
548 // for code generation and assumes its size to be buffer_size. If the buffer
549 // is too small, a fatal error occurs. No deallocation of the buffer is done
550 // upon destruction of the assembler.
551 Assembler(void* buffer, int buffer_size);
552 ~Assembler();
553
554 // GetCode emits any pending (non-emitted) code and fills the descriptor
555 // desc. GetCode() is idempotent; it returns the same result if no other
556 // Assembler functions are invoked in between GetCode() calls.
557 void GetCode(CodeDesc* desc);
558
559 // Label operations & relative jumps (PPUM Appendix D)
560 //
561 // Takes a branch opcode (cc) and a label (L) and generates
562 // either a backward branch or a forward branch and links it
563 // to the label fixup chain. Usage:
564 //
565 // Label L; // unbound label
566 // j(cc, &L); // forward branch to unbound label
567 // bind(&L); // bind label to the current pc
568 // j(cc, &L); // backward branch to bound label
569 // bind(&L); // illegal: a label may be bound only once
570 //
571 // Note: The same Label can be used for forward and backward branches
572 // but it may be bound only once.
573
574 void bind(Label* L); // binds an unbound label L to the current code position
575
576 // Returns the branch offset to the given label from the current code position
577 // Links the label to the current position if it is still unbound
578 // Manages the jump elimination optimization if the second parameter is true.
579 int branch_offset(Label* L, bool jump_elimination_allowed);
580
581 // Puts a labels target address at the given position.
582 // The high 8 bits are set to zero.
583 void label_at_put(Label* L, int at_offset);
584
585 // Return the address in the constant pool of the code target address used by
586 // the branch/call instruction at pc.
587 INLINE(static Address target_address_address_at(Address pc));
588
589 // Read/Modify the code target address in the branch/call instruction at pc.
590 INLINE(static Address target_address_at(Address pc));
591 INLINE(static void set_target_address_at(Address pc, Address target));
592
Steve Blockd0582a62009-12-15 09:54:21 +0000593 // This sets the branch destination (which is in the constant pool on ARM).
594 // This is for calls and branches within generated code.
595 inline static void set_target_at(Address constant_pool_entry, Address target);
596
597 // This sets the branch destination (which is in the constant pool on ARM).
598 // This is for calls and branches to runtime code.
599 inline static void set_external_target_at(Address constant_pool_entry,
600 Address target) {
601 set_target_at(constant_pool_entry, target);
602 }
603
604 // Here we are patching the address in the constant pool, not the actual call
605 // instruction. The address in the constant pool is the same size as a
606 // pointer.
607 static const int kCallTargetSize = kPointerSize;
608 static const int kExternalTargetSize = kPointerSize;
609
Steve Blocka7e24c12009-10-30 11:49:00 +0000610 // Size of an instruction.
611 static const int kInstrSize = sizeof(Instr);
612
613 // Distance between the instruction referring to the address of the call
Steve Block6ded16b2010-05-10 14:33:55 +0100614 // target and the return address.
615#ifdef USE_BLX
616 // Call sequence is:
617 // ldr ip, [pc, #...] @ call address
618 // blx ip
619 // @ return address
620 static const int kCallTargetAddressOffset = 2 * kInstrSize;
621#else
622 // Call sequence is:
623 // mov lr, pc
624 // ldr pc, [pc, #...] @ call address
625 // @ return address
Steve Blocka7e24c12009-10-30 11:49:00 +0000626 static const int kCallTargetAddressOffset = kInstrSize;
Steve Block6ded16b2010-05-10 14:33:55 +0100627#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000628
629 // Distance between start of patched return sequence and the emitted address
630 // to jump to.
Steve Block6ded16b2010-05-10 14:33:55 +0100631#ifdef USE_BLX
632 // Return sequence is:
633 // ldr ip, [pc, #0] @ emited address and start
634 // blx ip
635 static const int kPatchReturnSequenceAddressOffset = 0 * kInstrSize;
636#else
637 // Return sequence is:
638 // mov lr, pc @ start of sequence
639 // ldr pc, [pc, #-4] @ emited address
640 static const int kPatchReturnSequenceAddressOffset = kInstrSize;
641#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000642
643 // Difference between address of current opcode and value read from pc
644 // register.
645 static const int kPcLoadDelta = 8;
646
Steve Blockd0582a62009-12-15 09:54:21 +0000647 static const int kJSReturnSequenceLength = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +0000648
649 // ---------------------------------------------------------------------------
650 // Code generation
651
652 // Insert the smallest number of nop instructions
653 // possible to align the pc offset to a multiple
654 // of m. m must be a power of 2 (>= 4).
655 void Align(int m);
656
657 // Branch instructions
658 void b(int branch_offset, Condition cond = al);
659 void bl(int branch_offset, Condition cond = al);
660 void blx(int branch_offset); // v5 and above
661 void blx(Register target, Condition cond = al); // v5 and above
662 void bx(Register target, Condition cond = al); // v5 and above, plus v4t
663
664 // Convenience branch instructions using labels
665 void b(Label* L, Condition cond = al) {
666 b(branch_offset(L, cond == al), cond);
667 }
668 void b(Condition cond, Label* L) { b(branch_offset(L, cond == al), cond); }
669 void bl(Label* L, Condition cond = al) { bl(branch_offset(L, false), cond); }
670 void bl(Condition cond, Label* L) { bl(branch_offset(L, false), cond); }
671 void blx(Label* L) { blx(branch_offset(L, false)); } // v5 and above
672
673 // Data-processing instructions
Andrei Popescu31002712010-02-23 13:46:05 +0000674 void ubfx(Register dst, Register src1, const Operand& src2,
675 const Operand& src3, Condition cond = al);
676
Steve Blocka7e24c12009-10-30 11:49:00 +0000677 void and_(Register dst, Register src1, const Operand& src2,
678 SBit s = LeaveCC, Condition cond = al);
679
680 void eor(Register dst, Register src1, const Operand& src2,
681 SBit s = LeaveCC, Condition cond = al);
682
683 void sub(Register dst, Register src1, const Operand& src2,
684 SBit s = LeaveCC, Condition cond = al);
685 void sub(Register dst, Register src1, Register src2,
686 SBit s = LeaveCC, Condition cond = al) {
687 sub(dst, src1, Operand(src2), s, cond);
688 }
689
690 void rsb(Register dst, Register src1, const Operand& src2,
691 SBit s = LeaveCC, Condition cond = al);
692
693 void add(Register dst, Register src1, const Operand& src2,
694 SBit s = LeaveCC, Condition cond = al);
695
696 void adc(Register dst, Register src1, const Operand& src2,
697 SBit s = LeaveCC, Condition cond = al);
698
699 void sbc(Register dst, Register src1, const Operand& src2,
700 SBit s = LeaveCC, Condition cond = al);
701
702 void rsc(Register dst, Register src1, const Operand& src2,
703 SBit s = LeaveCC, Condition cond = al);
704
705 void tst(Register src1, const Operand& src2, Condition cond = al);
706 void tst(Register src1, Register src2, Condition cond = al) {
707 tst(src1, Operand(src2), cond);
708 }
709
710 void teq(Register src1, const Operand& src2, Condition cond = al);
711
712 void cmp(Register src1, const Operand& src2, Condition cond = al);
713 void cmp(Register src1, Register src2, Condition cond = al) {
714 cmp(src1, Operand(src2), cond);
715 }
716
717 void cmn(Register src1, const Operand& src2, Condition cond = al);
718
719 void orr(Register dst, Register src1, const Operand& src2,
720 SBit s = LeaveCC, Condition cond = al);
721 void orr(Register dst, Register src1, Register src2,
722 SBit s = LeaveCC, Condition cond = al) {
723 orr(dst, src1, Operand(src2), s, cond);
724 }
725
726 void mov(Register dst, const Operand& src,
727 SBit s = LeaveCC, Condition cond = al);
728 void mov(Register dst, Register src, SBit s = LeaveCC, Condition cond = al) {
729 mov(dst, Operand(src), s, cond);
730 }
731
732 void bic(Register dst, Register src1, const Operand& src2,
733 SBit s = LeaveCC, Condition cond = al);
734
735 void mvn(Register dst, const Operand& src,
736 SBit s = LeaveCC, Condition cond = al);
737
738 // Multiply instructions
739
740 void mla(Register dst, Register src1, Register src2, Register srcA,
741 SBit s = LeaveCC, Condition cond = al);
742
743 void mul(Register dst, Register src1, Register src2,
744 SBit s = LeaveCC, Condition cond = al);
745
746 void smlal(Register dstL, Register dstH, Register src1, Register src2,
747 SBit s = LeaveCC, Condition cond = al);
748
749 void smull(Register dstL, Register dstH, Register src1, Register src2,
750 SBit s = LeaveCC, Condition cond = al);
751
752 void umlal(Register dstL, Register dstH, Register src1, Register src2,
753 SBit s = LeaveCC, Condition cond = al);
754
755 void umull(Register dstL, Register dstH, Register src1, Register src2,
756 SBit s = LeaveCC, Condition cond = al);
757
758 // Miscellaneous arithmetic instructions
759
760 void clz(Register dst, Register src, Condition cond = al); // v5 and above
761
762 // Status register access instructions
763
764 void mrs(Register dst, SRegister s, Condition cond = al);
765 void msr(SRegisterFieldMask fields, const Operand& src, Condition cond = al);
766
767 // Load/Store instructions
768 void ldr(Register dst, const MemOperand& src, Condition cond = al);
769 void str(Register src, const MemOperand& dst, Condition cond = al);
770 void ldrb(Register dst, const MemOperand& src, Condition cond = al);
771 void strb(Register src, const MemOperand& dst, Condition cond = al);
772 void ldrh(Register dst, const MemOperand& src, Condition cond = al);
773 void strh(Register src, const MemOperand& dst, Condition cond = al);
774 void ldrsb(Register dst, const MemOperand& src, Condition cond = al);
775 void ldrsh(Register dst, const MemOperand& src, Condition cond = al);
Leon Clarkef7060e22010-06-03 12:02:55 +0100776 void ldrd(Register dst1,
777 Register dst2,
778 const MemOperand& src, Condition cond = al);
779 void strd(Register src1,
780 Register src2,
781 const MemOperand& dst, Condition cond = al);
Steve Blocka7e24c12009-10-30 11:49:00 +0000782
783 // Load/Store multiple instructions
784 void ldm(BlockAddrMode am, Register base, RegList dst, Condition cond = al);
785 void stm(BlockAddrMode am, Register base, RegList src, Condition cond = al);
786
Steve Blocka7e24c12009-10-30 11:49:00 +0000787 // Exception-generating instructions and debugging support
788 void stop(const char* msg);
789
790 void bkpt(uint32_t imm16); // v5 and above
791 void swi(uint32_t imm24, Condition cond = al);
792
793 // Coprocessor instructions
794
795 void cdp(Coprocessor coproc, int opcode_1,
796 CRegister crd, CRegister crn, CRegister crm,
797 int opcode_2, Condition cond = al);
798
799 void cdp2(Coprocessor coproc, int opcode_1,
800 CRegister crd, CRegister crn, CRegister crm,
801 int opcode_2); // v5 and above
802
803 void mcr(Coprocessor coproc, int opcode_1,
804 Register rd, CRegister crn, CRegister crm,
805 int opcode_2 = 0, Condition cond = al);
806
807 void mcr2(Coprocessor coproc, int opcode_1,
808 Register rd, CRegister crn, CRegister crm,
809 int opcode_2 = 0); // v5 and above
810
811 void mrc(Coprocessor coproc, int opcode_1,
812 Register rd, CRegister crn, CRegister crm,
813 int opcode_2 = 0, Condition cond = al);
814
815 void mrc2(Coprocessor coproc, int opcode_1,
816 Register rd, CRegister crn, CRegister crm,
817 int opcode_2 = 0); // v5 and above
818
819 void ldc(Coprocessor coproc, CRegister crd, const MemOperand& src,
820 LFlag l = Short, Condition cond = al);
821 void ldc(Coprocessor coproc, CRegister crd, Register base, int option,
822 LFlag l = Short, Condition cond = al);
823
824 void ldc2(Coprocessor coproc, CRegister crd, const MemOperand& src,
825 LFlag l = Short); // v5 and above
826 void ldc2(Coprocessor coproc, CRegister crd, Register base, int option,
827 LFlag l = Short); // v5 and above
828
829 void stc(Coprocessor coproc, CRegister crd, const MemOperand& dst,
830 LFlag l = Short, Condition cond = al);
831 void stc(Coprocessor coproc, CRegister crd, Register base, int option,
832 LFlag l = Short, Condition cond = al);
833
834 void stc2(Coprocessor coproc, CRegister crd, const MemOperand& dst,
835 LFlag l = Short); // v5 and above
836 void stc2(Coprocessor coproc, CRegister crd, Register base, int option,
837 LFlag l = Short); // v5 and above
838
Steve Blockd0582a62009-12-15 09:54:21 +0000839 // Support for VFP.
840 // All these APIs support S0 to S31 and D0 to D15.
841 // Currently these APIs do not support extended D registers, i.e, D16 to D31.
842 // However, some simple modifications can allow
843 // these APIs to support D16 to D31.
844
Leon Clarked91b9f72010-01-27 17:25:45 +0000845 void vldr(const DwVfpRegister dst,
846 const Register base,
847 int offset, // Offset must be a multiple of 4.
848 const Condition cond = al);
Steve Block6ded16b2010-05-10 14:33:55 +0100849
850 void vldr(const SwVfpRegister dst,
851 const Register base,
852 int offset, // Offset must be a multiple of 4.
853 const Condition cond = al);
854
Leon Clarked91b9f72010-01-27 17:25:45 +0000855 void vstr(const DwVfpRegister src,
856 const Register base,
857 int offset, // Offset must be a multiple of 4.
858 const Condition cond = al);
Leon Clarkee46be812010-01-19 14:06:41 +0000859 void vmov(const DwVfpRegister dst,
860 const Register src1,
Steve Blockd0582a62009-12-15 09:54:21 +0000861 const Register src2,
Leon Clarkee46be812010-01-19 14:06:41 +0000862 const Condition cond = al);
863 void vmov(const Register dst1,
864 const Register dst2,
865 const DwVfpRegister src,
866 const Condition cond = al);
867 void vmov(const SwVfpRegister dst,
868 const Register src,
869 const Condition cond = al);
870 void vmov(const Register dst,
871 const SwVfpRegister src,
872 const Condition cond = al);
Steve Block6ded16b2010-05-10 14:33:55 +0100873 void vcvt_f64_s32(const DwVfpRegister dst,
874 const SwVfpRegister src,
875 const Condition cond = al);
876 void vcvt_f32_s32(const SwVfpRegister dst,
877 const SwVfpRegister src,
878 const Condition cond = al);
879 void vcvt_f64_u32(const DwVfpRegister dst,
880 const SwVfpRegister src,
881 const Condition cond = al);
882 void vcvt_s32_f64(const SwVfpRegister dst,
883 const DwVfpRegister src,
884 const Condition cond = al);
885 void vcvt_u32_f64(const SwVfpRegister dst,
886 const DwVfpRegister src,
887 const Condition cond = al);
888 void vcvt_f64_f32(const DwVfpRegister dst,
889 const SwVfpRegister src,
890 const Condition cond = al);
891 void vcvt_f32_f64(const SwVfpRegister dst,
892 const DwVfpRegister src,
893 const Condition cond = al);
Leon Clarkee46be812010-01-19 14:06:41 +0000894
895 void vadd(const DwVfpRegister dst,
896 const DwVfpRegister src1,
897 const DwVfpRegister src2,
898 const Condition cond = al);
899 void vsub(const DwVfpRegister dst,
900 const DwVfpRegister src1,
901 const DwVfpRegister src2,
902 const Condition cond = al);
903 void vmul(const DwVfpRegister dst,
904 const DwVfpRegister src1,
905 const DwVfpRegister src2,
906 const Condition cond = al);
907 void vdiv(const DwVfpRegister dst,
908 const DwVfpRegister src1,
909 const DwVfpRegister src2,
910 const Condition cond = al);
911 void vcmp(const DwVfpRegister src1,
912 const DwVfpRegister src2,
Steve Blockd0582a62009-12-15 09:54:21 +0000913 const SBit s = LeaveCC,
914 const Condition cond = al);
915 void vmrs(const Register dst,
916 const Condition cond = al);
917
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 // Pseudo instructions
Steve Block6ded16b2010-05-10 14:33:55 +0100919 void nop(int type = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000920
921 void push(Register src, Condition cond = al) {
922 str(src, MemOperand(sp, 4, NegPreIndex), cond);
923 }
924
925 void pop(Register dst, Condition cond = al) {
926 ldr(dst, MemOperand(sp, 4, PostIndex), cond);
927 }
928
929 void pop() {
930 add(sp, sp, Operand(kPointerSize));
931 }
932
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 // Jump unconditionally to given label.
934 void jmp(Label* L) { b(L, al); }
935
936 // Check the code size generated from label to here.
937 int InstructionsGeneratedSince(Label* l) {
938 return (pc_offset() - l->pos()) / kInstrSize;
939 }
940
Steve Blockd0582a62009-12-15 09:54:21 +0000941 // Check whether an immediate fits an addressing mode 1 instruction.
942 bool ImmediateFitsAddrMode1Instruction(int32_t imm32);
943
Steve Block6ded16b2010-05-10 14:33:55 +0100944 // Class for scoping postponing the constant pool generation.
945 class BlockConstPoolScope {
946 public:
947 explicit BlockConstPoolScope(Assembler* assem) : assem_(assem) {
948 assem_->StartBlockConstPool();
949 }
950 ~BlockConstPoolScope() {
951 assem_->EndBlockConstPool();
952 }
953
954 private:
955 Assembler* assem_;
956
957 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockConstPoolScope);
958 };
959
Steve Blockd0582a62009-12-15 09:54:21 +0000960 // Postpone the generation of the constant pool for the specified number of
961 // instructions.
962 void BlockConstPoolFor(int instructions);
963
Steve Blocka7e24c12009-10-30 11:49:00 +0000964 // Debugging
965
966 // Mark address of the ExitJSFrame code.
967 void RecordJSReturn();
968
969 // Record a comment relocation entry that can be used by a disassembler.
970 // Use --debug_code to enable.
971 void RecordComment(const char* msg);
972
973 void RecordPosition(int pos);
974 void RecordStatementPosition(int pos);
975 void WriteRecordedPositions();
976
977 int pc_offset() const { return pc_ - buffer_; }
978 int current_position() const { return current_position_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100979 int current_statement_position() const { return current_statement_position_; }
980
Leon Clarkef7060e22010-06-03 12:02:55 +0100981 bool can_peephole_optimize(int instructions) {
982 if (!FLAG_peephole_optimization) return false;
983 if (last_bound_pos_ > pc_offset() - instructions * kInstrSize) return false;
984 return reloc_info_writer.last_pc() <= pc_ - instructions * kInstrSize;
985 }
986
Steve Block6ded16b2010-05-10 14:33:55 +0100987 // Read/patch instructions
988 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
989 static void instr_at_put(byte* pc, Instr instr) {
990 *reinterpret_cast<Instr*>(pc) = instr;
991 }
992 static bool IsNop(Instr instr, int type = 0);
993 static bool IsBranch(Instr instr);
994 static int GetBranchOffset(Instr instr);
995 static bool IsLdrRegisterImmediate(Instr instr);
996 static int GetLdrRegisterImmediateOffset(Instr instr);
997 static Instr SetLdrRegisterImmediateOffset(Instr instr, int offset);
Leon Clarkef7060e22010-06-03 12:02:55 +0100998 static Register GetRd(Instr instr);
999 static bool IsPush(Instr instr);
1000 static bool IsPop(Instr instr);
1001 static bool IsStrRegFpOffset(Instr instr);
1002 static bool IsLdrRegFpOffset(Instr instr);
1003 static bool IsStrRegFpNegOffset(Instr instr);
1004 static bool IsLdrRegFpNegOffset(Instr instr);
Steve Block6ded16b2010-05-10 14:33:55 +01001005
Steve Blocka7e24c12009-10-30 11:49:00 +00001006
1007 protected:
1008 int buffer_space() const { return reloc_info_writer.pos() - pc_; }
1009
1010 // Read/patch instructions
Steve Blocka7e24c12009-10-30 11:49:00 +00001011 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
1012 void instr_at_put(int pos, Instr instr) {
1013 *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
1014 }
1015
1016 // Decode branch instruction at pos and return branch target pos
1017 int target_at(int pos);
1018
1019 // Patch branch instruction at pos to branch to given branch target pos
1020 void target_at_put(int pos, int target_pos);
1021
1022 // Check if is time to emit a constant pool for pending reloc info entries
1023 void CheckConstPool(bool force_emit, bool require_jump);
1024
1025 // Block the emission of the constant pool before pc_offset
1026 void BlockConstPoolBefore(int pc_offset) {
1027 if (no_const_pool_before_ < pc_offset) no_const_pool_before_ = pc_offset;
1028 }
1029
Steve Block6ded16b2010-05-10 14:33:55 +01001030 void StartBlockConstPool() {
1031 const_pool_blocked_nesting_++;
1032 }
1033 void EndBlockConstPool() {
1034 const_pool_blocked_nesting_--;
1035 }
1036
Steve Blocka7e24c12009-10-30 11:49:00 +00001037 private:
1038 // Code buffer:
1039 // The buffer into which code and relocation info are generated.
1040 byte* buffer_;
1041 int buffer_size_;
1042 // True if the assembler owns the buffer, false if buffer is external.
1043 bool own_buffer_;
1044
1045 // Buffer size and constant pool distance are checked together at regular
1046 // intervals of kBufferCheckInterval emitted bytes
1047 static const int kBufferCheckInterval = 1*KB/2;
1048 int next_buffer_check_; // pc offset of next buffer check
1049
1050 // Code generation
1051 // The relocation writer's position is at least kGap bytes below the end of
1052 // the generated instructions. This is so that multi-instruction sequences do
1053 // not have to check for overflow. The same is true for writes of large
1054 // relocation info entries.
1055 static const int kGap = 32;
1056 byte* pc_; // the program counter; moves forward
1057
1058 // Constant pool generation
1059 // Pools are emitted in the instruction stream, preferably after unconditional
1060 // jumps or after returns from functions (in dead code locations).
1061 // If a long code sequence does not contain unconditional jumps, it is
1062 // necessary to emit the constant pool before the pool gets too far from the
1063 // location it is accessed from. In this case, we emit a jump over the emitted
1064 // constant pool.
1065 // Constants in the pool may be addresses of functions that gets relocated;
1066 // if so, a relocation info entry is associated to the constant pool entry.
1067
1068 // Repeated checking whether the constant pool should be emitted is rather
1069 // expensive. By default we only check again once a number of instructions
1070 // has been generated. That also means that the sizing of the buffers is not
1071 // an exact science, and that we rely on some slop to not overrun buffers.
1072 static const int kCheckConstIntervalInst = 32;
1073 static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize;
1074
1075
1076 // Pools are emitted after function return and in dead code at (more or less)
1077 // regular intervals of kDistBetweenPools bytes
1078 static const int kDistBetweenPools = 1*KB;
1079
1080 // Constants in pools are accessed via pc relative addressing, which can
1081 // reach +/-4KB thereby defining a maximum distance between the instruction
1082 // and the accessed constant. We satisfy this constraint by limiting the
1083 // distance between pools.
1084 static const int kMaxDistBetweenPools = 4*KB - 2*kBufferCheckInterval;
1085
Steve Block6ded16b2010-05-10 14:33:55 +01001086 // Emission of the constant pool may be blocked in some code sequences.
1087 int const_pool_blocked_nesting_; // Block emission if this is not zero.
1088 int no_const_pool_before_; // Block emission before this pc offset.
Steve Blocka7e24c12009-10-30 11:49:00 +00001089
1090 // Keep track of the last emitted pool to guarantee a maximal distance
1091 int last_const_pool_end_; // pc offset following the last constant pool
1092
1093 // Relocation info generation
1094 // Each relocation is encoded as a variable size value
1095 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
1096 RelocInfoWriter reloc_info_writer;
1097 // Relocation info records are also used during code generation as temporary
1098 // containers for constants and code target addresses until they are emitted
1099 // to the constant pool. These pending relocation info records are temporarily
1100 // stored in a separate buffer until a constant pool is emitted.
1101 // If every instruction in a long sequence is accessing the pool, we need one
1102 // pending relocation entry per instruction.
1103 static const int kMaxNumPRInfo = kMaxDistBetweenPools/kInstrSize;
1104 RelocInfo prinfo_[kMaxNumPRInfo]; // the buffer of pending relocation info
1105 int num_prinfo_; // number of pending reloc info entries in the buffer
1106
1107 // The bound position, before this we cannot do instruction elimination.
1108 int last_bound_pos_;
1109
1110 // source position information
1111 int current_position_;
1112 int current_statement_position_;
1113 int written_position_;
1114 int written_statement_position_;
1115
1116 // Code emission
1117 inline void CheckBuffer();
1118 void GrowBuffer();
1119 inline void emit(Instr x);
1120
1121 // Instruction generation
1122 void addrmod1(Instr instr, Register rn, Register rd, const Operand& x);
1123 void addrmod2(Instr instr, Register rd, const MemOperand& x);
1124 void addrmod3(Instr instr, Register rd, const MemOperand& x);
1125 void addrmod4(Instr instr, Register rn, RegList rl);
1126 void addrmod5(Instr instr, CRegister crd, const MemOperand& x);
1127
1128 // Labels
1129 void print(Label* L);
1130 void bind_to(Label* L, int pos);
1131 void link_to(Label* L, Label* appendix);
1132 void next(Label* L);
1133
1134 // Record reloc info for current pc_
1135 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1136
1137 friend class RegExpMacroAssemblerARM;
1138 friend class RelocInfo;
1139 friend class CodePatcher;
Steve Block6ded16b2010-05-10 14:33:55 +01001140 friend class BlockConstPoolScope;
Steve Blocka7e24c12009-10-30 11:49:00 +00001141};
1142
1143} } // namespace v8::internal
1144
1145#endif // V8_ARM_ASSEMBLER_ARM_H_