blob: b468e82f9545d65508f3f0f69cd7cc1a922717e9 [file] [log] [blame]
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_X64)
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
34#include "assembler-x64.h"
35#include "macro-assembler-x64.h"
36#include "serialize.h"
37#include "debug.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010038#include "heap.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40namespace v8 {
41namespace internal {
42
43MacroAssembler::MacroAssembler(void* buffer, int size)
Steve Block3ce2e202009-11-05 08:53:23 +000044 : Assembler(buffer, size),
Steve Block3ce2e202009-11-05 08:53:23 +000045 generating_stub_(false),
46 allow_stub_calls_(true),
47 code_object_(Heap::undefined_value()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000048}
49
50
Steve Block3ce2e202009-11-05 08:53:23 +000051void MacroAssembler::LoadRoot(Register destination, Heap::RootListIndex index) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +010052 movq(destination, Operand(kRootRegister,
53 (index << kPointerSizeLog2) - kRootRegisterBias));
54}
55
56
57void MacroAssembler::LoadRootIndexed(Register destination,
58 Register variable_offset,
59 int fixed_offset) {
60 movq(destination,
61 Operand(kRootRegister,
62 variable_offset, times_pointer_size,
63 (fixed_offset << kPointerSizeLog2) - kRootRegisterBias));
Steve Blocka7e24c12009-10-30 11:49:00 +000064}
65
66
Kristian Monsen25f61362010-05-21 11:50:48 +010067void MacroAssembler::StoreRoot(Register source, Heap::RootListIndex index) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +010068 movq(Operand(kRootRegister, (index << kPointerSizeLog2) - kRootRegisterBias),
69 source);
Kristian Monsen25f61362010-05-21 11:50:48 +010070}
71
72
Steve Blocka7e24c12009-10-30 11:49:00 +000073void MacroAssembler::PushRoot(Heap::RootListIndex index) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +010074 push(Operand(kRootRegister, (index << kPointerSizeLog2) - kRootRegisterBias));
Steve Blocka7e24c12009-10-30 11:49:00 +000075}
76
77
Steve Block3ce2e202009-11-05 08:53:23 +000078void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +010079 cmpq(with, Operand(kRootRegister,
80 (index << kPointerSizeLog2) - kRootRegisterBias));
Steve Blocka7e24c12009-10-30 11:49:00 +000081}
82
83
Steve Block1e0659c2011-05-24 12:43:12 +010084void MacroAssembler::CompareRoot(const Operand& with,
85 Heap::RootListIndex index) {
86 ASSERT(!with.AddressUsesRegister(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +000087 LoadRoot(kScratchRegister, index);
88 cmpq(with, kScratchRegister);
89}
90
91
Steve Block6ded16b2010-05-10 14:33:55 +010092void MacroAssembler::RecordWriteHelper(Register object,
93 Register addr,
94 Register scratch) {
95 if (FLAG_debug_code) {
96 // Check that the object is not in new space.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010097 NearLabel not_in_new_space;
Steve Block6ded16b2010-05-10 14:33:55 +010098 InNewSpace(object, scratch, not_equal, &not_in_new_space);
99 Abort("new-space object passed to RecordWriteHelper");
100 bind(&not_in_new_space);
101 }
102
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 // Compute the page start address from the heap object pointer, and reuse
104 // the 'object' register for it.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100105 and_(object, Immediate(~Page::kPageAlignmentMask));
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100107 // Compute number of region covering addr. See Page::GetRegionNumberForAddress
108 // method for more details.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100109 shrl(addr, Immediate(Page::kRegionSizeLog2));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100110 andl(addr, Immediate(Page::kPageAlignmentMask >> Page::kRegionSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100112 // Set dirty mark for region.
113 bts(Operand(object, Page::kDirtyFlagOffset), addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114}
115
116
Steve Blocka7e24c12009-10-30 11:49:00 +0000117void MacroAssembler::RecordWrite(Register object,
118 int offset,
119 Register value,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100120 Register index) {
Leon Clarke4515c472010-02-03 11:58:03 +0000121 // The compiled code assumes that record write doesn't change the
122 // context register, so we check that none of the clobbered
123 // registers are rsi.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100124 ASSERT(!object.is(rsi) && !value.is(rsi) && !index.is(rsi));
Leon Clarke4515c472010-02-03 11:58:03 +0000125
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100126 // First, check if a write barrier is even needed. The tests below
127 // catch stores of Smis and stores into young gen.
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 Label done;
Steve Block3ce2e202009-11-05 08:53:23 +0000129 JumpIfSmi(value, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100131 RecordWriteNonSmi(object, offset, value, index);
Steve Block3ce2e202009-11-05 08:53:23 +0000132 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000133
134 // Clobber all input registers when running with the debug-code flag
135 // turned on to provoke errors. This clobbering repeats the
136 // clobbering done inside RecordWriteNonSmi but it's necessary to
137 // avoid having the fast case for smis leave the registers
138 // unchanged.
139 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100140 movq(object, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
141 movq(value, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100142 movq(index, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Leon Clarke4515c472010-02-03 11:58:03 +0000143 }
Steve Block3ce2e202009-11-05 08:53:23 +0000144}
145
146
Steve Block8defd9f2010-07-08 12:39:36 +0100147void MacroAssembler::RecordWrite(Register object,
148 Register address,
149 Register value) {
150 // The compiled code assumes that record write doesn't change the
151 // context register, so we check that none of the clobbered
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100152 // registers are rsi.
Steve Block8defd9f2010-07-08 12:39:36 +0100153 ASSERT(!object.is(rsi) && !value.is(rsi) && !address.is(rsi));
154
155 // First, check if a write barrier is even needed. The tests below
156 // catch stores of Smis and stores into young gen.
157 Label done;
158 JumpIfSmi(value, &done);
159
160 InNewSpace(object, value, equal, &done);
161
162 RecordWriteHelper(object, address, value);
163
164 bind(&done);
165
166 // Clobber all input registers when running with the debug-code flag
167 // turned on to provoke errors.
168 if (FLAG_debug_code) {
169 movq(object, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
170 movq(address, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
171 movq(value, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
172 }
173}
174
175
Steve Block3ce2e202009-11-05 08:53:23 +0000176void MacroAssembler::RecordWriteNonSmi(Register object,
177 int offset,
178 Register scratch,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100179 Register index) {
Steve Block3ce2e202009-11-05 08:53:23 +0000180 Label done;
Leon Clarke4515c472010-02-03 11:58:03 +0000181
182 if (FLAG_debug_code) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100183 NearLabel okay;
Leon Clarke4515c472010-02-03 11:58:03 +0000184 JumpIfNotSmi(object, &okay);
185 Abort("MacroAssembler::RecordWriteNonSmi cannot deal with smis");
186 bind(&okay);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100187
188 if (offset == 0) {
189 // index must be int32.
190 Register tmp = index.is(rax) ? rbx : rax;
191 push(tmp);
192 movl(tmp, index);
193 cmpq(tmp, index);
194 Check(equal, "Index register for RecordWrite must be untagged int32.");
195 pop(tmp);
196 }
Leon Clarke4515c472010-02-03 11:58:03 +0000197 }
198
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100199 // Test that the object address is not in the new space. We cannot
200 // update page dirty marks for new space pages.
Steve Block6ded16b2010-05-10 14:33:55 +0100201 InNewSpace(object, scratch, equal, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
Steve Block6ded16b2010-05-10 14:33:55 +0100203 // The offset is relative to a tagged or untagged HeapObject pointer,
204 // so either offset or offset + kHeapObjectTag must be a
205 // multiple of kPointerSize.
206 ASSERT(IsAligned(offset, kPointerSize) ||
207 IsAligned(offset + kHeapObjectTag, kPointerSize));
208
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100209 Register dst = index;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100210 if (offset != 0) {
211 lea(dst, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100213 // array access: calculate the destination address in the same manner as
214 // KeyedStoreIC::GenerateGeneric.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100215 lea(dst, FieldOperand(object,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100216 index,
217 times_pointer_size,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100218 FixedArray::kHeaderSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100220 RecordWriteHelper(object, dst, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
222 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000223
224 // Clobber all input registers when running with the debug-code flag
225 // turned on to provoke errors.
226 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100227 movq(object, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
228 movq(scratch, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100229 movq(index, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Steve Block6ded16b2010-05-10 14:33:55 +0100230 }
231}
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233void MacroAssembler::Assert(Condition cc, const char* msg) {
234 if (FLAG_debug_code) Check(cc, msg);
235}
236
237
Iain Merrick75681382010-08-19 15:07:18 +0100238void MacroAssembler::AssertFastElements(Register elements) {
239 if (FLAG_debug_code) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100240 NearLabel ok;
Iain Merrick75681382010-08-19 15:07:18 +0100241 CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
242 Heap::kFixedArrayMapRootIndex);
243 j(equal, &ok);
244 CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
245 Heap::kFixedCOWArrayMapRootIndex);
246 j(equal, &ok);
247 Abort("JSObject with fast elements map has slow elements");
248 bind(&ok);
249 }
250}
251
252
Steve Blocka7e24c12009-10-30 11:49:00 +0000253void MacroAssembler::Check(Condition cc, const char* msg) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100254 NearLabel L;
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 j(cc, &L);
256 Abort(msg);
257 // will not return here
258 bind(&L);
259}
260
261
Steve Block6ded16b2010-05-10 14:33:55 +0100262void MacroAssembler::CheckStackAlignment() {
263 int frame_alignment = OS::ActivationFrameAlignment();
264 int frame_alignment_mask = frame_alignment - 1;
265 if (frame_alignment > kPointerSize) {
266 ASSERT(IsPowerOf2(frame_alignment));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100267 NearLabel alignment_as_expected;
Steve Block6ded16b2010-05-10 14:33:55 +0100268 testq(rsp, Immediate(frame_alignment_mask));
269 j(zero, &alignment_as_expected);
270 // Abort if stack is not aligned.
271 int3();
272 bind(&alignment_as_expected);
273 }
274}
275
276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277void MacroAssembler::NegativeZeroTest(Register result,
278 Register op,
279 Label* then_label) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100280 NearLabel ok;
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 testl(result, result);
282 j(not_zero, &ok);
283 testl(op, op);
284 j(sign, then_label);
285 bind(&ok);
286}
287
288
289void MacroAssembler::Abort(const char* msg) {
290 // We want to pass the msg string like a smi to avoid GC
291 // problems, however msg is not guaranteed to be aligned
292 // properly. Instead, we pass an aligned pointer that is
293 // a proper v8 smi, but also pass the alignment difference
294 // from the real pointer as a smi.
295 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
296 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
297 // Note: p0 might not be a valid Smi *value*, but it has a valid Smi tag.
298 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
299#ifdef DEBUG
300 if (msg != NULL) {
301 RecordComment("Abort message: ");
302 RecordComment(msg);
303 }
304#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000305 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100306 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +0000307
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 push(rax);
309 movq(kScratchRegister, p0, RelocInfo::NONE);
310 push(kScratchRegister);
311 movq(kScratchRegister,
Steve Blockd0582a62009-12-15 09:54:21 +0000312 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(p1 - p0))),
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 RelocInfo::NONE);
314 push(kScratchRegister);
315 CallRuntime(Runtime::kAbort, 2);
316 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +0000317 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +0000318}
319
320
321void MacroAssembler::CallStub(CodeStub* stub) {
322 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
323 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
324}
325
326
John Reck59135872010-11-02 12:39:01 -0700327MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100328 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -0700329 MaybeObject* result = stub->TryGetCode();
Ben Murdochbb769b22010-08-11 14:56:33 +0100330 if (!result->IsFailure()) {
John Reck59135872010-11-02 12:39:01 -0700331 call(Handle<Code>(Code::cast(result->ToObjectUnchecked())),
332 RelocInfo::CODE_TARGET);
Ben Murdochbb769b22010-08-11 14:56:33 +0100333 }
334 return result;
335}
336
337
Leon Clarkee46be812010-01-19 14:06:41 +0000338void MacroAssembler::TailCallStub(CodeStub* stub) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800339 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Leon Clarkee46be812010-01-19 14:06:41 +0000340 Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
341}
342
343
John Reck59135872010-11-02 12:39:01 -0700344MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100345 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -0700346 MaybeObject* result = stub->TryGetCode();
Ben Murdochbb769b22010-08-11 14:56:33 +0100347 if (!result->IsFailure()) {
John Reck59135872010-11-02 12:39:01 -0700348 jmp(Handle<Code>(Code::cast(result->ToObjectUnchecked())),
349 RelocInfo::CODE_TARGET);
Ben Murdochbb769b22010-08-11 14:56:33 +0100350 }
351 return result;
352}
353
354
Steve Blocka7e24c12009-10-30 11:49:00 +0000355void MacroAssembler::StubReturn(int argc) {
356 ASSERT(argc >= 1 && generating_stub());
357 ret((argc - 1) * kPointerSize);
358}
359
360
361void MacroAssembler::IllegalOperation(int num_arguments) {
362 if (num_arguments > 0) {
363 addq(rsp, Immediate(num_arguments * kPointerSize));
364 }
365 LoadRoot(rax, Heap::kUndefinedValueRootIndex);
366}
367
368
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100369void MacroAssembler::IndexFromHash(Register hash, Register index) {
370 // The assert checks that the constants for the maximum number of digits
371 // for an array index cached in the hash field and the number of bits
372 // reserved for it does not conflict.
373 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
374 (1 << String::kArrayIndexValueBits));
375 // We want the smi-tagged index in key. Even if we subsequently go to
376 // the slow case, converting the key to a smi is always valid.
377 // key: string key
378 // hash: key's hash field, including its array index value.
379 and_(hash, Immediate(String::kArrayIndexValueMask));
380 shr(hash, Immediate(String::kHashShift));
381 // Here we actually clobber the key which will be used if calling into
382 // runtime later. However as the new key is the numeric value of a string key
383 // there is no difference in using either key.
384 Integer32ToSmi(index, hash);
385}
386
387
Steve Blocka7e24c12009-10-30 11:49:00 +0000388void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
389 CallRuntime(Runtime::FunctionForId(id), num_arguments);
390}
391
392
Steve Block1e0659c2011-05-24 12:43:12 +0100393void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
394 Runtime::Function* function = Runtime::FunctionForId(id);
395 Set(rax, function->nargs);
396 movq(rbx, ExternalReference(function));
397 CEntryStub ces(1);
398 ces.SaveDoubles();
399 CallStub(&ces);
400}
401
402
John Reck59135872010-11-02 12:39:01 -0700403MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
404 int num_arguments) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100405 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
406}
407
408
Steve Blocka7e24c12009-10-30 11:49:00 +0000409void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
410 // If the expected number of arguments of the runtime function is
411 // constant, we check that the actual number of arguments match the
412 // expectation.
413 if (f->nargs >= 0 && f->nargs != num_arguments) {
414 IllegalOperation(num_arguments);
415 return;
416 }
417
Leon Clarke4515c472010-02-03 11:58:03 +0000418 // TODO(1236192): Most runtime routines don't need the number of
419 // arguments passed in because it is constant. At some point we
420 // should remove this need and make the runtime routine entry code
421 // smarter.
Steve Block8defd9f2010-07-08 12:39:36 +0100422 Set(rax, num_arguments);
Leon Clarke4515c472010-02-03 11:58:03 +0000423 movq(rbx, ExternalReference(f));
424 CEntryStub ces(f->result_size);
425 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +0000426}
427
428
John Reck59135872010-11-02 12:39:01 -0700429MaybeObject* MacroAssembler::TryCallRuntime(Runtime::Function* f,
430 int num_arguments) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100431 if (f->nargs >= 0 && f->nargs != num_arguments) {
432 IllegalOperation(num_arguments);
433 // Since we did not call the stub, there was no allocation failure.
434 // Return some non-failure object.
435 return Heap::undefined_value();
436 }
437
438 // TODO(1236192): Most runtime routines don't need the number of
439 // arguments passed in because it is constant. At some point we
440 // should remove this need and make the runtime routine entry code
441 // smarter.
442 Set(rax, num_arguments);
443 movq(rbx, ExternalReference(f));
444 CEntryStub ces(f->result_size);
445 return TryCallStub(&ces);
446}
447
448
Andrei Popescu402d9372010-02-26 13:31:12 +0000449void MacroAssembler::CallExternalReference(const ExternalReference& ext,
450 int num_arguments) {
Steve Block8defd9f2010-07-08 12:39:36 +0100451 Set(rax, num_arguments);
Andrei Popescu402d9372010-02-26 13:31:12 +0000452 movq(rbx, ext);
453
454 CEntryStub stub(1);
455 CallStub(&stub);
456}
457
458
Steve Block6ded16b2010-05-10 14:33:55 +0100459void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
460 int num_arguments,
461 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 // ----------- S t a t e -------------
463 // -- rsp[0] : return address
464 // -- rsp[8] : argument num_arguments - 1
465 // ...
466 // -- rsp[8 * num_arguments] : argument 0 (receiver)
467 // -----------------------------------
468
469 // TODO(1236192): Most runtime routines don't need the number of
470 // arguments passed in because it is constant. At some point we
471 // should remove this need and make the runtime routine entry code
472 // smarter.
Steve Block8defd9f2010-07-08 12:39:36 +0100473 Set(rax, num_arguments);
Steve Block6ded16b2010-05-10 14:33:55 +0100474 JumpToExternalReference(ext, result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000475}
476
477
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800478MaybeObject* MacroAssembler::TryTailCallExternalReference(
479 const ExternalReference& ext, int num_arguments, int result_size) {
480 // ----------- S t a t e -------------
481 // -- rsp[0] : return address
482 // -- rsp[8] : argument num_arguments - 1
483 // ...
484 // -- rsp[8 * num_arguments] : argument 0 (receiver)
485 // -----------------------------------
486
487 // TODO(1236192): Most runtime routines don't need the number of
488 // arguments passed in because it is constant. At some point we
489 // should remove this need and make the runtime routine entry code
490 // smarter.
491 Set(rax, num_arguments);
492 return TryJumpToExternalReference(ext, result_size);
493}
494
495
Steve Block6ded16b2010-05-10 14:33:55 +0100496void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
497 int num_arguments,
498 int result_size) {
499 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
500}
501
502
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800503MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
504 int num_arguments,
505 int result_size) {
506 return TryTailCallExternalReference(ExternalReference(fid),
507 num_arguments,
508 result_size);
509}
510
511
Ben Murdochbb769b22010-08-11 14:56:33 +0100512static int Offset(ExternalReference ref0, ExternalReference ref1) {
513 int64_t offset = (ref0.address() - ref1.address());
514 // Check that fits into int.
515 ASSERT(static_cast<int>(offset) == offset);
516 return static_cast<int>(offset);
517}
518
519
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800520void MacroAssembler::PrepareCallApiFunction(int arg_stack_space) {
521#ifdef _WIN64
522 // We need to prepare a slot for result handle on stack and put
523 // a pointer to it into 1st arg register.
524 EnterApiExitFrame(arg_stack_space + 1);
525
526 // rcx must be used to pass the pointer to the return value slot.
527 lea(rcx, StackSpaceOperand(arg_stack_space));
528#else
529 EnterApiExitFrame(arg_stack_space);
530#endif
Ben Murdochbb769b22010-08-11 14:56:33 +0100531}
532
533
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800534MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(
535 ApiFunction* function, int stack_space) {
John Reck59135872010-11-02 12:39:01 -0700536 Label empty_result;
537 Label prologue;
538 Label promote_scheduled_exception;
539 Label delete_allocated_handles;
540 Label leave_exit_frame;
Ben Murdochbb769b22010-08-11 14:56:33 +0100541 Label write_back;
Ben Murdochbb769b22010-08-11 14:56:33 +0100542
John Reck59135872010-11-02 12:39:01 -0700543 ExternalReference next_address =
544 ExternalReference::handle_scope_next_address();
545 const int kNextOffset = 0;
546 const int kLimitOffset = Offset(
547 ExternalReference::handle_scope_limit_address(),
548 next_address);
549 const int kLevelOffset = Offset(
550 ExternalReference::handle_scope_level_address(),
551 next_address);
552 ExternalReference scheduled_exception_address =
553 ExternalReference::scheduled_exception_address();
Ben Murdochbb769b22010-08-11 14:56:33 +0100554
John Reck59135872010-11-02 12:39:01 -0700555 // Allocate HandleScope in callee-save registers.
556 Register prev_next_address_reg = r14;
557 Register prev_limit_reg = rbx;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800558 Register base_reg = r12;
John Reck59135872010-11-02 12:39:01 -0700559 movq(base_reg, next_address);
560 movq(prev_next_address_reg, Operand(base_reg, kNextOffset));
561 movq(prev_limit_reg, Operand(base_reg, kLimitOffset));
562 addl(Operand(base_reg, kLevelOffset), Immediate(1));
563 // Call the api function!
564 movq(rax,
565 reinterpret_cast<int64_t>(function->address()),
566 RelocInfo::RUNTIME_ENTRY);
567 call(rax);
Ben Murdochbb769b22010-08-11 14:56:33 +0100568
John Reck59135872010-11-02 12:39:01 -0700569#ifdef _WIN64
570 // rax keeps a pointer to v8::Handle, unpack it.
571 movq(rax, Operand(rax, 0));
572#endif
573 // Check if the result handle holds 0.
574 testq(rax, rax);
575 j(zero, &empty_result);
576 // It was non-zero. Dereference to get the result value.
577 movq(rax, Operand(rax, 0));
578 bind(&prologue);
Ben Murdochbb769b22010-08-11 14:56:33 +0100579
John Reck59135872010-11-02 12:39:01 -0700580 // No more valid handles (the result handle was the last one). Restore
581 // previous handle scope.
582 subl(Operand(base_reg, kLevelOffset), Immediate(1));
583 movq(Operand(base_reg, kNextOffset), prev_next_address_reg);
584 cmpq(prev_limit_reg, Operand(base_reg, kLimitOffset));
585 j(not_equal, &delete_allocated_handles);
586 bind(&leave_exit_frame);
Ben Murdochbb769b22010-08-11 14:56:33 +0100587
John Reck59135872010-11-02 12:39:01 -0700588 // Check if the function scheduled an exception.
589 movq(rsi, scheduled_exception_address);
590 Cmp(Operand(rsi, 0), Factory::the_hole_value());
591 j(not_equal, &promote_scheduled_exception);
Ben Murdochbb769b22010-08-11 14:56:33 +0100592
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800593 LeaveApiExitFrame();
594 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -0700595
596 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800597 MaybeObject* result = TryTailCallRuntime(Runtime::kPromoteScheduledException,
598 0, 1);
599 if (result->IsFailure()) {
600 return result;
601 }
John Reck59135872010-11-02 12:39:01 -0700602
603 bind(&empty_result);
604 // It was zero; the result is undefined.
605 Move(rax, Factory::undefined_value());
606 jmp(&prologue);
607
608 // HandleScope limit has changed. Delete allocated extensions.
609 bind(&delete_allocated_handles);
610 movq(Operand(base_reg, kLimitOffset), prev_limit_reg);
611 movq(prev_limit_reg, rax);
612 movq(rax, ExternalReference::delete_handle_scope_extensions());
613 call(rax);
614 movq(rax, prev_limit_reg);
615 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800616
617 return result;
Ben Murdochbb769b22010-08-11 14:56:33 +0100618}
619
620
Steve Block6ded16b2010-05-10 14:33:55 +0100621void MacroAssembler::JumpToExternalReference(const ExternalReference& ext,
622 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 // Set the entry point and jump to the C entry runtime stub.
624 movq(rbx, ext);
625 CEntryStub ces(result_size);
Steve Block3ce2e202009-11-05 08:53:23 +0000626 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
Steve Blocka7e24c12009-10-30 11:49:00 +0000627}
628
629
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800630MaybeObject* MacroAssembler::TryJumpToExternalReference(
631 const ExternalReference& ext, int result_size) {
632 // Set the entry point and jump to the C entry runtime stub.
633 movq(rbx, ext);
634 CEntryStub ces(result_size);
635 return TryTailCallStub(&ces);
636}
637
638
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100639void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
640 InvokeFlag flag,
641 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000642 // Calls are not allowed in some stubs.
643 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
Steve Blocka7e24c12009-10-30 11:49:00 +0000644
Andrei Popescu402d9372010-02-26 13:31:12 +0000645 // Rely on the assertion to check that the number of provided
646 // arguments match the expected number of arguments. Fake a
647 // parameter count to avoid emitting code to do the check.
648 ParameterCount expected(0);
649 GetBuiltinEntry(rdx, id);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100650 InvokeCode(rdx, expected, expected, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000651}
652
Andrei Popescu402d9372010-02-26 13:31:12 +0000653
Steve Block791712a2010-08-27 10:21:07 +0100654void MacroAssembler::GetBuiltinFunction(Register target,
655 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +0100656 // Load the builtins object into target register.
657 movq(target, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
658 movq(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
Steve Block791712a2010-08-27 10:21:07 +0100659 movq(target, FieldOperand(target,
660 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
661}
Steve Block6ded16b2010-05-10 14:33:55 +0100662
Steve Block791712a2010-08-27 10:21:07 +0100663
664void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
665 ASSERT(!target.is(rdi));
Andrei Popescu402d9372010-02-26 13:31:12 +0000666 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +0100667 GetBuiltinFunction(rdi, id);
668 movq(target, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000669}
670
671
672void MacroAssembler::Set(Register dst, int64_t x) {
673 if (x == 0) {
Steve Block8defd9f2010-07-08 12:39:36 +0100674 xorl(dst, dst);
Steve Blocka7e24c12009-10-30 11:49:00 +0000675 } else if (is_int32(x)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000676 movq(dst, Immediate(static_cast<int32_t>(x)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000677 } else if (is_uint32(x)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000678 movl(dst, Immediate(static_cast<uint32_t>(x)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 } else {
680 movq(dst, x, RelocInfo::NONE);
681 }
682}
683
Steve Blocka7e24c12009-10-30 11:49:00 +0000684void MacroAssembler::Set(const Operand& dst, int64_t x) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100685 if (is_int32(x)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000686 movq(dst, Immediate(static_cast<int32_t>(x)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000687 } else {
688 movq(kScratchRegister, x, RelocInfo::NONE);
689 movq(dst, kScratchRegister);
690 }
691}
692
Steve Blocka7e24c12009-10-30 11:49:00 +0000693// ----------------------------------------------------------------------------
694// Smi tagging, untagging and tag detection.
695
Steve Block8defd9f2010-07-08 12:39:36 +0100696Register MacroAssembler::GetSmiConstant(Smi* source) {
697 int value = source->value();
698 if (value == 0) {
699 xorl(kScratchRegister, kScratchRegister);
700 return kScratchRegister;
701 }
702 if (value == 1) {
703 return kSmiConstantRegister;
704 }
705 LoadSmiConstant(kScratchRegister, source);
706 return kScratchRegister;
707}
708
709void MacroAssembler::LoadSmiConstant(Register dst, Smi* source) {
710 if (FLAG_debug_code) {
711 movq(dst,
712 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
713 RelocInfo::NONE);
714 cmpq(dst, kSmiConstantRegister);
715 if (allow_stub_calls()) {
716 Assert(equal, "Uninitialized kSmiConstantRegister");
717 } else {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100718 NearLabel ok;
Steve Block8defd9f2010-07-08 12:39:36 +0100719 j(equal, &ok);
720 int3();
721 bind(&ok);
722 }
723 }
724 if (source->value() == 0) {
725 xorl(dst, dst);
726 return;
727 }
728 int value = source->value();
729 bool negative = value < 0;
730 unsigned int uvalue = negative ? -value : value;
731
732 switch (uvalue) {
733 case 9:
734 lea(dst, Operand(kSmiConstantRegister, kSmiConstantRegister, times_8, 0));
735 break;
736 case 8:
737 xorl(dst, dst);
738 lea(dst, Operand(dst, kSmiConstantRegister, times_8, 0));
739 break;
740 case 4:
741 xorl(dst, dst);
742 lea(dst, Operand(dst, kSmiConstantRegister, times_4, 0));
743 break;
744 case 5:
745 lea(dst, Operand(kSmiConstantRegister, kSmiConstantRegister, times_4, 0));
746 break;
747 case 3:
748 lea(dst, Operand(kSmiConstantRegister, kSmiConstantRegister, times_2, 0));
749 break;
750 case 2:
751 lea(dst, Operand(kSmiConstantRegister, kSmiConstantRegister, times_1, 0));
752 break;
753 case 1:
754 movq(dst, kSmiConstantRegister);
755 break;
756 case 0:
757 UNREACHABLE();
758 return;
759 default:
760 movq(dst, reinterpret_cast<uint64_t>(source), RelocInfo::NONE);
761 return;
762 }
763 if (negative) {
764 neg(dst);
765 }
766}
767
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100768
Steve Blocka7e24c12009-10-30 11:49:00 +0000769void MacroAssembler::Integer32ToSmi(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000771 if (!dst.is(src)) {
772 movl(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 }
Steve Block3ce2e202009-11-05 08:53:23 +0000774 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000775}
776
777
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100778void MacroAssembler::Integer32ToSmiField(const Operand& dst, Register src) {
779 if (FLAG_debug_code) {
780 testb(dst, Immediate(0x01));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100781 NearLabel ok;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100782 j(zero, &ok);
783 if (allow_stub_calls()) {
784 Abort("Integer32ToSmiField writing to non-smi location");
785 } else {
786 int3();
787 }
788 bind(&ok);
789 }
790 ASSERT(kSmiShift % kBitsPerByte == 0);
791 movl(Operand(dst, kSmiShift / kBitsPerByte), src);
792}
793
794
Steve Block3ce2e202009-11-05 08:53:23 +0000795void MacroAssembler::Integer64PlusConstantToSmi(Register dst,
796 Register src,
797 int constant) {
798 if (dst.is(src)) {
799 addq(dst, Immediate(constant));
800 } else {
801 lea(dst, Operand(src, constant));
802 }
803 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000804}
805
806
807void MacroAssembler::SmiToInteger32(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 ASSERT_EQ(0, kSmiTag);
809 if (!dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000810 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 }
Steve Block3ce2e202009-11-05 08:53:23 +0000812 shr(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000813}
814
815
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100816void MacroAssembler::SmiToInteger32(Register dst, const Operand& src) {
817 movl(dst, Operand(src, kSmiShift / kBitsPerByte));
818}
819
820
Steve Blocka7e24c12009-10-30 11:49:00 +0000821void MacroAssembler::SmiToInteger64(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000823 if (!dst.is(src)) {
824 movq(dst, src);
825 }
826 sar(dst, Immediate(kSmiShift));
827}
828
829
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100830void MacroAssembler::SmiToInteger64(Register dst, const Operand& src) {
831 movsxlq(dst, Operand(src, kSmiShift / kBitsPerByte));
832}
833
834
Steve Block3ce2e202009-11-05 08:53:23 +0000835void MacroAssembler::SmiTest(Register src) {
836 testq(src, src);
837}
838
839
840void MacroAssembler::SmiCompare(Register dst, Register src) {
841 cmpq(dst, src);
842}
843
844
845void MacroAssembler::SmiCompare(Register dst, Smi* src) {
846 ASSERT(!dst.is(kScratchRegister));
847 if (src->value() == 0) {
848 testq(dst, dst);
849 } else {
Iain Merrick75681382010-08-19 15:07:18 +0100850 Register constant_reg = GetSmiConstant(src);
851 cmpq(dst, constant_reg);
Steve Block3ce2e202009-11-05 08:53:23 +0000852 }
853}
854
855
Leon Clarkef7060e22010-06-03 12:02:55 +0100856void MacroAssembler::SmiCompare(Register dst, const Operand& src) {
Steve Block6ded16b2010-05-10 14:33:55 +0100857 cmpq(dst, src);
858}
859
860
Steve Block3ce2e202009-11-05 08:53:23 +0000861void MacroAssembler::SmiCompare(const Operand& dst, Register src) {
862 cmpq(dst, src);
863}
864
865
866void MacroAssembler::SmiCompare(const Operand& dst, Smi* src) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100867 cmpl(Operand(dst, kSmiShift / kBitsPerByte), Immediate(src->value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000868}
869
870
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100871void MacroAssembler::SmiCompareInteger32(const Operand& dst, Register src) {
872 cmpl(Operand(dst, kSmiShift / kBitsPerByte), src);
873}
874
875
Steve Blocka7e24c12009-10-30 11:49:00 +0000876void MacroAssembler::PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
877 Register src,
878 int power) {
879 ASSERT(power >= 0);
880 ASSERT(power < 64);
881 if (power == 0) {
882 SmiToInteger64(dst, src);
883 return;
884 }
Steve Block3ce2e202009-11-05 08:53:23 +0000885 if (!dst.is(src)) {
886 movq(dst, src);
887 }
888 if (power < kSmiShift) {
889 sar(dst, Immediate(kSmiShift - power));
890 } else if (power > kSmiShift) {
891 shl(dst, Immediate(power - kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 }
893}
894
895
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100896void MacroAssembler::PositiveSmiDivPowerOfTwoToInteger32(Register dst,
897 Register src,
898 int power) {
899 ASSERT((0 <= power) && (power < 32));
900 if (dst.is(src)) {
901 shr(dst, Immediate(power + kSmiShift));
902 } else {
903 UNIMPLEMENTED(); // Not used.
904 }
905}
906
907
Steve Blocka7e24c12009-10-30 11:49:00 +0000908Condition MacroAssembler::CheckSmi(Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 ASSERT_EQ(0, kSmiTag);
910 testb(src, Immediate(kSmiTagMask));
Steve Block3ce2e202009-11-05 08:53:23 +0000911 return zero;
Steve Blocka7e24c12009-10-30 11:49:00 +0000912}
913
914
Steve Block1e0659c2011-05-24 12:43:12 +0100915Condition MacroAssembler::CheckSmi(const Operand& src) {
916 ASSERT_EQ(0, kSmiTag);
917 testb(src, Immediate(kSmiTagMask));
918 return zero;
919}
920
921
Ben Murdochf87a2032010-10-22 12:50:53 +0100922Condition MacroAssembler::CheckNonNegativeSmi(Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000923 ASSERT_EQ(0, kSmiTag);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100924 // Test that both bits of the mask 0x8000000000000001 are zero.
Steve Block3ce2e202009-11-05 08:53:23 +0000925 movq(kScratchRegister, src);
926 rol(kScratchRegister, Immediate(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100927 testb(kScratchRegister, Immediate(3));
Steve Blocka7e24c12009-10-30 11:49:00 +0000928 return zero;
929}
930
931
Steve Blocka7e24c12009-10-30 11:49:00 +0000932Condition MacroAssembler::CheckBothSmi(Register first, Register second) {
933 if (first.is(second)) {
934 return CheckSmi(first);
935 }
Steve Block8defd9f2010-07-08 12:39:36 +0100936 ASSERT(kSmiTag == 0 && kHeapObjectTag == 1 && kHeapObjectTagMask == 3);
937 leal(kScratchRegister, Operand(first, second, times_1, 0));
938 testb(kScratchRegister, Immediate(0x03));
Steve Block3ce2e202009-11-05 08:53:23 +0000939 return zero;
Steve Blocka7e24c12009-10-30 11:49:00 +0000940}
941
942
Ben Murdochf87a2032010-10-22 12:50:53 +0100943Condition MacroAssembler::CheckBothNonNegativeSmi(Register first,
944 Register second) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000945 if (first.is(second)) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100946 return CheckNonNegativeSmi(first);
Leon Clarked91b9f72010-01-27 17:25:45 +0000947 }
Steve Block8defd9f2010-07-08 12:39:36 +0100948 movq(kScratchRegister, first);
949 or_(kScratchRegister, second);
Leon Clarked91b9f72010-01-27 17:25:45 +0000950 rol(kScratchRegister, Immediate(1));
Ben Murdochf87a2032010-10-22 12:50:53 +0100951 testl(kScratchRegister, Immediate(3));
Leon Clarked91b9f72010-01-27 17:25:45 +0000952 return zero;
953}
954
955
Ben Murdochbb769b22010-08-11 14:56:33 +0100956Condition MacroAssembler::CheckEitherSmi(Register first,
957 Register second,
958 Register scratch) {
Leon Clarkee46be812010-01-19 14:06:41 +0000959 if (first.is(second)) {
960 return CheckSmi(first);
961 }
Ben Murdochbb769b22010-08-11 14:56:33 +0100962 if (scratch.is(second)) {
963 andl(scratch, first);
964 } else {
965 if (!scratch.is(first)) {
966 movl(scratch, first);
967 }
968 andl(scratch, second);
969 }
970 testb(scratch, Immediate(kSmiTagMask));
Leon Clarkee46be812010-01-19 14:06:41 +0000971 return zero;
972}
973
974
Steve Blocka7e24c12009-10-30 11:49:00 +0000975Condition MacroAssembler::CheckIsMinSmi(Register src) {
Steve Block8defd9f2010-07-08 12:39:36 +0100976 ASSERT(!src.is(kScratchRegister));
977 // If we overflow by subtracting one, it's the minimal smi value.
978 cmpq(src, kSmiConstantRegister);
979 return overflow;
Steve Blocka7e24c12009-10-30 11:49:00 +0000980}
981
Steve Blocka7e24c12009-10-30 11:49:00 +0000982
983Condition MacroAssembler::CheckInteger32ValidSmiValue(Register src) {
Steve Block3ce2e202009-11-05 08:53:23 +0000984 // A 32-bit integer value can always be converted to a smi.
985 return always;
Steve Blocka7e24c12009-10-30 11:49:00 +0000986}
987
988
Steve Block3ce2e202009-11-05 08:53:23 +0000989Condition MacroAssembler::CheckUInteger32ValidSmiValue(Register src) {
990 // An unsigned 32-bit integer value is valid as long as the high bit
991 // is not set.
Steve Block8defd9f2010-07-08 12:39:36 +0100992 testl(src, src);
993 return positive;
Steve Block3ce2e202009-11-05 08:53:23 +0000994}
995
996
Steve Block1e0659c2011-05-24 12:43:12 +0100997void MacroAssembler::CheckSmiToIndicator(Register dst, Register src) {
998 if (dst.is(src)) {
999 andl(dst, Immediate(kSmiTagMask));
1000 } else {
1001 movl(dst, Immediate(kSmiTagMask));
1002 andl(dst, src);
1003 }
1004}
1005
1006
1007void MacroAssembler::CheckSmiToIndicator(Register dst, const Operand& src) {
1008 if (!(src.AddressUsesRegister(dst))) {
1009 movl(dst, Immediate(kSmiTagMask));
1010 andl(dst, src);
1011 } else {
1012 movl(dst, src);
1013 andl(dst, Immediate(kSmiTagMask));
1014 }
1015}
1016
1017
Steve Block3ce2e202009-11-05 08:53:23 +00001018void MacroAssembler::SmiAddConstant(Register dst, Register src, Smi* constant) {
1019 if (constant->value() == 0) {
1020 if (!dst.is(src)) {
1021 movq(dst, src);
1022 }
Steve Block8defd9f2010-07-08 12:39:36 +01001023 return;
Steve Block3ce2e202009-11-05 08:53:23 +00001024 } else if (dst.is(src)) {
1025 ASSERT(!dst.is(kScratchRegister));
Steve Block8defd9f2010-07-08 12:39:36 +01001026 switch (constant->value()) {
1027 case 1:
1028 addq(dst, kSmiConstantRegister);
1029 return;
1030 case 2:
1031 lea(dst, Operand(src, kSmiConstantRegister, times_2, 0));
1032 return;
1033 case 4:
1034 lea(dst, Operand(src, kSmiConstantRegister, times_4, 0));
1035 return;
1036 case 8:
1037 lea(dst, Operand(src, kSmiConstantRegister, times_8, 0));
1038 return;
1039 default:
1040 Register constant_reg = GetSmiConstant(constant);
1041 addq(dst, constant_reg);
1042 return;
1043 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001044 } else {
Steve Block8defd9f2010-07-08 12:39:36 +01001045 switch (constant->value()) {
1046 case 1:
1047 lea(dst, Operand(src, kSmiConstantRegister, times_1, 0));
1048 return;
1049 case 2:
1050 lea(dst, Operand(src, kSmiConstantRegister, times_2, 0));
1051 return;
1052 case 4:
1053 lea(dst, Operand(src, kSmiConstantRegister, times_4, 0));
1054 return;
1055 case 8:
1056 lea(dst, Operand(src, kSmiConstantRegister, times_8, 0));
1057 return;
1058 default:
1059 LoadSmiConstant(dst, constant);
1060 addq(dst, src);
1061 return;
1062 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001063 }
1064}
1065
1066
Leon Clarkef7060e22010-06-03 12:02:55 +01001067void MacroAssembler::SmiAddConstant(const Operand& dst, Smi* constant) {
1068 if (constant->value() != 0) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001069 addl(Operand(dst, kSmiShift / kBitsPerByte), Immediate(constant->value()));
Leon Clarkef7060e22010-06-03 12:02:55 +01001070 }
1071}
1072
1073
Steve Block3ce2e202009-11-05 08:53:23 +00001074void MacroAssembler::SmiSubConstant(Register dst, Register src, Smi* constant) {
1075 if (constant->value() == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001076 if (!dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001077 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 }
Steve Block3ce2e202009-11-05 08:53:23 +00001079 } else if (dst.is(src)) {
1080 ASSERT(!dst.is(kScratchRegister));
Steve Block8defd9f2010-07-08 12:39:36 +01001081 Register constant_reg = GetSmiConstant(constant);
1082 subq(dst, constant_reg);
Steve Block3ce2e202009-11-05 08:53:23 +00001083 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001084 if (constant->value() == Smi::kMinValue) {
Steve Block8defd9f2010-07-08 12:39:36 +01001085 LoadSmiConstant(dst, constant);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001086 // Adding and subtracting the min-value gives the same result, it only
1087 // differs on the overflow bit, which we don't check here.
1088 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001089 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001090 // Subtract by adding the negation.
Steve Block8defd9f2010-07-08 12:39:36 +01001091 LoadSmiConstant(dst, Smi::FromInt(-constant->value()));
Steve Block3ce2e202009-11-05 08:53:23 +00001092 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001093 }
1094 }
1095}
1096
1097
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001098void MacroAssembler::SmiAdd(Register dst,
1099 Register src1,
1100 Register src2) {
1101 // No overflow checking. Use only when it's known that
1102 // overflowing is impossible.
1103 ASSERT(!dst.is(src2));
1104 if (dst.is(src1)) {
1105 addq(dst, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 } else {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001107 movq(dst, src1);
1108 addq(dst, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001109 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001110 Assert(no_overflow, "Smi addition overflow");
Steve Blocka7e24c12009-10-30 11:49:00 +00001111}
1112
1113
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001114void MacroAssembler::SmiSub(Register dst, Register src1, Register src2) {
1115 // No overflow checking. Use only when it's known that
1116 // overflowing is impossible (e.g., subtracting two positive smis).
1117 ASSERT(!dst.is(src2));
1118 if (dst.is(src1)) {
1119 subq(dst, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001120 } else {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001121 movq(dst, src1);
1122 subq(dst, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001123 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001124 Assert(no_overflow, "Smi subtraction overflow");
Steve Blocka7e24c12009-10-30 11:49:00 +00001125}
1126
1127
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001128void MacroAssembler::SmiSub(Register dst,
Steve Blocka7e24c12009-10-30 11:49:00 +00001129 Register src1,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001130 const Operand& src2) {
1131 // No overflow checking. Use only when it's known that
1132 // overflowing is impossible (e.g., subtracting two positive smis).
1133 if (dst.is(src1)) {
1134 subq(dst, src2);
1135 } else {
1136 movq(dst, src1);
1137 subq(dst, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001138 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001139 Assert(no_overflow, "Smi subtraction overflow");
Steve Blocka7e24c12009-10-30 11:49:00 +00001140}
1141
1142
1143void MacroAssembler::SmiNot(Register dst, Register src) {
Steve Block3ce2e202009-11-05 08:53:23 +00001144 ASSERT(!dst.is(kScratchRegister));
1145 ASSERT(!src.is(kScratchRegister));
1146 // Set tag and padding bits before negating, so that they are zero afterwards.
1147 movl(kScratchRegister, Immediate(~0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 if (dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001149 xor_(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +00001150 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001151 lea(dst, Operand(src, kScratchRegister, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001152 }
Steve Block3ce2e202009-11-05 08:53:23 +00001153 not_(dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001154}
1155
1156
1157void MacroAssembler::SmiAnd(Register dst, Register src1, Register src2) {
Steve Block3ce2e202009-11-05 08:53:23 +00001158 ASSERT(!dst.is(src2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001160 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001161 }
1162 and_(dst, src2);
1163}
1164
1165
Steve Block3ce2e202009-11-05 08:53:23 +00001166void MacroAssembler::SmiAndConstant(Register dst, Register src, Smi* constant) {
1167 if (constant->value() == 0) {
Steve Block9fac8402011-05-12 15:51:54 +01001168 Set(dst, 0);
Steve Block3ce2e202009-11-05 08:53:23 +00001169 } else if (dst.is(src)) {
1170 ASSERT(!dst.is(kScratchRegister));
Steve Block8defd9f2010-07-08 12:39:36 +01001171 Register constant_reg = GetSmiConstant(constant);
1172 and_(dst, constant_reg);
Steve Block3ce2e202009-11-05 08:53:23 +00001173 } else {
Steve Block8defd9f2010-07-08 12:39:36 +01001174 LoadSmiConstant(dst, constant);
Steve Block3ce2e202009-11-05 08:53:23 +00001175 and_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001176 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001177}
1178
1179
1180void MacroAssembler::SmiOr(Register dst, Register src1, Register src2) {
1181 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001182 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001183 }
1184 or_(dst, src2);
1185}
1186
1187
Steve Block3ce2e202009-11-05 08:53:23 +00001188void MacroAssembler::SmiOrConstant(Register dst, Register src, Smi* constant) {
1189 if (dst.is(src)) {
1190 ASSERT(!dst.is(kScratchRegister));
Steve Block8defd9f2010-07-08 12:39:36 +01001191 Register constant_reg = GetSmiConstant(constant);
1192 or_(dst, constant_reg);
Steve Block3ce2e202009-11-05 08:53:23 +00001193 } else {
Steve Block8defd9f2010-07-08 12:39:36 +01001194 LoadSmiConstant(dst, constant);
Steve Block3ce2e202009-11-05 08:53:23 +00001195 or_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001196 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001197}
1198
Steve Block3ce2e202009-11-05 08:53:23 +00001199
Steve Blocka7e24c12009-10-30 11:49:00 +00001200void MacroAssembler::SmiXor(Register dst, Register src1, Register src2) {
1201 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001202 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001203 }
1204 xor_(dst, src2);
1205}
1206
1207
Steve Block3ce2e202009-11-05 08:53:23 +00001208void MacroAssembler::SmiXorConstant(Register dst, Register src, Smi* constant) {
1209 if (dst.is(src)) {
1210 ASSERT(!dst.is(kScratchRegister));
Steve Block8defd9f2010-07-08 12:39:36 +01001211 Register constant_reg = GetSmiConstant(constant);
1212 xor_(dst, constant_reg);
Steve Block3ce2e202009-11-05 08:53:23 +00001213 } else {
Steve Block8defd9f2010-07-08 12:39:36 +01001214 LoadSmiConstant(dst, constant);
Steve Block3ce2e202009-11-05 08:53:23 +00001215 xor_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001217}
1218
1219
Steve Blocka7e24c12009-10-30 11:49:00 +00001220void MacroAssembler::SmiShiftArithmeticRightConstant(Register dst,
1221 Register src,
1222 int shift_value) {
Steve Block3ce2e202009-11-05 08:53:23 +00001223 ASSERT(is_uint5(shift_value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 if (shift_value > 0) {
1225 if (dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001226 sar(dst, Immediate(shift_value + kSmiShift));
1227 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 } else {
1229 UNIMPLEMENTED(); // Not used.
1230 }
1231 }
1232}
1233
1234
Steve Blocka7e24c12009-10-30 11:49:00 +00001235void MacroAssembler::SmiShiftLeftConstant(Register dst,
1236 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +01001237 int shift_value) {
Steve Block3ce2e202009-11-05 08:53:23 +00001238 if (!dst.is(src)) {
1239 movq(dst, src);
1240 }
1241 if (shift_value > 0) {
1242 shl(dst, Immediate(shift_value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001243 }
1244}
1245
1246
1247void MacroAssembler::SmiShiftLeft(Register dst,
1248 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +01001249 Register src2) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001250 ASSERT(!dst.is(rcx));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001251 NearLabel result_ok;
Steve Block3ce2e202009-11-05 08:53:23 +00001252 // Untag shift amount.
1253 if (!dst.is(src1)) {
1254 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001255 }
Steve Block3ce2e202009-11-05 08:53:23 +00001256 SmiToInteger32(rcx, src2);
1257 // Shift amount specified by lower 5 bits, not six as the shl opcode.
1258 and_(rcx, Immediate(0x1f));
Steve Blockd0582a62009-12-15 09:54:21 +00001259 shl_cl(dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001260}
1261
1262
Steve Blocka7e24c12009-10-30 11:49:00 +00001263void MacroAssembler::SmiShiftArithmeticRight(Register dst,
1264 Register src1,
1265 Register src2) {
Steve Block3ce2e202009-11-05 08:53:23 +00001266 ASSERT(!dst.is(kScratchRegister));
1267 ASSERT(!src1.is(kScratchRegister));
1268 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001269 ASSERT(!dst.is(rcx));
Steve Block3ce2e202009-11-05 08:53:23 +00001270 if (src1.is(rcx)) {
1271 movq(kScratchRegister, src1);
1272 } else if (src2.is(rcx)) {
1273 movq(kScratchRegister, src2);
1274 }
1275 if (!dst.is(src1)) {
1276 movq(dst, src1);
1277 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001278 SmiToInteger32(rcx, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001279 orl(rcx, Immediate(kSmiShift));
Steve Blockd0582a62009-12-15 09:54:21 +00001280 sar_cl(dst); // Shift 32 + original rcx & 0x1f.
Steve Block3ce2e202009-11-05 08:53:23 +00001281 shl(dst, Immediate(kSmiShift));
1282 if (src1.is(rcx)) {
1283 movq(src1, kScratchRegister);
1284 } else if (src2.is(rcx)) {
1285 movq(src2, kScratchRegister);
1286 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001287}
1288
1289
Steve Block3ce2e202009-11-05 08:53:23 +00001290SmiIndex MacroAssembler::SmiToIndex(Register dst,
1291 Register src,
1292 int shift) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001293 ASSERT(is_uint6(shift));
Steve Block3ce2e202009-11-05 08:53:23 +00001294 // There is a possible optimization if shift is in the range 60-63, but that
1295 // will (and must) never happen.
1296 if (!dst.is(src)) {
1297 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 }
Steve Block3ce2e202009-11-05 08:53:23 +00001299 if (shift < kSmiShift) {
1300 sar(dst, Immediate(kSmiShift - shift));
1301 } else {
1302 shl(dst, Immediate(shift - kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001303 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001304 return SmiIndex(dst, times_1);
1305}
1306
Steve Blocka7e24c12009-10-30 11:49:00 +00001307SmiIndex MacroAssembler::SmiToNegativeIndex(Register dst,
1308 Register src,
1309 int shift) {
1310 // Register src holds a positive smi.
1311 ASSERT(is_uint6(shift));
Steve Block3ce2e202009-11-05 08:53:23 +00001312 if (!dst.is(src)) {
1313 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001314 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001315 neg(dst);
Steve Block3ce2e202009-11-05 08:53:23 +00001316 if (shift < kSmiShift) {
1317 sar(dst, Immediate(kSmiShift - shift));
1318 } else {
1319 shl(dst, Immediate(shift - kSmiShift));
1320 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001321 return SmiIndex(dst, times_1);
1322}
1323
1324
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001325void MacroAssembler::Move(Register dst, Register src) {
1326 if (!dst.is(src)) {
1327 movq(dst, src);
Steve Block6ded16b2010-05-10 14:33:55 +01001328 }
Steve Block6ded16b2010-05-10 14:33:55 +01001329}
1330
1331
Steve Blocka7e24c12009-10-30 11:49:00 +00001332void MacroAssembler::Move(Register dst, Handle<Object> source) {
1333 ASSERT(!source->IsFailure());
1334 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001335 Move(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001336 } else {
1337 movq(dst, source, RelocInfo::EMBEDDED_OBJECT);
1338 }
1339}
1340
1341
1342void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001343 ASSERT(!source->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +00001344 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001345 Move(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001346 } else {
1347 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1348 movq(dst, kScratchRegister);
1349 }
1350}
1351
1352
1353void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001354 if (source->IsSmi()) {
1355 SmiCompare(dst, Smi::cast(*source));
1356 } else {
1357 Move(kScratchRegister, source);
1358 cmpq(dst, kScratchRegister);
1359 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001360}
1361
1362
1363void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
1364 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001365 SmiCompare(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001366 } else {
1367 ASSERT(source->IsHeapObject());
1368 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1369 cmpq(dst, kScratchRegister);
1370 }
1371}
1372
1373
1374void MacroAssembler::Push(Handle<Object> source) {
1375 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001376 Push(Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001377 } else {
1378 ASSERT(source->IsHeapObject());
1379 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1380 push(kScratchRegister);
1381 }
1382}
1383
1384
1385void MacroAssembler::Push(Smi* source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001386 intptr_t smi = reinterpret_cast<intptr_t>(source);
1387 if (is_int32(smi)) {
1388 push(Immediate(static_cast<int32_t>(smi)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001389 } else {
Steve Block8defd9f2010-07-08 12:39:36 +01001390 Register constant = GetSmiConstant(source);
1391 push(constant);
Steve Block3ce2e202009-11-05 08:53:23 +00001392 }
1393}
1394
1395
Leon Clarkee46be812010-01-19 14:06:41 +00001396void MacroAssembler::Drop(int stack_elements) {
1397 if (stack_elements > 0) {
1398 addq(rsp, Immediate(stack_elements * kPointerSize));
1399 }
1400}
1401
1402
Steve Block3ce2e202009-11-05 08:53:23 +00001403void MacroAssembler::Test(const Operand& src, Smi* source) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001404 testl(Operand(src, kIntSize), Immediate(source->value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001405}
1406
1407
1408void MacroAssembler::Jump(ExternalReference ext) {
1409 movq(kScratchRegister, ext);
1410 jmp(kScratchRegister);
1411}
1412
1413
1414void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
1415 movq(kScratchRegister, destination, rmode);
1416 jmp(kScratchRegister);
1417}
1418
1419
1420void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
Steve Block3ce2e202009-11-05 08:53:23 +00001421 // TODO(X64): Inline this
1422 jmp(code_object, rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001423}
1424
1425
1426void MacroAssembler::Call(ExternalReference ext) {
1427 movq(kScratchRegister, ext);
1428 call(kScratchRegister);
1429}
1430
1431
1432void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
1433 movq(kScratchRegister, destination, rmode);
1434 call(kScratchRegister);
1435}
1436
1437
1438void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
1439 ASSERT(RelocInfo::IsCodeTarget(rmode));
Steve Block3ce2e202009-11-05 08:53:23 +00001440 call(code_object, rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001441}
1442
1443
Steve Block1e0659c2011-05-24 12:43:12 +01001444void MacroAssembler::Pushad() {
1445 push(rax);
1446 push(rcx);
1447 push(rdx);
1448 push(rbx);
1449 // Not pushing rsp or rbp.
1450 push(rsi);
1451 push(rdi);
1452 push(r8);
1453 push(r9);
1454 // r10 is kScratchRegister.
1455 push(r11);
1456 push(r12);
1457 // r13 is kRootRegister.
1458 push(r14);
1459 // r15 is kSmiConstantRegister
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001460 STATIC_ASSERT(11 == kNumSafepointSavedRegisters);
1461 // Use lea for symmetry with Popad.
1462 int sp_delta =
1463 (kNumSafepointRegisters - kNumSafepointSavedRegisters) * kPointerSize;
1464 lea(rsp, Operand(rsp, -sp_delta));
Steve Block1e0659c2011-05-24 12:43:12 +01001465}
1466
1467
1468void MacroAssembler::Popad() {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001469 // Popad must not change the flags, so use lea instead of addq.
1470 int sp_delta =
1471 (kNumSafepointRegisters - kNumSafepointSavedRegisters) * kPointerSize;
1472 lea(rsp, Operand(rsp, sp_delta));
Steve Block1e0659c2011-05-24 12:43:12 +01001473 pop(r14);
1474 pop(r12);
1475 pop(r11);
1476 pop(r9);
1477 pop(r8);
1478 pop(rdi);
1479 pop(rsi);
1480 pop(rbx);
1481 pop(rdx);
1482 pop(rcx);
1483 pop(rax);
1484}
1485
1486
1487void MacroAssembler::Dropad() {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001488 addq(rsp, Immediate(kNumSafepointRegisters * kPointerSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001489}
1490
1491
1492// Order general registers are pushed by Pushad:
1493// rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14.
1494int MacroAssembler::kSafepointPushRegisterIndices[Register::kNumRegisters] = {
1495 0,
1496 1,
1497 2,
1498 3,
1499 -1,
1500 -1,
1501 4,
1502 5,
1503 6,
1504 7,
1505 -1,
1506 8,
1507 9,
1508 -1,
1509 10,
1510 -1
1511};
1512
1513
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001514void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
1515 movq(SafepointRegisterSlot(dst), src);
1516}
1517
1518
1519void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
1520 movq(dst, SafepointRegisterSlot(src));
1521}
1522
1523
1524Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
1525 return Operand(rsp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
1526}
1527
1528
Steve Blocka7e24c12009-10-30 11:49:00 +00001529void MacroAssembler::PushTryHandler(CodeLocation try_location,
1530 HandlerType type) {
1531 // Adjust this code if not the case.
1532 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1533
1534 // The pc (return address) is already on TOS. This code pushes state,
1535 // frame pointer and current handler. Check that they are expected
1536 // next on the stack, in that order.
1537 ASSERT_EQ(StackHandlerConstants::kStateOffset,
1538 StackHandlerConstants::kPCOffset - kPointerSize);
1539 ASSERT_EQ(StackHandlerConstants::kFPOffset,
1540 StackHandlerConstants::kStateOffset - kPointerSize);
1541 ASSERT_EQ(StackHandlerConstants::kNextOffset,
1542 StackHandlerConstants::kFPOffset - kPointerSize);
1543
1544 if (try_location == IN_JAVASCRIPT) {
1545 if (type == TRY_CATCH_HANDLER) {
1546 push(Immediate(StackHandler::TRY_CATCH));
1547 } else {
1548 push(Immediate(StackHandler::TRY_FINALLY));
1549 }
1550 push(rbp);
1551 } else {
1552 ASSERT(try_location == IN_JS_ENTRY);
1553 // The frame pointer does not point to a JS frame so we save NULL
1554 // for rbp. We expect the code throwing an exception to check rbp
1555 // before dereferencing it to restore the context.
1556 push(Immediate(StackHandler::ENTRY));
1557 push(Immediate(0)); // NULL frame pointer.
1558 }
1559 // Save the current handler.
1560 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
1561 push(Operand(kScratchRegister, 0));
1562 // Link this handler.
1563 movq(Operand(kScratchRegister, 0), rsp);
1564}
1565
1566
Leon Clarkee46be812010-01-19 14:06:41 +00001567void MacroAssembler::PopTryHandler() {
1568 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
1569 // Unlink this handler.
1570 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
1571 pop(Operand(kScratchRegister, 0));
1572 // Remove the remaining fields.
1573 addq(rsp, Immediate(StackHandlerConstants::kSize - kPointerSize));
1574}
1575
1576
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001577void MacroAssembler::Throw(Register value) {
1578 // Check that stack should contain next handler, frame pointer, state and
1579 // return address in that order.
1580 STATIC_ASSERT(StackHandlerConstants::kFPOffset + kPointerSize ==
1581 StackHandlerConstants::kStateOffset);
1582 STATIC_ASSERT(StackHandlerConstants::kStateOffset + kPointerSize ==
1583 StackHandlerConstants::kPCOffset);
1584 // Keep thrown value in rax.
1585 if (!value.is(rax)) {
1586 movq(rax, value);
1587 }
1588
1589 ExternalReference handler_address(Top::k_handler_address);
1590 movq(kScratchRegister, handler_address);
1591 movq(rsp, Operand(kScratchRegister, 0));
1592 // get next in chain
1593 pop(rcx);
1594 movq(Operand(kScratchRegister, 0), rcx);
1595 pop(rbp); // pop frame pointer
1596 pop(rdx); // remove state
1597
1598 // Before returning we restore the context from the frame pointer if not NULL.
1599 // The frame pointer is NULL in the exception handler of a JS entry frame.
1600 Set(rsi, 0); // Tentatively set context pointer to NULL
1601 NearLabel skip;
1602 cmpq(rbp, Immediate(0));
1603 j(equal, &skip);
1604 movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
1605 bind(&skip);
1606 ret(0);
1607}
1608
1609
1610void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
1611 Register value) {
1612 // Keep thrown value in rax.
1613 if (!value.is(rax)) {
1614 movq(rax, value);
1615 }
1616 // Fetch top stack handler.
1617 ExternalReference handler_address(Top::k_handler_address);
1618 movq(kScratchRegister, handler_address);
1619 movq(rsp, Operand(kScratchRegister, 0));
1620
1621 // Unwind the handlers until the ENTRY handler is found.
1622 NearLabel loop, done;
1623 bind(&loop);
1624 // Load the type of the current stack handler.
1625 const int kStateOffset = StackHandlerConstants::kStateOffset;
1626 cmpq(Operand(rsp, kStateOffset), Immediate(StackHandler::ENTRY));
1627 j(equal, &done);
1628 // Fetch the next handler in the list.
1629 const int kNextOffset = StackHandlerConstants::kNextOffset;
1630 movq(rsp, Operand(rsp, kNextOffset));
1631 jmp(&loop);
1632 bind(&done);
1633
1634 // Set the top handler address to next handler past the current ENTRY handler.
1635 movq(kScratchRegister, handler_address);
1636 pop(Operand(kScratchRegister, 0));
1637
1638 if (type == OUT_OF_MEMORY) {
1639 // Set external caught exception to false.
1640 ExternalReference external_caught(Top::k_external_caught_exception_address);
1641 movq(rax, Immediate(false));
1642 store_rax(external_caught);
1643
1644 // Set pending exception and rax to out of memory exception.
1645 ExternalReference pending_exception(Top::k_pending_exception_address);
1646 movq(rax, Failure::OutOfMemoryException(), RelocInfo::NONE);
1647 store_rax(pending_exception);
1648 }
1649
1650 // Clear the context pointer.
1651 Set(rsi, 0);
1652
1653 // Restore registers from handler.
1654 STATIC_ASSERT(StackHandlerConstants::kNextOffset + kPointerSize ==
1655 StackHandlerConstants::kFPOffset);
1656 pop(rbp); // FP
1657 STATIC_ASSERT(StackHandlerConstants::kFPOffset + kPointerSize ==
1658 StackHandlerConstants::kStateOffset);
1659 pop(rdx); // State
1660
1661 STATIC_ASSERT(StackHandlerConstants::kStateOffset + kPointerSize ==
1662 StackHandlerConstants::kPCOffset);
1663 ret(0);
1664}
1665
1666
Steve Blocka7e24c12009-10-30 11:49:00 +00001667void MacroAssembler::Ret() {
1668 ret(0);
1669}
1670
1671
Steve Block1e0659c2011-05-24 12:43:12 +01001672void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1673 if (is_uint16(bytes_dropped)) {
1674 ret(bytes_dropped);
1675 } else {
1676 pop(scratch);
1677 addq(rsp, Immediate(bytes_dropped));
1678 push(scratch);
1679 ret(0);
1680 }
1681}
1682
1683
Steve Blocka7e24c12009-10-30 11:49:00 +00001684void MacroAssembler::FCmp() {
Steve Block3ce2e202009-11-05 08:53:23 +00001685 fucomip();
Steve Block8defd9f2010-07-08 12:39:36 +01001686 fstp(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001687}
1688
1689
1690void MacroAssembler::CmpObjectType(Register heap_object,
1691 InstanceType type,
1692 Register map) {
1693 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
1694 CmpInstanceType(map, type);
1695}
1696
1697
1698void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
1699 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
1700 Immediate(static_cast<int8_t>(type)));
1701}
1702
1703
Andrei Popescu31002712010-02-23 13:46:05 +00001704void MacroAssembler::CheckMap(Register obj,
1705 Handle<Map> map,
1706 Label* fail,
1707 bool is_heap_object) {
1708 if (!is_heap_object) {
1709 JumpIfSmi(obj, fail);
1710 }
1711 Cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
1712 j(not_equal, fail);
1713}
1714
1715
Leon Clarkef7060e22010-06-03 12:02:55 +01001716void MacroAssembler::AbortIfNotNumber(Register object) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001717 NearLabel ok;
Andrei Popescu402d9372010-02-26 13:31:12 +00001718 Condition is_smi = CheckSmi(object);
1719 j(is_smi, &ok);
1720 Cmp(FieldOperand(object, HeapObject::kMapOffset),
1721 Factory::heap_number_map());
Leon Clarkef7060e22010-06-03 12:02:55 +01001722 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +00001723 bind(&ok);
1724}
1725
1726
Iain Merrick75681382010-08-19 15:07:18 +01001727void MacroAssembler::AbortIfSmi(Register object) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001728 NearLabel ok;
Iain Merrick75681382010-08-19 15:07:18 +01001729 Condition is_smi = CheckSmi(object);
1730 Assert(NegateCondition(is_smi), "Operand is a smi");
1731}
1732
1733
Leon Clarkef7060e22010-06-03 12:02:55 +01001734void MacroAssembler::AbortIfNotSmi(Register object) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001735 NearLabel ok;
Steve Block6ded16b2010-05-10 14:33:55 +01001736 Condition is_smi = CheckSmi(object);
Iain Merrick75681382010-08-19 15:07:18 +01001737 Assert(is_smi, "Operand is not a smi");
Steve Block6ded16b2010-05-10 14:33:55 +01001738}
1739
1740
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001741void MacroAssembler::AbortIfNotString(Register object) {
1742 testb(object, Immediate(kSmiTagMask));
1743 Assert(not_equal, "Operand is not a string");
1744 push(object);
1745 movq(object, FieldOperand(object, HeapObject::kMapOffset));
1746 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
1747 pop(object);
1748 Assert(below, "Operand is not a string");
1749}
1750
1751
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001752void MacroAssembler::AbortIfNotRootValue(Register src,
1753 Heap::RootListIndex root_value_index,
1754 const char* message) {
1755 ASSERT(!src.is(kScratchRegister));
1756 LoadRoot(kScratchRegister, root_value_index);
1757 cmpq(src, kScratchRegister);
1758 Check(equal, message);
1759}
1760
1761
1762
Leon Clarked91b9f72010-01-27 17:25:45 +00001763Condition MacroAssembler::IsObjectStringType(Register heap_object,
1764 Register map,
1765 Register instance_type) {
1766 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
Leon Clarke4515c472010-02-03 11:58:03 +00001767 movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
Leon Clarked91b9f72010-01-27 17:25:45 +00001768 ASSERT(kNotStringTag != 0);
1769 testb(instance_type, Immediate(kIsNotStringMask));
1770 return zero;
1771}
1772
1773
Steve Blocka7e24c12009-10-30 11:49:00 +00001774void MacroAssembler::TryGetFunctionPrototype(Register function,
1775 Register result,
1776 Label* miss) {
1777 // Check that the receiver isn't a smi.
1778 testl(function, Immediate(kSmiTagMask));
1779 j(zero, miss);
1780
1781 // Check that the function really is a function.
1782 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1783 j(not_equal, miss);
1784
1785 // Make sure that the function has an instance prototype.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001786 NearLabel non_instance;
Steve Blocka7e24c12009-10-30 11:49:00 +00001787 testb(FieldOperand(result, Map::kBitFieldOffset),
1788 Immediate(1 << Map::kHasNonInstancePrototype));
1789 j(not_zero, &non_instance);
1790
1791 // Get the prototype or initial map from the function.
1792 movq(result,
1793 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1794
1795 // If the prototype or initial map is the hole, don't return it and
1796 // simply miss the cache instead. This will allow us to allocate a
1797 // prototype object on-demand in the runtime system.
1798 CompareRoot(result, Heap::kTheHoleValueRootIndex);
1799 j(equal, miss);
1800
1801 // If the function does not have an initial map, we're done.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001802 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +00001803 CmpObjectType(result, MAP_TYPE, kScratchRegister);
1804 j(not_equal, &done);
1805
1806 // Get the prototype from the initial map.
1807 movq(result, FieldOperand(result, Map::kPrototypeOffset));
1808 jmp(&done);
1809
1810 // Non-instance prototype: Fetch prototype from constructor field
1811 // in initial map.
1812 bind(&non_instance);
1813 movq(result, FieldOperand(result, Map::kConstructorOffset));
1814
1815 // All done.
1816 bind(&done);
1817}
1818
1819
1820void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1821 if (FLAG_native_code_counters && counter->Enabled()) {
1822 movq(kScratchRegister, ExternalReference(counter));
1823 movl(Operand(kScratchRegister, 0), Immediate(value));
1824 }
1825}
1826
1827
1828void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1829 ASSERT(value > 0);
1830 if (FLAG_native_code_counters && counter->Enabled()) {
1831 movq(kScratchRegister, ExternalReference(counter));
1832 Operand operand(kScratchRegister, 0);
1833 if (value == 1) {
1834 incl(operand);
1835 } else {
1836 addl(operand, Immediate(value));
1837 }
1838 }
1839}
1840
1841
1842void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1843 ASSERT(value > 0);
1844 if (FLAG_native_code_counters && counter->Enabled()) {
1845 movq(kScratchRegister, ExternalReference(counter));
1846 Operand operand(kScratchRegister, 0);
1847 if (value == 1) {
1848 decl(operand);
1849 } else {
1850 subl(operand, Immediate(value));
1851 }
1852 }
1853}
1854
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001855
Steve Blocka7e24c12009-10-30 11:49:00 +00001856#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +00001857void MacroAssembler::DebugBreak() {
1858 ASSERT(allow_stub_calls());
Steve Block9fac8402011-05-12 15:51:54 +01001859 Set(rax, 0); // No arguments.
Andrei Popescu402d9372010-02-26 13:31:12 +00001860 movq(rbx, ExternalReference(Runtime::kDebugBreak));
1861 CEntryStub ces(1);
1862 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
Steve Blocka7e24c12009-10-30 11:49:00 +00001863}
Andrei Popescu402d9372010-02-26 13:31:12 +00001864#endif // ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +00001865
1866
Steve Blocka7e24c12009-10-30 11:49:00 +00001867void MacroAssembler::InvokeCode(Register code,
1868 const ParameterCount& expected,
1869 const ParameterCount& actual,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001870 InvokeFlag flag,
1871 PostCallGenerator* post_call_generator) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001872 NearLabel done;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001873 InvokePrologue(expected,
1874 actual,
1875 Handle<Code>::null(),
1876 code,
1877 &done,
1878 flag,
1879 post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001880 if (flag == CALL_FUNCTION) {
1881 call(code);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001882 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001883 } else {
1884 ASSERT(flag == JUMP_FUNCTION);
1885 jmp(code);
1886 }
1887 bind(&done);
1888}
1889
1890
1891void MacroAssembler::InvokeCode(Handle<Code> code,
1892 const ParameterCount& expected,
1893 const ParameterCount& actual,
1894 RelocInfo::Mode rmode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001895 InvokeFlag flag,
1896 PostCallGenerator* post_call_generator) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001897 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +00001898 Register dummy = rax;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001899 InvokePrologue(expected,
1900 actual,
1901 code,
1902 dummy,
1903 &done,
1904 flag,
1905 post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001906 if (flag == CALL_FUNCTION) {
1907 Call(code, rmode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001908 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001909 } else {
1910 ASSERT(flag == JUMP_FUNCTION);
1911 Jump(code, rmode);
1912 }
1913 bind(&done);
1914}
1915
1916
1917void MacroAssembler::InvokeFunction(Register function,
1918 const ParameterCount& actual,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001919 InvokeFlag flag,
1920 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001921 ASSERT(function.is(rdi));
1922 movq(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1923 movq(rsi, FieldOperand(function, JSFunction::kContextOffset));
1924 movsxlq(rbx,
1925 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001926 // Advances rdx to the end of the Code object header, to the start of
1927 // the executable code.
Steve Block791712a2010-08-27 10:21:07 +01001928 movq(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001929
1930 ParameterCount expected(rbx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001931 InvokeCode(rdx, expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001932}
1933
1934
Andrei Popescu402d9372010-02-26 13:31:12 +00001935void MacroAssembler::InvokeFunction(JSFunction* function,
1936 const ParameterCount& actual,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001937 InvokeFlag flag,
1938 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001939 ASSERT(function->is_compiled());
1940 // Get the function and setup the context.
1941 Move(rdi, Handle<JSFunction>(function));
1942 movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
1943
Steve Block1e0659c2011-05-24 12:43:12 +01001944 if (V8::UseCrankshaft()) {
1945 // Since Crankshaft can recompile a function, we need to load
1946 // the Code object every time we call the function.
1947 movq(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
1948 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001949 InvokeCode(rdx, expected, actual, flag, post_call_generator);
Steve Block1e0659c2011-05-24 12:43:12 +01001950 } else {
1951 // Invoke the cached code.
1952 Handle<Code> code(function->code());
1953 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001954 InvokeCode(code,
1955 expected,
1956 actual,
1957 RelocInfo::CODE_TARGET,
1958 flag,
1959 post_call_generator);
Steve Block1e0659c2011-05-24 12:43:12 +01001960 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001961}
1962
1963
Steve Blocka7e24c12009-10-30 11:49:00 +00001964void MacroAssembler::EnterFrame(StackFrame::Type type) {
1965 push(rbp);
1966 movq(rbp, rsp);
1967 push(rsi); // Context.
Steve Block3ce2e202009-11-05 08:53:23 +00001968 Push(Smi::FromInt(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00001969 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
1970 push(kScratchRegister);
1971 if (FLAG_debug_code) {
1972 movq(kScratchRegister,
1973 Factory::undefined_value(),
1974 RelocInfo::EMBEDDED_OBJECT);
1975 cmpq(Operand(rsp, 0), kScratchRegister);
1976 Check(not_equal, "code object not properly patched");
1977 }
1978}
1979
1980
1981void MacroAssembler::LeaveFrame(StackFrame::Type type) {
1982 if (FLAG_debug_code) {
Steve Block3ce2e202009-11-05 08:53:23 +00001983 Move(kScratchRegister, Smi::FromInt(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00001984 cmpq(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
1985 Check(equal, "stack frame types must match");
1986 }
1987 movq(rsp, rbp);
1988 pop(rbp);
1989}
1990
1991
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001992void MacroAssembler::EnterExitFramePrologue(bool save_rax) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001993 // Setup the frame structure on the stack.
1994 // All constants are relative to the frame pointer of the exit frame.
1995 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
1996 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
1997 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
1998 push(rbp);
1999 movq(rbp, rsp);
2000
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002001 // Reserve room for entry stack pointer and push the code object.
Steve Block3ce2e202009-11-05 08:53:23 +00002002 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +00002003 push(Immediate(0)); // Saved entry sp, patched before call.
2004 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
2005 push(kScratchRegister); // Accessed from EditFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +00002006
2007 // Save the frame pointer and the context in top.
2008 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
2009 ExternalReference context_address(Top::k_context_address);
Ben Murdochbb769b22010-08-11 14:56:33 +01002010 if (save_rax) {
2011 movq(r14, rax); // Backup rax before we use it.
2012 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002013
2014 movq(rax, rbp);
2015 store_rax(c_entry_fp_address);
2016 movq(rax, rsi);
2017 store_rax(context_address);
Ben Murdochbb769b22010-08-11 14:56:33 +01002018}
Steve Blocka7e24c12009-10-30 11:49:00 +00002019
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002020
Steve Block1e0659c2011-05-24 12:43:12 +01002021void MacroAssembler::EnterExitFrameEpilogue(int arg_stack_space,
2022 bool save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002023#ifdef _WIN64
Steve Block1e0659c2011-05-24 12:43:12 +01002024 const int kShadowSpace = 4;
2025 arg_stack_space += kShadowSpace;
Steve Blocka7e24c12009-10-30 11:49:00 +00002026#endif
Steve Block1e0659c2011-05-24 12:43:12 +01002027 // Optionally save all XMM registers.
2028 if (save_doubles) {
2029 CpuFeatures::Scope scope(SSE2);
2030 int space = XMMRegister::kNumRegisters * kDoubleSize +
2031 arg_stack_space * kPointerSize;
2032 subq(rsp, Immediate(space));
2033 int offset = -2 * kPointerSize;
2034 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; i++) {
2035 XMMRegister reg = XMMRegister::FromAllocationIndex(i);
2036 movsd(Operand(rbp, offset - ((i + 1) * kDoubleSize)), reg);
2037 }
2038 } else if (arg_stack_space > 0) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002039 subq(rsp, Immediate(arg_stack_space * kPointerSize));
2040 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002041
2042 // Get the required frame alignment for the OS.
2043 static const int kFrameAlignment = OS::ActivationFrameAlignment();
2044 if (kFrameAlignment > 0) {
2045 ASSERT(IsPowerOf2(kFrameAlignment));
2046 movq(kScratchRegister, Immediate(-kFrameAlignment));
2047 and_(rsp, kScratchRegister);
2048 }
2049
2050 // Patch the saved entry sp.
2051 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
2052}
2053
2054
Steve Block1e0659c2011-05-24 12:43:12 +01002055void MacroAssembler::EnterExitFrame(int arg_stack_space, bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002056 EnterExitFramePrologue(true);
Ben Murdochbb769b22010-08-11 14:56:33 +01002057
2058 // Setup argv in callee-saved register r12. It is reused in LeaveExitFrame,
2059 // so it must be retained across the C-call.
2060 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
2061 lea(r12, Operand(rbp, r14, times_pointer_size, offset));
2062
Steve Block1e0659c2011-05-24 12:43:12 +01002063 EnterExitFrameEpilogue(arg_stack_space, save_doubles);
Ben Murdochbb769b22010-08-11 14:56:33 +01002064}
2065
2066
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002067void MacroAssembler::EnterApiExitFrame(int arg_stack_space) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002068 EnterExitFramePrologue(false);
Steve Block1e0659c2011-05-24 12:43:12 +01002069 EnterExitFrameEpilogue(arg_stack_space, false);
Ben Murdochbb769b22010-08-11 14:56:33 +01002070}
2071
2072
Steve Block1e0659c2011-05-24 12:43:12 +01002073void MacroAssembler::LeaveExitFrame(bool save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002074 // Registers:
Steve Block8defd9f2010-07-08 12:39:36 +01002075 // r12 : argv
Steve Block1e0659c2011-05-24 12:43:12 +01002076 if (save_doubles) {
2077 int offset = -2 * kPointerSize;
2078 for (int i = 0; i < XMMRegister::kNumAllocatableRegisters; i++) {
2079 XMMRegister reg = XMMRegister::FromAllocationIndex(i);
2080 movsd(reg, Operand(rbp, offset - ((i + 1) * kDoubleSize)));
2081 }
2082 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002083 // Get the return address from the stack and restore the frame pointer.
2084 movq(rcx, Operand(rbp, 1 * kPointerSize));
2085 movq(rbp, Operand(rbp, 0 * kPointerSize));
2086
Steve Block1e0659c2011-05-24 12:43:12 +01002087 // Drop everything up to and including the arguments and the receiver
Steve Blocka7e24c12009-10-30 11:49:00 +00002088 // from the caller stack.
Steve Block8defd9f2010-07-08 12:39:36 +01002089 lea(rsp, Operand(r12, 1 * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00002090
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002091 // Push the return address to get ready to return.
2092 push(rcx);
2093
2094 LeaveExitFrameEpilogue();
2095}
2096
2097
2098void MacroAssembler::LeaveApiExitFrame() {
2099 movq(rsp, rbp);
2100 pop(rbp);
2101
2102 LeaveExitFrameEpilogue();
2103}
2104
2105
2106void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +00002107 // Restore current context from top and clear it in debug mode.
2108 ExternalReference context_address(Top::k_context_address);
2109 movq(kScratchRegister, context_address);
2110 movq(rsi, Operand(kScratchRegister, 0));
2111#ifdef DEBUG
2112 movq(Operand(kScratchRegister, 0), Immediate(0));
2113#endif
2114
Steve Blocka7e24c12009-10-30 11:49:00 +00002115 // Clear the top frame.
2116 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
2117 movq(kScratchRegister, c_entry_fp_address);
2118 movq(Operand(kScratchRegister, 0), Immediate(0));
2119}
2120
2121
Steve Blocka7e24c12009-10-30 11:49:00 +00002122void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
2123 Register scratch,
2124 Label* miss) {
2125 Label same_contexts;
2126
2127 ASSERT(!holder_reg.is(scratch));
2128 ASSERT(!scratch.is(kScratchRegister));
2129 // Load current lexical context from the stack frame.
2130 movq(scratch, Operand(rbp, StandardFrameConstants::kContextOffset));
2131
2132 // When generating debug code, make sure the lexical context is set.
2133 if (FLAG_debug_code) {
2134 cmpq(scratch, Immediate(0));
2135 Check(not_equal, "we should not have an empty lexical context");
2136 }
2137 // Load the global context of the current context.
2138 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
2139 movq(scratch, FieldOperand(scratch, offset));
2140 movq(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
2141
2142 // Check the context is a global context.
2143 if (FLAG_debug_code) {
2144 Cmp(FieldOperand(scratch, HeapObject::kMapOffset),
2145 Factory::global_context_map());
2146 Check(equal, "JSGlobalObject::global_context should be a global context.");
2147 }
2148
2149 // Check if both contexts are the same.
2150 cmpq(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
2151 j(equal, &same_contexts);
2152
2153 // Compare security tokens.
2154 // Check that the security token in the calling global object is
2155 // compatible with the security token in the receiving global
2156 // object.
2157
2158 // Check the context is a global context.
2159 if (FLAG_debug_code) {
2160 // Preserve original value of holder_reg.
2161 push(holder_reg);
2162 movq(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
2163 CompareRoot(holder_reg, Heap::kNullValueRootIndex);
2164 Check(not_equal, "JSGlobalProxy::context() should not be null.");
2165
2166 // Read the first word and compare to global_context_map(),
2167 movq(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
2168 CompareRoot(holder_reg, Heap::kGlobalContextMapRootIndex);
2169 Check(equal, "JSGlobalObject::global_context should be a global context.");
2170 pop(holder_reg);
2171 }
2172
2173 movq(kScratchRegister,
2174 FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
Steve Block3ce2e202009-11-05 08:53:23 +00002175 int token_offset =
2176 Context::kHeaderSize + Context::SECURITY_TOKEN_INDEX * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002177 movq(scratch, FieldOperand(scratch, token_offset));
2178 cmpq(scratch, FieldOperand(kScratchRegister, token_offset));
2179 j(not_equal, miss);
2180
2181 bind(&same_contexts);
2182}
2183
2184
2185void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +00002186 Register scratch,
2187 AllocationFlags flags) {
2188 ExternalReference new_space_allocation_top =
2189 ExternalReference::new_space_allocation_top_address();
2190
2191 // Just return if allocation top is already known.
2192 if ((flags & RESULT_CONTAINS_TOP) != 0) {
2193 // No use of scratch if allocation top is provided.
Steve Block6ded16b2010-05-10 14:33:55 +01002194 ASSERT(!scratch.is_valid());
Steve Blocka7e24c12009-10-30 11:49:00 +00002195#ifdef DEBUG
2196 // Assert that result actually contains top on entry.
2197 movq(kScratchRegister, new_space_allocation_top);
2198 cmpq(result, Operand(kScratchRegister, 0));
2199 Check(equal, "Unexpected allocation top");
2200#endif
2201 return;
2202 }
2203
Steve Block6ded16b2010-05-10 14:33:55 +01002204 // Move address of new object to result. Use scratch register if available,
2205 // and keep address in scratch until call to UpdateAllocationTopHelper.
2206 if (scratch.is_valid()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002207 movq(scratch, new_space_allocation_top);
2208 movq(result, Operand(scratch, 0));
Steve Block6ded16b2010-05-10 14:33:55 +01002209 } else if (result.is(rax)) {
2210 load_rax(new_space_allocation_top);
2211 } else {
2212 movq(kScratchRegister, new_space_allocation_top);
2213 movq(result, Operand(kScratchRegister, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00002214 }
2215}
2216
2217
2218void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
2219 Register scratch) {
Steve Blockd0582a62009-12-15 09:54:21 +00002220 if (FLAG_debug_code) {
2221 testq(result_end, Immediate(kObjectAlignmentMask));
2222 Check(zero, "Unaligned allocation in new space");
2223 }
2224
Steve Blocka7e24c12009-10-30 11:49:00 +00002225 ExternalReference new_space_allocation_top =
2226 ExternalReference::new_space_allocation_top_address();
2227
2228 // Update new top.
2229 if (result_end.is(rax)) {
2230 // rax can be stored directly to a memory location.
2231 store_rax(new_space_allocation_top);
2232 } else {
2233 // Register required - use scratch provided if available.
Steve Block6ded16b2010-05-10 14:33:55 +01002234 if (scratch.is_valid()) {
2235 movq(Operand(scratch, 0), result_end);
2236 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00002237 movq(kScratchRegister, new_space_allocation_top);
2238 movq(Operand(kScratchRegister, 0), result_end);
Steve Blocka7e24c12009-10-30 11:49:00 +00002239 }
2240 }
2241}
2242
2243
2244void MacroAssembler::AllocateInNewSpace(int object_size,
2245 Register result,
2246 Register result_end,
2247 Register scratch,
2248 Label* gc_required,
2249 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07002250 if (!FLAG_inline_new) {
2251 if (FLAG_debug_code) {
2252 // Trash the registers to simulate an allocation failure.
2253 movl(result, Immediate(0x7091));
2254 if (result_end.is_valid()) {
2255 movl(result_end, Immediate(0x7191));
2256 }
2257 if (scratch.is_valid()) {
2258 movl(scratch, Immediate(0x7291));
2259 }
2260 }
2261 jmp(gc_required);
2262 return;
2263 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002264 ASSERT(!result.is(result_end));
2265
2266 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002267 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00002268
2269 // Calculate new top and bail out if new space is exhausted.
2270 ExternalReference new_space_allocation_limit =
2271 ExternalReference::new_space_allocation_limit_address();
Steve Block6ded16b2010-05-10 14:33:55 +01002272
2273 Register top_reg = result_end.is_valid() ? result_end : result;
2274
Steve Block1e0659c2011-05-24 12:43:12 +01002275 if (!top_reg.is(result)) {
2276 movq(top_reg, result);
Steve Block6ded16b2010-05-10 14:33:55 +01002277 }
Steve Block1e0659c2011-05-24 12:43:12 +01002278 addq(top_reg, Immediate(object_size));
2279 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00002280 movq(kScratchRegister, new_space_allocation_limit);
Steve Block6ded16b2010-05-10 14:33:55 +01002281 cmpq(top_reg, Operand(kScratchRegister, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00002282 j(above, gc_required);
2283
2284 // Update allocation top.
Steve Block6ded16b2010-05-10 14:33:55 +01002285 UpdateAllocationTopHelper(top_reg, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +00002286
Steve Block6ded16b2010-05-10 14:33:55 +01002287 if (top_reg.is(result)) {
2288 if ((flags & TAG_OBJECT) != 0) {
2289 subq(result, Immediate(object_size - kHeapObjectTag));
2290 } else {
2291 subq(result, Immediate(object_size));
2292 }
2293 } else if ((flags & TAG_OBJECT) != 0) {
2294 // Tag the result if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00002295 addq(result, Immediate(kHeapObjectTag));
2296 }
2297}
2298
2299
2300void MacroAssembler::AllocateInNewSpace(int header_size,
2301 ScaleFactor element_size,
2302 Register element_count,
2303 Register result,
2304 Register result_end,
2305 Register scratch,
2306 Label* gc_required,
2307 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07002308 if (!FLAG_inline_new) {
2309 if (FLAG_debug_code) {
2310 // Trash the registers to simulate an allocation failure.
2311 movl(result, Immediate(0x7091));
2312 movl(result_end, Immediate(0x7191));
2313 if (scratch.is_valid()) {
2314 movl(scratch, Immediate(0x7291));
2315 }
2316 // Register element_count is not modified by the function.
2317 }
2318 jmp(gc_required);
2319 return;
2320 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002321 ASSERT(!result.is(result_end));
2322
2323 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002324 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00002325
2326 // Calculate new top and bail out if new space is exhausted.
2327 ExternalReference new_space_allocation_limit =
2328 ExternalReference::new_space_allocation_limit_address();
Steve Block1e0659c2011-05-24 12:43:12 +01002329
2330 // We assume that element_count*element_size + header_size does not
2331 // overflow.
2332 lea(result_end, Operand(element_count, element_size, header_size));
2333 addq(result_end, result);
2334 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00002335 movq(kScratchRegister, new_space_allocation_limit);
2336 cmpq(result_end, Operand(kScratchRegister, 0));
2337 j(above, gc_required);
2338
2339 // Update allocation top.
2340 UpdateAllocationTopHelper(result_end, scratch);
2341
2342 // Tag the result if requested.
2343 if ((flags & TAG_OBJECT) != 0) {
2344 addq(result, Immediate(kHeapObjectTag));
2345 }
2346}
2347
2348
2349void MacroAssembler::AllocateInNewSpace(Register object_size,
2350 Register result,
2351 Register result_end,
2352 Register scratch,
2353 Label* gc_required,
2354 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07002355 if (!FLAG_inline_new) {
2356 if (FLAG_debug_code) {
2357 // Trash the registers to simulate an allocation failure.
2358 movl(result, Immediate(0x7091));
2359 movl(result_end, Immediate(0x7191));
2360 if (scratch.is_valid()) {
2361 movl(scratch, Immediate(0x7291));
2362 }
2363 // object_size is left unchanged by this function.
2364 }
2365 jmp(gc_required);
2366 return;
2367 }
2368 ASSERT(!result.is(result_end));
2369
Steve Blocka7e24c12009-10-30 11:49:00 +00002370 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002371 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00002372
2373 // Calculate new top and bail out if new space is exhausted.
2374 ExternalReference new_space_allocation_limit =
2375 ExternalReference::new_space_allocation_limit_address();
2376 if (!object_size.is(result_end)) {
2377 movq(result_end, object_size);
2378 }
2379 addq(result_end, result);
Steve Block1e0659c2011-05-24 12:43:12 +01002380 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00002381 movq(kScratchRegister, new_space_allocation_limit);
2382 cmpq(result_end, Operand(kScratchRegister, 0));
2383 j(above, gc_required);
2384
2385 // Update allocation top.
2386 UpdateAllocationTopHelper(result_end, scratch);
2387
2388 // Tag the result if requested.
2389 if ((flags & TAG_OBJECT) != 0) {
2390 addq(result, Immediate(kHeapObjectTag));
2391 }
2392}
2393
2394
2395void MacroAssembler::UndoAllocationInNewSpace(Register object) {
2396 ExternalReference new_space_allocation_top =
2397 ExternalReference::new_space_allocation_top_address();
2398
2399 // Make sure the object has no tag before resetting top.
2400 and_(object, Immediate(~kHeapObjectTagMask));
2401 movq(kScratchRegister, new_space_allocation_top);
2402#ifdef DEBUG
2403 cmpq(object, Operand(kScratchRegister, 0));
2404 Check(below, "Undo allocation of non allocated memory");
2405#endif
2406 movq(Operand(kScratchRegister, 0), object);
2407}
2408
2409
Steve Block3ce2e202009-11-05 08:53:23 +00002410void MacroAssembler::AllocateHeapNumber(Register result,
2411 Register scratch,
2412 Label* gc_required) {
2413 // Allocate heap number in new space.
2414 AllocateInNewSpace(HeapNumber::kSize,
2415 result,
2416 scratch,
2417 no_reg,
2418 gc_required,
2419 TAG_OBJECT);
2420
2421 // Set the map.
2422 LoadRoot(kScratchRegister, Heap::kHeapNumberMapRootIndex);
2423 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2424}
2425
2426
Leon Clarkee46be812010-01-19 14:06:41 +00002427void MacroAssembler::AllocateTwoByteString(Register result,
2428 Register length,
2429 Register scratch1,
2430 Register scratch2,
2431 Register scratch3,
2432 Label* gc_required) {
2433 // Calculate the number of bytes needed for the characters in the string while
2434 // observing object alignment.
Steve Block6ded16b2010-05-10 14:33:55 +01002435 const int kHeaderAlignment = SeqTwoByteString::kHeaderSize &
2436 kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +00002437 ASSERT(kShortSize == 2);
2438 // scratch1 = length * 2 + kObjectAlignmentMask.
Steve Block6ded16b2010-05-10 14:33:55 +01002439 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask +
2440 kHeaderAlignment));
Leon Clarkee46be812010-01-19 14:06:41 +00002441 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Block6ded16b2010-05-10 14:33:55 +01002442 if (kHeaderAlignment > 0) {
2443 subq(scratch1, Immediate(kHeaderAlignment));
2444 }
Leon Clarkee46be812010-01-19 14:06:41 +00002445
2446 // Allocate two byte string in new space.
2447 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
2448 times_1,
2449 scratch1,
2450 result,
2451 scratch2,
2452 scratch3,
2453 gc_required,
2454 TAG_OBJECT);
2455
2456 // Set the map, length and hash field.
2457 LoadRoot(kScratchRegister, Heap::kStringMapRootIndex);
2458 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
Steve Block6ded16b2010-05-10 14:33:55 +01002459 Integer32ToSmi(scratch1, length);
2460 movq(FieldOperand(result, String::kLengthOffset), scratch1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002461 movq(FieldOperand(result, String::kHashFieldOffset),
Leon Clarkee46be812010-01-19 14:06:41 +00002462 Immediate(String::kEmptyHashField));
2463}
2464
2465
2466void MacroAssembler::AllocateAsciiString(Register result,
2467 Register length,
2468 Register scratch1,
2469 Register scratch2,
2470 Register scratch3,
2471 Label* gc_required) {
2472 // Calculate the number of bytes needed for the characters in the string while
2473 // observing object alignment.
Steve Block6ded16b2010-05-10 14:33:55 +01002474 const int kHeaderAlignment = SeqAsciiString::kHeaderSize &
2475 kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +00002476 movl(scratch1, length);
2477 ASSERT(kCharSize == 1);
Steve Block6ded16b2010-05-10 14:33:55 +01002478 addq(scratch1, Immediate(kObjectAlignmentMask + kHeaderAlignment));
Leon Clarkee46be812010-01-19 14:06:41 +00002479 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Block6ded16b2010-05-10 14:33:55 +01002480 if (kHeaderAlignment > 0) {
2481 subq(scratch1, Immediate(kHeaderAlignment));
2482 }
Leon Clarkee46be812010-01-19 14:06:41 +00002483
2484 // Allocate ascii string in new space.
2485 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
2486 times_1,
2487 scratch1,
2488 result,
2489 scratch2,
2490 scratch3,
2491 gc_required,
2492 TAG_OBJECT);
2493
2494 // Set the map, length and hash field.
2495 LoadRoot(kScratchRegister, Heap::kAsciiStringMapRootIndex);
2496 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
Steve Block6ded16b2010-05-10 14:33:55 +01002497 Integer32ToSmi(scratch1, length);
2498 movq(FieldOperand(result, String::kLengthOffset), scratch1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002499 movq(FieldOperand(result, String::kHashFieldOffset),
Leon Clarkee46be812010-01-19 14:06:41 +00002500 Immediate(String::kEmptyHashField));
2501}
2502
2503
2504void MacroAssembler::AllocateConsString(Register result,
2505 Register scratch1,
2506 Register scratch2,
2507 Label* gc_required) {
2508 // Allocate heap number in new space.
2509 AllocateInNewSpace(ConsString::kSize,
2510 result,
2511 scratch1,
2512 scratch2,
2513 gc_required,
2514 TAG_OBJECT);
2515
2516 // Set the map. The other fields are left uninitialized.
2517 LoadRoot(kScratchRegister, Heap::kConsStringMapRootIndex);
2518 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2519}
2520
2521
2522void MacroAssembler::AllocateAsciiConsString(Register result,
2523 Register scratch1,
2524 Register scratch2,
2525 Label* gc_required) {
2526 // Allocate heap number in new space.
2527 AllocateInNewSpace(ConsString::kSize,
2528 result,
2529 scratch1,
2530 scratch2,
2531 gc_required,
2532 TAG_OBJECT);
2533
2534 // Set the map. The other fields are left uninitialized.
2535 LoadRoot(kScratchRegister, Heap::kConsAsciiStringMapRootIndex);
2536 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2537}
2538
2539
Steve Blockd0582a62009-12-15 09:54:21 +00002540void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2541 if (context_chain_length > 0) {
2542 // Move up the chain of contexts to the context containing the slot.
2543 movq(dst, Operand(rsi, Context::SlotOffset(Context::CLOSURE_INDEX)));
2544 // Load the function context (which is the incoming, outer context).
Leon Clarkee46be812010-01-19 14:06:41 +00002545 movq(dst, FieldOperand(dst, JSFunction::kContextOffset));
Steve Blockd0582a62009-12-15 09:54:21 +00002546 for (int i = 1; i < context_chain_length; i++) {
2547 movq(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
2548 movq(dst, FieldOperand(dst, JSFunction::kContextOffset));
2549 }
2550 // The context may be an intermediate context, not a function context.
2551 movq(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002552 } else {
2553 // Slot is in the current function context. Move it into the
2554 // destination register in case we store into it (the write barrier
2555 // cannot be allowed to destroy the context in rsi).
2556 movq(dst, rsi);
2557 }
2558
2559 // We should not have found a 'with' context by walking the context chain
2560 // (i.e., the static scope chain and runtime context chain do not agree).
2561 // A variable occurring in such a scope should have slot type LOOKUP and
2562 // not CONTEXT.
2563 if (FLAG_debug_code) {
2564 cmpq(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2565 Check(equal, "Yo dawg, I heard you liked function contexts "
2566 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00002567 }
2568}
2569
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002570
Ben Murdochb0fe1622011-05-05 13:52:32 +01002571void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2572 // Load the global or builtins object from the current context.
2573 movq(function, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2574 // Load the global context from the global or builtins object.
2575 movq(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
2576 // Load the function from the global context.
2577 movq(function, Operand(function, Context::SlotOffset(index)));
2578}
2579
2580
2581void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2582 Register map) {
2583 // Load the initial map. The global functions all have initial maps.
2584 movq(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2585 if (FLAG_debug_code) {
2586 Label ok, fail;
2587 CheckMap(map, Factory::meta_map(), &fail, false);
2588 jmp(&ok);
2589 bind(&fail);
2590 Abort("Global functions must have initial map");
2591 bind(&ok);
2592 }
2593}
2594
2595
Leon Clarke4515c472010-02-03 11:58:03 +00002596int MacroAssembler::ArgumentStackSlotsForCFunctionCall(int num_arguments) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002597 // On Windows 64 stack slots are reserved by the caller for all arguments
2598 // including the ones passed in registers, and space is always allocated for
2599 // the four register arguments even if the function takes fewer than four
2600 // arguments.
2601 // On AMD64 ABI (Linux/Mac) the first six arguments are passed in registers
2602 // and the caller does not reserve stack slots for them.
Leon Clarke4515c472010-02-03 11:58:03 +00002603 ASSERT(num_arguments >= 0);
2604#ifdef _WIN64
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002605 static const int kMinimumStackSlots = 4;
2606 if (num_arguments < kMinimumStackSlots) return kMinimumStackSlots;
2607 return num_arguments;
Leon Clarke4515c472010-02-03 11:58:03 +00002608#else
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002609 static const int kRegisterPassedArguments = 6;
2610 if (num_arguments < kRegisterPassedArguments) return 0;
2611 return num_arguments - kRegisterPassedArguments;
Leon Clarke4515c472010-02-03 11:58:03 +00002612#endif
Leon Clarke4515c472010-02-03 11:58:03 +00002613}
2614
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002615
Leon Clarke4515c472010-02-03 11:58:03 +00002616void MacroAssembler::PrepareCallCFunction(int num_arguments) {
2617 int frame_alignment = OS::ActivationFrameAlignment();
2618 ASSERT(frame_alignment != 0);
2619 ASSERT(num_arguments >= 0);
2620 // Make stack end at alignment and allocate space for arguments and old rsp.
2621 movq(kScratchRegister, rsp);
2622 ASSERT(IsPowerOf2(frame_alignment));
2623 int argument_slots_on_stack =
2624 ArgumentStackSlotsForCFunctionCall(num_arguments);
2625 subq(rsp, Immediate((argument_slots_on_stack + 1) * kPointerSize));
2626 and_(rsp, Immediate(-frame_alignment));
2627 movq(Operand(rsp, argument_slots_on_stack * kPointerSize), kScratchRegister);
2628}
2629
2630
2631void MacroAssembler::CallCFunction(ExternalReference function,
2632 int num_arguments) {
2633 movq(rax, function);
2634 CallCFunction(rax, num_arguments);
2635}
2636
2637
2638void MacroAssembler::CallCFunction(Register function, int num_arguments) {
Steve Block6ded16b2010-05-10 14:33:55 +01002639 // Check stack alignment.
2640 if (FLAG_debug_code) {
2641 CheckStackAlignment();
2642 }
2643
Leon Clarke4515c472010-02-03 11:58:03 +00002644 call(function);
2645 ASSERT(OS::ActivationFrameAlignment() != 0);
2646 ASSERT(num_arguments >= 0);
2647 int argument_slots_on_stack =
2648 ArgumentStackSlotsForCFunctionCall(num_arguments);
2649 movq(rsp, Operand(rsp, argument_slots_on_stack * kPointerSize));
2650}
2651
Steve Blockd0582a62009-12-15 09:54:21 +00002652
Steve Blocka7e24c12009-10-30 11:49:00 +00002653CodePatcher::CodePatcher(byte* address, int size)
2654 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
2655 // Create a new macro assembler pointing to the address of the code to patch.
2656 // The size is adjusted with kGap on order for the assembler to generate size
2657 // bytes of instructions without failing with buffer size constraints.
2658 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2659}
2660
2661
2662CodePatcher::~CodePatcher() {
2663 // Indicate that code has changed.
2664 CPU::FlushICache(address_, size_);
2665
2666 // Check that the code was patched as expected.
2667 ASSERT(masm_.pc_ == address_ + size_);
2668 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2669}
2670
Steve Blocka7e24c12009-10-30 11:49:00 +00002671} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002672
2673#endif // V8_TARGET_ARCH_X64