blob: 8b877f653bcbfd120a3f9800d56c3d1631f36b17 [file] [log] [blame]
Andrei Popescu31002712010-02-23 13:46:05 +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 are
6// 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 distribution.
14//
15// - Neither the name of Sun Microsystems or the names of contributors may
16// be used to endorse or promote products derived from this software without
17// specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// The original source code covered by the above license above has been
32// modified significantly by Google Inc.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010033// Copyright 2012 the V8 project authors. All rights reserved.
Andrei Popescu31002712010-02-23 13:46:05 +000034
35
36#ifndef V8_MIPS_ASSEMBLER_MIPS_H_
37#define V8_MIPS_ASSEMBLER_MIPS_H_
38
39#include <stdio.h>
40#include "assembler.h"
41#include "constants-mips.h"
42#include "serialize.h"
43
Andrei Popescu31002712010-02-23 13:46:05 +000044namespace v8 {
45namespace internal {
46
47// CPU Registers.
48//
49// 1) We would prefer to use an enum, but enum values are assignment-
50// compatible with int, which has caused code-generation bugs.
51//
52// 2) We would prefer to use a class instead of a struct but we don't like
53// the register initialization to depend on the particular initialization
54// order (which appears to be different on OS X, Linux, and Windows for the
55// installed versions of C++ we tried). Using a struct permits C-style
56// "initialization". Also, the Register objects cannot be const as this
57// forces initialization stubs in MSVC, making us dependent on initialization
58// order.
59//
60// 3) By not using an enum, we are possibly preventing the compiler from
61// doing certain constant folds, which may significantly reduce the
62// code generated for some assembly instructions (because they boil down
63// to a few constants). If this is a problem, we could change the code
64// such that we use an enum in optimized mode, and the struct in debug
65// mode. This way we get the compile-time error checking in debug mode
66// and best performance in optimized code.
67
68
69// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +000070// Implementation of Register and FPURegister.
Andrei Popescu31002712010-02-23 13:46:05 +000071
72// Core register.
73struct Register {
Steve Block44f0eee2011-05-26 01:26:41 +010074 static const int kNumRegisters = v8::internal::kNumRegisters;
Ben Murdoch257744e2011-11-30 15:57:28 +000075 static const int kNumAllocatableRegisters = 14; // v0 through t7.
76 static const int kSizeInBytes = 4;
Steve Block44f0eee2011-05-26 01:26:41 +010077
78 static int ToAllocationIndex(Register reg) {
79 return reg.code() - 2; // zero_reg and 'at' are skipped.
80 }
81
82 static Register FromAllocationIndex(int index) {
83 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
84 return from_code(index + 2); // zero_reg and 'at' are skipped.
85 }
86
87 static const char* AllocationIndexToString(int index) {
88 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
89 const char* const names[] = {
90 "v0",
91 "v1",
92 "a0",
93 "a1",
94 "a2",
95 "a3",
96 "t0",
97 "t1",
98 "t2",
99 "t3",
100 "t4",
101 "t5",
102 "t6",
103 "t7",
104 };
105 return names[index];
106 }
107
108 static Register from_code(int code) {
109 Register r = { code };
110 return r;
111 }
112
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100113 bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
114 bool is(Register reg) const { return code_ == reg.code_; }
115 int code() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000116 ASSERT(is_valid());
117 return code_;
118 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100119 int bit() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000120 ASSERT(is_valid());
121 return 1 << code_;
122 }
123
124 // Unfortunately we can't make this private in a struct.
125 int code_;
126};
127
Steve Block44f0eee2011-05-26 01:26:41 +0100128const Register no_reg = { -1 };
Andrei Popescu31002712010-02-23 13:46:05 +0000129
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000130const Register zero_reg = { 0 }; // Always zero.
131const Register at = { 1 }; // at: Reserved for synthetic instructions.
132const Register v0 = { 2 }; // v0, v1: Used when returning multiple values
133const Register v1 = { 3 }; // from subroutines.
134const Register a0 = { 4 }; // a0 - a4: Used to pass non-FP parameters.
Steve Block44f0eee2011-05-26 01:26:41 +0100135const Register a1 = { 5 };
136const Register a2 = { 6 };
137const Register a3 = { 7 };
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000138const Register t0 = { 8 }; // t0 - t9: Can be used without reservation, act
139const Register t1 = { 9 }; // as temporary registers and are allowed to
140const Register t2 = { 10 }; // be destroyed by subroutines.
Steve Block44f0eee2011-05-26 01:26:41 +0100141const Register t3 = { 11 };
142const Register t4 = { 12 };
143const Register t5 = { 13 };
144const Register t6 = { 14 };
145const Register t7 = { 15 };
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000146const Register s0 = { 16 }; // s0 - s7: Subroutine register variables.
147const Register s1 = { 17 }; // Subroutines that write to these registers
148const Register s2 = { 18 }; // must restore their values before exiting so
149const Register s3 = { 19 }; // that the caller can expect the values to be
150const Register s4 = { 20 }; // preserved.
Steve Block44f0eee2011-05-26 01:26:41 +0100151const Register s5 = { 21 };
152const Register s6 = { 22 };
153const Register s7 = { 23 };
154const Register t8 = { 24 };
155const Register t9 = { 25 };
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000156const Register k0 = { 26 }; // k0, k1: Reserved for system calls and
157const Register k1 = { 27 }; // interrupt handlers.
158const Register gp = { 28 }; // gp: Reserved.
159const Register sp = { 29 }; // sp: Stack pointer.
160const Register s8_fp = { 30 }; // fp: Frame pointer.
161const Register ra = { 31 }; // ra: Return address pointer.
Steve Block44f0eee2011-05-26 01:26:41 +0100162
Andrei Popescu31002712010-02-23 13:46:05 +0000163
164int ToNumber(Register reg);
165
166Register ToRegister(int num);
167
168// Coprocessor register.
169struct FPURegister {
Steve Block44f0eee2011-05-26 01:26:41 +0100170 static const int kNumRegisters = v8::internal::kNumFPURegisters;
Ben Murdoch589d6972011-11-30 16:04:58 +0000171
172 // TODO(plind): Warning, inconsistent numbering here. kNumFPURegisters refers
173 // to number of 32-bit FPU regs, but kNumAllocatableRegisters refers to
174 // number of Double regs (64-bit regs, or FPU-reg-pairs).
175
176 // A few double registers are reserved: one as a scratch register and one to
177 // hold 0.0.
178 // f28: 0.0
179 // f30: scratch register.
180 static const int kNumReservedRegisters = 2;
181 static const int kNumAllocatableRegisters = kNumRegisters / 2 -
182 kNumReservedRegisters;
183
Steve Block44f0eee2011-05-26 01:26:41 +0100184
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100185 inline static int ToAllocationIndex(FPURegister reg);
Steve Block44f0eee2011-05-26 01:26:41 +0100186
187 static FPURegister FromAllocationIndex(int index) {
188 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
Ben Murdoch589d6972011-11-30 16:04:58 +0000189 return from_code(index * 2);
Steve Block44f0eee2011-05-26 01:26:41 +0100190 }
191
192 static const char* AllocationIndexToString(int index) {
193 ASSERT(index >= 0 && index < kNumAllocatableRegisters);
194 const char* const names[] = {
Ben Murdoch589d6972011-11-30 16:04:58 +0000195 "f0",
Steve Block44f0eee2011-05-26 01:26:41 +0100196 "f2",
197 "f4",
198 "f6",
199 "f8",
200 "f10",
201 "f12",
202 "f14",
203 "f16",
204 "f18",
205 "f20",
206 "f22",
207 "f24",
Ben Murdoch589d6972011-11-30 16:04:58 +0000208 "f26"
Steve Block44f0eee2011-05-26 01:26:41 +0100209 };
210 return names[index];
211 }
212
213 static FPURegister from_code(int code) {
214 FPURegister r = { code };
215 return r;
216 }
217
218 bool is_valid() const { return 0 <= code_ && code_ < kNumFPURegisters ; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100219 bool is(FPURegister creg) const { return code_ == creg.code_; }
Ben Murdoch589d6972011-11-30 16:04:58 +0000220 FPURegister low() const {
221 // Find low reg of a Double-reg pair, which is the reg itself.
222 ASSERT(code_ % 2 == 0); // Specified Double reg must be even.
223 FPURegister reg;
224 reg.code_ = code_;
225 ASSERT(reg.is_valid());
226 return reg;
227 }
228 FPURegister high() const {
229 // Find high reg of a Doubel-reg pair, which is reg + 1.
230 ASSERT(code_ % 2 == 0); // Specified Double reg must be even.
231 FPURegister reg;
232 reg.code_ = code_ + 1;
233 ASSERT(reg.is_valid());
234 return reg;
235 }
236
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100237 int code() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000238 ASSERT(is_valid());
239 return code_;
240 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100241 int bit() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000242 ASSERT(is_valid());
243 return 1 << code_;
244 }
Steve Block44f0eee2011-05-26 01:26:41 +0100245 void setcode(int f) {
246 code_ = f;
247 ASSERT(is_valid());
248 }
Andrei Popescu31002712010-02-23 13:46:05 +0000249 // Unfortunately we can't make this private in a struct.
250 int code_;
251};
252
Ben Murdoch589d6972011-11-30 16:04:58 +0000253// V8 now supports the O32 ABI, and the FPU Registers are organized as 32
254// 32-bit registers, f0 through f31. When used as 'double' they are used
255// in pairs, starting with the even numbered register. So a double operation
256// on f0 really uses f0 and f1.
257// (Modern mips hardware also supports 32 64-bit registers, via setting
258// (priviledged) Status Register FR bit to 1. This is used by the N32 ABI,
259// but it is not in common use. Someday we will want to support this in v8.)
Andrei Popescu31002712010-02-23 13:46:05 +0000260
Ben Murdoch589d6972011-11-30 16:04:58 +0000261// For O32 ABI, Floats and Doubles refer to same set of 32 32-bit registers.
262typedef FPURegister DoubleRegister;
263typedef FPURegister FloatRegister;
264
265const FPURegister no_freg = { -1 };
Andrei Popescu31002712010-02-23 13:46:05 +0000266
Steve Block44f0eee2011-05-26 01:26:41 +0100267const FPURegister f0 = { 0 }; // Return value in hard float mode.
268const FPURegister f1 = { 1 };
269const FPURegister f2 = { 2 };
270const FPURegister f3 = { 3 };
271const FPURegister f4 = { 4 };
272const FPURegister f5 = { 5 };
273const FPURegister f6 = { 6 };
274const FPURegister f7 = { 7 };
275const FPURegister f8 = { 8 };
276const FPURegister f9 = { 9 };
277const FPURegister f10 = { 10 };
278const FPURegister f11 = { 11 };
279const FPURegister f12 = { 12 }; // Arg 0 in hard float mode.
280const FPURegister f13 = { 13 };
281const FPURegister f14 = { 14 }; // Arg 1 in hard float mode.
282const FPURegister f15 = { 15 };
283const FPURegister f16 = { 16 };
284const FPURegister f17 = { 17 };
285const FPURegister f18 = { 18 };
286const FPURegister f19 = { 19 };
287const FPURegister f20 = { 20 };
288const FPURegister f21 = { 21 };
289const FPURegister f22 = { 22 };
290const FPURegister f23 = { 23 };
291const FPURegister f24 = { 24 };
292const FPURegister f25 = { 25 };
293const FPURegister f26 = { 26 };
294const FPURegister f27 = { 27 };
295const FPURegister f28 = { 28 };
296const FPURegister f29 = { 29 };
297const FPURegister f30 = { 30 };
298const FPURegister f31 = { 31 };
Andrei Popescu31002712010-02-23 13:46:05 +0000299
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100300// Register aliases.
301// cp is assumed to be a callee saved register.
302static const Register& kLithiumScratchReg = s3; // Scratch register.
303static const Register& kLithiumScratchReg2 = s4; // Scratch register.
304static const Register& kRootRegister = s6; // Roots array pointer.
305static const Register& cp = s7; // JavaScript context pointer.
306static const Register& fp = s8_fp; // Alias for fp.
307static const DoubleRegister& kLithiumScratchDouble = f30;
308static const FPURegister& kDoubleRegZero = f28;
Ben Murdoch589d6972011-11-30 16:04:58 +0000309
Steve Block44f0eee2011-05-26 01:26:41 +0100310// FPU (coprocessor 1) control registers.
311// Currently only FCSR (#31) is implemented.
312struct FPUControlRegister {
Steve Block44f0eee2011-05-26 01:26:41 +0100313 bool is_valid() const { return code_ == kFCSRRegister; }
314 bool is(FPUControlRegister creg) const { return code_ == creg.code_; }
315 int code() const {
316 ASSERT(is_valid());
317 return code_;
318 }
319 int bit() const {
320 ASSERT(is_valid());
321 return 1 << code_;
322 }
323 void setcode(int f) {
324 code_ = f;
325 ASSERT(is_valid());
326 }
327 // Unfortunately we can't make this private in a struct.
328 int code_;
Andrei Popescu31002712010-02-23 13:46:05 +0000329};
330
Ben Murdoch257744e2011-11-30 15:57:28 +0000331const FPUControlRegister no_fpucreg = { kInvalidFPUControlRegister };
Steve Block44f0eee2011-05-26 01:26:41 +0100332const FPUControlRegister FCSR = { kFCSRRegister };
Andrei Popescu31002712010-02-23 13:46:05 +0000333
334
335// -----------------------------------------------------------------------------
336// Machine instruction Operands.
337
338// Class Operand represents a shifter operand in data processing instructions.
339class Operand BASE_EMBEDDED {
340 public:
341 // Immediate.
342 INLINE(explicit Operand(int32_t immediate,
343 RelocInfo::Mode rmode = RelocInfo::NONE));
344 INLINE(explicit Operand(const ExternalReference& f));
345 INLINE(explicit Operand(const char* s));
346 INLINE(explicit Operand(Object** opp));
347 INLINE(explicit Operand(Context** cpp));
348 explicit Operand(Handle<Object> handle);
349 INLINE(explicit Operand(Smi* value));
350
351 // Register.
352 INLINE(explicit Operand(Register rm));
353
354 // Return true if this is a register operand.
355 INLINE(bool is_reg() const);
356
357 Register rm() const { return rm_; }
358
359 private:
360 Register rm_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000361 int32_t imm32_; // Valid if rm_ == no_reg.
Andrei Popescu31002712010-02-23 13:46:05 +0000362 RelocInfo::Mode rmode_;
363
364 friend class Assembler;
365 friend class MacroAssembler;
366};
367
368
369// On MIPS we have only one adressing mode with base_reg + offset.
370// Class MemOperand represents a memory operand in load and store instructions.
371class MemOperand : public Operand {
372 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100373 explicit MemOperand(Register rn, int32_t offset = 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000374 int32_t offset() const { return offset_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000375
Ben Murdoch589d6972011-11-30 16:04:58 +0000376 bool OffsetIsInt16Encodable() const {
377 return is_int16(offset_);
378 }
379
Andrei Popescu31002712010-02-23 13:46:05 +0000380 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100381 int32_t offset_;
Andrei Popescu31002712010-02-23 13:46:05 +0000382
383 friend class Assembler;
384};
385
386
Steve Block44f0eee2011-05-26 01:26:41 +0100387// CpuFeatures keeps track of which features are supported by the target CPU.
388// Supported features must be enabled by a Scope before use.
Ben Murdoch257744e2011-11-30 15:57:28 +0000389class CpuFeatures : public AllStatic {
Steve Block44f0eee2011-05-26 01:26:41 +0100390 public:
391 // Detect features of the target CPU. Set safe defaults if the serializer
392 // is enabled (snapshots must be portable).
Ben Murdoch257744e2011-11-30 15:57:28 +0000393 static void Probe();
Steve Block44f0eee2011-05-26 01:26:41 +0100394
395 // Check whether a feature is supported by the target CPU.
Ben Murdoch257744e2011-11-30 15:57:28 +0000396 static bool IsSupported(CpuFeature f) {
397 ASSERT(initialized_);
Steve Block44f0eee2011-05-26 01:26:41 +0100398 if (f == FPU && !FLAG_enable_fpu) return false;
399 return (supported_ & (1u << f)) != 0;
400 }
401
Ben Murdoch257744e2011-11-30 15:57:28 +0000402
403#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100404 // Check whether a feature is currently enabled.
Ben Murdoch257744e2011-11-30 15:57:28 +0000405 static bool IsEnabled(CpuFeature f) {
406 ASSERT(initialized_);
407 Isolate* isolate = Isolate::UncheckedCurrent();
408 if (isolate == NULL) {
409 // When no isolate is available, work as if we're running in
410 // release mode.
411 return IsSupported(f);
412 }
413 unsigned enabled = static_cast<unsigned>(isolate->enabled_cpu_features());
414 return (enabled & (1u << f)) != 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100415 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000416#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100417
418 // Enable a specified feature within a scope.
419 class Scope BASE_EMBEDDED {
420#ifdef DEBUG
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000421
Steve Block44f0eee2011-05-26 01:26:41 +0100422 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000423 explicit Scope(CpuFeature f) {
424 unsigned mask = 1u << f;
425 ASSERT(CpuFeatures::IsSupported(f));
Steve Block44f0eee2011-05-26 01:26:41 +0100426 ASSERT(!Serializer::enabled() ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000427 (CpuFeatures::found_by_runtime_probing_ & mask) == 0);
428 isolate_ = Isolate::UncheckedCurrent();
429 old_enabled_ = 0;
430 if (isolate_ != NULL) {
431 old_enabled_ = static_cast<unsigned>(isolate_->enabled_cpu_features());
432 isolate_->set_enabled_cpu_features(old_enabled_ | mask);
433 }
Steve Block44f0eee2011-05-26 01:26:41 +0100434 }
435 ~Scope() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000436 ASSERT_EQ(Isolate::UncheckedCurrent(), isolate_);
437 if (isolate_ != NULL) {
438 isolate_->set_enabled_cpu_features(old_enabled_);
439 }
440 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000441
442 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100443 Isolate* isolate_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000444 unsigned old_enabled_;
Steve Block44f0eee2011-05-26 01:26:41 +0100445#else
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000446
447 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100448 explicit Scope(CpuFeature f) {}
449#endif
450 };
451
Ben Murdoch257744e2011-11-30 15:57:28 +0000452 class TryForceFeatureScope BASE_EMBEDDED {
453 public:
454 explicit TryForceFeatureScope(CpuFeature f)
455 : old_supported_(CpuFeatures::supported_) {
456 if (CanForce()) {
457 CpuFeatures::supported_ |= (1u << f);
458 }
459 }
460
461 ~TryForceFeatureScope() {
462 if (CanForce()) {
463 CpuFeatures::supported_ = old_supported_;
464 }
465 }
466
467 private:
468 static bool CanForce() {
469 // It's only safe to temporarily force support of CPU features
470 // when there's only a single isolate, which is guaranteed when
471 // the serializer is enabled.
472 return Serializer::enabled();
473 }
474
475 const unsigned old_supported_;
476 };
477
Steve Block44f0eee2011-05-26 01:26:41 +0100478 private:
Ben Murdoch257744e2011-11-30 15:57:28 +0000479#ifdef DEBUG
480 static bool initialized_;
481#endif
482 static unsigned supported_;
483 static unsigned found_by_runtime_probing_;
Steve Block44f0eee2011-05-26 01:26:41 +0100484
485 DISALLOW_COPY_AND_ASSIGN(CpuFeatures);
486};
487
488
489class Assembler : public AssemblerBase {
Andrei Popescu31002712010-02-23 13:46:05 +0000490 public:
491 // Create an assembler. Instructions and relocation information are emitted
492 // into a buffer, with the instructions starting from the beginning and the
493 // relocation information starting from the end of the buffer. See CodeDesc
494 // for a detailed comment on the layout (globals.h).
495 //
496 // If the provided buffer is NULL, the assembler allocates and grows its own
497 // buffer, and buffer_size determines the initial buffer size. The buffer is
498 // owned by the assembler and deallocated upon destruction of the assembler.
499 //
500 // If the provided buffer is not NULL, the assembler uses the provided buffer
501 // for code generation and assumes its size to be buffer_size. If the buffer
502 // is too small, a fatal error occurs. No deallocation of the buffer is done
503 // upon destruction of the assembler.
Ben Murdoch257744e2011-11-30 15:57:28 +0000504 Assembler(Isolate* isolate, void* buffer, int buffer_size);
Andrei Popescu31002712010-02-23 13:46:05 +0000505 ~Assembler();
506
Steve Block44f0eee2011-05-26 01:26:41 +0100507 // Overrides the default provided by FLAG_debug_code.
508 void set_emit_debug_code(bool value) { emit_debug_code_ = value; }
509
Andrei Popescu31002712010-02-23 13:46:05 +0000510 // GetCode emits any pending (non-emitted) code and fills the descriptor
511 // desc. GetCode() is idempotent; it returns the same result if no other
512 // Assembler functions are invoked in between GetCode() calls.
513 void GetCode(CodeDesc* desc);
514
515 // Label operations & relative jumps (PPUM Appendix D).
516 //
517 // Takes a branch opcode (cc) and a label (L) and generates
518 // either a backward branch or a forward branch and links it
519 // to the label fixup chain. Usage:
520 //
521 // Label L; // unbound label
522 // j(cc, &L); // forward branch to unbound label
523 // bind(&L); // bind label to the current pc
524 // j(cc, &L); // backward branch to bound label
525 // bind(&L); // illegal: a label may be bound only once
526 //
527 // Note: The same Label can be used for forward and backward branches
528 // but it may be bound only once.
Ben Murdoch257744e2011-11-30 15:57:28 +0000529 void bind(Label* L); // Binds an unbound label L to current code position.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000530 // Determines if Label is bound and near enough so that branch instruction
531 // can be used to reach it, instead of jump instruction.
532 bool is_near(Label* L);
Andrei Popescu31002712010-02-23 13:46:05 +0000533
Ben Murdoch257744e2011-11-30 15:57:28 +0000534 // Returns the branch offset to the given label from the current code
535 // position. Links the label to the current position if it is still unbound.
Andrei Popescu31002712010-02-23 13:46:05 +0000536 // Manages the jump elimination optimization if the second parameter is true.
537 int32_t branch_offset(Label* L, bool jump_elimination_allowed);
538 int32_t shifted_branch_offset(Label* L, bool jump_elimination_allowed) {
539 int32_t o = branch_offset(L, jump_elimination_allowed);
540 ASSERT((o & 3) == 0); // Assert the offset is aligned.
541 return o >> 2;
542 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000543 uint32_t jump_address(Label* L);
Andrei Popescu31002712010-02-23 13:46:05 +0000544
545 // Puts a labels target address at the given position.
546 // The high 8 bits are set to zero.
547 void label_at_put(Label* L, int at_offset);
548
Andrei Popescu31002712010-02-23 13:46:05 +0000549 // Read/Modify the code target address in the branch/call instruction at pc.
550 static Address target_address_at(Address pc);
551 static void set_target_address_at(Address pc, Address target);
552
Ben Murdoch589d6972011-11-30 16:04:58 +0000553 static void JumpLabelToJumpRegister(Address pc);
554
Andrei Popescu31002712010-02-23 13:46:05 +0000555 // This sets the branch destination (which gets loaded at the call address).
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100556 // This is for calls and branches within generated code. The serializer
557 // has already deserialized the lui/ori instructions etc.
558 inline static void deserialization_set_special_target_at(
559 Address instruction_payload, Address target) {
560 set_target_address_at(
561 instruction_payload - kInstructionsFor32BitConstant * kInstrSize,
562 target);
Andrei Popescu31002712010-02-23 13:46:05 +0000563 }
564
565 // This sets the branch destination.
566 // This is for calls and branches to runtime code.
567 inline static void set_external_target_at(Address instruction_payload,
568 Address target) {
569 set_target_address_at(instruction_payload, target);
570 }
571
Steve Block44f0eee2011-05-26 01:26:41 +0100572 // Size of an instruction.
573 static const int kInstrSize = sizeof(Instr);
574
575 // Difference between address of current opcode and target address offset.
576 static const int kBranchPCOffset = 4;
577
578 // Here we are patching the address in the LUI/ORI instruction pair.
579 // These values are used in the serialization process and must be zero for
580 // MIPS platform, as Code, Embedded Object or External-reference pointers
581 // are split across two consecutive instructions and don't exist separately
582 // in the code, so the serializer should not step forwards in memory after
583 // a target is resolved and written.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100584 static const int kSpecialTargetSize = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100585
586 // Number of consecutive instructions used to store 32bit constant.
Ben Murdoch589d6972011-11-30 16:04:58 +0000587 // Before jump-optimizations, this constant was used in
588 // RelocInfo::target_address_address() function to tell serializer address of
589 // the instruction that follows LUI/ORI instruction pair. Now, with new jump
590 // optimization, where jump-through-register instruction that usually
591 // follows LUI/ORI pair is substituted with J/JAL, this constant equals
592 // to 3 instructions (LUI+ORI+J/JAL/JR/JALR).
593 static const int kInstructionsFor32BitConstant = 3;
Andrei Popescu31002712010-02-23 13:46:05 +0000594
595 // Distance between the instruction referring to the address of the call
596 // target and the return address.
597 static const int kCallTargetAddressOffset = 4 * kInstrSize;
598
599 // Distance between start of patched return sequence and the emitted address
600 // to jump to.
Steve Block44f0eee2011-05-26 01:26:41 +0100601 static const int kPatchReturnSequenceAddressOffset = 0;
Andrei Popescu31002712010-02-23 13:46:05 +0000602
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100603 // Distance between start of patched debug break slot and the emitted address
604 // to jump to.
Steve Block44f0eee2011-05-26 01:26:41 +0100605 static const int kPatchDebugBreakSlotAddressOffset = 0 * kInstrSize;
606
607 // Difference between address of current opcode and value read from pc
608 // register.
609 static const int kPcLoadDelta = 4;
610
611 // Number of instructions used for the JS return sequence. The constant is
612 // used by the debugger to patch the JS return sequence.
613 static const int kJSReturnSequenceInstructions = 7;
614 static const int kDebugBreakSlotInstructions = 4;
615 static const int kDebugBreakSlotLength =
616 kDebugBreakSlotInstructions * kInstrSize;
617
Andrei Popescu31002712010-02-23 13:46:05 +0000618
619 // ---------------------------------------------------------------------------
620 // Code generation.
621
Steve Block44f0eee2011-05-26 01:26:41 +0100622 // Insert the smallest number of nop instructions
623 // possible to align the pc offset to a multiple
624 // of m. m must be a power of 2 (>= 4).
625 void Align(int m);
626 // Aligns code to something that's optimal for a jump target for the platform.
627 void CodeTargetAlign();
628
629 // Different nop operations are used by the code generator to detect certain
630 // states of the generated code.
631 enum NopMarkerTypes {
632 NON_MARKING_NOP = 0,
633 DEBUG_BREAK_NOP,
634 // IC markers.
635 PROPERTY_ACCESS_INLINED,
636 PROPERTY_ACCESS_INLINED_CONTEXT,
637 PROPERTY_ACCESS_INLINED_CONTEXT_DONT_DELETE,
638 // Helper values.
639 LAST_CODE_MARKER,
640 FIRST_IC_MARKER = PROPERTY_ACCESS_INLINED
641 };
642
Ben Murdoch257744e2011-11-30 15:57:28 +0000643 // Type == 0 is the default non-marking type.
Steve Block44f0eee2011-05-26 01:26:41 +0100644 void nop(unsigned int type = 0) {
645 ASSERT(type < 32);
646 sll(zero_reg, zero_reg, type, true);
647 }
Andrei Popescu31002712010-02-23 13:46:05 +0000648
649
Ben Murdoch257744e2011-11-30 15:57:28 +0000650 // --------Branch-and-jump-instructions----------
Andrei Popescu31002712010-02-23 13:46:05 +0000651 // We don't use likely variant of instructions.
652 void b(int16_t offset);
653 void b(Label* L) { b(branch_offset(L, false)>>2); }
654 void bal(int16_t offset);
655 void bal(Label* L) { bal(branch_offset(L, false)>>2); }
656
657 void beq(Register rs, Register rt, int16_t offset);
658 void beq(Register rs, Register rt, Label* L) {
659 beq(rs, rt, branch_offset(L, false) >> 2);
660 }
661 void bgez(Register rs, int16_t offset);
662 void bgezal(Register rs, int16_t offset);
663 void bgtz(Register rs, int16_t offset);
664 void blez(Register rs, int16_t offset);
665 void bltz(Register rs, int16_t offset);
666 void bltzal(Register rs, int16_t offset);
667 void bne(Register rs, Register rt, int16_t offset);
668 void bne(Register rs, Register rt, Label* L) {
669 bne(rs, rt, branch_offset(L, false)>>2);
670 }
671
672 // Never use the int16_t b(l)cond version with a branch offset
Ben Murdoch257744e2011-11-30 15:57:28 +0000673 // instead of using the Label* version.
Andrei Popescu31002712010-02-23 13:46:05 +0000674
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100675 // Jump targets must be in the current 256 MB-aligned region. i.e. 28 bits.
Andrei Popescu31002712010-02-23 13:46:05 +0000676 void j(int32_t target);
677 void jal(int32_t target);
678 void jalr(Register rs, Register rd = ra);
679 void jr(Register target);
Ben Murdoch589d6972011-11-30 16:04:58 +0000680 void j_or_jr(int32_t target, Register rs);
681 void jal_or_jalr(int32_t target, Register rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000682
683
684 //-------Data-processing-instructions---------
685
686 // Arithmetic.
Andrei Popescu31002712010-02-23 13:46:05 +0000687 void addu(Register rd, Register rs, Register rt);
Andrei Popescu31002712010-02-23 13:46:05 +0000688 void subu(Register rd, Register rs, Register rt);
689 void mult(Register rs, Register rt);
690 void multu(Register rs, Register rt);
691 void div(Register rs, Register rt);
692 void divu(Register rs, Register rt);
693 void mul(Register rd, Register rs, Register rt);
694
Andrei Popescu31002712010-02-23 13:46:05 +0000695 void addiu(Register rd, Register rs, int32_t j);
696
697 // Logical.
698 void and_(Register rd, Register rs, Register rt);
699 void or_(Register rd, Register rs, Register rt);
700 void xor_(Register rd, Register rs, Register rt);
701 void nor(Register rd, Register rs, Register rt);
702
703 void andi(Register rd, Register rs, int32_t j);
704 void ori(Register rd, Register rs, int32_t j);
705 void xori(Register rd, Register rs, int32_t j);
706 void lui(Register rd, int32_t j);
707
708 // Shifts.
Steve Block44f0eee2011-05-26 01:26:41 +0100709 // Please note: sll(zero_reg, zero_reg, x) instructions are reserved as nop
710 // and may cause problems in normal code. coming_from_nop makes sure this
711 // doesn't happen.
712 void sll(Register rd, Register rt, uint16_t sa, bool coming_from_nop = false);
Andrei Popescu31002712010-02-23 13:46:05 +0000713 void sllv(Register rd, Register rt, Register rs);
714 void srl(Register rd, Register rt, uint16_t sa);
715 void srlv(Register rd, Register rt, Register rs);
716 void sra(Register rt, Register rd, uint16_t sa);
717 void srav(Register rt, Register rd, Register rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100718 void rotr(Register rd, Register rt, uint16_t sa);
719 void rotrv(Register rd, Register rt, Register rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000720
721
722 //------------Memory-instructions-------------
723
724 void lb(Register rd, const MemOperand& rs);
725 void lbu(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100726 void lh(Register rd, const MemOperand& rs);
727 void lhu(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000728 void lw(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100729 void lwl(Register rd, const MemOperand& rs);
730 void lwr(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000731 void sb(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100732 void sh(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000733 void sw(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100734 void swl(Register rd, const MemOperand& rs);
735 void swr(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000736
737
738 //-------------Misc-instructions--------------
739
740 // Break / Trap instructions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000741 void break_(uint32_t code, bool break_as_stop = false);
742 void stop(const char* msg, uint32_t code = kMaxStopCode);
Andrei Popescu31002712010-02-23 13:46:05 +0000743 void tge(Register rs, Register rt, uint16_t code);
744 void tgeu(Register rs, Register rt, uint16_t code);
745 void tlt(Register rs, Register rt, uint16_t code);
746 void tltu(Register rs, Register rt, uint16_t code);
747 void teq(Register rs, Register rt, uint16_t code);
748 void tne(Register rs, Register rt, uint16_t code);
749
750 // Move from HI/LO register.
751 void mfhi(Register rd);
752 void mflo(Register rd);
753
754 // Set on less than.
755 void slt(Register rd, Register rs, Register rt);
756 void sltu(Register rd, Register rs, Register rt);
757 void slti(Register rd, Register rs, int32_t j);
758 void sltiu(Register rd, Register rs, int32_t j);
759
Steve Block44f0eee2011-05-26 01:26:41 +0100760 // Conditional move.
761 void movz(Register rd, Register rs, Register rt);
762 void movn(Register rd, Register rs, Register rt);
763 void movt(Register rd, Register rs, uint16_t cc = 0);
764 void movf(Register rd, Register rs, uint16_t cc = 0);
765
766 // Bit twiddling.
767 void clz(Register rd, Register rs);
768 void ins_(Register rt, Register rs, uint16_t pos, uint16_t size);
769 void ext_(Register rt, Register rs, uint16_t pos, uint16_t size);
Andrei Popescu31002712010-02-23 13:46:05 +0000770
771 //--------Coprocessor-instructions----------------
772
773 // Load, store, and move.
774 void lwc1(FPURegister fd, const MemOperand& src);
775 void ldc1(FPURegister fd, const MemOperand& src);
776
777 void swc1(FPURegister fs, const MemOperand& dst);
778 void sdc1(FPURegister fs, const MemOperand& dst);
779
Steve Block44f0eee2011-05-26 01:26:41 +0100780 void mtc1(Register rt, FPURegister fs);
781 void mfc1(Register rt, FPURegister fs);
782
783 void ctc1(Register rt, FPUControlRegister fs);
784 void cfc1(Register rt, FPUControlRegister fs);
785
786 // Arithmetic.
787 void add_d(FPURegister fd, FPURegister fs, FPURegister ft);
788 void sub_d(FPURegister fd, FPURegister fs, FPURegister ft);
789 void mul_d(FPURegister fd, FPURegister fs, FPURegister ft);
790 void div_d(FPURegister fd, FPURegister fs, FPURegister ft);
791 void abs_d(FPURegister fd, FPURegister fs);
792 void mov_d(FPURegister fd, FPURegister fs);
793 void neg_d(FPURegister fd, FPURegister fs);
794 void sqrt_d(FPURegister fd, FPURegister fs);
Andrei Popescu31002712010-02-23 13:46:05 +0000795
796 // Conversion.
797 void cvt_w_s(FPURegister fd, FPURegister fs);
798 void cvt_w_d(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100799 void trunc_w_s(FPURegister fd, FPURegister fs);
800 void trunc_w_d(FPURegister fd, FPURegister fs);
801 void round_w_s(FPURegister fd, FPURegister fs);
802 void round_w_d(FPURegister fd, FPURegister fs);
803 void floor_w_s(FPURegister fd, FPURegister fs);
804 void floor_w_d(FPURegister fd, FPURegister fs);
805 void ceil_w_s(FPURegister fd, FPURegister fs);
806 void ceil_w_d(FPURegister fd, FPURegister fs);
Andrei Popescu31002712010-02-23 13:46:05 +0000807
808 void cvt_l_s(FPURegister fd, FPURegister fs);
809 void cvt_l_d(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100810 void trunc_l_s(FPURegister fd, FPURegister fs);
811 void trunc_l_d(FPURegister fd, FPURegister fs);
812 void round_l_s(FPURegister fd, FPURegister fs);
813 void round_l_d(FPURegister fd, FPURegister fs);
814 void floor_l_s(FPURegister fd, FPURegister fs);
815 void floor_l_d(FPURegister fd, FPURegister fs);
816 void ceil_l_s(FPURegister fd, FPURegister fs);
817 void ceil_l_d(FPURegister fd, FPURegister fs);
Andrei Popescu31002712010-02-23 13:46:05 +0000818
819 void cvt_s_w(FPURegister fd, FPURegister fs);
820 void cvt_s_l(FPURegister fd, FPURegister fs);
821 void cvt_s_d(FPURegister fd, FPURegister fs);
822
823 void cvt_d_w(FPURegister fd, FPURegister fs);
824 void cvt_d_l(FPURegister fd, FPURegister fs);
825 void cvt_d_s(FPURegister fd, FPURegister fs);
826
827 // Conditions and branches.
828 void c(FPUCondition cond, SecondaryField fmt,
829 FPURegister ft, FPURegister fs, uint16_t cc = 0);
830
831 void bc1f(int16_t offset, uint16_t cc = 0);
832 void bc1f(Label* L, uint16_t cc = 0) { bc1f(branch_offset(L, false)>>2, cc); }
833 void bc1t(int16_t offset, uint16_t cc = 0);
834 void bc1t(Label* L, uint16_t cc = 0) { bc1t(branch_offset(L, false)>>2, cc); }
Steve Block44f0eee2011-05-26 01:26:41 +0100835 void fcmp(FPURegister src1, const double src2, FPUCondition cond);
Andrei Popescu31002712010-02-23 13:46:05 +0000836
837 // Check the code size generated from label to here.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000838 int SizeOfCodeGeneratedSince(Label* label) {
839 return pc_offset() - label->pos();
840 }
841
842 // Check the number of instructions generated from label to here.
843 int InstructionsGeneratedSince(Label* label) {
844 return SizeOfCodeGeneratedSince(label) / kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +0000845 }
846
Steve Block44f0eee2011-05-26 01:26:41 +0100847 // Class for scoping postponing the trampoline pool generation.
848 class BlockTrampolinePoolScope {
849 public:
850 explicit BlockTrampolinePoolScope(Assembler* assem) : assem_(assem) {
851 assem_->StartBlockTrampolinePool();
852 }
853 ~BlockTrampolinePoolScope() {
854 assem_->EndBlockTrampolinePool();
855 }
856
857 private:
858 Assembler* assem_;
859
860 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockTrampolinePoolScope);
861 };
862
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000863 // Class for postponing the assembly buffer growth. Typically used for
864 // sequences of instructions that must be emitted as a unit, before
865 // buffer growth (and relocation) can occur.
866 // This blocking scope is not nestable.
867 class BlockGrowBufferScope {
868 public:
869 explicit BlockGrowBufferScope(Assembler* assem) : assem_(assem) {
870 assem_->StartBlockGrowBuffer();
871 }
872 ~BlockGrowBufferScope() {
873 assem_->EndBlockGrowBuffer();
874 }
875
876 private:
877 Assembler* assem_;
878
879 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockGrowBufferScope);
880 };
881
Andrei Popescu31002712010-02-23 13:46:05 +0000882 // Debugging.
883
884 // Mark address of the ExitJSFrame code.
885 void RecordJSReturn();
886
Steve Block44f0eee2011-05-26 01:26:41 +0100887 // Mark address of a debug break slot.
888 void RecordDebugBreakSlot();
889
Ben Murdoch257744e2011-11-30 15:57:28 +0000890 // Record the AST id of the CallIC being compiled, so that it can be placed
891 // in the relocation information.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000892 void SetRecordedAstId(unsigned ast_id) {
893 ASSERT(recorded_ast_id_ == kNoASTId);
894 recorded_ast_id_ = ast_id;
895 }
896
897 unsigned RecordedAstId() {
898 ASSERT(recorded_ast_id_ != kNoASTId);
899 return recorded_ast_id_;
900 }
901
902 void ClearRecordedAstId() { recorded_ast_id_ = kNoASTId; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000903
Andrei Popescu31002712010-02-23 13:46:05 +0000904 // Record a comment relocation entry that can be used by a disassembler.
Steve Block44f0eee2011-05-26 01:26:41 +0100905 // Use --code-comments to enable.
Andrei Popescu31002712010-02-23 13:46:05 +0000906 void RecordComment(const char* msg);
907
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000908 static int RelocateInternalReference(byte* pc, intptr_t pc_delta);
909
Steve Block44f0eee2011-05-26 01:26:41 +0100910 // Writes a single byte or word of data in the code stream. Used for
911 // inline tables, e.g., jump-tables.
912 void db(uint8_t data);
913 void dd(uint32_t data);
Andrei Popescu31002712010-02-23 13:46:05 +0000914
915 int32_t pc_offset() const { return pc_ - buffer_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100916
917 PositionsRecorder* positions_recorder() { return &positions_recorder_; }
918
Steve Block44f0eee2011-05-26 01:26:41 +0100919 // Postpone the generation of the trampoline pool for the specified number of
920 // instructions.
921 void BlockTrampolinePoolFor(int instructions);
922
Andrei Popescu31002712010-02-23 13:46:05 +0000923 // Check if there is less than kGap bytes available in the buffer.
924 // If this is the case, we need to grow the buffer before emitting
925 // an instruction or relocation information.
926 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
927
928 // Get the number of bytes available in the buffer.
929 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
930
Andrei Popescu31002712010-02-23 13:46:05 +0000931 // Read/patch instructions.
932 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
Steve Block44f0eee2011-05-26 01:26:41 +0100933 static void instr_at_put(byte* pc, Instr instr) {
Andrei Popescu31002712010-02-23 13:46:05 +0000934 *reinterpret_cast<Instr*>(pc) = instr;
935 }
936 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
937 void instr_at_put(int pos, Instr instr) {
938 *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
939 }
940
941 // Check if an instruction is a branch of some kind.
Steve Block44f0eee2011-05-26 01:26:41 +0100942 static bool IsBranch(Instr instr);
Ben Murdoch257744e2011-11-30 15:57:28 +0000943 static bool IsBeq(Instr instr);
944 static bool IsBne(Instr instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100945
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000946 static bool IsJump(Instr instr);
947 static bool IsJ(Instr instr);
948 static bool IsLui(Instr instr);
949 static bool IsOri(Instr instr);
950
Ben Murdoch589d6972011-11-30 16:04:58 +0000951 static bool IsJal(Instr instr);
952 static bool IsJr(Instr instr);
953 static bool IsJalr(Instr instr);
954
Steve Block44f0eee2011-05-26 01:26:41 +0100955 static bool IsNop(Instr instr, unsigned int type);
956 static bool IsPop(Instr instr);
957 static bool IsPush(Instr instr);
958 static bool IsLwRegFpOffset(Instr instr);
959 static bool IsSwRegFpOffset(Instr instr);
960 static bool IsLwRegFpNegOffset(Instr instr);
961 static bool IsSwRegFpNegOffset(Instr instr);
962
Ben Murdoch257744e2011-11-30 15:57:28 +0000963 static Register GetRtReg(Instr instr);
964 static Register GetRsReg(Instr instr);
965 static Register GetRdReg(Instr instr);
966
967 static uint32_t GetRt(Instr instr);
968 static uint32_t GetRtField(Instr instr);
969 static uint32_t GetRs(Instr instr);
970 static uint32_t GetRsField(Instr instr);
971 static uint32_t GetRd(Instr instr);
972 static uint32_t GetRdField(Instr instr);
973 static uint32_t GetSa(Instr instr);
974 static uint32_t GetSaField(Instr instr);
975 static uint32_t GetOpcodeField(Instr instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000976 static uint32_t GetFunction(Instr instr);
977 static uint32_t GetFunctionField(Instr instr);
Ben Murdoch257744e2011-11-30 15:57:28 +0000978 static uint32_t GetImmediate16(Instr instr);
979 static uint32_t GetLabelConst(Instr instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100980
981 static int32_t GetBranchOffset(Instr instr);
982 static bool IsLw(Instr instr);
983 static int16_t GetLwOffset(Instr instr);
984 static Instr SetLwOffset(Instr instr, int16_t offset);
985
986 static bool IsSw(Instr instr);
987 static Instr SetSwOffset(Instr instr, int16_t offset);
988 static bool IsAddImmediate(Instr instr);
989 static Instr SetAddImmediateOffset(Instr instr, int16_t offset);
990
Ben Murdoch257744e2011-11-30 15:57:28 +0000991 static bool IsAndImmediate(Instr instr);
992
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000993 void CheckTrampolinePool();
Steve Block44f0eee2011-05-26 01:26:41 +0100994
995 protected:
Ben Murdoch257744e2011-11-30 15:57:28 +0000996 // Relocation for a type-recording IC has the AST id added to it. This
997 // member variable is a way to pass the information from the call site to
998 // the relocation info.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000999 unsigned recorded_ast_id_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001000
Steve Block44f0eee2011-05-26 01:26:41 +01001001 bool emit_debug_code() const { return emit_debug_code_; }
1002
1003 int32_t buffer_space() const { return reloc_info_writer.pos() - pc_; }
Andrei Popescu31002712010-02-23 13:46:05 +00001004
1005 // Decode branch instruction at pos and return branch target pos.
1006 int target_at(int32_t pos);
1007
1008 // Patch branch instruction at pos to branch to given branch target pos.
1009 void target_at_put(int32_t pos, int32_t target_pos);
1010
1011 // Say if we need to relocate with this mode.
Steve Block44f0eee2011-05-26 01:26:41 +01001012 bool MustUseReg(RelocInfo::Mode rmode);
Andrei Popescu31002712010-02-23 13:46:05 +00001013
1014 // Record reloc info for current pc_.
1015 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1016
Steve Block44f0eee2011-05-26 01:26:41 +01001017 // Block the emission of the trampoline pool before pc_offset.
1018 void BlockTrampolinePoolBefore(int pc_offset) {
1019 if (no_trampoline_pool_before_ < pc_offset)
1020 no_trampoline_pool_before_ = pc_offset;
1021 }
1022
1023 void StartBlockTrampolinePool() {
1024 trampoline_pool_blocked_nesting_++;
1025 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001026
Steve Block44f0eee2011-05-26 01:26:41 +01001027 void EndBlockTrampolinePool() {
1028 trampoline_pool_blocked_nesting_--;
1029 }
1030
1031 bool is_trampoline_pool_blocked() const {
1032 return trampoline_pool_blocked_nesting_ > 0;
1033 }
1034
Ben Murdoch257744e2011-11-30 15:57:28 +00001035 bool has_exception() const {
1036 return internal_trampoline_exception_;
1037 }
1038
Ben Murdoch589d6972011-11-30 16:04:58 +00001039 void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi);
1040
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001041 bool is_trampoline_emitted() const {
1042 return trampoline_emitted_;
1043 }
1044
1045 // Temporarily block automatic assembly buffer growth.
1046 void StartBlockGrowBuffer() {
1047 ASSERT(!block_buffer_growth_);
1048 block_buffer_growth_ = true;
1049 }
1050
1051 void EndBlockGrowBuffer() {
1052 ASSERT(block_buffer_growth_);
1053 block_buffer_growth_ = false;
1054 }
1055
1056 bool is_buffer_growth_blocked() const {
1057 return block_buffer_growth_;
1058 }
1059
Andrei Popescu31002712010-02-23 13:46:05 +00001060 private:
1061 // Code buffer:
1062 // The buffer into which code and relocation info are generated.
1063 byte* buffer_;
1064 int buffer_size_;
1065 // True if the assembler owns the buffer, false if buffer is external.
1066 bool own_buffer_;
1067
1068 // Buffer size and constant pool distance are checked together at regular
1069 // intervals of kBufferCheckInterval emitted bytes.
1070 static const int kBufferCheckInterval = 1*KB/2;
1071
1072 // Code generation.
1073 // The relocation writer's position is at least kGap bytes below the end of
1074 // the generated instructions. This is so that multi-instruction sequences do
1075 // not have to check for overflow. The same is true for writes of large
1076 // relocation info entries.
1077 static const int kGap = 32;
1078 byte* pc_; // The program counter - moves forward.
1079
Steve Block44f0eee2011-05-26 01:26:41 +01001080
1081 // Repeated checking whether the trampoline pool should be emitted is rather
1082 // expensive. By default we only check again once a number of instructions
1083 // has been generated.
1084 static const int kCheckConstIntervalInst = 32;
1085 static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize;
1086
1087 int next_buffer_check_; // pc offset of next buffer check.
1088
1089 // Emission of the trampoline pool may be blocked in some code sequences.
1090 int trampoline_pool_blocked_nesting_; // Block emission if this is not zero.
1091 int no_trampoline_pool_before_; // Block emission before this pc offset.
1092
1093 // Keep track of the last emitted pool to guarantee a maximal distance.
1094 int last_trampoline_pool_end_; // pc offset of the end of the last pool.
1095
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001096 // Automatic growth of the assembly buffer may be blocked for some sequences.
1097 bool block_buffer_growth_; // Block growth when true.
1098
Andrei Popescu31002712010-02-23 13:46:05 +00001099 // Relocation information generation.
1100 // Each relocation is encoded as a variable size value.
1101 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
1102 RelocInfoWriter reloc_info_writer;
1103
1104 // The bound position, before this we cannot do instruction elimination.
1105 int last_bound_pos_;
1106
Andrei Popescu31002712010-02-23 13:46:05 +00001107 // Code emission.
1108 inline void CheckBuffer();
1109 void GrowBuffer();
1110 inline void emit(Instr x);
Steve Block44f0eee2011-05-26 01:26:41 +01001111 inline void CheckTrampolinePoolQuick();
Andrei Popescu31002712010-02-23 13:46:05 +00001112
1113 // Instruction generation.
1114 // We have 3 different kind of encoding layout on MIPS.
1115 // However due to many different types of objects encoded in the same fields
1116 // we have quite a few aliases for each mode.
1117 // Using the same structure to refer to Register and FPURegister would spare a
1118 // few aliases, but mixing both does not look clean to me.
1119 // Anyway we could surely implement this differently.
1120
1121 void GenInstrRegister(Opcode opcode,
1122 Register rs,
1123 Register rt,
1124 Register rd,
1125 uint16_t sa = 0,
1126 SecondaryField func = NULLSF);
1127
1128 void GenInstrRegister(Opcode opcode,
Steve Block44f0eee2011-05-26 01:26:41 +01001129 Register rs,
1130 Register rt,
1131 uint16_t msb,
1132 uint16_t lsb,
1133 SecondaryField func);
1134
1135 void GenInstrRegister(Opcode opcode,
Andrei Popescu31002712010-02-23 13:46:05 +00001136 SecondaryField fmt,
1137 FPURegister ft,
1138 FPURegister fs,
1139 FPURegister fd,
1140 SecondaryField func = NULLSF);
1141
1142 void GenInstrRegister(Opcode opcode,
1143 SecondaryField fmt,
1144 Register rt,
1145 FPURegister fs,
1146 FPURegister fd,
1147 SecondaryField func = NULLSF);
1148
Steve Block44f0eee2011-05-26 01:26:41 +01001149 void GenInstrRegister(Opcode opcode,
1150 SecondaryField fmt,
1151 Register rt,
1152 FPUControlRegister fs,
1153 SecondaryField func = NULLSF);
1154
Andrei Popescu31002712010-02-23 13:46:05 +00001155
1156 void GenInstrImmediate(Opcode opcode,
1157 Register rs,
1158 Register rt,
1159 int32_t j);
1160 void GenInstrImmediate(Opcode opcode,
1161 Register rs,
1162 SecondaryField SF,
1163 int32_t j);
1164 void GenInstrImmediate(Opcode opcode,
1165 Register r1,
1166 FPURegister r2,
1167 int32_t j);
1168
1169
1170 void GenInstrJump(Opcode opcode,
1171 uint32_t address);
1172
Steve Block44f0eee2011-05-26 01:26:41 +01001173 // Helpers.
1174 void LoadRegPlusOffsetToAt(const MemOperand& src);
Andrei Popescu31002712010-02-23 13:46:05 +00001175
1176 // Labels.
1177 void print(Label* L);
1178 void bind_to(Label* L, int pos);
Andrei Popescu31002712010-02-23 13:46:05 +00001179 void next(Label* L);
1180
Steve Block44f0eee2011-05-26 01:26:41 +01001181 // One trampoline consists of:
1182 // - space for trampoline slots,
1183 // - space for labels.
1184 //
1185 // Space for trampoline slots is equal to slot_count * 2 * kInstrSize.
1186 // Space for trampoline slots preceeds space for labels. Each label is of one
1187 // instruction size, so total amount for labels is equal to
1188 // label_count * kInstrSize.
1189 class Trampoline {
1190 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001191 Trampoline() {
1192 start_ = 0;
1193 next_slot_ = 0;
1194 free_slot_count_ = 0;
1195 end_ = 0;
1196 }
1197 Trampoline(int start, int slot_count) {
Steve Block44f0eee2011-05-26 01:26:41 +01001198 start_ = start;
1199 next_slot_ = start;
1200 free_slot_count_ = slot_count;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001201 end_ = start + slot_count * kTrampolineSlotsSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001202 }
1203 int start() {
1204 return start_;
1205 }
1206 int end() {
1207 return end_;
1208 }
1209 int take_slot() {
Ben Murdoch257744e2011-11-30 15:57:28 +00001210 int trampoline_slot = kInvalidSlotPos;
1211 if (free_slot_count_ <= 0) {
1212 // We have run out of space on trampolines.
1213 // Make sure we fail in debug mode, so we become aware of each case
1214 // when this happens.
1215 ASSERT(0);
1216 // Internal exception will be caught.
1217 } else {
1218 trampoline_slot = next_slot_;
1219 free_slot_count_--;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001220 next_slot_ += kTrampolineSlotsSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00001221 }
Steve Block44f0eee2011-05-26 01:26:41 +01001222 return trampoline_slot;
1223 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001224
Steve Block44f0eee2011-05-26 01:26:41 +01001225 private:
1226 int start_;
1227 int end_;
1228 int next_slot_;
1229 int free_slot_count_;
Steve Block44f0eee2011-05-26 01:26:41 +01001230 };
1231
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001232 int32_t get_trampoline_entry(int32_t pos);
1233 int unbound_labels_count_;
1234 // If trampoline is emitted, generated code is becoming large. As this is
1235 // already a slow case which can possibly break our code generation for the
1236 // extreme case, we use this information to trigger different mode of
1237 // branch instruction generation, where we use jump instructions rather
1238 // than regular branch instructions.
1239 bool trampoline_emitted_;
1240 static const int kTrampolineSlotsSize = 4 * kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001241 static const int kMaxBranchOffset = (1 << (18 - 1)) - 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00001242 static const int kInvalidSlotPos = -1;
Steve Block44f0eee2011-05-26 01:26:41 +01001243
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001244 Trampoline trampoline_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001245 bool internal_trampoline_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +01001246
Andrei Popescu31002712010-02-23 13:46:05 +00001247 friend class RegExpMacroAssemblerMIPS;
1248 friend class RelocInfo;
Steve Block44f0eee2011-05-26 01:26:41 +01001249 friend class CodePatcher;
1250 friend class BlockTrampolinePoolScope;
1251
1252 PositionsRecorder positions_recorder_;
Steve Block44f0eee2011-05-26 01:26:41 +01001253 bool emit_debug_code_;
1254 friend class PositionsRecorder;
1255 friend class EnsureSpace;
1256};
1257
1258
1259class EnsureSpace BASE_EMBEDDED {
1260 public:
1261 explicit EnsureSpace(Assembler* assembler) {
1262 assembler->CheckBuffer();
1263 }
Andrei Popescu31002712010-02-23 13:46:05 +00001264};
1265
1266} } // namespace v8::internal
1267
1268#endif // V8_ARM_ASSEMBLER_MIPS_H_