blob: 90c3499a9adcdc563d267e9edb8dc483282fef3c [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Andrei Popescu31002712010-02-23 13:46:05 +00004
Ben Murdoch257744e2011-11-30 15:57:28 +00005#include <limits.h> // For LONG_MIN, LONG_MAX.
Andrei Popescu31002712010-02-23 13:46:05 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/v8.h"
Andrei Popescu31002712010-02-23 13:46:05 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#if V8_TARGET_ARCH_MIPS
Leon Clarkef7060e22010-06-03 12:02:55 +010010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/base/bits.h"
12#include "src/base/division-by-constant.h"
13#include "src/bootstrapper.h"
14#include "src/codegen.h"
15#include "src/cpu-profiler.h"
16#include "src/debug.h"
17#include "src/isolate-inl.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018#include "src/runtime/runtime.h"
Andrei Popescu31002712010-02-23 13:46:05 +000019
20namespace v8 {
21namespace internal {
22
Ben Murdoch257744e2011-11-30 15:57:28 +000023MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
24 : Assembler(arg_isolate, buffer, size),
Andrei Popescu31002712010-02-23 13:46:05 +000025 generating_stub_(false),
Emily Bernierd0a1eb72015-03-24 16:35:39 -040026 has_frame_(false),
27 has_double_zero_reg_set_(false) {
Ben Murdoch257744e2011-11-30 15:57:28 +000028 if (isolate() != NULL) {
29 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
30 isolate());
31 }
Andrei Popescu31002712010-02-23 13:46:05 +000032}
33
34
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035void MacroAssembler::Load(Register dst,
36 const MemOperand& src,
37 Representation r) {
38 DCHECK(!r.IsDouble());
39 if (r.IsInteger8()) {
40 lb(dst, src);
41 } else if (r.IsUInteger8()) {
42 lbu(dst, src);
43 } else if (r.IsInteger16()) {
44 lh(dst, src);
45 } else if (r.IsUInteger16()) {
46 lhu(dst, src);
47 } else {
48 lw(dst, src);
49 }
50}
51
52
53void MacroAssembler::Store(Register src,
54 const MemOperand& dst,
55 Representation r) {
56 DCHECK(!r.IsDouble());
57 if (r.IsInteger8() || r.IsUInteger8()) {
58 sb(src, dst);
59 } else if (r.IsInteger16() || r.IsUInteger16()) {
60 sh(src, dst);
61 } else {
62 if (r.IsHeapObject()) {
63 AssertNotSmi(src);
64 } else if (r.IsSmi()) {
65 AssertSmi(src);
66 }
67 sw(src, dst);
68 }
69}
70
71
Andrei Popescu31002712010-02-23 13:46:05 +000072void MacroAssembler::LoadRoot(Register destination,
73 Heap::RootListIndex index) {
Steve Block6ded16b2010-05-10 14:33:55 +010074 lw(destination, MemOperand(s6, index << kPointerSizeLog2));
Andrei Popescu31002712010-02-23 13:46:05 +000075}
76
Steve Block44f0eee2011-05-26 01:26:41 +010077
Andrei Popescu31002712010-02-23 13:46:05 +000078void MacroAssembler::LoadRoot(Register destination,
79 Heap::RootListIndex index,
80 Condition cond,
81 Register src1, const Operand& src2) {
Steve Block44f0eee2011-05-26 01:26:41 +010082 Branch(2, NegateCondition(cond), src1, src2);
Steve Block6ded16b2010-05-10 14:33:55 +010083 lw(destination, MemOperand(s6, index << kPointerSizeLog2));
Andrei Popescu31002712010-02-23 13:46:05 +000084}
85
86
Steve Block44f0eee2011-05-26 01:26:41 +010087void MacroAssembler::StoreRoot(Register source,
88 Heap::RootListIndex index) {
89 sw(source, MemOperand(s6, index << kPointerSizeLog2));
90}
91
92
93void MacroAssembler::StoreRoot(Register source,
94 Heap::RootListIndex index,
95 Condition cond,
96 Register src1, const Operand& src2) {
97 Branch(2, NegateCondition(cond), src1, src2);
98 sw(source, MemOperand(s6, index << kPointerSizeLog2));
99}
100
101
Ben Murdoch257744e2011-11-30 15:57:28 +0000102// Push and pop all registers that can hold pointers.
103void MacroAssembler::PushSafepointRegisters() {
104 // Safepoints expect a block of kNumSafepointRegisters values on the
105 // stack, so adjust the stack for unsaved registers.
106 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 DCHECK(num_unsaved >= 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100108 if (num_unsaved > 0) {
109 Subu(sp, sp, Operand(num_unsaved * kPointerSize));
110 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000111 MultiPush(kSafepointSavedRegisters);
112}
113
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000114
Ben Murdoch257744e2011-11-30 15:57:28 +0000115void MacroAssembler::PopSafepointRegisters() {
116 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
117 MultiPop(kSafepointSavedRegisters);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100118 if (num_unsaved > 0) {
119 Addu(sp, sp, Operand(num_unsaved * kPointerSize));
120 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000121}
122
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123
Ben Murdoch257744e2011-11-30 15:57:28 +0000124void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
125 sw(src, SafepointRegisterSlot(dst));
126}
127
128
129void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
130 lw(dst, SafepointRegisterSlot(src));
131}
132
133
134int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
135 // The registers are pushed starting with the highest encoding,
136 // which means that lowest encodings are closest to the stack pointer.
137 return kSafepointRegisterStackIndexMap[reg_code];
138}
139
140
141MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
142 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
143}
144
145
146MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100147 UNIMPLEMENTED_MIPS();
Ben Murdoch257744e2011-11-30 15:57:28 +0000148 // General purpose registers are pushed last on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 int doubles_size = FPURegister::NumAllocatableRegisters() * kDoubleSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000150 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
151 return MemOperand(sp, doubles_size + register_offset);
152}
153
154
Steve Block44f0eee2011-05-26 01:26:41 +0100155void MacroAssembler::InNewSpace(Register object,
156 Register scratch,
157 Condition cc,
158 Label* branch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 DCHECK(cc == eq || cc == ne);
Steve Block44f0eee2011-05-26 01:26:41 +0100160 And(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
161 Branch(branch, cc, scratch,
162 Operand(ExternalReference::new_space_start(isolate())));
163}
164
165
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100166void MacroAssembler::RecordWriteField(
167 Register object,
168 int offset,
169 Register value,
170 Register dst,
171 RAStatus ra_status,
172 SaveFPRegsMode save_fp,
173 RememberedSetAction remembered_set_action,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 SmiCheck smi_check,
175 PointersToHereCheck pointers_to_here_check_for_value) {
176 DCHECK(!AreAliased(value, dst, t8, object));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100177 // First, check if a write barrier is even needed. The tests below
178 // catch stores of Smis.
Steve Block44f0eee2011-05-26 01:26:41 +0100179 Label done;
180
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100181 // Skip barrier if writing a smi.
182 if (smi_check == INLINE_SMI_CHECK) {
183 JumpIfSmi(value, &done);
184 }
Steve Block44f0eee2011-05-26 01:26:41 +0100185
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100186 // Although the object register is tagged, the offset is relative to the start
187 // of the object, so so offset must be a multiple of kPointerSize.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 DCHECK(IsAligned(offset, kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100189
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100190 Addu(dst, object, Operand(offset - kHeapObjectTag));
191 if (emit_debug_code()) {
192 Label ok;
193 And(t8, dst, Operand((1 << kPointerSizeLog2) - 1));
194 Branch(&ok, eq, t8, Operand(zero_reg));
195 stop("Unaligned cell in write barrier");
196 bind(&ok);
197 }
198
199 RecordWrite(object,
200 dst,
201 value,
202 ra_status,
203 save_fp,
204 remembered_set_action,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 OMIT_SMI_CHECK,
206 pointers_to_here_check_for_value);
Steve Block44f0eee2011-05-26 01:26:41 +0100207
208 bind(&done);
209
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100210 // Clobber clobbered input registers when running with the debug-code flag
Steve Block44f0eee2011-05-26 01:26:41 +0100211 // turned on to provoke errors.
Ben Murdoch257744e2011-11-30 15:57:28 +0000212 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 li(value, Operand(bit_cast<int32_t>(kZapValue + 4)));
214 li(dst, Operand(bit_cast<int32_t>(kZapValue + 8)));
215 }
216}
217
218
219// Will clobber 4 registers: object, map, dst, ip. The
220// register 'object' contains a heap object pointer.
221void MacroAssembler::RecordWriteForMap(Register object,
222 Register map,
223 Register dst,
224 RAStatus ra_status,
225 SaveFPRegsMode fp_mode) {
226 if (emit_debug_code()) {
227 DCHECK(!dst.is(at));
228 lw(dst, FieldMemOperand(map, HeapObject::kMapOffset));
229 Check(eq,
230 kWrongAddressOrValuePassedToRecordWrite,
231 dst,
232 Operand(isolate()->factory()->meta_map()));
233 }
234
235 if (!FLAG_incremental_marking) {
236 return;
237 }
238
239 if (emit_debug_code()) {
240 lw(at, FieldMemOperand(object, HeapObject::kMapOffset));
241 Check(eq,
242 kWrongAddressOrValuePassedToRecordWrite,
243 map,
244 Operand(at));
245 }
246
247 Label done;
248
249 // A single check of the map's pages interesting flag suffices, since it is
250 // only set during incremental collection, and then it's also guaranteed that
251 // the from object's page's interesting flag is also set. This optimization
252 // relies on the fact that maps can never be in new space.
253 CheckPageFlag(map,
254 map, // Used as scratch.
255 MemoryChunk::kPointersToHereAreInterestingMask,
256 eq,
257 &done);
258
259 Addu(dst, object, Operand(HeapObject::kMapOffset - kHeapObjectTag));
260 if (emit_debug_code()) {
261 Label ok;
262 And(at, dst, Operand((1 << kPointerSizeLog2) - 1));
263 Branch(&ok, eq, at, Operand(zero_reg));
264 stop("Unaligned cell in write barrier");
265 bind(&ok);
266 }
267
268 // Record the actual write.
269 if (ra_status == kRAHasNotBeenSaved) {
270 push(ra);
271 }
272 RecordWriteStub stub(isolate(), object, map, dst, OMIT_REMEMBERED_SET,
273 fp_mode);
274 CallStub(&stub);
275 if (ra_status == kRAHasNotBeenSaved) {
276 pop(ra);
277 }
278
279 bind(&done);
280
281 // Count number of write barriers in generated code.
282 isolate()->counters()->write_barriers_static()->Increment();
283 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, at, dst);
284
285 // Clobber clobbered registers when running with the debug-code flag
286 // turned on to provoke errors.
287 if (emit_debug_code()) {
288 li(dst, Operand(bit_cast<int32_t>(kZapValue + 12)));
289 li(map, Operand(bit_cast<int32_t>(kZapValue + 16)));
Steve Block44f0eee2011-05-26 01:26:41 +0100290 }
291}
292
293
294// Will clobber 4 registers: object, address, scratch, ip. The
295// register 'object' contains a heap object pointer. The heap object
296// tag is shifted away.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297void MacroAssembler::RecordWrite(
298 Register object,
299 Register address,
300 Register value,
301 RAStatus ra_status,
302 SaveFPRegsMode fp_mode,
303 RememberedSetAction remembered_set_action,
304 SmiCheck smi_check,
305 PointersToHereCheck pointers_to_here_check_for_value) {
306 DCHECK(!AreAliased(object, address, value, t8));
307 DCHECK(!AreAliased(object, address, value, t9));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100308
309 if (emit_debug_code()) {
310 lw(at, MemOperand(address));
311 Assert(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 eq, kWrongAddressOrValuePassedToRecordWrite, at, Operand(value));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100313 }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000314
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 if (remembered_set_action == OMIT_REMEMBERED_SET &&
316 !FLAG_incremental_marking) {
317 return;
318 }
319
320 // First, check if a write barrier is even needed. The tests below
321 // catch stores of smis and stores into the young generation.
Steve Block44f0eee2011-05-26 01:26:41 +0100322 Label done;
323
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100324 if (smi_check == INLINE_SMI_CHECK) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 DCHECK_EQ(0, kSmiTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100326 JumpIfSmi(value, &done);
327 }
328
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
330 CheckPageFlag(value,
331 value, // Used as scratch.
332 MemoryChunk::kPointersToHereAreInterestingMask,
333 eq,
334 &done);
335 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100336 CheckPageFlag(object,
337 value, // Used as scratch.
338 MemoryChunk::kPointersFromHereAreInterestingMask,
339 eq,
340 &done);
Steve Block44f0eee2011-05-26 01:26:41 +0100341
342 // Record the actual write.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100343 if (ra_status == kRAHasNotBeenSaved) {
344 push(ra);
345 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
347 fp_mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100348 CallStub(&stub);
349 if (ra_status == kRAHasNotBeenSaved) {
350 pop(ra);
351 }
Steve Block44f0eee2011-05-26 01:26:41 +0100352
353 bind(&done);
354
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355 // Count number of write barriers in generated code.
356 isolate()->counters()->write_barriers_static()->Increment();
357 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, at,
358 value);
359
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100360 // Clobber clobbered registers when running with the debug-code flag
Steve Block44f0eee2011-05-26 01:26:41 +0100361 // turned on to provoke errors.
Ben Murdoch257744e2011-11-30 15:57:28 +0000362 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 li(address, Operand(bit_cast<int32_t>(kZapValue + 12)));
364 li(value, Operand(bit_cast<int32_t>(kZapValue + 16)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100365 }
366}
367
368
369void MacroAssembler::RememberedSetHelper(Register object, // For debug tests.
370 Register address,
371 Register scratch,
372 SaveFPRegsMode fp_mode,
373 RememberedSetFinalAction and_then) {
374 Label done;
375 if (emit_debug_code()) {
376 Label ok;
377 JumpIfNotInNewSpace(object, scratch, &ok);
378 stop("Remembered set pointer is in new space");
379 bind(&ok);
380 }
381 // Load store buffer top.
382 ExternalReference store_buffer =
383 ExternalReference::store_buffer_top(isolate());
384 li(t8, Operand(store_buffer));
385 lw(scratch, MemOperand(t8));
386 // Store pointer to buffer and increment buffer top.
387 sw(address, MemOperand(scratch));
388 Addu(scratch, scratch, kPointerSize);
389 // Write back new top of buffer.
390 sw(scratch, MemOperand(t8));
391 // Call stub on end of buffer.
392 // Check for end of buffer.
393 And(t8, scratch, Operand(StoreBuffer::kStoreBufferOverflowBit));
394 if (and_then == kFallThroughAtEnd) {
395 Branch(&done, eq, t8, Operand(zero_reg));
396 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397 DCHECK(and_then == kReturnAtEnd);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100398 Ret(eq, t8, Operand(zero_reg));
399 }
400 push(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 StoreBufferOverflowStub store_buffer_overflow(isolate(), fp_mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100402 CallStub(&store_buffer_overflow);
403 pop(ra);
404 bind(&done);
405 if (and_then == kReturnAtEnd) {
406 Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100407 }
408}
409
410
411// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000412// Allocation support.
Steve Block44f0eee2011-05-26 01:26:41 +0100413
414
415void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
416 Register scratch,
417 Label* miss) {
418 Label same_contexts;
419
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 DCHECK(!holder_reg.is(scratch));
421 DCHECK(!holder_reg.is(at));
422 DCHECK(!scratch.is(at));
Steve Block44f0eee2011-05-26 01:26:41 +0100423
424 // Load current lexical context from the stack frame.
425 lw(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
426 // In debug mode, make sure the lexical context is set.
427#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 Check(ne, kWeShouldNotHaveAnEmptyLexicalContext,
Steve Block44f0eee2011-05-26 01:26:41 +0100429 scratch, Operand(zero_reg));
430#endif
431
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 // Load the native context of the current context.
433 int offset =
434 Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +0100435 lw(scratch, FieldMemOperand(scratch, offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000436 lw(scratch, FieldMemOperand(scratch, GlobalObject::kNativeContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100437
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 // Check the context is a native context.
Ben Murdoch257744e2011-11-30 15:57:28 +0000439 if (emit_debug_code()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000440 push(holder_reg); // Temporarily save holder on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 // Read the first word and compare to the native_context_map.
Steve Block44f0eee2011-05-26 01:26:41 +0100442 lw(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 LoadRoot(at, Heap::kNativeContextMapRootIndex);
444 Check(eq, kJSGlobalObjectNativeContextShouldBeANativeContext,
Steve Block44f0eee2011-05-26 01:26:41 +0100445 holder_reg, Operand(at));
Ben Murdoch257744e2011-11-30 15:57:28 +0000446 pop(holder_reg); // Restore holder.
Steve Block44f0eee2011-05-26 01:26:41 +0100447 }
448
449 // Check if both contexts are the same.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 lw(at, FieldMemOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100451 Branch(&same_contexts, eq, scratch, Operand(at));
452
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453 // Check the context is a native context.
Ben Murdoch257744e2011-11-30 15:57:28 +0000454 if (emit_debug_code()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000455 push(holder_reg); // Temporarily save holder on the stack.
Steve Block44f0eee2011-05-26 01:26:41 +0100456 mov(holder_reg, at); // Move at to its holding place.
457 LoadRoot(at, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 Check(ne, kJSGlobalProxyContextShouldNotBeNull,
Steve Block44f0eee2011-05-26 01:26:41 +0100459 holder_reg, Operand(at));
460
461 lw(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 LoadRoot(at, Heap::kNativeContextMapRootIndex);
463 Check(eq, kJSGlobalObjectNativeContextShouldBeANativeContext,
Steve Block44f0eee2011-05-26 01:26:41 +0100464 holder_reg, Operand(at));
465 // Restore at is not needed. at is reloaded below.
Ben Murdoch257744e2011-11-30 15:57:28 +0000466 pop(holder_reg); // Restore holder.
Steve Block44f0eee2011-05-26 01:26:41 +0100467 // Restore at to holder's context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 lw(at, FieldMemOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100469 }
470
471 // Check that the security token in the calling global object is
472 // compatible with the security token in the receiving global
473 // object.
474 int token_offset = Context::kHeaderSize +
475 Context::SECURITY_TOKEN_INDEX * kPointerSize;
476
477 lw(scratch, FieldMemOperand(scratch, token_offset));
478 lw(at, FieldMemOperand(at, token_offset));
479 Branch(miss, ne, scratch, Operand(at));
480
481 bind(&same_contexts);
Andrei Popescu31002712010-02-23 13:46:05 +0000482}
483
484
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000485// Compute the hash code from the untagged key. This must be kept in sync with
486// ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
487// code-stub-hydrogen.cc
Ben Murdochc7cc0282012-03-05 14:35:55 +0000488void MacroAssembler::GetNumberHash(Register reg0, Register scratch) {
489 // First of all we assign the hash seed to scratch.
490 LoadRoot(scratch, Heap::kHashSeedRootIndex);
491 SmiUntag(scratch);
492
493 // Xor original key with a seed.
494 xor_(reg0, reg0, scratch);
495
496 // Compute the hash code from the untagged key. This must be kept in sync
497 // with ComputeIntegerHash in utils.h.
498 //
499 // hash = ~hash + (hash << 15);
500 nor(scratch, reg0, zero_reg);
501 sll(at, reg0, 15);
502 addu(reg0, scratch, at);
503
504 // hash = hash ^ (hash >> 12);
505 srl(at, reg0, 12);
506 xor_(reg0, reg0, at);
507
508 // hash = hash + (hash << 2);
509 sll(at, reg0, 2);
510 addu(reg0, reg0, at);
511
512 // hash = hash ^ (hash >> 4);
513 srl(at, reg0, 4);
514 xor_(reg0, reg0, at);
515
516 // hash = hash * 2057;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100517 sll(scratch, reg0, 11);
518 sll(at, reg0, 3);
519 addu(reg0, reg0, at);
520 addu(reg0, reg0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000521
522 // hash = hash ^ (hash >> 16);
523 srl(at, reg0, 16);
524 xor_(reg0, reg0, at);
525}
526
527
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000528void MacroAssembler::LoadFromNumberDictionary(Label* miss,
529 Register elements,
530 Register key,
531 Register result,
532 Register reg0,
533 Register reg1,
534 Register reg2) {
535 // Register use:
536 //
537 // elements - holds the slow-case elements of the receiver on entry.
538 // Unchanged unless 'result' is the same register.
539 //
540 // key - holds the smi key on entry.
541 // Unchanged unless 'result' is the same register.
542 //
543 //
544 // result - holds the result on exit if the load succeeded.
545 // Allowed to be the same as 'key' or 'result'.
546 // Unchanged on bailout so 'key' or 'result' can be used
547 // in further computation.
548 //
549 // Scratch registers:
550 //
551 // reg0 - holds the untagged key on entry and holds the hash once computed.
552 //
553 // reg1 - Used to hold the capacity mask of the dictionary.
554 //
555 // reg2 - Used for the index into the dictionary.
556 // at - Temporary (avoid MacroAssembler instructions also using 'at').
557 Label done;
558
Ben Murdochc7cc0282012-03-05 14:35:55 +0000559 GetNumberHash(reg0, reg1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000560
561 // Compute the capacity mask.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000562 lw(reg1, FieldMemOperand(elements, SeededNumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000563 sra(reg1, reg1, kSmiTagSize);
564 Subu(reg1, reg1, Operand(1));
565
566 // Generate an unrolled loop that performs a few probes before giving up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000567 for (int i = 0; i < kNumberDictionaryProbes; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000568 // Use reg2 for index calculations and keep the hash intact in reg0.
569 mov(reg2, reg0);
570 // Compute the masked index: (hash + i + i * i) & mask.
571 if (i > 0) {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000572 Addu(reg2, reg2, Operand(SeededNumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000573 }
574 and_(reg2, reg2, reg1);
575
576 // Scale the index by multiplying by the element size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 DCHECK(SeededNumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000578 sll(at, reg2, 1); // 2x.
579 addu(reg2, reg2, at); // reg2 = reg2 * 3.
580
581 // Check if the key is identical to the name.
582 sll(at, reg2, kPointerSizeLog2);
583 addu(reg2, elements, at);
584
Ben Murdochc7cc0282012-03-05 14:35:55 +0000585 lw(at, FieldMemOperand(reg2, SeededNumberDictionary::kElementsStartOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 if (i != kNumberDictionaryProbes - 1) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000587 Branch(&done, eq, key, Operand(at));
588 } else {
589 Branch(miss, ne, key, Operand(at));
590 }
591 }
592
593 bind(&done);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400594 // Check that the value is a field property.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000595 // reg2: elements + (index * kPointerSize).
596 const int kDetailsOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +0000597 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000598 lw(reg1, FieldMemOperand(reg2, kDetailsOffset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400599 DCHECK_EQ(FIELD, 0);
Ben Murdoch589d6972011-11-30 16:04:58 +0000600 And(at, reg1, Operand(Smi::FromInt(PropertyDetails::TypeField::kMask)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000601 Branch(miss, ne, at, Operand(zero_reg));
602
603 // Get the value at the masked, scaled index and return.
604 const int kValueOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +0000605 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000606 lw(result, FieldMemOperand(reg2, kValueOffset));
607}
608
609
Andrei Popescu31002712010-02-23 13:46:05 +0000610// ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000611// Instruction macros.
Andrei Popescu31002712010-02-23 13:46:05 +0000612
Andrei Popescu31002712010-02-23 13:46:05 +0000613void MacroAssembler::Addu(Register rd, Register rs, const Operand& rt) {
614 if (rt.is_reg()) {
615 addu(rd, rs, rt.rm());
616 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100617 if (is_int16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000618 addiu(rd, rs, rt.imm32_);
619 } else {
620 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000621 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000622 li(at, rt);
623 addu(rd, rs, at);
624 }
625 }
626}
627
628
Steve Block44f0eee2011-05-26 01:26:41 +0100629void MacroAssembler::Subu(Register rd, Register rs, const Operand& rt) {
630 if (rt.is_reg()) {
631 subu(rd, rs, rt.rm());
632 } else {
633 if (is_int16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
634 addiu(rd, rs, -rt.imm32_); // No subiu instr, use addiu(x, y, -imm).
635 } else {
636 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 DCHECK(!rs.is(at));
Steve Block44f0eee2011-05-26 01:26:41 +0100638 li(at, rt);
639 subu(rd, rs, at);
640 }
641 }
642}
643
644
Andrei Popescu31002712010-02-23 13:46:05 +0000645void MacroAssembler::Mul(Register rd, Register rs, const Operand& rt) {
646 if (rt.is_reg()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 if (IsMipsArchVariant(kLoongson)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100648 mult(rs, rt.rm());
649 mflo(rd);
650 } else {
651 mul(rd, rs, rt.rm());
652 }
Andrei Popescu31002712010-02-23 13:46:05 +0000653 } else {
654 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000655 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000656 li(at, rt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000657 if (IsMipsArchVariant(kLoongson)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100658 mult(rs, at);
659 mflo(rd);
660 } else {
661 mul(rd, rs, at);
662 }
Andrei Popescu31002712010-02-23 13:46:05 +0000663 }
664}
665
666
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667void MacroAssembler::Mul(Register rd_hi, Register rd_lo,
668 Register rs, const Operand& rt) {
669 if (rt.is_reg()) {
670 if (!IsMipsArchVariant(kMips32r6)) {
671 mult(rs, rt.rm());
672 mflo(rd_lo);
673 mfhi(rd_hi);
674 } else {
675 if (rd_lo.is(rs)) {
676 DCHECK(!rd_hi.is(rs));
677 DCHECK(!rd_hi.is(rt.rm()) && !rd_lo.is(rt.rm()));
678 muh(rd_hi, rs, rt.rm());
679 mul(rd_lo, rs, rt.rm());
680 } else {
681 DCHECK(!rd_hi.is(rt.rm()) && !rd_lo.is(rt.rm()));
682 mul(rd_lo, rs, rt.rm());
683 muh(rd_hi, rs, rt.rm());
684 }
685 }
686 } else {
687 // li handles the relocation.
688 DCHECK(!rs.is(at));
689 li(at, rt);
690 if (!IsMipsArchVariant(kMips32r6)) {
691 mult(rs, at);
692 mflo(rd_lo);
693 mfhi(rd_hi);
694 } else {
695 if (rd_lo.is(rs)) {
696 DCHECK(!rd_hi.is(rs));
697 DCHECK(!rd_hi.is(at) && !rd_lo.is(at));
698 muh(rd_hi, rs, at);
699 mul(rd_lo, rs, at);
700 } else {
701 DCHECK(!rd_hi.is(at) && !rd_lo.is(at));
702 mul(rd_lo, rs, at);
703 muh(rd_hi, rs, at);
704 }
705 }
706 }
707}
708
709
710void MacroAssembler::Mulh(Register rd, Register rs, const Operand& rt) {
711 if (rt.is_reg()) {
712 if (!IsMipsArchVariant(kMips32r6)) {
713 mult(rs, rt.rm());
714 mfhi(rd);
715 } else {
716 muh(rd, rs, rt.rm());
717 }
718 } else {
719 // li handles the relocation.
720 DCHECK(!rs.is(at));
721 li(at, rt);
722 if (!IsMipsArchVariant(kMips32r6)) {
723 mult(rs, at);
724 mfhi(rd);
725 } else {
726 muh(rd, rs, at);
727 }
728 }
729}
730
731
Andrei Popescu31002712010-02-23 13:46:05 +0000732void MacroAssembler::Mult(Register rs, const Operand& rt) {
733 if (rt.is_reg()) {
734 mult(rs, rt.rm());
735 } else {
736 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000738 li(at, rt);
739 mult(rs, at);
740 }
741}
742
743
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400744void MacroAssembler::Mulhu(Register rd, Register rs, const Operand& rt) {
745 if (rt.is_reg()) {
746 if (!IsMipsArchVariant(kMips32r6)) {
747 multu(rs, rt.rm());
748 mfhi(rd);
749 } else {
750 muhu(rd, rs, rt.rm());
751 }
752 } else {
753 // li handles the relocation.
754 DCHECK(!rs.is(at));
755 li(at, rt);
756 if (!IsMipsArchVariant(kMips32r6)) {
757 multu(rs, at);
758 mfhi(rd);
759 } else {
760 muhu(rd, rs, at);
761 }
762 }
763}
764
765
Andrei Popescu31002712010-02-23 13:46:05 +0000766void MacroAssembler::Multu(Register rs, const Operand& rt) {
767 if (rt.is_reg()) {
768 multu(rs, rt.rm());
769 } else {
770 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000771 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000772 li(at, rt);
773 multu(rs, at);
774 }
775}
776
777
778void MacroAssembler::Div(Register rs, const Operand& rt) {
779 if (rt.is_reg()) {
780 div(rs, rt.rm());
781 } else {
782 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000783 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000784 li(at, rt);
785 div(rs, at);
786 }
787}
788
789
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790void MacroAssembler::Div(Register rem, Register res,
791 Register rs, const Operand& rt) {
792 if (rt.is_reg()) {
793 if (!IsMipsArchVariant(kMips32r6)) {
794 div(rs, rt.rm());
795 mflo(res);
796 mfhi(rem);
797 } else {
798 div(res, rs, rt.rm());
799 mod(rem, rs, rt.rm());
800 }
801 } else {
802 // li handles the relocation.
803 DCHECK(!rs.is(at));
804 li(at, rt);
805 if (!IsMipsArchVariant(kMips32r6)) {
806 div(rs, at);
807 mflo(res);
808 mfhi(rem);
809 } else {
810 div(res, rs, at);
811 mod(rem, rs, at);
812 }
813 }
814}
815
816
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400817void MacroAssembler::Div(Register res, Register rs, const Operand& rt) {
818 if (rt.is_reg()) {
819 if (!IsMipsArchVariant(kMips32r6)) {
820 div(rs, rt.rm());
821 mflo(res);
822 } else {
823 div(res, rs, rt.rm());
824 }
825 } else {
826 // li handles the relocation.
827 DCHECK(!rs.is(at));
828 li(at, rt);
829 if (!IsMipsArchVariant(kMips32r6)) {
830 div(rs, at);
831 mflo(res);
832 } else {
833 div(res, rs, at);
834 }
835 }
836}
837
838
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839void MacroAssembler::Mod(Register rd, Register rs, const Operand& rt) {
840 if (rt.is_reg()) {
841 if (!IsMipsArchVariant(kMips32r6)) {
842 div(rs, rt.rm());
843 mfhi(rd);
844 } else {
845 mod(rd, rs, rt.rm());
846 }
847 } else {
848 // li handles the relocation.
849 DCHECK(!rs.is(at));
850 li(at, rt);
851 if (!IsMipsArchVariant(kMips32r6)) {
852 div(rs, at);
853 mfhi(rd);
854 } else {
855 mod(rd, rs, at);
856 }
857 }
858}
859
860
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400861void MacroAssembler::Modu(Register rd, Register rs, const Operand& rt) {
862 if (rt.is_reg()) {
863 if (!IsMipsArchVariant(kMips32r6)) {
864 divu(rs, rt.rm());
865 mfhi(rd);
866 } else {
867 modu(rd, rs, rt.rm());
868 }
869 } else {
870 // li handles the relocation.
871 DCHECK(!rs.is(at));
872 li(at, rt);
873 if (!IsMipsArchVariant(kMips32r6)) {
874 divu(rs, at);
875 mfhi(rd);
876 } else {
877 modu(rd, rs, at);
878 }
879 }
880}
881
882
Andrei Popescu31002712010-02-23 13:46:05 +0000883void MacroAssembler::Divu(Register rs, const Operand& rt) {
884 if (rt.is_reg()) {
885 divu(rs, rt.rm());
886 } else {
887 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000889 li(at, rt);
890 divu(rs, at);
891 }
892}
893
894
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400895void MacroAssembler::Divu(Register res, Register rs, const Operand& rt) {
896 if (rt.is_reg()) {
897 if (!IsMipsArchVariant(kMips32r6)) {
898 divu(rs, rt.rm());
899 mflo(res);
900 } else {
901 divu(res, rs, rt.rm());
902 }
903 } else {
904 // li handles the relocation.
905 DCHECK(!rs.is(at));
906 li(at, rt);
907 if (!IsMipsArchVariant(kMips32r6)) {
908 divu(rs, at);
909 mflo(res);
910 } else {
911 divu(res, rs, at);
912 }
913 }
914}
915
916
Andrei Popescu31002712010-02-23 13:46:05 +0000917void MacroAssembler::And(Register rd, Register rs, const Operand& rt) {
918 if (rt.is_reg()) {
919 and_(rd, rs, rt.rm());
920 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100921 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000922 andi(rd, rs, rt.imm32_);
923 } else {
924 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000925 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000926 li(at, rt);
927 and_(rd, rs, at);
928 }
929 }
930}
931
932
933void MacroAssembler::Or(Register rd, Register rs, const Operand& rt) {
934 if (rt.is_reg()) {
935 or_(rd, rs, rt.rm());
936 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100937 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000938 ori(rd, rs, rt.imm32_);
939 } else {
940 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000942 li(at, rt);
943 or_(rd, rs, at);
944 }
945 }
946}
947
948
949void MacroAssembler::Xor(Register rd, Register rs, const Operand& rt) {
950 if (rt.is_reg()) {
951 xor_(rd, rs, rt.rm());
952 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100953 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000954 xori(rd, rs, rt.imm32_);
955 } else {
956 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000957 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000958 li(at, rt);
959 xor_(rd, rs, at);
960 }
961 }
962}
963
964
965void MacroAssembler::Nor(Register rd, Register rs, const Operand& rt) {
966 if (rt.is_reg()) {
967 nor(rd, rs, rt.rm());
968 } else {
969 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000971 li(at, rt);
972 nor(rd, rs, at);
973 }
974}
975
976
Ben Murdoch257744e2011-11-30 15:57:28 +0000977void MacroAssembler::Neg(Register rs, const Operand& rt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 DCHECK(rt.is_reg());
979 DCHECK(!at.is(rs));
980 DCHECK(!at.is(rt.rm()));
Ben Murdoch257744e2011-11-30 15:57:28 +0000981 li(at, -1);
982 xor_(rs, rt.rm(), at);
983}
984
985
Andrei Popescu31002712010-02-23 13:46:05 +0000986void MacroAssembler::Slt(Register rd, Register rs, const Operand& rt) {
987 if (rt.is_reg()) {
988 slt(rd, rs, rt.rm());
989 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100990 if (is_int16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000991 slti(rd, rs, rt.imm32_);
992 } else {
993 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000994 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +0000995 li(at, rt);
996 slt(rd, rs, at);
997 }
998 }
999}
1000
1001
1002void MacroAssembler::Sltu(Register rd, Register rs, const Operand& rt) {
1003 if (rt.is_reg()) {
1004 sltu(rd, rs, rt.rm());
1005 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001006 if (is_uint16(rt.imm32_) && !MustUseReg(rt.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001007 sltiu(rd, rs, rt.imm32_);
1008 } else {
1009 // li handles the relocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 DCHECK(!rs.is(at));
Andrei Popescu31002712010-02-23 13:46:05 +00001011 li(at, rt);
1012 sltu(rd, rs, at);
1013 }
1014 }
1015}
1016
1017
Steve Block44f0eee2011-05-26 01:26:41 +01001018void MacroAssembler::Ror(Register rd, Register rs, const Operand& rt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001019 if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
Steve Block44f0eee2011-05-26 01:26:41 +01001020 if (rt.is_reg()) {
1021 rotrv(rd, rs, rt.rm());
1022 } else {
1023 rotr(rd, rs, rt.imm32_);
1024 }
1025 } else {
1026 if (rt.is_reg()) {
1027 subu(at, zero_reg, rt.rm());
1028 sllv(at, rs, at);
1029 srlv(rd, rs, rt.rm());
1030 or_(rd, rd, at);
1031 } else {
1032 if (rt.imm32_ == 0) {
1033 srl(rd, rs, 0);
1034 } else {
1035 srl(at, rs, rt.imm32_);
1036 sll(rd, rs, (0x20 - rt.imm32_) & 0x1f);
1037 or_(rd, rd, at);
1038 }
1039 }
1040 }
Andrei Popescu31002712010-02-23 13:46:05 +00001041}
1042
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043
1044void MacroAssembler::Pref(int32_t hint, const MemOperand& rs) {
1045 if (IsMipsArchVariant(kLoongson)) {
1046 lw(zero_reg, rs);
1047 } else {
1048 pref(hint, rs);
1049 }
1050}
1051
1052
1053// ------------Pseudo-instructions-------------
1054
1055void MacroAssembler::Ulw(Register rd, const MemOperand& rs) {
1056 lwr(rd, rs);
1057 lwl(rd, MemOperand(rs.rm(), rs.offset() + 3));
1058}
1059
1060
1061void MacroAssembler::Usw(Register rd, const MemOperand& rs) {
1062 swr(rd, rs);
1063 swl(rd, MemOperand(rs.rm(), rs.offset() + 3));
1064}
1065
1066
1067void MacroAssembler::li(Register dst, Handle<Object> value, LiFlags mode) {
1068 AllowDeferredHandleDereference smi_check;
1069 if (value->IsSmi()) {
1070 li(dst, Operand(value), mode);
1071 } else {
1072 DCHECK(value->IsHeapObject());
1073 if (isolate()->heap()->InNewSpace(*value)) {
1074 Handle<Cell> cell = isolate()->factory()->NewCell(value);
1075 li(dst, Operand(cell));
1076 lw(dst, FieldMemOperand(dst, Cell::kValueOffset));
1077 } else {
1078 li(dst, Operand(value));
1079 }
1080 }
1081}
1082
Steve Block44f0eee2011-05-26 01:26:41 +01001083
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001084void MacroAssembler::li(Register rd, Operand j, LiFlags mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001085 DCHECK(!j.is_reg());
Steve Block44f0eee2011-05-26 01:26:41 +01001086 BlockTrampolinePoolScope block_trampoline_pool(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001087 if (!MustUseReg(j.rmode_) && mode == OPTIMIZE_SIZE) {
Andrei Popescu31002712010-02-23 13:46:05 +00001088 // Normal load of an immediate value which does not need Relocation Info.
1089 if (is_int16(j.imm32_)) {
1090 addiu(rd, zero_reg, j.imm32_);
Steve Block44f0eee2011-05-26 01:26:41 +01001091 } else if (!(j.imm32_ & kHiMask)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001092 ori(rd, zero_reg, j.imm32_);
Steve Block44f0eee2011-05-26 01:26:41 +01001093 } else if (!(j.imm32_ & kImm16Mask)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001094 lui(rd, (j.imm32_ >> kLuiShift) & kImm16Mask);
Andrei Popescu31002712010-02-23 13:46:05 +00001095 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001096 lui(rd, (j.imm32_ >> kLuiShift) & kImm16Mask);
Steve Block44f0eee2011-05-26 01:26:41 +01001097 ori(rd, rd, (j.imm32_ & kImm16Mask));
Andrei Popescu31002712010-02-23 13:46:05 +00001098 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001099 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001100 if (MustUseReg(j.rmode_)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001101 RecordRelocInfo(j.rmode_, j.imm32_);
1102 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001103 // We always need the same number of instructions as we may need to patch
Andrei Popescu31002712010-02-23 13:46:05 +00001104 // this code to load another value which may need 2 instructions to load.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001105 lui(rd, (j.imm32_ >> kLuiShift) & kImm16Mask);
Ben Murdoch257744e2011-11-30 15:57:28 +00001106 ori(rd, rd, (j.imm32_ & kImm16Mask));
Andrei Popescu31002712010-02-23 13:46:05 +00001107 }
1108}
1109
1110
Andrei Popescu31002712010-02-23 13:46:05 +00001111void MacroAssembler::MultiPush(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001112 int16_t num_to_push = NumberOfBitsSet(regs);
1113 int16_t stack_offset = num_to_push * kPointerSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001114
Ben Murdoch589d6972011-11-30 16:04:58 +00001115 Subu(sp, sp, Operand(stack_offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001116 for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
Andrei Popescu31002712010-02-23 13:46:05 +00001117 if ((regs & (1 << i)) != 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001118 stack_offset -= kPointerSize;
1119 sw(ToRegister(i), MemOperand(sp, stack_offset));
Andrei Popescu31002712010-02-23 13:46:05 +00001120 }
1121 }
1122}
1123
1124
1125void MacroAssembler::MultiPushReversed(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001126 int16_t num_to_push = NumberOfBitsSet(regs);
1127 int16_t stack_offset = num_to_push * kPointerSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001128
Ben Murdoch589d6972011-11-30 16:04:58 +00001129 Subu(sp, sp, Operand(stack_offset));
Steve Block6ded16b2010-05-10 14:33:55 +01001130 for (int16_t i = 0; i < kNumRegisters; i++) {
Andrei Popescu31002712010-02-23 13:46:05 +00001131 if ((regs & (1 << i)) != 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001132 stack_offset -= kPointerSize;
1133 sw(ToRegister(i), MemOperand(sp, stack_offset));
Andrei Popescu31002712010-02-23 13:46:05 +00001134 }
1135 }
1136}
1137
1138
1139void MacroAssembler::MultiPop(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001140 int16_t stack_offset = 0;
Andrei Popescu31002712010-02-23 13:46:05 +00001141
Steve Block6ded16b2010-05-10 14:33:55 +01001142 for (int16_t i = 0; i < kNumRegisters; i++) {
Andrei Popescu31002712010-02-23 13:46:05 +00001143 if ((regs & (1 << i)) != 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001144 lw(ToRegister(i), MemOperand(sp, stack_offset));
1145 stack_offset += kPointerSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001146 }
1147 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001148 addiu(sp, sp, stack_offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001149}
1150
1151
1152void MacroAssembler::MultiPopReversed(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001153 int16_t stack_offset = 0;
Andrei Popescu31002712010-02-23 13:46:05 +00001154
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001155 for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
Andrei Popescu31002712010-02-23 13:46:05 +00001156 if ((regs & (1 << i)) != 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001157 lw(ToRegister(i), MemOperand(sp, stack_offset));
1158 stack_offset += kPointerSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001159 }
1160 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001161 addiu(sp, sp, stack_offset);
1162}
1163
1164
1165void MacroAssembler::MultiPushFPU(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001166 int16_t num_to_push = NumberOfBitsSet(regs);
1167 int16_t stack_offset = num_to_push * kDoubleSize;
1168
1169 Subu(sp, sp, Operand(stack_offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001170 for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001171 if ((regs & (1 << i)) != 0) {
1172 stack_offset -= kDoubleSize;
1173 sdc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1174 }
1175 }
1176}
1177
1178
1179void MacroAssembler::MultiPushReversedFPU(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001180 int16_t num_to_push = NumberOfBitsSet(regs);
1181 int16_t stack_offset = num_to_push * kDoubleSize;
1182
1183 Subu(sp, sp, Operand(stack_offset));
1184 for (int16_t i = 0; i < kNumRegisters; i++) {
1185 if ((regs & (1 << i)) != 0) {
1186 stack_offset -= kDoubleSize;
1187 sdc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1188 }
1189 }
1190}
1191
1192
1193void MacroAssembler::MultiPopFPU(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001194 int16_t stack_offset = 0;
1195
1196 for (int16_t i = 0; i < kNumRegisters; i++) {
1197 if ((regs & (1 << i)) != 0) {
1198 ldc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1199 stack_offset += kDoubleSize;
1200 }
1201 }
1202 addiu(sp, sp, stack_offset);
1203}
1204
1205
1206void MacroAssembler::MultiPopReversedFPU(RegList regs) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001207 int16_t stack_offset = 0;
1208
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001209 for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001210 if ((regs & (1 << i)) != 0) {
1211 ldc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1212 stack_offset += kDoubleSize;
1213 }
1214 }
1215 addiu(sp, sp, stack_offset);
Andrei Popescu31002712010-02-23 13:46:05 +00001216}
1217
1218
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001219void MacroAssembler::FlushICache(Register address, unsigned instructions) {
1220 RegList saved_regs = kJSCallerSaved | ra.bit();
1221 MultiPush(saved_regs);
1222 AllowExternalCallThatCantCauseGC scope(this);
1223
1224 // Save to a0 in case address == t0.
1225 Move(a0, address);
1226 PrepareCallCFunction(2, t0);
1227
1228 li(a1, instructions * kInstrSize);
1229 CallCFunction(ExternalReference::flush_icache_function(isolate()), 2);
1230 MultiPop(saved_regs);
1231}
1232
1233
Steve Block44f0eee2011-05-26 01:26:41 +01001234void MacroAssembler::Ext(Register rt,
1235 Register rs,
1236 uint16_t pos,
1237 uint16_t size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001238 DCHECK(pos < 32);
1239 DCHECK(pos + size < 33);
Andrei Popescu31002712010-02-23 13:46:05 +00001240
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001241 if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
Steve Block44f0eee2011-05-26 01:26:41 +01001242 ext_(rt, rs, pos, size);
1243 } else {
1244 // Move rs to rt and shift it left then right to get the
1245 // desired bitfield on the right side and zeroes on the left.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001246 int shift_left = 32 - (pos + size);
1247 sll(rt, rs, shift_left); // Acts as a move if shift_left == 0.
1248
1249 int shift_right = 32 - size;
1250 if (shift_right > 0) {
1251 srl(rt, rt, shift_right);
1252 }
Steve Block44f0eee2011-05-26 01:26:41 +01001253 }
1254}
1255
1256
1257void MacroAssembler::Ins(Register rt,
1258 Register rs,
1259 uint16_t pos,
1260 uint16_t size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261 DCHECK(pos < 32);
1262 DCHECK(pos + size <= 32);
1263 DCHECK(size != 0);
Steve Block44f0eee2011-05-26 01:26:41 +01001264
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001265 if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
Steve Block44f0eee2011-05-26 01:26:41 +01001266 ins_(rt, rs, pos, size);
1267 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001268 DCHECK(!rt.is(t8) && !rs.is(t8));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001269 Subu(at, zero_reg, Operand(1));
1270 srl(at, at, 32 - size);
1271 and_(t8, rs, at);
1272 sll(t8, t8, pos);
1273 sll(at, at, pos);
1274 nor(at, at, zero_reg);
1275 and_(at, rt, at);
1276 or_(rt, t8, at);
Steve Block44f0eee2011-05-26 01:26:41 +01001277 }
1278}
1279
1280
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001281void MacroAssembler::Cvt_d_uw(FPURegister fd,
1282 FPURegister fs,
1283 FPURegister scratch) {
1284 // Move the data from fs to t8.
1285 mfc1(t8, fs);
1286 Cvt_d_uw(fd, t8, scratch);
Steve Block44f0eee2011-05-26 01:26:41 +01001287}
1288
1289
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001290void MacroAssembler::Cvt_d_uw(FPURegister fd,
1291 Register rs,
1292 FPURegister scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01001293 // Convert rs to a FP value in fd (and fd + 1).
1294 // We do this by converting rs minus the MSB to avoid sign conversion,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001295 // then adding 2^31 to the result (if needed).
Steve Block44f0eee2011-05-26 01:26:41 +01001296
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001297 DCHECK(!fd.is(scratch));
1298 DCHECK(!rs.is(t9));
1299 DCHECK(!rs.is(at));
Steve Block44f0eee2011-05-26 01:26:41 +01001300
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001301 // Save rs's MSB to t9.
1302 Ext(t9, rs, 31, 1);
Steve Block44f0eee2011-05-26 01:26:41 +01001303 // Remove rs's MSB.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001304 Ext(at, rs, 0, 31);
1305 // Move the result to fd.
1306 mtc1(at, fd);
Steve Block44f0eee2011-05-26 01:26:41 +01001307
1308 // Convert fd to a real FP value.
1309 cvt_d_w(fd, fd);
1310
1311 Label conversion_done;
1312
1313 // If rs's MSB was 0, it's done.
1314 // Otherwise we need to add that to the FP register.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001315 Branch(&conversion_done, eq, t9, Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +01001316
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001317 // Load 2^31 into f20 as its float representation.
1318 li(at, 0x41E00000);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001319 mtc1(zero_reg, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 Mthc1(at, scratch);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001321 // Add it to fd.
1322 add_d(fd, fd, scratch);
Steve Block44f0eee2011-05-26 01:26:41 +01001323
Steve Block44f0eee2011-05-26 01:26:41 +01001324 bind(&conversion_done);
1325}
1326
1327
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001328void MacroAssembler::Trunc_uw_d(FPURegister fd,
1329 FPURegister fs,
1330 FPURegister scratch) {
1331 Trunc_uw_d(fs, t8, scratch);
1332 mtc1(t8, fd);
Steve Block44f0eee2011-05-26 01:26:41 +01001333}
1334
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001335
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001336void MacroAssembler::Trunc_w_d(FPURegister fd, FPURegister fs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337 if (IsMipsArchVariant(kLoongson) && fd.is(fs)) {
1338 Mfhc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001339 trunc_w_d(fd, fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340 Mthc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001341 } else {
1342 trunc_w_d(fd, fs);
1343 }
1344}
1345
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001346
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001347void MacroAssembler::Round_w_d(FPURegister fd, FPURegister fs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348 if (IsMipsArchVariant(kLoongson) && fd.is(fs)) {
1349 Mfhc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001350 round_w_d(fd, fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351 Mthc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001352 } else {
1353 round_w_d(fd, fs);
1354 }
1355}
1356
1357
1358void MacroAssembler::Floor_w_d(FPURegister fd, FPURegister fs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001359 if (IsMipsArchVariant(kLoongson) && fd.is(fs)) {
1360 Mfhc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001361 floor_w_d(fd, fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001362 Mthc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001363 } else {
1364 floor_w_d(fd, fs);
1365 }
1366}
1367
1368
1369void MacroAssembler::Ceil_w_d(FPURegister fd, FPURegister fs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001370 if (IsMipsArchVariant(kLoongson) && fd.is(fs)) {
1371 Mfhc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001372 ceil_w_d(fd, fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001373 Mthc1(t8, fs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001374 } else {
1375 ceil_w_d(fd, fs);
1376 }
1377}
1378
Steve Block44f0eee2011-05-26 01:26:41 +01001379
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001380void MacroAssembler::Trunc_uw_d(FPURegister fd,
1381 Register rs,
1382 FPURegister scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001383 DCHECK(!fd.is(scratch));
1384 DCHECK(!rs.is(at));
Steve Block44f0eee2011-05-26 01:26:41 +01001385
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001386 // Load 2^31 into scratch as its float representation.
1387 li(at, 0x41E00000);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001388 mtc1(zero_reg, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001389 Mthc1(at, scratch);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001390 // Test if scratch > fd.
Ben Murdoch85b71792012-04-11 18:30:58 +01001391 // If fd < 2^31 we can convert it normally.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001392 Label simple_convert;
1393 BranchF(&simple_convert, NULL, lt, fd, scratch);
Steve Block44f0eee2011-05-26 01:26:41 +01001394
1395 // First we subtract 2^31 from fd, then trunc it to rs
1396 // and add 2^31 to rs.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001397 sub_d(scratch, fd, scratch);
1398 trunc_w_d(scratch, scratch);
1399 mfc1(rs, scratch);
1400 Or(rs, rs, 1 << 31);
Steve Block44f0eee2011-05-26 01:26:41 +01001401
1402 Label done;
1403 Branch(&done);
1404 // Simple conversion.
1405 bind(&simple_convert);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001406 trunc_w_d(scratch, fd);
1407 mfc1(rs, scratch);
Steve Block44f0eee2011-05-26 01:26:41 +01001408
1409 bind(&done);
1410}
1411
1412
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413void MacroAssembler::Mthc1(Register rt, FPURegister fs) {
1414 if (IsFp64Mode()) {
1415 mthc1(rt, fs);
1416 } else {
1417 mtc1(rt, fs.high());
1418 }
1419}
1420
1421
1422void MacroAssembler::Mfhc1(Register rt, FPURegister fs) {
1423 if (IsFp64Mode()) {
1424 mfhc1(rt, fs);
1425 } else {
1426 mfc1(rt, fs.high());
1427 }
1428}
1429
1430
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001431void MacroAssembler::BranchF(Label* target,
1432 Label* nan,
1433 Condition cc,
1434 FPURegister cmp1,
1435 FPURegister cmp2,
1436 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437 BlockTrampolinePoolScope block_trampoline_pool(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001438 if (cc == al) {
1439 Branch(bd, target);
1440 return;
1441 }
1442
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001443 DCHECK(nan || target);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001444 // Check for unordered (NaN) cases.
1445 if (nan) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001446 if (!IsMipsArchVariant(kMips32r6)) {
1447 c(UN, D, cmp1, cmp2);
1448 bc1t(nan);
1449 } else {
1450 // Use kDoubleCompareReg for comparison result. It has to be unavailable
1451 // to lithium register allocator.
1452 DCHECK(!cmp1.is(kDoubleCompareReg) && !cmp2.is(kDoubleCompareReg));
1453 cmp(UN, L, kDoubleCompareReg, cmp1, cmp2);
1454 bc1nez(nan, kDoubleCompareReg);
1455 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001456 }
1457
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001458 if (!IsMipsArchVariant(kMips32r6)) {
1459 if (target) {
1460 // Here NaN cases were either handled by this function or are assumed to
1461 // have been handled by the caller.
1462 switch (cc) {
1463 case lt:
1464 c(OLT, D, cmp1, cmp2);
1465 bc1t(target);
1466 break;
1467 case gt:
1468 c(ULE, D, cmp1, cmp2);
1469 bc1f(target);
1470 break;
1471 case ge:
1472 c(ULT, D, cmp1, cmp2);
1473 bc1f(target);
1474 break;
1475 case le:
1476 c(OLE, D, cmp1, cmp2);
1477 bc1t(target);
1478 break;
1479 case eq:
1480 c(EQ, D, cmp1, cmp2);
1481 bc1t(target);
1482 break;
1483 case ueq:
1484 c(UEQ, D, cmp1, cmp2);
1485 bc1t(target);
1486 break;
1487 case ne:
1488 c(EQ, D, cmp1, cmp2);
1489 bc1f(target);
1490 break;
1491 case nue:
1492 c(UEQ, D, cmp1, cmp2);
1493 bc1f(target);
1494 break;
1495 default:
1496 CHECK(0);
1497 }
1498 }
1499 } else {
1500 if (target) {
1501 // Here NaN cases were either handled by this function or are assumed to
1502 // have been handled by the caller.
1503 // Unsigned conditions are treated as their signed counterpart.
1504 // Use kDoubleCompareReg for comparison result, it is
1505 // valid in fp64 (FR = 1) mode which is implied for mips32r6.
1506 DCHECK(!cmp1.is(kDoubleCompareReg) && !cmp2.is(kDoubleCompareReg));
1507 switch (cc) {
1508 case lt:
1509 cmp(OLT, L, kDoubleCompareReg, cmp1, cmp2);
1510 bc1nez(target, kDoubleCompareReg);
1511 break;
1512 case gt:
1513 cmp(ULE, L, kDoubleCompareReg, cmp1, cmp2);
1514 bc1eqz(target, kDoubleCompareReg);
1515 break;
1516 case ge:
1517 cmp(ULT, L, kDoubleCompareReg, cmp1, cmp2);
1518 bc1eqz(target, kDoubleCompareReg);
1519 break;
1520 case le:
1521 cmp(OLE, L, kDoubleCompareReg, cmp1, cmp2);
1522 bc1nez(target, kDoubleCompareReg);
1523 break;
1524 case eq:
1525 cmp(EQ, L, kDoubleCompareReg, cmp1, cmp2);
1526 bc1nez(target, kDoubleCompareReg);
1527 break;
1528 case ueq:
1529 cmp(UEQ, L, kDoubleCompareReg, cmp1, cmp2);
1530 bc1nez(target, kDoubleCompareReg);
1531 break;
1532 case ne:
1533 cmp(EQ, L, kDoubleCompareReg, cmp1, cmp2);
1534 bc1eqz(target, kDoubleCompareReg);
1535 break;
1536 case nue:
1537 cmp(UEQ, L, kDoubleCompareReg, cmp1, cmp2);
1538 bc1eqz(target, kDoubleCompareReg);
1539 break;
1540 default:
1541 CHECK(0);
1542 }
1543 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001544 }
1545
1546 if (bd == PROTECT) {
1547 nop();
1548 }
1549}
1550
1551
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001552void MacroAssembler::Move(FPURegister dst, float imm) {
1553 li(at, Operand(bit_cast<int32_t>(imm)));
1554 mtc1(at, dst);
1555}
1556
1557
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001558void MacroAssembler::Move(FPURegister dst, double imm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001559 static const DoubleRepresentation minus_zero(-0.0);
1560 static const DoubleRepresentation zero(0.0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001561 DoubleRepresentation value_rep(imm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001562 // Handle special values first.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001563 if (value_rep == zero && has_double_zero_reg_set_) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001564 mov_d(dst, kDoubleRegZero);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001565 } else if (value_rep == minus_zero && has_double_zero_reg_set_) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001566 neg_d(dst, kDoubleRegZero);
1567 } else {
1568 uint32_t lo, hi;
1569 DoubleAsTwoUInt32(imm, &lo, &hi);
1570 // Move the low part of the double into the lower of the corresponding FPU
1571 // register of FPU register pair.
1572 if (lo != 0) {
1573 li(at, Operand(lo));
1574 mtc1(at, dst);
1575 } else {
1576 mtc1(zero_reg, dst);
1577 }
1578 // Move the high part of the double into the higher of the corresponding FPU
1579 // register of FPU register pair.
1580 if (hi != 0) {
1581 li(at, Operand(hi));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001582 Mthc1(at, dst);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001583 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001584 Mthc1(zero_reg, dst);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001585 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001586 if (dst.is(kDoubleRegZero)) has_double_zero_reg_set_ = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001587 }
1588}
1589
1590
1591void MacroAssembler::Movz(Register rd, Register rs, Register rt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001592 if (IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r6)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001593 Label done;
1594 Branch(&done, ne, rt, Operand(zero_reg));
1595 mov(rd, rs);
1596 bind(&done);
1597 } else {
1598 movz(rd, rs, rt);
1599 }
1600}
1601
1602
1603void MacroAssembler::Movn(Register rd, Register rs, Register rt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001604 if (IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r6)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001605 Label done;
1606 Branch(&done, eq, rt, Operand(zero_reg));
1607 mov(rd, rs);
1608 bind(&done);
1609 } else {
1610 movn(rd, rs, rt);
1611 }
1612}
1613
1614
1615void MacroAssembler::Movt(Register rd, Register rs, uint16_t cc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616 if (IsMipsArchVariant(kLoongson)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001617 // Tests an FP condition code and then conditionally move rs to rd.
1618 // We do not currently use any FPU cc bit other than bit 0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001619 DCHECK(cc == 0);
1620 DCHECK(!(rs.is(t8) || rd.is(t8)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001621 Label done;
1622 Register scratch = t8;
1623 // For testing purposes we need to fetch content of the FCSR register and
1624 // than test its cc (floating point condition code) bit (for cc = 0, it is
1625 // 24. bit of the FCSR).
1626 cfc1(scratch, FCSR);
1627 // For the MIPS I, II and III architectures, the contents of scratch is
1628 // UNPREDICTABLE for the instruction immediately following CFC1.
1629 nop();
1630 srl(scratch, scratch, 16);
1631 andi(scratch, scratch, 0x0080);
1632 Branch(&done, eq, scratch, Operand(zero_reg));
1633 mov(rd, rs);
1634 bind(&done);
1635 } else {
1636 movt(rd, rs, cc);
1637 }
1638}
1639
1640
1641void MacroAssembler::Movf(Register rd, Register rs, uint16_t cc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 if (IsMipsArchVariant(kLoongson)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001643 // Tests an FP condition code and then conditionally move rs to rd.
1644 // We do not currently use any FPU cc bit other than bit 0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001645 DCHECK(cc == 0);
1646 DCHECK(!(rs.is(t8) || rd.is(t8)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001647 Label done;
1648 Register scratch = t8;
1649 // For testing purposes we need to fetch content of the FCSR register and
1650 // than test its cc (floating point condition code) bit (for cc = 0, it is
1651 // 24. bit of the FCSR).
1652 cfc1(scratch, FCSR);
1653 // For the MIPS I, II and III architectures, the contents of scratch is
1654 // UNPREDICTABLE for the instruction immediately following CFC1.
1655 nop();
1656 srl(scratch, scratch, 16);
1657 andi(scratch, scratch, 0x0080);
1658 Branch(&done, ne, scratch, Operand(zero_reg));
1659 mov(rd, rs);
1660 bind(&done);
1661 } else {
1662 movf(rd, rs, cc);
1663 }
1664}
1665
1666
1667void MacroAssembler::Clz(Register rd, Register rs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001668 if (IsMipsArchVariant(kLoongson)) {
1669 DCHECK(!(rd.is(t8) || rd.is(t9)) && !(rs.is(t8) || rs.is(t9)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001670 Register mask = t8;
1671 Register scratch = t9;
1672 Label loop, end;
1673 mov(at, rs);
1674 mov(rd, zero_reg);
1675 lui(mask, 0x8000);
1676 bind(&loop);
1677 and_(scratch, at, mask);
1678 Branch(&end, ne, scratch, Operand(zero_reg));
1679 addiu(rd, rd, 1);
1680 Branch(&loop, ne, mask, Operand(zero_reg), USE_DELAY_SLOT);
1681 srl(mask, mask, 1);
1682 bind(&end);
1683 } else {
1684 clz(rd, rs);
1685 }
1686}
1687
1688
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001689void MacroAssembler::EmitFPUTruncate(FPURoundingMode rounding_mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001690 Register result,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001691 DoubleRegister double_input,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001692 Register scratch,
1693 DoubleRegister double_scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001694 Register except_flag,
1695 CheckForInexactConversion check_inexact) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001696 DCHECK(!result.is(scratch));
1697 DCHECK(!double_input.is(double_scratch));
1698 DCHECK(!except_flag.is(scratch));
1699
1700 Label done;
1701
1702 // Clear the except flag (0 = no exception)
1703 mov(except_flag, zero_reg);
1704
1705 // Test for values that can be exactly represented as a signed 32-bit integer.
1706 cvt_w_d(double_scratch, double_input);
1707 mfc1(result, double_scratch);
1708 cvt_d_w(double_scratch, double_scratch);
1709 BranchF(&done, NULL, eq, double_input, double_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001710
1711 int32_t except_mask = kFCSRFlagMask; // Assume interested in all exceptions.
1712
1713 if (check_inexact == kDontCheckForInexactConversion) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001714 // Ignore inexact exceptions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001715 except_mask &= ~kFCSRInexactFlagMask;
1716 }
1717
1718 // Save FCSR.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001719 cfc1(scratch, FCSR);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001720 // Disable FPU exceptions.
1721 ctc1(zero_reg, FCSR);
1722
1723 // Do operation based on rounding mode.
1724 switch (rounding_mode) {
1725 case kRoundToNearest:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001726 Round_w_d(double_scratch, double_input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001727 break;
1728 case kRoundToZero:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001729 Trunc_w_d(double_scratch, double_input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001730 break;
1731 case kRoundToPlusInf:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732 Ceil_w_d(double_scratch, double_input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001733 break;
1734 case kRoundToMinusInf:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001735 Floor_w_d(double_scratch, double_input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001736 break;
1737 } // End of switch-statement.
1738
1739 // Retrieve FCSR.
1740 cfc1(except_flag, FCSR);
1741 // Restore FCSR.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001742 ctc1(scratch, FCSR);
1743 // Move the converted value into the result register.
1744 mfc1(result, double_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001745
1746 // Check for fpu exceptions.
1747 And(except_flag, except_flag, Operand(except_mask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001748
Ben Murdoch257744e2011-11-30 15:57:28 +00001749 bind(&done);
1750}
1751
1752
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001753void MacroAssembler::TryInlineTruncateDoubleToI(Register result,
1754 DoubleRegister double_input,
1755 Label* done) {
1756 DoubleRegister single_scratch = kLithiumScratchDouble.low();
1757 Register scratch = at;
1758 Register scratch2 = t9;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001759
1760 // Clear cumulative exception flags and save the FCSR.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001761 cfc1(scratch2, FCSR);
1762 ctc1(zero_reg, FCSR);
1763 // Try a conversion to a signed integer.
1764 trunc_w_d(single_scratch, double_input);
1765 mfc1(result, single_scratch);
1766 // Retrieve and restore the FCSR.
1767 cfc1(scratch, FCSR);
1768 ctc1(scratch2, FCSR);
1769 // Check for overflow and NaNs.
1770 And(scratch,
1771 scratch,
1772 kFCSROverflowFlagMask | kFCSRUnderflowFlagMask | kFCSRInvalidOpFlagMask);
1773 // If we had no exceptions we are done.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001774 Branch(done, eq, scratch, Operand(zero_reg));
1775}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001776
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001777
1778void MacroAssembler::TruncateDoubleToI(Register result,
1779 DoubleRegister double_input) {
1780 Label done;
1781
1782 TryInlineTruncateDoubleToI(result, double_input, &done);
1783
1784 // If we fell through then inline version didn't succeed - call stub instead.
1785 push(ra);
1786 Subu(sp, sp, Operand(kDoubleSize)); // Put input on stack.
1787 sdc1(double_input, MemOperand(sp, 0));
1788
1789 DoubleToIStub stub(isolate(), sp, result, 0, true, true);
1790 CallStub(&stub);
1791
1792 Addu(sp, sp, Operand(kDoubleSize));
1793 pop(ra);
1794
1795 bind(&done);
1796}
1797
1798
1799void MacroAssembler::TruncateHeapNumberToI(Register result, Register object) {
1800 Label done;
1801 DoubleRegister double_scratch = f12;
1802 DCHECK(!result.is(object));
1803
1804 ldc1(double_scratch,
1805 MemOperand(object, HeapNumber::kValueOffset - kHeapObjectTag));
1806 TryInlineTruncateDoubleToI(result, double_scratch, &done);
1807
1808 // If we fell through then inline version didn't succeed - call stub instead.
1809 push(ra);
1810 DoubleToIStub stub(isolate(),
1811 object,
1812 result,
1813 HeapNumber::kValueOffset - kHeapObjectTag,
1814 true,
1815 true);
1816 CallStub(&stub);
1817 pop(ra);
1818
1819 bind(&done);
1820}
1821
1822
1823void MacroAssembler::TruncateNumberToI(Register object,
1824 Register result,
1825 Register heap_number_map,
1826 Register scratch,
1827 Label* not_number) {
1828 Label done;
1829 DCHECK(!result.is(object));
1830
1831 UntagAndJumpIfSmi(result, object, &done);
1832 JumpIfNotHeapNumber(object, heap_number_map, scratch, not_number);
1833 TruncateHeapNumberToI(result, object);
1834
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001835 bind(&done);
1836}
1837
1838
Ben Murdoch257744e2011-11-30 15:57:28 +00001839void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1840 Register src,
1841 int num_least_bits) {
1842 Ext(dst, src, kSmiTagSize, num_least_bits);
1843}
1844
1845
1846void MacroAssembler::GetLeastBitsFromInt32(Register dst,
1847 Register src,
1848 int num_least_bits) {
1849 And(dst, src, Operand((1 << num_least_bits) - 1));
1850}
1851
1852
Steve Block44f0eee2011-05-26 01:26:41 +01001853// Emulated condtional branches do not emit a nop in the branch delay slot.
1854//
1855// BRANCH_ARGS_CHECK checks that conditional jump arguments are correct.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001856#define BRANCH_ARGS_CHECK(cond, rs, rt) DCHECK( \
Steve Block44f0eee2011-05-26 01:26:41 +01001857 (cond == cc_always && rs.is(zero_reg) && rt.rm().is(zero_reg)) || \
1858 (cond != cc_always && (!rs.is(zero_reg) || !rt.rm().is(zero_reg))))
1859
1860
1861void MacroAssembler::Branch(int16_t offset, BranchDelaySlot bdslot) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001862 BranchShort(offset, bdslot);
1863}
1864
1865
1866void MacroAssembler::Branch(int16_t offset, Condition cond, Register rs,
1867 const Operand& rt,
1868 BranchDelaySlot bdslot) {
1869 BranchShort(offset, cond, rs, rt, bdslot);
1870}
1871
1872
1873void MacroAssembler::Branch(Label* L, BranchDelaySlot bdslot) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001874 if (L->is_bound()) {
1875 if (is_near(L)) {
1876 BranchShort(L, bdslot);
1877 } else {
1878 Jr(L, bdslot);
1879 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001880 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001881 if (is_trampoline_emitted()) {
1882 Jr(L, bdslot);
1883 } else {
1884 BranchShort(L, bdslot);
1885 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001886 }
1887}
1888
1889
1890void MacroAssembler::Branch(Label* L, Condition cond, Register rs,
1891 const Operand& rt,
1892 BranchDelaySlot bdslot) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001893 if (L->is_bound()) {
1894 if (is_near(L)) {
1895 BranchShort(L, cond, rs, rt, bdslot);
1896 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001897 if (cond != cc_always) {
1898 Label skip;
1899 Condition neg_cond = NegateCondition(cond);
1900 BranchShort(&skip, neg_cond, rs, rt);
1901 Jr(L, bdslot);
1902 bind(&skip);
1903 } else {
1904 Jr(L, bdslot);
1905 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001906 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001907 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001908 if (is_trampoline_emitted()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001909 if (cond != cc_always) {
1910 Label skip;
1911 Condition neg_cond = NegateCondition(cond);
1912 BranchShort(&skip, neg_cond, rs, rt);
1913 Jr(L, bdslot);
1914 bind(&skip);
1915 } else {
1916 Jr(L, bdslot);
1917 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001918 } else {
1919 BranchShort(L, cond, rs, rt, bdslot);
1920 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001921 }
1922}
1923
1924
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001925void MacroAssembler::Branch(Label* L,
1926 Condition cond,
1927 Register rs,
1928 Heap::RootListIndex index,
1929 BranchDelaySlot bdslot) {
1930 LoadRoot(at, index);
1931 Branch(L, cond, rs, Operand(at), bdslot);
1932}
1933
1934
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001935void MacroAssembler::BranchShort(int16_t offset, BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01001936 b(offset);
1937
1938 // Emit a nop in the branch delay slot if required.
1939 if (bdslot == PROTECT)
1940 nop();
1941}
1942
1943
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001944void MacroAssembler::BranchShort(int16_t offset, Condition cond, Register rs,
1945 const Operand& rt,
1946 BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01001947 BRANCH_ARGS_CHECK(cond, rs, rt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001948 DCHECK(!rs.is(zero_reg));
Steve Block6ded16b2010-05-10 14:33:55 +01001949 Register r2 = no_reg;
Steve Block44f0eee2011-05-26 01:26:41 +01001950 Register scratch = at;
1951
Andrei Popescu31002712010-02-23 13:46:05 +00001952 if (rt.is_reg()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001953 // NOTE: 'at' can be clobbered by Branch but it is legal to use it as rs or
1954 // rt.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001955 BlockTrampolinePoolScope block_trampoline_pool(this);
Andrei Popescu31002712010-02-23 13:46:05 +00001956 r2 = rt.rm_;
Steve Block44f0eee2011-05-26 01:26:41 +01001957 switch (cond) {
1958 case cc_always:
1959 b(offset);
1960 break;
1961 case eq:
1962 beq(rs, r2, offset);
1963 break;
1964 case ne:
1965 bne(rs, r2, offset);
1966 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001967 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01001968 case greater:
1969 if (r2.is(zero_reg)) {
1970 bgtz(rs, offset);
1971 } else {
1972 slt(scratch, r2, rs);
1973 bne(scratch, zero_reg, offset);
1974 }
1975 break;
1976 case greater_equal:
1977 if (r2.is(zero_reg)) {
1978 bgez(rs, offset);
1979 } else {
1980 slt(scratch, rs, r2);
1981 beq(scratch, zero_reg, offset);
1982 }
1983 break;
1984 case less:
1985 if (r2.is(zero_reg)) {
1986 bltz(rs, offset);
1987 } else {
1988 slt(scratch, rs, r2);
1989 bne(scratch, zero_reg, offset);
1990 }
1991 break;
1992 case less_equal:
1993 if (r2.is(zero_reg)) {
1994 blez(rs, offset);
1995 } else {
1996 slt(scratch, r2, rs);
1997 beq(scratch, zero_reg, offset);
1998 }
1999 break;
Andrei Popescu31002712010-02-23 13:46:05 +00002000 // Unsigned comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01002001 case Ugreater:
2002 if (r2.is(zero_reg)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002003 bne(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002004 } else {
2005 sltu(scratch, r2, rs);
2006 bne(scratch, zero_reg, offset);
2007 }
2008 break;
2009 case Ugreater_equal:
2010 if (r2.is(zero_reg)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002011 b(offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002012 } else {
2013 sltu(scratch, rs, r2);
2014 beq(scratch, zero_reg, offset);
2015 }
2016 break;
2017 case Uless:
2018 if (r2.is(zero_reg)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002019 // No code needs to be emitted.
2020 return;
Steve Block44f0eee2011-05-26 01:26:41 +01002021 } else {
2022 sltu(scratch, rs, r2);
2023 bne(scratch, zero_reg, offset);
2024 }
2025 break;
2026 case Uless_equal:
2027 if (r2.is(zero_reg)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002028 beq(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002029 } else {
2030 sltu(scratch, r2, rs);
2031 beq(scratch, zero_reg, offset);
2032 }
2033 break;
2034 default:
2035 UNREACHABLE();
2036 }
2037 } else {
2038 // Be careful to always use shifted_branch_offset only just before the
2039 // branch instruction, as the location will be remember for patching the
2040 // target.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002041 BlockTrampolinePoolScope block_trampoline_pool(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002042 switch (cond) {
2043 case cc_always:
2044 b(offset);
2045 break;
2046 case eq:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002047 if (rt.imm32_ == 0) {
2048 beq(rs, zero_reg, offset);
2049 } else {
2050 // We don't want any other register but scratch clobbered.
2051 DCHECK(!scratch.is(rs));
2052 r2 = scratch;
2053 li(r2, rt);
2054 beq(rs, r2, offset);
2055 }
Steve Block44f0eee2011-05-26 01:26:41 +01002056 break;
2057 case ne:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002058 if (rt.imm32_ == 0) {
2059 bne(rs, zero_reg, offset);
2060 } else {
2061 // We don't want any other register but scratch clobbered.
2062 DCHECK(!scratch.is(rs));
2063 r2 = scratch;
2064 li(r2, rt);
2065 bne(rs, r2, offset);
2066 }
Steve Block44f0eee2011-05-26 01:26:41 +01002067 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002068 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01002069 case greater:
2070 if (rt.imm32_ == 0) {
2071 bgtz(rs, offset);
2072 } else {
2073 r2 = scratch;
2074 li(r2, rt);
2075 slt(scratch, r2, rs);
2076 bne(scratch, zero_reg, offset);
2077 }
2078 break;
2079 case greater_equal:
2080 if (rt.imm32_ == 0) {
2081 bgez(rs, offset);
2082 } else if (is_int16(rt.imm32_)) {
2083 slti(scratch, rs, rt.imm32_);
2084 beq(scratch, zero_reg, offset);
2085 } else {
2086 r2 = scratch;
2087 li(r2, rt);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002088 slt(scratch, rs, r2);
Steve Block44f0eee2011-05-26 01:26:41 +01002089 beq(scratch, zero_reg, offset);
2090 }
2091 break;
2092 case less:
2093 if (rt.imm32_ == 0) {
2094 bltz(rs, offset);
2095 } else if (is_int16(rt.imm32_)) {
2096 slti(scratch, rs, rt.imm32_);
2097 bne(scratch, zero_reg, offset);
2098 } else {
2099 r2 = scratch;
2100 li(r2, rt);
2101 slt(scratch, rs, r2);
2102 bne(scratch, zero_reg, offset);
2103 }
2104 break;
2105 case less_equal:
2106 if (rt.imm32_ == 0) {
2107 blez(rs, offset);
2108 } else {
2109 r2 = scratch;
2110 li(r2, rt);
2111 slt(scratch, r2, rs);
2112 beq(scratch, zero_reg, offset);
2113 }
2114 break;
2115 // Unsigned comparison.
2116 case Ugreater:
2117 if (rt.imm32_ == 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002118 bne(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002119 } else {
2120 r2 = scratch;
2121 li(r2, rt);
2122 sltu(scratch, r2, rs);
2123 bne(scratch, zero_reg, offset);
2124 }
2125 break;
2126 case Ugreater_equal:
2127 if (rt.imm32_ == 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002128 b(offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002129 } else if (is_int16(rt.imm32_)) {
2130 sltiu(scratch, rs, rt.imm32_);
2131 beq(scratch, zero_reg, offset);
2132 } else {
2133 r2 = scratch;
2134 li(r2, rt);
2135 sltu(scratch, rs, r2);
2136 beq(scratch, zero_reg, offset);
2137 }
2138 break;
2139 case Uless:
2140 if (rt.imm32_ == 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002141 // No code needs to be emitted.
2142 return;
Steve Block44f0eee2011-05-26 01:26:41 +01002143 } else if (is_int16(rt.imm32_)) {
2144 sltiu(scratch, rs, rt.imm32_);
2145 bne(scratch, zero_reg, offset);
2146 } else {
2147 r2 = scratch;
2148 li(r2, rt);
2149 sltu(scratch, rs, r2);
2150 bne(scratch, zero_reg, offset);
2151 }
2152 break;
2153 case Uless_equal:
2154 if (rt.imm32_ == 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002155 beq(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002156 } else {
2157 r2 = scratch;
2158 li(r2, rt);
2159 sltu(scratch, r2, rs);
2160 beq(scratch, zero_reg, offset);
2161 }
2162 break;
2163 default:
2164 UNREACHABLE();
2165 }
Andrei Popescu31002712010-02-23 13:46:05 +00002166 }
Steve Block44f0eee2011-05-26 01:26:41 +01002167 // Emit a nop in the branch delay slot if required.
2168 if (bdslot == PROTECT)
2169 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00002170}
2171
2172
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002173void MacroAssembler::BranchShort(Label* L, BranchDelaySlot bdslot) {
Andrei Popescu31002712010-02-23 13:46:05 +00002174 // We use branch_offset as an argument for the branch instructions to be sure
2175 // it is called just before generating the branch instruction, as needed.
2176
Steve Block44f0eee2011-05-26 01:26:41 +01002177 b(shifted_branch_offset(L, false));
Andrei Popescu31002712010-02-23 13:46:05 +00002178
Steve Block44f0eee2011-05-26 01:26:41 +01002179 // Emit a nop in the branch delay slot if required.
2180 if (bdslot == PROTECT)
2181 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00002182}
2183
2184
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002185void MacroAssembler::BranchShort(Label* L, Condition cond, Register rs,
2186 const Operand& rt,
2187 BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01002188 BRANCH_ARGS_CHECK(cond, rs, rt);
2189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002190 int32_t offset = 0;
Steve Block44f0eee2011-05-26 01:26:41 +01002191 Register r2 = no_reg;
2192 Register scratch = at;
2193 if (rt.is_reg()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002194 BlockTrampolinePoolScope block_trampoline_pool(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002195 r2 = rt.rm_;
2196 // Be careful to always use shifted_branch_offset only just before the
2197 // branch instruction, as the location will be remember for patching the
2198 // target.
2199 switch (cond) {
2200 case cc_always:
2201 offset = shifted_branch_offset(L, false);
2202 b(offset);
2203 break;
2204 case eq:
2205 offset = shifted_branch_offset(L, false);
2206 beq(rs, r2, offset);
2207 break;
2208 case ne:
2209 offset = shifted_branch_offset(L, false);
2210 bne(rs, r2, offset);
2211 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002212 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01002213 case greater:
2214 if (r2.is(zero_reg)) {
2215 offset = shifted_branch_offset(L, false);
2216 bgtz(rs, offset);
2217 } else {
2218 slt(scratch, r2, rs);
2219 offset = shifted_branch_offset(L, false);
2220 bne(scratch, zero_reg, offset);
2221 }
2222 break;
2223 case greater_equal:
2224 if (r2.is(zero_reg)) {
2225 offset = shifted_branch_offset(L, false);
2226 bgez(rs, offset);
2227 } else {
2228 slt(scratch, rs, r2);
2229 offset = shifted_branch_offset(L, false);
2230 beq(scratch, zero_reg, offset);
2231 }
2232 break;
2233 case less:
2234 if (r2.is(zero_reg)) {
2235 offset = shifted_branch_offset(L, false);
2236 bltz(rs, offset);
2237 } else {
2238 slt(scratch, rs, r2);
2239 offset = shifted_branch_offset(L, false);
2240 bne(scratch, zero_reg, offset);
2241 }
2242 break;
2243 case less_equal:
2244 if (r2.is(zero_reg)) {
2245 offset = shifted_branch_offset(L, false);
2246 blez(rs, offset);
2247 } else {
2248 slt(scratch, r2, rs);
2249 offset = shifted_branch_offset(L, false);
2250 beq(scratch, zero_reg, offset);
2251 }
2252 break;
2253 // Unsigned comparison.
2254 case Ugreater:
2255 if (r2.is(zero_reg)) {
2256 offset = shifted_branch_offset(L, false);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002257 bne(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002258 } else {
2259 sltu(scratch, r2, rs);
2260 offset = shifted_branch_offset(L, false);
2261 bne(scratch, zero_reg, offset);
2262 }
2263 break;
2264 case Ugreater_equal:
2265 if (r2.is(zero_reg)) {
2266 offset = shifted_branch_offset(L, false);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002267 b(offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002268 } else {
2269 sltu(scratch, rs, r2);
2270 offset = shifted_branch_offset(L, false);
2271 beq(scratch, zero_reg, offset);
2272 }
2273 break;
2274 case Uless:
2275 if (r2.is(zero_reg)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002276 // No code needs to be emitted.
2277 return;
Steve Block44f0eee2011-05-26 01:26:41 +01002278 } else {
2279 sltu(scratch, rs, r2);
2280 offset = shifted_branch_offset(L, false);
2281 bne(scratch, zero_reg, offset);
2282 }
2283 break;
2284 case Uless_equal:
2285 if (r2.is(zero_reg)) {
2286 offset = shifted_branch_offset(L, false);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002287 beq(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002288 } else {
2289 sltu(scratch, r2, rs);
2290 offset = shifted_branch_offset(L, false);
2291 beq(scratch, zero_reg, offset);
2292 }
2293 break;
2294 default:
2295 UNREACHABLE();
2296 }
2297 } else {
2298 // Be careful to always use shifted_branch_offset only just before the
2299 // branch instruction, as the location will be remember for patching the
2300 // target.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002301 BlockTrampolinePoolScope block_trampoline_pool(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002302 switch (cond) {
2303 case cc_always:
2304 offset = shifted_branch_offset(L, false);
2305 b(offset);
2306 break;
2307 case eq:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002308 if (rt.imm32_ == 0) {
2309 offset = shifted_branch_offset(L, false);
2310 beq(rs, zero_reg, offset);
2311 } else {
2312 DCHECK(!scratch.is(rs));
2313 r2 = scratch;
2314 li(r2, rt);
2315 offset = shifted_branch_offset(L, false);
2316 beq(rs, r2, offset);
2317 }
Steve Block44f0eee2011-05-26 01:26:41 +01002318 break;
2319 case ne:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002320 if (rt.imm32_ == 0) {
2321 offset = shifted_branch_offset(L, false);
2322 bne(rs, zero_reg, offset);
2323 } else {
2324 DCHECK(!scratch.is(rs));
2325 r2 = scratch;
2326 li(r2, rt);
2327 offset = shifted_branch_offset(L, false);
2328 bne(rs, r2, offset);
2329 }
Steve Block44f0eee2011-05-26 01:26:41 +01002330 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002331 // Signed comparison.
Steve Block44f0eee2011-05-26 01:26:41 +01002332 case greater:
2333 if (rt.imm32_ == 0) {
2334 offset = shifted_branch_offset(L, false);
2335 bgtz(rs, offset);
2336 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002337 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002338 r2 = scratch;
2339 li(r2, rt);
2340 slt(scratch, r2, rs);
2341 offset = shifted_branch_offset(L, false);
2342 bne(scratch, zero_reg, offset);
2343 }
2344 break;
2345 case greater_equal:
2346 if (rt.imm32_ == 0) {
2347 offset = shifted_branch_offset(L, false);
2348 bgez(rs, offset);
2349 } else if (is_int16(rt.imm32_)) {
2350 slti(scratch, rs, rt.imm32_);
2351 offset = shifted_branch_offset(L, false);
2352 beq(scratch, zero_reg, offset);
2353 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002354 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002355 r2 = scratch;
2356 li(r2, rt);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002357 slt(scratch, rs, r2);
Steve Block44f0eee2011-05-26 01:26:41 +01002358 offset = shifted_branch_offset(L, false);
2359 beq(scratch, zero_reg, offset);
2360 }
2361 break;
2362 case less:
2363 if (rt.imm32_ == 0) {
2364 offset = shifted_branch_offset(L, false);
2365 bltz(rs, offset);
2366 } else if (is_int16(rt.imm32_)) {
2367 slti(scratch, rs, rt.imm32_);
2368 offset = shifted_branch_offset(L, false);
2369 bne(scratch, zero_reg, offset);
2370 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002371 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002372 r2 = scratch;
2373 li(r2, rt);
2374 slt(scratch, rs, r2);
2375 offset = shifted_branch_offset(L, false);
2376 bne(scratch, zero_reg, offset);
2377 }
2378 break;
2379 case less_equal:
2380 if (rt.imm32_ == 0) {
2381 offset = shifted_branch_offset(L, false);
2382 blez(rs, offset);
2383 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002384 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002385 r2 = scratch;
2386 li(r2, rt);
2387 slt(scratch, r2, rs);
2388 offset = shifted_branch_offset(L, false);
2389 beq(scratch, zero_reg, offset);
2390 }
2391 break;
2392 // Unsigned comparison.
2393 case Ugreater:
2394 if (rt.imm32_ == 0) {
2395 offset = shifted_branch_offset(L, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002396 bne(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002397 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002398 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002399 r2 = scratch;
2400 li(r2, rt);
2401 sltu(scratch, r2, rs);
2402 offset = shifted_branch_offset(L, false);
2403 bne(scratch, zero_reg, offset);
2404 }
2405 break;
2406 case Ugreater_equal:
2407 if (rt.imm32_ == 0) {
2408 offset = shifted_branch_offset(L, false);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002409 b(offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002410 } else if (is_int16(rt.imm32_)) {
2411 sltiu(scratch, rs, rt.imm32_);
2412 offset = shifted_branch_offset(L, false);
2413 beq(scratch, zero_reg, offset);
2414 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002415 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002416 r2 = scratch;
2417 li(r2, rt);
2418 sltu(scratch, rs, r2);
2419 offset = shifted_branch_offset(L, false);
2420 beq(scratch, zero_reg, offset);
2421 }
2422 break;
2423 case Uless:
2424 if (rt.imm32_ == 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002425 // No code needs to be emitted.
2426 return;
Steve Block44f0eee2011-05-26 01:26:41 +01002427 } else if (is_int16(rt.imm32_)) {
2428 sltiu(scratch, rs, rt.imm32_);
2429 offset = shifted_branch_offset(L, false);
2430 bne(scratch, zero_reg, offset);
2431 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002432 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002433 r2 = scratch;
2434 li(r2, rt);
2435 sltu(scratch, rs, r2);
2436 offset = shifted_branch_offset(L, false);
2437 bne(scratch, zero_reg, offset);
2438 }
2439 break;
2440 case Uless_equal:
2441 if (rt.imm32_ == 0) {
2442 offset = shifted_branch_offset(L, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002443 beq(rs, zero_reg, offset);
Steve Block44f0eee2011-05-26 01:26:41 +01002444 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 DCHECK(!scratch.is(rs));
Steve Block44f0eee2011-05-26 01:26:41 +01002446 r2 = scratch;
2447 li(r2, rt);
2448 sltu(scratch, r2, rs);
2449 offset = shifted_branch_offset(L, false);
2450 beq(scratch, zero_reg, offset);
2451 }
2452 break;
2453 default:
2454 UNREACHABLE();
2455 }
2456 }
2457 // Check that offset could actually hold on an int16_t.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002458 DCHECK(is_int16(offset));
Steve Block44f0eee2011-05-26 01:26:41 +01002459 // Emit a nop in the branch delay slot if required.
2460 if (bdslot == PROTECT)
2461 nop();
2462}
2463
2464
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002465void MacroAssembler::BranchAndLink(int16_t offset, BranchDelaySlot bdslot) {
2466 BranchAndLinkShort(offset, bdslot);
2467}
2468
2469
2470void MacroAssembler::BranchAndLink(int16_t offset, Condition cond, Register rs,
2471 const Operand& rt,
2472 BranchDelaySlot bdslot) {
2473 BranchAndLinkShort(offset, cond, rs, rt, bdslot);
2474}
2475
2476
2477void MacroAssembler::BranchAndLink(Label* L, BranchDelaySlot bdslot) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002478 if (L->is_bound()) {
2479 if (is_near(L)) {
2480 BranchAndLinkShort(L, bdslot);
2481 } else {
2482 Jalr(L, bdslot);
2483 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002484 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002485 if (is_trampoline_emitted()) {
2486 Jalr(L, bdslot);
2487 } else {
2488 BranchAndLinkShort(L, bdslot);
2489 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002490 }
2491}
2492
2493
2494void MacroAssembler::BranchAndLink(Label* L, Condition cond, Register rs,
2495 const Operand& rt,
2496 BranchDelaySlot bdslot) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002497 if (L->is_bound()) {
2498 if (is_near(L)) {
2499 BranchAndLinkShort(L, cond, rs, rt, bdslot);
2500 } else {
2501 Label skip;
2502 Condition neg_cond = NegateCondition(cond);
2503 BranchShort(&skip, neg_cond, rs, rt);
2504 Jalr(L, bdslot);
2505 bind(&skip);
2506 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002507 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002508 if (is_trampoline_emitted()) {
2509 Label skip;
2510 Condition neg_cond = NegateCondition(cond);
2511 BranchShort(&skip, neg_cond, rs, rt);
2512 Jalr(L, bdslot);
2513 bind(&skip);
2514 } else {
2515 BranchAndLinkShort(L, cond, rs, rt, bdslot);
2516 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002517 }
2518}
2519
2520
Andrei Popescu31002712010-02-23 13:46:05 +00002521// We need to use a bgezal or bltzal, but they can't be used directly with the
2522// slt instructions. We could use sub or add instead but we would miss overflow
2523// cases, so we keep slt and add an intermediate third instruction.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002524void MacroAssembler::BranchAndLinkShort(int16_t offset,
2525 BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01002526 bal(offset);
Andrei Popescu31002712010-02-23 13:46:05 +00002527
Steve Block44f0eee2011-05-26 01:26:41 +01002528 // Emit a nop in the branch delay slot if required.
2529 if (bdslot == PROTECT)
2530 nop();
Andrei Popescu31002712010-02-23 13:46:05 +00002531}
2532
2533
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002534void MacroAssembler::BranchAndLinkShort(int16_t offset, Condition cond,
2535 Register rs, const Operand& rt,
2536 BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01002537 BRANCH_ARGS_CHECK(cond, rs, rt);
Steve Block6ded16b2010-05-10 14:33:55 +01002538 Register r2 = no_reg;
Steve Block44f0eee2011-05-26 01:26:41 +01002539 Register scratch = at;
2540
Andrei Popescu31002712010-02-23 13:46:05 +00002541 if (rt.is_reg()) {
2542 r2 = rt.rm_;
2543 } else if (cond != cc_always) {
2544 r2 = scratch;
2545 li(r2, rt);
2546 }
2547
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002548 if (!IsMipsArchVariant(kMips32r6)) {
2549 BlockTrampolinePoolScope block_trampoline_pool(this);
2550 switch (cond) {
2551 case cc_always:
2552 bal(offset);
2553 break;
2554 case eq:
2555 bne(rs, r2, 2);
2556 nop();
2557 bal(offset);
2558 break;
2559 case ne:
2560 beq(rs, r2, 2);
2561 nop();
2562 bal(offset);
2563 break;
Andrei Popescu31002712010-02-23 13:46:05 +00002564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002565 // Signed comparison.
2566 case greater:
2567 slt(scratch, r2, rs);
2568 addiu(scratch, scratch, -1);
2569 bgezal(scratch, offset);
2570 break;
2571 case greater_equal:
2572 slt(scratch, rs, r2);
2573 addiu(scratch, scratch, -1);
2574 bltzal(scratch, offset);
2575 break;
2576 case less:
2577 slt(scratch, rs, r2);
2578 addiu(scratch, scratch, -1);
2579 bgezal(scratch, offset);
2580 break;
2581 case less_equal:
2582 slt(scratch, r2, rs);
2583 addiu(scratch, scratch, -1);
2584 bltzal(scratch, offset);
2585 break;
Andrei Popescu31002712010-02-23 13:46:05 +00002586
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002587 // Unsigned comparison.
2588 case Ugreater:
2589 sltu(scratch, r2, rs);
2590 addiu(scratch, scratch, -1);
2591 bgezal(scratch, offset);
2592 break;
2593 case Ugreater_equal:
2594 sltu(scratch, rs, r2);
2595 addiu(scratch, scratch, -1);
2596 bltzal(scratch, offset);
2597 break;
2598 case Uless:
2599 sltu(scratch, rs, r2);
2600 addiu(scratch, scratch, -1);
2601 bgezal(scratch, offset);
2602 break;
2603 case Uless_equal:
2604 sltu(scratch, r2, rs);
2605 addiu(scratch, scratch, -1);
2606 bltzal(scratch, offset);
2607 break;
Andrei Popescu31002712010-02-23 13:46:05 +00002608
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002609 default:
2610 UNREACHABLE();
2611 }
2612 } else {
2613 BlockTrampolinePoolScope block_trampoline_pool(this);
2614 switch (cond) {
2615 case cc_always:
2616 bal(offset);
2617 break;
2618 case eq:
2619 bne(rs, r2, 2);
2620 nop();
2621 bal(offset);
2622 break;
2623 case ne:
2624 beq(rs, r2, 2);
2625 nop();
2626 bal(offset);
2627 break;
2628
2629 // Signed comparison.
2630 case greater:
2631 // rs > rt
2632 slt(scratch, r2, rs);
2633 beq(scratch, zero_reg, 2);
2634 nop();
2635 bal(offset);
2636 break;
2637 case greater_equal:
2638 // rs >= rt
2639 slt(scratch, rs, r2);
2640 bne(scratch, zero_reg, 2);
2641 nop();
2642 bal(offset);
2643 break;
2644 case less:
2645 // rs < r2
2646 slt(scratch, rs, r2);
2647 bne(scratch, zero_reg, 2);
2648 nop();
2649 bal(offset);
2650 break;
2651 case less_equal:
2652 // rs <= r2
2653 slt(scratch, r2, rs);
2654 bne(scratch, zero_reg, 2);
2655 nop();
2656 bal(offset);
2657 break;
2658
2659
2660 // Unsigned comparison.
2661 case Ugreater:
2662 // rs > rt
2663 sltu(scratch, r2, rs);
2664 beq(scratch, zero_reg, 2);
2665 nop();
2666 bal(offset);
2667 break;
2668 case Ugreater_equal:
2669 // rs >= rt
2670 sltu(scratch, rs, r2);
2671 bne(scratch, zero_reg, 2);
2672 nop();
2673 bal(offset);
2674 break;
2675 case Uless:
2676 // rs < r2
2677 sltu(scratch, rs, r2);
2678 bne(scratch, zero_reg, 2);
2679 nop();
2680 bal(offset);
2681 break;
2682 case Uless_equal:
2683 // rs <= r2
2684 sltu(scratch, r2, rs);
2685 bne(scratch, zero_reg, 2);
2686 nop();
2687 bal(offset);
2688 break;
2689 default:
2690 UNREACHABLE();
2691 }
Andrei Popescu31002712010-02-23 13:46:05 +00002692 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002693
Steve Block44f0eee2011-05-26 01:26:41 +01002694 // Emit a nop in the branch delay slot if required.
2695 if (bdslot == PROTECT)
2696 nop();
2697}
2698
2699
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002700void MacroAssembler::BranchAndLinkShort(Label* L, BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01002701 bal(shifted_branch_offset(L, false));
2702
2703 // Emit a nop in the branch delay slot if required.
2704 if (bdslot == PROTECT)
2705 nop();
2706}
2707
2708
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002709void MacroAssembler::BranchAndLinkShort(Label* L, Condition cond, Register rs,
2710 const Operand& rt,
2711 BranchDelaySlot bdslot) {
Steve Block44f0eee2011-05-26 01:26:41 +01002712 BRANCH_ARGS_CHECK(cond, rs, rt);
2713
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714 int32_t offset = 0;
Steve Block44f0eee2011-05-26 01:26:41 +01002715 Register r2 = no_reg;
2716 Register scratch = at;
2717 if (rt.is_reg()) {
2718 r2 = rt.rm_;
2719 } else if (cond != cc_always) {
2720 r2 = scratch;
2721 li(r2, rt);
2722 }
2723
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002724 if (!IsMipsArchVariant(kMips32r6)) {
2725 BlockTrampolinePoolScope block_trampoline_pool(this);
2726 switch (cond) {
2727 case cc_always:
2728 offset = shifted_branch_offset(L, false);
2729 bal(offset);
2730 break;
2731 case eq:
2732 bne(rs, r2, 2);
2733 nop();
2734 offset = shifted_branch_offset(L, false);
2735 bal(offset);
2736 break;
2737 case ne:
2738 beq(rs, r2, 2);
2739 nop();
2740 offset = shifted_branch_offset(L, false);
2741 bal(offset);
2742 break;
Steve Block44f0eee2011-05-26 01:26:41 +01002743
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002744 // Signed comparison.
2745 case greater:
2746 slt(scratch, r2, rs);
2747 addiu(scratch, scratch, -1);
2748 offset = shifted_branch_offset(L, false);
2749 bgezal(scratch, offset);
2750 break;
2751 case greater_equal:
2752 slt(scratch, rs, r2);
2753 addiu(scratch, scratch, -1);
2754 offset = shifted_branch_offset(L, false);
2755 bltzal(scratch, offset);
2756 break;
2757 case less:
2758 slt(scratch, rs, r2);
2759 addiu(scratch, scratch, -1);
2760 offset = shifted_branch_offset(L, false);
2761 bgezal(scratch, offset);
2762 break;
2763 case less_equal:
2764 slt(scratch, r2, rs);
2765 addiu(scratch, scratch, -1);
2766 offset = shifted_branch_offset(L, false);
2767 bltzal(scratch, offset);
2768 break;
Steve Block44f0eee2011-05-26 01:26:41 +01002769
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002770 // Unsigned comparison.
2771 case Ugreater:
2772 sltu(scratch, r2, rs);
2773 addiu(scratch, scratch, -1);
2774 offset = shifted_branch_offset(L, false);
2775 bgezal(scratch, offset);
2776 break;
2777 case Ugreater_equal:
2778 sltu(scratch, rs, r2);
2779 addiu(scratch, scratch, -1);
2780 offset = shifted_branch_offset(L, false);
2781 bltzal(scratch, offset);
2782 break;
2783 case Uless:
2784 sltu(scratch, rs, r2);
2785 addiu(scratch, scratch, -1);
2786 offset = shifted_branch_offset(L, false);
2787 bgezal(scratch, offset);
2788 break;
2789 case Uless_equal:
2790 sltu(scratch, r2, rs);
2791 addiu(scratch, scratch, -1);
2792 offset = shifted_branch_offset(L, false);
2793 bltzal(scratch, offset);
2794 break;
Steve Block44f0eee2011-05-26 01:26:41 +01002795
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002796 default:
2797 UNREACHABLE();
2798 }
2799 } else {
2800 BlockTrampolinePoolScope block_trampoline_pool(this);
2801 switch (cond) {
2802 case cc_always:
2803 offset = shifted_branch_offset(L, false);
2804 bal(offset);
2805 break;
2806 case eq:
2807 bne(rs, r2, 2);
2808 nop();
2809 offset = shifted_branch_offset(L, false);
2810 bal(offset);
2811 break;
2812 case ne:
2813 beq(rs, r2, 2);
2814 nop();
2815 offset = shifted_branch_offset(L, false);
2816 bal(offset);
2817 break;
2818
2819 // Signed comparison.
2820 case greater:
2821 // rs > rt
2822 slt(scratch, r2, rs);
2823 beq(scratch, zero_reg, 2);
2824 nop();
2825 offset = shifted_branch_offset(L, false);
2826 bal(offset);
2827 break;
2828 case greater_equal:
2829 // rs >= rt
2830 slt(scratch, rs, r2);
2831 bne(scratch, zero_reg, 2);
2832 nop();
2833 offset = shifted_branch_offset(L, false);
2834 bal(offset);
2835 break;
2836 case less:
2837 // rs < r2
2838 slt(scratch, rs, r2);
2839 bne(scratch, zero_reg, 2);
2840 nop();
2841 offset = shifted_branch_offset(L, false);
2842 bal(offset);
2843 break;
2844 case less_equal:
2845 // rs <= r2
2846 slt(scratch, r2, rs);
2847 bne(scratch, zero_reg, 2);
2848 nop();
2849 offset = shifted_branch_offset(L, false);
2850 bal(offset);
2851 break;
2852
2853
2854 // Unsigned comparison.
2855 case Ugreater:
2856 // rs > rt
2857 sltu(scratch, r2, rs);
2858 beq(scratch, zero_reg, 2);
2859 nop();
2860 offset = shifted_branch_offset(L, false);
2861 bal(offset);
2862 break;
2863 case Ugreater_equal:
2864 // rs >= rt
2865 sltu(scratch, rs, r2);
2866 bne(scratch, zero_reg, 2);
2867 nop();
2868 offset = shifted_branch_offset(L, false);
2869 bal(offset);
2870 break;
2871 case Uless:
2872 // rs < r2
2873 sltu(scratch, rs, r2);
2874 bne(scratch, zero_reg, 2);
2875 nop();
2876 offset = shifted_branch_offset(L, false);
2877 bal(offset);
2878 break;
2879 case Uless_equal:
2880 // rs <= r2
2881 sltu(scratch, r2, rs);
2882 bne(scratch, zero_reg, 2);
2883 nop();
2884 offset = shifted_branch_offset(L, false);
2885 bal(offset);
2886 break;
2887
2888 default:
2889 UNREACHABLE();
2890 }
Steve Block44f0eee2011-05-26 01:26:41 +01002891 }
2892
2893 // Check that offset could actually hold on an int16_t.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002894 DCHECK(is_int16(offset));
Steve Block44f0eee2011-05-26 01:26:41 +01002895
2896 // Emit a nop in the branch delay slot if required.
2897 if (bdslot == PROTECT)
2898 nop();
2899}
2900
2901
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002902void MacroAssembler::Jump(Register target,
Steve Block44f0eee2011-05-26 01:26:41 +01002903 Condition cond,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002904 Register rs,
2905 const Operand& rt,
2906 BranchDelaySlot bd) {
2907 BlockTrampolinePoolScope block_trampoline_pool(this);
2908 if (cond == cc_always) {
2909 jr(target);
2910 } else {
2911 BRANCH_ARGS_CHECK(cond, rs, rt);
2912 Branch(2, NegateCondition(cond), rs, rt);
2913 jr(target);
2914 }
2915 // Emit a nop in the branch delay slot if required.
2916 if (bd == PROTECT)
2917 nop();
2918}
2919
2920
2921void MacroAssembler::Jump(intptr_t target,
2922 RelocInfo::Mode rmode,
2923 Condition cond,
2924 Register rs,
2925 const Operand& rt,
2926 BranchDelaySlot bd) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002927 Label skip;
2928 if (cond != cc_always) {
2929 Branch(USE_DELAY_SLOT, &skip, NegateCondition(cond), rs, rt);
2930 }
2931 // The first instruction of 'li' may be placed in the delay slot.
2932 // This is not an issue, t9 is expected to be clobbered anyway.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002933 li(t9, Operand(target, rmode));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002934 Jump(t9, al, zero_reg, Operand(zero_reg), bd);
2935 bind(&skip);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002936}
2937
2938
2939void MacroAssembler::Jump(Address target,
2940 RelocInfo::Mode rmode,
2941 Condition cond,
2942 Register rs,
2943 const Operand& rt,
2944 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002945 DCHECK(!RelocInfo::IsCodeTarget(rmode));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002946 Jump(reinterpret_cast<intptr_t>(target), rmode, cond, rs, rt, bd);
2947}
2948
2949
2950void MacroAssembler::Jump(Handle<Code> code,
2951 RelocInfo::Mode rmode,
2952 Condition cond,
2953 Register rs,
2954 const Operand& rt,
2955 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002956 DCHECK(RelocInfo::IsCodeTarget(rmode));
2957 AllowDeferredHandleDereference embedding_raw_address;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002958 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond, rs, rt, bd);
2959}
2960
2961
2962int MacroAssembler::CallSize(Register target,
2963 Condition cond,
2964 Register rs,
2965 const Operand& rt,
2966 BranchDelaySlot bd) {
2967 int size = 0;
2968
2969 if (cond == cc_always) {
2970 size += 1;
2971 } else {
2972 size += 3;
Steve Block44f0eee2011-05-26 01:26:41 +01002973 }
2974
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002975 if (bd == PROTECT)
2976 size += 1;
Steve Block44f0eee2011-05-26 01:26:41 +01002977
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002978 return size * kInstrSize;
2979}
Steve Block44f0eee2011-05-26 01:26:41 +01002980
Steve Block44f0eee2011-05-26 01:26:41 +01002981
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002982// Note: To call gcc-compiled C code on mips, you must call thru t9.
2983void MacroAssembler::Call(Register target,
2984 Condition cond,
2985 Register rs,
2986 const Operand& rt,
2987 BranchDelaySlot bd) {
2988 BlockTrampolinePoolScope block_trampoline_pool(this);
2989 Label start;
2990 bind(&start);
2991 if (cond == cc_always) {
2992 jalr(target);
2993 } else {
2994 BRANCH_ARGS_CHECK(cond, rs, rt);
2995 Branch(2, NegateCondition(cond), rs, rt);
2996 jalr(target);
Steve Block44f0eee2011-05-26 01:26:41 +01002997 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002998 // Emit a nop in the branch delay slot if required.
2999 if (bd == PROTECT)
3000 nop();
3001
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003002 DCHECK_EQ(CallSize(target, cond, rs, rt, bd),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003003 SizeOfCodeGeneratedSince(&start));
3004}
3005
3006
3007int MacroAssembler::CallSize(Address target,
3008 RelocInfo::Mode rmode,
3009 Condition cond,
3010 Register rs,
3011 const Operand& rt,
3012 BranchDelaySlot bd) {
3013 int size = CallSize(t9, cond, rs, rt, bd);
3014 return size + 2 * kInstrSize;
3015}
3016
3017
3018void MacroAssembler::Call(Address target,
3019 RelocInfo::Mode rmode,
3020 Condition cond,
3021 Register rs,
3022 const Operand& rt,
3023 BranchDelaySlot bd) {
3024 BlockTrampolinePoolScope block_trampoline_pool(this);
3025 Label start;
3026 bind(&start);
3027 int32_t target_int = reinterpret_cast<int32_t>(target);
3028 // Must record previous source positions before the
3029 // li() generates a new code target.
3030 positions_recorder()->WriteRecordedPositions();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003031 li(t9, Operand(target_int, rmode), CONSTANT_SIZE);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003032 Call(t9, cond, rs, rt, bd);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003033 DCHECK_EQ(CallSize(target, rmode, cond, rs, rt, bd),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003034 SizeOfCodeGeneratedSince(&start));
3035}
3036
3037
3038int MacroAssembler::CallSize(Handle<Code> code,
3039 RelocInfo::Mode rmode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003040 TypeFeedbackId ast_id,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003041 Condition cond,
3042 Register rs,
3043 const Operand& rt,
3044 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003045 AllowDeferredHandleDereference using_raw_address;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003046 return CallSize(reinterpret_cast<Address>(code.location()),
3047 rmode, cond, rs, rt, bd);
3048}
3049
3050
3051void MacroAssembler::Call(Handle<Code> code,
3052 RelocInfo::Mode rmode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003053 TypeFeedbackId ast_id,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003054 Condition cond,
3055 Register rs,
3056 const Operand& rt,
3057 BranchDelaySlot bd) {
3058 BlockTrampolinePoolScope block_trampoline_pool(this);
3059 Label start;
3060 bind(&start);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003061 DCHECK(RelocInfo::IsCodeTarget(rmode));
3062 if (rmode == RelocInfo::CODE_TARGET && !ast_id.IsNone()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003063 SetRecordedAstId(ast_id);
3064 rmode = RelocInfo::CODE_TARGET_WITH_ID;
3065 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003066 AllowDeferredHandleDereference embedding_raw_address;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003067 Call(reinterpret_cast<Address>(code.location()), rmode, cond, rs, rt, bd);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003068 DCHECK_EQ(CallSize(code, rmode, ast_id, cond, rs, rt, bd),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003069 SizeOfCodeGeneratedSince(&start));
3070}
3071
3072
3073void MacroAssembler::Ret(Condition cond,
3074 Register rs,
3075 const Operand& rt,
3076 BranchDelaySlot bd) {
3077 Jump(ra, cond, rs, rt, bd);
3078}
3079
3080
3081void MacroAssembler::J(Label* L, BranchDelaySlot bdslot) {
3082 BlockTrampolinePoolScope block_trampoline_pool(this);
3083
3084 uint32_t imm28;
3085 imm28 = jump_address(L);
3086 imm28 &= kImm28Mask;
3087 { BlockGrowBufferScope block_buf_growth(this);
3088 // Buffer growth (and relocation) must be blocked for internal references
3089 // until associated instructions are emitted and available to be patched.
3090 RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
3091 j(imm28);
3092 }
3093 // Emit a nop in the branch delay slot if required.
3094 if (bdslot == PROTECT)
3095 nop();
3096}
3097
3098
3099void MacroAssembler::Jr(Label* L, BranchDelaySlot bdslot) {
3100 BlockTrampolinePoolScope block_trampoline_pool(this);
3101
3102 uint32_t imm32;
3103 imm32 = jump_address(L);
3104 { BlockGrowBufferScope block_buf_growth(this);
3105 // Buffer growth (and relocation) must be blocked for internal references
3106 // until associated instructions are emitted and available to be patched.
3107 RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
3108 lui(at, (imm32 & kHiMask) >> kLuiShift);
3109 ori(at, at, (imm32 & kImm16Mask));
3110 }
3111 jr(at);
3112
3113 // Emit a nop in the branch delay slot if required.
3114 if (bdslot == PROTECT)
3115 nop();
3116}
3117
3118
3119void MacroAssembler::Jalr(Label* L, BranchDelaySlot bdslot) {
3120 BlockTrampolinePoolScope block_trampoline_pool(this);
3121
3122 uint32_t imm32;
3123 imm32 = jump_address(L);
3124 { BlockGrowBufferScope block_buf_growth(this);
3125 // Buffer growth (and relocation) must be blocked for internal references
3126 // until associated instructions are emitted and available to be patched.
3127 RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
3128 lui(at, (imm32 & kHiMask) >> kLuiShift);
3129 ori(at, at, (imm32 & kImm16Mask));
3130 }
3131 jalr(at);
3132
3133 // Emit a nop in the branch delay slot if required.
3134 if (bdslot == PROTECT)
3135 nop();
Steve Block44f0eee2011-05-26 01:26:41 +01003136}
3137
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003138
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003139void MacroAssembler::DropAndRet(int drop) {
3140 Ret(USE_DELAY_SLOT);
3141 addiu(sp, sp, drop * kPointerSize);
3142}
Steve Block44f0eee2011-05-26 01:26:41 +01003143
3144void MacroAssembler::DropAndRet(int drop,
3145 Condition cond,
3146 Register r1,
3147 const Operand& r2) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003148 // Both Drop and Ret need to be conditional.
Steve Block44f0eee2011-05-26 01:26:41 +01003149 Label skip;
3150 if (cond != cc_always) {
3151 Branch(&skip, NegateCondition(cond), r1, r2);
3152 }
3153
3154 Drop(drop);
3155 Ret();
3156
3157 if (cond != cc_always) {
3158 bind(&skip);
3159 }
3160}
3161
3162
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003163void MacroAssembler::Drop(int count,
3164 Condition cond,
3165 Register reg,
3166 const Operand& op) {
3167 if (count <= 0) {
3168 return;
3169 }
3170
3171 Label skip;
3172
3173 if (cond != al) {
3174 Branch(&skip, NegateCondition(cond), reg, op);
3175 }
3176
3177 addiu(sp, sp, count * kPointerSize);
3178
3179 if (cond != al) {
3180 bind(&skip);
3181 }
3182}
3183
3184
3185
Steve Block44f0eee2011-05-26 01:26:41 +01003186void MacroAssembler::Swap(Register reg1,
3187 Register reg2,
3188 Register scratch) {
3189 if (scratch.is(no_reg)) {
3190 Xor(reg1, reg1, Operand(reg2));
3191 Xor(reg2, reg2, Operand(reg1));
3192 Xor(reg1, reg1, Operand(reg2));
3193 } else {
3194 mov(scratch, reg1);
3195 mov(reg1, reg2);
3196 mov(reg2, scratch);
3197 }
Andrei Popescu31002712010-02-23 13:46:05 +00003198}
3199
3200
3201void MacroAssembler::Call(Label* target) {
Steve Block44f0eee2011-05-26 01:26:41 +01003202 BranchAndLink(target);
3203}
3204
3205
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003206void MacroAssembler::Push(Handle<Object> handle) {
3207 li(at, Operand(handle));
3208 push(at);
3209}
3210
3211
Steve Block44f0eee2011-05-26 01:26:41 +01003212void MacroAssembler::DebugBreak() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003213 PrepareCEntryArgs(0);
3214 PrepareCEntryFunction(ExternalReference(Runtime::kDebugBreak, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003215 CEntryStub ces(isolate(), 1);
3216 DCHECK(AllowThisStubCall(&ces));
Steve Block44f0eee2011-05-26 01:26:41 +01003217 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
3218}
3219
Steve Block6ded16b2010-05-10 14:33:55 +01003220
Andrei Popescu31002712010-02-23 13:46:05 +00003221// ---------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00003222// Exception handling.
Andrei Popescu31002712010-02-23 13:46:05 +00003223
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003224void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
3225 int handler_index) {
Steve Block6ded16b2010-05-10 14:33:55 +01003226 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003227 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
3228 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003229 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3230 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3231 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3232 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003233
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003234 // For the JSEntry handler, we must preserve a0-a3 and s0.
3235 // t1-t3 are available. We will build up the handler from the bottom by
3236 // pushing on the stack.
3237 // Set up the code object (t1) and the state (t2) for pushing.
3238 unsigned state =
3239 StackHandler::IndexField::encode(handler_index) |
3240 StackHandler::KindField::encode(kind);
3241 li(t1, Operand(CodeObject()), CONSTANT_SIZE);
3242 li(t2, Operand(state));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003243
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003244 // Push the frame pointer, context, state, and code object.
3245 if (kind == StackHandler::JS_ENTRY) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003246 DCHECK_EQ(Smi::FromInt(0), 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003247 // The second zero_reg indicates no context.
3248 // The first zero_reg is the NULL frame pointer.
3249 // The operands are reversed to match the order of MultiPush/Pop.
3250 Push(zero_reg, zero_reg, t2, t1);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003251 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003252 MultiPush(t1.bit() | t2.bit() | cp.bit() | fp.bit());
Ben Murdoch85b71792012-04-11 18:30:58 +01003253 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003254
3255 // Link the current handler as the next handler.
3256 li(t2, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
3257 lw(t1, MemOperand(t2));
3258 push(t1);
3259 // Set this new handler as the current one.
3260 sw(sp, MemOperand(t2));
Andrei Popescu31002712010-02-23 13:46:05 +00003261}
3262
3263
3264void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003265 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003266 pop(a1);
3267 Addu(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
Ben Murdoch589d6972011-11-30 16:04:58 +00003268 li(at, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01003269 sw(a1, MemOperand(at));
Andrei Popescu31002712010-02-23 13:46:05 +00003270}
3271
3272
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003273void MacroAssembler::JumpToHandlerEntry() {
3274 // Compute the handler entry address and jump to it. The handler table is
3275 // a fixed array of (smi-tagged) code offsets.
3276 // v0 = exception, a1 = code object, a2 = state.
3277 lw(a3, FieldMemOperand(a1, Code::kHandlerTableOffset)); // Handler table.
3278 Addu(a3, a3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3279 srl(a2, a2, StackHandler::kKindWidth); // Handler index.
3280 sll(a2, a2, kPointerSizeLog2);
3281 Addu(a2, a3, a2);
3282 lw(a2, MemOperand(a2)); // Smi-tagged offset.
3283 Addu(a1, a1, Operand(Code::kHeaderSize - kHeapObjectTag)); // Code start.
3284 sra(t9, a2, kSmiTagSize);
3285 Addu(t9, t9, a1);
3286 Jump(t9); // Jump.
3287}
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003288
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003289
3290void MacroAssembler::Throw(Register value) {
Ben Murdoch85b71792012-04-11 18:30:58 +01003291 // Adjust this code if not the case.
3292 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003293 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3294 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3295 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3296 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3297 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoch85b71792012-04-11 18:30:58 +01003298
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003299 // The exception is expected in v0.
3300 Move(v0, value);
3301
3302 // Drop the stack pointer to the top of the top handler.
Ben Murdoch589d6972011-11-30 16:04:58 +00003303 li(a3, Operand(ExternalReference(Isolate::kHandlerAddress,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003304 isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00003305 lw(sp, MemOperand(a3));
3306
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003307 // Restore the next handler.
Ben Murdoch257744e2011-11-30 15:57:28 +00003308 pop(a2);
3309 sw(a2, MemOperand(a3));
Ben Murdoch257744e2011-11-30 15:57:28 +00003310
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003311 // Get the code object (a1) and state (a2). Restore the context and frame
3312 // pointer.
3313 MultiPop(a1.bit() | a2.bit() | cp.bit() | fp.bit());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003314
3315 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003316 // (kind == ENTRY) == (fp == 0) == (cp == 0), so we could test either fp
3317 // or cp.
Ben Murdoch257744e2011-11-30 15:57:28 +00003318 Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003319 Branch(&done, eq, cp, Operand(zero_reg));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003320 sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003321 bind(&done);
3322
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003323 JumpToHandlerEntry();
Ben Murdoch257744e2011-11-30 15:57:28 +00003324}
3325
3326
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003327void MacroAssembler::ThrowUncatchable(Register value) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003328 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003329 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
3330 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003331 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3332 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3333 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3334 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00003335
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003336 // The exception is expected in v0.
3337 if (!value.is(v0)) {
3338 mov(v0, value);
3339 }
3340 // Drop the stack pointer to the top of the top stack handler.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003341 li(a3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
3342 lw(sp, MemOperand(a3));
Ben Murdoch257744e2011-11-30 15:57:28 +00003343
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003344 // Unwind the handlers until the ENTRY handler is found.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003345 Label fetch_next, check_kind;
3346 jmp(&check_kind);
3347 bind(&fetch_next);
3348 lw(sp, MemOperand(sp, StackHandlerConstants::kNextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003349
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003350 bind(&check_kind);
3351 STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
3352 lw(a2, MemOperand(sp, StackHandlerConstants::kStateOffset));
3353 And(a2, a2, Operand(StackHandler::KindField::kMask));
3354 Branch(&fetch_next, ne, a2, Operand(zero_reg));
3355
3356 // Set the top handler address to next handler past the top ENTRY handler.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003357 pop(a2);
3358 sw(a2, MemOperand(a3));
Ben Murdoch257744e2011-11-30 15:57:28 +00003359
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003360 // Get the code object (a1) and state (a2). Clear the context and frame
3361 // pointer (0 was saved in the handler).
3362 MultiPop(a1.bit() | a2.bit() | cp.bit() | fp.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00003363
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003364 JumpToHandlerEntry();
Ben Murdoch257744e2011-11-30 15:57:28 +00003365}
3366
3367
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003368void MacroAssembler::Allocate(int object_size,
3369 Register result,
3370 Register scratch1,
3371 Register scratch2,
3372 Label* gc_required,
3373 AllocationFlags flags) {
3374 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
Steve Block44f0eee2011-05-26 01:26:41 +01003375 if (!FLAG_inline_new) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003376 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003377 // Trash the registers to simulate an allocation failure.
3378 li(result, 0x7091);
3379 li(scratch1, 0x7191);
3380 li(scratch2, 0x7291);
3381 }
3382 jmp(gc_required);
3383 return;
Steve Block6ded16b2010-05-10 14:33:55 +01003384 }
3385
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003386 DCHECK(!result.is(scratch1));
3387 DCHECK(!result.is(scratch2));
3388 DCHECK(!scratch1.is(scratch2));
3389 DCHECK(!scratch1.is(t9));
3390 DCHECK(!scratch2.is(t9));
3391 DCHECK(!result.is(t9));
Steve Block6ded16b2010-05-10 14:33:55 +01003392
Steve Block44f0eee2011-05-26 01:26:41 +01003393 // Make object size into bytes.
3394 if ((flags & SIZE_IN_WORDS) != 0) {
3395 object_size *= kPointerSize;
3396 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003397 DCHECK_EQ(0, object_size & kObjectAlignmentMask);
Steve Block6ded16b2010-05-10 14:33:55 +01003398
Steve Block44f0eee2011-05-26 01:26:41 +01003399 // Check relative positions of allocation top and limit addresses.
3400 // ARM adds additional checks to make sure the ldm instruction can be
3401 // used. On MIPS we don't have ldm so we don't need additional checks either.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003402 ExternalReference allocation_top =
3403 AllocationUtils::GetAllocationTopReference(isolate(), flags);
3404 ExternalReference allocation_limit =
3405 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
3406
Steve Block44f0eee2011-05-26 01:26:41 +01003407 intptr_t top =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003408 reinterpret_cast<intptr_t>(allocation_top.address());
Steve Block44f0eee2011-05-26 01:26:41 +01003409 intptr_t limit =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003410 reinterpret_cast<intptr_t>(allocation_limit.address());
3411 DCHECK((limit - top) == kPointerSize);
Steve Block44f0eee2011-05-26 01:26:41 +01003412
3413 // Set up allocation top address and object size registers.
3414 Register topaddr = scratch1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003415 li(topaddr, Operand(allocation_top));
Steve Block44f0eee2011-05-26 01:26:41 +01003416
3417 // This code stores a temporary value in t9.
3418 if ((flags & RESULT_CONTAINS_TOP) == 0) {
3419 // Load allocation top into result and allocation limit into t9.
3420 lw(result, MemOperand(topaddr));
3421 lw(t9, MemOperand(topaddr, kPointerSize));
3422 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00003423 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003424 // Assert that result actually contains top on entry. t9 is used
3425 // immediately below so this use of t9 does not cause difference with
3426 // respect to register content between debug and release mode.
3427 lw(t9, MemOperand(topaddr));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003428 Check(eq, kUnexpectedAllocationTop, result, Operand(t9));
Steve Block44f0eee2011-05-26 01:26:41 +01003429 }
3430 // Load allocation limit into t9. Result already contains allocation top.
3431 lw(t9, MemOperand(topaddr, limit - top));
3432 }
3433
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003434 if ((flags & DOUBLE_ALIGNMENT) != 0) {
3435 // Align the next allocation. Storing the filler map without checking top is
3436 // safe in new-space because the limit of the heap is aligned there.
3437 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
3438 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
3439 And(scratch2, result, Operand(kDoubleAlignmentMask));
3440 Label aligned;
3441 Branch(&aligned, eq, scratch2, Operand(zero_reg));
3442 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
3443 Branch(gc_required, Ugreater_equal, result, Operand(t9));
3444 }
3445 li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
3446 sw(scratch2, MemOperand(result));
3447 Addu(result, result, Operand(kDoubleSize / 2));
3448 bind(&aligned);
3449 }
3450
Steve Block44f0eee2011-05-26 01:26:41 +01003451 // Calculate new top and bail out if new space is exhausted. Use result
3452 // to calculate the new top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003453 Addu(scratch2, result, Operand(object_size));
Steve Block44f0eee2011-05-26 01:26:41 +01003454 Branch(gc_required, Ugreater, scratch2, Operand(t9));
3455 sw(scratch2, MemOperand(topaddr));
3456
3457 // Tag object if requested.
3458 if ((flags & TAG_OBJECT) != 0) {
3459 Addu(result, result, Operand(kHeapObjectTag));
3460 }
Steve Block6ded16b2010-05-10 14:33:55 +01003461}
3462
3463
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003464void MacroAssembler::Allocate(Register object_size,
3465 Register result,
3466 Register scratch1,
3467 Register scratch2,
3468 Label* gc_required,
3469 AllocationFlags flags) {
Steve Block44f0eee2011-05-26 01:26:41 +01003470 if (!FLAG_inline_new) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003471 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003472 // Trash the registers to simulate an allocation failure.
3473 li(result, 0x7091);
3474 li(scratch1, 0x7191);
3475 li(scratch2, 0x7291);
3476 }
3477 jmp(gc_required);
3478 return;
3479 }
3480
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003481 DCHECK(!result.is(scratch1));
3482 DCHECK(!result.is(scratch2));
3483 DCHECK(!scratch1.is(scratch2));
3484 DCHECK(!object_size.is(t9));
3485 DCHECK(!scratch1.is(t9) && !scratch2.is(t9) && !result.is(t9));
Steve Block44f0eee2011-05-26 01:26:41 +01003486
3487 // Check relative positions of allocation top and limit addresses.
3488 // ARM adds additional checks to make sure the ldm instruction can be
3489 // used. On MIPS we don't have ldm so we don't need additional checks either.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003490 ExternalReference allocation_top =
3491 AllocationUtils::GetAllocationTopReference(isolate(), flags);
3492 ExternalReference allocation_limit =
3493 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
Steve Block44f0eee2011-05-26 01:26:41 +01003494 intptr_t top =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003495 reinterpret_cast<intptr_t>(allocation_top.address());
Steve Block44f0eee2011-05-26 01:26:41 +01003496 intptr_t limit =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003497 reinterpret_cast<intptr_t>(allocation_limit.address());
3498 DCHECK((limit - top) == kPointerSize);
Steve Block44f0eee2011-05-26 01:26:41 +01003499
3500 // Set up allocation top address and object size registers.
3501 Register topaddr = scratch1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003502 li(topaddr, Operand(allocation_top));
Steve Block44f0eee2011-05-26 01:26:41 +01003503
3504 // This code stores a temporary value in t9.
3505 if ((flags & RESULT_CONTAINS_TOP) == 0) {
3506 // Load allocation top into result and allocation limit into t9.
3507 lw(result, MemOperand(topaddr));
3508 lw(t9, MemOperand(topaddr, kPointerSize));
3509 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00003510 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003511 // Assert that result actually contains top on entry. t9 is used
3512 // immediately below so this use of t9 does not cause difference with
3513 // respect to register content between debug and release mode.
3514 lw(t9, MemOperand(topaddr));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003515 Check(eq, kUnexpectedAllocationTop, result, Operand(t9));
Steve Block44f0eee2011-05-26 01:26:41 +01003516 }
3517 // Load allocation limit into t9. Result already contains allocation top.
3518 lw(t9, MemOperand(topaddr, limit - top));
3519 }
3520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003521 if ((flags & DOUBLE_ALIGNMENT) != 0) {
3522 // Align the next allocation. Storing the filler map without checking top is
3523 // safe in new-space because the limit of the heap is aligned there.
3524 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
3525 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
3526 And(scratch2, result, Operand(kDoubleAlignmentMask));
3527 Label aligned;
3528 Branch(&aligned, eq, scratch2, Operand(zero_reg));
3529 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
3530 Branch(gc_required, Ugreater_equal, result, Operand(t9));
3531 }
3532 li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
3533 sw(scratch2, MemOperand(result));
3534 Addu(result, result, Operand(kDoubleSize / 2));
3535 bind(&aligned);
3536 }
3537
Steve Block44f0eee2011-05-26 01:26:41 +01003538 // Calculate new top and bail out if new space is exhausted. Use result
3539 // to calculate the new top. Object size may be in words so a shift is
3540 // required to get the number of bytes.
3541 if ((flags & SIZE_IN_WORDS) != 0) {
3542 sll(scratch2, object_size, kPointerSizeLog2);
3543 Addu(scratch2, result, scratch2);
3544 } else {
3545 Addu(scratch2, result, Operand(object_size));
3546 }
3547 Branch(gc_required, Ugreater, scratch2, Operand(t9));
3548
3549 // Update allocation top. result temporarily holds the new top.
Ben Murdoch257744e2011-11-30 15:57:28 +00003550 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003551 And(t9, scratch2, Operand(kObjectAlignmentMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003552 Check(eq, kUnalignedAllocationInNewSpace, t9, Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +01003553 }
3554 sw(scratch2, MemOperand(topaddr));
3555
3556 // Tag object if requested.
3557 if ((flags & TAG_OBJECT) != 0) {
3558 Addu(result, result, Operand(kHeapObjectTag));
3559 }
3560}
3561
3562
3563void MacroAssembler::UndoAllocationInNewSpace(Register object,
3564 Register scratch) {
3565 ExternalReference new_space_allocation_top =
3566 ExternalReference::new_space_allocation_top_address(isolate());
3567
3568 // Make sure the object has no tag before resetting top.
3569 And(object, object, Operand(~kHeapObjectTagMask));
3570#ifdef DEBUG
3571 // Check that the object un-allocated is below the current top.
3572 li(scratch, Operand(new_space_allocation_top));
3573 lw(scratch, MemOperand(scratch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003574 Check(less, kUndoAllocationOfNonAllocatedMemory,
Steve Block44f0eee2011-05-26 01:26:41 +01003575 object, Operand(scratch));
3576#endif
3577 // Write the address of the object to un-allocate as the current top.
3578 li(scratch, Operand(new_space_allocation_top));
3579 sw(object, MemOperand(scratch));
3580}
3581
3582
3583void MacroAssembler::AllocateTwoByteString(Register result,
3584 Register length,
3585 Register scratch1,
3586 Register scratch2,
3587 Register scratch3,
3588 Label* gc_required) {
3589 // Calculate the number of bytes needed for the characters in the string while
3590 // observing object alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003591 DCHECK((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003592 sll(scratch1, length, 1); // Length in bytes, not chars.
3593 addiu(scratch1, scratch1,
3594 kObjectAlignmentMask + SeqTwoByteString::kHeaderSize);
3595 And(scratch1, scratch1, Operand(~kObjectAlignmentMask));
3596
3597 // Allocate two-byte string in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003598 Allocate(scratch1,
3599 result,
3600 scratch2,
3601 scratch3,
3602 gc_required,
3603 TAG_OBJECT);
Steve Block44f0eee2011-05-26 01:26:41 +01003604
3605 // Set the map, length and hash field.
3606 InitializeNewString(result,
3607 length,
3608 Heap::kStringMapRootIndex,
3609 scratch1,
3610 scratch2);
3611}
3612
3613
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003614void MacroAssembler::AllocateOneByteString(Register result, Register length,
3615 Register scratch1, Register scratch2,
3616 Register scratch3,
3617 Label* gc_required) {
Steve Block44f0eee2011-05-26 01:26:41 +01003618 // Calculate the number of bytes needed for the characters in the string
3619 // while observing object alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003620 DCHECK((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3621 DCHECK(kCharSize == 1);
3622 addiu(scratch1, length, kObjectAlignmentMask + SeqOneByteString::kHeaderSize);
Steve Block44f0eee2011-05-26 01:26:41 +01003623 And(scratch1, scratch1, Operand(~kObjectAlignmentMask));
3624
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003625 // Allocate one-byte string in new space.
3626 Allocate(scratch1,
3627 result,
3628 scratch2,
3629 scratch3,
3630 gc_required,
3631 TAG_OBJECT);
Steve Block44f0eee2011-05-26 01:26:41 +01003632
3633 // Set the map, length and hash field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003634 InitializeNewString(result, length, Heap::kOneByteStringMapRootIndex,
3635 scratch1, scratch2);
Steve Block44f0eee2011-05-26 01:26:41 +01003636}
3637
3638
3639void MacroAssembler::AllocateTwoByteConsString(Register result,
3640 Register length,
3641 Register scratch1,
3642 Register scratch2,
3643 Label* gc_required) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003644 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
3645 TAG_OBJECT);
Steve Block44f0eee2011-05-26 01:26:41 +01003646 InitializeNewString(result,
3647 length,
3648 Heap::kConsStringMapRootIndex,
3649 scratch1,
3650 scratch2);
3651}
3652
3653
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003654void MacroAssembler::AllocateOneByteConsString(Register result, Register length,
3655 Register scratch1,
3656 Register scratch2,
3657 Label* gc_required) {
3658 Allocate(ConsString::kSize,
3659 result,
3660 scratch1,
3661 scratch2,
3662 gc_required,
3663 TAG_OBJECT);
3664
3665 InitializeNewString(result, length, Heap::kConsOneByteStringMapRootIndex,
3666 scratch1, scratch2);
Steve Block44f0eee2011-05-26 01:26:41 +01003667}
3668
3669
Ben Murdoch589d6972011-11-30 16:04:58 +00003670void MacroAssembler::AllocateTwoByteSlicedString(Register result,
3671 Register length,
3672 Register scratch1,
3673 Register scratch2,
3674 Label* gc_required) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003675 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
3676 TAG_OBJECT);
Ben Murdoch589d6972011-11-30 16:04:58 +00003677
3678 InitializeNewString(result,
3679 length,
3680 Heap::kSlicedStringMapRootIndex,
3681 scratch1,
3682 scratch2);
3683}
3684
3685
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003686void MacroAssembler::AllocateOneByteSlicedString(Register result,
3687 Register length,
3688 Register scratch1,
3689 Register scratch2,
3690 Label* gc_required) {
3691 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
3692 TAG_OBJECT);
Ben Murdoch589d6972011-11-30 16:04:58 +00003693
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003694 InitializeNewString(result, length, Heap::kSlicedOneByteStringMapRootIndex,
3695 scratch1, scratch2);
3696}
3697
3698
3699void MacroAssembler::JumpIfNotUniqueNameInstanceType(Register reg,
3700 Label* not_unique_name) {
3701 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3702 Label succeed;
3703 And(at, reg, Operand(kIsNotStringMask | kIsNotInternalizedMask));
3704 Branch(&succeed, eq, at, Operand(zero_reg));
3705 Branch(not_unique_name, ne, reg, Operand(SYMBOL_TYPE));
3706
3707 bind(&succeed);
Ben Murdoch589d6972011-11-30 16:04:58 +00003708}
3709
3710
Steve Block44f0eee2011-05-26 01:26:41 +01003711// Allocates a heap number or jumps to the label if the young space is full and
3712// a scavenge is needed.
3713void MacroAssembler::AllocateHeapNumber(Register result,
3714 Register scratch1,
3715 Register scratch2,
3716 Register heap_number_map,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003717 Label* need_gc,
3718 TaggingMode tagging_mode,
3719 MutableMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01003720 // Allocate an object in the heap for the heap number and tag it as a heap
3721 // object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003722 Allocate(HeapNumber::kSize, result, scratch1, scratch2, need_gc,
3723 tagging_mode == TAG_RESULT ? TAG_OBJECT : NO_ALLOCATION_FLAGS);
3724
3725 Heap::RootListIndex map_index = mode == MUTABLE
3726 ? Heap::kMutableHeapNumberMapRootIndex
3727 : Heap::kHeapNumberMapRootIndex;
3728 AssertIsRoot(heap_number_map, map_index);
Steve Block44f0eee2011-05-26 01:26:41 +01003729
3730 // Store heap number map in the allocated object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003731 if (tagging_mode == TAG_RESULT) {
3732 sw(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
3733 } else {
3734 sw(heap_number_map, MemOperand(result, HeapObject::kMapOffset));
3735 }
Steve Block44f0eee2011-05-26 01:26:41 +01003736}
3737
3738
3739void MacroAssembler::AllocateHeapNumberWithValue(Register result,
3740 FPURegister value,
3741 Register scratch1,
3742 Register scratch2,
3743 Label* gc_required) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003744 LoadRoot(t8, Heap::kHeapNumberMapRootIndex);
3745 AllocateHeapNumber(result, scratch1, scratch2, t8, gc_required);
Steve Block44f0eee2011-05-26 01:26:41 +01003746 sdc1(value, FieldMemOperand(result, HeapNumber::kValueOffset));
3747}
3748
3749
3750// Copies a fixed number of fields of heap objects from src to dst.
3751void MacroAssembler::CopyFields(Register dst,
3752 Register src,
3753 RegList temps,
3754 int field_count) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003755 DCHECK((temps & dst.bit()) == 0);
3756 DCHECK((temps & src.bit()) == 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003757 // Primitive implementation using only one temporary register.
3758
3759 Register tmp = no_reg;
3760 // Find a temp register in temps list.
3761 for (int i = 0; i < kNumRegisters; i++) {
3762 if ((temps & (1 << i)) != 0) {
3763 tmp.code_ = i;
3764 break;
3765 }
3766 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003767 DCHECK(!tmp.is(no_reg));
Steve Block44f0eee2011-05-26 01:26:41 +01003768
3769 for (int i = 0; i < field_count; i++) {
3770 lw(tmp, FieldMemOperand(src, i * kPointerSize));
3771 sw(tmp, FieldMemOperand(dst, i * kPointerSize));
3772 }
3773}
3774
3775
Ben Murdoch257744e2011-11-30 15:57:28 +00003776void MacroAssembler::CopyBytes(Register src,
3777 Register dst,
3778 Register length,
3779 Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003780 Label align_loop_1, word_loop, byte_loop, byte_loop_1, done;
Ben Murdoch257744e2011-11-30 15:57:28 +00003781
3782 // Align src before copying in word size chunks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003783 Branch(&byte_loop, le, length, Operand(kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003784 bind(&align_loop_1);
3785 And(scratch, src, kPointerSize - 1);
3786 Branch(&word_loop, eq, scratch, Operand(zero_reg));
3787 lbu(scratch, MemOperand(src));
3788 Addu(src, src, 1);
3789 sb(scratch, MemOperand(dst));
3790 Addu(dst, dst, 1);
3791 Subu(length, length, Operand(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003792 Branch(&align_loop_1, ne, length, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00003793
3794 // Copy bytes in word size chunks.
3795 bind(&word_loop);
3796 if (emit_debug_code()) {
3797 And(scratch, src, kPointerSize - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003798 Assert(eq, kExpectingAlignmentForCopyBytes,
Ben Murdoch257744e2011-11-30 15:57:28 +00003799 scratch, Operand(zero_reg));
3800 }
3801 Branch(&byte_loop, lt, length, Operand(kPointerSize));
3802 lw(scratch, MemOperand(src));
3803 Addu(src, src, kPointerSize);
3804
3805 // TODO(kalmard) check if this can be optimized to use sw in most cases.
3806 // Can't use unaligned access - copy byte by byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003807 if (kArchEndian == kLittle) {
3808 sb(scratch, MemOperand(dst, 0));
3809 srl(scratch, scratch, 8);
3810 sb(scratch, MemOperand(dst, 1));
3811 srl(scratch, scratch, 8);
3812 sb(scratch, MemOperand(dst, 2));
3813 srl(scratch, scratch, 8);
3814 sb(scratch, MemOperand(dst, 3));
3815 } else {
3816 sb(scratch, MemOperand(dst, 3));
3817 srl(scratch, scratch, 8);
3818 sb(scratch, MemOperand(dst, 2));
3819 srl(scratch, scratch, 8);
3820 sb(scratch, MemOperand(dst, 1));
3821 srl(scratch, scratch, 8);
3822 sb(scratch, MemOperand(dst, 0));
3823 }
3824
Ben Murdoch257744e2011-11-30 15:57:28 +00003825 Addu(dst, dst, 4);
3826
3827 Subu(length, length, Operand(kPointerSize));
3828 Branch(&word_loop);
3829
3830 // Copy the last bytes if any left.
3831 bind(&byte_loop);
3832 Branch(&done, eq, length, Operand(zero_reg));
3833 bind(&byte_loop_1);
3834 lbu(scratch, MemOperand(src));
3835 Addu(src, src, 1);
3836 sb(scratch, MemOperand(dst));
3837 Addu(dst, dst, 1);
3838 Subu(length, length, Operand(1));
3839 Branch(&byte_loop_1, ne, length, Operand(zero_reg));
3840 bind(&done);
3841}
3842
3843
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003844void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
3845 Register end_offset,
3846 Register filler) {
3847 Label loop, entry;
3848 Branch(&entry);
3849 bind(&loop);
3850 sw(filler, MemOperand(start_offset));
3851 Addu(start_offset, start_offset, kPointerSize);
3852 bind(&entry);
3853 Branch(&loop, lt, start_offset, Operand(end_offset));
3854}
3855
3856
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003857void MacroAssembler::CheckFastElements(Register map,
3858 Register scratch,
3859 Label* fail) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003860 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3861 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3862 STATIC_ASSERT(FAST_ELEMENTS == 2);
3863 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003864 lbu(scratch, FieldMemOperand(map, Map::kBitField2Offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003865 Branch(fail, hi, scratch,
3866 Operand(Map::kMaximumBitField2FastHoleyElementValue));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003867}
3868
3869
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003870void MacroAssembler::CheckFastObjectElements(Register map,
3871 Register scratch,
3872 Label* fail) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003873 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3874 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3875 STATIC_ASSERT(FAST_ELEMENTS == 2);
3876 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003877 lbu(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3878 Branch(fail, ls, scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003879 Operand(Map::kMaximumBitField2FastHoleySmiElementValue));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003880 Branch(fail, hi, scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003881 Operand(Map::kMaximumBitField2FastHoleyElementValue));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003882}
3883
3884
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003885void MacroAssembler::CheckFastSmiElements(Register map,
3886 Register scratch,
3887 Label* fail) {
3888 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3889 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003890 lbu(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3891 Branch(fail, hi, scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003892 Operand(Map::kMaximumBitField2FastHoleySmiElementValue));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003893}
3894
3895
3896void MacroAssembler::StoreNumberToDoubleElements(Register value_reg,
3897 Register key_reg,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003898 Register elements_reg,
3899 Register scratch1,
3900 Register scratch2,
3901 Register scratch3,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003902 Label* fail,
3903 int elements_offset) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003904 Label smi_value, maybe_nan, have_double_value, is_nan, done;
3905 Register mantissa_reg = scratch2;
3906 Register exponent_reg = scratch3;
3907
3908 // Handle smi values specially.
3909 JumpIfSmi(value_reg, &smi_value);
3910
3911 // Ensure that the object is a heap number
3912 CheckMap(value_reg,
3913 scratch1,
3914 Heap::kHeapNumberMapRootIndex,
3915 fail,
3916 DONT_DO_SMI_CHECK);
3917
3918 // Check for nan: all NaN values have a value greater (signed) than 0x7ff00000
3919 // in the exponent.
3920 li(scratch1, Operand(kNaNOrInfinityLowerBoundUpper32));
3921 lw(exponent_reg, FieldMemOperand(value_reg, HeapNumber::kExponentOffset));
3922 Branch(&maybe_nan, ge, exponent_reg, Operand(scratch1));
3923
3924 lw(mantissa_reg, FieldMemOperand(value_reg, HeapNumber::kMantissaOffset));
3925
3926 bind(&have_double_value);
3927 sll(scratch1, key_reg, kDoubleSizeLog2 - kSmiTagSize);
3928 Addu(scratch1, scratch1, elements_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003929 sw(mantissa_reg,
3930 FieldMemOperand(scratch1, FixedDoubleArray::kHeaderSize - elements_offset
3931 + kHoleNanLower32Offset));
3932 sw(exponent_reg,
3933 FieldMemOperand(scratch1, FixedDoubleArray::kHeaderSize - elements_offset
3934 + kHoleNanUpper32Offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003935 jmp(&done);
3936
3937 bind(&maybe_nan);
3938 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
3939 // it's an Infinity, and the non-NaN code path applies.
3940 Branch(&is_nan, gt, exponent_reg, Operand(scratch1));
3941 lw(mantissa_reg, FieldMemOperand(value_reg, HeapNumber::kMantissaOffset));
3942 Branch(&have_double_value, eq, mantissa_reg, Operand(zero_reg));
3943 bind(&is_nan);
3944 // Load canonical NaN for storing into the double array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003945 LoadRoot(at, Heap::kNanValueRootIndex);
3946 lw(mantissa_reg, FieldMemOperand(at, HeapNumber::kMantissaOffset));
3947 lw(exponent_reg, FieldMemOperand(at, HeapNumber::kExponentOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003948 jmp(&have_double_value);
3949
3950 bind(&smi_value);
3951 Addu(scratch1, elements_reg,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003952 Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag -
3953 elements_offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003954 sll(scratch2, key_reg, kDoubleSizeLog2 - kSmiTagSize);
3955 Addu(scratch1, scratch1, scratch2);
3956 // scratch1 is now effective address of the double element
3957
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003958 Register untagged_value = elements_reg;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003959 SmiUntag(untagged_value, value_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003960 mtc1(untagged_value, f2);
3961 cvt_d_w(f0, f2);
3962 sdc1(f0, MemOperand(scratch1, 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003963 bind(&done);
3964}
3965
3966
3967void MacroAssembler::CompareMapAndBranch(Register obj,
3968 Register scratch,
3969 Handle<Map> map,
3970 Label* early_success,
3971 Condition cond,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003972 Label* branch_to) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003973 lw(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003974 CompareMapAndBranch(scratch, map, early_success, cond, branch_to);
3975}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003976
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003977
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003978void MacroAssembler::CompareMapAndBranch(Register obj_map,
3979 Handle<Map> map,
3980 Label* early_success,
3981 Condition cond,
3982 Label* branch_to) {
3983 Branch(branch_to, cond, obj_map, Operand(map));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003984}
3985
3986
Steve Block44f0eee2011-05-26 01:26:41 +01003987void MacroAssembler::CheckMap(Register obj,
3988 Register scratch,
3989 Handle<Map> map,
3990 Label* fail,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003991 SmiCheckType smi_check_type) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003992 if (smi_check_type == DO_SMI_CHECK) {
Steve Block44f0eee2011-05-26 01:26:41 +01003993 JumpIfSmi(obj, fail);
3994 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003995 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003996 CompareMapAndBranch(obj, scratch, map, &success, ne, fail);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003997 bind(&success);
Steve Block44f0eee2011-05-26 01:26:41 +01003998}
3999
4000
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004001void MacroAssembler::DispatchWeakMap(Register obj, Register scratch1,
4002 Register scratch2, Handle<WeakCell> cell,
4003 Handle<Code> success,
4004 SmiCheckType smi_check_type) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004005 Label fail;
4006 if (smi_check_type == DO_SMI_CHECK) {
4007 JumpIfSmi(obj, &fail);
4008 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004009 lw(scratch1, FieldMemOperand(obj, HeapObject::kMapOffset));
4010 GetWeakValue(scratch2, cell);
4011 Jump(success, RelocInfo::CODE_TARGET, eq, scratch1, Operand(scratch2));
Ben Murdoch257744e2011-11-30 15:57:28 +00004012 bind(&fail);
4013}
4014
4015
Steve Block44f0eee2011-05-26 01:26:41 +01004016void MacroAssembler::CheckMap(Register obj,
4017 Register scratch,
4018 Heap::RootListIndex index,
4019 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00004020 SmiCheckType smi_check_type) {
4021 if (smi_check_type == DO_SMI_CHECK) {
Steve Block44f0eee2011-05-26 01:26:41 +01004022 JumpIfSmi(obj, fail);
4023 }
4024 lw(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
4025 LoadRoot(at, index);
4026 Branch(fail, ne, scratch, Operand(at));
Steve Block6ded16b2010-05-10 14:33:55 +01004027}
4028
4029
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004030void MacroAssembler::GetWeakValue(Register value, Handle<WeakCell> cell) {
4031 li(value, Operand(cell));
4032 lw(value, FieldMemOperand(value, WeakCell::kValueOffset));
4033}
4034
4035
4036void MacroAssembler::LoadWeakValue(Register value, Handle<WeakCell> cell,
4037 Label* miss) {
4038 GetWeakValue(value, cell);
4039 JumpIfSmi(value, miss);
4040}
4041
4042
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004043void MacroAssembler::MovFromFloatResult(DoubleRegister dst) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004044 if (IsMipsSoftFloatABI) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004045 if (kArchEndian == kLittle) {
4046 Move(dst, v0, v1);
4047 } else {
4048 Move(dst, v1, v0);
4049 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004050 } else {
4051 Move(dst, f0); // Reg f0 is o32 ABI FP return value.
4052 }
4053}
4054
4055
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004056void MacroAssembler::MovFromFloatParameter(DoubleRegister dst) {
4057 if (IsMipsSoftFloatABI) {
4058 if (kArchEndian == kLittle) {
4059 Move(dst, a0, a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00004060 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004061 Move(dst, a1, a0);
Ben Murdoch257744e2011-11-30 15:57:28 +00004062 }
4063 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004064 Move(dst, f12); // Reg f12 is o32 ABI FP first argument value.
Ben Murdoch257744e2011-11-30 15:57:28 +00004065 }
4066}
4067
4068
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004069void MacroAssembler::MovToFloatParameter(DoubleRegister src) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004070 if (!IsMipsSoftFloatABI) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004071 Move(f12, src);
Ben Murdoch257744e2011-11-30 15:57:28 +00004072 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004073 if (kArchEndian == kLittle) {
4074 Move(a0, a1, src);
4075 } else {
4076 Move(a1, a0, src);
4077 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004078 }
4079}
4080
4081
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004082void MacroAssembler::MovToFloatResult(DoubleRegister src) {
4083 if (!IsMipsSoftFloatABI) {
4084 Move(f0, src);
Ben Murdoch257744e2011-11-30 15:57:28 +00004085 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004086 if (kArchEndian == kLittle) {
4087 Move(v0, v1, src);
4088 } else {
4089 Move(v1, v0, src);
4090 }
4091 }
4092}
4093
4094
4095void MacroAssembler::MovToFloatParameters(DoubleRegister src1,
4096 DoubleRegister src2) {
4097 if (!IsMipsSoftFloatABI) {
4098 if (src2.is(f12)) {
4099 DCHECK(!src1.is(f14));
4100 Move(f14, src2);
4101 Move(f12, src1);
4102 } else {
4103 Move(f12, src1);
4104 Move(f14, src2);
4105 }
4106 } else {
4107 if (kArchEndian == kLittle) {
4108 Move(a0, a1, src1);
4109 Move(a2, a3, src2);
4110 } else {
4111 Move(a1, a0, src1);
4112 Move(a3, a2, src2);
4113 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004114 }
4115}
4116
4117
Steve Block6ded16b2010-05-10 14:33:55 +01004118// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00004119// JavaScript invokes.
Steve Block6ded16b2010-05-10 14:33:55 +01004120
4121void MacroAssembler::InvokePrologue(const ParameterCount& expected,
4122 const ParameterCount& actual,
4123 Handle<Code> code_constant,
4124 Register code_reg,
4125 Label* done,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004126 bool* definitely_mismatches,
Steve Block44f0eee2011-05-26 01:26:41 +01004127 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004128 const CallWrapper& call_wrapper) {
Steve Block6ded16b2010-05-10 14:33:55 +01004129 bool definitely_matches = false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004130 *definitely_mismatches = false;
Steve Block6ded16b2010-05-10 14:33:55 +01004131 Label regular_invoke;
4132
4133 // Check whether the expected and actual arguments count match. If not,
4134 // setup registers according to contract with ArgumentsAdaptorTrampoline:
4135 // a0: actual arguments count
4136 // a1: function (passed through to callee)
4137 // a2: expected arguments count
Steve Block6ded16b2010-05-10 14:33:55 +01004138
4139 // The code below is made a lot easier because the calling code already sets
4140 // up actual and expected registers according to the contract if values are
4141 // passed in registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004142 DCHECK(actual.is_immediate() || actual.reg().is(a0));
4143 DCHECK(expected.is_immediate() || expected.reg().is(a2));
4144 DCHECK((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(a3));
Steve Block6ded16b2010-05-10 14:33:55 +01004145
4146 if (expected.is_immediate()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004147 DCHECK(actual.is_immediate());
Steve Block6ded16b2010-05-10 14:33:55 +01004148 if (expected.immediate() == actual.immediate()) {
4149 definitely_matches = true;
4150 } else {
4151 li(a0, Operand(actual.immediate()));
4152 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
4153 if (expected.immediate() == sentinel) {
4154 // Don't worry about adapting arguments for builtins that
4155 // don't want that done. Skip adaption code by making it look
4156 // like we have a match between expected and actual number of
4157 // arguments.
4158 definitely_matches = true;
4159 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004160 *definitely_mismatches = true;
Steve Block6ded16b2010-05-10 14:33:55 +01004161 li(a2, Operand(expected.immediate()));
4162 }
4163 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004164 } else if (actual.is_immediate()) {
4165 Branch(&regular_invoke, eq, expected.reg(), Operand(actual.immediate()));
4166 li(a0, Operand(actual.immediate()));
Steve Block6ded16b2010-05-10 14:33:55 +01004167 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00004168 Branch(&regular_invoke, eq, expected.reg(), Operand(actual.reg()));
Steve Block6ded16b2010-05-10 14:33:55 +01004169 }
4170
4171 if (!definitely_matches) {
4172 if (!code_constant.is_null()) {
4173 li(a3, Operand(code_constant));
4174 addiu(a3, a3, Code::kHeaderSize - kHeapObjectTag);
4175 }
4176
Steve Block44f0eee2011-05-26 01:26:41 +01004177 Handle<Code> adaptor =
4178 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Block6ded16b2010-05-10 14:33:55 +01004179 if (flag == CALL_FUNCTION) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004180 call_wrapper.BeforeCall(CallSize(adaptor));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004181 Call(adaptor);
Ben Murdoch257744e2011-11-30 15:57:28 +00004182 call_wrapper.AfterCall();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004183 if (!*definitely_mismatches) {
4184 Branch(done);
4185 }
Steve Block6ded16b2010-05-10 14:33:55 +01004186 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004187 Jump(adaptor, RelocInfo::CODE_TARGET);
Steve Block6ded16b2010-05-10 14:33:55 +01004188 }
4189 bind(&regular_invoke);
4190 }
4191}
4192
Steve Block44f0eee2011-05-26 01:26:41 +01004193
Steve Block6ded16b2010-05-10 14:33:55 +01004194void MacroAssembler::InvokeCode(Register code,
4195 const ParameterCount& expected,
4196 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +01004197 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004198 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004199 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004200 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004201
Steve Block6ded16b2010-05-10 14:33:55 +01004202 Label done;
4203
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004204 bool definitely_mismatches = false;
4205 InvokePrologue(expected, actual, Handle<Code>::null(), code,
4206 &done, &definitely_mismatches, flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004207 call_wrapper);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004208 if (!definitely_mismatches) {
4209 if (flag == CALL_FUNCTION) {
4210 call_wrapper.BeforeCall(CallSize(code));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004211 Call(code);
4212 call_wrapper.AfterCall();
4213 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004214 DCHECK(flag == JUMP_FUNCTION);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004215 Jump(code);
4216 }
4217 // Continue here if InvokePrologue does handle the invocation due to
4218 // mismatched parameter counts.
4219 bind(&done);
Steve Block6ded16b2010-05-10 14:33:55 +01004220 }
Steve Block6ded16b2010-05-10 14:33:55 +01004221}
4222
4223
Steve Block6ded16b2010-05-10 14:33:55 +01004224void MacroAssembler::InvokeFunction(Register function,
4225 const ParameterCount& actual,
Steve Block44f0eee2011-05-26 01:26:41 +01004226 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004227 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004228 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004229 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004230
Steve Block6ded16b2010-05-10 14:33:55 +01004231 // Contract with called JS functions requires that function is passed in a1.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004232 DCHECK(function.is(a1));
Steve Block6ded16b2010-05-10 14:33:55 +01004233 Register expected_reg = a2;
4234 Register code_reg = a3;
4235
4236 lw(code_reg, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
4237 lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4238 lw(expected_reg,
4239 FieldMemOperand(code_reg,
4240 SharedFunctionInfo::kFormalParameterCountOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01004241 sra(expected_reg, expected_reg, kSmiTagSize);
4242 lw(code_reg, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01004243
4244 ParameterCount expected(expected_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004245 InvokeCode(code_reg, expected, actual, flag, call_wrapper);
Steve Block44f0eee2011-05-26 01:26:41 +01004246}
4247
4248
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004249void MacroAssembler::InvokeFunction(Register function,
4250 const ParameterCount& expected,
Steve Block44f0eee2011-05-26 01:26:41 +01004251 const ParameterCount& actual,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004252 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004253 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004254 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004255 DCHECK(flag == JUMP_FUNCTION || has_frame());
4256
4257 // Contract with called JS functions requires that function is passed in a1.
4258 DCHECK(function.is(a1));
Steve Block44f0eee2011-05-26 01:26:41 +01004259
4260 // Get the function and setup the context.
Steve Block44f0eee2011-05-26 01:26:41 +01004261 lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4262
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004263 // We call indirectly through the code field in the function to
4264 // allow recompilation to take effect without changing any of the
4265 // call sites.
4266 lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004267 InvokeCode(a3, expected, actual, flag, call_wrapper);
4268}
4269
4270
4271void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
4272 const ParameterCount& expected,
4273 const ParameterCount& actual,
4274 InvokeFlag flag,
4275 const CallWrapper& call_wrapper) {
4276 li(a1, function);
4277 InvokeFunction(a1, expected, actual, flag, call_wrapper);
Steve Block44f0eee2011-05-26 01:26:41 +01004278}
4279
4280
4281void MacroAssembler::IsObjectJSObjectType(Register heap_object,
4282 Register map,
4283 Register scratch,
4284 Label* fail) {
4285 lw(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
4286 IsInstanceJSObjectType(map, scratch, fail);
4287}
4288
4289
4290void MacroAssembler::IsInstanceJSObjectType(Register map,
4291 Register scratch,
4292 Label* fail) {
4293 lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004294 Branch(fail, lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
4295 Branch(fail, gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Steve Block44f0eee2011-05-26 01:26:41 +01004296}
4297
4298
4299void MacroAssembler::IsObjectJSStringType(Register object,
4300 Register scratch,
4301 Label* fail) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004302 DCHECK(kNotStringTag != 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004303
4304 lw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4305 lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4306 And(scratch, scratch, Operand(kIsNotStringMask));
4307 Branch(fail, ne, scratch, Operand(zero_reg));
Steve Block6ded16b2010-05-10 14:33:55 +01004308}
4309
4310
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004311void MacroAssembler::IsObjectNameType(Register object,
4312 Register scratch,
4313 Label* fail) {
4314 lw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4315 lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4316 Branch(fail, hi, scratch, Operand(LAST_NAME_TYPE));
4317}
4318
4319
Steve Block6ded16b2010-05-10 14:33:55 +01004320// ---------------------------------------------------------------------------
4321// Support functions.
4322
Steve Block44f0eee2011-05-26 01:26:41 +01004323
4324void MacroAssembler::TryGetFunctionPrototype(Register function,
4325 Register result,
4326 Register scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004327 Label* miss,
4328 bool miss_on_bound_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004329 Label non_instance;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004330 if (miss_on_bound_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004331 // Check that the receiver isn't a smi.
4332 JumpIfSmi(function, miss);
4333
4334 // Check that the function really is a function. Load map into result reg.
4335 GetObjectType(function, result, scratch);
4336 Branch(miss, ne, scratch, Operand(JS_FUNCTION_TYPE));
4337
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004338 lw(scratch,
4339 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
4340 lw(scratch,
4341 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
4342 And(scratch, scratch,
4343 Operand(Smi::FromInt(1 << SharedFunctionInfo::kBoundFunction)));
4344 Branch(miss, ne, scratch, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004345
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004346 // Make sure that the function has an instance prototype.
4347 lbu(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
4348 And(scratch, scratch, Operand(1 << Map::kHasNonInstancePrototype));
4349 Branch(&non_instance, ne, scratch, Operand(zero_reg));
4350 }
Steve Block44f0eee2011-05-26 01:26:41 +01004351
4352 // Get the prototype or initial map from the function.
4353 lw(result,
4354 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
4355
4356 // If the prototype or initial map is the hole, don't return it and
4357 // simply miss the cache instead. This will allow us to allocate a
4358 // prototype object on-demand in the runtime system.
4359 LoadRoot(t8, Heap::kTheHoleValueRootIndex);
4360 Branch(miss, eq, result, Operand(t8));
4361
4362 // If the function does not have an initial map, we're done.
4363 Label done;
4364 GetObjectType(result, scratch, scratch);
4365 Branch(&done, ne, scratch, Operand(MAP_TYPE));
4366
4367 // Get the prototype from the initial map.
4368 lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01004369
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004370 if (miss_on_bound_function) {
4371 jmp(&done);
4372
4373 // Non-instance prototype: Fetch prototype from constructor field
4374 // in initial map.
4375 bind(&non_instance);
4376 lw(result, FieldMemOperand(result, Map::kConstructorOffset));
4377 }
Steve Block44f0eee2011-05-26 01:26:41 +01004378
4379 // All done.
4380 bind(&done);
4381}
Steve Block6ded16b2010-05-10 14:33:55 +01004382
4383
Steve Block44f0eee2011-05-26 01:26:41 +01004384void MacroAssembler::GetObjectType(Register object,
4385 Register map,
4386 Register type_reg) {
4387 lw(map, FieldMemOperand(object, HeapObject::kMapOffset));
4388 lbu(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
4389}
Steve Block6ded16b2010-05-10 14:33:55 +01004390
4391
4392// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00004393// Runtime calls.
Steve Block6ded16b2010-05-10 14:33:55 +01004394
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004395void MacroAssembler::CallStub(CodeStub* stub,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004396 TypeFeedbackId ast_id,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004397 Condition cond,
4398 Register r1,
4399 const Operand& r2,
4400 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004401 DCHECK(AllowThisStubCall(stub)); // Stub calls are not allowed in some stubs.
4402 Call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id,
4403 cond, r1, r2, bd);
Andrei Popescu31002712010-02-23 13:46:05 +00004404}
4405
4406
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004407void MacroAssembler::TailCallStub(CodeStub* stub,
4408 Condition cond,
4409 Register r1,
4410 const Operand& r2,
4411 BranchDelaySlot bd) {
4412 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond, r1, r2, bd);
Andrei Popescu31002712010-02-23 13:46:05 +00004413}
4414
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004415
Ben Murdoch257744e2011-11-30 15:57:28 +00004416static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
4417 return ref0.address() - ref1.address();
4418}
4419
4420
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004421void MacroAssembler::CallApiFunctionAndReturn(
4422 Register function_address,
4423 ExternalReference thunk_ref,
4424 int stack_space,
4425 MemOperand return_value_operand,
4426 MemOperand* context_restore_operand) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004427 ExternalReference next_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004428 ExternalReference::handle_scope_next_address(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00004429 const int kNextOffset = 0;
4430 const int kLimitOffset = AddressOffset(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004431 ExternalReference::handle_scope_limit_address(isolate()),
Ben Murdoch257744e2011-11-30 15:57:28 +00004432 next_address);
4433 const int kLevelOffset = AddressOffset(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004434 ExternalReference::handle_scope_level_address(isolate()),
Ben Murdoch257744e2011-11-30 15:57:28 +00004435 next_address);
4436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004437 DCHECK(function_address.is(a1) || function_address.is(a2));
4438
4439 Label profiler_disabled;
4440 Label end_profiler_check;
4441 li(t9, Operand(ExternalReference::is_profiling_address(isolate())));
4442 lb(t9, MemOperand(t9, 0));
4443 Branch(&profiler_disabled, eq, t9, Operand(zero_reg));
4444
4445 // Additional parameter is the address of the actual callback.
4446 li(t9, Operand(thunk_ref));
4447 jmp(&end_profiler_check);
4448
4449 bind(&profiler_disabled);
4450 mov(t9, function_address);
4451 bind(&end_profiler_check);
4452
Ben Murdoch257744e2011-11-30 15:57:28 +00004453 // Allocate HandleScope in callee-save registers.
4454 li(s3, Operand(next_address));
4455 lw(s0, MemOperand(s3, kNextOffset));
4456 lw(s1, MemOperand(s3, kLimitOffset));
4457 lw(s2, MemOperand(s3, kLevelOffset));
4458 Addu(s2, s2, Operand(1));
4459 sw(s2, MemOperand(s3, kLevelOffset));
4460
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004461 if (FLAG_log_timer_events) {
4462 FrameScope frame(this, StackFrame::MANUAL);
4463 PushSafepointRegisters();
4464 PrepareCallCFunction(1, a0);
4465 li(a0, Operand(ExternalReference::isolate_address(isolate())));
4466 CallCFunction(ExternalReference::log_enter_external_function(isolate()), 1);
4467 PopSafepointRegisters();
4468 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004469
4470 // Native call returns to the DirectCEntry stub which redirects to the
4471 // return address pushed on stack (could have moved after GC).
4472 // DirectCEntry stub itself is generated early and never moves.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004473 DirectCEntryStub stub(isolate());
4474 stub.GenerateCall(this, t9);
Ben Murdoch257744e2011-11-30 15:57:28 +00004475
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004476 if (FLAG_log_timer_events) {
4477 FrameScope frame(this, StackFrame::MANUAL);
4478 PushSafepointRegisters();
4479 PrepareCallCFunction(1, a0);
4480 li(a0, Operand(ExternalReference::isolate_address(isolate())));
4481 CallCFunction(ExternalReference::log_leave_external_function(isolate()), 1);
4482 PopSafepointRegisters();
4483 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004484
4485 Label promote_scheduled_exception;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004486 Label exception_handled;
Ben Murdoch257744e2011-11-30 15:57:28 +00004487 Label delete_allocated_handles;
4488 Label leave_exit_frame;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004489 Label return_value_loaded;
Ben Murdoch257744e2011-11-30 15:57:28 +00004490
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004491 // Load value from ReturnValue.
4492 lw(v0, return_value_operand);
4493 bind(&return_value_loaded);
Ben Murdoch257744e2011-11-30 15:57:28 +00004494
4495 // No more valid handles (the result handle was the last one). Restore
4496 // previous handle scope.
4497 sw(s0, MemOperand(s3, kNextOffset));
4498 if (emit_debug_code()) {
4499 lw(a1, MemOperand(s3, kLevelOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004500 Check(eq, kUnexpectedLevelAfterReturnFromApiCall, a1, Operand(s2));
Ben Murdoch257744e2011-11-30 15:57:28 +00004501 }
4502 Subu(s2, s2, Operand(1));
4503 sw(s2, MemOperand(s3, kLevelOffset));
4504 lw(at, MemOperand(s3, kLimitOffset));
4505 Branch(&delete_allocated_handles, ne, s1, Operand(at));
4506
4507 // Check if the function scheduled an exception.
4508 bind(&leave_exit_frame);
4509 LoadRoot(t0, Heap::kTheHoleValueRootIndex);
4510 li(at, Operand(ExternalReference::scheduled_exception_address(isolate())));
4511 lw(t1, MemOperand(at));
4512 Branch(&promote_scheduled_exception, ne, t0, Operand(t1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004513 bind(&exception_handled);
4514
4515 bool restore_context = context_restore_operand != NULL;
4516 if (restore_context) {
4517 lw(cp, *context_restore_operand);
4518 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004519 li(s0, Operand(stack_space));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004520 LeaveExitFrame(false, s0, !restore_context, EMIT_RETURN);
Ben Murdoch257744e2011-11-30 15:57:28 +00004521
4522 bind(&promote_scheduled_exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004523 {
4524 FrameScope frame(this, StackFrame::INTERNAL);
4525 CallExternalReference(
4526 ExternalReference(Runtime::kPromoteScheduledException, isolate()),
4527 0);
4528 }
4529 jmp(&exception_handled);
Ben Murdoch257744e2011-11-30 15:57:28 +00004530
4531 // HandleScope limit has changed. Delete allocated extensions.
4532 bind(&delete_allocated_handles);
4533 sw(s1, MemOperand(s3, kLimitOffset));
4534 mov(s0, v0);
4535 mov(a0, v0);
4536 PrepareCallCFunction(1, s1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004537 li(a0, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00004538 CallCFunction(ExternalReference::delete_handle_scope_extensions(isolate()),
4539 1);
4540 mov(v0, s0);
4541 jmp(&leave_exit_frame);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004542}
Ben Murdoch257744e2011-11-30 15:57:28 +00004543
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004544
4545bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004546 return has_frame_ || !stub->SometimesSetsUpAFrame();
Ben Murdoch257744e2011-11-30 15:57:28 +00004547}
4548
Andrei Popescu31002712010-02-23 13:46:05 +00004549
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004550void MacroAssembler::IndexFromHash(Register hash, Register index) {
Steve Block44f0eee2011-05-26 01:26:41 +01004551 // If the hash field contains an array index pick it out. The assert checks
4552 // that the constants for the maximum number of digits for an array index
4553 // cached in the hash field and the number of bits reserved for it does not
4554 // conflict.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004555 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
Steve Block44f0eee2011-05-26 01:26:41 +01004556 (1 << String::kArrayIndexValueBits));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004557 DecodeFieldToSmi<String::ArrayIndexValueBits>(index, hash);
Steve Block44f0eee2011-05-26 01:26:41 +01004558}
4559
4560
4561void MacroAssembler::ObjectToDoubleFPURegister(Register object,
4562 FPURegister result,
4563 Register scratch1,
4564 Register scratch2,
4565 Register heap_number_map,
4566 Label* not_number,
4567 ObjectToDoubleFlags flags) {
4568 Label done;
4569 if ((flags & OBJECT_NOT_SMI) == 0) {
4570 Label not_smi;
4571 JumpIfNotSmi(object, &not_smi);
4572 // Remove smi tag and convert to double.
4573 sra(scratch1, object, kSmiTagSize);
4574 mtc1(scratch1, result);
4575 cvt_d_w(result, result);
4576 Branch(&done);
4577 bind(&not_smi);
4578 }
4579 // Check for heap number and load double value from it.
4580 lw(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
4581 Branch(not_number, ne, scratch1, Operand(heap_number_map));
4582
4583 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
4584 // If exponent is all ones the number is either a NaN or +/-Infinity.
4585 Register exponent = scratch1;
4586 Register mask_reg = scratch2;
4587 lw(exponent, FieldMemOperand(object, HeapNumber::kExponentOffset));
4588 li(mask_reg, HeapNumber::kExponentMask);
4589
4590 And(exponent, exponent, mask_reg);
4591 Branch(not_number, eq, exponent, Operand(mask_reg));
4592 }
4593 ldc1(result, FieldMemOperand(object, HeapNumber::kValueOffset));
4594 bind(&done);
4595}
4596
4597
Steve Block44f0eee2011-05-26 01:26:41 +01004598void MacroAssembler::SmiToDoubleFPURegister(Register smi,
4599 FPURegister value,
4600 Register scratch1) {
4601 sra(scratch1, smi, kSmiTagSize);
4602 mtc1(scratch1, value);
4603 cvt_d_w(value, value);
4604}
4605
4606
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004607void MacroAssembler::AdduAndCheckForOverflow(Register dst, Register left,
4608 const Operand& right,
4609 Register overflow_dst,
4610 Register scratch) {
4611 if (right.is_reg()) {
4612 AdduAndCheckForOverflow(dst, left, right.rm(), overflow_dst, scratch);
4613 } else {
4614 if (dst.is(left)) {
4615 mov(scratch, left); // Preserve left.
4616 addiu(dst, left, right.immediate()); // Left is overwritten.
4617 xor_(scratch, dst, scratch); // Original left.
4618 // Load right since xori takes uint16 as immediate.
4619 addiu(t9, zero_reg, right.immediate());
4620 xor_(overflow_dst, dst, t9);
4621 and_(overflow_dst, overflow_dst, scratch);
4622 } else {
4623 addiu(dst, left, right.immediate());
4624 xor_(overflow_dst, dst, left);
4625 // Load right since xori takes uint16 as immediate.
4626 addiu(t9, zero_reg, right.immediate());
4627 xor_(scratch, dst, t9);
4628 and_(overflow_dst, scratch, overflow_dst);
4629 }
4630 }
4631}
4632
4633
4634void MacroAssembler::AdduAndCheckForOverflow(Register dst, Register left,
Ben Murdoch257744e2011-11-30 15:57:28 +00004635 Register right,
4636 Register overflow_dst,
4637 Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004638 DCHECK(!dst.is(overflow_dst));
4639 DCHECK(!dst.is(scratch));
4640 DCHECK(!overflow_dst.is(scratch));
4641 DCHECK(!overflow_dst.is(left));
4642 DCHECK(!overflow_dst.is(right));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004643
4644 if (left.is(right) && dst.is(left)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004645 DCHECK(!dst.is(t9));
4646 DCHECK(!scratch.is(t9));
4647 DCHECK(!left.is(t9));
4648 DCHECK(!right.is(t9));
4649 DCHECK(!overflow_dst.is(t9));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004650 mov(t9, right);
4651 right = t9;
4652 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004653
Ben Murdoch257744e2011-11-30 15:57:28 +00004654 if (dst.is(left)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004655 mov(scratch, left); // Preserve left.
4656 addu(dst, left, right); // Left is overwritten.
4657 xor_(scratch, dst, scratch); // Original left.
4658 xor_(overflow_dst, dst, right);
4659 and_(overflow_dst, overflow_dst, scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +00004660 } else if (dst.is(right)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004661 mov(scratch, right); // Preserve right.
4662 addu(dst, left, right); // Right is overwritten.
4663 xor_(scratch, dst, scratch); // Original right.
4664 xor_(overflow_dst, dst, left);
4665 and_(overflow_dst, overflow_dst, scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +00004666 } else {
4667 addu(dst, left, right);
4668 xor_(overflow_dst, dst, left);
4669 xor_(scratch, dst, right);
4670 and_(overflow_dst, scratch, overflow_dst);
4671 }
4672}
4673
4674
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004675void MacroAssembler::SubuAndCheckForOverflow(Register dst, Register left,
4676 const Operand& right,
4677 Register overflow_dst,
4678 Register scratch) {
4679 if (right.is_reg()) {
4680 SubuAndCheckForOverflow(dst, left, right.rm(), overflow_dst, scratch);
4681 } else {
4682 if (dst.is(left)) {
4683 mov(scratch, left); // Preserve left.
4684 addiu(dst, left, -(right.immediate())); // Left is overwritten.
4685 xor_(overflow_dst, dst, scratch); // scratch is original left.
4686 // Load right since xori takes uint16 as immediate.
4687 addiu(t9, zero_reg, right.immediate());
4688 xor_(scratch, scratch, t9); // scratch is original left.
4689 and_(overflow_dst, scratch, overflow_dst);
4690 } else {
4691 addiu(dst, left, -(right.immediate()));
4692 xor_(overflow_dst, dst, left);
4693 // Load right since xori takes uint16 as immediate.
4694 addiu(t9, zero_reg, right.immediate());
4695 xor_(scratch, left, t9);
4696 and_(overflow_dst, scratch, overflow_dst);
4697 }
4698 }
4699}
4700
4701
4702void MacroAssembler::SubuAndCheckForOverflow(Register dst, Register left,
Ben Murdoch257744e2011-11-30 15:57:28 +00004703 Register right,
4704 Register overflow_dst,
4705 Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004706 DCHECK(!dst.is(overflow_dst));
4707 DCHECK(!dst.is(scratch));
4708 DCHECK(!overflow_dst.is(scratch));
4709 DCHECK(!overflow_dst.is(left));
4710 DCHECK(!overflow_dst.is(right));
4711 DCHECK(!scratch.is(left));
4712 DCHECK(!scratch.is(right));
Ben Murdoch257744e2011-11-30 15:57:28 +00004713
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004714 // This happens with some crankshaft code. Since Subu works fine if
4715 // left == right, let's not make that restriction here.
4716 if (left.is(right)) {
4717 mov(dst, zero_reg);
4718 mov(overflow_dst, zero_reg);
4719 return;
4720 }
4721
Ben Murdoch257744e2011-11-30 15:57:28 +00004722 if (dst.is(left)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004723 mov(scratch, left); // Preserve left.
4724 subu(dst, left, right); // Left is overwritten.
4725 xor_(overflow_dst, dst, scratch); // scratch is original left.
4726 xor_(scratch, scratch, right); // scratch is original left.
4727 and_(overflow_dst, scratch, overflow_dst);
Ben Murdoch257744e2011-11-30 15:57:28 +00004728 } else if (dst.is(right)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004729 mov(scratch, right); // Preserve right.
4730 subu(dst, left, right); // Right is overwritten.
4731 xor_(overflow_dst, dst, left);
4732 xor_(scratch, left, scratch); // Original right.
4733 and_(overflow_dst, scratch, overflow_dst);
Ben Murdoch257744e2011-11-30 15:57:28 +00004734 } else {
4735 subu(dst, left, right);
4736 xor_(overflow_dst, dst, left);
4737 xor_(scratch, left, right);
4738 and_(overflow_dst, scratch, overflow_dst);
4739 }
4740}
4741
4742
Steve Block44f0eee2011-05-26 01:26:41 +01004743void MacroAssembler::CallRuntime(const Runtime::Function* f,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004744 int num_arguments,
4745 SaveFPRegsMode save_doubles) {
Steve Block6ded16b2010-05-10 14:33:55 +01004746 // All parameters are on the stack. v0 has the return value after call.
4747
4748 // If the expected number of arguments of the runtime function is
4749 // constant, we check that the actual number of arguments match the
4750 // expectation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004751 CHECK(f->nargs < 0 || f->nargs == num_arguments);
Steve Block6ded16b2010-05-10 14:33:55 +01004752
4753 // TODO(1236192): Most runtime routines don't need the number of
4754 // arguments passed in because it is constant. At some point we
4755 // should remove this need and make the runtime routine entry code
4756 // smarter.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004757 PrepareCEntryArgs(num_arguments);
4758 PrepareCEntryFunction(ExternalReference(f, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004759 CEntryStub stub(isolate(), 1, save_doubles);
Steve Block6ded16b2010-05-10 14:33:55 +01004760 CallStub(&stub);
Andrei Popescu31002712010-02-23 13:46:05 +00004761}
4762
4763
Steve Block44f0eee2011-05-26 01:26:41 +01004764void MacroAssembler::CallExternalReference(const ExternalReference& ext,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004765 int num_arguments,
4766 BranchDelaySlot bd) {
4767 PrepareCEntryArgs(num_arguments);
4768 PrepareCEntryFunction(ext);
Steve Block44f0eee2011-05-26 01:26:41 +01004769
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004770 CEntryStub stub(isolate(), 1);
4771 CallStub(&stub, TypeFeedbackId::None(), al, zero_reg, Operand(zero_reg), bd);
Steve Block44f0eee2011-05-26 01:26:41 +01004772}
4773
4774
Steve Block6ded16b2010-05-10 14:33:55 +01004775void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
4776 int num_arguments,
4777 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01004778 // TODO(1236192): Most runtime routines don't need the number of
4779 // arguments passed in because it is constant. At some point we
4780 // should remove this need and make the runtime routine entry code
4781 // smarter.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004782 PrepareCEntryArgs(num_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01004783 JumpToExternalReference(ext);
Andrei Popescu31002712010-02-23 13:46:05 +00004784}
4785
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004786
Steve Block6ded16b2010-05-10 14:33:55 +01004787void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
Andrei Popescu31002712010-02-23 13:46:05 +00004788 int num_arguments,
4789 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01004790 TailCallExternalReference(ExternalReference(fid, isolate()),
4791 num_arguments,
4792 result_size);
Andrei Popescu31002712010-02-23 13:46:05 +00004793}
4794
4795
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004796void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin,
4797 BranchDelaySlot bd) {
4798 PrepareCEntryFunction(builtin);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004799 CEntryStub stub(isolate(), 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004800 Jump(stub.GetCode(),
4801 RelocInfo::CODE_TARGET,
4802 al,
4803 zero_reg,
4804 Operand(zero_reg),
4805 bd);
Andrei Popescu31002712010-02-23 13:46:05 +00004806}
4807
4808
4809void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +00004810 InvokeFlag flag,
4811 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004812 // You can't call a builtin without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004813 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004814
Steve Block44f0eee2011-05-26 01:26:41 +01004815 GetBuiltinEntry(t9, id);
Ben Murdoch257744e2011-11-30 15:57:28 +00004816 if (flag == CALL_FUNCTION) {
4817 call_wrapper.BeforeCall(CallSize(t9));
Steve Block44f0eee2011-05-26 01:26:41 +01004818 Call(t9);
Ben Murdoch257744e2011-11-30 15:57:28 +00004819 call_wrapper.AfterCall();
Steve Block44f0eee2011-05-26 01:26:41 +01004820 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004821 DCHECK(flag == JUMP_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01004822 Jump(t9);
4823 }
4824}
4825
4826
4827void MacroAssembler::GetBuiltinFunction(Register target,
4828 Builtins::JavaScript id) {
4829 // Load the builtins object into target register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004830 lw(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
Steve Block44f0eee2011-05-26 01:26:41 +01004831 lw(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
4832 // Load the JavaScript builtin function from the builtins object.
4833 lw(target, FieldMemOperand(target,
4834 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
Andrei Popescu31002712010-02-23 13:46:05 +00004835}
4836
4837
4838void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004839 DCHECK(!target.is(a1));
Steve Block44f0eee2011-05-26 01:26:41 +01004840 GetBuiltinFunction(a1, id);
4841 // Load the code entry point from the builtins object.
4842 lw(target, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
Andrei Popescu31002712010-02-23 13:46:05 +00004843}
4844
4845
4846void MacroAssembler::SetCounter(StatsCounter* counter, int value,
4847 Register scratch1, Register scratch2) {
Steve Block44f0eee2011-05-26 01:26:41 +01004848 if (FLAG_native_code_counters && counter->Enabled()) {
4849 li(scratch1, Operand(value));
4850 li(scratch2, Operand(ExternalReference(counter)));
4851 sw(scratch1, MemOperand(scratch2));
4852 }
Andrei Popescu31002712010-02-23 13:46:05 +00004853}
4854
4855
4856void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
4857 Register scratch1, Register scratch2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004858 DCHECK(value > 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004859 if (FLAG_native_code_counters && counter->Enabled()) {
4860 li(scratch2, Operand(ExternalReference(counter)));
4861 lw(scratch1, MemOperand(scratch2));
4862 Addu(scratch1, scratch1, Operand(value));
4863 sw(scratch1, MemOperand(scratch2));
4864 }
Andrei Popescu31002712010-02-23 13:46:05 +00004865}
4866
4867
4868void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
4869 Register scratch1, Register scratch2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004870 DCHECK(value > 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004871 if (FLAG_native_code_counters && counter->Enabled()) {
4872 li(scratch2, Operand(ExternalReference(counter)));
4873 lw(scratch1, MemOperand(scratch2));
4874 Subu(scratch1, scratch1, Operand(value));
4875 sw(scratch1, MemOperand(scratch2));
4876 }
Andrei Popescu31002712010-02-23 13:46:05 +00004877}
4878
4879
Steve Block6ded16b2010-05-10 14:33:55 +01004880// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00004881// Debugging.
Andrei Popescu31002712010-02-23 13:46:05 +00004882
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004883void MacroAssembler::Assert(Condition cc, BailoutReason reason,
Andrei Popescu31002712010-02-23 13:46:05 +00004884 Register rs, Operand rt) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004885 if (emit_debug_code())
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004886 Check(cc, reason, rs, rt);
Steve Block44f0eee2011-05-26 01:26:41 +01004887}
4888
4889
4890void MacroAssembler::AssertFastElements(Register elements) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004891 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004892 DCHECK(!elements.is(at));
Steve Block44f0eee2011-05-26 01:26:41 +01004893 Label ok;
Ben Murdoch257744e2011-11-30 15:57:28 +00004894 push(elements);
Steve Block44f0eee2011-05-26 01:26:41 +01004895 lw(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
4896 LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4897 Branch(&ok, eq, elements, Operand(at));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004898 LoadRoot(at, Heap::kFixedDoubleArrayMapRootIndex);
4899 Branch(&ok, eq, elements, Operand(at));
Steve Block44f0eee2011-05-26 01:26:41 +01004900 LoadRoot(at, Heap::kFixedCOWArrayMapRootIndex);
4901 Branch(&ok, eq, elements, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004902 Abort(kJSObjectWithFastElementsMapHasSlowElements);
Steve Block44f0eee2011-05-26 01:26:41 +01004903 bind(&ok);
Ben Murdoch257744e2011-11-30 15:57:28 +00004904 pop(elements);
Steve Block44f0eee2011-05-26 01:26:41 +01004905 }
Andrei Popescu31002712010-02-23 13:46:05 +00004906}
4907
4908
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004909void MacroAssembler::Check(Condition cc, BailoutReason reason,
Andrei Popescu31002712010-02-23 13:46:05 +00004910 Register rs, Operand rt) {
Steve Block44f0eee2011-05-26 01:26:41 +01004911 Label L;
4912 Branch(&L, cc, rs, rt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004913 Abort(reason);
Ben Murdoch257744e2011-11-30 15:57:28 +00004914 // Will not return here.
Steve Block44f0eee2011-05-26 01:26:41 +01004915 bind(&L);
Andrei Popescu31002712010-02-23 13:46:05 +00004916}
4917
4918
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004919void MacroAssembler::Abort(BailoutReason reason) {
Steve Block44f0eee2011-05-26 01:26:41 +01004920 Label abort_start;
4921 bind(&abort_start);
Steve Block44f0eee2011-05-26 01:26:41 +01004922#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004923 const char* msg = GetBailoutReason(reason);
Steve Block44f0eee2011-05-26 01:26:41 +01004924 if (msg != NULL) {
4925 RecordComment("Abort message: ");
4926 RecordComment(msg);
4927 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004928
4929 if (FLAG_trap_on_abort) {
4930 stop(msg);
4931 return;
4932 }
Steve Block44f0eee2011-05-26 01:26:41 +01004933#endif
Steve Block44f0eee2011-05-26 01:26:41 +01004934
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004935 li(a0, Operand(Smi::FromInt(reason)));
Ben Murdoch257744e2011-11-30 15:57:28 +00004936 push(a0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004937 // Disable stub call restrictions to always allow calls to abort.
4938 if (!has_frame_) {
4939 // We don't actually want to generate a pile of code for this, so just
4940 // claim there is a stack frame, without generating one.
4941 FrameScope scope(this, StackFrame::NONE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004942 CallRuntime(Runtime::kAbort, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004943 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004944 CallRuntime(Runtime::kAbort, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004945 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004946 // Will not return here.
Steve Block44f0eee2011-05-26 01:26:41 +01004947 if (is_trampoline_pool_blocked()) {
4948 // If the calling code cares about the exact number of
4949 // instructions generated, we insert padding here to keep the size
4950 // of the Abort macro constant.
4951 // Currently in debug mode with debug_code enabled the number of
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004952 // generated instructions is 10, so we use this as a maximum value.
4953 static const int kExpectedAbortInstructions = 10;
Steve Block44f0eee2011-05-26 01:26:41 +01004954 int abort_instructions = InstructionsGeneratedSince(&abort_start);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004955 DCHECK(abort_instructions <= kExpectedAbortInstructions);
Steve Block44f0eee2011-05-26 01:26:41 +01004956 while (abort_instructions++ < kExpectedAbortInstructions) {
4957 nop();
4958 }
4959 }
4960}
4961
4962
4963void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
4964 if (context_chain_length > 0) {
4965 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004966 lw(dst, MemOperand(cp, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Block44f0eee2011-05-26 01:26:41 +01004967 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004968 lw(dst, MemOperand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Block44f0eee2011-05-26 01:26:41 +01004969 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004970 } else {
4971 // Slot is in the current function context. Move it into the
4972 // destination register in case we store into it (the write barrier
4973 // cannot be allowed to destroy the context in esi).
4974 Move(dst, cp);
4975 }
Steve Block44f0eee2011-05-26 01:26:41 +01004976}
4977
4978
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004979void MacroAssembler::LoadTransitionedArrayMapConditional(
4980 ElementsKind expected_kind,
4981 ElementsKind transitioned_kind,
4982 Register map_in_out,
4983 Register scratch,
4984 Label* no_map_match) {
4985 // Load the global or builtins object from the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004986 lw(scratch,
4987 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4988 lw(scratch, FieldMemOperand(scratch, GlobalObject::kNativeContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004989
4990 // Check that the function's map is the same as the expected cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004991 lw(scratch,
4992 MemOperand(scratch,
4993 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
4994 size_t offset = expected_kind * kPointerSize +
4995 FixedArrayBase::kHeaderSize;
4996 lw(at, FieldMemOperand(scratch, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004997 Branch(no_map_match, ne, map_in_out, Operand(at));
4998
4999 // Use the transitioned cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005000 offset = transitioned_kind * kPointerSize +
5001 FixedArrayBase::kHeaderSize;
5002 lw(map_in_out, FieldMemOperand(scratch, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005003}
5004
5005
Steve Block44f0eee2011-05-26 01:26:41 +01005006void MacroAssembler::LoadGlobalFunction(int index, Register function) {
5007 // Load the global or builtins object from the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005008 lw(function,
5009 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
5010 // Load the native context from the global or builtins object.
Steve Block44f0eee2011-05-26 01:26:41 +01005011 lw(function, FieldMemOperand(function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005012 GlobalObject::kNativeContextOffset));
5013 // Load the function from the native context.
Steve Block44f0eee2011-05-26 01:26:41 +01005014 lw(function, MemOperand(function, Context::SlotOffset(index)));
5015}
5016
5017
5018void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
5019 Register map,
5020 Register scratch) {
5021 // Load the initial map. The global functions all have initial maps.
5022 lw(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00005023 if (emit_debug_code()) {
Steve Block44f0eee2011-05-26 01:26:41 +01005024 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00005025 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
Steve Block44f0eee2011-05-26 01:26:41 +01005026 Branch(&ok);
5027 bind(&fail);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005028 Abort(kGlobalFunctionsMustHaveInitialMap);
Steve Block44f0eee2011-05-26 01:26:41 +01005029 bind(&ok);
5030 }
Andrei Popescu31002712010-02-23 13:46:05 +00005031}
5032
Steve Block6ded16b2010-05-10 14:33:55 +01005033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005034void MacroAssembler::StubPrologue() {
5035 Push(ra, fp, cp);
5036 Push(Smi::FromInt(StackFrame::STUB));
5037 // Adjust FP to point to saved FP.
5038 Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
5039}
5040
5041
5042void MacroAssembler::Prologue(bool code_pre_aging) {
5043 PredictableCodeSizeScope predictible_code_size_scope(
5044 this, kNoCodeAgeSequenceLength);
5045 // The following three instructions must remain together and unmodified
5046 // for code aging to work properly.
5047 if (code_pre_aging) {
5048 // Pre-age the code.
5049 Code* stub = Code::GetPreAgedCodeAgeStub(isolate());
5050 nop(Assembler::CODE_AGE_MARKER_NOP);
5051 // Load the stub address to t9 and call it,
5052 // GetCodeAgeAndParity() extracts the stub address from this instruction.
5053 li(t9,
5054 Operand(reinterpret_cast<uint32_t>(stub->instruction_start())),
5055 CONSTANT_SIZE);
5056 nop(); // Prevent jalr to jal optimization.
5057 jalr(t9, a0);
5058 nop(); // Branch delay slot nop.
5059 nop(); // Pad the empty space.
5060 } else {
5061 Push(ra, fp, cp, a1);
5062 nop(Assembler::CODE_AGE_SEQUENCE_NOP);
5063 // Adjust fp to point to caller's fp.
5064 Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
5065 }
5066}
5067
5068
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005069void MacroAssembler::EnterFrame(StackFrame::Type type,
5070 bool load_constant_pool_pointer_reg) {
5071 // Out-of-line constant pool not implemented on mips.
5072 UNREACHABLE();
5073}
5074
5075
Steve Block6ded16b2010-05-10 14:33:55 +01005076void MacroAssembler::EnterFrame(StackFrame::Type type) {
5077 addiu(sp, sp, -5 * kPointerSize);
Steve Block44f0eee2011-05-26 01:26:41 +01005078 li(t8, Operand(Smi::FromInt(type)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005079 li(t9, Operand(CodeObject()), CONSTANT_SIZE);
Steve Block6ded16b2010-05-10 14:33:55 +01005080 sw(ra, MemOperand(sp, 4 * kPointerSize));
5081 sw(fp, MemOperand(sp, 3 * kPointerSize));
5082 sw(cp, MemOperand(sp, 2 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005083 sw(t8, MemOperand(sp, 1 * kPointerSize));
5084 sw(t9, MemOperand(sp, 0 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005085 // Adjust FP to point to saved FP.
5086 Addu(fp, sp,
5087 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01005088}
5089
5090
5091void MacroAssembler::LeaveFrame(StackFrame::Type type) {
5092 mov(sp, fp);
5093 lw(fp, MemOperand(sp, 0 * kPointerSize));
5094 lw(ra, MemOperand(sp, 1 * kPointerSize));
5095 addiu(sp, sp, 2 * kPointerSize);
5096}
5097
5098
Ben Murdoch257744e2011-11-30 15:57:28 +00005099void MacroAssembler::EnterExitFrame(bool save_doubles,
5100 int stack_space) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005101 // Set up the frame structure on the stack.
Ben Murdoch257744e2011-11-30 15:57:28 +00005102 STATIC_ASSERT(2 * kPointerSize == ExitFrameConstants::kCallerSPDisplacement);
5103 STATIC_ASSERT(1 * kPointerSize == ExitFrameConstants::kCallerPCOffset);
5104 STATIC_ASSERT(0 * kPointerSize == ExitFrameConstants::kCallerFPOffset);
Steve Block6ded16b2010-05-10 14:33:55 +01005105
Ben Murdoch257744e2011-11-30 15:57:28 +00005106 // This is how the stack will look:
5107 // fp + 2 (==kCallerSPDisplacement) - old stack's end
5108 // [fp + 1 (==kCallerPCOffset)] - saved old ra
5109 // [fp + 0 (==kCallerFPOffset)] - saved old fp
5110 // [fp - 1 (==kSPOffset)] - sp of the called function
5111 // [fp - 2 (==kCodeOffset)] - CodeObject
5112 // fp - (2 + stack_space + alignment) == sp == [fp - kSPOffset] - top of the
5113 // new stack (will contain saved ra)
Steve Block6ded16b2010-05-10 14:33:55 +01005114
5115 // Save registers.
Ben Murdoch257744e2011-11-30 15:57:28 +00005116 addiu(sp, sp, -4 * kPointerSize);
5117 sw(ra, MemOperand(sp, 3 * kPointerSize));
5118 sw(fp, MemOperand(sp, 2 * kPointerSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005119 addiu(fp, sp, 2 * kPointerSize); // Set up new frame pointer.
Steve Block6ded16b2010-05-10 14:33:55 +01005120
Ben Murdoch257744e2011-11-30 15:57:28 +00005121 if (emit_debug_code()) {
5122 sw(zero_reg, MemOperand(fp, ExitFrameConstants::kSPOffset));
5123 }
5124
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005125 // Accessed from ExitFrame::code_slot.
5126 li(t8, Operand(CodeObject()), CONSTANT_SIZE);
Ben Murdoch257744e2011-11-30 15:57:28 +00005127 sw(t8, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01005128
5129 // Save the frame pointer and the context in top.
Ben Murdoch589d6972011-11-30 16:04:58 +00005130 li(t8, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01005131 sw(fp, MemOperand(t8));
Ben Murdoch589d6972011-11-30 16:04:58 +00005132 li(t8, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01005133 sw(cp, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01005134
Ben Murdoch257744e2011-11-30 15:57:28 +00005135 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
Steve Block44f0eee2011-05-26 01:26:41 +01005136 if (save_doubles) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005137 // The stack must be allign to 0 modulo 8 for stores with sdc1.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005138 DCHECK(kDoubleSize == frame_alignment);
Ben Murdoch257744e2011-11-30 15:57:28 +00005139 if (frame_alignment > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005140 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Ben Murdoch257744e2011-11-30 15:57:28 +00005141 And(sp, sp, Operand(-frame_alignment)); // Align stack.
5142 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005143 int space = FPURegister::kMaxNumRegisters * kDoubleSize;
Steve Block44f0eee2011-05-26 01:26:41 +01005144 Subu(sp, sp, Operand(space));
5145 // Remember: we only need to save every 2nd double FPU value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005146 for (int i = 0; i < FPURegister::kMaxNumRegisters; i+=2) {
Steve Block44f0eee2011-05-26 01:26:41 +01005147 FPURegister reg = FPURegister::from_code(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00005148 sdc1(reg, MemOperand(sp, i * kDoubleSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005149 }
Steve Block44f0eee2011-05-26 01:26:41 +01005150 }
Ben Murdoch257744e2011-11-30 15:57:28 +00005151
5152 // Reserve place for the return address, stack space and an optional slot
5153 // (used by the DirectCEntryStub to hold the return value if a struct is
5154 // returned) and align the frame preparing for calling the runtime function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005155 DCHECK(stack_space >= 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00005156 Subu(sp, sp, Operand((stack_space + 2) * kPointerSize));
5157 if (frame_alignment > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005158 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Ben Murdoch257744e2011-11-30 15:57:28 +00005159 And(sp, sp, Operand(-frame_alignment)); // Align stack.
5160 }
5161
5162 // Set the exit frame sp value to point just before the return address
5163 // location.
5164 addiu(at, sp, kPointerSize);
5165 sw(at, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01005166}
5167
5168
Ben Murdoch257744e2011-11-30 15:57:28 +00005169void MacroAssembler::LeaveExitFrame(bool save_doubles,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005170 Register argument_count,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005171 bool restore_context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005172 bool do_return) {
Steve Block44f0eee2011-05-26 01:26:41 +01005173 // Optionally restore all double registers.
5174 if (save_doubles) {
Steve Block44f0eee2011-05-26 01:26:41 +01005175 // Remember: we only need to restore every 2nd double FPU value.
Ben Murdoch257744e2011-11-30 15:57:28 +00005176 lw(t8, MemOperand(fp, ExitFrameConstants::kSPOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005177 for (int i = 0; i < FPURegister::kMaxNumRegisters; i+=2) {
Steve Block44f0eee2011-05-26 01:26:41 +01005178 FPURegister reg = FPURegister::from_code(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00005179 ldc1(reg, MemOperand(t8, i * kDoubleSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005180 }
5181 }
5182
Steve Block6ded16b2010-05-10 14:33:55 +01005183 // Clear top frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00005184 li(t8, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01005185 sw(zero_reg, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01005186
5187 // Restore current context from top and clear it in debug mode.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005188 if (restore_context) {
5189 li(t8, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
5190 lw(cp, MemOperand(t8));
5191 }
Steve Block6ded16b2010-05-10 14:33:55 +01005192#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005193 li(t8, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01005194 sw(a3, MemOperand(t8));
Steve Block6ded16b2010-05-10 14:33:55 +01005195#endif
5196
5197 // Pop the arguments, restore registers, and return.
5198 mov(sp, fp); // Respect ABI stack constraint.
Ben Murdoch257744e2011-11-30 15:57:28 +00005199 lw(fp, MemOperand(sp, ExitFrameConstants::kCallerFPOffset));
5200 lw(ra, MemOperand(sp, ExitFrameConstants::kCallerPCOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005201
Ben Murdoch257744e2011-11-30 15:57:28 +00005202 if (argument_count.is_valid()) {
5203 sll(t8, argument_count, kPointerSizeLog2);
5204 addu(sp, sp, t8);
5205 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005206
5207 if (do_return) {
5208 Ret(USE_DELAY_SLOT);
5209 // If returning, the instruction in the delay slot will be the addiu below.
5210 }
5211 addiu(sp, sp, 8);
Steve Block6ded16b2010-05-10 14:33:55 +01005212}
5213
5214
Steve Block44f0eee2011-05-26 01:26:41 +01005215void MacroAssembler::InitializeNewString(Register string,
5216 Register length,
5217 Heap::RootListIndex map_index,
5218 Register scratch1,
5219 Register scratch2) {
5220 sll(scratch1, length, kSmiTagSize);
5221 LoadRoot(scratch2, map_index);
5222 sw(scratch1, FieldMemOperand(string, String::kLengthOffset));
5223 li(scratch1, Operand(String::kEmptyHashField));
5224 sw(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
5225 sw(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
5226}
5227
5228
5229int MacroAssembler::ActivationFrameAlignment() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005230#if V8_HOST_ARCH_MIPS
Steve Block44f0eee2011-05-26 01:26:41 +01005231 // Running on the real platform. Use the alignment as mandated by the local
5232 // environment.
5233 // Note: This will break if we ever start generating snapshots on one Mips
5234 // platform for another Mips platform with a different alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005235 return base::OS::ActivationFrameAlignment();
5236#else // V8_HOST_ARCH_MIPS
Steve Block44f0eee2011-05-26 01:26:41 +01005237 // If we are using the simulator then we should always align to the expected
5238 // alignment. As the simulator is used to generate snapshots we do not know
5239 // if the target platform will need alignment, so this is controlled from a
5240 // flag.
5241 return FLAG_sim_stack_alignment;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005242#endif // V8_HOST_ARCH_MIPS
Steve Block44f0eee2011-05-26 01:26:41 +01005243}
5244
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005245
Ben Murdoch257744e2011-11-30 15:57:28 +00005246void MacroAssembler::AssertStackIsAligned() {
5247 if (emit_debug_code()) {
5248 const int frame_alignment = ActivationFrameAlignment();
5249 const int frame_alignment_mask = frame_alignment - 1;
Steve Block44f0eee2011-05-26 01:26:41 +01005250
Ben Murdoch257744e2011-11-30 15:57:28 +00005251 if (frame_alignment > kPointerSize) {
5252 Label alignment_as_expected;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005253 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Ben Murdoch257744e2011-11-30 15:57:28 +00005254 andi(at, sp, frame_alignment_mask);
5255 Branch(&alignment_as_expected, eq, at, Operand(zero_reg));
5256 // Don't use Check here, as it will call Runtime_Abort re-entering here.
5257 stop("Unexpected stack alignment");
5258 bind(&alignment_as_expected);
5259 }
Steve Block6ded16b2010-05-10 14:33:55 +01005260 }
Steve Block6ded16b2010-05-10 14:33:55 +01005261}
5262
Steve Block44f0eee2011-05-26 01:26:41 +01005263
Steve Block44f0eee2011-05-26 01:26:41 +01005264void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
5265 Register reg,
5266 Register scratch,
5267 Label* not_power_of_two_or_zero) {
5268 Subu(scratch, reg, Operand(1));
5269 Branch(USE_DELAY_SLOT, not_power_of_two_or_zero, lt,
5270 scratch, Operand(zero_reg));
5271 and_(at, scratch, reg); // In the delay slot.
5272 Branch(not_power_of_two_or_zero, ne, at, Operand(zero_reg));
5273}
5274
5275
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005276void MacroAssembler::SmiTagCheckOverflow(Register reg, Register overflow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005277 DCHECK(!reg.is(overflow));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005278 mov(overflow, reg); // Save original value.
5279 SmiTag(reg);
5280 xor_(overflow, overflow, reg); // Overflow if (value ^ 2 * value) < 0.
5281}
5282
5283
5284void MacroAssembler::SmiTagCheckOverflow(Register dst,
5285 Register src,
5286 Register overflow) {
5287 if (dst.is(src)) {
5288 // Fall back to slower case.
5289 SmiTagCheckOverflow(dst, overflow);
5290 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005291 DCHECK(!dst.is(src));
5292 DCHECK(!dst.is(overflow));
5293 DCHECK(!src.is(overflow));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005294 SmiTag(dst, src);
5295 xor_(overflow, dst, src); // Overflow if (value ^ 2 * value) < 0.
5296 }
5297}
5298
5299
5300void MacroAssembler::UntagAndJumpIfSmi(Register dst,
5301 Register src,
5302 Label* smi_case) {
5303 JumpIfSmi(src, smi_case, at, USE_DELAY_SLOT);
5304 SmiUntag(dst, src);
5305}
5306
5307
5308void MacroAssembler::UntagAndJumpIfNotSmi(Register dst,
5309 Register src,
5310 Label* non_smi_case) {
5311 JumpIfNotSmi(src, non_smi_case, at, USE_DELAY_SLOT);
5312 SmiUntag(dst, src);
5313}
5314
5315void MacroAssembler::JumpIfSmi(Register value,
5316 Label* smi_label,
5317 Register scratch,
5318 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005319 DCHECK_EQ(0, kSmiTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005320 andi(scratch, value, kSmiTagMask);
5321 Branch(bd, smi_label, eq, scratch, Operand(zero_reg));
5322}
5323
5324void MacroAssembler::JumpIfNotSmi(Register value,
5325 Label* not_smi_label,
5326 Register scratch,
5327 BranchDelaySlot bd) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005328 DCHECK_EQ(0, kSmiTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005329 andi(scratch, value, kSmiTagMask);
5330 Branch(bd, not_smi_label, ne, scratch, Operand(zero_reg));
5331}
5332
5333
Steve Block44f0eee2011-05-26 01:26:41 +01005334void MacroAssembler::JumpIfNotBothSmi(Register reg1,
5335 Register reg2,
5336 Label* on_not_both_smi) {
5337 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005338 DCHECK_EQ(1, kSmiTagMask);
Steve Block44f0eee2011-05-26 01:26:41 +01005339 or_(at, reg1, reg2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005340 JumpIfNotSmi(at, on_not_both_smi);
Steve Block44f0eee2011-05-26 01:26:41 +01005341}
5342
5343
5344void MacroAssembler::JumpIfEitherSmi(Register reg1,
5345 Register reg2,
5346 Label* on_either_smi) {
5347 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005348 DCHECK_EQ(1, kSmiTagMask);
Steve Block44f0eee2011-05-26 01:26:41 +01005349 // Both Smi tags must be 1 (not Smi).
5350 and_(at, reg1, reg2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005351 JumpIfSmi(at, on_either_smi);
Steve Block44f0eee2011-05-26 01:26:41 +01005352}
5353
5354
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005355void MacroAssembler::AssertNotSmi(Register object) {
5356 if (emit_debug_code()) {
5357 STATIC_ASSERT(kSmiTag == 0);
5358 andi(at, object, kSmiTagMask);
5359 Check(ne, kOperandIsASmi, at, Operand(zero_reg));
5360 }
Steve Block44f0eee2011-05-26 01:26:41 +01005361}
5362
5363
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005364void MacroAssembler::AssertSmi(Register object) {
5365 if (emit_debug_code()) {
5366 STATIC_ASSERT(kSmiTag == 0);
5367 andi(at, object, kSmiTagMask);
5368 Check(eq, kOperandIsASmi, at, Operand(zero_reg));
5369 }
Steve Block44f0eee2011-05-26 01:26:41 +01005370}
5371
5372
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005373void MacroAssembler::AssertString(Register object) {
5374 if (emit_debug_code()) {
5375 STATIC_ASSERT(kSmiTag == 0);
5376 SmiTst(object, t0);
5377 Check(ne, kOperandIsASmiAndNotAString, t0, Operand(zero_reg));
5378 push(object);
5379 lw(object, FieldMemOperand(object, HeapObject::kMapOffset));
5380 lbu(object, FieldMemOperand(object, Map::kInstanceTypeOffset));
5381 Check(lo, kOperandIsNotAString, object, Operand(FIRST_NONSTRING_TYPE));
5382 pop(object);
5383 }
Ben Murdoch257744e2011-11-30 15:57:28 +00005384}
5385
5386
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005387void MacroAssembler::AssertName(Register object) {
5388 if (emit_debug_code()) {
5389 STATIC_ASSERT(kSmiTag == 0);
5390 SmiTst(object, t0);
5391 Check(ne, kOperandIsASmiAndNotAName, t0, Operand(zero_reg));
5392 push(object);
5393 lw(object, FieldMemOperand(object, HeapObject::kMapOffset));
5394 lbu(object, FieldMemOperand(object, Map::kInstanceTypeOffset));
5395 Check(le, kOperandIsNotAName, object, Operand(LAST_NAME_TYPE));
5396 pop(object);
5397 }
5398}
5399
5400
5401void MacroAssembler::AssertUndefinedOrAllocationSite(Register object,
5402 Register scratch) {
5403 if (emit_debug_code()) {
5404 Label done_checking;
5405 AssertNotSmi(object);
5406 LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5407 Branch(&done_checking, eq, object, Operand(scratch));
5408 push(object);
5409 lw(object, FieldMemOperand(object, HeapObject::kMapOffset));
5410 LoadRoot(scratch, Heap::kAllocationSiteMapRootIndex);
5411 Assert(eq, kExpectedUndefinedOrCell, object, Operand(scratch));
5412 pop(object);
5413 bind(&done_checking);
5414 }
5415}
5416
5417
5418void MacroAssembler::AssertIsRoot(Register reg, Heap::RootListIndex index) {
5419 if (emit_debug_code()) {
5420 DCHECK(!reg.is(at));
5421 LoadRoot(at, index);
5422 Check(eq, kHeapNumberMapRegisterClobbered, reg, Operand(at));
5423 }
Steve Block44f0eee2011-05-26 01:26:41 +01005424}
5425
5426
5427void MacroAssembler::JumpIfNotHeapNumber(Register object,
5428 Register heap_number_map,
5429 Register scratch,
5430 Label* on_not_heap_number) {
5431 lw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005432 AssertIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01005433 Branch(on_not_heap_number, ne, scratch, Operand(heap_number_map));
5434}
5435
5436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005437void MacroAssembler::LookupNumberStringCache(Register object,
5438 Register result,
5439 Register scratch1,
5440 Register scratch2,
5441 Register scratch3,
5442 Label* not_found) {
5443 // Use of registers. Register result is used as a temporary.
5444 Register number_string_cache = result;
5445 Register mask = scratch3;
5446
5447 // Load the number string cache.
5448 LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
5449
5450 // Make the hash mask from the length of the number string cache. It
5451 // contains two elements (number and string) for each cache entry.
5452 lw(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
5453 // Divide length by two (length is a smi).
5454 sra(mask, mask, kSmiTagSize + 1);
5455 Addu(mask, mask, -1); // Make mask.
5456
5457 // Calculate the entry in the number string cache. The hash value in the
5458 // number string cache for smis is just the smi value, and the hash for
5459 // doubles is the xor of the upper and lower words. See
5460 // Heap::GetNumberStringCache.
5461 Label is_smi;
5462 Label load_result_from_cache;
5463 JumpIfSmi(object, &is_smi);
5464 CheckMap(object,
5465 scratch1,
5466 Heap::kHeapNumberMapRootIndex,
5467 not_found,
5468 DONT_DO_SMI_CHECK);
5469
5470 STATIC_ASSERT(8 == kDoubleSize);
5471 Addu(scratch1,
5472 object,
5473 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
5474 lw(scratch2, MemOperand(scratch1, kPointerSize));
5475 lw(scratch1, MemOperand(scratch1, 0));
5476 Xor(scratch1, scratch1, Operand(scratch2));
5477 And(scratch1, scratch1, Operand(mask));
5478
5479 // Calculate address of entry in string cache: each entry consists
5480 // of two pointer sized fields.
5481 sll(scratch1, scratch1, kPointerSizeLog2 + 1);
5482 Addu(scratch1, number_string_cache, scratch1);
5483
5484 Register probe = mask;
5485 lw(probe, FieldMemOperand(scratch1, FixedArray::kHeaderSize));
5486 JumpIfSmi(probe, not_found);
5487 ldc1(f12, FieldMemOperand(object, HeapNumber::kValueOffset));
5488 ldc1(f14, FieldMemOperand(probe, HeapNumber::kValueOffset));
5489 BranchF(&load_result_from_cache, NULL, eq, f12, f14);
5490 Branch(not_found);
5491
5492 bind(&is_smi);
5493 Register scratch = scratch1;
5494 sra(scratch, object, 1); // Shift away the tag.
5495 And(scratch, mask, Operand(scratch));
5496
5497 // Calculate address of entry in string cache: each entry consists
5498 // of two pointer sized fields.
5499 sll(scratch, scratch, kPointerSizeLog2 + 1);
5500 Addu(scratch, number_string_cache, scratch);
5501
5502 // Check if the entry is the smi we are looking for.
5503 lw(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
5504 Branch(not_found, ne, object, Operand(probe));
5505
5506 // Get the result from the cache.
5507 bind(&load_result_from_cache);
5508 lw(result, FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
5509
5510 IncrementCounter(isolate()->counters()->number_to_string_native(),
5511 1,
5512 scratch1,
5513 scratch2);
5514}
5515
5516
5517void MacroAssembler::JumpIfNonSmisNotBothSequentialOneByteStrings(
5518 Register first, Register second, Register scratch1, Register scratch2,
Steve Block44f0eee2011-05-26 01:26:41 +01005519 Label* failure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005520 // Test that both first and second are sequential one-byte strings.
Steve Block44f0eee2011-05-26 01:26:41 +01005521 // Assume that they are non-smis.
5522 lw(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
5523 lw(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
5524 lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
5525 lbu(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
5526
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005527 JumpIfBothInstanceTypesAreNotSequentialOneByte(scratch1, scratch2, scratch1,
5528 scratch2, failure);
Steve Block44f0eee2011-05-26 01:26:41 +01005529}
5530
5531
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005532void MacroAssembler::JumpIfNotBothSequentialOneByteStrings(Register first,
5533 Register second,
5534 Register scratch1,
5535 Register scratch2,
5536 Label* failure) {
Steve Block44f0eee2011-05-26 01:26:41 +01005537 // Check that neither is a smi.
5538 STATIC_ASSERT(kSmiTag == 0);
5539 And(scratch1, first, Operand(second));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005540 JumpIfSmi(scratch1, failure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005541 JumpIfNonSmisNotBothSequentialOneByteStrings(first, second, scratch1,
5542 scratch2, failure);
Steve Block44f0eee2011-05-26 01:26:41 +01005543}
5544
5545
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005546void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialOneByte(
5547 Register first, Register second, Register scratch1, Register scratch2,
Steve Block44f0eee2011-05-26 01:26:41 +01005548 Label* failure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005549 const int kFlatOneByteStringMask =
Steve Block44f0eee2011-05-26 01:26:41 +01005550 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005551 const int kFlatOneByteStringTag =
5552 kStringTag | kOneByteStringTag | kSeqStringTag;
5553 DCHECK(kFlatOneByteStringTag <= 0xffff); // Ensure this fits 16-bit immed.
5554 andi(scratch1, first, kFlatOneByteStringMask);
5555 Branch(failure, ne, scratch1, Operand(kFlatOneByteStringTag));
5556 andi(scratch2, second, kFlatOneByteStringMask);
5557 Branch(failure, ne, scratch2, Operand(kFlatOneByteStringTag));
Steve Block44f0eee2011-05-26 01:26:41 +01005558}
5559
5560
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005561void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(Register type,
5562 Register scratch,
5563 Label* failure) {
5564 const int kFlatOneByteStringMask =
Steve Block44f0eee2011-05-26 01:26:41 +01005565 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005566 const int kFlatOneByteStringTag =
5567 kStringTag | kOneByteStringTag | kSeqStringTag;
5568 And(scratch, type, Operand(kFlatOneByteStringMask));
5569 Branch(failure, ne, scratch, Operand(kFlatOneByteStringTag));
Steve Block44f0eee2011-05-26 01:26:41 +01005570}
5571
5572
5573static const int kRegisterPassedArguments = 4;
5574
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005575int MacroAssembler::CalculateStackPassedWords(int num_reg_arguments,
5576 int num_double_arguments) {
5577 int stack_passed_words = 0;
5578 num_reg_arguments += 2 * num_double_arguments;
5579
5580 // Up to four simple arguments are passed in registers a0..a3.
5581 if (num_reg_arguments > kRegisterPassedArguments) {
5582 stack_passed_words += num_reg_arguments - kRegisterPassedArguments;
5583 }
5584 stack_passed_words += kCArgSlotCount;
5585 return stack_passed_words;
5586}
5587
5588
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005589void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
5590 Register index,
5591 Register value,
5592 Register scratch,
5593 uint32_t encoding_mask) {
5594 Label is_object;
5595 SmiTst(string, at);
5596 Check(ne, kNonObject, at, Operand(zero_reg));
5597
5598 lw(at, FieldMemOperand(string, HeapObject::kMapOffset));
5599 lbu(at, FieldMemOperand(at, Map::kInstanceTypeOffset));
5600
5601 andi(at, at, kStringRepresentationMask | kStringEncodingMask);
5602 li(scratch, Operand(encoding_mask));
5603 Check(eq, kUnexpectedStringType, at, Operand(scratch));
5604
5605 // The index is assumed to be untagged coming in, tag it to compare with the
5606 // string length without using a temp register, it is restored at the end of
5607 // this function.
5608 Label index_tag_ok, index_tag_bad;
5609 TrySmiTag(index, scratch, &index_tag_bad);
5610 Branch(&index_tag_ok);
5611 bind(&index_tag_bad);
5612 Abort(kIndexIsTooLarge);
5613 bind(&index_tag_ok);
5614
5615 lw(at, FieldMemOperand(string, String::kLengthOffset));
5616 Check(lt, kIndexIsTooLarge, index, Operand(at));
5617
5618 DCHECK(Smi::FromInt(0) == 0);
5619 Check(ge, kIndexIsNegative, index, Operand(zero_reg));
5620
5621 SmiUntag(index, index);
5622}
5623
5624
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005625void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
5626 int num_double_arguments,
5627 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01005628 int frame_alignment = ActivationFrameAlignment();
5629
Steve Block44f0eee2011-05-26 01:26:41 +01005630 // Up to four simple arguments are passed in registers a0..a3.
5631 // Those four arguments must have reserved argument slots on the stack for
5632 // mips, even though those argument slots are not normally used.
5633 // Remaining arguments are pushed on the stack, above (higher address than)
5634 // the argument slots.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005635 int stack_passed_arguments = CalculateStackPassedWords(
5636 num_reg_arguments, num_double_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01005637 if (frame_alignment > kPointerSize) {
5638 // Make stack end at alignment and make room for num_arguments - 4 words
5639 // and the original value of sp.
5640 mov(scratch, sp);
5641 Subu(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005642 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Steve Block44f0eee2011-05-26 01:26:41 +01005643 And(sp, sp, Operand(-frame_alignment));
5644 sw(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
5645 } else {
5646 Subu(sp, sp, Operand(stack_passed_arguments * kPointerSize));
5647 }
5648}
5649
5650
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005651void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
5652 Register scratch) {
5653 PrepareCallCFunction(num_reg_arguments, 0, scratch);
5654}
5655
5656
Steve Block44f0eee2011-05-26 01:26:41 +01005657void MacroAssembler::CallCFunction(ExternalReference function,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005658 int num_reg_arguments,
5659 int num_double_arguments) {
5660 li(t8, Operand(function));
5661 CallCFunctionHelper(t8, num_reg_arguments, num_double_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01005662}
5663
5664
5665void MacroAssembler::CallCFunction(Register function,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005666 int num_reg_arguments,
5667 int num_double_arguments) {
5668 CallCFunctionHelper(function, num_reg_arguments, num_double_arguments);
5669}
5670
5671
5672void MacroAssembler::CallCFunction(ExternalReference function,
Steve Block44f0eee2011-05-26 01:26:41 +01005673 int num_arguments) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005674 CallCFunction(function, num_arguments, 0);
5675}
5676
5677
5678void MacroAssembler::CallCFunction(Register function,
5679 int num_arguments) {
5680 CallCFunction(function, num_arguments, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01005681}
5682
5683
5684void MacroAssembler::CallCFunctionHelper(Register function,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005685 int num_reg_arguments,
5686 int num_double_arguments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005687 DCHECK(has_frame());
Steve Block44f0eee2011-05-26 01:26:41 +01005688 // Make sure that the stack is aligned before calling a C function unless
5689 // running in the simulator. The simulator has its own alignment check which
5690 // provides more information.
5691 // The argument stots are presumed to have been set up by
5692 // PrepareCallCFunction. The C function must be called via t9, for mips ABI.
5693
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005694#if V8_HOST_ARCH_MIPS
Steve Block44f0eee2011-05-26 01:26:41 +01005695 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005696 int frame_alignment = base::OS::ActivationFrameAlignment();
Steve Block44f0eee2011-05-26 01:26:41 +01005697 int frame_alignment_mask = frame_alignment - 1;
5698 if (frame_alignment > kPointerSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005699 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Steve Block44f0eee2011-05-26 01:26:41 +01005700 Label alignment_as_expected;
5701 And(at, sp, Operand(frame_alignment_mask));
5702 Branch(&alignment_as_expected, eq, at, Operand(zero_reg));
5703 // Don't use Check here, as it will call Runtime_Abort possibly
5704 // re-entering here.
5705 stop("Unexpected alignment in CallCFunction");
5706 bind(&alignment_as_expected);
5707 }
5708 }
5709#endif // V8_HOST_ARCH_MIPS
5710
5711 // Just call directly. The function called cannot cause a GC, or
5712 // allow preemption, so the return address in the link register
5713 // stays correct.
Steve Block44f0eee2011-05-26 01:26:41 +01005714
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005715 if (!function.is(t9)) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005716 mov(t9, function);
Steve Block44f0eee2011-05-26 01:26:41 +01005717 function = t9;
5718 }
5719
5720 Call(function);
5721
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005722 int stack_passed_arguments = CalculateStackPassedWords(
5723 num_reg_arguments, num_double_arguments);
Steve Block44f0eee2011-05-26 01:26:41 +01005724
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005725 if (base::OS::ActivationFrameAlignment() > kPointerSize) {
Steve Block44f0eee2011-05-26 01:26:41 +01005726 lw(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
5727 } else {
5728 Addu(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
5729 }
5730}
5731
5732
5733#undef BRANCH_ARGS_CHECK
5734
5735
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005736void MacroAssembler::PatchRelocatedValue(Register li_location,
5737 Register scratch,
5738 Register new_value) {
5739 lw(scratch, MemOperand(li_location));
5740 // At this point scratch is a lui(at, ...) instruction.
5741 if (emit_debug_code()) {
5742 And(scratch, scratch, kOpcodeMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005743 Check(eq, kTheInstructionToPatchShouldBeALui,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005744 scratch, Operand(LUI));
5745 lw(scratch, MemOperand(li_location));
5746 }
5747 srl(t9, new_value, kImm16Bits);
5748 Ins(scratch, t9, 0, kImm16Bits);
5749 sw(scratch, MemOperand(li_location));
5750
5751 lw(scratch, MemOperand(li_location, kInstrSize));
5752 // scratch is now ori(at, ...).
5753 if (emit_debug_code()) {
5754 And(scratch, scratch, kOpcodeMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005755 Check(eq, kTheInstructionToPatchShouldBeAnOri,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005756 scratch, Operand(ORI));
5757 lw(scratch, MemOperand(li_location, kInstrSize));
5758 }
5759 Ins(scratch, new_value, 0, kImm16Bits);
5760 sw(scratch, MemOperand(li_location, kInstrSize));
5761
5762 // Update the I-cache so the new lui and ori can be executed.
5763 FlushICache(li_location, 2);
5764}
5765
5766void MacroAssembler::GetRelocatedValue(Register li_location,
5767 Register value,
5768 Register scratch) {
5769 lw(value, MemOperand(li_location));
5770 if (emit_debug_code()) {
5771 And(value, value, kOpcodeMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005772 Check(eq, kTheInstructionShouldBeALui,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005773 value, Operand(LUI));
5774 lw(value, MemOperand(li_location));
5775 }
5776
5777 // value now holds a lui instruction. Extract the immediate.
5778 sll(value, value, kImm16Bits);
5779
5780 lw(scratch, MemOperand(li_location, kInstrSize));
5781 if (emit_debug_code()) {
5782 And(scratch, scratch, kOpcodeMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005783 Check(eq, kTheInstructionShouldBeAnOri,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005784 scratch, Operand(ORI));
5785 lw(scratch, MemOperand(li_location, kInstrSize));
5786 }
5787 // "scratch" now holds an ori instruction. Extract the immediate.
5788 andi(scratch, scratch, kImm16Mask);
5789
5790 // Merge the results.
5791 or_(value, value, scratch);
5792}
5793
5794
5795void MacroAssembler::CheckPageFlag(
5796 Register object,
5797 Register scratch,
5798 int mask,
5799 Condition cc,
5800 Label* condition_met) {
5801 And(scratch, object, Operand(~Page::kPageAlignmentMask));
5802 lw(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
5803 And(scratch, scratch, Operand(mask));
5804 Branch(condition_met, cc, scratch, Operand(zero_reg));
5805}
5806
5807
5808void MacroAssembler::JumpIfBlack(Register object,
5809 Register scratch0,
5810 Register scratch1,
5811 Label* on_black) {
5812 HasColor(object, scratch0, scratch1, on_black, 1, 0); // kBlackBitPattern.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005813 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005814}
5815
5816
5817void MacroAssembler::HasColor(Register object,
5818 Register bitmap_scratch,
5819 Register mask_scratch,
5820 Label* has_color,
5821 int first_bit,
5822 int second_bit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005823 DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, t8));
5824 DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, t9));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005825
5826 GetMarkBits(object, bitmap_scratch, mask_scratch);
5827
5828 Label other_color, word_boundary;
5829 lw(t9, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5830 And(t8, t9, Operand(mask_scratch));
5831 Branch(&other_color, first_bit == 1 ? eq : ne, t8, Operand(zero_reg));
5832 // Shift left 1 by adding.
5833 Addu(mask_scratch, mask_scratch, Operand(mask_scratch));
5834 Branch(&word_boundary, eq, mask_scratch, Operand(zero_reg));
5835 And(t8, t9, Operand(mask_scratch));
5836 Branch(has_color, second_bit == 1 ? ne : eq, t8, Operand(zero_reg));
5837 jmp(&other_color);
5838
5839 bind(&word_boundary);
5840 lw(t9, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize));
5841 And(t9, t9, Operand(1));
5842 Branch(has_color, second_bit == 1 ? ne : eq, t9, Operand(zero_reg));
5843 bind(&other_color);
5844}
5845
5846
5847// Detect some, but not all, common pointer-free objects. This is used by the
5848// incremental write barrier which doesn't care about oddballs (they are always
5849// marked black immediately so this code is not hit).
5850void MacroAssembler::JumpIfDataObject(Register value,
5851 Register scratch,
5852 Label* not_data_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005853 DCHECK(!AreAliased(value, scratch, t8, no_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005854 Label is_data_object;
5855 lw(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5856 LoadRoot(t8, Heap::kHeapNumberMapRootIndex);
5857 Branch(&is_data_object, eq, t8, Operand(scratch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005858 DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5859 DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005860 // If it's a string and it's not a cons string then it's an object containing
5861 // no GC pointers.
5862 lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
5863 And(t8, scratch, Operand(kIsIndirectStringMask | kIsNotStringMask));
5864 Branch(not_data_object, ne, t8, Operand(zero_reg));
5865 bind(&is_data_object);
5866}
5867
5868
5869void MacroAssembler::GetMarkBits(Register addr_reg,
5870 Register bitmap_reg,
5871 Register mask_reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005872 DCHECK(!AreAliased(addr_reg, bitmap_reg, mask_reg, no_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005873 And(bitmap_reg, addr_reg, Operand(~Page::kPageAlignmentMask));
5874 Ext(mask_reg, addr_reg, kPointerSizeLog2, Bitmap::kBitsPerCellLog2);
5875 const int kLowBits = kPointerSizeLog2 + Bitmap::kBitsPerCellLog2;
5876 Ext(t8, addr_reg, kLowBits, kPageSizeBits - kLowBits);
5877 sll(t8, t8, kPointerSizeLog2);
5878 Addu(bitmap_reg, bitmap_reg, t8);
5879 li(t8, Operand(1));
5880 sllv(mask_reg, t8, mask_reg);
5881}
5882
5883
5884void MacroAssembler::EnsureNotWhite(
5885 Register value,
5886 Register bitmap_scratch,
5887 Register mask_scratch,
5888 Register load_scratch,
5889 Label* value_is_white_and_not_data) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005890 DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, t8));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005891 GetMarkBits(value, bitmap_scratch, mask_scratch);
5892
5893 // If the value is black or grey we don't need to do anything.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005894 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
5895 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5896 DCHECK(strcmp(Marking::kGreyBitPattern, "11") == 0);
5897 DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005898
5899 Label done;
5900
5901 // Since both black and grey have a 1 in the first position and white does
5902 // not have a 1 there we only need to check one bit.
5903 lw(load_scratch, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5904 And(t8, mask_scratch, load_scratch);
5905 Branch(&done, ne, t8, Operand(zero_reg));
5906
5907 if (emit_debug_code()) {
5908 // Check for impossible bit pattern.
5909 Label ok;
5910 // sll may overflow, making the check conservative.
5911 sll(t8, mask_scratch, 1);
5912 And(t8, load_scratch, t8);
5913 Branch(&ok, eq, t8, Operand(zero_reg));
5914 stop("Impossible marking bit pattern");
5915 bind(&ok);
5916 }
5917
5918 // Value is white. We check whether it is data that doesn't need scanning.
5919 // Currently only checks for HeapNumber and non-cons strings.
5920 Register map = load_scratch; // Holds map while checking type.
5921 Register length = load_scratch; // Holds length of object after testing type.
5922 Label is_data_object;
5923
5924 // Check for heap-number
5925 lw(map, FieldMemOperand(value, HeapObject::kMapOffset));
5926 LoadRoot(t8, Heap::kHeapNumberMapRootIndex);
5927 {
5928 Label skip;
5929 Branch(&skip, ne, t8, Operand(map));
5930 li(length, HeapNumber::kSize);
5931 Branch(&is_data_object);
5932 bind(&skip);
5933 }
5934
5935 // Check for strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005936 DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5937 DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005938 // If it's a string and it's not a cons string then it's an object containing
5939 // no GC pointers.
5940 Register instance_type = load_scratch;
5941 lbu(instance_type, FieldMemOperand(map, Map::kInstanceTypeOffset));
5942 And(t8, instance_type, Operand(kIsIndirectStringMask | kIsNotStringMask));
5943 Branch(value_is_white_and_not_data, ne, t8, Operand(zero_reg));
5944 // It's a non-indirect (non-cons and non-slice) string.
5945 // If it's external, the length is just ExternalString::kSize.
5946 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
5947 // External strings are the only ones with the kExternalStringTag bit
5948 // set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005949 DCHECK_EQ(0, kSeqStringTag & kExternalStringTag);
5950 DCHECK_EQ(0, kConsStringTag & kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005951 And(t8, instance_type, Operand(kExternalStringTag));
5952 {
5953 Label skip;
5954 Branch(&skip, eq, t8, Operand(zero_reg));
5955 li(length, ExternalString::kSize);
5956 Branch(&is_data_object);
5957 bind(&skip);
5958 }
5959
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005960 // Sequential string, either Latin1 or UC16.
5961 // For Latin1 (char-size of 1) we shift the smi tag away to get the length.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005962 // For UC16 (char-size of 2) we just leave the smi tag in place, thereby
5963 // getting the length multiplied by 2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005964 DCHECK(kOneByteStringTag == 4 && kStringEncodingMask == 4);
5965 DCHECK(kSmiTag == 0 && kSmiTagSize == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005966 lw(t9, FieldMemOperand(value, String::kLengthOffset));
5967 And(t8, instance_type, Operand(kStringEncodingMask));
5968 {
5969 Label skip;
5970 Branch(&skip, eq, t8, Operand(zero_reg));
5971 srl(t9, t9, 1);
5972 bind(&skip);
5973 }
5974 Addu(length, t9, Operand(SeqString::kHeaderSize + kObjectAlignmentMask));
5975 And(length, length, Operand(~kObjectAlignmentMask));
5976
5977 bind(&is_data_object);
5978 // Value is a data object, and it is white. Mark it black. Since we know
5979 // that the object is white we can make it black by flipping one bit.
5980 lw(t8, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5981 Or(t8, t8, Operand(mask_scratch));
5982 sw(t8, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5983
5984 And(bitmap_scratch, bitmap_scratch, Operand(~Page::kPageAlignmentMask));
5985 lw(t8, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
5986 Addu(t8, t8, Operand(length));
5987 sw(t8, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
5988
5989 bind(&done);
5990}
5991
5992
Ben Murdoch257744e2011-11-30 15:57:28 +00005993void MacroAssembler::LoadInstanceDescriptors(Register map,
5994 Register descriptors) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005995 lw(descriptors, FieldMemOperand(map, Map::kDescriptorsOffset));
5996}
5997
5998
5999void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
6000 lw(dst, FieldMemOperand(map, Map::kBitField3Offset));
6001 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
6002}
6003
6004
6005void MacroAssembler::EnumLength(Register dst, Register map) {
6006 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
6007 lw(dst, FieldMemOperand(map, Map::kBitField3Offset));
6008 And(dst, dst, Operand(Map::EnumLengthBits::kMask));
6009 SmiTag(dst);
Ben Murdoch257744e2011-11-30 15:57:28 +00006010}
6011
6012
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006013void MacroAssembler::CheckEnumCache(Register null_value, Label* call_runtime) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006014 Register empty_fixed_array_value = t2;
6015 LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006016 Label next, start;
6017 mov(a2, a0);
6018
6019 // Check if the enum length field is properly initialized, indicating that
6020 // there is an enum cache.
6021 lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
6022
6023 EnumLength(a3, a1);
6024 Branch(
6025 call_runtime, eq, a3, Operand(Smi::FromInt(kInvalidEnumCacheSentinel)));
6026
6027 jmp(&start);
6028
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006029 bind(&next);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006030 lw(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006031
6032 // For all objects but the receiver, check that the cache is empty.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006033 EnumLength(a3, a1);
6034 Branch(call_runtime, ne, a3, Operand(Smi::FromInt(0)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006036 bind(&start);
6037
6038 // Check that there are no elements. Register a2 contains the current JS
6039 // object we've reached through the prototype chain.
6040 Label no_elements;
6041 lw(a2, FieldMemOperand(a2, JSObject::kElementsOffset));
6042 Branch(&no_elements, eq, a2, Operand(empty_fixed_array_value));
6043
6044 // Second chance, the object may be using the empty slow element dictionary.
6045 LoadRoot(at, Heap::kEmptySlowElementDictionaryRootIndex);
6046 Branch(call_runtime, ne, a2, Operand(at));
6047
6048 bind(&no_elements);
6049 lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
6050 Branch(&next, ne, a2, Operand(null_value));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006051}
6052
6053
6054void MacroAssembler::ClampUint8(Register output_reg, Register input_reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006055 DCHECK(!output_reg.is(input_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006056 Label done;
6057 li(output_reg, Operand(255));
6058 // Normal branch: nop in delay slot.
6059 Branch(&done, gt, input_reg, Operand(output_reg));
6060 // Use delay slot in this branch.
6061 Branch(USE_DELAY_SLOT, &done, lt, input_reg, Operand(zero_reg));
6062 mov(output_reg, zero_reg); // In delay slot.
6063 mov(output_reg, input_reg); // Value is in range 0..255.
6064 bind(&done);
6065}
6066
6067
6068void MacroAssembler::ClampDoubleToUint8(Register result_reg,
6069 DoubleRegister input_reg,
6070 DoubleRegister temp_double_reg) {
6071 Label above_zero;
6072 Label done;
6073 Label in_bounds;
6074
6075 Move(temp_double_reg, 0.0);
6076 BranchF(&above_zero, NULL, gt, input_reg, temp_double_reg);
6077
6078 // Double value is less than zero, NaN or Inf, return 0.
6079 mov(result_reg, zero_reg);
6080 Branch(&done);
6081
6082 // Double value is >= 255, return 255.
6083 bind(&above_zero);
6084 Move(temp_double_reg, 255.0);
6085 BranchF(&in_bounds, NULL, le, input_reg, temp_double_reg);
6086 li(result_reg, Operand(255));
6087 Branch(&done);
6088
6089 // In 0-255 range, round and truncate.
6090 bind(&in_bounds);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006091 cvt_w_d(temp_double_reg, input_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006092 mfc1(result_reg, temp_double_reg);
6093 bind(&done);
6094}
6095
6096
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006097void MacroAssembler::TestJSArrayForAllocationMemento(
6098 Register receiver_reg,
6099 Register scratch_reg,
6100 Label* no_memento_found,
6101 Condition cond,
6102 Label* allocation_memento_present) {
6103 ExternalReference new_space_start =
6104 ExternalReference::new_space_start(isolate());
6105 ExternalReference new_space_allocation_top =
6106 ExternalReference::new_space_allocation_top_address(isolate());
6107 Addu(scratch_reg, receiver_reg,
6108 Operand(JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
6109 Branch(no_memento_found, lt, scratch_reg, Operand(new_space_start));
6110 li(at, Operand(new_space_allocation_top));
6111 lw(at, MemOperand(at));
6112 Branch(no_memento_found, gt, scratch_reg, Operand(at));
6113 lw(scratch_reg, MemOperand(scratch_reg, -AllocationMemento::kSize));
6114 if (allocation_memento_present) {
6115 Branch(allocation_memento_present, cond, scratch_reg,
6116 Operand(isolate()->factory()->allocation_memento_map()));
6117 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006118}
6119
6120
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006121Register GetRegisterThatIsNotOneOf(Register reg1,
6122 Register reg2,
6123 Register reg3,
6124 Register reg4,
6125 Register reg5,
6126 Register reg6) {
6127 RegList regs = 0;
6128 if (reg1.is_valid()) regs |= reg1.bit();
6129 if (reg2.is_valid()) regs |= reg2.bit();
6130 if (reg3.is_valid()) regs |= reg3.bit();
6131 if (reg4.is_valid()) regs |= reg4.bit();
6132 if (reg5.is_valid()) regs |= reg5.bit();
6133 if (reg6.is_valid()) regs |= reg6.bit();
6134
6135 for (int i = 0; i < Register::NumAllocatableRegisters(); i++) {
6136 Register candidate = Register::FromAllocationIndex(i);
6137 if (regs & candidate.bit()) continue;
6138 return candidate;
6139 }
6140 UNREACHABLE();
6141 return no_reg;
6142}
6143
6144
6145void MacroAssembler::JumpIfDictionaryInPrototypeChain(
6146 Register object,
6147 Register scratch0,
6148 Register scratch1,
6149 Label* found) {
6150 DCHECK(!scratch1.is(scratch0));
6151 Factory* factory = isolate()->factory();
6152 Register current = scratch0;
6153 Label loop_again;
6154
6155 // Scratch contained elements pointer.
6156 Move(current, object);
6157
6158 // Loop based on the map going up the prototype chain.
6159 bind(&loop_again);
6160 lw(current, FieldMemOperand(current, HeapObject::kMapOffset));
6161 lb(scratch1, FieldMemOperand(current, Map::kBitField2Offset));
6162 DecodeField<Map::ElementsKindBits>(scratch1);
6163 Branch(found, eq, scratch1, Operand(DICTIONARY_ELEMENTS));
6164 lw(current, FieldMemOperand(current, Map::kPrototypeOffset));
6165 Branch(&loop_again, ne, current, Operand(factory->null_value()));
6166}
6167
6168
6169bool AreAliased(Register reg1,
6170 Register reg2,
6171 Register reg3,
6172 Register reg4,
6173 Register reg5,
6174 Register reg6,
6175 Register reg7,
6176 Register reg8) {
6177 int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
6178 reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
6179 reg7.is_valid() + reg8.is_valid();
6180
6181 RegList regs = 0;
6182 if (reg1.is_valid()) regs |= reg1.bit();
6183 if (reg2.is_valid()) regs |= reg2.bit();
6184 if (reg3.is_valid()) regs |= reg3.bit();
6185 if (reg4.is_valid()) regs |= reg4.bit();
6186 if (reg5.is_valid()) regs |= reg5.bit();
6187 if (reg6.is_valid()) regs |= reg6.bit();
6188 if (reg7.is_valid()) regs |= reg7.bit();
6189 if (reg8.is_valid()) regs |= reg8.bit();
6190 int n_of_non_aliasing_regs = NumRegs(regs);
6191
6192 return n_of_valid_regs != n_of_non_aliasing_regs;
6193}
6194
6195
6196CodePatcher::CodePatcher(byte* address,
6197 int instructions,
6198 FlushICache flush_cache)
Steve Block44f0eee2011-05-26 01:26:41 +01006199 : address_(address),
Steve Block44f0eee2011-05-26 01:26:41 +01006200 size_(instructions * Assembler::kInstrSize),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006201 masm_(NULL, address, size_ + Assembler::kGap),
6202 flush_cache_(flush_cache) {
Steve Block44f0eee2011-05-26 01:26:41 +01006203 // Create a new macro assembler pointing to the address of the code to patch.
6204 // The size is adjusted with kGap on order for the assembler to generate size
6205 // bytes of instructions without failing with buffer size constraints.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006206 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
Steve Block44f0eee2011-05-26 01:26:41 +01006207}
6208
6209
6210CodePatcher::~CodePatcher() {
6211 // Indicate that code has changed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006212 if (flush_cache_ == FLUSH) {
6213 CpuFeatures::FlushICache(address_, size_);
6214 }
Steve Block44f0eee2011-05-26 01:26:41 +01006215
6216 // Check that the code was patched as expected.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006217 DCHECK(masm_.pc_ == address_ + size_);
6218 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
Steve Block44f0eee2011-05-26 01:26:41 +01006219}
6220
6221
Ben Murdoch257744e2011-11-30 15:57:28 +00006222void CodePatcher::Emit(Instr instr) {
6223 masm()->emit(instr);
Steve Block44f0eee2011-05-26 01:26:41 +01006224}
6225
6226
6227void CodePatcher::Emit(Address addr) {
6228 masm()->emit(reinterpret_cast<Instr>(addr));
6229}
6230
6231
Ben Murdoch257744e2011-11-30 15:57:28 +00006232void CodePatcher::ChangeBranchCondition(Condition cond) {
6233 Instr instr = Assembler::instr_at(masm_.pc_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006234 DCHECK(Assembler::IsBranch(instr));
Ben Murdoch257744e2011-11-30 15:57:28 +00006235 uint32_t opcode = Assembler::GetOpcodeField(instr);
6236 // Currently only the 'eq' and 'ne' cond values are supported and the simple
6237 // branch instructions (with opcode being the branch type).
6238 // There are some special cases (see Assembler::IsBranch()) so extending this
6239 // would be tricky.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006240 DCHECK(opcode == BEQ ||
Ben Murdoch257744e2011-11-30 15:57:28 +00006241 opcode == BNE ||
6242 opcode == BLEZ ||
6243 opcode == BGTZ ||
6244 opcode == BEQL ||
6245 opcode == BNEL ||
6246 opcode == BLEZL ||
6247 opcode == BGTZL);
6248 opcode = (cond == eq) ? BEQ : BNE;
6249 instr = (instr & ~kOpcodeMask) | opcode;
6250 masm_.emit(instr);
6251}
Steve Block44f0eee2011-05-26 01:26:41 +01006252
6253
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006254void MacroAssembler::TruncatingDiv(Register result,
6255 Register dividend,
6256 int32_t divisor) {
6257 DCHECK(!dividend.is(result));
6258 DCHECK(!dividend.is(at));
6259 DCHECK(!result.is(at));
6260 base::MagicNumbersForDivision<uint32_t> mag =
6261 base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
6262 li(at, Operand(mag.multiplier));
6263 Mulh(result, dividend, Operand(at));
6264 bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
6265 if (divisor > 0 && neg) {
6266 Addu(result, result, Operand(dividend));
6267 }
6268 if (divisor < 0 && !neg && mag.multiplier > 0) {
6269 Subu(result, result, Operand(dividend));
6270 }
6271 if (mag.shift > 0) sra(result, result, mag.shift);
6272 srl(at, dividend, 31);
6273 Addu(result, result, Operand(at));
6274}
6275
6276
Andrei Popescu31002712010-02-23 13:46:05 +00006277} } // namespace v8::internal
6278
Leon Clarkef7060e22010-06-03 12:02:55 +01006279#endif // V8_TARGET_ARCH_MIPS