blob: 0fcf6f1d8514485cc306736ebe6d32c1bd4660cb [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// 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
53// Registers aliases
Steve Block6ded16b2010-05-10 14:33:55 +010054// cp is assumed to be a callee saved register.
Steve Block44f0eee2011-05-26 01:26:41 +010055const Register roots = s6; // Roots array pointer.
Ben Murdoch257744e2011-11-30 15:57:28 +000056const Register cp = s7; // JavaScript context pointer.
57const Register fp = s8_fp; // Alias for fp.
58// Registers used for condition evaluation.
Steve Block44f0eee2011-05-26 01:26:41 +010059const Register condReg1 = s4;
60const Register condReg2 = s5;
Andrei Popescu31002712010-02-23 13:46:05 +000061
Steve Block44f0eee2011-05-26 01:26:41 +010062
63// 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);
141 int CallSize(Register target, COND_ARGS);
142 void Call(Register target, COND_ARGS);
143 int CallSize(Address target, RelocInfo::Mode rmode, COND_ARGS);
144 void Call(Address target, RelocInfo::Mode rmode, COND_ARGS);
145 int CallSize(Handle<Code> code,
146 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
147 unsigned ast_id = kNoASTId,
148 COND_ARGS);
149 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);
154 inline void Ret(BranchDelaySlot bd) {
155 Ret(al, zero_reg, Operand(zero_reg), bd);
156 }
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
167 void DropAndRet(int drop = 0,
168 Condition cond = cc_always,
169 Register reg = no_reg,
170 const Operand& op = Operand(no_reg));
171
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
223
224 // 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);
230
231
232 // 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);
237
238 // 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,
246 Register scratch0,
247 Register scratch1);
248
249 // 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);
257
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 Murdoch3fb3ca82011-12-02 17:19:32 +0000269
270 void LoadFromNumberDictionary(Label* miss,
271 Register elements,
272 Register key,
273 Register result,
274 Register reg0,
275 Register reg1,
276 Register reg2);
277
278
Steve Block44f0eee2011-05-26 01:26:41 +0100279 inline void MarkCode(NopMarkerTypes type) {
280 nop(type);
Steve Block6ded16b2010-05-10 14:33:55 +0100281 }
282
Steve Block44f0eee2011-05-26 01:26:41 +0100283 // Check if the given instruction is a 'type' marker.
284 // ie. check if it is a sll zero_reg, zero_reg, <type> (referenced as
285 // nop(type)). These instructions are generated to mark special location in
286 // the code, like some special IC code.
287 static inline bool IsMarkedCode(Instr instr, int type) {
288 ASSERT((FIRST_IC_MARKER <= type) && (type < LAST_CODE_MARKER));
289 return IsNop(instr, type);
290 }
Andrei Popescu31002712010-02-23 13:46:05 +0000291
292
Steve Block44f0eee2011-05-26 01:26:41 +0100293 static inline int GetCodeMarker(Instr instr) {
294 uint32_t opcode = ((instr & kOpcodeMask));
295 uint32_t rt = ((instr & kRtFieldMask) >> kRtShift);
296 uint32_t rs = ((instr & kRsFieldMask) >> kRsShift);
297 uint32_t sa = ((instr & kSaFieldMask) >> kSaShift);
298
299 // Return <n> if we have a sll zero_reg, zero_reg, n
300 // else return -1.
301 bool sllzz = (opcode == SLL &&
302 rt == static_cast<uint32_t>(ToNumber(zero_reg)) &&
303 rs == static_cast<uint32_t>(ToNumber(zero_reg)));
304 int type =
305 (sllzz && FIRST_IC_MARKER <= sa && sa < LAST_CODE_MARKER) ? sa : -1;
306 ASSERT((type == -1) ||
307 ((FIRST_IC_MARKER <= type) && (type < LAST_CODE_MARKER)));
308 return type;
309 }
310
311
312
313 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000314 // Allocation support.
Steve Block44f0eee2011-05-26 01:26:41 +0100315
316 // Allocate an object in new space. The object_size is specified
317 // either in bytes or in words if the allocation flag SIZE_IN_WORDS
318 // is passed. If the new space is exhausted control continues at the
319 // gc_required label. The allocated object is returned in result. If
320 // the flag tag_allocated_object is true the result is tagged as as
321 // a heap object. All registers are clobbered also when control
322 // continues at the gc_required label.
323 void AllocateInNewSpace(int object_size,
324 Register result,
325 Register scratch1,
326 Register scratch2,
327 Label* gc_required,
328 AllocationFlags flags);
329 void AllocateInNewSpace(Register object_size,
330 Register result,
331 Register scratch1,
332 Register scratch2,
333 Label* gc_required,
334 AllocationFlags flags);
335
336 // Undo allocation in new space. The object passed and objects allocated after
337 // it will no longer be allocated. The caller must make sure that no pointers
338 // are left to the object(s) no longer allocated as they would be invalid when
339 // allocation is undone.
340 void UndoAllocationInNewSpace(Register object, Register scratch);
341
342
343 void AllocateTwoByteString(Register result,
344 Register length,
345 Register scratch1,
346 Register scratch2,
347 Register scratch3,
348 Label* gc_required);
349 void AllocateAsciiString(Register result,
350 Register length,
351 Register scratch1,
352 Register scratch2,
353 Register scratch3,
354 Label* gc_required);
355 void AllocateTwoByteConsString(Register result,
356 Register length,
357 Register scratch1,
358 Register scratch2,
359 Label* gc_required);
360 void AllocateAsciiConsString(Register result,
361 Register length,
362 Register scratch1,
363 Register scratch2,
364 Label* gc_required);
365
366 // Allocates a heap number or jumps to the gc_required label if the young
367 // space is full and a scavenge is needed. All registers are clobbered also
368 // when control continues at the gc_required label.
369 void AllocateHeapNumber(Register result,
370 Register scratch1,
371 Register scratch2,
372 Register heap_number_map,
373 Label* gc_required);
374 void AllocateHeapNumberWithValue(Register result,
375 FPURegister value,
376 Register scratch1,
377 Register scratch2,
378 Label* gc_required);
379
Andrei Popescu31002712010-02-23 13:46:05 +0000380 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000381 // Instruction macros.
Andrei Popescu31002712010-02-23 13:46:05 +0000382
Steve Block44f0eee2011-05-26 01:26:41 +0100383#define DEFINE_INSTRUCTION(instr) \
Andrei Popescu31002712010-02-23 13:46:05 +0000384 void instr(Register rd, Register rs, const Operand& rt); \
385 void instr(Register rd, Register rs, Register rt) { \
386 instr(rd, rs, Operand(rt)); \
387 } \
388 void instr(Register rs, Register rt, int32_t j) { \
389 instr(rs, rt, Operand(j)); \
390 }
391
Steve Block44f0eee2011-05-26 01:26:41 +0100392#define DEFINE_INSTRUCTION2(instr) \
Andrei Popescu31002712010-02-23 13:46:05 +0000393 void instr(Register rs, const Operand& rt); \
394 void instr(Register rs, Register rt) { \
395 instr(rs, Operand(rt)); \
396 } \
397 void instr(Register rs, int32_t j) { \
398 instr(rs, Operand(j)); \
399 }
400
Andrei Popescu31002712010-02-23 13:46:05 +0000401 DEFINE_INSTRUCTION(Addu);
Steve Block44f0eee2011-05-26 01:26:41 +0100402 DEFINE_INSTRUCTION(Subu);
Andrei Popescu31002712010-02-23 13:46:05 +0000403 DEFINE_INSTRUCTION(Mul);
404 DEFINE_INSTRUCTION2(Mult);
405 DEFINE_INSTRUCTION2(Multu);
406 DEFINE_INSTRUCTION2(Div);
407 DEFINE_INSTRUCTION2(Divu);
408
409 DEFINE_INSTRUCTION(And);
410 DEFINE_INSTRUCTION(Or);
411 DEFINE_INSTRUCTION(Xor);
412 DEFINE_INSTRUCTION(Nor);
Ben Murdoch257744e2011-11-30 15:57:28 +0000413 DEFINE_INSTRUCTION2(Neg);
Andrei Popescu31002712010-02-23 13:46:05 +0000414
415 DEFINE_INSTRUCTION(Slt);
416 DEFINE_INSTRUCTION(Sltu);
417
Steve Block44f0eee2011-05-26 01:26:41 +0100418 // MIPS32 R2 instruction macro.
419 DEFINE_INSTRUCTION(Ror);
420
Andrei Popescu31002712010-02-23 13:46:05 +0000421#undef DEFINE_INSTRUCTION
422#undef DEFINE_INSTRUCTION2
423
424
Ben Murdoch257744e2011-11-30 15:57:28 +0000425 // ---------------------------------------------------------------------------
426 // Pseudo-instructions.
Andrei Popescu31002712010-02-23 13:46:05 +0000427
428 void mov(Register rd, Register rt) { or_(rd, rt, zero_reg); }
Andrei Popescu31002712010-02-23 13:46:05 +0000429
Ben Murdoch257744e2011-11-30 15:57:28 +0000430 // Load int32 in the rd register.
Andrei Popescu31002712010-02-23 13:46:05 +0000431 void li(Register rd, Operand j, bool gen2instr = false);
432 inline void li(Register rd, int32_t j, bool gen2instr = false) {
433 li(rd, Operand(j), gen2instr);
434 }
Steve Block44f0eee2011-05-26 01:26:41 +0100435 inline void li(Register dst, Handle<Object> value, bool gen2instr = false) {
436 li(dst, Operand(value), gen2instr);
437 }
Andrei Popescu31002712010-02-23 13:46:05 +0000438
Andrei Popescu31002712010-02-23 13:46:05 +0000439 // Push multiple registers on the stack.
Steve Block6ded16b2010-05-10 14:33:55 +0100440 // Registers are saved in numerical order, with higher numbered registers
Ben Murdoch257744e2011-11-30 15:57:28 +0000441 // saved in higher memory addresses.
Andrei Popescu31002712010-02-23 13:46:05 +0000442 void MultiPush(RegList regs);
443 void MultiPushReversed(RegList regs);
Steve Block44f0eee2011-05-26 01:26:41 +0100444
Ben Murdoch257744e2011-11-30 15:57:28 +0000445 // Lower case push() for compatibility with arch-independent code.
446 void push(Register src) {
Andrei Popescu31002712010-02-23 13:46:05 +0000447 Addu(sp, sp, Operand(-kPointerSize));
448 sw(src, MemOperand(sp, 0));
449 }
Steve Block44f0eee2011-05-26 01:26:41 +0100450
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000451 // Push a handle.
452 void Push(Handle<Object> handle);
453
Ben Murdoch257744e2011-11-30 15:57:28 +0000454 // Push two registers. Pushes leftmost register first (to highest address).
455 void Push(Register src1, Register src2) {
Steve Block44f0eee2011-05-26 01:26:41 +0100456 Subu(sp, sp, Operand(2 * kPointerSize));
457 sw(src1, MemOperand(sp, 1 * kPointerSize));
458 sw(src2, MemOperand(sp, 0 * kPointerSize));
459 }
460
Ben Murdoch257744e2011-11-30 15:57:28 +0000461 // Push three registers. Pushes leftmost register first (to highest address).
462 void Push(Register src1, Register src2, Register src3) {
463 Subu(sp, sp, Operand(3 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100464 sw(src1, MemOperand(sp, 2 * kPointerSize));
465 sw(src2, MemOperand(sp, 1 * kPointerSize));
466 sw(src3, MemOperand(sp, 0 * kPointerSize));
467 }
468
Ben Murdoch257744e2011-11-30 15:57:28 +0000469 // Push four registers. Pushes leftmost register first (to highest address).
470 void Push(Register src1, Register src2, Register src3, Register src4) {
471 Subu(sp, sp, Operand(4 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100472 sw(src1, MemOperand(sp, 3 * kPointerSize));
473 sw(src2, MemOperand(sp, 2 * kPointerSize));
474 sw(src3, MemOperand(sp, 1 * kPointerSize));
475 sw(src4, MemOperand(sp, 0 * kPointerSize));
476 }
477
Andrei Popescu31002712010-02-23 13:46:05 +0000478 void Push(Register src, Condition cond, Register tst1, Register tst2) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000479 // Since we don't have conditional execution we use a Branch.
Steve Block44f0eee2011-05-26 01:26:41 +0100480 Branch(3, cond, tst1, Operand(tst2));
Ben Murdoch257744e2011-11-30 15:57:28 +0000481 Subu(sp, sp, Operand(kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +0000482 sw(src, MemOperand(sp, 0));
483 }
484
485 // Pops multiple values from the stack and load them in the
486 // registers specified in regs. Pop order is the opposite as in MultiPush.
487 void MultiPop(RegList regs);
488 void MultiPopReversed(RegList regs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000489
490 // Lower case pop() for compatibility with arch-independent code.
491 void pop(Register dst) {
Andrei Popescu31002712010-02-23 13:46:05 +0000492 lw(dst, MemOperand(sp, 0));
493 Addu(sp, sp, Operand(kPointerSize));
494 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000495
496 // Pop two registers. Pops rightmost register first (from lower address).
497 void Pop(Register src1, Register src2) {
498 ASSERT(!src1.is(src2));
499 lw(src2, MemOperand(sp, 0 * kPointerSize));
500 lw(src1, MemOperand(sp, 1 * kPointerSize));
501 Addu(sp, sp, 2 * kPointerSize);
502 }
503
Steve Block44f0eee2011-05-26 01:26:41 +0100504 void Pop(uint32_t count = 1) {
505 Addu(sp, sp, Operand(count * kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +0000506 }
507
Steve Block44f0eee2011-05-26 01:26:41 +0100508 // Push and pop the registers that can hold pointers, as defined by the
509 // RegList constant kSafepointSavedRegisters.
Ben Murdoch257744e2011-11-30 15:57:28 +0000510 void PushSafepointRegisters();
511 void PopSafepointRegisters();
512 void PushSafepointRegistersAndDoubles();
513 void PopSafepointRegistersAndDoubles();
514 // Store value in register src in the safepoint stack slot for
515 // register dst.
516 void StoreToSafepointRegisterSlot(Register src, Register dst);
517 void StoreToSafepointRegistersAndDoublesSlot(Register src, Register dst);
518 // Load the value of the src register from its safepoint stack slot
519 // into register dst.
520 void LoadFromSafepointRegisterSlot(Register dst, Register src);
Steve Block44f0eee2011-05-26 01:26:41 +0100521
522 // MIPS32 R2 instruction macro.
523 void Ins(Register rt, Register rs, uint16_t pos, uint16_t size);
524 void Ext(Register rt, Register rs, uint16_t pos, uint16_t size);
525
526 // Convert unsigned word to double.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000527 void Cvt_d_uw(FPURegister fd, FPURegister fs, FPURegister scratch);
528 void Cvt_d_uw(FPURegister fd, Register rs, FPURegister scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100529
530 // Convert double to unsigned word.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000531 void Trunc_uw_d(FPURegister fd, FPURegister fs, FPURegister scratch);
532 void Trunc_uw_d(FPURegister fd, Register rs, FPURegister scratch);
Steve Block44f0eee2011-05-26 01:26:41 +0100533
534 // Convert the HeapNumber pointed to by source to a 32bits signed integer
535 // dest. If the HeapNumber does not fit into a 32bits signed integer branch
536 // to not_int32 label. If FPU is available double_scratch is used but not
537 // scratch2.
538 void ConvertToInt32(Register source,
539 Register dest,
540 Register scratch,
541 Register scratch2,
542 FPURegister double_scratch,
543 Label *not_int32);
544
Ben Murdoch257744e2011-11-30 15:57:28 +0000545 // Helper for EmitECMATruncate.
546 // This will truncate a floating-point value outside of the singed 32bit
547 // integer range to a 32bit signed integer.
548 // Expects the double value loaded in input_high and input_low.
549 // Exits with the answer in 'result'.
550 // Note that this code does not work for values in the 32bit range!
551 void EmitOutOfInt32RangeTruncate(Register result,
552 Register input_high,
553 Register input_low,
554 Register scratch);
555
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000556 // Performs a truncating conversion of a floating point number as used by
557 // the JS bitwise operations. See ECMA-262 9.5: ToInt32.
558 // Exits with 'result' holding the answer and all other registers clobbered.
559 void EmitECMATruncate(Register result,
560 FPURegister double_input,
561 FPURegister single_scratch,
562 Register scratch,
563 Register scratch2,
564 Register scratch3);
565
Steve Block44f0eee2011-05-26 01:26:41 +0100566 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000567 // Activation frames.
Steve Block6ded16b2010-05-10 14:33:55 +0100568
569 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
570 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
571
Steve Block44f0eee2011-05-26 01:26:41 +0100572 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
573 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
574
575 // Enter exit frame.
Ben Murdoch257744e2011-11-30 15:57:28 +0000576 // argc - argument count to be dropped by LeaveExitFrame.
577 // save_doubles - saves FPU registers on stack, currently disabled.
578 // stack_space - extra stack space.
579 void EnterExitFrame(bool save_doubles,
580 int stack_space = 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100581
Ben Murdoch257744e2011-11-30 15:57:28 +0000582 // Leave the current exit frame.
583 void LeaveExitFrame(bool save_doubles, Register arg_count);
Steve Block6ded16b2010-05-10 14:33:55 +0100584
Steve Block44f0eee2011-05-26 01:26:41 +0100585 // Get the actual activation frame alignment for target environment.
586 static int ActivationFrameAlignment();
Steve Block6ded16b2010-05-10 14:33:55 +0100587
Ben Murdoch257744e2011-11-30 15:57:28 +0000588 // Make sure the stack is aligned. Only emits code in debug mode.
589 void AssertStackIsAligned();
590
Steve Block44f0eee2011-05-26 01:26:41 +0100591 void LoadContext(Register dst, int context_chain_length);
Steve Block6ded16b2010-05-10 14:33:55 +0100592
Steve Block44f0eee2011-05-26 01:26:41 +0100593 void LoadGlobalFunction(int index, Register function);
594
595 // Load the initial map from the global function. The registers
596 // function and map can be the same, function is then overwritten.
597 void LoadGlobalFunctionInitialMap(Register function,
598 Register map,
599 Register scratch);
600
601 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000602 // JavaScript invokes.
603
604 // Setup call kind marking in t1. The method takes t1 as an
605 // explicit first parameter to make the code more readable at the
606 // call sites.
607 void SetCallKind(Register dst, CallKind kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100608
609 // Invoke the JavaScript function code by either calling or jumping.
610 void InvokeCode(Register code,
611 const ParameterCount& expected,
612 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +0100613 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000614 const CallWrapper& call_wrapper,
615 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100616
617 void InvokeCode(Handle<Code> code,
618 const ParameterCount& expected,
619 const ParameterCount& actual,
620 RelocInfo::Mode rmode,
Ben Murdoch257744e2011-11-30 15:57:28 +0000621 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000622 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100623
624 // Invoke the JavaScript function in the given register. Changes the
625 // current context to the context in the function before invoking.
626 void InvokeFunction(Register function,
627 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +0100628 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000629 const CallWrapper& call_wrapper,
630 CallKind call_kind);
Steve Block44f0eee2011-05-26 01:26:41 +0100631
632 void InvokeFunction(JSFunction* function,
633 const ParameterCount& actual,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000634 InvokeFlag flag,
635 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +0100636
637
Steve Block44f0eee2011-05-26 01:26:41 +0100638 void IsObjectJSObjectType(Register heap_object,
639 Register map,
640 Register scratch,
641 Label* fail);
642
643 void IsInstanceJSObjectType(Register map,
644 Register scratch,
645 Label* fail);
646
647 void IsObjectJSStringType(Register object,
648 Register scratch,
649 Label* fail);
650
Steve Block6ded16b2010-05-10 14:33:55 +0100651#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100652 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000653 // Debugger Support.
Steve Block6ded16b2010-05-10 14:33:55 +0100654
Steve Block6ded16b2010-05-10 14:33:55 +0100655 void DebugBreak();
656#endif
657
658
Steve Block44f0eee2011-05-26 01:26:41 +0100659 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000660 // Exception handling.
Andrei Popescu31002712010-02-23 13:46:05 +0000661
662 // Push a new try handler and link into try handler chain.
Steve Block6ded16b2010-05-10 14:33:55 +0100663 // The return address must be passed in register ra.
Steve Block44f0eee2011-05-26 01:26:41 +0100664 // Clobber t0, t1, t2.
Andrei Popescu31002712010-02-23 13:46:05 +0000665 void PushTryHandler(CodeLocation try_location, HandlerType type);
666
667 // Unlink the stack handler on top of the stack from the try handler chain.
668 // Must preserve the result register.
669 void PopTryHandler();
670
Ben Murdoch257744e2011-11-30 15:57:28 +0000671 // Passes thrown value (in v0) to the handler of top of the try handler chain.
672 void Throw(Register value);
673
674 // Propagates an uncatchable exception to the top of the current JS stack's
675 // handler chain.
676 void ThrowUncatchable(UncatchableExceptionType type, Register value);
677
Steve Block44f0eee2011-05-26 01:26:41 +0100678 // Copies a fixed number of fields of heap objects from src to dst.
679 void CopyFields(Register dst, Register src, RegList temps, int field_count);
Andrei Popescu31002712010-02-23 13:46:05 +0000680
Ben Murdoch257744e2011-11-30 15:57:28 +0000681 // Copies a number of bytes from src to dst. All registers are clobbered. On
682 // exit src and dst will point to the place just after where the last byte was
683 // read or written and length will be zero.
684 void CopyBytes(Register src,
685 Register dst,
686 Register length,
687 Register scratch);
688
Steve Block44f0eee2011-05-26 01:26:41 +0100689 // -------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000690 // Support functions.
691
Steve Block44f0eee2011-05-26 01:26:41 +0100692 // Try to get function prototype of a function and puts the value in
693 // the result register. Checks that the function really is a
694 // function and jumps to the miss label if the fast checks fail. The
695 // function register will be untouched; the other registers may be
696 // clobbered.
697 void TryGetFunctionPrototype(Register function,
698 Register result,
699 Register scratch,
700 Label* miss);
701
Steve Block6ded16b2010-05-10 14:33:55 +0100702 void GetObjectType(Register function,
703 Register map,
704 Register type_reg);
705
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000706 // Check if a map for a JSObject indicates that the object has fast elements.
707 // Jump to the specified label if it does not.
708 void CheckFastElements(Register map,
709 Register scratch,
710 Label* fail);
711
Steve Block44f0eee2011-05-26 01:26:41 +0100712 // Check if the map of an object is equal to a specified map (either
713 // given directly or as an index into the root list) and branch to
714 // label if not. Skip the smi check if not required (object is known
Ben Murdoch257744e2011-11-30 15:57:28 +0000715 // to be a heap object).
Steve Block44f0eee2011-05-26 01:26:41 +0100716 void CheckMap(Register obj,
717 Register scratch,
718 Handle<Map> map,
719 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +0000720 SmiCheckType smi_check_type);
Andrei Popescu31002712010-02-23 13:46:05 +0000721
Steve Block44f0eee2011-05-26 01:26:41 +0100722 void CheckMap(Register obj,
723 Register scratch,
724 Heap::RootListIndex index,
725 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +0000726 SmiCheckType smi_check_type);
727
728 // Check if the map of an object is equal to a specified map and branch to a
729 // specified target if equal. Skip the smi check if not required (object is
730 // known to be a heap object)
731 void DispatchMap(Register obj,
732 Register scratch,
733 Handle<Map> map,
734 Handle<Code> success,
735 SmiCheckType smi_check_type);
Steve Block6ded16b2010-05-10 14:33:55 +0100736
737 // Generates code for reporting that an illegal operation has
738 // occurred.
739 void IllegalOperation(int num_arguments);
740
Steve Block44f0eee2011-05-26 01:26:41 +0100741 // Picks out an array index from the hash field.
742 // Register use:
743 // hash - holds the index's hash. Clobbered.
744 // index - holds the overwritten index on exit.
745 void IndexFromHash(Register hash, Register index);
Andrei Popescu31002712010-02-23 13:46:05 +0000746
Ben Murdoch257744e2011-11-30 15:57:28 +0000747 // Get the number of least significant bits from a register.
748 void GetLeastBitsFromSmi(Register dst, Register src, int num_least_bits);
749 void GetLeastBitsFromInt32(Register dst, Register src, int mun_least_bits);
750
Steve Block44f0eee2011-05-26 01:26:41 +0100751 // Load the value of a number object into a FPU double register. If the
752 // object is not a number a jump to the label not_number is performed
753 // and the FPU double register is unchanged.
754 void ObjectToDoubleFPURegister(
755 Register object,
756 FPURegister value,
757 Register scratch1,
758 Register scratch2,
759 Register heap_number_map,
760 Label* not_number,
761 ObjectToDoubleFlags flags = NO_OBJECT_TO_DOUBLE_FLAGS);
762
763 // Load the value of a smi object into a FPU double register. The register
764 // scratch1 can be the same register as smi in which case smi will hold the
765 // untagged value afterwards.
766 void SmiToDoubleFPURegister(Register smi,
767 FPURegister value,
768 Register scratch1);
769
770 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000771 // Overflow handling functions.
772 // Usage: first call the appropriate arithmetic function, then call one of the
773 // jump functions with the overflow_dst register as the second parameter.
774
775 void AdduAndCheckForOverflow(Register dst,
776 Register left,
777 Register right,
778 Register overflow_dst,
779 Register scratch = at);
780
781 void SubuAndCheckForOverflow(Register dst,
782 Register left,
783 Register right,
784 Register overflow_dst,
785 Register scratch = at);
786
787 void BranchOnOverflow(Label* label,
788 Register overflow_check,
789 BranchDelaySlot bd = PROTECT) {
790 Branch(label, lt, overflow_check, Operand(zero_reg), bd);
791 }
792
793 void BranchOnNoOverflow(Label* label,
794 Register overflow_check,
795 BranchDelaySlot bd = PROTECT) {
796 Branch(label, ge, overflow_check, Operand(zero_reg), bd);
797 }
798
799 void RetOnOverflow(Register overflow_check, BranchDelaySlot bd = PROTECT) {
800 Ret(lt, overflow_check, Operand(zero_reg), bd);
801 }
802
803 void RetOnNoOverflow(Register overflow_check, BranchDelaySlot bd = PROTECT) {
804 Ret(ge, overflow_check, Operand(zero_reg), bd);
805 }
806
807 // -------------------------------------------------------------------------
808 // Runtime calls.
Andrei Popescu31002712010-02-23 13:46:05 +0000809
810 // Call a code stub.
811 void CallStub(CodeStub* stub, Condition cond = cc_always,
812 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +0100813
Ben Murdoch257744e2011-11-30 15:57:28 +0000814 // Call a code stub and return the code object called. Try to generate
815 // the code if necessary. Do not perform a GC but instead return a retry
816 // after GC failure.
817 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub,
818 Condition cond = cc_always,
819 Register r1 = zero_reg,
820 const Operand& r2 =
821 Operand(zero_reg));
822
Steve Block44f0eee2011-05-26 01:26:41 +0100823 // Tail call a code stub (jump).
824 void TailCallStub(CodeStub* stub);
825
Ben Murdoch257744e2011-11-30 15:57:28 +0000826 // Tail call a code stub (jump) and return the code object called. Try to
827 // generate the code if necessary. Do not perform a GC but instead return
828 // a retry after GC failure.
829 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub,
830 Condition cond = cc_always,
831 Register r1 = zero_reg,
832 const Operand& r2 =
833 Operand(zero_reg));
834
Andrei Popescu31002712010-02-23 13:46:05 +0000835 void CallJSExitStub(CodeStub* stub);
836
Andrei Popescu31002712010-02-23 13:46:05 +0000837 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100838 void CallRuntime(const Runtime::Function* f, int num_arguments);
839 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Andrei Popescu31002712010-02-23 13:46:05 +0000840
841 // Convenience function: Same as above, but takes the fid instead.
842 void CallRuntime(Runtime::FunctionId fid, int num_arguments);
843
Steve Block44f0eee2011-05-26 01:26:41 +0100844 // Convenience function: call an external reference.
845 void CallExternalReference(const ExternalReference& ext,
846 int num_arguments);
847
Andrei Popescu31002712010-02-23 13:46:05 +0000848 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100849 // Like JumpToExternalReference, but also takes care of passing the number
Andrei Popescu31002712010-02-23 13:46:05 +0000850 // of parameters.
Steve Block6ded16b2010-05-10 14:33:55 +0100851 void TailCallExternalReference(const ExternalReference& ext,
852 int num_arguments,
853 int result_size);
854
Ben Murdoch257744e2011-11-30 15:57:28 +0000855 // Tail call of a runtime routine (jump). Try to generate the code if
856 // necessary. Do not perform a GC but instead return a retry after GC
857 // failure.
858 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
859 const ExternalReference& ext, int num_arguments, int result_size);
860
Steve Block6ded16b2010-05-10 14:33:55 +0100861 // Convenience function: tail call a runtime routine (jump).
862 void TailCallRuntime(Runtime::FunctionId fid,
Andrei Popescu31002712010-02-23 13:46:05 +0000863 int num_arguments,
864 int result_size);
865
Steve Block44f0eee2011-05-26 01:26:41 +0100866 // Before calling a C-function from generated code, align arguments on stack
867 // and add space for the four mips argument slots.
868 // After aligning the frame, non-register arguments must be stored on the
869 // stack, after the argument-slots using helper: CFunctionArgumentOperand().
870 // The argument count assumes all arguments are word sized.
871 // Some compilers/platforms require the stack to be aligned when calling
872 // C++ code.
873 // Needs a scratch register to do some arithmetic. This register will be
874 // trashed.
875 void PrepareCallCFunction(int num_arguments, Register scratch);
876
877 // Arguments 1-4 are placed in registers a0 thru a3 respectively.
878 // Arguments 5..n are stored to stack using following:
879 // sw(t0, CFunctionArgumentOperand(5));
880
881 // Calls a C function and cleans up the space for arguments allocated
882 // by PrepareCallCFunction. The called function is not allowed to trigger a
883 // garbage collection, since that might move the code and invalidate the
884 // return address (unless this is somehow accounted for by the called
885 // function).
886 void CallCFunction(ExternalReference function, int num_arguments);
887 void CallCFunction(Register function, Register scratch, int num_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +0000888 void GetCFunctionDoubleResult(const DoubleRegister dst);
889
890 // There are two ways of passing double arguments on MIPS, depending on
891 // whether soft or hard floating point ABI is used. These functions
892 // abstract parameter passing for the three different ways we call
893 // C functions from generated code.
894 void SetCallCDoubleArguments(DoubleRegister dreg);
895 void SetCallCDoubleArguments(DoubleRegister dreg1, DoubleRegister dreg2);
896 void SetCallCDoubleArguments(DoubleRegister dreg, Register reg);
897
898 // Calls an API function. Allocates HandleScope, extracts returned value
899 // from handle and propagates exceptions. Restores context.
900 MaybeObject* TryCallApiFunctionAndReturn(ExternalReference function,
901 int stack_space);
Steve Block44f0eee2011-05-26 01:26:41 +0100902
Andrei Popescu31002712010-02-23 13:46:05 +0000903 // Jump to the builtin routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100904 void JumpToExternalReference(const ExternalReference& builtin);
Andrei Popescu31002712010-02-23 13:46:05 +0000905
Ben Murdoch257744e2011-11-30 15:57:28 +0000906 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
907
Andrei Popescu31002712010-02-23 13:46:05 +0000908 // Invoke specified builtin JavaScript function. Adds an entry to
909 // the unresolved list if the name does not resolve.
Steve Block44f0eee2011-05-26 01:26:41 +0100910 void InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +0000911 InvokeFlag flag,
912 const CallWrapper& call_wrapper = NullCallWrapper());
Andrei Popescu31002712010-02-23 13:46:05 +0000913
914 // Store the code object for the given builtin in the target register and
Steve Block44f0eee2011-05-26 01:26:41 +0100915 // setup the function in a1.
Andrei Popescu31002712010-02-23 13:46:05 +0000916 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
917
Steve Block44f0eee2011-05-26 01:26:41 +0100918 // Store the function for the given builtin in the target register.
919 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
920
Andrei Popescu31002712010-02-23 13:46:05 +0000921 struct Unresolved {
922 int pc;
Ben Murdoch257744e2011-11-30 15:57:28 +0000923 uint32_t flags; // See Bootstrapper::FixupFlags decoders/encoders.
Andrei Popescu31002712010-02-23 13:46:05 +0000924 const char* name;
925 };
Andrei Popescu31002712010-02-23 13:46:05 +0000926
Ben Murdoch257744e2011-11-30 15:57:28 +0000927 Handle<Object> CodeObject() {
928 ASSERT(!code_object_.is_null());
929 return code_object_;
930 }
Andrei Popescu31002712010-02-23 13:46:05 +0000931
Steve Block44f0eee2011-05-26 01:26:41 +0100932 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000933 // StatsCounter support.
Andrei Popescu31002712010-02-23 13:46:05 +0000934
935 void SetCounter(StatsCounter* counter, int value,
936 Register scratch1, Register scratch2);
937 void IncrementCounter(StatsCounter* counter, int value,
938 Register scratch1, Register scratch2);
939 void DecrementCounter(StatsCounter* counter, int value,
940 Register scratch1, Register scratch2);
941
942
Steve Block44f0eee2011-05-26 01:26:41 +0100943 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000944 // Debugging.
Andrei Popescu31002712010-02-23 13:46:05 +0000945
946 // Calls Abort(msg) if the condition cc is not satisfied.
947 // Use --debug_code to enable.
948 void Assert(Condition cc, const char* msg, Register rs, Operand rt);
Steve Block44f0eee2011-05-26 01:26:41 +0100949 void AssertRegisterIsRoot(Register reg, Heap::RootListIndex index);
950 void AssertFastElements(Register elements);
Andrei Popescu31002712010-02-23 13:46:05 +0000951
952 // Like Assert(), but always enabled.
953 void Check(Condition cc, const char* msg, Register rs, Operand rt);
954
955 // Print a message to stdout and abort execution.
956 void Abort(const char* msg);
957
958 // Verify restrictions about code generated in stubs.
959 void set_generating_stub(bool value) { generating_stub_ = value; }
960 bool generating_stub() { return generating_stub_; }
961 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
962 bool allow_stub_calls() { return allow_stub_calls_; }
963
Steve Block44f0eee2011-05-26 01:26:41 +0100964 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000965 // Number utilities.
Steve Block6ded16b2010-05-10 14:33:55 +0100966
Steve Block44f0eee2011-05-26 01:26:41 +0100967 // Check whether the value of reg is a power of two and not zero. If not
968 // control continues at the label not_power_of_two. If reg is a power of two
969 // the register scratch contains the value of (reg - 1) when control falls
970 // through.
971 void JumpIfNotPowerOfTwoOrZero(Register reg,
972 Register scratch,
973 Label* not_power_of_two_or_zero);
974
975 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000976 // Smi utilities.
Steve Block44f0eee2011-05-26 01:26:41 +0100977
978 // Try to convert int32 to smi. If the value is to large, preserve
979 // the original value and jump to not_a_smi. Destroys scratch and
980 // sets flags.
981 // This is only used by crankshaft atm so it is unimplemented on MIPS.
982 void TrySmiTag(Register reg, Label* not_a_smi, Register scratch) {
983 UNIMPLEMENTED_MIPS();
984 }
985
986 void SmiTag(Register reg) {
987 Addu(reg, reg, reg);
988 }
989
990 void SmiTag(Register dst, Register src) {
991 Addu(dst, src, src);
992 }
993
994 void SmiUntag(Register reg) {
995 sra(reg, reg, kSmiTagSize);
996 }
997
998 void SmiUntag(Register dst, Register src) {
999 sra(dst, src, kSmiTagSize);
1000 }
1001
1002 // Jump the register contains a smi.
1003 inline void JumpIfSmi(Register value, Label* smi_label,
1004 Register scratch = at) {
1005 ASSERT_EQ(0, kSmiTag);
1006 andi(scratch, value, kSmiTagMask);
1007 Branch(smi_label, eq, scratch, Operand(zero_reg));
1008 }
1009
1010 // Jump if the register contains a non-smi.
1011 inline void JumpIfNotSmi(Register value, Label* not_smi_label,
1012 Register scratch = at) {
1013 ASSERT_EQ(0, kSmiTag);
1014 andi(scratch, value, kSmiTagMask);
1015 Branch(not_smi_label, ne, scratch, Operand(zero_reg));
1016 }
1017
1018 // Jump if either of the registers contain a non-smi.
1019 void JumpIfNotBothSmi(Register reg1, Register reg2, Label* on_not_both_smi);
1020 // Jump if either of the registers contain a smi.
1021 void JumpIfEitherSmi(Register reg1, Register reg2, Label* on_either_smi);
1022
1023 // Abort execution if argument is a smi. Used in debug code.
1024 void AbortIfSmi(Register object);
1025 void AbortIfNotSmi(Register object);
1026
Ben Murdoch257744e2011-11-30 15:57:28 +00001027 // Abort execution if argument is a string. Used in debug code.
1028 void AbortIfNotString(Register object);
1029
Steve Block44f0eee2011-05-26 01:26:41 +01001030 // Abort execution if argument is not the root value with the given index.
1031 void AbortIfNotRootValue(Register src,
1032 Heap::RootListIndex root_value_index,
1033 const char* message);
1034
1035 // ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00001036 // HeapNumber utilities.
Steve Block44f0eee2011-05-26 01:26:41 +01001037
1038 void JumpIfNotHeapNumber(Register object,
1039 Register heap_number_map,
1040 Register scratch,
1041 Label* on_not_heap_number);
1042
1043 // -------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00001044 // String utilities.
Steve Block44f0eee2011-05-26 01:26:41 +01001045
1046 // Checks if both instance types are sequential ASCII strings and jumps to
1047 // label if either is not.
1048 void JumpIfBothInstanceTypesAreNotSequentialAscii(
1049 Register first_object_instance_type,
1050 Register second_object_instance_type,
1051 Register scratch1,
1052 Register scratch2,
1053 Label* failure);
1054
1055 // Check if instance type is sequential ASCII string and jump to label if
1056 // it is not.
1057 void JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1058 Register scratch,
1059 Label* failure);
1060
1061 // Test that both first and second are sequential ASCII strings.
1062 // Assume that they are non-smis.
1063 void JumpIfNonSmisNotBothSequentialAsciiStrings(Register first,
1064 Register second,
1065 Register scratch1,
1066 Register scratch2,
1067 Label* failure);
1068
1069 // Test that both first and second are sequential ASCII strings.
1070 // Check that they are non-smis.
1071 void JumpIfNotBothSequentialAsciiStrings(Register first,
1072 Register second,
1073 Register scratch1,
1074 Register scratch2,
1075 Label* failure);
1076
Ben Murdoch257744e2011-11-30 15:57:28 +00001077 void LoadInstanceDescriptors(Register map, Register descriptors);
1078
Steve Block44f0eee2011-05-26 01:26:41 +01001079 private:
1080 void CallCFunctionHelper(Register function,
1081 ExternalReference function_reference,
1082 Register scratch,
1083 int num_arguments);
1084
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001085 void BranchShort(int16_t offset, BranchDelaySlot bdslot = PROTECT);
1086 void BranchShort(int16_t offset, Condition cond, Register rs,
1087 const Operand& rt,
1088 BranchDelaySlot bdslot = PROTECT);
1089 void BranchShort(Label* L, BranchDelaySlot bdslot = PROTECT);
1090 void BranchShort(Label* L, Condition cond, Register rs,
1091 const Operand& rt,
1092 BranchDelaySlot bdslot = PROTECT);
1093 void BranchAndLinkShort(int16_t offset, BranchDelaySlot bdslot = PROTECT);
1094 void BranchAndLinkShort(int16_t offset, Condition cond, Register rs,
1095 const Operand& rt,
1096 BranchDelaySlot bdslot = PROTECT);
1097 void BranchAndLinkShort(Label* L, BranchDelaySlot bdslot = PROTECT);
1098 void BranchAndLinkShort(Label* L, Condition cond, Register rs,
1099 const Operand& rt,
1100 BranchDelaySlot bdslot = PROTECT);
1101 void J(Label* L, BranchDelaySlot bdslot);
1102 void Jr(Label* L, BranchDelaySlot bdslot);
1103 void Jalr(Label* L, BranchDelaySlot bdslot);
Steve Block6ded16b2010-05-10 14:33:55 +01001104
1105 // Helper functions for generating invokes.
1106 void InvokePrologue(const ParameterCount& expected,
1107 const ParameterCount& actual,
1108 Handle<Code> code_constant,
1109 Register code_reg,
1110 Label* done,
Steve Block44f0eee2011-05-26 01:26:41 +01001111 InvokeFlag flag,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001112 const CallWrapper& call_wrapper,
1113 CallKind call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01001114
1115 // Get the code for the given builtin. Returns if able to resolve
1116 // the function in the 'resolved' flag.
1117 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
1118
1119 // Activation support.
Steve Block6ded16b2010-05-10 14:33:55 +01001120 void EnterFrame(StackFrame::Type type);
1121 void LeaveFrame(StackFrame::Type type);
Steve Block44f0eee2011-05-26 01:26:41 +01001122
1123 void InitializeNewString(Register string,
1124 Register length,
1125 Heap::RootListIndex map_index,
1126 Register scratch1,
1127 Register scratch2);
1128
Ben Murdoch257744e2011-11-30 15:57:28 +00001129 // Compute memory operands for safepoint stack slots.
1130 static int SafepointRegisterStackIndex(int reg_code);
1131 MemOperand SafepointRegisterSlot(Register reg);
1132 MemOperand SafepointRegistersAndDoublesSlot(Register reg);
Steve Block44f0eee2011-05-26 01:26:41 +01001133
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001134 bool UseAbsoluteCodePointers();
1135
Steve Block44f0eee2011-05-26 01:26:41 +01001136 bool generating_stub_;
1137 bool allow_stub_calls_;
1138 // This handle will be patched with the code object on installation.
1139 Handle<Object> code_object_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001140
1141 // Needs access to SafepointRegisterStackIndex for optimized frame
1142 // traversal.
1143 friend class OptimizedFrame;
Steve Block44f0eee2011-05-26 01:26:41 +01001144};
1145
1146
Steve Block44f0eee2011-05-26 01:26:41 +01001147// The code patcher is used to patch (typically) small parts of code e.g. for
1148// debugging and other types of instrumentation. When using the code patcher
1149// the exact number of bytes specified must be emitted. It is not legal to emit
1150// relocation information. If any of these constraints are violated it causes
1151// an assertion to fail.
1152class CodePatcher {
1153 public:
1154 CodePatcher(byte* address, int instructions);
1155 virtual ~CodePatcher();
1156
1157 // Macro assembler to emit code.
1158 MacroAssembler* masm() { return &masm_; }
1159
1160 // Emit an instruction directly.
Ben Murdoch257744e2011-11-30 15:57:28 +00001161 void Emit(Instr instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001162
1163 // Emit an address directly.
1164 void Emit(Address addr);
1165
Ben Murdoch257744e2011-11-30 15:57:28 +00001166 // Change the condition part of an instruction leaving the rest of the current
1167 // instruction unchanged.
1168 void ChangeBranchCondition(Condition cond);
1169
Steve Block44f0eee2011-05-26 01:26:41 +01001170 private:
1171 byte* address_; // The address of the code being patched.
1172 int instructions_; // Number of instructions of the expected patch size.
1173 int size_; // Number of bytes of the expected patch size.
1174 MacroAssembler masm_; // Macro assembler used to generate the code.
1175};
Andrei Popescu31002712010-02-23 13:46:05 +00001176
1177
1178// -----------------------------------------------------------------------------
1179// Static helper functions.
1180
Steve Block44f0eee2011-05-26 01:26:41 +01001181static MemOperand ContextOperand(Register context, int index) {
1182 return MemOperand(context, Context::SlotOffset(index));
1183}
1184
1185
1186static inline MemOperand GlobalObjectOperand() {
1187 return ContextOperand(cp, Context::GLOBAL_INDEX);
1188}
1189
1190
Andrei Popescu31002712010-02-23 13:46:05 +00001191// Generate a MemOperand for loading a field from an object.
1192static inline MemOperand FieldMemOperand(Register object, int offset) {
1193 return MemOperand(object, offset - kHeapObjectTag);
1194}
1195
1196
Ben Murdoch257744e2011-11-30 15:57:28 +00001197// Generate a MemOperand for storing arguments 5..N on the stack
1198// when calling CallCFunction().
1199static inline MemOperand CFunctionArgumentOperand(int index) {
1200 ASSERT(index > StandardFrameConstants::kCArgSlotCount);
1201 // Argument 5 takes the slot just past the four Arg-slots.
1202 int offset =
1203 (index - 5) * kPointerSize + StandardFrameConstants::kCArgsSlotsSize;
1204 return MemOperand(sp, offset);
1205}
1206
Andrei Popescu31002712010-02-23 13:46:05 +00001207
1208#ifdef GENERATED_CODE_COVERAGE
1209#define CODE_COVERAGE_STRINGIFY(x) #x
1210#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1211#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1212#define ACCESS_MASM(masm) masm->stop(__FILE_LINE__); masm->
1213#else
1214#define ACCESS_MASM(masm) masm->
1215#endif
1216
1217} } // namespace v8::internal
1218
1219#endif // V8_MIPS_MACRO_ASSEMBLER_MIPS_H_