blob: 38259d7651443aedf41b91042855635fda94ad2a [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"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017#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
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400348void MacroAssembler::LoadUint32(XMMRegister dst, const Operand& src) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 Label done;
350 cmp(src, Immediate(0));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400351 ExternalReference uint32_bias = ExternalReference::address_of_uint32_bias();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352 Cvtsi2sd(dst, src);
353 j(not_sign, &done, Label::kNear);
354 addsd(dst, Operand::StaticVariable(uint32_bias));
355 bind(&done);
356}
357
358
359void MacroAssembler::RecordWriteArray(
360 Register object,
361 Register value,
362 Register index,
363 SaveFPRegsMode save_fp,
364 RememberedSetAction remembered_set_action,
365 SmiCheck smi_check,
366 PointersToHereCheck pointers_to_here_check_for_value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100367 // First, check if a write barrier is even needed. The tests below
368 // catch stores of Smis.
369 Label done;
370
371 // Skip barrier if writing a smi.
372 if (smi_check == INLINE_SMI_CHECK) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000373 DCHECK_EQ(0, kSmiTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100374 test(value, Immediate(kSmiTagMask));
375 j(zero, &done);
376 }
377
378 // Array access: calculate the destination address in the same manner as
379 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
380 // into an array of words.
381 Register dst = index;
382 lea(dst, Operand(object, index, times_half_pointer_size,
383 FixedArray::kHeaderSize - kHeapObjectTag));
384
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000385 RecordWrite(object, dst, value, save_fp, remembered_set_action,
386 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100387
388 bind(&done);
389
390 // Clobber clobbered input registers when running with the debug-code flag
391 // turned on to provoke errors.
392 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
394 mov(index, Immediate(bit_cast<int32_t>(kZapValue)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000395 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000396}
397
398
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399void MacroAssembler::RecordWriteField(
400 Register object,
401 int offset,
402 Register value,
403 Register dst,
404 SaveFPRegsMode save_fp,
405 RememberedSetAction remembered_set_action,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 SmiCheck smi_check,
407 PointersToHereCheck pointers_to_here_check_for_value) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100408 // First, check if a write barrier is even needed. The tests below
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100409 // catch stores of Smis.
Ben Murdoch257744e2011-11-30 15:57:28 +0000410 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000411
412 // Skip barrier if writing a smi.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100413 if (smi_check == INLINE_SMI_CHECK) {
414 JumpIfSmi(value, &done, Label::kNear);
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100416
417 // Although the object register is tagged, the offset is relative to the start
418 // of the object, so so offset must be a multiple of kPointerSize.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000419 DCHECK(IsAligned(offset, kPointerSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100420
421 lea(dst, FieldOperand(object, offset));
422 if (emit_debug_code()) {
423 Label ok;
424 test_b(dst, (1 << kPointerSizeLog2) - 1);
425 j(zero, &ok, Label::kNear);
426 int3();
427 bind(&ok);
428 }
429
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 RecordWrite(object, dst, value, save_fp, remembered_set_action,
431 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000432
433 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000434
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100435 // Clobber clobbered input registers when running with the debug-code flag
Leon Clarke4515c472010-02-03 11:58:03 +0000436 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100437 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
439 mov(dst, Immediate(bit_cast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000440 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000441}
442
443
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000444void MacroAssembler::RecordWriteForMap(
445 Register object,
446 Handle<Map> map,
447 Register scratch1,
448 Register scratch2,
449 SaveFPRegsMode save_fp) {
450 Label done;
451
452 Register address = scratch1;
453 Register value = scratch2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100454 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 Label ok;
456 lea(address, FieldOperand(object, HeapObject::kMapOffset));
457 test_b(address, (1 << kPointerSizeLog2) - 1);
458 j(zero, &ok, Label::kNear);
459 int3();
460 bind(&ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100461 }
462
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000463 DCHECK(!object.is(value));
464 DCHECK(!object.is(address));
465 DCHECK(!value.is(address));
466 AssertNotSmi(object);
467
468 if (!FLAG_incremental_marking) {
469 return;
470 }
471
472 // Compute the address.
473 lea(address, FieldOperand(object, HeapObject::kMapOffset));
474
475 // A single check of the map's pages interesting flag suffices, since it is
476 // only set during incremental collection, and then it's also guaranteed that
477 // the from object's page's interesting flag is also set. This optimization
478 // relies on the fact that maps can never be in new space.
479 DCHECK(!isolate()->heap()->InNewSpace(*map));
480 CheckPageFlagForMap(map,
481 MemoryChunk::kPointersToHereAreInterestingMask,
482 zero,
483 &done,
484 Label::kNear);
485
486 RecordWriteStub stub(isolate(), object, value, address, OMIT_REMEMBERED_SET,
487 save_fp);
488 CallStub(&stub);
489
490 bind(&done);
491
492 // Count number of write barriers in generated code.
493 isolate()->counters()->write_barriers_static()->Increment();
494 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
495
496 // Clobber clobbered input registers when running with the debug-code flag
497 // turned on to provoke errors.
498 if (emit_debug_code()) {
499 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
500 mov(scratch1, Immediate(bit_cast<int32_t>(kZapValue)));
501 mov(scratch2, Immediate(bit_cast<int32_t>(kZapValue)));
502 }
503}
504
505
506void MacroAssembler::RecordWrite(
507 Register object,
508 Register address,
509 Register value,
510 SaveFPRegsMode fp_mode,
511 RememberedSetAction remembered_set_action,
512 SmiCheck smi_check,
513 PointersToHereCheck pointers_to_here_check_for_value) {
514 DCHECK(!object.is(value));
515 DCHECK(!object.is(address));
516 DCHECK(!value.is(address));
517 AssertNotSmi(object);
518
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100519 if (remembered_set_action == OMIT_REMEMBERED_SET &&
520 !FLAG_incremental_marking) {
521 return;
522 }
523
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100525 Label ok;
526 cmp(value, Operand(address, 0));
527 j(equal, &ok, Label::kNear);
528 int3();
529 bind(&ok);
530 }
531
Steve Block8defd9f2010-07-08 12:39:36 +0100532 // First, check if a write barrier is even needed. The tests below
533 // catch stores of Smis and stores into young gen.
534 Label done;
535
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100536 if (smi_check == INLINE_SMI_CHECK) {
537 // Skip barrier if writing a smi.
538 JumpIfSmi(value, &done, Label::kNear);
539 }
Steve Block8defd9f2010-07-08 12:39:36 +0100540
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
542 CheckPageFlag(value,
543 value, // Used as scratch.
544 MemoryChunk::kPointersToHereAreInterestingMask,
545 zero,
546 &done,
547 Label::kNear);
548 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100549 CheckPageFlag(object,
550 value, // Used as scratch.
551 MemoryChunk::kPointersFromHereAreInterestingMask,
552 zero,
553 &done,
554 Label::kNear);
Steve Block8defd9f2010-07-08 12:39:36 +0100555
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
557 fp_mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100558 CallStub(&stub);
Steve Block8defd9f2010-07-08 12:39:36 +0100559
560 bind(&done);
561
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562 // Count number of write barriers in generated code.
563 isolate()->counters()->write_barriers_static()->Increment();
564 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
565
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100566 // Clobber clobbered registers when running with the debug-code flag
Steve Block8defd9f2010-07-08 12:39:36 +0100567 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100568 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569 mov(address, Immediate(bit_cast<int32_t>(kZapValue)));
570 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
Steve Block8defd9f2010-07-08 12:39:36 +0100571 }
572}
573
574
Andrei Popescu402d9372010-02-26 13:31:12 +0000575void MacroAssembler::DebugBreak() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576 Move(eax, Immediate(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100577 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 CEntryStub ces(isolate(), 1);
Andrei Popescu402d9372010-02-26 13:31:12 +0000579 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
580}
Steve Blocka7e24c12009-10-30 11:49:00 +0000581
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100582
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583void MacroAssembler::Cvtsi2sd(XMMRegister dst, const Operand& src) {
584 xorps(dst, dst);
585 cvtsi2sd(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000586}
587
588
Steve Block053d10c2011-06-13 19:13:29 +0100589bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
590 static const int kMaxImmediateBits = 17;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 if (!RelocInfo::IsNone(x.rmode_)) return false;
Steve Block053d10c2011-06-13 19:13:29 +0100592 return !is_intn(x.x_, kMaxImmediateBits);
593}
594
595
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596void MacroAssembler::SafeMove(Register dst, const Immediate& x) {
Steve Block053d10c2011-06-13 19:13:29 +0100597 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 Move(dst, Immediate(x.x_ ^ jit_cookie()));
Steve Block053d10c2011-06-13 19:13:29 +0100599 xor_(dst, jit_cookie());
600 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 Move(dst, x);
Steve Block053d10c2011-06-13 19:13:29 +0100602 }
603}
604
605
606void MacroAssembler::SafePush(const Immediate& x) {
607 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
608 push(Immediate(x.x_ ^ jit_cookie()));
609 xor_(Operand(esp, 0), Immediate(jit_cookie()));
610 } else {
611 push(x);
612 }
613}
614
615
Steve Blocka7e24c12009-10-30 11:49:00 +0000616void MacroAssembler::CmpObjectType(Register heap_object,
617 InstanceType type,
618 Register map) {
619 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
620 CmpInstanceType(map, type);
621}
622
623
624void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
625 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
626 static_cast<int8_t>(type));
627}
628
629
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000630void MacroAssembler::CheckFastElements(Register map,
631 Label* fail,
632 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000633 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
634 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
635 STATIC_ASSERT(FAST_ELEMENTS == 2);
636 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000637 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638 Map::kMaximumBitField2FastHoleyElementValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000639 j(above, fail, distance);
640}
641
642
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100643void MacroAssembler::CheckFastObjectElements(Register map,
644 Label* fail,
645 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
647 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
648 STATIC_ASSERT(FAST_ELEMENTS == 2);
649 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100650 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000651 Map::kMaximumBitField2FastHoleySmiElementValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100652 j(below_equal, fail, distance);
653 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 Map::kMaximumBitField2FastHoleyElementValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100655 j(above, fail, distance);
656}
657
658
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000659void MacroAssembler::CheckFastSmiElements(Register map,
660 Label* fail,
661 Label::Distance distance) {
662 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
663 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100664 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665 Map::kMaximumBitField2FastHoleySmiElementValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100666 j(above, fail, distance);
667}
668
669
670void MacroAssembler::StoreNumberToDoubleElements(
671 Register maybe_number,
672 Register elements,
673 Register key,
674 Register scratch1,
675 XMMRegister scratch2,
676 Label* fail,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000677 int elements_offset) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100678 Label smi_value, done, maybe_nan, not_nan, is_nan, have_double_value;
679 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
680
681 CheckMap(maybe_number,
682 isolate()->factory()->heap_number_map(),
683 fail,
684 DONT_DO_SMI_CHECK);
685
686 // Double value, canonicalize NaN.
687 uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
688 cmp(FieldOperand(maybe_number, offset),
689 Immediate(kNaNOrInfinityLowerBoundUpper32));
690 j(greater_equal, &maybe_nan, Label::kNear);
691
692 bind(&not_nan);
693 ExternalReference canonical_nan_reference =
694 ExternalReference::address_of_canonical_non_hole_nan();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000695 movsd(scratch2, FieldOperand(maybe_number, HeapNumber::kValueOffset));
696 bind(&have_double_value);
697 movsd(FieldOperand(elements, key, times_4,
698 FixedDoubleArray::kHeaderSize - elements_offset),
699 scratch2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100700 jmp(&done);
701
702 bind(&maybe_nan);
703 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
704 // it's an Infinity, and the non-NaN code path applies.
705 j(greater, &is_nan, Label::kNear);
706 cmp(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
707 j(zero, &not_nan);
708 bind(&is_nan);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 movsd(scratch2, Operand::StaticVariable(canonical_nan_reference));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100710 jmp(&have_double_value, Label::kNear);
711
712 bind(&smi_value);
713 // Value is a smi. Convert to a double and store.
714 // Preserve original value.
715 mov(scratch1, maybe_number);
716 SmiUntag(scratch1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 Cvtsi2sd(scratch2, scratch1);
718 movsd(FieldOperand(elements, key, times_4,
719 FixedDoubleArray::kHeaderSize - elements_offset),
720 scratch2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100721 bind(&done);
722}
723
724
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000725void MacroAssembler::CompareMap(Register obj, Handle<Map> map) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100726 cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100727}
728
729
Andrei Popescu31002712010-02-23 13:46:05 +0000730void MacroAssembler::CheckMap(Register obj,
731 Handle<Map> map,
732 Label* fail,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 SmiCheckType smi_check_type) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000734 if (smi_check_type == DO_SMI_CHECK) {
735 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +0000736 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100737
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 CompareMap(obj, map);
Andrei Popescu31002712010-02-23 13:46:05 +0000739 j(not_equal, fail);
740}
741
742
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400743void MacroAssembler::DispatchWeakMap(Register obj, Register scratch1,
744 Register scratch2, Handle<WeakCell> cell,
745 Handle<Code> success,
746 SmiCheckType smi_check_type) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000747 Label fail;
748 if (smi_check_type == DO_SMI_CHECK) {
749 JumpIfSmi(obj, &fail);
750 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400751 mov(scratch1, FieldOperand(obj, HeapObject::kMapOffset));
752 CmpWeakValue(scratch1, cell, scratch2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000753 j(equal, success);
754
755 bind(&fail);
756}
757
758
Leon Clarkee46be812010-01-19 14:06:41 +0000759Condition MacroAssembler::IsObjectStringType(Register heap_object,
760 Register map,
761 Register instance_type) {
762 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
763 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000764 STATIC_ASSERT(kNotStringTag != 0);
Leon Clarkee46be812010-01-19 14:06:41 +0000765 test(instance_type, Immediate(kIsNotStringMask));
766 return zero;
767}
768
769
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000770Condition MacroAssembler::IsObjectNameType(Register heap_object,
771 Register map,
772 Register instance_type) {
773 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
774 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
775 cmpb(instance_type, static_cast<uint8_t>(LAST_NAME_TYPE));
776 return below_equal;
777}
778
779
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100780void MacroAssembler::IsObjectJSObjectType(Register heap_object,
781 Register map,
782 Register scratch,
783 Label* fail) {
784 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
785 IsInstanceJSObjectType(map, scratch, fail);
786}
787
788
789void MacroAssembler::IsInstanceJSObjectType(Register map,
790 Register scratch,
791 Label* fail) {
792 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100793 sub(scratch, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000794 cmp(scratch,
795 LAST_NONCALLABLE_SPEC_OBJECT_TYPE - FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100796 j(above, fail);
797}
798
799
Steve Blocka7e24c12009-10-30 11:49:00 +0000800void MacroAssembler::FCmp() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000801 fucomip();
802 fstp(0);
803}
804
805
806void MacroAssembler::AssertNumber(Register object) {
807 if (emit_debug_code()) {
808 Label ok;
809 JumpIfSmi(object, &ok);
810 cmp(FieldOperand(object, HeapObject::kMapOffset),
811 isolate()->factory()->heap_number_map());
812 Check(equal, kOperandNotANumber);
813 bind(&ok);
Steve Block3ce2e202009-11-05 08:53:23 +0000814 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000815}
816
817
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818void MacroAssembler::AssertSmi(Register object) {
819 if (emit_debug_code()) {
820 test(object, Immediate(kSmiTagMask));
821 Check(equal, kOperandIsNotASmi);
822 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000823}
824
825
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826void MacroAssembler::AssertString(Register object) {
827 if (emit_debug_code()) {
828 test(object, Immediate(kSmiTagMask));
829 Check(not_equal, kOperandIsASmiAndNotAString);
830 push(object);
831 mov(object, FieldOperand(object, HeapObject::kMapOffset));
832 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
833 pop(object);
834 Check(below, kOperandIsNotAString);
835 }
Iain Merrick75681382010-08-19 15:07:18 +0100836}
837
838
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839void MacroAssembler::AssertName(Register object) {
840 if (emit_debug_code()) {
841 test(object, Immediate(kSmiTagMask));
842 Check(not_equal, kOperandIsASmiAndNotAName);
843 push(object);
844 mov(object, FieldOperand(object, HeapObject::kMapOffset));
845 CmpInstanceType(object, LAST_NAME_TYPE);
846 pop(object);
847 Check(below_equal, kOperandIsNotAName);
848 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100849}
850
851
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852void MacroAssembler::AssertUndefinedOrAllocationSite(Register object) {
853 if (emit_debug_code()) {
854 Label done_checking;
855 AssertNotSmi(object);
856 cmp(object, isolate()->factory()->undefined_value());
857 j(equal, &done_checking);
858 cmp(FieldOperand(object, 0),
859 Immediate(isolate()->factory()->allocation_site_map()));
860 Assert(equal, kExpectedUndefinedOrCell);
861 bind(&done_checking);
862 }
863}
864
865
866void MacroAssembler::AssertNotSmi(Register object) {
867 if (emit_debug_code()) {
868 test(object, Immediate(kSmiTagMask));
869 Check(not_equal, kOperandIsASmi);
870 }
871}
872
873
874void MacroAssembler::StubPrologue() {
875 push(ebp); // Caller's frame pointer.
876 mov(ebp, esp);
877 push(esi); // Callee's context.
878 push(Immediate(Smi::FromInt(StackFrame::STUB)));
879}
880
881
882void MacroAssembler::Prologue(bool code_pre_aging) {
883 PredictableCodeSizeScope predictible_code_size_scope(this,
884 kNoCodeAgeSequenceLength);
885 if (code_pre_aging) {
886 // Pre-age the code.
887 call(isolate()->builtins()->MarkCodeAsExecutedOnce(),
888 RelocInfo::CODE_AGE_SEQUENCE);
889 Nop(kNoCodeAgeSequenceLength - Assembler::kCallInstructionLength);
890 } else {
891 push(ebp); // Caller's frame pointer.
892 mov(ebp, esp);
893 push(esi); // Callee's context.
894 push(edi); // Callee's JS function.
895 }
Steve Block6ded16b2010-05-10 14:33:55 +0100896}
897
898
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400899void MacroAssembler::EnterFrame(StackFrame::Type type,
900 bool load_constant_pool_pointer_reg) {
901 // Out-of-line constant pool not implemented on ia32.
902 UNREACHABLE();
903}
904
905
Steve Blocka7e24c12009-10-30 11:49:00 +0000906void MacroAssembler::EnterFrame(StackFrame::Type type) {
907 push(ebp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100908 mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 push(esi);
910 push(Immediate(Smi::FromInt(type)));
911 push(Immediate(CodeObject()));
Steve Block44f0eee2011-05-26 01:26:41 +0100912 if (emit_debug_code()) {
913 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 Check(not_equal, kCodeObjectNotProperlyPatched);
Steve Blocka7e24c12009-10-30 11:49:00 +0000915 }
916}
917
918
919void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100920 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
922 Immediate(Smi::FromInt(type)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923 Check(equal, kStackFrameTypesMustMatch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 }
925 leave();
926}
927
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100928
929void MacroAssembler::EnterExitFramePrologue() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100930 // Set up the frame structure on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931 DCHECK(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
932 DCHECK(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
933 DCHECK(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000934 push(ebp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100935 mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000936
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100937 // Reserve room for entry stack pointer and push the code object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 DCHECK(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000939 push(Immediate(0)); // Saved entry sp, patched before call.
940 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000941
942 // Save the frame pointer and the context in top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress, isolate());
944 ExternalReference context_address(Isolate::kContextAddress, isolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400945 ExternalReference c_function_address(Isolate::kCFunctionAddress, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000946 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
947 mov(Operand::StaticVariable(context_address), esi);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400948 mov(Operand::StaticVariable(c_function_address), ebx);
Steve Blockd0582a62009-12-15 09:54:21 +0000949}
Steve Blocka7e24c12009-10-30 11:49:00 +0000950
Steve Blocka7e24c12009-10-30 11:49:00 +0000951
Ben Murdochb0fe1622011-05-05 13:52:32 +0100952void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
953 // Optionally save all XMM registers.
954 if (save_doubles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 int space = XMMRegister::kMaxNumRegisters * kDoubleSize +
956 argc * kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100957 sub(esp, Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100958 const int offset = -2 * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000959 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100960 XMMRegister reg = XMMRegister::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961 movsd(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100962 }
963 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100964 sub(esp, Immediate(argc * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100965 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000966
967 // Get the required frame alignment for the OS.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968 const int kFrameAlignment = base::OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000969 if (kFrameAlignment > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 DCHECK(base::bits::IsPowerOfTwo32(kFrameAlignment));
Steve Blocka7e24c12009-10-30 11:49:00 +0000971 and_(esp, -kFrameAlignment);
972 }
973
974 // Patch the saved entry sp.
975 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
976}
977
978
Ben Murdochb0fe1622011-05-05 13:52:32 +0100979void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100980 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000981
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100982 // Set up argc and argv in callee-saved registers.
Steve Blockd0582a62009-12-15 09:54:21 +0000983 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100984 mov(edi, eax);
Steve Blockd0582a62009-12-15 09:54:21 +0000985 lea(esi, Operand(ebp, eax, times_4, offset));
986
Steve Block44f0eee2011-05-26 01:26:41 +0100987 // Reserve space for argc, argv and isolate.
988 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000989}
990
991
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800992void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100993 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100994 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000995}
996
997
Ben Murdochb0fe1622011-05-05 13:52:32 +0100998void MacroAssembler::LeaveExitFrame(bool save_doubles) {
999 // Optionally restore all XMM registers.
1000 if (save_doubles) {
Steve Block1e0659c2011-05-24 12:43:12 +01001001 const int offset = -2 * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001003 XMMRegister reg = XMMRegister::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 movsd(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001005 }
1006 }
1007
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 // Get the return address from the stack and restore the frame pointer.
1009 mov(ecx, Operand(ebp, 1 * kPointerSize));
1010 mov(ebp, Operand(ebp, 0 * kPointerSize));
1011
1012 // Pop the arguments and the receiver from the caller stack.
1013 lea(esp, Operand(esi, 1 * kPointerSize));
1014
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001015 // Push the return address to get ready to return.
1016 push(ecx);
1017
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 LeaveExitFrameEpilogue(true);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001019}
1020
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001021
1022void MacroAssembler::LeaveExitFrameEpilogue(bool restore_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001023 // Restore current context from top and clear it in debug mode.
Ben Murdoch589d6972011-11-30 16:04:58 +00001024 ExternalReference context_address(Isolate::kContextAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025 if (restore_context) {
1026 mov(esi, Operand::StaticVariable(context_address));
1027 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001028#ifdef DEBUG
1029 mov(Operand::StaticVariable(context_address), Immediate(0));
1030#endif
1031
Steve Blocka7e24c12009-10-30 11:49:00 +00001032 // Clear the top frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00001033 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01001034 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001035 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
1036}
1037
1038
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001039void MacroAssembler::LeaveApiExitFrame(bool restore_context) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001040 mov(esp, ebp);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001041 pop(ebp);
1042
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043 LeaveExitFrameEpilogue(restore_context);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001044}
1045
1046
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001047void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
1048 int handler_index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001049 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001050 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1051 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001052 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1053 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1054 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1055 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
1056
1057 // We will build up the handler from the bottom by pushing on the stack.
1058 // First push the frame pointer and context.
1059 if (kind == StackHandler::JS_ENTRY) {
1060 // The frame pointer does not point to a JS frame so we save NULL for
1061 // ebp. We expect the code throwing an exception to check ebp before
1062 // dereferencing it to restore the context.
Ben Murdoch85b71792012-04-11 18:30:58 +01001063 push(Immediate(0)); // NULL frame pointer.
1064 push(Immediate(Smi::FromInt(0))); // No context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001065 } else {
1066 push(ebp);
1067 push(esi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001069 // Push the state and the code object.
1070 unsigned state =
1071 StackHandler::IndexField::encode(handler_index) |
1072 StackHandler::KindField::encode(kind);
1073 push(Immediate(state));
1074 Push(CodeObject());
1075
1076 // Link the current handler as the next handler.
1077 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
1078 push(Operand::StaticVariable(handler_address));
1079 // Set this new handler as the current one.
1080 mov(Operand::StaticVariable(handler_address), esp);
Steve Blocka7e24c12009-10-30 11:49:00 +00001081}
1082
1083
Leon Clarkee46be812010-01-19 14:06:41 +00001084void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001085 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001086 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
1087 pop(Operand::StaticVariable(handler_address));
1088 add(esp, Immediate(StackHandlerConstants::kSize - kPointerSize));
1089}
1090
1091
1092void MacroAssembler::JumpToHandlerEntry() {
1093 // Compute the handler entry address and jump to it. The handler table is
1094 // a fixed array of (smi-tagged) code offsets.
1095 // eax = exception, edi = code object, edx = state.
1096 mov(ebx, FieldOperand(edi, Code::kHandlerTableOffset));
1097 shr(edx, StackHandler::kKindWidth);
1098 mov(edx, FieldOperand(ebx, edx, times_4, FixedArray::kHeaderSize));
1099 SmiUntag(edx);
1100 lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
1101 jmp(edi);
Leon Clarkee46be812010-01-19 14:06:41 +00001102}
1103
1104
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001105void MacroAssembler::Throw(Register value) {
1106 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001107 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1108 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001109 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1110 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1111 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1112 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
1113
1114 // The exception is expected in eax.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001115 if (!value.is(eax)) {
1116 mov(eax, value);
1117 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001118 // Drop the stack pointer to the top of the top handler.
1119 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001120 mov(esp, Operand::StaticVariable(handler_address));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001121 // Restore the next handler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001122 pop(Operand::StaticVariable(handler_address));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001123
1124 // Remove the code object and state, compute the handler address in edi.
1125 pop(edi); // Code object.
1126 pop(edx); // Index and state.
1127
1128 // Restore the context and frame pointer.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001129 pop(esi); // Context.
1130 pop(ebp); // Frame pointer.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001131
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001132 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001133 // (kind == ENTRY) == (ebp == 0) == (esi == 0), so we could test either
1134 // ebp or esi.
Ben Murdoch257744e2011-11-30 15:57:28 +00001135 Label skip;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001136 test(esi, esi);
1137 j(zero, &skip, Label::kNear);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001138 mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001139 bind(&skip);
1140
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001141 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001142}
1143
1144
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001145void MacroAssembler::ThrowUncatchable(Register value) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001146 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001147 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1148 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001149 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1150 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1151 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1152 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001153
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001154 // The exception is expected in eax.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001155 if (!value.is(eax)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001156 mov(eax, value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001157 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001158 // Drop the stack pointer to the top of the top stack handler.
1159 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001160 mov(esp, Operand::StaticVariable(handler_address));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001161
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001162 // Unwind the handlers until the top ENTRY handler is found.
1163 Label fetch_next, check_kind;
1164 jmp(&check_kind, Label::kNear);
1165 bind(&fetch_next);
1166 mov(esp, Operand(esp, StackHandlerConstants::kNextOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001167
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001168 bind(&check_kind);
1169 STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
1170 test(Operand(esp, StackHandlerConstants::kStateOffset),
1171 Immediate(StackHandler::KindField::kMask));
1172 j(not_zero, &fetch_next);
1173
1174 // Set the top handler address to next handler past the top ENTRY handler.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001175 pop(Operand::StaticVariable(handler_address));
1176
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001177 // Remove the code object and state, compute the handler address in edi.
1178 pop(edi); // Code object.
1179 pop(edx); // Index and state.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001180
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001181 // Clear the context pointer and frame pointer (0 was saved in the handler).
1182 pop(esi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001183 pop(ebp);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001184
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001185 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001186}
1187
1188
Steve Blocka7e24c12009-10-30 11:49:00 +00001189void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001190 Register scratch1,
1191 Register scratch2,
Steve Blocka7e24c12009-10-30 11:49:00 +00001192 Label* miss) {
1193 Label same_contexts;
1194
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001195 DCHECK(!holder_reg.is(scratch1));
1196 DCHECK(!holder_reg.is(scratch2));
1197 DCHECK(!scratch1.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001198
1199 // Load current lexical context from the stack frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 mov(scratch1, Operand(ebp, StandardFrameConstants::kContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001201
1202 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +01001203 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001204 cmp(scratch1, Immediate(0));
1205 Check(not_equal, kWeShouldNotHaveAnEmptyLexicalContext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001207 // Load the native context of the current context.
1208 int offset =
1209 Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
1210 mov(scratch1, FieldOperand(scratch1, offset));
1211 mov(scratch1, FieldOperand(scratch1, GlobalObject::kNativeContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001212
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213 // Check the context is a native context.
Steve Block44f0eee2011-05-26 01:26:41 +01001214 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001215 // Read the first word and compare to native_context_map.
1216 cmp(FieldOperand(scratch1, HeapObject::kMapOffset),
1217 isolate()->factory()->native_context_map());
1218 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001219 }
1220
1221 // Check if both contexts are the same.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 cmp(scratch1, FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001223 j(equal, &same_contexts);
Steve Blocka7e24c12009-10-30 11:49:00 +00001224
1225 // Compare security tokens, save holder_reg on the stack so we can use it
1226 // as a temporary register.
1227 //
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 // Check that the security token in the calling global object is
1229 // compatible with the security token in the receiving global
1230 // object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001231 mov(scratch2,
1232 FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001233
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001234 // Check the context is a native context.
Steve Block44f0eee2011-05-26 01:26:41 +01001235 if (emit_debug_code()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001236 cmp(scratch2, isolate()->factory()->null_value());
1237 Check(not_equal, kJSGlobalProxyContextShouldNotBeNull);
Steve Blocka7e24c12009-10-30 11:49:00 +00001238
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239 // Read the first word and compare to native_context_map(),
1240 cmp(FieldOperand(scratch2, HeapObject::kMapOffset),
1241 isolate()->factory()->native_context_map());
1242 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001243 }
1244
1245 int token_offset = Context::kHeaderSize +
1246 Context::SECURITY_TOKEN_INDEX * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001247 mov(scratch1, FieldOperand(scratch1, token_offset));
1248 cmp(scratch1, FieldOperand(scratch2, token_offset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001249 j(not_equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001250
1251 bind(&same_contexts);
1252}
1253
1254
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255// Compute the hash code from the untagged key. This must be kept in sync with
1256// ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
1257// code-stub-hydrogen.cc
Ben Murdochc7cc0282012-03-05 14:35:55 +00001258//
1259// Note: r0 will contain hash code
1260void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
1261 // Xor original key with a seed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001262 if (serializer_enabled()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001263 ExternalReference roots_array_start =
1264 ExternalReference::roots_array_start(isolate());
Ben Murdochc7cc0282012-03-05 14:35:55 +00001265 mov(scratch, Immediate(Heap::kHashSeedRootIndex));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001266 mov(scratch,
1267 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001268 SmiUntag(scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001269 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001270 } else {
1271 int32_t seed = isolate()->heap()->HashSeed();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001272 xor_(r0, Immediate(seed));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001273 }
1274
1275 // hash = ~hash + (hash << 15);
1276 mov(scratch, r0);
1277 not_(r0);
1278 shl(scratch, 15);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001279 add(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001280 // hash = hash ^ (hash >> 12);
1281 mov(scratch, r0);
1282 shr(scratch, 12);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001283 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001284 // hash = hash + (hash << 2);
1285 lea(r0, Operand(r0, r0, times_4, 0));
1286 // hash = hash ^ (hash >> 4);
1287 mov(scratch, r0);
1288 shr(scratch, 4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001289 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001290 // hash = hash * 2057;
1291 imul(r0, r0, 2057);
1292 // hash = hash ^ (hash >> 16);
1293 mov(scratch, r0);
1294 shr(scratch, 16);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001295 xor_(r0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001296}
1297
1298
1299
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001300void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1301 Register elements,
1302 Register key,
1303 Register r0,
1304 Register r1,
1305 Register r2,
1306 Register result) {
1307 // Register use:
1308 //
1309 // elements - holds the slow-case elements of the receiver and is unchanged.
1310 //
1311 // key - holds the smi key on entry and is unchanged.
1312 //
1313 // Scratch registers:
1314 //
1315 // r0 - holds the untagged key on entry and holds the hash once computed.
1316 //
1317 // r1 - used to hold the capacity mask of the dictionary
1318 //
1319 // r2 - used for the index into the dictionary.
1320 //
1321 // result - holds the result on exit if the load succeeds and we fall through.
1322
1323 Label done;
1324
Ben Murdochc7cc0282012-03-05 14:35:55 +00001325 GetNumberHash(r0, r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001326
1327 // Compute capacity mask.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001328 mov(r1, FieldOperand(elements, SeededNumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001329 shr(r1, kSmiTagSize); // convert smi to int
1330 dec(r1);
1331
1332 // Generate an unrolled loop that performs a few probes before giving up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 for (int i = 0; i < kNumberDictionaryProbes; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001334 // Use r2 for index calculations and keep the hash intact in r0.
1335 mov(r2, r0);
1336 // Compute the masked index: (hash + i + i * i) & mask.
1337 if (i > 0) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001338 add(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001339 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001340 and_(r2, r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001341
1342 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001343 DCHECK(SeededNumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001344 lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
1345
1346 // Check if the key matches.
1347 cmp(key, FieldOperand(elements,
1348 r2,
1349 times_pointer_size,
Ben Murdochc7cc0282012-03-05 14:35:55 +00001350 SeededNumberDictionary::kElementsStartOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351 if (i != (kNumberDictionaryProbes - 1)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001352 j(equal, &done);
1353 } else {
1354 j(not_equal, miss);
1355 }
1356 }
1357
1358 bind(&done);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001359 // Check that the value is a field property.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001360 const int kDetailsOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001361 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001362 DCHECK_EQ(FIELD, 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001363 test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
Ben Murdoch589d6972011-11-30 16:04:58 +00001364 Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001365 j(not_zero, miss);
1366
1367 // Get the value at the masked, scaled index.
1368 const int kValueOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001369 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001370 mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
1371}
1372
1373
Steve Blocka7e24c12009-10-30 11:49:00 +00001374void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +00001375 Register scratch,
1376 AllocationFlags flags) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001377 ExternalReference allocation_top =
1378 AllocationUtils::GetAllocationTopReference(isolate(), flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001379
1380 // Just return if allocation top is already known.
1381 if ((flags & RESULT_CONTAINS_TOP) != 0) {
1382 // No use of scratch if allocation top is provided.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001383 DCHECK(scratch.is(no_reg));
Steve Blocka7e24c12009-10-30 11:49:00 +00001384#ifdef DEBUG
1385 // Assert that result actually contains top on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001386 cmp(result, Operand::StaticVariable(allocation_top));
1387 Check(equal, kUnexpectedAllocationTop);
Steve Blocka7e24c12009-10-30 11:49:00 +00001388#endif
1389 return;
1390 }
1391
1392 // Move address of new object to result. Use scratch register if available.
1393 if (scratch.is(no_reg)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 mov(result, Operand::StaticVariable(allocation_top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001395 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001396 mov(scratch, Immediate(allocation_top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001397 mov(result, Operand(scratch, 0));
1398 }
1399}
1400
1401
1402void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403 Register scratch,
1404 AllocationFlags flags) {
Steve Block44f0eee2011-05-26 01:26:41 +01001405 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001406 test(result_end, Immediate(kObjectAlignmentMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407 Check(zero, kUnalignedAllocationInNewSpace);
Steve Blockd0582a62009-12-15 09:54:21 +00001408 }
1409
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001410 ExternalReference allocation_top =
1411 AllocationUtils::GetAllocationTopReference(isolate(), flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001412
1413 // Update new top. Use scratch if available.
1414 if (scratch.is(no_reg)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415 mov(Operand::StaticVariable(allocation_top), result_end);
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 } else {
1417 mov(Operand(scratch, 0), result_end);
1418 }
1419}
1420
1421
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001422void MacroAssembler::Allocate(int object_size,
1423 Register result,
1424 Register result_end,
1425 Register scratch,
1426 Label* gc_required,
1427 AllocationFlags flags) {
1428 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
1429 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
John Reck59135872010-11-02 12:39:01 -07001430 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001431 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001432 // Trash the registers to simulate an allocation failure.
1433 mov(result, Immediate(0x7091));
1434 if (result_end.is_valid()) {
1435 mov(result_end, Immediate(0x7191));
1436 }
1437 if (scratch.is_valid()) {
1438 mov(scratch, Immediate(0x7291));
1439 }
1440 }
1441 jmp(gc_required);
1442 return;
1443 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001444 DCHECK(!result.is(result_end));
Steve Blocka7e24c12009-10-30 11:49:00 +00001445
1446 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001447 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001448
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001449 ExternalReference allocation_limit =
1450 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1451
1452 // Align the next allocation. Storing the filler map without checking top is
1453 // safe in new-space because the limit of the heap is aligned there.
1454 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1455 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1456 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1457 Label aligned;
1458 test(result, Immediate(kDoubleAlignmentMask));
1459 j(zero, &aligned, Label::kNear);
1460 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
1461 cmp(result, Operand::StaticVariable(allocation_limit));
1462 j(above_equal, gc_required);
1463 }
1464 mov(Operand(result, 0),
1465 Immediate(isolate()->factory()->one_pointer_filler_map()));
1466 add(result, Immediate(kDoubleSize / 2));
1467 bind(&aligned);
1468 }
1469
1470 // Calculate new top and bail out if space is exhausted.
Ben Murdochbb769b22010-08-11 14:56:33 +01001471 Register top_reg = result_end.is_valid() ? result_end : result;
Steve Block1e0659c2011-05-24 12:43:12 +01001472 if (!top_reg.is(result)) {
1473 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +01001474 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001475 add(top_reg, Immediate(object_size));
Ben Murdoch257744e2011-11-30 15:57:28 +00001476 j(carry, gc_required);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001477 cmp(top_reg, Operand::StaticVariable(allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001478 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001479
Leon Clarkee46be812010-01-19 14:06:41 +00001480 // Update allocation top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001481 UpdateAllocationTopHelper(top_reg, scratch, flags);
Ben Murdochbb769b22010-08-11 14:56:33 +01001482
1483 // Tag result if requested.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484 bool tag_result = (flags & TAG_OBJECT) != 0;
Ben Murdochbb769b22010-08-11 14:56:33 +01001485 if (top_reg.is(result)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486 if (tag_result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001487 sub(result, Immediate(object_size - kHeapObjectTag));
Ben Murdochbb769b22010-08-11 14:56:33 +01001488 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001489 sub(result, Immediate(object_size));
Ben Murdochbb769b22010-08-11 14:56:33 +01001490 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001491 } else if (tag_result) {
1492 DCHECK(kHeapObjectTag == 1);
1493 inc(result);
Ben Murdochbb769b22010-08-11 14:56:33 +01001494 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001495}
1496
1497
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001498void MacroAssembler::Allocate(int header_size,
1499 ScaleFactor element_size,
1500 Register element_count,
1501 RegisterValueType element_count_type,
1502 Register result,
1503 Register result_end,
1504 Register scratch,
1505 Label* gc_required,
1506 AllocationFlags flags) {
1507 DCHECK((flags & SIZE_IN_WORDS) == 0);
John Reck59135872010-11-02 12:39:01 -07001508 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001509 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001510 // Trash the registers to simulate an allocation failure.
1511 mov(result, Immediate(0x7091));
1512 mov(result_end, Immediate(0x7191));
1513 if (scratch.is_valid()) {
1514 mov(scratch, Immediate(0x7291));
1515 }
1516 // Register element_count is not modified by the function.
1517 }
1518 jmp(gc_required);
1519 return;
1520 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001521 DCHECK(!result.is(result_end));
Steve Blocka7e24c12009-10-30 11:49:00 +00001522
1523 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001524 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001525
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001526 ExternalReference allocation_limit =
1527 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
Steve Block1e0659c2011-05-24 12:43:12 +01001528
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001529 // Align the next allocation. Storing the filler map without checking top is
1530 // safe in new-space because the limit of the heap is aligned there.
1531 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1532 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1533 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1534 Label aligned;
1535 test(result, Immediate(kDoubleAlignmentMask));
1536 j(zero, &aligned, Label::kNear);
1537 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
1538 cmp(result, Operand::StaticVariable(allocation_limit));
1539 j(above_equal, gc_required);
1540 }
1541 mov(Operand(result, 0),
1542 Immediate(isolate()->factory()->one_pointer_filler_map()));
1543 add(result, Immediate(kDoubleSize / 2));
1544 bind(&aligned);
1545 }
1546
1547 // Calculate new top and bail out if space is exhausted.
Steve Block1e0659c2011-05-24 12:43:12 +01001548 // We assume that element_count*element_size + header_size does not
1549 // overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001550 if (element_count_type == REGISTER_VALUE_IS_SMI) {
1551 STATIC_ASSERT(static_cast<ScaleFactor>(times_2 - 1) == times_1);
1552 STATIC_ASSERT(static_cast<ScaleFactor>(times_4 - 1) == times_2);
1553 STATIC_ASSERT(static_cast<ScaleFactor>(times_8 - 1) == times_4);
1554 DCHECK(element_size >= times_2);
1555 DCHECK(kSmiTagSize == 1);
1556 element_size = static_cast<ScaleFactor>(element_size - 1);
1557 } else {
1558 DCHECK(element_count_type == REGISTER_VALUE_IS_INT32);
1559 }
Steve Block1e0659c2011-05-24 12:43:12 +01001560 lea(result_end, Operand(element_count, element_size, header_size));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001561 add(result_end, result);
Steve Block1e0659c2011-05-24 12:43:12 +01001562 j(carry, gc_required);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001563 cmp(result_end, Operand::StaticVariable(allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +00001564 j(above, gc_required);
1565
Steve Blocka7e24c12009-10-30 11:49:00 +00001566 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001567 DCHECK(kHeapObjectTag == 1);
1568 inc(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001569 }
Leon Clarkee46be812010-01-19 14:06:41 +00001570
1571 // Update allocation top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001572 UpdateAllocationTopHelper(result_end, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001573}
1574
1575
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001576void MacroAssembler::Allocate(Register object_size,
1577 Register result,
1578 Register result_end,
1579 Register scratch,
1580 Label* gc_required,
1581 AllocationFlags flags) {
1582 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
John Reck59135872010-11-02 12:39:01 -07001583 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001584 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001585 // Trash the registers to simulate an allocation failure.
1586 mov(result, Immediate(0x7091));
1587 mov(result_end, Immediate(0x7191));
1588 if (scratch.is_valid()) {
1589 mov(scratch, Immediate(0x7291));
1590 }
1591 // object_size is left unchanged by this function.
1592 }
1593 jmp(gc_required);
1594 return;
1595 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001596 DCHECK(!result.is(result_end));
Steve Blocka7e24c12009-10-30 11:49:00 +00001597
1598 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001599 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001600
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001601 ExternalReference allocation_limit =
1602 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1603
1604 // Align the next allocation. Storing the filler map without checking top is
1605 // safe in new-space because the limit of the heap is aligned there.
1606 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1607 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1608 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1609 Label aligned;
1610 test(result, Immediate(kDoubleAlignmentMask));
1611 j(zero, &aligned, Label::kNear);
1612 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
1613 cmp(result, Operand::StaticVariable(allocation_limit));
1614 j(above_equal, gc_required);
1615 }
1616 mov(Operand(result, 0),
1617 Immediate(isolate()->factory()->one_pointer_filler_map()));
1618 add(result, Immediate(kDoubleSize / 2));
1619 bind(&aligned);
1620 }
1621
1622 // Calculate new top and bail out if space is exhausted.
Steve Blocka7e24c12009-10-30 11:49:00 +00001623 if (!object_size.is(result_end)) {
1624 mov(result_end, object_size);
1625 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001626 add(result_end, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001627 j(carry, gc_required);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001628 cmp(result_end, Operand::StaticVariable(allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001629 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001630
Steve Blocka7e24c12009-10-30 11:49:00 +00001631 // Tag result if requested.
1632 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001633 DCHECK(kHeapObjectTag == 1);
1634 inc(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001635 }
Leon Clarkee46be812010-01-19 14:06:41 +00001636
1637 // Update allocation top.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001638 UpdateAllocationTopHelper(result_end, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00001639}
1640
1641
1642void MacroAssembler::UndoAllocationInNewSpace(Register object) {
1643 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001644 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001645
1646 // Make sure the object has no tag before resetting top.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001647 and_(object, Immediate(~kHeapObjectTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +00001648#ifdef DEBUG
1649 cmp(object, Operand::StaticVariable(new_space_allocation_top));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001650 Check(below, kUndoAllocationOfNonAllocatedMemory);
Steve Blocka7e24c12009-10-30 11:49:00 +00001651#endif
1652 mov(Operand::StaticVariable(new_space_allocation_top), object);
1653}
1654
1655
Steve Block3ce2e202009-11-05 08:53:23 +00001656void MacroAssembler::AllocateHeapNumber(Register result,
1657 Register scratch1,
1658 Register scratch2,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 Label* gc_required,
1660 MutableMode mode) {
Steve Block3ce2e202009-11-05 08:53:23 +00001661 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001662 Allocate(HeapNumber::kSize, result, scratch1, scratch2, gc_required,
1663 TAG_OBJECT);
1664
1665 Handle<Map> map = mode == MUTABLE
1666 ? isolate()->factory()->mutable_heap_number_map()
1667 : isolate()->factory()->heap_number_map();
Steve Block3ce2e202009-11-05 08:53:23 +00001668
1669 // Set the map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001670 mov(FieldOperand(result, HeapObject::kMapOffset), Immediate(map));
Steve Block3ce2e202009-11-05 08:53:23 +00001671}
1672
1673
Steve Blockd0582a62009-12-15 09:54:21 +00001674void MacroAssembler::AllocateTwoByteString(Register result,
1675 Register length,
1676 Register scratch1,
1677 Register scratch2,
1678 Register scratch3,
1679 Label* gc_required) {
1680 // Calculate the number of bytes needed for the characters in the string while
1681 // observing object alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 DCHECK((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1683 DCHECK(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +00001684 // scratch1 = length * 2 + kObjectAlignmentMask.
1685 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001686 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001687
1688 // Allocate two byte string in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001689 Allocate(SeqTwoByteString::kHeaderSize,
1690 times_1,
1691 scratch1,
1692 REGISTER_VALUE_IS_INT32,
1693 result,
1694 scratch2,
1695 scratch3,
1696 gc_required,
1697 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001698
1699 // Set the map, length and hash field.
1700 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001701 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001702 mov(scratch1, length);
1703 SmiTag(scratch1);
1704 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001705 mov(FieldOperand(result, String::kHashFieldOffset),
1706 Immediate(String::kEmptyHashField));
1707}
1708
1709
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001710void MacroAssembler::AllocateOneByteString(Register result, Register length,
1711 Register scratch1, Register scratch2,
1712 Register scratch3,
1713 Label* gc_required) {
Steve Blockd0582a62009-12-15 09:54:21 +00001714 // Calculate the number of bytes needed for the characters in the string while
1715 // observing object alignment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001716 DCHECK((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +00001717 mov(scratch1, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001718 DCHECK(kCharSize == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001719 add(scratch1, Immediate(kObjectAlignmentMask));
1720 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001721
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001722 // Allocate one-byte string in new space.
1723 Allocate(SeqOneByteString::kHeaderSize,
1724 times_1,
1725 scratch1,
1726 REGISTER_VALUE_IS_INT32,
1727 result,
1728 scratch2,
1729 scratch3,
1730 gc_required,
1731 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001732
1733 // Set the map, length and hash field.
1734 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001735 Immediate(isolate()->factory()->one_byte_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001736 mov(scratch1, length);
1737 SmiTag(scratch1);
1738 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001739 mov(FieldOperand(result, String::kHashFieldOffset),
1740 Immediate(String::kEmptyHashField));
1741}
1742
1743
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001744void MacroAssembler::AllocateOneByteString(Register result, int length,
1745 Register scratch1, Register scratch2,
1746 Label* gc_required) {
1747 DCHECK(length > 0);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001748
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001749 // Allocate one-byte string in new space.
1750 Allocate(SeqOneByteString::SizeFor(length), result, scratch1, scratch2,
1751 gc_required, TAG_OBJECT);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001752
1753 // Set the map, length and hash field.
1754 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001755 Immediate(isolate()->factory()->one_byte_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001756 mov(FieldOperand(result, String::kLengthOffset),
1757 Immediate(Smi::FromInt(length)));
1758 mov(FieldOperand(result, String::kHashFieldOffset),
1759 Immediate(String::kEmptyHashField));
1760}
1761
1762
Ben Murdoch589d6972011-11-30 16:04:58 +00001763void MacroAssembler::AllocateTwoByteConsString(Register result,
Steve Blockd0582a62009-12-15 09:54:21 +00001764 Register scratch1,
1765 Register scratch2,
1766 Label* gc_required) {
1767 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001768 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
1769 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001770
1771 // Set the map. The other fields are left uninitialized.
1772 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001773 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001774}
1775
1776
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001777void MacroAssembler::AllocateOneByteConsString(Register result,
1778 Register scratch1,
1779 Register scratch2,
1780 Label* gc_required) {
1781 Allocate(ConsString::kSize,
1782 result,
1783 scratch1,
1784 scratch2,
1785 gc_required,
1786 TAG_OBJECT);
Steve Blockd0582a62009-12-15 09:54:21 +00001787
1788 // Set the map. The other fields are left uninitialized.
1789 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001790 Immediate(isolate()->factory()->cons_one_byte_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001791}
1792
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001793
Ben Murdoch589d6972011-11-30 16:04:58 +00001794void MacroAssembler::AllocateTwoByteSlicedString(Register result,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001795 Register scratch1,
1796 Register scratch2,
1797 Label* gc_required) {
1798 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001799 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1800 TAG_OBJECT);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001801
1802 // Set the map. The other fields are left uninitialized.
1803 mov(FieldOperand(result, HeapObject::kMapOffset),
1804 Immediate(isolate()->factory()->sliced_string_map()));
1805}
1806
1807
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001808void MacroAssembler::AllocateOneByteSlicedString(Register result,
1809 Register scratch1,
1810 Register scratch2,
1811 Label* gc_required) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001812 // Allocate heap number in new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001813 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1814 TAG_OBJECT);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001815
1816 // Set the map. The other fields are left uninitialized.
1817 mov(FieldOperand(result, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001818 Immediate(isolate()->factory()->sliced_one_byte_string_map()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001819}
1820
1821
Ben Murdochb8e0da22011-05-16 14:20:40 +01001822// Copy memory, byte-by-byte, from source to destination. Not optimized for
1823// long or aligned copies. The contents of scratch and length are destroyed.
1824// Source and destination are incremented by length.
1825// Many variants of movsb, loop unrolling, word moves, and indexed operands
1826// have been tried here already, and this is fastest.
1827// A simpler loop is faster on small copies, but 30% slower on large ones.
1828// The cld() instruction must have been emitted, to set the direction flag(),
1829// before calling this function.
1830void MacroAssembler::CopyBytes(Register source,
1831 Register destination,
1832 Register length,
1833 Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001834 Label short_loop, len4, len8, len12, done, short_string;
1835 DCHECK(source.is(esi));
1836 DCHECK(destination.is(edi));
1837 DCHECK(length.is(ecx));
1838 cmp(length, Immediate(4));
1839 j(below, &short_string, Label::kNear);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001840
1841 // Because source is 4-byte aligned in our uses of this function,
1842 // we keep source aligned for the rep_movs call by copying the odd bytes
1843 // at the end of the ranges.
1844 mov(scratch, Operand(source, length, times_1, -4));
1845 mov(Operand(destination, length, times_1, -4), scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846
1847 cmp(length, Immediate(8));
1848 j(below_equal, &len4, Label::kNear);
1849 cmp(length, Immediate(12));
1850 j(below_equal, &len8, Label::kNear);
1851 cmp(length, Immediate(16));
1852 j(below_equal, &len12, Label::kNear);
1853
Ben Murdochb8e0da22011-05-16 14:20:40 +01001854 mov(scratch, ecx);
1855 shr(ecx, 2);
1856 rep_movs();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001857 and_(scratch, Immediate(0x3));
1858 add(destination, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001859 jmp(&done, Label::kNear);
1860
1861 bind(&len12);
1862 mov(scratch, Operand(source, 8));
1863 mov(Operand(destination, 8), scratch);
1864 bind(&len8);
1865 mov(scratch, Operand(source, 4));
1866 mov(Operand(destination, 4), scratch);
1867 bind(&len4);
1868 mov(scratch, Operand(source, 0));
1869 mov(Operand(destination, 0), scratch);
1870 add(destination, length);
1871 jmp(&done, Label::kNear);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001872
1873 bind(&short_string);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001874 test(length, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001875 j(zero, &done, Label::kNear);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001876
1877 bind(&short_loop);
1878 mov_b(scratch, Operand(source, 0));
1879 mov_b(Operand(destination, 0), scratch);
1880 inc(source);
1881 inc(destination);
1882 dec(length);
1883 j(not_zero, &short_loop);
1884
1885 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001886}
1887
Steve Blockd0582a62009-12-15 09:54:21 +00001888
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001889void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
1890 Register end_offset,
1891 Register filler) {
1892 Label loop, entry;
1893 jmp(&entry);
1894 bind(&loop);
1895 mov(Operand(start_offset, 0), filler);
1896 add(start_offset, Immediate(kPointerSize));
1897 bind(&entry);
1898 cmp(start_offset, end_offset);
1899 j(less, &loop);
1900}
1901
1902
1903void MacroAssembler::BooleanBitTest(Register object,
1904 int field_offset,
1905 int bit_index) {
1906 bit_index += kSmiTagSize + kSmiShiftSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001907 DCHECK(base::bits::IsPowerOfTwo32(kBitsPerByte));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001908 int byte_index = bit_index / kBitsPerByte;
1909 int byte_bit_index = bit_index & (kBitsPerByte - 1);
1910 test_b(FieldOperand(object, field_offset + byte_index),
1911 static_cast<byte>(1 << byte_bit_index));
1912}
1913
1914
1915
Steve Blocka7e24c12009-10-30 11:49:00 +00001916void MacroAssembler::NegativeZeroTest(Register result,
1917 Register op,
1918 Label* then_label) {
1919 Label ok;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001920 test(result, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001921 j(not_zero, &ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001922 test(op, op);
Ben Murdoch257744e2011-11-30 15:57:28 +00001923 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001924 bind(&ok);
1925}
1926
1927
1928void MacroAssembler::NegativeZeroTest(Register result,
1929 Register op1,
1930 Register op2,
1931 Register scratch,
1932 Label* then_label) {
1933 Label ok;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001934 test(result, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001935 j(not_zero, &ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001936 mov(scratch, op1);
1937 or_(scratch, op2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001938 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001939 bind(&ok);
1940}
1941
1942
1943void MacroAssembler::TryGetFunctionPrototype(Register function,
1944 Register result,
1945 Register scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001946 Label* miss,
1947 bool miss_on_bound_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001948 Label non_instance;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001949 if (miss_on_bound_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001950 // Check that the receiver isn't a smi.
1951 JumpIfSmi(function, miss);
1952
1953 // Check that the function really is a function.
1954 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1955 j(not_equal, miss);
1956
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001957 // If a bound function, go to miss label.
1958 mov(scratch,
1959 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1960 BooleanBitTest(scratch, SharedFunctionInfo::kCompilerHintsOffset,
1961 SharedFunctionInfo::kBoundFunction);
1962 j(not_zero, miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001963
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001964 // Make sure that the function has an instance prototype.
1965 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1966 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1967 j(not_zero, &non_instance);
1968 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001969
1970 // Get the prototype or initial map from the function.
1971 mov(result,
1972 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1973
1974 // If the prototype or initial map is the hole, don't return it and
1975 // simply miss the cache instead. This will allow us to allocate a
1976 // prototype object on-demand in the runtime system.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001977 cmp(result, Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001978 j(equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001979
1980 // If the function does not have an initial map, we're done.
1981 Label done;
1982 CmpObjectType(result, MAP_TYPE, scratch);
1983 j(not_equal, &done);
1984
1985 // Get the prototype from the initial map.
1986 mov(result, FieldOperand(result, Map::kPrototypeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001987
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001988 if (miss_on_bound_function) {
1989 jmp(&done);
1990
1991 // Non-instance prototype: Fetch prototype from constructor field
1992 // in initial map.
1993 bind(&non_instance);
1994 mov(result, FieldOperand(result, Map::kConstructorOffset));
1995 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001996
1997 // All done.
1998 bind(&done);
1999}
2000
2001
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002002void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
2003 DCHECK(AllowThisStubCall(stub)); // Calls are not allowed in some stubs.
Ben Murdoch257744e2011-11-30 15:57:28 +00002004 call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
Steve Blocka7e24c12009-10-30 11:49:00 +00002005}
2006
2007
Steve Blockd0582a62009-12-15 09:54:21 +00002008void MacroAssembler::TailCallStub(CodeStub* stub) {
Steve Blockd0582a62009-12-15 09:54:21 +00002009 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
2010}
2011
2012
Steve Blocka7e24c12009-10-30 11:49:00 +00002013void MacroAssembler::StubReturn(int argc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002014 DCHECK(argc >= 1 && generating_stub());
Steve Blocka7e24c12009-10-30 11:49:00 +00002015 ret((argc - 1) * kPointerSize);
2016}
2017
2018
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002019bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002020 return has_frame_ || !stub->SometimesSetsUpAFrame();
Steve Blocka7e24c12009-10-30 11:49:00 +00002021}
2022
2023
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002024void MacroAssembler::IndexFromHash(Register hash, Register index) {
2025 // The assert checks that the constants for the maximum number of digits
2026 // for an array index cached in the hash field and the number of bits
2027 // reserved for it does not conflict.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002028 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002029 (1 << String::kArrayIndexValueBits));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002030 if (!index.is(hash)) {
2031 mov(index, hash);
2032 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002033 DecodeFieldToSmi<String::ArrayIndexValueBits>(index);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002034}
2035
2036
Steve Block44f0eee2011-05-26 01:26:41 +01002037void MacroAssembler::CallRuntime(const Runtime::Function* f,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002038 int num_arguments,
2039 SaveFPRegsMode save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002040 // If the expected number of arguments of the runtime function is
2041 // constant, we check that the actual number of arguments match the
2042 // expectation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002043 CHECK(f->nargs < 0 || f->nargs == num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00002044
Leon Clarke4515c472010-02-03 11:58:03 +00002045 // TODO(1236192): Most runtime routines don't need the number of
2046 // arguments passed in because it is constant. At some point we
2047 // should remove this need and make the runtime routine entry code
2048 // smarter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002049 Move(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01002050 mov(ebx, Immediate(ExternalReference(f, isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002051 CEntryStub ces(isolate(), 1, save_doubles);
Leon Clarke4515c472010-02-03 11:58:03 +00002052 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00002053}
2054
2055
Ben Murdochbb769b22010-08-11 14:56:33 +01002056void MacroAssembler::CallExternalReference(ExternalReference ref,
2057 int num_arguments) {
2058 mov(eax, Immediate(num_arguments));
2059 mov(ebx, Immediate(ref));
2060
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002061 CEntryStub stub(isolate(), 1);
Ben Murdochbb769b22010-08-11 14:56:33 +01002062 CallStub(&stub);
2063}
2064
2065
Steve Block6ded16b2010-05-10 14:33:55 +01002066void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2067 int num_arguments,
2068 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002069 // TODO(1236192): Most runtime routines don't need the number of
2070 // arguments passed in because it is constant. At some point we
2071 // should remove this need and make the runtime routine entry code
2072 // smarter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002073 Move(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002074 JumpToExternalReference(ext);
2075}
2076
2077
2078void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2079 int num_arguments,
2080 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002081 TailCallExternalReference(ExternalReference(fid, isolate()),
2082 num_arguments,
2083 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002084}
2085
2086
John Reck59135872010-11-02 12:39:01 -07002087Operand ApiParameterOperand(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002088 return Operand(esp, index * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07002089}
2090
2091
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002092void MacroAssembler::PrepareCallApiFunction(int argc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093 EnterApiExitFrame(argc);
2094 if (emit_debug_code()) {
2095 mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002096 }
2097}
2098
2099
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002100void MacroAssembler::CallApiFunctionAndReturn(
2101 Register function_address,
2102 ExternalReference thunk_ref,
2103 Operand thunk_last_arg,
2104 int stack_space,
2105 Operand return_value_operand,
2106 Operand* context_restore_operand) {
Steve Blockd0582a62009-12-15 09:54:21 +00002107 ExternalReference next_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002108 ExternalReference::handle_scope_next_address(isolate());
Steve Blockd0582a62009-12-15 09:54:21 +00002109 ExternalReference limit_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110 ExternalReference::handle_scope_limit_address(isolate());
John Reck59135872010-11-02 12:39:01 -07002111 ExternalReference level_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002112 ExternalReference::handle_scope_level_address(isolate());
Steve Blockd0582a62009-12-15 09:54:21 +00002113
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002114 DCHECK(edx.is(function_address));
John Reck59135872010-11-02 12:39:01 -07002115 // Allocate HandleScope in callee-save registers.
2116 mov(ebx, Operand::StaticVariable(next_address));
2117 mov(edi, Operand::StaticVariable(limit_address));
2118 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00002119
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002120 if (FLAG_log_timer_events) {
2121 FrameScope frame(this, StackFrame::MANUAL);
2122 PushSafepointRegisters();
2123 PrepareCallCFunction(1, eax);
2124 mov(Operand(esp, 0),
2125 Immediate(ExternalReference::isolate_address(isolate())));
2126 CallCFunction(ExternalReference::log_enter_external_function(isolate()), 1);
2127 PopSafepointRegisters();
Leon Clarkee46be812010-01-19 14:06:41 +00002128 }
Steve Blockd0582a62009-12-15 09:54:21 +00002129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002130
2131 Label profiler_disabled;
2132 Label end_profiler_check;
2133 mov(eax, Immediate(ExternalReference::is_profiling_address(isolate())));
2134 cmpb(Operand(eax, 0), 0);
2135 j(zero, &profiler_disabled);
2136
2137 // Additional parameter is the address of the actual getter function.
2138 mov(thunk_last_arg, function_address);
2139 // Call the api function.
2140 mov(eax, Immediate(thunk_ref));
2141 call(eax);
2142 jmp(&end_profiler_check);
2143
2144 bind(&profiler_disabled);
2145 // Call the api function.
2146 call(function_address);
2147 bind(&end_profiler_check);
2148
2149 if (FLAG_log_timer_events) {
2150 FrameScope frame(this, StackFrame::MANUAL);
2151 PushSafepointRegisters();
2152 PrepareCallCFunction(1, eax);
2153 mov(Operand(esp, 0),
2154 Immediate(ExternalReference::isolate_address(isolate())));
2155 CallCFunction(ExternalReference::log_leave_external_function(isolate()), 1);
2156 PopSafepointRegisters();
2157 }
2158
John Reck59135872010-11-02 12:39:01 -07002159 Label prologue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002160 // Load the value from ReturnValue
2161 mov(eax, return_value_operand);
2162
John Reck59135872010-11-02 12:39:01 -07002163 Label promote_scheduled_exception;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002164 Label exception_handled;
John Reck59135872010-11-02 12:39:01 -07002165 Label delete_allocated_handles;
2166 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00002167
John Reck59135872010-11-02 12:39:01 -07002168 bind(&prologue);
2169 // No more valid handles (the result handle was the last one). Restore
2170 // previous handle scope.
2171 mov(Operand::StaticVariable(next_address), ebx);
2172 sub(Operand::StaticVariable(level_address), Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002173 Assert(above_equal, kInvalidHandleScopeLevel);
John Reck59135872010-11-02 12:39:01 -07002174 cmp(edi, Operand::StaticVariable(limit_address));
Ben Murdoch257744e2011-11-30 15:57:28 +00002175 j(not_equal, &delete_allocated_handles);
John Reck59135872010-11-02 12:39:01 -07002176 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00002177
John Reck59135872010-11-02 12:39:01 -07002178 // Check if the function scheduled an exception.
2179 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01002180 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07002181 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01002182 Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00002183 j(not_equal, &promote_scheduled_exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002184 bind(&exception_handled);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002185
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002186#if ENABLE_EXTRA_CHECKS
2187 // Check if the function returned a valid JavaScript value.
2188 Label ok;
2189 Register return_value = eax;
2190 Register map = ecx;
2191
2192 JumpIfSmi(return_value, &ok, Label::kNear);
2193 mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
2194
2195 CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2196 j(below, &ok, Label::kNear);
2197
2198 CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2199 j(above_equal, &ok, Label::kNear);
2200
2201 cmp(map, isolate()->factory()->heap_number_map());
2202 j(equal, &ok, Label::kNear);
2203
2204 cmp(return_value, isolate()->factory()->undefined_value());
2205 j(equal, &ok, Label::kNear);
2206
2207 cmp(return_value, isolate()->factory()->true_value());
2208 j(equal, &ok, Label::kNear);
2209
2210 cmp(return_value, isolate()->factory()->false_value());
2211 j(equal, &ok, Label::kNear);
2212
2213 cmp(return_value, isolate()->factory()->null_value());
2214 j(equal, &ok, Label::kNear);
2215
2216 Abort(kAPICallReturnedInvalidObject);
2217
2218 bind(&ok);
2219#endif
2220
2221 bool restore_context = context_restore_operand != NULL;
2222 if (restore_context) {
2223 mov(esi, *context_restore_operand);
2224 }
2225 LeaveApiExitFrame(!restore_context);
2226 ret(stack_space * kPointerSize);
2227
2228 bind(&promote_scheduled_exception);
2229 {
2230 FrameScope frame(this, StackFrame::INTERNAL);
2231 CallRuntime(Runtime::kPromoteScheduledException, 0);
2232 }
2233 jmp(&exception_handled);
Leon Clarkee46be812010-01-19 14:06:41 +00002234
John Reck59135872010-11-02 12:39:01 -07002235 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01002236 ExternalReference delete_extensions =
2237 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07002238 bind(&delete_allocated_handles);
2239 mov(Operand::StaticVariable(limit_address), edi);
2240 mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002241 mov(Operand(esp, 0),
2242 Immediate(ExternalReference::isolate_address(isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +01002243 mov(eax, Immediate(delete_extensions));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002244 call(eax);
John Reck59135872010-11-02 12:39:01 -07002245 mov(eax, edi);
2246 jmp(&leave_exit_frame);
Steve Blockd0582a62009-12-15 09:54:21 +00002247}
2248
2249
Steve Block6ded16b2010-05-10 14:33:55 +01002250void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002251 // Set the entry point and jump to the C entry runtime stub.
2252 mov(ebx, Immediate(ext));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002253 CEntryStub ces(isolate(), 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002254 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
2255}
2256
2257
2258void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2259 const ParameterCount& actual,
2260 Handle<Code> code_constant,
2261 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +00002262 Label* done,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002263 bool* definitely_mismatches,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002264 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002265 Label::Distance done_near,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002266 const CallWrapper& call_wrapper) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002267 bool definitely_matches = false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002268 *definitely_mismatches = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002269 Label invoke;
2270 if (expected.is_immediate()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002271 DCHECK(actual.is_immediate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002272 if (expected.immediate() == actual.immediate()) {
2273 definitely_matches = true;
2274 } else {
2275 mov(eax, actual.immediate());
2276 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
2277 if (expected.immediate() == sentinel) {
2278 // Don't worry about adapting arguments for builtins that
2279 // don't want that done. Skip adaption code by making it look
2280 // like we have a match between expected and actual number of
2281 // arguments.
2282 definitely_matches = true;
2283 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002284 *definitely_mismatches = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002285 mov(ebx, expected.immediate());
2286 }
2287 }
2288 } else {
2289 if (actual.is_immediate()) {
2290 // Expected is in register, actual is immediate. This is the
2291 // case when we invoke function values without going through the
2292 // IC mechanism.
2293 cmp(expected.reg(), actual.immediate());
2294 j(equal, &invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002295 DCHECK(expected.reg().is(ebx));
Steve Blocka7e24c12009-10-30 11:49:00 +00002296 mov(eax, actual.immediate());
2297 } else if (!expected.reg().is(actual.reg())) {
2298 // Both expected and actual are in (different) registers. This
2299 // is the case when we invoke functions using call and apply.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002300 cmp(expected.reg(), actual.reg());
Steve Blocka7e24c12009-10-30 11:49:00 +00002301 j(equal, &invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002302 DCHECK(actual.reg().is(eax));
2303 DCHECK(expected.reg().is(ebx));
Steve Blocka7e24c12009-10-30 11:49:00 +00002304 }
2305 }
2306
2307 if (!definitely_matches) {
2308 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01002309 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00002310 if (!code_constant.is_null()) {
2311 mov(edx, Immediate(code_constant));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002312 add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00002313 } else if (!code_operand.is_reg(edx)) {
2314 mov(edx, code_operand);
2315 }
2316
2317 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002318 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
Steve Blocka7e24c12009-10-30 11:49:00 +00002319 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdoch257744e2011-11-30 15:57:28 +00002320 call_wrapper.AfterCall();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002321 if (!*definitely_mismatches) {
2322 jmp(done, done_near);
2323 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002324 } else {
2325 jmp(adaptor, RelocInfo::CODE_TARGET);
2326 }
2327 bind(&invoke);
2328 }
2329}
2330
2331
2332void MacroAssembler::InvokeCode(const Operand& code,
2333 const ParameterCount& expected,
2334 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002335 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002336 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002337 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002338 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002339
Ben Murdoch257744e2011-11-30 15:57:28 +00002340 Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002341 bool definitely_mismatches = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002342 InvokePrologue(expected, actual, Handle<Code>::null(), code,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002343 &done, &definitely_mismatches, flag, Label::kNear,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002344 call_wrapper);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002345 if (!definitely_mismatches) {
2346 if (flag == CALL_FUNCTION) {
2347 call_wrapper.BeforeCall(CallSize(code));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002348 call(code);
2349 call_wrapper.AfterCall();
2350 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002351 DCHECK(flag == JUMP_FUNCTION);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002352 jmp(code);
2353 }
2354 bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +00002355 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002356}
2357
2358
Steve Blocka7e24c12009-10-30 11:49:00 +00002359void MacroAssembler::InvokeFunction(Register fun,
2360 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002361 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002362 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002363 // You can't call a function without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002364 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002365
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002366 DCHECK(fun.is(edi));
Steve Blocka7e24c12009-10-30 11:49:00 +00002367 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2368 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2369 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002370 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00002371
2372 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01002373 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002374 expected, actual, flag, call_wrapper);
2375}
2376
2377
2378void MacroAssembler::InvokeFunction(Register fun,
2379 const ParameterCount& expected,
2380 const ParameterCount& actual,
2381 InvokeFlag flag,
2382 const CallWrapper& call_wrapper) {
2383 // You can't call a function without a valid frame.
2384 DCHECK(flag == JUMP_FUNCTION || has_frame());
2385
2386 DCHECK(fun.is(edi));
2387 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2388
2389 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2390 expected, actual, flag, call_wrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +00002391}
2392
2393
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002394void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002395 const ParameterCount& expected,
Andrei Popescu402d9372010-02-26 13:31:12 +00002396 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002397 InvokeFlag flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002398 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002399 LoadHeapObject(edi, function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002400 InvokeFunction(edi, expected, actual, flag, call_wrapper);
Andrei Popescu402d9372010-02-26 13:31:12 +00002401}
2402
2403
Ben Murdochb0fe1622011-05-05 13:52:32 +01002404void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
2405 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00002406 const CallWrapper& call_wrapper) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002407 // You can't call a builtin without a valid frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002408 DCHECK(flag == JUMP_FUNCTION || has_frame());
Steve Blocka7e24c12009-10-30 11:49:00 +00002409
2410 // Rely on the assertion to check that the number of provided
2411 // arguments match the expected number of arguments. Fake a
2412 // parameter count to avoid emitting code to do the check.
2413 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01002414 GetBuiltinFunction(edi, id);
2415 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002416 expected, expected, flag, call_wrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +00002417}
2418
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002419
Steve Block791712a2010-08-27 10:21:07 +01002420void MacroAssembler::GetBuiltinFunction(Register target,
2421 Builtins::JavaScript id) {
2422 // Load the JavaScript builtin function from the builtins object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002423 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
Steve Block791712a2010-08-27 10:21:07 +01002424 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
2425 mov(target, FieldOperand(target,
2426 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2427}
Steve Blocka7e24c12009-10-30 11:49:00 +00002428
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002429
Steve Blocka7e24c12009-10-30 11:49:00 +00002430void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002431 DCHECK(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00002432 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01002433 GetBuiltinFunction(edi, id);
2434 // Load the code entry point from the function into the target register.
2435 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002436}
2437
2438
Steve Blockd0582a62009-12-15 09:54:21 +00002439void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2440 if (context_chain_length > 0) {
2441 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002442 mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002443 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002444 mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002445 }
Steve Block1e0659c2011-05-24 12:43:12 +01002446 } else {
2447 // Slot is in the current function context. Move it into the
2448 // destination register in case we store into it (the write barrier
2449 // cannot be allowed to destroy the context in esi).
2450 mov(dst, esi);
2451 }
2452
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002453 // We should not have found a with context by walking the context chain
Steve Block1e0659c2011-05-24 12:43:12 +01002454 // (i.e., the static scope chain and runtime context chain do not agree).
2455 // A variable occurring in such a scope should have slot type LOOKUP and
2456 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01002457 if (emit_debug_code()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002458 cmp(FieldOperand(dst, HeapObject::kMapOffset),
2459 isolate()->factory()->with_context_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002460 Check(not_equal, kVariableResolvedToWithContext);
Steve Blockd0582a62009-12-15 09:54:21 +00002461 }
2462}
2463
2464
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002465void MacroAssembler::LoadTransitionedArrayMapConditional(
2466 ElementsKind expected_kind,
2467 ElementsKind transitioned_kind,
2468 Register map_in_out,
2469 Register scratch,
2470 Label* no_map_match) {
2471 // Load the global or builtins object from the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002472 mov(scratch, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2473 mov(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002474
2475 // Check that the function's map is the same as the expected cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002476 mov(scratch, Operand(scratch,
2477 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
2478
2479 size_t offset = expected_kind * kPointerSize +
2480 FixedArrayBase::kHeaderSize;
2481 cmp(map_in_out, FieldOperand(scratch, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002482 j(not_equal, no_map_match);
2483
2484 // Use the transitioned cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002485 offset = transitioned_kind * kPointerSize +
2486 FixedArrayBase::kHeaderSize;
2487 mov(map_in_out, FieldOperand(scratch, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002488}
2489
2490
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002491void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2492 // Load the global or builtins object from the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002493 mov(function,
2494 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2495 // Load the native context from the global or builtins object.
2496 mov(function,
2497 FieldOperand(function, GlobalObject::kNativeContextOffset));
2498 // Load the function from the native context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002499 mov(function, Operand(function, Context::SlotOffset(index)));
2500}
2501
2502
2503void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2504 Register map) {
2505 // Load the initial map. The global functions all have initial maps.
2506 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002507 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002508 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00002509 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002510 jmp(&ok);
2511 bind(&fail);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002512 Abort(kGlobalFunctionsMustHaveInitialMap);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002513 bind(&ok);
2514 }
2515}
2516
Steve Blockd0582a62009-12-15 09:54:21 +00002517
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002518// Store the value in register src in the safepoint register stack
2519// slot for register dst.
2520void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
2521 mov(SafepointRegisterSlot(dst), src);
2522}
2523
2524
2525void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
2526 mov(SafepointRegisterSlot(dst), src);
2527}
2528
2529
2530void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
2531 mov(dst, SafepointRegisterSlot(src));
2532}
2533
2534
2535Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
2536 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
2537}
2538
2539
Ben Murdochb0fe1622011-05-05 13:52:32 +01002540int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2541 // The registers are pushed starting with the lowest encoding,
2542 // which means that lowest encodings are furthest away from
2543 // the stack pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002544 DCHECK(reg_code >= 0 && reg_code < kNumSafepointRegisters);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002545 return kNumSafepointRegisters - reg_code - 1;
2546}
2547
2548
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002549void MacroAssembler::LoadHeapObject(Register result,
2550 Handle<HeapObject> object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002551 AllowDeferredHandleDereference embedding_raw_address;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002552 if (isolate()->heap()->InNewSpace(*object)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2554 mov(result, Operand::ForCell(cell));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002555 } else {
2556 mov(result, object);
2557 }
2558}
2559
2560
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002561void MacroAssembler::CmpHeapObject(Register reg, Handle<HeapObject> object) {
2562 AllowDeferredHandleDereference using_raw_address;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002563 if (isolate()->heap()->InNewSpace(*object)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2565 cmp(reg, Operand::ForCell(cell));
2566 } else {
2567 cmp(reg, object);
2568 }
2569}
2570
2571
2572void MacroAssembler::PushHeapObject(Handle<HeapObject> object) {
2573 AllowDeferredHandleDereference using_raw_address;
2574 if (isolate()->heap()->InNewSpace(*object)) {
2575 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2576 push(Operand::ForCell(cell));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002577 } else {
2578 Push(object);
2579 }
2580}
2581
2582
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002583void MacroAssembler::CmpWeakValue(Register value, Handle<WeakCell> cell,
2584 Register scratch) {
2585 mov(scratch, cell);
2586 cmp(value, FieldOperand(scratch, WeakCell::kValueOffset));
2587}
2588
2589
2590void MacroAssembler::LoadWeakValue(Register value, Handle<WeakCell> cell,
2591 Label* miss) {
2592 mov(value, cell);
2593 mov(value, FieldOperand(value, WeakCell::kValueOffset));
2594 JumpIfSmi(value, miss);
2595}
2596
2597
Steve Blocka7e24c12009-10-30 11:49:00 +00002598void MacroAssembler::Ret() {
2599 ret(0);
2600}
2601
2602
Steve Block1e0659c2011-05-24 12:43:12 +01002603void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
2604 if (is_uint16(bytes_dropped)) {
2605 ret(bytes_dropped);
2606 } else {
2607 pop(scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002608 add(esp, Immediate(bytes_dropped));
Steve Block1e0659c2011-05-24 12:43:12 +01002609 push(scratch);
2610 ret(0);
2611 }
2612}
2613
2614
Leon Clarkee46be812010-01-19 14:06:41 +00002615void MacroAssembler::Drop(int stack_elements) {
2616 if (stack_elements > 0) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002617 add(esp, Immediate(stack_elements * kPointerSize));
Leon Clarkee46be812010-01-19 14:06:41 +00002618 }
2619}
2620
2621
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002622void MacroAssembler::Move(Register dst, Register src) {
2623 if (!dst.is(src)) {
2624 mov(dst, src);
2625 }
2626}
2627
2628
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002629void MacroAssembler::Move(Register dst, const Immediate& x) {
2630 if (x.is_zero()) {
2631 xor_(dst, dst); // Shorter than mov of 32-bit immediate 0.
2632 } else {
2633 mov(dst, x);
2634 }
2635}
2636
2637
2638void MacroAssembler::Move(const Operand& dst, const Immediate& x) {
2639 mov(dst, x);
2640}
2641
2642
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002643void MacroAssembler::Move(XMMRegister dst, uint32_t src) {
2644 if (src == 0) {
2645 pxor(dst, dst);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002646 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002647 unsigned cnt = base::bits::CountPopulation32(src);
2648 unsigned nlz = base::bits::CountLeadingZeros32(src);
2649 unsigned ntz = base::bits::CountTrailingZeros32(src);
2650 if (nlz + cnt + ntz == 32) {
2651 pcmpeqd(dst, dst);
2652 if (ntz == 0) {
2653 psrld(dst, 32 - cnt);
2654 } else {
2655 pslld(dst, 32 - cnt);
2656 if (nlz != 0) psrld(dst, nlz);
2657 }
2658 } else {
2659 push(eax);
2660 mov(eax, Immediate(src));
2661 movd(dst, Operand(eax));
2662 pop(eax);
2663 }
2664 }
2665}
2666
2667
2668void MacroAssembler::Move(XMMRegister dst, uint64_t src) {
2669 uint32_t lower = static_cast<uint32_t>(src);
2670 uint32_t upper = static_cast<uint32_t>(src >> 32);
2671 if (upper == 0) {
2672 Move(dst, lower);
2673 } else {
2674 unsigned cnt = base::bits::CountPopulation64(src);
2675 unsigned nlz = base::bits::CountLeadingZeros64(src);
2676 unsigned ntz = base::bits::CountTrailingZeros64(src);
2677 if (nlz + cnt + ntz == 64) {
2678 pcmpeqd(dst, dst);
2679 if (ntz == 0) {
2680 psrlq(dst, 64 - cnt);
2681 } else {
2682 psllq(dst, 64 - cnt);
2683 if (nlz != 0) psrlq(dst, nlz);
2684 }
2685 } else if (lower == 0) {
2686 Move(dst, upper);
2687 psllq(dst, 32);
2688 } else if (CpuFeatures::IsSupported(SSE4_1)) {
2689 CpuFeatureScope scope(this, SSE4_1);
2690 push(eax);
2691 Move(eax, Immediate(lower));
2692 movd(dst, Operand(eax));
2693 Move(eax, Immediate(upper));
2694 pinsrd(dst, Operand(eax), 1);
2695 pop(eax);
2696 } else {
2697 push(Immediate(upper));
2698 push(Immediate(lower));
2699 movsd(dst, Operand(esp, 0));
2700 add(esp, Immediate(kDoubleSize));
2701 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002702 }
2703}
2704
2705
Steve Blocka7e24c12009-10-30 11:49:00 +00002706void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
2707 if (FLAG_native_code_counters && counter->Enabled()) {
2708 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
2709 }
2710}
2711
2712
2713void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714 DCHECK(value > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002715 if (FLAG_native_code_counters && counter->Enabled()) {
2716 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2717 if (value == 1) {
2718 inc(operand);
2719 } else {
2720 add(operand, Immediate(value));
2721 }
2722 }
2723}
2724
2725
2726void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002727 DCHECK(value > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002728 if (FLAG_native_code_counters && counter->Enabled()) {
2729 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2730 if (value == 1) {
2731 dec(operand);
2732 } else {
2733 sub(operand, Immediate(value));
2734 }
2735 }
2736}
2737
2738
Leon Clarked91b9f72010-01-27 17:25:45 +00002739void MacroAssembler::IncrementCounter(Condition cc,
2740 StatsCounter* counter,
2741 int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002742 DCHECK(value > 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002743 if (FLAG_native_code_counters && counter->Enabled()) {
2744 Label skip;
2745 j(NegateCondition(cc), &skip);
2746 pushfd();
2747 IncrementCounter(counter, value);
2748 popfd();
2749 bind(&skip);
2750 }
2751}
2752
2753
2754void MacroAssembler::DecrementCounter(Condition cc,
2755 StatsCounter* counter,
2756 int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002757 DCHECK(value > 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002758 if (FLAG_native_code_counters && counter->Enabled()) {
2759 Label skip;
2760 j(NegateCondition(cc), &skip);
2761 pushfd();
2762 DecrementCounter(counter, value);
2763 popfd();
2764 bind(&skip);
2765 }
2766}
2767
2768
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002769void MacroAssembler::Assert(Condition cc, BailoutReason reason) {
2770 if (emit_debug_code()) Check(cc, reason);
Steve Blocka7e24c12009-10-30 11:49:00 +00002771}
2772
2773
Iain Merrick75681382010-08-19 15:07:18 +01002774void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002775 if (emit_debug_code()) {
2776 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01002777 Label ok;
2778 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01002779 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01002780 j(equal, &ok);
2781 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002782 Immediate(factory->fixed_double_array_map()));
2783 j(equal, &ok);
2784 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01002785 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01002786 j(equal, &ok);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002787 Abort(kJSObjectWithFastElementsMapHasSlowElements);
Iain Merrick75681382010-08-19 15:07:18 +01002788 bind(&ok);
2789 }
2790}
2791
2792
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002793void MacroAssembler::Check(Condition cc, BailoutReason reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002794 Label L;
Ben Murdoch257744e2011-11-30 15:57:28 +00002795 j(cc, &L);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002796 Abort(reason);
Steve Blocka7e24c12009-10-30 11:49:00 +00002797 // will not return here
2798 bind(&L);
2799}
2800
2801
Steve Block6ded16b2010-05-10 14:33:55 +01002802void MacroAssembler::CheckStackAlignment() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002803 int frame_alignment = base::OS::ActivationFrameAlignment();
Steve Block6ded16b2010-05-10 14:33:55 +01002804 int frame_alignment_mask = frame_alignment - 1;
2805 if (frame_alignment > kPointerSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002806 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Steve Block6ded16b2010-05-10 14:33:55 +01002807 Label alignment_as_expected;
2808 test(esp, Immediate(frame_alignment_mask));
2809 j(zero, &alignment_as_expected);
2810 // Abort if stack is not aligned.
2811 int3();
2812 bind(&alignment_as_expected);
2813 }
2814}
2815
2816
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002817void MacroAssembler::Abort(BailoutReason reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002818#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002819 const char* msg = GetBailoutReason(reason);
Steve Blocka7e24c12009-10-30 11:49:00 +00002820 if (msg != NULL) {
2821 RecordComment("Abort message: ");
2822 RecordComment(msg);
2823 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002824
2825 if (FLAG_trap_on_abort) {
2826 int3();
2827 return;
2828 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002829#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002830
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002831 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(reason))));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002832 // Disable stub call restrictions to always allow calls to abort.
2833 if (!has_frame_) {
2834 // We don't actually want to generate a pile of code for this, so just
2835 // claim there is a stack frame, without generating one.
2836 FrameScope scope(this, StackFrame::NONE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002837 CallRuntime(Runtime::kAbort, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002838 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002839 CallRuntime(Runtime::kAbort, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002840 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002841 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00002842 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00002843}
2844
2845
Ben Murdoch257744e2011-11-30 15:57:28 +00002846void MacroAssembler::LoadInstanceDescriptors(Register map,
2847 Register descriptors) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002848 mov(descriptors, FieldOperand(map, Map::kDescriptorsOffset));
2849}
2850
2851
2852void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
2853 mov(dst, FieldOperand(map, Map::kBitField3Offset));
2854 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
Iain Merrick75681382010-08-19 15:07:18 +01002855}
2856
2857
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002858void MacroAssembler::LoadPowerOf2(XMMRegister dst,
2859 Register scratch,
2860 int power) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002861 DCHECK(is_uintn(power + HeapNumber::kExponentBias,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002862 HeapNumber::kExponentBits));
2863 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002864 movd(dst, scratch);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002865 psllq(dst, HeapNumber::kMantissaBits);
2866}
2867
2868
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002869void MacroAssembler::LookupNumberStringCache(Register object,
2870 Register result,
2871 Register scratch1,
2872 Register scratch2,
2873 Label* not_found) {
2874 // Use of registers. Register result is used as a temporary.
2875 Register number_string_cache = result;
2876 Register mask = scratch1;
2877 Register scratch = scratch2;
2878
2879 // Load the number string cache.
2880 LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
2881 // Make the hash mask from the length of the number string cache. It
2882 // contains two elements (number and string) for each cache entry.
2883 mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2884 shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
2885 sub(mask, Immediate(1)); // Make mask.
2886
2887 // Calculate the entry in the number string cache. The hash value in the
2888 // number string cache for smis is just the smi value, and the hash for
2889 // doubles is the xor of the upper and lower words. See
2890 // Heap::GetNumberStringCache.
2891 Label smi_hash_calculated;
2892 Label load_result_from_cache;
2893 Label not_smi;
2894 STATIC_ASSERT(kSmiTag == 0);
2895 JumpIfNotSmi(object, &not_smi, Label::kNear);
2896 mov(scratch, object);
2897 SmiUntag(scratch);
2898 jmp(&smi_hash_calculated, Label::kNear);
2899 bind(&not_smi);
2900 cmp(FieldOperand(object, HeapObject::kMapOffset),
2901 isolate()->factory()->heap_number_map());
2902 j(not_equal, not_found);
2903 STATIC_ASSERT(8 == kDoubleSize);
2904 mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2905 xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2906 // Object is heap number and hash is now in scratch. Calculate cache index.
2907 and_(scratch, mask);
2908 Register index = scratch;
2909 Register probe = mask;
2910 mov(probe,
2911 FieldOperand(number_string_cache,
2912 index,
2913 times_twice_pointer_size,
2914 FixedArray::kHeaderSize));
2915 JumpIfSmi(probe, not_found);
2916 movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2917 ucomisd(xmm0, FieldOperand(probe, HeapNumber::kValueOffset));
2918 j(parity_even, not_found); // Bail out if NaN is involved.
2919 j(not_equal, not_found); // The cache did not contain this value.
2920 jmp(&load_result_from_cache, Label::kNear);
2921
2922 bind(&smi_hash_calculated);
2923 // Object is smi and hash is now in scratch. Calculate cache index.
2924 and_(scratch, mask);
2925 // Check if the entry is the smi we are looking for.
2926 cmp(object,
2927 FieldOperand(number_string_cache,
2928 index,
2929 times_twice_pointer_size,
2930 FixedArray::kHeaderSize));
2931 j(not_equal, not_found);
2932
2933 // Get the result from the cache.
2934 bind(&load_result_from_cache);
2935 mov(result,
2936 FieldOperand(number_string_cache,
2937 index,
2938 times_twice_pointer_size,
2939 FixedArray::kHeaderSize + kPointerSize));
2940 IncrementCounter(isolate()->counters()->number_to_string_native(), 1);
2941}
2942
2943
2944void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(
2945 Register instance_type, Register scratch, Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002946 if (!scratch.is(instance_type)) {
2947 mov(scratch, instance_type);
2948 }
2949 and_(scratch,
2950 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002951 cmp(scratch, kStringTag | kSeqStringTag | kOneByteStringTag);
Andrei Popescu402d9372010-02-26 13:31:12 +00002952 j(not_equal, failure);
2953}
2954
2955
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002956void MacroAssembler::JumpIfNotBothSequentialOneByteStrings(Register object1,
2957 Register object2,
2958 Register scratch1,
2959 Register scratch2,
2960 Label* failure) {
Leon Clarked91b9f72010-01-27 17:25:45 +00002961 // Check that both objects are not smis.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002962 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002963 mov(scratch1, object1);
2964 and_(scratch1, object2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002965 JumpIfSmi(scratch1, failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002966
2967 // Load instance type for both strings.
2968 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2969 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2970 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2971 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2972
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002973 // Check that both are flat one-byte strings.
2974 const int kFlatOneByteStringMask =
Leon Clarked91b9f72010-01-27 17:25:45 +00002975 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002976 const int kFlatOneByteStringTag =
2977 kStringTag | kOneByteStringTag | kSeqStringTag;
Leon Clarked91b9f72010-01-27 17:25:45 +00002978 // Interleave bits from both instance types and compare them in one check.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002979 DCHECK_EQ(0, kFlatOneByteStringMask & (kFlatOneByteStringMask << 3));
2980 and_(scratch1, kFlatOneByteStringMask);
2981 and_(scratch2, kFlatOneByteStringMask);
Leon Clarked91b9f72010-01-27 17:25:45 +00002982 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002983 cmp(scratch1, kFlatOneByteStringTag | (kFlatOneByteStringTag << 3));
Leon Clarked91b9f72010-01-27 17:25:45 +00002984 j(not_equal, failure);
2985}
2986
2987
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002988void MacroAssembler::JumpIfNotUniqueNameInstanceType(Operand operand,
2989 Label* not_unique_name,
2990 Label::Distance distance) {
2991 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2992 Label succeed;
2993 test(operand, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2994 j(zero, &succeed);
2995 cmpb(operand, static_cast<uint8_t>(SYMBOL_TYPE));
2996 j(not_equal, not_unique_name, distance);
2997
2998 bind(&succeed);
2999}
3000
3001
3002void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
3003 Register index,
3004 Register value,
3005 uint32_t encoding_mask) {
3006 Label is_object;
3007 JumpIfNotSmi(string, &is_object, Label::kNear);
3008 Abort(kNonObject);
3009 bind(&is_object);
3010
3011 push(value);
3012 mov(value, FieldOperand(string, HeapObject::kMapOffset));
3013 movzx_b(value, FieldOperand(value, Map::kInstanceTypeOffset));
3014
3015 and_(value, Immediate(kStringRepresentationMask | kStringEncodingMask));
3016 cmp(value, Immediate(encoding_mask));
3017 pop(value);
3018 Check(equal, kUnexpectedStringType);
3019
3020 // The index is assumed to be untagged coming in, tag it to compare with the
3021 // string length without using a temp register, it is restored at the end of
3022 // this function.
3023 SmiTag(index);
3024 Check(no_overflow, kIndexIsTooLarge);
3025
3026 cmp(index, FieldOperand(string, String::kLengthOffset));
3027 Check(less, kIndexIsTooLarge);
3028
3029 cmp(index, Immediate(Smi::FromInt(0)));
3030 Check(greater_equal, kIndexIsNegative);
3031
3032 // Restore the index
3033 SmiUntag(index);
3034}
3035
3036
Steve Block6ded16b2010-05-10 14:33:55 +01003037void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003038 int frame_alignment = base::OS::ActivationFrameAlignment();
Ben Murdoch8b112d22011-06-08 16:22:53 +01003039 if (frame_alignment != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01003040 // Make stack end at alignment and make room for num_arguments words
3041 // and the original value of esp.
3042 mov(scratch, esp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003043 sub(esp, Immediate((num_arguments + 1) * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003044 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Ben Murdoch8b112d22011-06-08 16:22:53 +01003045 and_(esp, -frame_alignment);
Steve Block6ded16b2010-05-10 14:33:55 +01003046 mov(Operand(esp, num_arguments * kPointerSize), scratch);
3047 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003048 sub(esp, Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01003049 }
3050}
3051
3052
3053void MacroAssembler::CallCFunction(ExternalReference function,
3054 int num_arguments) {
3055 // Trashing eax is ok as it will be the return value.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003056 mov(eax, Immediate(function));
Steve Block6ded16b2010-05-10 14:33:55 +01003057 CallCFunction(eax, num_arguments);
3058}
3059
3060
3061void MacroAssembler::CallCFunction(Register function,
3062 int num_arguments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003063 DCHECK(has_frame());
Steve Block6ded16b2010-05-10 14:33:55 +01003064 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01003065 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01003066 CheckStackAlignment();
3067 }
3068
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003069 call(function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003070 if (base::OS::ActivationFrameAlignment() != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01003071 mov(esp, Operand(esp, num_arguments * kPointerSize));
3072 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003073 add(esp, Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01003074 }
3075}
3076
3077
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003078#ifdef DEBUG
3079bool AreAliased(Register reg1,
3080 Register reg2,
3081 Register reg3,
3082 Register reg4,
3083 Register reg5,
3084 Register reg6,
3085 Register reg7,
3086 Register reg8) {
3087 int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
3088 reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
3089 reg7.is_valid() + reg8.is_valid();
3090
3091 RegList regs = 0;
3092 if (reg1.is_valid()) regs |= reg1.bit();
3093 if (reg2.is_valid()) regs |= reg2.bit();
3094 if (reg3.is_valid()) regs |= reg3.bit();
3095 if (reg4.is_valid()) regs |= reg4.bit();
3096 if (reg5.is_valid()) regs |= reg5.bit();
3097 if (reg6.is_valid()) regs |= reg6.bit();
3098 if (reg7.is_valid()) regs |= reg7.bit();
3099 if (reg8.is_valid()) regs |= reg8.bit();
3100 int n_of_non_aliasing_regs = NumRegs(regs);
3101
3102 return n_of_valid_regs != n_of_non_aliasing_regs;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003103}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003104#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003105
3106
Steve Blocka7e24c12009-10-30 11:49:00 +00003107CodePatcher::CodePatcher(byte* address, int size)
Ben Murdoch8b112d22011-06-08 16:22:53 +01003108 : address_(address),
3109 size_(size),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003110 masm_(NULL, address, size + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003111 // Create a new macro assembler pointing to the address of the code to patch.
3112 // The size is adjusted with kGap on order for the assembler to generate size
3113 // bytes of instructions without failing with buffer size constraints.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003114 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003115}
3116
3117
3118CodePatcher::~CodePatcher() {
3119 // Indicate that code has changed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003120 CpuFeatures::FlushICache(address_, size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003121
3122 // Check that the code was patched as expected.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003123 DCHECK(masm_.pc_ == address_ + size_);
3124 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003125}
3126
3127
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003128void MacroAssembler::CheckPageFlag(
3129 Register object,
3130 Register scratch,
3131 int mask,
3132 Condition cc,
3133 Label* condition_met,
3134 Label::Distance condition_met_distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003135 DCHECK(cc == zero || cc == not_zero);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003136 if (scratch.is(object)) {
3137 and_(scratch, Immediate(~Page::kPageAlignmentMask));
3138 } else {
3139 mov(scratch, Immediate(~Page::kPageAlignmentMask));
3140 and_(scratch, object);
3141 }
3142 if (mask < (1 << kBitsPerByte)) {
3143 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
3144 static_cast<uint8_t>(mask));
3145 } else {
3146 test(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
3147 }
3148 j(cc, condition_met, condition_met_distance);
3149}
3150
3151
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003152void MacroAssembler::CheckPageFlagForMap(
3153 Handle<Map> map,
3154 int mask,
3155 Condition cc,
3156 Label* condition_met,
3157 Label::Distance condition_met_distance) {
3158 DCHECK(cc == zero || cc == not_zero);
3159 Page* page = Page::FromAddress(map->address());
3160 DCHECK(!serializer_enabled()); // Serializer cannot match page_flags.
3161 ExternalReference reference(ExternalReference::page_flags(page));
3162 // The inlined static address check of the page's flags relies
3163 // on maps never being compacted.
3164 DCHECK(!isolate()->heap()->mark_compact_collector()->
3165 IsOnEvacuationCandidate(*map));
3166 if (mask < (1 << kBitsPerByte)) {
3167 test_b(Operand::StaticVariable(reference), static_cast<uint8_t>(mask));
3168 } else {
3169 test(Operand::StaticVariable(reference), Immediate(mask));
3170 }
3171 j(cc, condition_met, condition_met_distance);
3172}
3173
3174
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003175void MacroAssembler::JumpIfBlack(Register object,
3176 Register scratch0,
3177 Register scratch1,
3178 Label* on_black,
3179 Label::Distance on_black_near) {
3180 HasColor(object, scratch0, scratch1,
3181 on_black, on_black_near,
3182 1, 0); // kBlackBitPattern.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003183 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003184}
3185
3186
3187void MacroAssembler::HasColor(Register object,
3188 Register bitmap_scratch,
3189 Register mask_scratch,
3190 Label* has_color,
3191 Label::Distance has_color_distance,
3192 int first_bit,
3193 int second_bit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003194 DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, ecx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003195
3196 GetMarkBits(object, bitmap_scratch, mask_scratch);
3197
3198 Label other_color, word_boundary;
3199 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3200 j(first_bit == 1 ? zero : not_zero, &other_color, Label::kNear);
3201 add(mask_scratch, mask_scratch); // Shift left 1 by adding.
3202 j(zero, &word_boundary, Label::kNear);
3203 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3204 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3205 jmp(&other_color, Label::kNear);
3206
3207 bind(&word_boundary);
3208 test_b(Operand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize), 1);
3209
3210 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3211 bind(&other_color);
3212}
3213
3214
3215void MacroAssembler::GetMarkBits(Register addr_reg,
3216 Register bitmap_reg,
3217 Register mask_reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003218 DCHECK(!AreAliased(addr_reg, mask_reg, bitmap_reg, ecx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003219 mov(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
3220 and_(bitmap_reg, addr_reg);
3221 mov(ecx, addr_reg);
3222 int shift =
3223 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
3224 shr(ecx, shift);
3225 and_(ecx,
3226 (Page::kPageAlignmentMask >> shift) & ~(Bitmap::kBytesPerCell - 1));
3227
3228 add(bitmap_reg, ecx);
3229 mov(ecx, addr_reg);
3230 shr(ecx, kPointerSizeLog2);
3231 and_(ecx, (1 << Bitmap::kBitsPerCellLog2) - 1);
3232 mov(mask_reg, Immediate(1));
3233 shl_cl(mask_reg);
3234}
3235
3236
3237void MacroAssembler::EnsureNotWhite(
3238 Register value,
3239 Register bitmap_scratch,
3240 Register mask_scratch,
3241 Label* value_is_white_and_not_data,
3242 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003243 DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, ecx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003244 GetMarkBits(value, bitmap_scratch, mask_scratch);
3245
3246 // If the value is black or grey we don't need to do anything.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003247 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
3248 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
3249 DCHECK(strcmp(Marking::kGreyBitPattern, "11") == 0);
3250 DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003251
3252 Label done;
3253
3254 // Since both black and grey have a 1 in the first position and white does
3255 // not have a 1 there we only need to check one bit.
3256 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3257 j(not_zero, &done, Label::kNear);
3258
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003259 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003260 // Check for impossible bit pattern.
3261 Label ok;
3262 push(mask_scratch);
3263 // shl. May overflow making the check conservative.
3264 add(mask_scratch, mask_scratch);
3265 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3266 j(zero, &ok, Label::kNear);
3267 int3();
3268 bind(&ok);
3269 pop(mask_scratch);
3270 }
3271
3272 // Value is white. We check whether it is data that doesn't need scanning.
3273 // Currently only checks for HeapNumber and non-cons strings.
3274 Register map = ecx; // Holds map while checking type.
3275 Register length = ecx; // Holds length of object after checking type.
3276 Label not_heap_number;
3277 Label is_data_object;
3278
3279 // Check for heap-number
3280 mov(map, FieldOperand(value, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003281 cmp(map, isolate()->factory()->heap_number_map());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003282 j(not_equal, &not_heap_number, Label::kNear);
3283 mov(length, Immediate(HeapNumber::kSize));
3284 jmp(&is_data_object, Label::kNear);
3285
3286 bind(&not_heap_number);
3287 // Check for strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003288 DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3289 DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003290 // If it's a string and it's not a cons string then it's an object containing
3291 // no GC pointers.
3292 Register instance_type = ecx;
3293 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3294 test_b(instance_type, kIsIndirectStringMask | kIsNotStringMask);
3295 j(not_zero, value_is_white_and_not_data);
3296 // It's a non-indirect (non-cons and non-slice) string.
3297 // If it's external, the length is just ExternalString::kSize.
3298 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
3299 Label not_external;
3300 // External strings are the only ones with the kExternalStringTag bit
3301 // set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003302 DCHECK_EQ(0, kSeqStringTag & kExternalStringTag);
3303 DCHECK_EQ(0, kConsStringTag & kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003304 test_b(instance_type, kExternalStringTag);
3305 j(zero, &not_external, Label::kNear);
3306 mov(length, Immediate(ExternalString::kSize));
3307 jmp(&is_data_object, Label::kNear);
3308
3309 bind(&not_external);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003310 // Sequential string, either Latin1 or UC16.
3311 DCHECK(kOneByteStringTag == 0x04);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003312 and_(length, Immediate(kStringEncodingMask));
3313 xor_(length, Immediate(kStringEncodingMask));
3314 add(length, Immediate(0x04));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003315 // Value now either 4 (if Latin1) or 8 (if UC16), i.e., char-size shifted
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003316 // by 2. If we multiply the string length as smi by this, it still
3317 // won't overflow a 32-bit value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003318 DCHECK_EQ(SeqOneByteString::kMaxSize, SeqTwoByteString::kMaxSize);
3319 DCHECK(SeqOneByteString::kMaxSize <=
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003320 static_cast<int>(0xffffffffu >> (2 + kSmiTagSize)));
3321 imul(length, FieldOperand(value, String::kLengthOffset));
3322 shr(length, 2 + kSmiTagSize + kSmiShiftSize);
3323 add(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
3324 and_(length, Immediate(~kObjectAlignmentMask));
3325
3326 bind(&is_data_object);
3327 // Value is a data object, and it is white. Mark it black. Since we know
3328 // that the object is white we can make it black by flipping one bit.
3329 or_(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
3330
3331 and_(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
3332 add(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset),
3333 length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003334 if (emit_debug_code()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003335 mov(length, Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3336 cmp(length, Operand(bitmap_scratch, MemoryChunk::kSizeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003337 Check(less_equal, kLiveBytesCountOverflowChunkSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003338 }
3339
3340 bind(&done);
3341}
3342
3343
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003344void MacroAssembler::EnumLength(Register dst, Register map) {
3345 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
3346 mov(dst, FieldOperand(map, Map::kBitField3Offset));
3347 and_(dst, Immediate(Map::EnumLengthBits::kMask));
3348 SmiTag(dst);
3349}
3350
3351
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003352void MacroAssembler::CheckEnumCache(Label* call_runtime) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003353 Label next, start;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003354 mov(ecx, eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003356 // Check if the enum length field is properly initialized, indicating that
3357 // there is an enum cache.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003358 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003359
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003360 EnumLength(edx, ebx);
3361 cmp(edx, Immediate(Smi::FromInt(kInvalidEnumCacheSentinel)));
3362 j(equal, call_runtime);
3363
3364 jmp(&start);
3365
3366 bind(&next);
3367 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003368
3369 // For all objects but the receiver, check that the cache is empty.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003370 EnumLength(edx, ebx);
3371 cmp(edx, Immediate(Smi::FromInt(0)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003372 j(not_equal, call_runtime);
3373
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003374 bind(&start);
3375
3376 // Check that there are no elements. Register rcx contains the current JS
3377 // object we've reached through the prototype chain.
3378 Label no_elements;
3379 mov(ecx, FieldOperand(ecx, JSObject::kElementsOffset));
3380 cmp(ecx, isolate()->factory()->empty_fixed_array());
3381 j(equal, &no_elements);
3382
3383 // Second chance, the object may be using the empty slow element dictionary.
3384 cmp(ecx, isolate()->factory()->empty_slow_element_dictionary());
3385 j(not_equal, call_runtime);
3386
3387 bind(&no_elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003388 mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
3389 cmp(ecx, isolate()->factory()->null_value());
3390 j(not_equal, &next);
3391}
3392
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003393
3394void MacroAssembler::TestJSArrayForAllocationMemento(
3395 Register receiver_reg,
3396 Register scratch_reg,
3397 Label* no_memento_found) {
3398 ExternalReference new_space_start =
3399 ExternalReference::new_space_start(isolate());
3400 ExternalReference new_space_allocation_top =
3401 ExternalReference::new_space_allocation_top_address(isolate());
3402
3403 lea(scratch_reg, Operand(receiver_reg,
3404 JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
3405 cmp(scratch_reg, Immediate(new_space_start));
3406 j(less, no_memento_found);
3407 cmp(scratch_reg, Operand::StaticVariable(new_space_allocation_top));
3408 j(greater, no_memento_found);
3409 cmp(MemOperand(scratch_reg, -AllocationMemento::kSize),
3410 Immediate(isolate()->factory()->allocation_memento_map()));
3411}
3412
3413
3414void MacroAssembler::JumpIfDictionaryInPrototypeChain(
3415 Register object,
3416 Register scratch0,
3417 Register scratch1,
3418 Label* found) {
3419 DCHECK(!scratch1.is(scratch0));
3420 Factory* factory = isolate()->factory();
3421 Register current = scratch0;
3422 Label loop_again;
3423
3424 // scratch contained elements pointer.
3425 mov(current, object);
3426
3427 // Loop based on the map going up the prototype chain.
3428 bind(&loop_again);
3429 mov(current, FieldOperand(current, HeapObject::kMapOffset));
3430 mov(scratch1, FieldOperand(current, Map::kBitField2Offset));
3431 DecodeField<Map::ElementsKindBits>(scratch1);
3432 cmp(scratch1, Immediate(DICTIONARY_ELEMENTS));
3433 j(equal, found);
3434 mov(current, FieldOperand(current, Map::kPrototypeOffset));
3435 cmp(current, Immediate(factory->null_value()));
3436 j(not_equal, &loop_again);
3437}
3438
3439
3440void MacroAssembler::TruncatingDiv(Register dividend, int32_t divisor) {
3441 DCHECK(!dividend.is(eax));
3442 DCHECK(!dividend.is(edx));
3443 base::MagicNumbersForDivision<uint32_t> mag =
3444 base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
3445 mov(eax, Immediate(mag.multiplier));
3446 imul(dividend);
3447 bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
3448 if (divisor > 0 && neg) add(edx, dividend);
3449 if (divisor < 0 && !neg && mag.multiplier > 0) sub(edx, dividend);
3450 if (mag.shift > 0) sar(edx, mag.shift);
3451 mov(eax, dividend);
3452 shr(eax, 31);
3453 add(edx, eax);
3454}
3455
3456
Steve Blocka7e24c12009-10-30 11:49:00 +00003457} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01003458
3459#endif // V8_TARGET_ARCH_IA32