blob: b46167d1f9ae3f6b33e9449c7b1a8280acbe3a05 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_X87
6
7#include "src/base/bits.h"
8#include "src/base/division-by-constant.h"
9#include "src/bootstrapper.h"
10#include "src/codegen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/debug/debug.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012#include "src/runtime/runtime.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/x87/frames-x87.h"
14#include "src/x87/macro-assembler-x87.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015
16namespace v8 {
17namespace internal {
18
19// -------------------------------------------------------------------------
20// MacroAssembler implementation.
21
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size,
23 CodeObjectRequired create_code_object)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024 : Assembler(arg_isolate, buffer, size),
25 generating_stub_(false),
26 has_frame_(false) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027 if (create_code_object == CodeObjectRequired::kYes) {
28 code_object_ =
29 Handle<Object>::New(isolate()->heap()->undefined_value(), isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 }
31}
32
33
34void MacroAssembler::Load(Register dst, const Operand& src, Representation r) {
35 DCHECK(!r.IsDouble());
36 if (r.IsInteger8()) {
37 movsx_b(dst, src);
38 } else if (r.IsUInteger8()) {
39 movzx_b(dst, src);
40 } else if (r.IsInteger16()) {
41 movsx_w(dst, src);
42 } else if (r.IsUInteger16()) {
43 movzx_w(dst, src);
44 } else {
45 mov(dst, src);
46 }
47}
48
49
50void MacroAssembler::Store(Register src, const Operand& dst, Representation r) {
51 DCHECK(!r.IsDouble());
52 if (r.IsInteger8() || r.IsUInteger8()) {
53 mov_b(dst, src);
54 } else if (r.IsInteger16() || r.IsUInteger16()) {
55 mov_w(dst, src);
56 } else {
57 if (r.IsHeapObject()) {
58 AssertNotSmi(src);
59 } else if (r.IsSmi()) {
60 AssertSmi(src);
61 }
62 mov(dst, src);
63 }
64}
65
66
67void MacroAssembler::LoadRoot(Register destination, Heap::RootListIndex index) {
68 if (isolate()->heap()->RootCanBeTreatedAsConstant(index)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 mov(destination, isolate()->heap()->root_handle(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 return;
71 }
72 ExternalReference roots_array_start =
73 ExternalReference::roots_array_start(isolate());
74 mov(destination, Immediate(index));
75 mov(destination, Operand::StaticArray(destination,
76 times_pointer_size,
77 roots_array_start));
78}
79
80
81void MacroAssembler::StoreRoot(Register source,
82 Register scratch,
83 Heap::RootListIndex index) {
84 DCHECK(Heap::RootCanBeWrittenAfterInitialization(index));
85 ExternalReference roots_array_start =
86 ExternalReference::roots_array_start(isolate());
87 mov(scratch, Immediate(index));
88 mov(Operand::StaticArray(scratch, times_pointer_size, roots_array_start),
89 source);
90}
91
92
93void MacroAssembler::CompareRoot(Register with,
94 Register scratch,
95 Heap::RootListIndex index) {
96 ExternalReference roots_array_start =
97 ExternalReference::roots_array_start(isolate());
98 mov(scratch, Immediate(index));
99 cmp(with, Operand::StaticArray(scratch,
100 times_pointer_size,
101 roots_array_start));
102}
103
104
105void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
106 DCHECK(isolate()->heap()->RootCanBeTreatedAsConstant(index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000107 cmp(with, isolate()->heap()->root_handle(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108}
109
110
111void MacroAssembler::CompareRoot(const Operand& with,
112 Heap::RootListIndex index) {
113 DCHECK(isolate()->heap()->RootCanBeTreatedAsConstant(index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 cmp(with, isolate()->heap()->root_handle(index));
115}
116
117
118void MacroAssembler::PushRoot(Heap::RootListIndex index) {
119 DCHECK(isolate()->heap()->RootCanBeTreatedAsConstant(index));
120 Push(isolate()->heap()->root_handle(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121}
122
Ben Murdoch097c5b22016-05-18 11:27:45 +0100123#define REG(Name) \
124 { Register::kCode_##Name }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125
Ben Murdoch097c5b22016-05-18 11:27:45 +0100126static const Register saved_regs[] = {REG(eax), REG(ecx), REG(edx)};
127
128#undef REG
129
130static const int kNumberOfSavedRegs = sizeof(saved_regs) / sizeof(Register);
131
132void MacroAssembler::PushCallerSaved(SaveFPRegsMode fp_mode,
133 Register exclusion1, Register exclusion2,
134 Register exclusion3) {
135 // We don't allow a GC during a store buffer overflow so there is no need to
136 // store the registers in any particular way, but we do have to store and
137 // restore them.
138 for (int i = 0; i < kNumberOfSavedRegs; i++) {
139 Register reg = saved_regs[i];
140 if (!reg.is(exclusion1) && !reg.is(exclusion2) && !reg.is(exclusion3)) {
141 push(reg);
142 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100144 if (fp_mode == kSaveFPRegs) {
145 // Save FPU state in m108byte.
146 sub(esp, Immediate(108));
147 fnsave(Operand(esp, 0));
148 }
149}
150
151void MacroAssembler::PopCallerSaved(SaveFPRegsMode fp_mode, Register exclusion1,
152 Register exclusion2, Register exclusion3) {
153 if (fp_mode == kSaveFPRegs) {
154 // Restore FPU state in m108byte.
155 frstor(Operand(esp, 0));
156 add(esp, Immediate(108));
157 }
158
159 for (int i = kNumberOfSavedRegs - 1; i >= 0; i--) {
160 Register reg = saved_regs[i];
161 if (!reg.is(exclusion1) && !reg.is(exclusion2) && !reg.is(exclusion3)) {
162 pop(reg);
163 }
164 }
165}
166
167void MacroAssembler::InNewSpace(Register object, Register scratch, Condition cc,
168 Label* condition_met,
169 Label::Distance distance) {
170 const int mask =
171 (1 << MemoryChunk::IN_FROM_SPACE) | (1 << MemoryChunk::IN_TO_SPACE);
172 CheckPageFlag(object, scratch, mask, cc, condition_met, distance);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173}
174
175
176void MacroAssembler::RememberedSetHelper(
177 Register object, // Only used for debug checks.
178 Register addr, Register scratch, SaveFPRegsMode save_fp,
179 MacroAssembler::RememberedSetFinalAction and_then) {
180 Label done;
181 if (emit_debug_code()) {
182 Label ok;
183 JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
184 int3();
185 bind(&ok);
186 }
187 // Load store buffer top.
188 ExternalReference store_buffer =
189 ExternalReference::store_buffer_top(isolate());
190 mov(scratch, Operand::StaticVariable(store_buffer));
191 // Store pointer to buffer.
192 mov(Operand(scratch, 0), addr);
193 // Increment buffer top.
194 add(scratch, Immediate(kPointerSize));
195 // Write back new top of buffer.
196 mov(Operand::StaticVariable(store_buffer), scratch);
197 // Call stub on end of buffer.
198 // Check for end of buffer.
Ben Murdochda12d292016-06-02 14:46:10 +0100199 test(scratch, Immediate(StoreBuffer::kStoreBufferMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 if (and_then == kReturnAtEnd) {
201 Label buffer_overflowed;
Ben Murdochda12d292016-06-02 14:46:10 +0100202 j(equal, &buffer_overflowed, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 ret(0);
204 bind(&buffer_overflowed);
205 } else {
206 DCHECK(and_then == kFallThroughAtEnd);
Ben Murdochda12d292016-06-02 14:46:10 +0100207 j(not_equal, &done, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 }
209 StoreBufferOverflowStub store_buffer_overflow(isolate(), save_fp);
210 CallStub(&store_buffer_overflow);
211 if (and_then == kReturnAtEnd) {
212 ret(0);
213 } else {
214 DCHECK(and_then == kFallThroughAtEnd);
215 bind(&done);
216 }
217}
218
219
220void MacroAssembler::ClampTOSToUint8(Register result_reg) {
221 Label done, conv_failure;
222 sub(esp, Immediate(kPointerSize));
223 fnclex();
224 fist_s(Operand(esp, 0));
225 pop(result_reg);
226 X87CheckIA();
227 j(equal, &conv_failure, Label::kNear);
228 test(result_reg, Immediate(0xFFFFFF00));
229 j(zero, &done, Label::kNear);
230 setcc(sign, result_reg);
231 sub(result_reg, Immediate(1));
232 and_(result_reg, Immediate(255));
233 jmp(&done, Label::kNear);
234 bind(&conv_failure);
235 fnclex();
236 fldz();
237 fld(1);
238 FCmp();
239 setcc(below, result_reg); // 1 if negative, 0 if positive.
240 dec_b(result_reg); // 0 if negative, 255 if positive.
241 bind(&done);
242}
243
244
245void MacroAssembler::ClampUint8(Register reg) {
246 Label done;
247 test(reg, Immediate(0xFFFFFF00));
248 j(zero, &done, Label::kNear);
249 setcc(negative, reg); // 1 if negative, 0 if positive.
250 dec_b(reg); // 0 if negative, 255 if positive.
251 bind(&done);
252}
253
254
255void MacroAssembler::SlowTruncateToI(Register result_reg,
256 Register input_reg,
257 int offset) {
258 DoubleToIStub stub(isolate(), input_reg, result_reg, offset, true);
259 call(stub.GetCode(), RelocInfo::CODE_TARGET);
260}
261
262
263void MacroAssembler::TruncateX87TOSToI(Register result_reg) {
264 sub(esp, Immediate(kDoubleSize));
265 fst_d(MemOperand(esp, 0));
266 SlowTruncateToI(result_reg, esp, 0);
267 add(esp, Immediate(kDoubleSize));
268}
269
270
271void MacroAssembler::X87TOSToI(Register result_reg,
272 MinusZeroMode minus_zero_mode,
273 Label* lost_precision, Label* is_nan,
274 Label* minus_zero, Label::Distance dst) {
275 Label done;
276 sub(esp, Immediate(kPointerSize));
277 fld(0);
278 fist_s(MemOperand(esp, 0));
279 fild_s(MemOperand(esp, 0));
280 pop(result_reg);
281 FCmp();
282 j(not_equal, lost_precision, dst);
283 j(parity_even, is_nan, dst);
284 if (minus_zero_mode == FAIL_ON_MINUS_ZERO) {
285 test(result_reg, Operand(result_reg));
286 j(not_zero, &done, Label::kNear);
287 // To check for minus zero, we load the value again as float, and check
288 // if that is still 0.
289 sub(esp, Immediate(kPointerSize));
290 fst_s(MemOperand(esp, 0));
291 pop(result_reg);
292 test(result_reg, Operand(result_reg));
293 j(not_zero, minus_zero, dst);
294 }
295 bind(&done);
296}
297
298
299void MacroAssembler::TruncateHeapNumberToI(Register result_reg,
300 Register input_reg) {
301 Label done, slow_case;
302
303 SlowTruncateToI(result_reg, input_reg);
304 bind(&done);
305}
306
307
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400308void MacroAssembler::LoadUint32NoSSE2(const Operand& src) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 Label done;
310 push(src);
311 fild_s(Operand(esp, 0));
312 cmp(src, Immediate(0));
313 j(not_sign, &done, Label::kNear);
314 ExternalReference uint32_bias =
315 ExternalReference::address_of_uint32_bias();
316 fld_d(Operand::StaticVariable(uint32_bias));
317 faddp(1);
318 bind(&done);
319 add(esp, Immediate(kPointerSize));
320}
321
322
323void MacroAssembler::RecordWriteArray(
324 Register object, Register value, Register index, SaveFPRegsMode save_fp,
325 RememberedSetAction remembered_set_action, SmiCheck smi_check,
326 PointersToHereCheck pointers_to_here_check_for_value) {
327 // First, check if a write barrier is even needed. The tests below
328 // catch stores of Smis.
329 Label done;
330
331 // Skip barrier if writing a smi.
332 if (smi_check == INLINE_SMI_CHECK) {
333 DCHECK_EQ(0, kSmiTag);
334 test(value, Immediate(kSmiTagMask));
335 j(zero, &done);
336 }
337
338 // Array access: calculate the destination address in the same manner as
339 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
340 // into an array of words.
341 Register dst = index;
342 lea(dst, Operand(object, index, times_half_pointer_size,
343 FixedArray::kHeaderSize - kHeapObjectTag));
344
345 RecordWrite(object, dst, value, save_fp, remembered_set_action,
346 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
347
348 bind(&done);
349
350 // Clobber clobbered input registers when running with the debug-code flag
351 // turned on to provoke errors.
352 if (emit_debug_code()) {
353 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
354 mov(index, Immediate(bit_cast<int32_t>(kZapValue)));
355 }
356}
357
358
359void MacroAssembler::RecordWriteField(
360 Register object, int offset, Register value, Register dst,
361 SaveFPRegsMode save_fp, RememberedSetAction remembered_set_action,
362 SmiCheck smi_check, PointersToHereCheck pointers_to_here_check_for_value) {
363 // First, check if a write barrier is even needed. The tests below
364 // catch stores of Smis.
365 Label done;
366
367 // Skip barrier if writing a smi.
368 if (smi_check == INLINE_SMI_CHECK) {
369 JumpIfSmi(value, &done, Label::kNear);
370 }
371
372 // Although the object register is tagged, the offset is relative to the start
373 // of the object, so so offset must be a multiple of kPointerSize.
374 DCHECK(IsAligned(offset, kPointerSize));
375
376 lea(dst, FieldOperand(object, offset));
377 if (emit_debug_code()) {
378 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +0100379 test_b(dst, Immediate((1 << kPointerSizeLog2) - 1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 j(zero, &ok, Label::kNear);
381 int3();
382 bind(&ok);
383 }
384
385 RecordWrite(object, dst, value, save_fp, remembered_set_action,
386 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
387
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()) {
393 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
394 mov(dst, Immediate(bit_cast<int32_t>(kZapValue)));
395 }
396}
397
398
399void MacroAssembler::RecordWriteForMap(Register object, Handle<Map> map,
400 Register scratch1, Register scratch2,
401 SaveFPRegsMode save_fp) {
402 Label done;
403
404 Register address = scratch1;
405 Register value = scratch2;
406 if (emit_debug_code()) {
407 Label ok;
408 lea(address, FieldOperand(object, HeapObject::kMapOffset));
Ben Murdochda12d292016-06-02 14:46:10 +0100409 test_b(address, Immediate((1 << kPointerSizeLog2) - 1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410 j(zero, &ok, Label::kNear);
411 int3();
412 bind(&ok);
413 }
414
415 DCHECK(!object.is(value));
416 DCHECK(!object.is(address));
417 DCHECK(!value.is(address));
418 AssertNotSmi(object);
419
420 if (!FLAG_incremental_marking) {
421 return;
422 }
423
424 // Compute the address.
425 lea(address, FieldOperand(object, HeapObject::kMapOffset));
426
427 // A single check of the map's pages interesting flag suffices, since it is
428 // only set during incremental collection, and then it's also guaranteed that
429 // the from object's page's interesting flag is also set. This optimization
430 // relies on the fact that maps can never be in new space.
431 DCHECK(!isolate()->heap()->InNewSpace(*map));
432 CheckPageFlagForMap(map,
433 MemoryChunk::kPointersToHereAreInterestingMask,
434 zero,
435 &done,
436 Label::kNear);
437
438 RecordWriteStub stub(isolate(), object, value, address, OMIT_REMEMBERED_SET,
439 save_fp);
440 CallStub(&stub);
441
442 bind(&done);
443
444 // Count number of write barriers in generated code.
445 isolate()->counters()->write_barriers_static()->Increment();
446 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
447
448 // Clobber clobbered input registers when running with the debug-code flag
449 // turned on to provoke errors.
450 if (emit_debug_code()) {
451 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
452 mov(scratch1, Immediate(bit_cast<int32_t>(kZapValue)));
453 mov(scratch2, Immediate(bit_cast<int32_t>(kZapValue)));
454 }
455}
456
457
458void MacroAssembler::RecordWrite(
459 Register object, Register address, Register value, SaveFPRegsMode fp_mode,
460 RememberedSetAction remembered_set_action, SmiCheck smi_check,
461 PointersToHereCheck pointers_to_here_check_for_value) {
462 DCHECK(!object.is(value));
463 DCHECK(!object.is(address));
464 DCHECK(!value.is(address));
465 AssertNotSmi(object);
466
467 if (remembered_set_action == OMIT_REMEMBERED_SET &&
468 !FLAG_incremental_marking) {
469 return;
470 }
471
472 if (emit_debug_code()) {
473 Label ok;
474 cmp(value, Operand(address, 0));
475 j(equal, &ok, Label::kNear);
476 int3();
477 bind(&ok);
478 }
479
480 // First, check if a write barrier is even needed. The tests below
481 // catch stores of Smis and stores into young gen.
482 Label done;
483
484 if (smi_check == INLINE_SMI_CHECK) {
485 // Skip barrier if writing a smi.
486 JumpIfSmi(value, &done, Label::kNear);
487 }
488
489 if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
490 CheckPageFlag(value,
491 value, // Used as scratch.
492 MemoryChunk::kPointersToHereAreInterestingMask,
493 zero,
494 &done,
495 Label::kNear);
496 }
497 CheckPageFlag(object,
498 value, // Used as scratch.
499 MemoryChunk::kPointersFromHereAreInterestingMask,
500 zero,
501 &done,
502 Label::kNear);
503
504 RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
505 fp_mode);
506 CallStub(&stub);
507
508 bind(&done);
509
510 // Count number of write barriers in generated code.
511 isolate()->counters()->write_barriers_static()->Increment();
512 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
513
514 // Clobber clobbered registers when running with the debug-code flag
515 // turned on to provoke errors.
516 if (emit_debug_code()) {
517 mov(address, Immediate(bit_cast<int32_t>(kZapValue)));
518 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
519 }
520}
521
Ben Murdoch097c5b22016-05-18 11:27:45 +0100522void MacroAssembler::RecordWriteCodeEntryField(Register js_function,
523 Register code_entry,
524 Register scratch) {
525 const int offset = JSFunction::kCodeEntryOffset;
526
527 // Since a code entry (value) is always in old space, we don't need to update
528 // remembered set. If incremental marking is off, there is nothing for us to
529 // do.
530 if (!FLAG_incremental_marking) return;
531
532 DCHECK(!js_function.is(code_entry));
533 DCHECK(!js_function.is(scratch));
534 DCHECK(!code_entry.is(scratch));
535 AssertNotSmi(js_function);
536
537 if (emit_debug_code()) {
538 Label ok;
539 lea(scratch, FieldOperand(js_function, offset));
540 cmp(code_entry, Operand(scratch, 0));
541 j(equal, &ok, Label::kNear);
542 int3();
543 bind(&ok);
544 }
545
546 // First, check if a write barrier is even needed. The tests below
547 // catch stores of Smis and stores into young gen.
548 Label done;
549
550 CheckPageFlag(code_entry, scratch,
551 MemoryChunk::kPointersToHereAreInterestingMask, zero, &done,
552 Label::kNear);
553 CheckPageFlag(js_function, scratch,
554 MemoryChunk::kPointersFromHereAreInterestingMask, zero, &done,
555 Label::kNear);
556
557 // Save input registers.
558 push(js_function);
559 push(code_entry);
560
561 const Register dst = scratch;
562 lea(dst, FieldOperand(js_function, offset));
563
564 // Save caller-saved registers.
565 PushCallerSaved(kDontSaveFPRegs, js_function, code_entry);
566
567 int argument_count = 3;
568 PrepareCallCFunction(argument_count, code_entry);
569 mov(Operand(esp, 0 * kPointerSize), js_function);
570 mov(Operand(esp, 1 * kPointerSize), dst); // Slot.
571 mov(Operand(esp, 2 * kPointerSize),
572 Immediate(ExternalReference::isolate_address(isolate())));
573
574 {
575 AllowExternalCallThatCantCauseGC scope(this);
576 CallCFunction(
577 ExternalReference::incremental_marking_record_write_code_entry_function(
578 isolate()),
579 argument_count);
580 }
581
582 // Restore caller-saved registers.
583 PopCallerSaved(kDontSaveFPRegs, js_function, code_entry);
584
585 // Restore input registers.
586 pop(code_entry);
587 pop(js_function);
588
589 bind(&done);
590}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591
592void MacroAssembler::DebugBreak() {
593 Move(eax, Immediate(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000594 mov(ebx, Immediate(ExternalReference(Runtime::kHandleDebuggerStatement,
595 isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596 CEntryStub ces(isolate(), 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000597 call(ces.GetCode(), RelocInfo::DEBUGGER_STATEMENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598}
599
Ben Murdochda12d292016-06-02 14:46:10 +0100600void MacroAssembler::ShlPair(Register high, Register low, uint8_t shift) {
601 if (shift >= 32) {
602 mov(high, low);
603 shl(high, shift - 32);
604 xor_(low, low);
605 } else {
606 shld(high, low, shift);
607 shl(low, shift);
608 }
609}
610
611void MacroAssembler::ShlPair_cl(Register high, Register low) {
612 shld_cl(high, low);
613 shl_cl(low);
614 Label done;
615 test(ecx, Immediate(0x20));
616 j(equal, &done, Label::kNear);
617 mov(high, low);
618 xor_(low, low);
619 bind(&done);
620}
621
622void MacroAssembler::ShrPair(Register high, Register low, uint8_t shift) {
623 if (shift >= 32) {
624 mov(low, high);
625 shr(low, shift - 32);
626 xor_(high, high);
627 } else {
628 shrd(high, low, shift);
629 shr(high, shift);
630 }
631}
632
633void MacroAssembler::ShrPair_cl(Register high, Register low) {
634 shrd_cl(low, high);
635 shr_cl(high);
636 Label done;
637 test(ecx, Immediate(0x20));
638 j(equal, &done, Label::kNear);
639 mov(low, high);
640 xor_(high, high);
641 bind(&done);
642}
643
644void MacroAssembler::SarPair(Register high, Register low, uint8_t shift) {
645 if (shift >= 32) {
646 mov(low, high);
647 sar(low, shift - 32);
648 sar(high, 31);
649 } else {
650 shrd(high, low, shift);
651 sar(high, shift);
652 }
653}
654
655void MacroAssembler::SarPair_cl(Register high, Register low) {
656 shrd_cl(low, high);
657 sar_cl(high);
658 Label done;
659 test(ecx, Immediate(0x20));
660 j(equal, &done, Label::kNear);
661 mov(low, high);
662 sar(high, 31);
663 bind(&done);
664}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665
666bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
667 static const int kMaxImmediateBits = 17;
668 if (!RelocInfo::IsNone(x.rmode_)) return false;
669 return !is_intn(x.x_, kMaxImmediateBits);
670}
671
672
673void MacroAssembler::SafeMove(Register dst, const Immediate& x) {
674 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
675 Move(dst, Immediate(x.x_ ^ jit_cookie()));
676 xor_(dst, jit_cookie());
677 } else {
678 Move(dst, x);
679 }
680}
681
682
683void MacroAssembler::SafePush(const Immediate& x) {
684 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
685 push(Immediate(x.x_ ^ jit_cookie()));
686 xor_(Operand(esp, 0), Immediate(jit_cookie()));
687 } else {
688 push(x);
689 }
690}
691
692
693void MacroAssembler::CmpObjectType(Register heap_object,
694 InstanceType type,
695 Register map) {
696 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
697 CmpInstanceType(map, type);
698}
699
700
701void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
Ben Murdochda12d292016-06-02 14:46:10 +0100702 cmpb(FieldOperand(map, Map::kInstanceTypeOffset), Immediate(type));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000703}
704
705
706void MacroAssembler::CheckFastElements(Register map,
707 Label* fail,
708 Label::Distance distance) {
709 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
710 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
711 STATIC_ASSERT(FAST_ELEMENTS == 2);
712 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
713 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochda12d292016-06-02 14:46:10 +0100714 Immediate(Map::kMaximumBitField2FastHoleyElementValue));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 j(above, fail, distance);
716}
717
718
719void MacroAssembler::CheckFastObjectElements(Register map,
720 Label* fail,
721 Label::Distance distance) {
722 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
723 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
724 STATIC_ASSERT(FAST_ELEMENTS == 2);
725 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
726 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochda12d292016-06-02 14:46:10 +0100727 Immediate(Map::kMaximumBitField2FastHoleySmiElementValue));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728 j(below_equal, fail, distance);
729 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochda12d292016-06-02 14:46:10 +0100730 Immediate(Map::kMaximumBitField2FastHoleyElementValue));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 j(above, fail, distance);
732}
733
734
735void MacroAssembler::CheckFastSmiElements(Register map,
736 Label* fail,
737 Label::Distance distance) {
738 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
739 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
740 cmpb(FieldOperand(map, Map::kBitField2Offset),
Ben Murdochda12d292016-06-02 14:46:10 +0100741 Immediate(Map::kMaximumBitField2FastHoleySmiElementValue));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 j(above, fail, distance);
743}
744
745
746void MacroAssembler::StoreNumberToDoubleElements(
747 Register maybe_number,
748 Register elements,
749 Register key,
750 Register scratch,
751 Label* fail,
752 int elements_offset) {
753 Label smi_value, done, maybe_nan, not_nan, is_nan, have_double_value;
754 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
755
756 CheckMap(maybe_number,
757 isolate()->factory()->heap_number_map(),
758 fail,
759 DONT_DO_SMI_CHECK);
760
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 fld_d(FieldOperand(maybe_number, HeapNumber::kValueOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000762 jmp(&done, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000763
764 bind(&smi_value);
765 // Value is a smi. Convert to a double and store.
766 // Preserve original value.
767 mov(scratch, maybe_number);
768 SmiUntag(scratch);
769 push(scratch);
770 fild_s(Operand(esp, 0));
771 pop(scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000772 bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000773 fstp_d(FieldOperand(elements, key, times_4,
774 FixedDoubleArray::kHeaderSize - elements_offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775}
776
777
778void MacroAssembler::CompareMap(Register obj, Handle<Map> map) {
779 cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
780}
781
782
783void MacroAssembler::CheckMap(Register obj,
784 Handle<Map> map,
785 Label* fail,
786 SmiCheckType smi_check_type) {
787 if (smi_check_type == DO_SMI_CHECK) {
788 JumpIfSmi(obj, fail);
789 }
790
791 CompareMap(obj, map);
792 j(not_equal, fail);
793}
794
795
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400796void MacroAssembler::DispatchWeakMap(Register obj, Register scratch1,
797 Register scratch2, Handle<WeakCell> cell,
798 Handle<Code> success,
799 SmiCheckType smi_check_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000800 Label fail;
801 if (smi_check_type == DO_SMI_CHECK) {
802 JumpIfSmi(obj, &fail);
803 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400804 mov(scratch1, FieldOperand(obj, HeapObject::kMapOffset));
805 CmpWeakValue(scratch1, cell, scratch2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000806 j(equal, success);
807
808 bind(&fail);
809}
810
811
812Condition MacroAssembler::IsObjectStringType(Register heap_object,
813 Register map,
814 Register instance_type) {
815 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
816 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
817 STATIC_ASSERT(kNotStringTag != 0);
818 test(instance_type, Immediate(kIsNotStringMask));
819 return zero;
820}
821
822
823Condition MacroAssembler::IsObjectNameType(Register heap_object,
824 Register map,
825 Register instance_type) {
826 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
827 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
Ben Murdochda12d292016-06-02 14:46:10 +0100828 cmpb(instance_type, Immediate(LAST_NAME_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000829 return below_equal;
830}
831
832
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833void MacroAssembler::FCmp() {
834 fucompp();
835 push(eax);
836 fnstsw_ax();
837 sahf();
838 pop(eax);
839}
840
841
842void MacroAssembler::FXamMinusZero() {
843 fxam();
844 push(eax);
845 fnstsw_ax();
846 and_(eax, Immediate(0x4700));
847 // For minus zero, C3 == 1 && C1 == 1.
848 cmp(eax, Immediate(0x4200));
849 pop(eax);
850 fstp(0);
851}
852
853
854void MacroAssembler::FXamSign() {
855 fxam();
856 push(eax);
857 fnstsw_ax();
858 // For negative value (including -0.0), C1 == 1.
859 and_(eax, Immediate(0x0200));
860 pop(eax);
861 fstp(0);
862}
863
864
865void MacroAssembler::X87CheckIA() {
866 push(eax);
867 fnstsw_ax();
868 // For #IA, IE == 1 && SF == 0.
869 and_(eax, Immediate(0x0041));
870 cmp(eax, Immediate(0x0001));
871 pop(eax);
872}
873
874
875// rc=00B, round to nearest.
876// rc=01B, round down.
877// rc=10B, round up.
878// rc=11B, round toward zero.
879void MacroAssembler::X87SetRC(int rc) {
880 sub(esp, Immediate(kPointerSize));
881 fnstcw(MemOperand(esp, 0));
882 and_(MemOperand(esp, 0), Immediate(0xF3FF));
883 or_(MemOperand(esp, 0), Immediate(rc));
884 fldcw(MemOperand(esp, 0));
885 add(esp, Immediate(kPointerSize));
886}
887
888
889void MacroAssembler::X87SetFPUCW(int cw) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000890 RecordComment("-- X87SetFPUCW start --");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 push(Immediate(cw));
892 fldcw(MemOperand(esp, 0));
893 add(esp, Immediate(kPointerSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000894 RecordComment("-- X87SetFPUCW end--");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895}
896
897
898void MacroAssembler::AssertNumber(Register object) {
899 if (emit_debug_code()) {
900 Label ok;
901 JumpIfSmi(object, &ok);
902 cmp(FieldOperand(object, HeapObject::kMapOffset),
903 isolate()->factory()->heap_number_map());
904 Check(equal, kOperandNotANumber);
905 bind(&ok);
906 }
907}
908
Ben Murdochda12d292016-06-02 14:46:10 +0100909void MacroAssembler::AssertNotNumber(Register object) {
910 if (emit_debug_code()) {
911 test(object, Immediate(kSmiTagMask));
912 Check(not_equal, kOperandIsANumber);
913 cmp(FieldOperand(object, HeapObject::kMapOffset),
914 isolate()->factory()->heap_number_map());
915 Check(not_equal, kOperandIsANumber);
916 }
917}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918
919void MacroAssembler::AssertSmi(Register object) {
920 if (emit_debug_code()) {
921 test(object, Immediate(kSmiTagMask));
922 Check(equal, kOperandIsNotASmi);
923 }
924}
925
926
927void MacroAssembler::AssertString(Register object) {
928 if (emit_debug_code()) {
929 test(object, Immediate(kSmiTagMask));
930 Check(not_equal, kOperandIsASmiAndNotAString);
931 push(object);
932 mov(object, FieldOperand(object, HeapObject::kMapOffset));
933 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
934 pop(object);
935 Check(below, kOperandIsNotAString);
936 }
937}
938
939
940void MacroAssembler::AssertName(Register object) {
941 if (emit_debug_code()) {
942 test(object, Immediate(kSmiTagMask));
943 Check(not_equal, kOperandIsASmiAndNotAName);
944 push(object);
945 mov(object, FieldOperand(object, HeapObject::kMapOffset));
946 CmpInstanceType(object, LAST_NAME_TYPE);
947 pop(object);
948 Check(below_equal, kOperandIsNotAName);
949 }
950}
951
952
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000953void MacroAssembler::AssertFunction(Register object) {
954 if (emit_debug_code()) {
955 test(object, Immediate(kSmiTagMask));
956 Check(not_equal, kOperandIsASmiAndNotAFunction);
957 Push(object);
958 CmpObjectType(object, JS_FUNCTION_TYPE, object);
959 Pop(object);
960 Check(equal, kOperandIsNotAFunction);
961 }
962}
963
964
965void MacroAssembler::AssertBoundFunction(Register object) {
966 if (emit_debug_code()) {
967 test(object, Immediate(kSmiTagMask));
968 Check(not_equal, kOperandIsASmiAndNotABoundFunction);
969 Push(object);
970 CmpObjectType(object, JS_BOUND_FUNCTION_TYPE, object);
971 Pop(object);
972 Check(equal, kOperandIsNotABoundFunction);
973 }
974}
975
Ben Murdoch097c5b22016-05-18 11:27:45 +0100976void MacroAssembler::AssertReceiver(Register object) {
977 if (emit_debug_code()) {
978 test(object, Immediate(kSmiTagMask));
979 Check(not_equal, kOperandIsASmiAndNotAReceiver);
980 Push(object);
981 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
982 CmpObjectType(object, FIRST_JS_RECEIVER_TYPE, object);
983 Pop(object);
984 Check(above_equal, kOperandIsNotAReceiver);
985 }
986}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000987
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000988void MacroAssembler::AssertUndefinedOrAllocationSite(Register object) {
989 if (emit_debug_code()) {
990 Label done_checking;
991 AssertNotSmi(object);
992 cmp(object, isolate()->factory()->undefined_value());
993 j(equal, &done_checking);
994 cmp(FieldOperand(object, 0),
995 Immediate(isolate()->factory()->allocation_site_map()));
996 Assert(equal, kExpectedUndefinedOrCell);
997 bind(&done_checking);
998 }
999}
1000
1001
1002void MacroAssembler::AssertNotSmi(Register object) {
1003 if (emit_debug_code()) {
1004 test(object, Immediate(kSmiTagMask));
1005 Check(not_equal, kOperandIsASmi);
1006 }
1007}
1008
Ben Murdochda12d292016-06-02 14:46:10 +01001009void MacroAssembler::StubPrologue(StackFrame::Type type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 push(ebp); // Caller's frame pointer.
1011 mov(ebp, esp);
Ben Murdochda12d292016-06-02 14:46:10 +01001012 push(Immediate(Smi::FromInt(type)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013}
1014
1015
1016void MacroAssembler::Prologue(bool code_pre_aging) {
1017 PredictableCodeSizeScope predictible_code_size_scope(this,
1018 kNoCodeAgeSequenceLength);
1019 if (code_pre_aging) {
1020 // Pre-age the code.
1021 call(isolate()->builtins()->MarkCodeAsExecutedOnce(),
1022 RelocInfo::CODE_AGE_SEQUENCE);
1023 Nop(kNoCodeAgeSequenceLength - Assembler::kCallInstructionLength);
1024 } else {
1025 push(ebp); // Caller's frame pointer.
1026 mov(ebp, esp);
1027 push(esi); // Callee's context.
1028 push(edi); // Callee's JS function.
1029 }
1030}
1031
1032
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001033void MacroAssembler::EmitLoadTypeFeedbackVector(Register vector) {
1034 mov(vector, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1035 mov(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
1036 mov(vector, FieldOperand(vector, SharedFunctionInfo::kFeedbackVectorOffset));
1037}
1038
1039
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001040void MacroAssembler::EnterFrame(StackFrame::Type type,
1041 bool load_constant_pool_pointer_reg) {
1042 // Out-of-line constant pool not implemented on x87.
1043 UNREACHABLE();
1044}
1045
1046
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001047void MacroAssembler::EnterFrame(StackFrame::Type type) {
1048 push(ebp);
1049 mov(ebp, esp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001050 push(Immediate(Smi::FromInt(type)));
Ben Murdochda12d292016-06-02 14:46:10 +01001051 if (type == StackFrame::INTERNAL) {
1052 push(Immediate(CodeObject()));
1053 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001054 if (emit_debug_code()) {
1055 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
1056 Check(not_equal, kCodeObjectNotProperlyPatched);
1057 }
1058}
1059
1060
1061void MacroAssembler::LeaveFrame(StackFrame::Type type) {
1062 if (emit_debug_code()) {
Ben Murdochda12d292016-06-02 14:46:10 +01001063 cmp(Operand(ebp, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064 Immediate(Smi::FromInt(type)));
1065 Check(equal, kStackFrameTypesMustMatch);
1066 }
1067 leave();
1068}
1069
1070
1071void MacroAssembler::EnterExitFramePrologue() {
1072 // Set up the frame structure on the stack.
Ben Murdochda12d292016-06-02 14:46:10 +01001073 DCHECK_EQ(+2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
1074 DCHECK_EQ(+1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
1075 DCHECK_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001076 push(ebp);
1077 mov(ebp, esp);
1078
1079 // Reserve room for entry stack pointer and push the code object.
Ben Murdochda12d292016-06-02 14:46:10 +01001080 push(Immediate(Smi::FromInt(StackFrame::EXIT)));
1081 DCHECK_EQ(-2 * kPointerSize, ExitFrameConstants::kSPOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001082 push(Immediate(0)); // Saved entry sp, patched before call.
Ben Murdochda12d292016-06-02 14:46:10 +01001083 DCHECK_EQ(-3 * kPointerSize, ExitFrameConstants::kCodeOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
1085
1086 // Save the frame pointer and the context in top.
1087 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress, isolate());
1088 ExternalReference context_address(Isolate::kContextAddress, isolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001089 ExternalReference c_function_address(Isolate::kCFunctionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001090 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
1091 mov(Operand::StaticVariable(context_address), esi);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001092 mov(Operand::StaticVariable(c_function_address), ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001093}
1094
1095
1096void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
1097 // Optionally save FPU state.
1098 if (save_doubles) {
1099 // Store FPU state to m108byte.
1100 int space = 108 + argc * kPointerSize;
1101 sub(esp, Immediate(space));
Ben Murdochda12d292016-06-02 14:46:10 +01001102 const int offset = -ExitFrameConstants::kFixedFrameSizeFromFp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103 fnsave(MemOperand(ebp, offset - 108));
1104 } else {
1105 sub(esp, Immediate(argc * kPointerSize));
1106 }
1107
1108 // Get the required frame alignment for the OS.
1109 const int kFrameAlignment = base::OS::ActivationFrameAlignment();
1110 if (kFrameAlignment > 0) {
1111 DCHECK(base::bits::IsPowerOfTwo32(kFrameAlignment));
1112 and_(esp, -kFrameAlignment);
1113 }
1114
1115 // Patch the saved entry sp.
1116 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
1117}
1118
1119
Ben Murdoch097c5b22016-05-18 11:27:45 +01001120void MacroAssembler::EnterExitFrame(int argc, bool save_doubles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121 EnterExitFramePrologue();
1122
1123 // Set up argc and argv in callee-saved registers.
1124 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
1125 mov(edi, eax);
1126 lea(esi, Operand(ebp, eax, times_4, offset));
1127
1128 // Reserve space for argc, argv and isolate.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001129 EnterExitFrameEpilogue(argc, save_doubles);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130}
1131
1132
1133void MacroAssembler::EnterApiExitFrame(int argc) {
1134 EnterExitFramePrologue();
1135 EnterExitFrameEpilogue(argc, false);
1136}
1137
1138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001139void MacroAssembler::LeaveExitFrame(bool save_doubles, bool pop_arguments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001140 // Optionally restore FPU state.
1141 if (save_doubles) {
Ben Murdochda12d292016-06-02 14:46:10 +01001142 const int offset = -ExitFrameConstants::kFixedFrameSizeFromFp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143 frstor(MemOperand(ebp, offset - 108));
1144 }
1145
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001146 if (pop_arguments) {
1147 // Get the return address from the stack and restore the frame pointer.
1148 mov(ecx, Operand(ebp, 1 * kPointerSize));
1149 mov(ebp, Operand(ebp, 0 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001150
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001151 // Pop the arguments and the receiver from the caller stack.
1152 lea(esp, Operand(esi, 1 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001154 // Push the return address to get ready to return.
1155 push(ecx);
1156 } else {
1157 // Otherwise just leave the exit frame.
1158 leave();
1159 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160
1161 LeaveExitFrameEpilogue(true);
1162}
1163
1164
1165void MacroAssembler::LeaveExitFrameEpilogue(bool restore_context) {
1166 // Restore current context from top and clear it in debug mode.
1167 ExternalReference context_address(Isolate::kContextAddress, isolate());
1168 if (restore_context) {
1169 mov(esi, Operand::StaticVariable(context_address));
1170 }
1171#ifdef DEBUG
1172 mov(Operand::StaticVariable(context_address), Immediate(0));
1173#endif
1174
1175 // Clear the top frame.
1176 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
1177 isolate());
1178 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
1179}
1180
1181
1182void MacroAssembler::LeaveApiExitFrame(bool restore_context) {
1183 mov(esp, ebp);
1184 pop(ebp);
1185
1186 LeaveExitFrameEpilogue(restore_context);
1187}
1188
1189
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001190void MacroAssembler::PushStackHandler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001191 // Adjust this code if not the case.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001192 STATIC_ASSERT(StackHandlerConstants::kSize == 1 * kPointerSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001193 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001194
1195 // Link the current handler as the next handler.
1196 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
1197 push(Operand::StaticVariable(handler_address));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001198
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001199 // Set this new handler as the current one.
1200 mov(Operand::StaticVariable(handler_address), esp);
1201}
1202
1203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001204void MacroAssembler::PopStackHandler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1206 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
1207 pop(Operand::StaticVariable(handler_address));
1208 add(esp, Immediate(StackHandlerConstants::kSize - kPointerSize));
1209}
1210
1211
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001212void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1213 Register scratch1,
1214 Register scratch2,
1215 Label* miss) {
1216 Label same_contexts;
1217
1218 DCHECK(!holder_reg.is(scratch1));
1219 DCHECK(!holder_reg.is(scratch2));
1220 DCHECK(!scratch1.is(scratch2));
1221
Ben Murdochda12d292016-06-02 14:46:10 +01001222 // Load current lexical context from the active StandardFrame, which
1223 // may require crawling past STUB frames.
1224 Label load_context;
1225 Label has_context;
1226 mov(scratch2, ebp);
1227 bind(&load_context);
1228 mov(scratch1,
1229 MemOperand(scratch2, CommonFrameConstants::kContextOrFrameTypeOffset));
1230 JumpIfNotSmi(scratch1, &has_context);
1231 mov(scratch2, MemOperand(scratch2, CommonFrameConstants::kCallerFPOffset));
1232 jmp(&load_context);
1233 bind(&has_context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001234
1235 // When generating debug code, make sure the lexical context is set.
1236 if (emit_debug_code()) {
1237 cmp(scratch1, Immediate(0));
1238 Check(not_equal, kWeShouldNotHaveAnEmptyLexicalContext);
1239 }
1240 // Load the native context of the current context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001241 mov(scratch1, ContextOperand(scratch1, Context::NATIVE_CONTEXT_INDEX));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001242
1243 // Check the context is a native context.
1244 if (emit_debug_code()) {
1245 // Read the first word and compare to native_context_map.
1246 cmp(FieldOperand(scratch1, HeapObject::kMapOffset),
1247 isolate()->factory()->native_context_map());
1248 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
1249 }
1250
1251 // Check if both contexts are the same.
1252 cmp(scratch1, FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
1253 j(equal, &same_contexts);
1254
1255 // Compare security tokens, save holder_reg on the stack so we can use it
1256 // as a temporary register.
1257 //
1258 // Check that the security token in the calling global object is
1259 // compatible with the security token in the receiving global
1260 // object.
1261 mov(scratch2,
1262 FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
1263
1264 // Check the context is a native context.
1265 if (emit_debug_code()) {
1266 cmp(scratch2, isolate()->factory()->null_value());
1267 Check(not_equal, kJSGlobalProxyContextShouldNotBeNull);
1268
1269 // Read the first word and compare to native_context_map(),
1270 cmp(FieldOperand(scratch2, HeapObject::kMapOffset),
1271 isolate()->factory()->native_context_map());
1272 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
1273 }
1274
1275 int token_offset = Context::kHeaderSize +
1276 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1277 mov(scratch1, FieldOperand(scratch1, token_offset));
1278 cmp(scratch1, FieldOperand(scratch2, token_offset));
1279 j(not_equal, miss);
1280
1281 bind(&same_contexts);
1282}
1283
1284
1285// Compute the hash code from the untagged key. This must be kept in sync with
1286// ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
1287// code-stub-hydrogen.cc
1288//
1289// Note: r0 will contain hash code
1290void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
1291 // Xor original key with a seed.
1292 if (serializer_enabled()) {
1293 ExternalReference roots_array_start =
1294 ExternalReference::roots_array_start(isolate());
1295 mov(scratch, Immediate(Heap::kHashSeedRootIndex));
1296 mov(scratch,
1297 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
1298 SmiUntag(scratch);
1299 xor_(r0, scratch);
1300 } else {
1301 int32_t seed = isolate()->heap()->HashSeed();
1302 xor_(r0, Immediate(seed));
1303 }
1304
1305 // hash = ~hash + (hash << 15);
1306 mov(scratch, r0);
1307 not_(r0);
1308 shl(scratch, 15);
1309 add(r0, scratch);
1310 // hash = hash ^ (hash >> 12);
1311 mov(scratch, r0);
1312 shr(scratch, 12);
1313 xor_(r0, scratch);
1314 // hash = hash + (hash << 2);
1315 lea(r0, Operand(r0, r0, times_4, 0));
1316 // hash = hash ^ (hash >> 4);
1317 mov(scratch, r0);
1318 shr(scratch, 4);
1319 xor_(r0, scratch);
1320 // hash = hash * 2057;
1321 imul(r0, r0, 2057);
1322 // hash = hash ^ (hash >> 16);
1323 mov(scratch, r0);
1324 shr(scratch, 16);
1325 xor_(r0, scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001326 and_(r0, 0x3fffffff);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001327}
1328
1329
1330
1331void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1332 Register elements,
1333 Register key,
1334 Register r0,
1335 Register r1,
1336 Register r2,
1337 Register result) {
1338 // Register use:
1339 //
1340 // elements - holds the slow-case elements of the receiver and is unchanged.
1341 //
1342 // key - holds the smi key on entry and is unchanged.
1343 //
1344 // Scratch registers:
1345 //
1346 // r0 - holds the untagged key on entry and holds the hash once computed.
1347 //
1348 // r1 - used to hold the capacity mask of the dictionary
1349 //
1350 // r2 - used for the index into the dictionary.
1351 //
1352 // result - holds the result on exit if the load succeeds and we fall through.
1353
1354 Label done;
1355
1356 GetNumberHash(r0, r1);
1357
1358 // Compute capacity mask.
1359 mov(r1, FieldOperand(elements, SeededNumberDictionary::kCapacityOffset));
1360 shr(r1, kSmiTagSize); // convert smi to int
1361 dec(r1);
1362
1363 // Generate an unrolled loop that performs a few probes before giving up.
1364 for (int i = 0; i < kNumberDictionaryProbes; i++) {
1365 // Use r2 for index calculations and keep the hash intact in r0.
1366 mov(r2, r0);
1367 // Compute the masked index: (hash + i + i * i) & mask.
1368 if (i > 0) {
1369 add(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
1370 }
1371 and_(r2, r1);
1372
1373 // Scale the index by multiplying by the entry size.
1374 DCHECK(SeededNumberDictionary::kEntrySize == 3);
1375 lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
1376
1377 // Check if the key matches.
1378 cmp(key, FieldOperand(elements,
1379 r2,
1380 times_pointer_size,
1381 SeededNumberDictionary::kElementsStartOffset));
1382 if (i != (kNumberDictionaryProbes - 1)) {
1383 j(equal, &done);
1384 } else {
1385 j(not_equal, miss);
1386 }
1387 }
1388
1389 bind(&done);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001390 // Check that the value is a field property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391 const int kDetailsOffset =
1392 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001393 DCHECK_EQ(DATA, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
1395 Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
1396 j(not_zero, miss);
1397
1398 // Get the value at the masked, scaled index.
1399 const int kValueOffset =
1400 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
1401 mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
1402}
1403
1404
1405void MacroAssembler::LoadAllocationTopHelper(Register result,
1406 Register scratch,
1407 AllocationFlags flags) {
1408 ExternalReference allocation_top =
1409 AllocationUtils::GetAllocationTopReference(isolate(), flags);
1410
1411 // Just return if allocation top is already known.
1412 if ((flags & RESULT_CONTAINS_TOP) != 0) {
1413 // No use of scratch if allocation top is provided.
1414 DCHECK(scratch.is(no_reg));
1415#ifdef DEBUG
1416 // Assert that result actually contains top on entry.
1417 cmp(result, Operand::StaticVariable(allocation_top));
1418 Check(equal, kUnexpectedAllocationTop);
1419#endif
1420 return;
1421 }
1422
1423 // Move address of new object to result. Use scratch register if available.
1424 if (scratch.is(no_reg)) {
1425 mov(result, Operand::StaticVariable(allocation_top));
1426 } else {
1427 mov(scratch, Immediate(allocation_top));
1428 mov(result, Operand(scratch, 0));
1429 }
1430}
1431
1432
1433void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
1434 Register scratch,
1435 AllocationFlags flags) {
1436 if (emit_debug_code()) {
1437 test(result_end, Immediate(kObjectAlignmentMask));
1438 Check(zero, kUnalignedAllocationInNewSpace);
1439 }
1440
1441 ExternalReference allocation_top =
1442 AllocationUtils::GetAllocationTopReference(isolate(), flags);
1443
1444 // Update new top. Use scratch if available.
1445 if (scratch.is(no_reg)) {
1446 mov(Operand::StaticVariable(allocation_top), result_end);
1447 } else {
1448 mov(Operand(scratch, 0), result_end);
1449 }
1450}
1451
1452
1453void MacroAssembler::Allocate(int object_size,
1454 Register result,
1455 Register result_end,
1456 Register scratch,
1457 Label* gc_required,
1458 AllocationFlags flags) {
1459 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
1460 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
1461 if (!FLAG_inline_new) {
1462 if (emit_debug_code()) {
1463 // Trash the registers to simulate an allocation failure.
1464 mov(result, Immediate(0x7091));
1465 if (result_end.is_valid()) {
1466 mov(result_end, Immediate(0x7191));
1467 }
1468 if (scratch.is_valid()) {
1469 mov(scratch, Immediate(0x7291));
1470 }
1471 }
1472 jmp(gc_required);
1473 return;
1474 }
1475 DCHECK(!result.is(result_end));
1476
1477 // Load address of new object into result.
1478 LoadAllocationTopHelper(result, scratch, flags);
1479
1480 ExternalReference allocation_limit =
1481 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1482
1483 // Align the next allocation. Storing the filler map without checking top is
1484 // safe in new-space because the limit of the heap is aligned there.
1485 if ((flags & DOUBLE_ALIGNMENT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1487 Label aligned;
1488 test(result, Immediate(kDoubleAlignmentMask));
1489 j(zero, &aligned, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001490 if ((flags & PRETENURE) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001491 cmp(result, Operand::StaticVariable(allocation_limit));
1492 j(above_equal, gc_required);
1493 }
1494 mov(Operand(result, 0),
1495 Immediate(isolate()->factory()->one_pointer_filler_map()));
1496 add(result, Immediate(kDoubleSize / 2));
1497 bind(&aligned);
1498 }
1499
1500 // Calculate new top and bail out if space is exhausted.
1501 Register top_reg = result_end.is_valid() ? result_end : result;
1502 if (!top_reg.is(result)) {
1503 mov(top_reg, result);
1504 }
1505 add(top_reg, Immediate(object_size));
1506 j(carry, gc_required);
1507 cmp(top_reg, Operand::StaticVariable(allocation_limit));
1508 j(above, gc_required);
1509
1510 // Update allocation top.
1511 UpdateAllocationTopHelper(top_reg, scratch, flags);
1512
1513 // Tag result if requested.
1514 bool tag_result = (flags & TAG_OBJECT) != 0;
1515 if (top_reg.is(result)) {
1516 if (tag_result) {
1517 sub(result, Immediate(object_size - kHeapObjectTag));
1518 } else {
1519 sub(result, Immediate(object_size));
1520 }
1521 } else if (tag_result) {
1522 DCHECK(kHeapObjectTag == 1);
1523 inc(result);
1524 }
1525}
1526
1527
1528void MacroAssembler::Allocate(int header_size,
1529 ScaleFactor element_size,
1530 Register element_count,
1531 RegisterValueType element_count_type,
1532 Register result,
1533 Register result_end,
1534 Register scratch,
1535 Label* gc_required,
1536 AllocationFlags flags) {
1537 DCHECK((flags & SIZE_IN_WORDS) == 0);
1538 if (!FLAG_inline_new) {
1539 if (emit_debug_code()) {
1540 // Trash the registers to simulate an allocation failure.
1541 mov(result, Immediate(0x7091));
1542 mov(result_end, Immediate(0x7191));
1543 if (scratch.is_valid()) {
1544 mov(scratch, Immediate(0x7291));
1545 }
1546 // Register element_count is not modified by the function.
1547 }
1548 jmp(gc_required);
1549 return;
1550 }
1551 DCHECK(!result.is(result_end));
1552
1553 // Load address of new object into result.
1554 LoadAllocationTopHelper(result, scratch, flags);
1555
1556 ExternalReference allocation_limit =
1557 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1558
1559 // Align the next allocation. Storing the filler map without checking top is
1560 // safe in new-space because the limit of the heap is aligned there.
1561 if ((flags & DOUBLE_ALIGNMENT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001562 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1563 Label aligned;
1564 test(result, Immediate(kDoubleAlignmentMask));
1565 j(zero, &aligned, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001566 if ((flags & PRETENURE) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001567 cmp(result, Operand::StaticVariable(allocation_limit));
1568 j(above_equal, gc_required);
1569 }
1570 mov(Operand(result, 0),
1571 Immediate(isolate()->factory()->one_pointer_filler_map()));
1572 add(result, Immediate(kDoubleSize / 2));
1573 bind(&aligned);
1574 }
1575
1576 // Calculate new top and bail out if space is exhausted.
1577 // We assume that element_count*element_size + header_size does not
1578 // overflow.
1579 if (element_count_type == REGISTER_VALUE_IS_SMI) {
1580 STATIC_ASSERT(static_cast<ScaleFactor>(times_2 - 1) == times_1);
1581 STATIC_ASSERT(static_cast<ScaleFactor>(times_4 - 1) == times_2);
1582 STATIC_ASSERT(static_cast<ScaleFactor>(times_8 - 1) == times_4);
1583 DCHECK(element_size >= times_2);
1584 DCHECK(kSmiTagSize == 1);
1585 element_size = static_cast<ScaleFactor>(element_size - 1);
1586 } else {
1587 DCHECK(element_count_type == REGISTER_VALUE_IS_INT32);
1588 }
1589 lea(result_end, Operand(element_count, element_size, header_size));
1590 add(result_end, result);
1591 j(carry, gc_required);
1592 cmp(result_end, Operand::StaticVariable(allocation_limit));
1593 j(above, gc_required);
1594
1595 if ((flags & TAG_OBJECT) != 0) {
1596 DCHECK(kHeapObjectTag == 1);
1597 inc(result);
1598 }
1599
1600 // Update allocation top.
1601 UpdateAllocationTopHelper(result_end, scratch, flags);
1602}
1603
1604
1605void MacroAssembler::Allocate(Register object_size,
1606 Register result,
1607 Register result_end,
1608 Register scratch,
1609 Label* gc_required,
1610 AllocationFlags flags) {
1611 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
1612 if (!FLAG_inline_new) {
1613 if (emit_debug_code()) {
1614 // Trash the registers to simulate an allocation failure.
1615 mov(result, Immediate(0x7091));
1616 mov(result_end, Immediate(0x7191));
1617 if (scratch.is_valid()) {
1618 mov(scratch, Immediate(0x7291));
1619 }
1620 // object_size is left unchanged by this function.
1621 }
1622 jmp(gc_required);
1623 return;
1624 }
1625 DCHECK(!result.is(result_end));
1626
1627 // Load address of new object into result.
1628 LoadAllocationTopHelper(result, scratch, flags);
1629
1630 ExternalReference allocation_limit =
1631 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1632
1633 // Align the next allocation. Storing the filler map without checking top is
1634 // safe in new-space because the limit of the heap is aligned there.
1635 if ((flags & DOUBLE_ALIGNMENT) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001636 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
1637 Label aligned;
1638 test(result, Immediate(kDoubleAlignmentMask));
1639 j(zero, &aligned, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001640 if ((flags & PRETENURE) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001641 cmp(result, Operand::StaticVariable(allocation_limit));
1642 j(above_equal, gc_required);
1643 }
1644 mov(Operand(result, 0),
1645 Immediate(isolate()->factory()->one_pointer_filler_map()));
1646 add(result, Immediate(kDoubleSize / 2));
1647 bind(&aligned);
1648 }
1649
1650 // Calculate new top and bail out if space is exhausted.
1651 if (!object_size.is(result_end)) {
1652 mov(result_end, object_size);
1653 }
1654 add(result_end, result);
1655 j(carry, gc_required);
1656 cmp(result_end, Operand::StaticVariable(allocation_limit));
1657 j(above, gc_required);
1658
1659 // Tag result if requested.
1660 if ((flags & TAG_OBJECT) != 0) {
1661 DCHECK(kHeapObjectTag == 1);
1662 inc(result);
1663 }
1664
1665 // Update allocation top.
1666 UpdateAllocationTopHelper(result_end, scratch, flags);
1667}
1668
1669
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001670void MacroAssembler::AllocateHeapNumber(Register result,
1671 Register scratch1,
1672 Register scratch2,
1673 Label* gc_required,
1674 MutableMode mode) {
1675 // Allocate heap number in new space.
1676 Allocate(HeapNumber::kSize, result, scratch1, scratch2, gc_required,
1677 TAG_OBJECT);
1678
1679 Handle<Map> map = mode == MUTABLE
1680 ? isolate()->factory()->mutable_heap_number_map()
1681 : isolate()->factory()->heap_number_map();
1682
1683 // Set the map.
1684 mov(FieldOperand(result, HeapObject::kMapOffset), Immediate(map));
1685}
1686
1687
1688void MacroAssembler::AllocateTwoByteString(Register result,
1689 Register length,
1690 Register scratch1,
1691 Register scratch2,
1692 Register scratch3,
1693 Label* gc_required) {
1694 // Calculate the number of bytes needed for the characters in the string while
1695 // observing object alignment.
1696 DCHECK((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1697 DCHECK(kShortSize == 2);
1698 // scratch1 = length * 2 + kObjectAlignmentMask.
1699 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
1700 and_(scratch1, Immediate(~kObjectAlignmentMask));
1701
1702 // Allocate two byte string in new space.
1703 Allocate(SeqTwoByteString::kHeaderSize,
1704 times_1,
1705 scratch1,
1706 REGISTER_VALUE_IS_INT32,
1707 result,
1708 scratch2,
1709 scratch3,
1710 gc_required,
1711 TAG_OBJECT);
1712
1713 // Set the map, length and hash field.
1714 mov(FieldOperand(result, HeapObject::kMapOffset),
1715 Immediate(isolate()->factory()->string_map()));
1716 mov(scratch1, length);
1717 SmiTag(scratch1);
1718 mov(FieldOperand(result, String::kLengthOffset), scratch1);
1719 mov(FieldOperand(result, String::kHashFieldOffset),
1720 Immediate(String::kEmptyHashField));
1721}
1722
1723
1724void MacroAssembler::AllocateOneByteString(Register result, Register length,
1725 Register scratch1, Register scratch2,
1726 Register scratch3,
1727 Label* gc_required) {
1728 // Calculate the number of bytes needed for the characters in the string while
1729 // observing object alignment.
1730 DCHECK((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1731 mov(scratch1, length);
1732 DCHECK(kCharSize == 1);
1733 add(scratch1, Immediate(kObjectAlignmentMask));
1734 and_(scratch1, Immediate(~kObjectAlignmentMask));
1735
1736 // Allocate one-byte string in new space.
1737 Allocate(SeqOneByteString::kHeaderSize,
1738 times_1,
1739 scratch1,
1740 REGISTER_VALUE_IS_INT32,
1741 result,
1742 scratch2,
1743 scratch3,
1744 gc_required,
1745 TAG_OBJECT);
1746
1747 // Set the map, length and hash field.
1748 mov(FieldOperand(result, HeapObject::kMapOffset),
1749 Immediate(isolate()->factory()->one_byte_string_map()));
1750 mov(scratch1, length);
1751 SmiTag(scratch1);
1752 mov(FieldOperand(result, String::kLengthOffset), scratch1);
1753 mov(FieldOperand(result, String::kHashFieldOffset),
1754 Immediate(String::kEmptyHashField));
1755}
1756
1757
1758void MacroAssembler::AllocateOneByteString(Register result, int length,
1759 Register scratch1, Register scratch2,
1760 Label* gc_required) {
1761 DCHECK(length > 0);
1762
1763 // Allocate one-byte string in new space.
1764 Allocate(SeqOneByteString::SizeFor(length), result, scratch1, scratch2,
1765 gc_required, TAG_OBJECT);
1766
1767 // Set the map, length and hash field.
1768 mov(FieldOperand(result, HeapObject::kMapOffset),
1769 Immediate(isolate()->factory()->one_byte_string_map()));
1770 mov(FieldOperand(result, String::kLengthOffset),
1771 Immediate(Smi::FromInt(length)));
1772 mov(FieldOperand(result, String::kHashFieldOffset),
1773 Immediate(String::kEmptyHashField));
1774}
1775
1776
1777void MacroAssembler::AllocateTwoByteConsString(Register result,
1778 Register scratch1,
1779 Register scratch2,
1780 Label* gc_required) {
1781 // Allocate heap number in new space.
1782 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
1783 TAG_OBJECT);
1784
1785 // Set the map. The other fields are left uninitialized.
1786 mov(FieldOperand(result, HeapObject::kMapOffset),
1787 Immediate(isolate()->factory()->cons_string_map()));
1788}
1789
1790
1791void MacroAssembler::AllocateOneByteConsString(Register result,
1792 Register scratch1,
1793 Register scratch2,
1794 Label* gc_required) {
1795 Allocate(ConsString::kSize,
1796 result,
1797 scratch1,
1798 scratch2,
1799 gc_required,
1800 TAG_OBJECT);
1801
1802 // Set the map. The other fields are left uninitialized.
1803 mov(FieldOperand(result, HeapObject::kMapOffset),
1804 Immediate(isolate()->factory()->cons_one_byte_string_map()));
1805}
1806
1807
1808void MacroAssembler::AllocateTwoByteSlicedString(Register result,
1809 Register scratch1,
1810 Register scratch2,
1811 Label* gc_required) {
1812 // Allocate heap number in new space.
1813 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1814 TAG_OBJECT);
1815
1816 // Set the map. The other fields are left uninitialized.
1817 mov(FieldOperand(result, HeapObject::kMapOffset),
1818 Immediate(isolate()->factory()->sliced_string_map()));
1819}
1820
1821
1822void MacroAssembler::AllocateOneByteSlicedString(Register result,
1823 Register scratch1,
1824 Register scratch2,
1825 Label* gc_required) {
1826 // Allocate heap number in new space.
1827 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1828 TAG_OBJECT);
1829
1830 // Set the map. The other fields are left uninitialized.
1831 mov(FieldOperand(result, HeapObject::kMapOffset),
1832 Immediate(isolate()->factory()->sliced_one_byte_string_map()));
1833}
1834
1835
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001836void MacroAssembler::AllocateJSValue(Register result, Register constructor,
1837 Register value, Register scratch,
1838 Label* gc_required) {
1839 DCHECK(!result.is(constructor));
1840 DCHECK(!result.is(scratch));
1841 DCHECK(!result.is(value));
1842
1843 // Allocate JSValue in new space.
1844 Allocate(JSValue::kSize, result, scratch, no_reg, gc_required, TAG_OBJECT);
1845
1846 // Initialize the JSValue.
1847 LoadGlobalFunctionInitialMap(constructor, scratch);
1848 mov(FieldOperand(result, HeapObject::kMapOffset), scratch);
1849 LoadRoot(scratch, Heap::kEmptyFixedArrayRootIndex);
1850 mov(FieldOperand(result, JSObject::kPropertiesOffset), scratch);
1851 mov(FieldOperand(result, JSObject::kElementsOffset), scratch);
1852 mov(FieldOperand(result, JSValue::kValueOffset), value);
1853 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
1854}
1855
1856
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001857// Copy memory, byte-by-byte, from source to destination. Not optimized for
1858// long or aligned copies. The contents of scratch and length are destroyed.
1859// Source and destination are incremented by length.
1860// Many variants of movsb, loop unrolling, word moves, and indexed operands
1861// have been tried here already, and this is fastest.
1862// A simpler loop is faster on small copies, but 30% slower on large ones.
1863// The cld() instruction must have been emitted, to set the direction flag(),
1864// before calling this function.
1865void MacroAssembler::CopyBytes(Register source,
1866 Register destination,
1867 Register length,
1868 Register scratch) {
1869 Label short_loop, len4, len8, len12, done, short_string;
1870 DCHECK(source.is(esi));
1871 DCHECK(destination.is(edi));
1872 DCHECK(length.is(ecx));
1873 cmp(length, Immediate(4));
1874 j(below, &short_string, Label::kNear);
1875
1876 // Because source is 4-byte aligned in our uses of this function,
1877 // we keep source aligned for the rep_movs call by copying the odd bytes
1878 // at the end of the ranges.
1879 mov(scratch, Operand(source, length, times_1, -4));
1880 mov(Operand(destination, length, times_1, -4), scratch);
1881
1882 cmp(length, Immediate(8));
1883 j(below_equal, &len4, Label::kNear);
1884 cmp(length, Immediate(12));
1885 j(below_equal, &len8, Label::kNear);
1886 cmp(length, Immediate(16));
1887 j(below_equal, &len12, Label::kNear);
1888
1889 mov(scratch, ecx);
1890 shr(ecx, 2);
1891 rep_movs();
1892 and_(scratch, Immediate(0x3));
1893 add(destination, scratch);
1894 jmp(&done, Label::kNear);
1895
1896 bind(&len12);
1897 mov(scratch, Operand(source, 8));
1898 mov(Operand(destination, 8), scratch);
1899 bind(&len8);
1900 mov(scratch, Operand(source, 4));
1901 mov(Operand(destination, 4), scratch);
1902 bind(&len4);
1903 mov(scratch, Operand(source, 0));
1904 mov(Operand(destination, 0), scratch);
1905 add(destination, length);
1906 jmp(&done, Label::kNear);
1907
1908 bind(&short_string);
1909 test(length, length);
1910 j(zero, &done, Label::kNear);
1911
1912 bind(&short_loop);
1913 mov_b(scratch, Operand(source, 0));
1914 mov_b(Operand(destination, 0), scratch);
1915 inc(source);
1916 inc(destination);
1917 dec(length);
1918 j(not_zero, &short_loop);
1919
1920 bind(&done);
1921}
1922
1923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001924void MacroAssembler::InitializeFieldsWithFiller(Register current_address,
1925 Register end_address,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001926 Register filler) {
1927 Label loop, entry;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001928 jmp(&entry, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001929 bind(&loop);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001930 mov(Operand(current_address, 0), filler);
1931 add(current_address, Immediate(kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001932 bind(&entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001933 cmp(current_address, end_address);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001934 j(below, &loop, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001935}
1936
1937
1938void MacroAssembler::BooleanBitTest(Register object,
1939 int field_offset,
1940 int bit_index) {
1941 bit_index += kSmiTagSize + kSmiShiftSize;
1942 DCHECK(base::bits::IsPowerOfTwo32(kBitsPerByte));
1943 int byte_index = bit_index / kBitsPerByte;
1944 int byte_bit_index = bit_index & (kBitsPerByte - 1);
1945 test_b(FieldOperand(object, field_offset + byte_index),
Ben Murdochda12d292016-06-02 14:46:10 +01001946 Immediate(1 << byte_bit_index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001947}
1948
1949
1950
1951void MacroAssembler::NegativeZeroTest(Register result,
1952 Register op,
1953 Label* then_label) {
1954 Label ok;
1955 test(result, result);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001956 j(not_zero, &ok, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001957 test(op, op);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001958 j(sign, then_label, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001959 bind(&ok);
1960}
1961
1962
1963void MacroAssembler::NegativeZeroTest(Register result,
1964 Register op1,
1965 Register op2,
1966 Register scratch,
1967 Label* then_label) {
1968 Label ok;
1969 test(result, result);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001970 j(not_zero, &ok, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001971 mov(scratch, op1);
1972 or_(scratch, op2);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001973 j(sign, then_label, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001974 bind(&ok);
1975}
1976
1977
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001978void MacroAssembler::GetMapConstructor(Register result, Register map,
1979 Register temp) {
1980 Label done, loop;
1981 mov(result, FieldOperand(map, Map::kConstructorOrBackPointerOffset));
1982 bind(&loop);
1983 JumpIfSmi(result, &done, Label::kNear);
1984 CmpObjectType(result, MAP_TYPE, temp);
1985 j(not_equal, &done, Label::kNear);
1986 mov(result, FieldOperand(result, Map::kConstructorOrBackPointerOffset));
1987 jmp(&loop);
1988 bind(&done);
1989}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001990
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001991
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001992void MacroAssembler::TryGetFunctionPrototype(Register function, Register result,
1993 Register scratch, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001994 // Get the prototype or initial map from the function.
1995 mov(result,
1996 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1997
1998 // If the prototype or initial map is the hole, don't return it and
1999 // simply miss the cache instead. This will allow us to allocate a
2000 // prototype object on-demand in the runtime system.
2001 cmp(result, Immediate(isolate()->factory()->the_hole_value()));
2002 j(equal, miss);
2003
2004 // If the function does not have an initial map, we're done.
2005 Label done;
2006 CmpObjectType(result, MAP_TYPE, scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007 j(not_equal, &done, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002008
2009 // Get the prototype from the initial map.
2010 mov(result, FieldOperand(result, Map::kPrototypeOffset));
2011
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002012 // All done.
2013 bind(&done);
2014}
2015
2016
2017void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
2018 DCHECK(AllowThisStubCall(stub)); // Calls are not allowed in some stubs.
2019 call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
2020}
2021
2022
2023void MacroAssembler::TailCallStub(CodeStub* stub) {
2024 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
2025}
2026
2027
2028void MacroAssembler::StubReturn(int argc) {
2029 DCHECK(argc >= 1 && generating_stub());
2030 ret((argc - 1) * kPointerSize);
2031}
2032
2033
2034bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
2035 return has_frame_ || !stub->SometimesSetsUpAFrame();
2036}
2037
2038
2039void MacroAssembler::IndexFromHash(Register hash, Register index) {
2040 // The assert checks that the constants for the maximum number of digits
2041 // for an array index cached in the hash field and the number of bits
2042 // reserved for it does not conflict.
2043 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
2044 (1 << String::kArrayIndexValueBits));
2045 if (!index.is(hash)) {
2046 mov(index, hash);
2047 }
2048 DecodeFieldToSmi<String::ArrayIndexValueBits>(index);
2049}
2050
2051
2052void MacroAssembler::CallRuntime(const Runtime::Function* f, int num_arguments,
2053 SaveFPRegsMode save_doubles) {
2054 // If the expected number of arguments of the runtime function is
2055 // constant, we check that the actual number of arguments match the
2056 // expectation.
2057 CHECK(f->nargs < 0 || f->nargs == num_arguments);
2058
2059 // TODO(1236192): Most runtime routines don't need the number of
2060 // arguments passed in because it is constant. At some point we
2061 // should remove this need and make the runtime routine entry code
2062 // smarter.
2063 Move(eax, Immediate(num_arguments));
2064 mov(ebx, Immediate(ExternalReference(f, isolate())));
2065 CEntryStub ces(isolate(), 1, save_doubles);
2066 CallStub(&ces);
2067}
2068
2069
2070void MacroAssembler::CallExternalReference(ExternalReference ref,
2071 int num_arguments) {
2072 mov(eax, Immediate(num_arguments));
2073 mov(ebx, Immediate(ref));
2074
2075 CEntryStub stub(isolate(), 1);
2076 CallStub(&stub);
2077}
2078
2079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002080void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid) {
2081 // ----------- S t a t e -------------
2082 // -- esp[0] : return address
2083 // -- esp[8] : argument num_arguments - 1
2084 // ...
2085 // -- esp[8 * num_arguments] : argument 0 (receiver)
2086 //
2087 // For runtime functions with variable arguments:
2088 // -- eax : number of arguments
2089 // -----------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002090
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002091 const Runtime::Function* function = Runtime::FunctionForId(fid);
2092 DCHECK_EQ(1, function->result_size);
2093 if (function->nargs >= 0) {
2094 // TODO(1236192): Most runtime routines don't need the number of
2095 // arguments passed in because it is constant. At some point we
2096 // should remove this need and make the runtime routine entry code
2097 // smarter.
2098 mov(eax, Immediate(function->nargs));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002099 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002100 JumpToExternalReference(ExternalReference(fid, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002101}
2102
2103
2104void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
2105 // Set the entry point and jump to the C entry runtime stub.
2106 mov(ebx, Immediate(ext));
2107 CEntryStub ces(isolate(), 1);
2108 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
2109}
2110
Ben Murdochda12d292016-06-02 14:46:10 +01002111void MacroAssembler::PrepareForTailCall(
2112 const ParameterCount& callee_args_count, Register caller_args_count_reg,
2113 Register scratch0, Register scratch1, ReturnAddressState ra_state,
2114 int number_of_temp_values_after_return_address) {
2115#if DEBUG
2116 if (callee_args_count.is_reg()) {
2117 DCHECK(!AreAliased(callee_args_count.reg(), caller_args_count_reg, scratch0,
2118 scratch1));
2119 } else {
2120 DCHECK(!AreAliased(caller_args_count_reg, scratch0, scratch1));
2121 }
2122 DCHECK(ra_state != ReturnAddressState::kNotOnStack ||
2123 number_of_temp_values_after_return_address == 0);
2124#endif
2125
2126 // Calculate the destination address where we will put the return address
2127 // after we drop current frame.
2128 Register new_sp_reg = scratch0;
2129 if (callee_args_count.is_reg()) {
2130 sub(caller_args_count_reg, callee_args_count.reg());
2131 lea(new_sp_reg,
2132 Operand(ebp, caller_args_count_reg, times_pointer_size,
2133 StandardFrameConstants::kCallerPCOffset -
2134 number_of_temp_values_after_return_address * kPointerSize));
2135 } else {
2136 lea(new_sp_reg, Operand(ebp, caller_args_count_reg, times_pointer_size,
2137 StandardFrameConstants::kCallerPCOffset -
2138 (callee_args_count.immediate() +
2139 number_of_temp_values_after_return_address) *
2140 kPointerSize));
2141 }
2142
2143 if (FLAG_debug_code) {
2144 cmp(esp, new_sp_reg);
2145 Check(below, kStackAccessBelowStackPointer);
2146 }
2147
2148 // Copy return address from caller's frame to current frame's return address
2149 // to avoid its trashing and let the following loop copy it to the right
2150 // place.
2151 Register tmp_reg = scratch1;
2152 if (ra_state == ReturnAddressState::kOnStack) {
2153 mov(tmp_reg, Operand(ebp, StandardFrameConstants::kCallerPCOffset));
2154 mov(Operand(esp, number_of_temp_values_after_return_address * kPointerSize),
2155 tmp_reg);
2156 } else {
2157 DCHECK(ReturnAddressState::kNotOnStack == ra_state);
2158 DCHECK_EQ(0, number_of_temp_values_after_return_address);
2159 Push(Operand(ebp, StandardFrameConstants::kCallerPCOffset));
2160 }
2161
2162 // Restore caller's frame pointer now as it could be overwritten by
2163 // the copying loop.
2164 mov(ebp, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2165
2166 // +2 here is to copy both receiver and return address.
2167 Register count_reg = caller_args_count_reg;
2168 if (callee_args_count.is_reg()) {
2169 lea(count_reg, Operand(callee_args_count.reg(),
2170 2 + number_of_temp_values_after_return_address));
2171 } else {
2172 mov(count_reg, Immediate(callee_args_count.immediate() + 2 +
2173 number_of_temp_values_after_return_address));
2174 // TODO(ishell): Unroll copying loop for small immediate values.
2175 }
2176
2177 // Now copy callee arguments to the caller frame going backwards to avoid
2178 // callee arguments corruption (source and destination areas could overlap).
2179 Label loop, entry;
2180 jmp(&entry, Label::kNear);
2181 bind(&loop);
2182 dec(count_reg);
2183 mov(tmp_reg, Operand(esp, count_reg, times_pointer_size, 0));
2184 mov(Operand(new_sp_reg, count_reg, times_pointer_size, 0), tmp_reg);
2185 bind(&entry);
2186 cmp(count_reg, Immediate(0));
2187 j(not_equal, &loop, Label::kNear);
2188
2189 // Leave current frame.
2190 mov(esp, new_sp_reg);
2191}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002192
2193void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2194 const ParameterCount& actual,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002195 Label* done,
2196 bool* definitely_mismatches,
2197 InvokeFlag flag,
2198 Label::Distance done_near,
2199 const CallWrapper& call_wrapper) {
2200 bool definitely_matches = false;
2201 *definitely_mismatches = false;
2202 Label invoke;
2203 if (expected.is_immediate()) {
2204 DCHECK(actual.is_immediate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002205 mov(eax, actual.immediate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002206 if (expected.immediate() == actual.immediate()) {
2207 definitely_matches = true;
2208 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
2210 if (expected.immediate() == sentinel) {
2211 // Don't worry about adapting arguments for builtins that
2212 // don't want that done. Skip adaption code by making it look
2213 // like we have a match between expected and actual number of
2214 // arguments.
2215 definitely_matches = true;
2216 } else {
2217 *definitely_mismatches = true;
2218 mov(ebx, expected.immediate());
2219 }
2220 }
2221 } else {
2222 if (actual.is_immediate()) {
2223 // Expected is in register, actual is immediate. This is the
2224 // case when we invoke function values without going through the
2225 // IC mechanism.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002226 mov(eax, actual.immediate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002227 cmp(expected.reg(), actual.immediate());
2228 j(equal, &invoke);
2229 DCHECK(expected.reg().is(ebx));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002230 } else if (!expected.reg().is(actual.reg())) {
2231 // Both expected and actual are in (different) registers. This
2232 // is the case when we invoke functions using call and apply.
2233 cmp(expected.reg(), actual.reg());
2234 j(equal, &invoke);
2235 DCHECK(actual.reg().is(eax));
2236 DCHECK(expected.reg().is(ebx));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002237 } else {
2238 Move(eax, actual.reg());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002239 }
2240 }
2241
2242 if (!definitely_matches) {
2243 Handle<Code> adaptor =
2244 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002245 if (flag == CALL_FUNCTION) {
2246 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
2247 call(adaptor, RelocInfo::CODE_TARGET);
2248 call_wrapper.AfterCall();
2249 if (!*definitely_mismatches) {
2250 jmp(done, done_near);
2251 }
2252 } else {
2253 jmp(adaptor, RelocInfo::CODE_TARGET);
2254 }
2255 bind(&invoke);
2256 }
2257}
2258
2259
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002260void MacroAssembler::FloodFunctionIfStepping(Register fun, Register new_target,
2261 const ParameterCount& expected,
2262 const ParameterCount& actual) {
2263 Label skip_flooding;
2264 ExternalReference step_in_enabled =
2265 ExternalReference::debug_step_in_enabled_address(isolate());
Ben Murdochda12d292016-06-02 14:46:10 +01002266 cmpb(Operand::StaticVariable(step_in_enabled), Immediate(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002267 j(equal, &skip_flooding);
2268 {
2269 FrameScope frame(this,
2270 has_frame() ? StackFrame::NONE : StackFrame::INTERNAL);
2271 if (expected.is_reg()) {
2272 SmiTag(expected.reg());
2273 Push(expected.reg());
2274 }
2275 if (actual.is_reg()) {
2276 SmiTag(actual.reg());
2277 Push(actual.reg());
2278 }
2279 if (new_target.is_valid()) {
2280 Push(new_target);
2281 }
2282 Push(fun);
2283 Push(fun);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002284 CallRuntime(Runtime::kDebugPrepareStepInIfStepping);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002285 Pop(fun);
2286 if (new_target.is_valid()) {
2287 Pop(new_target);
2288 }
2289 if (actual.is_reg()) {
2290 Pop(actual.reg());
2291 SmiUntag(actual.reg());
2292 }
2293 if (expected.is_reg()) {
2294 Pop(expected.reg());
2295 SmiUntag(expected.reg());
2296 }
2297 }
2298 bind(&skip_flooding);
2299}
2300
2301
2302void MacroAssembler::InvokeFunctionCode(Register function, Register new_target,
2303 const ParameterCount& expected,
2304 const ParameterCount& actual,
2305 InvokeFlag flag,
2306 const CallWrapper& call_wrapper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002307 // You can't call a function without a valid frame.
2308 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002309 DCHECK(function.is(edi));
2310 DCHECK_IMPLIES(new_target.is_valid(), new_target.is(edx));
2311
2312 if (call_wrapper.NeedsDebugStepCheck()) {
2313 FloodFunctionIfStepping(function, new_target, expected, actual);
2314 }
2315
2316 // Clear the new.target register if not given.
2317 if (!new_target.is_valid()) {
2318 mov(edx, isolate()->factory()->undefined_value());
2319 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002320
2321 Label done;
2322 bool definitely_mismatches = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002323 InvokePrologue(expected, actual, &done, &definitely_mismatches, flag,
2324 Label::kNear, call_wrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002325 if (!definitely_mismatches) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002326 // We call indirectly through the code field in the function to
2327 // allow recompilation to take effect without changing any of the
2328 // call sites.
2329 Operand code = FieldOperand(function, JSFunction::kCodeEntryOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002330 if (flag == CALL_FUNCTION) {
2331 call_wrapper.BeforeCall(CallSize(code));
2332 call(code);
2333 call_wrapper.AfterCall();
2334 } else {
2335 DCHECK(flag == JUMP_FUNCTION);
2336 jmp(code);
2337 }
2338 bind(&done);
2339 }
2340}
2341
2342
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002343void MacroAssembler::InvokeFunction(Register fun, Register new_target,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002344 const ParameterCount& actual,
2345 InvokeFlag flag,
2346 const CallWrapper& call_wrapper) {
2347 // You can't call a function without a valid frame.
2348 DCHECK(flag == JUMP_FUNCTION || has_frame());
2349
2350 DCHECK(fun.is(edi));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002351 mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002352 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002353 mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002354 SmiUntag(ebx);
2355
2356 ParameterCount expected(ebx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002357 InvokeFunctionCode(edi, new_target, expected, actual, flag, call_wrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002358}
2359
2360
2361void MacroAssembler::InvokeFunction(Register fun,
2362 const ParameterCount& expected,
2363 const ParameterCount& actual,
2364 InvokeFlag flag,
2365 const CallWrapper& call_wrapper) {
2366 // You can't call a function without a valid frame.
2367 DCHECK(flag == JUMP_FUNCTION || has_frame());
2368
2369 DCHECK(fun.is(edi));
2370 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002372 InvokeFunctionCode(edi, no_reg, expected, actual, flag, call_wrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002373}
2374
2375
2376void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
2377 const ParameterCount& expected,
2378 const ParameterCount& actual,
2379 InvokeFlag flag,
2380 const CallWrapper& call_wrapper) {
2381 LoadHeapObject(edi, function);
2382 InvokeFunction(edi, expected, actual, flag, call_wrapper);
2383}
2384
2385
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002386void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2387 if (context_chain_length > 0) {
2388 // Move up the chain of contexts to the context containing the slot.
2389 mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2390 for (int i = 1; i < context_chain_length; i++) {
2391 mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2392 }
2393 } else {
2394 // Slot is in the current function context. Move it into the
2395 // destination register in case we store into it (the write barrier
2396 // cannot be allowed to destroy the context in esi).
2397 mov(dst, esi);
2398 }
2399
2400 // We should not have found a with context by walking the context chain
2401 // (i.e., the static scope chain and runtime context chain do not agree).
2402 // A variable occurring in such a scope should have slot type LOOKUP and
2403 // not CONTEXT.
2404 if (emit_debug_code()) {
2405 cmp(FieldOperand(dst, HeapObject::kMapOffset),
2406 isolate()->factory()->with_context_map());
2407 Check(not_equal, kVariableResolvedToWithContext);
2408 }
2409}
2410
2411
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002412void MacroAssembler::LoadGlobalProxy(Register dst) {
2413 mov(dst, NativeContextOperand());
2414 mov(dst, ContextOperand(dst, Context::GLOBAL_PROXY_INDEX));
2415}
2416
2417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002418void MacroAssembler::LoadTransitionedArrayMapConditional(
2419 ElementsKind expected_kind,
2420 ElementsKind transitioned_kind,
2421 Register map_in_out,
2422 Register scratch,
2423 Label* no_map_match) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002424 DCHECK(IsFastElementsKind(expected_kind));
2425 DCHECK(IsFastElementsKind(transitioned_kind));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002426
2427 // Check that the function's map is the same as the expected cached map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002428 mov(scratch, NativeContextOperand());
2429 cmp(map_in_out,
2430 ContextOperand(scratch, Context::ArrayMapIndex(expected_kind)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002431 j(not_equal, no_map_match);
2432
2433 // Use the transitioned cached map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002434 mov(map_in_out,
2435 ContextOperand(scratch, Context::ArrayMapIndex(transitioned_kind)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002436}
2437
2438
2439void MacroAssembler::LoadGlobalFunction(int index, Register function) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002440 // Load the native context from the current context.
2441 mov(function, NativeContextOperand());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002442 // Load the function from the native context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002443 mov(function, ContextOperand(function, index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002444}
2445
2446
2447void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2448 Register map) {
2449 // Load the initial map. The global functions all have initial maps.
2450 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2451 if (emit_debug_code()) {
2452 Label ok, fail;
2453 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
2454 jmp(&ok);
2455 bind(&fail);
2456 Abort(kGlobalFunctionsMustHaveInitialMap);
2457 bind(&ok);
2458 }
2459}
2460
2461
2462// Store the value in register src in the safepoint register stack
2463// slot for register dst.
2464void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
2465 mov(SafepointRegisterSlot(dst), src);
2466}
2467
2468
2469void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
2470 mov(SafepointRegisterSlot(dst), src);
2471}
2472
2473
2474void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
2475 mov(dst, SafepointRegisterSlot(src));
2476}
2477
2478
2479Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
2480 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
2481}
2482
2483
2484int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2485 // The registers are pushed starting with the lowest encoding,
2486 // which means that lowest encodings are furthest away from
2487 // the stack pointer.
2488 DCHECK(reg_code >= 0 && reg_code < kNumSafepointRegisters);
2489 return kNumSafepointRegisters - reg_code - 1;
2490}
2491
2492
2493void MacroAssembler::LoadHeapObject(Register result,
2494 Handle<HeapObject> object) {
2495 AllowDeferredHandleDereference embedding_raw_address;
2496 if (isolate()->heap()->InNewSpace(*object)) {
2497 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2498 mov(result, Operand::ForCell(cell));
2499 } else {
2500 mov(result, object);
2501 }
2502}
2503
2504
2505void MacroAssembler::CmpHeapObject(Register reg, Handle<HeapObject> object) {
2506 AllowDeferredHandleDereference using_raw_address;
2507 if (isolate()->heap()->InNewSpace(*object)) {
2508 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2509 cmp(reg, Operand::ForCell(cell));
2510 } else {
2511 cmp(reg, object);
2512 }
2513}
2514
2515
2516void MacroAssembler::PushHeapObject(Handle<HeapObject> object) {
2517 AllowDeferredHandleDereference using_raw_address;
2518 if (isolate()->heap()->InNewSpace(*object)) {
2519 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2520 push(Operand::ForCell(cell));
2521 } else {
2522 Push(object);
2523 }
2524}
2525
2526
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002527void MacroAssembler::CmpWeakValue(Register value, Handle<WeakCell> cell,
2528 Register scratch) {
2529 mov(scratch, cell);
2530 cmp(value, FieldOperand(scratch, WeakCell::kValueOffset));
2531}
2532
2533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534void MacroAssembler::GetWeakValue(Register value, Handle<WeakCell> cell) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002535 mov(value, cell);
2536 mov(value, FieldOperand(value, WeakCell::kValueOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002537}
2538
2539
2540void MacroAssembler::LoadWeakValue(Register value, Handle<WeakCell> cell,
2541 Label* miss) {
2542 GetWeakValue(value, cell);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002543 JumpIfSmi(value, miss);
2544}
2545
2546
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002547void MacroAssembler::Ret() {
2548 ret(0);
2549}
2550
2551
2552void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
2553 if (is_uint16(bytes_dropped)) {
2554 ret(bytes_dropped);
2555 } else {
2556 pop(scratch);
2557 add(esp, Immediate(bytes_dropped));
2558 push(scratch);
2559 ret(0);
2560 }
2561}
2562
2563
2564void MacroAssembler::VerifyX87StackDepth(uint32_t depth) {
2565 // Turn off the stack depth check when serializer is enabled to reduce the
2566 // code size.
2567 if (serializer_enabled()) return;
2568 // Make sure the floating point stack is either empty or has depth items.
2569 DCHECK(depth <= 7);
2570 // This is very expensive.
2571 DCHECK(FLAG_debug_code && FLAG_enable_slow_asserts);
2572
2573 // The top-of-stack (tos) is 7 if there is one item pushed.
2574 int tos = (8 - depth) % 8;
2575 const int kTopMask = 0x3800;
2576 push(eax);
2577 fwait();
2578 fnstsw_ax();
2579 and_(eax, kTopMask);
2580 shr(eax, 11);
2581 cmp(eax, Immediate(tos));
2582 Check(equal, kUnexpectedFPUStackDepthAfterInstruction);
2583 fnclex();
2584 pop(eax);
2585}
2586
2587
2588void MacroAssembler::Drop(int stack_elements) {
2589 if (stack_elements > 0) {
2590 add(esp, Immediate(stack_elements * kPointerSize));
2591 }
2592}
2593
2594
2595void MacroAssembler::Move(Register dst, Register src) {
2596 if (!dst.is(src)) {
2597 mov(dst, src);
2598 }
2599}
2600
2601
2602void MacroAssembler::Move(Register dst, const Immediate& x) {
2603 if (x.is_zero()) {
2604 xor_(dst, dst); // Shorter than mov of 32-bit immediate 0.
2605 } else {
2606 mov(dst, x);
2607 }
2608}
2609
2610
2611void MacroAssembler::Move(const Operand& dst, const Immediate& x) {
2612 mov(dst, x);
2613}
2614
2615
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002616void MacroAssembler::Lzcnt(Register dst, const Operand& src) {
2617 // TODO(intel): Add support for LZCNT (with ABM/BMI1).
2618 Label not_zero_src;
2619 bsr(dst, src);
2620 j(not_zero, &not_zero_src, Label::kNear);
2621 Move(dst, Immediate(63)); // 63^31 == 32
2622 bind(&not_zero_src);
2623 xor_(dst, Immediate(31)); // for x in [0..31], 31^x == 31-x.
2624}
2625
2626
2627void MacroAssembler::Tzcnt(Register dst, const Operand& src) {
2628 // TODO(intel): Add support for TZCNT (with ABM/BMI1).
2629 Label not_zero_src;
2630 bsf(dst, src);
2631 j(not_zero, &not_zero_src, Label::kNear);
2632 Move(dst, Immediate(32)); // The result of tzcnt is 32 if src = 0.
2633 bind(&not_zero_src);
2634}
2635
2636
2637void MacroAssembler::Popcnt(Register dst, const Operand& src) {
2638 // TODO(intel): Add support for POPCNT (with POPCNT)
2639 // if (CpuFeatures::IsSupported(POPCNT)) {
2640 // CpuFeatureScope scope(this, POPCNT);
2641 // popcnt(dst, src);
2642 // return;
2643 // }
2644 UNREACHABLE();
2645}
2646
2647
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002648void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
2649 if (FLAG_native_code_counters && counter->Enabled()) {
2650 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
2651 }
2652}
2653
2654
2655void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
2656 DCHECK(value > 0);
2657 if (FLAG_native_code_counters && counter->Enabled()) {
2658 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2659 if (value == 1) {
2660 inc(operand);
2661 } else {
2662 add(operand, Immediate(value));
2663 }
2664 }
2665}
2666
2667
2668void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
2669 DCHECK(value > 0);
2670 if (FLAG_native_code_counters && counter->Enabled()) {
2671 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2672 if (value == 1) {
2673 dec(operand);
2674 } else {
2675 sub(operand, Immediate(value));
2676 }
2677 }
2678}
2679
2680
2681void MacroAssembler::IncrementCounter(Condition cc,
2682 StatsCounter* counter,
2683 int value) {
2684 DCHECK(value > 0);
2685 if (FLAG_native_code_counters && counter->Enabled()) {
2686 Label skip;
2687 j(NegateCondition(cc), &skip);
2688 pushfd();
2689 IncrementCounter(counter, value);
2690 popfd();
2691 bind(&skip);
2692 }
2693}
2694
2695
2696void MacroAssembler::DecrementCounter(Condition cc,
2697 StatsCounter* counter,
2698 int value) {
2699 DCHECK(value > 0);
2700 if (FLAG_native_code_counters && counter->Enabled()) {
2701 Label skip;
2702 j(NegateCondition(cc), &skip);
2703 pushfd();
2704 DecrementCounter(counter, value);
2705 popfd();
2706 bind(&skip);
2707 }
2708}
2709
2710
2711void MacroAssembler::Assert(Condition cc, BailoutReason reason) {
2712 if (emit_debug_code()) Check(cc, reason);
2713}
2714
2715
2716void MacroAssembler::AssertFastElements(Register elements) {
2717 if (emit_debug_code()) {
2718 Factory* factory = isolate()->factory();
2719 Label ok;
2720 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2721 Immediate(factory->fixed_array_map()));
2722 j(equal, &ok);
2723 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2724 Immediate(factory->fixed_double_array_map()));
2725 j(equal, &ok);
2726 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2727 Immediate(factory->fixed_cow_array_map()));
2728 j(equal, &ok);
2729 Abort(kJSObjectWithFastElementsMapHasSlowElements);
2730 bind(&ok);
2731 }
2732}
2733
2734
2735void MacroAssembler::Check(Condition cc, BailoutReason reason) {
2736 Label L;
2737 j(cc, &L);
2738 Abort(reason);
2739 // will not return here
2740 bind(&L);
2741}
2742
2743
2744void MacroAssembler::CheckStackAlignment() {
2745 int frame_alignment = base::OS::ActivationFrameAlignment();
2746 int frame_alignment_mask = frame_alignment - 1;
2747 if (frame_alignment > kPointerSize) {
2748 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
2749 Label alignment_as_expected;
2750 test(esp, Immediate(frame_alignment_mask));
2751 j(zero, &alignment_as_expected);
2752 // Abort if stack is not aligned.
2753 int3();
2754 bind(&alignment_as_expected);
2755 }
2756}
2757
2758
2759void MacroAssembler::Abort(BailoutReason reason) {
2760#ifdef DEBUG
2761 const char* msg = GetBailoutReason(reason);
2762 if (msg != NULL) {
2763 RecordComment("Abort message: ");
2764 RecordComment(msg);
2765 }
2766
2767 if (FLAG_trap_on_abort) {
2768 int3();
2769 return;
2770 }
2771#endif
2772
2773 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(reason))));
2774 // Disable stub call restrictions to always allow calls to abort.
2775 if (!has_frame_) {
2776 // We don't actually want to generate a pile of code for this, so just
2777 // claim there is a stack frame, without generating one.
2778 FrameScope scope(this, StackFrame::NONE);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002779 CallRuntime(Runtime::kAbort);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002780 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002781 CallRuntime(Runtime::kAbort);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002782 }
2783 // will not return here
2784 int3();
2785}
2786
2787
2788void MacroAssembler::LoadInstanceDescriptors(Register map,
2789 Register descriptors) {
2790 mov(descriptors, FieldOperand(map, Map::kDescriptorsOffset));
2791}
2792
2793
2794void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
2795 mov(dst, FieldOperand(map, Map::kBitField3Offset));
2796 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
2797}
2798
2799
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002800void MacroAssembler::LoadAccessor(Register dst, Register holder,
2801 int accessor_index,
2802 AccessorComponent accessor) {
2803 mov(dst, FieldOperand(holder, HeapObject::kMapOffset));
2804 LoadInstanceDescriptors(dst, dst);
2805 mov(dst, FieldOperand(dst, DescriptorArray::GetValueOffset(accessor_index)));
2806 int offset = accessor == ACCESSOR_GETTER ? AccessorPair::kGetterOffset
2807 : AccessorPair::kSetterOffset;
2808 mov(dst, FieldOperand(dst, offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002809}
2810
2811
2812void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(
2813 Register instance_type, Register scratch, Label* failure) {
2814 if (!scratch.is(instance_type)) {
2815 mov(scratch, instance_type);
2816 }
2817 and_(scratch,
2818 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2819 cmp(scratch, kStringTag | kSeqStringTag | kOneByteStringTag);
2820 j(not_equal, failure);
2821}
2822
2823
2824void MacroAssembler::JumpIfNotBothSequentialOneByteStrings(Register object1,
2825 Register object2,
2826 Register scratch1,
2827 Register scratch2,
2828 Label* failure) {
2829 // Check that both objects are not smis.
2830 STATIC_ASSERT(kSmiTag == 0);
2831 mov(scratch1, object1);
2832 and_(scratch1, object2);
2833 JumpIfSmi(scratch1, failure);
2834
2835 // Load instance type for both strings.
2836 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2837 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2838 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2839 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2840
2841 // Check that both are flat one-byte strings.
2842 const int kFlatOneByteStringMask =
2843 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2844 const int kFlatOneByteStringTag =
2845 kStringTag | kOneByteStringTag | kSeqStringTag;
2846 // Interleave bits from both instance types and compare them in one check.
2847 DCHECK_EQ(0, kFlatOneByteStringMask & (kFlatOneByteStringMask << 3));
2848 and_(scratch1, kFlatOneByteStringMask);
2849 and_(scratch2, kFlatOneByteStringMask);
2850 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
2851 cmp(scratch1, kFlatOneByteStringTag | (kFlatOneByteStringTag << 3));
2852 j(not_equal, failure);
2853}
2854
2855
2856void MacroAssembler::JumpIfNotUniqueNameInstanceType(Operand operand,
2857 Label* not_unique_name,
2858 Label::Distance distance) {
2859 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2860 Label succeed;
2861 test(operand, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2862 j(zero, &succeed);
Ben Murdochda12d292016-06-02 14:46:10 +01002863 cmpb(operand, Immediate(SYMBOL_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002864 j(not_equal, not_unique_name, distance);
2865
2866 bind(&succeed);
2867}
2868
2869
2870void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
2871 Register index,
2872 Register value,
2873 uint32_t encoding_mask) {
2874 Label is_object;
2875 JumpIfNotSmi(string, &is_object, Label::kNear);
2876 Abort(kNonObject);
2877 bind(&is_object);
2878
2879 push(value);
2880 mov(value, FieldOperand(string, HeapObject::kMapOffset));
2881 movzx_b(value, FieldOperand(value, Map::kInstanceTypeOffset));
2882
2883 and_(value, Immediate(kStringRepresentationMask | kStringEncodingMask));
2884 cmp(value, Immediate(encoding_mask));
2885 pop(value);
2886 Check(equal, kUnexpectedStringType);
2887
2888 // The index is assumed to be untagged coming in, tag it to compare with the
2889 // string length without using a temp register, it is restored at the end of
2890 // this function.
2891 SmiTag(index);
2892 Check(no_overflow, kIndexIsTooLarge);
2893
2894 cmp(index, FieldOperand(string, String::kLengthOffset));
2895 Check(less, kIndexIsTooLarge);
2896
2897 cmp(index, Immediate(Smi::FromInt(0)));
2898 Check(greater_equal, kIndexIsNegative);
2899
2900 // Restore the index
2901 SmiUntag(index);
2902}
2903
2904
2905void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2906 int frame_alignment = base::OS::ActivationFrameAlignment();
2907 if (frame_alignment != 0) {
2908 // Make stack end at alignment and make room for num_arguments words
2909 // and the original value of esp.
2910 mov(scratch, esp);
2911 sub(esp, Immediate((num_arguments + 1) * kPointerSize));
2912 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
2913 and_(esp, -frame_alignment);
2914 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2915 } else {
2916 sub(esp, Immediate(num_arguments * kPointerSize));
2917 }
2918}
2919
2920
2921void MacroAssembler::CallCFunction(ExternalReference function,
2922 int num_arguments) {
2923 // Trashing eax is ok as it will be the return value.
2924 mov(eax, Immediate(function));
2925 CallCFunction(eax, num_arguments);
2926}
2927
2928
2929void MacroAssembler::CallCFunction(Register function,
2930 int num_arguments) {
2931 DCHECK(has_frame());
2932 // Check stack alignment.
2933 if (emit_debug_code()) {
2934 CheckStackAlignment();
2935 }
2936
2937 call(function);
2938 if (base::OS::ActivationFrameAlignment() != 0) {
2939 mov(esp, Operand(esp, num_arguments * kPointerSize));
2940 } else {
2941 add(esp, Immediate(num_arguments * kPointerSize));
2942 }
2943}
2944
2945
2946#ifdef DEBUG
2947bool AreAliased(Register reg1,
2948 Register reg2,
2949 Register reg3,
2950 Register reg4,
2951 Register reg5,
2952 Register reg6,
2953 Register reg7,
2954 Register reg8) {
2955 int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
2956 reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
2957 reg7.is_valid() + reg8.is_valid();
2958
2959 RegList regs = 0;
2960 if (reg1.is_valid()) regs |= reg1.bit();
2961 if (reg2.is_valid()) regs |= reg2.bit();
2962 if (reg3.is_valid()) regs |= reg3.bit();
2963 if (reg4.is_valid()) regs |= reg4.bit();
2964 if (reg5.is_valid()) regs |= reg5.bit();
2965 if (reg6.is_valid()) regs |= reg6.bit();
2966 if (reg7.is_valid()) regs |= reg7.bit();
2967 if (reg8.is_valid()) regs |= reg8.bit();
2968 int n_of_non_aliasing_regs = NumRegs(regs);
2969
2970 return n_of_valid_regs != n_of_non_aliasing_regs;
2971}
2972#endif
2973
2974
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002975CodePatcher::CodePatcher(Isolate* isolate, byte* address, int size)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002976 : address_(address),
2977 size_(size),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002978 masm_(isolate, address, size + Assembler::kGap, CodeObjectRequired::kNo) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002979 // Create a new macro assembler pointing to the address of the code to patch.
2980 // The size is adjusted with kGap on order for the assembler to generate size
2981 // bytes of instructions without failing with buffer size constraints.
2982 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2983}
2984
2985
2986CodePatcher::~CodePatcher() {
2987 // Indicate that code has changed.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002988 Assembler::FlushICache(masm_.isolate(), address_, size_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002989
2990 // Check that the code was patched as expected.
2991 DCHECK(masm_.pc_ == address_ + size_);
2992 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2993}
2994
2995
2996void MacroAssembler::CheckPageFlag(
2997 Register object,
2998 Register scratch,
2999 int mask,
3000 Condition cc,
3001 Label* condition_met,
3002 Label::Distance condition_met_distance) {
3003 DCHECK(cc == zero || cc == not_zero);
3004 if (scratch.is(object)) {
3005 and_(scratch, Immediate(~Page::kPageAlignmentMask));
3006 } else {
3007 mov(scratch, Immediate(~Page::kPageAlignmentMask));
3008 and_(scratch, object);
3009 }
3010 if (mask < (1 << kBitsPerByte)) {
Ben Murdochda12d292016-06-02 14:46:10 +01003011 test_b(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003012 } else {
3013 test(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
3014 }
3015 j(cc, condition_met, condition_met_distance);
3016}
3017
3018
3019void MacroAssembler::CheckPageFlagForMap(
3020 Handle<Map> map,
3021 int mask,
3022 Condition cc,
3023 Label* condition_met,
3024 Label::Distance condition_met_distance) {
3025 DCHECK(cc == zero || cc == not_zero);
3026 Page* page = Page::FromAddress(map->address());
3027 DCHECK(!serializer_enabled()); // Serializer cannot match page_flags.
3028 ExternalReference reference(ExternalReference::page_flags(page));
3029 // The inlined static address check of the page's flags relies
3030 // on maps never being compacted.
3031 DCHECK(!isolate()->heap()->mark_compact_collector()->
3032 IsOnEvacuationCandidate(*map));
3033 if (mask < (1 << kBitsPerByte)) {
Ben Murdochda12d292016-06-02 14:46:10 +01003034 test_b(Operand::StaticVariable(reference), Immediate(mask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003035 } else {
3036 test(Operand::StaticVariable(reference), Immediate(mask));
3037 }
3038 j(cc, condition_met, condition_met_distance);
3039}
3040
3041
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003042void MacroAssembler::JumpIfBlack(Register object,
3043 Register scratch0,
3044 Register scratch1,
3045 Label* on_black,
3046 Label::Distance on_black_near) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003047 HasColor(object, scratch0, scratch1, on_black, on_black_near, 1,
3048 1); // kBlackBitPattern.
3049 DCHECK(strcmp(Marking::kBlackBitPattern, "11") == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003050}
3051
3052
3053void MacroAssembler::HasColor(Register object,
3054 Register bitmap_scratch,
3055 Register mask_scratch,
3056 Label* has_color,
3057 Label::Distance has_color_distance,
3058 int first_bit,
3059 int second_bit) {
3060 DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, ecx));
3061
3062 GetMarkBits(object, bitmap_scratch, mask_scratch);
3063
3064 Label other_color, word_boundary;
3065 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3066 j(first_bit == 1 ? zero : not_zero, &other_color, Label::kNear);
3067 add(mask_scratch, mask_scratch); // Shift left 1 by adding.
3068 j(zero, &word_boundary, Label::kNear);
3069 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3070 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3071 jmp(&other_color, Label::kNear);
3072
3073 bind(&word_boundary);
Ben Murdochda12d292016-06-02 14:46:10 +01003074 test_b(Operand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize),
3075 Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003076
3077 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3078 bind(&other_color);
3079}
3080
3081
3082void MacroAssembler::GetMarkBits(Register addr_reg,
3083 Register bitmap_reg,
3084 Register mask_reg) {
3085 DCHECK(!AreAliased(addr_reg, mask_reg, bitmap_reg, ecx));
3086 mov(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
3087 and_(bitmap_reg, addr_reg);
3088 mov(ecx, addr_reg);
3089 int shift =
3090 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
3091 shr(ecx, shift);
3092 and_(ecx,
3093 (Page::kPageAlignmentMask >> shift) & ~(Bitmap::kBytesPerCell - 1));
3094
3095 add(bitmap_reg, ecx);
3096 mov(ecx, addr_reg);
3097 shr(ecx, kPointerSizeLog2);
3098 and_(ecx, (1 << Bitmap::kBitsPerCellLog2) - 1);
3099 mov(mask_reg, Immediate(1));
3100 shl_cl(mask_reg);
3101}
3102
3103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003104void MacroAssembler::JumpIfWhite(Register value, Register bitmap_scratch,
3105 Register mask_scratch, Label* value_is_white,
3106 Label::Distance distance) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003107 DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, ecx));
3108 GetMarkBits(value, bitmap_scratch, mask_scratch);
3109
3110 // If the value is black or grey we don't need to do anything.
3111 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003112 DCHECK(strcmp(Marking::kBlackBitPattern, "11") == 0);
3113 DCHECK(strcmp(Marking::kGreyBitPattern, "10") == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003114 DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
3115
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003116 // Since both black and grey have a 1 in the first position and white does
3117 // not have a 1 there we only need to check one bit.
3118 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003119 j(zero, value_is_white, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003120}
3121
3122
3123void MacroAssembler::EnumLength(Register dst, Register map) {
3124 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
3125 mov(dst, FieldOperand(map, Map::kBitField3Offset));
3126 and_(dst, Immediate(Map::EnumLengthBits::kMask));
3127 SmiTag(dst);
3128}
3129
3130
3131void MacroAssembler::CheckEnumCache(Label* call_runtime) {
3132 Label next, start;
3133 mov(ecx, eax);
3134
3135 // Check if the enum length field is properly initialized, indicating that
3136 // there is an enum cache.
3137 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
3138
3139 EnumLength(edx, ebx);
3140 cmp(edx, Immediate(Smi::FromInt(kInvalidEnumCacheSentinel)));
3141 j(equal, call_runtime);
3142
3143 jmp(&start);
3144
3145 bind(&next);
3146 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
3147
3148 // For all objects but the receiver, check that the cache is empty.
3149 EnumLength(edx, ebx);
3150 cmp(edx, Immediate(Smi::FromInt(0)));
3151 j(not_equal, call_runtime);
3152
3153 bind(&start);
3154
3155 // Check that there are no elements. Register rcx contains the current JS
3156 // object we've reached through the prototype chain.
3157 Label no_elements;
3158 mov(ecx, FieldOperand(ecx, JSObject::kElementsOffset));
3159 cmp(ecx, isolate()->factory()->empty_fixed_array());
3160 j(equal, &no_elements);
3161
3162 // Second chance, the object may be using the empty slow element dictionary.
3163 cmp(ecx, isolate()->factory()->empty_slow_element_dictionary());
3164 j(not_equal, call_runtime);
3165
3166 bind(&no_elements);
3167 mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
3168 cmp(ecx, isolate()->factory()->null_value());
3169 j(not_equal, &next);
3170}
3171
3172
3173void MacroAssembler::TestJSArrayForAllocationMemento(
3174 Register receiver_reg,
3175 Register scratch_reg,
3176 Label* no_memento_found) {
Ben Murdochda12d292016-06-02 14:46:10 +01003177 Label map_check;
3178 Label top_check;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003179 ExternalReference new_space_allocation_top =
3180 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochda12d292016-06-02 14:46:10 +01003181 const int kMementoMapOffset = JSArray::kSize - kHeapObjectTag;
3182 const int kMementoEndOffset = kMementoMapOffset + AllocationMemento::kSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003183
Ben Murdochda12d292016-06-02 14:46:10 +01003184 // Bail out if the object is not in new space.
3185 JumpIfNotInNewSpace(receiver_reg, scratch_reg, no_memento_found);
3186 // If the object is in new space, we need to check whether it is on the same
3187 // page as the current top.
3188 lea(scratch_reg, Operand(receiver_reg, kMementoEndOffset));
3189 xor_(scratch_reg, Operand::StaticVariable(new_space_allocation_top));
3190 test(scratch_reg, Immediate(~Page::kPageAlignmentMask));
3191 j(zero, &top_check);
3192 // The object is on a different page than allocation top. Bail out if the
3193 // object sits on the page boundary as no memento can follow and we cannot
3194 // touch the memory following it.
3195 lea(scratch_reg, Operand(receiver_reg, kMementoEndOffset));
3196 xor_(scratch_reg, receiver_reg);
3197 test(scratch_reg, Immediate(~Page::kPageAlignmentMask));
3198 j(not_zero, no_memento_found);
3199 // Continue with the actual map check.
3200 jmp(&map_check);
3201 // If top is on the same page as the current object, we need to check whether
3202 // we are below top.
3203 bind(&top_check);
3204 lea(scratch_reg, Operand(receiver_reg, kMementoEndOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003205 cmp(scratch_reg, Operand::StaticVariable(new_space_allocation_top));
3206 j(greater, no_memento_found);
Ben Murdochda12d292016-06-02 14:46:10 +01003207 // Memento map check.
3208 bind(&map_check);
3209 mov(scratch_reg, Operand(receiver_reg, kMementoMapOffset));
3210 cmp(scratch_reg, Immediate(isolate()->factory()->allocation_memento_map()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003211}
3212
3213
3214void MacroAssembler::JumpIfDictionaryInPrototypeChain(
3215 Register object,
3216 Register scratch0,
3217 Register scratch1,
3218 Label* found) {
3219 DCHECK(!scratch1.is(scratch0));
3220 Factory* factory = isolate()->factory();
3221 Register current = scratch0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003222 Label loop_again, end;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003223
3224 // scratch contained elements pointer.
3225 mov(current, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003226 mov(current, FieldOperand(current, HeapObject::kMapOffset));
3227 mov(current, FieldOperand(current, Map::kPrototypeOffset));
3228 cmp(current, Immediate(factory->null_value()));
3229 j(equal, &end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003230
3231 // Loop based on the map going up the prototype chain.
3232 bind(&loop_again);
3233 mov(current, FieldOperand(current, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003234 STATIC_ASSERT(JS_PROXY_TYPE < JS_OBJECT_TYPE);
3235 STATIC_ASSERT(JS_VALUE_TYPE < JS_OBJECT_TYPE);
3236 CmpInstanceType(current, JS_OBJECT_TYPE);
3237 j(below, found);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003238 mov(scratch1, FieldOperand(current, Map::kBitField2Offset));
3239 DecodeField<Map::ElementsKindBits>(scratch1);
3240 cmp(scratch1, Immediate(DICTIONARY_ELEMENTS));
3241 j(equal, found);
3242 mov(current, FieldOperand(current, Map::kPrototypeOffset));
3243 cmp(current, Immediate(factory->null_value()));
3244 j(not_equal, &loop_again);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003245
3246 bind(&end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003247}
3248
3249
3250void MacroAssembler::TruncatingDiv(Register dividend, int32_t divisor) {
3251 DCHECK(!dividend.is(eax));
3252 DCHECK(!dividend.is(edx));
3253 base::MagicNumbersForDivision<uint32_t> mag =
3254 base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
3255 mov(eax, Immediate(mag.multiplier));
3256 imul(dividend);
3257 bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
3258 if (divisor > 0 && neg) add(edx, dividend);
3259 if (divisor < 0 && !neg && mag.multiplier > 0) sub(edx, dividend);
3260 if (mag.shift > 0) sar(edx, mag.shift);
3261 mov(eax, dividend);
3262 shr(eax, 31);
3263 add(edx, eax);
3264}
3265
3266
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003267} // namespace internal
3268} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003269
3270#endif // V8_TARGET_ARCH_X87