blob: c968ffcd151f47515cc827be75bb726e5b41cb9a [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Andrei Popescu31002712010-02-23 13:46:05 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_MIPS_MACRO_ASSEMBLER_MIPS_H_
29#define V8_MIPS_MACRO_ASSEMBLER_MIPS_H_
30
31#include "assembler.h"
32#include "mips/assembler-mips.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "v8globals.h"
Andrei Popescu31002712010-02-23 13:46:05 +000034
35namespace v8 {
36namespace internal {
37
38// Forward declaration.
39class JumpTarget;
40
Steve Block44f0eee2011-05-26 01:26:41 +010041// Reserved Register Usage Summary.
42//
43// Registers t8, t9, and at are reserved for use by the MacroAssembler.
44//
45// The programmer should know that the MacroAssembler may clobber these three,
46// but won't touch other registers except in special cases.
47//
48// Per the MIPS ABI, register t9 must be used for indirect function call
49// via 'jalr t9' or 'jr t9' instructions. This is relied upon by gcc when
50// trying to update gp register for position-independent-code. Whenever
51// MIPS generated code calls C code, it must be via t9 register.
Andrei Popescu31002712010-02-23 13:46:05 +000052
Ben Murdoch85b71792012-04-11 18:30:58 +010053// Registers aliases
54// cp is assumed to be a callee saved register.
55const Register roots = s6; // Roots array pointer.
56const Register cp = s7; // JavaScript context pointer.
57const Register fp = s8_fp; // Alias for fp.
58// Registers used for condition evaluation.
59const Register condReg1 = s4;
60const Register condReg2 = s5;
61
Ben Murdoch592a9fc2012-03-05 11:04:45 +000062
Steve Block44f0eee2011-05-26 01:26:41 +010063// Flags used for the AllocateInNewSpace functions.
64enum AllocationFlags {
65 // No special flags.
66 NO_ALLOCATION_FLAGS = 0,
67 // Return the pointer to the allocated already tagged as a heap object.
68 TAG_OBJECT = 1 << 0,
69 // The content of the result register already contains the allocation top in
70 // new space.
71 RESULT_CONTAINS_TOP = 1 << 1,
72 // Specify that the requested size of the space to allocate is specified in
73 // words instead of bytes.
74 SIZE_IN_WORDS = 1 << 2
75};
76
77// Flags used for the ObjectToDoubleFPURegister function.
78enum ObjectToDoubleFlags {
79 // No special flags.
80 NO_OBJECT_TO_DOUBLE_FLAGS = 0,
81 // Object is known to be a non smi.
82 OBJECT_NOT_SMI = 1 << 0,
83 // Don't load NaNs or infinities, branch to the non number case instead.
84 AVOID_NANS_AND_INFINITIES = 1 << 1
85};
86
87// Allow programmer to use Branch Delay Slot of Branches, Jumps, Calls.
88enum BranchDelaySlot {
89 USE_DELAY_SLOT,
90 PROTECT
91};
92
Andrei Popescu31002712010-02-23 13:46:05 +000093// MacroAssembler implements a collection of frequently used macros.
94class MacroAssembler: public Assembler {
95 public:
Ben Murdoch257744e2011-11-30 15:57:28 +000096 // The isolate parameter can be NULL if the macro assembler should
97 // not use isolate-dependent functionality. In this case, it's the
98 // responsibility of the caller to never invoke such function on the
99 // macro assembler.
100 MacroAssembler(Isolate* isolate, void* buffer, int size);
Andrei Popescu31002712010-02-23 13:46:05 +0000101
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000102 // Arguments macros.
Steve Block44f0eee2011-05-26 01:26:41 +0100103#define COND_TYPED_ARGS Condition cond, Register r1, const Operand& r2
104#define COND_ARGS cond, r1, r2
105
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000106 // Cases when relocation is not needed.
Steve Block44f0eee2011-05-26 01:26:41 +0100107#define DECLARE_NORELOC_PROTOTYPE(Name, target_type) \
108 void Name(target_type target, BranchDelaySlot bd = PROTECT); \
109 inline void Name(BranchDelaySlot bd, target_type target) { \
110 Name(target, bd); \
111 } \
112 void Name(target_type target, \
113 COND_TYPED_ARGS, \
114 BranchDelaySlot bd = PROTECT); \
115 inline void Name(BranchDelaySlot bd, \
116 target_type target, \
117 COND_TYPED_ARGS) { \
118 Name(target, COND_ARGS, bd); \
119 }
120
Steve Block44f0eee2011-05-26 01:26:41 +0100121#define DECLARE_BRANCH_PROTOTYPES(Name) \
122 DECLARE_NORELOC_PROTOTYPE(Name, Label*) \
123 DECLARE_NORELOC_PROTOTYPE(Name, int16_t)
124
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125 DECLARE_BRANCH_PROTOTYPES(Branch)
126 DECLARE_BRANCH_PROTOTYPES(BranchAndLink)
Steve Block44f0eee2011-05-26 01:26:41 +0100127
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128#undef DECLARE_BRANCH_PROTOTYPES
Steve Block44f0eee2011-05-26 01:26:41 +0100129#undef COND_TYPED_ARGS
130#undef COND_ARGS
Andrei Popescu31002712010-02-23 13:46:05 +0000131
Ben Murdoch257744e2011-11-30 15:57:28 +0000132
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000133 // Jump, Call, and Ret pseudo instructions implementing inter-working.
134#define COND_ARGS Condition cond = al, Register rs = zero_reg, \
135 const Operand& rt = Operand(zero_reg), BranchDelaySlot bd = PROTECT
136
137 void Jump(Register target, COND_ARGS);
138 void Jump(intptr_t target, RelocInfo::Mode rmode, COND_ARGS);
139 void Jump(Address target, RelocInfo::Mode rmode, COND_ARGS);
140 void Jump(Handle<Code> code, RelocInfo::Mode rmode, COND_ARGS);
Ben Murdoch85b71792012-04-11 18:30:58 +0100141 int CallSize(Register target, COND_ARGS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000142 void Call(Register target, COND_ARGS);
Ben Murdoch85b71792012-04-11 18:30:58 +0100143 int CallSize(Address target, RelocInfo::Mode rmode, COND_ARGS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000144 void Call(Address target, RelocInfo::Mode rmode, COND_ARGS);
Ben Murdoch85b71792012-04-11 18:30:58 +0100145 int CallSize(Handle<Code> code,
146 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
147 unsigned ast_id = kNoASTId,
148 COND_ARGS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000149 void Call(Handle<Code> code,
150 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
151 unsigned ast_id = kNoASTId,
152 COND_ARGS);
153 void Ret(COND_ARGS);
Ben Murdoch85b71792012-04-11 18:30:58 +0100154 inline void Ret(BranchDelaySlot bd) {
155 Ret(al, zero_reg, Operand(zero_reg), bd);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000156 }
157
158#undef COND_ARGS
Ben Murdoch257744e2011-11-30 15:57:28 +0000159
Andrei Popescu31002712010-02-23 13:46:05 +0000160 // Emit code to discard a non-negative number of pointer-sized elements
161 // from the stack, clobbering only the sp register.
Steve Block44f0eee2011-05-26 01:26:41 +0100162 void Drop(int count,
163 Condition cond = cc_always,
164 Register reg = no_reg,
165 const Operand& op = Operand(no_reg));
166
Ben Murdoch85b71792012-04-11 18:30:58 +0100167 void DropAndRet(int drop = 0,
168 Condition cond = cc_always,
169 Register reg = no_reg,
170 const Operand& op = Operand(no_reg));
Steve Block44f0eee2011-05-26 01:26:41 +0100171
172 // Swap two registers. If the scratch register is omitted then a slightly
173 // less efficient form using xor instead of mov is emitted.
174 void Swap(Register reg1, Register reg2, Register scratch = no_reg);
Andrei Popescu31002712010-02-23 13:46:05 +0000175
176 void Call(Label* target);
Steve Block44f0eee2011-05-26 01:26:41 +0100177
Ben Murdoch257744e2011-11-30 15:57:28 +0000178 inline void Move(Register dst, Register src) {
179 if (!dst.is(src)) {
180 mov(dst, src);
181 }
182 }
183
184 inline void Move(FPURegister dst, FPURegister src) {
185 if (!dst.is(src)) {
186 mov_d(dst, src);
187 }
188 }
189
190 inline void Move(Register dst_low, Register dst_high, FPURegister src) {
191 mfc1(dst_low, src);
192 mfc1(dst_high, FPURegister::from_code(src.code() + 1));
193 }
194
195 inline void Move(FPURegister dst, Register src_low, Register src_high) {
196 mtc1(src_low, dst);
197 mtc1(src_high, FPURegister::from_code(dst.code() + 1));
198 }
Andrei Popescu31002712010-02-23 13:46:05 +0000199
200 // Jump unconditionally to given label.
201 // We NEED a nop in the branch delay slot, as it used by v8, for example in
202 // CodeGenerator::ProcessDeferred().
Steve Block6ded16b2010-05-10 14:33:55 +0100203 // Currently the branch delay slot is filled by the MacroAssembler.
Andrei Popescu31002712010-02-23 13:46:05 +0000204 // Use rather b(Label) for code generation.
205 void jmp(Label* L) {
Steve Block44f0eee2011-05-26 01:26:41 +0100206 Branch(L);
Andrei Popescu31002712010-02-23 13:46:05 +0000207 }
208
209 // Load an object from the root table.
210 void LoadRoot(Register destination,
211 Heap::RootListIndex index);
212 void LoadRoot(Register destination,
213 Heap::RootListIndex index,
214 Condition cond, Register src1, const Operand& src2);
215
Steve Block44f0eee2011-05-26 01:26:41 +0100216 // Store an object to the root table.
217 void StoreRoot(Register source,
218 Heap::RootListIndex index);
219 void StoreRoot(Register source,
220 Heap::RootListIndex index,
221 Condition cond, Register src1, const Operand& src2);
222
Ben Murdochc7cc0282012-03-05 14:35:55 +0000223
Ben Murdoch85b71792012-04-11 18:30:58 +0100224 // Check if object is in new space.
225 // scratch can be object itself, but it will be clobbered.
226 void InNewSpace(Register object,
227 Register scratch,
228 Condition cc, // eq for new space, ne otherwise.
229 Label* branch);
Steve Block44f0eee2011-05-26 01:26:41 +0100230
231
Ben Murdoch85b71792012-04-11 18:30:58 +0100232 // For the page containing |object| mark the region covering [address]
233 // dirty. The object address must be in the first 8K of an allocated page.
234 void RecordWriteHelper(Register object,
235 Register address,
236 Register scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100237
Ben Murdoch85b71792012-04-11 18:30:58 +0100238 // For the page containing |object| mark the region covering
239 // [object+offset] dirty. The object address must be in the first 8K
240 // of an allocated page. The 'scratch' registers are used in the
241 // implementation and all 3 registers are clobbered by the
242 // operation, as well as the 'at' register. RecordWrite updates the
243 // write barrier even when storing smis.
244 void RecordWrite(Register object,
245 Operand offset,
Steve Block44f0eee2011-05-26 01:26:41 +0100246 Register scratch0,
Ben Murdoch85b71792012-04-11 18:30:58 +0100247 Register scratch1);
Steve Block44f0eee2011-05-26 01:26:41 +0100248
Ben Murdoch85b71792012-04-11 18:30:58 +0100249 // For the page containing |object| mark the region covering
250 // [address] dirty. The object address must be in the first 8K of an
251 // allocated page. All 3 registers are clobbered by the operation,
252 // as well as the ip register. RecordWrite updates the write barrier
253 // even when storing smis.
254 void RecordWrite(Register object,
255 Register address,
256 Register scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100257
258
259 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000260 // Inline caching support.
Steve Block44f0eee2011-05-26 01:26:41 +0100261
262 // Generate code for checking access rights - used for security checks
263 // on access to global objects across environments. The holder register
264 // is left untouched, whereas both scratch registers are clobbered.
265 void CheckAccessGlobalProxy(Register holder_reg,
266 Register scratch,
267 Label* miss);
268
Ben Murdochc7cc0282012-03-05 14:35:55 +0000269 void GetNumberHash(Register reg0, Register scratch);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000270
271 void LoadFromNumberDictionary(Label* miss,
272 Register elements,
273 Register key,
274 Register result,
275 Register reg0,
276 Register reg1,
277 Register reg2);
278
279
Steve Block44f0eee2011-05-26 01:26:41 +0100280 inline void MarkCode(NopMarkerTypes type) {
281 nop(type);
Steve Block6ded16b2010-05-10 14:33:55 +0100282 }
283
Steve Block44f0eee2011-05-26 01:26:41 +0100284 // Check if the given instruction is a 'type' marker.
Ben Murdoch85b71792012-04-11 18:30:58 +0100285 // ie. check if it is a sll zero_reg, zero_reg, <type> (referenced as
Steve Block44f0eee2011-05-26 01:26:41 +0100286 // nop(type)). These instructions are generated to mark special location in
287 // the code, like some special IC code.
288 static inline bool IsMarkedCode(Instr instr, int type) {
289 ASSERT((FIRST_IC_MARKER <= type) && (type < LAST_CODE_MARKER));
290 return IsNop(instr, type);
291 }
Andrei Popescu31002712010-02-23 13:46:05 +0000292
293
Steve Block44f0eee2011-05-26 01:26:41 +0100294 static inline int GetCodeMarker(Instr instr) {
295 uint32_t opcode = ((instr & kOpcodeMask));
296 uint32_t rt = ((instr & kRtFieldMask) >> kRtShift);
297 uint32_t rs = ((instr & kRsFieldMask) >> kRsShift);
298 uint32_t sa = ((instr & kSaFieldMask) >> kSaShift);
299
300 // Return <n> if we have a sll zero_reg, zero_reg, n
301 // else return -1.
302 bool sllzz = (opcode == SLL &&
303 rt == static_cast<uint32_t>(ToNumber(zero_reg)) &&
304 rs == static_cast<uint32_t>(ToNumber(zero_reg)));
305 int type =
306 (sllzz && FIRST_IC_MARKER <= sa && sa < LAST_CODE_MARKER) ? sa : -1;
307 ASSERT((type == -1) ||
308 ((FIRST_IC_MARKER <= type) && (type < LAST_CODE_MARKER)));
309 return type;
310 }
311
312
313
314 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000315 // Allocation support.
Steve Block44f0eee2011-05-26 01:26:41 +0100316
317 // Allocate an object in new space. The object_size is specified
318 // either in bytes or in words if the allocation flag SIZE_IN_WORDS
319 // is passed. If the new space is exhausted control continues at the
320 // gc_required label. The allocated object is returned in result. If
321 // the flag tag_allocated_object is true the result is tagged as as
322 // a heap object. All registers are clobbered also when control
323 // continues at the gc_required label.
324 void AllocateInNewSpace(int object_size,
325 Register result,
326 Register scratch1,
327 Register scratch2,
328 Label* gc_required,
329 AllocationFlags flags);
330 void AllocateInNewSpace(Register object_size,
331 Register result,
332 Register scratch1,
333 Register scratch2,
334 Label* gc_required,
335 AllocationFlags flags);
336
337 // Undo allocation in new space. The object passed and objects allocated after
338 // it will no longer be allocated. The caller must make sure that no pointers
339 // are left to the object(s) no longer allocated as they would be invalid when
340 // allocation is undone.
341 void UndoAllocationInNewSpace(Register object, Register scratch);
342
343
344 void AllocateTwoByteString(Register result,
345 Register length,
346 Register scratch1,
347 Register scratch2,
348 Register scratch3,
349 Label* gc_required);
350 void AllocateAsciiString(Register result,
351 Register length,
352 Register scratch1,
353 Register scratch2,
354 Register scratch3,
355 Label* gc_required);
356 void AllocateTwoByteConsString(Register result,
357 Register length,
358 Register scratch1,
359 Register scratch2,
360 Label* gc_required);
361 void AllocateAsciiConsString(Register result,
362 Register length,
363 Register scratch1,
364 Register scratch2,
365 Label* gc_required);
Ben Murdoch589d6972011-11-30 16:04:58 +0000366 void AllocateTwoByteSlicedString(Register result,
367 Register length,
368 Register scratch1,
369 Register scratch2,
370 Label* gc_required);
371 void AllocateAsciiSlicedString(Register result,
372 Register length,
373 Register scratch1,
374 Register scratch2,
375 Label* gc_required);
Steve Block44f0eee2011-05-26 01:26:41 +0100376
377 // Allocates a heap number or jumps to the gc_required label if the young
378 // space is full and a scavenge is needed. All registers are clobbered also
379 // when control continues at the gc_required label.
380 void AllocateHeapNumber(Register result,
381 Register scratch1,
382 Register scratch2,
383 Register heap_number_map,
384 Label* gc_required);
385 void AllocateHeapNumberWithValue(Register result,
386 FPURegister value,
387 Register scratch1,
388 Register scratch2,
389 Label* gc_required);
390
Andrei Popescu31002712010-02-23 13:46:05 +0000391 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000392 // Instruction macros.
Andrei Popescu31002712010-02-23 13:46:05 +0000393
Steve Block44f0eee2011-05-26 01:26:41 +0100394#define DEFINE_INSTRUCTION(instr) \
Andrei Popescu31002712010-02-23 13:46:05 +0000395 void instr(Register rd, Register rs, const Operand& rt); \
396 void instr(Register rd, Register rs, Register rt) { \
397 instr(rd, rs, Operand(rt)); \
398 } \
399 void instr(Register rs, Register rt, int32_t j) { \
400 instr(rs, rt, Operand(j)); \
401 }
402
Steve Block44f0eee2011-05-26 01:26:41 +0100403#define DEFINE_INSTRUCTION2(instr) \
Andrei Popescu31002712010-02-23 13:46:05 +0000404 void instr(Register rs, const Operand& rt); \
405 void instr(Register rs, Register rt) { \
406 instr(rs, Operand(rt)); \
407 } \
408 void instr(Register rs, int32_t j) { \
409 instr(rs, Operand(j)); \
410 }
411
Andrei Popescu31002712010-02-23 13:46:05 +0000412 DEFINE_INSTRUCTION(Addu);
Steve Block44f0eee2011-05-26 01:26:41 +0100413 DEFINE_INSTRUCTION(Subu);
Andrei Popescu31002712010-02-23 13:46:05 +0000414 DEFINE_INSTRUCTION(Mul);
415 DEFINE_INSTRUCTION2(Mult);
416 DEFINE_INSTRUCTION2(Multu);
417 DEFINE_INSTRUCTION2(Div);
418 DEFINE_INSTRUCTION2(Divu);
419
420 DEFINE_INSTRUCTION(And);
421 DEFINE_INSTRUCTION(Or);
422 DEFINE_INSTRUCTION(Xor);
423 DEFINE_INSTRUCTION(Nor);
Ben Murdoch257744e2011-11-30 15:57:28 +0000424 DEFINE_INSTRUCTION2(Neg);
Andrei Popescu31002712010-02-23 13:46:05 +0000425
426 DEFINE_INSTRUCTION(Slt);
427 DEFINE_INSTRUCTION(Sltu);
428
Steve Block44f0eee2011-05-26 01:26:41 +0100429 // MIPS32 R2 instruction macro.
430 DEFINE_INSTRUCTION(Ror);
431
Andrei Popescu31002712010-02-23 13:46:05 +0000432#undef DEFINE_INSTRUCTION
433#undef DEFINE_INSTRUCTION2
434
435
Ben Murdoch257744e2011-11-30 15:57:28 +0000436 // ---------------------------------------------------------------------------
437 // Pseudo-instructions.
Andrei Popescu31002712010-02-23 13:46:05 +0000438
439 void mov(Register rd, Register rt) { or_(rd, rt, zero_reg); }
Andrei Popescu31002712010-02-23 13:46:05 +0000440
Ben Murdoch257744e2011-11-30 15:57:28 +0000441 // Load int32 in the rd register.
Ben Murdoch85b71792012-04-11 18:30:58 +0100442 void li(Register rd, Operand j, bool gen2instr = false);
443 inline void li(Register rd, int32_t j, bool gen2instr = false) {
444 li(rd, Operand(j), gen2instr);
Andrei Popescu31002712010-02-23 13:46:05 +0000445 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100446 inline void li(Register dst, Handle<Object> value, bool gen2instr = false) {
447 li(dst, Operand(value), gen2instr);
Steve Block44f0eee2011-05-26 01:26:41 +0100448 }
Andrei Popescu31002712010-02-23 13:46:05 +0000449
Andrei Popescu31002712010-02-23 13:46:05 +0000450 // Push multiple registers on the stack.
Steve Block6ded16b2010-05-10 14:33:55 +0100451 // Registers are saved in numerical order, with higher numbered registers
Ben Murdoch257744e2011-11-30 15:57:28 +0000452 // saved in higher memory addresses.
Andrei Popescu31002712010-02-23 13:46:05 +0000453 void MultiPush(RegList regs);
454 void MultiPushReversed(RegList regs);
Steve Block44f0eee2011-05-26 01:26:41 +0100455
Ben Murdoch589d6972011-11-30 16:04:58 +0000456 void MultiPushFPU(RegList regs);
457 void MultiPushReversedFPU(RegList regs);
458
Ben Murdoch257744e2011-11-30 15:57:28 +0000459 // Lower case push() for compatibility with arch-independent code.
460 void push(Register src) {
Andrei Popescu31002712010-02-23 13:46:05 +0000461 Addu(sp, sp, Operand(-kPointerSize));
462 sw(src, MemOperand(sp, 0));
463 }
Steve Block44f0eee2011-05-26 01:26:41 +0100464
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000465 // Push a handle.
466 void Push(Handle<Object> handle);
467
Ben Murdoch257744e2011-11-30 15:57:28 +0000468 // Push two registers. Pushes leftmost register first (to highest address).
469 void Push(Register src1, Register src2) {
Steve Block44f0eee2011-05-26 01:26:41 +0100470 Subu(sp, sp, Operand(2 * kPointerSize));
471 sw(src1, MemOperand(sp, 1 * kPointerSize));
472 sw(src2, MemOperand(sp, 0 * kPointerSize));
473 }
474
Ben Murdoch257744e2011-11-30 15:57:28 +0000475 // Push three registers. Pushes leftmost register first (to highest address).
476 void Push(Register src1, Register src2, Register src3) {
477 Subu(sp, sp, Operand(3 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100478 sw(src1, MemOperand(sp, 2 * kPointerSize));
479 sw(src2, MemOperand(sp, 1 * kPointerSize));
480 sw(src3, MemOperand(sp, 0 * kPointerSize));
481 }
482
Ben Murdoch257744e2011-11-30 15:57:28 +0000483 // Push four registers. Pushes leftmost register first (to highest address).
484 void Push(Register src1, Register src2, Register src3, Register src4) {
485 Subu(sp, sp, Operand(4 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100486 sw(src1, MemOperand(sp, 3 * kPointerSize));
487 sw(src2, MemOperand(sp, 2 * kPointerSize));
488 sw(src3, MemOperand(sp, 1 * kPointerSize));
489 sw(src4, MemOperand(sp, 0 * kPointerSize));
490 }
491
Andrei Popescu31002712010-02-23 13:46:05 +0000492 void Push(Register src, Condition cond, Register tst1, Register tst2) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000493 // Since we don't have conditional execution we use a Branch.
Steve Block44f0eee2011-05-26 01:26:41 +0100494 Branch(3, cond, tst1, Operand(tst2));
Ben Murdoch257744e2011-11-30 15:57:28 +0000495 Subu(sp, sp, Operand(kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +0000496 sw(src, MemOperand(sp, 0));
497 }
498
499 // Pops multiple values from the stack and load them in the
500 // registers specified in regs. Pop order is the opposite as in MultiPush.
501 void MultiPop(RegList regs);
502 void MultiPopReversed(RegList regs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000503
Ben Murdoch589d6972011-11-30 16:04:58 +0000504 void MultiPopFPU(RegList regs);
505 void MultiPopReversedFPU(RegList regs);
506
Ben Murdoch257744e2011-11-30 15:57:28 +0000507 // Lower case pop() for compatibility with arch-independent code.
508 void pop(Register dst) {
Andrei Popescu31002712010-02-23 13:46:05 +0000509 lw(dst, MemOperand(sp, 0));
510 Addu(sp, sp, Operand(kPointerSize));
511 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000512
513 // Pop two registers. Pops rightmost register first (from lower address).
514 void Pop(Register src1, Register src2) {
515 ASSERT(!src1.is(src2));
516 lw(src2, MemOperand(sp, 0 * kPointerSize));
517 lw(src1, MemOperand(sp, 1 * kPointerSize));
518 Addu(sp, sp, 2 * kPointerSize);
519 }
520
Steve Block44f0eee2011-05-26 01:26:41 +0100521 void Pop(uint32_t count = 1) {
522 Addu(sp, sp, Operand(count * kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +0000523 }
524
Steve Block44f0eee2011-05-26 01:26:41 +0100525 // Push and pop the registers that can hold pointers, as defined by the
526 // RegList constant kSafepointSavedRegisters.
Ben Murdoch257744e2011-11-30 15:57:28 +0000527 void PushSafepointRegisters();
528 void PopSafepointRegisters();
529 void PushSafepointRegistersAndDoubles();
530 void PopSafepointRegistersAndDoubles();
531 // Store value in register src in the safepoint stack slot for
532 // register dst.
533 void StoreToSafepointRegisterSlot(Register src, Register dst);
534 void StoreToSafepointRegistersAndDoublesSlot(Register src, Register dst);
535 // Load the value of the src register from its safepoint stack slot
536 // into register dst.
537 void LoadFromSafepointRegisterSlot(Register dst, Register src);
Steve Block44f0eee2011-05-26 01:26:41 +0100538
539 // MIPS32 R2 instruction macro.
540 void Ins(Register rt, Register rs, uint16_t pos, uint16_t size);
541 void Ext(Register rt, Register rs, uint16_t pos, uint16_t size);
542
543 // Convert unsigned word to double.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000544 void Cvt_d_uw(FPURegister fd, FPURegister fs, FPURegister scratch);
545 void Cvt_d_uw(FPURegister fd, Register rs, FPURegister scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100546
547 // Convert double to unsigned word.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000548 void Trunc_uw_d(FPURegister fd, FPURegister fs, FPURegister scratch);
549 void Trunc_uw_d(FPURegister fd, Register rs, FPURegister scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100550
551 // Convert the HeapNumber pointed to by source to a 32bits signed integer
552 // dest. If the HeapNumber does not fit into a 32bits signed integer branch
553 // to not_int32 label. If FPU is available double_scratch is used but not
554 // scratch2.
555 void ConvertToInt32(Register source,
556 Register dest,
557 Register scratch,
558 Register scratch2,
559 FPURegister double_scratch,
560 Label *not_int32);
561
Ben Murdoch257744e2011-11-30 15:57:28 +0000562 // Helper for EmitECMATruncate.
563 // This will truncate a floating-point value outside of the singed 32bit
564 // integer range to a 32bit signed integer.
565 // Expects the double value loaded in input_high and input_low.
566 // Exits with the answer in 'result'.
567 // Note that this code does not work for values in the 32bit range!
568 void EmitOutOfInt32RangeTruncate(Register result,
569 Register input_high,
570 Register input_low,
571 Register scratch);
572
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000573 // Performs a truncating conversion of a floating point number as used by
574 // the JS bitwise operations. See ECMA-262 9.5: ToInt32.
575 // Exits with 'result' holding the answer and all other registers clobbered.
576 void EmitECMATruncate(Register result,
577 FPURegister double_input,
578 FPURegister single_scratch,
579 Register scratch,
580 Register scratch2,
581 Register scratch3);
582
Ben Murdoch85b71792012-04-11 18:30:58 +0100583 // -------------------------------------------------------------------------
584 // Activation frames.
585
586 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
587 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
588
589 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
590 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
591
Steve Block44f0eee2011-05-26 01:26:41 +0100592 // Enter exit frame.
Ben Murdoch257744e2011-11-30 15:57:28 +0000593 // argc - argument count to be dropped by LeaveExitFrame.
594 // save_doubles - saves FPU registers on stack, currently disabled.
595 // stack_space - extra stack space.
596 void EnterExitFrame(bool save_doubles,
597 int stack_space = 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100598
Ben Murdoch257744e2011-11-30 15:57:28 +0000599 // Leave the current exit frame.
Ben Murdoch85b71792012-04-11 18:30:58 +0100600 void LeaveExitFrame(bool save_doubles, Register arg_count);
Steve Block6ded16b2010-05-10 14:33:55 +0100601
Steve Block44f0eee2011-05-26 01:26:41 +0100602 // Get the actual activation frame alignment for target environment.
603 static int ActivationFrameAlignment();
Steve Block6ded16b2010-05-10 14:33:55 +0100604
Ben Murdoch257744e2011-11-30 15:57:28 +0000605 // Make sure the stack is aligned. Only emits code in debug mode.
606 void AssertStackIsAligned();
607
Steve Block44f0eee2011-05-26 01:26:41 +0100608 void LoadContext(Register dst, int context_chain_length);
Steve Block6ded16b2010-05-10 14:33:55 +0100609
Steve Block44f0eee2011-05-26 01:26:41 +0100610 void LoadGlobalFunction(int index, Register function);
611
612 // Load the initial map from the global function. The registers
613 // function and map can be the same, function is then overwritten.
614 void LoadGlobalFunctionInitialMap(Register function,
615 Register map,
616 Register scratch);
617
618 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000619 // JavaScript invokes.
620
Ben Murdoch85b71792012-04-11 18:30:58 +0100621 // Setup call kind marking in t1. The method takes t1 as an
Ben Murdoch257744e2011-11-30 15:57:28 +0000622 // explicit first parameter to make the code more readable at the
623 // call sites.
624 void SetCallKind(Register dst, CallKind kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100625
626 // Invoke the JavaScript function code by either calling or jumping.
627 void InvokeCode(Register code,
628 const ParameterCount& expected,
629 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +0100630 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000631 const CallWrapper& call_wrapper,
632 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100633
634 void InvokeCode(Handle<Code> code,
635 const ParameterCount& expected,
636 const ParameterCount& actual,
637 RelocInfo::Mode rmode,
Ben Murdoch257744e2011-11-30 15:57:28 +0000638 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000639 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100640
641 // Invoke the JavaScript function in the given register. Changes the
642 // current context to the context in the function before invoking.
643 void InvokeFunction(Register function,
644 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +0100645 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000646 const CallWrapper& call_wrapper,
647 CallKind call_kind);
Steve Block44f0eee2011-05-26 01:26:41 +0100648
Ben Murdoch85b71792012-04-11 18:30:58 +0100649 void InvokeFunction(JSFunction* function,
Steve Block44f0eee2011-05-26 01:26:41 +0100650 const ParameterCount& actual,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000651 InvokeFlag flag,
652 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100653
654
Steve Block44f0eee2011-05-26 01:26:41 +0100655 void IsObjectJSObjectType(Register heap_object,
656 Register map,
657 Register scratch,
658 Label* fail);
659
660 void IsInstanceJSObjectType(Register map,
661 Register scratch,
662 Label* fail);
663
664 void IsObjectJSStringType(Register object,
665 Register scratch,
666 Label* fail);
667
Steve Block6ded16b2010-05-10 14:33:55 +0100668#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100669 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000670 // Debugger Support.
Steve Block6ded16b2010-05-10 14:33:55 +0100671
Steve Block6ded16b2010-05-10 14:33:55 +0100672 void DebugBreak();
673#endif
674
Ben Murdoch85b71792012-04-11 18:30:58 +0100675 void InitializeRootRegister() {
676 ExternalReference roots_address =
677 ExternalReference::roots_address(isolate());
678 li(kRootRegister, Operand(roots_address));
679 }
Steve Block6ded16b2010-05-10 14:33:55 +0100680
Steve Block44f0eee2011-05-26 01:26:41 +0100681 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000682 // Exception handling.
Andrei Popescu31002712010-02-23 13:46:05 +0000683
684 // Push a new try handler and link into try handler chain.
Ben Murdoch85b71792012-04-11 18:30:58 +0100685 // The return address must be passed in register ra.
686 // Clobber t0, t1, t2.
687 void PushTryHandler(CodeLocation try_location, HandlerType type);
Andrei Popescu31002712010-02-23 13:46:05 +0000688
689 // Unlink the stack handler on top of the stack from the try handler chain.
690 // Must preserve the result register.
691 void PopTryHandler();
692
Ben Murdoch85b71792012-04-11 18:30:58 +0100693 // Passes thrown value (in v0) to the handler of top of the try handler chain.
Ben Murdoch257744e2011-11-30 15:57:28 +0000694 void Throw(Register value);
695
696 // Propagates an uncatchable exception to the top of the current JS stack's
697 // handler chain.
Ben Murdoch85b71792012-04-11 18:30:58 +0100698 void ThrowUncatchable(UncatchableExceptionType type, Register value);
Ben Murdoch257744e2011-11-30 15:57:28 +0000699
Steve Block44f0eee2011-05-26 01:26:41 +0100700 // Copies a fixed number of fields of heap objects from src to dst.
701 void CopyFields(Register dst, Register src, RegList temps, int field_count);
Andrei Popescu31002712010-02-23 13:46:05 +0000702
Ben Murdoch257744e2011-11-30 15:57:28 +0000703 // Copies a number of bytes from src to dst. All registers are clobbered. On
704 // exit src and dst will point to the place just after where the last byte was
705 // read or written and length will be zero.
706 void CopyBytes(Register src,
707 Register dst,
708 Register length,
709 Register scratch);
710
Steve Block44f0eee2011-05-26 01:26:41 +0100711 // -------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000712 // Support functions.
713
Steve Block44f0eee2011-05-26 01:26:41 +0100714 // Try to get function prototype of a function and puts the value in
715 // the result register. Checks that the function really is a
716 // function and jumps to the miss label if the fast checks fail. The
717 // function register will be untouched; the other registers may be
718 // clobbered.
719 void TryGetFunctionPrototype(Register function,
720 Register result,
721 Register scratch,
Ben Murdoch85b71792012-04-11 18:30:58 +0100722 Label* miss);
Steve Block44f0eee2011-05-26 01:26:41 +0100723
Steve Block6ded16b2010-05-10 14:33:55 +0100724 void GetObjectType(Register function,
725 Register map,
726 Register type_reg);
727
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000728 // Check if a map for a JSObject indicates that the object has fast elements.
729 // Jump to the specified label if it does not.
730 void CheckFastElements(Register map,
731 Register scratch,
732 Label* fail);
733
Ben Murdoch85b71792012-04-11 18:30:58 +0100734 // Check if the map of an object is equal to a specified map (either
735 // given directly or as an index into the root list) and branch to
736 // label if not. Skip the smi check if not required (object is known
737 // to be a heap object).
Steve Block44f0eee2011-05-26 01:26:41 +0100738 void CheckMap(Register obj,
739 Register scratch,
740 Handle<Map> map,
741 Label* fail,
Ben Murdoch85b71792012-04-11 18:30:58 +0100742 SmiCheckType smi_check_type);
Andrei Popescu31002712010-02-23 13:46:05 +0000743
Steve Block44f0eee2011-05-26 01:26:41 +0100744 void CheckMap(Register obj,
745 Register scratch,
746 Heap::RootListIndex index,
747 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +0000748 SmiCheckType smi_check_type);
749
750 // Check if the map of an object is equal to a specified map and branch to a
751 // specified target if equal. Skip the smi check if not required (object is
752 // known to be a heap object)
753 void DispatchMap(Register obj,
754 Register scratch,
755 Handle<Map> map,
756 Handle<Code> success,
757 SmiCheckType smi_check_type);
Steve Block6ded16b2010-05-10 14:33:55 +0100758
759 // Generates code for reporting that an illegal operation has
760 // occurred.
761 void IllegalOperation(int num_arguments);
762
Steve Block44f0eee2011-05-26 01:26:41 +0100763 // Picks out an array index from the hash field.
764 // Register use:
765 // hash - holds the index's hash. Clobbered.
766 // index - holds the overwritten index on exit.
767 void IndexFromHash(Register hash, Register index);
Andrei Popescu31002712010-02-23 13:46:05 +0000768
Ben Murdoch257744e2011-11-30 15:57:28 +0000769 // Get the number of least significant bits from a register.
770 void GetLeastBitsFromSmi(Register dst, Register src, int num_least_bits);
771 void GetLeastBitsFromInt32(Register dst, Register src, int mun_least_bits);
772
Steve Block44f0eee2011-05-26 01:26:41 +0100773 // Load the value of a number object into a FPU double register. If the
774 // object is not a number a jump to the label not_number is performed
775 // and the FPU double register is unchanged.
776 void ObjectToDoubleFPURegister(
777 Register object,
778 FPURegister value,
779 Register scratch1,
780 Register scratch2,
781 Register heap_number_map,
782 Label* not_number,
783 ObjectToDoubleFlags flags = NO_OBJECT_TO_DOUBLE_FLAGS);
784
785 // Load the value of a smi object into a FPU double register. The register
786 // scratch1 can be the same register as smi in which case smi will hold the
787 // untagged value afterwards.
788 void SmiToDoubleFPURegister(Register smi,
789 FPURegister value,
790 Register scratch1);
791
792 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000793 // Overflow handling functions.
794 // Usage: first call the appropriate arithmetic function, then call one of the
795 // jump functions with the overflow_dst register as the second parameter.
796
797 void AdduAndCheckForOverflow(Register dst,
798 Register left,
799 Register right,
800 Register overflow_dst,
801 Register scratch = at);
802
803 void SubuAndCheckForOverflow(Register dst,
804 Register left,
805 Register right,
806 Register overflow_dst,
807 Register scratch = at);
808
809 void BranchOnOverflow(Label* label,
810 Register overflow_check,
811 BranchDelaySlot bd = PROTECT) {
812 Branch(label, lt, overflow_check, Operand(zero_reg), bd);
813 }
814
815 void BranchOnNoOverflow(Label* label,
816 Register overflow_check,
817 BranchDelaySlot bd = PROTECT) {
818 Branch(label, ge, overflow_check, Operand(zero_reg), bd);
819 }
820
821 void RetOnOverflow(Register overflow_check, BranchDelaySlot bd = PROTECT) {
822 Ret(lt, overflow_check, Operand(zero_reg), bd);
823 }
824
825 void RetOnNoOverflow(Register overflow_check, BranchDelaySlot bd = PROTECT) {
826 Ret(ge, overflow_check, Operand(zero_reg), bd);
827 }
828
829 // -------------------------------------------------------------------------
830 // Runtime calls.
Andrei Popescu31002712010-02-23 13:46:05 +0000831
832 // Call a code stub.
Ben Murdoch85b71792012-04-11 18:30:58 +0100833 void CallStub(CodeStub* stub, Condition cond = cc_always,
834 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
835
836 // Call a code stub and return the code object called. Try to generate
837 // the code if necessary. Do not perform a GC but instead return a retry
838 // after GC failure.
839 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub,
840 Condition cond = cc_always,
841 Register r1 = zero_reg,
842 const Operand& r2 =
843 Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +0100844
845 // Tail call a code stub (jump).
846 void TailCallStub(CodeStub* stub);
847
Ben Murdoch85b71792012-04-11 18:30:58 +0100848 // Tail call a code stub (jump) and return the code object called. Try to
849 // generate the code if necessary. Do not perform a GC but instead return
850 // a retry after GC failure.
851 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub,
852 Condition cond = cc_always,
853 Register r1 = zero_reg,
854 const Operand& r2 =
855 Operand(zero_reg));
856
Andrei Popescu31002712010-02-23 13:46:05 +0000857 void CallJSExitStub(CodeStub* stub);
858
Andrei Popescu31002712010-02-23 13:46:05 +0000859 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100860 void CallRuntime(const Runtime::Function* f, int num_arguments);
861 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Andrei Popescu31002712010-02-23 13:46:05 +0000862
863 // Convenience function: Same as above, but takes the fid instead.
864 void CallRuntime(Runtime::FunctionId fid, int num_arguments);
865
Steve Block44f0eee2011-05-26 01:26:41 +0100866 // Convenience function: call an external reference.
867 void CallExternalReference(const ExternalReference& ext,
Ben Murdoch85b71792012-04-11 18:30:58 +0100868 int num_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +0100869
Andrei Popescu31002712010-02-23 13:46:05 +0000870 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100871 // Like JumpToExternalReference, but also takes care of passing the number
Andrei Popescu31002712010-02-23 13:46:05 +0000872 // of parameters.
Steve Block6ded16b2010-05-10 14:33:55 +0100873 void TailCallExternalReference(const ExternalReference& ext,
874 int num_arguments,
875 int result_size);
876
Ben Murdoch85b71792012-04-11 18:30:58 +0100877 // Tail call of a runtime routine (jump). Try to generate the code if
878 // necessary. Do not perform a GC but instead return a retry after GC
879 // failure.
880 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
881 const ExternalReference& ext, int num_arguments, int result_size);
882
Steve Block6ded16b2010-05-10 14:33:55 +0100883 // Convenience function: tail call a runtime routine (jump).
884 void TailCallRuntime(Runtime::FunctionId fid,
Andrei Popescu31002712010-02-23 13:46:05 +0000885 int num_arguments,
886 int result_size);
887
Steve Block44f0eee2011-05-26 01:26:41 +0100888 // Before calling a C-function from generated code, align arguments on stack
889 // and add space for the four mips argument slots.
890 // After aligning the frame, non-register arguments must be stored on the
891 // stack, after the argument-slots using helper: CFunctionArgumentOperand().
892 // The argument count assumes all arguments are word sized.
893 // Some compilers/platforms require the stack to be aligned when calling
894 // C++ code.
895 // Needs a scratch register to do some arithmetic. This register will be
896 // trashed.
Ben Murdoch85b71792012-04-11 18:30:58 +0100897 void PrepareCallCFunction(int num_arguments, Register scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100898
899 // Arguments 1-4 are placed in registers a0 thru a3 respectively.
900 // Arguments 5..n are stored to stack using following:
901 // sw(t0, CFunctionArgumentOperand(5));
902
903 // Calls a C function and cleans up the space for arguments allocated
904 // by PrepareCallCFunction. The called function is not allowed to trigger a
905 // garbage collection, since that might move the code and invalidate the
906 // return address (unless this is somehow accounted for by the called
907 // function).
908 void CallCFunction(ExternalReference function, int num_arguments);
Ben Murdoch85b71792012-04-11 18:30:58 +0100909 void CallCFunction(Register function, Register scratch, int num_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +0000910 void GetCFunctionDoubleResult(const DoubleRegister dst);
911
912 // There are two ways of passing double arguments on MIPS, depending on
913 // whether soft or hard floating point ABI is used. These functions
914 // abstract parameter passing for the three different ways we call
915 // C functions from generated code.
916 void SetCallCDoubleArguments(DoubleRegister dreg);
917 void SetCallCDoubleArguments(DoubleRegister dreg1, DoubleRegister dreg2);
918 void SetCallCDoubleArguments(DoubleRegister dreg, Register reg);
919
Ben Murdoch85b71792012-04-11 18:30:58 +0100920 // Calls an API function. Allocates HandleScope, extracts returned value
921 // from handle and propagates exceptions. Restores context.
922 MaybeObject* TryCallApiFunctionAndReturn(ExternalReference function,
923 int stack_space);
Steve Block44f0eee2011-05-26 01:26:41 +0100924
Andrei Popescu31002712010-02-23 13:46:05 +0000925 // Jump to the builtin routine.
Ben Murdoch85b71792012-04-11 18:30:58 +0100926 void JumpToExternalReference(const ExternalReference& builtin);
927
928 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
Andrei Popescu31002712010-02-23 13:46:05 +0000929
930 // Invoke specified builtin JavaScript function. Adds an entry to
931 // the unresolved list if the name does not resolve.
Steve Block44f0eee2011-05-26 01:26:41 +0100932 void InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +0000933 InvokeFlag flag,
934 const CallWrapper& call_wrapper = NullCallWrapper());
Andrei Popescu31002712010-02-23 13:46:05 +0000935
936 // Store the code object for the given builtin in the target register and
Steve Block44f0eee2011-05-26 01:26:41 +0100937 // setup the function in a1.
Andrei Popescu31002712010-02-23 13:46:05 +0000938 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
939
Steve Block44f0eee2011-05-26 01:26:41 +0100940 // Store the function for the given builtin in the target register.
941 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
942
Andrei Popescu31002712010-02-23 13:46:05 +0000943 struct Unresolved {
944 int pc;
Ben Murdoch257744e2011-11-30 15:57:28 +0000945 uint32_t flags; // See Bootstrapper::FixupFlags decoders/encoders.
Andrei Popescu31002712010-02-23 13:46:05 +0000946 const char* name;
947 };
Andrei Popescu31002712010-02-23 13:46:05 +0000948
Ben Murdoch257744e2011-11-30 15:57:28 +0000949 Handle<Object> CodeObject() {
950 ASSERT(!code_object_.is_null());
951 return code_object_;
952 }
Andrei Popescu31002712010-02-23 13:46:05 +0000953
Steve Block44f0eee2011-05-26 01:26:41 +0100954 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000955 // StatsCounter support.
Andrei Popescu31002712010-02-23 13:46:05 +0000956
957 void SetCounter(StatsCounter* counter, int value,
958 Register scratch1, Register scratch2);
959 void IncrementCounter(StatsCounter* counter, int value,
960 Register scratch1, Register scratch2);
961 void DecrementCounter(StatsCounter* counter, int value,
962 Register scratch1, Register scratch2);
963
964
Steve Block44f0eee2011-05-26 01:26:41 +0100965 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000966 // Debugging.
Andrei Popescu31002712010-02-23 13:46:05 +0000967
968 // Calls Abort(msg) if the condition cc is not satisfied.
969 // Use --debug_code to enable.
970 void Assert(Condition cc, const char* msg, Register rs, Operand rt);
Steve Block44f0eee2011-05-26 01:26:41 +0100971 void AssertRegisterIsRoot(Register reg, Heap::RootListIndex index);
972 void AssertFastElements(Register elements);
Andrei Popescu31002712010-02-23 13:46:05 +0000973
974 // Like Assert(), but always enabled.
975 void Check(Condition cc, const char* msg, Register rs, Operand rt);
976
977 // Print a message to stdout and abort execution.
978 void Abort(const char* msg);
979
980 // Verify restrictions about code generated in stubs.
981 void set_generating_stub(bool value) { generating_stub_ = value; }
982 bool generating_stub() { return generating_stub_; }
983 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
984 bool allow_stub_calls() { return allow_stub_calls_; }
985
Steve Block44f0eee2011-05-26 01:26:41 +0100986 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000987 // Number utilities.
Steve Block6ded16b2010-05-10 14:33:55 +0100988
Steve Block44f0eee2011-05-26 01:26:41 +0100989 // Check whether the value of reg is a power of two and not zero. If not
990 // control continues at the label not_power_of_two. If reg is a power of two
991 // the register scratch contains the value of (reg - 1) when control falls
992 // through.
993 void JumpIfNotPowerOfTwoOrZero(Register reg,
994 Register scratch,
995 Label* not_power_of_two_or_zero);
996
997 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000998 // Smi utilities.
Steve Block44f0eee2011-05-26 01:26:41 +0100999
Ben Murdoch85b71792012-04-11 18:30:58 +01001000 // Try to convert int32 to smi. If the value is to large, preserve
1001 // the original value and jump to not_a_smi. Destroys scratch and
1002 // sets flags.
1003 // This is only used by crankshaft atm so it is unimplemented on MIPS.
1004 void TrySmiTag(Register reg, Label* not_a_smi, Register scratch) {
1005 UNIMPLEMENTED_MIPS();
1006 }
1007
Steve Block44f0eee2011-05-26 01:26:41 +01001008 void SmiTag(Register reg) {
1009 Addu(reg, reg, reg);
1010 }
1011
1012 void SmiTag(Register dst, Register src) {
1013 Addu(dst, src, src);
1014 }
1015
1016 void SmiUntag(Register reg) {
1017 sra(reg, reg, kSmiTagSize);
1018 }
1019
1020 void SmiUntag(Register dst, Register src) {
1021 sra(dst, src, kSmiTagSize);
1022 }
1023
1024 // Jump the register contains a smi.
Ben Murdoch85b71792012-04-11 18:30:58 +01001025 inline void JumpIfSmi(Register value, Label* smi_label,
1026 Register scratch = at) {
1027 ASSERT_EQ(0, kSmiTag);
1028 andi(scratch, value, kSmiTagMask);
1029 Branch(smi_label, eq, scratch, Operand(zero_reg));
1030 }
Steve Block44f0eee2011-05-26 01:26:41 +01001031
1032 // Jump if the register contains a non-smi.
Ben Murdoch85b71792012-04-11 18:30:58 +01001033 inline void JumpIfNotSmi(Register value, Label* not_smi_label,
1034 Register scratch = at) {
1035 ASSERT_EQ(0, kSmiTag);
1036 andi(scratch, value, kSmiTagMask);
1037 Branch(not_smi_label, ne, scratch, Operand(zero_reg));
1038 }
Steve Block44f0eee2011-05-26 01:26:41 +01001039
1040 // Jump if either of the registers contain a non-smi.
1041 void JumpIfNotBothSmi(Register reg1, Register reg2, Label* on_not_both_smi);
1042 // Jump if either of the registers contain a smi.
1043 void JumpIfEitherSmi(Register reg1, Register reg2, Label* on_either_smi);
1044
1045 // Abort execution if argument is a smi. Used in debug code.
1046 void AbortIfSmi(Register object);
1047 void AbortIfNotSmi(Register object);
1048
Ben Murdoch257744e2011-11-30 15:57:28 +00001049 // Abort execution if argument is a string. Used in debug code.
1050 void AbortIfNotString(Register object);
1051
Steve Block44f0eee2011-05-26 01:26:41 +01001052 // Abort execution if argument is not the root value with the given index.
1053 void AbortIfNotRootValue(Register src,
1054 Heap::RootListIndex root_value_index,
1055 const char* message);
1056
1057 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00001058 // HeapNumber utilities.
Steve Block44f0eee2011-05-26 01:26:41 +01001059
1060 void JumpIfNotHeapNumber(Register object,
1061 Register heap_number_map,
1062 Register scratch,
1063 Label* on_not_heap_number);
1064
1065 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00001066 // String utilities.
Steve Block44f0eee2011-05-26 01:26:41 +01001067
1068 // Checks if both instance types are sequential ASCII strings and jumps to
1069 // label if either is not.
1070 void JumpIfBothInstanceTypesAreNotSequentialAscii(
1071 Register first_object_instance_type,
1072 Register second_object_instance_type,
1073 Register scratch1,
1074 Register scratch2,
1075 Label* failure);
1076
1077 // Check if instance type is sequential ASCII string and jump to label if
1078 // it is not.
1079 void JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1080 Register scratch,
1081 Label* failure);
1082
1083 // Test that both first and second are sequential ASCII strings.
1084 // Assume that they are non-smis.
1085 void JumpIfNonSmisNotBothSequentialAsciiStrings(Register first,
1086 Register second,
1087 Register scratch1,
1088 Register scratch2,
1089 Label* failure);
1090
1091 // Test that both first and second are sequential ASCII strings.
1092 // Check that they are non-smis.
1093 void JumpIfNotBothSequentialAsciiStrings(Register first,
1094 Register second,
1095 Register scratch1,
1096 Register scratch2,
1097 Label* failure);
1098
Ben Murdoch257744e2011-11-30 15:57:28 +00001099 void LoadInstanceDescriptors(Register map, Register descriptors);
1100
Steve Block44f0eee2011-05-26 01:26:41 +01001101 private:
1102 void CallCFunctionHelper(Register function,
Ben Murdoch85b71792012-04-11 18:30:58 +01001103 ExternalReference function_reference,
1104 Register scratch,
1105 int num_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01001106
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001107 void BranchShort(int16_t offset, BranchDelaySlot bdslot = PROTECT);
1108 void BranchShort(int16_t offset, Condition cond, Register rs,
1109 const Operand& rt,
1110 BranchDelaySlot bdslot = PROTECT);
1111 void BranchShort(Label* L, BranchDelaySlot bdslot = PROTECT);
1112 void BranchShort(Label* L, Condition cond, Register rs,
1113 const Operand& rt,
1114 BranchDelaySlot bdslot = PROTECT);
1115 void BranchAndLinkShort(int16_t offset, BranchDelaySlot bdslot = PROTECT);
1116 void BranchAndLinkShort(int16_t offset, Condition cond, Register rs,
1117 const Operand& rt,
1118 BranchDelaySlot bdslot = PROTECT);
1119 void BranchAndLinkShort(Label* L, BranchDelaySlot bdslot = PROTECT);
1120 void BranchAndLinkShort(Label* L, Condition cond, Register rs,
1121 const Operand& rt,
1122 BranchDelaySlot bdslot = PROTECT);
1123 void J(Label* L, BranchDelaySlot bdslot);
1124 void Jr(Label* L, BranchDelaySlot bdslot);
1125 void Jalr(Label* L, BranchDelaySlot bdslot);
Steve Block6ded16b2010-05-10 14:33:55 +01001126
1127 // Helper functions for generating invokes.
1128 void InvokePrologue(const ParameterCount& expected,
1129 const ParameterCount& actual,
1130 Handle<Code> code_constant,
1131 Register code_reg,
1132 Label* done,
Steve Block44f0eee2011-05-26 01:26:41 +01001133 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001134 const CallWrapper& call_wrapper,
1135 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01001136
1137 // Get the code for the given builtin. Returns if able to resolve
1138 // the function in the 'resolved' flag.
1139 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
1140
Ben Murdoch85b71792012-04-11 18:30:58 +01001141 // Activation support.
1142 void EnterFrame(StackFrame::Type type);
1143 void LeaveFrame(StackFrame::Type type);
1144
Steve Block44f0eee2011-05-26 01:26:41 +01001145 void InitializeNewString(Register string,
1146 Register length,
1147 Heap::RootListIndex map_index,
1148 Register scratch1,
1149 Register scratch2);
1150
Ben Murdoch257744e2011-11-30 15:57:28 +00001151 // Compute memory operands for safepoint stack slots.
1152 static int SafepointRegisterStackIndex(int reg_code);
1153 MemOperand SafepointRegisterSlot(Register reg);
1154 MemOperand SafepointRegistersAndDoublesSlot(Register reg);
Steve Block44f0eee2011-05-26 01:26:41 +01001155
Ben Murdoch85b71792012-04-11 18:30:58 +01001156 bool UseAbsoluteCodePointers();
1157
Steve Block44f0eee2011-05-26 01:26:41 +01001158 bool generating_stub_;
1159 bool allow_stub_calls_;
1160 // This handle will be patched with the code object on installation.
1161 Handle<Object> code_object_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001162
1163 // Needs access to SafepointRegisterStackIndex for optimized frame
1164 // traversal.
1165 friend class OptimizedFrame;
Steve Block44f0eee2011-05-26 01:26:41 +01001166};
1167
1168
Steve Block44f0eee2011-05-26 01:26:41 +01001169// The code patcher is used to patch (typically) small parts of code e.g. for
1170// debugging and other types of instrumentation. When using the code patcher
1171// the exact number of bytes specified must be emitted. It is not legal to emit
1172// relocation information. If any of these constraints are violated it causes
1173// an assertion to fail.
1174class CodePatcher {
1175 public:
1176 CodePatcher(byte* address, int instructions);
1177 virtual ~CodePatcher();
1178
1179 // Macro assembler to emit code.
1180 MacroAssembler* masm() { return &masm_; }
1181
1182 // Emit an instruction directly.
Ben Murdoch257744e2011-11-30 15:57:28 +00001183 void Emit(Instr instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001184
1185 // Emit an address directly.
1186 void Emit(Address addr);
1187
Ben Murdoch257744e2011-11-30 15:57:28 +00001188 // Change the condition part of an instruction leaving the rest of the current
1189 // instruction unchanged.
1190 void ChangeBranchCondition(Condition cond);
1191
Steve Block44f0eee2011-05-26 01:26:41 +01001192 private:
1193 byte* address_; // The address of the code being patched.
1194 int instructions_; // Number of instructions of the expected patch size.
1195 int size_; // Number of bytes of the expected patch size.
1196 MacroAssembler masm_; // Macro assembler used to generate the code.
1197};
Andrei Popescu31002712010-02-23 13:46:05 +00001198
1199
Ben Murdoch85b71792012-04-11 18:30:58 +01001200// -----------------------------------------------------------------------------
1201// Static helper functions.
1202
1203static MemOperand ContextOperand(Register context, int index) {
1204 return MemOperand(context, Context::SlotOffset(index));
1205}
1206
1207
1208static inline MemOperand GlobalObjectOperand() {
1209 return ContextOperand(cp, Context::GLOBAL_INDEX);
1210}
1211
1212
1213// Generate a MemOperand for loading a field from an object.
1214static inline MemOperand FieldMemOperand(Register object, int offset) {
1215 return MemOperand(object, offset - kHeapObjectTag);
1216}
1217
1218
1219// Generate a MemOperand for storing arguments 5..N on the stack
1220// when calling CallCFunction().
1221static inline MemOperand CFunctionArgumentOperand(int index) {
1222 ASSERT(index > kCArgSlotCount);
1223 // Argument 5 takes the slot just past the four Arg-slots.
1224 int offset = (index - 5) * kPointerSize + kCArgsSlotsSize;
1225 return MemOperand(sp, offset);
1226}
1227
Andrei Popescu31002712010-02-23 13:46:05 +00001228
1229#ifdef GENERATED_CODE_COVERAGE
1230#define CODE_COVERAGE_STRINGIFY(x) #x
1231#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1232#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1233#define ACCESS_MASM(masm) masm->stop(__FILE_LINE__); masm->
1234#else
1235#define ACCESS_MASM(masm) masm->
1236#endif
1237
1238} } // namespace v8::internal
1239
1240#endif // V8_MIPS_MACRO_ASSEMBLER_MIPS_H_