blob: 8b342a28453617abb8db72bb539e2b530a1ce04f [file] [log] [blame]
Steve Block44f0eee2011-05-26 01:26:41 +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
Ben Murdoch257744e2011-11-30 15:57:28 +000028#include <limits.h> // For LONG_MIN, LONG_MAX.
Andrei Popescu31002712010-02-23 13:46:05 +000029
30#include "v8.h"
31
Leon Clarkef7060e22010-06-03 12:02:55 +010032#if defined(V8_TARGET_ARCH_MIPS)
33
Andrei Popescu31002712010-02-23 13:46:05 +000034#include "bootstrapper.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000035#include "codegen.h"
Andrei Popescu31002712010-02-23 13:46:05 +000036#include "debug.h"
37#include "runtime.h"
38
39namespace v8 {
40namespace internal {
41
Ben Murdoch257744e2011-11-30 15:57:28 +000042MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
43 : Assembler(arg_isolate, buffer, size),
Andrei Popescu31002712010-02-23 13:46:05 +000044 generating_stub_(false),
Ben Murdoch257744e2011-11-30 15:57:28 +000045 allow_stub_calls_(true) {
46 if (isolate() != NULL) {
47 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
48 isolate());
49 }
Andrei Popescu31002712010-02-23 13:46:05 +000050}
51
52
Ben Murdoch257744e2011-11-30 15:57:28 +000053// Arguments macros.
Steve Block44f0eee2011-05-26 01:26:41 +010054#define COND_TYPED_ARGS Condition cond, Register r1, const Operand& r2
55#define COND_ARGS cond, r1, r2
Andrei Popescu31002712010-02-23 13:46:05 +000056
Steve Block44f0eee2011-05-26 01:26:41 +010057#define REGISTER_TARGET_BODY(Name) \
58void MacroAssembler::Name(Register target, \
59 BranchDelaySlot bd) { \
60 Name(Operand(target), bd); \
61} \
62void MacroAssembler::Name(Register target, COND_TYPED_ARGS, \
63 BranchDelaySlot bd) { \
64 Name(Operand(target), COND_ARGS, bd); \
Andrei Popescu31002712010-02-23 13:46:05 +000065}
66
67
Steve Block44f0eee2011-05-26 01:26:41 +010068#define INT_PTR_TARGET_BODY(Name) \
69void MacroAssembler::Name(intptr_t target, RelocInfo::Mode rmode, \
70 BranchDelaySlot bd) { \
71 Name(Operand(target, rmode), bd); \
72} \
73void MacroAssembler::Name(intptr_t target, \
74 RelocInfo::Mode rmode, \
75 COND_TYPED_ARGS, \
76 BranchDelaySlot bd) { \
77 Name(Operand(target, rmode), COND_ARGS, bd); \
Andrei Popescu31002712010-02-23 13:46:05 +000078}
79
80
Steve Block44f0eee2011-05-26 01:26:41 +010081#define BYTE_PTR_TARGET_BODY(Name) \
82void MacroAssembler::Name(byte* target, RelocInfo::Mode rmode, \
83 BranchDelaySlot bd) { \
84 Name(reinterpret_cast<intptr_t>(target), rmode, bd); \
85} \
86void MacroAssembler::Name(byte* target, \
87 RelocInfo::Mode rmode, \
88 COND_TYPED_ARGS, \
89 BranchDelaySlot bd) { \
90 Name(reinterpret_cast<intptr_t>(target), rmode, COND_ARGS, bd); \
Andrei Popescu31002712010-02-23 13:46:05 +000091}
92
93
Steve Block44f0eee2011-05-26 01:26:41 +010094#define CODE_TARGET_BODY(Name) \
95void MacroAssembler::Name(Handle<Code> target, RelocInfo::Mode rmode, \
96 BranchDelaySlot bd) { \
97 Name(reinterpret_cast<intptr_t>(target.location()), rmode, bd); \
98} \
99void MacroAssembler::Name(Handle<Code> target, \
100 RelocInfo::Mode rmode, \
101 COND_TYPED_ARGS, \
102 BranchDelaySlot bd) { \
103 Name(reinterpret_cast<intptr_t>(target.location()), rmode, COND_ARGS, bd); \
Andrei Popescu31002712010-02-23 13:46:05 +0000104}
105
106
Steve Block44f0eee2011-05-26 01:26:41 +0100107REGISTER_TARGET_BODY(Jump)
108REGISTER_TARGET_BODY(Call)
109INT_PTR_TARGET_BODY(Jump)
110INT_PTR_TARGET_BODY(Call)
111BYTE_PTR_TARGET_BODY(Jump)
112BYTE_PTR_TARGET_BODY(Call)
113CODE_TARGET_BODY(Jump)
114CODE_TARGET_BODY(Call)
115
116#undef COND_TYPED_ARGS
117#undef COND_ARGS
118#undef REGISTER_TARGET_BODY
119#undef BYTE_PTR_TARGET_BODY
120#undef CODE_TARGET_BODY
121
122
123void MacroAssembler::Ret(BranchDelaySlot bd) {
124 Jump(Operand(ra), bd);
Andrei Popescu31002712010-02-23 13:46:05 +0000125}
126
127
Steve Block44f0eee2011-05-26 01:26:41 +0100128void MacroAssembler::Ret(Condition cond, Register r1, const Operand& r2,
129 BranchDelaySlot bd) {
130 Jump(Operand(ra), cond, r1, r2, bd);
Andrei Popescu31002712010-02-23 13:46:05 +0000131}
132
133
134void MacroAssembler::LoadRoot(Register destination,
135 Heap::RootListIndex index) {
Steve Block6ded16b2010-05-10 14:33:55 +0100136 lw(destination, MemOperand(s6, index << kPointerSizeLog2));
Andrei Popescu31002712010-02-23 13:46:05 +0000137}
138
Steve Block44f0eee2011-05-26 01:26:41 +0100139
Andrei Popescu31002712010-02-23 13:46:05 +0000140void MacroAssembler::LoadRoot(Register destination,
141 Heap::RootListIndex index,
142 Condition cond,
143 Register src1, const Operand& src2) {
Steve Block44f0eee2011-05-26 01:26:41 +0100144 Branch(2, NegateCondition(cond), src1, src2);
Steve Block6ded16b2010-05-10 14:33:55 +0100145 lw(destination, MemOperand(s6, index << kPointerSizeLog2));
Andrei Popescu31002712010-02-23 13:46:05 +0000146}
147
148
Steve Block44f0eee2011-05-26 01:26:41 +0100149void MacroAssembler::StoreRoot(Register source,
150 Heap::RootListIndex index) {
151 sw(source, MemOperand(s6, index << kPointerSizeLog2));
152}
153
154
155void MacroAssembler::StoreRoot(Register source,
156 Heap::RootListIndex index,
157 Condition cond,
158 Register src1, const Operand& src2) {
159 Branch(2, NegateCondition(cond), src1, src2);
160 sw(source, MemOperand(s6, index << kPointerSizeLog2));
161}
162
163
164void MacroAssembler::RecordWriteHelper(Register object,
165 Register address,
166 Register scratch) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000167 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100168 // Check that the object is not in new space.
169 Label not_in_new_space;
170 InNewSpace(object, scratch, ne, &not_in_new_space);
171 Abort("new-space object passed to RecordWriteHelper");
172 bind(&not_in_new_space);
173 }
174
175 // Calculate page address: Clear bits from 0 to kPageSizeBits.
176 if (mips32r2) {
177 Ins(object, zero_reg, 0, kPageSizeBits);
178 } else {
179 // The Ins macro is slow on r1, so use shifts instead.
180 srl(object, object, kPageSizeBits);
181 sll(object, object, kPageSizeBits);
182 }
183
184 // Calculate region number.
185 Ext(address, address, Page::kRegionSizeLog2,
186 kPageSizeBits - Page::kRegionSizeLog2);
187
188 // Mark region dirty.
189 lw(scratch, MemOperand(object, Page::kDirtyFlagOffset));
190 li(at, Operand(1));
191 sllv(at, at, address);
192 or_(scratch, scratch, at);
193 sw(scratch, MemOperand(object, Page::kDirtyFlagOffset));
194}
195
Ben Murdoch257744e2011-11-30 15:57:28 +0000196// Push and pop all registers that can hold pointers.
197void MacroAssembler::PushSafepointRegisters() {
198 // Safepoints expect a block of kNumSafepointRegisters values on the
199 // stack, so adjust the stack for unsaved registers.
200 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
201 ASSERT(num_unsaved >= 0);
202 Subu(sp, sp, Operand(num_unsaved * kPointerSize));
203 MultiPush(kSafepointSavedRegisters);
204}
205
206void MacroAssembler::PopSafepointRegisters() {
207 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
208 MultiPop(kSafepointSavedRegisters);
209 Addu(sp, sp, Operand(num_unsaved * kPointerSize));
210}
211
212void MacroAssembler::PushSafepointRegistersAndDoubles() {
213 PushSafepointRegisters();
214 Subu(sp, sp, Operand(FPURegister::kNumAllocatableRegisters * kDoubleSize));
215 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; i+=2) {
216 FPURegister reg = FPURegister::FromAllocationIndex(i);
217 sdc1(reg, MemOperand(sp, i * kDoubleSize));
218 }
219}
220
221void MacroAssembler::PopSafepointRegistersAndDoubles() {
222 for (int i = 0; i < FPURegister::kNumAllocatableRegisters; i+=2) {
223 FPURegister reg = FPURegister::FromAllocationIndex(i);
224 ldc1(reg, MemOperand(sp, i * kDoubleSize));
225 }
226 Addu(sp, sp, Operand(FPURegister::kNumAllocatableRegisters * kDoubleSize));
227 PopSafepointRegisters();
228}
229
230void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register src,
231 Register dst) {
232 sw(src, SafepointRegistersAndDoublesSlot(dst));
233}
234
235
236void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
237 sw(src, SafepointRegisterSlot(dst));
238}
239
240
241void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
242 lw(dst, SafepointRegisterSlot(src));
243}
244
245
246int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
247 // The registers are pushed starting with the highest encoding,
248 // which means that lowest encodings are closest to the stack pointer.
249 return kSafepointRegisterStackIndexMap[reg_code];
250}
251
252
253MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
254 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
255}
256
257
258MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
259 // General purpose registers are pushed last on the stack.
260 int doubles_size = FPURegister::kNumAllocatableRegisters * kDoubleSize;
261 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
262 return MemOperand(sp, doubles_size + register_offset);
263}
264
265
266
Steve Block44f0eee2011-05-26 01:26:41 +0100267
268void MacroAssembler::InNewSpace(Register object,
269 Register scratch,
270 Condition cc,
271 Label* branch) {
272 ASSERT(cc == eq || cc == ne);
273 And(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
274 Branch(branch, cc, scratch,
275 Operand(ExternalReference::new_space_start(isolate())));
276}
277
278
279// Will clobber 4 registers: object, scratch0, scratch1, at. The
280// register 'object' contains a heap object pointer. The heap object
281// tag is shifted away.
282void MacroAssembler::RecordWrite(Register object,
283 Operand offset,
284 Register scratch0,
285 Register scratch1) {
286 // The compiled code assumes that record write doesn't change the
287 // context register, so we check that none of the clobbered
288 // registers are cp.
289 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
290
291 Label done;
292
293 // First, test that the object is not in the new space. We cannot set
294 // region marks for new space pages.
295 InNewSpace(object, scratch0, eq, &done);
296
297 // Add offset into the object.
298 Addu(scratch0, object, offset);
299
300 // Record the actual write.
301 RecordWriteHelper(object, scratch0, scratch1);
302
303 bind(&done);
304
305 // Clobber all input registers when running with the debug-code flag
306 // turned on to provoke errors.
Ben Murdoch257744e2011-11-30 15:57:28 +0000307 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100308 li(object, Operand(BitCast<int32_t>(kZapValue)));
309 li(scratch0, Operand(BitCast<int32_t>(kZapValue)));
310 li(scratch1, Operand(BitCast<int32_t>(kZapValue)));
311 }
312}
313
314
315// Will clobber 4 registers: object, address, scratch, ip. The
316// register 'object' contains a heap object pointer. The heap object
317// tag is shifted away.
318void MacroAssembler::RecordWrite(Register object,
319 Register address,
Andrei Popescu31002712010-02-23 13:46:05 +0000320 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +0100321 // The compiled code assumes that record write doesn't change the
322 // context register, so we check that none of the clobbered
323 // registers are cp.
324 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
325
326 Label done;
327
328 // First, test that the object is not in the new space. We cannot set
329 // region marks for new space pages.
330 InNewSpace(object, scratch, eq, &done);
331
332 // Record the actual write.
333 RecordWriteHelper(object, address, scratch);
334
335 bind(&done);
336
337 // Clobber all input registers when running with the debug-code flag
338 // turned on to provoke errors.
Ben Murdoch257744e2011-11-30 15:57:28 +0000339 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100340 li(object, Operand(BitCast<int32_t>(kZapValue)));
341 li(address, Operand(BitCast<int32_t>(kZapValue)));
342 li(scratch, Operand(BitCast<int32_t>(kZapValue)));
343 }
344}
345
346
347// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000348// Allocation support.
Steve Block44f0eee2011-05-26 01:26:41 +0100349
350
351void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
352 Register scratch,
353 Label* miss) {
354 Label same_contexts;
355
356 ASSERT(!holder_reg.is(scratch));
357 ASSERT(!holder_reg.is(at));
358 ASSERT(!scratch.is(at));
359
360 // Load current lexical context from the stack frame.
361 lw(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
362 // In debug mode, make sure the lexical context is set.
363#ifdef DEBUG
364 Check(ne, "we should not have an empty lexical context",
365 scratch, Operand(zero_reg));
366#endif
367
368 // Load the global context of the current context.
369 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
370 lw(scratch, FieldMemOperand(scratch, offset));
371 lw(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
372
373 // Check the context is a global context.
Ben Murdoch257744e2011-11-30 15:57:28 +0000374 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100375 // TODO(119): Avoid push(holder_reg)/pop(holder_reg).
Ben Murdoch257744e2011-11-30 15:57:28 +0000376 push(holder_reg); // Temporarily save holder on the stack.
Steve Block44f0eee2011-05-26 01:26:41 +0100377 // Read the first word and compare to the global_context_map.
378 lw(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
379 LoadRoot(at, Heap::kGlobalContextMapRootIndex);
380 Check(eq, "JSGlobalObject::global_context should be a global context.",
381 holder_reg, Operand(at));
Ben Murdoch257744e2011-11-30 15:57:28 +0000382 pop(holder_reg); // Restore holder.
Steve Block44f0eee2011-05-26 01:26:41 +0100383 }
384
385 // Check if both contexts are the same.
386 lw(at, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
387 Branch(&same_contexts, eq, scratch, Operand(at));
388
389 // Check the context is a global context.
Ben Murdoch257744e2011-11-30 15:57:28 +0000390 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100391 // TODO(119): Avoid push(holder_reg)/pop(holder_reg).
Ben Murdoch257744e2011-11-30 15:57:28 +0000392 push(holder_reg); // Temporarily save holder on the stack.
Steve Block44f0eee2011-05-26 01:26:41 +0100393 mov(holder_reg, at); // Move at to its holding place.
394 LoadRoot(at, Heap::kNullValueRootIndex);
395 Check(ne, "JSGlobalProxy::context() should not be null.",
396 holder_reg, Operand(at));
397
398 lw(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
399 LoadRoot(at, Heap::kGlobalContextMapRootIndex);
400 Check(eq, "JSGlobalObject::global_context should be a global context.",
401 holder_reg, Operand(at));
402 // Restore at is not needed. at is reloaded below.
Ben Murdoch257744e2011-11-30 15:57:28 +0000403 pop(holder_reg); // Restore holder.
Steve Block44f0eee2011-05-26 01:26:41 +0100404 // Restore at to holder's context.
405 lw(at, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
406 }
407
408 // Check that the security token in the calling global object is
409 // compatible with the security token in the receiving global
410 // object.
411 int token_offset = Context::kHeaderSize +
412 Context::SECURITY_TOKEN_INDEX * kPointerSize;
413
414 lw(scratch, FieldMemOperand(scratch, token_offset));
415 lw(at, FieldMemOperand(at, token_offset));
416 Branch(miss, ne, scratch, Operand(at));
417
418 bind(&same_contexts);
Andrei Popescu31002712010-02-23 13:46:05 +0000419}
420
421
422// ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000423// Instruction macros.
Andrei Popescu31002712010-02-23 13:46:05 +0000424
Andrei Popescu31002712010-02-23 13:46:05 +0000425void MacroAssembler::Addu(Register rd, Register rs, const Operand& rt) {
426 if (rt.is_reg()) {
427 addu(rd, rs, rt.rm());
428 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100429 if (is_int16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000430 addiu(rd, rs, rt.imm32_);
431 } else {
432 // li handles the relocation.
433 ASSERT(!rs.is(at));
434 li(at, rt);
435 addu(rd, rs, at);
436 }
437 }
438}
439
440
Steve Block44f0eee2011-05-26 01:26:41 +0100441void MacroAssembler::Subu(Register rd, Register rs, const Operand& rt) {
442 if (rt.is_reg()) {
443 subu(rd, rs, rt.rm());
444 } else {
445 if (is_int16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
446 addiu(rd, rs, -rt.imm32_); // No subiu instr, use addiu(x, y, -imm).
447 } else {
448 // li handles the relocation.
449 ASSERT(!rs.is(at));
450 li(at, rt);
451 subu(rd, rs, at);
452 }
453 }
454}
455
456
Andrei Popescu31002712010-02-23 13:46:05 +0000457void MacroAssembler::Mul(Register rd, Register rs, const Operand& rt) {
458 if (rt.is_reg()) {
459 mul(rd, rs, rt.rm());
460 } else {
461 // li handles the relocation.
462 ASSERT(!rs.is(at));
463 li(at, rt);
464 mul(rd, rs, at);
465 }
466}
467
468
469void MacroAssembler::Mult(Register rs, const Operand& rt) {
470 if (rt.is_reg()) {
471 mult(rs, rt.rm());
472 } else {
473 // li handles the relocation.
474 ASSERT(!rs.is(at));
475 li(at, rt);
476 mult(rs, at);
477 }
478}
479
480
481void MacroAssembler::Multu(Register rs, const Operand& rt) {
482 if (rt.is_reg()) {
483 multu(rs, rt.rm());
484 } else {
485 // li handles the relocation.
486 ASSERT(!rs.is(at));
487 li(at, rt);
488 multu(rs, at);
489 }
490}
491
492
493void MacroAssembler::Div(Register rs, const Operand& rt) {
494 if (rt.is_reg()) {
495 div(rs, rt.rm());
496 } else {
497 // li handles the relocation.
498 ASSERT(!rs.is(at));
499 li(at, rt);
500 div(rs, at);
501 }
502}
503
504
505void MacroAssembler::Divu(Register rs, const Operand& rt) {
506 if (rt.is_reg()) {
507 divu(rs, rt.rm());
508 } else {
509 // li handles the relocation.
510 ASSERT(!rs.is(at));
511 li(at, rt);
512 divu(rs, at);
513 }
514}
515
516
517void MacroAssembler::And(Register rd, Register rs, const Operand& rt) {
518 if (rt.is_reg()) {
519 and_(rd, rs, rt.rm());
520 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100521 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000522 andi(rd, rs, rt.imm32_);
523 } else {
524 // li handles the relocation.
525 ASSERT(!rs.is(at));
526 li(at, rt);
527 and_(rd, rs, at);
528 }
529 }
530}
531
532
533void MacroAssembler::Or(Register rd, Register rs, const Operand& rt) {
534 if (rt.is_reg()) {
535 or_(rd, rs, rt.rm());
536 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100537 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000538 ori(rd, rs, rt.imm32_);
539 } else {
540 // li handles the relocation.
541 ASSERT(!rs.is(at));
542 li(at, rt);
543 or_(rd, rs, at);
544 }
545 }
546}
547
548
549void MacroAssembler::Xor(Register rd, Register rs, const Operand& rt) {
550 if (rt.is_reg()) {
551 xor_(rd, rs, rt.rm());
552 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100553 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000554 xori(rd, rs, rt.imm32_);
555 } else {
556 // li handles the relocation.
557 ASSERT(!rs.is(at));
558 li(at, rt);
559 xor_(rd, rs, at);
560 }
561 }
562}
563
564
565void MacroAssembler::Nor(Register rd, Register rs, const Operand& rt) {
566 if (rt.is_reg()) {
567 nor(rd, rs, rt.rm());
568 } else {
569 // li handles the relocation.
570 ASSERT(!rs.is(at));
571 li(at, rt);
572 nor(rd, rs, at);
573 }
574}
575
576
Ben Murdoch257744e2011-11-30 15:57:28 +0000577void MacroAssembler::Neg(Register rs, const Operand& rt) {
578 ASSERT(rt.is_reg());
579 ASSERT(!at.is(rs));
580 ASSERT(!at.is(rt.rm()));
581 li(at, -1);
582 xor_(rs, rt.rm(), at);
583}
584
585
Andrei Popescu31002712010-02-23 13:46:05 +0000586void MacroAssembler::Slt(Register rd, Register rs, const Operand& rt) {
587 if (rt.is_reg()) {
588 slt(rd, rs, rt.rm());
589 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100590 if (is_int16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000591 slti(rd, rs, rt.imm32_);
592 } else {
593 // li handles the relocation.
594 ASSERT(!rs.is(at));
595 li(at, rt);
596 slt(rd, rs, at);
597 }
598 }
599}
600
601
602void MacroAssembler::Sltu(Register rd, Register rs, const Operand& rt) {
603 if (rt.is_reg()) {
604 sltu(rd, rs, rt.rm());
605 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100606 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000607 sltiu(rd, rs, rt.imm32_);
608 } else {
609 // li handles the relocation.
610 ASSERT(!rs.is(at));
611 li(at, rt);
612 sltu(rd, rs, at);
613 }
614 }
615}
616
617
Steve Block44f0eee2011-05-26 01:26:41 +0100618void MacroAssembler::Ror(Register rd, Register rs, const Operand& rt) {
619 if (mips32r2) {
620 if (rt.is_reg()) {
621 rotrv(rd, rs, rt.rm());
622 } else {
623 rotr(rd, rs, rt.imm32_);
624 }
625 } else {
626 if (rt.is_reg()) {
627 subu(at, zero_reg, rt.rm());
628 sllv(at, rs, at);
629 srlv(rd, rs, rt.rm());
630 or_(rd, rd, at);
631 } else {
632 if (rt.imm32_ == 0) {
633 srl(rd, rs, 0);
634 } else {
635 srl(at, rs, rt.imm32_);
636 sll(rd, rs, (0x20 - rt.imm32_) & 0x1f);
637 or_(rd, rd, at);
638 }
639 }
640 }
Andrei Popescu31002712010-02-23 13:46:05 +0000641}
642
643
Steve Block44f0eee2011-05-26 01:26:41 +0100644//------------Pseudo-instructions-------------
645
Andrei Popescu31002712010-02-23 13:46:05 +0000646void MacroAssembler::li(Register rd, Operand j, bool gen2instr) {
647 ASSERT(!j.is_reg());
Steve Block44f0eee2011-05-26 01:26:41 +0100648 BlockTrampolinePoolScope block_trampoline_pool(this);
649 if (!MustUseReg(j.rmode_) && !gen2instr) {
Andrei Popescu31002712010-02-23 13:46:05 +0000650 // Normal load of an immediate value which does not need Relocation Info.
651 if (is_int16(j.imm32_)) {
652 addiu(rd, zero_reg, j.imm32_);
Steve Block44f0eee2011-05-26 01:26:41 +0100653 } else if (!(j.imm32_ & kHiMask)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000654 ori(rd, zero_reg, j.imm32_);
Steve Block44f0eee2011-05-26 01:26:41 +0100655 } else if (!(j.imm32_ & kImm16Mask)) {
656 lui(rd, (j.imm32_ & kHiMask) >> kLuiShift);
Andrei Popescu31002712010-02-23 13:46:05 +0000657 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100658 lui(rd, (j.imm32_ & kHiMask) >> kLuiShift);
659 ori(rd, rd, (j.imm32_ & kImm16Mask));
Andrei Popescu31002712010-02-23 13:46:05 +0000660 }
Steve Block44f0eee2011-05-26 01:26:41 +0100661 } else if (MustUseReg(j.rmode_) || gen2instr) {
662 if (MustUseReg(j.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000663 RecordRelocInfo(j.rmode_, j.imm32_);
664 }
665 // We need always the same number of instructions as we may need to patch
666 // this code to load another value which may need 2 instructions to load.
Ben Murdoch257744e2011-11-30 15:57:28 +0000667 lui(rd, (j.imm32_ & kHiMask) >> kLuiShift);
668 ori(rd, rd, (j.imm32_ & kImm16Mask));
Andrei Popescu31002712010-02-23 13:46:05 +0000669 }
670}
671
672
Ben Murdoch257744e2011-11-30 15:57:28 +0000673// Exception-generating instructions and debugging support.
Andrei Popescu31002712010-02-23 13:46:05 +0000674void MacroAssembler::stop(const char* msg) {
675 // TO_UPGRADE: Just a break for now. Maybe we could upgrade it.
676 // We use the 0x54321 value to be able to find it easily when reading memory.
677 break_(0x54321);
678}
679
680
681void MacroAssembler::MultiPush(RegList regs) {
682 int16_t NumSaved = 0;
683 int16_t NumToPush = NumberOfBitsSet(regs);
684
685 addiu(sp, sp, -4 * NumToPush);
Steve Block6ded16b2010-05-10 14:33:55 +0100686 for (int16_t i = kNumRegisters; i > 0; i--) {
Andrei Popescu31002712010-02-23 13:46:05 +0000687 if ((regs & (1 << i)) != 0) {
688 sw(ToRegister(i), MemOperand(sp, 4 * (NumToPush - ++NumSaved)));
689 }
690 }
691}
692
693
694void MacroAssembler::MultiPushReversed(RegList regs) {
695 int16_t NumSaved = 0;
696 int16_t NumToPush = NumberOfBitsSet(regs);
697
698 addiu(sp, sp, -4 * NumToPush);
Steve Block6ded16b2010-05-10 14:33:55 +0100699 for (int16_t i = 0; i < kNumRegisters; i++) {
Andrei Popescu31002712010-02-23 13:46:05 +0000700 if ((regs & (1 << i)) != 0) {
701 sw(ToRegister(i), MemOperand(sp, 4 * (NumToPush - ++NumSaved)));
702 }
703 }
704}
705
706
707void MacroAssembler::MultiPop(RegList regs) {
708 int16_t NumSaved = 0;
709
Steve Block6ded16b2010-05-10 14:33:55 +0100710 for (int16_t i = 0; i < kNumRegisters; i++) {
Andrei Popescu31002712010-02-23 13:46:05 +0000711 if ((regs & (1 << i)) != 0) {
712 lw(ToRegister(i), MemOperand(sp, 4 * (NumSaved++)));
713 }
714 }
715 addiu(sp, sp, 4 * NumSaved);
716}
717
718
719void MacroAssembler::MultiPopReversed(RegList regs) {
720 int16_t NumSaved = 0;
721
Steve Block6ded16b2010-05-10 14:33:55 +0100722 for (int16_t i = kNumRegisters; i > 0; i--) {
Andrei Popescu31002712010-02-23 13:46:05 +0000723 if ((regs & (1 << i)) != 0) {
724 lw(ToRegister(i), MemOperand(sp, 4 * (NumSaved++)));
725 }
726 }
727 addiu(sp, sp, 4 * NumSaved);
728}
729
730
Steve Block44f0eee2011-05-26 01:26:41 +0100731void MacroAssembler::Ext(Register rt,
732 Register rs,
733 uint16_t pos,
734 uint16_t size) {
735 ASSERT(pos < 32);
736 ASSERT(pos + size < 32);
Andrei Popescu31002712010-02-23 13:46:05 +0000737
Steve Block44f0eee2011-05-26 01:26:41 +0100738 if (mips32r2) {
739 ext_(rt, rs, pos, size);
740 } else {
741 // Move rs to rt and shift it left then right to get the
742 // desired bitfield on the right side and zeroes on the left.
743 sll(rt, rs, 32 - (pos + size));
744 srl(rt, rt, 32 - size);
745 }
746}
747
748
749void MacroAssembler::Ins(Register rt,
750 Register rs,
751 uint16_t pos,
752 uint16_t size) {
753 ASSERT(pos < 32);
754 ASSERT(pos + size < 32);
755
756 if (mips32r2) {
757 ins_(rt, rs, pos, size);
758 } else {
759 ASSERT(!rt.is(t8) && !rs.is(t8));
760
761 srl(t8, rt, pos + size);
762 // The left chunk from rt that needs to
763 // be saved is on the right side of t8.
764 sll(at, t8, pos + size);
765 // The 'at' register now contains the left chunk on
766 // the left (proper position) and zeroes.
767 sll(t8, rt, 32 - pos);
768 // t8 now contains the right chunk on the left and zeroes.
769 srl(t8, t8, 32 - pos);
770 // t8 now contains the right chunk on
771 // the right (proper position) and zeroes.
772 or_(rt, at, t8);
773 // rt now contains the left and right chunks from the original rt
774 // in their proper position and zeroes in the middle.
775 sll(t8, rs, 32 - size);
776 // t8 now contains the chunk from rs on the left and zeroes.
777 srl(t8, t8, 32 - size - pos);
778 // t8 now contains the original chunk from rs in
779 // the middle (proper position).
780 or_(rt, rt, t8);
781 // rt now contains the result of the ins instruction in R2 mode.
782 }
783}
784
785
786void MacroAssembler::Cvt_d_uw(FPURegister fd, FPURegister fs) {
787 // Move the data from fs to t4.
788 mfc1(t4, fs);
789 return Cvt_d_uw(fd, t4);
790}
791
792
793void MacroAssembler::Cvt_d_uw(FPURegister fd, Register rs) {
794 // Convert rs to a FP value in fd (and fd + 1).
795 // We do this by converting rs minus the MSB to avoid sign conversion,
796 // then adding 2^31-1 and 1 to the result.
797
798 ASSERT(!fd.is(f20));
799 ASSERT(!rs.is(t9));
800 ASSERT(!rs.is(t8));
801
Ben Murdoch257744e2011-11-30 15:57:28 +0000802 // Save rs's MSB to t8.
Steve Block44f0eee2011-05-26 01:26:41 +0100803 And(t8, rs, 0x80000000);
804 // Remove rs's MSB.
805 And(t9, rs, 0x7FFFFFFF);
Ben Murdoch257744e2011-11-30 15:57:28 +0000806 // Move t9 to fd.
Steve Block44f0eee2011-05-26 01:26:41 +0100807 mtc1(t9, fd);
808
809 // Convert fd to a real FP value.
810 cvt_d_w(fd, fd);
811
812 Label conversion_done;
813
814 // If rs's MSB was 0, it's done.
815 // Otherwise we need to add that to the FP register.
816 Branch(&conversion_done, eq, t8, Operand(zero_reg));
817
818 // First load 2^31 - 1 into f20.
819 Or(t9, zero_reg, 0x7FFFFFFF);
820 mtc1(t9, f20);
821
822 // Convert it to FP and add it to fd.
823 cvt_d_w(f20, f20);
824 add_d(fd, fd, f20);
825 // Now add 1.
826 Or(t9, zero_reg, 1);
827 mtc1(t9, f20);
828
829 cvt_d_w(f20, f20);
830 add_d(fd, fd, f20);
831 bind(&conversion_done);
832}
833
834
835void MacroAssembler::Trunc_uw_d(FPURegister fd, FPURegister fs) {
836 Trunc_uw_d(fs, t4);
837 mtc1(t4, fd);
838}
839
840
841void MacroAssembler::Trunc_uw_d(FPURegister fd, Register rs) {
842 ASSERT(!fd.is(f22));
843 ASSERT(!rs.is(t6));
844
845 // Load 2^31 into f22.
846 Or(t6, zero_reg, 0x80000000);
847 Cvt_d_uw(f22, t6);
848
849 // Test if f22 > fd.
850 c(OLT, D, fd, f22);
851
852 Label simple_convert;
853 // If fd < 2^31 we can convert it normally.
854 bc1t(&simple_convert);
855
856 // First we subtract 2^31 from fd, then trunc it to rs
857 // and add 2^31 to rs.
858
859 sub_d(f22, fd, f22);
860 trunc_w_d(f22, f22);
861 mfc1(rs, f22);
862 or_(rs, rs, t6);
863
864 Label done;
865 Branch(&done);
866 // Simple conversion.
867 bind(&simple_convert);
868 trunc_w_d(f22, fd);
869 mfc1(rs, f22);
870
871 bind(&done);
872}
873
874
875// Tries to get a signed int32 out of a double precision floating point heap
876// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
877// 32bits signed integer range.
878// This method implementation differs from the ARM version for performance
879// reasons.
880void MacroAssembler::ConvertToInt32(Register source,
881 Register dest,
882 Register scratch,
883 Register scratch2,
884 FPURegister double_scratch,
885 Label *not_int32) {
886 Label right_exponent, done;
887 // Get exponent word (ENDIAN issues).
888 lw(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
889 // Get exponent alone in scratch2.
890 And(scratch2, scratch, Operand(HeapNumber::kExponentMask));
891 // Load dest with zero. We use this either for the final shift or
892 // for the answer.
893 mov(dest, zero_reg);
894 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
895 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
896 // the exponent that we are fastest at and also the highest exponent we can
897 // handle here.
898 const uint32_t non_smi_exponent =
899 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
900 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
901 Branch(&right_exponent, eq, scratch2, Operand(non_smi_exponent));
902 // If the exponent is higher than that then go to not_int32 case. This
903 // catches numbers that don't fit in a signed int32, infinities and NaNs.
904 Branch(not_int32, gt, scratch2, Operand(non_smi_exponent));
905
906 // We know the exponent is smaller than 30 (biased). If it is less than
907 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
908 // it rounds to zero.
909 const uint32_t zero_exponent =
910 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
911 Subu(scratch2, scratch2, Operand(zero_exponent));
912 // Dest already has a Smi zero.
913 Branch(&done, lt, scratch2, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000914 if (!CpuFeatures::IsSupported(FPU)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100915 // We have a shifted exponent between 0 and 30 in scratch2.
916 srl(dest, scratch2, HeapNumber::kExponentShift);
917 // We now have the exponent in dest. Subtract from 30 to get
918 // how much to shift down.
919 li(at, Operand(30));
920 subu(dest, at, dest);
921 }
922 bind(&right_exponent);
Ben Murdoch257744e2011-11-30 15:57:28 +0000923 if (CpuFeatures::IsSupported(FPU)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100924 CpuFeatures::Scope scope(FPU);
925 // MIPS FPU instructions implementing double precision to integer
926 // conversion using round to zero. Since the FP value was qualified
927 // above, the resulting integer should be a legal int32.
928 // The original 'Exponent' word is still in scratch.
929 lwc1(double_scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
930 mtc1(scratch, FPURegister::from_code(double_scratch.code() + 1));
931 trunc_w_d(double_scratch, double_scratch);
932 mfc1(dest, double_scratch);
933 } else {
934 // On entry, dest has final downshift, scratch has original sign/exp/mant.
935 // Save sign bit in top bit of dest.
936 And(scratch2, scratch, Operand(0x80000000));
937 Or(dest, dest, Operand(scratch2));
938 // Put back the implicit 1, just above mantissa field.
939 Or(scratch, scratch, Operand(1 << HeapNumber::kExponentShift));
940
941 // Shift up the mantissa bits to take up the space the exponent used to
942 // take. We just orred in the implicit bit so that took care of one and
943 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
944 // distance. But we want to clear the sign-bit so shift one more bit
945 // left, then shift right one bit.
946 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
947 sll(scratch, scratch, shift_distance + 1);
948 srl(scratch, scratch, 1);
949
950 // Get the second half of the double. For some exponents we don't
951 // actually need this because the bits get shifted out again, but
952 // it's probably slower to test than just to do it.
953 lw(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
954 // Extract the top 10 bits, and insert those bottom 10 bits of scratch.
955 // The width of the field here is the same as the shift amount above.
956 const int field_width = shift_distance;
957 Ext(scratch2, scratch2, 32-shift_distance, field_width);
958 Ins(scratch, scratch2, 0, field_width);
959 // Move down according to the exponent.
960 srlv(scratch, scratch, dest);
961 // Prepare the negative version of our integer.
962 subu(scratch2, zero_reg, scratch);
963 // Trick to check sign bit (msb) held in dest, count leading zero.
964 // 0 indicates negative, save negative version with conditional move.
965 clz(dest, dest);
966 movz(scratch, scratch2, dest);
967 mov(dest, scratch);
968 }
969 bind(&done);
970}
971
972
Ben Murdoch257744e2011-11-30 15:57:28 +0000973void MacroAssembler::EmitOutOfInt32RangeTruncate(Register result,
974 Register input_high,
975 Register input_low,
976 Register scratch) {
977 Label done, normal_exponent, restore_sign;
978 // Extract the biased exponent in result.
979 Ext(result,
980 input_high,
981 HeapNumber::kExponentShift,
982 HeapNumber::kExponentBits);
983
984 // Check for Infinity and NaNs, which should return 0.
985 Subu(scratch, result, HeapNumber::kExponentMask);
986 movz(result, zero_reg, scratch);
987 Branch(&done, eq, scratch, Operand(zero_reg));
988
989 // Express exponent as delta to (number of mantissa bits + 31).
990 Subu(result,
991 result,
992 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31));
993
994 // If the delta is strictly positive, all bits would be shifted away,
995 // which means that we can return 0.
996 Branch(&normal_exponent, le, result, Operand(zero_reg));
997 mov(result, zero_reg);
998 Branch(&done);
999
1000 bind(&normal_exponent);
1001 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
1002 // Calculate shift.
1003 Addu(scratch, result, Operand(kShiftBase + HeapNumber::kMantissaBits));
1004
1005 // Save the sign.
1006 Register sign = result;
1007 result = no_reg;
1008 And(sign, input_high, Operand(HeapNumber::kSignMask));
1009
1010 // On ARM shifts > 31 bits are valid and will result in zero. On MIPS we need
1011 // to check for this specific case.
1012 Label high_shift_needed, high_shift_done;
1013 Branch(&high_shift_needed, lt, scratch, Operand(32));
1014 mov(input_high, zero_reg);
1015 Branch(&high_shift_done);
1016 bind(&high_shift_needed);
1017
1018 // Set the implicit 1 before the mantissa part in input_high.
1019 Or(input_high,
1020 input_high,
1021 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
1022 // Shift the mantissa bits to the correct position.
1023 // We don't need to clear non-mantissa bits as they will be shifted away.
1024 // If they weren't, it would mean that the answer is in the 32bit range.
1025 sllv(input_high, input_high, scratch);
1026
1027 bind(&high_shift_done);
1028
1029 // Replace the shifted bits with bits from the lower mantissa word.
1030 Label pos_shift, shift_done;
1031 li(at, 32);
1032 subu(scratch, at, scratch);
1033 Branch(&pos_shift, ge, scratch, Operand(zero_reg));
1034
1035 // Negate scratch.
1036 Subu(scratch, zero_reg, scratch);
1037 sllv(input_low, input_low, scratch);
1038 Branch(&shift_done);
1039
1040 bind(&pos_shift);
1041 srlv(input_low, input_low, scratch);
1042
1043 bind(&shift_done);
1044 Or(input_high, input_high, Operand(input_low));
1045 // Restore sign if necessary.
1046 mov(scratch, sign);
1047 result = sign;
1048 sign = no_reg;
1049 Subu(result, zero_reg, input_high);
1050 movz(result, input_high, scratch);
1051 bind(&done);
1052}
1053
1054
1055void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1056 Register src,
1057 int num_least_bits) {
1058 Ext(dst, src, kSmiTagSize, num_least_bits);
1059}
1060
1061
1062void MacroAssembler::GetLeastBitsFromInt32(Register dst,
1063 Register src,
1064 int num_least_bits) {
1065 And(dst, src, Operand((1 << num_least_bits) - 1));
1066}
1067
1068
Steve Block44f0eee2011-05-26 01:26:41 +01001069// Emulated condtional branches do not emit a nop in the branch delay slot.
1070//
1071// BRANCH_ARGS_CHECK checks that conditional jump arguments are correct.
1072#define BRANCH_ARGS_CHECK(cond, rs, rt) ASSERT( \
1073 (cond == cc_always && rs.is(zero_reg) && rt.rm().is(zero_reg)) || \
1074 (cond != cc_always && (!rs.is(zero_reg) || !rt.rm().is(zero_reg))))
1075
1076
1077void MacroAssembler::Branch(int16_t offset, BranchDelaySlot bdslot) {
1078 b(offset);
1079
1080 // Emit a nop in the branch delay slot if required.
1081 if (bdslot == PROTECT)
1082 nop();
1083}
1084
1085
1086void MacroAssembler::Branch(int16_t offset, Condition cond, Register rs,
1087 const Operand& rt,
1088 BranchDelaySlot bdslot) {
1089 BRANCH_ARGS_CHECK(cond, rs, rt);
1090 ASSERT(!rs.is(zero_reg));
Steve Block6ded16b2010-05-10 14:33:55 +01001091 Register r2 = no_reg;
Steve Block44f0eee2011-05-26 01:26:41 +01001092 Register scratch = at;
1093
Andrei Popescu31002712010-02-23 13:46:05 +00001094 if (rt.is_reg()) {
1095 // We don't want any other register but scratch clobbered.
1096 ASSERT(!scratch.is(rs) && !scratch.is(rt.rm_));
1097 r2 = rt.rm_;
Steve Block44f0eee2011-05-26 01:26:41 +01001098 switch (cond) {
1099 case cc_always:
1100 b(offset);
1101 break;
1102 case eq:
1103 beq(rs, r2, offset);
1104 break;
1105 case ne:
1106 bne(rs, r2, offset);
1107 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001108 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001109 case greater:
1110 if (r2.is(zero_reg)) {
1111 bgtz(rs, offset);
1112 } else {
1113 slt(scratch, r2, rs);
1114 bne(scratch, zero_reg, offset);
1115 }
1116 break;
1117 case greater_equal:
1118 if (r2.is(zero_reg)) {
1119 bgez(rs, offset);
1120 } else {
1121 slt(scratch, rs, r2);
1122 beq(scratch, zero_reg, offset);
1123 }
1124 break;
1125 case less:
1126 if (r2.is(zero_reg)) {
1127 bltz(rs, offset);
1128 } else {
1129 slt(scratch, rs, r2);
1130 bne(scratch, zero_reg, offset);
1131 }
1132 break;
1133 case less_equal:
1134 if (r2.is(zero_reg)) {
1135 blez(rs, offset);
1136 } else {
1137 slt(scratch, r2, rs);
1138 beq(scratch, zero_reg, offset);
1139 }
1140 break;
Andrei Popescu31002712010-02-23 13:46:05 +00001141 // Unsigned comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001142 case Ugreater:
1143 if (r2.is(zero_reg)) {
1144 bgtz(rs, offset);
1145 } else {
1146 sltu(scratch, r2, rs);
1147 bne(scratch, zero_reg, offset);
1148 }
1149 break;
1150 case Ugreater_equal:
1151 if (r2.is(zero_reg)) {
1152 bgez(rs, offset);
1153 } else {
1154 sltu(scratch, rs, r2);
1155 beq(scratch, zero_reg, offset);
1156 }
1157 break;
1158 case Uless:
1159 if (r2.is(zero_reg)) {
1160 b(offset);
1161 } else {
1162 sltu(scratch, rs, r2);
1163 bne(scratch, zero_reg, offset);
1164 }
1165 break;
1166 case Uless_equal:
1167 if (r2.is(zero_reg)) {
1168 b(offset);
1169 } else {
1170 sltu(scratch, r2, rs);
1171 beq(scratch, zero_reg, offset);
1172 }
1173 break;
1174 default:
1175 UNREACHABLE();
1176 }
1177 } else {
1178 // Be careful to always use shifted_branch_offset only just before the
1179 // branch instruction, as the location will be remember for patching the
1180 // target.
1181 switch (cond) {
1182 case cc_always:
1183 b(offset);
1184 break;
1185 case eq:
1186 // We don't want any other register but scratch clobbered.
1187 ASSERT(!scratch.is(rs));
1188 r2 = scratch;
1189 li(r2, rt);
1190 beq(rs, r2, offset);
1191 break;
1192 case ne:
1193 // We don't want any other register but scratch clobbered.
1194 ASSERT(!scratch.is(rs));
1195 r2 = scratch;
1196 li(r2, rt);
1197 bne(rs, r2, offset);
1198 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001199 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001200 case greater:
1201 if (rt.imm32_ == 0) {
1202 bgtz(rs, offset);
1203 } else {
1204 r2 = scratch;
1205 li(r2, rt);
1206 slt(scratch, r2, rs);
1207 bne(scratch, zero_reg, offset);
1208 }
1209 break;
1210 case greater_equal:
1211 if (rt.imm32_ == 0) {
1212 bgez(rs, offset);
1213 } else if (is_int16(rt.imm32_)) {
1214 slti(scratch, rs, rt.imm32_);
1215 beq(scratch, zero_reg, offset);
1216 } else {
1217 r2 = scratch;
1218 li(r2, rt);
1219 sltu(scratch, rs, r2);
1220 beq(scratch, zero_reg, offset);
1221 }
1222 break;
1223 case less:
1224 if (rt.imm32_ == 0) {
1225 bltz(rs, offset);
1226 } else if (is_int16(rt.imm32_)) {
1227 slti(scratch, rs, rt.imm32_);
1228 bne(scratch, zero_reg, offset);
1229 } else {
1230 r2 = scratch;
1231 li(r2, rt);
1232 slt(scratch, rs, r2);
1233 bne(scratch, zero_reg, offset);
1234 }
1235 break;
1236 case less_equal:
1237 if (rt.imm32_ == 0) {
1238 blez(rs, offset);
1239 } else {
1240 r2 = scratch;
1241 li(r2, rt);
1242 slt(scratch, r2, rs);
1243 beq(scratch, zero_reg, offset);
1244 }
1245 break;
1246 // Unsigned comparison.
1247 case Ugreater:
1248 if (rt.imm32_ == 0) {
1249 bgtz(rs, offset);
1250 } else {
1251 r2 = scratch;
1252 li(r2, rt);
1253 sltu(scratch, r2, rs);
1254 bne(scratch, zero_reg, offset);
1255 }
1256 break;
1257 case Ugreater_equal:
1258 if (rt.imm32_ == 0) {
1259 bgez(rs, offset);
1260 } else if (is_int16(rt.imm32_)) {
1261 sltiu(scratch, rs, rt.imm32_);
1262 beq(scratch, zero_reg, offset);
1263 } else {
1264 r2 = scratch;
1265 li(r2, rt);
1266 sltu(scratch, rs, r2);
1267 beq(scratch, zero_reg, offset);
1268 }
1269 break;
1270 case Uless:
1271 if (rt.imm32_ == 0) {
1272 b(offset);
1273 } else if (is_int16(rt.imm32_)) {
1274 sltiu(scratch, rs, rt.imm32_);
1275 bne(scratch, zero_reg, offset);
1276 } else {
1277 r2 = scratch;
1278 li(r2, rt);
1279 sltu(scratch, rs, r2);
1280 bne(scratch, zero_reg, offset);
1281 }
1282 break;
1283 case Uless_equal:
1284 if (rt.imm32_ == 0) {
1285 b(offset);
1286 } else {
1287 r2 = scratch;
1288 li(r2, rt);
1289 sltu(scratch, r2, rs);
1290 beq(scratch, zero_reg, offset);
1291 }
1292 break;
1293 default:
1294 UNREACHABLE();
1295 }
Andrei Popescu31002712010-02-23 13:46:05 +00001296 }
Steve Block44f0eee2011-05-26 01:26:41 +01001297 // Emit a nop in the branch delay slot if required.
1298 if (bdslot == PROTECT)
1299 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00001300}
1301
1302
Steve Block44f0eee2011-05-26 01:26:41 +01001303void MacroAssembler::Branch(Label* L, BranchDelaySlot bdslot) {
Andrei Popescu31002712010-02-23 13:46:05 +00001304 // We use branch_offset as an argument for the branch instructions to be sure
1305 // it is called just before generating the branch instruction, as needed.
1306
Steve Block44f0eee2011-05-26 01:26:41 +01001307 b(shifted_branch_offset(L, false));
Andrei Popescu31002712010-02-23 13:46:05 +00001308
Steve Block44f0eee2011-05-26 01:26:41 +01001309 // Emit a nop in the branch delay slot if required.
1310 if (bdslot == PROTECT)
1311 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00001312}
1313
1314
Steve Block44f0eee2011-05-26 01:26:41 +01001315void MacroAssembler::Branch(Label* L, Condition cond, Register rs,
1316 const Operand& rt,
1317 BranchDelaySlot bdslot) {
1318 BRANCH_ARGS_CHECK(cond, rs, rt);
1319
1320 int32_t offset;
1321 Register r2 = no_reg;
1322 Register scratch = at;
1323 if (rt.is_reg()) {
1324 r2 = rt.rm_;
1325 // Be careful to always use shifted_branch_offset only just before the
1326 // branch instruction, as the location will be remember for patching the
1327 // target.
1328 switch (cond) {
1329 case cc_always:
1330 offset = shifted_branch_offset(L, false);
1331 b(offset);
1332 break;
1333 case eq:
1334 offset = shifted_branch_offset(L, false);
1335 beq(rs, r2, offset);
1336 break;
1337 case ne:
1338 offset = shifted_branch_offset(L, false);
1339 bne(rs, r2, offset);
1340 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001341 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001342 case greater:
1343 if (r2.is(zero_reg)) {
1344 offset = shifted_branch_offset(L, false);
1345 bgtz(rs, offset);
1346 } else {
1347 slt(scratch, r2, rs);
1348 offset = shifted_branch_offset(L, false);
1349 bne(scratch, zero_reg, offset);
1350 }
1351 break;
1352 case greater_equal:
1353 if (r2.is(zero_reg)) {
1354 offset = shifted_branch_offset(L, false);
1355 bgez(rs, offset);
1356 } else {
1357 slt(scratch, rs, r2);
1358 offset = shifted_branch_offset(L, false);
1359 beq(scratch, zero_reg, offset);
1360 }
1361 break;
1362 case less:
1363 if (r2.is(zero_reg)) {
1364 offset = shifted_branch_offset(L, false);
1365 bltz(rs, offset);
1366 } else {
1367 slt(scratch, rs, r2);
1368 offset = shifted_branch_offset(L, false);
1369 bne(scratch, zero_reg, offset);
1370 }
1371 break;
1372 case less_equal:
1373 if (r2.is(zero_reg)) {
1374 offset = shifted_branch_offset(L, false);
1375 blez(rs, offset);
1376 } else {
1377 slt(scratch, r2, rs);
1378 offset = shifted_branch_offset(L, false);
1379 beq(scratch, zero_reg, offset);
1380 }
1381 break;
1382 // Unsigned comparison.
1383 case Ugreater:
1384 if (r2.is(zero_reg)) {
1385 offset = shifted_branch_offset(L, false);
1386 bgtz(rs, offset);
1387 } else {
1388 sltu(scratch, r2, rs);
1389 offset = shifted_branch_offset(L, false);
1390 bne(scratch, zero_reg, offset);
1391 }
1392 break;
1393 case Ugreater_equal:
1394 if (r2.is(zero_reg)) {
1395 offset = shifted_branch_offset(L, false);
1396 bgez(rs, offset);
1397 } else {
1398 sltu(scratch, rs, r2);
1399 offset = shifted_branch_offset(L, false);
1400 beq(scratch, zero_reg, offset);
1401 }
1402 break;
1403 case Uless:
1404 if (r2.is(zero_reg)) {
1405 offset = shifted_branch_offset(L, false);
1406 b(offset);
1407 } else {
1408 sltu(scratch, rs, r2);
1409 offset = shifted_branch_offset(L, false);
1410 bne(scratch, zero_reg, offset);
1411 }
1412 break;
1413 case Uless_equal:
1414 if (r2.is(zero_reg)) {
1415 offset = shifted_branch_offset(L, false);
1416 b(offset);
1417 } else {
1418 sltu(scratch, r2, rs);
1419 offset = shifted_branch_offset(L, false);
1420 beq(scratch, zero_reg, offset);
1421 }
1422 break;
1423 default:
1424 UNREACHABLE();
1425 }
1426 } else {
1427 // Be careful to always use shifted_branch_offset only just before the
1428 // branch instruction, as the location will be remember for patching the
1429 // target.
1430 switch (cond) {
1431 case cc_always:
1432 offset = shifted_branch_offset(L, false);
1433 b(offset);
1434 break;
1435 case eq:
1436 r2 = scratch;
1437 li(r2, rt);
1438 offset = shifted_branch_offset(L, false);
1439 beq(rs, r2, offset);
1440 break;
1441 case ne:
1442 r2 = scratch;
1443 li(r2, rt);
1444 offset = shifted_branch_offset(L, false);
1445 bne(rs, r2, offset);
1446 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001447 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001448 case greater:
1449 if (rt.imm32_ == 0) {
1450 offset = shifted_branch_offset(L, false);
1451 bgtz(rs, offset);
1452 } else {
1453 r2 = scratch;
1454 li(r2, rt);
1455 slt(scratch, r2, rs);
1456 offset = shifted_branch_offset(L, false);
1457 bne(scratch, zero_reg, offset);
1458 }
1459 break;
1460 case greater_equal:
1461 if (rt.imm32_ == 0) {
1462 offset = shifted_branch_offset(L, false);
1463 bgez(rs, offset);
1464 } else if (is_int16(rt.imm32_)) {
1465 slti(scratch, rs, rt.imm32_);
1466 offset = shifted_branch_offset(L, false);
1467 beq(scratch, zero_reg, offset);
1468 } else {
1469 r2 = scratch;
1470 li(r2, rt);
1471 sltu(scratch, rs, r2);
1472 offset = shifted_branch_offset(L, false);
1473 beq(scratch, zero_reg, offset);
1474 }
1475 break;
1476 case less:
1477 if (rt.imm32_ == 0) {
1478 offset = shifted_branch_offset(L, false);
1479 bltz(rs, offset);
1480 } else if (is_int16(rt.imm32_)) {
1481 slti(scratch, rs, rt.imm32_);
1482 offset = shifted_branch_offset(L, false);
1483 bne(scratch, zero_reg, offset);
1484 } else {
1485 r2 = scratch;
1486 li(r2, rt);
1487 slt(scratch, rs, r2);
1488 offset = shifted_branch_offset(L, false);
1489 bne(scratch, zero_reg, offset);
1490 }
1491 break;
1492 case less_equal:
1493 if (rt.imm32_ == 0) {
1494 offset = shifted_branch_offset(L, false);
1495 blez(rs, offset);
1496 } else {
1497 r2 = scratch;
1498 li(r2, rt);
1499 slt(scratch, r2, rs);
1500 offset = shifted_branch_offset(L, false);
1501 beq(scratch, zero_reg, offset);
1502 }
1503 break;
1504 // Unsigned comparison.
1505 case Ugreater:
1506 if (rt.imm32_ == 0) {
1507 offset = shifted_branch_offset(L, false);
1508 bgtz(rs, offset);
1509 } else {
1510 r2 = scratch;
1511 li(r2, rt);
1512 sltu(scratch, r2, rs);
1513 offset = shifted_branch_offset(L, false);
1514 bne(scratch, zero_reg, offset);
1515 }
1516 break;
1517 case Ugreater_equal:
1518 if (rt.imm32_ == 0) {
1519 offset = shifted_branch_offset(L, false);
1520 bgez(rs, offset);
1521 } else if (is_int16(rt.imm32_)) {
1522 sltiu(scratch, rs, rt.imm32_);
1523 offset = shifted_branch_offset(L, false);
1524 beq(scratch, zero_reg, offset);
1525 } else {
1526 r2 = scratch;
1527 li(r2, rt);
1528 sltu(scratch, rs, r2);
1529 offset = shifted_branch_offset(L, false);
1530 beq(scratch, zero_reg, offset);
1531 }
1532 break;
1533 case Uless:
1534 if (rt.imm32_ == 0) {
1535 offset = shifted_branch_offset(L, false);
1536 b(offset);
1537 } else if (is_int16(rt.imm32_)) {
1538 sltiu(scratch, rs, rt.imm32_);
1539 offset = shifted_branch_offset(L, false);
1540 bne(scratch, zero_reg, offset);
1541 } else {
1542 r2 = scratch;
1543 li(r2, rt);
1544 sltu(scratch, rs, r2);
1545 offset = shifted_branch_offset(L, false);
1546 bne(scratch, zero_reg, offset);
1547 }
1548 break;
1549 case Uless_equal:
1550 if (rt.imm32_ == 0) {
1551 offset = shifted_branch_offset(L, false);
1552 b(offset);
1553 } else {
1554 r2 = scratch;
1555 li(r2, rt);
1556 sltu(scratch, r2, rs);
1557 offset = shifted_branch_offset(L, false);
1558 beq(scratch, zero_reg, offset);
1559 }
1560 break;
1561 default:
1562 UNREACHABLE();
1563 }
1564 }
1565 // Check that offset could actually hold on an int16_t.
1566 ASSERT(is_int16(offset));
1567 // Emit a nop in the branch delay slot if required.
1568 if (bdslot == PROTECT)
1569 nop();
1570}
1571
1572
Andrei Popescu31002712010-02-23 13:46:05 +00001573// We need to use a bgezal or bltzal, but they can't be used directly with the
1574// slt instructions. We could use sub or add instead but we would miss overflow
1575// cases, so we keep slt and add an intermediate third instruction.
Steve Block44f0eee2011-05-26 01:26:41 +01001576void MacroAssembler::BranchAndLink(int16_t offset,
1577 BranchDelaySlot bdslot) {
1578 bal(offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001579
Steve Block44f0eee2011-05-26 01:26:41 +01001580 // Emit a nop in the branch delay slot if required.
1581 if (bdslot == PROTECT)
1582 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00001583}
1584
1585
Steve Block44f0eee2011-05-26 01:26:41 +01001586void MacroAssembler::BranchAndLink(int16_t offset, Condition cond, Register rs,
1587 const Operand& rt,
1588 BranchDelaySlot bdslot) {
1589 BRANCH_ARGS_CHECK(cond, rs, rt);
Steve Block6ded16b2010-05-10 14:33:55 +01001590 Register r2 = no_reg;
Steve Block44f0eee2011-05-26 01:26:41 +01001591 Register scratch = at;
1592
Andrei Popescu31002712010-02-23 13:46:05 +00001593 if (rt.is_reg()) {
1594 r2 = rt.rm_;
1595 } else if (cond != cc_always) {
1596 r2 = scratch;
1597 li(r2, rt);
1598 }
1599
1600 switch (cond) {
1601 case cc_always:
Steve Block44f0eee2011-05-26 01:26:41 +01001602 bal(offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001603 break;
1604 case eq:
1605 bne(rs, r2, 2);
1606 nop();
Steve Block44f0eee2011-05-26 01:26:41 +01001607 bal(offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001608 break;
1609 case ne:
1610 beq(rs, r2, 2);
1611 nop();
Steve Block44f0eee2011-05-26 01:26:41 +01001612 bal(offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001613 break;
1614
Ben Murdoch257744e2011-11-30 15:57:28 +00001615 // Signed comparison.
Andrei Popescu31002712010-02-23 13:46:05 +00001616 case greater:
1617 slt(scratch, r2, rs);
1618 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001619 bgezal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001620 break;
1621 case greater_equal:
1622 slt(scratch, rs, r2);
1623 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001624 bltzal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001625 break;
1626 case less:
1627 slt(scratch, rs, r2);
1628 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001629 bgezal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001630 break;
1631 case less_equal:
1632 slt(scratch, r2, rs);
1633 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001634 bltzal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001635 break;
1636
1637 // Unsigned comparison.
1638 case Ugreater:
1639 sltu(scratch, r2, rs);
1640 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001641 bgezal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001642 break;
1643 case Ugreater_equal:
1644 sltu(scratch, rs, r2);
1645 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001646 bltzal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001647 break;
1648 case Uless:
1649 sltu(scratch, rs, r2);
1650 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001651 bgezal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001652 break;
1653 case Uless_equal:
1654 sltu(scratch, r2, rs);
1655 addiu(scratch, scratch, -1);
Steve Block44f0eee2011-05-26 01:26:41 +01001656 bltzal(scratch, offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001657 break;
1658
1659 default:
1660 UNREACHABLE();
1661 }
Steve Block44f0eee2011-05-26 01:26:41 +01001662 // Emit a nop in the branch delay slot if required.
1663 if (bdslot == PROTECT)
1664 nop();
1665}
1666
1667
1668void MacroAssembler::BranchAndLink(Label* L, BranchDelaySlot bdslot) {
1669 bal(shifted_branch_offset(L, false));
1670
1671 // Emit a nop in the branch delay slot if required.
1672 if (bdslot == PROTECT)
1673 nop();
1674}
1675
1676
1677void MacroAssembler::BranchAndLink(Label* L, Condition cond, Register rs,
1678 const Operand& rt,
1679 BranchDelaySlot bdslot) {
1680 BRANCH_ARGS_CHECK(cond, rs, rt);
1681
1682 int32_t offset;
1683 Register r2 = no_reg;
1684 Register scratch = at;
1685 if (rt.is_reg()) {
1686 r2 = rt.rm_;
1687 } else if (cond != cc_always) {
1688 r2 = scratch;
1689 li(r2, rt);
1690 }
1691
1692 switch (cond) {
1693 case cc_always:
1694 offset = shifted_branch_offset(L, false);
1695 bal(offset);
1696 break;
1697 case eq:
1698 bne(rs, r2, 2);
1699 nop();
1700 offset = shifted_branch_offset(L, false);
1701 bal(offset);
1702 break;
1703 case ne:
1704 beq(rs, r2, 2);
1705 nop();
1706 offset = shifted_branch_offset(L, false);
1707 bal(offset);
1708 break;
1709
Ben Murdoch257744e2011-11-30 15:57:28 +00001710 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001711 case greater:
1712 slt(scratch, r2, rs);
1713 addiu(scratch, scratch, -1);
1714 offset = shifted_branch_offset(L, false);
1715 bgezal(scratch, offset);
1716 break;
1717 case greater_equal:
1718 slt(scratch, rs, r2);
1719 addiu(scratch, scratch, -1);
1720 offset = shifted_branch_offset(L, false);
1721 bltzal(scratch, offset);
1722 break;
1723 case less:
1724 slt(scratch, rs, r2);
1725 addiu(scratch, scratch, -1);
1726 offset = shifted_branch_offset(L, false);
1727 bgezal(scratch, offset);
1728 break;
1729 case less_equal:
1730 slt(scratch, r2, rs);
1731 addiu(scratch, scratch, -1);
1732 offset = shifted_branch_offset(L, false);
1733 bltzal(scratch, offset);
1734 break;
1735
1736 // Unsigned comparison.
1737 case Ugreater:
1738 sltu(scratch, r2, rs);
1739 addiu(scratch, scratch, -1);
1740 offset = shifted_branch_offset(L, false);
1741 bgezal(scratch, offset);
1742 break;
1743 case Ugreater_equal:
1744 sltu(scratch, rs, r2);
1745 addiu(scratch, scratch, -1);
1746 offset = shifted_branch_offset(L, false);
1747 bltzal(scratch, offset);
1748 break;
1749 case Uless:
1750 sltu(scratch, rs, r2);
1751 addiu(scratch, scratch, -1);
1752 offset = shifted_branch_offset(L, false);
1753 bgezal(scratch, offset);
1754 break;
1755 case Uless_equal:
1756 sltu(scratch, r2, rs);
1757 addiu(scratch, scratch, -1);
1758 offset = shifted_branch_offset(L, false);
1759 bltzal(scratch, offset);
1760 break;
1761
1762 default:
1763 UNREACHABLE();
1764 }
1765
1766 // Check that offset could actually hold on an int16_t.
1767 ASSERT(is_int16(offset));
1768
1769 // Emit a nop in the branch delay slot if required.
1770 if (bdslot == PROTECT)
1771 nop();
1772}
1773
1774
1775void MacroAssembler::Jump(const Operand& target, BranchDelaySlot bdslot) {
1776 BlockTrampolinePoolScope block_trampoline_pool(this);
1777 if (target.is_reg()) {
1778 jr(target.rm());
1779 } else {
1780 if (!MustUseReg(target.rmode_)) {
1781 j(target.imm32_);
1782 } else {
1783 li(t9, target);
1784 jr(t9);
1785 }
1786 }
1787 // Emit a nop in the branch delay slot if required.
1788 if (bdslot == PROTECT)
1789 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00001790}
1791
1792
1793void MacroAssembler::Jump(const Operand& target,
Steve Block44f0eee2011-05-26 01:26:41 +01001794 Condition cond, Register rs, const Operand& rt,
1795 BranchDelaySlot bdslot) {
1796 BlockTrampolinePoolScope block_trampoline_pool(this);
1797 BRANCH_ARGS_CHECK(cond, rs, rt);
Andrei Popescu31002712010-02-23 13:46:05 +00001798 if (target.is_reg()) {
1799 if (cond == cc_always) {
1800 jr(target.rm());
1801 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001802 Branch(2, NegateCondition(cond), rs, rt);
Andrei Popescu31002712010-02-23 13:46:05 +00001803 jr(target.rm());
1804 }
Steve Block44f0eee2011-05-26 01:26:41 +01001805 } else { // Not register target.
1806 if (!MustUseReg(target.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001807 if (cond == cc_always) {
1808 j(target.imm32_);
1809 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001810 Branch(2, NegateCondition(cond), rs, rt);
Steve Block6ded16b2010-05-10 14:33:55 +01001811 j(target.imm32_); // Will generate only one instruction.
Andrei Popescu31002712010-02-23 13:46:05 +00001812 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001813 } else { // MustUseReg(target).
Steve Block44f0eee2011-05-26 01:26:41 +01001814 li(t9, target);
Andrei Popescu31002712010-02-23 13:46:05 +00001815 if (cond == cc_always) {
Steve Block44f0eee2011-05-26 01:26:41 +01001816 jr(t9);
Andrei Popescu31002712010-02-23 13:46:05 +00001817 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001818 Branch(2, NegateCondition(cond), rs, rt);
1819 jr(t9); // Will generate only one instruction.
Andrei Popescu31002712010-02-23 13:46:05 +00001820 }
1821 }
1822 }
Steve Block44f0eee2011-05-26 01:26:41 +01001823 // Emit a nop in the branch delay slot if required.
1824 if (bdslot == PROTECT)
1825 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00001826}
1827
1828
Ben Murdoch257744e2011-11-30 15:57:28 +00001829int MacroAssembler::CallSize(Handle<Code> code, RelocInfo::Mode rmode) {
1830 return 4 * kInstrSize;
1831}
1832
1833
1834int MacroAssembler::CallSize(Register reg) {
1835 return 2 * kInstrSize;
1836}
1837
1838
Steve Block44f0eee2011-05-26 01:26:41 +01001839// Note: To call gcc-compiled C code on mips, you must call thru t9.
1840void MacroAssembler::Call(const Operand& target, BranchDelaySlot bdslot) {
1841 BlockTrampolinePoolScope block_trampoline_pool(this);
1842 if (target.is_reg()) {
1843 jalr(target.rm());
Ben Murdoch257744e2011-11-30 15:57:28 +00001844 } else { // !target.is_reg().
Steve Block44f0eee2011-05-26 01:26:41 +01001845 if (!MustUseReg(target.rmode_)) {
1846 jal(target.imm32_);
Ben Murdoch257744e2011-11-30 15:57:28 +00001847 } else { // MustUseReg(target).
1848 // Must record previous source positions before the
1849 // li() generates a new code target.
1850 positions_recorder()->WriteRecordedPositions();
Steve Block44f0eee2011-05-26 01:26:41 +01001851 li(t9, target);
1852 jalr(t9);
1853 }
1854 }
1855 // Emit a nop in the branch delay slot if required.
1856 if (bdslot == PROTECT)
1857 nop();
1858}
1859
1860
1861// Note: To call gcc-compiled C code on mips, you must call thru t9.
Andrei Popescu31002712010-02-23 13:46:05 +00001862void MacroAssembler::Call(const Operand& target,
Steve Block44f0eee2011-05-26 01:26:41 +01001863 Condition cond, Register rs, const Operand& rt,
1864 BranchDelaySlot bdslot) {
1865 BlockTrampolinePoolScope block_trampoline_pool(this);
1866 BRANCH_ARGS_CHECK(cond, rs, rt);
Andrei Popescu31002712010-02-23 13:46:05 +00001867 if (target.is_reg()) {
1868 if (cond == cc_always) {
1869 jalr(target.rm());
1870 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001871 Branch(2, NegateCondition(cond), rs, rt);
Andrei Popescu31002712010-02-23 13:46:05 +00001872 jalr(target.rm());
1873 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001874 } else { // !target.is_reg().
Steve Block44f0eee2011-05-26 01:26:41 +01001875 if (!MustUseReg(target.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001876 if (cond == cc_always) {
1877 jal(target.imm32_);
1878 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001879 Branch(2, NegateCondition(cond), rs, rt);
Steve Block6ded16b2010-05-10 14:33:55 +01001880 jal(target.imm32_); // Will generate only one instruction.
Andrei Popescu31002712010-02-23 13:46:05 +00001881 }
Steve Block44f0eee2011-05-26 01:26:41 +01001882 } else { // MustUseReg(target)
1883 li(t9, target);
Andrei Popescu31002712010-02-23 13:46:05 +00001884 if (cond == cc_always) {
Steve Block44f0eee2011-05-26 01:26:41 +01001885 jalr(t9);
Andrei Popescu31002712010-02-23 13:46:05 +00001886 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001887 Branch(2, NegateCondition(cond), rs, rt);
1888 jalr(t9); // Will generate only one instruction.
Andrei Popescu31002712010-02-23 13:46:05 +00001889 }
1890 }
1891 }
Steve Block44f0eee2011-05-26 01:26:41 +01001892 // Emit a nop in the branch delay slot if required.
1893 if (bdslot == PROTECT)
1894 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00001895}
1896
1897
Ben Murdoch257744e2011-11-30 15:57:28 +00001898void MacroAssembler::CallWithAstId(Handle<Code> code,
1899 RelocInfo::Mode rmode,
1900 unsigned ast_id,
1901 Condition cond,
1902 Register r1,
1903 const Operand& r2) {
1904 ASSERT(rmode == RelocInfo::CODE_TARGET_WITH_ID);
1905 ASSERT(ast_id != kNoASTId);
1906 ASSERT(ast_id_for_reloc_info_ == kNoASTId);
1907 ast_id_for_reloc_info_ = ast_id;
1908 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond, r1, r2);
1909}
1910
1911
Steve Block44f0eee2011-05-26 01:26:41 +01001912void MacroAssembler::Drop(int count,
1913 Condition cond,
1914 Register reg,
1915 const Operand& op) {
1916 if (count <= 0) {
1917 return;
1918 }
1919
1920 Label skip;
1921
1922 if (cond != al) {
1923 Branch(&skip, NegateCondition(cond), reg, op);
1924 }
1925
1926 if (count > 0) {
1927 addiu(sp, sp, count * kPointerSize);
1928 }
1929
1930 if (cond != al) {
1931 bind(&skip);
1932 }
1933}
1934
1935
1936void MacroAssembler::DropAndRet(int drop,
1937 Condition cond,
1938 Register r1,
1939 const Operand& r2) {
1940 // This is a workaround to make sure only one branch instruction is
1941 // generated. It relies on Drop and Ret not creating branches if
1942 // cond == cc_always.
1943 Label skip;
1944 if (cond != cc_always) {
1945 Branch(&skip, NegateCondition(cond), r1, r2);
1946 }
1947
1948 Drop(drop);
1949 Ret();
1950
1951 if (cond != cc_always) {
1952 bind(&skip);
1953 }
1954}
1955
1956
1957void MacroAssembler::Swap(Register reg1,
1958 Register reg2,
1959 Register scratch) {
1960 if (scratch.is(no_reg)) {
1961 Xor(reg1, reg1, Operand(reg2));
1962 Xor(reg2, reg2, Operand(reg1));
1963 Xor(reg1, reg1, Operand(reg2));
1964 } else {
1965 mov(scratch, reg1);
1966 mov(reg1, reg2);
1967 mov(reg2, scratch);
1968 }
Andrei Popescu31002712010-02-23 13:46:05 +00001969}
1970
1971
1972void MacroAssembler::Call(Label* target) {
Steve Block44f0eee2011-05-26 01:26:41 +01001973 BranchAndLink(target);
1974}
1975
1976
Steve Block6ded16b2010-05-10 14:33:55 +01001977#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block6ded16b2010-05-10 14:33:55 +01001978
Steve Block44f0eee2011-05-26 01:26:41 +01001979void MacroAssembler::DebugBreak() {
1980 ASSERT(allow_stub_calls());
1981 mov(a0, zero_reg);
1982 li(a1, Operand(ExternalReference(Runtime::kDebugBreak, isolate())));
1983 CEntryStub ces(1);
1984 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
1985}
1986
1987#endif // ENABLE_DEBUGGER_SUPPORT
Steve Block6ded16b2010-05-10 14:33:55 +01001988
1989
Andrei Popescu31002712010-02-23 13:46:05 +00001990// ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00001991// Exception handling.
Andrei Popescu31002712010-02-23 13:46:05 +00001992
1993void MacroAssembler::PushTryHandler(CodeLocation try_location,
1994 HandlerType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001995 // Adjust this code if not the case.
1996 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1997 // The return address is passed in register ra.
1998 if (try_location == IN_JAVASCRIPT) {
1999 if (type == TRY_CATCH_HANDLER) {
2000 li(t0, Operand(StackHandler::TRY_CATCH));
2001 } else {
2002 li(t0, Operand(StackHandler::TRY_FINALLY));
2003 }
2004 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
2005 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
2006 && StackHandlerConstants::kPCOffset == 3 * kPointerSize
2007 && StackHandlerConstants::kNextOffset == 0 * kPointerSize);
2008 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +01002009 li(t2, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Steve Block6ded16b2010-05-10 14:33:55 +01002010 lw(t1, MemOperand(t2));
2011
2012 addiu(sp, sp, -StackHandlerConstants::kSize);
2013 sw(ra, MemOperand(sp, 12));
2014 sw(fp, MemOperand(sp, 8));
2015 sw(t0, MemOperand(sp, 4));
2016 sw(t1, MemOperand(sp, 0));
2017
2018 // Link this handler as the new current one.
2019 sw(sp, MemOperand(t2));
2020
2021 } else {
2022 // Must preserve a0-a3, and s0 (argv).
2023 ASSERT(try_location == IN_JS_ENTRY);
2024 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
2025 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
2026 && StackHandlerConstants::kPCOffset == 3 * kPointerSize
2027 && StackHandlerConstants::kNextOffset == 0 * kPointerSize);
2028
2029 // The frame pointer does not point to a JS frame so we save NULL
2030 // for fp. We expect the code throwing an exception to check fp
2031 // before dereferencing it to restore the context.
2032 li(t0, Operand(StackHandler::ENTRY));
2033
2034 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +01002035 li(t2, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Steve Block6ded16b2010-05-10 14:33:55 +01002036 lw(t1, MemOperand(t2));
2037
2038 addiu(sp, sp, -StackHandlerConstants::kSize);
2039 sw(ra, MemOperand(sp, 12));
2040 sw(zero_reg, MemOperand(sp, 8));
2041 sw(t0, MemOperand(sp, 4));
2042 sw(t1, MemOperand(sp, 0));
2043
2044 // Link this handler as the new current one.
2045 sw(sp, MemOperand(t2));
2046 }
Andrei Popescu31002712010-02-23 13:46:05 +00002047}
2048
2049
2050void MacroAssembler::PopTryHandler() {
Steve Block44f0eee2011-05-26 01:26:41 +01002051 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
2052 pop(a1);
2053 Addu(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2054 li(at, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
2055 sw(a1, MemOperand(at));
Andrei Popescu31002712010-02-23 13:46:05 +00002056}
2057
2058
Ben Murdoch257744e2011-11-30 15:57:28 +00002059void MacroAssembler::Throw(Register value) {
2060 // v0 is expected to hold the exception.
2061 Move(v0, value);
2062
2063 // Adjust this code if not the case.
2064 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
2065
2066 // Drop the sp to the top of the handler.
2067 li(a3, Operand(ExternalReference(Isolate::k_handler_address,
2068 isolate())));
2069 lw(sp, MemOperand(a3));
2070
2071 // Restore the next handler and frame pointer, discard handler state.
2072 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
2073 pop(a2);
2074 sw(a2, MemOperand(a3));
2075 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
2076 MultiPop(a3.bit() | fp.bit());
2077
2078 // Before returning we restore the context from the frame pointer if
2079 // not NULL. The frame pointer is NULL in the exception handler of a
2080 // JS entry frame.
2081 // Set cp to NULL if fp is NULL.
2082 Label done;
2083 Branch(USE_DELAY_SLOT, &done, eq, fp, Operand(zero_reg));
2084 mov(cp, zero_reg); // In branch delay slot.
2085 lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2086 bind(&done);
2087
2088#ifdef DEBUG
2089 // When emitting debug_code, set ra as return address for the jump.
2090 // 5 instructions: add: 1, pop: 2, jump: 2.
2091 const int kOffsetRaInstructions = 5;
2092 Label find_ra;
2093
2094 if (emit_debug_code()) {
2095 // Compute ra for the Jump(t9).
2096 const int kOffsetRaBytes = kOffsetRaInstructions * Assembler::kInstrSize;
2097
2098 // This branch-and-link sequence is needed to get the current PC on mips,
2099 // saved to the ra register. Then adjusted for instruction count.
2100 bal(&find_ra); // bal exposes branch-delay.
2101 nop(); // Branch delay slot nop.
2102 bind(&find_ra);
2103 addiu(ra, ra, kOffsetRaBytes);
2104 }
2105#endif
2106
2107 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
2108 pop(t9); // 2 instructions: lw, add sp.
2109 Jump(t9); // 2 instructions: jr, nop (in delay slot).
2110
2111 if (emit_debug_code()) {
2112 // Make sure that the expected number of instructions were generated.
2113 ASSERT_EQ(kOffsetRaInstructions,
2114 InstructionsGeneratedSince(&find_ra));
2115 }
2116}
2117
2118
2119void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
2120 Register value) {
2121 // Adjust this code if not the case.
2122 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
2123
2124 // v0 is expected to hold the exception.
2125 Move(v0, value);
2126
2127 // Drop sp to the top stack handler.
2128 li(a3, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
2129 lw(sp, MemOperand(a3));
2130
2131 // Unwind the handlers until the ENTRY handler is found.
2132 Label loop, done;
2133 bind(&loop);
2134 // Load the type of the current stack handler.
2135 const int kStateOffset = StackHandlerConstants::kStateOffset;
2136 lw(a2, MemOperand(sp, kStateOffset));
2137 Branch(&done, eq, a2, Operand(StackHandler::ENTRY));
2138 // Fetch the next handler in the list.
2139 const int kNextOffset = StackHandlerConstants::kNextOffset;
2140 lw(sp, MemOperand(sp, kNextOffset));
2141 jmp(&loop);
2142 bind(&done);
2143
2144 // Set the top handler address to next handler past the current ENTRY handler.
2145 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
2146 pop(a2);
2147 sw(a2, MemOperand(a3));
2148
2149 if (type == OUT_OF_MEMORY) {
2150 // Set external caught exception to false.
2151 ExternalReference external_caught(
2152 Isolate::k_external_caught_exception_address, isolate());
2153 li(a0, Operand(false, RelocInfo::NONE));
2154 li(a2, Operand(external_caught));
2155 sw(a0, MemOperand(a2));
2156
2157 // Set pending exception and v0 to out of memory exception.
2158 Failure* out_of_memory = Failure::OutOfMemoryException();
2159 li(v0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
2160 li(a2, Operand(ExternalReference(Isolate::k_pending_exception_address,
2161 isolate())));
2162 sw(v0, MemOperand(a2));
2163 }
2164
2165 // Stack layout at this point. See also StackHandlerConstants.
2166 // sp -> state (ENTRY)
2167 // fp
2168 // ra
2169
2170 // Discard handler state (a2 is not used) and restore frame pointer.
2171 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
2172 MultiPop(a2.bit() | fp.bit()); // a2: discarded state.
2173 // Before returning we restore the context from the frame pointer if
2174 // not NULL. The frame pointer is NULL in the exception handler of a
2175 // JS entry frame.
2176 Label cp_null;
2177 Branch(USE_DELAY_SLOT, &cp_null, eq, fp, Operand(zero_reg));
2178 mov(cp, zero_reg); // In the branch delay slot.
2179 lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2180 bind(&cp_null);
2181
2182#ifdef DEBUG
2183 // When emitting debug_code, set ra as return address for the jump.
2184 // 5 instructions: add: 1, pop: 2, jump: 2.
2185 const int kOffsetRaInstructions = 5;
2186 Label find_ra;
2187
2188 if (emit_debug_code()) {
2189 // Compute ra for the Jump(t9).
2190 const int kOffsetRaBytes = kOffsetRaInstructions * Assembler::kInstrSize;
2191
2192 // This branch-and-link sequence is needed to get the current PC on mips,
2193 // saved to the ra register. Then adjusted for instruction count.
2194 bal(&find_ra); // bal exposes branch-delay slot.
2195 nop(); // Branch delay slot nop.
2196 bind(&find_ra);
2197 addiu(ra, ra, kOffsetRaBytes);
2198 }
2199#endif
2200 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
2201 pop(t9); // 2 instructions: lw, add sp.
2202 Jump(t9); // 2 instructions: jr, nop (in delay slot).
2203
2204 if (emit_debug_code()) {
2205 // Make sure that the expected number of instructions were generated.
2206 ASSERT_EQ(kOffsetRaInstructions,
2207 InstructionsGeneratedSince(&find_ra));
2208 }
2209}
2210
2211
Steve Block44f0eee2011-05-26 01:26:41 +01002212void MacroAssembler::AllocateInNewSpace(int object_size,
2213 Register result,
2214 Register scratch1,
2215 Register scratch2,
2216 Label* gc_required,
2217 AllocationFlags flags) {
2218 if (!FLAG_inline_new) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002219 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002220 // Trash the registers to simulate an allocation failure.
2221 li(result, 0x7091);
2222 li(scratch1, 0x7191);
2223 li(scratch2, 0x7291);
2224 }
2225 jmp(gc_required);
2226 return;
Steve Block6ded16b2010-05-10 14:33:55 +01002227 }
2228
Steve Block44f0eee2011-05-26 01:26:41 +01002229 ASSERT(!result.is(scratch1));
2230 ASSERT(!result.is(scratch2));
2231 ASSERT(!scratch1.is(scratch2));
2232 ASSERT(!scratch1.is(t9));
2233 ASSERT(!scratch2.is(t9));
2234 ASSERT(!result.is(t9));
Steve Block6ded16b2010-05-10 14:33:55 +01002235
Steve Block44f0eee2011-05-26 01:26:41 +01002236 // Make object size into bytes.
2237 if ((flags & SIZE_IN_WORDS) != 0) {
2238 object_size *= kPointerSize;
2239 }
2240 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
Steve Block6ded16b2010-05-10 14:33:55 +01002241
Steve Block44f0eee2011-05-26 01:26:41 +01002242 // Check relative positions of allocation top and limit addresses.
2243 // ARM adds additional checks to make sure the ldm instruction can be
2244 // used. On MIPS we don't have ldm so we don't need additional checks either.
2245 ExternalReference new_space_allocation_top =
2246 ExternalReference::new_space_allocation_top_address(isolate());
2247 ExternalReference new_space_allocation_limit =
2248 ExternalReference::new_space_allocation_limit_address(isolate());
2249 intptr_t top =
2250 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
2251 intptr_t limit =
2252 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
2253 ASSERT((limit - top) == kPointerSize);
2254
2255 // Set up allocation top address and object size registers.
2256 Register topaddr = scratch1;
2257 Register obj_size_reg = scratch2;
2258 li(topaddr, Operand(new_space_allocation_top));
2259 li(obj_size_reg, Operand(object_size));
2260
2261 // This code stores a temporary value in t9.
2262 if ((flags & RESULT_CONTAINS_TOP) == 0) {
2263 // Load allocation top into result and allocation limit into t9.
2264 lw(result, MemOperand(topaddr));
2265 lw(t9, MemOperand(topaddr, kPointerSize));
2266 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002267 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002268 // Assert that result actually contains top on entry. t9 is used
2269 // immediately below so this use of t9 does not cause difference with
2270 // respect to register content between debug and release mode.
2271 lw(t9, MemOperand(topaddr));
2272 Check(eq, "Unexpected allocation top", result, Operand(t9));
2273 }
2274 // Load allocation limit into t9. Result already contains allocation top.
2275 lw(t9, MemOperand(topaddr, limit - top));
2276 }
2277
2278 // Calculate new top and bail out if new space is exhausted. Use result
2279 // to calculate the new top.
2280 Addu(scratch2, result, Operand(obj_size_reg));
2281 Branch(gc_required, Ugreater, scratch2, Operand(t9));
2282 sw(scratch2, MemOperand(topaddr));
2283
2284 // Tag object if requested.
2285 if ((flags & TAG_OBJECT) != 0) {
2286 Addu(result, result, Operand(kHeapObjectTag));
2287 }
Steve Block6ded16b2010-05-10 14:33:55 +01002288}
2289
2290
Steve Block44f0eee2011-05-26 01:26:41 +01002291void MacroAssembler::AllocateInNewSpace(Register object_size,
2292 Register result,
2293 Register scratch1,
2294 Register scratch2,
2295 Label* gc_required,
2296 AllocationFlags flags) {
2297 if (!FLAG_inline_new) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002298 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002299 // Trash the registers to simulate an allocation failure.
2300 li(result, 0x7091);
2301 li(scratch1, 0x7191);
2302 li(scratch2, 0x7291);
2303 }
2304 jmp(gc_required);
2305 return;
2306 }
2307
2308 ASSERT(!result.is(scratch1));
2309 ASSERT(!result.is(scratch2));
2310 ASSERT(!scratch1.is(scratch2));
2311 ASSERT(!scratch1.is(t9) && !scratch2.is(t9) && !result.is(t9));
2312
2313 // Check relative positions of allocation top and limit addresses.
2314 // ARM adds additional checks to make sure the ldm instruction can be
2315 // used. On MIPS we don't have ldm so we don't need additional checks either.
2316 ExternalReference new_space_allocation_top =
2317 ExternalReference::new_space_allocation_top_address(isolate());
2318 ExternalReference new_space_allocation_limit =
2319 ExternalReference::new_space_allocation_limit_address(isolate());
2320 intptr_t top =
2321 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
2322 intptr_t limit =
2323 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
2324 ASSERT((limit - top) == kPointerSize);
2325
2326 // Set up allocation top address and object size registers.
2327 Register topaddr = scratch1;
2328 li(topaddr, Operand(new_space_allocation_top));
2329
2330 // This code stores a temporary value in t9.
2331 if ((flags & RESULT_CONTAINS_TOP) == 0) {
2332 // Load allocation top into result and allocation limit into t9.
2333 lw(result, MemOperand(topaddr));
2334 lw(t9, MemOperand(topaddr, kPointerSize));
2335 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002336 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002337 // Assert that result actually contains top on entry. t9 is used
2338 // immediately below so this use of t9 does not cause difference with
2339 // respect to register content between debug and release mode.
2340 lw(t9, MemOperand(topaddr));
2341 Check(eq, "Unexpected allocation top", result, Operand(t9));
2342 }
2343 // Load allocation limit into t9. Result already contains allocation top.
2344 lw(t9, MemOperand(topaddr, limit - top));
2345 }
2346
2347 // Calculate new top and bail out if new space is exhausted. Use result
2348 // to calculate the new top. Object size may be in words so a shift is
2349 // required to get the number of bytes.
2350 if ((flags & SIZE_IN_WORDS) != 0) {
2351 sll(scratch2, object_size, kPointerSizeLog2);
2352 Addu(scratch2, result, scratch2);
2353 } else {
2354 Addu(scratch2, result, Operand(object_size));
2355 }
2356 Branch(gc_required, Ugreater, scratch2, Operand(t9));
2357
2358 // Update allocation top. result temporarily holds the new top.
Ben Murdoch257744e2011-11-30 15:57:28 +00002359 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002360 And(t9, scratch2, Operand(kObjectAlignmentMask));
2361 Check(eq, "Unaligned allocation in new space", t9, Operand(zero_reg));
2362 }
2363 sw(scratch2, MemOperand(topaddr));
2364
2365 // Tag object if requested.
2366 if ((flags & TAG_OBJECT) != 0) {
2367 Addu(result, result, Operand(kHeapObjectTag));
2368 }
2369}
2370
2371
2372void MacroAssembler::UndoAllocationInNewSpace(Register object,
2373 Register scratch) {
2374 ExternalReference new_space_allocation_top =
2375 ExternalReference::new_space_allocation_top_address(isolate());
2376
2377 // Make sure the object has no tag before resetting top.
2378 And(object, object, Operand(~kHeapObjectTagMask));
2379#ifdef DEBUG
2380 // Check that the object un-allocated is below the current top.
2381 li(scratch, Operand(new_space_allocation_top));
2382 lw(scratch, MemOperand(scratch));
2383 Check(less, "Undo allocation of non allocated memory",
2384 object, Operand(scratch));
2385#endif
2386 // Write the address of the object to un-allocate as the current top.
2387 li(scratch, Operand(new_space_allocation_top));
2388 sw(object, MemOperand(scratch));
2389}
2390
2391
2392void MacroAssembler::AllocateTwoByteString(Register result,
2393 Register length,
2394 Register scratch1,
2395 Register scratch2,
2396 Register scratch3,
2397 Label* gc_required) {
2398 // Calculate the number of bytes needed for the characters in the string while
2399 // observing object alignment.
2400 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
2401 sll(scratch1, length, 1); // Length in bytes, not chars.
2402 addiu(scratch1, scratch1,
2403 kObjectAlignmentMask + SeqTwoByteString::kHeaderSize);
2404 And(scratch1, scratch1, Operand(~kObjectAlignmentMask));
2405
2406 // Allocate two-byte string in new space.
2407 AllocateInNewSpace(scratch1,
2408 result,
2409 scratch2,
2410 scratch3,
2411 gc_required,
2412 TAG_OBJECT);
2413
2414 // Set the map, length and hash field.
2415 InitializeNewString(result,
2416 length,
2417 Heap::kStringMapRootIndex,
2418 scratch1,
2419 scratch2);
2420}
2421
2422
2423void MacroAssembler::AllocateAsciiString(Register result,
2424 Register length,
2425 Register scratch1,
2426 Register scratch2,
2427 Register scratch3,
2428 Label* gc_required) {
2429 // Calculate the number of bytes needed for the characters in the string
2430 // while observing object alignment.
2431 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
2432 ASSERT(kCharSize == 1);
2433 addiu(scratch1, length, kObjectAlignmentMask + SeqAsciiString::kHeaderSize);
2434 And(scratch1, scratch1, Operand(~kObjectAlignmentMask));
2435
2436 // Allocate ASCII string in new space.
2437 AllocateInNewSpace(scratch1,
2438 result,
2439 scratch2,
2440 scratch3,
2441 gc_required,
2442 TAG_OBJECT);
2443
2444 // Set the map, length and hash field.
2445 InitializeNewString(result,
2446 length,
2447 Heap::kAsciiStringMapRootIndex,
2448 scratch1,
2449 scratch2);
2450}
2451
2452
2453void MacroAssembler::AllocateTwoByteConsString(Register result,
2454 Register length,
2455 Register scratch1,
2456 Register scratch2,
2457 Label* gc_required) {
2458 AllocateInNewSpace(ConsString::kSize,
2459 result,
2460 scratch1,
2461 scratch2,
2462 gc_required,
2463 TAG_OBJECT);
2464 InitializeNewString(result,
2465 length,
2466 Heap::kConsStringMapRootIndex,
2467 scratch1,
2468 scratch2);
2469}
2470
2471
2472void MacroAssembler::AllocateAsciiConsString(Register result,
2473 Register length,
2474 Register scratch1,
2475 Register scratch2,
2476 Label* gc_required) {
2477 AllocateInNewSpace(ConsString::kSize,
2478 result,
2479 scratch1,
2480 scratch2,
2481 gc_required,
2482 TAG_OBJECT);
2483 InitializeNewString(result,
2484 length,
2485 Heap::kConsAsciiStringMapRootIndex,
2486 scratch1,
2487 scratch2);
2488}
2489
2490
2491// Allocates a heap number or jumps to the label if the young space is full and
2492// a scavenge is needed.
2493void MacroAssembler::AllocateHeapNumber(Register result,
2494 Register scratch1,
2495 Register scratch2,
2496 Register heap_number_map,
2497 Label* need_gc) {
2498 // Allocate an object in the heap for the heap number and tag it as a heap
2499 // object.
2500 AllocateInNewSpace(HeapNumber::kSize,
2501 result,
2502 scratch1,
2503 scratch2,
2504 need_gc,
2505 TAG_OBJECT);
2506
2507 // Store heap number map in the allocated object.
2508 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2509 sw(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
2510}
2511
2512
2513void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2514 FPURegister value,
2515 Register scratch1,
2516 Register scratch2,
2517 Label* gc_required) {
2518 LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
2519 AllocateHeapNumber(result, scratch1, scratch2, t6, gc_required);
2520 sdc1(value, FieldMemOperand(result, HeapNumber::kValueOffset));
2521}
2522
2523
2524// Copies a fixed number of fields of heap objects from src to dst.
2525void MacroAssembler::CopyFields(Register dst,
2526 Register src,
2527 RegList temps,
2528 int field_count) {
2529 ASSERT((temps & dst.bit()) == 0);
2530 ASSERT((temps & src.bit()) == 0);
2531 // Primitive implementation using only one temporary register.
2532
2533 Register tmp = no_reg;
2534 // Find a temp register in temps list.
2535 for (int i = 0; i < kNumRegisters; i++) {
2536 if ((temps & (1 << i)) != 0) {
2537 tmp.code_ = i;
2538 break;
2539 }
2540 }
2541 ASSERT(!tmp.is(no_reg));
2542
2543 for (int i = 0; i < field_count; i++) {
2544 lw(tmp, FieldMemOperand(src, i * kPointerSize));
2545 sw(tmp, FieldMemOperand(dst, i * kPointerSize));
2546 }
2547}
2548
2549
Ben Murdoch257744e2011-11-30 15:57:28 +00002550void MacroAssembler::CopyBytes(Register src,
2551 Register dst,
2552 Register length,
2553 Register scratch) {
2554 Label align_loop, align_loop_1, word_loop, byte_loop, byte_loop_1, done;
2555
2556 // Align src before copying in word size chunks.
2557 bind(&align_loop);
2558 Branch(&done, eq, length, Operand(zero_reg));
2559 bind(&align_loop_1);
2560 And(scratch, src, kPointerSize - 1);
2561 Branch(&word_loop, eq, scratch, Operand(zero_reg));
2562 lbu(scratch, MemOperand(src));
2563 Addu(src, src, 1);
2564 sb(scratch, MemOperand(dst));
2565 Addu(dst, dst, 1);
2566 Subu(length, length, Operand(1));
2567 Branch(&byte_loop_1, ne, length, Operand(zero_reg));
2568
2569 // Copy bytes in word size chunks.
2570 bind(&word_loop);
2571 if (emit_debug_code()) {
2572 And(scratch, src, kPointerSize - 1);
2573 Assert(eq, "Expecting alignment for CopyBytes",
2574 scratch, Operand(zero_reg));
2575 }
2576 Branch(&byte_loop, lt, length, Operand(kPointerSize));
2577 lw(scratch, MemOperand(src));
2578 Addu(src, src, kPointerSize);
2579
2580 // TODO(kalmard) check if this can be optimized to use sw in most cases.
2581 // Can't use unaligned access - copy byte by byte.
2582 sb(scratch, MemOperand(dst, 0));
2583 srl(scratch, scratch, 8);
2584 sb(scratch, MemOperand(dst, 1));
2585 srl(scratch, scratch, 8);
2586 sb(scratch, MemOperand(dst, 2));
2587 srl(scratch, scratch, 8);
2588 sb(scratch, MemOperand(dst, 3));
2589 Addu(dst, dst, 4);
2590
2591 Subu(length, length, Operand(kPointerSize));
2592 Branch(&word_loop);
2593
2594 // Copy the last bytes if any left.
2595 bind(&byte_loop);
2596 Branch(&done, eq, length, Operand(zero_reg));
2597 bind(&byte_loop_1);
2598 lbu(scratch, MemOperand(src));
2599 Addu(src, src, 1);
2600 sb(scratch, MemOperand(dst));
2601 Addu(dst, dst, 1);
2602 Subu(length, length, Operand(1));
2603 Branch(&byte_loop_1, ne, length, Operand(zero_reg));
2604 bind(&done);
2605}
2606
2607
Steve Block44f0eee2011-05-26 01:26:41 +01002608void MacroAssembler::CheckMap(Register obj,
2609 Register scratch,
2610 Handle<Map> map,
2611 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00002612 SmiCheckType smi_check_type) {
2613 if (smi_check_type == DO_SMI_CHECK) {
Steve Block44f0eee2011-05-26 01:26:41 +01002614 JumpIfSmi(obj, fail);
2615 }
2616 lw(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2617 li(at, Operand(map));
2618 Branch(fail, ne, scratch, Operand(at));
2619}
2620
2621
Ben Murdoch257744e2011-11-30 15:57:28 +00002622void MacroAssembler::DispatchMap(Register obj,
2623 Register scratch,
2624 Handle<Map> map,
2625 Handle<Code> success,
2626 SmiCheckType smi_check_type) {
2627 Label fail;
2628 if (smi_check_type == DO_SMI_CHECK) {
2629 JumpIfSmi(obj, &fail);
2630 }
2631 lw(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2632 Jump(success, RelocInfo::CODE_TARGET, eq, scratch, Operand(map));
2633 bind(&fail);
2634}
2635
2636
Steve Block44f0eee2011-05-26 01:26:41 +01002637void MacroAssembler::CheckMap(Register obj,
2638 Register scratch,
2639 Heap::RootListIndex index,
2640 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00002641 SmiCheckType smi_check_type) {
2642 if (smi_check_type == DO_SMI_CHECK) {
Steve Block44f0eee2011-05-26 01:26:41 +01002643 JumpIfSmi(obj, fail);
2644 }
2645 lw(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2646 LoadRoot(at, index);
2647 Branch(fail, ne, scratch, Operand(at));
Steve Block6ded16b2010-05-10 14:33:55 +01002648}
2649
2650
Ben Murdoch257744e2011-11-30 15:57:28 +00002651void MacroAssembler::GetCFunctionDoubleResult(const DoubleRegister dst) {
2652 CpuFeatures::Scope scope(FPU);
2653 if (IsMipsSoftFloatABI) {
2654 Move(dst, v0, v1);
2655 } else {
2656 Move(dst, f0); // Reg f0 is o32 ABI FP return value.
2657 }
2658}
2659
2660
2661void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg) {
2662 CpuFeatures::Scope scope(FPU);
2663 if (!IsMipsSoftFloatABI) {
2664 Move(f12, dreg);
2665 } else {
2666 Move(a0, a1, dreg);
2667 }
2668}
2669
2670
2671void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg1,
2672 DoubleRegister dreg2) {
2673 CpuFeatures::Scope scope(FPU);
2674 if (!IsMipsSoftFloatABI) {
2675 if (dreg2.is(f12)) {
2676 ASSERT(!dreg1.is(f14));
2677 Move(f14, dreg2);
2678 Move(f12, dreg1);
2679 } else {
2680 Move(f12, dreg1);
2681 Move(f14, dreg2);
2682 }
2683 } else {
2684 Move(a0, a1, dreg1);
2685 Move(a2, a3, dreg2);
2686 }
2687}
2688
2689
2690void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg,
2691 Register reg) {
2692 CpuFeatures::Scope scope(FPU);
2693 if (!IsMipsSoftFloatABI) {
2694 Move(f12, dreg);
2695 Move(a2, reg);
2696 } else {
2697 Move(a2, reg);
2698 Move(a0, a1, dreg);
2699 }
2700}
2701
2702
2703void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
2704 // This macro takes the dst register to make the code more readable
2705 // at the call sites. However, the dst register has to be t1 to
2706 // follow the calling convention which requires the call type to be
2707 // in t1.
2708 ASSERT(dst.is(t1));
2709 if (call_kind == CALL_AS_FUNCTION) {
2710 li(dst, Operand(Smi::FromInt(1)));
2711 } else {
2712 li(dst, Operand(Smi::FromInt(0)));
2713 }
2714}
2715
2716
Steve Block6ded16b2010-05-10 14:33:55 +01002717// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00002718// JavaScript invokes.
Steve Block6ded16b2010-05-10 14:33:55 +01002719
2720void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2721 const ParameterCount& actual,
2722 Handle<Code> code_constant,
2723 Register code_reg,
2724 Label* done,
Steve Block44f0eee2011-05-26 01:26:41 +01002725 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002726 const CallWrapper& call_wrapper,
2727 CallKind call_kind) {
Steve Block6ded16b2010-05-10 14:33:55 +01002728 bool definitely_matches = false;
2729 Label regular_invoke;
2730
2731 // Check whether the expected and actual arguments count match. If not,
2732 // setup registers according to contract with ArgumentsAdaptorTrampoline:
2733 // a0: actual arguments count
2734 // a1: function (passed through to callee)
2735 // a2: expected arguments count
2736 // a3: callee code entry
2737
2738 // The code below is made a lot easier because the calling code already sets
2739 // up actual and expected registers according to the contract if values are
2740 // passed in registers.
2741 ASSERT(actual.is_immediate() || actual.reg().is(a0));
2742 ASSERT(expected.is_immediate() || expected.reg().is(a2));
2743 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(a3));
2744
2745 if (expected.is_immediate()) {
2746 ASSERT(actual.is_immediate());
2747 if (expected.immediate() == actual.immediate()) {
2748 definitely_matches = true;
2749 } else {
2750 li(a0, Operand(actual.immediate()));
2751 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
2752 if (expected.immediate() == sentinel) {
2753 // Don't worry about adapting arguments for builtins that
2754 // don't want that done. Skip adaption code by making it look
2755 // like we have a match between expected and actual number of
2756 // arguments.
2757 definitely_matches = true;
2758 } else {
2759 li(a2, Operand(expected.immediate()));
2760 }
2761 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002762 } else if (actual.is_immediate()) {
2763 Branch(&regular_invoke, eq, expected.reg(), Operand(actual.immediate()));
2764 li(a0, Operand(actual.immediate()));
Steve Block6ded16b2010-05-10 14:33:55 +01002765 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002766 Branch(&regular_invoke, eq, expected.reg(), Operand(actual.reg()));
Steve Block6ded16b2010-05-10 14:33:55 +01002767 }
2768
2769 if (!definitely_matches) {
2770 if (!code_constant.is_null()) {
2771 li(a3, Operand(code_constant));
2772 addiu(a3, a3, Code::kHeaderSize - kHeapObjectTag);
2773 }
2774
Steve Block44f0eee2011-05-26 01:26:41 +01002775 Handle<Code> adaptor =
2776 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Block6ded16b2010-05-10 14:33:55 +01002777 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002778 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
2779 SetCallKind(t1, call_kind);
Steve Block44f0eee2011-05-26 01:26:41 +01002780 Call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdoch257744e2011-11-30 15:57:28 +00002781 call_wrapper.AfterCall();
Steve Block44f0eee2011-05-26 01:26:41 +01002782 jmp(done);
Steve Block6ded16b2010-05-10 14:33:55 +01002783 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002784 SetCallKind(t1, call_kind);
Steve Block44f0eee2011-05-26 01:26:41 +01002785 Jump(adaptor, RelocInfo::CODE_TARGET);
Steve Block6ded16b2010-05-10 14:33:55 +01002786 }
2787 bind(&regular_invoke);
2788 }
2789}
2790
Steve Block44f0eee2011-05-26 01:26:41 +01002791
Steve Block6ded16b2010-05-10 14:33:55 +01002792void MacroAssembler::InvokeCode(Register code,
2793 const ParameterCount& expected,
2794 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +01002795 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002796 const CallWrapper& call_wrapper,
2797 CallKind call_kind) {
Steve Block6ded16b2010-05-10 14:33:55 +01002798 Label done;
2799
Steve Block44f0eee2011-05-26 01:26:41 +01002800 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002801 call_wrapper, call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01002802 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002803 SetCallKind(t1, call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01002804 Call(code);
2805 } else {
2806 ASSERT(flag == JUMP_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00002807 SetCallKind(t1, call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01002808 Jump(code);
2809 }
2810 // Continue here if InvokePrologue does handle the invocation due to
2811 // mismatched parameter counts.
2812 bind(&done);
2813}
2814
2815
2816void MacroAssembler::InvokeCode(Handle<Code> code,
2817 const ParameterCount& expected,
2818 const ParameterCount& actual,
2819 RelocInfo::Mode rmode,
Ben Murdoch257744e2011-11-30 15:57:28 +00002820 InvokeFlag flag,
2821 CallKind call_kind) {
Steve Block6ded16b2010-05-10 14:33:55 +01002822 Label done;
2823
Ben Murdoch257744e2011-11-30 15:57:28 +00002824 InvokePrologue(expected, actual, code, no_reg, &done, flag,
2825 NullCallWrapper(), call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01002826 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002827 SetCallKind(t1, call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01002828 Call(code, rmode);
2829 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002830 SetCallKind(t1, call_kind);
Steve Block6ded16b2010-05-10 14:33:55 +01002831 Jump(code, rmode);
2832 }
2833 // Continue here if InvokePrologue does handle the invocation due to
2834 // mismatched parameter counts.
2835 bind(&done);
2836}
2837
2838
2839void MacroAssembler::InvokeFunction(Register function,
2840 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +01002841 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002842 const CallWrapper& call_wrapper,
2843 CallKind call_kind) {
Steve Block6ded16b2010-05-10 14:33:55 +01002844 // Contract with called JS functions requires that function is passed in a1.
2845 ASSERT(function.is(a1));
2846 Register expected_reg = a2;
2847 Register code_reg = a3;
2848
2849 lw(code_reg, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2850 lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
2851 lw(expected_reg,
2852 FieldMemOperand(code_reg,
2853 SharedFunctionInfo::kFormalParameterCountOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002854 sra(expected_reg, expected_reg, kSmiTagSize);
2855 lw(code_reg, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002856
2857 ParameterCount expected(expected_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00002858 InvokeCode(code_reg, expected, actual, flag, call_wrapper, call_kind);
Steve Block44f0eee2011-05-26 01:26:41 +01002859}
2860
2861
2862void MacroAssembler::InvokeFunction(JSFunction* function,
2863 const ParameterCount& actual,
2864 InvokeFlag flag) {
2865 ASSERT(function->is_compiled());
2866
2867 // Get the function and setup the context.
2868 li(a1, Operand(Handle<JSFunction>(function)));
2869 lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
2870
2871 // Invoke the cached code.
2872 Handle<Code> code(function->code());
2873 ParameterCount expected(function->shared()->formal_parameter_count());
2874 if (V8::UseCrankshaft()) {
2875 UNIMPLEMENTED_MIPS();
2876 } else {
2877 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
2878 }
2879}
2880
2881
2882void MacroAssembler::IsObjectJSObjectType(Register heap_object,
2883 Register map,
2884 Register scratch,
2885 Label* fail) {
2886 lw(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
2887 IsInstanceJSObjectType(map, scratch, fail);
2888}
2889
2890
2891void MacroAssembler::IsInstanceJSObjectType(Register map,
2892 Register scratch,
2893 Label* fail) {
2894 lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2895 Branch(fail, lt, scratch, Operand(FIRST_JS_OBJECT_TYPE));
2896 Branch(fail, gt, scratch, Operand(LAST_JS_OBJECT_TYPE));
2897}
2898
2899
2900void MacroAssembler::IsObjectJSStringType(Register object,
2901 Register scratch,
2902 Label* fail) {
2903 ASSERT(kNotStringTag != 0);
2904
2905 lw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2906 lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
2907 And(scratch, scratch, Operand(kIsNotStringMask));
2908 Branch(fail, ne, scratch, Operand(zero_reg));
Steve Block6ded16b2010-05-10 14:33:55 +01002909}
2910
2911
2912// ---------------------------------------------------------------------------
2913// Support functions.
2914
Steve Block44f0eee2011-05-26 01:26:41 +01002915
2916void MacroAssembler::TryGetFunctionPrototype(Register function,
2917 Register result,
2918 Register scratch,
2919 Label* miss) {
2920 // Check that the receiver isn't a smi.
2921 JumpIfSmi(function, miss);
2922
2923 // Check that the function really is a function. Load map into result reg.
2924 GetObjectType(function, result, scratch);
2925 Branch(miss, ne, scratch, Operand(JS_FUNCTION_TYPE));
2926
2927 // Make sure that the function has an instance prototype.
2928 Label non_instance;
2929 lbu(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
2930 And(scratch, scratch, Operand(1 << Map::kHasNonInstancePrototype));
2931 Branch(&non_instance, ne, scratch, Operand(zero_reg));
2932
2933 // Get the prototype or initial map from the function.
2934 lw(result,
2935 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2936
2937 // If the prototype or initial map is the hole, don't return it and
2938 // simply miss the cache instead. This will allow us to allocate a
2939 // prototype object on-demand in the runtime system.
2940 LoadRoot(t8, Heap::kTheHoleValueRootIndex);
2941 Branch(miss, eq, result, Operand(t8));
2942
2943 // If the function does not have an initial map, we're done.
2944 Label done;
2945 GetObjectType(result, scratch, scratch);
2946 Branch(&done, ne, scratch, Operand(MAP_TYPE));
2947
2948 // Get the prototype from the initial map.
2949 lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
2950 jmp(&done);
2951
2952 // Non-instance prototype: Fetch prototype from constructor field
2953 // in initial map.
2954 bind(&non_instance);
2955 lw(result, FieldMemOperand(result, Map::kConstructorOffset));
2956
2957 // All done.
2958 bind(&done);
2959}
Steve Block6ded16b2010-05-10 14:33:55 +01002960
2961
Steve Block44f0eee2011-05-26 01:26:41 +01002962void MacroAssembler::GetObjectType(Register object,
2963 Register map,
2964 Register type_reg) {
2965 lw(map, FieldMemOperand(object, HeapObject::kMapOffset));
2966 lbu(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
2967}
Steve Block6ded16b2010-05-10 14:33:55 +01002968
2969
2970// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00002971// Runtime calls.
Steve Block6ded16b2010-05-10 14:33:55 +01002972
Andrei Popescu31002712010-02-23 13:46:05 +00002973void MacroAssembler::CallStub(CodeStub* stub, Condition cond,
2974 Register r1, const Operand& r2) {
Steve Block6ded16b2010-05-10 14:33:55 +01002975 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
2976 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond, r1, r2);
Andrei Popescu31002712010-02-23 13:46:05 +00002977}
2978
2979
Ben Murdoch257744e2011-11-30 15:57:28 +00002980MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub, Condition cond,
2981 Register r1, const Operand& r2) {
2982 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
2983 Object* result;
2984 { MaybeObject* maybe_result = stub->TryGetCode();
2985 if (!maybe_result->ToObject(&result)) return maybe_result;
2986 }
2987 Call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET, cond, r1, r2);
2988 return result;
2989}
2990
2991
2992
Steve Block44f0eee2011-05-26 01:26:41 +01002993void MacroAssembler::TailCallStub(CodeStub* stub) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002994 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Steve Block44f0eee2011-05-26 01:26:41 +01002995 Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
Andrei Popescu31002712010-02-23 13:46:05 +00002996}
2997
Ben Murdoch257744e2011-11-30 15:57:28 +00002998MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub,
2999 Condition cond,
3000 Register r1,
3001 const Operand& r2) {
3002 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
3003 Object* result;
3004 { MaybeObject* maybe_result = stub->TryGetCode();
3005 if (!maybe_result->ToObject(&result)) return maybe_result;
3006 }
3007 Jump(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET, cond, r1, r2);
3008 return result;
3009}
3010
3011
3012static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
3013 return ref0.address() - ref1.address();
3014}
3015
3016
3017MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(
3018 ExternalReference function, int stack_space) {
3019 ExternalReference next_address =
3020 ExternalReference::handle_scope_next_address();
3021 const int kNextOffset = 0;
3022 const int kLimitOffset = AddressOffset(
3023 ExternalReference::handle_scope_limit_address(),
3024 next_address);
3025 const int kLevelOffset = AddressOffset(
3026 ExternalReference::handle_scope_level_address(),
3027 next_address);
3028
3029 // Allocate HandleScope in callee-save registers.
3030 li(s3, Operand(next_address));
3031 lw(s0, MemOperand(s3, kNextOffset));
3032 lw(s1, MemOperand(s3, kLimitOffset));
3033 lw(s2, MemOperand(s3, kLevelOffset));
3034 Addu(s2, s2, Operand(1));
3035 sw(s2, MemOperand(s3, kLevelOffset));
3036
3037 // The O32 ABI requires us to pass a pointer in a0 where the returned struct
3038 // (4 bytes) will be placed. This is also built into the Simulator.
3039 // Set up the pointer to the returned value (a0). It was allocated in
3040 // EnterExitFrame.
3041 addiu(a0, fp, ExitFrameConstants::kStackSpaceOffset);
3042
3043 // Native call returns to the DirectCEntry stub which redirects to the
3044 // return address pushed on stack (could have moved after GC).
3045 // DirectCEntry stub itself is generated early and never moves.
3046 DirectCEntryStub stub;
3047 stub.GenerateCall(this, function);
3048
3049 // As mentioned above, on MIPS a pointer is returned - we need to dereference
3050 // it to get the actual return value (which is also a pointer).
3051 lw(v0, MemOperand(v0));
3052
3053 Label promote_scheduled_exception;
3054 Label delete_allocated_handles;
3055 Label leave_exit_frame;
3056
3057 // If result is non-zero, dereference to get the result value
3058 // otherwise set it to undefined.
3059 Label skip;
3060 LoadRoot(a0, Heap::kUndefinedValueRootIndex);
3061 Branch(&skip, eq, v0, Operand(zero_reg));
3062 lw(a0, MemOperand(v0));
3063 bind(&skip);
3064 mov(v0, a0);
3065
3066 // No more valid handles (the result handle was the last one). Restore
3067 // previous handle scope.
3068 sw(s0, MemOperand(s3, kNextOffset));
3069 if (emit_debug_code()) {
3070 lw(a1, MemOperand(s3, kLevelOffset));
3071 Check(eq, "Unexpected level after return from api call", a1, Operand(s2));
3072 }
3073 Subu(s2, s2, Operand(1));
3074 sw(s2, MemOperand(s3, kLevelOffset));
3075 lw(at, MemOperand(s3, kLimitOffset));
3076 Branch(&delete_allocated_handles, ne, s1, Operand(at));
3077
3078 // Check if the function scheduled an exception.
3079 bind(&leave_exit_frame);
3080 LoadRoot(t0, Heap::kTheHoleValueRootIndex);
3081 li(at, Operand(ExternalReference::scheduled_exception_address(isolate())));
3082 lw(t1, MemOperand(at));
3083 Branch(&promote_scheduled_exception, ne, t0, Operand(t1));
3084 li(s0, Operand(stack_space));
3085 LeaveExitFrame(false, s0);
3086 Ret();
3087
3088 bind(&promote_scheduled_exception);
3089 MaybeObject* result = TryTailCallExternalReference(
3090 ExternalReference(Runtime::kPromoteScheduledException, isolate()), 0, 1);
3091 if (result->IsFailure()) {
3092 return result;
3093 }
3094
3095 // HandleScope limit has changed. Delete allocated extensions.
3096 bind(&delete_allocated_handles);
3097 sw(s1, MemOperand(s3, kLimitOffset));
3098 mov(s0, v0);
3099 mov(a0, v0);
3100 PrepareCallCFunction(1, s1);
3101 li(a0, Operand(ExternalReference::isolate_address()));
3102 CallCFunction(ExternalReference::delete_handle_scope_extensions(isolate()),
3103 1);
3104 mov(v0, s0);
3105 jmp(&leave_exit_frame);
3106
3107 return result;
3108}
3109
Andrei Popescu31002712010-02-23 13:46:05 +00003110
Steve Block6ded16b2010-05-10 14:33:55 +01003111void MacroAssembler::IllegalOperation(int num_arguments) {
3112 if (num_arguments > 0) {
3113 addiu(sp, sp, num_arguments * kPointerSize);
3114 }
3115 LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3116}
3117
3118
Steve Block44f0eee2011-05-26 01:26:41 +01003119void MacroAssembler::IndexFromHash(Register hash,
3120 Register index) {
3121 // If the hash field contains an array index pick it out. The assert checks
3122 // that the constants for the maximum number of digits for an array index
3123 // cached in the hash field and the number of bits reserved for it does not
3124 // conflict.
3125 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
3126 (1 << String::kArrayIndexValueBits));
3127 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
3128 // the low kHashShift bits.
3129 STATIC_ASSERT(kSmiTag == 0);
3130 Ext(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
3131 sll(index, hash, kSmiTagSize);
3132}
3133
3134
3135void MacroAssembler::ObjectToDoubleFPURegister(Register object,
3136 FPURegister result,
3137 Register scratch1,
3138 Register scratch2,
3139 Register heap_number_map,
3140 Label* not_number,
3141 ObjectToDoubleFlags flags) {
3142 Label done;
3143 if ((flags & OBJECT_NOT_SMI) == 0) {
3144 Label not_smi;
3145 JumpIfNotSmi(object, &not_smi);
3146 // Remove smi tag and convert to double.
3147 sra(scratch1, object, kSmiTagSize);
3148 mtc1(scratch1, result);
3149 cvt_d_w(result, result);
3150 Branch(&done);
3151 bind(&not_smi);
3152 }
3153 // Check for heap number and load double value from it.
3154 lw(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
3155 Branch(not_number, ne, scratch1, Operand(heap_number_map));
3156
3157 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
3158 // If exponent is all ones the number is either a NaN or +/-Infinity.
3159 Register exponent = scratch1;
3160 Register mask_reg = scratch2;
3161 lw(exponent, FieldMemOperand(object, HeapNumber::kExponentOffset));
3162 li(mask_reg, HeapNumber::kExponentMask);
3163
3164 And(exponent, exponent, mask_reg);
3165 Branch(not_number, eq, exponent, Operand(mask_reg));
3166 }
3167 ldc1(result, FieldMemOperand(object, HeapNumber::kValueOffset));
3168 bind(&done);
3169}
3170
3171
Steve Block44f0eee2011-05-26 01:26:41 +01003172void MacroAssembler::SmiToDoubleFPURegister(Register smi,
3173 FPURegister value,
3174 Register scratch1) {
3175 sra(scratch1, smi, kSmiTagSize);
3176 mtc1(scratch1, value);
3177 cvt_d_w(value, value);
3178}
3179
3180
Ben Murdoch257744e2011-11-30 15:57:28 +00003181void MacroAssembler::AdduAndCheckForOverflow(Register dst,
3182 Register left,
3183 Register right,
3184 Register overflow_dst,
3185 Register scratch) {
3186 ASSERT(!dst.is(overflow_dst));
3187 ASSERT(!dst.is(scratch));
3188 ASSERT(!overflow_dst.is(scratch));
3189 ASSERT(!overflow_dst.is(left));
3190 ASSERT(!overflow_dst.is(right));
3191 ASSERT(!left.is(right));
3192
3193 // TODO(kalmard) There must be a way to optimize dst == left and dst == right
3194 // cases.
3195
3196 if (dst.is(left)) {
3197 addu(overflow_dst, left, right);
3198 xor_(dst, overflow_dst, left);
3199 xor_(scratch, overflow_dst, right);
3200 and_(scratch, scratch, dst);
3201 mov(dst, overflow_dst);
3202 mov(overflow_dst, scratch);
3203 } else if (dst.is(right)) {
3204 addu(overflow_dst, left, right);
3205 xor_(dst, overflow_dst, right);
3206 xor_(scratch, overflow_dst, left);
3207 and_(scratch, scratch, dst);
3208 mov(dst, overflow_dst);
3209 mov(overflow_dst, scratch);
3210 } else {
3211 addu(dst, left, right);
3212 xor_(overflow_dst, dst, left);
3213 xor_(scratch, dst, right);
3214 and_(overflow_dst, scratch, overflow_dst);
3215 }
3216}
3217
3218
3219void MacroAssembler::SubuAndCheckForOverflow(Register dst,
3220 Register left,
3221 Register right,
3222 Register overflow_dst,
3223 Register scratch) {
3224 ASSERT(!dst.is(overflow_dst));
3225 ASSERT(!dst.is(scratch));
3226 ASSERT(!overflow_dst.is(scratch));
3227 ASSERT(!overflow_dst.is(left));
3228 ASSERT(!overflow_dst.is(right));
3229 ASSERT(!left.is(right));
3230 ASSERT(!scratch.is(left));
3231 ASSERT(!scratch.is(right));
3232
3233 // TODO(kalmard) There must be a way to optimize dst == left and dst == right
3234 // cases.
3235
3236 if (dst.is(left)) {
3237 subu(overflow_dst, left, right);
3238 xor_(scratch, overflow_dst, left);
3239 xor_(dst, left, right);
3240 and_(scratch, scratch, dst);
3241 mov(dst, overflow_dst);
3242 mov(overflow_dst, scratch);
3243 } else if (dst.is(right)) {
3244 subu(overflow_dst, left, right);
3245 xor_(dst, left, right);
3246 xor_(scratch, overflow_dst, left);
3247 and_(scratch, scratch, dst);
3248 mov(dst, overflow_dst);
3249 mov(overflow_dst, scratch);
3250 } else {
3251 subu(dst, left, right);
3252 xor_(overflow_dst, dst, left);
3253 xor_(scratch, left, right);
3254 and_(overflow_dst, scratch, overflow_dst);
3255 }
3256}
3257
3258
Steve Block44f0eee2011-05-26 01:26:41 +01003259void MacroAssembler::CallRuntime(const Runtime::Function* f,
3260 int num_arguments) {
Steve Block6ded16b2010-05-10 14:33:55 +01003261 // All parameters are on the stack. v0 has the return value after call.
3262
3263 // If the expected number of arguments of the runtime function is
3264 // constant, we check that the actual number of arguments match the
3265 // expectation.
3266 if (f->nargs >= 0 && f->nargs != num_arguments) {
3267 IllegalOperation(num_arguments);
3268 return;
3269 }
3270
3271 // TODO(1236192): Most runtime routines don't need the number of
3272 // arguments passed in because it is constant. At some point we
3273 // should remove this need and make the runtime routine entry code
3274 // smarter.
3275 li(a0, num_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01003276 li(a1, Operand(ExternalReference(f, isolate())));
Steve Block6ded16b2010-05-10 14:33:55 +01003277 CEntryStub stub(1);
3278 CallStub(&stub);
Andrei Popescu31002712010-02-23 13:46:05 +00003279}
3280
3281
Steve Block44f0eee2011-05-26 01:26:41 +01003282void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
3283 const Runtime::Function* function = Runtime::FunctionForId(id);
3284 li(a0, Operand(function->nargs));
3285 li(a1, Operand(ExternalReference(function, isolate())));
3286 CEntryStub stub(1);
3287 stub.SaveDoubles();
3288 CallStub(&stub);
3289}
3290
3291
Andrei Popescu31002712010-02-23 13:46:05 +00003292void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
Steve Block6ded16b2010-05-10 14:33:55 +01003293 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
3294}
3295
3296
Steve Block44f0eee2011-05-26 01:26:41 +01003297void MacroAssembler::CallExternalReference(const ExternalReference& ext,
3298 int num_arguments) {
3299 li(a0, Operand(num_arguments));
3300 li(a1, Operand(ext));
3301
3302 CEntryStub stub(1);
3303 CallStub(&stub);
3304}
3305
3306
Steve Block6ded16b2010-05-10 14:33:55 +01003307void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
3308 int num_arguments,
3309 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01003310 // TODO(1236192): Most runtime routines don't need the number of
3311 // arguments passed in because it is constant. At some point we
3312 // should remove this need and make the runtime routine entry code
3313 // smarter.
3314 li(a0, Operand(num_arguments));
3315 JumpToExternalReference(ext);
Andrei Popescu31002712010-02-23 13:46:05 +00003316}
3317
Ben Murdoch257744e2011-11-30 15:57:28 +00003318MaybeObject* MacroAssembler::TryTailCallExternalReference(
3319 const ExternalReference& ext, int num_arguments, int result_size) {
3320 // TODO(1236192): Most runtime routines don't need the number of
3321 // arguments passed in because it is constant. At some point we
3322 // should remove this need and make the runtime routine entry code
3323 // smarter.
3324 li(a0, num_arguments);
3325 return TryJumpToExternalReference(ext);
3326}
3327
Andrei Popescu31002712010-02-23 13:46:05 +00003328
Steve Block6ded16b2010-05-10 14:33:55 +01003329void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
Andrei Popescu31002712010-02-23 13:46:05 +00003330 int num_arguments,
3331 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01003332 TailCallExternalReference(ExternalReference(fid, isolate()),
3333 num_arguments,
3334 result_size);
Andrei Popescu31002712010-02-23 13:46:05 +00003335}
3336
3337
Steve Block6ded16b2010-05-10 14:33:55 +01003338void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Block44f0eee2011-05-26 01:26:41 +01003339 li(a1, Operand(builtin));
3340 CEntryStub stub(1);
3341 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
Andrei Popescu31002712010-02-23 13:46:05 +00003342}
3343
3344
Ben Murdoch257744e2011-11-30 15:57:28 +00003345MaybeObject* MacroAssembler::TryJumpToExternalReference(
3346 const ExternalReference& builtin) {
3347 li(a1, Operand(builtin));
3348 CEntryStub stub(1);
3349 return TryTailCallStub(&stub);
3350}
3351
3352
Andrei Popescu31002712010-02-23 13:46:05 +00003353void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +00003354 InvokeFlag flag,
3355 const CallWrapper& call_wrapper) {
Steve Block44f0eee2011-05-26 01:26:41 +01003356 GetBuiltinEntry(t9, id);
Ben Murdoch257744e2011-11-30 15:57:28 +00003357 if (flag == CALL_FUNCTION) {
3358 call_wrapper.BeforeCall(CallSize(t9));
Steve Block44f0eee2011-05-26 01:26:41 +01003359 Call(t9);
Ben Murdoch257744e2011-11-30 15:57:28 +00003360 call_wrapper.AfterCall();
Steve Block44f0eee2011-05-26 01:26:41 +01003361 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00003362 ASSERT(flag == JUMP_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01003363 Jump(t9);
3364 }
3365}
3366
3367
3368void MacroAssembler::GetBuiltinFunction(Register target,
3369 Builtins::JavaScript id) {
3370 // Load the builtins object into target register.
3371 lw(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3372 lw(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
3373 // Load the JavaScript builtin function from the builtins object.
3374 lw(target, FieldMemOperand(target,
3375 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
Andrei Popescu31002712010-02-23 13:46:05 +00003376}
3377
3378
3379void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block44f0eee2011-05-26 01:26:41 +01003380 ASSERT(!target.is(a1));
3381 GetBuiltinFunction(a1, id);
3382 // Load the code entry point from the builtins object.
3383 lw(target, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
Andrei Popescu31002712010-02-23 13:46:05 +00003384}
3385
3386
3387void MacroAssembler::SetCounter(StatsCounter* counter, int value,
3388 Register scratch1, Register scratch2) {
Steve Block44f0eee2011-05-26 01:26:41 +01003389 if (FLAG_native_code_counters && counter->Enabled()) {
3390 li(scratch1, Operand(value));
3391 li(scratch2, Operand(ExternalReference(counter)));
3392 sw(scratch1, MemOperand(scratch2));
3393 }
Andrei Popescu31002712010-02-23 13:46:05 +00003394}
3395
3396
3397void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
3398 Register scratch1, Register scratch2) {
Steve Block44f0eee2011-05-26 01:26:41 +01003399 ASSERT(value > 0);
3400 if (FLAG_native_code_counters && counter->Enabled()) {
3401 li(scratch2, Operand(ExternalReference(counter)));
3402 lw(scratch1, MemOperand(scratch2));
3403 Addu(scratch1, scratch1, Operand(value));
3404 sw(scratch1, MemOperand(scratch2));
3405 }
Andrei Popescu31002712010-02-23 13:46:05 +00003406}
3407
3408
3409void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
3410 Register scratch1, Register scratch2) {
Steve Block44f0eee2011-05-26 01:26:41 +01003411 ASSERT(value > 0);
3412 if (FLAG_native_code_counters && counter->Enabled()) {
3413 li(scratch2, Operand(ExternalReference(counter)));
3414 lw(scratch1, MemOperand(scratch2));
3415 Subu(scratch1, scratch1, Operand(value));
3416 sw(scratch1, MemOperand(scratch2));
3417 }
Andrei Popescu31002712010-02-23 13:46:05 +00003418}
3419
3420
Steve Block6ded16b2010-05-10 14:33:55 +01003421// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00003422// Debugging.
Andrei Popescu31002712010-02-23 13:46:05 +00003423
3424void MacroAssembler::Assert(Condition cc, const char* msg,
3425 Register rs, Operand rt) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003426 if (emit_debug_code())
Steve Block44f0eee2011-05-26 01:26:41 +01003427 Check(cc, msg, rs, rt);
3428}
3429
3430
3431void MacroAssembler::AssertRegisterIsRoot(Register reg,
3432 Heap::RootListIndex index) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003433 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003434 LoadRoot(at, index);
3435 Check(eq, "Register did not match expected root", reg, Operand(at));
3436 }
3437}
3438
3439
3440void MacroAssembler::AssertFastElements(Register elements) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003441 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003442 ASSERT(!elements.is(at));
3443 Label ok;
Ben Murdoch257744e2011-11-30 15:57:28 +00003444 push(elements);
Steve Block44f0eee2011-05-26 01:26:41 +01003445 lw(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
3446 LoadRoot(at, Heap::kFixedArrayMapRootIndex);
3447 Branch(&ok, eq, elements, Operand(at));
3448 LoadRoot(at, Heap::kFixedCOWArrayMapRootIndex);
3449 Branch(&ok, eq, elements, Operand(at));
3450 Abort("JSObject with fast elements map has slow elements");
3451 bind(&ok);
Ben Murdoch257744e2011-11-30 15:57:28 +00003452 pop(elements);
Steve Block44f0eee2011-05-26 01:26:41 +01003453 }
Andrei Popescu31002712010-02-23 13:46:05 +00003454}
3455
3456
3457void MacroAssembler::Check(Condition cc, const char* msg,
3458 Register rs, Operand rt) {
Steve Block44f0eee2011-05-26 01:26:41 +01003459 Label L;
3460 Branch(&L, cc, rs, rt);
3461 Abort(msg);
Ben Murdoch257744e2011-11-30 15:57:28 +00003462 // Will not return here.
Steve Block44f0eee2011-05-26 01:26:41 +01003463 bind(&L);
Andrei Popescu31002712010-02-23 13:46:05 +00003464}
3465
3466
3467void MacroAssembler::Abort(const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01003468 Label abort_start;
3469 bind(&abort_start);
3470 // We want to pass the msg string like a smi to avoid GC
3471 // problems, however msg is not guaranteed to be aligned
3472 // properly. Instead, we pass an aligned pointer that is
3473 // a proper v8 smi, but also pass the alignment difference
3474 // from the real pointer as a smi.
3475 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
3476 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
3477 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
3478#ifdef DEBUG
3479 if (msg != NULL) {
3480 RecordComment("Abort message: ");
3481 RecordComment(msg);
3482 }
3483#endif
3484 // Disable stub call restrictions to always allow calls to abort.
3485 AllowStubCallsScope allow_scope(this, true);
3486
3487 li(a0, Operand(p0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003488 push(a0);
Steve Block44f0eee2011-05-26 01:26:41 +01003489 li(a0, Operand(Smi::FromInt(p1 - p0)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003490 push(a0);
Steve Block44f0eee2011-05-26 01:26:41 +01003491 CallRuntime(Runtime::kAbort, 2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003492 // Will not return here.
Steve Block44f0eee2011-05-26 01:26:41 +01003493 if (is_trampoline_pool_blocked()) {
3494 // If the calling code cares about the exact number of
3495 // instructions generated, we insert padding here to keep the size
3496 // of the Abort macro constant.
3497 // Currently in debug mode with debug_code enabled the number of
3498 // generated instructions is 14, so we use this as a maximum value.
3499 static const int kExpectedAbortInstructions = 14;
3500 int abort_instructions = InstructionsGeneratedSince(&abort_start);
3501 ASSERT(abort_instructions <= kExpectedAbortInstructions);
3502 while (abort_instructions++ < kExpectedAbortInstructions) {
3503 nop();
3504 }
3505 }
3506}
3507
3508
3509void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
3510 if (context_chain_length > 0) {
3511 // Move up the chain of contexts to the context containing the slot.
3512 lw(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
3513 // Load the function context (which is the incoming, outer context).
3514 lw(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
3515 for (int i = 1; i < context_chain_length; i++) {
3516 lw(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
3517 lw(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
3518 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003519 } else {
3520 // Slot is in the current function context. Move it into the
3521 // destination register in case we store into it (the write barrier
3522 // cannot be allowed to destroy the context in esi).
3523 Move(dst, cp);
3524 }
3525
3526 // We should not have found a 'with' context by walking the context chain
3527 // (i.e., the static scope chain and runtime context chain do not agree).
3528 // A variable occurring in such a scope should have slot type LOOKUP and
3529 // not CONTEXT.
3530 if (emit_debug_code()) {
3531 lw(t9, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
3532 Check(eq, "Yo dawg, I heard you liked function contexts "
3533 "so I put function contexts in all your contexts",
3534 dst, Operand(t9));
Steve Block44f0eee2011-05-26 01:26:41 +01003535 }
3536}
3537
3538
3539void MacroAssembler::LoadGlobalFunction(int index, Register function) {
3540 // Load the global or builtins object from the current context.
3541 lw(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3542 // Load the global context from the global or builtins object.
3543 lw(function, FieldMemOperand(function,
3544 GlobalObject::kGlobalContextOffset));
3545 // Load the function from the global context.
3546 lw(function, MemOperand(function, Context::SlotOffset(index)));
3547}
3548
3549
3550void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
3551 Register map,
3552 Register scratch) {
3553 // Load the initial map. The global functions all have initial maps.
3554 lw(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003555 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003556 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00003557 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
Steve Block44f0eee2011-05-26 01:26:41 +01003558 Branch(&ok);
3559 bind(&fail);
3560 Abort("Global functions must have initial map");
3561 bind(&ok);
3562 }
Andrei Popescu31002712010-02-23 13:46:05 +00003563}
3564
Steve Block6ded16b2010-05-10 14:33:55 +01003565
3566void MacroAssembler::EnterFrame(StackFrame::Type type) {
3567 addiu(sp, sp, -5 * kPointerSize);
Steve Block44f0eee2011-05-26 01:26:41 +01003568 li(t8, Operand(Smi::FromInt(type)));
3569 li(t9, Operand(CodeObject()));
Steve Block6ded16b2010-05-10 14:33:55 +01003570 sw(ra, MemOperand(sp, 4 * kPointerSize));
3571 sw(fp, MemOperand(sp, 3 * kPointerSize));
3572 sw(cp, MemOperand(sp, 2 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003573 sw(t8, MemOperand(sp, 1 * kPointerSize));
3574 sw(t9, MemOperand(sp, 0 * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01003575 addiu(fp, sp, 3 * kPointerSize);
3576}
3577
3578
3579void MacroAssembler::LeaveFrame(StackFrame::Type type) {
3580 mov(sp, fp);
3581 lw(fp, MemOperand(sp, 0 * kPointerSize));
3582 lw(ra, MemOperand(sp, 1 * kPointerSize));
3583 addiu(sp, sp, 2 * kPointerSize);
3584}
3585
3586
Ben Murdoch257744e2011-11-30 15:57:28 +00003587void MacroAssembler::EnterExitFrame(bool save_doubles,
3588 int stack_space) {
3589 // Setup the frame structure on the stack.
3590 STATIC_ASSERT(2 * kPointerSize == ExitFrameConstants::kCallerSPDisplacement);
3591 STATIC_ASSERT(1 * kPointerSize == ExitFrameConstants::kCallerPCOffset);
3592 STATIC_ASSERT(0 * kPointerSize == ExitFrameConstants::kCallerFPOffset);
Steve Block6ded16b2010-05-10 14:33:55 +01003593
Ben Murdoch257744e2011-11-30 15:57:28 +00003594 // This is how the stack will look:
3595 // fp + 2 (==kCallerSPDisplacement) - old stack's end
3596 // [fp + 1 (==kCallerPCOffset)] - saved old ra
3597 // [fp + 0 (==kCallerFPOffset)] - saved old fp
3598 // [fp - 1 (==kSPOffset)] - sp of the called function
3599 // [fp - 2 (==kCodeOffset)] - CodeObject
3600 // fp - (2 + stack_space + alignment) == sp == [fp - kSPOffset] - top of the
3601 // new stack (will contain saved ra)
Steve Block6ded16b2010-05-10 14:33:55 +01003602
3603 // Save registers.
Ben Murdoch257744e2011-11-30 15:57:28 +00003604 addiu(sp, sp, -4 * kPointerSize);
3605 sw(ra, MemOperand(sp, 3 * kPointerSize));
3606 sw(fp, MemOperand(sp, 2 * kPointerSize));
3607 addiu(fp, sp, 2 * kPointerSize); // Setup new frame pointer.
Steve Block6ded16b2010-05-10 14:33:55 +01003608
Ben Murdoch257744e2011-11-30 15:57:28 +00003609 if (emit_debug_code()) {
3610 sw(zero_reg, MemOperand(fp, ExitFrameConstants::kSPOffset));
3611 }
3612
3613 li(t8, Operand(CodeObject())); // Accessed from ExitFrame::code_slot.
3614 sw(t8, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01003615
3616 // Save the frame pointer and the context in top.
Steve Block44f0eee2011-05-26 01:26:41 +01003617 li(t8, Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate())));
3618 sw(fp, MemOperand(t8));
3619 li(t8, Operand(ExternalReference(Isolate::k_context_address, isolate())));
3620 sw(cp, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01003621
Ben Murdoch257744e2011-11-30 15:57:28 +00003622 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
Steve Block44f0eee2011-05-26 01:26:41 +01003623 if (save_doubles) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003624 // The stack must be allign to 0 modulo 8 for stores with sdc1.
Steve Block44f0eee2011-05-26 01:26:41 +01003625 ASSERT(kDoubleSize == frame_alignment);
Ben Murdoch257744e2011-11-30 15:57:28 +00003626 if (frame_alignment > 0) {
3627 ASSERT(IsPowerOf2(frame_alignment));
3628 And(sp, sp, Operand(-frame_alignment)); // Align stack.
3629 }
3630 int space = FPURegister::kNumRegisters * kDoubleSize;
Steve Block44f0eee2011-05-26 01:26:41 +01003631 Subu(sp, sp, Operand(space));
3632 // Remember: we only need to save every 2nd double FPU value.
3633 for (int i = 0; i < FPURegister::kNumRegisters; i+=2) {
3634 FPURegister reg = FPURegister::from_code(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00003635 sdc1(reg, MemOperand(sp, i * kDoubleSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003636 }
Steve Block44f0eee2011-05-26 01:26:41 +01003637 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003638
3639 // Reserve place for the return address, stack space and an optional slot
3640 // (used by the DirectCEntryStub to hold the return value if a struct is
3641 // returned) and align the frame preparing for calling the runtime function.
3642 ASSERT(stack_space >= 0);
3643 Subu(sp, sp, Operand((stack_space + 2) * kPointerSize));
3644 if (frame_alignment > 0) {
3645 ASSERT(IsPowerOf2(frame_alignment));
3646 And(sp, sp, Operand(-frame_alignment)); // Align stack.
3647 }
3648
3649 // Set the exit frame sp value to point just before the return address
3650 // location.
3651 addiu(at, sp, kPointerSize);
3652 sw(at, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01003653}
3654
3655
Ben Murdoch257744e2011-11-30 15:57:28 +00003656void MacroAssembler::LeaveExitFrame(bool save_doubles,
3657 Register argument_count) {
Steve Block44f0eee2011-05-26 01:26:41 +01003658 // Optionally restore all double registers.
3659 if (save_doubles) {
Steve Block44f0eee2011-05-26 01:26:41 +01003660 // Remember: we only need to restore every 2nd double FPU value.
Ben Murdoch257744e2011-11-30 15:57:28 +00003661 lw(t8, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01003662 for (int i = 0; i < FPURegister::kNumRegisters; i+=2) {
3663 FPURegister reg = FPURegister::from_code(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00003664 ldc1(reg, MemOperand(t8, i * kDoubleSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003665 }
3666 }
3667
Steve Block6ded16b2010-05-10 14:33:55 +01003668 // Clear top frame.
Steve Block44f0eee2011-05-26 01:26:41 +01003669 li(t8, Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate())));
3670 sw(zero_reg, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01003671
3672 // Restore current context from top and clear it in debug mode.
Steve Block44f0eee2011-05-26 01:26:41 +01003673 li(t8, Operand(ExternalReference(Isolate::k_context_address, isolate())));
3674 lw(cp, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01003675#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +01003676 sw(a3, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01003677#endif
3678
3679 // Pop the arguments, restore registers, and return.
3680 mov(sp, fp); // Respect ABI stack constraint.
Ben Murdoch257744e2011-11-30 15:57:28 +00003681 lw(fp, MemOperand(sp, ExitFrameConstants::kCallerFPOffset));
3682 lw(ra, MemOperand(sp, ExitFrameConstants::kCallerPCOffset));
3683 addiu(sp, sp, 8);
3684 if (argument_count.is_valid()) {
3685 sll(t8, argument_count, kPointerSizeLog2);
3686 addu(sp, sp, t8);
3687 }
Steve Block6ded16b2010-05-10 14:33:55 +01003688}
3689
3690
Steve Block44f0eee2011-05-26 01:26:41 +01003691void MacroAssembler::InitializeNewString(Register string,
3692 Register length,
3693 Heap::RootListIndex map_index,
3694 Register scratch1,
3695 Register scratch2) {
3696 sll(scratch1, length, kSmiTagSize);
3697 LoadRoot(scratch2, map_index);
3698 sw(scratch1, FieldMemOperand(string, String::kLengthOffset));
3699 li(scratch1, Operand(String::kEmptyHashField));
3700 sw(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
3701 sw(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
3702}
3703
3704
3705int MacroAssembler::ActivationFrameAlignment() {
3706#if defined(V8_HOST_ARCH_MIPS)
3707 // Running on the real platform. Use the alignment as mandated by the local
3708 // environment.
3709 // Note: This will break if we ever start generating snapshots on one Mips
3710 // platform for another Mips platform with a different alignment.
3711 return OS::ActivationFrameAlignment();
3712#else // defined(V8_HOST_ARCH_MIPS)
3713 // If we are using the simulator then we should always align to the expected
3714 // alignment. As the simulator is used to generate snapshots we do not know
3715 // if the target platform will need alignment, so this is controlled from a
3716 // flag.
3717 return FLAG_sim_stack_alignment;
3718#endif // defined(V8_HOST_ARCH_MIPS)
3719}
3720
Ben Murdoch257744e2011-11-30 15:57:28 +00003721void MacroAssembler::AssertStackIsAligned() {
3722 if (emit_debug_code()) {
3723 const int frame_alignment = ActivationFrameAlignment();
3724 const int frame_alignment_mask = frame_alignment - 1;
Steve Block44f0eee2011-05-26 01:26:41 +01003725
Ben Murdoch257744e2011-11-30 15:57:28 +00003726 if (frame_alignment > kPointerSize) {
3727 Label alignment_as_expected;
3728 ASSERT(IsPowerOf2(frame_alignment));
3729 andi(at, sp, frame_alignment_mask);
3730 Branch(&alignment_as_expected, eq, at, Operand(zero_reg));
3731 // Don't use Check here, as it will call Runtime_Abort re-entering here.
3732 stop("Unexpected stack alignment");
3733 bind(&alignment_as_expected);
3734 }
Steve Block6ded16b2010-05-10 14:33:55 +01003735 }
Steve Block6ded16b2010-05-10 14:33:55 +01003736}
3737
Steve Block44f0eee2011-05-26 01:26:41 +01003738
Steve Block44f0eee2011-05-26 01:26:41 +01003739void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
3740 Register reg,
3741 Register scratch,
3742 Label* not_power_of_two_or_zero) {
3743 Subu(scratch, reg, Operand(1));
3744 Branch(USE_DELAY_SLOT, not_power_of_two_or_zero, lt,
3745 scratch, Operand(zero_reg));
3746 and_(at, scratch, reg); // In the delay slot.
3747 Branch(not_power_of_two_or_zero, ne, at, Operand(zero_reg));
3748}
3749
3750
3751void MacroAssembler::JumpIfNotBothSmi(Register reg1,
3752 Register reg2,
3753 Label* on_not_both_smi) {
3754 STATIC_ASSERT(kSmiTag == 0);
3755 ASSERT_EQ(1, kSmiTagMask);
3756 or_(at, reg1, reg2);
3757 andi(at, at, kSmiTagMask);
3758 Branch(on_not_both_smi, ne, at, Operand(zero_reg));
3759}
3760
3761
3762void MacroAssembler::JumpIfEitherSmi(Register reg1,
3763 Register reg2,
3764 Label* on_either_smi) {
3765 STATIC_ASSERT(kSmiTag == 0);
3766 ASSERT_EQ(1, kSmiTagMask);
3767 // Both Smi tags must be 1 (not Smi).
3768 and_(at, reg1, reg2);
3769 andi(at, at, kSmiTagMask);
3770 Branch(on_either_smi, eq, at, Operand(zero_reg));
3771}
3772
3773
3774void MacroAssembler::AbortIfSmi(Register object) {
3775 STATIC_ASSERT(kSmiTag == 0);
3776 andi(at, object, kSmiTagMask);
3777 Assert(ne, "Operand is a smi", at, Operand(zero_reg));
3778}
3779
3780
3781void MacroAssembler::AbortIfNotSmi(Register object) {
3782 STATIC_ASSERT(kSmiTag == 0);
3783 andi(at, object, kSmiTagMask);
3784 Assert(eq, "Operand is a smi", at, Operand(zero_reg));
3785}
3786
3787
Ben Murdoch257744e2011-11-30 15:57:28 +00003788void MacroAssembler::AbortIfNotString(Register object) {
3789 STATIC_ASSERT(kSmiTag == 0);
3790 And(t0, object, Operand(kSmiTagMask));
3791 Assert(ne, "Operand is not a string", t0, Operand(zero_reg));
3792 push(object);
3793 lw(object, FieldMemOperand(object, HeapObject::kMapOffset));
3794 lbu(object, FieldMemOperand(object, Map::kInstanceTypeOffset));
3795 Assert(lo, "Operand is not a string", object, Operand(FIRST_NONSTRING_TYPE));
3796 pop(object);
3797}
3798
3799
Steve Block44f0eee2011-05-26 01:26:41 +01003800void MacroAssembler::AbortIfNotRootValue(Register src,
3801 Heap::RootListIndex root_value_index,
3802 const char* message) {
3803 ASSERT(!src.is(at));
3804 LoadRoot(at, root_value_index);
3805 Assert(eq, message, src, Operand(at));
3806}
3807
3808
3809void MacroAssembler::JumpIfNotHeapNumber(Register object,
3810 Register heap_number_map,
3811 Register scratch,
3812 Label* on_not_heap_number) {
3813 lw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
3814 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
3815 Branch(on_not_heap_number, ne, scratch, Operand(heap_number_map));
3816}
3817
3818
3819void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
3820 Register first,
3821 Register second,
3822 Register scratch1,
3823 Register scratch2,
3824 Label* failure) {
3825 // Test that both first and second are sequential ASCII strings.
3826 // Assume that they are non-smis.
3827 lw(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
3828 lw(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
3829 lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3830 lbu(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
3831
3832 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
3833 scratch2,
3834 scratch1,
3835 scratch2,
3836 failure);
3837}
3838
3839
3840void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
3841 Register second,
3842 Register scratch1,
3843 Register scratch2,
3844 Label* failure) {
3845 // Check that neither is a smi.
3846 STATIC_ASSERT(kSmiTag == 0);
3847 And(scratch1, first, Operand(second));
3848 And(scratch1, scratch1, Operand(kSmiTagMask));
3849 Branch(failure, eq, scratch1, Operand(zero_reg));
3850 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
3851 second,
3852 scratch1,
3853 scratch2,
3854 failure);
3855}
3856
3857
3858void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
3859 Register first,
3860 Register second,
3861 Register scratch1,
3862 Register scratch2,
3863 Label* failure) {
3864 int kFlatAsciiStringMask =
3865 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3866 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3867 ASSERT(kFlatAsciiStringTag <= 0xffff); // Ensure this fits 16-bit immed.
3868 andi(scratch1, first, kFlatAsciiStringMask);
3869 Branch(failure, ne, scratch1, Operand(kFlatAsciiStringTag));
3870 andi(scratch2, second, kFlatAsciiStringMask);
3871 Branch(failure, ne, scratch2, Operand(kFlatAsciiStringTag));
3872}
3873
3874
3875void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
3876 Register scratch,
3877 Label* failure) {
3878 int kFlatAsciiStringMask =
3879 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3880 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3881 And(scratch, type, Operand(kFlatAsciiStringMask));
3882 Branch(failure, ne, scratch, Operand(kFlatAsciiStringTag));
3883}
3884
3885
3886static const int kRegisterPassedArguments = 4;
3887
3888void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
3889 int frame_alignment = ActivationFrameAlignment();
3890
Steve Block44f0eee2011-05-26 01:26:41 +01003891 // Up to four simple arguments are passed in registers a0..a3.
3892 // Those four arguments must have reserved argument slots on the stack for
3893 // mips, even though those argument slots are not normally used.
3894 // Remaining arguments are pushed on the stack, above (higher address than)
3895 // the argument slots.
3896 ASSERT(StandardFrameConstants::kCArgsSlotsSize % kPointerSize == 0);
3897 int stack_passed_arguments = ((num_arguments <= kRegisterPassedArguments) ?
3898 0 : num_arguments - kRegisterPassedArguments) +
3899 (StandardFrameConstants::kCArgsSlotsSize /
3900 kPointerSize);
3901 if (frame_alignment > kPointerSize) {
3902 // Make stack end at alignment and make room for num_arguments - 4 words
3903 // and the original value of sp.
3904 mov(scratch, sp);
3905 Subu(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
3906 ASSERT(IsPowerOf2(frame_alignment));
3907 And(sp, sp, Operand(-frame_alignment));
3908 sw(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
3909 } else {
3910 Subu(sp, sp, Operand(stack_passed_arguments * kPointerSize));
3911 }
3912}
3913
3914
3915void MacroAssembler::CallCFunction(ExternalReference function,
3916 int num_arguments) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003917 CallCFunctionHelper(no_reg, function, t8, num_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01003918}
3919
3920
3921void MacroAssembler::CallCFunction(Register function,
3922 Register scratch,
3923 int num_arguments) {
3924 CallCFunctionHelper(function,
3925 ExternalReference::the_hole_value_location(isolate()),
3926 scratch,
3927 num_arguments);
3928}
3929
3930
3931void MacroAssembler::CallCFunctionHelper(Register function,
3932 ExternalReference function_reference,
3933 Register scratch,
3934 int num_arguments) {
Steve Block44f0eee2011-05-26 01:26:41 +01003935 // Make sure that the stack is aligned before calling a C function unless
3936 // running in the simulator. The simulator has its own alignment check which
3937 // provides more information.
3938 // The argument stots are presumed to have been set up by
3939 // PrepareCallCFunction. The C function must be called via t9, for mips ABI.
3940
3941#if defined(V8_HOST_ARCH_MIPS)
3942 if (emit_debug_code()) {
3943 int frame_alignment = OS::ActivationFrameAlignment();
3944 int frame_alignment_mask = frame_alignment - 1;
3945 if (frame_alignment > kPointerSize) {
3946 ASSERT(IsPowerOf2(frame_alignment));
3947 Label alignment_as_expected;
3948 And(at, sp, Operand(frame_alignment_mask));
3949 Branch(&alignment_as_expected, eq, at, Operand(zero_reg));
3950 // Don't use Check here, as it will call Runtime_Abort possibly
3951 // re-entering here.
3952 stop("Unexpected alignment in CallCFunction");
3953 bind(&alignment_as_expected);
3954 }
3955 }
3956#endif // V8_HOST_ARCH_MIPS
3957
3958 // Just call directly. The function called cannot cause a GC, or
3959 // allow preemption, so the return address in the link register
3960 // stays correct.
Steve Block44f0eee2011-05-26 01:26:41 +01003961
3962 if (function.is(no_reg)) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003963 function = t9;
3964 li(function, Operand(function_reference));
3965 } else if (!function.is(t9)) {
3966 mov(t9, function);
Steve Block44f0eee2011-05-26 01:26:41 +01003967 function = t9;
3968 }
3969
3970 Call(function);
3971
3972 ASSERT(StandardFrameConstants::kCArgsSlotsSize % kPointerSize == 0);
3973 int stack_passed_arguments = ((num_arguments <= kRegisterPassedArguments) ?
3974 0 : num_arguments - kRegisterPassedArguments) +
3975 (StandardFrameConstants::kCArgsSlotsSize /
3976 kPointerSize);
3977
3978 if (OS::ActivationFrameAlignment() > kPointerSize) {
3979 lw(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
3980 } else {
3981 Addu(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
3982 }
3983}
3984
3985
3986#undef BRANCH_ARGS_CHECK
3987
3988
Ben Murdoch257744e2011-11-30 15:57:28 +00003989void MacroAssembler::LoadInstanceDescriptors(Register map,
3990 Register descriptors) {
3991 lw(descriptors,
3992 FieldMemOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
3993 Label not_smi;
3994 JumpIfNotSmi(descriptors, &not_smi);
3995 li(descriptors, Operand(FACTORY->empty_descriptor_array()));
3996 bind(&not_smi);
3997}
3998
3999
Steve Block44f0eee2011-05-26 01:26:41 +01004000CodePatcher::CodePatcher(byte* address, int instructions)
4001 : address_(address),
4002 instructions_(instructions),
4003 size_(instructions * Assembler::kInstrSize),
Ben Murdoch257744e2011-11-30 15:57:28 +00004004 masm_(Isolate::Current(), address, size_ + Assembler::kGap) {
Steve Block44f0eee2011-05-26 01:26:41 +01004005 // Create a new macro assembler pointing to the address of the code to patch.
4006 // The size is adjusted with kGap on order for the assembler to generate size
4007 // bytes of instructions without failing with buffer size constraints.
4008 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
4009}
4010
4011
4012CodePatcher::~CodePatcher() {
4013 // Indicate that code has changed.
4014 CPU::FlushICache(address_, size_);
4015
4016 // Check that the code was patched as expected.
4017 ASSERT(masm_.pc_ == address_ + size_);
4018 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
4019}
4020
4021
Ben Murdoch257744e2011-11-30 15:57:28 +00004022void CodePatcher::Emit(Instr instr) {
4023 masm()->emit(instr);
Steve Block44f0eee2011-05-26 01:26:41 +01004024}
4025
4026
4027void CodePatcher::Emit(Address addr) {
4028 masm()->emit(reinterpret_cast<Instr>(addr));
4029}
4030
4031
Ben Murdoch257744e2011-11-30 15:57:28 +00004032void CodePatcher::ChangeBranchCondition(Condition cond) {
4033 Instr instr = Assembler::instr_at(masm_.pc_);
4034 ASSERT(Assembler::IsBranch(instr));
4035 uint32_t opcode = Assembler::GetOpcodeField(instr);
4036 // Currently only the 'eq' and 'ne' cond values are supported and the simple
4037 // branch instructions (with opcode being the branch type).
4038 // There are some special cases (see Assembler::IsBranch()) so extending this
4039 // would be tricky.
4040 ASSERT(opcode == BEQ ||
4041 opcode == BNE ||
4042 opcode == BLEZ ||
4043 opcode == BGTZ ||
4044 opcode == BEQL ||
4045 opcode == BNEL ||
4046 opcode == BLEZL ||
4047 opcode == BGTZL);
4048 opcode = (cond == eq) ? BEQ : BNE;
4049 instr = (instr & ~kOpcodeMask) | opcode;
4050 masm_.emit(instr);
4051}
Steve Block44f0eee2011-05-26 01:26:41 +01004052
4053
Andrei Popescu31002712010-02-23 13:46:05 +00004054} } // namespace v8::internal
4055
Leon Clarkef7060e22010-06-03 12:02:55 +01004056#endif // V8_TARGET_ARCH_MIPS