blob: 1a0b119d84d155dd306a426506f2f0bb82248ed5 [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::Abort(const char* msg) {
83 // We want to pass the msg string like a smi to avoid GC
84 // problems, however msg is not guaranteed to be aligned
85 // properly. Instead, we pass an aligned pointer that is
86 // a proper v8 smi, but also pass the alignment difference
87 // from the real pointer as a smi.
88 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
89 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
90 // Note: p0 might not be a valid Smi *value*, but it has a valid Smi tag.
91 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
92#ifdef DEBUG
93 if (msg != NULL) {
94 RecordComment("Abort message: ");
95 RecordComment(msg);
96 }
97#endif
98 push(rax);
99 movq(kScratchRegister, p0, RelocInfo::NONE);
100 push(kScratchRegister);
101 movq(kScratchRegister,
102 reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0)),
103 RelocInfo::NONE);
104 push(kScratchRegister);
105 CallRuntime(Runtime::kAbort, 2);
106 // will not return here
107}
108
109
110void MacroAssembler::CallStub(CodeStub* stub) {
111 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
112 movq(kScratchRegister, stub->GetCode(), RelocInfo::CODE_TARGET);
113 call(kScratchRegister);
114}
115
116
117void MacroAssembler::StubReturn(int argc) {
118 ASSERT(argc >= 1 && generating_stub());
119 ret((argc - 1) * kPointerSize);
120}
121
122
123void MacroAssembler::IllegalOperation(int num_arguments) {
124 if (num_arguments > 0) {
125 addq(rsp, Immediate(num_arguments * kPointerSize));
126 }
127 movq(rax, Factory::undefined_value(), RelocInfo::EMBEDDED_OBJECT);
128}
129
130
131void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
132 CallRuntime(Runtime::FunctionForId(id), num_arguments);
133}
134
135
136void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
137 // If the expected number of arguments of the runtime function is
138 // constant, we check that the actual number of arguments match the
139 // expectation.
140 if (f->nargs >= 0 && f->nargs != num_arguments) {
141 IllegalOperation(num_arguments);
142 return;
143 }
144
145 Runtime::FunctionId function_id =
146 static_cast<Runtime::FunctionId>(f->stub_id);
147 RuntimeStub stub(function_id, num_arguments);
148 CallStub(&stub);
149}
150
151
152void MacroAssembler::TailCallRuntime(ExternalReference const& ext,
153 int num_arguments) {
154 // TODO(1236192): Most runtime routines don't need the number of
155 // arguments passed in because it is constant. At some point we
156 // should remove this need and make the runtime routine entry code
157 // smarter.
158 movq(rax, Immediate(num_arguments));
159 JumpToBuiltin(ext);
160}
161
162
163void MacroAssembler::JumpToBuiltin(const ExternalReference& ext) {
164 // Set the entry point and jump to the C entry runtime stub.
165 movq(rbx, ext);
166 CEntryStub ces;
167 movq(kScratchRegister, ces.GetCode(), RelocInfo::CODE_TARGET);
168 jmp(kScratchRegister);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000169}
170
ager@chromium.orge2902be2009-06-08 12:21:35 +0000171
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000172void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
173 bool resolved;
174 Handle<Code> code = ResolveBuiltin(id, &resolved);
175
176 const char* name = Builtins::GetName(id);
177 int argc = Builtins::GetArgumentsCount(id);
178
179 movq(target, code, RelocInfo::EXTERNAL_REFERENCE); // Is external reference?
180 if (!resolved) {
181 uint32_t flags =
182 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
183 Bootstrapper::FixupFlagsIsPCRelative::encode(false) |
184 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
185 Unresolved entry = { pc_offset() - sizeof(intptr_t), flags, name };
186 unresolved_.Add(entry);
187 }
188 addq(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
189}
190
191
192Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
193 bool* resolved) {
194 // Move the builtin function into the temporary function slot by
195 // reading it from the builtins object. NOTE: We should be able to
196 // reduce this to two instructions by putting the function table in
197 // the global object instead of the "builtins" object and by using a
198 // real register for the function.
199 movq(rdx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
200 movq(rdx, FieldOperand(rdx, GlobalObject::kBuiltinsOffset));
201 int builtins_offset =
202 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
203 movq(rdi, FieldOperand(rdx, builtins_offset));
204
205
206 return Builtins::GetCode(id, resolved);
207}
208
209
ager@chromium.orge2902be2009-06-08 12:21:35 +0000210void MacroAssembler::Set(Register dst, int64_t x) {
211 if (is_int32(x)) {
212 movq(dst, Immediate(x));
213 } else if (is_uint32(x)) {
214 movl(dst, Immediate(x));
215 } else {
216 movq(dst, x, RelocInfo::NONE);
217 }
218}
219
220
221void MacroAssembler::Set(const Operand& dst, int64_t x) {
222 if (is_int32(x)) {
223 movq(kScratchRegister, Immediate(x));
224 } else if (is_uint32(x)) {
225 movl(kScratchRegister, Immediate(x));
226 } else {
227 movq(kScratchRegister, x, RelocInfo::NONE);
228 }
229 movq(dst, kScratchRegister);
230}
231
232
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000233bool MacroAssembler::IsUnsafeSmi(Smi* value) {
234 return false;
235}
236
237void MacroAssembler::LoadUnsafeSmi(Register dst, Smi* source) {
238 UNIMPLEMENTED();
239}
240
241
242void MacroAssembler::Move(Register dst, Handle<Object> source) {
243 if (source->IsSmi()) {
244 if (IsUnsafeSmi(source)) {
245 LoadUnsafeSmi(dst, source);
246 } else {
247 movq(dst, source, RelocInfo::NONE);
248 }
249 } else {
250 movq(dst, source, RelocInfo::EMBEDDED_OBJECT);
251 }
252}
253
254
255void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
256 Move(kScratchRegister, source);
257 movq(dst, kScratchRegister);
258}
259
260
261void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
262 Move(kScratchRegister, source);
263 cmpq(dst, kScratchRegister);
264}
265
266
ager@chromium.org3e875802009-06-29 08:26:34 +0000267void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
268 Move(kScratchRegister, source);
269 cmpq(dst, kScratchRegister);
270}
271
272
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000273void MacroAssembler::Push(Handle<Object> source) {
274 Move(kScratchRegister, source);
275 push(kScratchRegister);
276}
277
278
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000279void MacroAssembler::Jump(ExternalReference ext) {
280 movq(kScratchRegister, ext);
281 jmp(kScratchRegister);
282}
283
284
285void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
286 movq(kScratchRegister, destination, rmode);
287 jmp(kScratchRegister);
288}
289
290
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000291void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
292 WriteRecordedPositions();
293 ASSERT(RelocInfo::IsCodeTarget(rmode));
294 movq(kScratchRegister, code_object, rmode);
ager@chromium.org3e875802009-06-29 08:26:34 +0000295#ifdef DEBUG
296 Label target;
297 bind(&target);
298#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000299 jmp(kScratchRegister);
ager@chromium.org3e875802009-06-29 08:26:34 +0000300#ifdef DEBUG
301 ASSERT_EQ(kTargetAddrToReturnAddrDist,
302 SizeOfCodeGeneratedSince(&target) + kPointerSize);
303#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000304}
305
306
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000307void MacroAssembler::Call(ExternalReference ext) {
308 movq(kScratchRegister, ext);
309 call(kScratchRegister);
310}
311
312
313void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
314 movq(kScratchRegister, destination, rmode);
315 call(kScratchRegister);
316}
317
318
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000319void MacroAssembler::Call(Handle<Code> code_object, RelocInfo::Mode rmode) {
320 WriteRecordedPositions();
321 ASSERT(RelocInfo::IsCodeTarget(rmode));
322 movq(kScratchRegister, code_object, rmode);
323#ifdef DEBUG
324 Label target;
325 bind(&target);
326#endif
327 call(kScratchRegister);
328#ifdef DEBUG
329 ASSERT_EQ(kTargetAddrToReturnAddrDist,
330 SizeOfCodeGeneratedSince(&target) + kPointerSize);
331#endif
332}
333
334
ager@chromium.orge2902be2009-06-08 12:21:35 +0000335void MacroAssembler::PushTryHandler(CodeLocation try_location,
336 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000337 // Adjust this code if not the case.
338 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
339
340 // The pc (return address) is already on TOS. This code pushes state,
341 // frame pointer and current handler. Check that they are expected
342 // next on the stack, in that order.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000343 ASSERT_EQ(StackHandlerConstants::kStateOffset,
344 StackHandlerConstants::kPCOffset - kPointerSize);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000345 ASSERT_EQ(StackHandlerConstants::kFPOffset,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000346 StackHandlerConstants::kStateOffset - kPointerSize);
347 ASSERT_EQ(StackHandlerConstants::kNextOffset,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000348 StackHandlerConstants::kFPOffset - kPointerSize);
349
350 if (try_location == IN_JAVASCRIPT) {
351 if (type == TRY_CATCH_HANDLER) {
352 push(Immediate(StackHandler::TRY_CATCH));
353 } else {
354 push(Immediate(StackHandler::TRY_FINALLY));
355 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000356 push(rbp);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000357 } else {
358 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000359 // The frame pointer does not point to a JS frame so we save NULL
360 // for rbp. We expect the code throwing an exception to check rbp
361 // before dereferencing it to restore the context.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000362 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000363 push(Immediate(0)); // NULL frame pointer.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000364 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000365 // Save the current handler.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000366 movq(kScratchRegister, ExternalReference(Top::k_handler_address));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000367 push(Operand(kScratchRegister, 0));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000368 // Link this handler.
369 movq(Operand(kScratchRegister, 0), rsp);
370}
371
372
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000373void MacroAssembler::Ret() {
374 ret(0);
375}
376
377
ager@chromium.org3e875802009-06-29 08:26:34 +0000378void MacroAssembler::FCmp() {
379 fcompp();
380 push(rax);
381 fnstsw_ax();
382 // TODO(X64): Check that sahf is safe to use, using CPUProbe.
383 sahf();
384 pop(rax);
385}
386
387
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000388void MacroAssembler::CmpObjectType(Register heap_object,
389 InstanceType type,
390 Register map) {
391 movq(map, FieldOperand(heap_object, HeapObject::kMapOffset));
392 CmpInstanceType(map, type);
393}
394
395
396void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
397 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
398 Immediate(static_cast<int8_t>(type)));
399}
400
401
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000402void MacroAssembler::TryGetFunctionPrototype(Register function,
403 Register result,
404 Label* miss) {
405 // Check that the receiver isn't a smi.
406 testl(function, Immediate(kSmiTagMask));
407 j(zero, miss);
408
409 // Check that the function really is a function.
410 CmpObjectType(function, JS_FUNCTION_TYPE, result);
411 j(not_equal, miss);
412
413 // Make sure that the function has an instance prototype.
414 Label non_instance;
415 testb(FieldOperand(result, Map::kBitFieldOffset),
416 Immediate(1 << Map::kHasNonInstancePrototype));
417 j(not_zero, &non_instance);
418
419 // Get the prototype or initial map from the function.
420 movq(result,
421 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
422
423 // If the prototype or initial map is the hole, don't return it and
424 // simply miss the cache instead. This will allow us to allocate a
425 // prototype object on-demand in the runtime system.
426 Cmp(result, Factory::the_hole_value());
427 j(equal, miss);
428
429 // If the function does not have an initial map, we're done.
430 Label done;
431 CmpObjectType(result, MAP_TYPE, kScratchRegister);
432 j(not_equal, &done);
433
434 // Get the prototype from the initial map.
435 movq(result, FieldOperand(result, Map::kPrototypeOffset));
436 jmp(&done);
437
438 // Non-instance prototype: Fetch prototype from constructor field
439 // in initial map.
440 bind(&non_instance);
441 movq(result, FieldOperand(result, Map::kConstructorOffset));
442
443 // All done.
444 bind(&done);
445}
446
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000447
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.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000752 // All constants are relative to the frame pointer of the exit frame.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000753 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
754 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
755 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
756 push(rbp);
757 movq(rbp, rsp);
758
759 // Reserve room for entry stack pointer and push the debug marker.
760 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
761 push(Immediate(0)); // saved entry sp, patched before call
762 push(Immediate(type == StackFrame::EXIT_DEBUG ? 1 : 0));
763
764 // Save the frame pointer and the context in top.
765 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
766 ExternalReference context_address(Top::k_context_address);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000767 movq(r14, rax); // Backup rax before we use it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000768
769 movq(rax, rbp);
770 store_rax(c_entry_fp_address);
771 movq(rax, rsi);
772 store_rax(context_address);
773
774 // Setup argv in callee-saved register r15. It is reused in LeaveExitFrame,
775 // so it must be retained across the C-call.
776 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000777 lea(r15, Operand(rbp, r14, times_pointer_size, offset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000778
779#ifdef ENABLE_DEBUGGER_SUPPORT
780 // Save the state of all registers to the stack from the memory
781 // location. This is needed to allow nested break points.
782 if (type == StackFrame::EXIT_DEBUG) {
783 // TODO(1243899): This should be symmetric to
784 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
785 // correct here, but computed for the other call. Very error
786 // prone! FIX THIS. Actually there are deeper problems with
787 // register saving than this asymmetry (see the bug report
788 // associated with this issue).
789 PushRegistersFromMemory(kJSCallerSaved);
790 }
791#endif
792
793 // Reserve space for two arguments: argc and argv
794 subq(rsp, Immediate(2 * kPointerSize));
795
796 // Get the required frame alignment for the OS.
797 static const int kFrameAlignment = OS::ActivationFrameAlignment();
798 if (kFrameAlignment > 0) {
799 ASSERT(IsPowerOf2(kFrameAlignment));
800 movq(kScratchRegister, Immediate(-kFrameAlignment));
801 and_(rsp, kScratchRegister);
802 }
803
804 // Patch the saved entry sp.
805 movq(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
806}
807
808
809void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
810 // Registers:
811 // r15 : argv
812#ifdef ENABLE_DEBUGGER_SUPPORT
813 // Restore the memory copy of the registers by digging them out from
814 // the stack. This is needed to allow nested break points.
815 if (type == StackFrame::EXIT_DEBUG) {
816 // It's okay to clobber register ebx below because we don't need
817 // the function pointer after this.
818 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
819 int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
820 lea(rbx, Operand(rbp, kOffset));
821 CopyRegistersFromStackToMemory(rbx, rcx, kJSCallerSaved);
822 }
823#endif
824
825 // Get the return address from the stack and restore the frame pointer.
826 movq(rcx, Operand(rbp, 1 * kPointerSize));
827 movq(rbp, Operand(rbp, 0 * kPointerSize));
828
829 // Pop the arguments and the receiver from the caller stack.
830 lea(rsp, Operand(r15, 1 * kPointerSize));
831
832 // Restore current context from top and clear it in debug mode.
833 ExternalReference context_address(Top::k_context_address);
834 movq(kScratchRegister, context_address);
835 movq(rsi, Operand(kScratchRegister, 0));
836#ifdef DEBUG
837 movq(Operand(kScratchRegister, 0), Immediate(0));
838#endif
839
840 // Push the return address to get ready to return.
841 push(rcx);
842
843 // Clear the top frame.
844 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
845 movq(kScratchRegister, c_entry_fp_address);
846 movq(Operand(kScratchRegister, 0), Immediate(0));
847}
848
849
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000850} } // namespace v8::internal