blob: fc94e5726e05045b5164a588ca0054f5b011a90a [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
312void MacroAssembler::Push(Handle<Object> source) {
313 Move(kScratchRegister, source);
314 push(kScratchRegister);
315}
316
317
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000318void MacroAssembler::Jump(ExternalReference ext) {
319 movq(kScratchRegister, ext);
320 jmp(kScratchRegister);
321}
322
323
324void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
325 movq(kScratchRegister, destination, rmode);
326 jmp(kScratchRegister);
327}
328
329
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000330void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
331 WriteRecordedPositions();
332 ASSERT(RelocInfo::IsCodeTarget(rmode));
333 movq(kScratchRegister, code_object, rmode);
334 jmp(kScratchRegister);
335}
336
337
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000338void MacroAssembler::Call(ExternalReference ext) {
339 movq(kScratchRegister, ext);
340 call(kScratchRegister);
341}
342
343
344void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
345 movq(kScratchRegister, destination, rmode);
346 call(kScratchRegister);
347}
348
349
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000350void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
351 WriteRecordedPositions();
352 ASSERT(RelocInfo::IsCodeTarget(rmode));
353 movq(kScratchRegister, code_object, rmode);
354#ifdef DEBUG
355 Label target;
356 bind(&target);
357#endif
358 call(kScratchRegister);
359#ifdef DEBUG
360 ASSERT_EQ(kTargetAddrToReturnAddrDist,
361 SizeOfCodeGeneratedSince(&target) + kPointerSize);
362#endif
363}
364
365
ager@chromium.orge2902be2009-06-08 12:21:35 +0000366void MacroAssembler::PushTryHandler(CodeLocation try_location,
367 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000368 // Adjust this code if not the case.
369 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
370
371 // The pc (return address) is already on TOS. This code pushes state,
372 // frame pointer and current handler. Check that they are expected
373 // next on the stack, in that order.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000374 ASSERT_EQ(StackHandlerConstants::kStateOffset,
375 StackHandlerConstants::kPCOffset - kPointerSize);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000376 ASSERT_EQ(StackHandlerConstants::kFPOffset,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000377 StackHandlerConstants::kStateOffset - kPointerSize);
378 ASSERT_EQ(StackHandlerConstants::kNextOffset,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000379 StackHandlerConstants::kFPOffset - kPointerSize);
380
381 if (try_location == IN_JAVASCRIPT) {
382 if (type == TRY_CATCH_HANDLER) {
383 push(Immediate(StackHandler::TRY_CATCH));
384 } else {
385 push(Immediate(StackHandler::TRY_FINALLY));
386 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000387 push(rbp);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000388 } else {
389 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000390 // The frame pointer does not point to a JS frame so we save NULL
391 // for rbp. We expect the code throwing an exception to check rbp
392 // before dereferencing it to restore the context.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000393 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000394 push(Immediate(0)); // NULL frame pointer.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000395 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000396 // Save the current handler.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000397 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000398 push(Operand(kScratchRegister, 0));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000399 // Link this handler.
400 movq(Operand(kScratchRegister, 0), rsp);
401}
402
403
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000404void MacroAssembler::Ret() {
405 ret(0);
406}
407
408
409void MacroAssembler::CmpObjectType(Register heap_object,
410 InstanceType type,
411 Register map) {
412 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
413 CmpInstanceType(map, type);
414}
415
416
417void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
418 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
419 Immediate(static_cast<int8_t>(type)));
420}
421
422
423
424void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
425 if (FLAG_native_code_counters && counter->Enabled()) {
426 movq(kScratchRegister, ExternalReference(counter));
427 movl(Operand(kScratchRegister, 0), Immediate(value));
428 }
429}
430
431
432void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
433 ASSERT(value > 0);
434 if (FLAG_native_code_counters && counter->Enabled()) {
435 movq(kScratchRegister, ExternalReference(counter));
436 Operand operand(kScratchRegister, 0);
437 if (value == 1) {
438 incl(operand);
439 } else {
440 addl(operand, Immediate(value));
441 }
442 }
443}
444
445
446void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
447 ASSERT(value > 0);
448 if (FLAG_native_code_counters && counter->Enabled()) {
449 movq(kScratchRegister, ExternalReference(counter));
450 Operand operand(kScratchRegister, 0);
451 if (value == 1) {
452 decl(operand);
453 } else {
454 subl(operand, Immediate(value));
455 }
456 }
457}
458
459
460#ifdef ENABLE_DEBUGGER_SUPPORT
461
462void MacroAssembler::PushRegistersFromMemory(RegList regs) {
463 ASSERT((regs & ~kJSCallerSaved) == 0);
464 // Push the content of the memory location to the stack.
465 for (int i = 0; i < kNumJSCallerSaved; i++) {
466 int r = JSCallerSavedCode(i);
467 if ((regs & (1 << r)) != 0) {
468 ExternalReference reg_addr =
469 ExternalReference(Debug_Address::Register(i));
470 movq(kScratchRegister, reg_addr);
471 push(Operand(kScratchRegister, 0));
472 }
473 }
474}
475
476void MacroAssembler::SaveRegistersToMemory(RegList regs) {
477 ASSERT((regs & ~kJSCallerSaved) == 0);
478 // Copy the content of registers to memory location.
479 for (int i = 0; i < kNumJSCallerSaved; i++) {
480 int r = JSCallerSavedCode(i);
481 if ((regs & (1 << r)) != 0) {
482 Register reg = { r };
483 ExternalReference reg_addr =
484 ExternalReference(Debug_Address::Register(i));
485 movq(kScratchRegister, reg_addr);
486 movq(Operand(kScratchRegister, 0), reg);
487 }
488 }
489}
490
491
492void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
493 ASSERT((regs & ~kJSCallerSaved) == 0);
494 // Copy the content of memory location to registers.
495 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
496 int r = JSCallerSavedCode(i);
497 if ((regs & (1 << r)) != 0) {
498 Register reg = { r };
499 ExternalReference reg_addr =
500 ExternalReference(Debug_Address::Register(i));
501 movq(kScratchRegister, reg_addr);
502 movq(reg, Operand(kScratchRegister, 0));
503 }
504 }
505}
506
507
508void MacroAssembler::PopRegistersToMemory(RegList regs) {
509 ASSERT((regs & ~kJSCallerSaved) == 0);
510 // Pop the content from the stack to the memory location.
511 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
512 int r = JSCallerSavedCode(i);
513 if ((regs & (1 << r)) != 0) {
514 ExternalReference reg_addr =
515 ExternalReference(Debug_Address::Register(i));
516 movq(kScratchRegister, reg_addr);
517 pop(Operand(kScratchRegister, 0));
518 }
519 }
520}
521
522
523void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
524 Register scratch,
525 RegList regs) {
526 ASSERT(!scratch.is(kScratchRegister));
527 ASSERT(!base.is(kScratchRegister));
528 ASSERT(!base.is(scratch));
529 ASSERT((regs & ~kJSCallerSaved) == 0);
530 // Copy the content of the stack to the memory location and adjust base.
531 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
532 int r = JSCallerSavedCode(i);
533 if ((regs & (1 << r)) != 0) {
534 movq(scratch, Operand(base, 0));
535 ExternalReference reg_addr =
536 ExternalReference(Debug_Address::Register(i));
537 movq(kScratchRegister, reg_addr);
538 movq(Operand(kScratchRegister, 0), scratch);
539 lea(base, Operand(base, kPointerSize));
540 }
541 }
542}
543
544#endif // ENABLE_DEBUGGER_SUPPORT
545
546
547void MacroAssembler::InvokePrologue(const ParameterCount& expected,
548 const ParameterCount& actual,
549 Handle<Code> code_constant,
550 Register code_register,
551 Label* done,
552 InvokeFlag flag) {
553 bool definitely_matches = false;
554 Label invoke;
555 if (expected.is_immediate()) {
556 ASSERT(actual.is_immediate());
557 if (expected.immediate() == actual.immediate()) {
558 definitely_matches = true;
559 } else {
560 movq(rax, Immediate(actual.immediate()));
561 if (expected.immediate() ==
562 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
563 // Don't worry about adapting arguments for built-ins that
564 // don't want that done. Skip adaption code by making it look
565 // like we have a match between expected and actual number of
566 // arguments.
567 definitely_matches = true;
568 } else {
569 movq(rbx, Immediate(expected.immediate()));
570 }
571 }
572 } else {
573 if (actual.is_immediate()) {
574 // Expected is in register, actual is immediate. This is the
575 // case when we invoke function values without going through the
576 // IC mechanism.
577 cmpq(expected.reg(), Immediate(actual.immediate()));
578 j(equal, &invoke);
579 ASSERT(expected.reg().is(rbx));
580 movq(rax, Immediate(actual.immediate()));
581 } else if (!expected.reg().is(actual.reg())) {
582 // Both expected and actual are in (different) registers. This
583 // is the case when we invoke functions using call and apply.
584 cmpq(expected.reg(), actual.reg());
585 j(equal, &invoke);
586 ASSERT(actual.reg().is(rax));
587 ASSERT(expected.reg().is(rbx));
588 }
589 }
590
591 if (!definitely_matches) {
592 Handle<Code> adaptor =
593 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
594 if (!code_constant.is_null()) {
595 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
596 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
597 } else if (!code_register.is(rdx)) {
598 movq(rdx, code_register);
599 }
600
601 movq(kScratchRegister, adaptor, RelocInfo::CODE_TARGET);
602 if (flag == CALL_FUNCTION) {
603 call(kScratchRegister);
604 jmp(done);
605 } else {
606 jmp(kScratchRegister);
607 }
608 bind(&invoke);
609 }
610}
611
612
613
614
615void MacroAssembler::InvokeCode(Register code,
616 const ParameterCount& expected,
617 const ParameterCount& actual,
618 InvokeFlag flag) {
619 Label done;
620 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
621 if (flag == CALL_FUNCTION) {
622 call(code);
623 } else {
624 ASSERT(flag == JUMP_FUNCTION);
625 jmp(code);
626 }
627 bind(&done);
628}
629
630
631void MacroAssembler::InvokeCode(Handle<Code> code,
632 const ParameterCount& expected,
633 const ParameterCount& actual,
634 RelocInfo::Mode rmode,
635 InvokeFlag flag) {
636 Label done;
637 Register dummy = rax;
638 InvokePrologue(expected, actual, code, dummy, &done, flag);
639 movq(kScratchRegister, code, rmode);
640 if (flag == CALL_FUNCTION) {
641 call(kScratchRegister);
642 } else {
643 ASSERT(flag == JUMP_FUNCTION);
644 jmp(kScratchRegister);
645 }
646 bind(&done);
647}
648
649
650void MacroAssembler::InvokeFunction(Register function,
651 const ParameterCount& actual,
652 InvokeFlag flag) {
653 ASSERT(function.is(rdi));
654 movq(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
655 movq(rsi, FieldOperand(function, JSFunction::kContextOffset));
656 movl(rbx, FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
657 movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000658 // Advances rdx to the end of the Code object header, to the start of
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000659 // the executable code.
660 lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
661
662 ParameterCount expected(rbx);
663 InvokeCode(rdx, expected, actual, flag);
664}
665
666
667void MacroAssembler::EnterFrame(StackFrame::Type type) {
668 push(rbp);
669 movq(rbp, rsp);
670 push(rsi); // Context.
671 push(Immediate(Smi::FromInt(type)));
672 movq(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
673 push(kScratchRegister);
674 if (FLAG_debug_code) {
675 movq(kScratchRegister,
676 Factory::undefined_value(),
677 RelocInfo::EMBEDDED_OBJECT);
678 cmpq(Operand(rsp, 0), kScratchRegister);
679 Check(not_equal, "code object not properly patched");
680 }
681}
682
683
684void MacroAssembler::LeaveFrame(StackFrame::Type type) {
685 if (FLAG_debug_code) {
686 movq(kScratchRegister, Immediate(Smi::FromInt(type)));
687 cmpq(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
688 Check(equal, "stack frame types must match");
689 }
690 movq(rsp, rbp);
691 pop(rbp);
692}
693
694
695
696void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
697 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
698
699 // Setup the frame structure on the stack.
700 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
701 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
702 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
703 push(rbp);
704 movq(rbp, rsp);
705
706 // Reserve room for entry stack pointer and push the debug marker.
707 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
708 push(Immediate(0)); // saved entry sp, patched before call
709 push(Immediate(type == StackFrame::EXIT_DEBUG ? 1 : 0));
710
711 // Save the frame pointer and the context in top.
712 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
713 ExternalReference context_address(Top::k_context_address);
714 movq(rdi, rax); // Backup rax before we use it.
715
716 movq(rax, rbp);
717 store_rax(c_entry_fp_address);
718 movq(rax, rsi);
719 store_rax(context_address);
720
721 // Setup argv in callee-saved register r15. It is reused in LeaveExitFrame,
722 // so it must be retained across the C-call.
723 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
724 lea(r15, Operand(rbp, rdi, kTimesPointerSize, offset));
725
726#ifdef ENABLE_DEBUGGER_SUPPORT
727 // Save the state of all registers to the stack from the memory
728 // location. This is needed to allow nested break points.
729 if (type == StackFrame::EXIT_DEBUG) {
730 // TODO(1243899): This should be symmetric to
731 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
732 // correct here, but computed for the other call. Very error
733 // prone! FIX THIS. Actually there are deeper problems with
734 // register saving than this asymmetry (see the bug report
735 // associated with this issue).
736 PushRegistersFromMemory(kJSCallerSaved);
737 }
738#endif
739
740 // Reserve space for two arguments: argc and argv
741 subq(rsp, Immediate(2 * kPointerSize));
742
743 // Get the required frame alignment for the OS.
744 static const int kFrameAlignment = OS::ActivationFrameAlignment();
745 if (kFrameAlignment > 0) {
746 ASSERT(IsPowerOf2(kFrameAlignment));
747 movq(kScratchRegister, Immediate(-kFrameAlignment));
748 and_(rsp, kScratchRegister);
749 }
750
751 // Patch the saved entry sp.
752 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
753}
754
755
756void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
757 // Registers:
758 // r15 : argv
759#ifdef ENABLE_DEBUGGER_SUPPORT
760 // Restore the memory copy of the registers by digging them out from
761 // the stack. This is needed to allow nested break points.
762 if (type == StackFrame::EXIT_DEBUG) {
763 // It's okay to clobber register ebx below because we don't need
764 // the function pointer after this.
765 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
766 int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
767 lea(rbx, Operand(rbp, kOffset));
768 CopyRegistersFromStackToMemory(rbx, rcx, kJSCallerSaved);
769 }
770#endif
771
772 // Get the return address from the stack and restore the frame pointer.
773 movq(rcx, Operand(rbp, 1 * kPointerSize));
774 movq(rbp, Operand(rbp, 0 * kPointerSize));
775
776 // Pop the arguments and the receiver from the caller stack.
777 lea(rsp, Operand(r15, 1 * kPointerSize));
778
779 // Restore current context from top and clear it in debug mode.
780 ExternalReference context_address(Top::k_context_address);
781 movq(kScratchRegister, context_address);
782 movq(rsi, Operand(kScratchRegister, 0));
783#ifdef DEBUG
784 movq(Operand(kScratchRegister, 0), Immediate(0));
785#endif
786
787 // Push the return address to get ready to return.
788 push(rcx);
789
790 // Clear the top frame.
791 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
792 movq(kScratchRegister, c_entry_fp_address);
793 movq(Operand(kScratchRegister, 0), Immediate(0));
794}
795
796
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000797} } // namespace v8::internal