blob: 1c4fd6094d234ab7499be0225dbda554d69fb425 [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 {
Kristian Monsen0d5e1162010-09-30 15:31:59 +010072 bool is_valid() const { return 0 <= code_ && code_ < 16; }
73 bool is(Register reg) const { return code_ == reg.code_; }
74 int code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +000075 ASSERT(is_valid());
76 return code_;
77 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +010078 int bit() const {
Steve Blocka7e24c12009-10-30 11:49:00 +000079 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 {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100113 bool is_valid() const { return 0 <= code_ && code_ < 32; }
114 bool is(SwVfpRegister reg) const { return code_ == reg.code_; }
115 int code() const {
Leon Clarkee46be812010-01-19 14:06:41 +0000116 ASSERT(is_valid());
117 return code_;
118 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100119 int bit() const {
Leon Clarkee46be812010-01-19 14:06:41 +0000120 ASSERT(is_valid());
121 return 1 << code_;
122 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100123 void split_code(int* vm, int* m) const {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100124 ASSERT(is_valid());
125 *m = code_ & 0x1;
126 *vm = code_ >> 1;
127 }
Leon Clarkee46be812010-01-19 14:06:41 +0000128
129 int code_;
130};
131
132
133// Double word VFP register.
134struct DwVfpRegister {
135 // Supporting d0 to d15, can be later extended to d31.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100136 bool is_valid() const { return 0 <= code_ && code_ < 16; }
137 bool is(DwVfpRegister reg) const { return code_ == reg.code_; }
138 SwVfpRegister low() const {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100139 SwVfpRegister reg;
140 reg.code_ = code_ * 2;
141
142 ASSERT(reg.is_valid());
143 return reg;
144 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100145 SwVfpRegister high() const {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100146 SwVfpRegister reg;
147 reg.code_ = (code_ * 2) + 1;
148
149 ASSERT(reg.is_valid());
150 return reg;
151 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100152 int code() const {
Leon Clarkee46be812010-01-19 14:06:41 +0000153 ASSERT(is_valid());
154 return code_;
155 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100156 int bit() const {
Leon Clarkee46be812010-01-19 14:06:41 +0000157 ASSERT(is_valid());
158 return 1 << code_;
159 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100160 void split_code(int* vm, int* m) const {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100161 ASSERT(is_valid());
162 *m = (code_ & 0x10) >> 4;
163 *vm = code_ & 0x0F;
164 }
Leon Clarkee46be812010-01-19 14:06:41 +0000165
166 int code_;
167};
168
169
Steve Block6ded16b2010-05-10 14:33:55 +0100170// Support for the VFP registers s0 to s31 (d0 to d15).
Leon Clarkee46be812010-01-19 14:06:41 +0000171// Note that "s(N):s(N+1)" is the same as "d(N/2)".
Steve Block6ded16b2010-05-10 14:33:55 +0100172const SwVfpRegister s0 = { 0 };
173const SwVfpRegister s1 = { 1 };
174const SwVfpRegister s2 = { 2 };
175const SwVfpRegister s3 = { 3 };
176const SwVfpRegister s4 = { 4 };
177const SwVfpRegister s5 = { 5 };
178const SwVfpRegister s6 = { 6 };
179const SwVfpRegister s7 = { 7 };
180const SwVfpRegister s8 = { 8 };
181const SwVfpRegister s9 = { 9 };
182const SwVfpRegister s10 = { 10 };
183const SwVfpRegister s11 = { 11 };
184const SwVfpRegister s12 = { 12 };
185const SwVfpRegister s13 = { 13 };
186const SwVfpRegister s14 = { 14 };
187const SwVfpRegister s15 = { 15 };
188const SwVfpRegister s16 = { 16 };
189const SwVfpRegister s17 = { 17 };
190const SwVfpRegister s18 = { 18 };
191const SwVfpRegister s19 = { 19 };
192const SwVfpRegister s20 = { 20 };
193const SwVfpRegister s21 = { 21 };
194const SwVfpRegister s22 = { 22 };
195const SwVfpRegister s23 = { 23 };
196const SwVfpRegister s24 = { 24 };
197const SwVfpRegister s25 = { 25 };
198const SwVfpRegister s26 = { 26 };
199const SwVfpRegister s27 = { 27 };
200const SwVfpRegister s28 = { 28 };
201const SwVfpRegister s29 = { 29 };
202const SwVfpRegister s30 = { 30 };
203const SwVfpRegister s31 = { 31 };
Leon Clarkee46be812010-01-19 14:06:41 +0000204
Steve Block6ded16b2010-05-10 14:33:55 +0100205const DwVfpRegister d0 = { 0 };
206const DwVfpRegister d1 = { 1 };
207const DwVfpRegister d2 = { 2 };
208const DwVfpRegister d3 = { 3 };
209const DwVfpRegister d4 = { 4 };
210const DwVfpRegister d5 = { 5 };
211const DwVfpRegister d6 = { 6 };
212const DwVfpRegister d7 = { 7 };
213const DwVfpRegister d8 = { 8 };
214const DwVfpRegister d9 = { 9 };
215const DwVfpRegister d10 = { 10 };
216const DwVfpRegister d11 = { 11 };
217const DwVfpRegister d12 = { 12 };
218const DwVfpRegister d13 = { 13 };
219const DwVfpRegister d14 = { 14 };
220const DwVfpRegister d15 = { 15 };
Leon Clarkee46be812010-01-19 14:06:41 +0000221
Steve Blocka7e24c12009-10-30 11:49:00 +0000222
223// Coprocessor register
224struct CRegister {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100225 bool is_valid() const { return 0 <= code_ && code_ < 16; }
226 bool is(CRegister creg) const { return code_ == creg.code_; }
227 int code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 ASSERT(is_valid());
229 return code_;
230 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100231 int bit() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 ASSERT(is_valid());
233 return 1 << code_;
234 }
235
Andrei Popescu31002712010-02-23 13:46:05 +0000236 // Unfortunately we can't make this private in a struct.
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 int code_;
238};
239
240
Steve Block6ded16b2010-05-10 14:33:55 +0100241const CRegister no_creg = { -1 };
242
243const CRegister cr0 = { 0 };
244const CRegister cr1 = { 1 };
245const CRegister cr2 = { 2 };
246const CRegister cr3 = { 3 };
247const CRegister cr4 = { 4 };
248const CRegister cr5 = { 5 };
249const CRegister cr6 = { 6 };
250const CRegister cr7 = { 7 };
251const CRegister cr8 = { 8 };
252const CRegister cr9 = { 9 };
253const CRegister cr10 = { 10 };
254const CRegister cr11 = { 11 };
255const CRegister cr12 = { 12 };
256const CRegister cr13 = { 13 };
257const CRegister cr14 = { 14 };
258const CRegister cr15 = { 15 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000259
260
261// Coprocessor number
262enum Coprocessor {
263 p0 = 0,
264 p1 = 1,
265 p2 = 2,
266 p3 = 3,
267 p4 = 4,
268 p5 = 5,
269 p6 = 6,
270 p7 = 7,
271 p8 = 8,
272 p9 = 9,
273 p10 = 10,
274 p11 = 11,
275 p12 = 12,
276 p13 = 13,
277 p14 = 14,
278 p15 = 15
279};
280
281
Andrei Popescu31002712010-02-23 13:46:05 +0000282// Condition field in instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000283enum Condition {
284 eq = 0 << 28, // Z set equal.
285 ne = 1 << 28, // Z clear not equal.
286 nz = 1 << 28, // Z clear not zero.
287 cs = 2 << 28, // C set carry set.
288 hs = 2 << 28, // C set unsigned higher or same.
289 cc = 3 << 28, // C clear carry clear.
290 lo = 3 << 28, // C clear unsigned lower.
291 mi = 4 << 28, // N set negative.
292 pl = 5 << 28, // N clear positive or zero.
293 vs = 6 << 28, // V set overflow.
294 vc = 7 << 28, // V clear no overflow.
295 hi = 8 << 28, // C set, Z clear unsigned higher.
296 ls = 9 << 28, // C clear or Z set unsigned lower or same.
297 ge = 10 << 28, // N == V greater or equal.
298 lt = 11 << 28, // N != V less than.
299 gt = 12 << 28, // Z clear, N == V greater than.
300 le = 13 << 28, // Z set or N != V less then or equal
301 al = 14 << 28 // always.
302};
303
304
305// Returns the equivalent of !cc.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100306inline Condition NegateCondition(Condition cc) {
307 ASSERT(cc != al);
308 return static_cast<Condition>(cc ^ ne);
309}
Steve Blocka7e24c12009-10-30 11:49:00 +0000310
311
312// Corresponds to transposing the operands of a comparison.
313inline Condition ReverseCondition(Condition cc) {
314 switch (cc) {
315 case lo:
316 return hi;
317 case hi:
318 return lo;
319 case hs:
320 return ls;
321 case ls:
322 return hs;
323 case lt:
324 return gt;
325 case gt:
326 return lt;
327 case ge:
328 return le;
329 case le:
330 return ge;
331 default:
332 return cc;
333 };
334}
335
336
337// Branch hints are not used on the ARM. They are defined so that they can
338// appear in shared function signatures, but will be ignored in ARM
339// implementations.
340enum Hint { no_hint };
341
342// Hints are not used on the arm. Negating is trivial.
343inline Hint NegateHint(Hint ignored) { return no_hint; }
344
345
346// -----------------------------------------------------------------------------
347// Addressing modes and instruction variants
348
349// Shifter operand shift operation
350enum ShiftOp {
351 LSL = 0 << 5,
352 LSR = 1 << 5,
353 ASR = 2 << 5,
354 ROR = 3 << 5,
355 RRX = -1
356};
357
358
359// Condition code updating mode
360enum SBit {
361 SetCC = 1 << 20, // set condition code
362 LeaveCC = 0 << 20 // leave condition code unchanged
363};
364
365
366// Status register selection
367enum SRegister {
368 CPSR = 0 << 22,
369 SPSR = 1 << 22
370};
371
372
373// Status register fields
374enum SRegisterField {
375 CPSR_c = CPSR | 1 << 16,
376 CPSR_x = CPSR | 1 << 17,
377 CPSR_s = CPSR | 1 << 18,
378 CPSR_f = CPSR | 1 << 19,
379 SPSR_c = SPSR | 1 << 16,
380 SPSR_x = SPSR | 1 << 17,
381 SPSR_s = SPSR | 1 << 18,
382 SPSR_f = SPSR | 1 << 19
383};
384
385// Status register field mask (or'ed SRegisterField enum values)
386typedef uint32_t SRegisterFieldMask;
387
388
389// Memory operand addressing mode
390enum AddrMode {
391 // bit encoding P U W
392 Offset = (8|4|0) << 21, // offset (without writeback to base)
393 PreIndex = (8|4|1) << 21, // pre-indexed addressing with writeback
394 PostIndex = (0|4|0) << 21, // post-indexed addressing with writeback
395 NegOffset = (8|0|0) << 21, // negative offset (without writeback to base)
396 NegPreIndex = (8|0|1) << 21, // negative pre-indexed with writeback
397 NegPostIndex = (0|0|0) << 21 // negative post-indexed with writeback
398};
399
400
401// Load/store multiple addressing mode
402enum BlockAddrMode {
403 // bit encoding P U W
404 da = (0|0|0) << 21, // decrement after
405 ia = (0|4|0) << 21, // increment after
406 db = (8|0|0) << 21, // decrement before
407 ib = (8|4|0) << 21, // increment before
408 da_w = (0|0|1) << 21, // decrement after with writeback to base
409 ia_w = (0|4|1) << 21, // increment after with writeback to base
410 db_w = (8|0|1) << 21, // decrement before with writeback to base
411 ib_w = (8|4|1) << 21 // increment before with writeback to base
412};
413
414
415// Coprocessor load/store operand size
416enum LFlag {
417 Long = 1 << 22, // long load/store coprocessor
418 Short = 0 << 22 // short load/store coprocessor
419};
420
421
422// -----------------------------------------------------------------------------
423// Machine instruction Operands
424
425// Class Operand represents a shifter operand in data processing instructions
426class Operand BASE_EMBEDDED {
427 public:
428 // immediate
429 INLINE(explicit Operand(int32_t immediate,
430 RelocInfo::Mode rmode = RelocInfo::NONE));
431 INLINE(explicit Operand(const ExternalReference& f));
432 INLINE(explicit Operand(const char* s));
Steve Blocka7e24c12009-10-30 11:49:00 +0000433 explicit Operand(Handle<Object> handle);
434 INLINE(explicit Operand(Smi* value));
435
436 // rm
437 INLINE(explicit Operand(Register rm));
438
439 // rm <shift_op> shift_imm
440 explicit Operand(Register rm, ShiftOp shift_op, int shift_imm);
441
442 // rm <shift_op> rs
443 explicit Operand(Register rm, ShiftOp shift_op, Register rs);
444
445 // Return true if this is a register operand.
446 INLINE(bool is_reg() const);
447
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100448 // Return true of this operand fits in one instruction so that no
449 // 2-instruction solution with a load into the ip register is necessary.
450 bool is_single_instruction() const;
451
452 inline int32_t immediate() const {
453 ASSERT(!rm_.is_valid());
454 return imm32_;
455 }
456
Steve Blocka7e24c12009-10-30 11:49:00 +0000457 Register rm() const { return rm_; }
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100458 Register rs() const { return rs_; }
459 ShiftOp shift_op() const { return shift_op_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000460
461 private:
462 Register rm_;
463 Register rs_;
464 ShiftOp shift_op_;
465 int shift_imm_; // valid if rm_ != no_reg && rs_ == no_reg
466 int32_t imm32_; // valid if rm_ == no_reg
467 RelocInfo::Mode rmode_;
468
469 friend class Assembler;
470};
471
472
473// Class MemOperand represents a memory operand in load and store instructions
474class MemOperand BASE_EMBEDDED {
475 public:
476 // [rn +/- offset] Offset/NegOffset
477 // [rn +/- offset]! PreIndex/NegPreIndex
478 // [rn], +/- offset PostIndex/NegPostIndex
479 // offset is any signed 32-bit value; offset is first loaded to register ip if
480 // it does not fit the addressing mode (12-bit unsigned and sign bit)
481 explicit MemOperand(Register rn, int32_t offset = 0, AddrMode am = Offset);
482
483 // [rn +/- rm] Offset/NegOffset
484 // [rn +/- rm]! PreIndex/NegPreIndex
485 // [rn], +/- rm PostIndex/NegPostIndex
486 explicit MemOperand(Register rn, Register rm, AddrMode am = Offset);
487
488 // [rn +/- rm <shift_op> shift_imm] Offset/NegOffset
489 // [rn +/- rm <shift_op> shift_imm]! PreIndex/NegPreIndex
490 // [rn], +/- rm <shift_op> shift_imm PostIndex/NegPostIndex
491 explicit MemOperand(Register rn, Register rm,
492 ShiftOp shift_op, int shift_imm, AddrMode am = Offset);
493
Kristian Monsen25f61362010-05-21 11:50:48 +0100494 void set_offset(int32_t offset) {
495 ASSERT(rm_.is(no_reg));
496 offset_ = offset;
497 }
498
499 uint32_t offset() {
500 ASSERT(rm_.is(no_reg));
501 return offset_;
502 }
503
Leon Clarkef7060e22010-06-03 12:02:55 +0100504 Register rn() const { return rn_; }
505 Register rm() const { return rm_; }
Kristian Monsen25f61362010-05-21 11:50:48 +0100506
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 private:
508 Register rn_; // base
509 Register rm_; // register offset
510 int32_t offset_; // valid if rm_ == no_reg
511 ShiftOp shift_op_;
512 int shift_imm_; // valid if rm_ != no_reg && rs_ == no_reg
513 AddrMode am_; // bits P, U, and W
514
515 friend class Assembler;
516};
517
Steve Blockd0582a62009-12-15 09:54:21 +0000518// CpuFeatures keeps track of which features are supported by the target CPU.
519// Supported features must be enabled by a Scope before use.
520class CpuFeatures : public AllStatic {
521 public:
522 // Detect features of the target CPU. Set safe defaults if the serializer
523 // is enabled (snapshots must be portable).
524 static void Probe();
525
526 // Check whether a feature is supported by the target CPU.
527 static bool IsSupported(CpuFeature f) {
528 if (f == VFP3 && !FLAG_enable_vfp3) return false;
529 return (supported_ & (1u << f)) != 0;
530 }
531
532 // Check whether a feature is currently enabled.
533 static bool IsEnabled(CpuFeature f) {
534 return (enabled_ & (1u << f)) != 0;
535 }
536
537 // Enable a specified feature within a scope.
538 class Scope BASE_EMBEDDED {
539#ifdef DEBUG
540 public:
541 explicit Scope(CpuFeature f) {
542 ASSERT(CpuFeatures::IsSupported(f));
543 ASSERT(!Serializer::enabled() ||
544 (found_by_runtime_probing_ & (1u << f)) == 0);
545 old_enabled_ = CpuFeatures::enabled_;
546 CpuFeatures::enabled_ |= 1u << f;
547 }
548 ~Scope() { CpuFeatures::enabled_ = old_enabled_; }
549 private:
550 unsigned old_enabled_;
551#else
552 public:
553 explicit Scope(CpuFeature f) {}
554#endif
555 };
556
557 private:
558 static unsigned supported_;
559 static unsigned enabled_;
560 static unsigned found_by_runtime_probing_;
561};
562
Steve Blocka7e24c12009-10-30 11:49:00 +0000563
564typedef int32_t Instr;
565
566
567extern const Instr kMovLrPc;
Steve Block6ded16b2010-05-10 14:33:55 +0100568extern const Instr kLdrPCMask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000569extern const Instr kLdrPCPattern;
Steve Block6ded16b2010-05-10 14:33:55 +0100570extern const Instr kBlxRegMask;
571extern const Instr kBlxRegPattern;
Steve Blocka7e24c12009-10-30 11:49:00 +0000572
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100573extern const Instr kMovMvnMask;
574extern const Instr kMovMvnPattern;
575extern const Instr kMovMvnFlip;
576
577extern const Instr kMovLeaveCCMask;
578extern const Instr kMovLeaveCCPattern;
579extern const Instr kMovwMask;
580extern const Instr kMovwPattern;
581extern const Instr kMovwLeaveCCFlip;
582
583extern const Instr kCmpCmnMask;
584extern const Instr kCmpCmnPattern;
585extern const Instr kCmpCmnFlip;
586
587extern const Instr kALUMask;
588extern const Instr kAddPattern;
589extern const Instr kSubPattern;
590extern const Instr kAndPattern;
591extern const Instr kBicPattern;
592extern const Instr kAddSubFlip;
593extern const Instr kAndBicFlip;
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
595class Assembler : public Malloced {
596 public:
597 // Create an assembler. Instructions and relocation information are emitted
598 // into a buffer, with the instructions starting from the beginning and the
599 // relocation information starting from the end of the buffer. See CodeDesc
600 // for a detailed comment on the layout (globals.h).
601 //
602 // If the provided buffer is NULL, the assembler allocates and grows its own
603 // buffer, and buffer_size determines the initial buffer size. The buffer is
604 // owned by the assembler and deallocated upon destruction of the assembler.
605 //
606 // If the provided buffer is not NULL, the assembler uses the provided buffer
607 // for code generation and assumes its size to be buffer_size. If the buffer
608 // is too small, a fatal error occurs. No deallocation of the buffer is done
609 // upon destruction of the assembler.
610 Assembler(void* buffer, int buffer_size);
611 ~Assembler();
612
613 // GetCode emits any pending (non-emitted) code and fills the descriptor
614 // desc. GetCode() is idempotent; it returns the same result if no other
615 // Assembler functions are invoked in between GetCode() calls.
616 void GetCode(CodeDesc* desc);
617
618 // Label operations & relative jumps (PPUM Appendix D)
619 //
620 // Takes a branch opcode (cc) and a label (L) and generates
621 // either a backward branch or a forward branch and links it
622 // to the label fixup chain. Usage:
623 //
624 // Label L; // unbound label
625 // j(cc, &L); // forward branch to unbound label
626 // bind(&L); // bind label to the current pc
627 // j(cc, &L); // backward branch to bound label
628 // bind(&L); // illegal: a label may be bound only once
629 //
630 // Note: The same Label can be used for forward and backward branches
631 // but it may be bound only once.
632
633 void bind(Label* L); // binds an unbound label L to the current code position
634
635 // Returns the branch offset to the given label from the current code position
636 // Links the label to the current position if it is still unbound
637 // Manages the jump elimination optimization if the second parameter is true.
638 int branch_offset(Label* L, bool jump_elimination_allowed);
639
640 // Puts a labels target address at the given position.
641 // The high 8 bits are set to zero.
642 void label_at_put(Label* L, int at_offset);
643
644 // Return the address in the constant pool of the code target address used by
645 // the branch/call instruction at pc.
646 INLINE(static Address target_address_address_at(Address pc));
647
648 // Read/Modify the code target address in the branch/call instruction at pc.
649 INLINE(static Address target_address_at(Address pc));
650 INLINE(static void set_target_address_at(Address pc, Address target));
651
Steve Blockd0582a62009-12-15 09:54:21 +0000652 // This sets the branch destination (which is in the constant pool on ARM).
653 // This is for calls and branches within generated code.
654 inline static void set_target_at(Address constant_pool_entry, Address target);
655
656 // This sets the branch destination (which is in the constant pool on ARM).
657 // This is for calls and branches to runtime code.
658 inline static void set_external_target_at(Address constant_pool_entry,
659 Address target) {
660 set_target_at(constant_pool_entry, target);
661 }
662
663 // Here we are patching the address in the constant pool, not the actual call
664 // instruction. The address in the constant pool is the same size as a
665 // pointer.
666 static const int kCallTargetSize = kPointerSize;
667 static const int kExternalTargetSize = kPointerSize;
668
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 // Size of an instruction.
670 static const int kInstrSize = sizeof(Instr);
671
672 // Distance between the instruction referring to the address of the call
Steve Block6ded16b2010-05-10 14:33:55 +0100673 // target and the return address.
674#ifdef USE_BLX
675 // Call sequence is:
676 // ldr ip, [pc, #...] @ call address
677 // blx ip
678 // @ return address
679 static const int kCallTargetAddressOffset = 2 * kInstrSize;
680#else
681 // Call sequence is:
682 // mov lr, pc
683 // ldr pc, [pc, #...] @ call address
684 // @ return address
Steve Blocka7e24c12009-10-30 11:49:00 +0000685 static const int kCallTargetAddressOffset = kInstrSize;
Steve Block6ded16b2010-05-10 14:33:55 +0100686#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000687
688 // Distance between start of patched return sequence and the emitted address
689 // to jump to.
Steve Block6ded16b2010-05-10 14:33:55 +0100690#ifdef USE_BLX
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100691 // Patched return sequence is:
Steve Block6ded16b2010-05-10 14:33:55 +0100692 // ldr ip, [pc, #0] @ emited address and start
693 // blx ip
694 static const int kPatchReturnSequenceAddressOffset = 0 * kInstrSize;
695#else
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100696 // Patched return sequence is:
Steve Block6ded16b2010-05-10 14:33:55 +0100697 // mov lr, pc @ start of sequence
698 // ldr pc, [pc, #-4] @ emited address
699 static const int kPatchReturnSequenceAddressOffset = kInstrSize;
700#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000701
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100702 // Distance between start of patched debug break slot and the emitted address
703 // to jump to.
704#ifdef USE_BLX
705 // Patched debug break slot code is:
706 // ldr ip, [pc, #0] @ emited address and start
707 // blx ip
708 static const int kPatchDebugBreakSlotAddressOffset = 0 * kInstrSize;
709#else
710 // Patched debug break slot code is:
711 // mov lr, pc @ start of sequence
712 // ldr pc, [pc, #-4] @ emited address
713 static const int kPatchDebugBreakSlotAddressOffset = kInstrSize;
714#endif
715
Steve Blocka7e24c12009-10-30 11:49:00 +0000716 // Difference between address of current opcode and value read from pc
717 // register.
718 static const int kPcLoadDelta = 8;
719
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100720 static const int kJSReturnSequenceInstructions = 4;
721 static const int kDebugBreakSlotInstructions = 3;
722 static const int kDebugBreakSlotLength =
723 kDebugBreakSlotInstructions * kInstrSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000724
725 // ---------------------------------------------------------------------------
726 // Code generation
727
728 // Insert the smallest number of nop instructions
729 // possible to align the pc offset to a multiple
730 // of m. m must be a power of 2 (>= 4).
731 void Align(int m);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100732 // Aligns code to something that's optimal for a jump target for the platform.
733 void CodeTargetAlign();
Steve Blocka7e24c12009-10-30 11:49:00 +0000734
735 // Branch instructions
736 void b(int branch_offset, Condition cond = al);
737 void bl(int branch_offset, Condition cond = al);
738 void blx(int branch_offset); // v5 and above
739 void blx(Register target, Condition cond = al); // v5 and above
740 void bx(Register target, Condition cond = al); // v5 and above, plus v4t
741
742 // Convenience branch instructions using labels
743 void b(Label* L, Condition cond = al) {
744 b(branch_offset(L, cond == al), cond);
745 }
746 void b(Condition cond, Label* L) { b(branch_offset(L, cond == al), cond); }
747 void bl(Label* L, Condition cond = al) { bl(branch_offset(L, false), cond); }
748 void bl(Condition cond, Label* L) { bl(branch_offset(L, false), cond); }
749 void blx(Label* L) { blx(branch_offset(L, false)); } // v5 and above
750
751 // Data-processing instructions
Andrei Popescu31002712010-02-23 13:46:05 +0000752
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 void and_(Register dst, Register src1, const Operand& src2,
754 SBit s = LeaveCC, Condition cond = al);
755
756 void eor(Register dst, Register src1, const Operand& src2,
757 SBit s = LeaveCC, Condition cond = al);
758
759 void sub(Register dst, Register src1, const Operand& src2,
760 SBit s = LeaveCC, Condition cond = al);
761 void sub(Register dst, Register src1, Register src2,
762 SBit s = LeaveCC, Condition cond = al) {
763 sub(dst, src1, Operand(src2), s, cond);
764 }
765
766 void rsb(Register dst, Register src1, const Operand& src2,
767 SBit s = LeaveCC, Condition cond = al);
768
769 void add(Register dst, Register src1, const Operand& src2,
770 SBit s = LeaveCC, Condition cond = al);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100771 void add(Register dst, Register src1, Register src2,
772 SBit s = LeaveCC, Condition cond = al) {
773 add(dst, src1, Operand(src2), s, cond);
774 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000775
776 void adc(Register dst, Register src1, const Operand& src2,
777 SBit s = LeaveCC, Condition cond = al);
778
779 void sbc(Register dst, Register src1, const Operand& src2,
780 SBit s = LeaveCC, Condition cond = al);
781
782 void rsc(Register dst, Register src1, const Operand& src2,
783 SBit s = LeaveCC, Condition cond = al);
784
785 void tst(Register src1, const Operand& src2, Condition cond = al);
786 void tst(Register src1, Register src2, Condition cond = al) {
787 tst(src1, Operand(src2), cond);
788 }
789
790 void teq(Register src1, const Operand& src2, Condition cond = al);
791
792 void cmp(Register src1, const Operand& src2, Condition cond = al);
793 void cmp(Register src1, Register src2, Condition cond = al) {
794 cmp(src1, Operand(src2), cond);
795 }
796
797 void cmn(Register src1, const Operand& src2, Condition cond = al);
798
799 void orr(Register dst, Register src1, const Operand& src2,
800 SBit s = LeaveCC, Condition cond = al);
801 void orr(Register dst, Register src1, Register src2,
802 SBit s = LeaveCC, Condition cond = al) {
803 orr(dst, src1, Operand(src2), s, cond);
804 }
805
806 void mov(Register dst, const Operand& src,
807 SBit s = LeaveCC, Condition cond = al);
808 void mov(Register dst, Register src, SBit s = LeaveCC, Condition cond = al) {
809 mov(dst, Operand(src), s, cond);
810 }
811
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100812 // ARMv7 instructions for loading a 32 bit immediate in two instructions.
813 // This may actually emit a different mov instruction, but on an ARMv7 it
814 // is guaranteed to only emit one instruction.
815 void movw(Register reg, uint32_t immediate, Condition cond = al);
816 // The constant for movt should be in the range 0-0xffff.
817 void movt(Register reg, uint32_t immediate, Condition cond = al);
818
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 void bic(Register dst, Register src1, const Operand& src2,
820 SBit s = LeaveCC, Condition cond = al);
821
822 void mvn(Register dst, const Operand& src,
823 SBit s = LeaveCC, Condition cond = al);
824
825 // Multiply instructions
826
827 void mla(Register dst, Register src1, Register src2, Register srcA,
828 SBit s = LeaveCC, Condition cond = al);
829
830 void mul(Register dst, Register src1, Register src2,
831 SBit s = LeaveCC, Condition cond = al);
832
833 void smlal(Register dstL, Register dstH, Register src1, Register src2,
834 SBit s = LeaveCC, Condition cond = al);
835
836 void smull(Register dstL, Register dstH, Register src1, Register src2,
837 SBit s = LeaveCC, Condition cond = al);
838
839 void umlal(Register dstL, Register dstH, Register src1, Register src2,
840 SBit s = LeaveCC, Condition cond = al);
841
842 void umull(Register dstL, Register dstH, Register src1, Register src2,
843 SBit s = LeaveCC, Condition cond = al);
844
845 // Miscellaneous arithmetic instructions
846
847 void clz(Register dst, Register src, Condition cond = al); // v5 and above
848
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100849 // Saturating instructions. v6 and above.
850
851 // Unsigned saturate.
852 //
853 // Saturate an optionally shifted signed value to an unsigned range.
854 //
855 // usat dst, #satpos, src
856 // usat dst, #satpos, src, lsl #sh
857 // usat dst, #satpos, src, asr #sh
858 //
859 // Register dst will contain:
860 //
861 // 0, if s < 0
862 // (1 << satpos) - 1, if s > ((1 << satpos) - 1)
863 // s, otherwise
864 //
865 // where s is the contents of src after shifting (if used.)
866 void usat(Register dst, int satpos, const Operand& src, Condition cond = al);
867
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100868 // Bitfield manipulation instructions. v7 and above.
869
870 void ubfx(Register dst, Register src, int lsb, int width,
871 Condition cond = al);
872
873 void sbfx(Register dst, Register src, int lsb, int width,
874 Condition cond = al);
875
876 void bfc(Register dst, int lsb, int width, Condition cond = al);
877
878 void bfi(Register dst, Register src, int lsb, int width,
879 Condition cond = al);
880
Steve Blocka7e24c12009-10-30 11:49:00 +0000881 // Status register access instructions
882
883 void mrs(Register dst, SRegister s, Condition cond = al);
884 void msr(SRegisterFieldMask fields, const Operand& src, Condition cond = al);
885
886 // Load/Store instructions
887 void ldr(Register dst, const MemOperand& src, Condition cond = al);
888 void str(Register src, const MemOperand& dst, Condition cond = al);
889 void ldrb(Register dst, const MemOperand& src, Condition cond = al);
890 void strb(Register src, const MemOperand& dst, Condition cond = al);
891 void ldrh(Register dst, const MemOperand& src, Condition cond = al);
892 void strh(Register src, const MemOperand& dst, Condition cond = al);
893 void ldrsb(Register dst, const MemOperand& src, Condition cond = al);
894 void ldrsh(Register dst, const MemOperand& src, Condition cond = al);
Leon Clarkef7060e22010-06-03 12:02:55 +0100895 void ldrd(Register dst1,
896 Register dst2,
897 const MemOperand& src, Condition cond = al);
898 void strd(Register src1,
899 Register src2,
900 const MemOperand& dst, Condition cond = al);
Steve Blocka7e24c12009-10-30 11:49:00 +0000901
902 // Load/Store multiple instructions
903 void ldm(BlockAddrMode am, Register base, RegList dst, Condition cond = al);
904 void stm(BlockAddrMode am, Register base, RegList src, Condition cond = al);
905
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 // Exception-generating instructions and debugging support
907 void stop(const char* msg);
908
909 void bkpt(uint32_t imm16); // v5 and above
910 void swi(uint32_t imm24, Condition cond = al);
911
912 // Coprocessor instructions
913
914 void cdp(Coprocessor coproc, int opcode_1,
915 CRegister crd, CRegister crn, CRegister crm,
916 int opcode_2, Condition cond = al);
917
918 void cdp2(Coprocessor coproc, int opcode_1,
919 CRegister crd, CRegister crn, CRegister crm,
920 int opcode_2); // v5 and above
921
922 void mcr(Coprocessor coproc, int opcode_1,
923 Register rd, CRegister crn, CRegister crm,
924 int opcode_2 = 0, Condition cond = al);
925
926 void mcr2(Coprocessor coproc, int opcode_1,
927 Register rd, CRegister crn, CRegister crm,
928 int opcode_2 = 0); // v5 and above
929
930 void mrc(Coprocessor coproc, int opcode_1,
931 Register rd, CRegister crn, CRegister crm,
932 int opcode_2 = 0, Condition cond = al);
933
934 void mrc2(Coprocessor coproc, int opcode_1,
935 Register rd, CRegister crn, CRegister crm,
936 int opcode_2 = 0); // v5 and above
937
938 void ldc(Coprocessor coproc, CRegister crd, const MemOperand& src,
939 LFlag l = Short, Condition cond = al);
940 void ldc(Coprocessor coproc, CRegister crd, Register base, int option,
941 LFlag l = Short, Condition cond = al);
942
943 void ldc2(Coprocessor coproc, CRegister crd, const MemOperand& src,
944 LFlag l = Short); // v5 and above
945 void ldc2(Coprocessor coproc, CRegister crd, Register base, int option,
946 LFlag l = Short); // v5 and above
947
948 void stc(Coprocessor coproc, CRegister crd, const MemOperand& dst,
949 LFlag l = Short, Condition cond = al);
950 void stc(Coprocessor coproc, CRegister crd, Register base, int option,
951 LFlag l = Short, Condition cond = al);
952
953 void stc2(Coprocessor coproc, CRegister crd, const MemOperand& dst,
954 LFlag l = Short); // v5 and above
955 void stc2(Coprocessor coproc, CRegister crd, Register base, int option,
956 LFlag l = Short); // v5 and above
957
Steve Blockd0582a62009-12-15 09:54:21 +0000958 // Support for VFP.
959 // All these APIs support S0 to S31 and D0 to D15.
960 // Currently these APIs do not support extended D registers, i.e, D16 to D31.
961 // However, some simple modifications can allow
962 // these APIs to support D16 to D31.
963
Leon Clarked91b9f72010-01-27 17:25:45 +0000964 void vldr(const DwVfpRegister dst,
965 const Register base,
966 int offset, // Offset must be a multiple of 4.
967 const Condition cond = al);
Steve Block6ded16b2010-05-10 14:33:55 +0100968
969 void vldr(const SwVfpRegister dst,
970 const Register base,
971 int offset, // Offset must be a multiple of 4.
972 const Condition cond = al);
973
Leon Clarked91b9f72010-01-27 17:25:45 +0000974 void vstr(const DwVfpRegister src,
975 const Register base,
976 int offset, // Offset must be a multiple of 4.
977 const Condition cond = al);
Steve Block8defd9f2010-07-08 12:39:36 +0100978
Iain Merrick75681382010-08-19 15:07:18 +0100979 void vstr(const SwVfpRegister src,
980 const Register base,
981 int offset, // Offset must be a multiple of 4.
982 const Condition cond = al);
983
Steve Block8defd9f2010-07-08 12:39:36 +0100984 void vmov(const DwVfpRegister dst,
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100985 double imm,
986 const Condition cond = al);
987 void vmov(const SwVfpRegister dst,
988 const SwVfpRegister src,
989 const Condition cond = al);
990 void vmov(const DwVfpRegister dst,
Steve Block8defd9f2010-07-08 12:39:36 +0100991 const DwVfpRegister src,
992 const Condition cond = al);
Leon Clarkee46be812010-01-19 14:06:41 +0000993 void vmov(const DwVfpRegister dst,
994 const Register src1,
Steve Blockd0582a62009-12-15 09:54:21 +0000995 const Register src2,
Leon Clarkee46be812010-01-19 14:06:41 +0000996 const Condition cond = al);
997 void vmov(const Register dst1,
998 const Register dst2,
999 const DwVfpRegister src,
1000 const Condition cond = al);
1001 void vmov(const SwVfpRegister dst,
1002 const Register src,
1003 const Condition cond = al);
1004 void vmov(const Register dst,
1005 const SwVfpRegister src,
1006 const Condition cond = al);
Steve Block6ded16b2010-05-10 14:33:55 +01001007 void vcvt_f64_s32(const DwVfpRegister dst,
1008 const SwVfpRegister src,
1009 const Condition cond = al);
1010 void vcvt_f32_s32(const SwVfpRegister dst,
1011 const SwVfpRegister src,
1012 const Condition cond = al);
1013 void vcvt_f64_u32(const DwVfpRegister dst,
1014 const SwVfpRegister src,
1015 const Condition cond = al);
1016 void vcvt_s32_f64(const SwVfpRegister dst,
1017 const DwVfpRegister src,
1018 const Condition cond = al);
1019 void vcvt_u32_f64(const SwVfpRegister dst,
1020 const DwVfpRegister src,
1021 const Condition cond = al);
1022 void vcvt_f64_f32(const DwVfpRegister dst,
1023 const SwVfpRegister src,
1024 const Condition cond = al);
1025 void vcvt_f32_f64(const SwVfpRegister dst,
1026 const DwVfpRegister src,
1027 const Condition cond = al);
Leon Clarkee46be812010-01-19 14:06:41 +00001028
1029 void vadd(const DwVfpRegister dst,
1030 const DwVfpRegister src1,
1031 const DwVfpRegister src2,
1032 const Condition cond = al);
1033 void vsub(const DwVfpRegister dst,
1034 const DwVfpRegister src1,
1035 const DwVfpRegister src2,
1036 const Condition cond = al);
1037 void vmul(const DwVfpRegister dst,
1038 const DwVfpRegister src1,
1039 const DwVfpRegister src2,
1040 const Condition cond = al);
1041 void vdiv(const DwVfpRegister dst,
1042 const DwVfpRegister src1,
1043 const DwVfpRegister src2,
1044 const Condition cond = al);
1045 void vcmp(const DwVfpRegister src1,
1046 const DwVfpRegister src2,
Steve Blockd0582a62009-12-15 09:54:21 +00001047 const SBit s = LeaveCC,
1048 const Condition cond = al);
Iain Merrick75681382010-08-19 15:07:18 +01001049 void vcmp(const DwVfpRegister src1,
1050 const double src2,
1051 const SBit s = LeaveCC,
1052 const Condition cond = al);
Steve Blockd0582a62009-12-15 09:54:21 +00001053 void vmrs(const Register dst,
1054 const Condition cond = al);
Steve Block8defd9f2010-07-08 12:39:36 +01001055 void vsqrt(const DwVfpRegister dst,
1056 const DwVfpRegister src,
1057 const Condition cond = al);
Steve Blockd0582a62009-12-15 09:54:21 +00001058
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 // Pseudo instructions
Steve Block6ded16b2010-05-10 14:33:55 +01001060 void nop(int type = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001061
1062 void push(Register src, Condition cond = al) {
1063 str(src, MemOperand(sp, 4, NegPreIndex), cond);
1064 }
1065
1066 void pop(Register dst, Condition cond = al) {
1067 ldr(dst, MemOperand(sp, 4, PostIndex), cond);
1068 }
1069
1070 void pop() {
1071 add(sp, sp, Operand(kPointerSize));
1072 }
1073
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 // Jump unconditionally to given label.
1075 void jmp(Label* L) { b(L, al); }
1076
1077 // Check the code size generated from label to here.
1078 int InstructionsGeneratedSince(Label* l) {
1079 return (pc_offset() - l->pos()) / kInstrSize;
1080 }
1081
Steve Blockd0582a62009-12-15 09:54:21 +00001082 // Check whether an immediate fits an addressing mode 1 instruction.
1083 bool ImmediateFitsAddrMode1Instruction(int32_t imm32);
1084
Steve Block6ded16b2010-05-10 14:33:55 +01001085 // Class for scoping postponing the constant pool generation.
1086 class BlockConstPoolScope {
1087 public:
1088 explicit BlockConstPoolScope(Assembler* assem) : assem_(assem) {
1089 assem_->StartBlockConstPool();
1090 }
1091 ~BlockConstPoolScope() {
1092 assem_->EndBlockConstPool();
1093 }
1094
1095 private:
1096 Assembler* assem_;
1097
1098 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockConstPoolScope);
1099 };
1100
Steve Blockd0582a62009-12-15 09:54:21 +00001101 // Postpone the generation of the constant pool for the specified number of
1102 // instructions.
1103 void BlockConstPoolFor(int instructions);
1104
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 // Debugging
1106
1107 // Mark address of the ExitJSFrame code.
1108 void RecordJSReturn();
1109
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001110 // Mark address of a debug break slot.
1111 void RecordDebugBreakSlot();
1112
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 // Record a comment relocation entry that can be used by a disassembler.
1114 // Use --debug_code to enable.
1115 void RecordComment(const char* msg);
1116
1117 void RecordPosition(int pos);
1118 void RecordStatementPosition(int pos);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001119 bool WriteRecordedPositions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001120
1121 int pc_offset() const { return pc_ - buffer_; }
1122 int current_position() const { return current_position_; }
Steve Block6ded16b2010-05-10 14:33:55 +01001123 int current_statement_position() const { return current_statement_position_; }
1124
Leon Clarkef7060e22010-06-03 12:02:55 +01001125 bool can_peephole_optimize(int instructions) {
1126 if (!FLAG_peephole_optimization) return false;
1127 if (last_bound_pos_ > pc_offset() - instructions * kInstrSize) return false;
1128 return reloc_info_writer.last_pc() <= pc_ - instructions * kInstrSize;
1129 }
1130
Steve Block6ded16b2010-05-10 14:33:55 +01001131 // Read/patch instructions
1132 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
1133 static void instr_at_put(byte* pc, Instr instr) {
1134 *reinterpret_cast<Instr*>(pc) = instr;
1135 }
1136 static bool IsNop(Instr instr, int type = 0);
1137 static bool IsBranch(Instr instr);
1138 static int GetBranchOffset(Instr instr);
1139 static bool IsLdrRegisterImmediate(Instr instr);
1140 static int GetLdrRegisterImmediateOffset(Instr instr);
1141 static Instr SetLdrRegisterImmediateOffset(Instr instr, int offset);
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001142 static bool IsStrRegisterImmediate(Instr instr);
1143 static Instr SetStrRegisterImmediateOffset(Instr instr, int offset);
1144 static bool IsAddRegisterImmediate(Instr instr);
1145 static Instr SetAddRegisterImmediateOffset(Instr instr, int offset);
Leon Clarkef7060e22010-06-03 12:02:55 +01001146 static Register GetRd(Instr instr);
1147 static bool IsPush(Instr instr);
1148 static bool IsPop(Instr instr);
1149 static bool IsStrRegFpOffset(Instr instr);
1150 static bool IsLdrRegFpOffset(Instr instr);
1151 static bool IsStrRegFpNegOffset(Instr instr);
1152 static bool IsLdrRegFpNegOffset(Instr instr);
Steve Block6ded16b2010-05-10 14:33:55 +01001153
Steve Blocka7e24c12009-10-30 11:49:00 +00001154
1155 protected:
1156 int buffer_space() const { return reloc_info_writer.pos() - pc_; }
1157
1158 // Read/patch instructions
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
1160 void instr_at_put(int pos, Instr instr) {
1161 *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
1162 }
1163
1164 // Decode branch instruction at pos and return branch target pos
1165 int target_at(int pos);
1166
1167 // Patch branch instruction at pos to branch to given branch target pos
1168 void target_at_put(int pos, int target_pos);
1169
1170 // Check if is time to emit a constant pool for pending reloc info entries
1171 void CheckConstPool(bool force_emit, bool require_jump);
1172
1173 // Block the emission of the constant pool before pc_offset
1174 void BlockConstPoolBefore(int pc_offset) {
1175 if (no_const_pool_before_ < pc_offset) no_const_pool_before_ = pc_offset;
1176 }
1177
Steve Block6ded16b2010-05-10 14:33:55 +01001178 void StartBlockConstPool() {
1179 const_pool_blocked_nesting_++;
1180 }
1181 void EndBlockConstPool() {
1182 const_pool_blocked_nesting_--;
1183 }
Steve Block8defd9f2010-07-08 12:39:36 +01001184 bool is_const_pool_blocked() const { return const_pool_blocked_nesting_ > 0; }
Steve Block6ded16b2010-05-10 14:33:55 +01001185
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 private:
1187 // Code buffer:
1188 // The buffer into which code and relocation info are generated.
1189 byte* buffer_;
1190 int buffer_size_;
1191 // True if the assembler owns the buffer, false if buffer is external.
1192 bool own_buffer_;
1193
1194 // Buffer size and constant pool distance are checked together at regular
1195 // intervals of kBufferCheckInterval emitted bytes
1196 static const int kBufferCheckInterval = 1*KB/2;
1197 int next_buffer_check_; // pc offset of next buffer check
1198
1199 // Code generation
1200 // The relocation writer's position is at least kGap bytes below the end of
1201 // the generated instructions. This is so that multi-instruction sequences do
1202 // not have to check for overflow. The same is true for writes of large
1203 // relocation info entries.
1204 static const int kGap = 32;
1205 byte* pc_; // the program counter; moves forward
1206
1207 // Constant pool generation
1208 // Pools are emitted in the instruction stream, preferably after unconditional
1209 // jumps or after returns from functions (in dead code locations).
1210 // If a long code sequence does not contain unconditional jumps, it is
1211 // necessary to emit the constant pool before the pool gets too far from the
1212 // location it is accessed from. In this case, we emit a jump over the emitted
1213 // constant pool.
1214 // Constants in the pool may be addresses of functions that gets relocated;
1215 // if so, a relocation info entry is associated to the constant pool entry.
1216
1217 // Repeated checking whether the constant pool should be emitted is rather
1218 // expensive. By default we only check again once a number of instructions
1219 // has been generated. That also means that the sizing of the buffers is not
1220 // an exact science, and that we rely on some slop to not overrun buffers.
1221 static const int kCheckConstIntervalInst = 32;
1222 static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize;
1223
1224
1225 // Pools are emitted after function return and in dead code at (more or less)
1226 // regular intervals of kDistBetweenPools bytes
1227 static const int kDistBetweenPools = 1*KB;
1228
1229 // Constants in pools are accessed via pc relative addressing, which can
1230 // reach +/-4KB thereby defining a maximum distance between the instruction
1231 // and the accessed constant. We satisfy this constraint by limiting the
1232 // distance between pools.
1233 static const int kMaxDistBetweenPools = 4*KB - 2*kBufferCheckInterval;
1234
Steve Block6ded16b2010-05-10 14:33:55 +01001235 // Emission of the constant pool may be blocked in some code sequences.
1236 int const_pool_blocked_nesting_; // Block emission if this is not zero.
1237 int no_const_pool_before_; // Block emission before this pc offset.
Steve Blocka7e24c12009-10-30 11:49:00 +00001238
1239 // Keep track of the last emitted pool to guarantee a maximal distance
1240 int last_const_pool_end_; // pc offset following the last constant pool
1241
1242 // Relocation info generation
1243 // Each relocation is encoded as a variable size value
1244 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
1245 RelocInfoWriter reloc_info_writer;
1246 // Relocation info records are also used during code generation as temporary
1247 // containers for constants and code target addresses until they are emitted
1248 // to the constant pool. These pending relocation info records are temporarily
1249 // stored in a separate buffer until a constant pool is emitted.
1250 // If every instruction in a long sequence is accessing the pool, we need one
1251 // pending relocation entry per instruction.
1252 static const int kMaxNumPRInfo = kMaxDistBetweenPools/kInstrSize;
1253 RelocInfo prinfo_[kMaxNumPRInfo]; // the buffer of pending relocation info
1254 int num_prinfo_; // number of pending reloc info entries in the buffer
1255
1256 // The bound position, before this we cannot do instruction elimination.
1257 int last_bound_pos_;
1258
1259 // source position information
1260 int current_position_;
1261 int current_statement_position_;
1262 int written_position_;
1263 int written_statement_position_;
1264
1265 // Code emission
1266 inline void CheckBuffer();
1267 void GrowBuffer();
1268 inline void emit(Instr x);
1269
1270 // Instruction generation
1271 void addrmod1(Instr instr, Register rn, Register rd, const Operand& x);
1272 void addrmod2(Instr instr, Register rd, const MemOperand& x);
1273 void addrmod3(Instr instr, Register rd, const MemOperand& x);
1274 void addrmod4(Instr instr, Register rn, RegList rl);
1275 void addrmod5(Instr instr, CRegister crd, const MemOperand& x);
1276
1277 // Labels
1278 void print(Label* L);
1279 void bind_to(Label* L, int pos);
1280 void link_to(Label* L, Label* appendix);
1281 void next(Label* L);
1282
1283 // Record reloc info for current pc_
1284 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1285
1286 friend class RegExpMacroAssemblerARM;
1287 friend class RelocInfo;
1288 friend class CodePatcher;
Steve Block6ded16b2010-05-10 14:33:55 +01001289 friend class BlockConstPoolScope;
Steve Blocka7e24c12009-10-30 11:49:00 +00001290};
1291
1292} } // namespace v8::internal
1293
1294#endif // V8_ARM_ASSEMBLER_ARM_H_