blob: ba30c49ced678c18fcc4658323e9ff779e38ecd6 [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),
Steve Block44f0eee2011-05-26 01:26:41 +010048 code_object_(isolate()->heap()->undefined_value()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000049}
50
51
Steve Block6ded16b2010-05-10 14:33:55 +010052void MacroAssembler::RecordWriteHelper(Register object,
53 Register addr,
54 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +010055 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +010056 // 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.
Steve Block44f0eee2011-05-26 01:26:41 +0100116 if (emit_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.
Steve Block44f0eee2011-05-26 01:26:41 +0100144 if (emit_debug_code()) {
Steve Block8defd9f2010-07-08 12:39:36 +0100145 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));
Steve Block44f0eee2011-05-26 01:26:41 +0100155 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +0000156 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 Block44f0eee2011-05-26 01:26:41 +0100234 if (Isolate::Current()->cpu_features()->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),
Steve Block44f0eee2011-05-26 01:26:41 +0100253 isolate()->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()));
Steve Block44f0eee2011-05-26 01:26:41 +0100288 if (emit_debug_code()) {
289 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 Check(not_equal, "code object not properly patched");
291 }
292}
293
294
295void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100296 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 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.
Steve Block44f0eee2011-05-26 01:26:41 +0100319 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
320 isolate());
321 ExternalReference context_address(Isolate::k_context_address,
322 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
324 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000325}
Steve Blocka7e24c12009-10-30 11:49:00 +0000326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327
Ben Murdochb0fe1622011-05-05 13:52:32 +0100328void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
329 // Optionally save all XMM registers.
330 if (save_doubles) {
331 CpuFeatures::Scope scope(SSE2);
332 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
333 sub(Operand(esp), Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100334 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100335 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
336 XMMRegister reg = XMMRegister::from_code(i);
337 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
338 }
339 } else {
340 sub(Operand(esp), Immediate(argc * kPointerSize));
341 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000342
343 // Get the required frame alignment for the OS.
Steve Block44f0eee2011-05-26 01:26:41 +0100344 const int kFrameAlignment = OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 if (kFrameAlignment > 0) {
346 ASSERT(IsPowerOf2(kFrameAlignment));
347 and_(esp, -kFrameAlignment);
348 }
349
350 // Patch the saved entry sp.
351 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
352}
353
354
Ben Murdochb0fe1622011-05-05 13:52:32 +0100355void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100356 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000357
358 // Setup argc and argv in callee-saved registers.
359 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
360 mov(edi, Operand(eax));
361 lea(esi, Operand(ebp, eax, times_4, offset));
362
Steve Block44f0eee2011-05-26 01:26:41 +0100363 // Reserve space for argc, argv and isolate.
364 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000365}
366
367
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800368void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100369 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100370 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000371}
372
373
Ben Murdochb0fe1622011-05-05 13:52:32 +0100374void MacroAssembler::LeaveExitFrame(bool save_doubles) {
375 // Optionally restore all XMM registers.
376 if (save_doubles) {
377 CpuFeatures::Scope scope(SSE2);
Steve Block1e0659c2011-05-24 12:43:12 +0100378 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100379 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
380 XMMRegister reg = XMMRegister::from_code(i);
381 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
382 }
383 }
384
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 // Get the return address from the stack and restore the frame pointer.
386 mov(ecx, Operand(ebp, 1 * kPointerSize));
387 mov(ebp, Operand(ebp, 0 * kPointerSize));
388
389 // Pop the arguments and the receiver from the caller stack.
390 lea(esp, Operand(esi, 1 * kPointerSize));
391
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800392 // Push the return address to get ready to return.
393 push(ecx);
394
395 LeaveExitFrameEpilogue();
396}
397
398void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 // Restore current context from top and clear it in debug mode.
Steve Block44f0eee2011-05-26 01:26:41 +0100400 ExternalReference context_address(Isolate::k_context_address, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 mov(esi, Operand::StaticVariable(context_address));
402#ifdef DEBUG
403 mov(Operand::StaticVariable(context_address), Immediate(0));
404#endif
405
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 // Clear the top frame.
Steve Block44f0eee2011-05-26 01:26:41 +0100407 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
408 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
410}
411
412
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800413void MacroAssembler::LeaveApiExitFrame() {
414 mov(esp, Operand(ebp));
415 pop(ebp);
416
417 LeaveExitFrameEpilogue();
418}
419
420
Steve Blocka7e24c12009-10-30 11:49:00 +0000421void MacroAssembler::PushTryHandler(CodeLocation try_location,
422 HandlerType type) {
423 // Adjust this code if not the case.
424 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
425 // The pc (return address) is already on TOS.
426 if (try_location == IN_JAVASCRIPT) {
427 if (type == TRY_CATCH_HANDLER) {
428 push(Immediate(StackHandler::TRY_CATCH));
429 } else {
430 push(Immediate(StackHandler::TRY_FINALLY));
431 }
432 push(ebp);
433 } else {
434 ASSERT(try_location == IN_JS_ENTRY);
435 // The frame pointer does not point to a JS frame so we save NULL
436 // for ebp. We expect the code throwing an exception to check ebp
437 // before dereferencing it to restore the context.
438 push(Immediate(StackHandler::ENTRY));
439 push(Immediate(0)); // NULL frame pointer.
440 }
441 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100442 push(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
443 isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 // Link this handler as the new current one.
Steve Block44f0eee2011-05-26 01:26:41 +0100445 mov(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
446 isolate())),
447 esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448}
449
450
Leon Clarkee46be812010-01-19 14:06:41 +0000451void MacroAssembler::PopTryHandler() {
452 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
Steve Block44f0eee2011-05-26 01:26:41 +0100453 pop(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
454 isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +0000455 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
456}
457
458
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100459void MacroAssembler::Throw(Register value) {
460 // Adjust this code if not the case.
461 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
462
463 // eax must hold the exception.
464 if (!value.is(eax)) {
465 mov(eax, value);
466 }
467
468 // Drop the sp to the top of the handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100469 ExternalReference handler_address(Isolate::k_handler_address,
470 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100471 mov(esp, Operand::StaticVariable(handler_address));
472
473 // Restore next handler and frame pointer, discard handler state.
474 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
475 pop(Operand::StaticVariable(handler_address));
476 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
477 pop(ebp);
478 pop(edx); // Remove state.
479
480 // Before returning we restore the context from the frame pointer if
481 // not NULL. The frame pointer is NULL in the exception handler of
482 // a JS entry frame.
483 Set(esi, Immediate(0)); // Tentatively set context pointer to NULL.
484 NearLabel skip;
485 cmp(ebp, 0);
486 j(equal, &skip, not_taken);
487 mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
488 bind(&skip);
489
490 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
491 ret(0);
492}
493
494
495void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
496 Register value) {
497 // Adjust this code if not the case.
498 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
499
500 // eax must hold the exception.
501 if (!value.is(eax)) {
502 mov(eax, value);
503 }
504
505 // Drop sp to the top stack handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100506 ExternalReference handler_address(Isolate::k_handler_address,
507 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100508 mov(esp, Operand::StaticVariable(handler_address));
509
510 // Unwind the handlers until the ENTRY handler is found.
511 NearLabel loop, done;
512 bind(&loop);
513 // Load the type of the current stack handler.
514 const int kStateOffset = StackHandlerConstants::kStateOffset;
515 cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
516 j(equal, &done);
517 // Fetch the next handler in the list.
518 const int kNextOffset = StackHandlerConstants::kNextOffset;
519 mov(esp, Operand(esp, kNextOffset));
520 jmp(&loop);
521 bind(&done);
522
523 // Set the top handler address to next handler past the current ENTRY handler.
524 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
525 pop(Operand::StaticVariable(handler_address));
526
527 if (type == OUT_OF_MEMORY) {
528 // Set external caught exception to false.
Steve Block44f0eee2011-05-26 01:26:41 +0100529 ExternalReference external_caught(
530 Isolate::k_external_caught_exception_address,
531 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100532 mov(eax, false);
533 mov(Operand::StaticVariable(external_caught), eax);
534
535 // Set pending exception and eax to out of memory exception.
Steve Block44f0eee2011-05-26 01:26:41 +0100536 ExternalReference pending_exception(Isolate::k_pending_exception_address,
537 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100538 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
539 mov(Operand::StaticVariable(pending_exception), eax);
540 }
541
542 // Clear the context pointer.
543 Set(esi, Immediate(0));
544
545 // Restore fp from handler and discard handler state.
546 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
547 pop(ebp);
548 pop(edx); // State.
549
550 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
551 ret(0);
552}
553
554
Steve Blocka7e24c12009-10-30 11:49:00 +0000555void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
556 Register scratch,
557 Label* miss) {
558 Label same_contexts;
559
560 ASSERT(!holder_reg.is(scratch));
561
562 // Load current lexical context from the stack frame.
563 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
564
565 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +0100566 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000567 cmp(Operand(scratch), Immediate(0));
568 Check(not_equal, "we should not have an empty lexical context");
569 }
570 // Load the global context of the current context.
571 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
572 mov(scratch, FieldOperand(scratch, offset));
573 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
574
575 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100576 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000577 push(scratch);
578 // Read the first word and compare to global_context_map.
579 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100580 cmp(scratch, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 Check(equal, "JSGlobalObject::global_context should be a global context.");
582 pop(scratch);
583 }
584
585 // Check if both contexts are the same.
586 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
587 j(equal, &same_contexts, taken);
588
589 // Compare security tokens, save holder_reg on the stack so we can use it
590 // as a temporary register.
591 //
592 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
593 push(holder_reg);
594 // Check that the security token in the calling global object is
595 // compatible with the security token in the receiving global
596 // object.
597 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
598
599 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100600 if (emit_debug_code()) {
601 cmp(holder_reg, isolate()->factory()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000602 Check(not_equal, "JSGlobalProxy::context() should not be null.");
603
604 push(holder_reg);
605 // Read the first word and compare to global_context_map(),
606 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100607 cmp(holder_reg, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000608 Check(equal, "JSGlobalObject::global_context should be a global context.");
609 pop(holder_reg);
610 }
611
612 int token_offset = Context::kHeaderSize +
613 Context::SECURITY_TOKEN_INDEX * kPointerSize;
614 mov(scratch, FieldOperand(scratch, token_offset));
615 cmp(scratch, FieldOperand(holder_reg, token_offset));
616 pop(holder_reg);
617 j(not_equal, miss, not_taken);
618
619 bind(&same_contexts);
620}
621
622
623void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 Register scratch,
625 AllocationFlags flags) {
626 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100627 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000628
629 // Just return if allocation top is already known.
630 if ((flags & RESULT_CONTAINS_TOP) != 0) {
631 // No use of scratch if allocation top is provided.
632 ASSERT(scratch.is(no_reg));
633#ifdef DEBUG
634 // Assert that result actually contains top on entry.
635 cmp(result, Operand::StaticVariable(new_space_allocation_top));
636 Check(equal, "Unexpected allocation top");
637#endif
638 return;
639 }
640
641 // Move address of new object to result. Use scratch register if available.
642 if (scratch.is(no_reg)) {
643 mov(result, Operand::StaticVariable(new_space_allocation_top));
644 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 mov(Operand(scratch), Immediate(new_space_allocation_top));
646 mov(result, Operand(scratch, 0));
647 }
648}
649
650
651void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
652 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +0100653 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000654 test(result_end, Immediate(kObjectAlignmentMask));
655 Check(zero, "Unaligned allocation in new space");
656 }
657
Steve Blocka7e24c12009-10-30 11:49:00 +0000658 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100659 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000660
661 // Update new top. Use scratch if available.
662 if (scratch.is(no_reg)) {
663 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
664 } else {
665 mov(Operand(scratch, 0), result_end);
666 }
667}
668
669
670void MacroAssembler::AllocateInNewSpace(int object_size,
671 Register result,
672 Register result_end,
673 Register scratch,
674 Label* gc_required,
675 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700676 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100677 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700678 // Trash the registers to simulate an allocation failure.
679 mov(result, Immediate(0x7091));
680 if (result_end.is_valid()) {
681 mov(result_end, Immediate(0x7191));
682 }
683 if (scratch.is_valid()) {
684 mov(scratch, Immediate(0x7291));
685 }
686 }
687 jmp(gc_required);
688 return;
689 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000690 ASSERT(!result.is(result_end));
691
692 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800693 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000694
Ben Murdochbb769b22010-08-11 14:56:33 +0100695 Register top_reg = result_end.is_valid() ? result_end : result;
696
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 // Calculate new top and bail out if new space is exhausted.
698 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100699 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochbb769b22010-08-11 14:56:33 +0100700
Steve Block1e0659c2011-05-24 12:43:12 +0100701 if (!top_reg.is(result)) {
702 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +0100703 }
Steve Block1e0659c2011-05-24 12:43:12 +0100704 add(Operand(top_reg), Immediate(object_size));
705 j(carry, gc_required, not_taken);
Ben Murdochbb769b22010-08-11 14:56:33 +0100706 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 j(above, gc_required, not_taken);
708
Leon Clarkee46be812010-01-19 14:06:41 +0000709 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +0100710 UpdateAllocationTopHelper(top_reg, scratch);
711
712 // Tag result if requested.
713 if (top_reg.is(result)) {
714 if ((flags & TAG_OBJECT) != 0) {
715 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
716 } else {
717 sub(Operand(result), Immediate(object_size));
718 }
719 } else if ((flags & TAG_OBJECT) != 0) {
720 add(Operand(result), Immediate(kHeapObjectTag));
721 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000722}
723
724
725void MacroAssembler::AllocateInNewSpace(int header_size,
726 ScaleFactor element_size,
727 Register element_count,
728 Register result,
729 Register result_end,
730 Register scratch,
731 Label* gc_required,
732 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700733 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100734 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700735 // Trash the registers to simulate an allocation failure.
736 mov(result, Immediate(0x7091));
737 mov(result_end, Immediate(0x7191));
738 if (scratch.is_valid()) {
739 mov(scratch, Immediate(0x7291));
740 }
741 // Register element_count is not modified by the function.
742 }
743 jmp(gc_required);
744 return;
745 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 ASSERT(!result.is(result_end));
747
748 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800749 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000750
751 // Calculate new top and bail out if new space is exhausted.
752 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100753 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100754
755 // We assume that element_count*element_size + header_size does not
756 // overflow.
757 lea(result_end, Operand(element_count, element_size, header_size));
758 add(result_end, Operand(result));
759 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000760 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
761 j(above, gc_required);
762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 // Tag result if requested.
764 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000765 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 }
Leon Clarkee46be812010-01-19 14:06:41 +0000767
768 // Update allocation top.
769 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000770}
771
772
773void MacroAssembler::AllocateInNewSpace(Register object_size,
774 Register result,
775 Register result_end,
776 Register scratch,
777 Label* gc_required,
778 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700779 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100780 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700781 // Trash the registers to simulate an allocation failure.
782 mov(result, Immediate(0x7091));
783 mov(result_end, Immediate(0x7191));
784 if (scratch.is_valid()) {
785 mov(scratch, Immediate(0x7291));
786 }
787 // object_size is left unchanged by this function.
788 }
789 jmp(gc_required);
790 return;
791 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 ASSERT(!result.is(result_end));
793
794 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800795 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000796
797 // Calculate new top and bail out if new space is exhausted.
798 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100799 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000800 if (!object_size.is(result_end)) {
801 mov(result_end, object_size);
802 }
803 add(result_end, Operand(result));
Steve Block1e0659c2011-05-24 12:43:12 +0100804 j(carry, gc_required, not_taken);
Steve Blocka7e24c12009-10-30 11:49:00 +0000805 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
806 j(above, gc_required, not_taken);
807
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 // Tag result if requested.
809 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000810 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 }
Leon Clarkee46be812010-01-19 14:06:41 +0000812
813 // Update allocation top.
814 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000815}
816
817
818void MacroAssembler::UndoAllocationInNewSpace(Register object) {
819 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100820 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000821
822 // Make sure the object has no tag before resetting top.
823 and_(Operand(object), Immediate(~kHeapObjectTagMask));
824#ifdef DEBUG
825 cmp(object, Operand::StaticVariable(new_space_allocation_top));
826 Check(below, "Undo allocation of non allocated memory");
827#endif
828 mov(Operand::StaticVariable(new_space_allocation_top), object);
829}
830
831
Steve Block3ce2e202009-11-05 08:53:23 +0000832void MacroAssembler::AllocateHeapNumber(Register result,
833 Register scratch1,
834 Register scratch2,
835 Label* gc_required) {
836 // Allocate heap number in new space.
837 AllocateInNewSpace(HeapNumber::kSize,
838 result,
839 scratch1,
840 scratch2,
841 gc_required,
842 TAG_OBJECT);
843
844 // Set the map.
845 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100846 Immediate(isolate()->factory()->heap_number_map()));
Steve Block3ce2e202009-11-05 08:53:23 +0000847}
848
849
Steve Blockd0582a62009-12-15 09:54:21 +0000850void MacroAssembler::AllocateTwoByteString(Register result,
851 Register length,
852 Register scratch1,
853 Register scratch2,
854 Register scratch3,
855 Label* gc_required) {
856 // Calculate the number of bytes needed for the characters in the string while
857 // observing object alignment.
858 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000859 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000860 // scratch1 = length * 2 + kObjectAlignmentMask.
861 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000862 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
863
864 // Allocate two byte string in new space.
865 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
866 times_1,
867 scratch1,
868 result,
869 scratch2,
870 scratch3,
871 gc_required,
872 TAG_OBJECT);
873
874 // Set the map, length and hash field.
875 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100876 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100877 mov(scratch1, length);
878 SmiTag(scratch1);
879 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000880 mov(FieldOperand(result, String::kHashFieldOffset),
881 Immediate(String::kEmptyHashField));
882}
883
884
885void MacroAssembler::AllocateAsciiString(Register result,
886 Register length,
887 Register scratch1,
888 Register scratch2,
889 Register scratch3,
890 Label* gc_required) {
891 // Calculate the number of bytes needed for the characters in the string while
892 // observing object alignment.
893 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
894 mov(scratch1, length);
895 ASSERT(kCharSize == 1);
896 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
897 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
898
899 // Allocate ascii string in new space.
900 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
901 times_1,
902 scratch1,
903 result,
904 scratch2,
905 scratch3,
906 gc_required,
907 TAG_OBJECT);
908
909 // Set the map, length and hash field.
910 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100911 Immediate(isolate()->factory()->ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100912 mov(scratch1, length);
913 SmiTag(scratch1);
914 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000915 mov(FieldOperand(result, String::kHashFieldOffset),
916 Immediate(String::kEmptyHashField));
917}
918
919
Iain Merrick9ac36c92010-09-13 15:29:50 +0100920void MacroAssembler::AllocateAsciiString(Register result,
921 int length,
922 Register scratch1,
923 Register scratch2,
924 Label* gc_required) {
925 ASSERT(length > 0);
926
927 // Allocate ascii string in new space.
928 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
929 result,
930 scratch1,
931 scratch2,
932 gc_required,
933 TAG_OBJECT);
934
935 // Set the map, length and hash field.
936 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100937 Immediate(isolate()->factory()->ascii_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100938 mov(FieldOperand(result, String::kLengthOffset),
939 Immediate(Smi::FromInt(length)));
940 mov(FieldOperand(result, String::kHashFieldOffset),
941 Immediate(String::kEmptyHashField));
942}
943
944
Steve Blockd0582a62009-12-15 09:54:21 +0000945void MacroAssembler::AllocateConsString(Register result,
946 Register scratch1,
947 Register scratch2,
948 Label* gc_required) {
949 // Allocate heap number in new space.
950 AllocateInNewSpace(ConsString::kSize,
951 result,
952 scratch1,
953 scratch2,
954 gc_required,
955 TAG_OBJECT);
956
957 // Set the map. The other fields are left uninitialized.
958 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100959 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +0000960}
961
962
963void MacroAssembler::AllocateAsciiConsString(Register result,
964 Register scratch1,
965 Register scratch2,
966 Label* gc_required) {
967 // Allocate heap number in new space.
968 AllocateInNewSpace(ConsString::kSize,
969 result,
970 scratch1,
971 scratch2,
972 gc_required,
973 TAG_OBJECT);
974
975 // Set the map. The other fields are left uninitialized.
976 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100977 Immediate(isolate()->factory()->cons_ascii_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +0000978}
979
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800980
Ben Murdochb8e0da22011-05-16 14:20:40 +0100981// Copy memory, byte-by-byte, from source to destination. Not optimized for
982// long or aligned copies. The contents of scratch and length are destroyed.
983// Source and destination are incremented by length.
984// Many variants of movsb, loop unrolling, word moves, and indexed operands
985// have been tried here already, and this is fastest.
986// A simpler loop is faster on small copies, but 30% slower on large ones.
987// The cld() instruction must have been emitted, to set the direction flag(),
988// before calling this function.
989void MacroAssembler::CopyBytes(Register source,
990 Register destination,
991 Register length,
992 Register scratch) {
993 Label loop, done, short_string, short_loop;
994 // Experimentation shows that the short string loop is faster if length < 10.
995 cmp(Operand(length), Immediate(10));
996 j(less_equal, &short_string);
997
998 ASSERT(source.is(esi));
999 ASSERT(destination.is(edi));
1000 ASSERT(length.is(ecx));
1001
1002 // Because source is 4-byte aligned in our uses of this function,
1003 // we keep source aligned for the rep_movs call by copying the odd bytes
1004 // at the end of the ranges.
1005 mov(scratch, Operand(source, length, times_1, -4));
1006 mov(Operand(destination, length, times_1, -4), scratch);
1007 mov(scratch, ecx);
1008 shr(ecx, 2);
1009 rep_movs();
1010 and_(Operand(scratch), Immediate(0x3));
1011 add(destination, Operand(scratch));
1012 jmp(&done);
1013
1014 bind(&short_string);
1015 test(length, Operand(length));
1016 j(zero, &done);
1017
1018 bind(&short_loop);
1019 mov_b(scratch, Operand(source, 0));
1020 mov_b(Operand(destination, 0), scratch);
1021 inc(source);
1022 inc(destination);
1023 dec(length);
1024 j(not_zero, &short_loop);
1025
1026 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001027}
1028
Steve Blockd0582a62009-12-15 09:54:21 +00001029
Steve Blocka7e24c12009-10-30 11:49:00 +00001030void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
1031 Register result,
1032 Register op,
1033 JumpTarget* then_target) {
1034 JumpTarget ok;
1035 test(result, Operand(result));
1036 ok.Branch(not_zero, taken);
1037 test(op, Operand(op));
1038 then_target->Branch(sign, not_taken);
1039 ok.Bind();
1040}
1041
1042
1043void MacroAssembler::NegativeZeroTest(Register result,
1044 Register op,
1045 Label* then_label) {
1046 Label ok;
1047 test(result, Operand(result));
1048 j(not_zero, &ok, taken);
1049 test(op, Operand(op));
1050 j(sign, then_label, not_taken);
1051 bind(&ok);
1052}
1053
1054
1055void MacroAssembler::NegativeZeroTest(Register result,
1056 Register op1,
1057 Register op2,
1058 Register scratch,
1059 Label* then_label) {
1060 Label ok;
1061 test(result, Operand(result));
1062 j(not_zero, &ok, taken);
1063 mov(scratch, Operand(op1));
1064 or_(scratch, Operand(op2));
1065 j(sign, then_label, not_taken);
1066 bind(&ok);
1067}
1068
1069
1070void MacroAssembler::TryGetFunctionPrototype(Register function,
1071 Register result,
1072 Register scratch,
1073 Label* miss) {
1074 // Check that the receiver isn't a smi.
1075 test(function, Immediate(kSmiTagMask));
1076 j(zero, miss, not_taken);
1077
1078 // Check that the function really is a function.
1079 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1080 j(not_equal, miss, not_taken);
1081
1082 // Make sure that the function has an instance prototype.
1083 Label non_instance;
1084 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1085 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1086 j(not_zero, &non_instance, not_taken);
1087
1088 // Get the prototype or initial map from the function.
1089 mov(result,
1090 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1091
1092 // If the prototype or initial map is the hole, don't return it and
1093 // simply miss the cache instead. This will allow us to allocate a
1094 // prototype object on-demand in the runtime system.
Steve Block44f0eee2011-05-26 01:26:41 +01001095 cmp(Operand(result), Immediate(isolate()->factory()->the_hole_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001096 j(equal, miss, not_taken);
1097
1098 // If the function does not have an initial map, we're done.
1099 Label done;
1100 CmpObjectType(result, MAP_TYPE, scratch);
1101 j(not_equal, &done);
1102
1103 // Get the prototype from the initial map.
1104 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1105 jmp(&done);
1106
1107 // Non-instance prototype: Fetch prototype from constructor field
1108 // in initial map.
1109 bind(&non_instance);
1110 mov(result, FieldOperand(result, Map::kConstructorOffset));
1111
1112 // All done.
1113 bind(&done);
1114}
1115
1116
1117void MacroAssembler::CallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001118 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001119 call(stub->GetCode(), RelocInfo::CODE_TARGET);
1120}
1121
1122
John Reck59135872010-11-02 12:39:01 -07001123MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001124 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001125 Object* result;
1126 { MaybeObject* maybe_result = stub->TryGetCode();
1127 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001128 }
John Reck59135872010-11-02 12:39:01 -07001129 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001130 return result;
1131}
1132
1133
Steve Blockd0582a62009-12-15 09:54:21 +00001134void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001135 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +00001136 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1137}
1138
1139
John Reck59135872010-11-02 12:39:01 -07001140MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001141 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001142 Object* result;
1143 { MaybeObject* maybe_result = stub->TryGetCode();
1144 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001145 }
John Reck59135872010-11-02 12:39:01 -07001146 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001147 return result;
1148}
1149
1150
Steve Blocka7e24c12009-10-30 11:49:00 +00001151void MacroAssembler::StubReturn(int argc) {
1152 ASSERT(argc >= 1 && generating_stub());
1153 ret((argc - 1) * kPointerSize);
1154}
1155
1156
1157void MacroAssembler::IllegalOperation(int num_arguments) {
1158 if (num_arguments > 0) {
1159 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1160 }
Steve Block44f0eee2011-05-26 01:26:41 +01001161 mov(eax, Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001162}
1163
1164
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001165void MacroAssembler::IndexFromHash(Register hash, Register index) {
1166 // The assert checks that the constants for the maximum number of digits
1167 // for an array index cached in the hash field and the number of bits
1168 // reserved for it does not conflict.
1169 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1170 (1 << String::kArrayIndexValueBits));
1171 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1172 // the low kHashShift bits.
1173 and_(hash, String::kArrayIndexValueMask);
1174 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1175 if (String::kHashShift > kSmiTagSize) {
1176 shr(hash, String::kHashShift - kSmiTagSize);
1177 }
1178 if (!index.is(hash)) {
1179 mov(index, hash);
1180 }
1181}
1182
1183
Steve Blocka7e24c12009-10-30 11:49:00 +00001184void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1185 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1186}
1187
1188
Ben Murdochb0fe1622011-05-05 13:52:32 +01001189void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01001190 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001191 Set(eax, Immediate(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01001192 mov(ebx, Immediate(ExternalReference(function, isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001193 CEntryStub ces(1);
1194 ces.SaveDoubles();
1195 CallStub(&ces);
1196}
1197
1198
John Reck59135872010-11-02 12:39:01 -07001199MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1200 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001201 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1202}
1203
1204
Steve Block44f0eee2011-05-26 01:26:41 +01001205void MacroAssembler::CallRuntime(const Runtime::Function* f,
1206 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 // If the expected number of arguments of the runtime function is
1208 // constant, we check that the actual number of arguments match the
1209 // expectation.
1210 if (f->nargs >= 0 && f->nargs != num_arguments) {
1211 IllegalOperation(num_arguments);
1212 return;
1213 }
1214
Leon Clarke4515c472010-02-03 11:58:03 +00001215 // TODO(1236192): Most runtime routines don't need the number of
1216 // arguments passed in because it is constant. At some point we
1217 // should remove this need and make the runtime routine entry code
1218 // smarter.
1219 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001220 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001221 CEntryStub ces(1);
1222 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001223}
1224
1225
Steve Block44f0eee2011-05-26 01:26:41 +01001226MaybeObject* MacroAssembler::TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -07001227 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001228 if (f->nargs >= 0 && f->nargs != num_arguments) {
1229 IllegalOperation(num_arguments);
1230 // Since we did not call the stub, there was no allocation failure.
1231 // Return some non-failure object.
Steve Block44f0eee2011-05-26 01:26:41 +01001232 return isolate()->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +00001233 }
1234
Leon Clarke4515c472010-02-03 11:58:03 +00001235 // TODO(1236192): Most runtime routines don't need the number of
1236 // arguments passed in because it is constant. At some point we
1237 // should remove this need and make the runtime routine entry code
1238 // smarter.
1239 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001240 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001241 CEntryStub ces(1);
1242 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001243}
1244
1245
Ben Murdochbb769b22010-08-11 14:56:33 +01001246void MacroAssembler::CallExternalReference(ExternalReference ref,
1247 int num_arguments) {
1248 mov(eax, Immediate(num_arguments));
1249 mov(ebx, Immediate(ref));
1250
1251 CEntryStub stub(1);
1252 CallStub(&stub);
1253}
1254
1255
Steve Block6ded16b2010-05-10 14:33:55 +01001256void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1257 int num_arguments,
1258 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001259 // TODO(1236192): Most runtime routines don't need the number of
1260 // arguments passed in because it is constant. At some point we
1261 // should remove this need and make the runtime routine entry code
1262 // smarter.
1263 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001264 JumpToExternalReference(ext);
1265}
1266
1267
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001268MaybeObject* MacroAssembler::TryTailCallExternalReference(
1269 const ExternalReference& ext, int num_arguments, int result_size) {
1270 // TODO(1236192): Most runtime routines don't need the number of
1271 // arguments passed in because it is constant. At some point we
1272 // should remove this need and make the runtime routine entry code
1273 // smarter.
1274 Set(eax, Immediate(num_arguments));
1275 return TryJumpToExternalReference(ext);
1276}
1277
1278
Steve Block6ded16b2010-05-10 14:33:55 +01001279void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1280 int num_arguments,
1281 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01001282 TailCallExternalReference(ExternalReference(fid, isolate()),
1283 num_arguments,
1284 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001285}
1286
1287
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001288MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1289 int num_arguments,
1290 int result_size) {
1291 return TryTailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001292 ExternalReference(fid, isolate()), num_arguments, result_size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001293}
1294
1295
Ben Murdochb0fe1622011-05-05 13:52:32 +01001296// If true, a Handle<T> returned by value from a function with cdecl calling
1297// convention will be returned directly as a value of location_ field in a
1298// register eax.
1299// If false, it is returned as a pointer to a preallocated by caller memory
1300// region. Pointer to this region should be passed to a function as an
1301// implicit first argument.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001302#if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001303static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001304#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001305static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001306#endif
1307
1308
1309Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001310 return Operand(
1311 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001312}
1313
1314
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001315void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001316 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001317 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001318 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001319 // space for and pass an out parameter.
1320 } else {
1321 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001322 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001323
John Reck59135872010-11-02 12:39:01 -07001324 // The argument slots are filled as follows:
1325 //
1326 // n + 1: output cell
1327 // n: arg n
1328 // ...
1329 // 1: arg1
1330 // 0: pointer to the output cell
1331 //
1332 // Note that this is one more "argument" than the function expects
1333 // so the out cell will have to be popped explicitly after returning
1334 // from the function. The out cell contains Handle.
John Reck59135872010-11-02 12:39:01 -07001335
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001336 // pointer to out cell.
1337 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1338 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
Steve Block44f0eee2011-05-26 01:26:41 +01001339 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001340 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1341 }
1342 }
1343}
1344
1345
1346MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1347 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001348 ExternalReference next_address =
1349 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001350 ExternalReference limit_address =
1351 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001352 ExternalReference level_address =
1353 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001354
John Reck59135872010-11-02 12:39:01 -07001355 // Allocate HandleScope in callee-save registers.
1356 mov(ebx, Operand::StaticVariable(next_address));
1357 mov(edi, Operand::StaticVariable(limit_address));
1358 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001359
John Reck59135872010-11-02 12:39:01 -07001360 // Call the api function!
1361 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1362
Ben Murdochb0fe1622011-05-05 13:52:32 +01001363 if (!kReturnHandlesDirectly) {
John Reck59135872010-11-02 12:39:01 -07001364 // The returned value is a pointer to the handle holding the result.
1365 // Dereference this to get to the location.
1366 mov(eax, Operand(eax, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001367 }
Steve Blockd0582a62009-12-15 09:54:21 +00001368
John Reck59135872010-11-02 12:39:01 -07001369 Label empty_handle;
1370 Label prologue;
1371 Label promote_scheduled_exception;
1372 Label delete_allocated_handles;
1373 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001374
John Reck59135872010-11-02 12:39:01 -07001375 // Check if the result handle holds 0.
1376 test(eax, Operand(eax));
1377 j(zero, &empty_handle, not_taken);
1378 // It was non-zero. Dereference to get the result value.
1379 mov(eax, Operand(eax, 0));
1380 bind(&prologue);
1381 // No more valid handles (the result handle was the last one). Restore
1382 // previous handle scope.
1383 mov(Operand::StaticVariable(next_address), ebx);
1384 sub(Operand::StaticVariable(level_address), Immediate(1));
1385 Assert(above_equal, "Invalid HandleScope level");
1386 cmp(edi, Operand::StaticVariable(limit_address));
1387 j(not_equal, &delete_allocated_handles, not_taken);
1388 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001389
John Reck59135872010-11-02 12:39:01 -07001390 // Check if the function scheduled an exception.
1391 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01001392 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07001393 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01001394 Immediate(isolate()->factory()->the_hole_value()));
John Reck59135872010-11-02 12:39:01 -07001395 j(not_equal, &promote_scheduled_exception, not_taken);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001396 LeaveApiExitFrame();
1397 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001398 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001399 MaybeObject* result =
1400 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1401 if (result->IsFailure()) {
1402 return result;
1403 }
John Reck59135872010-11-02 12:39:01 -07001404 bind(&empty_handle);
1405 // It was zero; the result is undefined.
Steve Block44f0eee2011-05-26 01:26:41 +01001406 mov(eax, isolate()->factory()->undefined_value());
John Reck59135872010-11-02 12:39:01 -07001407 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001408
John Reck59135872010-11-02 12:39:01 -07001409 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01001410 ExternalReference delete_extensions =
1411 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07001412 bind(&delete_allocated_handles);
1413 mov(Operand::StaticVariable(limit_address), edi);
1414 mov(edi, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001415 mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
1416 mov(eax, Immediate(delete_extensions));
John Reck59135872010-11-02 12:39:01 -07001417 call(Operand(eax));
1418 mov(eax, edi);
1419 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001420
1421 return result;
Steve Blockd0582a62009-12-15 09:54:21 +00001422}
1423
1424
Steve Block6ded16b2010-05-10 14:33:55 +01001425void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001426 // Set the entry point and jump to the C entry runtime stub.
1427 mov(ebx, Immediate(ext));
1428 CEntryStub ces(1);
1429 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1430}
1431
1432
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001433MaybeObject* MacroAssembler::TryJumpToExternalReference(
1434 const ExternalReference& ext) {
1435 // Set the entry point and jump to the C entry runtime stub.
1436 mov(ebx, Immediate(ext));
1437 CEntryStub ces(1);
1438 return TryTailCallStub(&ces);
1439}
1440
1441
Steve Blocka7e24c12009-10-30 11:49:00 +00001442void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1443 const ParameterCount& actual,
1444 Handle<Code> code_constant,
1445 const Operand& code_operand,
Steve Block44f0eee2011-05-26 01:26:41 +01001446 NearLabel* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001447 InvokeFlag flag,
1448 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 bool definitely_matches = false;
1450 Label invoke;
1451 if (expected.is_immediate()) {
1452 ASSERT(actual.is_immediate());
1453 if (expected.immediate() == actual.immediate()) {
1454 definitely_matches = true;
1455 } else {
1456 mov(eax, actual.immediate());
1457 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1458 if (expected.immediate() == sentinel) {
1459 // Don't worry about adapting arguments for builtins that
1460 // don't want that done. Skip adaption code by making it look
1461 // like we have a match between expected and actual number of
1462 // arguments.
1463 definitely_matches = true;
1464 } else {
1465 mov(ebx, expected.immediate());
1466 }
1467 }
1468 } else {
1469 if (actual.is_immediate()) {
1470 // Expected is in register, actual is immediate. This is the
1471 // case when we invoke function values without going through the
1472 // IC mechanism.
1473 cmp(expected.reg(), actual.immediate());
1474 j(equal, &invoke);
1475 ASSERT(expected.reg().is(ebx));
1476 mov(eax, actual.immediate());
1477 } else if (!expected.reg().is(actual.reg())) {
1478 // Both expected and actual are in (different) registers. This
1479 // is the case when we invoke functions using call and apply.
1480 cmp(expected.reg(), Operand(actual.reg()));
1481 j(equal, &invoke);
1482 ASSERT(actual.reg().is(eax));
1483 ASSERT(expected.reg().is(ebx));
1484 }
1485 }
1486
1487 if (!definitely_matches) {
1488 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001489 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001490 if (!code_constant.is_null()) {
1491 mov(edx, Immediate(code_constant));
1492 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1493 } else if (!code_operand.is_reg(edx)) {
1494 mov(edx, code_operand);
1495 }
1496
1497 if (flag == CALL_FUNCTION) {
1498 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001499 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001500 jmp(done);
1501 } else {
1502 jmp(adaptor, RelocInfo::CODE_TARGET);
1503 }
1504 bind(&invoke);
1505 }
1506}
1507
1508
1509void MacroAssembler::InvokeCode(const Operand& code,
1510 const ParameterCount& expected,
1511 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001512 InvokeFlag flag,
1513 PostCallGenerator* post_call_generator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001514 NearLabel done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001515 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1516 &done, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 if (flag == CALL_FUNCTION) {
1518 call(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001519 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001520 } else {
1521 ASSERT(flag == JUMP_FUNCTION);
1522 jmp(code);
1523 }
1524 bind(&done);
1525}
1526
1527
1528void MacroAssembler::InvokeCode(Handle<Code> code,
1529 const ParameterCount& expected,
1530 const ParameterCount& actual,
1531 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001532 InvokeFlag flag,
1533 PostCallGenerator* post_call_generator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001534 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +00001535 Operand dummy(eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001536 InvokePrologue(expected, actual, code, dummy, &done,
1537 flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001538 if (flag == CALL_FUNCTION) {
1539 call(code, rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001540 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001541 } else {
1542 ASSERT(flag == JUMP_FUNCTION);
1543 jmp(code, rmode);
1544 }
1545 bind(&done);
1546}
1547
1548
1549void MacroAssembler::InvokeFunction(Register fun,
1550 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001551 InvokeFlag flag,
1552 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001553 ASSERT(fun.is(edi));
1554 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1555 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1556 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001557 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001558
1559 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01001560 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001561 expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001562}
1563
1564
Andrei Popescu402d9372010-02-26 13:31:12 +00001565void MacroAssembler::InvokeFunction(JSFunction* function,
1566 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001567 InvokeFlag flag,
1568 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001569 ASSERT(function->is_compiled());
1570 // Get the function and setup the context.
1571 mov(edi, Immediate(Handle<JSFunction>(function)));
1572 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001573
Andrei Popescu402d9372010-02-26 13:31:12 +00001574 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001575 if (V8::UseCrankshaft()) {
1576 // TODO(kasperl): For now, we always call indirectly through the
1577 // code field in the function to allow recompilation to take effect
1578 // without changing any of the call sites.
1579 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1580 expected, actual, flag, post_call_generator);
1581 } else {
1582 Handle<Code> code(function->code());
1583 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
1584 flag, post_call_generator);
1585 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001586}
1587
1588
Ben Murdochb0fe1622011-05-05 13:52:32 +01001589void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1590 InvokeFlag flag,
1591 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001592 // Calls are not allowed in some stubs.
1593 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1594
1595 // Rely on the assertion to check that the number of provided
1596 // arguments match the expected number of arguments. Fake a
1597 // parameter count to avoid emitting code to do the check.
1598 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01001599 GetBuiltinFunction(edi, id);
1600 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001601 expected, expected, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001602}
1603
Steve Block791712a2010-08-27 10:21:07 +01001604void MacroAssembler::GetBuiltinFunction(Register target,
1605 Builtins::JavaScript id) {
1606 // Load the JavaScript builtin function from the builtins object.
1607 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1608 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1609 mov(target, FieldOperand(target,
1610 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1611}
Steve Blocka7e24c12009-10-30 11:49:00 +00001612
1613void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001614 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00001615 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01001616 GetBuiltinFunction(edi, id);
1617 // Load the code entry point from the function into the target register.
1618 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001619}
1620
1621
Steve Blockd0582a62009-12-15 09:54:21 +00001622void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1623 if (context_chain_length > 0) {
1624 // Move up the chain of contexts to the context containing the slot.
1625 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1626 // Load the function context (which is the incoming, outer context).
1627 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1628 for (int i = 1; i < context_chain_length; i++) {
1629 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1630 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1631 }
Steve Block1e0659c2011-05-24 12:43:12 +01001632 } else {
1633 // Slot is in the current function context. Move it into the
1634 // destination register in case we store into it (the write barrier
1635 // cannot be allowed to destroy the context in esi).
1636 mov(dst, esi);
1637 }
1638
1639 // We should not have found a 'with' context by walking the context chain
1640 // (i.e., the static scope chain and runtime context chain do not agree).
1641 // A variable occurring in such a scope should have slot type LOOKUP and
1642 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01001643 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001644 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1645 Check(equal, "Yo dawg, I heard you liked function contexts "
1646 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00001647 }
1648}
1649
1650
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001651void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1652 // Load the global or builtins object from the current context.
1653 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1654 // Load the global context from the global or builtins object.
1655 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1656 // Load the function from the global context.
1657 mov(function, Operand(function, Context::SlotOffset(index)));
1658}
1659
1660
1661void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1662 Register map) {
1663 // Load the initial map. The global functions all have initial maps.
1664 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001665 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001666 Label ok, fail;
Steve Block44f0eee2011-05-26 01:26:41 +01001667 CheckMap(map, isolate()->factory()->meta_map(), &fail, false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001668 jmp(&ok);
1669 bind(&fail);
1670 Abort("Global functions must have initial map");
1671 bind(&ok);
1672 }
1673}
1674
Steve Blockd0582a62009-12-15 09:54:21 +00001675
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001676// Store the value in register src in the safepoint register stack
1677// slot for register dst.
1678void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
1679 mov(SafepointRegisterSlot(dst), src);
1680}
1681
1682
1683void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
1684 mov(SafepointRegisterSlot(dst), src);
1685}
1686
1687
1688void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
1689 mov(dst, SafepointRegisterSlot(src));
1690}
1691
1692
1693Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
1694 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
1695}
1696
1697
Ben Murdochb0fe1622011-05-05 13:52:32 +01001698int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1699 // The registers are pushed starting with the lowest encoding,
1700 // which means that lowest encodings are furthest away from
1701 // the stack pointer.
1702 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1703 return kNumSafepointRegisters - reg_code - 1;
1704}
1705
1706
Steve Blocka7e24c12009-10-30 11:49:00 +00001707void MacroAssembler::Ret() {
1708 ret(0);
1709}
1710
1711
Steve Block1e0659c2011-05-24 12:43:12 +01001712void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1713 if (is_uint16(bytes_dropped)) {
1714 ret(bytes_dropped);
1715 } else {
1716 pop(scratch);
1717 add(Operand(esp), Immediate(bytes_dropped));
1718 push(scratch);
1719 ret(0);
1720 }
1721}
1722
1723
1724
1725
Leon Clarkee46be812010-01-19 14:06:41 +00001726void MacroAssembler::Drop(int stack_elements) {
1727 if (stack_elements > 0) {
1728 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1729 }
1730}
1731
1732
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001733void MacroAssembler::Move(Register dst, Register src) {
1734 if (!dst.is(src)) {
1735 mov(dst, src);
1736 }
1737}
1738
1739
Leon Clarkee46be812010-01-19 14:06:41 +00001740void MacroAssembler::Move(Register dst, Handle<Object> value) {
1741 mov(dst, value);
1742}
1743
1744
Steve Blocka7e24c12009-10-30 11:49:00 +00001745void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1746 if (FLAG_native_code_counters && counter->Enabled()) {
1747 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1748 }
1749}
1750
1751
1752void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1753 ASSERT(value > 0);
1754 if (FLAG_native_code_counters && counter->Enabled()) {
1755 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1756 if (value == 1) {
1757 inc(operand);
1758 } else {
1759 add(operand, Immediate(value));
1760 }
1761 }
1762}
1763
1764
1765void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1766 ASSERT(value > 0);
1767 if (FLAG_native_code_counters && counter->Enabled()) {
1768 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1769 if (value == 1) {
1770 dec(operand);
1771 } else {
1772 sub(operand, Immediate(value));
1773 }
1774 }
1775}
1776
1777
Leon Clarked91b9f72010-01-27 17:25:45 +00001778void MacroAssembler::IncrementCounter(Condition cc,
1779 StatsCounter* counter,
1780 int value) {
1781 ASSERT(value > 0);
1782 if (FLAG_native_code_counters && counter->Enabled()) {
1783 Label skip;
1784 j(NegateCondition(cc), &skip);
1785 pushfd();
1786 IncrementCounter(counter, value);
1787 popfd();
1788 bind(&skip);
1789 }
1790}
1791
1792
1793void MacroAssembler::DecrementCounter(Condition cc,
1794 StatsCounter* counter,
1795 int value) {
1796 ASSERT(value > 0);
1797 if (FLAG_native_code_counters && counter->Enabled()) {
1798 Label skip;
1799 j(NegateCondition(cc), &skip);
1800 pushfd();
1801 DecrementCounter(counter, value);
1802 popfd();
1803 bind(&skip);
1804 }
1805}
1806
1807
Steve Blocka7e24c12009-10-30 11:49:00 +00001808void MacroAssembler::Assert(Condition cc, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01001809 if (emit_debug_code()) Check(cc, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00001810}
1811
1812
Iain Merrick75681382010-08-19 15:07:18 +01001813void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01001814 if (emit_debug_code()) {
1815 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01001816 Label ok;
1817 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001818 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001819 j(equal, &ok);
1820 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001821 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001822 j(equal, &ok);
1823 Abort("JSObject with fast elements map has slow elements");
1824 bind(&ok);
1825 }
1826}
1827
1828
Steve Blocka7e24c12009-10-30 11:49:00 +00001829void MacroAssembler::Check(Condition cc, const char* msg) {
1830 Label L;
1831 j(cc, &L, taken);
1832 Abort(msg);
1833 // will not return here
1834 bind(&L);
1835}
1836
1837
Steve Block6ded16b2010-05-10 14:33:55 +01001838void MacroAssembler::CheckStackAlignment() {
1839 int frame_alignment = OS::ActivationFrameAlignment();
1840 int frame_alignment_mask = frame_alignment - 1;
1841 if (frame_alignment > kPointerSize) {
1842 ASSERT(IsPowerOf2(frame_alignment));
1843 Label alignment_as_expected;
1844 test(esp, Immediate(frame_alignment_mask));
1845 j(zero, &alignment_as_expected);
1846 // Abort if stack is not aligned.
1847 int3();
1848 bind(&alignment_as_expected);
1849 }
1850}
1851
1852
Steve Blocka7e24c12009-10-30 11:49:00 +00001853void MacroAssembler::Abort(const char* msg) {
1854 // We want to pass the msg string like a smi to avoid GC
1855 // problems, however msg is not guaranteed to be aligned
1856 // properly. Instead, we pass an aligned pointer that is
1857 // a proper v8 smi, but also pass the alignment difference
1858 // from the real pointer as a smi.
1859 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1860 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1861 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1862#ifdef DEBUG
1863 if (msg != NULL) {
1864 RecordComment("Abort message: ");
1865 RecordComment(msg);
1866 }
1867#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001868 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001869 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001870
Steve Blocka7e24c12009-10-30 11:49:00 +00001871 push(eax);
1872 push(Immediate(p0));
1873 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1874 CallRuntime(Runtime::kAbort, 2);
1875 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00001876 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00001877}
1878
1879
Iain Merrick75681382010-08-19 15:07:18 +01001880void MacroAssembler::JumpIfNotNumber(Register reg,
1881 TypeInfo info,
1882 Label* on_not_number) {
Steve Block44f0eee2011-05-26 01:26:41 +01001883 if (emit_debug_code()) AbortIfSmi(reg);
Iain Merrick75681382010-08-19 15:07:18 +01001884 if (!info.IsNumber()) {
1885 cmp(FieldOperand(reg, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001886 isolate()->factory()->heap_number_map());
Iain Merrick75681382010-08-19 15:07:18 +01001887 j(not_equal, on_not_number);
1888 }
1889}
1890
1891
1892void MacroAssembler::ConvertToInt32(Register dst,
1893 Register source,
1894 Register scratch,
1895 TypeInfo info,
1896 Label* on_not_int32) {
Steve Block44f0eee2011-05-26 01:26:41 +01001897 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01001898 AbortIfSmi(source);
1899 AbortIfNotNumber(source);
1900 }
1901 if (info.IsInteger32()) {
1902 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1903 } else {
1904 Label done;
1905 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1906 ASSERT(!scratch.is(source));
1907 if (push_pop) {
1908 push(dst);
1909 scratch = dst;
1910 }
1911 if (scratch.is(no_reg)) scratch = dst;
1912 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1913 cmp(scratch, 0x80000000u);
1914 if (push_pop) {
1915 j(not_equal, &done);
1916 pop(dst);
1917 jmp(on_not_int32);
1918 } else {
1919 j(equal, on_not_int32);
1920 }
1921
1922 bind(&done);
1923 if (push_pop) {
1924 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1925 }
1926 if (!scratch.is(dst)) {
1927 mov(dst, scratch);
1928 }
1929 }
1930}
1931
1932
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001933void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1934 Register scratch,
1935 int power) {
1936 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1937 HeapNumber::kExponentBits));
1938 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1939 movd(dst, Operand(scratch));
1940 psllq(dst, HeapNumber::kMantissaBits);
1941}
1942
1943
Andrei Popescu402d9372010-02-26 13:31:12 +00001944void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1945 Register instance_type,
1946 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01001947 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001948 if (!scratch.is(instance_type)) {
1949 mov(scratch, instance_type);
1950 }
1951 and_(scratch,
1952 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1953 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1954 j(not_equal, failure);
1955}
1956
1957
Leon Clarked91b9f72010-01-27 17:25:45 +00001958void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1959 Register object2,
1960 Register scratch1,
1961 Register scratch2,
1962 Label* failure) {
1963 // Check that both objects are not smis.
1964 ASSERT_EQ(0, kSmiTag);
1965 mov(scratch1, Operand(object1));
1966 and_(scratch1, Operand(object2));
1967 test(scratch1, Immediate(kSmiTagMask));
1968 j(zero, failure);
1969
1970 // Load instance type for both strings.
1971 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1972 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1973 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1974 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1975
1976 // Check that both are flat ascii strings.
1977 const int kFlatAsciiStringMask =
1978 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1979 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1980 // Interleave bits from both instance types and compare them in one check.
1981 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1982 and_(scratch1, kFlatAsciiStringMask);
1983 and_(scratch2, kFlatAsciiStringMask);
1984 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1985 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1986 j(not_equal, failure);
1987}
1988
1989
Steve Block6ded16b2010-05-10 14:33:55 +01001990void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01001991 // Reserve space for Isolate address which is always passed as last parameter
1992 num_arguments += 1;
1993
Steve Block6ded16b2010-05-10 14:33:55 +01001994 int frameAlignment = OS::ActivationFrameAlignment();
1995 if (frameAlignment != 0) {
1996 // Make stack end at alignment and make room for num_arguments words
1997 // and the original value of esp.
1998 mov(scratch, esp);
1999 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
2000 ASSERT(IsPowerOf2(frameAlignment));
2001 and_(esp, -frameAlignment);
2002 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2003 } else {
2004 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
2005 }
2006}
2007
2008
2009void MacroAssembler::CallCFunction(ExternalReference function,
2010 int num_arguments) {
2011 // Trashing eax is ok as it will be the return value.
2012 mov(Operand(eax), Immediate(function));
2013 CallCFunction(eax, num_arguments);
2014}
2015
2016
2017void MacroAssembler::CallCFunction(Register function,
2018 int num_arguments) {
Steve Block44f0eee2011-05-26 01:26:41 +01002019 // Pass current isolate address as additional parameter.
2020 mov(Operand(esp, num_arguments * kPointerSize),
2021 Immediate(ExternalReference::isolate_address()));
2022 num_arguments += 1;
2023
Steve Block6ded16b2010-05-10 14:33:55 +01002024 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01002025 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002026 CheckStackAlignment();
2027 }
2028
2029 call(Operand(function));
2030 if (OS::ActivationFrameAlignment() != 0) {
2031 mov(esp, Operand(esp, num_arguments * kPointerSize));
2032 } else {
2033 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
2034 }
2035}
2036
2037
Steve Blocka7e24c12009-10-30 11:49:00 +00002038CodePatcher::CodePatcher(byte* address, int size)
2039 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
2040 // Create a new macro assembler pointing to the address of the code to patch.
2041 // The size is adjusted with kGap on order for the assembler to generate size
2042 // bytes of instructions without failing with buffer size constraints.
2043 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2044}
2045
2046
2047CodePatcher::~CodePatcher() {
2048 // Indicate that code has changed.
2049 CPU::FlushICache(address_, size_);
2050
2051 // Check that the code was patched as expected.
2052 ASSERT(masm_.pc_ == address_ + size_);
2053 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2054}
2055
2056
2057} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002058
2059#endif // V8_TARGET_ARCH_IA32