blob: 7480a6f9ef9b992b27f92c6139c38d0d6b048984 [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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_IA32
Leon Clarkef7060e22010-06-03 12:02:55 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/base/division-by-constant.h"
11#include "src/bootstrapper.h"
12#include "src/codegen.h"
13#include "src/cpu-profiler.h"
14#include "src/debug.h"
15#include "src/isolate-inl.h"
16#include "src/runtime.h"
17#include "src/serialize.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000018
19namespace v8 {
20namespace internal {
21
22// -------------------------------------------------------------------------
23// MacroAssembler implementation.
24
Ben Murdoch8b112d22011-06-08 16:22:53 +010025MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
26 : Assembler(arg_isolate, buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000027 generating_stub_(false),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010028 has_frame_(false) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010029 if (isolate() != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 // TODO(titzer): should we just use a null handle here instead?
Ben Murdoch8b112d22011-06-08 16:22:53 +010031 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
32 isolate());
33 }
Steve Blocka7e24c12009-10-30 11:49:00 +000034}
35
36
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037void MacroAssembler::Load(Register dst, const Operand& src, Representation r) {
38 DCHECK(!r.IsDouble());
39 if (r.IsInteger8()) {
40 movsx_b(dst, src);
41 } else if (r.IsUInteger8()) {
42 movzx_b(dst, src);
43 } else if (r.IsInteger16()) {
44 movsx_w(dst, src);
45 } else if (r.IsUInteger16()) {
46 movzx_w(dst, src);
47 } else {
48 mov(dst, src);
49 }
50}
51
52
53void MacroAssembler::Store(Register src, const Operand& dst, Representation r) {
54 DCHECK(!r.IsDouble());
55 if (r.IsInteger8() || r.IsUInteger8()) {
56 mov_b(dst, src);
57 } else if (r.IsInteger16() || r.IsUInteger16()) {
58 mov_w(dst, src);
59 } else {
60 if (r.IsHeapObject()) {
61 AssertNotSmi(src);
62 } else if (r.IsSmi()) {
63 AssertSmi(src);
64 }
65 mov(dst, src);
66 }
67}
68
69
70void MacroAssembler::LoadRoot(Register destination, Heap::RootListIndex index) {
71 if (isolate()->heap()->RootCanBeTreatedAsConstant(index)) {
72 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
73 mov(destination, value);
74 return;
75 }
76 ExternalReference roots_array_start =
77 ExternalReference::roots_array_start(isolate());
78 mov(destination, Immediate(index));
79 mov(destination, Operand::StaticArray(destination,
80 times_pointer_size,
81 roots_array_start));
82}
83
84
85void MacroAssembler::StoreRoot(Register source,
86 Register scratch,
87 Heap::RootListIndex index) {
88 DCHECK(Heap::RootCanBeWrittenAfterInitialization(index));
89 ExternalReference roots_array_start =
90 ExternalReference::roots_array_start(isolate());
91 mov(scratch, Immediate(index));
92 mov(Operand::StaticArray(scratch, times_pointer_size, roots_array_start),
93 source);
94}
95
96
97void MacroAssembler::CompareRoot(Register with,
98 Register scratch,
99 Heap::RootListIndex index) {
100 ExternalReference roots_array_start =
101 ExternalReference::roots_array_start(isolate());
102 mov(scratch, Immediate(index));
103 cmp(with, Operand::StaticArray(scratch,
104 times_pointer_size,
105 roots_array_start));
106}
107
108
109void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
110 DCHECK(isolate()->heap()->RootCanBeTreatedAsConstant(index));
111 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
112 cmp(with, value);
113}
114
115
116void MacroAssembler::CompareRoot(const Operand& with,
117 Heap::RootListIndex index) {
118 DCHECK(isolate()->heap()->RootCanBeTreatedAsConstant(index));
119 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
120 cmp(with, value);
121}
122
123
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100124void MacroAssembler::InNewSpace(
125 Register object,
126 Register scratch,
127 Condition cc,
128 Label* condition_met,
129 Label::Distance condition_met_distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 DCHECK(cc == equal || cc == not_equal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100131 if (scratch.is(object)) {
132 and_(scratch, Immediate(~Page::kPageAlignmentMask));
133 } else {
134 mov(scratch, Immediate(~Page::kPageAlignmentMask));
135 and_(scratch, object);
Steve Block6ded16b2010-05-10 14:33:55 +0100136 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100137 // Check that we can use a test_b.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 DCHECK(MemoryChunk::IN_FROM_SPACE < 8);
139 DCHECK(MemoryChunk::IN_TO_SPACE < 8);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100140 int mask = (1 << MemoryChunk::IN_FROM_SPACE)
141 | (1 << MemoryChunk::IN_TO_SPACE);
142 // If non-zero, the page belongs to new-space.
143 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
144 static_cast<uint8_t>(mask));
145 j(cc, condition_met, condition_met_distance);
146}
Steve Block6ded16b2010-05-10 14:33:55 +0100147
Steve Blocka7e24c12009-10-30 11:49:00 +0000148
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100149void MacroAssembler::RememberedSetHelper(
150 Register object, // Only used for debug checks.
151 Register addr,
152 Register scratch,
153 SaveFPRegsMode save_fp,
154 MacroAssembler::RememberedSetFinalAction and_then) {
155 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100157 Label ok;
158 JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
159 int3();
160 bind(&ok);
161 }
162 // Load store buffer top.
163 ExternalReference store_buffer =
164 ExternalReference::store_buffer_top(isolate());
165 mov(scratch, Operand::StaticVariable(store_buffer));
166 // Store pointer to buffer.
167 mov(Operand(scratch, 0), addr);
168 // Increment buffer top.
169 add(scratch, Immediate(kPointerSize));
170 // Write back new top of buffer.
171 mov(Operand::StaticVariable(store_buffer), scratch);
172 // Call stub on end of buffer.
173 // Check for end of buffer.
174 test(scratch, Immediate(StoreBuffer::kStoreBufferOverflowBit));
175 if (and_then == kReturnAtEnd) {
176 Label buffer_overflowed;
177 j(not_equal, &buffer_overflowed, Label::kNear);
178 ret(0);
179 bind(&buffer_overflowed);
180 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 DCHECK(and_then == kFallThroughAtEnd);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100182 j(equal, &done, Label::kNear);
183 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 StoreBufferOverflowStub store_buffer_overflow(isolate(), save_fp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100185 CallStub(&store_buffer_overflow);
186 if (and_then == kReturnAtEnd) {
187 ret(0);
188 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 DCHECK(and_then == kFallThroughAtEnd);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100190 bind(&done);
191 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000192}
193
194
195void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
196 XMMRegister scratch_reg,
197 Register result_reg) {
198 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 Label conv_failure;
200 xorps(scratch_reg, scratch_reg);
201 cvtsd2si(result_reg, input_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +0000202 test(result_reg, Immediate(0xFFFFFF00));
203 j(zero, &done, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 cmp(result_reg, Immediate(0x1));
205 j(overflow, &conv_failure, Label::kNear);
206 mov(result_reg, Immediate(0));
207 setcc(sign, result_reg);
208 sub(result_reg, Immediate(1));
209 and_(result_reg, Immediate(255));
210 jmp(&done, Label::kNear);
211 bind(&conv_failure);
212 Move(result_reg, Immediate(0));
213 ucomisd(input_reg, scratch_reg);
214 j(below, &done, Label::kNear);
215 Move(result_reg, Immediate(255));
Ben Murdoch257744e2011-11-30 15:57:28 +0000216 bind(&done);
217}
218
219
220void MacroAssembler::ClampUint8(Register reg) {
221 Label done;
222 test(reg, Immediate(0xFFFFFF00));
223 j(zero, &done, Label::kNear);
224 setcc(negative, reg); // 1 if negative, 0 if positive.
225 dec_b(reg); // 0 if negative, 255 if positive.
226 bind(&done);
227}
228
229
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230void MacroAssembler::SlowTruncateToI(Register result_reg,
231 Register input_reg,
232 int offset) {
233 DoubleToIStub stub(isolate(), input_reg, result_reg, offset, true);
234 call(stub.GetCode(), RelocInfo::CODE_TARGET);
235}
236
237
238void MacroAssembler::TruncateDoubleToI(Register result_reg,
239 XMMRegister input_reg) {
240 Label done;
241 cvttsd2si(result_reg, Operand(input_reg));
242 cmp(result_reg, 0x1);
243 j(no_overflow, &done, Label::kNear);
244
245 sub(esp, Immediate(kDoubleSize));
246 movsd(MemOperand(esp, 0), input_reg);
247 SlowTruncateToI(result_reg, esp, 0);
248 add(esp, Immediate(kDoubleSize));
249 bind(&done);
250}
251
252
253void MacroAssembler::DoubleToI(Register result_reg, XMMRegister input_reg,
254 XMMRegister scratch,
255 MinusZeroMode minus_zero_mode,
256 Label* lost_precision, Label* is_nan,
257 Label* minus_zero, Label::Distance dst) {
258 DCHECK(!input_reg.is(scratch));
259 cvttsd2si(result_reg, Operand(input_reg));
260 Cvtsi2sd(scratch, Operand(result_reg));
261 ucomisd(scratch, input_reg);
262 j(not_equal, lost_precision, dst);
263 j(parity_even, is_nan, dst);
264 if (minus_zero_mode == FAIL_ON_MINUS_ZERO) {
265 Label done;
266 // The integer converted back is equal to the original. We
267 // only have to test if we got -0 as an input.
268 test(result_reg, Operand(result_reg));
269 j(not_zero, &done, Label::kNear);
270 movmskpd(result_reg, input_reg);
271 // Bit 0 contains the sign of the double in input_reg.
272 // If input was positive, we are ok and return 0, otherwise
273 // jump to minus_zero.
274 and_(result_reg, 1);
275 j(not_zero, minus_zero, dst);
276 bind(&done);
277 }
278}
279
280
281void MacroAssembler::TruncateHeapNumberToI(Register result_reg,
282 Register input_reg) {
283 Label done, slow_case;
284
285 if (CpuFeatures::IsSupported(SSE3)) {
286 CpuFeatureScope scope(this, SSE3);
287 Label convert;
288 // Use more powerful conversion when sse3 is available.
289 // Load x87 register with heap number.
290 fld_d(FieldOperand(input_reg, HeapNumber::kValueOffset));
291 // Get exponent alone and check for too-big exponent.
292 mov(result_reg, FieldOperand(input_reg, HeapNumber::kExponentOffset));
293 and_(result_reg, HeapNumber::kExponentMask);
294 const uint32_t kTooBigExponent =
295 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
296 cmp(Operand(result_reg), Immediate(kTooBigExponent));
297 j(greater_equal, &slow_case, Label::kNear);
298
299 // Reserve space for 64 bit answer.
300 sub(Operand(esp), Immediate(kDoubleSize));
301 // Do conversion, which cannot fail because we checked the exponent.
302 fisttp_d(Operand(esp, 0));
303 mov(result_reg, Operand(esp, 0)); // Low word of answer is the result.
304 add(Operand(esp), Immediate(kDoubleSize));
305 jmp(&done, Label::kNear);
306
307 // Slow case.
308 bind(&slow_case);
309 if (input_reg.is(result_reg)) {
310 // Input is clobbered. Restore number from fpu stack
311 sub(Operand(esp), Immediate(kDoubleSize));
312 fstp_d(Operand(esp, 0));
313 SlowTruncateToI(result_reg, esp, 0);
314 add(esp, Immediate(kDoubleSize));
315 } else {
316 fstp(0);
317 SlowTruncateToI(result_reg, input_reg);
318 }
319 } else {
320 movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
321 cvttsd2si(result_reg, Operand(xmm0));
322 cmp(result_reg, 0x1);
323 j(no_overflow, &done, Label::kNear);
324 // Check if the input was 0x8000000 (kMinInt).
325 // If no, then we got an overflow and we deoptimize.
326 ExternalReference min_int = ExternalReference::address_of_min_int();
327 ucomisd(xmm0, Operand::StaticVariable(min_int));
328 j(not_equal, &slow_case, Label::kNear);
329 j(parity_even, &slow_case, Label::kNear); // NaN.
330 jmp(&done, Label::kNear);
331
332 // Slow case.
333 bind(&slow_case);
334 if (input_reg.is(result_reg)) {
335 // Input is clobbered. Restore number from double scratch.
336 sub(esp, Immediate(kDoubleSize));
337 movsd(MemOperand(esp, 0), xmm0);
338 SlowTruncateToI(result_reg, esp, 0);
339 add(esp, Immediate(kDoubleSize));
340 } else {
341 SlowTruncateToI(result_reg, input_reg);
342 }
343 }
344 bind(&done);
345}
346
347
348void MacroAssembler::LoadUint32(XMMRegister dst,
349 Register src) {
350 Label done;
351 cmp(src, Immediate(0));
352 ExternalReference uint32_bias =
353 ExternalReference::address_of_uint32_bias();
354 Cvtsi2sd(dst, src);
355 j(not_sign, &done, Label::kNear);
356 addsd(dst, Operand::StaticVariable(uint32_bias));
357 bind(&done);
358}
359
360
361void MacroAssembler::RecordWriteArray(
362 Register object,
363 Register value,
364 Register index,
365 SaveFPRegsMode save_fp,
366 RememberedSetAction remembered_set_action,
367 SmiCheck smi_check,
368 PointersToHereCheck pointers_to_here_check_for_value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100369 // First, check if a write barrier is even needed. The tests below
370 // catch stores of Smis.
371 Label done;
372
373 // Skip barrier if writing a smi.
374 if (smi_check == INLINE_SMI_CHECK) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375 DCHECK_EQ(0, kSmiTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100376 test(value, Immediate(kSmiTagMask));
377 j(zero, &done);
378 }
379
380 // Array access: calculate the destination address in the same manner as
381 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
382 // into an array of words.
383 Register dst = index;
384 lea(dst, Operand(object, index, times_half_pointer_size,
385 FixedArray::kHeaderSize - kHeapObjectTag));
386
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 RecordWrite(object, dst, value, save_fp, remembered_set_action,
388 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100389
390 bind(&done);
391
392 // Clobber clobbered input registers when running with the debug-code flag
393 // turned on to provoke errors.
394 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
396 mov(index, Immediate(bit_cast<int32_t>(kZapValue)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000397 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000398}
399
400
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100401void MacroAssembler::RecordWriteField(
402 Register object,
403 int offset,
404 Register value,
405 Register dst,
406 SaveFPRegsMode save_fp,
407 RememberedSetAction remembered_set_action,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 SmiCheck smi_check,
409 PointersToHereCheck pointers_to_here_check_for_value) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100410 // First, check if a write barrier is even needed. The tests below
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100411 // catch stores of Smis.
Ben Murdoch257744e2011-11-30 15:57:28 +0000412 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000413
414 // Skip barrier if writing a smi.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100415 if (smi_check == INLINE_SMI_CHECK) {
416 JumpIfSmi(value, &done, Label::kNear);
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418
419 // Although the object register is tagged, the offset is relative to the start
420 // of the object, so so offset must be a multiple of kPointerSize.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 DCHECK(IsAligned(offset, kPointerSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100422
423 lea(dst, FieldOperand(object, offset));
424 if (emit_debug_code()) {
425 Label ok;
426 test_b(dst, (1 << kPointerSizeLog2) - 1);
427 j(zero, &ok, Label::kNear);
428 int3();
429 bind(&ok);
430 }
431
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 RecordWrite(object, dst, value, save_fp, remembered_set_action,
433 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000434
435 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000436
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100437 // Clobber clobbered input registers when running with the debug-code flag
Leon Clarke4515c472010-02-03 11:58:03 +0000438 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100439 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
441 mov(dst, Immediate(bit_cast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000442 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000443}
444
445
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446void MacroAssembler::RecordWriteForMap(
447 Register object,
448 Handle<Map> map,
449 Register scratch1,
450 Register scratch2,
451 SaveFPRegsMode save_fp) {
452 Label done;
453
454 Register address = scratch1;
455 Register value = scratch2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100456 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000457 Label ok;
458 lea(address, FieldOperand(object, HeapObject::kMapOffset));
459 test_b(address, (1 << kPointerSizeLog2) - 1);
460 j(zero, &ok, Label::kNear);
461 int3();
462 bind(&ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100463 }
464
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 DCHECK(!object.is(value));
466 DCHECK(!object.is(address));
467 DCHECK(!value.is(address));
468 AssertNotSmi(object);
469
470 if (!FLAG_incremental_marking) {
471 return;
472 }
473
474 // Compute the address.
475 lea(address, FieldOperand(object, HeapObject::kMapOffset));
476
477 // A single check of the map's pages interesting flag suffices, since it is
478 // only set during incremental collection, and then it's also guaranteed that
479 // the from object's page's interesting flag is also set. This optimization
480 // relies on the fact that maps can never be in new space.
481 DCHECK(!isolate()->heap()->InNewSpace(*map));
482 CheckPageFlagForMap(map,
483 MemoryChunk::kPointersToHereAreInterestingMask,
484 zero,
485 &done,
486 Label::kNear);
487
488 RecordWriteStub stub(isolate(), object, value, address, OMIT_REMEMBERED_SET,
489 save_fp);
490 CallStub(&stub);
491
492 bind(&done);
493
494 // Count number of write barriers in generated code.
495 isolate()->counters()->write_barriers_static()->Increment();
496 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
497
498 // Clobber clobbered input registers when running with the debug-code flag
499 // turned on to provoke errors.
500 if (emit_debug_code()) {
501 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
502 mov(scratch1, Immediate(bit_cast<int32_t>(kZapValue)));
503 mov(scratch2, Immediate(bit_cast<int32_t>(kZapValue)));
504 }
505}
506
507
508void MacroAssembler::RecordWrite(
509 Register object,
510 Register address,
511 Register value,
512 SaveFPRegsMode fp_mode,
513 RememberedSetAction remembered_set_action,
514 SmiCheck smi_check,
515 PointersToHereCheck pointers_to_here_check_for_value) {
516 DCHECK(!object.is(value));
517 DCHECK(!object.is(address));
518 DCHECK(!value.is(address));
519 AssertNotSmi(object);
520
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100521 if (remembered_set_action == OMIT_REMEMBERED_SET &&
522 !FLAG_incremental_marking) {
523 return;
524 }
525
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100527 Label ok;
528 cmp(value, Operand(address, 0));
529 j(equal, &ok, Label::kNear);
530 int3();
531 bind(&ok);
532 }
533
Steve Block8defd9f2010-07-08 12:39:36 +0100534 // First, check if a write barrier is even needed. The tests below
535 // catch stores of Smis and stores into young gen.
536 Label done;
537
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100538 if (smi_check == INLINE_SMI_CHECK) {
539 // Skip barrier if writing a smi.
540 JumpIfSmi(value, &done, Label::kNear);
541 }
Steve Block8defd9f2010-07-08 12:39:36 +0100542
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
544 CheckPageFlag(value,
545 value, // Used as scratch.
546 MemoryChunk::kPointersToHereAreInterestingMask,
547 zero,
548 &done,
549 Label::kNear);
550 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100551 CheckPageFlag(object,
552 value, // Used as scratch.
553 MemoryChunk::kPointersFromHereAreInterestingMask,
554 zero,
555 &done,
556 Label::kNear);
Steve Block8defd9f2010-07-08 12:39:36 +0100557
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
559 fp_mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100560 CallStub(&stub);
Steve Block8defd9f2010-07-08 12:39:36 +0100561
562 bind(&done);
563
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564 // Count number of write barriers in generated code.
565 isolate()->counters()->write_barriers_static()->Increment();
566 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
567
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100568 // Clobber clobbered registers when running with the debug-code flag
Steve Block8defd9f2010-07-08 12:39:36 +0100569 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100570 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 mov(address, Immediate(bit_cast<int32_t>(kZapValue)));
572 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
Steve Block8defd9f2010-07-08 12:39:36 +0100573 }
574}
575
576
Andrei Popescu402d9372010-02-26 13:31:12 +0000577void MacroAssembler::DebugBreak() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 Move(eax, Immediate(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100579 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 CEntryStub ces(isolate(), 1);
Andrei Popescu402d9372010-02-26 13:31:12 +0000581 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
582}
Steve Blocka7e24c12009-10-30 11:49:00 +0000583
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100584
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000585void MacroAssembler::Cvtsi2sd(XMMRegister dst, const Operand& src) {
586 xorps(dst, dst);
587 cvtsi2sd(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000588}
589
590
Steve Block053d10c2011-06-13 19:13:29 +0100591bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
592 static const int kMaxImmediateBits = 17;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 if (!RelocInfo::IsNone(x.rmode_)) return false;
Steve Block053d10c2011-06-13 19:13:29 +0100594 return !is_intn(x.x_, kMaxImmediateBits);
595}
596
597
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598void MacroAssembler::SafeMove(Register dst, const Immediate& x) {
Steve Block053d10c2011-06-13 19:13:29 +0100599 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000600 Move(dst, Immediate(x.x_ ^ jit_cookie()));
Steve Block053d10c2011-06-13 19:13:29 +0100601 xor_(dst, jit_cookie());
602 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000603 Move(dst, x);
Steve Block053d10c2011-06-13 19:13:29 +0100604 }
605}
606
607
608void MacroAssembler::SafePush(const Immediate& x) {
609 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
610 push(Immediate(x.x_ ^ jit_cookie()));
611 xor_(Operand(esp, 0), Immediate(jit_cookie()));
612 } else {
613 push(x);
614 }
615}
616
617
Steve Blocka7e24c12009-10-30 11:49:00 +0000618void MacroAssembler::CmpObjectType(Register heap_object,
619 InstanceType type,
620 Register map) {
621 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
622 CmpInstanceType(map, type);
623}
624
625
626void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
627 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
628 static_cast<int8_t>(type));
629}
630
631
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000632void MacroAssembler::CheckFastElements(Register map,
633 Label* fail,
634 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
636 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
637 STATIC_ASSERT(FAST_ELEMENTS == 2);
638 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000639 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000640 Map::kMaximumBitField2FastHoleyElementValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000641 j(above, fail, distance);
642}
643
644
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100645void MacroAssembler::CheckFastObjectElements(Register map,
646 Label* fail,
647 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
649 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
650 STATIC_ASSERT(FAST_ELEMENTS == 2);
651 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100652 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653 Map::kMaximumBitField2FastHoleySmiElementValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100654 j(below_equal, fail, distance);
655 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656 Map::kMaximumBitField2FastHoleyElementValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100657 j(above, fail, distance);
658}
659
660
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661void MacroAssembler::CheckFastSmiElements(Register map,
662 Label* fail,
663 Label::Distance distance) {
664 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
665 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100666 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667 Map::kMaximumBitField2FastHoleySmiElementValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100668 j(above, fail, distance);
669}
670
671
672void MacroAssembler::StoreNumberToDoubleElements(
673 Register maybe_number,
674 Register elements,
675 Register key,
676 Register scratch1,
677 XMMRegister scratch2,
678 Label* fail,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000679 int elements_offset) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100680 Label smi_value, done, maybe_nan, not_nan, is_nan, have_double_value;
681 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
682
683 CheckMap(maybe_number,
684 isolate()->factory()->heap_number_map(),
685 fail,
686 DONT_DO_SMI_CHECK);
687
688 // Double value, canonicalize NaN.
689 uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
690 cmp(FieldOperand(maybe_number, offset),
691 Immediate(kNaNOrInfinityLowerBoundUpper32));
692 j(greater_equal, &maybe_nan, Label::kNear);
693
694 bind(&not_nan);
695 ExternalReference canonical_nan_reference =
696 ExternalReference::address_of_canonical_non_hole_nan();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000697 movsd(scratch2, FieldOperand(maybe_number, HeapNumber::kValueOffset));
698 bind(&have_double_value);
699 movsd(FieldOperand(elements, key, times_4,
700 FixedDoubleArray::kHeaderSize - elements_offset),
701 scratch2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100702 jmp(&done);
703
704 bind(&maybe_nan);
705 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
706 // it's an Infinity, and the non-NaN code path applies.
707 j(greater, &is_nan, Label::kNear);
708 cmp(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
709 j(zero, &not_nan);
710 bind(&is_nan);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000711 movsd(scratch2, Operand::StaticVariable(canonical_nan_reference));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100712 jmp(&have_double_value, Label::kNear);
713
714 bind(&smi_value);
715 // Value is a smi. Convert to a double and store.
716 // Preserve original value.
717 mov(scratch1, maybe_number);
718 SmiUntag(scratch1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 Cvtsi2sd(scratch2, scratch1);
720 movsd(FieldOperand(elements, key, times_4,
721 FixedDoubleArray::kHeaderSize - elements_offset),
722 scratch2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100723 bind(&done);
724}
725
726
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000727void MacroAssembler::CompareMap(Register obj, Handle<Map> map) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100728 cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100729}
730
731
Andrei Popescu31002712010-02-23 13:46:05 +0000732void MacroAssembler::CheckMap(Register obj,
733 Handle<Map> map,
734 Label* fail,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735 SmiCheckType smi_check_type) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000736 if (smi_check_type == DO_SMI_CHECK) {
737 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +0000738 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100739
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000740 CompareMap(obj, map);
Andrei Popescu31002712010-02-23 13:46:05 +0000741 j(not_equal, fail);
742}
743
744
Ben Murdoch257744e2011-11-30 15:57:28 +0000745void MacroAssembler::DispatchMap(Register obj,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746 Register unused,
Ben Murdoch257744e2011-11-30 15:57:28 +0000747 Handle<Map> map,
748 Handle<Code> success,
749 SmiCheckType smi_check_type) {
750 Label fail;
751 if (smi_check_type == DO_SMI_CHECK) {
752 JumpIfSmi(obj, &fail);
753 }
754 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
755 j(equal, success);
756
757 bind(&fail);
758}
759
760
Leon Clarkee46be812010-01-19 14:06:41 +0000761Condition MacroAssembler::IsObjectStringType(Register heap_object,
762 Register map,
763 Register instance_type) {
764 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
765 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000766 STATIC_ASSERT(kNotStringTag != 0);
Leon Clarkee46be812010-01-19 14:06:41 +0000767 test(instance_type, Immediate(kIsNotStringMask));
768 return zero;
769}
770
771
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772Condition MacroAssembler::IsObjectNameType(Register heap_object,
773 Register map,
774 Register instance_type) {
775 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
776 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
777 cmpb(instance_type, static_cast<uint8_t>(LAST_NAME_TYPE));
778 return below_equal;
779}
780
781
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100782void MacroAssembler::IsObjectJSObjectType(Register heap_object,
783 Register map,
784 Register scratch,
785 Label* fail) {
786 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
787 IsInstanceJSObjectType(map, scratch, fail);
788}
789
790
791void MacroAssembler::IsInstanceJSObjectType(Register map,
792 Register scratch,
793 Label* fail) {
794 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100795 sub(scratch, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000796 cmp(scratch,
797 LAST_NONCALLABLE_SPEC_OBJECT_TYPE - FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100798 j(above, fail);
799}
800
801
Steve Blocka7e24c12009-10-30 11:49:00 +0000802void MacroAssembler::FCmp() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000803 fucomip();
804 fstp(0);
805}
806
807
808void MacroAssembler::AssertNumber(Register object) {
809 if (emit_debug_code()) {
810 Label ok;
811 JumpIfSmi(object, &ok);
812 cmp(FieldOperand(object, HeapObject::kMapOffset),
813 isolate()->factory()->heap_number_map());
814 Check(equal, kOperandNotANumber);
815 bind(&ok);
Steve Block3ce2e202009-11-05 08:53:23 +0000816 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000817}
818
819
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000820void MacroAssembler::AssertSmi(Register object) {
821 if (emit_debug_code()) {
822 test(object, Immediate(kSmiTagMask));
823 Check(equal, kOperandIsNotASmi);
824 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000825}
826
827
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828void MacroAssembler::AssertString(Register object) {
829 if (emit_debug_code()) {
830 test(object, Immediate(kSmiTagMask));
831 Check(not_equal, kOperandIsASmiAndNotAString);
832 push(object);
833 mov(object, FieldOperand(object, HeapObject::kMapOffset));
834 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
835 pop(object);
836 Check(below, kOperandIsNotAString);
837 }
Iain Merrick75681382010-08-19 15:07:18 +0100838}
839
840
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000841void MacroAssembler::AssertName(Register object) {
842 if (emit_debug_code()) {
843 test(object, Immediate(kSmiTagMask));
844 Check(not_equal, kOperandIsASmiAndNotAName);
845 push(object);
846 mov(object, FieldOperand(object, HeapObject::kMapOffset));
847 CmpInstanceType(object, LAST_NAME_TYPE);
848 pop(object);
849 Check(below_equal, kOperandIsNotAName);
850 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100851}
852
853
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000854void MacroAssembler::AssertUndefinedOrAllocationSite(Register object) {
855 if (emit_debug_code()) {
856 Label done_checking;
857 AssertNotSmi(object);
858 cmp(object, isolate()->factory()->undefined_value());
859 j(equal, &done_checking);
860 cmp(FieldOperand(object, 0),
861 Immediate(isolate()->factory()->allocation_site_map()));
862 Assert(equal, kExpectedUndefinedOrCell);
863 bind(&done_checking);
864 }
865}
866
867
868void MacroAssembler::AssertNotSmi(Register object) {
869 if (emit_debug_code()) {
870 test(object, Immediate(kSmiTagMask));
871 Check(not_equal, kOperandIsASmi);
872 }
873}
874
875
876void MacroAssembler::StubPrologue() {
877 push(ebp); // Caller's frame pointer.
878 mov(ebp, esp);
879 push(esi); // Callee's context.
880 push(Immediate(Smi::FromInt(StackFrame::STUB)));
881}
882
883
884void MacroAssembler::Prologue(bool code_pre_aging) {
885 PredictableCodeSizeScope predictible_code_size_scope(this,
886 kNoCodeAgeSequenceLength);
887 if (code_pre_aging) {
888 // Pre-age the code.
889 call(isolate()->builtins()->MarkCodeAsExecutedOnce(),
890 RelocInfo::CODE_AGE_SEQUENCE);
891 Nop(kNoCodeAgeSequenceLength - Assembler::kCallInstructionLength);
892 } else {
893 push(ebp); // Caller's frame pointer.
894 mov(ebp, esp);
895 push(esi); // Callee's context.
896 push(edi); // Callee's JS function.
897 }
Steve Block6ded16b2010-05-10 14:33:55 +0100898}
899
900
Steve Blocka7e24c12009-10-30 11:49:00 +0000901void MacroAssembler::EnterFrame(StackFrame::Type type) {
902 push(ebp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100903 mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000904 push(esi);
905 push(Immediate(Smi::FromInt(type)));
906 push(Immediate(CodeObject()));
Steve Block44f0eee2011-05-26 01:26:41 +0100907 if (emit_debug_code()) {
908 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000909 Check(not_equal, kCodeObjectNotProperlyPatched);
Steve Blocka7e24c12009-10-30 11:49:00 +0000910 }
911}
912
913
914void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100915 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000916 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
917 Immediate(Smi::FromInt(type)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918 Check(equal, kStackFrameTypesMustMatch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000919 }
920 leave();
921}
922
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100923
924void MacroAssembler::EnterExitFramePrologue() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100925 // Set up the frame structure on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000926 DCHECK(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
927 DCHECK(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
928 DCHECK(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000929 push(ebp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100930 mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000931
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100932 // Reserve room for entry stack pointer and push the code object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000933 DCHECK(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000934 push(Immediate(0)); // Saved entry sp, patched before call.
935 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000936
937 // Save the frame pointer and the context in top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress, isolate());
939 ExternalReference context_address(Isolate::kContextAddress, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
941 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000942}
Steve Blocka7e24c12009-10-30 11:49:00 +0000943
Steve Blocka7e24c12009-10-30 11:49:00 +0000944
Ben Murdochb0fe1622011-05-05 13:52:32 +0100945void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
946 // Optionally save all XMM registers.
947 if (save_doubles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948 int space = XMMRegister::kMaxNumRegisters * kDoubleSize +
949 argc * kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100950 sub(esp, Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100951 const int offset = -2 * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100953 XMMRegister reg = XMMRegister::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000954 movsd(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100955 }
956 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100957 sub(esp, Immediate(argc * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100958 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000959
960 // Get the required frame alignment for the OS.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961 const int kFrameAlignment = base::OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000962 if (kFrameAlignment > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000963 DCHECK(base::bits::IsPowerOfTwo32(kFrameAlignment));
Steve Blocka7e24c12009-10-30 11:49:00 +0000964 and_(esp, -kFrameAlignment);
965 }
966
967 // Patch the saved entry sp.
968 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
969}
970
971
Ben Murdochb0fe1622011-05-05 13:52:32 +0100972void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100973 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000974
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100975 // Set up argc and argv in callee-saved registers.
Steve Blockd0582a62009-12-15 09:54:21 +0000976 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100977 mov(edi, eax);
Steve Blockd0582a62009-12-15 09:54:21 +0000978 lea(esi, Operand(ebp, eax, times_4, offset));
979
Steve Block44f0eee2011-05-26 01:26:41 +0100980 // Reserve space for argc, argv and isolate.
981 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000982}
983
984
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800985void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100986 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100987 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000988}
989
990
Ben Murdochb0fe1622011-05-05 13:52:32 +0100991void MacroAssembler::LeaveExitFrame(bool save_doubles) {
992 // Optionally restore all XMM registers.
993 if (save_doubles) {
Steve Block1e0659c2011-05-24 12:43:12 +0100994 const int offset = -2 * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100996 XMMRegister reg = XMMRegister::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 movsd(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100998 }
999 }
1000
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 // Get the return address from the stack and restore the frame pointer.
1002 mov(ecx, Operand(ebp, 1 * kPointerSize));
1003 mov(ebp, Operand(ebp, 0 * kPointerSize));
1004
1005 // Pop the arguments and the receiver from the caller stack.
1006 lea(esp, Operand(esi, 1 * kPointerSize));
1007
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001008 // Push the return address to get ready to return.
1009 push(ecx);
1010
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 LeaveExitFrameEpilogue(true);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001012}
1013
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001014
1015void MacroAssembler::LeaveExitFrameEpilogue(bool restore_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 // Restore current context from top and clear it in debug mode.
Ben Murdoch589d6972011-11-30 16:04:58 +00001017 ExternalReference context_address(Isolate::kContextAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 if (restore_context) {
1019 mov(esi, Operand::StaticVariable(context_address));
1020 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001021#ifdef DEBUG
1022 mov(Operand::StaticVariable(context_address), Immediate(0));
1023#endif
1024
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 // Clear the top frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00001026 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01001027 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
1029}
1030
1031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032void MacroAssembler::LeaveApiExitFrame(bool restore_context) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001033 mov(esp, ebp);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001034 pop(ebp);
1035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001036 LeaveExitFrameEpilogue(restore_context);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001037}
1038
1039
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001040void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
1041 int handler_index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001042 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001043 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1044 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001045 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1046 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1047 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1048 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
1049
1050 // We will build up the handler from the bottom by pushing on the stack.
1051 // First push the frame pointer and context.
1052 if (kind == StackHandler::JS_ENTRY) {
1053 // The frame pointer does not point to a JS frame so we save NULL for
1054 // ebp. We expect the code throwing an exception to check ebp before
1055 // dereferencing it to restore the context.
Ben Murdoch85b71792012-04-11 18:30:58 +01001056 push(Immediate(0)); // NULL frame pointer.
1057 push(Immediate(Smi::FromInt(0))); // No context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001058 } else {
1059 push(ebp);
1060 push(esi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001062 // Push the state and the code object.
1063 unsigned state =
1064 StackHandler::IndexField::encode(handler_index) |
1065 StackHandler::KindField::encode(kind);
1066 push(Immediate(state));
1067 Push(CodeObject());
1068
1069 // Link the current handler as the next handler.
1070 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
1071 push(Operand::StaticVariable(handler_address));
1072 // Set this new handler as the current one.
1073 mov(Operand::StaticVariable(handler_address), esp);
Steve Blocka7e24c12009-10-30 11:49:00 +00001074}
1075
1076
Leon Clarkee46be812010-01-19 14:06:41 +00001077void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001078 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001079 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
1080 pop(Operand::StaticVariable(handler_address));
1081 add(esp, Immediate(StackHandlerConstants::kSize - kPointerSize));
1082}
1083
1084
1085void MacroAssembler::JumpToHandlerEntry() {
1086 // Compute the handler entry address and jump to it. The handler table is
1087 // a fixed array of (smi-tagged) code offsets.
1088 // eax = exception, edi = code object, edx = state.
1089 mov(ebx, FieldOperand(edi, Code::kHandlerTableOffset));
1090 shr(edx, StackHandler::kKindWidth);
1091 mov(edx, FieldOperand(ebx, edx, times_4, FixedArray::kHeaderSize));
1092 SmiUntag(edx);
1093 lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
1094 jmp(edi);
Leon Clarkee46be812010-01-19 14:06:41 +00001095}
1096
1097
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001098void MacroAssembler::Throw(Register value) {
1099 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001100 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1101 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001102 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1103 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1104 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1105 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
1106
1107 // The exception is expected in eax.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001108 if (!value.is(eax)) {
1109 mov(eax, value);
1110 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001111 // Drop the stack pointer to the top of the top handler.
1112 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001113 mov(esp, Operand::StaticVariable(handler_address));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001114 // Restore the next handler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001115 pop(Operand::StaticVariable(handler_address));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001116
1117 // Remove the code object and state, compute the handler address in edi.
1118 pop(edi); // Code object.
1119 pop(edx); // Index and state.
1120
1121 // Restore the context and frame pointer.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001122 pop(esi); // Context.
1123 pop(ebp); // Frame pointer.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001124
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001125 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001126 // (kind == ENTRY) == (ebp == 0) == (esi == 0), so we could test either
1127 // ebp or esi.
Ben Murdoch257744e2011-11-30 15:57:28 +00001128 Label skip;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001129 test(esi, esi);
1130 j(zero, &skip, Label::kNear);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001131 mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001132 bind(&skip);
1133
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001134 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001135}
1136
1137
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001138void MacroAssembler::ThrowUncatchable(Register value) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001139 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001140 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1141 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001142 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1143 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1144 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1145 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001146
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001147 // The exception is expected in eax.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001148 if (!value.is(eax)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001149 mov(eax, value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001150 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001151 // Drop the stack pointer to the top of the top stack handler.
1152 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001153 mov(esp, Operand::StaticVariable(handler_address));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001154
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001155 // Unwind the handlers until the top ENTRY handler is found.
1156 Label fetch_next, check_kind;
1157 jmp(&check_kind, Label::kNear);
1158 bind(&fetch_next);
1159 mov(esp, Operand(esp, StackHandlerConstants::kNextOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001160
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001161 bind(&check_kind);
1162 STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
1163 test(Operand(esp, StackHandlerConstants::kStateOffset),
1164 Immediate(StackHandler::KindField::kMask));
1165 j(not_zero, &fetch_next);
1166
1167 // Set the top handler address to next handler past the top ENTRY handler.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001168 pop(Operand::StaticVariable(handler_address));
1169
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001170 // Remove the code object and state, compute the handler address in edi.
1171 pop(edi); // Code object.
1172 pop(edx); // Index and state.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001173
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001174 // Clear the context pointer and frame pointer (0 was saved in the handler).
1175 pop(esi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001176 pop(ebp);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001177
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001178 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001179}
1180
1181
Steve Blocka7e24c12009-10-30 11:49:00 +00001182void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001183 Register scratch1,
1184 Register scratch2,
Steve Blocka7e24c12009-10-30 11:49:00 +00001185 Label* miss) {
1186 Label same_contexts;
1187
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001188 DCHECK(!holder_reg.is(scratch1));
1189 DCHECK(!holder_reg.is(scratch2));
1190 DCHECK(!scratch1.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001191
1192 // Load current lexical context from the stack frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001193 mov(scratch1, Operand(ebp, StandardFrameConstants::kContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001194
1195 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +01001196 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001197 cmp(scratch1, Immediate(0));
1198 Check(not_equal, kWeShouldNotHaveAnEmptyLexicalContext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001199 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 // Load the native context of the current context.
1201 int offset =
1202 Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
1203 mov(scratch1, FieldOperand(scratch1, offset));
1204 mov(scratch1, FieldOperand(scratch1, GlobalObject::kNativeContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001205
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001206 // Check the context is a native context.
Steve Block44f0eee2011-05-26 01:26:41 +01001207 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001208 // Read the first word and compare to native_context_map.
1209 cmp(FieldOperand(scratch1, HeapObject::kMapOffset),
1210 isolate()->factory()->native_context_map());
1211 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001212 }
1213
1214 // Check if both contexts are the same.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001215 cmp(scratch1, FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001216 j(equal, &same_contexts);
Steve Blocka7e24c12009-10-30 11:49:00 +00001217
1218 // Compare security tokens, save holder_reg on the stack so we can use it
1219 // as a temporary register.
1220 //
Steve Blocka7e24c12009-10-30 11:49:00 +00001221 // Check that the security token in the calling global object is
1222 // compatible with the security token in the receiving global
1223 // object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 mov(scratch2,
1225 FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001226
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227 // Check the context is a native context.
Steve Block44f0eee2011-05-26 01:26:41 +01001228 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001229 cmp(scratch2, isolate()->factory()->null_value());
1230 Check(not_equal, kJSGlobalProxyContextShouldNotBeNull);
Steve Blocka7e24c12009-10-30 11:49:00 +00001231
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001232 // Read the first word and compare to native_context_map(),
1233 cmp(FieldOperand(scratch2, HeapObject::kMapOffset),
1234 isolate()->factory()->native_context_map());
1235 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001236 }
1237
1238 int token_offset = Context::kHeaderSize +
1239 Context::SECURITY_TOKEN_INDEX * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001240 mov(scratch1, FieldOperand(scratch1, token_offset));
1241 cmp(scratch1, FieldOperand(scratch2, token_offset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001242 j(not_equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001243
1244 bind(&same_contexts);
1245}
1246
1247
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001248// Compute the hash code from the untagged key. This must be kept in sync with
1249// ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
1250// code-stub-hydrogen.cc
Ben Murdochc7cc0282012-03-05 14:35:55 +00001251//
1252// Note: r0 will contain hash code
1253void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
1254 // Xor original key with a seed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 if (serializer_enabled()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001256 ExternalReference roots_array_start =
1257 ExternalReference::roots_array_start(isolate());
Ben Murdochc7cc0282012-03-05 14:35:55 +00001258 mov(scratch, Immediate(Heap::kHashSeedRootIndex));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001259 mov(scratch,
1260 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001261 SmiUntag(scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001262 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001263 } else {
1264 int32_t seed = isolate()->heap()->HashSeed();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001265 xor_(r0, Immediate(seed));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001266 }
1267
1268 // hash = ~hash + (hash << 15);
1269 mov(scratch, r0);
1270 not_(r0);
1271 shl(scratch, 15);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001272 add(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001273 // hash = hash ^ (hash >> 12);
1274 mov(scratch, r0);
1275 shr(scratch, 12);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001276 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001277 // hash = hash + (hash << 2);
1278 lea(r0, Operand(r0, r0, times_4, 0));
1279 // hash = hash ^ (hash >> 4);
1280 mov(scratch, r0);
1281 shr(scratch, 4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001282 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001283 // hash = hash * 2057;
1284 imul(r0, r0, 2057);
1285 // hash = hash ^ (hash >> 16);
1286 mov(scratch, r0);
1287 shr(scratch, 16);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001288 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001289}
1290
1291
1292
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001293void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1294 Register elements,
1295 Register key,
1296 Register r0,
1297 Register r1,
1298 Register r2,
1299 Register result) {
1300 // Register use:
1301 //
1302 // elements - holds the slow-case elements of the receiver and is unchanged.
1303 //
1304 // key - holds the smi key on entry and is unchanged.
1305 //
1306 // Scratch registers:
1307 //
1308 // r0 - holds the untagged key on entry and holds the hash once computed.
1309 //
1310 // r1 - used to hold the capacity mask of the dictionary
1311 //
1312 // r2 - used for the index into the dictionary.
1313 //
1314 // result - holds the result on exit if the load succeeds and we fall through.
1315
1316 Label done;
1317
Ben Murdochc7cc0282012-03-05 14:35:55 +00001318 GetNumberHash(r0, r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001319
1320 // Compute capacity mask.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001321 mov(r1, FieldOperand(elements, SeededNumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001322 shr(r1, kSmiTagSize); // convert smi to int
1323 dec(r1);
1324
1325 // Generate an unrolled loop that performs a few probes before giving up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001326 for (int i = 0; i < kNumberDictionaryProbes; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001327 // Use r2 for index calculations and keep the hash intact in r0.
1328 mov(r2, r0);
1329 // Compute the masked index: (hash + i + i * i) & mask.
1330 if (i > 0) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001331 add(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001332 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001333 and_(r2, r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001334
1335 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 DCHECK(SeededNumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001337 lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
1338
1339 // Check if the key matches.
1340 cmp(key, FieldOperand(elements,
1341 r2,
1342 times_pointer_size,
Ben Murdochc7cc0282012-03-05 14:35:55 +00001343 SeededNumberDictionary::kElementsStartOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 if (i != (kNumberDictionaryProbes - 1)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001345 j(equal, &done);
1346 } else {
1347 j(not_equal, miss);
1348 }
1349 }
1350
1351 bind(&done);
1352 // Check that the value is a normal propety.
1353 const int kDetailsOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001354 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001355 DCHECK_EQ(NORMAL, 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001356 test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
Ben Murdoch589d6972011-11-30 16:04:58 +00001357 Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001358 j(not_zero, miss);
1359
1360 // Get the value at the masked, scaled index.
1361 const int kValueOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001362 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001363 mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
1364}
1365
1366
Steve Blocka7e24c12009-10-30 11:49:00 +00001367void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +00001368 Register scratch,
1369 AllocationFlags flags) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001370 ExternalReference allocation_top =
1371 AllocationUtils::GetAllocationTopReference(isolate(), flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001372
1373 // Just return if allocation top is already known.
1374 if ((flags & RESULT_CONTAINS_TOP) != 0) {
1375 // No use of scratch if allocation top is provided.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001376 DCHECK(scratch.is(no_reg));
Steve Blocka7e24c12009-10-30 11:49:00 +00001377#ifdef DEBUG
1378 // Assert that result actually contains top on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001379 cmp(result, Operand::StaticVariable(allocation_top));
1380 Check(equal, kUnexpectedAllocationTop);
Steve Blocka7e24c12009-10-30 11:49:00 +00001381#endif
1382 return;
1383 }
1384
1385 // Move address of new object to result. Use scratch register if available.
1386 if (scratch.is(no_reg)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001387 mov(result, Operand::StaticVariable(allocation_top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001389 mov(scratch, Immediate(allocation_top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001390 mov(result, Operand(scratch, 0));
1391 }
1392}
1393
1394
1395void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001396 Register scratch,
1397 AllocationFlags flags) {
Steve Block44f0eee2011-05-26 01:26:41 +01001398 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001399 test(result_end, Immediate(kObjectAlignmentMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001400 Check(zero, kUnalignedAllocationInNewSpace);
Steve Blockd0582a62009-12-15 09:54:21 +00001401 }
1402
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403 ExternalReference allocation_top =
1404 AllocationUtils::GetAllocationTopReference(isolate(), flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001405
1406 // Update new top. Use scratch if available.
1407 if (scratch.is(no_reg)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001408 mov(Operand::StaticVariable(allocation_top), result_end);
Steve Blocka7e24c12009-10-30 11:49:00 +00001409 } else {
1410 mov(Operand(scratch, 0), result_end);
1411 }
1412}
1413
1414
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415void MacroAssembler::Allocate(int object_size,
1416 Register result,
1417 Register result_end,
1418 Register scratch,
1419 Label* gc_required,
1420 AllocationFlags flags) {
1421 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
1422 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
John Reck59135872010-11-02 12:39:01 -07001423 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001424 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001425 // Trash the registers to simulate an allocation failure.
1426 mov(result, Immediate(0x7091));
1427 if (result_end.is_valid()) {
1428 mov(result_end, Immediate(0x7191));
1429 }
1430 if (scratch.is_valid()) {
1431 mov(scratch, Immediate(0x7291));
1432 }
1433 }
1434 jmp(gc_required);
1435 return;
1436 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437 DCHECK(!result.is(result_end));
Steve Blocka7e24c12009-10-30 11:49:00 +00001438
1439 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001440 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001441
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001442 ExternalReference allocation_limit =
1443 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1444
1445 // Align the next allocation. Storing the filler map without checking top is
1446 // safe in new-space because the limit of the heap is aligned there.
1447 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1448 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1449 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1450 Label aligned;
1451 test(result, Immediate(kDoubleAlignmentMask));
1452 j(zero, &aligned, Label::kNear);
1453 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
1454 cmp(result, Operand::StaticVariable(allocation_limit));
1455 j(above_equal, gc_required);
1456 }
1457 mov(Operand(result, 0),
1458 Immediate(isolate()->factory()->one_pointer_filler_map()));
1459 add(result, Immediate(kDoubleSize / 2));
1460 bind(&aligned);
1461 }
1462
1463 // Calculate new top and bail out if space is exhausted.
Ben Murdochbb769b22010-08-11 14:56:33 +01001464 Register top_reg = result_end.is_valid() ? result_end : result;
Steve Block1e0659c2011-05-24 12:43:12 +01001465 if (!top_reg.is(result)) {
1466 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +01001467 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001468 add(top_reg, Immediate(object_size));
Ben Murdoch257744e2011-11-30 15:57:28 +00001469 j(carry, gc_required);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470 cmp(top_reg, Operand::StaticVariable(allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001471 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
Leon Clarkee46be812010-01-19 14:06:41 +00001473 // Update allocation top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001474 UpdateAllocationTopHelper(top_reg, scratch, flags);
Ben Murdochbb769b22010-08-11 14:56:33 +01001475
1476 // Tag result if requested.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001477 bool tag_result = (flags & TAG_OBJECT) != 0;
Ben Murdochbb769b22010-08-11 14:56:33 +01001478 if (top_reg.is(result)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001479 if (tag_result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001480 sub(result, Immediate(object_size - kHeapObjectTag));
Ben Murdochbb769b22010-08-11 14:56:33 +01001481 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001482 sub(result, Immediate(object_size));
Ben Murdochbb769b22010-08-11 14:56:33 +01001483 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484 } else if (tag_result) {
1485 DCHECK(kHeapObjectTag == 1);
1486 inc(result);
Ben Murdochbb769b22010-08-11 14:56:33 +01001487 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001488}
1489
1490
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001491void MacroAssembler::Allocate(int header_size,
1492 ScaleFactor element_size,
1493 Register element_count,
1494 RegisterValueType element_count_type,
1495 Register result,
1496 Register result_end,
1497 Register scratch,
1498 Label* gc_required,
1499 AllocationFlags flags) {
1500 DCHECK((flags & SIZE_IN_WORDS) == 0);
John Reck59135872010-11-02 12:39:01 -07001501 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001502 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001503 // Trash the registers to simulate an allocation failure.
1504 mov(result, Immediate(0x7091));
1505 mov(result_end, Immediate(0x7191));
1506 if (scratch.is_valid()) {
1507 mov(scratch, Immediate(0x7291));
1508 }
1509 // Register element_count is not modified by the function.
1510 }
1511 jmp(gc_required);
1512 return;
1513 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001514 DCHECK(!result.is(result_end));
Steve Blocka7e24c12009-10-30 11:49:00 +00001515
1516 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001517 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001518
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001519 ExternalReference allocation_limit =
1520 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
Steve Block1e0659c2011-05-24 12:43:12 +01001521
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001522 // Align the next allocation. Storing the filler map without checking top is
1523 // safe in new-space because the limit of the heap is aligned there.
1524 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1525 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1526 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1527 Label aligned;
1528 test(result, Immediate(kDoubleAlignmentMask));
1529 j(zero, &aligned, Label::kNear);
1530 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
1531 cmp(result, Operand::StaticVariable(allocation_limit));
1532 j(above_equal, gc_required);
1533 }
1534 mov(Operand(result, 0),
1535 Immediate(isolate()->factory()->one_pointer_filler_map()));
1536 add(result, Immediate(kDoubleSize / 2));
1537 bind(&aligned);
1538 }
1539
1540 // Calculate new top and bail out if space is exhausted.
Steve Block1e0659c2011-05-24 12:43:12 +01001541 // We assume that element_count*element_size + header_size does not
1542 // overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001543 if (element_count_type == REGISTER_VALUE_IS_SMI) {
1544 STATIC_ASSERT(static_cast<ScaleFactor>(times_2 - 1) == times_1);
1545 STATIC_ASSERT(static_cast<ScaleFactor>(times_4 - 1) == times_2);
1546 STATIC_ASSERT(static_cast<ScaleFactor>(times_8 - 1) == times_4);
1547 DCHECK(element_size >= times_2);
1548 DCHECK(kSmiTagSize == 1);
1549 element_size = static_cast<ScaleFactor>(element_size - 1);
1550 } else {
1551 DCHECK(element_count_type == REGISTER_VALUE_IS_INT32);
1552 }
Steve Block1e0659c2011-05-24 12:43:12 +01001553 lea(result_end, Operand(element_count, element_size, header_size));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001554 add(result_end, result);
Steve Block1e0659c2011-05-24 12:43:12 +01001555 j(carry, gc_required);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001556 cmp(result_end, Operand::StaticVariable(allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +00001557 j(above, gc_required);
1558
Steve Blocka7e24c12009-10-30 11:49:00 +00001559 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001560 DCHECK(kHeapObjectTag == 1);
1561 inc(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001562 }
Leon Clarkee46be812010-01-19 14:06:41 +00001563
1564 // Update allocation top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565 UpdateAllocationTopHelper(result_end, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001566}
1567
1568
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001569void MacroAssembler::Allocate(Register object_size,
1570 Register result,
1571 Register result_end,
1572 Register scratch,
1573 Label* gc_required,
1574 AllocationFlags flags) {
1575 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
John Reck59135872010-11-02 12:39:01 -07001576 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001577 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001578 // Trash the registers to simulate an allocation failure.
1579 mov(result, Immediate(0x7091));
1580 mov(result_end, Immediate(0x7191));
1581 if (scratch.is_valid()) {
1582 mov(scratch, Immediate(0x7291));
1583 }
1584 // object_size is left unchanged by this function.
1585 }
1586 jmp(gc_required);
1587 return;
1588 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 DCHECK(!result.is(result_end));
Steve Blocka7e24c12009-10-30 11:49:00 +00001590
1591 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001592 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001593
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001594 ExternalReference allocation_limit =
1595 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1596
1597 // Align the next allocation. Storing the filler map without checking top is
1598 // safe in new-space because the limit of the heap is aligned there.
1599 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1600 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1601 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1602 Label aligned;
1603 test(result, Immediate(kDoubleAlignmentMask));
1604 j(zero, &aligned, Label::kNear);
1605 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
1606 cmp(result, Operand::StaticVariable(allocation_limit));
1607 j(above_equal, gc_required);
1608 }
1609 mov(Operand(result, 0),
1610 Immediate(isolate()->factory()->one_pointer_filler_map()));
1611 add(result, Immediate(kDoubleSize / 2));
1612 bind(&aligned);
1613 }
1614
1615 // Calculate new top and bail out if space is exhausted.
Steve Blocka7e24c12009-10-30 11:49:00 +00001616 if (!object_size.is(result_end)) {
1617 mov(result_end, object_size);
1618 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001619 add(result_end, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001620 j(carry, gc_required);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001621 cmp(result_end, Operand::StaticVariable(allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001622 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001623
Steve Blocka7e24c12009-10-30 11:49:00 +00001624 // Tag result if requested.
1625 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626 DCHECK(kHeapObjectTag == 1);
1627 inc(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 }
Leon Clarkee46be812010-01-19 14:06:41 +00001629
1630 // Update allocation top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001631 UpdateAllocationTopHelper(result_end, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001632}
1633
1634
1635void MacroAssembler::UndoAllocationInNewSpace(Register object) {
1636 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001637 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001638
1639 // Make sure the object has no tag before resetting top.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001640 and_(object, Immediate(~kHeapObjectTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +00001641#ifdef DEBUG
1642 cmp(object, Operand::StaticVariable(new_space_allocation_top));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001643 Check(below, kUndoAllocationOfNonAllocatedMemory);
Steve Blocka7e24c12009-10-30 11:49:00 +00001644#endif
1645 mov(Operand::StaticVariable(new_space_allocation_top), object);
1646}
1647
1648
Steve Block3ce2e202009-11-05 08:53:23 +00001649void MacroAssembler::AllocateHeapNumber(Register result,
1650 Register scratch1,
1651 Register scratch2,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001652 Label* gc_required,
1653 MutableMode mode) {
Steve Block3ce2e202009-11-05 08:53:23 +00001654 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001655 Allocate(HeapNumber::kSize, result, scratch1, scratch2, gc_required,
1656 TAG_OBJECT);
1657
1658 Handle<Map> map = mode == MUTABLE
1659 ? isolate()->factory()->mutable_heap_number_map()
1660 : isolate()->factory()->heap_number_map();
Steve Block3ce2e202009-11-05 08:53:23 +00001661
1662 // Set the map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001663 mov(FieldOperand(result, HeapObject::kMapOffset), Immediate(map));
Steve Block3ce2e202009-11-05 08:53:23 +00001664}
1665
1666
Steve Blockd0582a62009-12-15 09:54:21 +00001667void MacroAssembler::AllocateTwoByteString(Register result,
1668 Register length,
1669 Register scratch1,
1670 Register scratch2,
1671 Register scratch3,
1672 Label* gc_required) {
1673 // Calculate the number of bytes needed for the characters in the string while
1674 // observing object alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001675 DCHECK((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1676 DCHECK(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +00001677 // scratch1 = length * 2 + kObjectAlignmentMask.
1678 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001679 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001680
1681 // Allocate two byte string in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 Allocate(SeqTwoByteString::kHeaderSize,
1683 times_1,
1684 scratch1,
1685 REGISTER_VALUE_IS_INT32,
1686 result,
1687 scratch2,
1688 scratch3,
1689 gc_required,
1690 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001691
1692 // Set the map, length and hash field.
1693 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001694 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001695 mov(scratch1, length);
1696 SmiTag(scratch1);
1697 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001698 mov(FieldOperand(result, String::kHashFieldOffset),
1699 Immediate(String::kEmptyHashField));
1700}
1701
1702
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001703void MacroAssembler::AllocateOneByteString(Register result, Register length,
1704 Register scratch1, Register scratch2,
1705 Register scratch3,
1706 Label* gc_required) {
Steve Blockd0582a62009-12-15 09:54:21 +00001707 // Calculate the number of bytes needed for the characters in the string while
1708 // observing object alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001709 DCHECK((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +00001710 mov(scratch1, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001711 DCHECK(kCharSize == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001712 add(scratch1, Immediate(kObjectAlignmentMask));
1713 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001714
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001715 // Allocate one-byte string in new space.
1716 Allocate(SeqOneByteString::kHeaderSize,
1717 times_1,
1718 scratch1,
1719 REGISTER_VALUE_IS_INT32,
1720 result,
1721 scratch2,
1722 scratch3,
1723 gc_required,
1724 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001725
1726 // Set the map, length and hash field.
1727 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001728 Immediate(isolate()->factory()->one_byte_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001729 mov(scratch1, length);
1730 SmiTag(scratch1);
1731 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001732 mov(FieldOperand(result, String::kHashFieldOffset),
1733 Immediate(String::kEmptyHashField));
1734}
1735
1736
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737void MacroAssembler::AllocateOneByteString(Register result, int length,
1738 Register scratch1, Register scratch2,
1739 Label* gc_required) {
1740 DCHECK(length > 0);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001741
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001742 // Allocate one-byte string in new space.
1743 Allocate(SeqOneByteString::SizeFor(length), result, scratch1, scratch2,
1744 gc_required, TAG_OBJECT);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001745
1746 // Set the map, length and hash field.
1747 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001748 Immediate(isolate()->factory()->one_byte_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001749 mov(FieldOperand(result, String::kLengthOffset),
1750 Immediate(Smi::FromInt(length)));
1751 mov(FieldOperand(result, String::kHashFieldOffset),
1752 Immediate(String::kEmptyHashField));
1753}
1754
1755
Ben Murdoch589d6972011-11-30 16:04:58 +00001756void MacroAssembler::AllocateTwoByteConsString(Register result,
Steve Blockd0582a62009-12-15 09:54:21 +00001757 Register scratch1,
1758 Register scratch2,
1759 Label* gc_required) {
1760 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001761 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
1762 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001763
1764 // Set the map. The other fields are left uninitialized.
1765 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001766 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001767}
1768
1769
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001770void MacroAssembler::AllocateOneByteConsString(Register result,
1771 Register scratch1,
1772 Register scratch2,
1773 Label* gc_required) {
1774 Allocate(ConsString::kSize,
1775 result,
1776 scratch1,
1777 scratch2,
1778 gc_required,
1779 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001780
1781 // Set the map. The other fields are left uninitialized.
1782 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001783 Immediate(isolate()->factory()->cons_one_byte_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001784}
1785
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001786
Ben Murdoch589d6972011-11-30 16:04:58 +00001787void MacroAssembler::AllocateTwoByteSlicedString(Register result,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001788 Register scratch1,
1789 Register scratch2,
1790 Label* gc_required) {
1791 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001792 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1793 TAG_OBJECT);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001794
1795 // Set the map. The other fields are left uninitialized.
1796 mov(FieldOperand(result, HeapObject::kMapOffset),
1797 Immediate(isolate()->factory()->sliced_string_map()));
1798}
1799
1800
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001801void MacroAssembler::AllocateOneByteSlicedString(Register result,
1802 Register scratch1,
1803 Register scratch2,
1804 Label* gc_required) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001805 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001806 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1807 TAG_OBJECT);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001808
1809 // Set the map. The other fields are left uninitialized.
1810 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001811 Immediate(isolate()->factory()->sliced_one_byte_string_map()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001812}
1813
1814
Ben Murdochb8e0da22011-05-16 14:20:40 +01001815// Copy memory, byte-by-byte, from source to destination. Not optimized for
1816// long or aligned copies. The contents of scratch and length are destroyed.
1817// Source and destination are incremented by length.
1818// Many variants of movsb, loop unrolling, word moves, and indexed operands
1819// have been tried here already, and this is fastest.
1820// A simpler loop is faster on small copies, but 30% slower on large ones.
1821// The cld() instruction must have been emitted, to set the direction flag(),
1822// before calling this function.
1823void MacroAssembler::CopyBytes(Register source,
1824 Register destination,
1825 Register length,
1826 Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001827 Label short_loop, len4, len8, len12, done, short_string;
1828 DCHECK(source.is(esi));
1829 DCHECK(destination.is(edi));
1830 DCHECK(length.is(ecx));
1831 cmp(length, Immediate(4));
1832 j(below, &short_string, Label::kNear);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001833
1834 // Because source is 4-byte aligned in our uses of this function,
1835 // we keep source aligned for the rep_movs call by copying the odd bytes
1836 // at the end of the ranges.
1837 mov(scratch, Operand(source, length, times_1, -4));
1838 mov(Operand(destination, length, times_1, -4), scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001839
1840 cmp(length, Immediate(8));
1841 j(below_equal, &len4, Label::kNear);
1842 cmp(length, Immediate(12));
1843 j(below_equal, &len8, Label::kNear);
1844 cmp(length, Immediate(16));
1845 j(below_equal, &len12, Label::kNear);
1846
Ben Murdochb8e0da22011-05-16 14:20:40 +01001847 mov(scratch, ecx);
1848 shr(ecx, 2);
1849 rep_movs();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001850 and_(scratch, Immediate(0x3));
1851 add(destination, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001852 jmp(&done, Label::kNear);
1853
1854 bind(&len12);
1855 mov(scratch, Operand(source, 8));
1856 mov(Operand(destination, 8), scratch);
1857 bind(&len8);
1858 mov(scratch, Operand(source, 4));
1859 mov(Operand(destination, 4), scratch);
1860 bind(&len4);
1861 mov(scratch, Operand(source, 0));
1862 mov(Operand(destination, 0), scratch);
1863 add(destination, length);
1864 jmp(&done, Label::kNear);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001865
1866 bind(&short_string);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001867 test(length, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001868 j(zero, &done, Label::kNear);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001869
1870 bind(&short_loop);
1871 mov_b(scratch, Operand(source, 0));
1872 mov_b(Operand(destination, 0), scratch);
1873 inc(source);
1874 inc(destination);
1875 dec(length);
1876 j(not_zero, &short_loop);
1877
1878 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001879}
1880
Steve Blockd0582a62009-12-15 09:54:21 +00001881
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001882void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
1883 Register end_offset,
1884 Register filler) {
1885 Label loop, entry;
1886 jmp(&entry);
1887 bind(&loop);
1888 mov(Operand(start_offset, 0), filler);
1889 add(start_offset, Immediate(kPointerSize));
1890 bind(&entry);
1891 cmp(start_offset, end_offset);
1892 j(less, &loop);
1893}
1894
1895
1896void MacroAssembler::BooleanBitTest(Register object,
1897 int field_offset,
1898 int bit_index) {
1899 bit_index += kSmiTagSize + kSmiShiftSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001900 DCHECK(base::bits::IsPowerOfTwo32(kBitsPerByte));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001901 int byte_index = bit_index / kBitsPerByte;
1902 int byte_bit_index = bit_index & (kBitsPerByte - 1);
1903 test_b(FieldOperand(object, field_offset + byte_index),
1904 static_cast<byte>(1 << byte_bit_index));
1905}
1906
1907
1908
Steve Blocka7e24c12009-10-30 11:49:00 +00001909void MacroAssembler::NegativeZeroTest(Register result,
1910 Register op,
1911 Label* then_label) {
1912 Label ok;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001913 test(result, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001914 j(not_zero, &ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001915 test(op, op);
Ben Murdoch257744e2011-11-30 15:57:28 +00001916 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 bind(&ok);
1918}
1919
1920
1921void MacroAssembler::NegativeZeroTest(Register result,
1922 Register op1,
1923 Register op2,
1924 Register scratch,
1925 Label* then_label) {
1926 Label ok;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001927 test(result, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001928 j(not_zero, &ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001929 mov(scratch, op1);
1930 or_(scratch, op2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001931 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001932 bind(&ok);
1933}
1934
1935
1936void MacroAssembler::TryGetFunctionPrototype(Register function,
1937 Register result,
1938 Register scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001939 Label* miss,
1940 bool miss_on_bound_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001941 Label non_instance;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001942 if (miss_on_bound_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001943 // Check that the receiver isn't a smi.
1944 JumpIfSmi(function, miss);
1945
1946 // Check that the function really is a function.
1947 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1948 j(not_equal, miss);
1949
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001950 // If a bound function, go to miss label.
1951 mov(scratch,
1952 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1953 BooleanBitTest(scratch, SharedFunctionInfo::kCompilerHintsOffset,
1954 SharedFunctionInfo::kBoundFunction);
1955 j(not_zero, miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001956
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001957 // Make sure that the function has an instance prototype.
1958 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1959 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1960 j(not_zero, &non_instance);
1961 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001962
1963 // Get the prototype or initial map from the function.
1964 mov(result,
1965 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1966
1967 // If the prototype or initial map is the hole, don't return it and
1968 // simply miss the cache instead. This will allow us to allocate a
1969 // prototype object on-demand in the runtime system.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001970 cmp(result, Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001971 j(equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001972
1973 // If the function does not have an initial map, we're done.
1974 Label done;
1975 CmpObjectType(result, MAP_TYPE, scratch);
1976 j(not_equal, &done);
1977
1978 // Get the prototype from the initial map.
1979 mov(result, FieldOperand(result, Map::kPrototypeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001980
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001981 if (miss_on_bound_function) {
1982 jmp(&done);
1983
1984 // Non-instance prototype: Fetch prototype from constructor field
1985 // in initial map.
1986 bind(&non_instance);
1987 mov(result, FieldOperand(result, Map::kConstructorOffset));
1988 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001989
1990 // All done.
1991 bind(&done);
1992}
1993
1994
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001995void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
1996 DCHECK(AllowThisStubCall(stub)); // Calls are not allowed in some stubs.
Ben Murdoch257744e2011-11-30 15:57:28 +00001997 call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001998}
1999
2000
Steve Blockd0582a62009-12-15 09:54:21 +00002001void MacroAssembler::TailCallStub(CodeStub* stub) {
Steve Blockd0582a62009-12-15 09:54:21 +00002002 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
2003}
2004
2005
Steve Blocka7e24c12009-10-30 11:49:00 +00002006void MacroAssembler::StubReturn(int argc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002007 DCHECK(argc >= 1 && generating_stub());
Steve Blocka7e24c12009-10-30 11:49:00 +00002008 ret((argc - 1) * kPointerSize);
2009}
2010
2011
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002012bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002013 return has_frame_ || !stub->SometimesSetsUpAFrame();
Steve Blocka7e24c12009-10-30 11:49:00 +00002014}
2015
2016
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002017void MacroAssembler::IndexFromHash(Register hash, Register index) {
2018 // The assert checks that the constants for the maximum number of digits
2019 // for an array index cached in the hash field and the number of bits
2020 // reserved for it does not conflict.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002021 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002022 (1 << String::kArrayIndexValueBits));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002023 if (!index.is(hash)) {
2024 mov(index, hash);
2025 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002026 DecodeFieldToSmi<String::ArrayIndexValueBits>(index);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002027}
2028
2029
Steve Block44f0eee2011-05-26 01:26:41 +01002030void MacroAssembler::CallRuntime(const Runtime::Function* f,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002031 int num_arguments,
2032 SaveFPRegsMode save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002033 // If the expected number of arguments of the runtime function is
2034 // constant, we check that the actual number of arguments match the
2035 // expectation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002036 CHECK(f->nargs < 0 || f->nargs == num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00002037
Leon Clarke4515c472010-02-03 11:58:03 +00002038 // TODO(1236192): Most runtime routines don't need the number of
2039 // arguments passed in because it is constant. At some point we
2040 // should remove this need and make the runtime routine entry code
2041 // smarter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002042 Move(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01002043 mov(ebx, Immediate(ExternalReference(f, isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002044 CEntryStub ces(isolate(), 1, save_doubles);
Leon Clarke4515c472010-02-03 11:58:03 +00002045 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00002046}
2047
2048
Ben Murdochbb769b22010-08-11 14:56:33 +01002049void MacroAssembler::CallExternalReference(ExternalReference ref,
2050 int num_arguments) {
2051 mov(eax, Immediate(num_arguments));
2052 mov(ebx, Immediate(ref));
2053
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002054 CEntryStub stub(isolate(), 1);
Ben Murdochbb769b22010-08-11 14:56:33 +01002055 CallStub(&stub);
2056}
2057
2058
Steve Block6ded16b2010-05-10 14:33:55 +01002059void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2060 int num_arguments,
2061 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002062 // TODO(1236192): Most runtime routines don't need the number of
2063 // arguments passed in because it is constant. At some point we
2064 // should remove this need and make the runtime routine entry code
2065 // smarter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002066 Move(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002067 JumpToExternalReference(ext);
2068}
2069
2070
2071void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2072 int num_arguments,
2073 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002074 TailCallExternalReference(ExternalReference(fid, isolate()),
2075 num_arguments,
2076 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002077}
2078
2079
John Reck59135872010-11-02 12:39:01 -07002080Operand ApiParameterOperand(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002081 return Operand(esp, index * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07002082}
2083
2084
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002085void MacroAssembler::PrepareCallApiFunction(int argc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002086 EnterApiExitFrame(argc);
2087 if (emit_debug_code()) {
2088 mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002089 }
2090}
2091
2092
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093void MacroAssembler::CallApiFunctionAndReturn(
2094 Register function_address,
2095 ExternalReference thunk_ref,
2096 Operand thunk_last_arg,
2097 int stack_space,
2098 Operand return_value_operand,
2099 Operand* context_restore_operand) {
Steve Blockd0582a62009-12-15 09:54:21 +00002100 ExternalReference next_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002101 ExternalReference::handle_scope_next_address(isolate());
Steve Blockd0582a62009-12-15 09:54:21 +00002102 ExternalReference limit_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002103 ExternalReference::handle_scope_limit_address(isolate());
John Reck59135872010-11-02 12:39:01 -07002104 ExternalReference level_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002105 ExternalReference::handle_scope_level_address(isolate());
Steve Blockd0582a62009-12-15 09:54:21 +00002106
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002107 DCHECK(edx.is(function_address));
John Reck59135872010-11-02 12:39:01 -07002108 // Allocate HandleScope in callee-save registers.
2109 mov(ebx, Operand::StaticVariable(next_address));
2110 mov(edi, Operand::StaticVariable(limit_address));
2111 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00002112
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002113 if (FLAG_log_timer_events) {
2114 FrameScope frame(this, StackFrame::MANUAL);
2115 PushSafepointRegisters();
2116 PrepareCallCFunction(1, eax);
2117 mov(Operand(esp, 0),
2118 Immediate(ExternalReference::isolate_address(isolate())));
2119 CallCFunction(ExternalReference::log_enter_external_function(isolate()), 1);
2120 PopSafepointRegisters();
Leon Clarkee46be812010-01-19 14:06:41 +00002121 }
Steve Blockd0582a62009-12-15 09:54:21 +00002122
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002123
2124 Label profiler_disabled;
2125 Label end_profiler_check;
2126 mov(eax, Immediate(ExternalReference::is_profiling_address(isolate())));
2127 cmpb(Operand(eax, 0), 0);
2128 j(zero, &profiler_disabled);
2129
2130 // Additional parameter is the address of the actual getter function.
2131 mov(thunk_last_arg, function_address);
2132 // Call the api function.
2133 mov(eax, Immediate(thunk_ref));
2134 call(eax);
2135 jmp(&end_profiler_check);
2136
2137 bind(&profiler_disabled);
2138 // Call the api function.
2139 call(function_address);
2140 bind(&end_profiler_check);
2141
2142 if (FLAG_log_timer_events) {
2143 FrameScope frame(this, StackFrame::MANUAL);
2144 PushSafepointRegisters();
2145 PrepareCallCFunction(1, eax);
2146 mov(Operand(esp, 0),
2147 Immediate(ExternalReference::isolate_address(isolate())));
2148 CallCFunction(ExternalReference::log_leave_external_function(isolate()), 1);
2149 PopSafepointRegisters();
2150 }
2151
John Reck59135872010-11-02 12:39:01 -07002152 Label prologue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002153 // Load the value from ReturnValue
2154 mov(eax, return_value_operand);
2155
John Reck59135872010-11-02 12:39:01 -07002156 Label promote_scheduled_exception;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002157 Label exception_handled;
John Reck59135872010-11-02 12:39:01 -07002158 Label delete_allocated_handles;
2159 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00002160
John Reck59135872010-11-02 12:39:01 -07002161 bind(&prologue);
2162 // No more valid handles (the result handle was the last one). Restore
2163 // previous handle scope.
2164 mov(Operand::StaticVariable(next_address), ebx);
2165 sub(Operand::StaticVariable(level_address), Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002166 Assert(above_equal, kInvalidHandleScopeLevel);
John Reck59135872010-11-02 12:39:01 -07002167 cmp(edi, Operand::StaticVariable(limit_address));
Ben Murdoch257744e2011-11-30 15:57:28 +00002168 j(not_equal, &delete_allocated_handles);
John Reck59135872010-11-02 12:39:01 -07002169 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00002170
John Reck59135872010-11-02 12:39:01 -07002171 // Check if the function scheduled an exception.
2172 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01002173 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07002174 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01002175 Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00002176 j(not_equal, &promote_scheduled_exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002177 bind(&exception_handled);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002178
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002179#if ENABLE_EXTRA_CHECKS
2180 // Check if the function returned a valid JavaScript value.
2181 Label ok;
2182 Register return_value = eax;
2183 Register map = ecx;
2184
2185 JumpIfSmi(return_value, &ok, Label::kNear);
2186 mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
2187
2188 CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2189 j(below, &ok, Label::kNear);
2190
2191 CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2192 j(above_equal, &ok, Label::kNear);
2193
2194 cmp(map, isolate()->factory()->heap_number_map());
2195 j(equal, &ok, Label::kNear);
2196
2197 cmp(return_value, isolate()->factory()->undefined_value());
2198 j(equal, &ok, Label::kNear);
2199
2200 cmp(return_value, isolate()->factory()->true_value());
2201 j(equal, &ok, Label::kNear);
2202
2203 cmp(return_value, isolate()->factory()->false_value());
2204 j(equal, &ok, Label::kNear);
2205
2206 cmp(return_value, isolate()->factory()->null_value());
2207 j(equal, &ok, Label::kNear);
2208
2209 Abort(kAPICallReturnedInvalidObject);
2210
2211 bind(&ok);
2212#endif
2213
2214 bool restore_context = context_restore_operand != NULL;
2215 if (restore_context) {
2216 mov(esi, *context_restore_operand);
2217 }
2218 LeaveApiExitFrame(!restore_context);
2219 ret(stack_space * kPointerSize);
2220
2221 bind(&promote_scheduled_exception);
2222 {
2223 FrameScope frame(this, StackFrame::INTERNAL);
2224 CallRuntime(Runtime::kPromoteScheduledException, 0);
2225 }
2226 jmp(&exception_handled);
Leon Clarkee46be812010-01-19 14:06:41 +00002227
John Reck59135872010-11-02 12:39:01 -07002228 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01002229 ExternalReference delete_extensions =
2230 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07002231 bind(&delete_allocated_handles);
2232 mov(Operand::StaticVariable(limit_address), edi);
2233 mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002234 mov(Operand(esp, 0),
2235 Immediate(ExternalReference::isolate_address(isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01002236 mov(eax, Immediate(delete_extensions));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002237 call(eax);
John Reck59135872010-11-02 12:39:01 -07002238 mov(eax, edi);
2239 jmp(&leave_exit_frame);
Steve Blockd0582a62009-12-15 09:54:21 +00002240}
2241
2242
Steve Block6ded16b2010-05-10 14:33:55 +01002243void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002244 // Set the entry point and jump to the C entry runtime stub.
2245 mov(ebx, Immediate(ext));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002246 CEntryStub ces(isolate(), 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002247 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
2248}
2249
2250
2251void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2252 const ParameterCount& actual,
2253 Handle<Code> code_constant,
2254 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +00002255 Label* done,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002256 bool* definitely_mismatches,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002257 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002258 Label::Distance done_near,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002259 const CallWrapper& call_wrapper) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002260 bool definitely_matches = false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002261 *definitely_mismatches = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002262 Label invoke;
2263 if (expected.is_immediate()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002264 DCHECK(actual.is_immediate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 if (expected.immediate() == actual.immediate()) {
2266 definitely_matches = true;
2267 } else {
2268 mov(eax, actual.immediate());
2269 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
2270 if (expected.immediate() == sentinel) {
2271 // Don't worry about adapting arguments for builtins that
2272 // don't want that done. Skip adaption code by making it look
2273 // like we have a match between expected and actual number of
2274 // arguments.
2275 definitely_matches = true;
2276 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002277 *definitely_mismatches = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002278 mov(ebx, expected.immediate());
2279 }
2280 }
2281 } else {
2282 if (actual.is_immediate()) {
2283 // Expected is in register, actual is immediate. This is the
2284 // case when we invoke function values without going through the
2285 // IC mechanism.
2286 cmp(expected.reg(), actual.immediate());
2287 j(equal, &invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002288 DCHECK(expected.reg().is(ebx));
Steve Blocka7e24c12009-10-30 11:49:00 +00002289 mov(eax, actual.immediate());
2290 } else if (!expected.reg().is(actual.reg())) {
2291 // Both expected and actual are in (different) registers. This
2292 // is the case when we invoke functions using call and apply.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002293 cmp(expected.reg(), actual.reg());
Steve Blocka7e24c12009-10-30 11:49:00 +00002294 j(equal, &invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002295 DCHECK(actual.reg().is(eax));
2296 DCHECK(expected.reg().is(ebx));
Steve Blocka7e24c12009-10-30 11:49:00 +00002297 }
2298 }
2299
2300 if (!definitely_matches) {
2301 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01002302 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00002303 if (!code_constant.is_null()) {
2304 mov(edx, Immediate(code_constant));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002305 add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00002306 } else if (!code_operand.is_reg(edx)) {
2307 mov(edx, code_operand);
2308 }
2309
2310 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002311 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
Steve Blocka7e24c12009-10-30 11:49:00 +00002312 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdoch257744e2011-11-30 15:57:28 +00002313 call_wrapper.AfterCall();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002314 if (!*definitely_mismatches) {
2315 jmp(done, done_near);
2316 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002317 } else {
2318 jmp(adaptor, RelocInfo::CODE_TARGET);
2319 }
2320 bind(&invoke);
2321 }
2322}
2323
2324
2325void MacroAssembler::InvokeCode(const Operand& code,
2326 const ParameterCount& expected,
2327 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002328 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002329 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002330 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002331 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002332
Ben Murdoch257744e2011-11-30 15:57:28 +00002333 Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002334 bool definitely_mismatches = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002335 InvokePrologue(expected, actual, Handle<Code>::null(), code,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002336 &done, &definitely_mismatches, flag, Label::kNear,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002337 call_wrapper);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002338 if (!definitely_mismatches) {
2339 if (flag == CALL_FUNCTION) {
2340 call_wrapper.BeforeCall(CallSize(code));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002341 call(code);
2342 call_wrapper.AfterCall();
2343 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002344 DCHECK(flag == JUMP_FUNCTION);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002345 jmp(code);
2346 }
2347 bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +00002348 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002349}
2350
2351
Steve Blocka7e24c12009-10-30 11:49:00 +00002352void MacroAssembler::InvokeFunction(Register fun,
2353 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002354 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002355 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002356 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002357 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002358
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002359 DCHECK(fun.is(edi));
Steve Blocka7e24c12009-10-30 11:49:00 +00002360 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2361 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2362 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002363 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00002364
2365 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01002366 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002367 expected, actual, flag, call_wrapper);
2368}
2369
2370
2371void MacroAssembler::InvokeFunction(Register fun,
2372 const ParameterCount& expected,
2373 const ParameterCount& actual,
2374 InvokeFlag flag,
2375 const CallWrapper& call_wrapper) {
2376 // You can't call a function without a valid frame.
2377 DCHECK(flag == JUMP_FUNCTION || has_frame());
2378
2379 DCHECK(fun.is(edi));
2380 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2381
2382 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2383 expected, actual, flag, call_wrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +00002384}
2385
2386
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002387void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002388 const ParameterCount& expected,
Andrei Popescu402d9372010-02-26 13:31:12 +00002389 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002390 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002391 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002392 LoadHeapObject(edi, function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002393 InvokeFunction(edi, expected, actual, flag, call_wrapper);
Andrei Popescu402d9372010-02-26 13:31:12 +00002394}
2395
2396
Ben Murdochb0fe1622011-05-05 13:52:32 +01002397void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
2398 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002399 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002400 // You can't call a builtin without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002401 DCHECK(flag == JUMP_FUNCTION || has_frame());
Steve Blocka7e24c12009-10-30 11:49:00 +00002402
2403 // Rely on the assertion to check that the number of provided
2404 // arguments match the expected number of arguments. Fake a
2405 // parameter count to avoid emitting code to do the check.
2406 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01002407 GetBuiltinFunction(edi, id);
2408 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002409 expected, expected, flag, call_wrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +00002410}
2411
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002412
Steve Block791712a2010-08-27 10:21:07 +01002413void MacroAssembler::GetBuiltinFunction(Register target,
2414 Builtins::JavaScript id) {
2415 // Load the JavaScript builtin function from the builtins object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002416 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
Steve Block791712a2010-08-27 10:21:07 +01002417 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
2418 mov(target, FieldOperand(target,
2419 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2420}
Steve Blocka7e24c12009-10-30 11:49:00 +00002421
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002422
Steve Blocka7e24c12009-10-30 11:49:00 +00002423void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002424 DCHECK(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00002425 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01002426 GetBuiltinFunction(edi, id);
2427 // Load the code entry point from the function into the target register.
2428 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002429}
2430
2431
Steve Blockd0582a62009-12-15 09:54:21 +00002432void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2433 if (context_chain_length > 0) {
2434 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002435 mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002436 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002437 mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002438 }
Steve Block1e0659c2011-05-24 12:43:12 +01002439 } else {
2440 // Slot is in the current function context. Move it into the
2441 // destination register in case we store into it (the write barrier
2442 // cannot be allowed to destroy the context in esi).
2443 mov(dst, esi);
2444 }
2445
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002446 // We should not have found a with context by walking the context chain
Steve Block1e0659c2011-05-24 12:43:12 +01002447 // (i.e., the static scope chain and runtime context chain do not agree).
2448 // A variable occurring in such a scope should have slot type LOOKUP and
2449 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01002450 if (emit_debug_code()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002451 cmp(FieldOperand(dst, HeapObject::kMapOffset),
2452 isolate()->factory()->with_context_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002453 Check(not_equal, kVariableResolvedToWithContext);
Steve Blockd0582a62009-12-15 09:54:21 +00002454 }
2455}
2456
2457
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002458void MacroAssembler::LoadTransitionedArrayMapConditional(
2459 ElementsKind expected_kind,
2460 ElementsKind transitioned_kind,
2461 Register map_in_out,
2462 Register scratch,
2463 Label* no_map_match) {
2464 // Load the global or builtins object from the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002465 mov(scratch, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2466 mov(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002467
2468 // Check that the function's map is the same as the expected cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002469 mov(scratch, Operand(scratch,
2470 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
2471
2472 size_t offset = expected_kind * kPointerSize +
2473 FixedArrayBase::kHeaderSize;
2474 cmp(map_in_out, FieldOperand(scratch, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002475 j(not_equal, no_map_match);
2476
2477 // Use the transitioned cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002478 offset = transitioned_kind * kPointerSize +
2479 FixedArrayBase::kHeaderSize;
2480 mov(map_in_out, FieldOperand(scratch, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002481}
2482
2483
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002484void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2485 // Load the global or builtins object from the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002486 mov(function,
2487 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2488 // Load the native context from the global or builtins object.
2489 mov(function,
2490 FieldOperand(function, GlobalObject::kNativeContextOffset));
2491 // Load the function from the native context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002492 mov(function, Operand(function, Context::SlotOffset(index)));
2493}
2494
2495
2496void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2497 Register map) {
2498 // Load the initial map. The global functions all have initial maps.
2499 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002500 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002501 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00002502 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002503 jmp(&ok);
2504 bind(&fail);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002505 Abort(kGlobalFunctionsMustHaveInitialMap);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002506 bind(&ok);
2507 }
2508}
2509
Steve Blockd0582a62009-12-15 09:54:21 +00002510
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002511// Store the value in register src in the safepoint register stack
2512// slot for register dst.
2513void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
2514 mov(SafepointRegisterSlot(dst), src);
2515}
2516
2517
2518void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
2519 mov(SafepointRegisterSlot(dst), src);
2520}
2521
2522
2523void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
2524 mov(dst, SafepointRegisterSlot(src));
2525}
2526
2527
2528Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
2529 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
2530}
2531
2532
Ben Murdochb0fe1622011-05-05 13:52:32 +01002533int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2534 // The registers are pushed starting with the lowest encoding,
2535 // which means that lowest encodings are furthest away from
2536 // the stack pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002537 DCHECK(reg_code >= 0 && reg_code < kNumSafepointRegisters);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002538 return kNumSafepointRegisters - reg_code - 1;
2539}
2540
2541
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002542void MacroAssembler::LoadHeapObject(Register result,
2543 Handle<HeapObject> object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002544 AllowDeferredHandleDereference embedding_raw_address;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002545 if (isolate()->heap()->InNewSpace(*object)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002546 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2547 mov(result, Operand::ForCell(cell));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002548 } else {
2549 mov(result, object);
2550 }
2551}
2552
2553
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002554void MacroAssembler::CmpHeapObject(Register reg, Handle<HeapObject> object) {
2555 AllowDeferredHandleDereference using_raw_address;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002556 if (isolate()->heap()->InNewSpace(*object)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002557 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2558 cmp(reg, Operand::ForCell(cell));
2559 } else {
2560 cmp(reg, object);
2561 }
2562}
2563
2564
2565void MacroAssembler::PushHeapObject(Handle<HeapObject> object) {
2566 AllowDeferredHandleDereference using_raw_address;
2567 if (isolate()->heap()->InNewSpace(*object)) {
2568 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2569 push(Operand::ForCell(cell));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002570 } else {
2571 Push(object);
2572 }
2573}
2574
2575
Steve Blocka7e24c12009-10-30 11:49:00 +00002576void MacroAssembler::Ret() {
2577 ret(0);
2578}
2579
2580
Steve Block1e0659c2011-05-24 12:43:12 +01002581void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
2582 if (is_uint16(bytes_dropped)) {
2583 ret(bytes_dropped);
2584 } else {
2585 pop(scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002586 add(esp, Immediate(bytes_dropped));
Steve Block1e0659c2011-05-24 12:43:12 +01002587 push(scratch);
2588 ret(0);
2589 }
2590}
2591
2592
Leon Clarkee46be812010-01-19 14:06:41 +00002593void MacroAssembler::Drop(int stack_elements) {
2594 if (stack_elements > 0) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002595 add(esp, Immediate(stack_elements * kPointerSize));
Leon Clarkee46be812010-01-19 14:06:41 +00002596 }
2597}
2598
2599
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002600void MacroAssembler::Move(Register dst, Register src) {
2601 if (!dst.is(src)) {
2602 mov(dst, src);
2603 }
2604}
2605
2606
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002607void MacroAssembler::Move(Register dst, const Immediate& x) {
2608 if (x.is_zero()) {
2609 xor_(dst, dst); // Shorter than mov of 32-bit immediate 0.
2610 } else {
2611 mov(dst, x);
2612 }
2613}
2614
2615
2616void MacroAssembler::Move(const Operand& dst, const Immediate& x) {
2617 mov(dst, x);
2618}
2619
2620
2621void MacroAssembler::Move(XMMRegister dst, double val) {
2622 // TODO(titzer): recognize double constants with ExternalReferences.
2623 uint64_t int_val = bit_cast<uint64_t, double>(val);
2624 if (int_val == 0) {
2625 xorps(dst, dst);
2626 } else {
2627 int32_t lower = static_cast<int32_t>(int_val);
2628 int32_t upper = static_cast<int32_t>(int_val >> kBitsPerInt);
2629 push(Immediate(upper));
2630 push(Immediate(lower));
2631 movsd(dst, Operand(esp, 0));
2632 add(esp, Immediate(kDoubleSize));
2633 }
2634}
2635
2636
Steve Blocka7e24c12009-10-30 11:49:00 +00002637void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
2638 if (FLAG_native_code_counters && counter->Enabled()) {
2639 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
2640 }
2641}
2642
2643
2644void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002645 DCHECK(value > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002646 if (FLAG_native_code_counters && counter->Enabled()) {
2647 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2648 if (value == 1) {
2649 inc(operand);
2650 } else {
2651 add(operand, Immediate(value));
2652 }
2653 }
2654}
2655
2656
2657void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002658 DCHECK(value > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002659 if (FLAG_native_code_counters && counter->Enabled()) {
2660 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2661 if (value == 1) {
2662 dec(operand);
2663 } else {
2664 sub(operand, Immediate(value));
2665 }
2666 }
2667}
2668
2669
Leon Clarked91b9f72010-01-27 17:25:45 +00002670void MacroAssembler::IncrementCounter(Condition cc,
2671 StatsCounter* counter,
2672 int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002673 DCHECK(value > 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002674 if (FLAG_native_code_counters && counter->Enabled()) {
2675 Label skip;
2676 j(NegateCondition(cc), &skip);
2677 pushfd();
2678 IncrementCounter(counter, value);
2679 popfd();
2680 bind(&skip);
2681 }
2682}
2683
2684
2685void MacroAssembler::DecrementCounter(Condition cc,
2686 StatsCounter* counter,
2687 int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002688 DCHECK(value > 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002689 if (FLAG_native_code_counters && counter->Enabled()) {
2690 Label skip;
2691 j(NegateCondition(cc), &skip);
2692 pushfd();
2693 DecrementCounter(counter, value);
2694 popfd();
2695 bind(&skip);
2696 }
2697}
2698
2699
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002700void MacroAssembler::Assert(Condition cc, BailoutReason reason) {
2701 if (emit_debug_code()) Check(cc, reason);
Steve Blocka7e24c12009-10-30 11:49:00 +00002702}
2703
2704
Iain Merrick75681382010-08-19 15:07:18 +01002705void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002706 if (emit_debug_code()) {
2707 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01002708 Label ok;
2709 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01002710 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01002711 j(equal, &ok);
2712 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002713 Immediate(factory->fixed_double_array_map()));
2714 j(equal, &ok);
2715 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01002716 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01002717 j(equal, &ok);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002718 Abort(kJSObjectWithFastElementsMapHasSlowElements);
Iain Merrick75681382010-08-19 15:07:18 +01002719 bind(&ok);
2720 }
2721}
2722
2723
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002724void MacroAssembler::Check(Condition cc, BailoutReason reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002725 Label L;
Ben Murdoch257744e2011-11-30 15:57:28 +00002726 j(cc, &L);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002727 Abort(reason);
Steve Blocka7e24c12009-10-30 11:49:00 +00002728 // will not return here
2729 bind(&L);
2730}
2731
2732
Steve Block6ded16b2010-05-10 14:33:55 +01002733void MacroAssembler::CheckStackAlignment() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002734 int frame_alignment = base::OS::ActivationFrameAlignment();
Steve Block6ded16b2010-05-10 14:33:55 +01002735 int frame_alignment_mask = frame_alignment - 1;
2736 if (frame_alignment > kPointerSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002737 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Steve Block6ded16b2010-05-10 14:33:55 +01002738 Label alignment_as_expected;
2739 test(esp, Immediate(frame_alignment_mask));
2740 j(zero, &alignment_as_expected);
2741 // Abort if stack is not aligned.
2742 int3();
2743 bind(&alignment_as_expected);
2744 }
2745}
2746
2747
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002748void MacroAssembler::Abort(BailoutReason reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002749#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002750 const char* msg = GetBailoutReason(reason);
Steve Blocka7e24c12009-10-30 11:49:00 +00002751 if (msg != NULL) {
2752 RecordComment("Abort message: ");
2753 RecordComment(msg);
2754 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002755
2756 if (FLAG_trap_on_abort) {
2757 int3();
2758 return;
2759 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002760#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002761
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002762 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(reason))));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002763 // Disable stub call restrictions to always allow calls to abort.
2764 if (!has_frame_) {
2765 // We don't actually want to generate a pile of code for this, so just
2766 // claim there is a stack frame, without generating one.
2767 FrameScope scope(this, StackFrame::NONE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002768 CallRuntime(Runtime::kAbort, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002769 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002770 CallRuntime(Runtime::kAbort, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002771 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002772 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00002773 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00002774}
2775
2776
Ben Murdoch257744e2011-11-30 15:57:28 +00002777void MacroAssembler::LoadInstanceDescriptors(Register map,
2778 Register descriptors) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002779 mov(descriptors, FieldOperand(map, Map::kDescriptorsOffset));
2780}
2781
2782
2783void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
2784 mov(dst, FieldOperand(map, Map::kBitField3Offset));
2785 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
Iain Merrick75681382010-08-19 15:07:18 +01002786}
2787
2788
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002789void MacroAssembler::LoadPowerOf2(XMMRegister dst,
2790 Register scratch,
2791 int power) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002792 DCHECK(is_uintn(power + HeapNumber::kExponentBias,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002793 HeapNumber::kExponentBits));
2794 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002795 movd(dst, scratch);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002796 psllq(dst, HeapNumber::kMantissaBits);
2797}
2798
2799
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002800void MacroAssembler::LookupNumberStringCache(Register object,
2801 Register result,
2802 Register scratch1,
2803 Register scratch2,
2804 Label* not_found) {
2805 // Use of registers. Register result is used as a temporary.
2806 Register number_string_cache = result;
2807 Register mask = scratch1;
2808 Register scratch = scratch2;
2809
2810 // Load the number string cache.
2811 LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
2812 // Make the hash mask from the length of the number string cache. It
2813 // contains two elements (number and string) for each cache entry.
2814 mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2815 shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
2816 sub(mask, Immediate(1)); // Make mask.
2817
2818 // Calculate the entry in the number string cache. The hash value in the
2819 // number string cache for smis is just the smi value, and the hash for
2820 // doubles is the xor of the upper and lower words. See
2821 // Heap::GetNumberStringCache.
2822 Label smi_hash_calculated;
2823 Label load_result_from_cache;
2824 Label not_smi;
2825 STATIC_ASSERT(kSmiTag == 0);
2826 JumpIfNotSmi(object, &not_smi, Label::kNear);
2827 mov(scratch, object);
2828 SmiUntag(scratch);
2829 jmp(&smi_hash_calculated, Label::kNear);
2830 bind(&not_smi);
2831 cmp(FieldOperand(object, HeapObject::kMapOffset),
2832 isolate()->factory()->heap_number_map());
2833 j(not_equal, not_found);
2834 STATIC_ASSERT(8 == kDoubleSize);
2835 mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2836 xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2837 // Object is heap number and hash is now in scratch. Calculate cache index.
2838 and_(scratch, mask);
2839 Register index = scratch;
2840 Register probe = mask;
2841 mov(probe,
2842 FieldOperand(number_string_cache,
2843 index,
2844 times_twice_pointer_size,
2845 FixedArray::kHeaderSize));
2846 JumpIfSmi(probe, not_found);
2847 movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2848 ucomisd(xmm0, FieldOperand(probe, HeapNumber::kValueOffset));
2849 j(parity_even, not_found); // Bail out if NaN is involved.
2850 j(not_equal, not_found); // The cache did not contain this value.
2851 jmp(&load_result_from_cache, Label::kNear);
2852
2853 bind(&smi_hash_calculated);
2854 // Object is smi and hash is now in scratch. Calculate cache index.
2855 and_(scratch, mask);
2856 // Check if the entry is the smi we are looking for.
2857 cmp(object,
2858 FieldOperand(number_string_cache,
2859 index,
2860 times_twice_pointer_size,
2861 FixedArray::kHeaderSize));
2862 j(not_equal, not_found);
2863
2864 // Get the result from the cache.
2865 bind(&load_result_from_cache);
2866 mov(result,
2867 FieldOperand(number_string_cache,
2868 index,
2869 times_twice_pointer_size,
2870 FixedArray::kHeaderSize + kPointerSize));
2871 IncrementCounter(isolate()->counters()->number_to_string_native(), 1);
2872}
2873
2874
2875void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(
2876 Register instance_type, Register scratch, Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002877 if (!scratch.is(instance_type)) {
2878 mov(scratch, instance_type);
2879 }
2880 and_(scratch,
2881 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002882 cmp(scratch, kStringTag | kSeqStringTag | kOneByteStringTag);
Andrei Popescu402d9372010-02-26 13:31:12 +00002883 j(not_equal, failure);
2884}
2885
2886
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002887void MacroAssembler::JumpIfNotBothSequentialOneByteStrings(Register object1,
2888 Register object2,
2889 Register scratch1,
2890 Register scratch2,
2891 Label* failure) {
Leon Clarked91b9f72010-01-27 17:25:45 +00002892 // Check that both objects are not smis.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002893 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002894 mov(scratch1, object1);
2895 and_(scratch1, object2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002896 JumpIfSmi(scratch1, failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002897
2898 // Load instance type for both strings.
2899 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2900 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2901 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2902 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2903
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002904 // Check that both are flat one-byte strings.
2905 const int kFlatOneByteStringMask =
Leon Clarked91b9f72010-01-27 17:25:45 +00002906 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002907 const int kFlatOneByteStringTag =
2908 kStringTag | kOneByteStringTag | kSeqStringTag;
Leon Clarked91b9f72010-01-27 17:25:45 +00002909 // Interleave bits from both instance types and compare them in one check.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002910 DCHECK_EQ(0, kFlatOneByteStringMask & (kFlatOneByteStringMask << 3));
2911 and_(scratch1, kFlatOneByteStringMask);
2912 and_(scratch2, kFlatOneByteStringMask);
Leon Clarked91b9f72010-01-27 17:25:45 +00002913 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002914 cmp(scratch1, kFlatOneByteStringTag | (kFlatOneByteStringTag << 3));
Leon Clarked91b9f72010-01-27 17:25:45 +00002915 j(not_equal, failure);
2916}
2917
2918
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002919void MacroAssembler::JumpIfNotUniqueNameInstanceType(Operand operand,
2920 Label* not_unique_name,
2921 Label::Distance distance) {
2922 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2923 Label succeed;
2924 test(operand, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2925 j(zero, &succeed);
2926 cmpb(operand, static_cast<uint8_t>(SYMBOL_TYPE));
2927 j(not_equal, not_unique_name, distance);
2928
2929 bind(&succeed);
2930}
2931
2932
2933void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
2934 Register index,
2935 Register value,
2936 uint32_t encoding_mask) {
2937 Label is_object;
2938 JumpIfNotSmi(string, &is_object, Label::kNear);
2939 Abort(kNonObject);
2940 bind(&is_object);
2941
2942 push(value);
2943 mov(value, FieldOperand(string, HeapObject::kMapOffset));
2944 movzx_b(value, FieldOperand(value, Map::kInstanceTypeOffset));
2945
2946 and_(value, Immediate(kStringRepresentationMask | kStringEncodingMask));
2947 cmp(value, Immediate(encoding_mask));
2948 pop(value);
2949 Check(equal, kUnexpectedStringType);
2950
2951 // The index is assumed to be untagged coming in, tag it to compare with the
2952 // string length without using a temp register, it is restored at the end of
2953 // this function.
2954 SmiTag(index);
2955 Check(no_overflow, kIndexIsTooLarge);
2956
2957 cmp(index, FieldOperand(string, String::kLengthOffset));
2958 Check(less, kIndexIsTooLarge);
2959
2960 cmp(index, Immediate(Smi::FromInt(0)));
2961 Check(greater_equal, kIndexIsNegative);
2962
2963 // Restore the index
2964 SmiUntag(index);
2965}
2966
2967
Steve Block6ded16b2010-05-10 14:33:55 +01002968void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002969 int frame_alignment = base::OS::ActivationFrameAlignment();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002970 if (frame_alignment != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01002971 // Make stack end at alignment and make room for num_arguments words
2972 // and the original value of esp.
2973 mov(scratch, esp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002974 sub(esp, Immediate((num_arguments + 1) * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002975 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Ben Murdoch8b112d22011-06-08 16:22:53 +01002976 and_(esp, -frame_alignment);
Steve Block6ded16b2010-05-10 14:33:55 +01002977 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2978 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002979 sub(esp, Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01002980 }
2981}
2982
2983
2984void MacroAssembler::CallCFunction(ExternalReference function,
2985 int num_arguments) {
2986 // Trashing eax is ok as it will be the return value.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002987 mov(eax, Immediate(function));
Steve Block6ded16b2010-05-10 14:33:55 +01002988 CallCFunction(eax, num_arguments);
2989}
2990
2991
2992void MacroAssembler::CallCFunction(Register function,
2993 int num_arguments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002994 DCHECK(has_frame());
Steve Block6ded16b2010-05-10 14:33:55 +01002995 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01002996 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002997 CheckStackAlignment();
2998 }
2999
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003000 call(function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003001 if (base::OS::ActivationFrameAlignment() != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01003002 mov(esp, Operand(esp, num_arguments * kPointerSize));
3003 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003004 add(esp, Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01003005 }
3006}
3007
3008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003009#ifdef DEBUG
3010bool AreAliased(Register reg1,
3011 Register reg2,
3012 Register reg3,
3013 Register reg4,
3014 Register reg5,
3015 Register reg6,
3016 Register reg7,
3017 Register reg8) {
3018 int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
3019 reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
3020 reg7.is_valid() + reg8.is_valid();
3021
3022 RegList regs = 0;
3023 if (reg1.is_valid()) regs |= reg1.bit();
3024 if (reg2.is_valid()) regs |= reg2.bit();
3025 if (reg3.is_valid()) regs |= reg3.bit();
3026 if (reg4.is_valid()) regs |= reg4.bit();
3027 if (reg5.is_valid()) regs |= reg5.bit();
3028 if (reg6.is_valid()) regs |= reg6.bit();
3029 if (reg7.is_valid()) regs |= reg7.bit();
3030 if (reg8.is_valid()) regs |= reg8.bit();
3031 int n_of_non_aliasing_regs = NumRegs(regs);
3032
3033 return n_of_valid_regs != n_of_non_aliasing_regs;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003034}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003035#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003036
3037
Steve Blocka7e24c12009-10-30 11:49:00 +00003038CodePatcher::CodePatcher(byte* address, int size)
Ben Murdoch8b112d22011-06-08 16:22:53 +01003039 : address_(address),
3040 size_(size),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003041 masm_(NULL, address, size + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003042 // Create a new macro assembler pointing to the address of the code to patch.
3043 // The size is adjusted with kGap on order for the assembler to generate size
3044 // bytes of instructions without failing with buffer size constraints.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003045 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003046}
3047
3048
3049CodePatcher::~CodePatcher() {
3050 // Indicate that code has changed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003051 CpuFeatures::FlushICache(address_, size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003052
3053 // Check that the code was patched as expected.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003054 DCHECK(masm_.pc_ == address_ + size_);
3055 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003056}
3057
3058
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003059void MacroAssembler::CheckPageFlag(
3060 Register object,
3061 Register scratch,
3062 int mask,
3063 Condition cc,
3064 Label* condition_met,
3065 Label::Distance condition_met_distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003066 DCHECK(cc == zero || cc == not_zero);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003067 if (scratch.is(object)) {
3068 and_(scratch, Immediate(~Page::kPageAlignmentMask));
3069 } else {
3070 mov(scratch, Immediate(~Page::kPageAlignmentMask));
3071 and_(scratch, object);
3072 }
3073 if (mask < (1 << kBitsPerByte)) {
3074 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
3075 static_cast<uint8_t>(mask));
3076 } else {
3077 test(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
3078 }
3079 j(cc, condition_met, condition_met_distance);
3080}
3081
3082
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003083void MacroAssembler::CheckPageFlagForMap(
3084 Handle<Map> map,
3085 int mask,
3086 Condition cc,
3087 Label* condition_met,
3088 Label::Distance condition_met_distance) {
3089 DCHECK(cc == zero || cc == not_zero);
3090 Page* page = Page::FromAddress(map->address());
3091 DCHECK(!serializer_enabled()); // Serializer cannot match page_flags.
3092 ExternalReference reference(ExternalReference::page_flags(page));
3093 // The inlined static address check of the page's flags relies
3094 // on maps never being compacted.
3095 DCHECK(!isolate()->heap()->mark_compact_collector()->
3096 IsOnEvacuationCandidate(*map));
3097 if (mask < (1 << kBitsPerByte)) {
3098 test_b(Operand::StaticVariable(reference), static_cast<uint8_t>(mask));
3099 } else {
3100 test(Operand::StaticVariable(reference), Immediate(mask));
3101 }
3102 j(cc, condition_met, condition_met_distance);
3103}
3104
3105
3106void MacroAssembler::CheckMapDeprecated(Handle<Map> map,
3107 Register scratch,
3108 Label* if_deprecated) {
3109 if (map->CanBeDeprecated()) {
3110 mov(scratch, map);
3111 mov(scratch, FieldOperand(scratch, Map::kBitField3Offset));
3112 and_(scratch, Immediate(Map::Deprecated::kMask));
3113 j(not_zero, if_deprecated);
3114 }
3115}
3116
3117
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003118void MacroAssembler::JumpIfBlack(Register object,
3119 Register scratch0,
3120 Register scratch1,
3121 Label* on_black,
3122 Label::Distance on_black_near) {
3123 HasColor(object, scratch0, scratch1,
3124 on_black, on_black_near,
3125 1, 0); // kBlackBitPattern.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003126 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003127}
3128
3129
3130void MacroAssembler::HasColor(Register object,
3131 Register bitmap_scratch,
3132 Register mask_scratch,
3133 Label* has_color,
3134 Label::Distance has_color_distance,
3135 int first_bit,
3136 int second_bit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003137 DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, ecx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003138
3139 GetMarkBits(object, bitmap_scratch, mask_scratch);
3140
3141 Label other_color, word_boundary;
3142 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3143 j(first_bit == 1 ? zero : not_zero, &other_color, Label::kNear);
3144 add(mask_scratch, mask_scratch); // Shift left 1 by adding.
3145 j(zero, &word_boundary, Label::kNear);
3146 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3147 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3148 jmp(&other_color, Label::kNear);
3149
3150 bind(&word_boundary);
3151 test_b(Operand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize), 1);
3152
3153 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3154 bind(&other_color);
3155}
3156
3157
3158void MacroAssembler::GetMarkBits(Register addr_reg,
3159 Register bitmap_reg,
3160 Register mask_reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003161 DCHECK(!AreAliased(addr_reg, mask_reg, bitmap_reg, ecx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003162 mov(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
3163 and_(bitmap_reg, addr_reg);
3164 mov(ecx, addr_reg);
3165 int shift =
3166 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
3167 shr(ecx, shift);
3168 and_(ecx,
3169 (Page::kPageAlignmentMask >> shift) & ~(Bitmap::kBytesPerCell - 1));
3170
3171 add(bitmap_reg, ecx);
3172 mov(ecx, addr_reg);
3173 shr(ecx, kPointerSizeLog2);
3174 and_(ecx, (1 << Bitmap::kBitsPerCellLog2) - 1);
3175 mov(mask_reg, Immediate(1));
3176 shl_cl(mask_reg);
3177}
3178
3179
3180void MacroAssembler::EnsureNotWhite(
3181 Register value,
3182 Register bitmap_scratch,
3183 Register mask_scratch,
3184 Label* value_is_white_and_not_data,
3185 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003186 DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, ecx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003187 GetMarkBits(value, bitmap_scratch, mask_scratch);
3188
3189 // If the value is black or grey we don't need to do anything.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003190 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
3191 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
3192 DCHECK(strcmp(Marking::kGreyBitPattern, "11") == 0);
3193 DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003194
3195 Label done;
3196
3197 // Since both black and grey have a 1 in the first position and white does
3198 // not have a 1 there we only need to check one bit.
3199 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3200 j(not_zero, &done, Label::kNear);
3201
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003202 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003203 // Check for impossible bit pattern.
3204 Label ok;
3205 push(mask_scratch);
3206 // shl. May overflow making the check conservative.
3207 add(mask_scratch, mask_scratch);
3208 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3209 j(zero, &ok, Label::kNear);
3210 int3();
3211 bind(&ok);
3212 pop(mask_scratch);
3213 }
3214
3215 // Value is white. We check whether it is data that doesn't need scanning.
3216 // Currently only checks for HeapNumber and non-cons strings.
3217 Register map = ecx; // Holds map while checking type.
3218 Register length = ecx; // Holds length of object after checking type.
3219 Label not_heap_number;
3220 Label is_data_object;
3221
3222 // Check for heap-number
3223 mov(map, FieldOperand(value, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003224 cmp(map, isolate()->factory()->heap_number_map());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003225 j(not_equal, &not_heap_number, Label::kNear);
3226 mov(length, Immediate(HeapNumber::kSize));
3227 jmp(&is_data_object, Label::kNear);
3228
3229 bind(&not_heap_number);
3230 // Check for strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003231 DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3232 DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003233 // If it's a string and it's not a cons string then it's an object containing
3234 // no GC pointers.
3235 Register instance_type = ecx;
3236 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3237 test_b(instance_type, kIsIndirectStringMask | kIsNotStringMask);
3238 j(not_zero, value_is_white_and_not_data);
3239 // It's a non-indirect (non-cons and non-slice) string.
3240 // If it's external, the length is just ExternalString::kSize.
3241 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
3242 Label not_external;
3243 // External strings are the only ones with the kExternalStringTag bit
3244 // set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003245 DCHECK_EQ(0, kSeqStringTag & kExternalStringTag);
3246 DCHECK_EQ(0, kConsStringTag & kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003247 test_b(instance_type, kExternalStringTag);
3248 j(zero, &not_external, Label::kNear);
3249 mov(length, Immediate(ExternalString::kSize));
3250 jmp(&is_data_object, Label::kNear);
3251
3252 bind(&not_external);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003253 // Sequential string, either Latin1 or UC16.
3254 DCHECK(kOneByteStringTag == 0x04);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003255 and_(length, Immediate(kStringEncodingMask));
3256 xor_(length, Immediate(kStringEncodingMask));
3257 add(length, Immediate(0x04));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003258 // Value now either 4 (if Latin1) or 8 (if UC16), i.e., char-size shifted
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003259 // by 2. If we multiply the string length as smi by this, it still
3260 // won't overflow a 32-bit value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003261 DCHECK_EQ(SeqOneByteString::kMaxSize, SeqTwoByteString::kMaxSize);
3262 DCHECK(SeqOneByteString::kMaxSize <=
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003263 static_cast<int>(0xffffffffu >> (2 + kSmiTagSize)));
3264 imul(length, FieldOperand(value, String::kLengthOffset));
3265 shr(length, 2 + kSmiTagSize + kSmiShiftSize);
3266 add(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
3267 and_(length, Immediate(~kObjectAlignmentMask));
3268
3269 bind(&is_data_object);
3270 // Value is a data object, and it is white. Mark it black. Since we know
3271 // that the object is white we can make it black by flipping one bit.
3272 or_(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
3273
3274 and_(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
3275 add(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset),
3276 length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003277 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003278 mov(length, Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3279 cmp(length, Operand(bitmap_scratch, MemoryChunk::kSizeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003280 Check(less_equal, kLiveBytesCountOverflowChunkSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003281 }
3282
3283 bind(&done);
3284}
3285
3286
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003287void MacroAssembler::EnumLength(Register dst, Register map) {
3288 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
3289 mov(dst, FieldOperand(map, Map::kBitField3Offset));
3290 and_(dst, Immediate(Map::EnumLengthBits::kMask));
3291 SmiTag(dst);
3292}
3293
3294
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003295void MacroAssembler::CheckEnumCache(Label* call_runtime) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003296 Label next, start;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003297 mov(ecx, eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003298
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003299 // Check if the enum length field is properly initialized, indicating that
3300 // there is an enum cache.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003301 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003302
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003303 EnumLength(edx, ebx);
3304 cmp(edx, Immediate(Smi::FromInt(kInvalidEnumCacheSentinel)));
3305 j(equal, call_runtime);
3306
3307 jmp(&start);
3308
3309 bind(&next);
3310 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003311
3312 // For all objects but the receiver, check that the cache is empty.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003313 EnumLength(edx, ebx);
3314 cmp(edx, Immediate(Smi::FromInt(0)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003315 j(not_equal, call_runtime);
3316
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003317 bind(&start);
3318
3319 // Check that there are no elements. Register rcx contains the current JS
3320 // object we've reached through the prototype chain.
3321 Label no_elements;
3322 mov(ecx, FieldOperand(ecx, JSObject::kElementsOffset));
3323 cmp(ecx, isolate()->factory()->empty_fixed_array());
3324 j(equal, &no_elements);
3325
3326 // Second chance, the object may be using the empty slow element dictionary.
3327 cmp(ecx, isolate()->factory()->empty_slow_element_dictionary());
3328 j(not_equal, call_runtime);
3329
3330 bind(&no_elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003331 mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
3332 cmp(ecx, isolate()->factory()->null_value());
3333 j(not_equal, &next);
3334}
3335
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003336
3337void MacroAssembler::TestJSArrayForAllocationMemento(
3338 Register receiver_reg,
3339 Register scratch_reg,
3340 Label* no_memento_found) {
3341 ExternalReference new_space_start =
3342 ExternalReference::new_space_start(isolate());
3343 ExternalReference new_space_allocation_top =
3344 ExternalReference::new_space_allocation_top_address(isolate());
3345
3346 lea(scratch_reg, Operand(receiver_reg,
3347 JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
3348 cmp(scratch_reg, Immediate(new_space_start));
3349 j(less, no_memento_found);
3350 cmp(scratch_reg, Operand::StaticVariable(new_space_allocation_top));
3351 j(greater, no_memento_found);
3352 cmp(MemOperand(scratch_reg, -AllocationMemento::kSize),
3353 Immediate(isolate()->factory()->allocation_memento_map()));
3354}
3355
3356
3357void MacroAssembler::JumpIfDictionaryInPrototypeChain(
3358 Register object,
3359 Register scratch0,
3360 Register scratch1,
3361 Label* found) {
3362 DCHECK(!scratch1.is(scratch0));
3363 Factory* factory = isolate()->factory();
3364 Register current = scratch0;
3365 Label loop_again;
3366
3367 // scratch contained elements pointer.
3368 mov(current, object);
3369
3370 // Loop based on the map going up the prototype chain.
3371 bind(&loop_again);
3372 mov(current, FieldOperand(current, HeapObject::kMapOffset));
3373 mov(scratch1, FieldOperand(current, Map::kBitField2Offset));
3374 DecodeField<Map::ElementsKindBits>(scratch1);
3375 cmp(scratch1, Immediate(DICTIONARY_ELEMENTS));
3376 j(equal, found);
3377 mov(current, FieldOperand(current, Map::kPrototypeOffset));
3378 cmp(current, Immediate(factory->null_value()));
3379 j(not_equal, &loop_again);
3380}
3381
3382
3383void MacroAssembler::TruncatingDiv(Register dividend, int32_t divisor) {
3384 DCHECK(!dividend.is(eax));
3385 DCHECK(!dividend.is(edx));
3386 base::MagicNumbersForDivision<uint32_t> mag =
3387 base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
3388 mov(eax, Immediate(mag.multiplier));
3389 imul(dividend);
3390 bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
3391 if (divisor > 0 && neg) add(edx, dividend);
3392 if (divisor < 0 && !neg && mag.multiplier > 0) sub(edx, dividend);
3393 if (mag.shift > 0) sar(edx, mag.shift);
3394 mov(eax, dividend);
3395 shr(eax, 31);
3396 add(edx, eax);
3397}
3398
3399
Steve Blocka7e24c12009-10-30 11:49:00 +00003400} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01003401
3402#endif // V8_TARGET_ARCH_IA32