blob: cd612b52b5dc0d0114214e078b1a935a91c3a8d0 [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
Steve Blocka7e24c12009-10-30 11:49:00 +0000451void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
452 Register scratch,
453 Label* miss) {
454 Label same_contexts;
455
456 ASSERT(!holder_reg.is(scratch));
457
458 // Load current lexical context from the stack frame.
459 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
460
461 // When generating debug code, make sure the lexical context is set.
462 if (FLAG_debug_code) {
463 cmp(Operand(scratch), Immediate(0));
464 Check(not_equal, "we should not have an empty lexical context");
465 }
466 // Load the global context of the current context.
467 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
468 mov(scratch, FieldOperand(scratch, offset));
469 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
470
471 // Check the context is a global context.
472 if (FLAG_debug_code) {
473 push(scratch);
474 // Read the first word and compare to global_context_map.
475 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
476 cmp(scratch, Factory::global_context_map());
477 Check(equal, "JSGlobalObject::global_context should be a global context.");
478 pop(scratch);
479 }
480
481 // Check if both contexts are the same.
482 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
483 j(equal, &same_contexts, taken);
484
485 // Compare security tokens, save holder_reg on the stack so we can use it
486 // as a temporary register.
487 //
488 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
489 push(holder_reg);
490 // Check that the security token in the calling global object is
491 // compatible with the security token in the receiving global
492 // object.
493 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
494
495 // Check the context is a global context.
496 if (FLAG_debug_code) {
497 cmp(holder_reg, Factory::null_value());
498 Check(not_equal, "JSGlobalProxy::context() should not be null.");
499
500 push(holder_reg);
501 // Read the first word and compare to global_context_map(),
502 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
503 cmp(holder_reg, Factory::global_context_map());
504 Check(equal, "JSGlobalObject::global_context should be a global context.");
505 pop(holder_reg);
506 }
507
508 int token_offset = Context::kHeaderSize +
509 Context::SECURITY_TOKEN_INDEX * kPointerSize;
510 mov(scratch, FieldOperand(scratch, token_offset));
511 cmp(scratch, FieldOperand(holder_reg, token_offset));
512 pop(holder_reg);
513 j(not_equal, miss, not_taken);
514
515 bind(&same_contexts);
516}
517
518
519void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 Register scratch,
521 AllocationFlags flags) {
522 ExternalReference new_space_allocation_top =
523 ExternalReference::new_space_allocation_top_address();
524
525 // Just return if allocation top is already known.
526 if ((flags & RESULT_CONTAINS_TOP) != 0) {
527 // No use of scratch if allocation top is provided.
528 ASSERT(scratch.is(no_reg));
529#ifdef DEBUG
530 // Assert that result actually contains top on entry.
531 cmp(result, Operand::StaticVariable(new_space_allocation_top));
532 Check(equal, "Unexpected allocation top");
533#endif
534 return;
535 }
536
537 // Move address of new object to result. Use scratch register if available.
538 if (scratch.is(no_reg)) {
539 mov(result, Operand::StaticVariable(new_space_allocation_top));
540 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 mov(Operand(scratch), Immediate(new_space_allocation_top));
542 mov(result, Operand(scratch, 0));
543 }
544}
545
546
547void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
548 Register scratch) {
Steve Blockd0582a62009-12-15 09:54:21 +0000549 if (FLAG_debug_code) {
550 test(result_end, Immediate(kObjectAlignmentMask));
551 Check(zero, "Unaligned allocation in new space");
552 }
553
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 ExternalReference new_space_allocation_top =
555 ExternalReference::new_space_allocation_top_address();
556
557 // Update new top. Use scratch if available.
558 if (scratch.is(no_reg)) {
559 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
560 } else {
561 mov(Operand(scratch, 0), result_end);
562 }
563}
564
565
566void MacroAssembler::AllocateInNewSpace(int object_size,
567 Register result,
568 Register result_end,
569 Register scratch,
570 Label* gc_required,
571 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700572 if (!FLAG_inline_new) {
573 if (FLAG_debug_code) {
574 // Trash the registers to simulate an allocation failure.
575 mov(result, Immediate(0x7091));
576 if (result_end.is_valid()) {
577 mov(result_end, Immediate(0x7191));
578 }
579 if (scratch.is_valid()) {
580 mov(scratch, Immediate(0x7291));
581 }
582 }
583 jmp(gc_required);
584 return;
585 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 ASSERT(!result.is(result_end));
587
588 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800589 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000590
Ben Murdochbb769b22010-08-11 14:56:33 +0100591 Register top_reg = result_end.is_valid() ? result_end : result;
592
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 // Calculate new top and bail out if new space is exhausted.
594 ExternalReference new_space_allocation_limit =
595 ExternalReference::new_space_allocation_limit_address();
Ben Murdochbb769b22010-08-11 14:56:33 +0100596
Steve Block1e0659c2011-05-24 12:43:12 +0100597 if (!top_reg.is(result)) {
598 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +0100599 }
Steve Block1e0659c2011-05-24 12:43:12 +0100600 add(Operand(top_reg), Immediate(object_size));
601 j(carry, gc_required, not_taken);
Ben Murdochbb769b22010-08-11 14:56:33 +0100602 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +0000603 j(above, gc_required, not_taken);
604
Leon Clarkee46be812010-01-19 14:06:41 +0000605 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +0100606 UpdateAllocationTopHelper(top_reg, scratch);
607
608 // Tag result if requested.
609 if (top_reg.is(result)) {
610 if ((flags & TAG_OBJECT) != 0) {
611 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
612 } else {
613 sub(Operand(result), Immediate(object_size));
614 }
615 } else if ((flags & TAG_OBJECT) != 0) {
616 add(Operand(result), Immediate(kHeapObjectTag));
617 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000618}
619
620
621void MacroAssembler::AllocateInNewSpace(int header_size,
622 ScaleFactor element_size,
623 Register element_count,
624 Register result,
625 Register result_end,
626 Register scratch,
627 Label* gc_required,
628 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700629 if (!FLAG_inline_new) {
630 if (FLAG_debug_code) {
631 // Trash the registers to simulate an allocation failure.
632 mov(result, Immediate(0x7091));
633 mov(result_end, Immediate(0x7191));
634 if (scratch.is_valid()) {
635 mov(scratch, Immediate(0x7291));
636 }
637 // Register element_count is not modified by the function.
638 }
639 jmp(gc_required);
640 return;
641 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000642 ASSERT(!result.is(result_end));
643
644 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800645 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000646
647 // Calculate new top and bail out if new space is exhausted.
648 ExternalReference new_space_allocation_limit =
649 ExternalReference::new_space_allocation_limit_address();
Steve Block1e0659c2011-05-24 12:43:12 +0100650
651 // We assume that element_count*element_size + header_size does not
652 // overflow.
653 lea(result_end, Operand(element_count, element_size, header_size));
654 add(result_end, Operand(result));
655 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
657 j(above, gc_required);
658
Steve Blocka7e24c12009-10-30 11:49:00 +0000659 // Tag result if requested.
660 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000661 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000662 }
Leon Clarkee46be812010-01-19 14:06:41 +0000663
664 // Update allocation top.
665 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000666}
667
668
669void MacroAssembler::AllocateInNewSpace(Register object_size,
670 Register result,
671 Register result_end,
672 Register scratch,
673 Label* gc_required,
674 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700675 if (!FLAG_inline_new) {
676 if (FLAG_debug_code) {
677 // Trash the registers to simulate an allocation failure.
678 mov(result, Immediate(0x7091));
679 mov(result_end, Immediate(0x7191));
680 if (scratch.is_valid()) {
681 mov(scratch, Immediate(0x7291));
682 }
683 // object_size is left unchanged by this function.
684 }
685 jmp(gc_required);
686 return;
687 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 ASSERT(!result.is(result_end));
689
690 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800691 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000692
693 // Calculate new top and bail out if new space is exhausted.
694 ExternalReference new_space_allocation_limit =
695 ExternalReference::new_space_allocation_limit_address();
696 if (!object_size.is(result_end)) {
697 mov(result_end, object_size);
698 }
699 add(result_end, Operand(result));
Steve Block1e0659c2011-05-24 12:43:12 +0100700 j(carry, gc_required, not_taken);
Steve Blocka7e24c12009-10-30 11:49:00 +0000701 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
702 j(above, gc_required, not_taken);
703
Steve Blocka7e24c12009-10-30 11:49:00 +0000704 // Tag result if requested.
705 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000706 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 }
Leon Clarkee46be812010-01-19 14:06:41 +0000708
709 // Update allocation top.
710 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000711}
712
713
714void MacroAssembler::UndoAllocationInNewSpace(Register object) {
715 ExternalReference new_space_allocation_top =
716 ExternalReference::new_space_allocation_top_address();
717
718 // Make sure the object has no tag before resetting top.
719 and_(Operand(object), Immediate(~kHeapObjectTagMask));
720#ifdef DEBUG
721 cmp(object, Operand::StaticVariable(new_space_allocation_top));
722 Check(below, "Undo allocation of non allocated memory");
723#endif
724 mov(Operand::StaticVariable(new_space_allocation_top), object);
725}
726
727
Steve Block3ce2e202009-11-05 08:53:23 +0000728void MacroAssembler::AllocateHeapNumber(Register result,
729 Register scratch1,
730 Register scratch2,
731 Label* gc_required) {
732 // Allocate heap number in new space.
733 AllocateInNewSpace(HeapNumber::kSize,
734 result,
735 scratch1,
736 scratch2,
737 gc_required,
738 TAG_OBJECT);
739
740 // Set the map.
741 mov(FieldOperand(result, HeapObject::kMapOffset),
742 Immediate(Factory::heap_number_map()));
743}
744
745
Steve Blockd0582a62009-12-15 09:54:21 +0000746void MacroAssembler::AllocateTwoByteString(Register result,
747 Register length,
748 Register scratch1,
749 Register scratch2,
750 Register scratch3,
751 Label* gc_required) {
752 // Calculate the number of bytes needed for the characters in the string while
753 // observing object alignment.
754 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000755 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000756 // scratch1 = length * 2 + kObjectAlignmentMask.
757 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000758 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
759
760 // Allocate two byte string in new space.
761 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
762 times_1,
763 scratch1,
764 result,
765 scratch2,
766 scratch3,
767 gc_required,
768 TAG_OBJECT);
769
770 // Set the map, length and hash field.
771 mov(FieldOperand(result, HeapObject::kMapOffset),
772 Immediate(Factory::string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100773 mov(scratch1, length);
774 SmiTag(scratch1);
775 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000776 mov(FieldOperand(result, String::kHashFieldOffset),
777 Immediate(String::kEmptyHashField));
778}
779
780
781void MacroAssembler::AllocateAsciiString(Register result,
782 Register length,
783 Register scratch1,
784 Register scratch2,
785 Register scratch3,
786 Label* gc_required) {
787 // Calculate the number of bytes needed for the characters in the string while
788 // observing object alignment.
789 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
790 mov(scratch1, length);
791 ASSERT(kCharSize == 1);
792 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
793 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
794
795 // Allocate ascii string in new space.
796 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
797 times_1,
798 scratch1,
799 result,
800 scratch2,
801 scratch3,
802 gc_required,
803 TAG_OBJECT);
804
805 // Set the map, length and hash field.
806 mov(FieldOperand(result, HeapObject::kMapOffset),
807 Immediate(Factory::ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100808 mov(scratch1, length);
809 SmiTag(scratch1);
810 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000811 mov(FieldOperand(result, String::kHashFieldOffset),
812 Immediate(String::kEmptyHashField));
813}
814
815
Iain Merrick9ac36c92010-09-13 15:29:50 +0100816void MacroAssembler::AllocateAsciiString(Register result,
817 int length,
818 Register scratch1,
819 Register scratch2,
820 Label* gc_required) {
821 ASSERT(length > 0);
822
823 // Allocate ascii string in new space.
824 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
825 result,
826 scratch1,
827 scratch2,
828 gc_required,
829 TAG_OBJECT);
830
831 // Set the map, length and hash field.
832 mov(FieldOperand(result, HeapObject::kMapOffset),
833 Immediate(Factory::ascii_string_map()));
834 mov(FieldOperand(result, String::kLengthOffset),
835 Immediate(Smi::FromInt(length)));
836 mov(FieldOperand(result, String::kHashFieldOffset),
837 Immediate(String::kEmptyHashField));
838}
839
840
Steve Blockd0582a62009-12-15 09:54:21 +0000841void MacroAssembler::AllocateConsString(Register result,
842 Register scratch1,
843 Register scratch2,
844 Label* gc_required) {
845 // Allocate heap number in new space.
846 AllocateInNewSpace(ConsString::kSize,
847 result,
848 scratch1,
849 scratch2,
850 gc_required,
851 TAG_OBJECT);
852
853 // Set the map. The other fields are left uninitialized.
854 mov(FieldOperand(result, HeapObject::kMapOffset),
855 Immediate(Factory::cons_string_map()));
856}
857
858
859void MacroAssembler::AllocateAsciiConsString(Register result,
860 Register scratch1,
861 Register scratch2,
862 Label* gc_required) {
863 // Allocate heap number in new space.
864 AllocateInNewSpace(ConsString::kSize,
865 result,
866 scratch1,
867 scratch2,
868 gc_required,
869 TAG_OBJECT);
870
871 // Set the map. The other fields are left uninitialized.
872 mov(FieldOperand(result, HeapObject::kMapOffset),
873 Immediate(Factory::cons_ascii_string_map()));
874}
875
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800876
Ben Murdochb8e0da22011-05-16 14:20:40 +0100877// Copy memory, byte-by-byte, from source to destination. Not optimized for
878// long or aligned copies. The contents of scratch and length are destroyed.
879// Source and destination are incremented by length.
880// Many variants of movsb, loop unrolling, word moves, and indexed operands
881// have been tried here already, and this is fastest.
882// A simpler loop is faster on small copies, but 30% slower on large ones.
883// The cld() instruction must have been emitted, to set the direction flag(),
884// before calling this function.
885void MacroAssembler::CopyBytes(Register source,
886 Register destination,
887 Register length,
888 Register scratch) {
889 Label loop, done, short_string, short_loop;
890 // Experimentation shows that the short string loop is faster if length < 10.
891 cmp(Operand(length), Immediate(10));
892 j(less_equal, &short_string);
893
894 ASSERT(source.is(esi));
895 ASSERT(destination.is(edi));
896 ASSERT(length.is(ecx));
897
898 // Because source is 4-byte aligned in our uses of this function,
899 // we keep source aligned for the rep_movs call by copying the odd bytes
900 // at the end of the ranges.
901 mov(scratch, Operand(source, length, times_1, -4));
902 mov(Operand(destination, length, times_1, -4), scratch);
903 mov(scratch, ecx);
904 shr(ecx, 2);
905 rep_movs();
906 and_(Operand(scratch), Immediate(0x3));
907 add(destination, Operand(scratch));
908 jmp(&done);
909
910 bind(&short_string);
911 test(length, Operand(length));
912 j(zero, &done);
913
914 bind(&short_loop);
915 mov_b(scratch, Operand(source, 0));
916 mov_b(Operand(destination, 0), scratch);
917 inc(source);
918 inc(destination);
919 dec(length);
920 j(not_zero, &short_loop);
921
922 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800923}
924
Steve Blockd0582a62009-12-15 09:54:21 +0000925
Steve Blocka7e24c12009-10-30 11:49:00 +0000926void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
927 Register result,
928 Register op,
929 JumpTarget* then_target) {
930 JumpTarget ok;
931 test(result, Operand(result));
932 ok.Branch(not_zero, taken);
933 test(op, Operand(op));
934 then_target->Branch(sign, not_taken);
935 ok.Bind();
936}
937
938
939void MacroAssembler::NegativeZeroTest(Register result,
940 Register op,
941 Label* then_label) {
942 Label ok;
943 test(result, Operand(result));
944 j(not_zero, &ok, taken);
945 test(op, Operand(op));
946 j(sign, then_label, not_taken);
947 bind(&ok);
948}
949
950
951void MacroAssembler::NegativeZeroTest(Register result,
952 Register op1,
953 Register op2,
954 Register scratch,
955 Label* then_label) {
956 Label ok;
957 test(result, Operand(result));
958 j(not_zero, &ok, taken);
959 mov(scratch, Operand(op1));
960 or_(scratch, Operand(op2));
961 j(sign, then_label, not_taken);
962 bind(&ok);
963}
964
965
966void MacroAssembler::TryGetFunctionPrototype(Register function,
967 Register result,
968 Register scratch,
969 Label* miss) {
970 // Check that the receiver isn't a smi.
971 test(function, Immediate(kSmiTagMask));
972 j(zero, miss, not_taken);
973
974 // Check that the function really is a function.
975 CmpObjectType(function, JS_FUNCTION_TYPE, result);
976 j(not_equal, miss, not_taken);
977
978 // Make sure that the function has an instance prototype.
979 Label non_instance;
980 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
981 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
982 j(not_zero, &non_instance, not_taken);
983
984 // Get the prototype or initial map from the function.
985 mov(result,
986 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
987
988 // If the prototype or initial map is the hole, don't return it and
989 // simply miss the cache instead. This will allow us to allocate a
990 // prototype object on-demand in the runtime system.
991 cmp(Operand(result), Immediate(Factory::the_hole_value()));
992 j(equal, miss, not_taken);
993
994 // If the function does not have an initial map, we're done.
995 Label done;
996 CmpObjectType(result, MAP_TYPE, scratch);
997 j(not_equal, &done);
998
999 // Get the prototype from the initial map.
1000 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1001 jmp(&done);
1002
1003 // Non-instance prototype: Fetch prototype from constructor field
1004 // in initial map.
1005 bind(&non_instance);
1006 mov(result, FieldOperand(result, Map::kConstructorOffset));
1007
1008 // All done.
1009 bind(&done);
1010}
1011
1012
1013void MacroAssembler::CallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001014 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001015 call(stub->GetCode(), RelocInfo::CODE_TARGET);
1016}
1017
1018
John Reck59135872010-11-02 12:39:01 -07001019MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001020 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001021 Object* result;
1022 { MaybeObject* maybe_result = stub->TryGetCode();
1023 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001024 }
John Reck59135872010-11-02 12:39:01 -07001025 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001026 return result;
1027}
1028
1029
Steve Blockd0582a62009-12-15 09:54:21 +00001030void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001031 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +00001032 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1033}
1034
1035
John Reck59135872010-11-02 12:39:01 -07001036MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001037 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001038 Object* result;
1039 { MaybeObject* maybe_result = stub->TryGetCode();
1040 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001041 }
John Reck59135872010-11-02 12:39:01 -07001042 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001043 return result;
1044}
1045
1046
Steve Blocka7e24c12009-10-30 11:49:00 +00001047void MacroAssembler::StubReturn(int argc) {
1048 ASSERT(argc >= 1 && generating_stub());
1049 ret((argc - 1) * kPointerSize);
1050}
1051
1052
1053void MacroAssembler::IllegalOperation(int num_arguments) {
1054 if (num_arguments > 0) {
1055 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1056 }
1057 mov(eax, Immediate(Factory::undefined_value()));
1058}
1059
1060
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001061void MacroAssembler::IndexFromHash(Register hash, Register index) {
1062 // The assert checks that the constants for the maximum number of digits
1063 // for an array index cached in the hash field and the number of bits
1064 // reserved for it does not conflict.
1065 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1066 (1 << String::kArrayIndexValueBits));
1067 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1068 // the low kHashShift bits.
1069 and_(hash, String::kArrayIndexValueMask);
1070 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1071 if (String::kHashShift > kSmiTagSize) {
1072 shr(hash, String::kHashShift - kSmiTagSize);
1073 }
1074 if (!index.is(hash)) {
1075 mov(index, hash);
1076 }
1077}
1078
1079
Steve Blocka7e24c12009-10-30 11:49:00 +00001080void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1081 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1082}
1083
1084
Ben Murdochb0fe1622011-05-05 13:52:32 +01001085void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1086 Runtime::Function* function = Runtime::FunctionForId(id);
1087 Set(eax, Immediate(function->nargs));
1088 mov(ebx, Immediate(ExternalReference(function)));
1089 CEntryStub ces(1);
1090 ces.SaveDoubles();
1091 CallStub(&ces);
1092}
1093
1094
John Reck59135872010-11-02 12:39:01 -07001095MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1096 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001097 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1098}
1099
1100
Steve Blocka7e24c12009-10-30 11:49:00 +00001101void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1102 // If the expected number of arguments of the runtime function is
1103 // constant, we check that the actual number of arguments match the
1104 // expectation.
1105 if (f->nargs >= 0 && f->nargs != num_arguments) {
1106 IllegalOperation(num_arguments);
1107 return;
1108 }
1109
Leon Clarke4515c472010-02-03 11:58:03 +00001110 // TODO(1236192): Most runtime routines don't need the number of
1111 // arguments passed in because it is constant. At some point we
1112 // should remove this need and make the runtime routine entry code
1113 // smarter.
1114 Set(eax, Immediate(num_arguments));
1115 mov(ebx, Immediate(ExternalReference(f)));
1116 CEntryStub ces(1);
1117 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001118}
1119
1120
John Reck59135872010-11-02 12:39:01 -07001121MaybeObject* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1122 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001123 if (f->nargs >= 0 && f->nargs != num_arguments) {
1124 IllegalOperation(num_arguments);
1125 // Since we did not call the stub, there was no allocation failure.
1126 // Return some non-failure object.
1127 return Heap::undefined_value();
1128 }
1129
Leon Clarke4515c472010-02-03 11:58:03 +00001130 // TODO(1236192): Most runtime routines don't need the number of
1131 // arguments passed in because it is constant. At some point we
1132 // should remove this need and make the runtime routine entry code
1133 // smarter.
1134 Set(eax, Immediate(num_arguments));
1135 mov(ebx, Immediate(ExternalReference(f)));
1136 CEntryStub ces(1);
1137 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001138}
1139
1140
Ben Murdochbb769b22010-08-11 14:56:33 +01001141void MacroAssembler::CallExternalReference(ExternalReference ref,
1142 int num_arguments) {
1143 mov(eax, Immediate(num_arguments));
1144 mov(ebx, Immediate(ref));
1145
1146 CEntryStub stub(1);
1147 CallStub(&stub);
1148}
1149
1150
Steve Block6ded16b2010-05-10 14:33:55 +01001151void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1152 int num_arguments,
1153 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001154 // TODO(1236192): Most runtime routines don't need the number of
1155 // arguments passed in because it is constant. At some point we
1156 // should remove this need and make the runtime routine entry code
1157 // smarter.
1158 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001159 JumpToExternalReference(ext);
1160}
1161
1162
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001163MaybeObject* MacroAssembler::TryTailCallExternalReference(
1164 const ExternalReference& ext, int num_arguments, int result_size) {
1165 // TODO(1236192): Most runtime routines don't need the number of
1166 // arguments passed in because it is constant. At some point we
1167 // should remove this need and make the runtime routine entry code
1168 // smarter.
1169 Set(eax, Immediate(num_arguments));
1170 return TryJumpToExternalReference(ext);
1171}
1172
1173
Steve Block6ded16b2010-05-10 14:33:55 +01001174void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1175 int num_arguments,
1176 int result_size) {
1177 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001178}
1179
1180
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001181MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1182 int num_arguments,
1183 int result_size) {
1184 return TryTailCallExternalReference(
1185 ExternalReference(fid), num_arguments, result_size);
1186}
1187
1188
Ben Murdochb0fe1622011-05-05 13:52:32 +01001189// If true, a Handle<T> returned by value from a function with cdecl calling
1190// convention will be returned directly as a value of location_ field in a
1191// register eax.
1192// If false, it is returned as a pointer to a preallocated by caller memory
1193// region. Pointer to this region should be passed to a function as an
1194// implicit first argument.
1195#if defined(USING_BSD_ABI) || defined(__MINGW32__)
1196static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001197#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001198static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001199#endif
1200
1201
1202Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001203 return Operand(
1204 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001205}
1206
1207
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001208void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001209 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001210 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001211 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001212 // space for and pass an out parameter.
1213 } else {
1214 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001215 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001216
John Reck59135872010-11-02 12:39:01 -07001217 // The argument slots are filled as follows:
1218 //
1219 // n + 1: output cell
1220 // n: arg n
1221 // ...
1222 // 1: arg1
1223 // 0: pointer to the output cell
1224 //
1225 // Note that this is one more "argument" than the function expects
1226 // so the out cell will have to be popped explicitly after returning
1227 // from the function. The out cell contains Handle.
John Reck59135872010-11-02 12:39:01 -07001228
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001229 // pointer to out cell.
1230 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1231 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
1232 if (FLAG_debug_code) {
1233 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1234 }
1235 }
1236}
1237
1238
1239MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1240 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001241 ExternalReference next_address =
1242 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001243 ExternalReference limit_address =
1244 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001245 ExternalReference level_address =
1246 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001247
John Reck59135872010-11-02 12:39:01 -07001248 // Allocate HandleScope in callee-save registers.
1249 mov(ebx, Operand::StaticVariable(next_address));
1250 mov(edi, Operand::StaticVariable(limit_address));
1251 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001252
John Reck59135872010-11-02 12:39:01 -07001253 // Call the api function!
1254 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1255
Ben Murdochb0fe1622011-05-05 13:52:32 +01001256 if (!kReturnHandlesDirectly) {
John Reck59135872010-11-02 12:39:01 -07001257 // The returned value is a pointer to the handle holding the result.
1258 // Dereference this to get to the location.
1259 mov(eax, Operand(eax, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001260 }
Steve Blockd0582a62009-12-15 09:54:21 +00001261
John Reck59135872010-11-02 12:39:01 -07001262 Label empty_handle;
1263 Label prologue;
1264 Label promote_scheduled_exception;
1265 Label delete_allocated_handles;
1266 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001267
John Reck59135872010-11-02 12:39:01 -07001268 // Check if the result handle holds 0.
1269 test(eax, Operand(eax));
1270 j(zero, &empty_handle, not_taken);
1271 // It was non-zero. Dereference to get the result value.
1272 mov(eax, Operand(eax, 0));
1273 bind(&prologue);
1274 // No more valid handles (the result handle was the last one). Restore
1275 // previous handle scope.
1276 mov(Operand::StaticVariable(next_address), ebx);
1277 sub(Operand::StaticVariable(level_address), Immediate(1));
1278 Assert(above_equal, "Invalid HandleScope level");
1279 cmp(edi, Operand::StaticVariable(limit_address));
1280 j(not_equal, &delete_allocated_handles, not_taken);
1281 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001282
John Reck59135872010-11-02 12:39:01 -07001283 // Check if the function scheduled an exception.
1284 ExternalReference scheduled_exception_address =
1285 ExternalReference::scheduled_exception_address();
1286 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block1e0659c2011-05-24 12:43:12 +01001287 Immediate(Factory::the_hole_value()));
John Reck59135872010-11-02 12:39:01 -07001288 j(not_equal, &promote_scheduled_exception, not_taken);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001289 LeaveApiExitFrame();
1290 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001291 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001292 MaybeObject* result =
1293 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1294 if (result->IsFailure()) {
1295 return result;
1296 }
John Reck59135872010-11-02 12:39:01 -07001297 bind(&empty_handle);
1298 // It was zero; the result is undefined.
1299 mov(eax, Factory::undefined_value());
1300 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001301
John Reck59135872010-11-02 12:39:01 -07001302 // HandleScope limit has changed. Delete allocated extensions.
1303 bind(&delete_allocated_handles);
1304 mov(Operand::StaticVariable(limit_address), edi);
1305 mov(edi, eax);
1306 mov(eax, Immediate(ExternalReference::delete_handle_scope_extensions()));
1307 call(Operand(eax));
1308 mov(eax, edi);
1309 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001310
1311 return result;
Steve Blockd0582a62009-12-15 09:54:21 +00001312}
1313
1314
Steve Block6ded16b2010-05-10 14:33:55 +01001315void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 // Set the entry point and jump to the C entry runtime stub.
1317 mov(ebx, Immediate(ext));
1318 CEntryStub ces(1);
1319 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1320}
1321
1322
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001323MaybeObject* MacroAssembler::TryJumpToExternalReference(
1324 const ExternalReference& ext) {
1325 // Set the entry point and jump to the C entry runtime stub.
1326 mov(ebx, Immediate(ext));
1327 CEntryStub ces(1);
1328 return TryTailCallStub(&ces);
1329}
1330
1331
Steve Blocka7e24c12009-10-30 11:49:00 +00001332void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1333 const ParameterCount& actual,
1334 Handle<Code> code_constant,
1335 const Operand& code_operand,
1336 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001337 InvokeFlag flag,
1338 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 bool definitely_matches = false;
1340 Label invoke;
1341 if (expected.is_immediate()) {
1342 ASSERT(actual.is_immediate());
1343 if (expected.immediate() == actual.immediate()) {
1344 definitely_matches = true;
1345 } else {
1346 mov(eax, actual.immediate());
1347 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1348 if (expected.immediate() == sentinel) {
1349 // Don't worry about adapting arguments for builtins that
1350 // don't want that done. Skip adaption code by making it look
1351 // like we have a match between expected and actual number of
1352 // arguments.
1353 definitely_matches = true;
1354 } else {
1355 mov(ebx, expected.immediate());
1356 }
1357 }
1358 } else {
1359 if (actual.is_immediate()) {
1360 // Expected is in register, actual is immediate. This is the
1361 // case when we invoke function values without going through the
1362 // IC mechanism.
1363 cmp(expected.reg(), actual.immediate());
1364 j(equal, &invoke);
1365 ASSERT(expected.reg().is(ebx));
1366 mov(eax, actual.immediate());
1367 } else if (!expected.reg().is(actual.reg())) {
1368 // Both expected and actual are in (different) registers. This
1369 // is the case when we invoke functions using call and apply.
1370 cmp(expected.reg(), Operand(actual.reg()));
1371 j(equal, &invoke);
1372 ASSERT(actual.reg().is(eax));
1373 ASSERT(expected.reg().is(ebx));
1374 }
1375 }
1376
1377 if (!definitely_matches) {
1378 Handle<Code> adaptor =
1379 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1380 if (!code_constant.is_null()) {
1381 mov(edx, Immediate(code_constant));
1382 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1383 } else if (!code_operand.is_reg(edx)) {
1384 mov(edx, code_operand);
1385 }
1386
1387 if (flag == CALL_FUNCTION) {
1388 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001389 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001390 jmp(done);
1391 } else {
1392 jmp(adaptor, RelocInfo::CODE_TARGET);
1393 }
1394 bind(&invoke);
1395 }
1396}
1397
1398
1399void MacroAssembler::InvokeCode(const Operand& code,
1400 const ParameterCount& expected,
1401 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001402 InvokeFlag flag,
1403 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001404 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001405 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1406 &done, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 if (flag == CALL_FUNCTION) {
1408 call(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001409 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 } else {
1411 ASSERT(flag == JUMP_FUNCTION);
1412 jmp(code);
1413 }
1414 bind(&done);
1415}
1416
1417
1418void MacroAssembler::InvokeCode(Handle<Code> code,
1419 const ParameterCount& expected,
1420 const ParameterCount& actual,
1421 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001422 InvokeFlag flag,
1423 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 Label done;
1425 Operand dummy(eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001426 InvokePrologue(expected, actual, code, dummy, &done,
1427 flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001428 if (flag == CALL_FUNCTION) {
1429 call(code, rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001430 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001431 } else {
1432 ASSERT(flag == JUMP_FUNCTION);
1433 jmp(code, rmode);
1434 }
1435 bind(&done);
1436}
1437
1438
1439void MacroAssembler::InvokeFunction(Register fun,
1440 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001441 InvokeFlag flag,
1442 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001443 ASSERT(fun.is(edi));
1444 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1445 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1446 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001447 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001448
1449 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01001450 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001451 expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001452}
1453
1454
Andrei Popescu402d9372010-02-26 13:31:12 +00001455void MacroAssembler::InvokeFunction(JSFunction* function,
1456 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001457 InvokeFlag flag,
1458 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001459 ASSERT(function->is_compiled());
1460 // Get the function and setup the context.
1461 mov(edi, Immediate(Handle<JSFunction>(function)));
1462 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001463
Andrei Popescu402d9372010-02-26 13:31:12 +00001464 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001465 if (V8::UseCrankshaft()) {
1466 // TODO(kasperl): For now, we always call indirectly through the
1467 // code field in the function to allow recompilation to take effect
1468 // without changing any of the call sites.
1469 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1470 expected, actual, flag, post_call_generator);
1471 } else {
1472 Handle<Code> code(function->code());
1473 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
1474 flag, post_call_generator);
1475 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001476}
1477
1478
Ben Murdochb0fe1622011-05-05 13:52:32 +01001479void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1480 InvokeFlag flag,
1481 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001482 // Calls are not allowed in some stubs.
1483 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1484
1485 // Rely on the assertion to check that the number of provided
1486 // arguments match the expected number of arguments. Fake a
1487 // parameter count to avoid emitting code to do the check.
1488 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01001489 GetBuiltinFunction(edi, id);
1490 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001491 expected, expected, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001492}
1493
Steve Block791712a2010-08-27 10:21:07 +01001494void MacroAssembler::GetBuiltinFunction(Register target,
1495 Builtins::JavaScript id) {
1496 // Load the JavaScript builtin function from the builtins object.
1497 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1498 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1499 mov(target, FieldOperand(target,
1500 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1501}
Steve Blocka7e24c12009-10-30 11:49:00 +00001502
1503void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001504 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00001505 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01001506 GetBuiltinFunction(edi, id);
1507 // Load the code entry point from the function into the target register.
1508 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001509}
1510
1511
Steve Blockd0582a62009-12-15 09:54:21 +00001512void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1513 if (context_chain_length > 0) {
1514 // Move up the chain of contexts to the context containing the slot.
1515 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1516 // Load the function context (which is the incoming, outer context).
1517 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1518 for (int i = 1; i < context_chain_length; i++) {
1519 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1520 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1521 }
Steve Block1e0659c2011-05-24 12:43:12 +01001522 } else {
1523 // Slot is in the current function context. Move it into the
1524 // destination register in case we store into it (the write barrier
1525 // cannot be allowed to destroy the context in esi).
1526 mov(dst, esi);
1527 }
1528
1529 // We should not have found a 'with' context by walking the context chain
1530 // (i.e., the static scope chain and runtime context chain do not agree).
1531 // A variable occurring in such a scope should have slot type LOOKUP and
1532 // not CONTEXT.
1533 if (FLAG_debug_code) {
1534 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1535 Check(equal, "Yo dawg, I heard you liked function contexts "
1536 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00001537 }
1538}
1539
1540
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001541void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1542 // Load the global or builtins object from the current context.
1543 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1544 // Load the global context from the global or builtins object.
1545 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1546 // Load the function from the global context.
1547 mov(function, Operand(function, Context::SlotOffset(index)));
1548}
1549
1550
1551void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1552 Register map) {
1553 // Load the initial map. The global functions all have initial maps.
1554 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1555 if (FLAG_debug_code) {
1556 Label ok, fail;
1557 CheckMap(map, Factory::meta_map(), &fail, false);
1558 jmp(&ok);
1559 bind(&fail);
1560 Abort("Global functions must have initial map");
1561 bind(&ok);
1562 }
1563}
1564
Steve Blockd0582a62009-12-15 09:54:21 +00001565
Ben Murdochb0fe1622011-05-05 13:52:32 +01001566int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1567 // The registers are pushed starting with the lowest encoding,
1568 // which means that lowest encodings are furthest away from
1569 // the stack pointer.
1570 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1571 return kNumSafepointRegisters - reg_code - 1;
1572}
1573
1574
Steve Blocka7e24c12009-10-30 11:49:00 +00001575void MacroAssembler::Ret() {
1576 ret(0);
1577}
1578
1579
Steve Block1e0659c2011-05-24 12:43:12 +01001580void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1581 if (is_uint16(bytes_dropped)) {
1582 ret(bytes_dropped);
1583 } else {
1584 pop(scratch);
1585 add(Operand(esp), Immediate(bytes_dropped));
1586 push(scratch);
1587 ret(0);
1588 }
1589}
1590
1591
1592
1593
Leon Clarkee46be812010-01-19 14:06:41 +00001594void MacroAssembler::Drop(int stack_elements) {
1595 if (stack_elements > 0) {
1596 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1597 }
1598}
1599
1600
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001601void MacroAssembler::Move(Register dst, Register src) {
1602 if (!dst.is(src)) {
1603 mov(dst, src);
1604 }
1605}
1606
1607
Leon Clarkee46be812010-01-19 14:06:41 +00001608void MacroAssembler::Move(Register dst, Handle<Object> value) {
1609 mov(dst, value);
1610}
1611
1612
Steve Blocka7e24c12009-10-30 11:49:00 +00001613void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1614 if (FLAG_native_code_counters && counter->Enabled()) {
1615 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1616 }
1617}
1618
1619
1620void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1621 ASSERT(value > 0);
1622 if (FLAG_native_code_counters && counter->Enabled()) {
1623 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1624 if (value == 1) {
1625 inc(operand);
1626 } else {
1627 add(operand, Immediate(value));
1628 }
1629 }
1630}
1631
1632
1633void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1634 ASSERT(value > 0);
1635 if (FLAG_native_code_counters && counter->Enabled()) {
1636 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1637 if (value == 1) {
1638 dec(operand);
1639 } else {
1640 sub(operand, Immediate(value));
1641 }
1642 }
1643}
1644
1645
Leon Clarked91b9f72010-01-27 17:25:45 +00001646void MacroAssembler::IncrementCounter(Condition cc,
1647 StatsCounter* counter,
1648 int value) {
1649 ASSERT(value > 0);
1650 if (FLAG_native_code_counters && counter->Enabled()) {
1651 Label skip;
1652 j(NegateCondition(cc), &skip);
1653 pushfd();
1654 IncrementCounter(counter, value);
1655 popfd();
1656 bind(&skip);
1657 }
1658}
1659
1660
1661void MacroAssembler::DecrementCounter(Condition cc,
1662 StatsCounter* counter,
1663 int value) {
1664 ASSERT(value > 0);
1665 if (FLAG_native_code_counters && counter->Enabled()) {
1666 Label skip;
1667 j(NegateCondition(cc), &skip);
1668 pushfd();
1669 DecrementCounter(counter, value);
1670 popfd();
1671 bind(&skip);
1672 }
1673}
1674
1675
Steve Blocka7e24c12009-10-30 11:49:00 +00001676void MacroAssembler::Assert(Condition cc, const char* msg) {
1677 if (FLAG_debug_code) Check(cc, msg);
1678}
1679
1680
Iain Merrick75681382010-08-19 15:07:18 +01001681void MacroAssembler::AssertFastElements(Register elements) {
1682 if (FLAG_debug_code) {
1683 Label ok;
1684 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1685 Immediate(Factory::fixed_array_map()));
1686 j(equal, &ok);
1687 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1688 Immediate(Factory::fixed_cow_array_map()));
1689 j(equal, &ok);
1690 Abort("JSObject with fast elements map has slow elements");
1691 bind(&ok);
1692 }
1693}
1694
1695
Steve Blocka7e24c12009-10-30 11:49:00 +00001696void MacroAssembler::Check(Condition cc, const char* msg) {
1697 Label L;
1698 j(cc, &L, taken);
1699 Abort(msg);
1700 // will not return here
1701 bind(&L);
1702}
1703
1704
Steve Block6ded16b2010-05-10 14:33:55 +01001705void MacroAssembler::CheckStackAlignment() {
1706 int frame_alignment = OS::ActivationFrameAlignment();
1707 int frame_alignment_mask = frame_alignment - 1;
1708 if (frame_alignment > kPointerSize) {
1709 ASSERT(IsPowerOf2(frame_alignment));
1710 Label alignment_as_expected;
1711 test(esp, Immediate(frame_alignment_mask));
1712 j(zero, &alignment_as_expected);
1713 // Abort if stack is not aligned.
1714 int3();
1715 bind(&alignment_as_expected);
1716 }
1717}
1718
1719
Steve Blocka7e24c12009-10-30 11:49:00 +00001720void MacroAssembler::Abort(const char* msg) {
1721 // We want to pass the msg string like a smi to avoid GC
1722 // problems, however msg is not guaranteed to be aligned
1723 // properly. Instead, we pass an aligned pointer that is
1724 // a proper v8 smi, but also pass the alignment difference
1725 // from the real pointer as a smi.
1726 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1727 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1728 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1729#ifdef DEBUG
1730 if (msg != NULL) {
1731 RecordComment("Abort message: ");
1732 RecordComment(msg);
1733 }
1734#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001735 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001736 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001737
Steve Blocka7e24c12009-10-30 11:49:00 +00001738 push(eax);
1739 push(Immediate(p0));
1740 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1741 CallRuntime(Runtime::kAbort, 2);
1742 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00001743 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00001744}
1745
1746
Iain Merrick75681382010-08-19 15:07:18 +01001747void MacroAssembler::JumpIfNotNumber(Register reg,
1748 TypeInfo info,
1749 Label* on_not_number) {
1750 if (FLAG_debug_code) AbortIfSmi(reg);
1751 if (!info.IsNumber()) {
1752 cmp(FieldOperand(reg, HeapObject::kMapOffset),
1753 Factory::heap_number_map());
1754 j(not_equal, on_not_number);
1755 }
1756}
1757
1758
1759void MacroAssembler::ConvertToInt32(Register dst,
1760 Register source,
1761 Register scratch,
1762 TypeInfo info,
1763 Label* on_not_int32) {
1764 if (FLAG_debug_code) {
1765 AbortIfSmi(source);
1766 AbortIfNotNumber(source);
1767 }
1768 if (info.IsInteger32()) {
1769 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1770 } else {
1771 Label done;
1772 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1773 ASSERT(!scratch.is(source));
1774 if (push_pop) {
1775 push(dst);
1776 scratch = dst;
1777 }
1778 if (scratch.is(no_reg)) scratch = dst;
1779 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1780 cmp(scratch, 0x80000000u);
1781 if (push_pop) {
1782 j(not_equal, &done);
1783 pop(dst);
1784 jmp(on_not_int32);
1785 } else {
1786 j(equal, on_not_int32);
1787 }
1788
1789 bind(&done);
1790 if (push_pop) {
1791 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1792 }
1793 if (!scratch.is(dst)) {
1794 mov(dst, scratch);
1795 }
1796 }
1797}
1798
1799
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001800void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1801 Register scratch,
1802 int power) {
1803 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1804 HeapNumber::kExponentBits));
1805 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1806 movd(dst, Operand(scratch));
1807 psllq(dst, HeapNumber::kMantissaBits);
1808}
1809
1810
Andrei Popescu402d9372010-02-26 13:31:12 +00001811void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1812 Register instance_type,
1813 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01001814 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001815 if (!scratch.is(instance_type)) {
1816 mov(scratch, instance_type);
1817 }
1818 and_(scratch,
1819 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1820 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1821 j(not_equal, failure);
1822}
1823
1824
Leon Clarked91b9f72010-01-27 17:25:45 +00001825void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1826 Register object2,
1827 Register scratch1,
1828 Register scratch2,
1829 Label* failure) {
1830 // Check that both objects are not smis.
1831 ASSERT_EQ(0, kSmiTag);
1832 mov(scratch1, Operand(object1));
1833 and_(scratch1, Operand(object2));
1834 test(scratch1, Immediate(kSmiTagMask));
1835 j(zero, failure);
1836
1837 // Load instance type for both strings.
1838 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1839 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1840 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1841 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1842
1843 // Check that both are flat ascii strings.
1844 const int kFlatAsciiStringMask =
1845 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1846 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1847 // Interleave bits from both instance types and compare them in one check.
1848 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1849 and_(scratch1, kFlatAsciiStringMask);
1850 and_(scratch2, kFlatAsciiStringMask);
1851 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1852 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1853 j(not_equal, failure);
1854}
1855
1856
Steve Block6ded16b2010-05-10 14:33:55 +01001857void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1858 int frameAlignment = OS::ActivationFrameAlignment();
1859 if (frameAlignment != 0) {
1860 // Make stack end at alignment and make room for num_arguments words
1861 // and the original value of esp.
1862 mov(scratch, esp);
1863 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1864 ASSERT(IsPowerOf2(frameAlignment));
1865 and_(esp, -frameAlignment);
1866 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1867 } else {
1868 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1869 }
1870}
1871
1872
1873void MacroAssembler::CallCFunction(ExternalReference function,
1874 int num_arguments) {
1875 // Trashing eax is ok as it will be the return value.
1876 mov(Operand(eax), Immediate(function));
1877 CallCFunction(eax, num_arguments);
1878}
1879
1880
1881void MacroAssembler::CallCFunction(Register function,
1882 int num_arguments) {
1883 // Check stack alignment.
1884 if (FLAG_debug_code) {
1885 CheckStackAlignment();
1886 }
1887
1888 call(Operand(function));
1889 if (OS::ActivationFrameAlignment() != 0) {
1890 mov(esp, Operand(esp, num_arguments * kPointerSize));
1891 } else {
1892 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1893 }
1894}
1895
1896
Steve Blocka7e24c12009-10-30 11:49:00 +00001897CodePatcher::CodePatcher(byte* address, int size)
1898 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
1899 // Create a new macro assembler pointing to the address of the code to patch.
1900 // The size is adjusted with kGap on order for the assembler to generate size
1901 // bytes of instructions without failing with buffer size constraints.
1902 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1903}
1904
1905
1906CodePatcher::~CodePatcher() {
1907 // Indicate that code has changed.
1908 CPU::FlushICache(address_, size_);
1909
1910 // Check that the code was patched as expected.
1911 ASSERT(masm_.pc_ == address_ + size_);
1912 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1913}
1914
1915
1916} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001917
1918#endif // V8_TARGET_ARCH_IA32