blob: 91b6651fe03776bb31cbc7ea56c36f9f949e952c [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_IA32)
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
34#include "debug.h"
35#include "runtime.h"
36#include "serialize.h"
37
38namespace v8 {
39namespace internal {
40
41// -------------------------------------------------------------------------
42// MacroAssembler implementation.
43
44MacroAssembler::MacroAssembler(void* buffer, int size)
45 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000046 generating_stub_(false),
47 allow_stub_calls_(true),
48 code_object_(Heap::undefined_value()) {
49}
50
51
Steve Block6ded16b2010-05-10 14:33:55 +010052void MacroAssembler::RecordWriteHelper(Register object,
53 Register addr,
54 Register scratch) {
55 if (FLAG_debug_code) {
56 // Check that the object is not in new space.
57 Label not_in_new_space;
58 InNewSpace(object, scratch, not_equal, &not_in_new_space);
59 Abort("new-space object passed to RecordWriteHelper");
60 bind(&not_in_new_space);
61 }
62
Steve Blocka7e24c12009-10-30 11:49:00 +000063 // Compute the page start address from the heap object pointer, and reuse
64 // the 'object' register for it.
Steve Block6ded16b2010-05-10 14:33:55 +010065 and_(object, ~Page::kPageAlignmentMask);
Steve Blocka7e24c12009-10-30 11:49:00 +000066
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010067 // Compute number of region covering addr. See Page::GetRegionNumberForAddress
68 // method for more details.
69 and_(addr, Page::kPageAlignmentMask);
70 shr(addr, Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +000071
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010072 // Set dirty mark for region.
73 bts(Operand(object, Page::kDirtyFlagOffset), addr);
Steve Blocka7e24c12009-10-30 11:49:00 +000074}
75
76
Kristian Monsen50ef84f2010-07-29 15:18:00 +010077void MacroAssembler::RecordWrite(Register object,
78 int offset,
79 Register value,
80 Register scratch) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010081 // First, check if a write barrier is even needed. The tests below
82 // catch stores of Smis and stores into young gen.
Ben Murdochb0fe1622011-05-05 13:52:32 +010083 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85 // Skip barrier if writing a smi.
86 ASSERT_EQ(0, kSmiTag);
87 test(value, Immediate(kSmiTagMask));
88 j(zero, &done);
89
Steve Block6ded16b2010-05-10 14:33:55 +010090 InNewSpace(object, value, equal, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +000091
Steve Block6ded16b2010-05-10 14:33:55 +010092 // The offset is relative to a tagged or untagged HeapObject pointer,
93 // so either offset or offset + kHeapObjectTag must be a
94 // multiple of kPointerSize.
95 ASSERT(IsAligned(offset, kPointerSize) ||
96 IsAligned(offset + kHeapObjectTag, kPointerSize));
97
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010098 Register dst = scratch;
99 if (offset != 0) {
100 lea(dst, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100102 // Array access: calculate the destination address in the same manner as
103 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
104 // into an array of words.
105 ASSERT_EQ(1, kSmiTagSize);
106 ASSERT_EQ(0, kSmiTag);
107 lea(dst, Operand(object, dst, times_half_pointer_size,
108 FixedArray::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100110 RecordWriteHelper(object, dst, value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
112 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000113
114 // Clobber all input registers when running with the debug-code flag
115 // turned on to provoke errors.
116 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100117 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
118 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
119 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000120 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000121}
122
123
Steve Block8defd9f2010-07-08 12:39:36 +0100124void MacroAssembler::RecordWrite(Register object,
125 Register address,
126 Register value) {
Steve Block8defd9f2010-07-08 12:39:36 +0100127 // First, check if a write barrier is even needed. The tests below
128 // catch stores of Smis and stores into young gen.
129 Label done;
130
131 // Skip barrier if writing a smi.
132 ASSERT_EQ(0, kSmiTag);
133 test(value, Immediate(kSmiTagMask));
134 j(zero, &done);
135
136 InNewSpace(object, value, equal, &done);
137
138 RecordWriteHelper(object, address, value);
139
140 bind(&done);
141
142 // Clobber all input registers when running with the debug-code flag
143 // turned on to provoke errors.
144 if (FLAG_debug_code) {
145 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
146 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
147 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
148 }
149}
150
151
Steve Blocka7e24c12009-10-30 11:49:00 +0000152#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000153void MacroAssembler::DebugBreak() {
154 Set(eax, Immediate(0));
155 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak)));
156 CEntryStub ces(1);
157 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
158}
Steve Blocka7e24c12009-10-30 11:49:00 +0000159#endif
160
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100161
Steve Blocka7e24c12009-10-30 11:49:00 +0000162void MacroAssembler::Set(Register dst, const Immediate& x) {
163 if (x.is_zero()) {
164 xor_(dst, Operand(dst)); // shorter than mov
165 } else {
166 mov(dst, x);
167 }
168}
169
170
171void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
172 mov(dst, x);
173}
174
175
176void MacroAssembler::CmpObjectType(Register heap_object,
177 InstanceType type,
178 Register map) {
179 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
180 CmpInstanceType(map, type);
181}
182
183
184void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
185 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
186 static_cast<int8_t>(type));
187}
188
189
Andrei Popescu31002712010-02-23 13:46:05 +0000190void MacroAssembler::CheckMap(Register obj,
191 Handle<Map> map,
192 Label* fail,
193 bool is_heap_object) {
194 if (!is_heap_object) {
195 test(obj, Immediate(kSmiTagMask));
196 j(zero, fail);
197 }
198 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
199 j(not_equal, fail);
200}
201
202
Leon Clarkee46be812010-01-19 14:06:41 +0000203Condition MacroAssembler::IsObjectStringType(Register heap_object,
204 Register map,
205 Register instance_type) {
206 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
207 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
208 ASSERT(kNotStringTag != 0);
209 test(instance_type, Immediate(kIsNotStringMask));
210 return zero;
211}
212
213
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100214void MacroAssembler::IsObjectJSObjectType(Register heap_object,
215 Register map,
216 Register scratch,
217 Label* fail) {
218 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
219 IsInstanceJSObjectType(map, scratch, fail);
220}
221
222
223void MacroAssembler::IsInstanceJSObjectType(Register map,
224 Register scratch,
225 Label* fail) {
226 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
227 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
228 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
229 j(above, fail);
230}
231
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233void MacroAssembler::FCmp() {
Steve Blockd0582a62009-12-15 09:54:21 +0000234 if (CpuFeatures::IsSupported(CMOV)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000235 fucomip();
236 ffree(0);
237 fincstp();
238 } else {
239 fucompp();
240 push(eax);
241 fnstsw_ax();
242 sahf();
243 pop(eax);
244 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000245}
246
247
Steve Block6ded16b2010-05-10 14:33:55 +0100248void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000249 Label ok;
250 test(object, Immediate(kSmiTagMask));
251 j(zero, &ok);
252 cmp(FieldOperand(object, HeapObject::kMapOffset),
253 Factory::heap_number_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100254 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +0000255 bind(&ok);
256}
257
258
Steve Block6ded16b2010-05-10 14:33:55 +0100259void MacroAssembler::AbortIfNotSmi(Register object) {
260 test(object, Immediate(kSmiTagMask));
Iain Merrick75681382010-08-19 15:07:18 +0100261 Assert(equal, "Operand is not a smi");
262}
263
264
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100265void MacroAssembler::AbortIfNotString(Register object) {
266 test(object, Immediate(kSmiTagMask));
267 Assert(not_equal, "Operand is not a string");
268 push(object);
269 mov(object, FieldOperand(object, HeapObject::kMapOffset));
270 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
271 pop(object);
272 Assert(below, "Operand is not a string");
273}
274
275
Iain Merrick75681382010-08-19 15:07:18 +0100276void MacroAssembler::AbortIfSmi(Register object) {
277 test(object, Immediate(kSmiTagMask));
278 Assert(not_equal, "Operand is a smi");
Steve Block6ded16b2010-05-10 14:33:55 +0100279}
280
281
Steve Blocka7e24c12009-10-30 11:49:00 +0000282void MacroAssembler::EnterFrame(StackFrame::Type type) {
283 push(ebp);
284 mov(ebp, Operand(esp));
285 push(esi);
286 push(Immediate(Smi::FromInt(type)));
287 push(Immediate(CodeObject()));
288 if (FLAG_debug_code) {
289 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
290 Check(not_equal, "code object not properly patched");
291 }
292}
293
294
295void MacroAssembler::LeaveFrame(StackFrame::Type type) {
296 if (FLAG_debug_code) {
297 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
298 Immediate(Smi::FromInt(type)));
299 Check(equal, "stack frame types must match");
300 }
301 leave();
302}
303
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100304
305void MacroAssembler::EnterExitFramePrologue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 // Setup the frame structure on the stack.
307 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
308 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
309 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
310 push(ebp);
311 mov(ebp, Operand(esp));
312
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100313 // Reserve room for entry stack pointer and push the code object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000315 push(Immediate(0)); // Saved entry sp, patched before call.
316 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000317
318 // Save the frame pointer and the context in top.
319 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
320 ExternalReference context_address(Top::k_context_address);
321 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
322 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000323}
Steve Blocka7e24c12009-10-30 11:49:00 +0000324
Steve Blocka7e24c12009-10-30 11:49:00 +0000325
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
327 // Optionally save all XMM registers.
328 if (save_doubles) {
329 CpuFeatures::Scope scope(SSE2);
330 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
331 sub(Operand(esp), Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100332 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100333 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
334 XMMRegister reg = XMMRegister::from_code(i);
335 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
336 }
337 } else {
338 sub(Operand(esp), Immediate(argc * kPointerSize));
339 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000340
341 // Get the required frame alignment for the OS.
342 static const int kFrameAlignment = OS::ActivationFrameAlignment();
343 if (kFrameAlignment > 0) {
344 ASSERT(IsPowerOf2(kFrameAlignment));
345 and_(esp, -kFrameAlignment);
346 }
347
348 // Patch the saved entry sp.
349 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
350}
351
352
Ben Murdochb0fe1622011-05-05 13:52:32 +0100353void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100354 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000355
356 // Setup argc and argv in callee-saved registers.
357 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
358 mov(edi, Operand(eax));
359 lea(esi, Operand(ebp, eax, times_4, offset));
360
Ben Murdochb0fe1622011-05-05 13:52:32 +0100361 EnterExitFrameEpilogue(2, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000362}
363
364
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800365void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100366 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100367 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000368}
369
370
Ben Murdochb0fe1622011-05-05 13:52:32 +0100371void MacroAssembler::LeaveExitFrame(bool save_doubles) {
372 // Optionally restore all XMM registers.
373 if (save_doubles) {
374 CpuFeatures::Scope scope(SSE2);
Steve Block1e0659c2011-05-24 12:43:12 +0100375 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100376 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
377 XMMRegister reg = XMMRegister::from_code(i);
378 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
379 }
380 }
381
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 // Get the return address from the stack and restore the frame pointer.
383 mov(ecx, Operand(ebp, 1 * kPointerSize));
384 mov(ebp, Operand(ebp, 0 * kPointerSize));
385
386 // Pop the arguments and the receiver from the caller stack.
387 lea(esp, Operand(esi, 1 * kPointerSize));
388
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800389 // Push the return address to get ready to return.
390 push(ecx);
391
392 LeaveExitFrameEpilogue();
393}
394
395void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 // Restore current context from top and clear it in debug mode.
397 ExternalReference context_address(Top::k_context_address);
398 mov(esi, Operand::StaticVariable(context_address));
399#ifdef DEBUG
400 mov(Operand::StaticVariable(context_address), Immediate(0));
401#endif
402
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 // Clear the top frame.
404 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
405 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
406}
407
408
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800409void MacroAssembler::LeaveApiExitFrame() {
410 mov(esp, Operand(ebp));
411 pop(ebp);
412
413 LeaveExitFrameEpilogue();
414}
415
416
Steve Blocka7e24c12009-10-30 11:49:00 +0000417void MacroAssembler::PushTryHandler(CodeLocation try_location,
418 HandlerType type) {
419 // Adjust this code if not the case.
420 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
421 // The pc (return address) is already on TOS.
422 if (try_location == IN_JAVASCRIPT) {
423 if (type == TRY_CATCH_HANDLER) {
424 push(Immediate(StackHandler::TRY_CATCH));
425 } else {
426 push(Immediate(StackHandler::TRY_FINALLY));
427 }
428 push(ebp);
429 } else {
430 ASSERT(try_location == IN_JS_ENTRY);
431 // The frame pointer does not point to a JS frame so we save NULL
432 // for ebp. We expect the code throwing an exception to check ebp
433 // before dereferencing it to restore the context.
434 push(Immediate(StackHandler::ENTRY));
435 push(Immediate(0)); // NULL frame pointer.
436 }
437 // Save the current handler as the next handler.
438 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
439 // Link this handler as the new current one.
440 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
441}
442
443
Leon Clarkee46be812010-01-19 14:06:41 +0000444void MacroAssembler::PopTryHandler() {
445 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
446 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
447 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
448}
449
450
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100451void MacroAssembler::Throw(Register value) {
452 // Adjust this code if not the case.
453 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
454
455 // eax must hold the exception.
456 if (!value.is(eax)) {
457 mov(eax, value);
458 }
459
460 // Drop the sp to the top of the handler.
461 ExternalReference handler_address(Top::k_handler_address);
462 mov(esp, Operand::StaticVariable(handler_address));
463
464 // Restore next handler and frame pointer, discard handler state.
465 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
466 pop(Operand::StaticVariable(handler_address));
467 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
468 pop(ebp);
469 pop(edx); // Remove state.
470
471 // Before returning we restore the context from the frame pointer if
472 // not NULL. The frame pointer is NULL in the exception handler of
473 // a JS entry frame.
474 Set(esi, Immediate(0)); // Tentatively set context pointer to NULL.
475 NearLabel skip;
476 cmp(ebp, 0);
477 j(equal, &skip, not_taken);
478 mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
479 bind(&skip);
480
481 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
482 ret(0);
483}
484
485
486void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
487 Register value) {
488 // Adjust this code if not the case.
489 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
490
491 // eax must hold the exception.
492 if (!value.is(eax)) {
493 mov(eax, value);
494 }
495
496 // Drop sp to the top stack handler.
497 ExternalReference handler_address(Top::k_handler_address);
498 mov(esp, Operand::StaticVariable(handler_address));
499
500 // Unwind the handlers until the ENTRY handler is found.
501 NearLabel loop, done;
502 bind(&loop);
503 // Load the type of the current stack handler.
504 const int kStateOffset = StackHandlerConstants::kStateOffset;
505 cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
506 j(equal, &done);
507 // Fetch the next handler in the list.
508 const int kNextOffset = StackHandlerConstants::kNextOffset;
509 mov(esp, Operand(esp, kNextOffset));
510 jmp(&loop);
511 bind(&done);
512
513 // Set the top handler address to next handler past the current ENTRY handler.
514 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
515 pop(Operand::StaticVariable(handler_address));
516
517 if (type == OUT_OF_MEMORY) {
518 // Set external caught exception to false.
519 ExternalReference external_caught(Top::k_external_caught_exception_address);
520 mov(eax, false);
521 mov(Operand::StaticVariable(external_caught), eax);
522
523 // Set pending exception and eax to out of memory exception.
524 ExternalReference pending_exception(Top::k_pending_exception_address);
525 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
526 mov(Operand::StaticVariable(pending_exception), eax);
527 }
528
529 // Clear the context pointer.
530 Set(esi, Immediate(0));
531
532 // Restore fp from handler and discard handler state.
533 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
534 pop(ebp);
535 pop(edx); // State.
536
537 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
538 ret(0);
539}
540
541
Steve Blocka7e24c12009-10-30 11:49:00 +0000542void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
543 Register scratch,
544 Label* miss) {
545 Label same_contexts;
546
547 ASSERT(!holder_reg.is(scratch));
548
549 // Load current lexical context from the stack frame.
550 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
551
552 // When generating debug code, make sure the lexical context is set.
553 if (FLAG_debug_code) {
554 cmp(Operand(scratch), Immediate(0));
555 Check(not_equal, "we should not have an empty lexical context");
556 }
557 // Load the global context of the current context.
558 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
559 mov(scratch, FieldOperand(scratch, offset));
560 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
561
562 // Check the context is a global context.
563 if (FLAG_debug_code) {
564 push(scratch);
565 // Read the first word and compare to global_context_map.
566 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
567 cmp(scratch, Factory::global_context_map());
568 Check(equal, "JSGlobalObject::global_context should be a global context.");
569 pop(scratch);
570 }
571
572 // Check if both contexts are the same.
573 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
574 j(equal, &same_contexts, taken);
575
576 // Compare security tokens, save holder_reg on the stack so we can use it
577 // as a temporary register.
578 //
579 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
580 push(holder_reg);
581 // Check that the security token in the calling global object is
582 // compatible with the security token in the receiving global
583 // object.
584 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
585
586 // Check the context is a global context.
587 if (FLAG_debug_code) {
588 cmp(holder_reg, Factory::null_value());
589 Check(not_equal, "JSGlobalProxy::context() should not be null.");
590
591 push(holder_reg);
592 // Read the first word and compare to global_context_map(),
593 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
594 cmp(holder_reg, Factory::global_context_map());
595 Check(equal, "JSGlobalObject::global_context should be a global context.");
596 pop(holder_reg);
597 }
598
599 int token_offset = Context::kHeaderSize +
600 Context::SECURITY_TOKEN_INDEX * kPointerSize;
601 mov(scratch, FieldOperand(scratch, token_offset));
602 cmp(scratch, FieldOperand(holder_reg, token_offset));
603 pop(holder_reg);
604 j(not_equal, miss, not_taken);
605
606 bind(&same_contexts);
607}
608
609
610void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000611 Register scratch,
612 AllocationFlags flags) {
613 ExternalReference new_space_allocation_top =
614 ExternalReference::new_space_allocation_top_address();
615
616 // Just return if allocation top is already known.
617 if ((flags & RESULT_CONTAINS_TOP) != 0) {
618 // No use of scratch if allocation top is provided.
619 ASSERT(scratch.is(no_reg));
620#ifdef DEBUG
621 // Assert that result actually contains top on entry.
622 cmp(result, Operand::StaticVariable(new_space_allocation_top));
623 Check(equal, "Unexpected allocation top");
624#endif
625 return;
626 }
627
628 // Move address of new object to result. Use scratch register if available.
629 if (scratch.is(no_reg)) {
630 mov(result, Operand::StaticVariable(new_space_allocation_top));
631 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000632 mov(Operand(scratch), Immediate(new_space_allocation_top));
633 mov(result, Operand(scratch, 0));
634 }
635}
636
637
638void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
639 Register scratch) {
Steve Blockd0582a62009-12-15 09:54:21 +0000640 if (FLAG_debug_code) {
641 test(result_end, Immediate(kObjectAlignmentMask));
642 Check(zero, "Unaligned allocation in new space");
643 }
644
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 ExternalReference new_space_allocation_top =
646 ExternalReference::new_space_allocation_top_address();
647
648 // Update new top. Use scratch if available.
649 if (scratch.is(no_reg)) {
650 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
651 } else {
652 mov(Operand(scratch, 0), result_end);
653 }
654}
655
656
657void MacroAssembler::AllocateInNewSpace(int object_size,
658 Register result,
659 Register result_end,
660 Register scratch,
661 Label* gc_required,
662 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700663 if (!FLAG_inline_new) {
664 if (FLAG_debug_code) {
665 // Trash the registers to simulate an allocation failure.
666 mov(result, Immediate(0x7091));
667 if (result_end.is_valid()) {
668 mov(result_end, Immediate(0x7191));
669 }
670 if (scratch.is_valid()) {
671 mov(scratch, Immediate(0x7291));
672 }
673 }
674 jmp(gc_required);
675 return;
676 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000677 ASSERT(!result.is(result_end));
678
679 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800680 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000681
Ben Murdochbb769b22010-08-11 14:56:33 +0100682 Register top_reg = result_end.is_valid() ? result_end : result;
683
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 // Calculate new top and bail out if new space is exhausted.
685 ExternalReference new_space_allocation_limit =
686 ExternalReference::new_space_allocation_limit_address();
Ben Murdochbb769b22010-08-11 14:56:33 +0100687
Steve Block1e0659c2011-05-24 12:43:12 +0100688 if (!top_reg.is(result)) {
689 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +0100690 }
Steve Block1e0659c2011-05-24 12:43:12 +0100691 add(Operand(top_reg), Immediate(object_size));
692 j(carry, gc_required, not_taken);
Ben Murdochbb769b22010-08-11 14:56:33 +0100693 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 j(above, gc_required, not_taken);
695
Leon Clarkee46be812010-01-19 14:06:41 +0000696 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +0100697 UpdateAllocationTopHelper(top_reg, scratch);
698
699 // Tag result if requested.
700 if (top_reg.is(result)) {
701 if ((flags & TAG_OBJECT) != 0) {
702 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
703 } else {
704 sub(Operand(result), Immediate(object_size));
705 }
706 } else if ((flags & TAG_OBJECT) != 0) {
707 add(Operand(result), Immediate(kHeapObjectTag));
708 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000709}
710
711
712void MacroAssembler::AllocateInNewSpace(int header_size,
713 ScaleFactor element_size,
714 Register element_count,
715 Register result,
716 Register result_end,
717 Register scratch,
718 Label* gc_required,
719 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700720 if (!FLAG_inline_new) {
721 if (FLAG_debug_code) {
722 // Trash the registers to simulate an allocation failure.
723 mov(result, Immediate(0x7091));
724 mov(result_end, Immediate(0x7191));
725 if (scratch.is_valid()) {
726 mov(scratch, Immediate(0x7291));
727 }
728 // Register element_count is not modified by the function.
729 }
730 jmp(gc_required);
731 return;
732 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 ASSERT(!result.is(result_end));
734
735 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800736 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000737
738 // Calculate new top and bail out if new space is exhausted.
739 ExternalReference new_space_allocation_limit =
740 ExternalReference::new_space_allocation_limit_address();
Steve Block1e0659c2011-05-24 12:43:12 +0100741
742 // We assume that element_count*element_size + header_size does not
743 // overflow.
744 lea(result_end, Operand(element_count, element_size, header_size));
745 add(result_end, Operand(result));
746 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000747 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
748 j(above, gc_required);
749
Steve Blocka7e24c12009-10-30 11:49:00 +0000750 // Tag result if requested.
751 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000752 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 }
Leon Clarkee46be812010-01-19 14:06:41 +0000754
755 // Update allocation top.
756 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000757}
758
759
760void MacroAssembler::AllocateInNewSpace(Register object_size,
761 Register result,
762 Register result_end,
763 Register scratch,
764 Label* gc_required,
765 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700766 if (!FLAG_inline_new) {
767 if (FLAG_debug_code) {
768 // Trash the registers to simulate an allocation failure.
769 mov(result, Immediate(0x7091));
770 mov(result_end, Immediate(0x7191));
771 if (scratch.is_valid()) {
772 mov(scratch, Immediate(0x7291));
773 }
774 // object_size is left unchanged by this function.
775 }
776 jmp(gc_required);
777 return;
778 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000779 ASSERT(!result.is(result_end));
780
781 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800782 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000783
784 // Calculate new top and bail out if new space is exhausted.
785 ExternalReference new_space_allocation_limit =
786 ExternalReference::new_space_allocation_limit_address();
787 if (!object_size.is(result_end)) {
788 mov(result_end, object_size);
789 }
790 add(result_end, Operand(result));
Steve Block1e0659c2011-05-24 12:43:12 +0100791 j(carry, gc_required, not_taken);
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
793 j(above, gc_required, not_taken);
794
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 // Tag result if requested.
796 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000797 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000798 }
Leon Clarkee46be812010-01-19 14:06:41 +0000799
800 // Update allocation top.
801 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000802}
803
804
805void MacroAssembler::UndoAllocationInNewSpace(Register object) {
806 ExternalReference new_space_allocation_top =
807 ExternalReference::new_space_allocation_top_address();
808
809 // Make sure the object has no tag before resetting top.
810 and_(Operand(object), Immediate(~kHeapObjectTagMask));
811#ifdef DEBUG
812 cmp(object, Operand::StaticVariable(new_space_allocation_top));
813 Check(below, "Undo allocation of non allocated memory");
814#endif
815 mov(Operand::StaticVariable(new_space_allocation_top), object);
816}
817
818
Steve Block3ce2e202009-11-05 08:53:23 +0000819void MacroAssembler::AllocateHeapNumber(Register result,
820 Register scratch1,
821 Register scratch2,
822 Label* gc_required) {
823 // Allocate heap number in new space.
824 AllocateInNewSpace(HeapNumber::kSize,
825 result,
826 scratch1,
827 scratch2,
828 gc_required,
829 TAG_OBJECT);
830
831 // Set the map.
832 mov(FieldOperand(result, HeapObject::kMapOffset),
833 Immediate(Factory::heap_number_map()));
834}
835
836
Steve Blockd0582a62009-12-15 09:54:21 +0000837void MacroAssembler::AllocateTwoByteString(Register result,
838 Register length,
839 Register scratch1,
840 Register scratch2,
841 Register scratch3,
842 Label* gc_required) {
843 // Calculate the number of bytes needed for the characters in the string while
844 // observing object alignment.
845 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000846 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000847 // scratch1 = length * 2 + kObjectAlignmentMask.
848 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000849 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
850
851 // Allocate two byte string in new space.
852 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
853 times_1,
854 scratch1,
855 result,
856 scratch2,
857 scratch3,
858 gc_required,
859 TAG_OBJECT);
860
861 // Set the map, length and hash field.
862 mov(FieldOperand(result, HeapObject::kMapOffset),
863 Immediate(Factory::string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100864 mov(scratch1, length);
865 SmiTag(scratch1);
866 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000867 mov(FieldOperand(result, String::kHashFieldOffset),
868 Immediate(String::kEmptyHashField));
869}
870
871
872void MacroAssembler::AllocateAsciiString(Register result,
873 Register length,
874 Register scratch1,
875 Register scratch2,
876 Register scratch3,
877 Label* gc_required) {
878 // Calculate the number of bytes needed for the characters in the string while
879 // observing object alignment.
880 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
881 mov(scratch1, length);
882 ASSERT(kCharSize == 1);
883 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
884 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
885
886 // Allocate ascii string in new space.
887 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
888 times_1,
889 scratch1,
890 result,
891 scratch2,
892 scratch3,
893 gc_required,
894 TAG_OBJECT);
895
896 // Set the map, length and hash field.
897 mov(FieldOperand(result, HeapObject::kMapOffset),
898 Immediate(Factory::ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100899 mov(scratch1, length);
900 SmiTag(scratch1);
901 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000902 mov(FieldOperand(result, String::kHashFieldOffset),
903 Immediate(String::kEmptyHashField));
904}
905
906
Iain Merrick9ac36c92010-09-13 15:29:50 +0100907void MacroAssembler::AllocateAsciiString(Register result,
908 int length,
909 Register scratch1,
910 Register scratch2,
911 Label* gc_required) {
912 ASSERT(length > 0);
913
914 // Allocate ascii string in new space.
915 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
916 result,
917 scratch1,
918 scratch2,
919 gc_required,
920 TAG_OBJECT);
921
922 // Set the map, length and hash field.
923 mov(FieldOperand(result, HeapObject::kMapOffset),
924 Immediate(Factory::ascii_string_map()));
925 mov(FieldOperand(result, String::kLengthOffset),
926 Immediate(Smi::FromInt(length)));
927 mov(FieldOperand(result, String::kHashFieldOffset),
928 Immediate(String::kEmptyHashField));
929}
930
931
Steve Blockd0582a62009-12-15 09:54:21 +0000932void MacroAssembler::AllocateConsString(Register result,
933 Register scratch1,
934 Register scratch2,
935 Label* gc_required) {
936 // Allocate heap number in new space.
937 AllocateInNewSpace(ConsString::kSize,
938 result,
939 scratch1,
940 scratch2,
941 gc_required,
942 TAG_OBJECT);
943
944 // Set the map. The other fields are left uninitialized.
945 mov(FieldOperand(result, HeapObject::kMapOffset),
946 Immediate(Factory::cons_string_map()));
947}
948
949
950void MacroAssembler::AllocateAsciiConsString(Register result,
951 Register scratch1,
952 Register scratch2,
953 Label* gc_required) {
954 // Allocate heap number in new space.
955 AllocateInNewSpace(ConsString::kSize,
956 result,
957 scratch1,
958 scratch2,
959 gc_required,
960 TAG_OBJECT);
961
962 // Set the map. The other fields are left uninitialized.
963 mov(FieldOperand(result, HeapObject::kMapOffset),
964 Immediate(Factory::cons_ascii_string_map()));
965}
966
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800967
Ben Murdochb8e0da22011-05-16 14:20:40 +0100968// Copy memory, byte-by-byte, from source to destination. Not optimized for
969// long or aligned copies. The contents of scratch and length are destroyed.
970// Source and destination are incremented by length.
971// Many variants of movsb, loop unrolling, word moves, and indexed operands
972// have been tried here already, and this is fastest.
973// A simpler loop is faster on small copies, but 30% slower on large ones.
974// The cld() instruction must have been emitted, to set the direction flag(),
975// before calling this function.
976void MacroAssembler::CopyBytes(Register source,
977 Register destination,
978 Register length,
979 Register scratch) {
980 Label loop, done, short_string, short_loop;
981 // Experimentation shows that the short string loop is faster if length < 10.
982 cmp(Operand(length), Immediate(10));
983 j(less_equal, &short_string);
984
985 ASSERT(source.is(esi));
986 ASSERT(destination.is(edi));
987 ASSERT(length.is(ecx));
988
989 // Because source is 4-byte aligned in our uses of this function,
990 // we keep source aligned for the rep_movs call by copying the odd bytes
991 // at the end of the ranges.
992 mov(scratch, Operand(source, length, times_1, -4));
993 mov(Operand(destination, length, times_1, -4), scratch);
994 mov(scratch, ecx);
995 shr(ecx, 2);
996 rep_movs();
997 and_(Operand(scratch), Immediate(0x3));
998 add(destination, Operand(scratch));
999 jmp(&done);
1000
1001 bind(&short_string);
1002 test(length, Operand(length));
1003 j(zero, &done);
1004
1005 bind(&short_loop);
1006 mov_b(scratch, Operand(source, 0));
1007 mov_b(Operand(destination, 0), scratch);
1008 inc(source);
1009 inc(destination);
1010 dec(length);
1011 j(not_zero, &short_loop);
1012
1013 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001014}
1015
Steve Blockd0582a62009-12-15 09:54:21 +00001016
Steve Blocka7e24c12009-10-30 11:49:00 +00001017void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
1018 Register result,
1019 Register op,
1020 JumpTarget* then_target) {
1021 JumpTarget ok;
1022 test(result, Operand(result));
1023 ok.Branch(not_zero, taken);
1024 test(op, Operand(op));
1025 then_target->Branch(sign, not_taken);
1026 ok.Bind();
1027}
1028
1029
1030void MacroAssembler::NegativeZeroTest(Register result,
1031 Register op,
1032 Label* then_label) {
1033 Label ok;
1034 test(result, Operand(result));
1035 j(not_zero, &ok, taken);
1036 test(op, Operand(op));
1037 j(sign, then_label, not_taken);
1038 bind(&ok);
1039}
1040
1041
1042void MacroAssembler::NegativeZeroTest(Register result,
1043 Register op1,
1044 Register op2,
1045 Register scratch,
1046 Label* then_label) {
1047 Label ok;
1048 test(result, Operand(result));
1049 j(not_zero, &ok, taken);
1050 mov(scratch, Operand(op1));
1051 or_(scratch, Operand(op2));
1052 j(sign, then_label, not_taken);
1053 bind(&ok);
1054}
1055
1056
1057void MacroAssembler::TryGetFunctionPrototype(Register function,
1058 Register result,
1059 Register scratch,
1060 Label* miss) {
1061 // Check that the receiver isn't a smi.
1062 test(function, Immediate(kSmiTagMask));
1063 j(zero, miss, not_taken);
1064
1065 // Check that the function really is a function.
1066 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1067 j(not_equal, miss, not_taken);
1068
1069 // Make sure that the function has an instance prototype.
1070 Label non_instance;
1071 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1072 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1073 j(not_zero, &non_instance, not_taken);
1074
1075 // Get the prototype or initial map from the function.
1076 mov(result,
1077 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1078
1079 // If the prototype or initial map is the hole, don't return it and
1080 // simply miss the cache instead. This will allow us to allocate a
1081 // prototype object on-demand in the runtime system.
1082 cmp(Operand(result), Immediate(Factory::the_hole_value()));
1083 j(equal, miss, not_taken);
1084
1085 // If the function does not have an initial map, we're done.
1086 Label done;
1087 CmpObjectType(result, MAP_TYPE, scratch);
1088 j(not_equal, &done);
1089
1090 // Get the prototype from the initial map.
1091 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1092 jmp(&done);
1093
1094 // Non-instance prototype: Fetch prototype from constructor field
1095 // in initial map.
1096 bind(&non_instance);
1097 mov(result, FieldOperand(result, Map::kConstructorOffset));
1098
1099 // All done.
1100 bind(&done);
1101}
1102
1103
1104void MacroAssembler::CallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001105 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 call(stub->GetCode(), RelocInfo::CODE_TARGET);
1107}
1108
1109
John Reck59135872010-11-02 12:39:01 -07001110MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001111 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001112 Object* result;
1113 { MaybeObject* maybe_result = stub->TryGetCode();
1114 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001115 }
John Reck59135872010-11-02 12:39:01 -07001116 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001117 return result;
1118}
1119
1120
Steve Blockd0582a62009-12-15 09:54:21 +00001121void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001122 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +00001123 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1124}
1125
1126
John Reck59135872010-11-02 12:39:01 -07001127MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001128 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001129 Object* result;
1130 { MaybeObject* maybe_result = stub->TryGetCode();
1131 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001132 }
John Reck59135872010-11-02 12:39:01 -07001133 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001134 return result;
1135}
1136
1137
Steve Blocka7e24c12009-10-30 11:49:00 +00001138void MacroAssembler::StubReturn(int argc) {
1139 ASSERT(argc >= 1 && generating_stub());
1140 ret((argc - 1) * kPointerSize);
1141}
1142
1143
1144void MacroAssembler::IllegalOperation(int num_arguments) {
1145 if (num_arguments > 0) {
1146 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1147 }
1148 mov(eax, Immediate(Factory::undefined_value()));
1149}
1150
1151
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001152void MacroAssembler::IndexFromHash(Register hash, Register index) {
1153 // The assert checks that the constants for the maximum number of digits
1154 // for an array index cached in the hash field and the number of bits
1155 // reserved for it does not conflict.
1156 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1157 (1 << String::kArrayIndexValueBits));
1158 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1159 // the low kHashShift bits.
1160 and_(hash, String::kArrayIndexValueMask);
1161 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1162 if (String::kHashShift > kSmiTagSize) {
1163 shr(hash, String::kHashShift - kSmiTagSize);
1164 }
1165 if (!index.is(hash)) {
1166 mov(index, hash);
1167 }
1168}
1169
1170
Steve Blocka7e24c12009-10-30 11:49:00 +00001171void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1172 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1173}
1174
1175
Ben Murdochb0fe1622011-05-05 13:52:32 +01001176void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1177 Runtime::Function* function = Runtime::FunctionForId(id);
1178 Set(eax, Immediate(function->nargs));
1179 mov(ebx, Immediate(ExternalReference(function)));
1180 CEntryStub ces(1);
1181 ces.SaveDoubles();
1182 CallStub(&ces);
1183}
1184
1185
John Reck59135872010-11-02 12:39:01 -07001186MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1187 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001188 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1189}
1190
1191
Steve Blocka7e24c12009-10-30 11:49:00 +00001192void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1193 // If the expected number of arguments of the runtime function is
1194 // constant, we check that the actual number of arguments match the
1195 // expectation.
1196 if (f->nargs >= 0 && f->nargs != num_arguments) {
1197 IllegalOperation(num_arguments);
1198 return;
1199 }
1200
Leon Clarke4515c472010-02-03 11:58:03 +00001201 // TODO(1236192): Most runtime routines don't need the number of
1202 // arguments passed in because it is constant. At some point we
1203 // should remove this need and make the runtime routine entry code
1204 // smarter.
1205 Set(eax, Immediate(num_arguments));
1206 mov(ebx, Immediate(ExternalReference(f)));
1207 CEntryStub ces(1);
1208 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001209}
1210
1211
John Reck59135872010-11-02 12:39:01 -07001212MaybeObject* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1213 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001214 if (f->nargs >= 0 && f->nargs != num_arguments) {
1215 IllegalOperation(num_arguments);
1216 // Since we did not call the stub, there was no allocation failure.
1217 // Return some non-failure object.
1218 return Heap::undefined_value();
1219 }
1220
Leon Clarke4515c472010-02-03 11:58:03 +00001221 // TODO(1236192): Most runtime routines don't need the number of
1222 // arguments passed in because it is constant. At some point we
1223 // should remove this need and make the runtime routine entry code
1224 // smarter.
1225 Set(eax, Immediate(num_arguments));
1226 mov(ebx, Immediate(ExternalReference(f)));
1227 CEntryStub ces(1);
1228 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001229}
1230
1231
Ben Murdochbb769b22010-08-11 14:56:33 +01001232void MacroAssembler::CallExternalReference(ExternalReference ref,
1233 int num_arguments) {
1234 mov(eax, Immediate(num_arguments));
1235 mov(ebx, Immediate(ref));
1236
1237 CEntryStub stub(1);
1238 CallStub(&stub);
1239}
1240
1241
Steve Block6ded16b2010-05-10 14:33:55 +01001242void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1243 int num_arguments,
1244 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001245 // TODO(1236192): Most runtime routines don't need the number of
1246 // arguments passed in because it is constant. At some point we
1247 // should remove this need and make the runtime routine entry code
1248 // smarter.
1249 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001250 JumpToExternalReference(ext);
1251}
1252
1253
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001254MaybeObject* MacroAssembler::TryTailCallExternalReference(
1255 const ExternalReference& ext, int num_arguments, int result_size) {
1256 // TODO(1236192): Most runtime routines don't need the number of
1257 // arguments passed in because it is constant. At some point we
1258 // should remove this need and make the runtime routine entry code
1259 // smarter.
1260 Set(eax, Immediate(num_arguments));
1261 return TryJumpToExternalReference(ext);
1262}
1263
1264
Steve Block6ded16b2010-05-10 14:33:55 +01001265void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1266 int num_arguments,
1267 int result_size) {
1268 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001269}
1270
1271
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001272MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1273 int num_arguments,
1274 int result_size) {
1275 return TryTailCallExternalReference(
1276 ExternalReference(fid), num_arguments, result_size);
1277}
1278
1279
Ben Murdochb0fe1622011-05-05 13:52:32 +01001280// If true, a Handle<T> returned by value from a function with cdecl calling
1281// convention will be returned directly as a value of location_ field in a
1282// register eax.
1283// If false, it is returned as a pointer to a preallocated by caller memory
1284// region. Pointer to this region should be passed to a function as an
1285// implicit first argument.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001286#if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001287static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001288#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001289static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001290#endif
1291
1292
1293Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001294 return Operand(
1295 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001296}
1297
1298
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001299void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001300 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001301 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001302 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001303 // space for and pass an out parameter.
1304 } else {
1305 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001306 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001307
John Reck59135872010-11-02 12:39:01 -07001308 // The argument slots are filled as follows:
1309 //
1310 // n + 1: output cell
1311 // n: arg n
1312 // ...
1313 // 1: arg1
1314 // 0: pointer to the output cell
1315 //
1316 // Note that this is one more "argument" than the function expects
1317 // so the out cell will have to be popped explicitly after returning
1318 // from the function. The out cell contains Handle.
John Reck59135872010-11-02 12:39:01 -07001319
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001320 // pointer to out cell.
1321 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1322 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
1323 if (FLAG_debug_code) {
1324 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1325 }
1326 }
1327}
1328
1329
1330MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1331 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001332 ExternalReference next_address =
1333 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001334 ExternalReference limit_address =
1335 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001336 ExternalReference level_address =
1337 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001338
John Reck59135872010-11-02 12:39:01 -07001339 // Allocate HandleScope in callee-save registers.
1340 mov(ebx, Operand::StaticVariable(next_address));
1341 mov(edi, Operand::StaticVariable(limit_address));
1342 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001343
John Reck59135872010-11-02 12:39:01 -07001344 // Call the api function!
1345 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1346
Ben Murdochb0fe1622011-05-05 13:52:32 +01001347 if (!kReturnHandlesDirectly) {
John Reck59135872010-11-02 12:39:01 -07001348 // The returned value is a pointer to the handle holding the result.
1349 // Dereference this to get to the location.
1350 mov(eax, Operand(eax, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001351 }
Steve Blockd0582a62009-12-15 09:54:21 +00001352
John Reck59135872010-11-02 12:39:01 -07001353 Label empty_handle;
1354 Label prologue;
1355 Label promote_scheduled_exception;
1356 Label delete_allocated_handles;
1357 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001358
John Reck59135872010-11-02 12:39:01 -07001359 // Check if the result handle holds 0.
1360 test(eax, Operand(eax));
1361 j(zero, &empty_handle, not_taken);
1362 // It was non-zero. Dereference to get the result value.
1363 mov(eax, Operand(eax, 0));
1364 bind(&prologue);
1365 // No more valid handles (the result handle was the last one). Restore
1366 // previous handle scope.
1367 mov(Operand::StaticVariable(next_address), ebx);
1368 sub(Operand::StaticVariable(level_address), Immediate(1));
1369 Assert(above_equal, "Invalid HandleScope level");
1370 cmp(edi, Operand::StaticVariable(limit_address));
1371 j(not_equal, &delete_allocated_handles, not_taken);
1372 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001373
John Reck59135872010-11-02 12:39:01 -07001374 // Check if the function scheduled an exception.
1375 ExternalReference scheduled_exception_address =
1376 ExternalReference::scheduled_exception_address();
1377 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block1e0659c2011-05-24 12:43:12 +01001378 Immediate(Factory::the_hole_value()));
John Reck59135872010-11-02 12:39:01 -07001379 j(not_equal, &promote_scheduled_exception, not_taken);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001380 LeaveApiExitFrame();
1381 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001382 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001383 MaybeObject* result =
1384 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1385 if (result->IsFailure()) {
1386 return result;
1387 }
John Reck59135872010-11-02 12:39:01 -07001388 bind(&empty_handle);
1389 // It was zero; the result is undefined.
1390 mov(eax, Factory::undefined_value());
1391 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001392
John Reck59135872010-11-02 12:39:01 -07001393 // HandleScope limit has changed. Delete allocated extensions.
1394 bind(&delete_allocated_handles);
1395 mov(Operand::StaticVariable(limit_address), edi);
1396 mov(edi, eax);
1397 mov(eax, Immediate(ExternalReference::delete_handle_scope_extensions()));
1398 call(Operand(eax));
1399 mov(eax, edi);
1400 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001401
1402 return result;
Steve Blockd0582a62009-12-15 09:54:21 +00001403}
1404
1405
Steve Block6ded16b2010-05-10 14:33:55 +01001406void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 // Set the entry point and jump to the C entry runtime stub.
1408 mov(ebx, Immediate(ext));
1409 CEntryStub ces(1);
1410 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1411}
1412
1413
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001414MaybeObject* MacroAssembler::TryJumpToExternalReference(
1415 const ExternalReference& ext) {
1416 // Set the entry point and jump to the C entry runtime stub.
1417 mov(ebx, Immediate(ext));
1418 CEntryStub ces(1);
1419 return TryTailCallStub(&ces);
1420}
1421
1422
Steve Blocka7e24c12009-10-30 11:49:00 +00001423void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1424 const ParameterCount& actual,
1425 Handle<Code> code_constant,
1426 const Operand& code_operand,
1427 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001428 InvokeFlag flag,
1429 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001430 bool definitely_matches = false;
1431 Label invoke;
1432 if (expected.is_immediate()) {
1433 ASSERT(actual.is_immediate());
1434 if (expected.immediate() == actual.immediate()) {
1435 definitely_matches = true;
1436 } else {
1437 mov(eax, actual.immediate());
1438 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1439 if (expected.immediate() == sentinel) {
1440 // Don't worry about adapting arguments for builtins that
1441 // don't want that done. Skip adaption code by making it look
1442 // like we have a match between expected and actual number of
1443 // arguments.
1444 definitely_matches = true;
1445 } else {
1446 mov(ebx, expected.immediate());
1447 }
1448 }
1449 } else {
1450 if (actual.is_immediate()) {
1451 // Expected is in register, actual is immediate. This is the
1452 // case when we invoke function values without going through the
1453 // IC mechanism.
1454 cmp(expected.reg(), actual.immediate());
1455 j(equal, &invoke);
1456 ASSERT(expected.reg().is(ebx));
1457 mov(eax, actual.immediate());
1458 } else if (!expected.reg().is(actual.reg())) {
1459 // Both expected and actual are in (different) registers. This
1460 // is the case when we invoke functions using call and apply.
1461 cmp(expected.reg(), Operand(actual.reg()));
1462 j(equal, &invoke);
1463 ASSERT(actual.reg().is(eax));
1464 ASSERT(expected.reg().is(ebx));
1465 }
1466 }
1467
1468 if (!definitely_matches) {
1469 Handle<Code> adaptor =
1470 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1471 if (!code_constant.is_null()) {
1472 mov(edx, Immediate(code_constant));
1473 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1474 } else if (!code_operand.is_reg(edx)) {
1475 mov(edx, code_operand);
1476 }
1477
1478 if (flag == CALL_FUNCTION) {
1479 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001480 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001481 jmp(done);
1482 } else {
1483 jmp(adaptor, RelocInfo::CODE_TARGET);
1484 }
1485 bind(&invoke);
1486 }
1487}
1488
1489
1490void MacroAssembler::InvokeCode(const Operand& code,
1491 const ParameterCount& expected,
1492 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001493 InvokeFlag flag,
1494 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001495 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001496 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1497 &done, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001498 if (flag == CALL_FUNCTION) {
1499 call(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001500 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001501 } else {
1502 ASSERT(flag == JUMP_FUNCTION);
1503 jmp(code);
1504 }
1505 bind(&done);
1506}
1507
1508
1509void MacroAssembler::InvokeCode(Handle<Code> code,
1510 const ParameterCount& expected,
1511 const ParameterCount& actual,
1512 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001513 InvokeFlag flag,
1514 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 Label done;
1516 Operand dummy(eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001517 InvokePrologue(expected, actual, code, dummy, &done,
1518 flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001519 if (flag == CALL_FUNCTION) {
1520 call(code, rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001521 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001522 } else {
1523 ASSERT(flag == JUMP_FUNCTION);
1524 jmp(code, rmode);
1525 }
1526 bind(&done);
1527}
1528
1529
1530void MacroAssembler::InvokeFunction(Register fun,
1531 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001532 InvokeFlag flag,
1533 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 ASSERT(fun.is(edi));
1535 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1536 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1537 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001538 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001539
1540 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01001541 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001542 expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001543}
1544
1545
Andrei Popescu402d9372010-02-26 13:31:12 +00001546void MacroAssembler::InvokeFunction(JSFunction* function,
1547 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001548 InvokeFlag flag,
1549 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001550 ASSERT(function->is_compiled());
1551 // Get the function and setup the context.
1552 mov(edi, Immediate(Handle<JSFunction>(function)));
1553 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001554
Andrei Popescu402d9372010-02-26 13:31:12 +00001555 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001556 if (V8::UseCrankshaft()) {
1557 // TODO(kasperl): For now, we always call indirectly through the
1558 // code field in the function to allow recompilation to take effect
1559 // without changing any of the call sites.
1560 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1561 expected, actual, flag, post_call_generator);
1562 } else {
1563 Handle<Code> code(function->code());
1564 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
1565 flag, post_call_generator);
1566 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001567}
1568
1569
Ben Murdochb0fe1622011-05-05 13:52:32 +01001570void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1571 InvokeFlag flag,
1572 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001573 // Calls are not allowed in some stubs.
1574 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1575
1576 // Rely on the assertion to check that the number of provided
1577 // arguments match the expected number of arguments. Fake a
1578 // parameter count to avoid emitting code to do the check.
1579 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01001580 GetBuiltinFunction(edi, id);
1581 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001582 expected, expected, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001583}
1584
Steve Block791712a2010-08-27 10:21:07 +01001585void MacroAssembler::GetBuiltinFunction(Register target,
1586 Builtins::JavaScript id) {
1587 // Load the JavaScript builtin function from the builtins object.
1588 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1589 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1590 mov(target, FieldOperand(target,
1591 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1592}
Steve Blocka7e24c12009-10-30 11:49:00 +00001593
1594void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001595 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00001596 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01001597 GetBuiltinFunction(edi, id);
1598 // Load the code entry point from the function into the target register.
1599 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001600}
1601
1602
Steve Blockd0582a62009-12-15 09:54:21 +00001603void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1604 if (context_chain_length > 0) {
1605 // Move up the chain of contexts to the context containing the slot.
1606 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1607 // Load the function context (which is the incoming, outer context).
1608 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1609 for (int i = 1; i < context_chain_length; i++) {
1610 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1611 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1612 }
Steve Block1e0659c2011-05-24 12:43:12 +01001613 } else {
1614 // Slot is in the current function context. Move it into the
1615 // destination register in case we store into it (the write barrier
1616 // cannot be allowed to destroy the context in esi).
1617 mov(dst, esi);
1618 }
1619
1620 // We should not have found a 'with' context by walking the context chain
1621 // (i.e., the static scope chain and runtime context chain do not agree).
1622 // A variable occurring in such a scope should have slot type LOOKUP and
1623 // not CONTEXT.
1624 if (FLAG_debug_code) {
1625 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1626 Check(equal, "Yo dawg, I heard you liked function contexts "
1627 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00001628 }
1629}
1630
1631
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001632void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1633 // Load the global or builtins object from the current context.
1634 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1635 // Load the global context from the global or builtins object.
1636 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1637 // Load the function from the global context.
1638 mov(function, Operand(function, Context::SlotOffset(index)));
1639}
1640
1641
1642void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1643 Register map) {
1644 // Load the initial map. The global functions all have initial maps.
1645 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1646 if (FLAG_debug_code) {
1647 Label ok, fail;
1648 CheckMap(map, Factory::meta_map(), &fail, false);
1649 jmp(&ok);
1650 bind(&fail);
1651 Abort("Global functions must have initial map");
1652 bind(&ok);
1653 }
1654}
1655
Steve Blockd0582a62009-12-15 09:54:21 +00001656
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001657// Store the value in register src in the safepoint register stack
1658// slot for register dst.
1659void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
1660 mov(SafepointRegisterSlot(dst), src);
1661}
1662
1663
1664void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
1665 mov(SafepointRegisterSlot(dst), src);
1666}
1667
1668
1669void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
1670 mov(dst, SafepointRegisterSlot(src));
1671}
1672
1673
1674Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
1675 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
1676}
1677
1678
Ben Murdochb0fe1622011-05-05 13:52:32 +01001679int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1680 // The registers are pushed starting with the lowest encoding,
1681 // which means that lowest encodings are furthest away from
1682 // the stack pointer.
1683 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1684 return kNumSafepointRegisters - reg_code - 1;
1685}
1686
1687
Steve Blocka7e24c12009-10-30 11:49:00 +00001688void MacroAssembler::Ret() {
1689 ret(0);
1690}
1691
1692
Steve Block1e0659c2011-05-24 12:43:12 +01001693void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1694 if (is_uint16(bytes_dropped)) {
1695 ret(bytes_dropped);
1696 } else {
1697 pop(scratch);
1698 add(Operand(esp), Immediate(bytes_dropped));
1699 push(scratch);
1700 ret(0);
1701 }
1702}
1703
1704
1705
1706
Leon Clarkee46be812010-01-19 14:06:41 +00001707void MacroAssembler::Drop(int stack_elements) {
1708 if (stack_elements > 0) {
1709 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1710 }
1711}
1712
1713
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001714void MacroAssembler::Move(Register dst, Register src) {
1715 if (!dst.is(src)) {
1716 mov(dst, src);
1717 }
1718}
1719
1720
Leon Clarkee46be812010-01-19 14:06:41 +00001721void MacroAssembler::Move(Register dst, Handle<Object> value) {
1722 mov(dst, value);
1723}
1724
1725
Steve Blocka7e24c12009-10-30 11:49:00 +00001726void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1727 if (FLAG_native_code_counters && counter->Enabled()) {
1728 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1729 }
1730}
1731
1732
1733void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1734 ASSERT(value > 0);
1735 if (FLAG_native_code_counters && counter->Enabled()) {
1736 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1737 if (value == 1) {
1738 inc(operand);
1739 } else {
1740 add(operand, Immediate(value));
1741 }
1742 }
1743}
1744
1745
1746void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1747 ASSERT(value > 0);
1748 if (FLAG_native_code_counters && counter->Enabled()) {
1749 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1750 if (value == 1) {
1751 dec(operand);
1752 } else {
1753 sub(operand, Immediate(value));
1754 }
1755 }
1756}
1757
1758
Leon Clarked91b9f72010-01-27 17:25:45 +00001759void MacroAssembler::IncrementCounter(Condition cc,
1760 StatsCounter* counter,
1761 int value) {
1762 ASSERT(value > 0);
1763 if (FLAG_native_code_counters && counter->Enabled()) {
1764 Label skip;
1765 j(NegateCondition(cc), &skip);
1766 pushfd();
1767 IncrementCounter(counter, value);
1768 popfd();
1769 bind(&skip);
1770 }
1771}
1772
1773
1774void MacroAssembler::DecrementCounter(Condition cc,
1775 StatsCounter* counter,
1776 int value) {
1777 ASSERT(value > 0);
1778 if (FLAG_native_code_counters && counter->Enabled()) {
1779 Label skip;
1780 j(NegateCondition(cc), &skip);
1781 pushfd();
1782 DecrementCounter(counter, value);
1783 popfd();
1784 bind(&skip);
1785 }
1786}
1787
1788
Steve Blocka7e24c12009-10-30 11:49:00 +00001789void MacroAssembler::Assert(Condition cc, const char* msg) {
1790 if (FLAG_debug_code) Check(cc, msg);
1791}
1792
1793
Iain Merrick75681382010-08-19 15:07:18 +01001794void MacroAssembler::AssertFastElements(Register elements) {
1795 if (FLAG_debug_code) {
1796 Label ok;
1797 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1798 Immediate(Factory::fixed_array_map()));
1799 j(equal, &ok);
1800 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1801 Immediate(Factory::fixed_cow_array_map()));
1802 j(equal, &ok);
1803 Abort("JSObject with fast elements map has slow elements");
1804 bind(&ok);
1805 }
1806}
1807
1808
Steve Blocka7e24c12009-10-30 11:49:00 +00001809void MacroAssembler::Check(Condition cc, const char* msg) {
1810 Label L;
1811 j(cc, &L, taken);
1812 Abort(msg);
1813 // will not return here
1814 bind(&L);
1815}
1816
1817
Steve Block6ded16b2010-05-10 14:33:55 +01001818void MacroAssembler::CheckStackAlignment() {
1819 int frame_alignment = OS::ActivationFrameAlignment();
1820 int frame_alignment_mask = frame_alignment - 1;
1821 if (frame_alignment > kPointerSize) {
1822 ASSERT(IsPowerOf2(frame_alignment));
1823 Label alignment_as_expected;
1824 test(esp, Immediate(frame_alignment_mask));
1825 j(zero, &alignment_as_expected);
1826 // Abort if stack is not aligned.
1827 int3();
1828 bind(&alignment_as_expected);
1829 }
1830}
1831
1832
Steve Blocka7e24c12009-10-30 11:49:00 +00001833void MacroAssembler::Abort(const char* msg) {
1834 // We want to pass the msg string like a smi to avoid GC
1835 // problems, however msg is not guaranteed to be aligned
1836 // properly. Instead, we pass an aligned pointer that is
1837 // a proper v8 smi, but also pass the alignment difference
1838 // from the real pointer as a smi.
1839 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1840 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1841 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1842#ifdef DEBUG
1843 if (msg != NULL) {
1844 RecordComment("Abort message: ");
1845 RecordComment(msg);
1846 }
1847#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001848 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001849 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001850
Steve Blocka7e24c12009-10-30 11:49:00 +00001851 push(eax);
1852 push(Immediate(p0));
1853 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1854 CallRuntime(Runtime::kAbort, 2);
1855 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00001856 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00001857}
1858
1859
Iain Merrick75681382010-08-19 15:07:18 +01001860void MacroAssembler::JumpIfNotNumber(Register reg,
1861 TypeInfo info,
1862 Label* on_not_number) {
1863 if (FLAG_debug_code) AbortIfSmi(reg);
1864 if (!info.IsNumber()) {
1865 cmp(FieldOperand(reg, HeapObject::kMapOffset),
1866 Factory::heap_number_map());
1867 j(not_equal, on_not_number);
1868 }
1869}
1870
1871
1872void MacroAssembler::ConvertToInt32(Register dst,
1873 Register source,
1874 Register scratch,
1875 TypeInfo info,
1876 Label* on_not_int32) {
1877 if (FLAG_debug_code) {
1878 AbortIfSmi(source);
1879 AbortIfNotNumber(source);
1880 }
1881 if (info.IsInteger32()) {
1882 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1883 } else {
1884 Label done;
1885 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1886 ASSERT(!scratch.is(source));
1887 if (push_pop) {
1888 push(dst);
1889 scratch = dst;
1890 }
1891 if (scratch.is(no_reg)) scratch = dst;
1892 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1893 cmp(scratch, 0x80000000u);
1894 if (push_pop) {
1895 j(not_equal, &done);
1896 pop(dst);
1897 jmp(on_not_int32);
1898 } else {
1899 j(equal, on_not_int32);
1900 }
1901
1902 bind(&done);
1903 if (push_pop) {
1904 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1905 }
1906 if (!scratch.is(dst)) {
1907 mov(dst, scratch);
1908 }
1909 }
1910}
1911
1912
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001913void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1914 Register scratch,
1915 int power) {
1916 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1917 HeapNumber::kExponentBits));
1918 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1919 movd(dst, Operand(scratch));
1920 psllq(dst, HeapNumber::kMantissaBits);
1921}
1922
1923
Andrei Popescu402d9372010-02-26 13:31:12 +00001924void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1925 Register instance_type,
1926 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01001927 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001928 if (!scratch.is(instance_type)) {
1929 mov(scratch, instance_type);
1930 }
1931 and_(scratch,
1932 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1933 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1934 j(not_equal, failure);
1935}
1936
1937
Leon Clarked91b9f72010-01-27 17:25:45 +00001938void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1939 Register object2,
1940 Register scratch1,
1941 Register scratch2,
1942 Label* failure) {
1943 // Check that both objects are not smis.
1944 ASSERT_EQ(0, kSmiTag);
1945 mov(scratch1, Operand(object1));
1946 and_(scratch1, Operand(object2));
1947 test(scratch1, Immediate(kSmiTagMask));
1948 j(zero, failure);
1949
1950 // Load instance type for both strings.
1951 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1952 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1953 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1954 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1955
1956 // Check that both are flat ascii strings.
1957 const int kFlatAsciiStringMask =
1958 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1959 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1960 // Interleave bits from both instance types and compare them in one check.
1961 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1962 and_(scratch1, kFlatAsciiStringMask);
1963 and_(scratch2, kFlatAsciiStringMask);
1964 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1965 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1966 j(not_equal, failure);
1967}
1968
1969
Steve Block6ded16b2010-05-10 14:33:55 +01001970void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1971 int frameAlignment = OS::ActivationFrameAlignment();
1972 if (frameAlignment != 0) {
1973 // Make stack end at alignment and make room for num_arguments words
1974 // and the original value of esp.
1975 mov(scratch, esp);
1976 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1977 ASSERT(IsPowerOf2(frameAlignment));
1978 and_(esp, -frameAlignment);
1979 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1980 } else {
1981 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1982 }
1983}
1984
1985
1986void MacroAssembler::CallCFunction(ExternalReference function,
1987 int num_arguments) {
1988 // Trashing eax is ok as it will be the return value.
1989 mov(Operand(eax), Immediate(function));
1990 CallCFunction(eax, num_arguments);
1991}
1992
1993
1994void MacroAssembler::CallCFunction(Register function,
1995 int num_arguments) {
1996 // Check stack alignment.
1997 if (FLAG_debug_code) {
1998 CheckStackAlignment();
1999 }
2000
2001 call(Operand(function));
2002 if (OS::ActivationFrameAlignment() != 0) {
2003 mov(esp, Operand(esp, num_arguments * kPointerSize));
2004 } else {
2005 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
2006 }
2007}
2008
2009
Steve Blocka7e24c12009-10-30 11:49:00 +00002010CodePatcher::CodePatcher(byte* address, int size)
2011 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
2012 // Create a new macro assembler pointing to the address of the code to patch.
2013 // The size is adjusted with kGap on order for the assembler to generate size
2014 // bytes of instructions without failing with buffer size constraints.
2015 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2016}
2017
2018
2019CodePatcher::~CodePatcher() {
2020 // Indicate that code has changed.
2021 CPU::FlushICache(address_, size_);
2022
2023 // Check that the code was patched as expected.
2024 ASSERT(masm_.pc_ == address_ + size_);
2025 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2026}
2027
2028
2029} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002030
2031#endif // V8_TARGET_ARCH_IA32