blob: 13394cbc9b4dfa920c7408c678019fa41f05fe84 [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"
Ben Murdoch8b112d22011-06-08 16:22:53 +010033#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "debug.h"
35#include "runtime.h"
36#include "serialize.h"
37
38namespace v8 {
39namespace internal {
40
41// -------------------------------------------------------------------------
42// MacroAssembler implementation.
43
Ben Murdoch8b112d22011-06-08 16:22:53 +010044MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
45 : Assembler(arg_isolate, buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000046 generating_stub_(false),
Ben Murdoch8b112d22011-06-08 16:22:53 +010047 allow_stub_calls_(true) {
48 if (isolate() != NULL) {
49 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
50 isolate());
51 }
Steve Blocka7e24c12009-10-30 11:49:00 +000052}
53
54
Steve Block6ded16b2010-05-10 14:33:55 +010055void MacroAssembler::RecordWriteHelper(Register object,
56 Register addr,
57 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +010058 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +010059 // Check that the object is not in new space.
60 Label not_in_new_space;
61 InNewSpace(object, scratch, not_equal, &not_in_new_space);
62 Abort("new-space object passed to RecordWriteHelper");
63 bind(&not_in_new_space);
64 }
65
Steve Blocka7e24c12009-10-30 11:49:00 +000066 // Compute the page start address from the heap object pointer, and reuse
67 // the 'object' register for it.
Steve Block6ded16b2010-05-10 14:33:55 +010068 and_(object, ~Page::kPageAlignmentMask);
Steve Blocka7e24c12009-10-30 11:49:00 +000069
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010070 // Compute number of region covering addr. See Page::GetRegionNumberForAddress
71 // method for more details.
72 and_(addr, Page::kPageAlignmentMask);
73 shr(addr, Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +000074
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010075 // Set dirty mark for region.
76 bts(Operand(object, Page::kDirtyFlagOffset), addr);
Steve Blocka7e24c12009-10-30 11:49:00 +000077}
78
79
Kristian Monsen50ef84f2010-07-29 15:18:00 +010080void MacroAssembler::RecordWrite(Register object,
81 int offset,
82 Register value,
83 Register scratch) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010084 // First, check if a write barrier is even needed. The tests below
85 // catch stores of Smis and stores into young gen.
Ben Murdochb0fe1622011-05-05 13:52:32 +010086 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +000087
88 // Skip barrier if writing a smi.
89 ASSERT_EQ(0, kSmiTag);
90 test(value, Immediate(kSmiTagMask));
91 j(zero, &done);
92
Steve Block6ded16b2010-05-10 14:33:55 +010093 InNewSpace(object, value, equal, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +000094
Steve Block6ded16b2010-05-10 14:33:55 +010095 // The offset is relative to a tagged or untagged HeapObject pointer,
96 // so either offset or offset + kHeapObjectTag must be a
97 // multiple of kPointerSize.
98 ASSERT(IsAligned(offset, kPointerSize) ||
99 IsAligned(offset + kHeapObjectTag, kPointerSize));
100
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100101 Register dst = scratch;
102 if (offset != 0) {
103 lea(dst, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100105 // Array access: calculate the destination address in the same manner as
106 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
107 // into an array of words.
108 ASSERT_EQ(1, kSmiTagSize);
109 ASSERT_EQ(0, kSmiTag);
110 lea(dst, Operand(object, dst, times_half_pointer_size,
111 FixedArray::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100113 RecordWriteHelper(object, dst, value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
115 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000116
117 // Clobber all input registers when running with the debug-code flag
118 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100119 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100120 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
121 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
122 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000123 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000124}
125
126
Steve Block8defd9f2010-07-08 12:39:36 +0100127void MacroAssembler::RecordWrite(Register object,
128 Register address,
129 Register value) {
Steve Block8defd9f2010-07-08 12:39:36 +0100130 // First, check if a write barrier is even needed. The tests below
131 // catch stores of Smis and stores into young gen.
132 Label done;
133
134 // Skip barrier if writing a smi.
135 ASSERT_EQ(0, kSmiTag);
136 test(value, Immediate(kSmiTagMask));
137 j(zero, &done);
138
139 InNewSpace(object, value, equal, &done);
140
141 RecordWriteHelper(object, address, value);
142
143 bind(&done);
144
145 // Clobber all input registers when running with the debug-code flag
146 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100147 if (emit_debug_code()) {
Steve Block8defd9f2010-07-08 12:39:36 +0100148 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
149 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
150 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
151 }
152}
153
154
Steve Blocka7e24c12009-10-30 11:49:00 +0000155#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000156void MacroAssembler::DebugBreak() {
157 Set(eax, Immediate(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100158 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +0000159 CEntryStub ces(1);
160 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
161}
Steve Blocka7e24c12009-10-30 11:49:00 +0000162#endif
163
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100164
Steve Blocka7e24c12009-10-30 11:49:00 +0000165void MacroAssembler::Set(Register dst, const Immediate& x) {
166 if (x.is_zero()) {
Steve Block053d10c2011-06-13 19:13:29 +0100167 xor_(dst, Operand(dst)); // Shorter than mov.
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 } else {
169 mov(dst, x);
170 }
171}
172
173
174void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
175 mov(dst, x);
176}
177
178
Steve Block053d10c2011-06-13 19:13:29 +0100179bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
180 static const int kMaxImmediateBits = 17;
181 if (x.rmode_ != RelocInfo::NONE) return false;
182 return !is_intn(x.x_, kMaxImmediateBits);
183}
184
185
186void MacroAssembler::SafeSet(Register dst, const Immediate& x) {
187 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
188 Set(dst, Immediate(x.x_ ^ jit_cookie()));
189 xor_(dst, jit_cookie());
190 } else {
191 Set(dst, x);
192 }
193}
194
195
196void MacroAssembler::SafePush(const Immediate& x) {
197 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
198 push(Immediate(x.x_ ^ jit_cookie()));
199 xor_(Operand(esp, 0), Immediate(jit_cookie()));
200 } else {
201 push(x);
202 }
203}
204
205
Steve Blocka7e24c12009-10-30 11:49:00 +0000206void MacroAssembler::CmpObjectType(Register heap_object,
207 InstanceType type,
208 Register map) {
209 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
210 CmpInstanceType(map, type);
211}
212
213
214void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
215 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
216 static_cast<int8_t>(type));
217}
218
219
Andrei Popescu31002712010-02-23 13:46:05 +0000220void MacroAssembler::CheckMap(Register obj,
221 Handle<Map> map,
222 Label* fail,
223 bool is_heap_object) {
224 if (!is_heap_object) {
225 test(obj, Immediate(kSmiTagMask));
226 j(zero, fail);
227 }
228 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
229 j(not_equal, fail);
230}
231
232
Leon Clarkee46be812010-01-19 14:06:41 +0000233Condition MacroAssembler::IsObjectStringType(Register heap_object,
234 Register map,
235 Register instance_type) {
236 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
237 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
238 ASSERT(kNotStringTag != 0);
239 test(instance_type, Immediate(kIsNotStringMask));
240 return zero;
241}
242
243
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100244void MacroAssembler::IsObjectJSObjectType(Register heap_object,
245 Register map,
246 Register scratch,
247 Label* fail) {
248 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
249 IsInstanceJSObjectType(map, scratch, fail);
250}
251
252
253void MacroAssembler::IsInstanceJSObjectType(Register map,
254 Register scratch,
255 Label* fail) {
256 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
257 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
258 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
259 j(above, fail);
260}
261
262
Steve Blocka7e24c12009-10-30 11:49:00 +0000263void MacroAssembler::FCmp() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100264 if (CpuFeatures::IsSupported(CMOV)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000265 fucomip();
266 ffree(0);
267 fincstp();
268 } else {
269 fucompp();
270 push(eax);
271 fnstsw_ax();
272 sahf();
273 pop(eax);
274 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000275}
276
277
Steve Block6ded16b2010-05-10 14:33:55 +0100278void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000279 Label ok;
280 test(object, Immediate(kSmiTagMask));
281 j(zero, &ok);
282 cmp(FieldOperand(object, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100283 isolate()->factory()->heap_number_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100284 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +0000285 bind(&ok);
286}
287
288
Steve Block6ded16b2010-05-10 14:33:55 +0100289void MacroAssembler::AbortIfNotSmi(Register object) {
290 test(object, Immediate(kSmiTagMask));
Iain Merrick75681382010-08-19 15:07:18 +0100291 Assert(equal, "Operand is not a smi");
292}
293
294
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100295void MacroAssembler::AbortIfNotString(Register object) {
296 test(object, Immediate(kSmiTagMask));
297 Assert(not_equal, "Operand is not a string");
298 push(object);
299 mov(object, FieldOperand(object, HeapObject::kMapOffset));
300 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
301 pop(object);
302 Assert(below, "Operand is not a string");
303}
304
305
Iain Merrick75681382010-08-19 15:07:18 +0100306void MacroAssembler::AbortIfSmi(Register object) {
307 test(object, Immediate(kSmiTagMask));
308 Assert(not_equal, "Operand is a smi");
Steve Block6ded16b2010-05-10 14:33:55 +0100309}
310
311
Steve Blocka7e24c12009-10-30 11:49:00 +0000312void MacroAssembler::EnterFrame(StackFrame::Type type) {
313 push(ebp);
314 mov(ebp, Operand(esp));
315 push(esi);
316 push(Immediate(Smi::FromInt(type)));
317 push(Immediate(CodeObject()));
Steve Block44f0eee2011-05-26 01:26:41 +0100318 if (emit_debug_code()) {
319 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 Check(not_equal, "code object not properly patched");
321 }
322}
323
324
325void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100326 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
328 Immediate(Smi::FromInt(type)));
329 Check(equal, "stack frame types must match");
330 }
331 leave();
332}
333
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100334
335void MacroAssembler::EnterExitFramePrologue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 // Setup the frame structure on the stack.
337 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
338 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
339 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
340 push(ebp);
341 mov(ebp, Operand(esp));
342
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100343 // Reserve room for entry stack pointer and push the code object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000345 push(Immediate(0)); // Saved entry sp, patched before call.
346 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000347
348 // Save the frame pointer and the context in top.
Steve Block44f0eee2011-05-26 01:26:41 +0100349 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
350 isolate());
351 ExternalReference context_address(Isolate::k_context_address,
352 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
354 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000355}
Steve Blocka7e24c12009-10-30 11:49:00 +0000356
Steve Blocka7e24c12009-10-30 11:49:00 +0000357
Ben Murdochb0fe1622011-05-05 13:52:32 +0100358void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
359 // Optionally save all XMM registers.
360 if (save_doubles) {
361 CpuFeatures::Scope scope(SSE2);
362 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
363 sub(Operand(esp), Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100364 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100365 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
366 XMMRegister reg = XMMRegister::from_code(i);
367 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
368 }
369 } else {
370 sub(Operand(esp), Immediate(argc * kPointerSize));
371 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000372
373 // Get the required frame alignment for the OS.
Steve Block44f0eee2011-05-26 01:26:41 +0100374 const int kFrameAlignment = OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 if (kFrameAlignment > 0) {
376 ASSERT(IsPowerOf2(kFrameAlignment));
377 and_(esp, -kFrameAlignment);
378 }
379
380 // Patch the saved entry sp.
381 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
382}
383
384
Ben Murdochb0fe1622011-05-05 13:52:32 +0100385void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100386 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000387
388 // Setup argc and argv in callee-saved registers.
389 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
390 mov(edi, Operand(eax));
391 lea(esi, Operand(ebp, eax, times_4, offset));
392
Steve Block44f0eee2011-05-26 01:26:41 +0100393 // Reserve space for argc, argv and isolate.
394 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000395}
396
397
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800398void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100399 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100400 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000401}
402
403
Ben Murdochb0fe1622011-05-05 13:52:32 +0100404void MacroAssembler::LeaveExitFrame(bool save_doubles) {
405 // Optionally restore all XMM registers.
406 if (save_doubles) {
407 CpuFeatures::Scope scope(SSE2);
Steve Block1e0659c2011-05-24 12:43:12 +0100408 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100409 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
410 XMMRegister reg = XMMRegister::from_code(i);
411 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
412 }
413 }
414
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 // Get the return address from the stack and restore the frame pointer.
416 mov(ecx, Operand(ebp, 1 * kPointerSize));
417 mov(ebp, Operand(ebp, 0 * kPointerSize));
418
419 // Pop the arguments and the receiver from the caller stack.
420 lea(esp, Operand(esi, 1 * kPointerSize));
421
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800422 // Push the return address to get ready to return.
423 push(ecx);
424
425 LeaveExitFrameEpilogue();
426}
427
428void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 // Restore current context from top and clear it in debug mode.
Steve Block44f0eee2011-05-26 01:26:41 +0100430 ExternalReference context_address(Isolate::k_context_address, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 mov(esi, Operand::StaticVariable(context_address));
432#ifdef DEBUG
433 mov(Operand::StaticVariable(context_address), Immediate(0));
434#endif
435
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 // Clear the top frame.
Steve Block44f0eee2011-05-26 01:26:41 +0100437 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
438 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
440}
441
442
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800443void MacroAssembler::LeaveApiExitFrame() {
444 mov(esp, Operand(ebp));
445 pop(ebp);
446
447 LeaveExitFrameEpilogue();
448}
449
450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451void MacroAssembler::PushTryHandler(CodeLocation try_location,
452 HandlerType type) {
453 // Adjust this code if not the case.
454 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
455 // The pc (return address) is already on TOS.
456 if (try_location == IN_JAVASCRIPT) {
457 if (type == TRY_CATCH_HANDLER) {
458 push(Immediate(StackHandler::TRY_CATCH));
459 } else {
460 push(Immediate(StackHandler::TRY_FINALLY));
461 }
462 push(ebp);
463 } else {
464 ASSERT(try_location == IN_JS_ENTRY);
465 // The frame pointer does not point to a JS frame so we save NULL
466 // for ebp. We expect the code throwing an exception to check ebp
467 // before dereferencing it to restore the context.
468 push(Immediate(StackHandler::ENTRY));
469 push(Immediate(0)); // NULL frame pointer.
470 }
471 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100472 push(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
473 isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000474 // Link this handler as the new current one.
Steve Block44f0eee2011-05-26 01:26:41 +0100475 mov(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
476 isolate())),
477 esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478}
479
480
Leon Clarkee46be812010-01-19 14:06:41 +0000481void MacroAssembler::PopTryHandler() {
482 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
Steve Block44f0eee2011-05-26 01:26:41 +0100483 pop(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
484 isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +0000485 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
486}
487
488
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100489void MacroAssembler::Throw(Register value) {
490 // Adjust this code if not the case.
491 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
492
493 // eax must hold the exception.
494 if (!value.is(eax)) {
495 mov(eax, value);
496 }
497
498 // Drop the sp to the top of the handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100499 ExternalReference handler_address(Isolate::k_handler_address,
500 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100501 mov(esp, Operand::StaticVariable(handler_address));
502
503 // Restore next handler and frame pointer, discard handler state.
504 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
505 pop(Operand::StaticVariable(handler_address));
506 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
507 pop(ebp);
508 pop(edx); // Remove state.
509
510 // Before returning we restore the context from the frame pointer if
511 // not NULL. The frame pointer is NULL in the exception handler of
512 // a JS entry frame.
513 Set(esi, Immediate(0)); // Tentatively set context pointer to NULL.
514 NearLabel skip;
515 cmp(ebp, 0);
516 j(equal, &skip, not_taken);
517 mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
518 bind(&skip);
519
520 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
521 ret(0);
522}
523
524
525void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
526 Register value) {
527 // Adjust this code if not the case.
528 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
529
530 // eax must hold the exception.
531 if (!value.is(eax)) {
532 mov(eax, value);
533 }
534
535 // Drop sp to the top stack handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100536 ExternalReference handler_address(Isolate::k_handler_address,
537 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100538 mov(esp, Operand::StaticVariable(handler_address));
539
540 // Unwind the handlers until the ENTRY handler is found.
541 NearLabel loop, done;
542 bind(&loop);
543 // Load the type of the current stack handler.
544 const int kStateOffset = StackHandlerConstants::kStateOffset;
545 cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
546 j(equal, &done);
547 // Fetch the next handler in the list.
548 const int kNextOffset = StackHandlerConstants::kNextOffset;
549 mov(esp, Operand(esp, kNextOffset));
550 jmp(&loop);
551 bind(&done);
552
553 // Set the top handler address to next handler past the current ENTRY handler.
554 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
555 pop(Operand::StaticVariable(handler_address));
556
557 if (type == OUT_OF_MEMORY) {
558 // Set external caught exception to false.
Steve Block44f0eee2011-05-26 01:26:41 +0100559 ExternalReference external_caught(
560 Isolate::k_external_caught_exception_address,
561 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100562 mov(eax, false);
563 mov(Operand::StaticVariable(external_caught), eax);
564
565 // Set pending exception and eax to out of memory exception.
Steve Block44f0eee2011-05-26 01:26:41 +0100566 ExternalReference pending_exception(Isolate::k_pending_exception_address,
567 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100568 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
569 mov(Operand::StaticVariable(pending_exception), eax);
570 }
571
572 // Clear the context pointer.
573 Set(esi, Immediate(0));
574
575 // Restore fp from handler and discard handler state.
576 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
577 pop(ebp);
578 pop(edx); // State.
579
580 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
581 ret(0);
582}
583
584
Steve Blocka7e24c12009-10-30 11:49:00 +0000585void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
586 Register scratch,
587 Label* miss) {
588 Label same_contexts;
589
590 ASSERT(!holder_reg.is(scratch));
591
592 // Load current lexical context from the stack frame.
593 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
594
595 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +0100596 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000597 cmp(Operand(scratch), Immediate(0));
598 Check(not_equal, "we should not have an empty lexical context");
599 }
600 // Load the global context of the current context.
601 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
602 mov(scratch, FieldOperand(scratch, offset));
603 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
604
605 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100606 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000607 push(scratch);
608 // Read the first word and compare to global_context_map.
609 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100610 cmp(scratch, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000611 Check(equal, "JSGlobalObject::global_context should be a global context.");
612 pop(scratch);
613 }
614
615 // Check if both contexts are the same.
616 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
617 j(equal, &same_contexts, taken);
618
619 // Compare security tokens, save holder_reg on the stack so we can use it
620 // as a temporary register.
621 //
622 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
623 push(holder_reg);
624 // Check that the security token in the calling global object is
625 // compatible with the security token in the receiving global
626 // object.
627 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
628
629 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100630 if (emit_debug_code()) {
631 cmp(holder_reg, isolate()->factory()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000632 Check(not_equal, "JSGlobalProxy::context() should not be null.");
633
634 push(holder_reg);
635 // Read the first word and compare to global_context_map(),
636 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100637 cmp(holder_reg, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000638 Check(equal, "JSGlobalObject::global_context should be a global context.");
639 pop(holder_reg);
640 }
641
642 int token_offset = Context::kHeaderSize +
643 Context::SECURITY_TOKEN_INDEX * kPointerSize;
644 mov(scratch, FieldOperand(scratch, token_offset));
645 cmp(scratch, FieldOperand(holder_reg, token_offset));
646 pop(holder_reg);
647 j(not_equal, miss, not_taken);
648
649 bind(&same_contexts);
650}
651
652
653void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 Register scratch,
655 AllocationFlags flags) {
656 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100657 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000658
659 // Just return if allocation top is already known.
660 if ((flags & RESULT_CONTAINS_TOP) != 0) {
661 // No use of scratch if allocation top is provided.
662 ASSERT(scratch.is(no_reg));
663#ifdef DEBUG
664 // Assert that result actually contains top on entry.
665 cmp(result, Operand::StaticVariable(new_space_allocation_top));
666 Check(equal, "Unexpected allocation top");
667#endif
668 return;
669 }
670
671 // Move address of new object to result. Use scratch register if available.
672 if (scratch.is(no_reg)) {
673 mov(result, Operand::StaticVariable(new_space_allocation_top));
674 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000675 mov(Operand(scratch), Immediate(new_space_allocation_top));
676 mov(result, Operand(scratch, 0));
677 }
678}
679
680
681void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
682 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +0100683 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000684 test(result_end, Immediate(kObjectAlignmentMask));
685 Check(zero, "Unaligned allocation in new space");
686 }
687
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100689 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000690
691 // Update new top. Use scratch if available.
692 if (scratch.is(no_reg)) {
693 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
694 } else {
695 mov(Operand(scratch, 0), result_end);
696 }
697}
698
699
700void MacroAssembler::AllocateInNewSpace(int object_size,
701 Register result,
702 Register result_end,
703 Register scratch,
704 Label* gc_required,
705 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700706 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100707 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700708 // Trash the registers to simulate an allocation failure.
709 mov(result, Immediate(0x7091));
710 if (result_end.is_valid()) {
711 mov(result_end, Immediate(0x7191));
712 }
713 if (scratch.is_valid()) {
714 mov(scratch, Immediate(0x7291));
715 }
716 }
717 jmp(gc_required);
718 return;
719 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 ASSERT(!result.is(result_end));
721
722 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800723 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000724
Ben Murdochbb769b22010-08-11 14:56:33 +0100725 Register top_reg = result_end.is_valid() ? result_end : result;
726
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 // Calculate new top and bail out if new space is exhausted.
728 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100729 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochbb769b22010-08-11 14:56:33 +0100730
Steve Block1e0659c2011-05-24 12:43:12 +0100731 if (!top_reg.is(result)) {
732 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +0100733 }
Steve Block1e0659c2011-05-24 12:43:12 +0100734 add(Operand(top_reg), Immediate(object_size));
735 j(carry, gc_required, not_taken);
Ben Murdochbb769b22010-08-11 14:56:33 +0100736 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +0000737 j(above, gc_required, not_taken);
738
Leon Clarkee46be812010-01-19 14:06:41 +0000739 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +0100740 UpdateAllocationTopHelper(top_reg, scratch);
741
742 // Tag result if requested.
743 if (top_reg.is(result)) {
744 if ((flags & TAG_OBJECT) != 0) {
745 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
746 } else {
747 sub(Operand(result), Immediate(object_size));
748 }
749 } else if ((flags & TAG_OBJECT) != 0) {
750 add(Operand(result), Immediate(kHeapObjectTag));
751 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000752}
753
754
755void MacroAssembler::AllocateInNewSpace(int header_size,
756 ScaleFactor element_size,
757 Register element_count,
758 Register result,
759 Register result_end,
760 Register scratch,
761 Label* gc_required,
762 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700763 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100764 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700765 // Trash the registers to simulate an allocation failure.
766 mov(result, Immediate(0x7091));
767 mov(result_end, Immediate(0x7191));
768 if (scratch.is_valid()) {
769 mov(scratch, Immediate(0x7291));
770 }
771 // Register element_count is not modified by the function.
772 }
773 jmp(gc_required);
774 return;
775 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 ASSERT(!result.is(result_end));
777
778 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800779 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000780
781 // Calculate new top and bail out if new space is exhausted.
782 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100783 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100784
785 // We assume that element_count*element_size + header_size does not
786 // overflow.
787 lea(result_end, Operand(element_count, element_size, header_size));
788 add(result_end, Operand(result));
789 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000790 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
791 j(above, gc_required);
792
Steve Blocka7e24c12009-10-30 11:49:00 +0000793 // Tag result if requested.
794 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000795 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 }
Leon Clarkee46be812010-01-19 14:06:41 +0000797
798 // Update allocation top.
799 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000800}
801
802
803void MacroAssembler::AllocateInNewSpace(Register object_size,
804 Register result,
805 Register result_end,
806 Register scratch,
807 Label* gc_required,
808 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700809 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100810 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700811 // Trash the registers to simulate an allocation failure.
812 mov(result, Immediate(0x7091));
813 mov(result_end, Immediate(0x7191));
814 if (scratch.is_valid()) {
815 mov(scratch, Immediate(0x7291));
816 }
817 // object_size is left unchanged by this function.
818 }
819 jmp(gc_required);
820 return;
821 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 ASSERT(!result.is(result_end));
823
824 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800825 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000826
827 // Calculate new top and bail out if new space is exhausted.
828 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100829 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000830 if (!object_size.is(result_end)) {
831 mov(result_end, object_size);
832 }
833 add(result_end, Operand(result));
Steve Block1e0659c2011-05-24 12:43:12 +0100834 j(carry, gc_required, not_taken);
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
836 j(above, gc_required, not_taken);
837
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 // Tag result if requested.
839 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000840 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 }
Leon Clarkee46be812010-01-19 14:06:41 +0000842
843 // Update allocation top.
844 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000845}
846
847
848void MacroAssembler::UndoAllocationInNewSpace(Register object) {
849 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100850 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000851
852 // Make sure the object has no tag before resetting top.
853 and_(Operand(object), Immediate(~kHeapObjectTagMask));
854#ifdef DEBUG
855 cmp(object, Operand::StaticVariable(new_space_allocation_top));
856 Check(below, "Undo allocation of non allocated memory");
857#endif
858 mov(Operand::StaticVariable(new_space_allocation_top), object);
859}
860
861
Steve Block3ce2e202009-11-05 08:53:23 +0000862void MacroAssembler::AllocateHeapNumber(Register result,
863 Register scratch1,
864 Register scratch2,
865 Label* gc_required) {
866 // Allocate heap number in new space.
867 AllocateInNewSpace(HeapNumber::kSize,
868 result,
869 scratch1,
870 scratch2,
871 gc_required,
872 TAG_OBJECT);
873
874 // Set the map.
875 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100876 Immediate(isolate()->factory()->heap_number_map()));
Steve Block3ce2e202009-11-05 08:53:23 +0000877}
878
879
Steve Blockd0582a62009-12-15 09:54:21 +0000880void MacroAssembler::AllocateTwoByteString(Register result,
881 Register length,
882 Register scratch1,
883 Register scratch2,
884 Register scratch3,
885 Label* gc_required) {
886 // Calculate the number of bytes needed for the characters in the string while
887 // observing object alignment.
888 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000889 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000890 // scratch1 = length * 2 + kObjectAlignmentMask.
891 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000892 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
893
894 // Allocate two byte string in new space.
895 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
896 times_1,
897 scratch1,
898 result,
899 scratch2,
900 scratch3,
901 gc_required,
902 TAG_OBJECT);
903
904 // Set the map, length and hash field.
905 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100906 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100907 mov(scratch1, length);
908 SmiTag(scratch1);
909 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000910 mov(FieldOperand(result, String::kHashFieldOffset),
911 Immediate(String::kEmptyHashField));
912}
913
914
915void MacroAssembler::AllocateAsciiString(Register result,
916 Register length,
917 Register scratch1,
918 Register scratch2,
919 Register scratch3,
920 Label* gc_required) {
921 // Calculate the number of bytes needed for the characters in the string while
922 // observing object alignment.
923 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
924 mov(scratch1, length);
925 ASSERT(kCharSize == 1);
926 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
927 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
928
929 // Allocate ascii string in new space.
930 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
931 times_1,
932 scratch1,
933 result,
934 scratch2,
935 scratch3,
936 gc_required,
937 TAG_OBJECT);
938
939 // Set the map, length and hash field.
940 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100941 Immediate(isolate()->factory()->ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100942 mov(scratch1, length);
943 SmiTag(scratch1);
944 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000945 mov(FieldOperand(result, String::kHashFieldOffset),
946 Immediate(String::kEmptyHashField));
947}
948
949
Iain Merrick9ac36c92010-09-13 15:29:50 +0100950void MacroAssembler::AllocateAsciiString(Register result,
951 int length,
952 Register scratch1,
953 Register scratch2,
954 Label* gc_required) {
955 ASSERT(length > 0);
956
957 // Allocate ascii string in new space.
958 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
959 result,
960 scratch1,
961 scratch2,
962 gc_required,
963 TAG_OBJECT);
964
965 // Set the map, length and hash field.
966 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100967 Immediate(isolate()->factory()->ascii_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100968 mov(FieldOperand(result, String::kLengthOffset),
969 Immediate(Smi::FromInt(length)));
970 mov(FieldOperand(result, String::kHashFieldOffset),
971 Immediate(String::kEmptyHashField));
972}
973
974
Steve Blockd0582a62009-12-15 09:54:21 +0000975void MacroAssembler::AllocateConsString(Register result,
976 Register scratch1,
977 Register scratch2,
978 Label* gc_required) {
979 // Allocate heap number in new space.
980 AllocateInNewSpace(ConsString::kSize,
981 result,
982 scratch1,
983 scratch2,
984 gc_required,
985 TAG_OBJECT);
986
987 // Set the map. The other fields are left uninitialized.
988 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100989 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +0000990}
991
992
993void MacroAssembler::AllocateAsciiConsString(Register result,
994 Register scratch1,
995 Register scratch2,
996 Label* gc_required) {
997 // Allocate heap number in new space.
998 AllocateInNewSpace(ConsString::kSize,
999 result,
1000 scratch1,
1001 scratch2,
1002 gc_required,
1003 TAG_OBJECT);
1004
1005 // Set the map. The other fields are left uninitialized.
1006 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001007 Immediate(isolate()->factory()->cons_ascii_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001008}
1009
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001010
Ben Murdochb8e0da22011-05-16 14:20:40 +01001011// Copy memory, byte-by-byte, from source to destination. Not optimized for
1012// long or aligned copies. The contents of scratch and length are destroyed.
1013// Source and destination are incremented by length.
1014// Many variants of movsb, loop unrolling, word moves, and indexed operands
1015// have been tried here already, and this is fastest.
1016// A simpler loop is faster on small copies, but 30% slower on large ones.
1017// The cld() instruction must have been emitted, to set the direction flag(),
1018// before calling this function.
1019void MacroAssembler::CopyBytes(Register source,
1020 Register destination,
1021 Register length,
1022 Register scratch) {
1023 Label loop, done, short_string, short_loop;
1024 // Experimentation shows that the short string loop is faster if length < 10.
1025 cmp(Operand(length), Immediate(10));
1026 j(less_equal, &short_string);
1027
1028 ASSERT(source.is(esi));
1029 ASSERT(destination.is(edi));
1030 ASSERT(length.is(ecx));
1031
1032 // Because source is 4-byte aligned in our uses of this function,
1033 // we keep source aligned for the rep_movs call by copying the odd bytes
1034 // at the end of the ranges.
1035 mov(scratch, Operand(source, length, times_1, -4));
1036 mov(Operand(destination, length, times_1, -4), scratch);
1037 mov(scratch, ecx);
1038 shr(ecx, 2);
1039 rep_movs();
1040 and_(Operand(scratch), Immediate(0x3));
1041 add(destination, Operand(scratch));
1042 jmp(&done);
1043
1044 bind(&short_string);
1045 test(length, Operand(length));
1046 j(zero, &done);
1047
1048 bind(&short_loop);
1049 mov_b(scratch, Operand(source, 0));
1050 mov_b(Operand(destination, 0), scratch);
1051 inc(source);
1052 inc(destination);
1053 dec(length);
1054 j(not_zero, &short_loop);
1055
1056 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001057}
1058
Steve Blockd0582a62009-12-15 09:54:21 +00001059
Steve Blocka7e24c12009-10-30 11:49:00 +00001060void MacroAssembler::NegativeZeroTest(Register result,
1061 Register op,
1062 Label* then_label) {
1063 Label ok;
1064 test(result, Operand(result));
1065 j(not_zero, &ok, taken);
1066 test(op, Operand(op));
1067 j(sign, then_label, not_taken);
1068 bind(&ok);
1069}
1070
1071
1072void MacroAssembler::NegativeZeroTest(Register result,
1073 Register op1,
1074 Register op2,
1075 Register scratch,
1076 Label* then_label) {
1077 Label ok;
1078 test(result, Operand(result));
1079 j(not_zero, &ok, taken);
1080 mov(scratch, Operand(op1));
1081 or_(scratch, Operand(op2));
1082 j(sign, then_label, not_taken);
1083 bind(&ok);
1084}
1085
1086
1087void MacroAssembler::TryGetFunctionPrototype(Register function,
1088 Register result,
1089 Register scratch,
1090 Label* miss) {
1091 // Check that the receiver isn't a smi.
1092 test(function, Immediate(kSmiTagMask));
1093 j(zero, miss, not_taken);
1094
1095 // Check that the function really is a function.
1096 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1097 j(not_equal, miss, not_taken);
1098
1099 // Make sure that the function has an instance prototype.
1100 Label non_instance;
1101 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1102 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1103 j(not_zero, &non_instance, not_taken);
1104
1105 // Get the prototype or initial map from the function.
1106 mov(result,
1107 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1108
1109 // If the prototype or initial map is the hole, don't return it and
1110 // simply miss the cache instead. This will allow us to allocate a
1111 // prototype object on-demand in the runtime system.
Steve Block44f0eee2011-05-26 01:26:41 +01001112 cmp(Operand(result), Immediate(isolate()->factory()->the_hole_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 j(equal, miss, not_taken);
1114
1115 // If the function does not have an initial map, we're done.
1116 Label done;
1117 CmpObjectType(result, MAP_TYPE, scratch);
1118 j(not_equal, &done);
1119
1120 // Get the prototype from the initial map.
1121 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1122 jmp(&done);
1123
1124 // Non-instance prototype: Fetch prototype from constructor field
1125 // in initial map.
1126 bind(&non_instance);
1127 mov(result, FieldOperand(result, Map::kConstructorOffset));
1128
1129 // All done.
1130 bind(&done);
1131}
1132
1133
1134void MacroAssembler::CallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001135 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 call(stub->GetCode(), RelocInfo::CODE_TARGET);
1137}
1138
1139
John Reck59135872010-11-02 12:39:01 -07001140MaybeObject* MacroAssembler::TryCallStub(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 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001147 return result;
1148}
1149
1150
Steve Blockd0582a62009-12-15 09:54:21 +00001151void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001152 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +00001153 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1154}
1155
1156
John Reck59135872010-11-02 12:39:01 -07001157MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001158 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001159 Object* result;
1160 { MaybeObject* maybe_result = stub->TryGetCode();
1161 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001162 }
John Reck59135872010-11-02 12:39:01 -07001163 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001164 return result;
1165}
1166
1167
Steve Blocka7e24c12009-10-30 11:49:00 +00001168void MacroAssembler::StubReturn(int argc) {
1169 ASSERT(argc >= 1 && generating_stub());
1170 ret((argc - 1) * kPointerSize);
1171}
1172
1173
1174void MacroAssembler::IllegalOperation(int num_arguments) {
1175 if (num_arguments > 0) {
1176 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1177 }
Steve Block44f0eee2011-05-26 01:26:41 +01001178 mov(eax, Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001179}
1180
1181
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001182void MacroAssembler::IndexFromHash(Register hash, Register index) {
1183 // The assert checks that the constants for the maximum number of digits
1184 // for an array index cached in the hash field and the number of bits
1185 // reserved for it does not conflict.
1186 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1187 (1 << String::kArrayIndexValueBits));
1188 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1189 // the low kHashShift bits.
1190 and_(hash, String::kArrayIndexValueMask);
1191 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1192 if (String::kHashShift > kSmiTagSize) {
1193 shr(hash, String::kHashShift - kSmiTagSize);
1194 }
1195 if (!index.is(hash)) {
1196 mov(index, hash);
1197 }
1198}
1199
1200
Steve Blocka7e24c12009-10-30 11:49:00 +00001201void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1202 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1203}
1204
1205
Ben Murdochb0fe1622011-05-05 13:52:32 +01001206void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01001207 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001208 Set(eax, Immediate(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01001209 mov(ebx, Immediate(ExternalReference(function, isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001210 CEntryStub ces(1);
1211 ces.SaveDoubles();
1212 CallStub(&ces);
1213}
1214
1215
John Reck59135872010-11-02 12:39:01 -07001216MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1217 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001218 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1219}
1220
1221
Steve Block44f0eee2011-05-26 01:26:41 +01001222void MacroAssembler::CallRuntime(const Runtime::Function* f,
1223 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 // If the expected number of arguments of the runtime function is
1225 // constant, we check that the actual number of arguments match the
1226 // expectation.
1227 if (f->nargs >= 0 && f->nargs != num_arguments) {
1228 IllegalOperation(num_arguments);
1229 return;
1230 }
1231
Leon Clarke4515c472010-02-03 11:58:03 +00001232 // TODO(1236192): Most runtime routines don't need the number of
1233 // arguments passed in because it is constant. At some point we
1234 // should remove this need and make the runtime routine entry code
1235 // smarter.
1236 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001237 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001238 CEntryStub ces(1);
1239 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001240}
1241
1242
Steve Block44f0eee2011-05-26 01:26:41 +01001243MaybeObject* MacroAssembler::TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -07001244 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001245 if (f->nargs >= 0 && f->nargs != num_arguments) {
1246 IllegalOperation(num_arguments);
1247 // Since we did not call the stub, there was no allocation failure.
1248 // Return some non-failure object.
Steve Block44f0eee2011-05-26 01:26:41 +01001249 return isolate()->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +00001250 }
1251
Leon Clarke4515c472010-02-03 11:58:03 +00001252 // TODO(1236192): Most runtime routines don't need the number of
1253 // arguments passed in because it is constant. At some point we
1254 // should remove this need and make the runtime routine entry code
1255 // smarter.
1256 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001257 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001258 CEntryStub ces(1);
1259 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001260}
1261
1262
Ben Murdochbb769b22010-08-11 14:56:33 +01001263void MacroAssembler::CallExternalReference(ExternalReference ref,
1264 int num_arguments) {
1265 mov(eax, Immediate(num_arguments));
1266 mov(ebx, Immediate(ref));
1267
1268 CEntryStub stub(1);
1269 CallStub(&stub);
1270}
1271
1272
Steve Block6ded16b2010-05-10 14:33:55 +01001273void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1274 int num_arguments,
1275 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001276 // TODO(1236192): Most runtime routines don't need the number of
1277 // arguments passed in because it is constant. At some point we
1278 // should remove this need and make the runtime routine entry code
1279 // smarter.
1280 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001281 JumpToExternalReference(ext);
1282}
1283
1284
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001285MaybeObject* MacroAssembler::TryTailCallExternalReference(
1286 const ExternalReference& ext, int num_arguments, int result_size) {
1287 // TODO(1236192): Most runtime routines don't need the number of
1288 // arguments passed in because it is constant. At some point we
1289 // should remove this need and make the runtime routine entry code
1290 // smarter.
1291 Set(eax, Immediate(num_arguments));
1292 return TryJumpToExternalReference(ext);
1293}
1294
1295
Steve Block6ded16b2010-05-10 14:33:55 +01001296void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1297 int num_arguments,
1298 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01001299 TailCallExternalReference(ExternalReference(fid, isolate()),
1300 num_arguments,
1301 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001302}
1303
1304
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001305MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1306 int num_arguments,
1307 int result_size) {
1308 return TryTailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001309 ExternalReference(fid, isolate()), num_arguments, result_size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001310}
1311
1312
Ben Murdochb0fe1622011-05-05 13:52:32 +01001313// If true, a Handle<T> returned by value from a function with cdecl calling
1314// convention will be returned directly as a value of location_ field in a
1315// register eax.
1316// If false, it is returned as a pointer to a preallocated by caller memory
1317// region. Pointer to this region should be passed to a function as an
1318// implicit first argument.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001319#if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001320static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001321#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001322static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001323#endif
1324
1325
1326Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001327 return Operand(
1328 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001329}
1330
1331
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001332void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001333 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001334 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001335 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001336 // space for and pass an out parameter.
1337 } else {
1338 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001339 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001340
John Reck59135872010-11-02 12:39:01 -07001341 // The argument slots are filled as follows:
1342 //
1343 // n + 1: output cell
1344 // n: arg n
1345 // ...
1346 // 1: arg1
1347 // 0: pointer to the output cell
1348 //
1349 // Note that this is one more "argument" than the function expects
1350 // so the out cell will have to be popped explicitly after returning
1351 // from the function. The out cell contains Handle.
John Reck59135872010-11-02 12:39:01 -07001352
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001353 // pointer to out cell.
1354 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1355 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
Steve Block44f0eee2011-05-26 01:26:41 +01001356 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001357 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1358 }
1359 }
1360}
1361
1362
1363MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1364 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001365 ExternalReference next_address =
1366 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001367 ExternalReference limit_address =
1368 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001369 ExternalReference level_address =
1370 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001371
John Reck59135872010-11-02 12:39:01 -07001372 // Allocate HandleScope in callee-save registers.
1373 mov(ebx, Operand::StaticVariable(next_address));
1374 mov(edi, Operand::StaticVariable(limit_address));
1375 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001376
John Reck59135872010-11-02 12:39:01 -07001377 // Call the api function!
1378 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1379
Ben Murdochb0fe1622011-05-05 13:52:32 +01001380 if (!kReturnHandlesDirectly) {
John Reck59135872010-11-02 12:39:01 -07001381 // The returned value is a pointer to the handle holding the result.
1382 // Dereference this to get to the location.
1383 mov(eax, Operand(eax, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001384 }
Steve Blockd0582a62009-12-15 09:54:21 +00001385
John Reck59135872010-11-02 12:39:01 -07001386 Label empty_handle;
1387 Label prologue;
1388 Label promote_scheduled_exception;
1389 Label delete_allocated_handles;
1390 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001391
John Reck59135872010-11-02 12:39:01 -07001392 // Check if the result handle holds 0.
1393 test(eax, Operand(eax));
1394 j(zero, &empty_handle, not_taken);
1395 // It was non-zero. Dereference to get the result value.
1396 mov(eax, Operand(eax, 0));
1397 bind(&prologue);
1398 // No more valid handles (the result handle was the last one). Restore
1399 // previous handle scope.
1400 mov(Operand::StaticVariable(next_address), ebx);
1401 sub(Operand::StaticVariable(level_address), Immediate(1));
1402 Assert(above_equal, "Invalid HandleScope level");
1403 cmp(edi, Operand::StaticVariable(limit_address));
1404 j(not_equal, &delete_allocated_handles, not_taken);
1405 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001406
John Reck59135872010-11-02 12:39:01 -07001407 // Check if the function scheduled an exception.
1408 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01001409 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07001410 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01001411 Immediate(isolate()->factory()->the_hole_value()));
John Reck59135872010-11-02 12:39:01 -07001412 j(not_equal, &promote_scheduled_exception, not_taken);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001413 LeaveApiExitFrame();
1414 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001415 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001416 MaybeObject* result =
1417 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1418 if (result->IsFailure()) {
1419 return result;
1420 }
John Reck59135872010-11-02 12:39:01 -07001421 bind(&empty_handle);
1422 // It was zero; the result is undefined.
Steve Block44f0eee2011-05-26 01:26:41 +01001423 mov(eax, isolate()->factory()->undefined_value());
John Reck59135872010-11-02 12:39:01 -07001424 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001425
John Reck59135872010-11-02 12:39:01 -07001426 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01001427 ExternalReference delete_extensions =
1428 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07001429 bind(&delete_allocated_handles);
1430 mov(Operand::StaticVariable(limit_address), edi);
1431 mov(edi, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001432 mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
1433 mov(eax, Immediate(delete_extensions));
John Reck59135872010-11-02 12:39:01 -07001434 call(Operand(eax));
1435 mov(eax, edi);
1436 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001437
1438 return result;
Steve Blockd0582a62009-12-15 09:54:21 +00001439}
1440
1441
Steve Block6ded16b2010-05-10 14:33:55 +01001442void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001443 // Set the entry point and jump to the C entry runtime stub.
1444 mov(ebx, Immediate(ext));
1445 CEntryStub ces(1);
1446 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1447}
1448
1449
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001450MaybeObject* MacroAssembler::TryJumpToExternalReference(
1451 const ExternalReference& ext) {
1452 // Set the entry point and jump to the C entry runtime stub.
1453 mov(ebx, Immediate(ext));
1454 CEntryStub ces(1);
1455 return TryTailCallStub(&ces);
1456}
1457
1458
Steve Blocka7e24c12009-10-30 11:49:00 +00001459void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1460 const ParameterCount& actual,
1461 Handle<Code> code_constant,
1462 const Operand& code_operand,
Steve Block44f0eee2011-05-26 01:26:41 +01001463 NearLabel* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001464 InvokeFlag flag,
1465 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001466 bool definitely_matches = false;
1467 Label invoke;
1468 if (expected.is_immediate()) {
1469 ASSERT(actual.is_immediate());
1470 if (expected.immediate() == actual.immediate()) {
1471 definitely_matches = true;
1472 } else {
1473 mov(eax, actual.immediate());
1474 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1475 if (expected.immediate() == sentinel) {
1476 // Don't worry about adapting arguments for builtins that
1477 // don't want that done. Skip adaption code by making it look
1478 // like we have a match between expected and actual number of
1479 // arguments.
1480 definitely_matches = true;
1481 } else {
1482 mov(ebx, expected.immediate());
1483 }
1484 }
1485 } else {
1486 if (actual.is_immediate()) {
1487 // Expected is in register, actual is immediate. This is the
1488 // case when we invoke function values without going through the
1489 // IC mechanism.
1490 cmp(expected.reg(), actual.immediate());
1491 j(equal, &invoke);
1492 ASSERT(expected.reg().is(ebx));
1493 mov(eax, actual.immediate());
1494 } else if (!expected.reg().is(actual.reg())) {
1495 // Both expected and actual are in (different) registers. This
1496 // is the case when we invoke functions using call and apply.
1497 cmp(expected.reg(), Operand(actual.reg()));
1498 j(equal, &invoke);
1499 ASSERT(actual.reg().is(eax));
1500 ASSERT(expected.reg().is(ebx));
1501 }
1502 }
1503
1504 if (!definitely_matches) {
1505 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001506 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001507 if (!code_constant.is_null()) {
1508 mov(edx, Immediate(code_constant));
1509 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1510 } else if (!code_operand.is_reg(edx)) {
1511 mov(edx, code_operand);
1512 }
1513
1514 if (flag == CALL_FUNCTION) {
1515 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001516 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 jmp(done);
1518 } else {
1519 jmp(adaptor, RelocInfo::CODE_TARGET);
1520 }
1521 bind(&invoke);
1522 }
1523}
1524
1525
1526void MacroAssembler::InvokeCode(const Operand& code,
1527 const ParameterCount& expected,
1528 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001529 InvokeFlag flag,
1530 PostCallGenerator* post_call_generator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001531 NearLabel done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001532 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1533 &done, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 if (flag == CALL_FUNCTION) {
1535 call(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001536 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001537 } else {
1538 ASSERT(flag == JUMP_FUNCTION);
1539 jmp(code);
1540 }
1541 bind(&done);
1542}
1543
1544
1545void MacroAssembler::InvokeCode(Handle<Code> code,
1546 const ParameterCount& expected,
1547 const ParameterCount& actual,
1548 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001549 InvokeFlag flag,
1550 PostCallGenerator* post_call_generator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001551 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +00001552 Operand dummy(eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001553 InvokePrologue(expected, actual, code, dummy, &done,
1554 flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 if (flag == CALL_FUNCTION) {
1556 call(code, rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001557 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001558 } else {
1559 ASSERT(flag == JUMP_FUNCTION);
1560 jmp(code, rmode);
1561 }
1562 bind(&done);
1563}
1564
1565
1566void MacroAssembler::InvokeFunction(Register fun,
1567 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001568 InvokeFlag flag,
1569 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001570 ASSERT(fun.is(edi));
1571 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1572 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1573 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001574 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001575
1576 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01001577 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001578 expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001579}
1580
1581
Andrei Popescu402d9372010-02-26 13:31:12 +00001582void MacroAssembler::InvokeFunction(JSFunction* function,
1583 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001584 InvokeFlag flag,
1585 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001586 ASSERT(function->is_compiled());
1587 // Get the function and setup the context.
1588 mov(edi, Immediate(Handle<JSFunction>(function)));
1589 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001590
Andrei Popescu402d9372010-02-26 13:31:12 +00001591 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001592 if (V8::UseCrankshaft()) {
1593 // TODO(kasperl): For now, we always call indirectly through the
1594 // code field in the function to allow recompilation to take effect
1595 // without changing any of the call sites.
1596 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1597 expected, actual, flag, post_call_generator);
1598 } else {
1599 Handle<Code> code(function->code());
1600 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
1601 flag, post_call_generator);
1602 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001603}
1604
1605
Ben Murdochb0fe1622011-05-05 13:52:32 +01001606void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1607 InvokeFlag flag,
1608 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001609 // Calls are not allowed in some stubs.
1610 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1611
1612 // Rely on the assertion to check that the number of provided
1613 // arguments match the expected number of arguments. Fake a
1614 // parameter count to avoid emitting code to do the check.
1615 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01001616 GetBuiltinFunction(edi, id);
1617 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001618 expected, expected, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001619}
1620
Steve Block791712a2010-08-27 10:21:07 +01001621void MacroAssembler::GetBuiltinFunction(Register target,
1622 Builtins::JavaScript id) {
1623 // Load the JavaScript builtin function from the builtins object.
1624 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1625 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1626 mov(target, FieldOperand(target,
1627 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1628}
Steve Blocka7e24c12009-10-30 11:49:00 +00001629
1630void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001631 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00001632 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01001633 GetBuiltinFunction(edi, id);
1634 // Load the code entry point from the function into the target register.
1635 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001636}
1637
1638
Steve Blockd0582a62009-12-15 09:54:21 +00001639void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1640 if (context_chain_length > 0) {
1641 // Move up the chain of contexts to the context containing the slot.
1642 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1643 // Load the function context (which is the incoming, outer context).
1644 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1645 for (int i = 1; i < context_chain_length; i++) {
1646 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1647 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1648 }
Steve Block1e0659c2011-05-24 12:43:12 +01001649 } else {
1650 // Slot is in the current function context. Move it into the
1651 // destination register in case we store into it (the write barrier
1652 // cannot be allowed to destroy the context in esi).
1653 mov(dst, esi);
1654 }
1655
1656 // We should not have found a 'with' context by walking the context chain
1657 // (i.e., the static scope chain and runtime context chain do not agree).
1658 // A variable occurring in such a scope should have slot type LOOKUP and
1659 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01001660 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001661 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1662 Check(equal, "Yo dawg, I heard you liked function contexts "
1663 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00001664 }
1665}
1666
1667
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001668void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1669 // Load the global or builtins object from the current context.
1670 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1671 // Load the global context from the global or builtins object.
1672 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1673 // Load the function from the global context.
1674 mov(function, Operand(function, Context::SlotOffset(index)));
1675}
1676
1677
1678void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1679 Register map) {
1680 // Load the initial map. The global functions all have initial maps.
1681 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001682 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001683 Label ok, fail;
Steve Block44f0eee2011-05-26 01:26:41 +01001684 CheckMap(map, isolate()->factory()->meta_map(), &fail, false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001685 jmp(&ok);
1686 bind(&fail);
1687 Abort("Global functions must have initial map");
1688 bind(&ok);
1689 }
1690}
1691
Steve Blockd0582a62009-12-15 09:54:21 +00001692
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001693// Store the value in register src in the safepoint register stack
1694// slot for register dst.
1695void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
1696 mov(SafepointRegisterSlot(dst), src);
1697}
1698
1699
1700void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
1701 mov(SafepointRegisterSlot(dst), src);
1702}
1703
1704
1705void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
1706 mov(dst, SafepointRegisterSlot(src));
1707}
1708
1709
1710Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
1711 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
1712}
1713
1714
Ben Murdochb0fe1622011-05-05 13:52:32 +01001715int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1716 // The registers are pushed starting with the lowest encoding,
1717 // which means that lowest encodings are furthest away from
1718 // the stack pointer.
1719 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1720 return kNumSafepointRegisters - reg_code - 1;
1721}
1722
1723
Steve Blocka7e24c12009-10-30 11:49:00 +00001724void MacroAssembler::Ret() {
1725 ret(0);
1726}
1727
1728
Steve Block1e0659c2011-05-24 12:43:12 +01001729void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1730 if (is_uint16(bytes_dropped)) {
1731 ret(bytes_dropped);
1732 } else {
1733 pop(scratch);
1734 add(Operand(esp), Immediate(bytes_dropped));
1735 push(scratch);
1736 ret(0);
1737 }
1738}
1739
1740
1741
1742
Leon Clarkee46be812010-01-19 14:06:41 +00001743void MacroAssembler::Drop(int stack_elements) {
1744 if (stack_elements > 0) {
1745 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1746 }
1747}
1748
1749
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001750void MacroAssembler::Move(Register dst, Register src) {
1751 if (!dst.is(src)) {
1752 mov(dst, src);
1753 }
1754}
1755
1756
Leon Clarkee46be812010-01-19 14:06:41 +00001757void MacroAssembler::Move(Register dst, Handle<Object> value) {
1758 mov(dst, value);
1759}
1760
1761
Steve Blocka7e24c12009-10-30 11:49:00 +00001762void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1763 if (FLAG_native_code_counters && counter->Enabled()) {
1764 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1765 }
1766}
1767
1768
1769void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1770 ASSERT(value > 0);
1771 if (FLAG_native_code_counters && counter->Enabled()) {
1772 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1773 if (value == 1) {
1774 inc(operand);
1775 } else {
1776 add(operand, Immediate(value));
1777 }
1778 }
1779}
1780
1781
1782void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1783 ASSERT(value > 0);
1784 if (FLAG_native_code_counters && counter->Enabled()) {
1785 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1786 if (value == 1) {
1787 dec(operand);
1788 } else {
1789 sub(operand, Immediate(value));
1790 }
1791 }
1792}
1793
1794
Leon Clarked91b9f72010-01-27 17:25:45 +00001795void MacroAssembler::IncrementCounter(Condition cc,
1796 StatsCounter* counter,
1797 int value) {
1798 ASSERT(value > 0);
1799 if (FLAG_native_code_counters && counter->Enabled()) {
1800 Label skip;
1801 j(NegateCondition(cc), &skip);
1802 pushfd();
1803 IncrementCounter(counter, value);
1804 popfd();
1805 bind(&skip);
1806 }
1807}
1808
1809
1810void MacroAssembler::DecrementCounter(Condition cc,
1811 StatsCounter* counter,
1812 int value) {
1813 ASSERT(value > 0);
1814 if (FLAG_native_code_counters && counter->Enabled()) {
1815 Label skip;
1816 j(NegateCondition(cc), &skip);
1817 pushfd();
1818 DecrementCounter(counter, value);
1819 popfd();
1820 bind(&skip);
1821 }
1822}
1823
1824
Steve Blocka7e24c12009-10-30 11:49:00 +00001825void MacroAssembler::Assert(Condition cc, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01001826 if (emit_debug_code()) Check(cc, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00001827}
1828
1829
Iain Merrick75681382010-08-19 15:07:18 +01001830void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01001831 if (emit_debug_code()) {
1832 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01001833 Label ok;
1834 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001835 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001836 j(equal, &ok);
1837 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001838 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001839 j(equal, &ok);
1840 Abort("JSObject with fast elements map has slow elements");
1841 bind(&ok);
1842 }
1843}
1844
1845
Steve Blocka7e24c12009-10-30 11:49:00 +00001846void MacroAssembler::Check(Condition cc, const char* msg) {
1847 Label L;
1848 j(cc, &L, taken);
1849 Abort(msg);
1850 // will not return here
1851 bind(&L);
1852}
1853
1854
Steve Block6ded16b2010-05-10 14:33:55 +01001855void MacroAssembler::CheckStackAlignment() {
1856 int frame_alignment = OS::ActivationFrameAlignment();
1857 int frame_alignment_mask = frame_alignment - 1;
1858 if (frame_alignment > kPointerSize) {
1859 ASSERT(IsPowerOf2(frame_alignment));
1860 Label alignment_as_expected;
1861 test(esp, Immediate(frame_alignment_mask));
1862 j(zero, &alignment_as_expected);
1863 // Abort if stack is not aligned.
1864 int3();
1865 bind(&alignment_as_expected);
1866 }
1867}
1868
1869
Steve Blocka7e24c12009-10-30 11:49:00 +00001870void MacroAssembler::Abort(const char* msg) {
1871 // We want to pass the msg string like a smi to avoid GC
1872 // problems, however msg is not guaranteed to be aligned
1873 // properly. Instead, we pass an aligned pointer that is
1874 // a proper v8 smi, but also pass the alignment difference
1875 // from the real pointer as a smi.
1876 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1877 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1878 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1879#ifdef DEBUG
1880 if (msg != NULL) {
1881 RecordComment("Abort message: ");
1882 RecordComment(msg);
1883 }
1884#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001885 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001886 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001887
Steve Blocka7e24c12009-10-30 11:49:00 +00001888 push(eax);
1889 push(Immediate(p0));
1890 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1891 CallRuntime(Runtime::kAbort, 2);
1892 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00001893 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00001894}
1895
1896
Iain Merrick75681382010-08-19 15:07:18 +01001897void MacroAssembler::JumpIfNotNumber(Register reg,
1898 TypeInfo info,
1899 Label* on_not_number) {
Steve Block44f0eee2011-05-26 01:26:41 +01001900 if (emit_debug_code()) AbortIfSmi(reg);
Iain Merrick75681382010-08-19 15:07:18 +01001901 if (!info.IsNumber()) {
1902 cmp(FieldOperand(reg, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001903 isolate()->factory()->heap_number_map());
Iain Merrick75681382010-08-19 15:07:18 +01001904 j(not_equal, on_not_number);
1905 }
1906}
1907
1908
1909void MacroAssembler::ConvertToInt32(Register dst,
1910 Register source,
1911 Register scratch,
1912 TypeInfo info,
1913 Label* on_not_int32) {
Steve Block44f0eee2011-05-26 01:26:41 +01001914 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01001915 AbortIfSmi(source);
1916 AbortIfNotNumber(source);
1917 }
1918 if (info.IsInteger32()) {
1919 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1920 } else {
1921 Label done;
1922 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1923 ASSERT(!scratch.is(source));
1924 if (push_pop) {
1925 push(dst);
1926 scratch = dst;
1927 }
1928 if (scratch.is(no_reg)) scratch = dst;
1929 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1930 cmp(scratch, 0x80000000u);
1931 if (push_pop) {
1932 j(not_equal, &done);
1933 pop(dst);
1934 jmp(on_not_int32);
1935 } else {
1936 j(equal, on_not_int32);
1937 }
1938
1939 bind(&done);
1940 if (push_pop) {
1941 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1942 }
1943 if (!scratch.is(dst)) {
1944 mov(dst, scratch);
1945 }
1946 }
1947}
1948
1949
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001950void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1951 Register scratch,
1952 int power) {
1953 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1954 HeapNumber::kExponentBits));
1955 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1956 movd(dst, Operand(scratch));
1957 psllq(dst, HeapNumber::kMantissaBits);
1958}
1959
1960
Andrei Popescu402d9372010-02-26 13:31:12 +00001961void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1962 Register instance_type,
1963 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01001964 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001965 if (!scratch.is(instance_type)) {
1966 mov(scratch, instance_type);
1967 }
1968 and_(scratch,
1969 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1970 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1971 j(not_equal, failure);
1972}
1973
1974
Leon Clarked91b9f72010-01-27 17:25:45 +00001975void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1976 Register object2,
1977 Register scratch1,
1978 Register scratch2,
1979 Label* failure) {
1980 // Check that both objects are not smis.
1981 ASSERT_EQ(0, kSmiTag);
1982 mov(scratch1, Operand(object1));
1983 and_(scratch1, Operand(object2));
1984 test(scratch1, Immediate(kSmiTagMask));
1985 j(zero, failure);
1986
1987 // Load instance type for both strings.
1988 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1989 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1990 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1991 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1992
1993 // Check that both are flat ascii strings.
1994 const int kFlatAsciiStringMask =
1995 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1996 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1997 // Interleave bits from both instance types and compare them in one check.
1998 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1999 and_(scratch1, kFlatAsciiStringMask);
2000 and_(scratch2, kFlatAsciiStringMask);
2001 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
2002 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
2003 j(not_equal, failure);
2004}
2005
2006
Steve Block6ded16b2010-05-10 14:33:55 +01002007void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002008 int frame_alignment = OS::ActivationFrameAlignment();
2009 if (frame_alignment != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01002010 // Make stack end at alignment and make room for num_arguments words
2011 // and the original value of esp.
2012 mov(scratch, esp);
2013 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
Ben Murdoch8b112d22011-06-08 16:22:53 +01002014 ASSERT(IsPowerOf2(frame_alignment));
2015 and_(esp, -frame_alignment);
Steve Block6ded16b2010-05-10 14:33:55 +01002016 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2017 } else {
2018 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
2019 }
2020}
2021
2022
2023void MacroAssembler::CallCFunction(ExternalReference function,
2024 int num_arguments) {
2025 // Trashing eax is ok as it will be the return value.
2026 mov(Operand(eax), Immediate(function));
2027 CallCFunction(eax, num_arguments);
2028}
2029
2030
2031void MacroAssembler::CallCFunction(Register function,
2032 int num_arguments) {
2033 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01002034 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002035 CheckStackAlignment();
2036 }
2037
2038 call(Operand(function));
2039 if (OS::ActivationFrameAlignment() != 0) {
2040 mov(esp, Operand(esp, num_arguments * kPointerSize));
2041 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002042 add(Operand(esp), Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01002043 }
2044}
2045
2046
Steve Blocka7e24c12009-10-30 11:49:00 +00002047CodePatcher::CodePatcher(byte* address, int size)
Ben Murdoch8b112d22011-06-08 16:22:53 +01002048 : address_(address),
2049 size_(size),
2050 masm_(Isolate::Current(), address, size + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002051 // Create a new macro assembler pointing to the address of the code to patch.
2052 // The size is adjusted with kGap on order for the assembler to generate size
2053 // bytes of instructions without failing with buffer size constraints.
2054 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2055}
2056
2057
2058CodePatcher::~CodePatcher() {
2059 // Indicate that code has changed.
2060 CPU::FlushICache(address_, size_);
2061
2062 // Check that the code was patched as expected.
2063 ASSERT(masm_.pc_ == address_ + size_);
2064 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2065}
2066
2067
2068} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002069
2070#endif // V8_TARGET_ARCH_IA32