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