blob: 24bac7d3693c6b5d22dd98a31f0f7e25d934cee5 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
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) {
Steve Block6ded16b2010-05-10 14:33:55 +010052 movq(destination, Operand(kRootRegister, index << kPointerSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +000053}
54
55
Kristian Monsen25f61362010-05-21 11:50:48 +010056void MacroAssembler::StoreRoot(Register source, Heap::RootListIndex index) {
57 movq(Operand(kRootRegister, index << kPointerSizeLog2), source);
58}
59
60
Steve Blocka7e24c12009-10-30 11:49:00 +000061void MacroAssembler::PushRoot(Heap::RootListIndex index) {
Steve Block6ded16b2010-05-10 14:33:55 +010062 push(Operand(kRootRegister, index << kPointerSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +000063}
64
65
Steve Block3ce2e202009-11-05 08:53:23 +000066void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
Steve Block6ded16b2010-05-10 14:33:55 +010067 cmpq(with, Operand(kRootRegister, index << kPointerSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +000068}
69
70
Steve Block3ce2e202009-11-05 08:53:23 +000071void MacroAssembler::CompareRoot(Operand with, Heap::RootListIndex index) {
Steve Blocka7e24c12009-10-30 11:49:00 +000072 LoadRoot(kScratchRegister, index);
73 cmpq(with, kScratchRegister);
74}
75
76
Steve Blockd0582a62009-12-15 09:54:21 +000077void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
78 CompareRoot(rsp, Heap::kStackLimitRootIndex);
79 j(below, on_stack_overflow);
80}
81
82
Steve Block6ded16b2010-05-10 14:33:55 +010083void MacroAssembler::RecordWriteHelper(Register object,
84 Register addr,
85 Register scratch) {
86 if (FLAG_debug_code) {
87 // Check that the object is not in new space.
88 Label not_in_new_space;
89 InNewSpace(object, scratch, not_equal, &not_in_new_space);
90 Abort("new-space object passed to RecordWriteHelper");
91 bind(&not_in_new_space);
92 }
93
Steve Blocka7e24c12009-10-30 11:49:00 +000094 // Compute the page start address from the heap object pointer, and reuse
95 // the 'object' register for it.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010096 and_(object, Immediate(~Page::kPageAlignmentMask));
Steve Blocka7e24c12009-10-30 11:49:00 +000097
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010098 // Compute number of region covering addr. See Page::GetRegionNumberForAddress
99 // method for more details.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100100 shrl(addr, Immediate(Page::kRegionSizeLog2));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100101 andl(addr, Immediate(Page::kPageAlignmentMask >> Page::kRegionSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100103 // Set dirty mark for region.
104 bts(Operand(object, Page::kDirtyFlagOffset), addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000105}
106
107
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100108// For page containing |object| mark region covering [object+offset] dirty.
Steve Blocka7e24c12009-10-30 11:49:00 +0000109// object is the object being stored into, value is the object being stored.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100110// If offset is zero, then the index register contains the array index into
111// the elements array represented a zero extended int32. Otherwise it can be
112// used as a scratch register.
Steve Blocka7e24c12009-10-30 11:49:00 +0000113// All registers are clobbered by the operation.
114void MacroAssembler::RecordWrite(Register object,
115 int offset,
116 Register value,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100117 Register index) {
Leon Clarke4515c472010-02-03 11:58:03 +0000118 // The compiled code assumes that record write doesn't change the
119 // context register, so we check that none of the clobbered
120 // registers are rsi.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100121 ASSERT(!object.is(rsi) && !value.is(rsi) && !index.is(rsi));
Leon Clarke4515c472010-02-03 11:58:03 +0000122
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100123 // First, check if a write barrier is even needed. The tests below
124 // catch stores of Smis and stores into young gen.
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 Label done;
Steve Block3ce2e202009-11-05 08:53:23 +0000126 JumpIfSmi(value, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000127
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100128 RecordWriteNonSmi(object, offset, value, index);
Steve Block3ce2e202009-11-05 08:53:23 +0000129 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000130
131 // Clobber all input registers when running with the debug-code flag
132 // turned on to provoke errors. This clobbering repeats the
133 // clobbering done inside RecordWriteNonSmi but it's necessary to
134 // avoid having the fast case for smis leave the registers
135 // unchanged.
136 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100137 movq(object, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
138 movq(value, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100139 movq(index, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Leon Clarke4515c472010-02-03 11:58:03 +0000140 }
Steve Block3ce2e202009-11-05 08:53:23 +0000141}
142
143
144void MacroAssembler::RecordWriteNonSmi(Register object,
145 int offset,
146 Register scratch,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100147 Register index) {
Steve Block3ce2e202009-11-05 08:53:23 +0000148 Label done;
Leon Clarke4515c472010-02-03 11:58:03 +0000149
150 if (FLAG_debug_code) {
151 Label okay;
152 JumpIfNotSmi(object, &okay);
153 Abort("MacroAssembler::RecordWriteNonSmi cannot deal with smis");
154 bind(&okay);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100155
156 if (offset == 0) {
157 // index must be int32.
158 Register tmp = index.is(rax) ? rbx : rax;
159 push(tmp);
160 movl(tmp, index);
161 cmpq(tmp, index);
162 Check(equal, "Index register for RecordWrite must be untagged int32.");
163 pop(tmp);
164 }
Leon Clarke4515c472010-02-03 11:58:03 +0000165 }
166
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100167 // Test that the object address is not in the new space. We cannot
168 // update page dirty marks for new space pages.
Steve Block6ded16b2010-05-10 14:33:55 +0100169 InNewSpace(object, scratch, equal, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000170
Steve Block6ded16b2010-05-10 14:33:55 +0100171 // The offset is relative to a tagged or untagged HeapObject pointer,
172 // so either offset or offset + kHeapObjectTag must be a
173 // multiple of kPointerSize.
174 ASSERT(IsAligned(offset, kPointerSize) ||
175 IsAligned(offset + kHeapObjectTag, kPointerSize));
176
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100177 Register dst = index;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100178 if (offset != 0) {
179 lea(dst, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100181 // array access: calculate the destination address in the same manner as
182 // KeyedStoreIC::GenerateGeneric.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100183 lea(dst, FieldOperand(object,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100184 index,
185 times_pointer_size,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100186 FixedArray::kHeaderSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100188 RecordWriteHelper(object, dst, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000189
190 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000191
192 // Clobber all input registers when running with the debug-code flag
193 // turned on to provoke errors.
194 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100195 movq(object, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
196 movq(scratch, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100197 movq(index, BitCast<int64_t>(kZapValue), RelocInfo::NONE);
Steve Block6ded16b2010-05-10 14:33:55 +0100198 }
199}
200
201
202void MacroAssembler::InNewSpace(Register object,
203 Register scratch,
204 Condition cc,
205 Label* branch) {
206 if (Serializer::enabled()) {
207 // Can't do arithmetic on external references if it might get serialized.
208 // The mask isn't really an address. We load it as an external reference in
209 // case the size of the new space is different between the snapshot maker
210 // and the running system.
211 if (scratch.is(object)) {
212 movq(kScratchRegister, ExternalReference::new_space_mask());
213 and_(scratch, kScratchRegister);
214 } else {
215 movq(scratch, ExternalReference::new_space_mask());
216 and_(scratch, object);
217 }
218 movq(kScratchRegister, ExternalReference::new_space_start());
219 cmpq(scratch, kScratchRegister);
220 j(cc, branch);
221 } else {
222 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
223 intptr_t new_space_start =
224 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
225 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
226 if (scratch.is(object)) {
227 addq(scratch, kScratchRegister);
228 } else {
229 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
230 }
231 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
232 j(cc, branch);
Leon Clarke4515c472010-02-03 11:58:03 +0000233 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000234}
235
236
237void MacroAssembler::Assert(Condition cc, const char* msg) {
238 if (FLAG_debug_code) Check(cc, msg);
239}
240
241
242void MacroAssembler::Check(Condition cc, const char* msg) {
243 Label L;
244 j(cc, &L);
245 Abort(msg);
246 // will not return here
247 bind(&L);
248}
249
250
Steve Block6ded16b2010-05-10 14:33:55 +0100251void MacroAssembler::CheckStackAlignment() {
252 int frame_alignment = OS::ActivationFrameAlignment();
253 int frame_alignment_mask = frame_alignment - 1;
254 if (frame_alignment > kPointerSize) {
255 ASSERT(IsPowerOf2(frame_alignment));
256 Label alignment_as_expected;
257 testq(rsp, Immediate(frame_alignment_mask));
258 j(zero, &alignment_as_expected);
259 // Abort if stack is not aligned.
260 int3();
261 bind(&alignment_as_expected);
262 }
263}
264
265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266void MacroAssembler::NegativeZeroTest(Register result,
267 Register op,
268 Label* then_label) {
269 Label ok;
270 testl(result, result);
271 j(not_zero, &ok);
272 testl(op, op);
273 j(sign, then_label);
274 bind(&ok);
275}
276
277
278void MacroAssembler::Abort(const char* msg) {
279 // We want to pass the msg string like a smi to avoid GC
280 // problems, however msg is not guaranteed to be aligned
281 // properly. Instead, we pass an aligned pointer that is
282 // a proper v8 smi, but also pass the alignment difference
283 // from the real pointer as a smi.
284 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
285 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
286 // Note: p0 might not be a valid Smi *value*, but it has a valid Smi tag.
287 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
288#ifdef DEBUG
289 if (msg != NULL) {
290 RecordComment("Abort message: ");
291 RecordComment(msg);
292 }
293#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000294 // Disable stub call restrictions to always allow calls to abort.
295 set_allow_stub_calls(true);
296
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 push(rax);
298 movq(kScratchRegister, p0, RelocInfo::NONE);
299 push(kScratchRegister);
300 movq(kScratchRegister,
Steve Blockd0582a62009-12-15 09:54:21 +0000301 reinterpret_cast<intptr_t>(Smi::FromInt(static_cast<int>(p1 - p0))),
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 RelocInfo::NONE);
303 push(kScratchRegister);
304 CallRuntime(Runtime::kAbort, 2);
305 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +0000306 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +0000307}
308
309
310void MacroAssembler::CallStub(CodeStub* stub) {
311 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
312 Call(stub->GetCode(), RelocInfo::CODE_TARGET);
313}
314
315
Leon Clarkee46be812010-01-19 14:06:41 +0000316void MacroAssembler::TailCallStub(CodeStub* stub) {
317 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
318 Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
319}
320
321
Steve Blocka7e24c12009-10-30 11:49:00 +0000322void MacroAssembler::StubReturn(int argc) {
323 ASSERT(argc >= 1 && generating_stub());
324 ret((argc - 1) * kPointerSize);
325}
326
327
328void MacroAssembler::IllegalOperation(int num_arguments) {
329 if (num_arguments > 0) {
330 addq(rsp, Immediate(num_arguments * kPointerSize));
331 }
332 LoadRoot(rax, Heap::kUndefinedValueRootIndex);
333}
334
335
336void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
337 CallRuntime(Runtime::FunctionForId(id), num_arguments);
338}
339
340
341void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
342 // If the expected number of arguments of the runtime function is
343 // constant, we check that the actual number of arguments match the
344 // expectation.
345 if (f->nargs >= 0 && f->nargs != num_arguments) {
346 IllegalOperation(num_arguments);
347 return;
348 }
349
Leon Clarke4515c472010-02-03 11:58:03 +0000350 // TODO(1236192): Most runtime routines don't need the number of
351 // arguments passed in because it is constant. At some point we
352 // should remove this need and make the runtime routine entry code
353 // smarter.
354 movq(rax, Immediate(num_arguments));
355 movq(rbx, ExternalReference(f));
356 CEntryStub ces(f->result_size);
357 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358}
359
360
Andrei Popescu402d9372010-02-26 13:31:12 +0000361void MacroAssembler::CallExternalReference(const ExternalReference& ext,
362 int num_arguments) {
363 movq(rax, Immediate(num_arguments));
364 movq(rbx, ext);
365
366 CEntryStub stub(1);
367 CallStub(&stub);
368}
369
370
Steve Block6ded16b2010-05-10 14:33:55 +0100371void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
372 int num_arguments,
373 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 // ----------- S t a t e -------------
375 // -- rsp[0] : return address
376 // -- rsp[8] : argument num_arguments - 1
377 // ...
378 // -- rsp[8 * num_arguments] : argument 0 (receiver)
379 // -----------------------------------
380
381 // TODO(1236192): Most runtime routines don't need the number of
382 // arguments passed in because it is constant. At some point we
383 // should remove this need and make the runtime routine entry code
384 // smarter.
385 movq(rax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +0100386 JumpToExternalReference(ext, result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000387}
388
389
Steve Block6ded16b2010-05-10 14:33:55 +0100390void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
391 int num_arguments,
392 int result_size) {
393 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
394}
395
396
397void MacroAssembler::JumpToExternalReference(const ExternalReference& ext,
398 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 // Set the entry point and jump to the C entry runtime stub.
400 movq(rbx, ext);
401 CEntryStub ces(result_size);
Steve Block3ce2e202009-11-05 08:53:23 +0000402 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
Steve Blocka7e24c12009-10-30 11:49:00 +0000403}
404
405
Andrei Popescu402d9372010-02-26 13:31:12 +0000406void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
407 // Calls are not allowed in some stubs.
408 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
Steve Blocka7e24c12009-10-30 11:49:00 +0000409
Andrei Popescu402d9372010-02-26 13:31:12 +0000410 // Rely on the assertion to check that the number of provided
411 // arguments match the expected number of arguments. Fake a
412 // parameter count to avoid emitting code to do the check.
413 ParameterCount expected(0);
414 GetBuiltinEntry(rdx, id);
415 InvokeCode(rdx, expected, expected, flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000416}
417
Andrei Popescu402d9372010-02-26 13:31:12 +0000418
419void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +0100420 ASSERT(!target.is(rdi));
421
422 // Load the builtins object into target register.
423 movq(target, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
424 movq(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
425
Andrei Popescu402d9372010-02-26 13:31:12 +0000426 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +0100427 movq(rdi, FieldOperand(target, JSBuiltinsObject::OffsetOfFunctionWithId(id)));
428
429 // Load the code entry point from the builtins object.
430 movq(target, FieldOperand(target, JSBuiltinsObject::OffsetOfCodeWithId(id)));
431 if (FLAG_debug_code) {
432 // Make sure the code objects in the builtins object and in the
433 // builtin function are the same.
434 push(target);
435 movq(target, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
436 movq(target, FieldOperand(target, SharedFunctionInfo::kCodeOffset));
437 cmpq(target, Operand(rsp, 0));
438 Assert(equal, "Builtin code object changed");
439 pop(target);
440 }
441 lea(target, FieldOperand(target, Code::kHeaderSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000442}
443
444
445void MacroAssembler::Set(Register dst, int64_t x) {
446 if (x == 0) {
447 xor_(dst, dst);
448 } else if (is_int32(x)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000449 movq(dst, Immediate(static_cast<int32_t>(x)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 } else if (is_uint32(x)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000451 movl(dst, Immediate(static_cast<uint32_t>(x)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 } else {
453 movq(dst, x, RelocInfo::NONE);
454 }
455}
456
457
458void MacroAssembler::Set(const Operand& dst, int64_t x) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100459 if (is_int32(x)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000460 movq(dst, Immediate(static_cast<int32_t>(x)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 } else {
462 movq(kScratchRegister, x, RelocInfo::NONE);
463 movq(dst, kScratchRegister);
464 }
465}
466
Steve Blocka7e24c12009-10-30 11:49:00 +0000467// ----------------------------------------------------------------------------
468// Smi tagging, untagging and tag detection.
469
Steve Block3ce2e202009-11-05 08:53:23 +0000470static int kSmiShift = kSmiTagSize + kSmiShiftSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000471
472void MacroAssembler::Integer32ToSmi(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000473 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000474 if (!dst.is(src)) {
475 movl(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000476 }
Steve Block3ce2e202009-11-05 08:53:23 +0000477 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000478}
479
480
481void MacroAssembler::Integer32ToSmi(Register dst,
482 Register src,
483 Label* on_overflow) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000485 // 32-bit integer always fits in a long smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000486 if (!dst.is(src)) {
487 movl(dst, src);
488 }
Steve Block3ce2e202009-11-05 08:53:23 +0000489 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000490}
491
492
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100493void MacroAssembler::Integer32ToSmiField(const Operand& dst, Register src) {
494 if (FLAG_debug_code) {
495 testb(dst, Immediate(0x01));
496 Label ok;
497 j(zero, &ok);
498 if (allow_stub_calls()) {
499 Abort("Integer32ToSmiField writing to non-smi location");
500 } else {
501 int3();
502 }
503 bind(&ok);
504 }
505 ASSERT(kSmiShift % kBitsPerByte == 0);
506 movl(Operand(dst, kSmiShift / kBitsPerByte), src);
507}
508
509
Steve Block3ce2e202009-11-05 08:53:23 +0000510void MacroAssembler::Integer64PlusConstantToSmi(Register dst,
511 Register src,
512 int constant) {
513 if (dst.is(src)) {
514 addq(dst, Immediate(constant));
515 } else {
516 lea(dst, Operand(src, constant));
517 }
518 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000519}
520
521
522void MacroAssembler::SmiToInteger32(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 ASSERT_EQ(0, kSmiTag);
524 if (!dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000525 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000526 }
Steve Block3ce2e202009-11-05 08:53:23 +0000527 shr(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000528}
529
530
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100531void MacroAssembler::SmiToInteger32(Register dst, const Operand& src) {
532 movl(dst, Operand(src, kSmiShift / kBitsPerByte));
533}
534
535
Steve Blocka7e24c12009-10-30 11:49:00 +0000536void MacroAssembler::SmiToInteger64(Register dst, Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000538 if (!dst.is(src)) {
539 movq(dst, src);
540 }
541 sar(dst, Immediate(kSmiShift));
542}
543
544
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100545void MacroAssembler::SmiToInteger64(Register dst, const Operand& src) {
546 movsxlq(dst, Operand(src, kSmiShift / kBitsPerByte));
547}
548
549
Steve Block3ce2e202009-11-05 08:53:23 +0000550void MacroAssembler::SmiTest(Register src) {
551 testq(src, src);
552}
553
554
555void MacroAssembler::SmiCompare(Register dst, Register src) {
556 cmpq(dst, src);
557}
558
559
560void MacroAssembler::SmiCompare(Register dst, Smi* src) {
561 ASSERT(!dst.is(kScratchRegister));
562 if (src->value() == 0) {
563 testq(dst, dst);
564 } else {
565 Move(kScratchRegister, src);
566 cmpq(dst, kScratchRegister);
567 }
568}
569
570
Leon Clarkef7060e22010-06-03 12:02:55 +0100571void MacroAssembler::SmiCompare(Register dst, const Operand& src) {
Steve Block6ded16b2010-05-10 14:33:55 +0100572 cmpq(dst, src);
573}
574
575
Steve Block3ce2e202009-11-05 08:53:23 +0000576void MacroAssembler::SmiCompare(const Operand& dst, Register src) {
577 cmpq(dst, src);
578}
579
580
581void MacroAssembler::SmiCompare(const Operand& dst, Smi* src) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100582 cmpl(Operand(dst, kSmiShift / kBitsPerByte), Immediate(src->value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000583}
584
585
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100586void MacroAssembler::SmiCompareInteger32(const Operand& dst, Register src) {
587 cmpl(Operand(dst, kSmiShift / kBitsPerByte), src);
588}
589
590
Steve Blocka7e24c12009-10-30 11:49:00 +0000591void MacroAssembler::PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
592 Register src,
593 int power) {
594 ASSERT(power >= 0);
595 ASSERT(power < 64);
596 if (power == 0) {
597 SmiToInteger64(dst, src);
598 return;
599 }
Steve Block3ce2e202009-11-05 08:53:23 +0000600 if (!dst.is(src)) {
601 movq(dst, src);
602 }
603 if (power < kSmiShift) {
604 sar(dst, Immediate(kSmiShift - power));
605 } else if (power > kSmiShift) {
606 shl(dst, Immediate(power - kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +0000607 }
608}
609
610
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100611void MacroAssembler::PositiveSmiDivPowerOfTwoToInteger32(Register dst,
612 Register src,
613 int power) {
614 ASSERT((0 <= power) && (power < 32));
615 if (dst.is(src)) {
616 shr(dst, Immediate(power + kSmiShift));
617 } else {
618 UNIMPLEMENTED(); // Not used.
619 }
620}
621
622
Steve Blocka7e24c12009-10-30 11:49:00 +0000623Condition MacroAssembler::CheckSmi(Register src) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 ASSERT_EQ(0, kSmiTag);
625 testb(src, Immediate(kSmiTagMask));
Steve Block3ce2e202009-11-05 08:53:23 +0000626 return zero;
Steve Blocka7e24c12009-10-30 11:49:00 +0000627}
628
629
630Condition MacroAssembler::CheckPositiveSmi(Register src) {
631 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000632 movq(kScratchRegister, src);
633 rol(kScratchRegister, Immediate(1));
634 testl(kScratchRegister, Immediate(0x03));
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 return zero;
636}
637
638
Steve Blocka7e24c12009-10-30 11:49:00 +0000639Condition MacroAssembler::CheckBothSmi(Register first, Register second) {
640 if (first.is(second)) {
641 return CheckSmi(first);
642 }
643 movl(kScratchRegister, first);
644 orl(kScratchRegister, second);
Steve Block3ce2e202009-11-05 08:53:23 +0000645 testb(kScratchRegister, Immediate(kSmiTagMask));
646 return zero;
Steve Blocka7e24c12009-10-30 11:49:00 +0000647}
648
649
Leon Clarked91b9f72010-01-27 17:25:45 +0000650Condition MacroAssembler::CheckBothPositiveSmi(Register first,
651 Register second) {
652 if (first.is(second)) {
653 return CheckPositiveSmi(first);
654 }
655 movl(kScratchRegister, first);
656 orl(kScratchRegister, second);
657 rol(kScratchRegister, Immediate(1));
658 testl(kScratchRegister, Immediate(0x03));
659 return zero;
660}
661
662
663
Leon Clarkee46be812010-01-19 14:06:41 +0000664Condition MacroAssembler::CheckEitherSmi(Register first, Register second) {
665 if (first.is(second)) {
666 return CheckSmi(first);
667 }
668 movl(kScratchRegister, first);
669 andl(kScratchRegister, second);
670 testb(kScratchRegister, Immediate(kSmiTagMask));
671 return zero;
672}
673
674
Steve Blocka7e24c12009-10-30 11:49:00 +0000675Condition MacroAssembler::CheckIsMinSmi(Register src) {
676 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
Steve Block3ce2e202009-11-05 08:53:23 +0000677 movq(kScratchRegister, src);
678 rol(kScratchRegister, Immediate(1));
679 cmpq(kScratchRegister, Immediate(1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000680 return equal;
681}
682
Steve Blocka7e24c12009-10-30 11:49:00 +0000683
684Condition MacroAssembler::CheckInteger32ValidSmiValue(Register src) {
Steve Block3ce2e202009-11-05 08:53:23 +0000685 // A 32-bit integer value can always be converted to a smi.
686 return always;
Steve Blocka7e24c12009-10-30 11:49:00 +0000687}
688
689
Steve Block3ce2e202009-11-05 08:53:23 +0000690Condition MacroAssembler::CheckUInteger32ValidSmiValue(Register src) {
691 // An unsigned 32-bit integer value is valid as long as the high bit
692 // is not set.
693 testq(src, Immediate(0x80000000));
694 return zero;
695}
696
697
698void MacroAssembler::SmiNeg(Register dst, Register src, Label* on_smi_result) {
699 if (dst.is(src)) {
700 ASSERT(!dst.is(kScratchRegister));
701 movq(kScratchRegister, src);
702 neg(dst); // Low 32 bits are retained as zero by negation.
703 // Test if result is zero or Smi::kMinValue.
704 cmpq(dst, kScratchRegister);
705 j(not_equal, on_smi_result);
706 movq(src, kScratchRegister);
707 } else {
708 movq(dst, src);
709 neg(dst);
710 cmpq(dst, src);
711 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
712 j(not_equal, on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000714}
715
716
717void MacroAssembler::SmiAdd(Register dst,
718 Register src1,
719 Register src2,
720 Label* on_not_smi_result) {
721 ASSERT(!dst.is(src2));
Steve Block6ded16b2010-05-10 14:33:55 +0100722 if (on_not_smi_result == NULL) {
723 // No overflow checking. Use only when it's known that
724 // overflowing is impossible.
725 if (dst.is(src1)) {
726 addq(dst, src2);
727 } else {
728 movq(dst, src1);
729 addq(dst, src2);
730 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100731 Assert(no_overflow, "Smi addition overflow");
Steve Block6ded16b2010-05-10 14:33:55 +0100732 } else if (dst.is(src1)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100733 movq(kScratchRegister, src1);
734 addq(kScratchRegister, src2);
735 j(overflow, on_not_smi_result);
736 movq(dst, kScratchRegister);
Steve Block3ce2e202009-11-05 08:53:23 +0000737 } else {
738 movq(dst, src1);
739 addq(dst, src2);
740 j(overflow, on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000741 }
742}
743
744
Steve Blocka7e24c12009-10-30 11:49:00 +0000745void MacroAssembler::SmiSub(Register dst,
746 Register src1,
747 Register src2,
748 Label* on_not_smi_result) {
749 ASSERT(!dst.is(src2));
Leon Clarked91b9f72010-01-27 17:25:45 +0000750 if (on_not_smi_result == NULL) {
751 // No overflow checking. Use only when it's known that
752 // overflowing is impossible (e.g., subtracting two positive smis).
753 if (dst.is(src1)) {
754 subq(dst, src2);
755 } else {
756 movq(dst, src1);
757 subq(dst, src2);
758 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100759 Assert(no_overflow, "Smi subtraction overflow");
Leon Clarked91b9f72010-01-27 17:25:45 +0000760 } else if (dst.is(src1)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100761 cmpq(dst, src2);
762 j(overflow, on_not_smi_result);
Steve Block3ce2e202009-11-05 08:53:23 +0000763 subq(dst, src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000764 } else {
765 movq(dst, src1);
766 subq(dst, src2);
767 j(overflow, on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000768 }
769}
770
771
Steve Block6ded16b2010-05-10 14:33:55 +0100772void MacroAssembler::SmiSub(Register dst,
773 Register src1,
Leon Clarkef7060e22010-06-03 12:02:55 +0100774 const Operand& src2,
Steve Block6ded16b2010-05-10 14:33:55 +0100775 Label* on_not_smi_result) {
776 if (on_not_smi_result == NULL) {
777 // No overflow checking. Use only when it's known that
778 // overflowing is impossible (e.g., subtracting two positive smis).
779 if (dst.is(src1)) {
780 subq(dst, src2);
781 } else {
782 movq(dst, src1);
783 subq(dst, src2);
784 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100785 Assert(no_overflow, "Smi subtraction overflow");
Steve Block6ded16b2010-05-10 14:33:55 +0100786 } else if (dst.is(src1)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100787 movq(kScratchRegister, src1);
788 subq(kScratchRegister, src2);
789 j(overflow, on_not_smi_result);
790 movq(src1, kScratchRegister);
Steve Block6ded16b2010-05-10 14:33:55 +0100791 } else {
792 movq(dst, src1);
793 subq(dst, src2);
794 j(overflow, on_not_smi_result);
795 }
796}
797
Steve Blocka7e24c12009-10-30 11:49:00 +0000798void MacroAssembler::SmiMul(Register dst,
799 Register src1,
800 Register src2,
801 Label* on_not_smi_result) {
802 ASSERT(!dst.is(src2));
Steve Block3ce2e202009-11-05 08:53:23 +0000803 ASSERT(!dst.is(kScratchRegister));
804 ASSERT(!src1.is(kScratchRegister));
805 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +0000806
807 if (dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000808 Label failure, zero_correct_result;
809 movq(kScratchRegister, src1); // Create backup for later testing.
810 SmiToInteger64(dst, src1);
811 imul(dst, src2);
812 j(overflow, &failure);
813
814 // Check for negative zero result. If product is zero, and one
815 // argument is negative, go to slow case.
816 Label correct_result;
817 testq(dst, dst);
818 j(not_zero, &correct_result);
819
820 movq(dst, kScratchRegister);
821 xor_(dst, src2);
822 j(positive, &zero_correct_result); // Result was positive zero.
823
824 bind(&failure); // Reused failure exit, restores src1.
825 movq(src1, kScratchRegister);
826 jmp(on_not_smi_result);
827
828 bind(&zero_correct_result);
829 xor_(dst, dst);
830
831 bind(&correct_result);
832 } else {
833 SmiToInteger64(dst, src1);
834 imul(dst, src2);
835 j(overflow, on_not_smi_result);
836 // Check for negative zero result. If product is zero, and one
837 // argument is negative, go to slow case.
838 Label correct_result;
839 testq(dst, dst);
840 j(not_zero, &correct_result);
841 // One of src1 and src2 is zero, the check whether the other is
842 // negative.
Steve Blocka7e24c12009-10-30 11:49:00 +0000843 movq(kScratchRegister, src1);
Steve Block3ce2e202009-11-05 08:53:23 +0000844 xor_(kScratchRegister, src2);
845 j(negative, on_not_smi_result);
846 bind(&correct_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000848}
849
850
851void MacroAssembler::SmiTryAddConstant(Register dst,
852 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000853 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 Label* on_not_smi_result) {
855 // Does not assume that src is a smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000856 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +0000857 ASSERT_EQ(0, kSmiTag);
Steve Block3ce2e202009-11-05 08:53:23 +0000858 ASSERT(!dst.is(kScratchRegister));
859 ASSERT(!src.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +0000860
Steve Block3ce2e202009-11-05 08:53:23 +0000861 JumpIfNotSmi(src, on_not_smi_result);
862 Register tmp = (dst.is(src) ? kScratchRegister : dst);
863 Move(tmp, constant);
864 addq(tmp, src);
865 j(overflow, on_not_smi_result);
866 if (dst.is(src)) {
867 movq(dst, tmp);
868 }
869}
870
871
872void MacroAssembler::SmiAddConstant(Register dst, Register src, Smi* constant) {
873 if (constant->value() == 0) {
874 if (!dst.is(src)) {
875 movq(dst, src);
876 }
877 } else if (dst.is(src)) {
878 ASSERT(!dst.is(kScratchRegister));
879
880 Move(kScratchRegister, constant);
881 addq(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000882 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000883 Move(dst, constant);
884 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 }
886}
887
888
Leon Clarkef7060e22010-06-03 12:02:55 +0100889void MacroAssembler::SmiAddConstant(const Operand& dst, Smi* constant) {
890 if (constant->value() != 0) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100891 addl(Operand(dst, kSmiShift / kBitsPerByte), Immediate(constant->value()));
Leon Clarkef7060e22010-06-03 12:02:55 +0100892 }
893}
894
895
Steve Blocka7e24c12009-10-30 11:49:00 +0000896void MacroAssembler::SmiAddConstant(Register dst,
897 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000898 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +0000900 if (constant->value() == 0) {
901 if (!dst.is(src)) {
902 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000903 }
Steve Block3ce2e202009-11-05 08:53:23 +0000904 } else if (dst.is(src)) {
905 ASSERT(!dst.is(kScratchRegister));
906
907 Move(kScratchRegister, constant);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100908 addq(kScratchRegister, dst);
909 j(overflow, on_not_smi_result);
910 movq(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +0000911 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000912 Move(dst, constant);
913 addq(dst, src);
914 j(overflow, on_not_smi_result);
915 }
916}
917
918
919void MacroAssembler::SmiSubConstant(Register dst, Register src, Smi* constant) {
920 if (constant->value() == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 if (!dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000922 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000923 }
Steve Block3ce2e202009-11-05 08:53:23 +0000924 } else if (dst.is(src)) {
925 ASSERT(!dst.is(kScratchRegister));
926
927 Move(kScratchRegister, constant);
928 subq(dst, kScratchRegister);
929 } else {
930 // Subtract by adding the negative, to do it in two operations.
931 if (constant->value() == Smi::kMinValue) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100932 Move(dst, constant);
933 // Adding and subtracting the min-value gives the same result, it only
934 // differs on the overflow bit, which we don't check here.
935 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000936 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100937 // Subtract by adding the negation.
Steve Block3ce2e202009-11-05 08:53:23 +0000938 Move(dst, Smi::FromInt(-constant->value()));
939 addq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 }
941 }
942}
943
944
945void MacroAssembler::SmiSubConstant(Register dst,
946 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000947 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000948 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +0000949 if (constant->value() == 0) {
950 if (!dst.is(src)) {
951 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000952 }
Steve Block3ce2e202009-11-05 08:53:23 +0000953 } else if (dst.is(src)) {
954 ASSERT(!dst.is(kScratchRegister));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100955 if (constant->value() == Smi::kMinValue) {
956 // Subtracting min-value from any non-negative value will overflow.
957 // We test the non-negativeness before doing the subtraction.
958 testq(src, src);
959 j(not_sign, on_not_smi_result);
960 Move(kScratchRegister, constant);
961 subq(dst, kScratchRegister);
962 } else {
963 // Subtract by adding the negation.
964 Move(kScratchRegister, Smi::FromInt(-constant->value()));
965 addq(kScratchRegister, dst);
966 j(overflow, on_not_smi_result);
967 movq(dst, kScratchRegister);
968 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000969 } else {
Steve Block3ce2e202009-11-05 08:53:23 +0000970 if (constant->value() == Smi::kMinValue) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100971 // Subtracting min-value from any non-negative value will overflow.
972 // We test the non-negativeness before doing the subtraction.
973 testq(src, src);
974 j(not_sign, on_not_smi_result);
975 Move(dst, constant);
976 // Adding and subtracting the min-value gives the same result, it only
977 // differs on the overflow bit, which we don't check here.
978 addq(dst, src);
Steve Block3ce2e202009-11-05 08:53:23 +0000979 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100980 // Subtract by adding the negation.
Steve Block3ce2e202009-11-05 08:53:23 +0000981 Move(dst, Smi::FromInt(-(constant->value())));
982 addq(dst, src);
983 j(overflow, on_not_smi_result);
984 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000985 }
986}
987
988
989void MacroAssembler::SmiDiv(Register dst,
990 Register src1,
991 Register src2,
992 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +0000993 ASSERT(!src1.is(kScratchRegister));
994 ASSERT(!src2.is(kScratchRegister));
995 ASSERT(!dst.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +0000996 ASSERT(!src2.is(rax));
997 ASSERT(!src2.is(rdx));
998 ASSERT(!src1.is(rdx));
999
1000 // Check for 0 divisor (result is +/-Infinity).
1001 Label positive_divisor;
Steve Block3ce2e202009-11-05 08:53:23 +00001002 testq(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001003 j(zero, on_not_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001004
Steve Block3ce2e202009-11-05 08:53:23 +00001005 if (src1.is(rax)) {
1006 movq(kScratchRegister, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001007 }
Steve Block3ce2e202009-11-05 08:53:23 +00001008 SmiToInteger32(rax, src1);
1009 // We need to rule out dividing Smi::kMinValue by -1, since that would
1010 // overflow in idiv and raise an exception.
1011 // We combine this with negative zero test (negative zero only happens
1012 // when dividing zero by a negative number).
Steve Blocka7e24c12009-10-30 11:49:00 +00001013
Steve Block3ce2e202009-11-05 08:53:23 +00001014 // We overshoot a little and go to slow case if we divide min-value
1015 // by any negative value, not just -1.
1016 Label safe_div;
1017 testl(rax, Immediate(0x7fffffff));
1018 j(not_zero, &safe_div);
1019 testq(src2, src2);
1020 if (src1.is(rax)) {
1021 j(positive, &safe_div);
1022 movq(src1, kScratchRegister);
1023 jmp(on_not_smi_result);
1024 } else {
1025 j(negative, on_not_smi_result);
1026 }
1027 bind(&safe_div);
1028
1029 SmiToInteger32(src2, src2);
1030 // Sign extend src1 into edx:eax.
1031 cdq();
Steve Blocka7e24c12009-10-30 11:49:00 +00001032 idivl(src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001033 Integer32ToSmi(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001034 // Check that the remainder is zero.
1035 testl(rdx, rdx);
Steve Block3ce2e202009-11-05 08:53:23 +00001036 if (src1.is(rax)) {
1037 Label smi_result;
1038 j(zero, &smi_result);
1039 movq(src1, kScratchRegister);
1040 jmp(on_not_smi_result);
1041 bind(&smi_result);
1042 } else {
1043 j(not_zero, on_not_smi_result);
1044 }
1045 if (!dst.is(src1) && src1.is(rax)) {
1046 movq(src1, kScratchRegister);
1047 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 Integer32ToSmi(dst, rax);
1049}
1050
1051
1052void MacroAssembler::SmiMod(Register dst,
1053 Register src1,
1054 Register src2,
1055 Label* on_not_smi_result) {
1056 ASSERT(!dst.is(kScratchRegister));
1057 ASSERT(!src1.is(kScratchRegister));
1058 ASSERT(!src2.is(kScratchRegister));
1059 ASSERT(!src2.is(rax));
1060 ASSERT(!src2.is(rdx));
1061 ASSERT(!src1.is(rdx));
Steve Block3ce2e202009-11-05 08:53:23 +00001062 ASSERT(!src1.is(src2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001063
Steve Block3ce2e202009-11-05 08:53:23 +00001064 testq(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001065 j(zero, on_not_smi_result);
1066
1067 if (src1.is(rax)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001068 movq(kScratchRegister, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 }
Steve Block3ce2e202009-11-05 08:53:23 +00001070 SmiToInteger32(rax, src1);
1071 SmiToInteger32(src2, src2);
1072
1073 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1074 Label safe_div;
1075 cmpl(rax, Immediate(Smi::kMinValue));
1076 j(not_equal, &safe_div);
1077 cmpl(src2, Immediate(-1));
1078 j(not_equal, &safe_div);
1079 // Retag inputs and go slow case.
1080 Integer32ToSmi(src2, src2);
1081 if (src1.is(rax)) {
1082 movq(src1, kScratchRegister);
1083 }
1084 jmp(on_not_smi_result);
1085 bind(&safe_div);
1086
Steve Blocka7e24c12009-10-30 11:49:00 +00001087 // Sign extend eax into edx:eax.
1088 cdq();
1089 idivl(src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001090 // Restore smi tags on inputs.
1091 Integer32ToSmi(src2, src2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001092 if (src1.is(rax)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001093 movq(src1, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +00001094 }
Steve Block3ce2e202009-11-05 08:53:23 +00001095 // Check for a negative zero result. If the result is zero, and the
1096 // dividend is negative, go slow to return a floating point negative zero.
1097 Label smi_result;
1098 testl(rdx, rdx);
1099 j(not_zero, &smi_result);
1100 testq(src1, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001101 j(negative, on_not_smi_result);
Steve Block3ce2e202009-11-05 08:53:23 +00001102 bind(&smi_result);
1103 Integer32ToSmi(dst, rdx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001104}
1105
1106
1107void MacroAssembler::SmiNot(Register dst, Register src) {
Steve Block3ce2e202009-11-05 08:53:23 +00001108 ASSERT(!dst.is(kScratchRegister));
1109 ASSERT(!src.is(kScratchRegister));
1110 // Set tag and padding bits before negating, so that they are zero afterwards.
1111 movl(kScratchRegister, Immediate(~0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001112 if (dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001113 xor_(dst, kScratchRegister);
Steve Blocka7e24c12009-10-30 11:49:00 +00001114 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001115 lea(dst, Operand(src, kScratchRegister, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001116 }
Steve Block3ce2e202009-11-05 08:53:23 +00001117 not_(dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001118}
1119
1120
1121void MacroAssembler::SmiAnd(Register dst, Register src1, Register src2) {
Steve Block3ce2e202009-11-05 08:53:23 +00001122 ASSERT(!dst.is(src2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001123 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001124 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001125 }
1126 and_(dst, src2);
1127}
1128
1129
Steve Block3ce2e202009-11-05 08:53:23 +00001130void MacroAssembler::SmiAndConstant(Register dst, Register src, Smi* constant) {
1131 if (constant->value() == 0) {
1132 xor_(dst, dst);
1133 } else if (dst.is(src)) {
1134 ASSERT(!dst.is(kScratchRegister));
1135 Move(kScratchRegister, constant);
1136 and_(dst, kScratchRegister);
1137 } else {
1138 Move(dst, constant);
1139 and_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001140 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001141}
1142
1143
1144void MacroAssembler::SmiOr(Register dst, Register src1, Register src2) {
1145 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001146 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001147 }
1148 or_(dst, src2);
1149}
1150
1151
Steve Block3ce2e202009-11-05 08:53:23 +00001152void MacroAssembler::SmiOrConstant(Register dst, Register src, Smi* constant) {
1153 if (dst.is(src)) {
1154 ASSERT(!dst.is(kScratchRegister));
1155 Move(kScratchRegister, constant);
1156 or_(dst, kScratchRegister);
1157 } else {
1158 Move(dst, constant);
1159 or_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001160 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001161}
1162
Steve Block3ce2e202009-11-05 08:53:23 +00001163
Steve Blocka7e24c12009-10-30 11:49:00 +00001164void MacroAssembler::SmiXor(Register dst, Register src1, Register src2) {
1165 if (!dst.is(src1)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001166 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001167 }
1168 xor_(dst, src2);
1169}
1170
1171
Steve Block3ce2e202009-11-05 08:53:23 +00001172void MacroAssembler::SmiXorConstant(Register dst, Register src, Smi* constant) {
1173 if (dst.is(src)) {
1174 ASSERT(!dst.is(kScratchRegister));
1175 Move(kScratchRegister, constant);
1176 xor_(dst, kScratchRegister);
1177 } else {
1178 Move(dst, constant);
1179 xor_(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001180 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001181}
1182
1183
Steve Blocka7e24c12009-10-30 11:49:00 +00001184void MacroAssembler::SmiShiftArithmeticRightConstant(Register dst,
1185 Register src,
1186 int shift_value) {
Steve Block3ce2e202009-11-05 08:53:23 +00001187 ASSERT(is_uint5(shift_value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 if (shift_value > 0) {
1189 if (dst.is(src)) {
Steve Block3ce2e202009-11-05 08:53:23 +00001190 sar(dst, Immediate(shift_value + kSmiShift));
1191 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001192 } else {
1193 UNIMPLEMENTED(); // Not used.
1194 }
1195 }
1196}
1197
1198
1199void MacroAssembler::SmiShiftLogicalRightConstant(Register dst,
1200 Register src,
1201 int shift_value,
1202 Label* on_not_smi_result) {
1203 // Logic right shift interprets its result as an *unsigned* number.
1204 if (dst.is(src)) {
1205 UNIMPLEMENTED(); // Not used.
1206 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001207 movq(dst, src);
1208 if (shift_value == 0) {
1209 testq(dst, dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001210 j(negative, on_not_smi_result);
1211 }
Steve Block3ce2e202009-11-05 08:53:23 +00001212 shr(dst, Immediate(shift_value + kSmiShift));
1213 shl(dst, Immediate(kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001214 }
1215}
1216
1217
1218void MacroAssembler::SmiShiftLeftConstant(Register dst,
1219 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +01001220 int shift_value) {
Steve Block3ce2e202009-11-05 08:53:23 +00001221 if (!dst.is(src)) {
1222 movq(dst, src);
1223 }
1224 if (shift_value > 0) {
1225 shl(dst, Immediate(shift_value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001226 }
1227}
1228
1229
1230void MacroAssembler::SmiShiftLeft(Register dst,
1231 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +01001232 Register src2) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001233 ASSERT(!dst.is(rcx));
1234 Label result_ok;
Steve Block3ce2e202009-11-05 08:53:23 +00001235 // Untag shift amount.
1236 if (!dst.is(src1)) {
1237 movq(dst, src1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001238 }
Steve Block3ce2e202009-11-05 08:53:23 +00001239 SmiToInteger32(rcx, src2);
1240 // Shift amount specified by lower 5 bits, not six as the shl opcode.
1241 and_(rcx, Immediate(0x1f));
Steve Blockd0582a62009-12-15 09:54:21 +00001242 shl_cl(dst);
Steve Blocka7e24c12009-10-30 11:49:00 +00001243}
1244
1245
1246void MacroAssembler::SmiShiftLogicalRight(Register dst,
1247 Register src1,
1248 Register src2,
1249 Label* on_not_smi_result) {
Steve Block3ce2e202009-11-05 08:53:23 +00001250 ASSERT(!dst.is(kScratchRegister));
1251 ASSERT(!src1.is(kScratchRegister));
1252 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001253 ASSERT(!dst.is(rcx));
1254 Label result_ok;
Steve Block3ce2e202009-11-05 08:53:23 +00001255 if (src1.is(rcx) || src2.is(rcx)) {
1256 movq(kScratchRegister, rcx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001257 }
Steve Block3ce2e202009-11-05 08:53:23 +00001258 if (!dst.is(src1)) {
1259 movq(dst, src1);
1260 }
1261 SmiToInteger32(rcx, src2);
1262 orl(rcx, Immediate(kSmiShift));
Steve Blockd0582a62009-12-15 09:54:21 +00001263 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
Steve Block3ce2e202009-11-05 08:53:23 +00001264 shl(dst, Immediate(kSmiShift));
1265 testq(dst, dst);
1266 if (src1.is(rcx) || src2.is(rcx)) {
1267 Label positive_result;
1268 j(positive, &positive_result);
1269 if (src1.is(rcx)) {
1270 movq(src1, kScratchRegister);
1271 } else {
1272 movq(src2, kScratchRegister);
1273 }
1274 jmp(on_not_smi_result);
1275 bind(&positive_result);
1276 } else {
1277 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1278 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001279}
1280
1281
1282void MacroAssembler::SmiShiftArithmeticRight(Register dst,
1283 Register src1,
1284 Register src2) {
Steve Block3ce2e202009-11-05 08:53:23 +00001285 ASSERT(!dst.is(kScratchRegister));
1286 ASSERT(!src1.is(kScratchRegister));
1287 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001288 ASSERT(!dst.is(rcx));
Steve Block3ce2e202009-11-05 08:53:23 +00001289 if (src1.is(rcx)) {
1290 movq(kScratchRegister, src1);
1291 } else if (src2.is(rcx)) {
1292 movq(kScratchRegister, src2);
1293 }
1294 if (!dst.is(src1)) {
1295 movq(dst, src1);
1296 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001297 SmiToInteger32(rcx, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001298 orl(rcx, Immediate(kSmiShift));
Steve Blockd0582a62009-12-15 09:54:21 +00001299 sar_cl(dst); // Shift 32 + original rcx & 0x1f.
Steve Block3ce2e202009-11-05 08:53:23 +00001300 shl(dst, Immediate(kSmiShift));
1301 if (src1.is(rcx)) {
1302 movq(src1, kScratchRegister);
1303 } else if (src2.is(rcx)) {
1304 movq(src2, kScratchRegister);
1305 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001306}
1307
1308
1309void MacroAssembler::SelectNonSmi(Register dst,
1310 Register src1,
1311 Register src2,
1312 Label* on_not_smis) {
Steve Block3ce2e202009-11-05 08:53:23 +00001313 ASSERT(!dst.is(kScratchRegister));
1314 ASSERT(!src1.is(kScratchRegister));
1315 ASSERT(!src2.is(kScratchRegister));
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 ASSERT(!dst.is(src1));
1317 ASSERT(!dst.is(src2));
1318 // Both operands must not be smis.
1319#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00001320 if (allow_stub_calls()) { // Check contains a stub call.
1321 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1322 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1323 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001324#endif
1325 ASSERT_EQ(0, kSmiTag);
1326 ASSERT_EQ(0, Smi::FromInt(0));
Steve Block3ce2e202009-11-05 08:53:23 +00001327 movl(kScratchRegister, Immediate(kSmiTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 and_(kScratchRegister, src1);
1329 testl(kScratchRegister, src2);
Steve Block3ce2e202009-11-05 08:53:23 +00001330 // If non-zero then both are smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001331 j(not_zero, on_not_smis);
Steve Blocka7e24c12009-10-30 11:49:00 +00001332
Steve Block3ce2e202009-11-05 08:53:23 +00001333 // Exactly one operand is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +00001334 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1335 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1336 subq(kScratchRegister, Immediate(1));
1337 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1338 movq(dst, src1);
1339 xor_(dst, src2);
1340 and_(dst, kScratchRegister);
1341 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1342 xor_(dst, src1);
Steve Block3ce2e202009-11-05 08:53:23 +00001343 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
Steve Blocka7e24c12009-10-30 11:49:00 +00001344}
1345
Steve Block3ce2e202009-11-05 08:53:23 +00001346SmiIndex MacroAssembler::SmiToIndex(Register dst,
1347 Register src,
1348 int shift) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001349 ASSERT(is_uint6(shift));
Steve Block3ce2e202009-11-05 08:53:23 +00001350 // There is a possible optimization if shift is in the range 60-63, but that
1351 // will (and must) never happen.
1352 if (!dst.is(src)) {
1353 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 }
Steve Block3ce2e202009-11-05 08:53:23 +00001355 if (shift < kSmiShift) {
1356 sar(dst, Immediate(kSmiShift - shift));
1357 } else {
1358 shl(dst, Immediate(shift - kSmiShift));
Steve Blocka7e24c12009-10-30 11:49:00 +00001359 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001360 return SmiIndex(dst, times_1);
1361}
1362
Steve Blocka7e24c12009-10-30 11:49:00 +00001363SmiIndex MacroAssembler::SmiToNegativeIndex(Register dst,
1364 Register src,
1365 int shift) {
1366 // Register src holds a positive smi.
1367 ASSERT(is_uint6(shift));
Steve Block3ce2e202009-11-05 08:53:23 +00001368 if (!dst.is(src)) {
1369 movq(dst, src);
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001371 neg(dst);
Steve Block3ce2e202009-11-05 08:53:23 +00001372 if (shift < kSmiShift) {
1373 sar(dst, Immediate(kSmiShift - shift));
1374 } else {
1375 shl(dst, Immediate(shift - kSmiShift));
1376 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001377 return SmiIndex(dst, times_1);
1378}
1379
1380
Steve Block3ce2e202009-11-05 08:53:23 +00001381void MacroAssembler::JumpIfSmi(Register src, Label* on_smi) {
1382 ASSERT_EQ(0, kSmiTag);
1383 Condition smi = CheckSmi(src);
1384 j(smi, on_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001385}
1386
Steve Block3ce2e202009-11-05 08:53:23 +00001387
1388void MacroAssembler::JumpIfNotSmi(Register src, Label* on_not_smi) {
1389 Condition smi = CheckSmi(src);
1390 j(NegateCondition(smi), on_not_smi);
1391}
1392
1393
1394void MacroAssembler::JumpIfNotPositiveSmi(Register src,
1395 Label* on_not_positive_smi) {
1396 Condition positive_smi = CheckPositiveSmi(src);
1397 j(NegateCondition(positive_smi), on_not_positive_smi);
1398}
1399
1400
1401void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1402 Smi* constant,
1403 Label* on_equals) {
1404 SmiCompare(src, constant);
1405 j(equal, on_equals);
1406}
1407
1408
1409void MacroAssembler::JumpIfNotValidSmiValue(Register src, Label* on_invalid) {
1410 Condition is_valid = CheckInteger32ValidSmiValue(src);
1411 j(NegateCondition(is_valid), on_invalid);
1412}
1413
1414
1415void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1416 Label* on_invalid) {
1417 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1418 j(NegateCondition(is_valid), on_invalid);
1419}
1420
1421
1422void MacroAssembler::JumpIfNotBothSmi(Register src1, Register src2,
1423 Label* on_not_both_smi) {
1424 Condition both_smi = CheckBothSmi(src1, src2);
1425 j(NegateCondition(both_smi), on_not_both_smi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001426}
1427
1428
Leon Clarked91b9f72010-01-27 17:25:45 +00001429void MacroAssembler::JumpIfNotBothPositiveSmi(Register src1, Register src2,
1430 Label* on_not_both_smi) {
1431 Condition both_smi = CheckBothPositiveSmi(src1, src2);
1432 j(NegateCondition(both_smi), on_not_both_smi);
1433}
1434
1435
1436
Leon Clarkee46be812010-01-19 14:06:41 +00001437void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1438 Register second_object,
1439 Register scratch1,
1440 Register scratch2,
1441 Label* on_fail) {
1442 // Check that both objects are not smis.
1443 Condition either_smi = CheckEitherSmi(first_object, second_object);
1444 j(either_smi, on_fail);
1445
1446 // Load instance type for both strings.
1447 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1448 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1449 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1450 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1451
1452 // Check that both are flat ascii strings.
1453 ASSERT(kNotStringTag != 0);
1454 const int kFlatAsciiStringMask =
1455 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
Leon Clarked91b9f72010-01-27 17:25:45 +00001456 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
Leon Clarkee46be812010-01-19 14:06:41 +00001457
1458 andl(scratch1, Immediate(kFlatAsciiStringMask));
1459 andl(scratch2, Immediate(kFlatAsciiStringMask));
1460 // Interleave the bits to check both scratch1 and scratch2 in one test.
1461 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1462 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1463 cmpl(scratch1,
Leon Clarked91b9f72010-01-27 17:25:45 +00001464 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
Leon Clarkee46be812010-01-19 14:06:41 +00001465 j(not_equal, on_fail);
1466}
1467
1468
Steve Block6ded16b2010-05-10 14:33:55 +01001469void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1470 Register instance_type,
1471 Register scratch,
1472 Label *failure) {
1473 if (!scratch.is(instance_type)) {
1474 movl(scratch, instance_type);
1475 }
1476
1477 const int kFlatAsciiStringMask =
1478 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1479
1480 andl(scratch, Immediate(kFlatAsciiStringMask));
1481 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1482 j(not_equal, failure);
1483}
1484
1485
1486void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1487 Register first_object_instance_type,
1488 Register second_object_instance_type,
1489 Register scratch1,
1490 Register scratch2,
1491 Label* on_fail) {
1492 // Load instance type for both strings.
1493 movq(scratch1, first_object_instance_type);
1494 movq(scratch2, second_object_instance_type);
1495
1496 // Check that both are flat ascii strings.
1497 ASSERT(kNotStringTag != 0);
1498 const int kFlatAsciiStringMask =
1499 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1500 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1501
1502 andl(scratch1, Immediate(kFlatAsciiStringMask));
1503 andl(scratch2, Immediate(kFlatAsciiStringMask));
1504 // Interleave the bits to check both scratch1 and scratch2 in one test.
1505 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1506 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1507 cmpl(scratch1,
1508 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1509 j(not_equal, on_fail);
1510}
1511
1512
Steve Blocka7e24c12009-10-30 11:49:00 +00001513void MacroAssembler::Move(Register dst, Handle<Object> source) {
1514 ASSERT(!source->IsFailure());
1515 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001516 Move(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 } else {
1518 movq(dst, source, RelocInfo::EMBEDDED_OBJECT);
1519 }
1520}
1521
1522
1523void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001524 ASSERT(!source->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001526 Move(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001527 } else {
1528 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1529 movq(dst, kScratchRegister);
1530 }
1531}
1532
1533
1534void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001535 if (source->IsSmi()) {
1536 SmiCompare(dst, Smi::cast(*source));
1537 } else {
1538 Move(kScratchRegister, source);
1539 cmpq(dst, kScratchRegister);
1540 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001541}
1542
1543
1544void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
1545 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001546 SmiCompare(dst, Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001547 } else {
1548 ASSERT(source->IsHeapObject());
1549 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1550 cmpq(dst, kScratchRegister);
1551 }
1552}
1553
1554
1555void MacroAssembler::Push(Handle<Object> source) {
1556 if (source->IsSmi()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001557 Push(Smi::cast(*source));
Steve Blocka7e24c12009-10-30 11:49:00 +00001558 } else {
1559 ASSERT(source->IsHeapObject());
1560 movq(kScratchRegister, source, RelocInfo::EMBEDDED_OBJECT);
1561 push(kScratchRegister);
1562 }
1563}
1564
1565
1566void MacroAssembler::Push(Smi* source) {
Steve Block3ce2e202009-11-05 08:53:23 +00001567 intptr_t smi = reinterpret_cast<intptr_t>(source);
1568 if (is_int32(smi)) {
1569 push(Immediate(static_cast<int32_t>(smi)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001570 } else {
Steve Block3ce2e202009-11-05 08:53:23 +00001571 Set(kScratchRegister, smi);
1572 push(kScratchRegister);
1573 }
1574}
1575
1576
Leon Clarkee46be812010-01-19 14:06:41 +00001577void MacroAssembler::Drop(int stack_elements) {
1578 if (stack_elements > 0) {
1579 addq(rsp, Immediate(stack_elements * kPointerSize));
1580 }
1581}
1582
1583
Steve Block3ce2e202009-11-05 08:53:23 +00001584void MacroAssembler::Test(const Operand& src, Smi* source) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001585 testl(Operand(src, kIntSize), Immediate(source->value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001586}
1587
1588
1589void MacroAssembler::Jump(ExternalReference ext) {
1590 movq(kScratchRegister, ext);
1591 jmp(kScratchRegister);
1592}
1593
1594
1595void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
1596 movq(kScratchRegister, destination, rmode);
1597 jmp(kScratchRegister);
1598}
1599
1600
1601void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
Steve Block3ce2e202009-11-05 08:53:23 +00001602 // TODO(X64): Inline this
1603 jmp(code_object, rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001604}
1605
1606
1607void MacroAssembler::Call(ExternalReference ext) {
1608 movq(kScratchRegister, ext);
1609 call(kScratchRegister);
1610}
1611
1612
1613void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
1614 movq(kScratchRegister, destination, rmode);
1615 call(kScratchRegister);
1616}
1617
1618
1619void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
1620 ASSERT(RelocInfo::IsCodeTarget(rmode));
1621 WriteRecordedPositions();
Steve Block3ce2e202009-11-05 08:53:23 +00001622 call(code_object, rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001623}
1624
1625
1626void MacroAssembler::PushTryHandler(CodeLocation try_location,
1627 HandlerType type) {
1628 // Adjust this code if not the case.
1629 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1630
1631 // The pc (return address) is already on TOS. This code pushes state,
1632 // frame pointer and current handler. Check that they are expected
1633 // next on the stack, in that order.
1634 ASSERT_EQ(StackHandlerConstants::kStateOffset,
1635 StackHandlerConstants::kPCOffset - kPointerSize);
1636 ASSERT_EQ(StackHandlerConstants::kFPOffset,
1637 StackHandlerConstants::kStateOffset - kPointerSize);
1638 ASSERT_EQ(StackHandlerConstants::kNextOffset,
1639 StackHandlerConstants::kFPOffset - kPointerSize);
1640
1641 if (try_location == IN_JAVASCRIPT) {
1642 if (type == TRY_CATCH_HANDLER) {
1643 push(Immediate(StackHandler::TRY_CATCH));
1644 } else {
1645 push(Immediate(StackHandler::TRY_FINALLY));
1646 }
1647 push(rbp);
1648 } else {
1649 ASSERT(try_location == IN_JS_ENTRY);
1650 // The frame pointer does not point to a JS frame so we save NULL
1651 // for rbp. We expect the code throwing an exception to check rbp
1652 // before dereferencing it to restore the context.
1653 push(Immediate(StackHandler::ENTRY));
1654 push(Immediate(0)); // NULL frame pointer.
1655 }
1656 // Save the current handler.
1657 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
1658 push(Operand(kScratchRegister, 0));
1659 // Link this handler.
1660 movq(Operand(kScratchRegister, 0), rsp);
1661}
1662
1663
Leon Clarkee46be812010-01-19 14:06:41 +00001664void MacroAssembler::PopTryHandler() {
1665 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
1666 // Unlink this handler.
1667 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
1668 pop(Operand(kScratchRegister, 0));
1669 // Remove the remaining fields.
1670 addq(rsp, Immediate(StackHandlerConstants::kSize - kPointerSize));
1671}
1672
1673
Steve Blocka7e24c12009-10-30 11:49:00 +00001674void MacroAssembler::Ret() {
1675 ret(0);
1676}
1677
1678
1679void MacroAssembler::FCmp() {
Steve Block3ce2e202009-11-05 08:53:23 +00001680 fucomip();
1681 ffree(0);
1682 fincstp();
Steve Blocka7e24c12009-10-30 11:49:00 +00001683}
1684
1685
1686void MacroAssembler::CmpObjectType(Register heap_object,
1687 InstanceType type,
1688 Register map) {
1689 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
1690 CmpInstanceType(map, type);
1691}
1692
1693
1694void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
1695 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
1696 Immediate(static_cast<int8_t>(type)));
1697}
1698
1699
Andrei Popescu31002712010-02-23 13:46:05 +00001700void MacroAssembler::CheckMap(Register obj,
1701 Handle<Map> map,
1702 Label* fail,
1703 bool is_heap_object) {
1704 if (!is_heap_object) {
1705 JumpIfSmi(obj, fail);
1706 }
1707 Cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
1708 j(not_equal, fail);
1709}
1710
1711
Leon Clarkef7060e22010-06-03 12:02:55 +01001712void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001713 Label ok;
1714 Condition is_smi = CheckSmi(object);
1715 j(is_smi, &ok);
1716 Cmp(FieldOperand(object, HeapObject::kMapOffset),
1717 Factory::heap_number_map());
Leon Clarkef7060e22010-06-03 12:02:55 +01001718 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +00001719 bind(&ok);
1720}
1721
1722
Leon Clarkef7060e22010-06-03 12:02:55 +01001723void MacroAssembler::AbortIfNotSmi(Register object) {
Steve Block6ded16b2010-05-10 14:33:55 +01001724 Label ok;
1725 Condition is_smi = CheckSmi(object);
Leon Clarkef7060e22010-06-03 12:02:55 +01001726 Assert(is_smi, "Operand not a smi");
Steve Block6ded16b2010-05-10 14:33:55 +01001727}
1728
1729
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001730void MacroAssembler::AbortIfNotRootValue(Register src,
1731 Heap::RootListIndex root_value_index,
1732 const char* message) {
1733 ASSERT(!src.is(kScratchRegister));
1734 LoadRoot(kScratchRegister, root_value_index);
1735 cmpq(src, kScratchRegister);
1736 Check(equal, message);
1737}
1738
1739
1740
Leon Clarked91b9f72010-01-27 17:25:45 +00001741Condition MacroAssembler::IsObjectStringType(Register heap_object,
1742 Register map,
1743 Register instance_type) {
1744 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
Leon Clarke4515c472010-02-03 11:58:03 +00001745 movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
Leon Clarked91b9f72010-01-27 17:25:45 +00001746 ASSERT(kNotStringTag != 0);
1747 testb(instance_type, Immediate(kIsNotStringMask));
1748 return zero;
1749}
1750
1751
Steve Blocka7e24c12009-10-30 11:49:00 +00001752void MacroAssembler::TryGetFunctionPrototype(Register function,
1753 Register result,
1754 Label* miss) {
1755 // Check that the receiver isn't a smi.
1756 testl(function, Immediate(kSmiTagMask));
1757 j(zero, miss);
1758
1759 // Check that the function really is a function.
1760 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1761 j(not_equal, miss);
1762
1763 // Make sure that the function has an instance prototype.
1764 Label non_instance;
1765 testb(FieldOperand(result, Map::kBitFieldOffset),
1766 Immediate(1 << Map::kHasNonInstancePrototype));
1767 j(not_zero, &non_instance);
1768
1769 // Get the prototype or initial map from the function.
1770 movq(result,
1771 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1772
1773 // If the prototype or initial map is the hole, don't return it and
1774 // simply miss the cache instead. This will allow us to allocate a
1775 // prototype object on-demand in the runtime system.
1776 CompareRoot(result, Heap::kTheHoleValueRootIndex);
1777 j(equal, miss);
1778
1779 // If the function does not have an initial map, we're done.
1780 Label done;
1781 CmpObjectType(result, MAP_TYPE, kScratchRegister);
1782 j(not_equal, &done);
1783
1784 // Get the prototype from the initial map.
1785 movq(result, FieldOperand(result, Map::kPrototypeOffset));
1786 jmp(&done);
1787
1788 // Non-instance prototype: Fetch prototype from constructor field
1789 // in initial map.
1790 bind(&non_instance);
1791 movq(result, FieldOperand(result, Map::kConstructorOffset));
1792
1793 // All done.
1794 bind(&done);
1795}
1796
1797
1798void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1799 if (FLAG_native_code_counters && counter->Enabled()) {
1800 movq(kScratchRegister, ExternalReference(counter));
1801 movl(Operand(kScratchRegister, 0), Immediate(value));
1802 }
1803}
1804
1805
1806void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1807 ASSERT(value > 0);
1808 if (FLAG_native_code_counters && counter->Enabled()) {
1809 movq(kScratchRegister, ExternalReference(counter));
1810 Operand operand(kScratchRegister, 0);
1811 if (value == 1) {
1812 incl(operand);
1813 } else {
1814 addl(operand, Immediate(value));
1815 }
1816 }
1817}
1818
1819
1820void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1821 ASSERT(value > 0);
1822 if (FLAG_native_code_counters && counter->Enabled()) {
1823 movq(kScratchRegister, ExternalReference(counter));
1824 Operand operand(kScratchRegister, 0);
1825 if (value == 1) {
1826 decl(operand);
1827 } else {
1828 subl(operand, Immediate(value));
1829 }
1830 }
1831}
1832
Steve Blocka7e24c12009-10-30 11:49:00 +00001833#ifdef ENABLE_DEBUGGER_SUPPORT
1834
1835void MacroAssembler::PushRegistersFromMemory(RegList regs) {
1836 ASSERT((regs & ~kJSCallerSaved) == 0);
1837 // Push the content of the memory location to the stack.
1838 for (int i = 0; i < kNumJSCallerSaved; i++) {
1839 int r = JSCallerSavedCode(i);
1840 if ((regs & (1 << r)) != 0) {
1841 ExternalReference reg_addr =
1842 ExternalReference(Debug_Address::Register(i));
1843 movq(kScratchRegister, reg_addr);
1844 push(Operand(kScratchRegister, 0));
1845 }
1846 }
1847}
1848
Steve Block3ce2e202009-11-05 08:53:23 +00001849
Steve Blocka7e24c12009-10-30 11:49:00 +00001850void MacroAssembler::SaveRegistersToMemory(RegList regs) {
1851 ASSERT((regs & ~kJSCallerSaved) == 0);
1852 // Copy the content of registers to memory location.
1853 for (int i = 0; i < kNumJSCallerSaved; i++) {
1854 int r = JSCallerSavedCode(i);
1855 if ((regs & (1 << r)) != 0) {
1856 Register reg = { r };
1857 ExternalReference reg_addr =
1858 ExternalReference(Debug_Address::Register(i));
1859 movq(kScratchRegister, reg_addr);
1860 movq(Operand(kScratchRegister, 0), reg);
1861 }
1862 }
1863}
1864
1865
1866void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
1867 ASSERT((regs & ~kJSCallerSaved) == 0);
1868 // Copy the content of memory location to registers.
1869 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1870 int r = JSCallerSavedCode(i);
1871 if ((regs & (1 << r)) != 0) {
1872 Register reg = { r };
1873 ExternalReference reg_addr =
1874 ExternalReference(Debug_Address::Register(i));
1875 movq(kScratchRegister, reg_addr);
1876 movq(reg, Operand(kScratchRegister, 0));
1877 }
1878 }
1879}
1880
1881
1882void MacroAssembler::PopRegistersToMemory(RegList regs) {
1883 ASSERT((regs & ~kJSCallerSaved) == 0);
1884 // Pop the content from the stack to the memory location.
1885 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1886 int r = JSCallerSavedCode(i);
1887 if ((regs & (1 << r)) != 0) {
1888 ExternalReference reg_addr =
1889 ExternalReference(Debug_Address::Register(i));
1890 movq(kScratchRegister, reg_addr);
1891 pop(Operand(kScratchRegister, 0));
1892 }
1893 }
1894}
1895
1896
1897void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
1898 Register scratch,
1899 RegList regs) {
1900 ASSERT(!scratch.is(kScratchRegister));
1901 ASSERT(!base.is(kScratchRegister));
1902 ASSERT(!base.is(scratch));
1903 ASSERT((regs & ~kJSCallerSaved) == 0);
1904 // Copy the content of the stack to the memory location and adjust base.
1905 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
1906 int r = JSCallerSavedCode(i);
1907 if ((regs & (1 << r)) != 0) {
1908 movq(scratch, Operand(base, 0));
1909 ExternalReference reg_addr =
1910 ExternalReference(Debug_Address::Register(i));
1911 movq(kScratchRegister, reg_addr);
1912 movq(Operand(kScratchRegister, 0), scratch);
1913 lea(base, Operand(base, kPointerSize));
1914 }
1915 }
1916}
1917
Andrei Popescu402d9372010-02-26 13:31:12 +00001918void MacroAssembler::DebugBreak() {
1919 ASSERT(allow_stub_calls());
1920 xor_(rax, rax); // no arguments
1921 movq(rbx, ExternalReference(Runtime::kDebugBreak));
1922 CEntryStub ces(1);
1923 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
Steve Blocka7e24c12009-10-30 11:49:00 +00001924}
Andrei Popescu402d9372010-02-26 13:31:12 +00001925#endif // ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +00001926
1927
1928void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1929 const ParameterCount& actual,
1930 Handle<Code> code_constant,
1931 Register code_register,
1932 Label* done,
1933 InvokeFlag flag) {
1934 bool definitely_matches = false;
1935 Label invoke;
1936 if (expected.is_immediate()) {
1937 ASSERT(actual.is_immediate());
1938 if (expected.immediate() == actual.immediate()) {
1939 definitely_matches = true;
1940 } else {
1941 movq(rax, Immediate(actual.immediate()));
1942 if (expected.immediate() ==
Steve Block3ce2e202009-11-05 08:53:23 +00001943 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001944 // Don't worry about adapting arguments for built-ins that
1945 // don't want that done. Skip adaption code by making it look
1946 // like we have a match between expected and actual number of
1947 // arguments.
1948 definitely_matches = true;
1949 } else {
1950 movq(rbx, Immediate(expected.immediate()));
1951 }
1952 }
1953 } else {
1954 if (actual.is_immediate()) {
1955 // Expected is in register, actual is immediate. This is the
1956 // case when we invoke function values without going through the
1957 // IC mechanism.
1958 cmpq(expected.reg(), Immediate(actual.immediate()));
1959 j(equal, &invoke);
1960 ASSERT(expected.reg().is(rbx));
1961 movq(rax, Immediate(actual.immediate()));
1962 } else if (!expected.reg().is(actual.reg())) {
1963 // Both expected and actual are in (different) registers. This
1964 // is the case when we invoke functions using call and apply.
1965 cmpq(expected.reg(), actual.reg());
1966 j(equal, &invoke);
1967 ASSERT(actual.reg().is(rax));
1968 ASSERT(expected.reg().is(rbx));
1969 }
1970 }
1971
1972 if (!definitely_matches) {
1973 Handle<Code> adaptor =
1974 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1975 if (!code_constant.is_null()) {
1976 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1977 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1978 } else if (!code_register.is(rdx)) {
1979 movq(rdx, code_register);
1980 }
1981
1982 if (flag == CALL_FUNCTION) {
1983 Call(adaptor, RelocInfo::CODE_TARGET);
1984 jmp(done);
1985 } else {
1986 Jump(adaptor, RelocInfo::CODE_TARGET);
1987 }
1988 bind(&invoke);
1989 }
1990}
1991
1992
1993void MacroAssembler::InvokeCode(Register code,
1994 const ParameterCount& expected,
1995 const ParameterCount& actual,
1996 InvokeFlag flag) {
1997 Label done;
1998 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1999 if (flag == CALL_FUNCTION) {
2000 call(code);
2001 } else {
2002 ASSERT(flag == JUMP_FUNCTION);
2003 jmp(code);
2004 }
2005 bind(&done);
2006}
2007
2008
2009void MacroAssembler::InvokeCode(Handle<Code> code,
2010 const ParameterCount& expected,
2011 const ParameterCount& actual,
2012 RelocInfo::Mode rmode,
2013 InvokeFlag flag) {
2014 Label done;
2015 Register dummy = rax;
2016 InvokePrologue(expected, actual, code, dummy, &done, flag);
2017 if (flag == CALL_FUNCTION) {
2018 Call(code, rmode);
2019 } else {
2020 ASSERT(flag == JUMP_FUNCTION);
2021 Jump(code, rmode);
2022 }
2023 bind(&done);
2024}
2025
2026
2027void MacroAssembler::InvokeFunction(Register function,
2028 const ParameterCount& actual,
2029 InvokeFlag flag) {
2030 ASSERT(function.is(rdi));
2031 movq(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
2032 movq(rsi, FieldOperand(function, JSFunction::kContextOffset));
2033 movsxlq(rbx,
2034 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
2035 movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
2036 // Advances rdx to the end of the Code object header, to the start of
2037 // the executable code.
2038 lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
2039
2040 ParameterCount expected(rbx);
2041 InvokeCode(rdx, expected, actual, flag);
2042}
2043
2044
Andrei Popescu402d9372010-02-26 13:31:12 +00002045void MacroAssembler::InvokeFunction(JSFunction* function,
2046 const ParameterCount& actual,
2047 InvokeFlag flag) {
2048 ASSERT(function->is_compiled());
2049 // Get the function and setup the context.
2050 Move(rdi, Handle<JSFunction>(function));
2051 movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
2052
2053 // Invoke the cached code.
2054 Handle<Code> code(function->code());
2055 ParameterCount expected(function->shared()->formal_parameter_count());
2056 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
2057}
2058
2059
Steve Blocka7e24c12009-10-30 11:49:00 +00002060void MacroAssembler::EnterFrame(StackFrame::Type type) {
2061 push(rbp);
2062 movq(rbp, rsp);
2063 push(rsi); // Context.
Steve Block3ce2e202009-11-05 08:53:23 +00002064 Push(Smi::FromInt(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00002065 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
2066 push(kScratchRegister);
2067 if (FLAG_debug_code) {
2068 movq(kScratchRegister,
2069 Factory::undefined_value(),
2070 RelocInfo::EMBEDDED_OBJECT);
2071 cmpq(Operand(rsp, 0), kScratchRegister);
2072 Check(not_equal, "code object not properly patched");
2073 }
2074}
2075
2076
2077void MacroAssembler::LeaveFrame(StackFrame::Type type) {
2078 if (FLAG_debug_code) {
Steve Block3ce2e202009-11-05 08:53:23 +00002079 Move(kScratchRegister, Smi::FromInt(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00002080 cmpq(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
2081 Check(equal, "stack frame types must match");
2082 }
2083 movq(rsp, rbp);
2084 pop(rbp);
2085}
2086
2087
Steve Blockd0582a62009-12-15 09:54:21 +00002088void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode, int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002089 // Setup the frame structure on the stack.
2090 // All constants are relative to the frame pointer of the exit frame.
2091 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
2092 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
2093 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
2094 push(rbp);
2095 movq(rbp, rsp);
2096
2097 // Reserve room for entry stack pointer and push the debug marker.
Steve Block3ce2e202009-11-05 08:53:23 +00002098 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +00002099 push(Immediate(0)); // Saved entry sp, patched before call.
2100 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
2101 push(kScratchRegister); // Accessed from EditFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +00002102
2103 // Save the frame pointer and the context in top.
2104 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
2105 ExternalReference context_address(Top::k_context_address);
2106 movq(r14, rax); // Backup rax before we use it.
2107
2108 movq(rax, rbp);
2109 store_rax(c_entry_fp_address);
2110 movq(rax, rsi);
2111 store_rax(context_address);
2112
2113 // Setup argv in callee-saved register r15. It is reused in LeaveExitFrame,
2114 // so it must be retained across the C-call.
2115 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
2116 lea(r15, Operand(rbp, r14, times_pointer_size, offset));
2117
2118#ifdef ENABLE_DEBUGGER_SUPPORT
2119 // Save the state of all registers to the stack from the memory
2120 // location. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +00002121 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002122 // TODO(1243899): This should be symmetric to
2123 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
2124 // correct here, but computed for the other call. Very error
2125 // prone! FIX THIS. Actually there are deeper problems with
2126 // register saving than this asymmetry (see the bug report
2127 // associated with this issue).
2128 PushRegistersFromMemory(kJSCallerSaved);
2129 }
2130#endif
2131
2132#ifdef _WIN64
2133 // Reserve space on stack for result and argument structures, if necessary.
2134 int result_stack_space = (result_size < 2) ? 0 : result_size * kPointerSize;
2135 // Reserve space for the Arguments object. The Windows 64-bit ABI
2136 // requires us to pass this structure as a pointer to its location on
2137 // the stack. The structure contains 2 values.
2138 int argument_stack_space = 2 * kPointerSize;
2139 // We also need backing space for 4 parameters, even though
2140 // we only pass one or two parameter, and it is in a register.
2141 int argument_mirror_space = 4 * kPointerSize;
2142 int total_stack_space =
2143 argument_mirror_space + argument_stack_space + result_stack_space;
2144 subq(rsp, Immediate(total_stack_space));
2145#endif
2146
2147 // Get the required frame alignment for the OS.
2148 static const int kFrameAlignment = OS::ActivationFrameAlignment();
2149 if (kFrameAlignment > 0) {
2150 ASSERT(IsPowerOf2(kFrameAlignment));
2151 movq(kScratchRegister, Immediate(-kFrameAlignment));
2152 and_(rsp, kScratchRegister);
2153 }
2154
2155 // Patch the saved entry sp.
2156 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
2157}
2158
2159
Steve Blockd0582a62009-12-15 09:54:21 +00002160void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode, int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002161 // Registers:
2162 // r15 : argv
2163#ifdef ENABLE_DEBUGGER_SUPPORT
2164 // Restore the memory copy of the registers by digging them out from
2165 // the stack. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +00002166 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002167 // It's okay to clobber register rbx below because we don't need
2168 // the function pointer after this.
2169 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00002170 int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002171 lea(rbx, Operand(rbp, kOffset));
2172 CopyRegistersFromStackToMemory(rbx, rcx, kJSCallerSaved);
2173 }
2174#endif
2175
2176 // Get the return address from the stack and restore the frame pointer.
2177 movq(rcx, Operand(rbp, 1 * kPointerSize));
2178 movq(rbp, Operand(rbp, 0 * kPointerSize));
2179
Steve Blocka7e24c12009-10-30 11:49:00 +00002180 // Pop everything up to and including the arguments and the receiver
2181 // from the caller stack.
2182 lea(rsp, Operand(r15, 1 * kPointerSize));
2183
2184 // Restore current context from top and clear it in debug mode.
2185 ExternalReference context_address(Top::k_context_address);
2186 movq(kScratchRegister, context_address);
2187 movq(rsi, Operand(kScratchRegister, 0));
2188#ifdef DEBUG
2189 movq(Operand(kScratchRegister, 0), Immediate(0));
2190#endif
2191
2192 // Push the return address to get ready to return.
2193 push(rcx);
2194
2195 // Clear the top frame.
2196 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
2197 movq(kScratchRegister, c_entry_fp_address);
2198 movq(Operand(kScratchRegister, 0), Immediate(0));
2199}
2200
2201
Steve Block3ce2e202009-11-05 08:53:23 +00002202Register MacroAssembler::CheckMaps(JSObject* object,
2203 Register object_reg,
2204 JSObject* holder,
2205 Register holder_reg,
Steve Blocka7e24c12009-10-30 11:49:00 +00002206 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01002207 int save_at_depth,
Steve Blocka7e24c12009-10-30 11:49:00 +00002208 Label* miss) {
2209 // Make sure there's no overlap between scratch and the other
2210 // registers.
2211 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
2212
2213 // Keep track of the current object in register reg. On the first
2214 // iteration, reg is an alias for object_reg, on later iterations,
2215 // it is an alias for holder_reg.
2216 Register reg = object_reg;
Steve Block6ded16b2010-05-10 14:33:55 +01002217 int depth = 0;
2218
2219 if (save_at_depth == depth) {
2220 movq(Operand(rsp, kPointerSize), object_reg);
2221 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002222
2223 // Check the maps in the prototype chain.
2224 // Traverse the prototype chain from the object and do map checks.
2225 while (object != holder) {
2226 depth++;
2227
2228 // Only global objects and objects that do not require access
2229 // checks are allowed in stubs.
2230 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
2231
2232 JSObject* prototype = JSObject::cast(object->GetPrototype());
2233 if (Heap::InNewSpace(prototype)) {
2234 // Get the map of the current object.
2235 movq(scratch, FieldOperand(reg, HeapObject::kMapOffset));
2236 Cmp(scratch, Handle<Map>(object->map()));
2237 // Branch on the result of the map check.
2238 j(not_equal, miss);
2239 // Check access rights to the global object. This has to happen
2240 // after the map check so that we know that the object is
2241 // actually a global object.
2242 if (object->IsJSGlobalProxy()) {
2243 CheckAccessGlobalProxy(reg, scratch, miss);
2244
2245 // Restore scratch register to be the map of the object.
2246 // We load the prototype from the map in the scratch register.
2247 movq(scratch, FieldOperand(reg, HeapObject::kMapOffset));
2248 }
2249 // The prototype is in new space; we cannot store a reference
2250 // to it in the code. Load it from the map.
2251 reg = holder_reg; // from now the object is in holder_reg
2252 movq(reg, FieldOperand(scratch, Map::kPrototypeOffset));
2253
2254 } else {
2255 // Check the map of the current object.
2256 Cmp(FieldOperand(reg, HeapObject::kMapOffset),
2257 Handle<Map>(object->map()));
2258 // Branch on the result of the map check.
2259 j(not_equal, miss);
2260 // Check access rights to the global object. This has to happen
2261 // after the map check so that we know that the object is
2262 // actually a global object.
2263 if (object->IsJSGlobalProxy()) {
2264 CheckAccessGlobalProxy(reg, scratch, miss);
2265 }
2266 // The prototype is in old space; load it directly.
2267 reg = holder_reg; // from now the object is in holder_reg
2268 Move(reg, Handle<JSObject>(prototype));
2269 }
2270
Steve Block6ded16b2010-05-10 14:33:55 +01002271 if (save_at_depth == depth) {
2272 movq(Operand(rsp, kPointerSize), reg);
2273 }
2274
Steve Blocka7e24c12009-10-30 11:49:00 +00002275 // Go to the next object in the prototype chain.
2276 object = prototype;
2277 }
2278
2279 // Check the holder map.
Steve Block3ce2e202009-11-05 08:53:23 +00002280 Cmp(FieldOperand(reg, HeapObject::kMapOffset), Handle<Map>(holder->map()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002281 j(not_equal, miss);
2282
2283 // Log the check depth.
Steve Block6ded16b2010-05-10 14:33:55 +01002284 LOG(IntEvent("check-maps-depth", depth + 1));
Steve Blocka7e24c12009-10-30 11:49:00 +00002285
2286 // Perform security check for access to the global object and return
2287 // the holder register.
2288 ASSERT(object == holder);
2289 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
2290 if (object->IsJSGlobalProxy()) {
2291 CheckAccessGlobalProxy(reg, scratch, miss);
2292 }
2293 return reg;
2294}
2295
2296
Steve Blocka7e24c12009-10-30 11:49:00 +00002297void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
2298 Register scratch,
2299 Label* miss) {
2300 Label same_contexts;
2301
2302 ASSERT(!holder_reg.is(scratch));
2303 ASSERT(!scratch.is(kScratchRegister));
2304 // Load current lexical context from the stack frame.
2305 movq(scratch, Operand(rbp, StandardFrameConstants::kContextOffset));
2306
2307 // When generating debug code, make sure the lexical context is set.
2308 if (FLAG_debug_code) {
2309 cmpq(scratch, Immediate(0));
2310 Check(not_equal, "we should not have an empty lexical context");
2311 }
2312 // Load the global context of the current context.
2313 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
2314 movq(scratch, FieldOperand(scratch, offset));
2315 movq(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
2316
2317 // Check the context is a global context.
2318 if (FLAG_debug_code) {
2319 Cmp(FieldOperand(scratch, HeapObject::kMapOffset),
2320 Factory::global_context_map());
2321 Check(equal, "JSGlobalObject::global_context should be a global context.");
2322 }
2323
2324 // Check if both contexts are the same.
2325 cmpq(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
2326 j(equal, &same_contexts);
2327
2328 // Compare security tokens.
2329 // Check that the security token in the calling global object is
2330 // compatible with the security token in the receiving global
2331 // object.
2332
2333 // Check the context is a global context.
2334 if (FLAG_debug_code) {
2335 // Preserve original value of holder_reg.
2336 push(holder_reg);
2337 movq(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
2338 CompareRoot(holder_reg, Heap::kNullValueRootIndex);
2339 Check(not_equal, "JSGlobalProxy::context() should not be null.");
2340
2341 // Read the first word and compare to global_context_map(),
2342 movq(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
2343 CompareRoot(holder_reg, Heap::kGlobalContextMapRootIndex);
2344 Check(equal, "JSGlobalObject::global_context should be a global context.");
2345 pop(holder_reg);
2346 }
2347
2348 movq(kScratchRegister,
2349 FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
Steve Block3ce2e202009-11-05 08:53:23 +00002350 int token_offset =
2351 Context::kHeaderSize + Context::SECURITY_TOKEN_INDEX * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002352 movq(scratch, FieldOperand(scratch, token_offset));
2353 cmpq(scratch, FieldOperand(kScratchRegister, token_offset));
2354 j(not_equal, miss);
2355
2356 bind(&same_contexts);
2357}
2358
2359
2360void MacroAssembler::LoadAllocationTopHelper(Register result,
2361 Register result_end,
2362 Register scratch,
2363 AllocationFlags flags) {
2364 ExternalReference new_space_allocation_top =
2365 ExternalReference::new_space_allocation_top_address();
2366
2367 // Just return if allocation top is already known.
2368 if ((flags & RESULT_CONTAINS_TOP) != 0) {
2369 // No use of scratch if allocation top is provided.
Steve Block6ded16b2010-05-10 14:33:55 +01002370 ASSERT(!scratch.is_valid());
Steve Blocka7e24c12009-10-30 11:49:00 +00002371#ifdef DEBUG
2372 // Assert that result actually contains top on entry.
2373 movq(kScratchRegister, new_space_allocation_top);
2374 cmpq(result, Operand(kScratchRegister, 0));
2375 Check(equal, "Unexpected allocation top");
2376#endif
2377 return;
2378 }
2379
Steve Block6ded16b2010-05-10 14:33:55 +01002380 // Move address of new object to result. Use scratch register if available,
2381 // and keep address in scratch until call to UpdateAllocationTopHelper.
2382 if (scratch.is_valid()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002383 ASSERT(!scratch.is(result_end));
2384 movq(scratch, new_space_allocation_top);
2385 movq(result, Operand(scratch, 0));
Steve Block6ded16b2010-05-10 14:33:55 +01002386 } else if (result.is(rax)) {
2387 load_rax(new_space_allocation_top);
2388 } else {
2389 movq(kScratchRegister, new_space_allocation_top);
2390 movq(result, Operand(kScratchRegister, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00002391 }
2392}
2393
2394
2395void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
2396 Register scratch) {
Steve Blockd0582a62009-12-15 09:54:21 +00002397 if (FLAG_debug_code) {
2398 testq(result_end, Immediate(kObjectAlignmentMask));
2399 Check(zero, "Unaligned allocation in new space");
2400 }
2401
Steve Blocka7e24c12009-10-30 11:49:00 +00002402 ExternalReference new_space_allocation_top =
2403 ExternalReference::new_space_allocation_top_address();
2404
2405 // Update new top.
2406 if (result_end.is(rax)) {
2407 // rax can be stored directly to a memory location.
2408 store_rax(new_space_allocation_top);
2409 } else {
2410 // Register required - use scratch provided if available.
Steve Block6ded16b2010-05-10 14:33:55 +01002411 if (scratch.is_valid()) {
2412 movq(Operand(scratch, 0), result_end);
2413 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00002414 movq(kScratchRegister, new_space_allocation_top);
2415 movq(Operand(kScratchRegister, 0), result_end);
Steve Blocka7e24c12009-10-30 11:49:00 +00002416 }
2417 }
2418}
2419
2420
2421void MacroAssembler::AllocateInNewSpace(int object_size,
2422 Register result,
2423 Register result_end,
2424 Register scratch,
2425 Label* gc_required,
2426 AllocationFlags flags) {
2427 ASSERT(!result.is(result_end));
2428
2429 // Load address of new object into result.
2430 LoadAllocationTopHelper(result, result_end, scratch, flags);
2431
2432 // Calculate new top and bail out if new space is exhausted.
2433 ExternalReference new_space_allocation_limit =
2434 ExternalReference::new_space_allocation_limit_address();
Steve Block6ded16b2010-05-10 14:33:55 +01002435
2436 Register top_reg = result_end.is_valid() ? result_end : result;
2437
2438 if (top_reg.is(result)) {
2439 addq(top_reg, Immediate(object_size));
2440 } else {
2441 lea(top_reg, Operand(result, object_size));
2442 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002443 movq(kScratchRegister, new_space_allocation_limit);
Steve Block6ded16b2010-05-10 14:33:55 +01002444 cmpq(top_reg, Operand(kScratchRegister, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00002445 j(above, gc_required);
2446
2447 // Update allocation top.
Steve Block6ded16b2010-05-10 14:33:55 +01002448 UpdateAllocationTopHelper(top_reg, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +00002449
Steve Block6ded16b2010-05-10 14:33:55 +01002450 if (top_reg.is(result)) {
2451 if ((flags & TAG_OBJECT) != 0) {
2452 subq(result, Immediate(object_size - kHeapObjectTag));
2453 } else {
2454 subq(result, Immediate(object_size));
2455 }
2456 } else if ((flags & TAG_OBJECT) != 0) {
2457 // Tag the result if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00002458 addq(result, Immediate(kHeapObjectTag));
2459 }
2460}
2461
2462
2463void MacroAssembler::AllocateInNewSpace(int header_size,
2464 ScaleFactor element_size,
2465 Register element_count,
2466 Register result,
2467 Register result_end,
2468 Register scratch,
2469 Label* gc_required,
2470 AllocationFlags flags) {
2471 ASSERT(!result.is(result_end));
2472
2473 // Load address of new object into result.
2474 LoadAllocationTopHelper(result, result_end, scratch, flags);
2475
2476 // Calculate new top and bail out if new space is exhausted.
2477 ExternalReference new_space_allocation_limit =
2478 ExternalReference::new_space_allocation_limit_address();
2479 lea(result_end, Operand(result, element_count, element_size, header_size));
2480 movq(kScratchRegister, new_space_allocation_limit);
2481 cmpq(result_end, Operand(kScratchRegister, 0));
2482 j(above, gc_required);
2483
2484 // Update allocation top.
2485 UpdateAllocationTopHelper(result_end, scratch);
2486
2487 // Tag the result if requested.
2488 if ((flags & TAG_OBJECT) != 0) {
2489 addq(result, Immediate(kHeapObjectTag));
2490 }
2491}
2492
2493
2494void MacroAssembler::AllocateInNewSpace(Register object_size,
2495 Register result,
2496 Register result_end,
2497 Register scratch,
2498 Label* gc_required,
2499 AllocationFlags flags) {
2500 // Load address of new object into result.
2501 LoadAllocationTopHelper(result, result_end, scratch, flags);
2502
2503 // Calculate new top and bail out if new space is exhausted.
2504 ExternalReference new_space_allocation_limit =
2505 ExternalReference::new_space_allocation_limit_address();
2506 if (!object_size.is(result_end)) {
2507 movq(result_end, object_size);
2508 }
2509 addq(result_end, result);
2510 movq(kScratchRegister, new_space_allocation_limit);
2511 cmpq(result_end, Operand(kScratchRegister, 0));
2512 j(above, gc_required);
2513
2514 // Update allocation top.
2515 UpdateAllocationTopHelper(result_end, scratch);
2516
2517 // Tag the result if requested.
2518 if ((flags & TAG_OBJECT) != 0) {
2519 addq(result, Immediate(kHeapObjectTag));
2520 }
2521}
2522
2523
2524void MacroAssembler::UndoAllocationInNewSpace(Register object) {
2525 ExternalReference new_space_allocation_top =
2526 ExternalReference::new_space_allocation_top_address();
2527
2528 // Make sure the object has no tag before resetting top.
2529 and_(object, Immediate(~kHeapObjectTagMask));
2530 movq(kScratchRegister, new_space_allocation_top);
2531#ifdef DEBUG
2532 cmpq(object, Operand(kScratchRegister, 0));
2533 Check(below, "Undo allocation of non allocated memory");
2534#endif
2535 movq(Operand(kScratchRegister, 0), object);
2536}
2537
2538
Steve Block3ce2e202009-11-05 08:53:23 +00002539void MacroAssembler::AllocateHeapNumber(Register result,
2540 Register scratch,
2541 Label* gc_required) {
2542 // Allocate heap number in new space.
2543 AllocateInNewSpace(HeapNumber::kSize,
2544 result,
2545 scratch,
2546 no_reg,
2547 gc_required,
2548 TAG_OBJECT);
2549
2550 // Set the map.
2551 LoadRoot(kScratchRegister, Heap::kHeapNumberMapRootIndex);
2552 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2553}
2554
2555
Leon Clarkee46be812010-01-19 14:06:41 +00002556void MacroAssembler::AllocateTwoByteString(Register result,
2557 Register length,
2558 Register scratch1,
2559 Register scratch2,
2560 Register scratch3,
2561 Label* gc_required) {
2562 // Calculate the number of bytes needed for the characters in the string while
2563 // observing object alignment.
Steve Block6ded16b2010-05-10 14:33:55 +01002564 const int kHeaderAlignment = SeqTwoByteString::kHeaderSize &
2565 kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +00002566 ASSERT(kShortSize == 2);
2567 // scratch1 = length * 2 + kObjectAlignmentMask.
Steve Block6ded16b2010-05-10 14:33:55 +01002568 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask +
2569 kHeaderAlignment));
Leon Clarkee46be812010-01-19 14:06:41 +00002570 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Block6ded16b2010-05-10 14:33:55 +01002571 if (kHeaderAlignment > 0) {
2572 subq(scratch1, Immediate(kHeaderAlignment));
2573 }
Leon Clarkee46be812010-01-19 14:06:41 +00002574
2575 // Allocate two byte string in new space.
2576 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
2577 times_1,
2578 scratch1,
2579 result,
2580 scratch2,
2581 scratch3,
2582 gc_required,
2583 TAG_OBJECT);
2584
2585 // Set the map, length and hash field.
2586 LoadRoot(kScratchRegister, Heap::kStringMapRootIndex);
2587 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
Steve Block6ded16b2010-05-10 14:33:55 +01002588 Integer32ToSmi(scratch1, length);
2589 movq(FieldOperand(result, String::kLengthOffset), scratch1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002590 movq(FieldOperand(result, String::kHashFieldOffset),
Leon Clarkee46be812010-01-19 14:06:41 +00002591 Immediate(String::kEmptyHashField));
2592}
2593
2594
2595void MacroAssembler::AllocateAsciiString(Register result,
2596 Register length,
2597 Register scratch1,
2598 Register scratch2,
2599 Register scratch3,
2600 Label* gc_required) {
2601 // Calculate the number of bytes needed for the characters in the string while
2602 // observing object alignment.
Steve Block6ded16b2010-05-10 14:33:55 +01002603 const int kHeaderAlignment = SeqAsciiString::kHeaderSize &
2604 kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +00002605 movl(scratch1, length);
2606 ASSERT(kCharSize == 1);
Steve Block6ded16b2010-05-10 14:33:55 +01002607 addq(scratch1, Immediate(kObjectAlignmentMask + kHeaderAlignment));
Leon Clarkee46be812010-01-19 14:06:41 +00002608 and_(scratch1, Immediate(~kObjectAlignmentMask));
Steve Block6ded16b2010-05-10 14:33:55 +01002609 if (kHeaderAlignment > 0) {
2610 subq(scratch1, Immediate(kHeaderAlignment));
2611 }
Leon Clarkee46be812010-01-19 14:06:41 +00002612
2613 // Allocate ascii string in new space.
2614 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
2615 times_1,
2616 scratch1,
2617 result,
2618 scratch2,
2619 scratch3,
2620 gc_required,
2621 TAG_OBJECT);
2622
2623 // Set the map, length and hash field.
2624 LoadRoot(kScratchRegister, Heap::kAsciiStringMapRootIndex);
2625 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
Steve Block6ded16b2010-05-10 14:33:55 +01002626 Integer32ToSmi(scratch1, length);
2627 movq(FieldOperand(result, String::kLengthOffset), scratch1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002628 movq(FieldOperand(result, String::kHashFieldOffset),
Leon Clarkee46be812010-01-19 14:06:41 +00002629 Immediate(String::kEmptyHashField));
2630}
2631
2632
2633void MacroAssembler::AllocateConsString(Register result,
2634 Register scratch1,
2635 Register scratch2,
2636 Label* gc_required) {
2637 // Allocate heap number in new space.
2638 AllocateInNewSpace(ConsString::kSize,
2639 result,
2640 scratch1,
2641 scratch2,
2642 gc_required,
2643 TAG_OBJECT);
2644
2645 // Set the map. The other fields are left uninitialized.
2646 LoadRoot(kScratchRegister, Heap::kConsStringMapRootIndex);
2647 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2648}
2649
2650
2651void MacroAssembler::AllocateAsciiConsString(Register result,
2652 Register scratch1,
2653 Register scratch2,
2654 Label* gc_required) {
2655 // Allocate heap number in new space.
2656 AllocateInNewSpace(ConsString::kSize,
2657 result,
2658 scratch1,
2659 scratch2,
2660 gc_required,
2661 TAG_OBJECT);
2662
2663 // Set the map. The other fields are left uninitialized.
2664 LoadRoot(kScratchRegister, Heap::kConsAsciiStringMapRootIndex);
2665 movq(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
2666}
2667
2668
Steve Blockd0582a62009-12-15 09:54:21 +00002669void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2670 if (context_chain_length > 0) {
2671 // Move up the chain of contexts to the context containing the slot.
2672 movq(dst, Operand(rsi, Context::SlotOffset(Context::CLOSURE_INDEX)));
2673 // Load the function context (which is the incoming, outer context).
Leon Clarkee46be812010-01-19 14:06:41 +00002674 movq(dst, FieldOperand(dst, JSFunction::kContextOffset));
Steve Blockd0582a62009-12-15 09:54:21 +00002675 for (int i = 1; i < context_chain_length; i++) {
2676 movq(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
2677 movq(dst, FieldOperand(dst, JSFunction::kContextOffset));
2678 }
2679 // The context may be an intermediate context, not a function context.
2680 movq(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2681 } else { // context is the current function context.
2682 // The context may be an intermediate context, not a function context.
2683 movq(dst, Operand(rsi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2684 }
2685}
2686
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002687
Leon Clarke4515c472010-02-03 11:58:03 +00002688int MacroAssembler::ArgumentStackSlotsForCFunctionCall(int num_arguments) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002689 // On Windows 64 stack slots are reserved by the caller for all arguments
2690 // including the ones passed in registers, and space is always allocated for
2691 // the four register arguments even if the function takes fewer than four
2692 // arguments.
2693 // On AMD64 ABI (Linux/Mac) the first six arguments are passed in registers
2694 // and the caller does not reserve stack slots for them.
Leon Clarke4515c472010-02-03 11:58:03 +00002695 ASSERT(num_arguments >= 0);
2696#ifdef _WIN64
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002697 static const int kMinimumStackSlots = 4;
2698 if (num_arguments < kMinimumStackSlots) return kMinimumStackSlots;
2699 return num_arguments;
Leon Clarke4515c472010-02-03 11:58:03 +00002700#else
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002701 static const int kRegisterPassedArguments = 6;
2702 if (num_arguments < kRegisterPassedArguments) return 0;
2703 return num_arguments - kRegisterPassedArguments;
Leon Clarke4515c472010-02-03 11:58:03 +00002704#endif
Leon Clarke4515c472010-02-03 11:58:03 +00002705}
2706
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002707
Leon Clarke4515c472010-02-03 11:58:03 +00002708void MacroAssembler::PrepareCallCFunction(int num_arguments) {
2709 int frame_alignment = OS::ActivationFrameAlignment();
2710 ASSERT(frame_alignment != 0);
2711 ASSERT(num_arguments >= 0);
2712 // Make stack end at alignment and allocate space for arguments and old rsp.
2713 movq(kScratchRegister, rsp);
2714 ASSERT(IsPowerOf2(frame_alignment));
2715 int argument_slots_on_stack =
2716 ArgumentStackSlotsForCFunctionCall(num_arguments);
2717 subq(rsp, Immediate((argument_slots_on_stack + 1) * kPointerSize));
2718 and_(rsp, Immediate(-frame_alignment));
2719 movq(Operand(rsp, argument_slots_on_stack * kPointerSize), kScratchRegister);
2720}
2721
2722
2723void MacroAssembler::CallCFunction(ExternalReference function,
2724 int num_arguments) {
2725 movq(rax, function);
2726 CallCFunction(rax, num_arguments);
2727}
2728
2729
2730void MacroAssembler::CallCFunction(Register function, int num_arguments) {
Steve Block6ded16b2010-05-10 14:33:55 +01002731 // Check stack alignment.
2732 if (FLAG_debug_code) {
2733 CheckStackAlignment();
2734 }
2735
Leon Clarke4515c472010-02-03 11:58:03 +00002736 call(function);
2737 ASSERT(OS::ActivationFrameAlignment() != 0);
2738 ASSERT(num_arguments >= 0);
2739 int argument_slots_on_stack =
2740 ArgumentStackSlotsForCFunctionCall(num_arguments);
2741 movq(rsp, Operand(rsp, argument_slots_on_stack * kPointerSize));
2742}
2743
Steve Blockd0582a62009-12-15 09:54:21 +00002744
Steve Blocka7e24c12009-10-30 11:49:00 +00002745CodePatcher::CodePatcher(byte* address, int size)
2746 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
2747 // Create a new macro assembler pointing to the address of the code to patch.
2748 // The size is adjusted with kGap on order for the assembler to generate size
2749 // bytes of instructions without failing with buffer size constraints.
2750 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2751}
2752
2753
2754CodePatcher::~CodePatcher() {
2755 // Indicate that code has changed.
2756 CPU::FlushICache(address_, size_);
2757
2758 // Check that the code was patched as expected.
2759 ASSERT(masm_.pc_ == address_ + size_);
2760 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2761}
2762
Steve Blocka7e24c12009-10-30 11:49:00 +00002763} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002764
2765#endif // V8_TARGET_ARCH_X64