blob: 38ada92a99812622212fb3d844413527bec4d2d5 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "assembler-x64.h"
33#include "macro-assembler-x64.h"
34#include "serialize.h"
35#include "debug.h"
36
37namespace v8 {
38namespace internal {
39
40MacroAssembler::MacroAssembler(void* buffer, int size)
41 : Assembler(buffer, size),
42 unresolved_(0),
43 generating_stub_(false),
44 allow_stub_calls_(true),
45 code_object_(Heap::undefined_value()) {
46}
47
48
49void MacroAssembler::LoadRoot(Register destination,
50 Heap::RootListIndex index) {
51 movq(destination, Operand(r13, index << kPointerSizeLog2));
52}
53
54
55void MacroAssembler::PushRoot(Heap::RootListIndex index) {
56 push(Operand(r13, index << kPointerSizeLog2));
57}
58
59
60void MacroAssembler::CompareRoot(Register with,
61 Heap::RootListIndex index) {
62 cmpq(with, Operand(r13, index << kPointerSizeLog2));
63}
64
65
66void MacroAssembler::CompareRoot(Operand with,
67 Heap::RootListIndex index) {
68 LoadRoot(kScratchRegister, index);
69 cmpq(with, kScratchRegister);
70}
71
72
73static void RecordWriteHelper(MacroAssembler* masm,
74 Register object,
75 Register addr,
76 Register scratch) {
77 Label fast;
78
79 // Compute the page start address from the heap object pointer, and reuse
80 // the 'object' register for it.
81 ASSERT(is_int32(~Page::kPageAlignmentMask));
82 masm->and_(object,
83 Immediate(static_cast<int32_t>(~Page::kPageAlignmentMask)));
84 Register page_start = object;
85
86 // Compute the bit addr in the remembered set/index of the pointer in the
87 // page. Reuse 'addr' as pointer_offset.
88 masm->subq(addr, page_start);
89 masm->shr(addr, Immediate(kPointerSizeLog2));
90 Register pointer_offset = addr;
91
92 // If the bit offset lies beyond the normal remembered set range, it is in
93 // the extra remembered set area of a large object.
94 masm->cmpq(pointer_offset, Immediate(Page::kPageSize / kPointerSize));
95 masm->j(less, &fast);
96
97 // Adjust 'page_start' so that addressing using 'pointer_offset' hits the
98 // extra remembered set after the large object.
99
100 // Load the array length into 'scratch'.
101 masm->movl(scratch,
102 Operand(page_start,
103 Page::kObjectStartOffset + FixedArray::kLengthOffset));
104 Register array_length = scratch;
105
106 // Extra remembered set starts right after the large object (a FixedArray), at
107 // page_start + kObjectStartOffset + objectSize
108 // where objectSize is FixedArray::kHeaderSize + kPointerSize * array_length.
109 // Add the delta between the end of the normal RSet and the start of the
110 // extra RSet to 'page_start', so that addressing the bit using
111 // 'pointer_offset' hits the extra RSet words.
112 masm->lea(page_start,
113 Operand(page_start, array_length, times_pointer_size,
114 Page::kObjectStartOffset + FixedArray::kHeaderSize
115 - Page::kRSetEndOffset));
116
117 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
118 // to limit code size. We should probably evaluate this decision by
119 // measuring the performance of an equivalent implementation using
120 // "simpler" instructions
121 masm->bind(&fast);
122 masm->bts(Operand(page_start, Page::kRSetOffset), pointer_offset);
123}
124
125
126class RecordWriteStub : public CodeStub {
127 public:
128 RecordWriteStub(Register object, Register addr, Register scratch)
129 : object_(object), addr_(addr), scratch_(scratch) { }
130
131 void Generate(MacroAssembler* masm);
132
133 private:
134 Register object_;
135 Register addr_;
136 Register scratch_;
137
138#ifdef DEBUG
139 void Print() {
140 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
141 object_.code(), addr_.code(), scratch_.code());
142 }
143#endif
144
145 // Minor key encoding in 12 bits of three registers (object, address and
146 // scratch) OOOOAAAASSSS.
147 class ScratchBits: public BitField<uint32_t, 0, 4> {};
148 class AddressBits: public BitField<uint32_t, 4, 4> {};
149 class ObjectBits: public BitField<uint32_t, 8, 4> {};
150
151 Major MajorKey() { return RecordWrite; }
152
153 int MinorKey() {
154 // Encode the registers.
155 return ObjectBits::encode(object_.code()) |
156 AddressBits::encode(addr_.code()) |
157 ScratchBits::encode(scratch_.code());
158 }
159};
160
161
162void RecordWriteStub::Generate(MacroAssembler* masm) {
163 RecordWriteHelper(masm, object_, addr_, scratch_);
164 masm->ret(0);
165}
166
167
168// Set the remembered set bit for [object+offset].
169// object is the object being stored into, value is the object being stored.
170// If offset is zero, then the scratch register contains the array index into
171// the elements array represented as a Smi.
172// All registers are clobbered by the operation.
173void MacroAssembler::RecordWrite(Register object,
174 int offset,
175 Register value,
176 Register scratch) {
177 // First, check if a remembered set write is even needed. The tests below
178 // catch stores of Smis and stores into young gen (which does not have space
179 // for the remembered set bits.
180 Label done;
181
182 // Test that the object address is not in the new space. We cannot
183 // set remembered set bits in the new space.
184 movq(value, object);
185 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
186 and_(value, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
187 movq(kScratchRegister, ExternalReference::new_space_start());
188 cmpq(value, kScratchRegister);
189 j(equal, &done);
190
191 if ((offset > 0) && (offset < Page::kMaxHeapObjectSize)) {
192 // Compute the bit offset in the remembered set, leave it in 'value'.
193 lea(value, Operand(object, offset));
194 ASSERT(is_int32(Page::kPageAlignmentMask));
195 and_(value, Immediate(static_cast<int32_t>(Page::kPageAlignmentMask)));
196 shr(value, Immediate(kObjectAlignmentBits));
197
198 // Compute the page address from the heap object pointer, leave it in
199 // 'object' (immediate value is sign extended).
200 and_(object, Immediate(~Page::kPageAlignmentMask));
201
202 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
203 // to limit code size. We should probably evaluate this decision by
204 // measuring the performance of an equivalent implementation using
205 // "simpler" instructions
206 bts(Operand(object, Page::kRSetOffset), value);
207 } else {
208 Register dst = scratch;
209 if (offset != 0) {
210 lea(dst, Operand(object, offset));
211 } else {
212 // array access: calculate the destination address in the same manner as
213 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 4 to get an offset
214 // into an array of pointers.
215 lea(dst, Operand(object, dst, times_half_pointer_size,
216 FixedArray::kHeaderSize - kHeapObjectTag));
217 }
218 // If we are already generating a shared stub, not inlining the
219 // record write code isn't going to save us any memory.
220 if (generating_stub()) {
221 RecordWriteHelper(this, object, dst, value);
222 } else {
223 RecordWriteStub stub(object, dst, value);
224 CallStub(&stub);
225 }
226 }
227
228 bind(&done);
229}
230
231
232void MacroAssembler::Assert(Condition cc, const char* msg) {
233 if (FLAG_debug_code) Check(cc, msg);
234}
235
236
237void MacroAssembler::Check(Condition cc, const char* msg) {
238 Label L;
239 j(cc, &L);
240 Abort(msg);
241 // will not return here
242 bind(&L);
243}
244
245
246void MacroAssembler::NegativeZeroTest(Register result,
247 Register op,
248 Label* then_label) {
249 Label ok;
250 testl(result, result);
251 j(not_zero, &ok);
252 testl(op, op);
253 j(sign, then_label);
254 bind(&ok);
255}
256
257
258void MacroAssembler::Abort(const char* msg) {
259 // We want to pass the msg string like a smi to avoid GC
260 // problems, however msg is not guaranteed to be aligned
261 // properly. Instead, we pass an aligned pointer that is
262 // a proper v8 smi, but also pass the alignment difference
263 // from the real pointer as a smi.
264 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
265 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
266 // Note: p0 might not be a valid Smi *value*, but it has a valid Smi tag.
267 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
268#ifdef DEBUG
269 if (msg != NULL) {
270 RecordComment("Abort message: ");
271 RecordComment(msg);
272 }
273#endif
274 push(rax);
275 movq(kScratchRegister, p0, RelocInfo::NONE);
276 push(kScratchRegister);
277 movq(kScratchRegister,
278 reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0)),
279 RelocInfo::NONE);
280 push(kScratchRegister);
281 CallRuntime(Runtime::kAbort, 2);
282 // will not return here
283}
284
285
286void MacroAssembler::CallStub(CodeStub* stub) {
287 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
288 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
289}
290
291
292void MacroAssembler::StubReturn(int argc) {
293 ASSERT(argc >= 1 && generating_stub());
294 ret((argc - 1) * kPointerSize);
295}
296
297
298void MacroAssembler::IllegalOperation(int num_arguments) {
299 if (num_arguments > 0) {
300 addq(rsp, Immediate(num_arguments * kPointerSize));
301 }
302 LoadRoot(rax, Heap::kUndefinedValueRootIndex);
303}
304
305
306void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
307 CallRuntime(Runtime::FunctionForId(id), num_arguments);
308}
309
310
311void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
312 // If the expected number of arguments of the runtime function is
313 // constant, we check that the actual number of arguments match the
314 // expectation.
315 if (f->nargs >= 0 && f->nargs != num_arguments) {
316 IllegalOperation(num_arguments);
317 return;
318 }
319
320 Runtime::FunctionId function_id =
321 static_cast<Runtime::FunctionId>(f->stub_id);
322 RuntimeStub stub(function_id, num_arguments);
323 CallStub(&stub);
324}
325
326
327void MacroAssembler::TailCallRuntime(ExternalReference const& ext,
328 int num_arguments,
329 int result_size) {
330 // ----------- S t a t e -------------
331 // -- rsp[0] : return address
332 // -- rsp[8] : argument num_arguments - 1
333 // ...
334 // -- rsp[8 * num_arguments] : argument 0 (receiver)
335 // -----------------------------------
336
337 // TODO(1236192): Most runtime routines don't need the number of
338 // arguments passed in because it is constant. At some point we
339 // should remove this need and make the runtime routine entry code
340 // smarter.
341 movq(rax, Immediate(num_arguments));
342 JumpToRuntime(ext, result_size);
343}
344
345
346void MacroAssembler::JumpToRuntime(const ExternalReference& ext,
347 int result_size) {
348 // Set the entry point and jump to the C entry runtime stub.
349 movq(rbx, ext);
350 CEntryStub ces(result_size);
351 movq(kScratchRegister, ces.GetCode(), RelocInfo::CODE_TARGET);
352 jmp(kScratchRegister);
353}
354
355
356void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
357 bool resolved;
358 Handle<Code> code = ResolveBuiltin(id, &resolved);
359
360 const char* name = Builtins::GetName(id);
361 int argc = Builtins::GetArgumentsCount(id);
362
363 movq(target, code, RelocInfo::EMBEDDED_OBJECT);
364 if (!resolved) {
365 uint32_t flags =
366 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
367 Bootstrapper::FixupFlagsIsPCRelative::encode(false) |
368 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
369 Unresolved entry = { pc_offset() - sizeof(intptr_t), flags, name };
370 unresolved_.Add(entry);
371 }
372 addq(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
373}
374
375
376Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
377 bool* resolved) {
378 // Move the builtin function into the temporary function slot by
379 // reading it from the builtins object. NOTE: We should be able to
380 // reduce this to two instructions by putting the function table in
381 // the global object instead of the "builtins" object and by using a
382 // real register for the function.
383 movq(rdx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
384 movq(rdx, FieldOperand(rdx, GlobalObject::kBuiltinsOffset));
385 int builtins_offset =
386 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
387 movq(rdi, FieldOperand(rdx, builtins_offset));
388
389
390 return Builtins::GetCode(id, resolved);
391}
392
393
394void MacroAssembler::Set(Register dst, int64_t x) {
395 if (x == 0) {
396 xor_(dst, dst);
397 } else if (is_int32(x)) {
398 movq(dst, Immediate(x));
399 } else if (is_uint32(x)) {
400 movl(dst, Immediate(x));
401 } else {
402 movq(dst, x, RelocInfo::NONE);
403 }
404}
405
406
407void MacroAssembler::Set(const Operand& dst, int64_t x) {
408 if (x == 0) {
409 xor_(kScratchRegister, kScratchRegister);
410 movq(dst, kScratchRegister);
411 } else if (is_int32(x)) {
412 movq(dst, Immediate(x));
413 } else if (is_uint32(x)) {
414 movl(dst, Immediate(x));
415 } else {
416 movq(kScratchRegister, x, RelocInfo::NONE);
417 movq(dst, kScratchRegister);
418 }
419}
420
421
422// ----------------------------------------------------------------------------
423// Smi tagging, untagging and tag detection.
424
425
426void MacroAssembler::Integer32ToSmi(Register dst, Register src) {
427 ASSERT_EQ(1, kSmiTagSize);
428 ASSERT_EQ(0, kSmiTag);
429#ifdef DEBUG
430 cmpq(src, Immediate(0xC0000000u));
431 Check(positive, "Smi conversion overflow");
432#endif
433 if (dst.is(src)) {
434 addl(dst, src);
435 } else {
436 lea(dst, Operand(src, src, times_1, 0));
437 }
438}
439
440
441void MacroAssembler::Integer32ToSmi(Register dst,
442 Register src,
443 Label* on_overflow) {
444 ASSERT_EQ(1, kSmiTagSize);
445 ASSERT_EQ(0, kSmiTag);
446 if (!dst.is(src)) {
447 movl(dst, src);
448 }
449 addl(dst, src);
450 j(overflow, on_overflow);
451}
452
453
454void MacroAssembler::Integer64AddToSmi(Register dst,
455 Register src,
456 int constant) {
457#ifdef DEBUG
458 movl(kScratchRegister, src);
459 addl(kScratchRegister, Immediate(constant));
460 Check(no_overflow, "Add-and-smi-convert overflow");
461 Condition valid = CheckInteger32ValidSmiValue(kScratchRegister);
462 Check(valid, "Add-and-smi-convert overflow");
463#endif
464 lea(dst, Operand(src, src, times_1, constant << kSmiTagSize));
465}
466
467
468void MacroAssembler::SmiToInteger32(Register dst, Register src) {
469 ASSERT_EQ(1, kSmiTagSize);
470 ASSERT_EQ(0, kSmiTag);
471 if (!dst.is(src)) {
472 movl(dst, src);
473 }
474 sarl(dst, Immediate(kSmiTagSize));
475}
476
477
478void MacroAssembler::SmiToInteger64(Register dst, Register src) {
479 ASSERT_EQ(1, kSmiTagSize);
480 ASSERT_EQ(0, kSmiTag);
481 movsxlq(dst, src);
482 sar(dst, Immediate(kSmiTagSize));
483}
484
485
486void MacroAssembler::PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
487 Register src,
488 int power) {
489 ASSERT(power >= 0);
490 ASSERT(power < 64);
491 if (power == 0) {
492 SmiToInteger64(dst, src);
493 return;
494 }
495 movsxlq(dst, src);
496 shl(dst, Immediate(power - 1));
497}
498
499void MacroAssembler::JumpIfSmi(Register src, Label* on_smi) {
500 ASSERT_EQ(0, kSmiTag);
501 testl(src, Immediate(kSmiTagMask));
502 j(zero, on_smi);
503}
504
505
506void MacroAssembler::JumpIfNotSmi(Register src, Label* on_not_smi) {
507 Condition not_smi = CheckNotSmi(src);
508 j(not_smi, on_not_smi);
509}
510
511
512void MacroAssembler::JumpIfNotPositiveSmi(Register src,
513 Label* on_not_positive_smi) {
514 Condition not_positive_smi = CheckNotPositiveSmi(src);
515 j(not_positive_smi, on_not_positive_smi);
516}
517
518
519void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
520 int constant,
521 Label* on_equals) {
522 if (Smi::IsValid(constant)) {
523 Condition are_equal = CheckSmiEqualsConstant(src, constant);
524 j(are_equal, on_equals);
525 }
526}
527
528
529void MacroAssembler::JumpIfSmiGreaterEqualsConstant(Register src,
530 int constant,
531 Label* on_greater_equals) {
532 if (Smi::IsValid(constant)) {
533 Condition are_greater_equal = CheckSmiGreaterEqualsConstant(src, constant);
534 j(are_greater_equal, on_greater_equals);
535 } else if (constant < Smi::kMinValue) {
536 jmp(on_greater_equals);
537 }
538}
539
540
541void MacroAssembler::JumpIfNotValidSmiValue(Register src, Label* on_invalid) {
542 Condition is_valid = CheckInteger32ValidSmiValue(src);
543 j(ReverseCondition(is_valid), on_invalid);
544}
545
546
547
548void MacroAssembler::JumpIfNotBothSmi(Register src1,
549 Register src2,
550 Label* on_not_both_smi) {
551 Condition not_both_smi = CheckNotBothSmi(src1, src2);
552 j(not_both_smi, on_not_both_smi);
553}
554
555Condition MacroAssembler::CheckSmi(Register src) {
556 testb(src, Immediate(kSmiTagMask));
557 return zero;
558}
559
560
561Condition MacroAssembler::CheckNotSmi(Register src) {
562 ASSERT_EQ(0, kSmiTag);
563 testb(src, Immediate(kSmiTagMask));
564 return not_zero;
565}
566
567
568Condition MacroAssembler::CheckPositiveSmi(Register src) {
569 ASSERT_EQ(0, kSmiTag);
570 testl(src, Immediate(static_cast<uint32_t>(0x80000000u | kSmiTagMask)));
571 return zero;
572}
573
574
575Condition MacroAssembler::CheckNotPositiveSmi(Register src) {
576 ASSERT_EQ(0, kSmiTag);
577 testl(src, Immediate(static_cast<uint32_t>(0x80000000u | kSmiTagMask)));
578 return not_zero;
579}
580
581
582Condition MacroAssembler::CheckBothSmi(Register first, Register second) {
583 if (first.is(second)) {
584 return CheckSmi(first);
585 }
586 movl(kScratchRegister, first);
587 orl(kScratchRegister, second);
588 return CheckSmi(kScratchRegister);
589}
590
591
592Condition MacroAssembler::CheckNotBothSmi(Register first, Register second) {
593 ASSERT_EQ(0, kSmiTag);
594 if (first.is(second)) {
595 return CheckNotSmi(first);
596 }
597 movl(kScratchRegister, first);
598 or_(kScratchRegister, second);
599 return CheckNotSmi(kScratchRegister);
600}
601
602
603Condition MacroAssembler::CheckIsMinSmi(Register src) {
604 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
605 cmpl(src, Immediate(0x40000000));
606 return equal;
607}
608
609Condition MacroAssembler::CheckSmiEqualsConstant(Register src, int constant) {
610 if (constant == 0) {
611 testl(src, src);
612 return zero;
613 }
614 if (Smi::IsValid(constant)) {
615 cmpl(src, Immediate(Smi::FromInt(constant)));
616 return zero;
617 }
618 // Can't be equal.
619 UNREACHABLE();
620 return no_condition;
621}
622
623
624Condition MacroAssembler::CheckSmiGreaterEqualsConstant(Register src,
625 int constant) {
626 if (constant == 0) {
627 testl(src, Immediate(static_cast<uint32_t>(0x80000000u)));
628 return positive;
629 }
630 if (Smi::IsValid(constant)) {
631 cmpl(src, Immediate(Smi::FromInt(constant)));
632 return greater_equal;
633 }
634 // Can't be equal.
635 UNREACHABLE();
636 return no_condition;
637}
638
639
640Condition MacroAssembler::CheckInteger32ValidSmiValue(Register src) {
641 // A 32-bit integer value can be converted to a smi if it is in the
642 // range [-2^30 .. 2^30-1]. That is equivalent to having its 32-bit
643 // representation have bits 30 and 31 be equal.
644 cmpl(src, Immediate(0xC0000000u));
645 return positive;
646}
647
648
649void MacroAssembler::SmiNeg(Register dst,
650 Register src,
651 Label* on_not_smi_result) {
652 if (!dst.is(src)) {
653 movl(dst, src);
654 }
655 negl(dst);
656 testl(dst, Immediate(0x7fffffff));
657 // If the result is zero or 0x80000000, negation failed to create a smi.
658 j(equal, on_not_smi_result);
659}
660
661
662void MacroAssembler::SmiAdd(Register dst,
663 Register src1,
664 Register src2,
665 Label* on_not_smi_result) {
666 ASSERT(!dst.is(src2));
667 if (!dst.is(src1)) {
668 movl(dst, src1);
669 }
670 addl(dst, src2);
671 if (!dst.is(src1)) {
672 j(overflow, on_not_smi_result);
673 } else {
674 Label smi_result;
675 j(no_overflow, &smi_result);
676 // Restore src1.
677 subl(src1, src2);
678 jmp(on_not_smi_result);
679 bind(&smi_result);
680 }
681}
682
683
684
685void MacroAssembler::SmiSub(Register dst,
686 Register src1,
687 Register src2,
688 Label* on_not_smi_result) {
689 ASSERT(!dst.is(src2));
690 if (!dst.is(src1)) {
691 movl(dst, src1);
692 }
693 subl(dst, src2);
694 if (!dst.is(src1)) {
695 j(overflow, on_not_smi_result);
696 } else {
697 Label smi_result;
698 j(no_overflow, &smi_result);
699 // Restore src1.
700 addl(src1, src2);
701 jmp(on_not_smi_result);
702 bind(&smi_result);
703 }
704}
705
706
707void MacroAssembler::SmiMul(Register dst,
708 Register src1,
709 Register src2,
710 Label* on_not_smi_result) {
711 ASSERT(!dst.is(src2));
712
713 if (dst.is(src1)) {
714 movq(kScratchRegister, src1);
715 }
716 SmiToInteger32(dst, src1);
717
718 imull(dst, src2);
719 j(overflow, on_not_smi_result);
720
721 // Check for negative zero result. If product is zero, and one
722 // argument is negative, go to slow case. The frame is unchanged
723 // in this block, so local control flow can use a Label rather
724 // than a JumpTarget.
725 Label non_zero_result;
726 testl(dst, dst);
727 j(not_zero, &non_zero_result);
728
729 // Test whether either operand is negative (the other must be zero).
730 orl(kScratchRegister, src2);
731 j(negative, on_not_smi_result);
732 bind(&non_zero_result);
733}
734
735
736void MacroAssembler::SmiTryAddConstant(Register dst,
737 Register src,
738 int32_t constant,
739 Label* on_not_smi_result) {
740 // Does not assume that src is a smi.
741 ASSERT_EQ(1, kSmiTagMask);
742 ASSERT_EQ(0, kSmiTag);
743 ASSERT(Smi::IsValid(constant));
744
745 Register tmp = (src.is(dst) ? kScratchRegister : dst);
746 movl(tmp, src);
747 addl(tmp, Immediate(Smi::FromInt(constant)));
748 if (tmp.is(kScratchRegister)) {
749 j(overflow, on_not_smi_result);
750 testl(tmp, Immediate(kSmiTagMask));
751 j(not_zero, on_not_smi_result);
752 movl(dst, tmp);
753 } else {
754 movl(kScratchRegister, Immediate(kSmiTagMask));
755 cmovl(overflow, dst, kScratchRegister);
756 testl(dst, kScratchRegister);
757 j(not_zero, on_not_smi_result);
758 }
759}
760
761
762void MacroAssembler::SmiAddConstant(Register dst,
763 Register src,
764 int32_t constant,
765 Label* on_not_smi_result) {
766 ASSERT(Smi::IsValid(constant));
767 if (on_not_smi_result == NULL) {
768 if (dst.is(src)) {
769 movl(dst, src);
770 } else {
771 lea(dst, Operand(src, constant << kSmiTagSize));
772 }
773 } else {
774 if (!dst.is(src)) {
775 movl(dst, src);
776 }
777 addl(dst, Immediate(Smi::FromInt(constant)));
778 if (!dst.is(src)) {
779 j(overflow, on_not_smi_result);
780 } else {
781 Label result_ok;
782 j(no_overflow, &result_ok);
783 subl(dst, Immediate(Smi::FromInt(constant)));
784 jmp(on_not_smi_result);
785 bind(&result_ok);
786 }
787 }
788}
789
790
791void MacroAssembler::SmiSubConstant(Register dst,
792 Register src,
793 int32_t constant,
794 Label* on_not_smi_result) {
795 ASSERT(Smi::IsValid(constant));
796 Smi* smi_value = Smi::FromInt(constant);
797 if (dst.is(src)) {
798 // Optimistic subtract - may change value of dst register,
799 // if it has garbage bits in the higher half, but will not change
800 // the value as a tagged smi.
801 subl(dst, Immediate(smi_value));
802 if (on_not_smi_result != NULL) {
803 Label add_success;
804 j(no_overflow, &add_success);
805 addl(dst, Immediate(smi_value));
806 jmp(on_not_smi_result);
807 bind(&add_success);
808 }
809 } else {
810 UNIMPLEMENTED(); // Not used yet.
811 }
812}
813
814
815void MacroAssembler::SmiDiv(Register dst,
816 Register src1,
817 Register src2,
818 Label* on_not_smi_result) {
819 ASSERT(!src2.is(rax));
820 ASSERT(!src2.is(rdx));
821 ASSERT(!src1.is(rdx));
822
823 // Check for 0 divisor (result is +/-Infinity).
824 Label positive_divisor;
825 testl(src2, src2);
826 j(zero, on_not_smi_result);
827 j(positive, &positive_divisor);
828 // Check for negative zero result. If the dividend is zero, and the
829 // divisor is negative, return a floating point negative zero.
830 testl(src1, src1);
831 j(zero, on_not_smi_result);
832 bind(&positive_divisor);
833
834 // Sign extend src1 into edx:eax.
835 if (!src1.is(rax)) {
836 movl(rax, src1);
837 }
838 cdq();
839
840 idivl(src2);
841 // Check for the corner case of dividing the most negative smi by
842 // -1. We cannot use the overflow flag, since it is not set by
843 // idiv instruction.
844 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
845 cmpl(rax, Immediate(0x40000000));
846 j(equal, on_not_smi_result);
847 // Check that the remainder is zero.
848 testl(rdx, rdx);
849 j(not_zero, on_not_smi_result);
850 // Tag the result and store it in the destination register.
851 Integer32ToSmi(dst, rax);
852}
853
854
855void MacroAssembler::SmiMod(Register dst,
856 Register src1,
857 Register src2,
858 Label* on_not_smi_result) {
859 ASSERT(!dst.is(kScratchRegister));
860 ASSERT(!src1.is(kScratchRegister));
861 ASSERT(!src2.is(kScratchRegister));
862 ASSERT(!src2.is(rax));
863 ASSERT(!src2.is(rdx));
864 ASSERT(!src1.is(rdx));
865
866 testl(src2, src2);
867 j(zero, on_not_smi_result);
868
869 if (src1.is(rax)) {
870 // Mist remember the value to see if a zero result should
871 // be a negative zero.
872 movl(kScratchRegister, rax);
873 } else {
874 movl(rax, src1);
875 }
876 // Sign extend eax into edx:eax.
877 cdq();
878 idivl(src2);
879 // Check for a negative zero result. If the result is zero, and the
880 // dividend is negative, return a floating point negative zero.
881 Label non_zero_result;
882 testl(rdx, rdx);
883 j(not_zero, &non_zero_result);
884 if (src1.is(rax)) {
885 testl(kScratchRegister, kScratchRegister);
886 } else {
887 testl(src1, src1);
888 }
889 j(negative, on_not_smi_result);
890 bind(&non_zero_result);
891 if (!dst.is(rdx)) {
892 movl(dst, rdx);
893 }
894}
895
896
897void MacroAssembler::SmiNot(Register dst, Register src) {
898 if (dst.is(src)) {
899 not_(dst);
900 // Remove inverted smi-tag. The mask is sign-extended to 64 bits.
901 xor_(src, Immediate(kSmiTagMask));
902 } else {
903 ASSERT_EQ(0, kSmiTag);
904 lea(dst, Operand(src, kSmiTagMask));
905 not_(dst);
906 }
907}
908
909
910void MacroAssembler::SmiAnd(Register dst, Register src1, Register src2) {
911 if (!dst.is(src1)) {
912 movl(dst, src1);
913 }
914 and_(dst, src2);
915}
916
917
918void MacroAssembler::SmiAndConstant(Register dst, Register src, int constant) {
919 ASSERT(Smi::IsValid(constant));
920 if (!dst.is(src)) {
921 movl(dst, src);
922 }
923 and_(dst, Immediate(Smi::FromInt(constant)));
924}
925
926
927void MacroAssembler::SmiOr(Register dst, Register src1, Register src2) {
928 if (!dst.is(src1)) {
929 movl(dst, src1);
930 }
931 or_(dst, src2);
932}
933
934
935void MacroAssembler::SmiOrConstant(Register dst, Register src, int constant) {
936 ASSERT(Smi::IsValid(constant));
937 if (!dst.is(src)) {
938 movl(dst, src);
939 }
940 or_(dst, Immediate(Smi::FromInt(constant)));
941}
942
943void MacroAssembler::SmiXor(Register dst, Register src1, Register src2) {
944 if (!dst.is(src1)) {
945 movl(dst, src1);
946 }
947 xor_(dst, src2);
948}
949
950
951void MacroAssembler::SmiXorConstant(Register dst, Register src, int constant) {
952 ASSERT(Smi::IsValid(constant));
953 if (!dst.is(src)) {
954 movl(dst, src);
955 }
956 xor_(dst, Immediate(Smi::FromInt(constant)));
957}
958
959
960
961void MacroAssembler::SmiShiftArithmeticRightConstant(Register dst,
962 Register src,
963 int shift_value) {
964 if (shift_value > 0) {
965 if (dst.is(src)) {
966 sarl(dst, Immediate(shift_value));
967 and_(dst, Immediate(~kSmiTagMask));
968 } else {
969 UNIMPLEMENTED(); // Not used.
970 }
971 }
972}
973
974
975void MacroAssembler::SmiShiftLogicalRightConstant(Register dst,
976 Register src,
977 int shift_value,
978 Label* on_not_smi_result) {
979 // Logic right shift interprets its result as an *unsigned* number.
980 if (dst.is(src)) {
981 UNIMPLEMENTED(); // Not used.
982 } else {
983 movl(dst, src);
984 // Untag the smi.
985 sarl(dst, Immediate(kSmiTagSize));
986 if (shift_value < 2) {
987 // A negative Smi shifted right two is in the positive Smi range,
988 // but if shifted only by zero or one, it never is.
989 j(negative, on_not_smi_result);
990 }
991 if (shift_value > 0) {
992 // Do the right shift on the integer value.
993 shrl(dst, Immediate(shift_value));
994 }
995 // Re-tag the result.
996 addl(dst, dst);
997 }
998}
999
1000
1001void MacroAssembler::SmiShiftLeftConstant(Register dst,
1002 Register src,
1003 int shift_value,
1004 Label* on_not_smi_result) {
1005 if (dst.is(src)) {
1006 UNIMPLEMENTED(); // Not used.
1007 } else {
1008 movl(dst, src);
1009 if (shift_value > 0) {
1010 // Treat dst as an untagged integer value equal to two times the
1011 // smi value of src, i.e., already shifted left by one.
1012 if (shift_value > 1) {
1013 shll(dst, Immediate(shift_value - 1));
1014 }
1015 // Convert int result to Smi, checking that it is in smi range.
1016 ASSERT(kSmiTagSize == 1); // adjust code if not the case
1017 Integer32ToSmi(dst, dst, on_not_smi_result);
1018 }
1019 }
1020}
1021
1022
1023void MacroAssembler::SmiShiftLeft(Register dst,
1024 Register src1,
1025 Register src2,
1026 Label* on_not_smi_result) {
1027 ASSERT(!dst.is(rcx));
1028 Label result_ok;
1029 // Untag both operands.
1030 SmiToInteger32(dst, src1);
1031 SmiToInteger32(rcx, src2);
1032 shll(dst);
1033 // Check that the *signed* result fits in a smi.
1034 Condition is_valid = CheckInteger32ValidSmiValue(dst);
1035 j(is_valid, &result_ok);
1036 // Restore the relevant bits of the source registers
1037 // and call the slow version.
1038 if (dst.is(src1)) {
1039 shrl(dst);
1040 Integer32ToSmi(dst, dst);
1041 }
1042 Integer32ToSmi(rcx, rcx);
1043 jmp(on_not_smi_result);
1044 bind(&result_ok);
1045 Integer32ToSmi(dst, dst);
1046}
1047
1048
1049void MacroAssembler::SmiShiftLogicalRight(Register dst,
1050 Register src1,
1051 Register src2,
1052 Label* on_not_smi_result) {
1053 ASSERT(!dst.is(rcx));
1054 Label result_ok;
1055 // Untag both operands.
1056 SmiToInteger32(dst, src1);
1057 SmiToInteger32(rcx, src2);
1058
1059 shrl(dst);
1060 // Check that the *unsigned* result fits in a smi.
1061 // I.e., that it is a valid positive smi value. The positive smi
1062 // values are 0..0x3fffffff, i.e., neither of the top-most two
1063 // bits can be set.
1064 //
1065 // These two cases can only happen with shifts by 0 or 1 when
1066 // handed a valid smi. If the answer cannot be represented by a
1067 // smi, restore the left and right arguments, and jump to slow
1068 // case. The low bit of the left argument may be lost, but only
1069 // in a case where it is dropped anyway.
1070 testl(dst, Immediate(0xc0000000));
1071 j(zero, &result_ok);
1072 if (dst.is(src1)) {
1073 shll(dst);
1074 Integer32ToSmi(dst, dst);
1075 }
1076 Integer32ToSmi(rcx, rcx);
1077 jmp(on_not_smi_result);
1078 bind(&result_ok);
1079 // Smi-tag the result in answer.
1080 Integer32ToSmi(dst, dst);
1081}
1082
1083
1084void MacroAssembler::SmiShiftArithmeticRight(Register dst,
1085 Register src1,
1086 Register src2) {
1087 ASSERT(!dst.is(rcx));
1088 // Untag both operands.
1089 SmiToInteger32(dst, src1);
1090 SmiToInteger32(rcx, src2);
1091 // Shift as integer.
1092 sarl(dst);
1093 // Retag result.
1094 Integer32ToSmi(dst, dst);
1095}
1096
1097
1098void MacroAssembler::SelectNonSmi(Register dst,
1099 Register src1,
1100 Register src2,
1101 Label* on_not_smis) {
1102 ASSERT(!dst.is(src1));
1103 ASSERT(!dst.is(src2));
1104 // Both operands must not be smis.
1105#ifdef DEBUG
1106 Condition not_both_smis = CheckNotBothSmi(src1, src2);
1107 Check(not_both_smis, "Both registers were smis.");
1108#endif
1109 ASSERT_EQ(0, kSmiTag);
1110 ASSERT_EQ(0, Smi::FromInt(0));
1111 movq(kScratchRegister, Immediate(kSmiTagMask));
1112 and_(kScratchRegister, src1);
1113 testl(kScratchRegister, src2);
1114 j(not_zero, on_not_smis);
1115 // One operand is a smi.
1116
1117 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1118 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1119 subq(kScratchRegister, Immediate(1));
1120 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1121 movq(dst, src1);
1122 xor_(dst, src2);
1123 and_(dst, kScratchRegister);
1124 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1125 xor_(dst, src1);
1126 // If src1 is a smi, dst is src2, else it is src1, i.e., a non-smi.
1127}
1128
1129
1130SmiIndex MacroAssembler::SmiToIndex(Register dst, Register src, int shift) {
1131 ASSERT(is_uint6(shift));
1132 if (shift == 0) { // times_1.
1133 SmiToInteger32(dst, src);
1134 return SmiIndex(dst, times_1);
1135 }
1136 if (shift <= 4) { // 2 - 16 times multiplier is handled using ScaleFactor.
1137 // We expect that all smis are actually zero-padded. If this holds after
1138 // checking, this line can be omitted.
1139 movl(dst, src); // Ensure that the smi is zero-padded.
1140 return SmiIndex(dst, static_cast<ScaleFactor>(shift - kSmiTagSize));
1141 }
1142 // Shift by shift-kSmiTagSize.
1143 movl(dst, src); // Ensure that the smi is zero-padded.
1144 shl(dst, Immediate(shift - kSmiTagSize));
1145 return SmiIndex(dst, times_1);
1146}
1147
1148
1149SmiIndex MacroAssembler::SmiToNegativeIndex(Register dst,
1150 Register src,
1151 int shift) {
1152 // Register src holds a positive smi.
1153 ASSERT(is_uint6(shift));
1154 if (shift == 0) { // times_1.
1155 SmiToInteger32(dst, src);
1156 neg(dst);
1157 return SmiIndex(dst, times_1);
1158 }
1159 if (shift <= 4) { // 2 - 16 times multiplier is handled using ScaleFactor.
1160 movl(dst, src);
1161 neg(dst);
1162 return SmiIndex(dst, static_cast<ScaleFactor>(shift - kSmiTagSize));
1163 }
1164 // Shift by shift-kSmiTagSize.
1165 movl(dst, src);
1166 neg(dst);
1167 shl(dst, Immediate(shift - kSmiTagSize));
1168 return SmiIndex(dst, times_1);
1169}
1170
1171
1172
1173bool MacroAssembler::IsUnsafeSmi(Smi* value) {
1174 return false;
1175}
1176
1177void MacroAssembler::LoadUnsafeSmi(Register dst, Smi* source) {
1178 UNIMPLEMENTED();
1179}
1180
1181
1182void MacroAssembler::Move(Register dst, Handle<Object> source) {
1183 ASSERT(!source->IsFailure());
1184 if (source->IsSmi()) {
1185 if (IsUnsafeSmi(source)) {
1186 LoadUnsafeSmi(dst, source);
1187 } else {
1188 int32_t smi = static_cast<int32_t>(reinterpret_cast<intptr_t>(*source));
1189 movq(dst, Immediate(smi));
1190 }
1191 } else {
1192 movq(dst, source, RelocInfo::EMBEDDED_OBJECT);
1193 }
1194}
1195
1196
1197void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
1198 if (source->IsSmi()) {
1199 int32_t smi = static_cast<int32_t>(reinterpret_cast<intptr_t>(*source));
1200 movq(dst, Immediate(smi));
1201 } else {
1202 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1203 movq(dst, kScratchRegister);
1204 }
1205}
1206
1207
1208void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
1209 Move(kScratchRegister, source);
1210 cmpq(dst, kScratchRegister);
1211}
1212
1213
1214void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
1215 if (source->IsSmi()) {
1216 if (IsUnsafeSmi(source)) {
1217 LoadUnsafeSmi(kScratchRegister, source);
1218 cmpl(dst, kScratchRegister);
1219 } else {
1220 // For smi-comparison, it suffices to compare the low 32 bits.
1221 int32_t smi = static_cast<int32_t>(reinterpret_cast<intptr_t>(*source));
1222 cmpl(dst, Immediate(smi));
1223 }
1224 } else {
1225 ASSERT(source->IsHeapObject());
1226 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1227 cmpq(dst, kScratchRegister);
1228 }
1229}
1230
1231
1232void MacroAssembler::Push(Handle<Object> source) {
1233 if (source->IsSmi()) {
1234 if (IsUnsafeSmi(source)) {
1235 LoadUnsafeSmi(kScratchRegister, source);
1236 push(kScratchRegister);
1237 } else {
1238 int32_t smi = static_cast<int32_t>(reinterpret_cast<intptr_t>(*source));
1239 push(Immediate(smi));
1240 }
1241 } else {
1242 ASSERT(source->IsHeapObject());
1243 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1244 push(kScratchRegister);
1245 }
1246}
1247
1248
1249void MacroAssembler::Push(Smi* source) {
1250 if (IsUnsafeSmi(source)) {
1251 LoadUnsafeSmi(kScratchRegister, source);
1252 push(kScratchRegister);
1253 } else {
1254 int32_t smi = static_cast<int32_t>(reinterpret_cast<intptr_t>(source));
1255 push(Immediate(smi));
1256 }
1257}
1258
1259
1260void MacroAssembler::Jump(ExternalReference ext) {
1261 movq(kScratchRegister, ext);
1262 jmp(kScratchRegister);
1263}
1264
1265
1266void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
1267 movq(kScratchRegister, destination, rmode);
1268 jmp(kScratchRegister);
1269}
1270
1271
1272void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
1273 ASSERT(RelocInfo::IsCodeTarget(rmode));
1274 movq(kScratchRegister, code_object, rmode);
1275#ifdef DEBUG
1276 Label target;
1277 bind(&target);
1278#endif
1279 jmp(kScratchRegister);
1280#ifdef DEBUG
1281 ASSERT_EQ(kCallTargetAddressOffset,
1282 SizeOfCodeGeneratedSince(&target) + kPointerSize);
1283#endif
1284}
1285
1286
1287void MacroAssembler::Call(ExternalReference ext) {
1288 movq(kScratchRegister, ext);
1289 call(kScratchRegister);
1290}
1291
1292
1293void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
1294 movq(kScratchRegister, destination, rmode);
1295 call(kScratchRegister);
1296}
1297
1298
1299void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
1300 ASSERT(RelocInfo::IsCodeTarget(rmode));
1301 WriteRecordedPositions();
1302 movq(kScratchRegister, code_object, rmode);
1303#ifdef DEBUG
1304 // Patch target is kPointer size bytes *before* target label.
1305 Label target;
1306 bind(&target);
1307#endif
1308 call(kScratchRegister);
1309#ifdef DEBUG
1310 ASSERT_EQ(kCallTargetAddressOffset,
1311 SizeOfCodeGeneratedSince(&target) + kPointerSize);
1312#endif
1313}
1314
1315
1316void MacroAssembler::PushTryHandler(CodeLocation try_location,
1317 HandlerType type) {
1318 // Adjust this code if not the case.
1319 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1320
1321 // The pc (return address) is already on TOS. This code pushes state,
1322 // frame pointer and current handler. Check that they are expected
1323 // next on the stack, in that order.
1324 ASSERT_EQ(StackHandlerConstants::kStateOffset,
1325 StackHandlerConstants::kPCOffset - kPointerSize);
1326 ASSERT_EQ(StackHandlerConstants::kFPOffset,
1327 StackHandlerConstants::kStateOffset - kPointerSize);
1328 ASSERT_EQ(StackHandlerConstants::kNextOffset,
1329 StackHandlerConstants::kFPOffset - kPointerSize);
1330
1331 if (try_location == IN_JAVASCRIPT) {
1332 if (type == TRY_CATCH_HANDLER) {
1333 push(Immediate(StackHandler::TRY_CATCH));
1334 } else {
1335 push(Immediate(StackHandler::TRY_FINALLY));
1336 }
1337 push(rbp);
1338 } else {
1339 ASSERT(try_location == IN_JS_ENTRY);
1340 // The frame pointer does not point to a JS frame so we save NULL
1341 // for rbp. We expect the code throwing an exception to check rbp
1342 // before dereferencing it to restore the context.
1343 push(Immediate(StackHandler::ENTRY));
1344 push(Immediate(0)); // NULL frame pointer.
1345 }
1346 // Save the current handler.
1347 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
1348 push(Operand(kScratchRegister, 0));
1349 // Link this handler.
1350 movq(Operand(kScratchRegister, 0), rsp);
1351}
1352
1353
1354void MacroAssembler::Ret() {
1355 ret(0);
1356}
1357
1358
1359void MacroAssembler::FCmp() {
1360 fucompp();
1361 push(rax);
1362 fnstsw_ax();
1363 if (CpuFeatures::IsSupported(CpuFeatures::SAHF)) {
1364 sahf();
1365 } else {
1366 shrl(rax, Immediate(8));
1367 and_(rax, Immediate(0xFF));
1368 push(rax);
1369 popfq();
1370 }
1371 pop(rax);
1372}
1373
1374
1375void MacroAssembler::CmpObjectType(Register heap_object,
1376 InstanceType type,
1377 Register map) {
1378 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
1379 CmpInstanceType(map, type);
1380}
1381
1382
1383void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
1384 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
1385 Immediate(static_cast<int8_t>(type)));
1386}
1387
1388
1389void MacroAssembler::TryGetFunctionPrototype(Register function,
1390 Register result,
1391 Label* miss) {
1392 // Check that the receiver isn't a smi.
1393 testl(function, Immediate(kSmiTagMask));
1394 j(zero, miss);
1395
1396 // Check that the function really is a function.
1397 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1398 j(not_equal, miss);
1399
1400 // Make sure that the function has an instance prototype.
1401 Label non_instance;
1402 testb(FieldOperand(result, Map::kBitFieldOffset),
1403 Immediate(1 << Map::kHasNonInstancePrototype));
1404 j(not_zero, &non_instance);
1405
1406 // Get the prototype or initial map from the function.
1407 movq(result,
1408 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1409
1410 // If the prototype or initial map is the hole, don't return it and
1411 // simply miss the cache instead. This will allow us to allocate a
1412 // prototype object on-demand in the runtime system.
1413 CompareRoot(result, Heap::kTheHoleValueRootIndex);
1414 j(equal, miss);
1415
1416 // If the function does not have an initial map, we're done.
1417 Label done;
1418 CmpObjectType(result, MAP_TYPE, kScratchRegister);
1419 j(not_equal, &done);
1420
1421 // Get the prototype from the initial map.
1422 movq(result, FieldOperand(result, Map::kPrototypeOffset));
1423 jmp(&done);
1424
1425 // Non-instance prototype: Fetch prototype from constructor field
1426 // in initial map.
1427 bind(&non_instance);
1428 movq(result, FieldOperand(result, Map::kConstructorOffset));
1429
1430 // All done.
1431 bind(&done);
1432}
1433
1434
1435void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1436 if (FLAG_native_code_counters && counter->Enabled()) {
1437 movq(kScratchRegister, ExternalReference(counter));
1438 movl(Operand(kScratchRegister, 0), Immediate(value));
1439 }
1440}
1441
1442
1443void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1444 ASSERT(value > 0);
1445 if (FLAG_native_code_counters && counter->Enabled()) {
1446 movq(kScratchRegister, ExternalReference(counter));
1447 Operand operand(kScratchRegister, 0);
1448 if (value == 1) {
1449 incl(operand);
1450 } else {
1451 addl(operand, Immediate(value));
1452 }
1453 }
1454}
1455
1456
1457void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1458 ASSERT(value > 0);
1459 if (FLAG_native_code_counters && counter->Enabled()) {
1460 movq(kScratchRegister, ExternalReference(counter));
1461 Operand operand(kScratchRegister, 0);
1462 if (value == 1) {
1463 decl(operand);
1464 } else {
1465 subl(operand, Immediate(value));
1466 }
1467 }
1468}
1469
1470
1471#ifdef ENABLE_DEBUGGER_SUPPORT
1472
1473void MacroAssembler::PushRegistersFromMemory(RegList regs) {
1474 ASSERT((regs & ~kJSCallerSaved) == 0);
1475 // Push the content of the memory location to the stack.
1476 for (int i = 0; i < kNumJSCallerSaved; i++) {
1477 int r = JSCallerSavedCode(i);
1478 if ((regs & (1 << r)) != 0) {
1479 ExternalReference reg_addr =
1480 ExternalReference(Debug_Address::Register(i));
1481 movq(kScratchRegister, reg_addr);
1482 push(Operand(kScratchRegister, 0));
1483 }
1484 }
1485}
1486
1487void MacroAssembler::SaveRegistersToMemory(RegList regs) {
1488 ASSERT((regs & ~kJSCallerSaved) == 0);
1489 // Copy the content of registers to memory location.
1490 for (int i = 0; i < kNumJSCallerSaved; i++) {
1491 int r = JSCallerSavedCode(i);
1492 if ((regs & (1 << r)) != 0) {
1493 Register reg = { r };
1494 ExternalReference reg_addr =
1495 ExternalReference(Debug_Address::Register(i));
1496 movq(kScratchRegister, reg_addr);
1497 movq(Operand(kScratchRegister, 0), reg);
1498 }
1499 }
1500}
1501
1502
1503void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
1504 ASSERT((regs & ~kJSCallerSaved) == 0);
1505 // Copy the content of memory location to registers.
1506 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1507 int r = JSCallerSavedCode(i);
1508 if ((regs & (1 << r)) != 0) {
1509 Register reg = { r };
1510 ExternalReference reg_addr =
1511 ExternalReference(Debug_Address::Register(i));
1512 movq(kScratchRegister, reg_addr);
1513 movq(reg, Operand(kScratchRegister, 0));
1514 }
1515 }
1516}
1517
1518
1519void MacroAssembler::PopRegistersToMemory(RegList regs) {
1520 ASSERT((regs & ~kJSCallerSaved) == 0);
1521 // Pop the content from the stack to the memory location.
1522 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1523 int r = JSCallerSavedCode(i);
1524 if ((regs & (1 << r)) != 0) {
1525 ExternalReference reg_addr =
1526 ExternalReference(Debug_Address::Register(i));
1527 movq(kScratchRegister, reg_addr);
1528 pop(Operand(kScratchRegister, 0));
1529 }
1530 }
1531}
1532
1533
1534void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
1535 Register scratch,
1536 RegList regs) {
1537 ASSERT(!scratch.is(kScratchRegister));
1538 ASSERT(!base.is(kScratchRegister));
1539 ASSERT(!base.is(scratch));
1540 ASSERT((regs & ~kJSCallerSaved) == 0);
1541 // Copy the content of the stack to the memory location and adjust base.
1542 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1543 int r = JSCallerSavedCode(i);
1544 if ((regs & (1 << r)) != 0) {
1545 movq(scratch, Operand(base, 0));
1546 ExternalReference reg_addr =
1547 ExternalReference(Debug_Address::Register(i));
1548 movq(kScratchRegister, reg_addr);
1549 movq(Operand(kScratchRegister, 0), scratch);
1550 lea(base, Operand(base, kPointerSize));
1551 }
1552 }
1553}
1554
1555#endif // ENABLE_DEBUGGER_SUPPORT
1556
1557
1558void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
1559 bool resolved;
1560 Handle<Code> code = ResolveBuiltin(id, &resolved);
1561
1562 // Calls are not allowed in some stubs.
1563 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1564
1565 // Rely on the assertion to check that the number of provided
1566 // arguments match the expected number of arguments. Fake a
1567 // parameter count to avoid emitting code to do the check.
1568 ParameterCount expected(0);
1569 InvokeCode(Handle<Code>(code), expected, expected,
1570 RelocInfo::CODE_TARGET, flag);
1571
1572 const char* name = Builtins::GetName(id);
1573 int argc = Builtins::GetArgumentsCount(id);
1574 // The target address for the jump is stored as an immediate at offset
1575 // kInvokeCodeAddressOffset.
1576 if (!resolved) {
1577 uint32_t flags =
1578 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
1579 Bootstrapper::FixupFlagsIsPCRelative::encode(false) |
1580 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
1581 Unresolved entry =
1582 { pc_offset() - kCallTargetAddressOffset, flags, name };
1583 unresolved_.Add(entry);
1584 }
1585}
1586
1587
1588void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1589 const ParameterCount& actual,
1590 Handle<Code> code_constant,
1591 Register code_register,
1592 Label* done,
1593 InvokeFlag flag) {
1594 bool definitely_matches = false;
1595 Label invoke;
1596 if (expected.is_immediate()) {
1597 ASSERT(actual.is_immediate());
1598 if (expected.immediate() == actual.immediate()) {
1599 definitely_matches = true;
1600 } else {
1601 movq(rax, Immediate(actual.immediate()));
1602 if (expected.immediate() ==
1603 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1604 // Don't worry about adapting arguments for built-ins that
1605 // don't want that done. Skip adaption code by making it look
1606 // like we have a match between expected and actual number of
1607 // arguments.
1608 definitely_matches = true;
1609 } else {
1610 movq(rbx, Immediate(expected.immediate()));
1611 }
1612 }
1613 } else {
1614 if (actual.is_immediate()) {
1615 // Expected is in register, actual is immediate. This is the
1616 // case when we invoke function values without going through the
1617 // IC mechanism.
1618 cmpq(expected.reg(), Immediate(actual.immediate()));
1619 j(equal, &invoke);
1620 ASSERT(expected.reg().is(rbx));
1621 movq(rax, Immediate(actual.immediate()));
1622 } else if (!expected.reg().is(actual.reg())) {
1623 // Both expected and actual are in (different) registers. This
1624 // is the case when we invoke functions using call and apply.
1625 cmpq(expected.reg(), actual.reg());
1626 j(equal, &invoke);
1627 ASSERT(actual.reg().is(rax));
1628 ASSERT(expected.reg().is(rbx));
1629 }
1630 }
1631
1632 if (!definitely_matches) {
1633 Handle<Code> adaptor =
1634 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1635 if (!code_constant.is_null()) {
1636 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1637 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1638 } else if (!code_register.is(rdx)) {
1639 movq(rdx, code_register);
1640 }
1641
1642 if (flag == CALL_FUNCTION) {
1643 Call(adaptor, RelocInfo::CODE_TARGET);
1644 jmp(done);
1645 } else {
1646 Jump(adaptor, RelocInfo::CODE_TARGET);
1647 }
1648 bind(&invoke);
1649 }
1650}
1651
1652
1653void MacroAssembler::InvokeCode(Register code,
1654 const ParameterCount& expected,
1655 const ParameterCount& actual,
1656 InvokeFlag flag) {
1657 Label done;
1658 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1659 if (flag == CALL_FUNCTION) {
1660 call(code);
1661 } else {
1662 ASSERT(flag == JUMP_FUNCTION);
1663 jmp(code);
1664 }
1665 bind(&done);
1666}
1667
1668
1669void MacroAssembler::InvokeCode(Handle<Code> code,
1670 const ParameterCount& expected,
1671 const ParameterCount& actual,
1672 RelocInfo::Mode rmode,
1673 InvokeFlag flag) {
1674 Label done;
1675 Register dummy = rax;
1676 InvokePrologue(expected, actual, code, dummy, &done, flag);
1677 if (flag == CALL_FUNCTION) {
1678 Call(code, rmode);
1679 } else {
1680 ASSERT(flag == JUMP_FUNCTION);
1681 Jump(code, rmode);
1682 }
1683 bind(&done);
1684}
1685
1686
1687void MacroAssembler::InvokeFunction(Register function,
1688 const ParameterCount& actual,
1689 InvokeFlag flag) {
1690 ASSERT(function.is(rdi));
1691 movq(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1692 movq(rsi, FieldOperand(function, JSFunction::kContextOffset));
1693 movsxlq(rbx,
1694 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
1695 movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
1696 // Advances rdx to the end of the Code object header, to the start of
1697 // the executable code.
1698 lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
1699
1700 ParameterCount expected(rbx);
1701 InvokeCode(rdx, expected, actual, flag);
1702}
1703
1704
1705void MacroAssembler::EnterFrame(StackFrame::Type type) {
1706 push(rbp);
1707 movq(rbp, rsp);
1708 push(rsi); // Context.
1709 push(Immediate(Smi::FromInt(type)));
1710 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
1711 push(kScratchRegister);
1712 if (FLAG_debug_code) {
1713 movq(kScratchRegister,
1714 Factory::undefined_value(),
1715 RelocInfo::EMBEDDED_OBJECT);
1716 cmpq(Operand(rsp, 0), kScratchRegister);
1717 Check(not_equal, "code object not properly patched");
1718 }
1719}
1720
1721
1722void MacroAssembler::LeaveFrame(StackFrame::Type type) {
1723 if (FLAG_debug_code) {
1724 movq(kScratchRegister, Immediate(Smi::FromInt(type)));
1725 cmpq(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
1726 Check(equal, "stack frame types must match");
1727 }
1728 movq(rsp, rbp);
1729 pop(rbp);
1730}
1731
1732
1733
1734void MacroAssembler::EnterExitFrame(StackFrame::Type type, int result_size) {
1735 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
1736
1737 // Setup the frame structure on the stack.
1738 // All constants are relative to the frame pointer of the exit frame.
1739 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
1740 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
1741 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
1742 push(rbp);
1743 movq(rbp, rsp);
1744
1745 // Reserve room for entry stack pointer and push the debug marker.
1746 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
1747 push(Immediate(0)); // saved entry sp, patched before call
1748 push(Immediate(type == StackFrame::EXIT_DEBUG ? 1 : 0));
1749
1750 // Save the frame pointer and the context in top.
1751 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
1752 ExternalReference context_address(Top::k_context_address);
1753 movq(r14, rax); // Backup rax before we use it.
1754
1755 movq(rax, rbp);
1756 store_rax(c_entry_fp_address);
1757 movq(rax, rsi);
1758 store_rax(context_address);
1759
1760 // Setup argv in callee-saved register r15. It is reused in LeaveExitFrame,
1761 // so it must be retained across the C-call.
1762 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
1763 lea(r15, Operand(rbp, r14, times_pointer_size, offset));
1764
1765#ifdef ENABLE_DEBUGGER_SUPPORT
1766 // Save the state of all registers to the stack from the memory
1767 // location. This is needed to allow nested break points.
1768 if (type == StackFrame::EXIT_DEBUG) {
1769 // TODO(1243899): This should be symmetric to
1770 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
1771 // correct here, but computed for the other call. Very error
1772 // prone! FIX THIS. Actually there are deeper problems with
1773 // register saving than this asymmetry (see the bug report
1774 // associated with this issue).
1775 PushRegistersFromMemory(kJSCallerSaved);
1776 }
1777#endif
1778
1779#ifdef _WIN64
1780 // Reserve space on stack for result and argument structures, if necessary.
1781 int result_stack_space = (result_size < 2) ? 0 : result_size * kPointerSize;
1782 // Reserve space for the Arguments object. The Windows 64-bit ABI
1783 // requires us to pass this structure as a pointer to its location on
1784 // the stack. The structure contains 2 values.
1785 int argument_stack_space = 2 * kPointerSize;
1786 // We also need backing space for 4 parameters, even though
1787 // we only pass one or two parameter, and it is in a register.
1788 int argument_mirror_space = 4 * kPointerSize;
1789 int total_stack_space =
1790 argument_mirror_space + argument_stack_space + result_stack_space;
1791 subq(rsp, Immediate(total_stack_space));
1792#endif
1793
1794 // Get the required frame alignment for the OS.
1795 static const int kFrameAlignment = OS::ActivationFrameAlignment();
1796 if (kFrameAlignment > 0) {
1797 ASSERT(IsPowerOf2(kFrameAlignment));
1798 movq(kScratchRegister, Immediate(-kFrameAlignment));
1799 and_(rsp, kScratchRegister);
1800 }
1801
1802 // Patch the saved entry sp.
1803 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
1804}
1805
1806
1807void MacroAssembler::LeaveExitFrame(StackFrame::Type type, int result_size) {
1808 // Registers:
1809 // r15 : argv
1810#ifdef ENABLE_DEBUGGER_SUPPORT
1811 // Restore the memory copy of the registers by digging them out from
1812 // the stack. This is needed to allow nested break points.
1813 if (type == StackFrame::EXIT_DEBUG) {
1814 // It's okay to clobber register rbx below because we don't need
1815 // the function pointer after this.
1816 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
1817 int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
1818 lea(rbx, Operand(rbp, kOffset));
1819 CopyRegistersFromStackToMemory(rbx, rcx, kJSCallerSaved);
1820 }
1821#endif
1822
1823 // Get the return address from the stack and restore the frame pointer.
1824 movq(rcx, Operand(rbp, 1 * kPointerSize));
1825 movq(rbp, Operand(rbp, 0 * kPointerSize));
1826
1827#ifdef _WIN64
1828 // If return value is on the stack, pop it to registers.
1829 if (result_size > 1) {
1830 ASSERT_EQ(2, result_size);
1831 // Position above 4 argument mirrors and arguments object.
1832 movq(rax, Operand(rsp, 6 * kPointerSize));
1833 movq(rdx, Operand(rsp, 7 * kPointerSize));
1834 }
1835#endif
1836
1837 // Pop everything up to and including the arguments and the receiver
1838 // from the caller stack.
1839 lea(rsp, Operand(r15, 1 * kPointerSize));
1840
1841 // Restore current context from top and clear it in debug mode.
1842 ExternalReference context_address(Top::k_context_address);
1843 movq(kScratchRegister, context_address);
1844 movq(rsi, Operand(kScratchRegister, 0));
1845#ifdef DEBUG
1846 movq(Operand(kScratchRegister, 0), Immediate(0));
1847#endif
1848
1849 // Push the return address to get ready to return.
1850 push(rcx);
1851
1852 // Clear the top frame.
1853 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
1854 movq(kScratchRegister, c_entry_fp_address);
1855 movq(Operand(kScratchRegister, 0), Immediate(0));
1856}
1857
1858
1859Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
1860 JSObject* holder, Register holder_reg,
1861 Register scratch,
1862 Label* miss) {
1863 // Make sure there's no overlap between scratch and the other
1864 // registers.
1865 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
1866
1867 // Keep track of the current object in register reg. On the first
1868 // iteration, reg is an alias for object_reg, on later iterations,
1869 // it is an alias for holder_reg.
1870 Register reg = object_reg;
1871 int depth = 1;
1872
1873 // Check the maps in the prototype chain.
1874 // Traverse the prototype chain from the object and do map checks.
1875 while (object != holder) {
1876 depth++;
1877
1878 // Only global objects and objects that do not require access
1879 // checks are allowed in stubs.
1880 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
1881
1882 JSObject* prototype = JSObject::cast(object->GetPrototype());
1883 if (Heap::InNewSpace(prototype)) {
1884 // Get the map of the current object.
1885 movq(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1886 Cmp(scratch, Handle<Map>(object->map()));
1887 // Branch on the result of the map check.
1888 j(not_equal, miss);
1889 // Check access rights to the global object. This has to happen
1890 // after the map check so that we know that the object is
1891 // actually a global object.
1892 if (object->IsJSGlobalProxy()) {
1893 CheckAccessGlobalProxy(reg, scratch, miss);
1894
1895 // Restore scratch register to be the map of the object.
1896 // We load the prototype from the map in the scratch register.
1897 movq(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1898 }
1899 // The prototype is in new space; we cannot store a reference
1900 // to it in the code. Load it from the map.
1901 reg = holder_reg; // from now the object is in holder_reg
1902 movq(reg, FieldOperand(scratch, Map::kPrototypeOffset));
1903
1904 } else {
1905 // Check the map of the current object.
1906 Cmp(FieldOperand(reg, HeapObject::kMapOffset),
1907 Handle<Map>(object->map()));
1908 // Branch on the result of the map check.
1909 j(not_equal, miss);
1910 // Check access rights to the global object. This has to happen
1911 // after the map check so that we know that the object is
1912 // actually a global object.
1913 if (object->IsJSGlobalProxy()) {
1914 CheckAccessGlobalProxy(reg, scratch, miss);
1915 }
1916 // The prototype is in old space; load it directly.
1917 reg = holder_reg; // from now the object is in holder_reg
1918 Move(reg, Handle<JSObject>(prototype));
1919 }
1920
1921 // Go to the next object in the prototype chain.
1922 object = prototype;
1923 }
1924
1925 // Check the holder map.
1926 Cmp(FieldOperand(reg, HeapObject::kMapOffset),
1927 Handle<Map>(holder->map()));
1928 j(not_equal, miss);
1929
1930 // Log the check depth.
1931 LOG(IntEvent("check-maps-depth", depth));
1932
1933 // Perform security check for access to the global object and return
1934 // the holder register.
1935 ASSERT(object == holder);
1936 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
1937 if (object->IsJSGlobalProxy()) {
1938 CheckAccessGlobalProxy(reg, scratch, miss);
1939 }
1940 return reg;
1941}
1942
1943
1944
1945
1946void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1947 Register scratch,
1948 Label* miss) {
1949 Label same_contexts;
1950
1951 ASSERT(!holder_reg.is(scratch));
1952 ASSERT(!scratch.is(kScratchRegister));
1953 // Load current lexical context from the stack frame.
1954 movq(scratch, Operand(rbp, StandardFrameConstants::kContextOffset));
1955
1956 // When generating debug code, make sure the lexical context is set.
1957 if (FLAG_debug_code) {
1958 cmpq(scratch, Immediate(0));
1959 Check(not_equal, "we should not have an empty lexical context");
1960 }
1961 // Load the global context of the current context.
1962 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1963 movq(scratch, FieldOperand(scratch, offset));
1964 movq(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
1965
1966 // Check the context is a global context.
1967 if (FLAG_debug_code) {
1968 Cmp(FieldOperand(scratch, HeapObject::kMapOffset),
1969 Factory::global_context_map());
1970 Check(equal, "JSGlobalObject::global_context should be a global context.");
1971 }
1972
1973 // Check if both contexts are the same.
1974 cmpq(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
1975 j(equal, &same_contexts);
1976
1977 // Compare security tokens.
1978 // Check that the security token in the calling global object is
1979 // compatible with the security token in the receiving global
1980 // object.
1981
1982 // Check the context is a global context.
1983 if (FLAG_debug_code) {
1984 // Preserve original value of holder_reg.
1985 push(holder_reg);
1986 movq(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
1987 CompareRoot(holder_reg, Heap::kNullValueRootIndex);
1988 Check(not_equal, "JSGlobalProxy::context() should not be null.");
1989
1990 // Read the first word and compare to global_context_map(),
1991 movq(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
1992 CompareRoot(holder_reg, Heap::kGlobalContextMapRootIndex);
1993 Check(equal, "JSGlobalObject::global_context should be a global context.");
1994 pop(holder_reg);
1995 }
1996
1997 movq(kScratchRegister,
1998 FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
1999 int token_offset = Context::kHeaderSize +
2000 Context::SECURITY_TOKEN_INDEX * kPointerSize;
2001 movq(scratch, FieldOperand(scratch, token_offset));
2002 cmpq(scratch, FieldOperand(kScratchRegister, token_offset));
2003 j(not_equal, miss);
2004
2005 bind(&same_contexts);
2006}
2007
2008
2009void MacroAssembler::LoadAllocationTopHelper(Register result,
2010 Register result_end,
2011 Register scratch,
2012 AllocationFlags flags) {
2013 ExternalReference new_space_allocation_top =
2014 ExternalReference::new_space_allocation_top_address();
2015
2016 // Just return if allocation top is already known.
2017 if ((flags & RESULT_CONTAINS_TOP) != 0) {
2018 // No use of scratch if allocation top is provided.
2019 ASSERT(scratch.is(no_reg));
2020#ifdef DEBUG
2021 // Assert that result actually contains top on entry.
2022 movq(kScratchRegister, new_space_allocation_top);
2023 cmpq(result, Operand(kScratchRegister, 0));
2024 Check(equal, "Unexpected allocation top");
2025#endif
2026 return;
2027 }
2028
2029 // Move address of new object to result. Use scratch register if available.
2030 if (scratch.is(no_reg)) {
2031 movq(kScratchRegister, new_space_allocation_top);
2032 movq(result, Operand(kScratchRegister, 0));
2033 } else {
2034 ASSERT(!scratch.is(result_end));
2035 movq(scratch, new_space_allocation_top);
2036 movq(result, Operand(scratch, 0));
2037 }
2038}
2039
2040
2041void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
2042 Register scratch) {
2043 ExternalReference new_space_allocation_top =
2044 ExternalReference::new_space_allocation_top_address();
2045
2046 // Update new top.
2047 if (result_end.is(rax)) {
2048 // rax can be stored directly to a memory location.
2049 store_rax(new_space_allocation_top);
2050 } else {
2051 // Register required - use scratch provided if available.
2052 if (scratch.is(no_reg)) {
2053 movq(kScratchRegister, new_space_allocation_top);
2054 movq(Operand(kScratchRegister, 0), result_end);
2055 } else {
2056 movq(Operand(scratch, 0), result_end);
2057 }
2058 }
2059}
2060
2061
2062void MacroAssembler::AllocateInNewSpace(int object_size,
2063 Register result,
2064 Register result_end,
2065 Register scratch,
2066 Label* gc_required,
2067 AllocationFlags flags) {
2068 ASSERT(!result.is(result_end));
2069
2070 // Load address of new object into result.
2071 LoadAllocationTopHelper(result, result_end, scratch, flags);
2072
2073 // Calculate new top and bail out if new space is exhausted.
2074 ExternalReference new_space_allocation_limit =
2075 ExternalReference::new_space_allocation_limit_address();
2076 lea(result_end, Operand(result, object_size));
2077 movq(kScratchRegister, new_space_allocation_limit);
2078 cmpq(result_end, Operand(kScratchRegister, 0));
2079 j(above, gc_required);
2080
2081 // Update allocation top.
2082 UpdateAllocationTopHelper(result_end, scratch);
2083
2084 // Tag the result if requested.
2085 if ((flags & TAG_OBJECT) != 0) {
2086 addq(result, Immediate(kHeapObjectTag));
2087 }
2088}
2089
2090
2091void MacroAssembler::AllocateInNewSpace(int header_size,
2092 ScaleFactor element_size,
2093 Register element_count,
2094 Register result,
2095 Register result_end,
2096 Register scratch,
2097 Label* gc_required,
2098 AllocationFlags flags) {
2099 ASSERT(!result.is(result_end));
2100
2101 // Load address of new object into result.
2102 LoadAllocationTopHelper(result, result_end, scratch, flags);
2103
2104 // Calculate new top and bail out if new space is exhausted.
2105 ExternalReference new_space_allocation_limit =
2106 ExternalReference::new_space_allocation_limit_address();
2107 lea(result_end, Operand(result, element_count, element_size, header_size));
2108 movq(kScratchRegister, new_space_allocation_limit);
2109 cmpq(result_end, Operand(kScratchRegister, 0));
2110 j(above, gc_required);
2111
2112 // Update allocation top.
2113 UpdateAllocationTopHelper(result_end, scratch);
2114
2115 // Tag the result if requested.
2116 if ((flags & TAG_OBJECT) != 0) {
2117 addq(result, Immediate(kHeapObjectTag));
2118 }
2119}
2120
2121
2122void MacroAssembler::AllocateInNewSpace(Register object_size,
2123 Register result,
2124 Register result_end,
2125 Register scratch,
2126 Label* gc_required,
2127 AllocationFlags flags) {
2128 // Load address of new object into result.
2129 LoadAllocationTopHelper(result, result_end, scratch, flags);
2130
2131 // Calculate new top and bail out if new space is exhausted.
2132 ExternalReference new_space_allocation_limit =
2133 ExternalReference::new_space_allocation_limit_address();
2134 if (!object_size.is(result_end)) {
2135 movq(result_end, object_size);
2136 }
2137 addq(result_end, result);
2138 movq(kScratchRegister, new_space_allocation_limit);
2139 cmpq(result_end, Operand(kScratchRegister, 0));
2140 j(above, gc_required);
2141
2142 // Update allocation top.
2143 UpdateAllocationTopHelper(result_end, scratch);
2144
2145 // Tag the result if requested.
2146 if ((flags & TAG_OBJECT) != 0) {
2147 addq(result, Immediate(kHeapObjectTag));
2148 }
2149}
2150
2151
2152void MacroAssembler::UndoAllocationInNewSpace(Register object) {
2153 ExternalReference new_space_allocation_top =
2154 ExternalReference::new_space_allocation_top_address();
2155
2156 // Make sure the object has no tag before resetting top.
2157 and_(object, Immediate(~kHeapObjectTagMask));
2158 movq(kScratchRegister, new_space_allocation_top);
2159#ifdef DEBUG
2160 cmpq(object, Operand(kScratchRegister, 0));
2161 Check(below, "Undo allocation of non allocated memory");
2162#endif
2163 movq(Operand(kScratchRegister, 0), object);
2164}
2165
2166
2167CodePatcher::CodePatcher(byte* address, int size)
2168 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
2169 // Create a new macro assembler pointing to the address of the code to patch.
2170 // The size is adjusted with kGap on order for the assembler to generate size
2171 // bytes of instructions without failing with buffer size constraints.
2172 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2173}
2174
2175
2176CodePatcher::~CodePatcher() {
2177 // Indicate that code has changed.
2178 CPU::FlushICache(address_, size_);
2179
2180 // Check that the code was patched as expected.
2181 ASSERT(masm_.pc_ == address_ + size_);
2182 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2183}
2184
2185
2186} } // namespace v8::internal