blob: f6a5ffecc79778871779c36dde11607052ca7258 [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"
ager@chromium.orgeadaf222009-06-16 09:43:10 +000034#include "debug.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035
36namespace v8 {
37namespace internal {
38
39MacroAssembler::MacroAssembler(void* buffer, int size)
40 : Assembler(buffer, size),
41 unresolved_(0),
42 generating_stub_(false),
43 allow_stub_calls_(true),
44 code_object_(Heap::undefined_value()) {
45}
46
ager@chromium.orge2902be2009-06-08 12:21:35 +000047
ager@chromium.org5aa501c2009-06-23 07:57:28 +000048// TODO(x64): For now, the write barrier is disabled on x64 and we
49// therefore generate no code. This should be fixed when the write
50// barrier is enabled.
51void MacroAssembler::RecordWrite(Register object, int offset,
52 Register value, Register scratch) {
53}
54
55
ager@chromium.orgeadaf222009-06-16 09:43:10 +000056void MacroAssembler::Assert(Condition cc, const char* msg) {
57 if (FLAG_debug_code) Check(cc, msg);
58}
59
60
61void MacroAssembler::Check(Condition cc, const char* msg) {
62 Label L;
63 j(cc, &L);
64 Abort(msg);
65 // will not return here
66 bind(&L);
67}
68
69
ager@chromium.org5aa501c2009-06-23 07:57:28 +000070void MacroAssembler::NegativeZeroTest(Register result,
71 Register op,
72 Label* then_label) {
73 Label ok;
74 testq(result, result);
75 j(not_zero, &ok);
76 testq(op, op);
77 j(sign, then_label);
78 bind(&ok);
79}
80
81
ager@chromium.orgeadaf222009-06-16 09:43:10 +000082void MacroAssembler::ConstructAndTestJSFunction() {
83 const int initial_buffer_size = 4 * KB;
84 char* buffer = new char[initial_buffer_size];
85 MacroAssembler masm(buffer, initial_buffer_size);
86
87 const uint64_t secret = V8_INT64_C(0xdeadbeefcafebabe);
88 Handle<String> constant =
89 Factory::NewStringFromAscii(Vector<const char>("451", 3), TENURED);
90#define __ ACCESS_MASM((&masm))
91 // Construct a simple JSfunction here, using Assembler and MacroAssembler
92 // commands.
93 __ movq(rax, constant, RelocInfo::EMBEDDED_OBJECT);
94 __ push(rax);
95 __ CallRuntime(Runtime::kStringParseFloat, 1);
96 __ movq(kScratchRegister, secret, RelocInfo::NONE);
97 __ addq(rax, kScratchRegister);
98 __ ret(0);
99#undef __
100 CodeDesc desc;
101 masm.GetCode(&desc);
102 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
103 Object* code = Heap::CreateCode(desc, NULL, flags, Handle<Object>::null());
104 if (!code->IsFailure()) {
105 Handle<Code> code_handle(Code::cast(code));
106 Handle<String> name =
107 Factory::NewStringFromAscii(Vector<const char>("foo", 3), NOT_TENURED);
108 Handle<JSFunction> function =
109 Factory::NewFunction(name,
110 JS_FUNCTION_TYPE,
111 JSObject::kHeaderSize,
112 code_handle,
113 true);
114 bool pending_exceptions;
115 Handle<Object> result =
116 Execution::Call(function,
117 Handle<Object>::cast(function),
118 0,
119 NULL,
120 &pending_exceptions);
121 CHECK(result->IsSmi());
122 CHECK(secret + (451 << kSmiTagSize) == reinterpret_cast<uint64_t>(*result));
123 }
124}
125
126
127void MacroAssembler::Abort(const char* msg) {
128 // We want to pass the msg string like a smi to avoid GC
129 // problems, however msg is not guaranteed to be aligned
130 // properly. Instead, we pass an aligned pointer that is
131 // a proper v8 smi, but also pass the alignment difference
132 // from the real pointer as a smi.
133 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
134 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
135 // Note: p0 might not be a valid Smi *value*, but it has a valid Smi tag.
136 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
137#ifdef DEBUG
138 if (msg != NULL) {
139 RecordComment("Abort message: ");
140 RecordComment(msg);
141 }
142#endif
143 push(rax);
144 movq(kScratchRegister, p0, RelocInfo::NONE);
145 push(kScratchRegister);
146 movq(kScratchRegister,
147 reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0)),
148 RelocInfo::NONE);
149 push(kScratchRegister);
150 CallRuntime(Runtime::kAbort, 2);
151 // will not return here
152}
153
154
155void MacroAssembler::CallStub(CodeStub* stub) {
156 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
157 movq(kScratchRegister, stub->GetCode(), RelocInfo::CODE_TARGET);
158 call(kScratchRegister);
159}
160
161
162void MacroAssembler::StubReturn(int argc) {
163 ASSERT(argc >= 1 && generating_stub());
164 ret((argc - 1) * kPointerSize);
165}
166
167
168void MacroAssembler::IllegalOperation(int num_arguments) {
169 if (num_arguments > 0) {
170 addq(rsp, Immediate(num_arguments * kPointerSize));
171 }
172 movq(rax, Factory::undefined_value(), RelocInfo::EMBEDDED_OBJECT);
173}
174
175
176void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
177 CallRuntime(Runtime::FunctionForId(id), num_arguments);
178}
179
180
181void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
182 // If the expected number of arguments of the runtime function is
183 // constant, we check that the actual number of arguments match the
184 // expectation.
185 if (f->nargs >= 0 && f->nargs != num_arguments) {
186 IllegalOperation(num_arguments);
187 return;
188 }
189
190 Runtime::FunctionId function_id =
191 static_cast<Runtime::FunctionId>(f->stub_id);
192 RuntimeStub stub(function_id, num_arguments);
193 CallStub(&stub);
194}
195
196
197void MacroAssembler::TailCallRuntime(ExternalReference const& ext,
198 int num_arguments) {
199 // TODO(1236192): Most runtime routines don't need the number of
200 // arguments passed in because it is constant. At some point we
201 // should remove this need and make the runtime routine entry code
202 // smarter.
203 movq(rax, Immediate(num_arguments));
204 JumpToBuiltin(ext);
205}
206
207
208void MacroAssembler::JumpToBuiltin(const ExternalReference& ext) {
209 // Set the entry point and jump to the C entry runtime stub.
210 movq(rbx, ext);
211 CEntryStub ces;
212 movq(kScratchRegister, ces.GetCode(), RelocInfo::CODE_TARGET);
213 jmp(kScratchRegister);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000214}
215
ager@chromium.orge2902be2009-06-08 12:21:35 +0000216
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000217void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
218 bool resolved;
219 Handle<Code> code = ResolveBuiltin(id, &resolved);
220
221 const char* name = Builtins::GetName(id);
222 int argc = Builtins::GetArgumentsCount(id);
223
224 movq(target, code, RelocInfo::EXTERNAL_REFERENCE); // Is external reference?
225 if (!resolved) {
226 uint32_t flags =
227 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
228 Bootstrapper::FixupFlagsIsPCRelative::encode(false) |
229 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
230 Unresolved entry = { pc_offset() - sizeof(intptr_t), flags, name };
231 unresolved_.Add(entry);
232 }
233 addq(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
234}
235
236
237Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
238 bool* resolved) {
239 // Move the builtin function into the temporary function slot by
240 // reading it from the builtins object. NOTE: We should be able to
241 // reduce this to two instructions by putting the function table in
242 // the global object instead of the "builtins" object and by using a
243 // real register for the function.
244 movq(rdx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
245 movq(rdx, FieldOperand(rdx, GlobalObject::kBuiltinsOffset));
246 int builtins_offset =
247 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
248 movq(rdi, FieldOperand(rdx, builtins_offset));
249
250
251 return Builtins::GetCode(id, resolved);
252}
253
254
ager@chromium.orge2902be2009-06-08 12:21:35 +0000255void MacroAssembler::Set(Register dst, int64_t x) {
256 if (is_int32(x)) {
257 movq(dst, Immediate(x));
258 } else if (is_uint32(x)) {
259 movl(dst, Immediate(x));
260 } else {
261 movq(dst, x, RelocInfo::NONE);
262 }
263}
264
265
266void MacroAssembler::Set(const Operand& dst, int64_t x) {
267 if (is_int32(x)) {
268 movq(kScratchRegister, Immediate(x));
269 } else if (is_uint32(x)) {
270 movl(kScratchRegister, Immediate(x));
271 } else {
272 movq(kScratchRegister, x, RelocInfo::NONE);
273 }
274 movq(dst, kScratchRegister);
275}
276
277
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000278bool MacroAssembler::IsUnsafeSmi(Smi* value) {
279 return false;
280}
281
282void MacroAssembler::LoadUnsafeSmi(Register dst, Smi* source) {
283 UNIMPLEMENTED();
284}
285
286
287void MacroAssembler::Move(Register dst, Handle<Object> source) {
288 if (source->IsSmi()) {
289 if (IsUnsafeSmi(source)) {
290 LoadUnsafeSmi(dst, source);
291 } else {
292 movq(dst, source, RelocInfo::NONE);
293 }
294 } else {
295 movq(dst, source, RelocInfo::EMBEDDED_OBJECT);
296 }
297}
298
299
300void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
301 Move(kScratchRegister, source);
302 movq(dst, kScratchRegister);
303}
304
305
306void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
307 Move(kScratchRegister, source);
308 cmpq(dst, kScratchRegister);
309}
310
311
ager@chromium.org3e875802009-06-29 08:26:34 +0000312void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
313 Move(kScratchRegister, source);
314 cmpq(dst, kScratchRegister);
315}
316
317
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000318void MacroAssembler::Push(Handle<Object> source) {
319 Move(kScratchRegister, source);
320 push(kScratchRegister);
321}
322
323
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000324void MacroAssembler::Jump(ExternalReference ext) {
325 movq(kScratchRegister, ext);
326 jmp(kScratchRegister);
327}
328
329
330void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
331 movq(kScratchRegister, destination, rmode);
332 jmp(kScratchRegister);
333}
334
335
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000336void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
337 WriteRecordedPositions();
338 ASSERT(RelocInfo::IsCodeTarget(rmode));
339 movq(kScratchRegister, code_object, rmode);
ager@chromium.org3e875802009-06-29 08:26:34 +0000340#ifdef DEBUG
341 Label target;
342 bind(&target);
343#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000344 jmp(kScratchRegister);
ager@chromium.org3e875802009-06-29 08:26:34 +0000345#ifdef DEBUG
346 ASSERT_EQ(kTargetAddrToReturnAddrDist,
347 SizeOfCodeGeneratedSince(&target) + kPointerSize);
348#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000349}
350
351
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000352void MacroAssembler::Call(ExternalReference ext) {
353 movq(kScratchRegister, ext);
354 call(kScratchRegister);
355}
356
357
358void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
359 movq(kScratchRegister, destination, rmode);
360 call(kScratchRegister);
361}
362
363
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000364void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
365 WriteRecordedPositions();
366 ASSERT(RelocInfo::IsCodeTarget(rmode));
367 movq(kScratchRegister, code_object, rmode);
368#ifdef DEBUG
369 Label target;
370 bind(&target);
371#endif
372 call(kScratchRegister);
373#ifdef DEBUG
374 ASSERT_EQ(kTargetAddrToReturnAddrDist,
375 SizeOfCodeGeneratedSince(&target) + kPointerSize);
376#endif
377}
378
379
ager@chromium.orge2902be2009-06-08 12:21:35 +0000380void MacroAssembler::PushTryHandler(CodeLocation try_location,
381 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000382 // Adjust this code if not the case.
383 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
384
385 // The pc (return address) is already on TOS. This code pushes state,
386 // frame pointer and current handler. Check that they are expected
387 // next on the stack, in that order.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000388 ASSERT_EQ(StackHandlerConstants::kStateOffset,
389 StackHandlerConstants::kPCOffset - kPointerSize);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000390 ASSERT_EQ(StackHandlerConstants::kFPOffset,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000391 StackHandlerConstants::kStateOffset - kPointerSize);
392 ASSERT_EQ(StackHandlerConstants::kNextOffset,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000393 StackHandlerConstants::kFPOffset - kPointerSize);
394
395 if (try_location == IN_JAVASCRIPT) {
396 if (type == TRY_CATCH_HANDLER) {
397 push(Immediate(StackHandler::TRY_CATCH));
398 } else {
399 push(Immediate(StackHandler::TRY_FINALLY));
400 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000401 push(rbp);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000402 } else {
403 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000404 // The frame pointer does not point to a JS frame so we save NULL
405 // for rbp. We expect the code throwing an exception to check rbp
406 // before dereferencing it to restore the context.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000407 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000408 push(Immediate(0)); // NULL frame pointer.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000409 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000410 // Save the current handler.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000411 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000412 push(Operand(kScratchRegister, 0));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000413 // Link this handler.
414 movq(Operand(kScratchRegister, 0), rsp);
415}
416
417
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000418void MacroAssembler::Ret() {
419 ret(0);
420}
421
422
ager@chromium.org3e875802009-06-29 08:26:34 +0000423void MacroAssembler::FCmp() {
424 fcompp();
425 push(rax);
426 fnstsw_ax();
427 // TODO(X64): Check that sahf is safe to use, using CPUProbe.
428 sahf();
429 pop(rax);
430}
431
432
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000433void MacroAssembler::CmpObjectType(Register heap_object,
434 InstanceType type,
435 Register map) {
436 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
437 CmpInstanceType(map, type);
438}
439
440
441void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
442 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
443 Immediate(static_cast<int8_t>(type)));
444}
445
446
447
448void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
449 if (FLAG_native_code_counters && counter->Enabled()) {
450 movq(kScratchRegister, ExternalReference(counter));
451 movl(Operand(kScratchRegister, 0), Immediate(value));
452 }
453}
454
455
456void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
457 ASSERT(value > 0);
458 if (FLAG_native_code_counters && counter->Enabled()) {
459 movq(kScratchRegister, ExternalReference(counter));
460 Operand operand(kScratchRegister, 0);
461 if (value == 1) {
462 incl(operand);
463 } else {
464 addl(operand, Immediate(value));
465 }
466 }
467}
468
469
470void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
471 ASSERT(value > 0);
472 if (FLAG_native_code_counters && counter->Enabled()) {
473 movq(kScratchRegister, ExternalReference(counter));
474 Operand operand(kScratchRegister, 0);
475 if (value == 1) {
476 decl(operand);
477 } else {
478 subl(operand, Immediate(value));
479 }
480 }
481}
482
483
484#ifdef ENABLE_DEBUGGER_SUPPORT
485
486void MacroAssembler::PushRegistersFromMemory(RegList regs) {
487 ASSERT((regs & ~kJSCallerSaved) == 0);
488 // Push the content of the memory location to the stack.
489 for (int i = 0; i < kNumJSCallerSaved; i++) {
490 int r = JSCallerSavedCode(i);
491 if ((regs & (1 << r)) != 0) {
492 ExternalReference reg_addr =
493 ExternalReference(Debug_Address::Register(i));
494 movq(kScratchRegister, reg_addr);
495 push(Operand(kScratchRegister, 0));
496 }
497 }
498}
499
500void MacroAssembler::SaveRegistersToMemory(RegList regs) {
501 ASSERT((regs & ~kJSCallerSaved) == 0);
502 // Copy the content of registers to memory location.
503 for (int i = 0; i < kNumJSCallerSaved; i++) {
504 int r = JSCallerSavedCode(i);
505 if ((regs & (1 << r)) != 0) {
506 Register reg = { r };
507 ExternalReference reg_addr =
508 ExternalReference(Debug_Address::Register(i));
509 movq(kScratchRegister, reg_addr);
510 movq(Operand(kScratchRegister, 0), reg);
511 }
512 }
513}
514
515
516void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
517 ASSERT((regs & ~kJSCallerSaved) == 0);
518 // Copy the content of memory location to registers.
519 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
520 int r = JSCallerSavedCode(i);
521 if ((regs & (1 << r)) != 0) {
522 Register reg = { r };
523 ExternalReference reg_addr =
524 ExternalReference(Debug_Address::Register(i));
525 movq(kScratchRegister, reg_addr);
526 movq(reg, Operand(kScratchRegister, 0));
527 }
528 }
529}
530
531
532void MacroAssembler::PopRegistersToMemory(RegList regs) {
533 ASSERT((regs & ~kJSCallerSaved) == 0);
534 // Pop the content from the stack to the memory location.
535 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
536 int r = JSCallerSavedCode(i);
537 if ((regs & (1 << r)) != 0) {
538 ExternalReference reg_addr =
539 ExternalReference(Debug_Address::Register(i));
540 movq(kScratchRegister, reg_addr);
541 pop(Operand(kScratchRegister, 0));
542 }
543 }
544}
545
546
547void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
548 Register scratch,
549 RegList regs) {
550 ASSERT(!scratch.is(kScratchRegister));
551 ASSERT(!base.is(kScratchRegister));
552 ASSERT(!base.is(scratch));
553 ASSERT((regs & ~kJSCallerSaved) == 0);
554 // Copy the content of the stack to the memory location and adjust base.
555 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
556 int r = JSCallerSavedCode(i);
557 if ((regs & (1 << r)) != 0) {
558 movq(scratch, Operand(base, 0));
559 ExternalReference reg_addr =
560 ExternalReference(Debug_Address::Register(i));
561 movq(kScratchRegister, reg_addr);
562 movq(Operand(kScratchRegister, 0), scratch);
563 lea(base, Operand(base, kPointerSize));
564 }
565 }
566}
567
568#endif // ENABLE_DEBUGGER_SUPPORT
569
570
ager@chromium.org3e875802009-06-29 08:26:34 +0000571void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
572 bool resolved;
573 Handle<Code> code = ResolveBuiltin(id, &resolved);
574
575 // Calls are not allowed in some stubs.
576 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
577
578 // Rely on the assertion to check that the number of provided
579 // arguments match the expected number of arguments. Fake a
580 // parameter count to avoid emitting code to do the check.
581 ParameterCount expected(0);
582 InvokeCode(Handle<Code>(code), expected, expected,
583 RelocInfo::CODE_TARGET, flag);
584
585 const char* name = Builtins::GetName(id);
586 int argc = Builtins::GetArgumentsCount(id);
587 // The target address for the jump is stored as an immediate at offset
588 // kInvokeCodeAddressOffset.
589 if (!resolved) {
590 uint32_t flags =
591 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
592 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
593 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
594 Unresolved entry =
595 { pc_offset() - kTargetAddrToReturnAddrDist, flags, name };
596 unresolved_.Add(entry);
597 }
598}
599
600
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000601void MacroAssembler::InvokePrologue(const ParameterCount& expected,
602 const ParameterCount& actual,
603 Handle<Code> code_constant,
604 Register code_register,
605 Label* done,
606 InvokeFlag flag) {
607 bool definitely_matches = false;
608 Label invoke;
609 if (expected.is_immediate()) {
610 ASSERT(actual.is_immediate());
611 if (expected.immediate() == actual.immediate()) {
612 definitely_matches = true;
613 } else {
614 movq(rax, Immediate(actual.immediate()));
615 if (expected.immediate() ==
616 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
617 // Don't worry about adapting arguments for built-ins that
618 // don't want that done. Skip adaption code by making it look
619 // like we have a match between expected and actual number of
620 // arguments.
621 definitely_matches = true;
622 } else {
623 movq(rbx, Immediate(expected.immediate()));
624 }
625 }
626 } else {
627 if (actual.is_immediate()) {
628 // Expected is in register, actual is immediate. This is the
629 // case when we invoke function values without going through the
630 // IC mechanism.
631 cmpq(expected.reg(), Immediate(actual.immediate()));
632 j(equal, &invoke);
633 ASSERT(expected.reg().is(rbx));
634 movq(rax, Immediate(actual.immediate()));
635 } else if (!expected.reg().is(actual.reg())) {
636 // Both expected and actual are in (different) registers. This
637 // is the case when we invoke functions using call and apply.
638 cmpq(expected.reg(), actual.reg());
639 j(equal, &invoke);
640 ASSERT(actual.reg().is(rax));
641 ASSERT(expected.reg().is(rbx));
642 }
643 }
644
645 if (!definitely_matches) {
646 Handle<Code> adaptor =
647 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
648 if (!code_constant.is_null()) {
649 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
650 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
651 } else if (!code_register.is(rdx)) {
652 movq(rdx, code_register);
653 }
654
655 movq(kScratchRegister, adaptor, RelocInfo::CODE_TARGET);
656 if (flag == CALL_FUNCTION) {
657 call(kScratchRegister);
658 jmp(done);
659 } else {
660 jmp(kScratchRegister);
661 }
662 bind(&invoke);
663 }
664}
665
666
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000667void MacroAssembler::InvokeCode(Register code,
668 const ParameterCount& expected,
669 const ParameterCount& actual,
670 InvokeFlag flag) {
671 Label done;
672 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
673 if (flag == CALL_FUNCTION) {
674 call(code);
675 } else {
676 ASSERT(flag == JUMP_FUNCTION);
677 jmp(code);
678 }
679 bind(&done);
680}
681
682
683void MacroAssembler::InvokeCode(Handle<Code> code,
684 const ParameterCount& expected,
685 const ParameterCount& actual,
686 RelocInfo::Mode rmode,
687 InvokeFlag flag) {
688 Label done;
689 Register dummy = rax;
690 InvokePrologue(expected, actual, code, dummy, &done, flag);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000691 if (flag == CALL_FUNCTION) {
ager@chromium.org3e875802009-06-29 08:26:34 +0000692 Call(code, rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000693 } else {
694 ASSERT(flag == JUMP_FUNCTION);
ager@chromium.org3e875802009-06-29 08:26:34 +0000695 Jump(code, rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000696 }
697 bind(&done);
698}
699
700
701void MacroAssembler::InvokeFunction(Register function,
702 const ParameterCount& actual,
703 InvokeFlag flag) {
704 ASSERT(function.is(rdi));
705 movq(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
706 movq(rsi, FieldOperand(function, JSFunction::kContextOffset));
ager@chromium.org3e875802009-06-29 08:26:34 +0000707 movsxlq(rbx,
708 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000709 movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000710 // Advances rdx to the end of the Code object header, to the start of
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000711 // the executable code.
712 lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
713
714 ParameterCount expected(rbx);
715 InvokeCode(rdx, expected, actual, flag);
716}
717
718
719void MacroAssembler::EnterFrame(StackFrame::Type type) {
720 push(rbp);
721 movq(rbp, rsp);
722 push(rsi); // Context.
723 push(Immediate(Smi::FromInt(type)));
724 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
725 push(kScratchRegister);
726 if (FLAG_debug_code) {
727 movq(kScratchRegister,
728 Factory::undefined_value(),
729 RelocInfo::EMBEDDED_OBJECT);
730 cmpq(Operand(rsp, 0), kScratchRegister);
731 Check(not_equal, "code object not properly patched");
732 }
733}
734
735
736void MacroAssembler::LeaveFrame(StackFrame::Type type) {
737 if (FLAG_debug_code) {
738 movq(kScratchRegister, Immediate(Smi::FromInt(type)));
739 cmpq(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
740 Check(equal, "stack frame types must match");
741 }
742 movq(rsp, rbp);
743 pop(rbp);
744}
745
746
747
748void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
749 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
750
751 // Setup the frame structure on the stack.
752 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
753 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
754 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
755 push(rbp);
756 movq(rbp, rsp);
757
758 // Reserve room for entry stack pointer and push the debug marker.
759 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
760 push(Immediate(0)); // saved entry sp, patched before call
761 push(Immediate(type == StackFrame::EXIT_DEBUG ? 1 : 0));
762
763 // Save the frame pointer and the context in top.
764 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
765 ExternalReference context_address(Top::k_context_address);
766 movq(rdi, rax); // Backup rax before we use it.
767
768 movq(rax, rbp);
769 store_rax(c_entry_fp_address);
770 movq(rax, rsi);
771 store_rax(context_address);
772
773 // Setup argv in callee-saved register r15. It is reused in LeaveExitFrame,
774 // so it must be retained across the C-call.
775 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
ager@chromium.org3e875802009-06-29 08:26:34 +0000776 lea(r15, Operand(rbp, rdi, times_pointer_size, offset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000777
778#ifdef ENABLE_DEBUGGER_SUPPORT
779 // Save the state of all registers to the stack from the memory
780 // location. This is needed to allow nested break points.
781 if (type == StackFrame::EXIT_DEBUG) {
782 // TODO(1243899): This should be symmetric to
783 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
784 // correct here, but computed for the other call. Very error
785 // prone! FIX THIS. Actually there are deeper problems with
786 // register saving than this asymmetry (see the bug report
787 // associated with this issue).
788 PushRegistersFromMemory(kJSCallerSaved);
789 }
790#endif
791
792 // Reserve space for two arguments: argc and argv
793 subq(rsp, Immediate(2 * kPointerSize));
794
795 // Get the required frame alignment for the OS.
796 static const int kFrameAlignment = OS::ActivationFrameAlignment();
797 if (kFrameAlignment > 0) {
798 ASSERT(IsPowerOf2(kFrameAlignment));
799 movq(kScratchRegister, Immediate(-kFrameAlignment));
800 and_(rsp, kScratchRegister);
801 }
802
803 // Patch the saved entry sp.
804 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
805}
806
807
808void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
809 // Registers:
810 // r15 : argv
811#ifdef ENABLE_DEBUGGER_SUPPORT
812 // Restore the memory copy of the registers by digging them out from
813 // the stack. This is needed to allow nested break points.
814 if (type == StackFrame::EXIT_DEBUG) {
815 // It's okay to clobber register ebx below because we don't need
816 // the function pointer after this.
817 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
818 int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
819 lea(rbx, Operand(rbp, kOffset));
820 CopyRegistersFromStackToMemory(rbx, rcx, kJSCallerSaved);
821 }
822#endif
823
824 // Get the return address from the stack and restore the frame pointer.
825 movq(rcx, Operand(rbp, 1 * kPointerSize));
826 movq(rbp, Operand(rbp, 0 * kPointerSize));
827
828 // Pop the arguments and the receiver from the caller stack.
829 lea(rsp, Operand(r15, 1 * kPointerSize));
830
831 // Restore current context from top and clear it in debug mode.
832 ExternalReference context_address(Top::k_context_address);
833 movq(kScratchRegister, context_address);
834 movq(rsi, Operand(kScratchRegister, 0));
835#ifdef DEBUG
836 movq(Operand(kScratchRegister, 0), Immediate(0));
837#endif
838
839 // Push the return address to get ready to return.
840 push(rcx);
841
842 // Clear the top frame.
843 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
844 movq(kScratchRegister, c_entry_fp_address);
845 movq(Operand(kScratchRegister, 0), Immediate(0));
846}
847
848
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000849} } // namespace v8::internal