blob: b2f69bb7a99de3047e2d9a0760793957f029fd10 [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)
Steve Block3ce2e202009-11-05 08:53:23 +000041 : Assembler(buffer, size),
42 unresolved_(0),
43 generating_stub_(false),
44 allow_stub_calls_(true),
45 code_object_(Heap::undefined_value()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000046}
47
48
Steve Block3ce2e202009-11-05 08:53:23 +000049void MacroAssembler::LoadRoot(Register destination, Heap::RootListIndex index) {
Steve Blocka7e24c12009-10-30 11:49:00 +000050 movq(destination, Operand(r13, index << kPointerSizeLog2));
51}
52
53
54void MacroAssembler::PushRoot(Heap::RootListIndex index) {
55 push(Operand(r13, index << kPointerSizeLog2));
56}
57
58
Steve Block3ce2e202009-11-05 08:53:23 +000059void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
Steve Blocka7e24c12009-10-30 11:49:00 +000060 cmpq(with, Operand(r13, index << kPointerSizeLog2));
61}
62
63
Steve Block3ce2e202009-11-05 08:53:23 +000064void MacroAssembler::CompareRoot(Operand with, Heap::RootListIndex index) {
Steve Blocka7e24c12009-10-30 11:49:00 +000065 LoadRoot(kScratchRegister, index);
66 cmpq(with, kScratchRegister);
67}
68
69
70static void RecordWriteHelper(MacroAssembler* masm,
71 Register object,
72 Register addr,
73 Register scratch) {
74 Label fast;
75
76 // Compute the page start address from the heap object pointer, and reuse
77 // the 'object' register for it.
78 ASSERT(is_int32(~Page::kPageAlignmentMask));
79 masm->and_(object,
80 Immediate(static_cast<int32_t>(~Page::kPageAlignmentMask)));
81 Register page_start = object;
82
83 // Compute the bit addr in the remembered set/index of the pointer in the
84 // page. Reuse 'addr' as pointer_offset.
85 masm->subq(addr, page_start);
86 masm->shr(addr, Immediate(kPointerSizeLog2));
87 Register pointer_offset = addr;
88
89 // If the bit offset lies beyond the normal remembered set range, it is in
90 // the extra remembered set area of a large object.
91 masm->cmpq(pointer_offset, Immediate(Page::kPageSize / kPointerSize));
92 masm->j(less, &fast);
93
94 // Adjust 'page_start' so that addressing using 'pointer_offset' hits the
95 // extra remembered set after the large object.
96
97 // Load the array length into 'scratch'.
98 masm->movl(scratch,
99 Operand(page_start,
100 Page::kObjectStartOffset + FixedArray::kLengthOffset));
101 Register array_length = scratch;
102
103 // Extra remembered set starts right after the large object (a FixedArray), at
104 // page_start + kObjectStartOffset + objectSize
105 // where objectSize is FixedArray::kHeaderSize + kPointerSize * array_length.
106 // Add the delta between the end of the normal RSet and the start of the
107 // extra RSet to 'page_start', so that addressing the bit using
108 // 'pointer_offset' hits the extra RSet words.
109 masm->lea(page_start,
110 Operand(page_start, array_length, times_pointer_size,
111 Page::kObjectStartOffset + FixedArray::kHeaderSize
112 - Page::kRSetEndOffset));
113
114 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
115 // to limit code size. We should probably evaluate this decision by
116 // measuring the performance of an equivalent implementation using
117 // "simpler" instructions
118 masm->bind(&fast);
119 masm->bts(Operand(page_start, Page::kRSetOffset), pointer_offset);
120}
121
122
123class RecordWriteStub : public CodeStub {
124 public:
125 RecordWriteStub(Register object, Register addr, Register scratch)
126 : object_(object), addr_(addr), scratch_(scratch) { }
127
128 void Generate(MacroAssembler* masm);
129
130 private:
131 Register object_;
132 Register addr_;
133 Register scratch_;
134
135#ifdef DEBUG
136 void Print() {
137 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
138 object_.code(), addr_.code(), scratch_.code());
139 }
140#endif
141
142 // Minor key encoding in 12 bits of three registers (object, address and
143 // scratch) OOOOAAAASSSS.
Steve Block3ce2e202009-11-05 08:53:23 +0000144 class ScratchBits : public BitField<uint32_t, 0, 4> {};
145 class AddressBits : public BitField<uint32_t, 4, 4> {};
146 class ObjectBits : public BitField<uint32_t, 8, 4> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000147
148 Major MajorKey() { return RecordWrite; }
149
150 int MinorKey() {
151 // Encode the registers.
152 return ObjectBits::encode(object_.code()) |
153 AddressBits::encode(addr_.code()) |
154 ScratchBits::encode(scratch_.code());
155 }
156};
157
158
159void RecordWriteStub::Generate(MacroAssembler* masm) {
160 RecordWriteHelper(masm, object_, addr_, scratch_);
161 masm->ret(0);
162}
163
164
165// Set the remembered set bit for [object+offset].
166// object is the object being stored into, value is the object being stored.
Steve Block3ce2e202009-11-05 08:53:23 +0000167// If offset is zero, then the smi_index register contains the array index into
168// the elements array represented as a smi. Otherwise it can be used as a
169// scratch register.
Steve Blocka7e24c12009-10-30 11:49:00 +0000170// All registers are clobbered by the operation.
171void MacroAssembler::RecordWrite(Register object,
172 int offset,
173 Register value,
Steve Block3ce2e202009-11-05 08:53:23 +0000174 Register smi_index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 // First, check if a remembered set write is even needed. The tests below
176 // catch stores of Smis and stores into young gen (which does not have space
177 // for the remembered set bits.
178 Label done;
Steve Block3ce2e202009-11-05 08:53:23 +0000179 JumpIfSmi(value, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000180
Steve Block3ce2e202009-11-05 08:53:23 +0000181 RecordWriteNonSmi(object, offset, value, smi_index);
182 bind(&done);
183}
184
185
186void MacroAssembler::RecordWriteNonSmi(Register object,
187 int offset,
188 Register scratch,
189 Register smi_index) {
190 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 // Test that the object address is not in the new space. We cannot
192 // set remembered set bits in the new space.
Steve Block3ce2e202009-11-05 08:53:23 +0000193 movq(scratch, object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
Steve Block3ce2e202009-11-05 08:53:23 +0000195 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 movq(kScratchRegister, ExternalReference::new_space_start());
Steve Block3ce2e202009-11-05 08:53:23 +0000197 cmpq(scratch, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 j(equal, &done);
199
200 if ((offset > 0) && (offset < Page::kMaxHeapObjectSize)) {
201 // Compute the bit offset in the remembered set, leave it in 'value'.
Steve Block3ce2e202009-11-05 08:53:23 +0000202 lea(scratch, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 ASSERT(is_int32(Page::kPageAlignmentMask));
Steve Block3ce2e202009-11-05 08:53:23 +0000204 and_(scratch, Immediate(static_cast<int32_t>(Page::kPageAlignmentMask)));
205 shr(scratch, Immediate(kObjectAlignmentBits));
Steve Blocka7e24c12009-10-30 11:49:00 +0000206
207 // Compute the page address from the heap object pointer, leave it in
208 // 'object' (immediate value is sign extended).
209 and_(object, Immediate(~Page::kPageAlignmentMask));
210
211 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
212 // to limit code size. We should probably evaluate this decision by
213 // measuring the performance of an equivalent implementation using
214 // "simpler" instructions
Steve Block3ce2e202009-11-05 08:53:23 +0000215 bts(Operand(object, Page::kRSetOffset), scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000217 Register dst = smi_index;
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 if (offset != 0) {
219 lea(dst, Operand(object, offset));
220 } else {
221 // array access: calculate the destination address in the same manner as
Steve Block3ce2e202009-11-05 08:53:23 +0000222 // KeyedStoreIC::GenerateGeneric.
223 SmiIndex index = SmiToIndex(smi_index, smi_index, kPointerSizeLog2);
224 lea(dst, Operand(object,
225 index.reg,
226 index.scale,
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 FixedArray::kHeaderSize - kHeapObjectTag));
228 }
229 // If we are already generating a shared stub, not inlining the
230 // record write code isn't going to save us any memory.
231 if (generating_stub()) {
Steve Block3ce2e202009-11-05 08:53:23 +0000232 RecordWriteHelper(this, object, dst, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000234 RecordWriteStub stub(object, dst, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 CallStub(&stub);
236 }
237 }
238
239 bind(&done);
240}
241
242
243void MacroAssembler::Assert(Condition cc, const char* msg) {
244 if (FLAG_debug_code) Check(cc, msg);
245}
246
247
248void MacroAssembler::Check(Condition cc, const char* msg) {
249 Label L;
250 j(cc, &L);
251 Abort(msg);
252 // will not return here
253 bind(&L);
254}
255
256
257void MacroAssembler::NegativeZeroTest(Register result,
258 Register op,
259 Label* then_label) {
260 Label ok;
261 testl(result, result);
262 j(not_zero, &ok);
263 testl(op, op);
264 j(sign, then_label);
265 bind(&ok);
266}
267
268
269void MacroAssembler::Abort(const char* msg) {
270 // We want to pass the msg string like a smi to avoid GC
271 // problems, however msg is not guaranteed to be aligned
272 // properly. Instead, we pass an aligned pointer that is
273 // a proper v8 smi, but also pass the alignment difference
274 // from the real pointer as a smi.
275 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
276 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
277 // Note: p0 might not be a valid Smi *value*, but it has a valid Smi tag.
278 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
279#ifdef DEBUG
280 if (msg != NULL) {
281 RecordComment("Abort message: ");
282 RecordComment(msg);
283 }
284#endif
285 push(rax);
286 movq(kScratchRegister, p0, RelocInfo::NONE);
287 push(kScratchRegister);
288 movq(kScratchRegister,
289 reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0)),
290 RelocInfo::NONE);
291 push(kScratchRegister);
292 CallRuntime(Runtime::kAbort, 2);
293 // will not return here
294}
295
296
297void MacroAssembler::CallStub(CodeStub* stub) {
298 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
299 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
300}
301
302
303void MacroAssembler::StubReturn(int argc) {
304 ASSERT(argc >= 1 && generating_stub());
305 ret((argc - 1) * kPointerSize);
306}
307
308
309void MacroAssembler::IllegalOperation(int num_arguments) {
310 if (num_arguments > 0) {
311 addq(rsp, Immediate(num_arguments * kPointerSize));
312 }
313 LoadRoot(rax, Heap::kUndefinedValueRootIndex);
314}
315
316
317void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
318 CallRuntime(Runtime::FunctionForId(id), num_arguments);
319}
320
321
322void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
323 // If the expected number of arguments of the runtime function is
324 // constant, we check that the actual number of arguments match the
325 // expectation.
326 if (f->nargs >= 0 && f->nargs != num_arguments) {
327 IllegalOperation(num_arguments);
328 return;
329 }
330
331 Runtime::FunctionId function_id =
332 static_cast<Runtime::FunctionId>(f->stub_id);
333 RuntimeStub stub(function_id, num_arguments);
334 CallStub(&stub);
335}
336
337
338void MacroAssembler::TailCallRuntime(ExternalReference const& ext,
339 int num_arguments,
340 int result_size) {
341 // ----------- S t a t e -------------
342 // -- rsp[0] : return address
343 // -- rsp[8] : argument num_arguments - 1
344 // ...
345 // -- rsp[8 * num_arguments] : argument 0 (receiver)
346 // -----------------------------------
347
348 // TODO(1236192): Most runtime routines don't need the number of
349 // arguments passed in because it is constant. At some point we
350 // should remove this need and make the runtime routine entry code
351 // smarter.
352 movq(rax, Immediate(num_arguments));
353 JumpToRuntime(ext, result_size);
354}
355
356
357void MacroAssembler::JumpToRuntime(const ExternalReference& ext,
358 int result_size) {
359 // Set the entry point and jump to the C entry runtime stub.
360 movq(rbx, ext);
361 CEntryStub ces(result_size);
Steve Block3ce2e202009-11-05 08:53:23 +0000362 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
Steve Blocka7e24c12009-10-30 11:49:00 +0000363}
364
365
366void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
367 bool resolved;
368 Handle<Code> code = ResolveBuiltin(id, &resolved);
369
370 const char* name = Builtins::GetName(id);
371 int argc = Builtins::GetArgumentsCount(id);
372
373 movq(target, code, RelocInfo::EMBEDDED_OBJECT);
374 if (!resolved) {
375 uint32_t flags =
376 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
Steve Blocka7e24c12009-10-30 11:49:00 +0000377 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
378 Unresolved entry = { pc_offset() - sizeof(intptr_t), flags, name };
379 unresolved_.Add(entry);
380 }
381 addq(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
382}
383
Steve Blocka7e24c12009-10-30 11:49:00 +0000384Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
385 bool* resolved) {
386 // Move the builtin function into the temporary function slot by
387 // reading it from the builtins object. NOTE: We should be able to
388 // reduce this to two instructions by putting the function table in
389 // the global object instead of the "builtins" object and by using a
390 // real register for the function.
391 movq(rdx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
392 movq(rdx, FieldOperand(rdx, GlobalObject::kBuiltinsOffset));
393 int builtins_offset =
394 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
395 movq(rdi, FieldOperand(rdx, builtins_offset));
396
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 return Builtins::GetCode(id, resolved);
398}
399
400
401void MacroAssembler::Set(Register dst, int64_t x) {
402 if (x == 0) {
403 xor_(dst, dst);
404 } else if (is_int32(x)) {
405 movq(dst, Immediate(x));
406 } else if (is_uint32(x)) {
407 movl(dst, Immediate(x));
408 } else {
409 movq(dst, x, RelocInfo::NONE);
410 }
411}
412
413
414void MacroAssembler::Set(const Operand& dst, int64_t x) {
415 if (x == 0) {
416 xor_(kScratchRegister, kScratchRegister);
417 movq(dst, kScratchRegister);
418 } else if (is_int32(x)) {
419 movq(dst, Immediate(x));
420 } else if (is_uint32(x)) {
421 movl(dst, Immediate(x));
422 } else {
423 movq(kScratchRegister, x, RelocInfo::NONE);
424 movq(dst, kScratchRegister);
425 }
426}
427
Steve Blocka7e24c12009-10-30 11:49:00 +0000428// ----------------------------------------------------------------------------
429// Smi tagging, untagging and tag detection.
430
Steve Block3ce2e202009-11-05 08:53:23 +0000431static int kSmiShift = kSmiTagSize + kSmiShiftSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000432
433void MacroAssembler::Integer32ToSmi(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000435 if (!dst.is(src)) {
436 movl(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 }
Steve Block3ce2e202009-11-05 08:53:23 +0000438 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000439}
440
441
442void MacroAssembler::Integer32ToSmi(Register dst,
443 Register src,
444 Label* on_overflow) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000446 // 32-bit integer always fits in a long smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 if (!dst.is(src)) {
448 movl(dst, src);
449 }
Steve Block3ce2e202009-11-05 08:53:23 +0000450 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000451}
452
453
Steve Block3ce2e202009-11-05 08:53:23 +0000454void MacroAssembler::Integer64PlusConstantToSmi(Register dst,
455 Register src,
456 int constant) {
457 if (dst.is(src)) {
458 addq(dst, Immediate(constant));
459 } else {
460 lea(dst, Operand(src, constant));
461 }
462 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000463}
464
465
466void MacroAssembler::SmiToInteger32(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 ASSERT_EQ(0, kSmiTag);
468 if (!dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000469 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 }
Steve Block3ce2e202009-11-05 08:53:23 +0000471 shr(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000472}
473
474
475void MacroAssembler::SmiToInteger64(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000476 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000477 if (!dst.is(src)) {
478 movq(dst, src);
479 }
480 sar(dst, Immediate(kSmiShift));
481}
482
483
484void MacroAssembler::SmiTest(Register src) {
485 testq(src, src);
486}
487
488
489void MacroAssembler::SmiCompare(Register dst, Register src) {
490 cmpq(dst, src);
491}
492
493
494void MacroAssembler::SmiCompare(Register dst, Smi* src) {
495 ASSERT(!dst.is(kScratchRegister));
496 if (src->value() == 0) {
497 testq(dst, dst);
498 } else {
499 Move(kScratchRegister, src);
500 cmpq(dst, kScratchRegister);
501 }
502}
503
504
505void MacroAssembler::SmiCompare(const Operand& dst, Register src) {
506 cmpq(dst, src);
507}
508
509
510void MacroAssembler::SmiCompare(const Operand& dst, Smi* src) {
511 if (src->value() == 0) {
512 // Only tagged long smi to have 32-bit representation.
513 cmpq(dst, Immediate(0));
514 } else {
515 Move(kScratchRegister, src);
516 cmpq(dst, kScratchRegister);
517 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000518}
519
520
521void MacroAssembler::PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
522 Register src,
523 int power) {
524 ASSERT(power >= 0);
525 ASSERT(power < 64);
526 if (power == 0) {
527 SmiToInteger64(dst, src);
528 return;
529 }
Steve Block3ce2e202009-11-05 08:53:23 +0000530 if (!dst.is(src)) {
531 movq(dst, src);
532 }
533 if (power < kSmiShift) {
534 sar(dst, Immediate(kSmiShift - power));
535 } else if (power > kSmiShift) {
536 shl(dst, Immediate(power - kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 }
538}
539
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541Condition MacroAssembler::CheckSmi(Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 ASSERT_EQ(0, kSmiTag);
543 testb(src, Immediate(kSmiTagMask));
Steve Block3ce2e202009-11-05 08:53:23 +0000544 return zero;
Steve Blocka7e24c12009-10-30 11:49:00 +0000545}
546
547
548Condition MacroAssembler::CheckPositiveSmi(Register src) {
549 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000550 movq(kScratchRegister, src);
551 rol(kScratchRegister, Immediate(1));
552 testl(kScratchRegister, Immediate(0x03));
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 return zero;
554}
555
556
Steve Blocka7e24c12009-10-30 11:49:00 +0000557Condition MacroAssembler::CheckBothSmi(Register first, Register second) {
558 if (first.is(second)) {
559 return CheckSmi(first);
560 }
561 movl(kScratchRegister, first);
562 orl(kScratchRegister, second);
Steve Block3ce2e202009-11-05 08:53:23 +0000563 testb(kScratchRegister, Immediate(kSmiTagMask));
564 return zero;
Steve Blocka7e24c12009-10-30 11:49:00 +0000565}
566
567
568Condition MacroAssembler::CheckIsMinSmi(Register src) {
569 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
Steve Block3ce2e202009-11-05 08:53:23 +0000570 movq(kScratchRegister, src);
571 rol(kScratchRegister, Immediate(1));
572 cmpq(kScratchRegister, Immediate(1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 return equal;
574}
575
Steve Blocka7e24c12009-10-30 11:49:00 +0000576
577Condition MacroAssembler::CheckInteger32ValidSmiValue(Register src) {
Steve Block3ce2e202009-11-05 08:53:23 +0000578 // A 32-bit integer value can always be converted to a smi.
579 return always;
Steve Blocka7e24c12009-10-30 11:49:00 +0000580}
581
582
Steve Block3ce2e202009-11-05 08:53:23 +0000583Condition MacroAssembler::CheckUInteger32ValidSmiValue(Register src) {
584 // An unsigned 32-bit integer value is valid as long as the high bit
585 // is not set.
586 testq(src, Immediate(0x80000000));
587 return zero;
588}
589
590
591void MacroAssembler::SmiNeg(Register dst, Register src, Label* on_smi_result) {
592 if (dst.is(src)) {
593 ASSERT(!dst.is(kScratchRegister));
594 movq(kScratchRegister, src);
595 neg(dst); // Low 32 bits are retained as zero by negation.
596 // Test if result is zero or Smi::kMinValue.
597 cmpq(dst, kScratchRegister);
598 j(not_equal, on_smi_result);
599 movq(src, kScratchRegister);
600 } else {
601 movq(dst, src);
602 neg(dst);
603 cmpq(dst, src);
604 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
605 j(not_equal, on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000606 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000607}
608
609
610void MacroAssembler::SmiAdd(Register dst,
611 Register src1,
612 Register src2,
613 Label* on_not_smi_result) {
614 ASSERT(!dst.is(src2));
Steve Block3ce2e202009-11-05 08:53:23 +0000615 if (dst.is(src1)) {
616 addq(dst, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 Label smi_result;
618 j(no_overflow, &smi_result);
619 // Restore src1.
Steve Block3ce2e202009-11-05 08:53:23 +0000620 subq(src1, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 jmp(on_not_smi_result);
622 bind(&smi_result);
Steve Block3ce2e202009-11-05 08:53:23 +0000623 } else {
624 movq(dst, src1);
625 addq(dst, src2);
626 j(overflow, on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000627 }
628}
629
630
Steve Blocka7e24c12009-10-30 11:49:00 +0000631void MacroAssembler::SmiSub(Register dst,
632 Register src1,
633 Register src2,
634 Label* on_not_smi_result) {
635 ASSERT(!dst.is(src2));
Steve Block3ce2e202009-11-05 08:53:23 +0000636 if (dst.is(src1)) {
637 subq(dst, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000638 Label smi_result;
639 j(no_overflow, &smi_result);
640 // Restore src1.
Steve Block3ce2e202009-11-05 08:53:23 +0000641 addq(src1, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000642 jmp(on_not_smi_result);
643 bind(&smi_result);
Steve Block3ce2e202009-11-05 08:53:23 +0000644 } else {
645 movq(dst, src1);
646 subq(dst, src2);
647 j(overflow, on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 }
649}
650
651
652void MacroAssembler::SmiMul(Register dst,
653 Register src1,
654 Register src2,
655 Label* on_not_smi_result) {
656 ASSERT(!dst.is(src2));
Steve Block3ce2e202009-11-05 08:53:23 +0000657 ASSERT(!dst.is(kScratchRegister));
658 ASSERT(!src1.is(kScratchRegister));
659 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +0000660
661 if (dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000662 Label failure, zero_correct_result;
663 movq(kScratchRegister, src1); // Create backup for later testing.
664 SmiToInteger64(dst, src1);
665 imul(dst, src2);
666 j(overflow, &failure);
667
668 // Check for negative zero result. If product is zero, and one
669 // argument is negative, go to slow case.
670 Label correct_result;
671 testq(dst, dst);
672 j(not_zero, &correct_result);
673
674 movq(dst, kScratchRegister);
675 xor_(dst, src2);
676 j(positive, &zero_correct_result); // Result was positive zero.
677
678 bind(&failure); // Reused failure exit, restores src1.
679 movq(src1, kScratchRegister);
680 jmp(on_not_smi_result);
681
682 bind(&zero_correct_result);
683 xor_(dst, dst);
684
685 bind(&correct_result);
686 } else {
687 SmiToInteger64(dst, src1);
688 imul(dst, src2);
689 j(overflow, on_not_smi_result);
690 // Check for negative zero result. If product is zero, and one
691 // argument is negative, go to slow case.
692 Label correct_result;
693 testq(dst, dst);
694 j(not_zero, &correct_result);
695 // One of src1 and src2 is zero, the check whether the other is
696 // negative.
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 movq(kScratchRegister, src1);
Steve Block3ce2e202009-11-05 08:53:23 +0000698 xor_(kScratchRegister, src2);
699 j(negative, on_not_smi_result);
700 bind(&correct_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000701 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000702}
703
704
705void MacroAssembler::SmiTryAddConstant(Register dst,
706 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000707 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 Label* on_not_smi_result) {
709 // Does not assume that src is a smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000710 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000712 ASSERT(!dst.is(kScratchRegister));
713 ASSERT(!src.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +0000714
Steve Block3ce2e202009-11-05 08:53:23 +0000715 JumpIfNotSmi(src, on_not_smi_result);
716 Register tmp = (dst.is(src) ? kScratchRegister : dst);
717 Move(tmp, constant);
718 addq(tmp, src);
719 j(overflow, on_not_smi_result);
720 if (dst.is(src)) {
721 movq(dst, tmp);
722 }
723}
724
725
726void MacroAssembler::SmiAddConstant(Register dst, Register src, Smi* constant) {
727 if (constant->value() == 0) {
728 if (!dst.is(src)) {
729 movq(dst, src);
730 }
731 } else if (dst.is(src)) {
732 ASSERT(!dst.is(kScratchRegister));
733
734 Move(kScratchRegister, constant);
735 addq(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000736 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000737 Move(dst, constant);
738 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 }
740}
741
742
743void MacroAssembler::SmiAddConstant(Register dst,
744 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000745 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +0000747 if (constant->value() == 0) {
748 if (!dst.is(src)) {
749 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000750 }
Steve Block3ce2e202009-11-05 08:53:23 +0000751 } else if (dst.is(src)) {
752 ASSERT(!dst.is(kScratchRegister));
753
754 Move(kScratchRegister, constant);
755 addq(dst, kScratchRegister);
756 Label result_ok;
757 j(no_overflow, &result_ok);
758 subq(dst, kScratchRegister);
759 jmp(on_not_smi_result);
760 bind(&result_ok);
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000762 Move(dst, constant);
763 addq(dst, src);
764 j(overflow, on_not_smi_result);
765 }
766}
767
768
769void MacroAssembler::SmiSubConstant(Register dst, Register src, Smi* constant) {
770 if (constant->value() == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000771 if (!dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000772 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 }
Steve Block3ce2e202009-11-05 08:53:23 +0000774 } else if (dst.is(src)) {
775 ASSERT(!dst.is(kScratchRegister));
776
777 Move(kScratchRegister, constant);
778 subq(dst, kScratchRegister);
779 } else {
780 // Subtract by adding the negative, to do it in two operations.
781 if (constant->value() == Smi::kMinValue) {
782 Move(kScratchRegister, constant);
783 movq(dst, src);
784 subq(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000786 Move(dst, Smi::FromInt(-constant->value()));
787 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 }
789 }
790}
791
792
793void MacroAssembler::SmiSubConstant(Register dst,
794 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000795 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +0000797 if (constant->value() == 0) {
798 if (!dst.is(src)) {
799 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000800 }
Steve Block3ce2e202009-11-05 08:53:23 +0000801 } else if (dst.is(src)) {
802 ASSERT(!dst.is(kScratchRegister));
803
804 Move(kScratchRegister, constant);
805 subq(dst, kScratchRegister);
806 Label sub_success;
807 j(no_overflow, &sub_success);
808 addq(src, kScratchRegister);
809 jmp(on_not_smi_result);
810 bind(&sub_success);
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000812 if (constant->value() == Smi::kMinValue) {
813 Move(kScratchRegister, constant);
814 movq(dst, src);
815 subq(dst, kScratchRegister);
816 j(overflow, on_not_smi_result);
817 } else {
818 Move(dst, Smi::FromInt(-(constant->value())));
819 addq(dst, src);
820 j(overflow, on_not_smi_result);
821 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 }
823}
824
825
826void MacroAssembler::SmiDiv(Register dst,
827 Register src1,
828 Register src2,
829 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +0000830 ASSERT(!src1.is(kScratchRegister));
831 ASSERT(!src2.is(kScratchRegister));
832 ASSERT(!dst.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +0000833 ASSERT(!src2.is(rax));
834 ASSERT(!src2.is(rdx));
835 ASSERT(!src1.is(rdx));
836
837 // Check for 0 divisor (result is +/-Infinity).
838 Label positive_divisor;
Steve Block3ce2e202009-11-05 08:53:23 +0000839 testq(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000840 j(zero, on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000841
Steve Block3ce2e202009-11-05 08:53:23 +0000842 if (src1.is(rax)) {
843 movq(kScratchRegister, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 }
Steve Block3ce2e202009-11-05 08:53:23 +0000845 SmiToInteger32(rax, src1);
846 // We need to rule out dividing Smi::kMinValue by -1, since that would
847 // overflow in idiv and raise an exception.
848 // We combine this with negative zero test (negative zero only happens
849 // when dividing zero by a negative number).
Steve Blocka7e24c12009-10-30 11:49:00 +0000850
Steve Block3ce2e202009-11-05 08:53:23 +0000851 // We overshoot a little and go to slow case if we divide min-value
852 // by any negative value, not just -1.
853 Label safe_div;
854 testl(rax, Immediate(0x7fffffff));
855 j(not_zero, &safe_div);
856 testq(src2, src2);
857 if (src1.is(rax)) {
858 j(positive, &safe_div);
859 movq(src1, kScratchRegister);
860 jmp(on_not_smi_result);
861 } else {
862 j(negative, on_not_smi_result);
863 }
864 bind(&safe_div);
865
866 SmiToInteger32(src2, src2);
867 // Sign extend src1 into edx:eax.
868 cdq();
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 idivl(src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000870 Integer32ToSmi(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 // Check that the remainder is zero.
872 testl(rdx, rdx);
Steve Block3ce2e202009-11-05 08:53:23 +0000873 if (src1.is(rax)) {
874 Label smi_result;
875 j(zero, &smi_result);
876 movq(src1, kScratchRegister);
877 jmp(on_not_smi_result);
878 bind(&smi_result);
879 } else {
880 j(not_zero, on_not_smi_result);
881 }
882 if (!dst.is(src1) && src1.is(rax)) {
883 movq(src1, kScratchRegister);
884 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 Integer32ToSmi(dst, rax);
886}
887
888
889void MacroAssembler::SmiMod(Register dst,
890 Register src1,
891 Register src2,
892 Label* on_not_smi_result) {
893 ASSERT(!dst.is(kScratchRegister));
894 ASSERT(!src1.is(kScratchRegister));
895 ASSERT(!src2.is(kScratchRegister));
896 ASSERT(!src2.is(rax));
897 ASSERT(!src2.is(rdx));
898 ASSERT(!src1.is(rdx));
Steve Block3ce2e202009-11-05 08:53:23 +0000899 ASSERT(!src1.is(src2));
Steve Blocka7e24c12009-10-30 11:49:00 +0000900
Steve Block3ce2e202009-11-05 08:53:23 +0000901 testq(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 j(zero, on_not_smi_result);
903
904 if (src1.is(rax)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000905 movq(kScratchRegister, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 }
Steve Block3ce2e202009-11-05 08:53:23 +0000907 SmiToInteger32(rax, src1);
908 SmiToInteger32(src2, src2);
909
910 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
911 Label safe_div;
912 cmpl(rax, Immediate(Smi::kMinValue));
913 j(not_equal, &safe_div);
914 cmpl(src2, Immediate(-1));
915 j(not_equal, &safe_div);
916 // Retag inputs and go slow case.
917 Integer32ToSmi(src2, src2);
918 if (src1.is(rax)) {
919 movq(src1, kScratchRegister);
920 }
921 jmp(on_not_smi_result);
922 bind(&safe_div);
923
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 // Sign extend eax into edx:eax.
925 cdq();
926 idivl(src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000927 // Restore smi tags on inputs.
928 Integer32ToSmi(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000929 if (src1.is(rax)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000930 movq(src1, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 }
Steve Block3ce2e202009-11-05 08:53:23 +0000932 // Check for a negative zero result. If the result is zero, and the
933 // dividend is negative, go slow to return a floating point negative zero.
934 Label smi_result;
935 testl(rdx, rdx);
936 j(not_zero, &smi_result);
937 testq(src1, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000938 j(negative, on_not_smi_result);
Steve Block3ce2e202009-11-05 08:53:23 +0000939 bind(&smi_result);
940 Integer32ToSmi(dst, rdx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000941}
942
943
944void MacroAssembler::SmiNot(Register dst, Register src) {
Steve Block3ce2e202009-11-05 08:53:23 +0000945 ASSERT(!dst.is(kScratchRegister));
946 ASSERT(!src.is(kScratchRegister));
947 // Set tag and padding bits before negating, so that they are zero afterwards.
948 movl(kScratchRegister, Immediate(~0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 if (dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000950 xor_(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000952 lea(dst, Operand(src, kScratchRegister, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000953 }
Steve Block3ce2e202009-11-05 08:53:23 +0000954 not_(dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000955}
956
957
958void MacroAssembler::SmiAnd(Register dst, Register src1, Register src2) {
Steve Block3ce2e202009-11-05 08:53:23 +0000959 ASSERT(!dst.is(src2));
Steve Blocka7e24c12009-10-30 11:49:00 +0000960 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000961 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000962 }
963 and_(dst, src2);
964}
965
966
Steve Block3ce2e202009-11-05 08:53:23 +0000967void MacroAssembler::SmiAndConstant(Register dst, Register src, Smi* constant) {
968 if (constant->value() == 0) {
969 xor_(dst, dst);
970 } else if (dst.is(src)) {
971 ASSERT(!dst.is(kScratchRegister));
972 Move(kScratchRegister, constant);
973 and_(dst, kScratchRegister);
974 } else {
975 Move(dst, constant);
976 and_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000978}
979
980
981void MacroAssembler::SmiOr(Register dst, Register src1, Register src2) {
982 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000983 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000984 }
985 or_(dst, src2);
986}
987
988
Steve Block3ce2e202009-11-05 08:53:23 +0000989void MacroAssembler::SmiOrConstant(Register dst, Register src, Smi* constant) {
990 if (dst.is(src)) {
991 ASSERT(!dst.is(kScratchRegister));
992 Move(kScratchRegister, constant);
993 or_(dst, kScratchRegister);
994 } else {
995 Move(dst, constant);
996 or_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000998}
999
Steve Block3ce2e202009-11-05 08:53:23 +00001000
Steve Blocka7e24c12009-10-30 11:49:00 +00001001void MacroAssembler::SmiXor(Register dst, Register src1, Register src2) {
1002 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001003 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001004 }
1005 xor_(dst, src2);
1006}
1007
1008
Steve Block3ce2e202009-11-05 08:53:23 +00001009void MacroAssembler::SmiXorConstant(Register dst, Register src, Smi* constant) {
1010 if (dst.is(src)) {
1011 ASSERT(!dst.is(kScratchRegister));
1012 Move(kScratchRegister, constant);
1013 xor_(dst, kScratchRegister);
1014 } else {
1015 Move(dst, constant);
1016 xor_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001018}
1019
1020
Steve Blocka7e24c12009-10-30 11:49:00 +00001021void MacroAssembler::SmiShiftArithmeticRightConstant(Register dst,
1022 Register src,
1023 int shift_value) {
Steve Block3ce2e202009-11-05 08:53:23 +00001024 ASSERT(is_uint5(shift_value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 if (shift_value > 0) {
1026 if (dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001027 sar(dst, Immediate(shift_value + kSmiShift));
1028 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 } else {
1030 UNIMPLEMENTED(); // Not used.
1031 }
1032 }
1033}
1034
1035
1036void MacroAssembler::SmiShiftLogicalRightConstant(Register dst,
1037 Register src,
1038 int shift_value,
1039 Label* on_not_smi_result) {
1040 // Logic right shift interprets its result as an *unsigned* number.
1041 if (dst.is(src)) {
1042 UNIMPLEMENTED(); // Not used.
1043 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001044 movq(dst, src);
1045 if (shift_value == 0) {
1046 testq(dst, dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001047 j(negative, on_not_smi_result);
1048 }
Steve Block3ce2e202009-11-05 08:53:23 +00001049 shr(dst, Immediate(shift_value + kSmiShift));
1050 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001051 }
1052}
1053
1054
1055void MacroAssembler::SmiShiftLeftConstant(Register dst,
1056 Register src,
1057 int shift_value,
1058 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +00001059 if (!dst.is(src)) {
1060 movq(dst, src);
1061 }
1062 if (shift_value > 0) {
1063 shl(dst, Immediate(shift_value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 }
1065}
1066
1067
1068void MacroAssembler::SmiShiftLeft(Register dst,
1069 Register src1,
1070 Register src2,
1071 Label* on_not_smi_result) {
1072 ASSERT(!dst.is(rcx));
1073 Label result_ok;
Steve Block3ce2e202009-11-05 08:53:23 +00001074 // Untag shift amount.
1075 if (!dst.is(src1)) {
1076 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001077 }
Steve Block3ce2e202009-11-05 08:53:23 +00001078 SmiToInteger32(rcx, src2);
1079 // Shift amount specified by lower 5 bits, not six as the shl opcode.
1080 and_(rcx, Immediate(0x1f));
1081 shl(dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001082}
1083
1084
1085void MacroAssembler::SmiShiftLogicalRight(Register dst,
1086 Register src1,
1087 Register src2,
1088 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +00001089 ASSERT(!dst.is(kScratchRegister));
1090 ASSERT(!src1.is(kScratchRegister));
1091 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001092 ASSERT(!dst.is(rcx));
1093 Label result_ok;
Steve Block3ce2e202009-11-05 08:53:23 +00001094 if (src1.is(rcx) || src2.is(rcx)) {
1095 movq(kScratchRegister, rcx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001096 }
Steve Block3ce2e202009-11-05 08:53:23 +00001097 if (!dst.is(src1)) {
1098 movq(dst, src1);
1099 }
1100 SmiToInteger32(rcx, src2);
1101 orl(rcx, Immediate(kSmiShift));
1102 shr(dst); // Shift is rcx modulo 0x1f + 32.
1103 shl(dst, Immediate(kSmiShift));
1104 testq(dst, dst);
1105 if (src1.is(rcx) || src2.is(rcx)) {
1106 Label positive_result;
1107 j(positive, &positive_result);
1108 if (src1.is(rcx)) {
1109 movq(src1, kScratchRegister);
1110 } else {
1111 movq(src2, kScratchRegister);
1112 }
1113 jmp(on_not_smi_result);
1114 bind(&positive_result);
1115 } else {
1116 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1117 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001118}
1119
1120
1121void MacroAssembler::SmiShiftArithmeticRight(Register dst,
1122 Register src1,
1123 Register src2) {
Steve Block3ce2e202009-11-05 08:53:23 +00001124 ASSERT(!dst.is(kScratchRegister));
1125 ASSERT(!src1.is(kScratchRegister));
1126 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001127 ASSERT(!dst.is(rcx));
Steve Block3ce2e202009-11-05 08:53:23 +00001128 if (src1.is(rcx)) {
1129 movq(kScratchRegister, src1);
1130 } else if (src2.is(rcx)) {
1131 movq(kScratchRegister, src2);
1132 }
1133 if (!dst.is(src1)) {
1134 movq(dst, src1);
1135 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 SmiToInteger32(rcx, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001137 orl(rcx, Immediate(kSmiShift));
1138 sar(dst); // Shift 32 + original rcx & 0x1f.
1139 shl(dst, Immediate(kSmiShift));
1140 if (src1.is(rcx)) {
1141 movq(src1, kScratchRegister);
1142 } else if (src2.is(rcx)) {
1143 movq(src2, kScratchRegister);
1144 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001145}
1146
1147
1148void MacroAssembler::SelectNonSmi(Register dst,
1149 Register src1,
1150 Register src2,
1151 Label* on_not_smis) {
Steve Block3ce2e202009-11-05 08:53:23 +00001152 ASSERT(!dst.is(kScratchRegister));
1153 ASSERT(!src1.is(kScratchRegister));
1154 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001155 ASSERT(!dst.is(src1));
1156 ASSERT(!dst.is(src2));
1157 // Both operands must not be smis.
1158#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00001159 if (allow_stub_calls()) { // Check contains a stub call.
1160 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1161 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1162 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001163#endif
1164 ASSERT_EQ(0, kSmiTag);
1165 ASSERT_EQ(0, Smi::FromInt(0));
Steve Block3ce2e202009-11-05 08:53:23 +00001166 movl(kScratchRegister, Immediate(kSmiTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +00001167 and_(kScratchRegister, src1);
1168 testl(kScratchRegister, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001169 // If non-zero then both are smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001170 j(not_zero, on_not_smis);
Steve Blocka7e24c12009-10-30 11:49:00 +00001171
Steve Block3ce2e202009-11-05 08:53:23 +00001172 // Exactly one operand is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +00001173 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1174 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1175 subq(kScratchRegister, Immediate(1));
1176 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1177 movq(dst, src1);
1178 xor_(dst, src2);
1179 and_(dst, kScratchRegister);
1180 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1181 xor_(dst, src1);
Steve Block3ce2e202009-11-05 08:53:23 +00001182 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
Steve Blocka7e24c12009-10-30 11:49:00 +00001183}
1184
Steve Block3ce2e202009-11-05 08:53:23 +00001185SmiIndex MacroAssembler::SmiToIndex(Register dst,
1186 Register src,
1187 int shift) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 ASSERT(is_uint6(shift));
Steve Block3ce2e202009-11-05 08:53:23 +00001189 // There is a possible optimization if shift is in the range 60-63, but that
1190 // will (and must) never happen.
1191 if (!dst.is(src)) {
1192 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001193 }
Steve Block3ce2e202009-11-05 08:53:23 +00001194 if (shift < kSmiShift) {
1195 sar(dst, Immediate(kSmiShift - shift));
1196 } else {
1197 shl(dst, Immediate(shift - kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001199 return SmiIndex(dst, times_1);
1200}
1201
Steve Blocka7e24c12009-10-30 11:49:00 +00001202SmiIndex MacroAssembler::SmiToNegativeIndex(Register dst,
1203 Register src,
1204 int shift) {
1205 // Register src holds a positive smi.
1206 ASSERT(is_uint6(shift));
Steve Block3ce2e202009-11-05 08:53:23 +00001207 if (!dst.is(src)) {
1208 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001209 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001210 neg(dst);
Steve Block3ce2e202009-11-05 08:53:23 +00001211 if (shift < kSmiShift) {
1212 sar(dst, Immediate(kSmiShift - shift));
1213 } else {
1214 shl(dst, Immediate(shift - kSmiShift));
1215 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 return SmiIndex(dst, times_1);
1217}
1218
1219
Steve Block3ce2e202009-11-05 08:53:23 +00001220void MacroAssembler::JumpIfSmi(Register src, Label* on_smi) {
1221 ASSERT_EQ(0, kSmiTag);
1222 Condition smi = CheckSmi(src);
1223 j(smi, on_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001224}
1225
Steve Block3ce2e202009-11-05 08:53:23 +00001226
1227void MacroAssembler::JumpIfNotSmi(Register src, Label* on_not_smi) {
1228 Condition smi = CheckSmi(src);
1229 j(NegateCondition(smi), on_not_smi);
1230}
1231
1232
1233void MacroAssembler::JumpIfNotPositiveSmi(Register src,
1234 Label* on_not_positive_smi) {
1235 Condition positive_smi = CheckPositiveSmi(src);
1236 j(NegateCondition(positive_smi), on_not_positive_smi);
1237}
1238
1239
1240void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1241 Smi* constant,
1242 Label* on_equals) {
1243 SmiCompare(src, constant);
1244 j(equal, on_equals);
1245}
1246
1247
1248void MacroAssembler::JumpIfNotValidSmiValue(Register src, Label* on_invalid) {
1249 Condition is_valid = CheckInteger32ValidSmiValue(src);
1250 j(NegateCondition(is_valid), on_invalid);
1251}
1252
1253
1254void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1255 Label* on_invalid) {
1256 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1257 j(NegateCondition(is_valid), on_invalid);
1258}
1259
1260
1261void MacroAssembler::JumpIfNotBothSmi(Register src1, Register src2,
1262 Label* on_not_both_smi) {
1263 Condition both_smi = CheckBothSmi(src1, src2);
1264 j(NegateCondition(both_smi), on_not_both_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001265}
1266
1267
1268void MacroAssembler::Move(Register dst, Handle<Object> source) {
1269 ASSERT(!source->IsFailure());
1270 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001271 Move(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001272 } else {
1273 movq(dst, source, RelocInfo::EMBEDDED_OBJECT);
1274 }
1275}
1276
1277
1278void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001279 ASSERT(!source->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +00001280 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001281 Move(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001282 } else {
1283 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1284 movq(dst, kScratchRegister);
1285 }
1286}
1287
1288
1289void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001290 if (source->IsSmi()) {
1291 SmiCompare(dst, Smi::cast(*source));
1292 } else {
1293 Move(kScratchRegister, source);
1294 cmpq(dst, kScratchRegister);
1295 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001296}
1297
1298
1299void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
1300 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001301 SmiCompare(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 } else {
1303 ASSERT(source->IsHeapObject());
1304 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1305 cmpq(dst, kScratchRegister);
1306 }
1307}
1308
1309
1310void MacroAssembler::Push(Handle<Object> source) {
1311 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001312 Push(Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001313 } else {
1314 ASSERT(source->IsHeapObject());
1315 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1316 push(kScratchRegister);
1317 }
1318}
1319
1320
1321void MacroAssembler::Push(Smi* source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001322 intptr_t smi = reinterpret_cast<intptr_t>(source);
1323 if (is_int32(smi)) {
1324 push(Immediate(static_cast<int32_t>(smi)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001325 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001326 Set(kScratchRegister, smi);
1327 push(kScratchRegister);
1328 }
1329}
1330
1331
1332void MacroAssembler::Test(const Operand& src, Smi* source) {
1333 intptr_t smi = reinterpret_cast<intptr_t>(source);
1334 if (is_int32(smi)) {
1335 testl(src, Immediate(static_cast<int32_t>(smi)));
1336 } else {
1337 Move(kScratchRegister, source);
1338 testq(src, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 }
1340}
1341
1342
1343void MacroAssembler::Jump(ExternalReference ext) {
1344 movq(kScratchRegister, ext);
1345 jmp(kScratchRegister);
1346}
1347
1348
1349void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
1350 movq(kScratchRegister, destination, rmode);
1351 jmp(kScratchRegister);
1352}
1353
1354
1355void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
Steve Block3ce2e202009-11-05 08:53:23 +00001356 // TODO(X64): Inline this
1357 jmp(code_object, rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001358}
1359
1360
1361void MacroAssembler::Call(ExternalReference ext) {
1362 movq(kScratchRegister, ext);
1363 call(kScratchRegister);
1364}
1365
1366
1367void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
1368 movq(kScratchRegister, destination, rmode);
1369 call(kScratchRegister);
1370}
1371
1372
1373void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
1374 ASSERT(RelocInfo::IsCodeTarget(rmode));
1375 WriteRecordedPositions();
Steve Block3ce2e202009-11-05 08:53:23 +00001376 call(code_object, rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001377}
1378
1379
1380void MacroAssembler::PushTryHandler(CodeLocation try_location,
1381 HandlerType type) {
1382 // Adjust this code if not the case.
1383 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1384
1385 // The pc (return address) is already on TOS. This code pushes state,
1386 // frame pointer and current handler. Check that they are expected
1387 // next on the stack, in that order.
1388 ASSERT_EQ(StackHandlerConstants::kStateOffset,
1389 StackHandlerConstants::kPCOffset - kPointerSize);
1390 ASSERT_EQ(StackHandlerConstants::kFPOffset,
1391 StackHandlerConstants::kStateOffset - kPointerSize);
1392 ASSERT_EQ(StackHandlerConstants::kNextOffset,
1393 StackHandlerConstants::kFPOffset - kPointerSize);
1394
1395 if (try_location == IN_JAVASCRIPT) {
1396 if (type == TRY_CATCH_HANDLER) {
1397 push(Immediate(StackHandler::TRY_CATCH));
1398 } else {
1399 push(Immediate(StackHandler::TRY_FINALLY));
1400 }
1401 push(rbp);
1402 } else {
1403 ASSERT(try_location == IN_JS_ENTRY);
1404 // The frame pointer does not point to a JS frame so we save NULL
1405 // for rbp. We expect the code throwing an exception to check rbp
1406 // before dereferencing it to restore the context.
1407 push(Immediate(StackHandler::ENTRY));
1408 push(Immediate(0)); // NULL frame pointer.
1409 }
1410 // Save the current handler.
1411 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
1412 push(Operand(kScratchRegister, 0));
1413 // Link this handler.
1414 movq(Operand(kScratchRegister, 0), rsp);
1415}
1416
1417
1418void MacroAssembler::Ret() {
1419 ret(0);
1420}
1421
1422
1423void MacroAssembler::FCmp() {
Steve Block3ce2e202009-11-05 08:53:23 +00001424 fucomip();
1425 ffree(0);
1426 fincstp();
Steve Blocka7e24c12009-10-30 11:49:00 +00001427}
1428
1429
1430void MacroAssembler::CmpObjectType(Register heap_object,
1431 InstanceType type,
1432 Register map) {
1433 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
1434 CmpInstanceType(map, type);
1435}
1436
1437
1438void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
1439 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
1440 Immediate(static_cast<int8_t>(type)));
1441}
1442
1443
1444void MacroAssembler::TryGetFunctionPrototype(Register function,
1445 Register result,
1446 Label* miss) {
1447 // Check that the receiver isn't a smi.
1448 testl(function, Immediate(kSmiTagMask));
1449 j(zero, miss);
1450
1451 // Check that the function really is a function.
1452 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1453 j(not_equal, miss);
1454
1455 // Make sure that the function has an instance prototype.
1456 Label non_instance;
1457 testb(FieldOperand(result, Map::kBitFieldOffset),
1458 Immediate(1 << Map::kHasNonInstancePrototype));
1459 j(not_zero, &non_instance);
1460
1461 // Get the prototype or initial map from the function.
1462 movq(result,
1463 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1464
1465 // If the prototype or initial map is the hole, don't return it and
1466 // simply miss the cache instead. This will allow us to allocate a
1467 // prototype object on-demand in the runtime system.
1468 CompareRoot(result, Heap::kTheHoleValueRootIndex);
1469 j(equal, miss);
1470
1471 // If the function does not have an initial map, we're done.
1472 Label done;
1473 CmpObjectType(result, MAP_TYPE, kScratchRegister);
1474 j(not_equal, &done);
1475
1476 // Get the prototype from the initial map.
1477 movq(result, FieldOperand(result, Map::kPrototypeOffset));
1478 jmp(&done);
1479
1480 // Non-instance prototype: Fetch prototype from constructor field
1481 // in initial map.
1482 bind(&non_instance);
1483 movq(result, FieldOperand(result, Map::kConstructorOffset));
1484
1485 // All done.
1486 bind(&done);
1487}
1488
1489
1490void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1491 if (FLAG_native_code_counters && counter->Enabled()) {
1492 movq(kScratchRegister, ExternalReference(counter));
1493 movl(Operand(kScratchRegister, 0), Immediate(value));
1494 }
1495}
1496
1497
1498void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1499 ASSERT(value > 0);
1500 if (FLAG_native_code_counters && counter->Enabled()) {
1501 movq(kScratchRegister, ExternalReference(counter));
1502 Operand operand(kScratchRegister, 0);
1503 if (value == 1) {
1504 incl(operand);
1505 } else {
1506 addl(operand, Immediate(value));
1507 }
1508 }
1509}
1510
1511
1512void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1513 ASSERT(value > 0);
1514 if (FLAG_native_code_counters && counter->Enabled()) {
1515 movq(kScratchRegister, ExternalReference(counter));
1516 Operand operand(kScratchRegister, 0);
1517 if (value == 1) {
1518 decl(operand);
1519 } else {
1520 subl(operand, Immediate(value));
1521 }
1522 }
1523}
1524
Steve Blocka7e24c12009-10-30 11:49:00 +00001525#ifdef ENABLE_DEBUGGER_SUPPORT
1526
1527void MacroAssembler::PushRegistersFromMemory(RegList regs) {
1528 ASSERT((regs & ~kJSCallerSaved) == 0);
1529 // Push the content of the memory location to the stack.
1530 for (int i = 0; i < kNumJSCallerSaved; i++) {
1531 int r = JSCallerSavedCode(i);
1532 if ((regs & (1 << r)) != 0) {
1533 ExternalReference reg_addr =
1534 ExternalReference(Debug_Address::Register(i));
1535 movq(kScratchRegister, reg_addr);
1536 push(Operand(kScratchRegister, 0));
1537 }
1538 }
1539}
1540
Steve Block3ce2e202009-11-05 08:53:23 +00001541
Steve Blocka7e24c12009-10-30 11:49:00 +00001542void MacroAssembler::SaveRegistersToMemory(RegList regs) {
1543 ASSERT((regs & ~kJSCallerSaved) == 0);
1544 // Copy the content of registers to memory location.
1545 for (int i = 0; i < kNumJSCallerSaved; i++) {
1546 int r = JSCallerSavedCode(i);
1547 if ((regs & (1 << r)) != 0) {
1548 Register reg = { r };
1549 ExternalReference reg_addr =
1550 ExternalReference(Debug_Address::Register(i));
1551 movq(kScratchRegister, reg_addr);
1552 movq(Operand(kScratchRegister, 0), reg);
1553 }
1554 }
1555}
1556
1557
1558void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
1559 ASSERT((regs & ~kJSCallerSaved) == 0);
1560 // Copy the content of memory location to registers.
1561 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1562 int r = JSCallerSavedCode(i);
1563 if ((regs & (1 << r)) != 0) {
1564 Register reg = { r };
1565 ExternalReference reg_addr =
1566 ExternalReference(Debug_Address::Register(i));
1567 movq(kScratchRegister, reg_addr);
1568 movq(reg, Operand(kScratchRegister, 0));
1569 }
1570 }
1571}
1572
1573
1574void MacroAssembler::PopRegistersToMemory(RegList regs) {
1575 ASSERT((regs & ~kJSCallerSaved) == 0);
1576 // Pop the content from the stack to the memory location.
1577 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1578 int r = JSCallerSavedCode(i);
1579 if ((regs & (1 << r)) != 0) {
1580 ExternalReference reg_addr =
1581 ExternalReference(Debug_Address::Register(i));
1582 movq(kScratchRegister, reg_addr);
1583 pop(Operand(kScratchRegister, 0));
1584 }
1585 }
1586}
1587
1588
1589void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
1590 Register scratch,
1591 RegList regs) {
1592 ASSERT(!scratch.is(kScratchRegister));
1593 ASSERT(!base.is(kScratchRegister));
1594 ASSERT(!base.is(scratch));
1595 ASSERT((regs & ~kJSCallerSaved) == 0);
1596 // Copy the content of the stack to the memory location and adjust base.
1597 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1598 int r = JSCallerSavedCode(i);
1599 if ((regs & (1 << r)) != 0) {
1600 movq(scratch, Operand(base, 0));
1601 ExternalReference reg_addr =
1602 ExternalReference(Debug_Address::Register(i));
1603 movq(kScratchRegister, reg_addr);
1604 movq(Operand(kScratchRegister, 0), scratch);
1605 lea(base, Operand(base, kPointerSize));
1606 }
1607 }
1608}
1609
1610#endif // ENABLE_DEBUGGER_SUPPORT
1611
1612
1613void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
1614 bool resolved;
1615 Handle<Code> code = ResolveBuiltin(id, &resolved);
1616
1617 // Calls are not allowed in some stubs.
1618 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1619
1620 // Rely on the assertion to check that the number of provided
1621 // arguments match the expected number of arguments. Fake a
1622 // parameter count to avoid emitting code to do the check.
1623 ParameterCount expected(0);
Steve Block3ce2e202009-11-05 08:53:23 +00001624 InvokeCode(Handle<Code>(code),
1625 expected,
1626 expected,
1627 RelocInfo::CODE_TARGET,
1628 flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00001629
1630 const char* name = Builtins::GetName(id);
1631 int argc = Builtins::GetArgumentsCount(id);
1632 // The target address for the jump is stored as an immediate at offset
1633 // kInvokeCodeAddressOffset.
1634 if (!resolved) {
1635 uint32_t flags =
1636 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
Steve Blocka7e24c12009-10-30 11:49:00 +00001637 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
1638 Unresolved entry =
1639 { pc_offset() - kCallTargetAddressOffset, flags, name };
1640 unresolved_.Add(entry);
1641 }
1642}
1643
1644
1645void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1646 const ParameterCount& actual,
1647 Handle<Code> code_constant,
1648 Register code_register,
1649 Label* done,
1650 InvokeFlag flag) {
1651 bool definitely_matches = false;
1652 Label invoke;
1653 if (expected.is_immediate()) {
1654 ASSERT(actual.is_immediate());
1655 if (expected.immediate() == actual.immediate()) {
1656 definitely_matches = true;
1657 } else {
1658 movq(rax, Immediate(actual.immediate()));
1659 if (expected.immediate() ==
Steve Block3ce2e202009-11-05 08:53:23 +00001660 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001661 // Don't worry about adapting arguments for built-ins that
1662 // don't want that done. Skip adaption code by making it look
1663 // like we have a match between expected and actual number of
1664 // arguments.
1665 definitely_matches = true;
1666 } else {
1667 movq(rbx, Immediate(expected.immediate()));
1668 }
1669 }
1670 } else {
1671 if (actual.is_immediate()) {
1672 // Expected is in register, actual is immediate. This is the
1673 // case when we invoke function values without going through the
1674 // IC mechanism.
1675 cmpq(expected.reg(), Immediate(actual.immediate()));
1676 j(equal, &invoke);
1677 ASSERT(expected.reg().is(rbx));
1678 movq(rax, Immediate(actual.immediate()));
1679 } else if (!expected.reg().is(actual.reg())) {
1680 // Both expected and actual are in (different) registers. This
1681 // is the case when we invoke functions using call and apply.
1682 cmpq(expected.reg(), actual.reg());
1683 j(equal, &invoke);
1684 ASSERT(actual.reg().is(rax));
1685 ASSERT(expected.reg().is(rbx));
1686 }
1687 }
1688
1689 if (!definitely_matches) {
1690 Handle<Code> adaptor =
1691 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1692 if (!code_constant.is_null()) {
1693 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1694 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1695 } else if (!code_register.is(rdx)) {
1696 movq(rdx, code_register);
1697 }
1698
1699 if (flag == CALL_FUNCTION) {
1700 Call(adaptor, RelocInfo::CODE_TARGET);
1701 jmp(done);
1702 } else {
1703 Jump(adaptor, RelocInfo::CODE_TARGET);
1704 }
1705 bind(&invoke);
1706 }
1707}
1708
1709
1710void MacroAssembler::InvokeCode(Register code,
1711 const ParameterCount& expected,
1712 const ParameterCount& actual,
1713 InvokeFlag flag) {
1714 Label done;
1715 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1716 if (flag == CALL_FUNCTION) {
1717 call(code);
1718 } else {
1719 ASSERT(flag == JUMP_FUNCTION);
1720 jmp(code);
1721 }
1722 bind(&done);
1723}
1724
1725
1726void MacroAssembler::InvokeCode(Handle<Code> code,
1727 const ParameterCount& expected,
1728 const ParameterCount& actual,
1729 RelocInfo::Mode rmode,
1730 InvokeFlag flag) {
1731 Label done;
1732 Register dummy = rax;
1733 InvokePrologue(expected, actual, code, dummy, &done, flag);
1734 if (flag == CALL_FUNCTION) {
1735 Call(code, rmode);
1736 } else {
1737 ASSERT(flag == JUMP_FUNCTION);
1738 Jump(code, rmode);
1739 }
1740 bind(&done);
1741}
1742
1743
1744void MacroAssembler::InvokeFunction(Register function,
1745 const ParameterCount& actual,
1746 InvokeFlag flag) {
1747 ASSERT(function.is(rdi));
1748 movq(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1749 movq(rsi, FieldOperand(function, JSFunction::kContextOffset));
1750 movsxlq(rbx,
1751 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
1752 movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
1753 // Advances rdx to the end of the Code object header, to the start of
1754 // the executable code.
1755 lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
1756
1757 ParameterCount expected(rbx);
1758 InvokeCode(rdx, expected, actual, flag);
1759}
1760
1761
1762void MacroAssembler::EnterFrame(StackFrame::Type type) {
1763 push(rbp);
1764 movq(rbp, rsp);
1765 push(rsi); // Context.
Steve Block3ce2e202009-11-05 08:53:23 +00001766 Push(Smi::FromInt(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00001767 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
1768 push(kScratchRegister);
1769 if (FLAG_debug_code) {
1770 movq(kScratchRegister,
1771 Factory::undefined_value(),
1772 RelocInfo::EMBEDDED_OBJECT);
1773 cmpq(Operand(rsp, 0), kScratchRegister);
1774 Check(not_equal, "code object not properly patched");
1775 }
1776}
1777
1778
1779void MacroAssembler::LeaveFrame(StackFrame::Type type) {
1780 if (FLAG_debug_code) {
Steve Block3ce2e202009-11-05 08:53:23 +00001781 Move(kScratchRegister, Smi::FromInt(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00001782 cmpq(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
1783 Check(equal, "stack frame types must match");
1784 }
1785 movq(rsp, rbp);
1786 pop(rbp);
1787}
1788
1789
Steve Blocka7e24c12009-10-30 11:49:00 +00001790void MacroAssembler::EnterExitFrame(StackFrame::Type type, int result_size) {
1791 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
1792
1793 // Setup the frame structure on the stack.
1794 // All constants are relative to the frame pointer of the exit frame.
1795 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
1796 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
1797 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
1798 push(rbp);
1799 movq(rbp, rsp);
1800
1801 // Reserve room for entry stack pointer and push the debug marker.
Steve Block3ce2e202009-11-05 08:53:23 +00001802 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001803 push(Immediate(0)); // saved entry sp, patched before call
1804 push(Immediate(type == StackFrame::EXIT_DEBUG ? 1 : 0));
1805
1806 // Save the frame pointer and the context in top.
1807 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
1808 ExternalReference context_address(Top::k_context_address);
1809 movq(r14, rax); // Backup rax before we use it.
1810
1811 movq(rax, rbp);
1812 store_rax(c_entry_fp_address);
1813 movq(rax, rsi);
1814 store_rax(context_address);
1815
1816 // Setup argv in callee-saved register r15. It is reused in LeaveExitFrame,
1817 // so it must be retained across the C-call.
1818 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
1819 lea(r15, Operand(rbp, r14, times_pointer_size, offset));
1820
1821#ifdef ENABLE_DEBUGGER_SUPPORT
1822 // Save the state of all registers to the stack from the memory
1823 // location. This is needed to allow nested break points.
1824 if (type == StackFrame::EXIT_DEBUG) {
1825 // TODO(1243899): This should be symmetric to
1826 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
1827 // correct here, but computed for the other call. Very error
1828 // prone! FIX THIS. Actually there are deeper problems with
1829 // register saving than this asymmetry (see the bug report
1830 // associated with this issue).
1831 PushRegistersFromMemory(kJSCallerSaved);
1832 }
1833#endif
1834
1835#ifdef _WIN64
1836 // Reserve space on stack for result and argument structures, if necessary.
1837 int result_stack_space = (result_size < 2) ? 0 : result_size * kPointerSize;
1838 // Reserve space for the Arguments object. The Windows 64-bit ABI
1839 // requires us to pass this structure as a pointer to its location on
1840 // the stack. The structure contains 2 values.
1841 int argument_stack_space = 2 * kPointerSize;
1842 // We also need backing space for 4 parameters, even though
1843 // we only pass one or two parameter, and it is in a register.
1844 int argument_mirror_space = 4 * kPointerSize;
1845 int total_stack_space =
1846 argument_mirror_space + argument_stack_space + result_stack_space;
1847 subq(rsp, Immediate(total_stack_space));
1848#endif
1849
1850 // Get the required frame alignment for the OS.
1851 static const int kFrameAlignment = OS::ActivationFrameAlignment();
1852 if (kFrameAlignment > 0) {
1853 ASSERT(IsPowerOf2(kFrameAlignment));
1854 movq(kScratchRegister, Immediate(-kFrameAlignment));
1855 and_(rsp, kScratchRegister);
1856 }
1857
1858 // Patch the saved entry sp.
1859 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
1860}
1861
1862
1863void MacroAssembler::LeaveExitFrame(StackFrame::Type type, int result_size) {
1864 // Registers:
1865 // r15 : argv
1866#ifdef ENABLE_DEBUGGER_SUPPORT
1867 // Restore the memory copy of the registers by digging them out from
1868 // the stack. This is needed to allow nested break points.
1869 if (type == StackFrame::EXIT_DEBUG) {
1870 // It's okay to clobber register rbx below because we don't need
1871 // the function pointer after this.
1872 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
1873 int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
1874 lea(rbx, Operand(rbp, kOffset));
1875 CopyRegistersFromStackToMemory(rbx, rcx, kJSCallerSaved);
1876 }
1877#endif
1878
1879 // Get the return address from the stack and restore the frame pointer.
1880 movq(rcx, Operand(rbp, 1 * kPointerSize));
1881 movq(rbp, Operand(rbp, 0 * kPointerSize));
1882
Steve Blocka7e24c12009-10-30 11:49:00 +00001883 // Pop everything up to and including the arguments and the receiver
1884 // from the caller stack.
1885 lea(rsp, Operand(r15, 1 * kPointerSize));
1886
1887 // Restore current context from top and clear it in debug mode.
1888 ExternalReference context_address(Top::k_context_address);
1889 movq(kScratchRegister, context_address);
1890 movq(rsi, Operand(kScratchRegister, 0));
1891#ifdef DEBUG
1892 movq(Operand(kScratchRegister, 0), Immediate(0));
1893#endif
1894
1895 // Push the return address to get ready to return.
1896 push(rcx);
1897
1898 // Clear the top frame.
1899 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
1900 movq(kScratchRegister, c_entry_fp_address);
1901 movq(Operand(kScratchRegister, 0), Immediate(0));
1902}
1903
1904
Steve Block3ce2e202009-11-05 08:53:23 +00001905Register MacroAssembler::CheckMaps(JSObject* object,
1906 Register object_reg,
1907 JSObject* holder,
1908 Register holder_reg,
Steve Blocka7e24c12009-10-30 11:49:00 +00001909 Register scratch,
1910 Label* miss) {
1911 // Make sure there's no overlap between scratch and the other
1912 // registers.
1913 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
1914
1915 // Keep track of the current object in register reg. On the first
1916 // iteration, reg is an alias for object_reg, on later iterations,
1917 // it is an alias for holder_reg.
1918 Register reg = object_reg;
1919 int depth = 1;
1920
1921 // Check the maps in the prototype chain.
1922 // Traverse the prototype chain from the object and do map checks.
1923 while (object != holder) {
1924 depth++;
1925
1926 // Only global objects and objects that do not require access
1927 // checks are allowed in stubs.
1928 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
1929
1930 JSObject* prototype = JSObject::cast(object->GetPrototype());
1931 if (Heap::InNewSpace(prototype)) {
1932 // Get the map of the current object.
1933 movq(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1934 Cmp(scratch, Handle<Map>(object->map()));
1935 // Branch on the result of the map check.
1936 j(not_equal, miss);
1937 // Check access rights to the global object. This has to happen
1938 // after the map check so that we know that the object is
1939 // actually a global object.
1940 if (object->IsJSGlobalProxy()) {
1941 CheckAccessGlobalProxy(reg, scratch, miss);
1942
1943 // Restore scratch register to be the map of the object.
1944 // We load the prototype from the map in the scratch register.
1945 movq(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1946 }
1947 // The prototype is in new space; we cannot store a reference
1948 // to it in the code. Load it from the map.
1949 reg = holder_reg; // from now the object is in holder_reg
1950 movq(reg, FieldOperand(scratch, Map::kPrototypeOffset));
1951
1952 } else {
1953 // Check the map of the current object.
1954 Cmp(FieldOperand(reg, HeapObject::kMapOffset),
1955 Handle<Map>(object->map()));
1956 // Branch on the result of the map check.
1957 j(not_equal, miss);
1958 // Check access rights to the global object. This has to happen
1959 // after the map check so that we know that the object is
1960 // actually a global object.
1961 if (object->IsJSGlobalProxy()) {
1962 CheckAccessGlobalProxy(reg, scratch, miss);
1963 }
1964 // The prototype is in old space; load it directly.
1965 reg = holder_reg; // from now the object is in holder_reg
1966 Move(reg, Handle<JSObject>(prototype));
1967 }
1968
1969 // Go to the next object in the prototype chain.
1970 object = prototype;
1971 }
1972
1973 // Check the holder map.
Steve Block3ce2e202009-11-05 08:53:23 +00001974 Cmp(FieldOperand(reg, HeapObject::kMapOffset), Handle<Map>(holder->map()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001975 j(not_equal, miss);
1976
1977 // Log the check depth.
1978 LOG(IntEvent("check-maps-depth", depth));
1979
1980 // Perform security check for access to the global object and return
1981 // the holder register.
1982 ASSERT(object == holder);
1983 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
1984 if (object->IsJSGlobalProxy()) {
1985 CheckAccessGlobalProxy(reg, scratch, miss);
1986 }
1987 return reg;
1988}
1989
1990
Steve Blocka7e24c12009-10-30 11:49:00 +00001991void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1992 Register scratch,
1993 Label* miss) {
1994 Label same_contexts;
1995
1996 ASSERT(!holder_reg.is(scratch));
1997 ASSERT(!scratch.is(kScratchRegister));
1998 // Load current lexical context from the stack frame.
1999 movq(scratch, Operand(rbp, StandardFrameConstants::kContextOffset));
2000
2001 // When generating debug code, make sure the lexical context is set.
2002 if (FLAG_debug_code) {
2003 cmpq(scratch, Immediate(0));
2004 Check(not_equal, "we should not have an empty lexical context");
2005 }
2006 // Load the global context of the current context.
2007 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
2008 movq(scratch, FieldOperand(scratch, offset));
2009 movq(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
2010
2011 // Check the context is a global context.
2012 if (FLAG_debug_code) {
2013 Cmp(FieldOperand(scratch, HeapObject::kMapOffset),
2014 Factory::global_context_map());
2015 Check(equal, "JSGlobalObject::global_context should be a global context.");
2016 }
2017
2018 // Check if both contexts are the same.
2019 cmpq(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
2020 j(equal, &same_contexts);
2021
2022 // Compare security tokens.
2023 // Check that the security token in the calling global object is
2024 // compatible with the security token in the receiving global
2025 // object.
2026
2027 // Check the context is a global context.
2028 if (FLAG_debug_code) {
2029 // Preserve original value of holder_reg.
2030 push(holder_reg);
2031 movq(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
2032 CompareRoot(holder_reg, Heap::kNullValueRootIndex);
2033 Check(not_equal, "JSGlobalProxy::context() should not be null.");
2034
2035 // Read the first word and compare to global_context_map(),
2036 movq(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
2037 CompareRoot(holder_reg, Heap::kGlobalContextMapRootIndex);
2038 Check(equal, "JSGlobalObject::global_context should be a global context.");
2039 pop(holder_reg);
2040 }
2041
2042 movq(kScratchRegister,
2043 FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
Steve Block3ce2e202009-11-05 08:53:23 +00002044 int token_offset =
2045 Context::kHeaderSize + Context::SECURITY_TOKEN_INDEX * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002046 movq(scratch, FieldOperand(scratch, token_offset));
2047 cmpq(scratch, FieldOperand(kScratchRegister, token_offset));
2048 j(not_equal, miss);
2049
2050 bind(&same_contexts);
2051}
2052
2053
2054void MacroAssembler::LoadAllocationTopHelper(Register result,
2055 Register result_end,
2056 Register scratch,
2057 AllocationFlags flags) {
2058 ExternalReference new_space_allocation_top =
2059 ExternalReference::new_space_allocation_top_address();
2060
2061 // Just return if allocation top is already known.
2062 if ((flags & RESULT_CONTAINS_TOP) != 0) {
2063 // No use of scratch if allocation top is provided.
2064 ASSERT(scratch.is(no_reg));
2065#ifdef DEBUG
2066 // Assert that result actually contains top on entry.
2067 movq(kScratchRegister, new_space_allocation_top);
2068 cmpq(result, Operand(kScratchRegister, 0));
2069 Check(equal, "Unexpected allocation top");
2070#endif
2071 return;
2072 }
2073
2074 // Move address of new object to result. Use scratch register if available.
2075 if (scratch.is(no_reg)) {
2076 movq(kScratchRegister, new_space_allocation_top);
2077 movq(result, Operand(kScratchRegister, 0));
2078 } else {
2079 ASSERT(!scratch.is(result_end));
2080 movq(scratch, new_space_allocation_top);
2081 movq(result, Operand(scratch, 0));
2082 }
2083}
2084
2085
2086void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
2087 Register scratch) {
2088 ExternalReference new_space_allocation_top =
2089 ExternalReference::new_space_allocation_top_address();
2090
2091 // Update new top.
2092 if (result_end.is(rax)) {
2093 // rax can be stored directly to a memory location.
2094 store_rax(new_space_allocation_top);
2095 } else {
2096 // Register required - use scratch provided if available.
2097 if (scratch.is(no_reg)) {
2098 movq(kScratchRegister, new_space_allocation_top);
2099 movq(Operand(kScratchRegister, 0), result_end);
2100 } else {
2101 movq(Operand(scratch, 0), result_end);
2102 }
2103 }
2104}
2105
2106
2107void MacroAssembler::AllocateInNewSpace(int object_size,
2108 Register result,
2109 Register result_end,
2110 Register scratch,
2111 Label* gc_required,
2112 AllocationFlags flags) {
2113 ASSERT(!result.is(result_end));
2114
2115 // Load address of new object into result.
2116 LoadAllocationTopHelper(result, result_end, scratch, flags);
2117
2118 // Calculate new top and bail out if new space is exhausted.
2119 ExternalReference new_space_allocation_limit =
2120 ExternalReference::new_space_allocation_limit_address();
2121 lea(result_end, Operand(result, object_size));
2122 movq(kScratchRegister, new_space_allocation_limit);
2123 cmpq(result_end, Operand(kScratchRegister, 0));
2124 j(above, gc_required);
2125
2126 // Update allocation top.
2127 UpdateAllocationTopHelper(result_end, scratch);
2128
2129 // Tag the result if requested.
2130 if ((flags & TAG_OBJECT) != 0) {
2131 addq(result, Immediate(kHeapObjectTag));
2132 }
2133}
2134
2135
2136void MacroAssembler::AllocateInNewSpace(int header_size,
2137 ScaleFactor element_size,
2138 Register element_count,
2139 Register result,
2140 Register result_end,
2141 Register scratch,
2142 Label* gc_required,
2143 AllocationFlags flags) {
2144 ASSERT(!result.is(result_end));
2145
2146 // Load address of new object into result.
2147 LoadAllocationTopHelper(result, result_end, scratch, flags);
2148
2149 // Calculate new top and bail out if new space is exhausted.
2150 ExternalReference new_space_allocation_limit =
2151 ExternalReference::new_space_allocation_limit_address();
2152 lea(result_end, Operand(result, element_count, element_size, header_size));
2153 movq(kScratchRegister, new_space_allocation_limit);
2154 cmpq(result_end, Operand(kScratchRegister, 0));
2155 j(above, gc_required);
2156
2157 // Update allocation top.
2158 UpdateAllocationTopHelper(result_end, scratch);
2159
2160 // Tag the result if requested.
2161 if ((flags & TAG_OBJECT) != 0) {
2162 addq(result, Immediate(kHeapObjectTag));
2163 }
2164}
2165
2166
2167void MacroAssembler::AllocateInNewSpace(Register object_size,
2168 Register result,
2169 Register result_end,
2170 Register scratch,
2171 Label* gc_required,
2172 AllocationFlags flags) {
2173 // Load address of new object into result.
2174 LoadAllocationTopHelper(result, result_end, scratch, flags);
2175
2176 // Calculate new top and bail out if new space is exhausted.
2177 ExternalReference new_space_allocation_limit =
2178 ExternalReference::new_space_allocation_limit_address();
2179 if (!object_size.is(result_end)) {
2180 movq(result_end, object_size);
2181 }
2182 addq(result_end, result);
2183 movq(kScratchRegister, new_space_allocation_limit);
2184 cmpq(result_end, Operand(kScratchRegister, 0));
2185 j(above, gc_required);
2186
2187 // Update allocation top.
2188 UpdateAllocationTopHelper(result_end, scratch);
2189
2190 // Tag the result if requested.
2191 if ((flags & TAG_OBJECT) != 0) {
2192 addq(result, Immediate(kHeapObjectTag));
2193 }
2194}
2195
2196
2197void MacroAssembler::UndoAllocationInNewSpace(Register object) {
2198 ExternalReference new_space_allocation_top =
2199 ExternalReference::new_space_allocation_top_address();
2200
2201 // Make sure the object has no tag before resetting top.
2202 and_(object, Immediate(~kHeapObjectTagMask));
2203 movq(kScratchRegister, new_space_allocation_top);
2204#ifdef DEBUG
2205 cmpq(object, Operand(kScratchRegister, 0));
2206 Check(below, "Undo allocation of non allocated memory");
2207#endif
2208 movq(Operand(kScratchRegister, 0), object);
2209}
2210
2211
Steve Block3ce2e202009-11-05 08:53:23 +00002212void MacroAssembler::AllocateHeapNumber(Register result,
2213 Register scratch,
2214 Label* gc_required) {
2215 // Allocate heap number in new space.
2216 AllocateInNewSpace(HeapNumber::kSize,
2217 result,
2218 scratch,
2219 no_reg,
2220 gc_required,
2221 TAG_OBJECT);
2222
2223 // Set the map.
2224 LoadRoot(kScratchRegister, Heap::kHeapNumberMapRootIndex);
2225 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2226}
2227
2228
Steve Blocka7e24c12009-10-30 11:49:00 +00002229CodePatcher::CodePatcher(byte* address, int size)
2230 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
2231 // Create a new macro assembler pointing to the address of the code to patch.
2232 // The size is adjusted with kGap on order for the assembler to generate size
2233 // bytes of instructions without failing with buffer size constraints.
2234 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2235}
2236
2237
2238CodePatcher::~CodePatcher() {
2239 // Indicate that code has changed.
2240 CPU::FlushICache(address_, size_);
2241
2242 // Check that the code was patched as expected.
2243 ASSERT(masm_.pc_ == address_ + size_);
2244 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2245}
2246
Steve Blocka7e24c12009-10-30 11:49:00 +00002247} } // namespace v8::internal