blob: ad567bca3d3981fd7f5292f202555558a929ea14 [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()) {
167 xor_(dst, Operand(dst)); // shorter than mov
168 } 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
179void MacroAssembler::CmpObjectType(Register heap_object,
180 InstanceType type,
181 Register map) {
182 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
183 CmpInstanceType(map, type);
184}
185
186
187void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
188 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
189 static_cast<int8_t>(type));
190}
191
192
Andrei Popescu31002712010-02-23 13:46:05 +0000193void MacroAssembler::CheckMap(Register obj,
194 Handle<Map> map,
195 Label* fail,
196 bool is_heap_object) {
197 if (!is_heap_object) {
198 test(obj, Immediate(kSmiTagMask));
199 j(zero, fail);
200 }
201 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
202 j(not_equal, fail);
203}
204
205
Leon Clarkee46be812010-01-19 14:06:41 +0000206Condition MacroAssembler::IsObjectStringType(Register heap_object,
207 Register map,
208 Register instance_type) {
209 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
210 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
211 ASSERT(kNotStringTag != 0);
212 test(instance_type, Immediate(kIsNotStringMask));
213 return zero;
214}
215
216
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100217void MacroAssembler::IsObjectJSObjectType(Register heap_object,
218 Register map,
219 Register scratch,
220 Label* fail) {
221 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
222 IsInstanceJSObjectType(map, scratch, fail);
223}
224
225
226void MacroAssembler::IsInstanceJSObjectType(Register map,
227 Register scratch,
228 Label* fail) {
229 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
230 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
231 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
232 j(above, fail);
233}
234
235
Steve Blocka7e24c12009-10-30 11:49:00 +0000236void MacroAssembler::FCmp() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100237 if (CpuFeatures::IsSupported(CMOV)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000238 fucomip();
239 ffree(0);
240 fincstp();
241 } else {
242 fucompp();
243 push(eax);
244 fnstsw_ax();
245 sahf();
246 pop(eax);
247 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000248}
249
250
Steve Block6ded16b2010-05-10 14:33:55 +0100251void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000252 Label ok;
253 test(object, Immediate(kSmiTagMask));
254 j(zero, &ok);
255 cmp(FieldOperand(object, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100256 isolate()->factory()->heap_number_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100257 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +0000258 bind(&ok);
259}
260
261
Steve Block6ded16b2010-05-10 14:33:55 +0100262void MacroAssembler::AbortIfNotSmi(Register object) {
263 test(object, Immediate(kSmiTagMask));
Iain Merrick75681382010-08-19 15:07:18 +0100264 Assert(equal, "Operand is not a smi");
265}
266
267
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100268void MacroAssembler::AbortIfNotString(Register object) {
269 test(object, Immediate(kSmiTagMask));
270 Assert(not_equal, "Operand is not a string");
271 push(object);
272 mov(object, FieldOperand(object, HeapObject::kMapOffset));
273 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
274 pop(object);
275 Assert(below, "Operand is not a string");
276}
277
278
Iain Merrick75681382010-08-19 15:07:18 +0100279void MacroAssembler::AbortIfSmi(Register object) {
280 test(object, Immediate(kSmiTagMask));
281 Assert(not_equal, "Operand is a smi");
Steve Block6ded16b2010-05-10 14:33:55 +0100282}
283
284
Steve Blocka7e24c12009-10-30 11:49:00 +0000285void MacroAssembler::EnterFrame(StackFrame::Type type) {
286 push(ebp);
287 mov(ebp, Operand(esp));
288 push(esi);
289 push(Immediate(Smi::FromInt(type)));
290 push(Immediate(CodeObject()));
Steve Block44f0eee2011-05-26 01:26:41 +0100291 if (emit_debug_code()) {
292 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 Check(not_equal, "code object not properly patched");
294 }
295}
296
297
298void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100299 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
301 Immediate(Smi::FromInt(type)));
302 Check(equal, "stack frame types must match");
303 }
304 leave();
305}
306
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100307
308void MacroAssembler::EnterExitFramePrologue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 // Setup the frame structure on the stack.
310 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
311 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
312 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
313 push(ebp);
314 mov(ebp, Operand(esp));
315
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100316 // Reserve room for entry stack pointer and push the code object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000318 push(Immediate(0)); // Saved entry sp, patched before call.
319 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000320
321 // Save the frame pointer and the context in top.
Steve Block44f0eee2011-05-26 01:26:41 +0100322 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
323 isolate());
324 ExternalReference context_address(Isolate::k_context_address,
325 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
327 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000328}
Steve Blocka7e24c12009-10-30 11:49:00 +0000329
Steve Blocka7e24c12009-10-30 11:49:00 +0000330
Ben Murdochb0fe1622011-05-05 13:52:32 +0100331void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
332 // Optionally save all XMM registers.
333 if (save_doubles) {
334 CpuFeatures::Scope scope(SSE2);
335 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
336 sub(Operand(esp), Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100337 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100338 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
339 XMMRegister reg = XMMRegister::from_code(i);
340 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
341 }
342 } else {
343 sub(Operand(esp), Immediate(argc * kPointerSize));
344 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000345
346 // Get the required frame alignment for the OS.
Steve Block44f0eee2011-05-26 01:26:41 +0100347 const int kFrameAlignment = OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 if (kFrameAlignment > 0) {
349 ASSERT(IsPowerOf2(kFrameAlignment));
350 and_(esp, -kFrameAlignment);
351 }
352
353 // Patch the saved entry sp.
354 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
355}
356
357
Ben Murdochb0fe1622011-05-05 13:52:32 +0100358void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100359 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000360
361 // Setup argc and argv in callee-saved registers.
362 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
363 mov(edi, Operand(eax));
364 lea(esi, Operand(ebp, eax, times_4, offset));
365
Steve Block44f0eee2011-05-26 01:26:41 +0100366 // Reserve space for argc, argv and isolate.
367 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000368}
369
370
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800371void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100372 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100373 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000374}
375
376
Ben Murdochb0fe1622011-05-05 13:52:32 +0100377void MacroAssembler::LeaveExitFrame(bool save_doubles) {
378 // Optionally restore all XMM registers.
379 if (save_doubles) {
380 CpuFeatures::Scope scope(SSE2);
Steve Block1e0659c2011-05-24 12:43:12 +0100381 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100382 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
383 XMMRegister reg = XMMRegister::from_code(i);
384 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
385 }
386 }
387
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 // Get the return address from the stack and restore the frame pointer.
389 mov(ecx, Operand(ebp, 1 * kPointerSize));
390 mov(ebp, Operand(ebp, 0 * kPointerSize));
391
392 // Pop the arguments and the receiver from the caller stack.
393 lea(esp, Operand(esi, 1 * kPointerSize));
394
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800395 // Push the return address to get ready to return.
396 push(ecx);
397
398 LeaveExitFrameEpilogue();
399}
400
401void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 // Restore current context from top and clear it in debug mode.
Steve Block44f0eee2011-05-26 01:26:41 +0100403 ExternalReference context_address(Isolate::k_context_address, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 mov(esi, Operand::StaticVariable(context_address));
405#ifdef DEBUG
406 mov(Operand::StaticVariable(context_address), Immediate(0));
407#endif
408
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 // Clear the top frame.
Steve Block44f0eee2011-05-26 01:26:41 +0100410 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
411 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
413}
414
415
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800416void MacroAssembler::LeaveApiExitFrame() {
417 mov(esp, Operand(ebp));
418 pop(ebp);
419
420 LeaveExitFrameEpilogue();
421}
422
423
Steve Blocka7e24c12009-10-30 11:49:00 +0000424void MacroAssembler::PushTryHandler(CodeLocation try_location,
425 HandlerType type) {
426 // Adjust this code if not the case.
427 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
428 // The pc (return address) is already on TOS.
429 if (try_location == IN_JAVASCRIPT) {
430 if (type == TRY_CATCH_HANDLER) {
431 push(Immediate(StackHandler::TRY_CATCH));
432 } else {
433 push(Immediate(StackHandler::TRY_FINALLY));
434 }
435 push(ebp);
436 } else {
437 ASSERT(try_location == IN_JS_ENTRY);
438 // The frame pointer does not point to a JS frame so we save NULL
439 // for ebp. We expect the code throwing an exception to check ebp
440 // before dereferencing it to restore the context.
441 push(Immediate(StackHandler::ENTRY));
442 push(Immediate(0)); // NULL frame pointer.
443 }
444 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100445 push(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
446 isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 // Link this handler as the new current one.
Steve Block44f0eee2011-05-26 01:26:41 +0100448 mov(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
449 isolate())),
450 esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000451}
452
453
Leon Clarkee46be812010-01-19 14:06:41 +0000454void MacroAssembler::PopTryHandler() {
455 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
Steve Block44f0eee2011-05-26 01:26:41 +0100456 pop(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
457 isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +0000458 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
459}
460
461
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100462void MacroAssembler::Throw(Register value) {
463 // Adjust this code if not the case.
464 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
465
466 // eax must hold the exception.
467 if (!value.is(eax)) {
468 mov(eax, value);
469 }
470
471 // Drop the sp to the top of the handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100472 ExternalReference handler_address(Isolate::k_handler_address,
473 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100474 mov(esp, Operand::StaticVariable(handler_address));
475
476 // Restore next handler and frame pointer, discard handler state.
477 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
478 pop(Operand::StaticVariable(handler_address));
479 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
480 pop(ebp);
481 pop(edx); // Remove state.
482
483 // Before returning we restore the context from the frame pointer if
484 // not NULL. The frame pointer is NULL in the exception handler of
485 // a JS entry frame.
486 Set(esi, Immediate(0)); // Tentatively set context pointer to NULL.
487 NearLabel skip;
488 cmp(ebp, 0);
489 j(equal, &skip, not_taken);
490 mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
491 bind(&skip);
492
493 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
494 ret(0);
495}
496
497
498void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
499 Register value) {
500 // Adjust this code if not the case.
501 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
502
503 // eax must hold the exception.
504 if (!value.is(eax)) {
505 mov(eax, value);
506 }
507
508 // Drop sp to the top stack handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100509 ExternalReference handler_address(Isolate::k_handler_address,
510 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100511 mov(esp, Operand::StaticVariable(handler_address));
512
513 // Unwind the handlers until the ENTRY handler is found.
514 NearLabel loop, done;
515 bind(&loop);
516 // Load the type of the current stack handler.
517 const int kStateOffset = StackHandlerConstants::kStateOffset;
518 cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
519 j(equal, &done);
520 // Fetch the next handler in the list.
521 const int kNextOffset = StackHandlerConstants::kNextOffset;
522 mov(esp, Operand(esp, kNextOffset));
523 jmp(&loop);
524 bind(&done);
525
526 // Set the top handler address to next handler past the current ENTRY handler.
527 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
528 pop(Operand::StaticVariable(handler_address));
529
530 if (type == OUT_OF_MEMORY) {
531 // Set external caught exception to false.
Steve Block44f0eee2011-05-26 01:26:41 +0100532 ExternalReference external_caught(
533 Isolate::k_external_caught_exception_address,
534 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100535 mov(eax, false);
536 mov(Operand::StaticVariable(external_caught), eax);
537
538 // Set pending exception and eax to out of memory exception.
Steve Block44f0eee2011-05-26 01:26:41 +0100539 ExternalReference pending_exception(Isolate::k_pending_exception_address,
540 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100541 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
542 mov(Operand::StaticVariable(pending_exception), eax);
543 }
544
545 // Clear the context pointer.
546 Set(esi, Immediate(0));
547
548 // Restore fp from handler and discard handler state.
549 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
550 pop(ebp);
551 pop(edx); // State.
552
553 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
554 ret(0);
555}
556
557
Steve Blocka7e24c12009-10-30 11:49:00 +0000558void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
559 Register scratch,
560 Label* miss) {
561 Label same_contexts;
562
563 ASSERT(!holder_reg.is(scratch));
564
565 // Load current lexical context from the stack frame.
566 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
567
568 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +0100569 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 cmp(Operand(scratch), Immediate(0));
571 Check(not_equal, "we should not have an empty lexical context");
572 }
573 // Load the global context of the current context.
574 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
575 mov(scratch, FieldOperand(scratch, offset));
576 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
577
578 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100579 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 push(scratch);
581 // Read the first word and compare to global_context_map.
582 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100583 cmp(scratch, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000584 Check(equal, "JSGlobalObject::global_context should be a global context.");
585 pop(scratch);
586 }
587
588 // Check if both contexts are the same.
589 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
590 j(equal, &same_contexts, taken);
591
592 // Compare security tokens, save holder_reg on the stack so we can use it
593 // as a temporary register.
594 //
595 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
596 push(holder_reg);
597 // Check that the security token in the calling global object is
598 // compatible with the security token in the receiving global
599 // object.
600 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
601
602 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100603 if (emit_debug_code()) {
604 cmp(holder_reg, isolate()->factory()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 Check(not_equal, "JSGlobalProxy::context() should not be null.");
606
607 push(holder_reg);
608 // Read the first word and compare to global_context_map(),
609 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100610 cmp(holder_reg, 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(holder_reg);
613 }
614
615 int token_offset = Context::kHeaderSize +
616 Context::SECURITY_TOKEN_INDEX * kPointerSize;
617 mov(scratch, FieldOperand(scratch, token_offset));
618 cmp(scratch, FieldOperand(holder_reg, token_offset));
619 pop(holder_reg);
620 j(not_equal, miss, not_taken);
621
622 bind(&same_contexts);
623}
624
625
626void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000627 Register scratch,
628 AllocationFlags flags) {
629 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100630 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000631
632 // Just return if allocation top is already known.
633 if ((flags & RESULT_CONTAINS_TOP) != 0) {
634 // No use of scratch if allocation top is provided.
635 ASSERT(scratch.is(no_reg));
636#ifdef DEBUG
637 // Assert that result actually contains top on entry.
638 cmp(result, Operand::StaticVariable(new_space_allocation_top));
639 Check(equal, "Unexpected allocation top");
640#endif
641 return;
642 }
643
644 // Move address of new object to result. Use scratch register if available.
645 if (scratch.is(no_reg)) {
646 mov(result, Operand::StaticVariable(new_space_allocation_top));
647 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 mov(Operand(scratch), Immediate(new_space_allocation_top));
649 mov(result, Operand(scratch, 0));
650 }
651}
652
653
654void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
655 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +0100656 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000657 test(result_end, Immediate(kObjectAlignmentMask));
658 Check(zero, "Unaligned allocation in new space");
659 }
660
Steve Blocka7e24c12009-10-30 11:49:00 +0000661 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100662 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000663
664 // Update new top. Use scratch if available.
665 if (scratch.is(no_reg)) {
666 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
667 } else {
668 mov(Operand(scratch, 0), result_end);
669 }
670}
671
672
673void MacroAssembler::AllocateInNewSpace(int object_size,
674 Register result,
675 Register result_end,
676 Register scratch,
677 Label* gc_required,
678 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700679 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100680 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700681 // Trash the registers to simulate an allocation failure.
682 mov(result, Immediate(0x7091));
683 if (result_end.is_valid()) {
684 mov(result_end, Immediate(0x7191));
685 }
686 if (scratch.is_valid()) {
687 mov(scratch, Immediate(0x7291));
688 }
689 }
690 jmp(gc_required);
691 return;
692 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 ASSERT(!result.is(result_end));
694
695 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800696 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000697
Ben Murdochbb769b22010-08-11 14:56:33 +0100698 Register top_reg = result_end.is_valid() ? result_end : result;
699
Steve Blocka7e24c12009-10-30 11:49:00 +0000700 // Calculate new top and bail out if new space is exhausted.
701 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100702 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochbb769b22010-08-11 14:56:33 +0100703
Steve Block1e0659c2011-05-24 12:43:12 +0100704 if (!top_reg.is(result)) {
705 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +0100706 }
Steve Block1e0659c2011-05-24 12:43:12 +0100707 add(Operand(top_reg), Immediate(object_size));
708 j(carry, gc_required, not_taken);
Ben Murdochbb769b22010-08-11 14:56:33 +0100709 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 j(above, gc_required, not_taken);
711
Leon Clarkee46be812010-01-19 14:06:41 +0000712 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +0100713 UpdateAllocationTopHelper(top_reg, scratch);
714
715 // Tag result if requested.
716 if (top_reg.is(result)) {
717 if ((flags & TAG_OBJECT) != 0) {
718 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
719 } else {
720 sub(Operand(result), Immediate(object_size));
721 }
722 } else if ((flags & TAG_OBJECT) != 0) {
723 add(Operand(result), Immediate(kHeapObjectTag));
724 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000725}
726
727
728void MacroAssembler::AllocateInNewSpace(int header_size,
729 ScaleFactor element_size,
730 Register element_count,
731 Register result,
732 Register result_end,
733 Register scratch,
734 Label* gc_required,
735 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700736 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100737 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700738 // Trash the registers to simulate an allocation failure.
739 mov(result, Immediate(0x7091));
740 mov(result_end, Immediate(0x7191));
741 if (scratch.is_valid()) {
742 mov(scratch, Immediate(0x7291));
743 }
744 // Register element_count is not modified by the function.
745 }
746 jmp(gc_required);
747 return;
748 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 ASSERT(!result.is(result_end));
750
751 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800752 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000753
754 // Calculate new top and bail out if new space is exhausted.
755 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100756 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100757
758 // We assume that element_count*element_size + header_size does not
759 // overflow.
760 lea(result_end, Operand(element_count, element_size, header_size));
761 add(result_end, Operand(result));
762 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
764 j(above, gc_required);
765
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 // Tag result if requested.
767 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000768 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000769 }
Leon Clarkee46be812010-01-19 14:06:41 +0000770
771 // Update allocation top.
772 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773}
774
775
776void MacroAssembler::AllocateInNewSpace(Register object_size,
777 Register result,
778 Register result_end,
779 Register scratch,
780 Label* gc_required,
781 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700782 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100783 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700784 // Trash the registers to simulate an allocation failure.
785 mov(result, Immediate(0x7091));
786 mov(result_end, Immediate(0x7191));
787 if (scratch.is_valid()) {
788 mov(scratch, Immediate(0x7291));
789 }
790 // object_size is left unchanged by this function.
791 }
792 jmp(gc_required);
793 return;
794 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 ASSERT(!result.is(result_end));
796
797 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800798 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000799
800 // Calculate new top and bail out if new space is exhausted.
801 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100802 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000803 if (!object_size.is(result_end)) {
804 mov(result_end, object_size);
805 }
806 add(result_end, Operand(result));
Steve Block1e0659c2011-05-24 12:43:12 +0100807 j(carry, gc_required, not_taken);
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
809 j(above, gc_required, not_taken);
810
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 // Tag result if requested.
812 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000813 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 }
Leon Clarkee46be812010-01-19 14:06:41 +0000815
816 // Update allocation top.
817 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000818}
819
820
821void MacroAssembler::UndoAllocationInNewSpace(Register object) {
822 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100823 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000824
825 // Make sure the object has no tag before resetting top.
826 and_(Operand(object), Immediate(~kHeapObjectTagMask));
827#ifdef DEBUG
828 cmp(object, Operand::StaticVariable(new_space_allocation_top));
829 Check(below, "Undo allocation of non allocated memory");
830#endif
831 mov(Operand::StaticVariable(new_space_allocation_top), object);
832}
833
834
Steve Block3ce2e202009-11-05 08:53:23 +0000835void MacroAssembler::AllocateHeapNumber(Register result,
836 Register scratch1,
837 Register scratch2,
838 Label* gc_required) {
839 // Allocate heap number in new space.
840 AllocateInNewSpace(HeapNumber::kSize,
841 result,
842 scratch1,
843 scratch2,
844 gc_required,
845 TAG_OBJECT);
846
847 // Set the map.
848 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100849 Immediate(isolate()->factory()->heap_number_map()));
Steve Block3ce2e202009-11-05 08:53:23 +0000850}
851
852
Steve Blockd0582a62009-12-15 09:54:21 +0000853void MacroAssembler::AllocateTwoByteString(Register result,
854 Register length,
855 Register scratch1,
856 Register scratch2,
857 Register scratch3,
858 Label* gc_required) {
859 // Calculate the number of bytes needed for the characters in the string while
860 // observing object alignment.
861 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000862 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000863 // scratch1 = length * 2 + kObjectAlignmentMask.
864 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000865 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
866
867 // Allocate two byte string in new space.
868 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
869 times_1,
870 scratch1,
871 result,
872 scratch2,
873 scratch3,
874 gc_required,
875 TAG_OBJECT);
876
877 // Set the map, length and hash field.
878 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100879 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100880 mov(scratch1, length);
881 SmiTag(scratch1);
882 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000883 mov(FieldOperand(result, String::kHashFieldOffset),
884 Immediate(String::kEmptyHashField));
885}
886
887
888void MacroAssembler::AllocateAsciiString(Register result,
889 Register length,
890 Register scratch1,
891 Register scratch2,
892 Register scratch3,
893 Label* gc_required) {
894 // Calculate the number of bytes needed for the characters in the string while
895 // observing object alignment.
896 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
897 mov(scratch1, length);
898 ASSERT(kCharSize == 1);
899 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
900 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
901
902 // Allocate ascii string in new space.
903 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
904 times_1,
905 scratch1,
906 result,
907 scratch2,
908 scratch3,
909 gc_required,
910 TAG_OBJECT);
911
912 // Set the map, length and hash field.
913 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100914 Immediate(isolate()->factory()->ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100915 mov(scratch1, length);
916 SmiTag(scratch1);
917 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000918 mov(FieldOperand(result, String::kHashFieldOffset),
919 Immediate(String::kEmptyHashField));
920}
921
922
Iain Merrick9ac36c92010-09-13 15:29:50 +0100923void MacroAssembler::AllocateAsciiString(Register result,
924 int length,
925 Register scratch1,
926 Register scratch2,
927 Label* gc_required) {
928 ASSERT(length > 0);
929
930 // Allocate ascii string in new space.
931 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
932 result,
933 scratch1,
934 scratch2,
935 gc_required,
936 TAG_OBJECT);
937
938 // Set the map, length and hash field.
939 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100940 Immediate(isolate()->factory()->ascii_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100941 mov(FieldOperand(result, String::kLengthOffset),
942 Immediate(Smi::FromInt(length)));
943 mov(FieldOperand(result, String::kHashFieldOffset),
944 Immediate(String::kEmptyHashField));
945}
946
947
Steve Blockd0582a62009-12-15 09:54:21 +0000948void MacroAssembler::AllocateConsString(Register result,
949 Register scratch1,
950 Register scratch2,
951 Label* gc_required) {
952 // Allocate heap number in new space.
953 AllocateInNewSpace(ConsString::kSize,
954 result,
955 scratch1,
956 scratch2,
957 gc_required,
958 TAG_OBJECT);
959
960 // Set the map. The other fields are left uninitialized.
961 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100962 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +0000963}
964
965
966void MacroAssembler::AllocateAsciiConsString(Register result,
967 Register scratch1,
968 Register scratch2,
969 Label* gc_required) {
970 // Allocate heap number in new space.
971 AllocateInNewSpace(ConsString::kSize,
972 result,
973 scratch1,
974 scratch2,
975 gc_required,
976 TAG_OBJECT);
977
978 // Set the map. The other fields are left uninitialized.
979 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100980 Immediate(isolate()->factory()->cons_ascii_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +0000981}
982
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800983
Ben Murdochb8e0da22011-05-16 14:20:40 +0100984// Copy memory, byte-by-byte, from source to destination. Not optimized for
985// long or aligned copies. The contents of scratch and length are destroyed.
986// Source and destination are incremented by length.
987// Many variants of movsb, loop unrolling, word moves, and indexed operands
988// have been tried here already, and this is fastest.
989// A simpler loop is faster on small copies, but 30% slower on large ones.
990// The cld() instruction must have been emitted, to set the direction flag(),
991// before calling this function.
992void MacroAssembler::CopyBytes(Register source,
993 Register destination,
994 Register length,
995 Register scratch) {
996 Label loop, done, short_string, short_loop;
997 // Experimentation shows that the short string loop is faster if length < 10.
998 cmp(Operand(length), Immediate(10));
999 j(less_equal, &short_string);
1000
1001 ASSERT(source.is(esi));
1002 ASSERT(destination.is(edi));
1003 ASSERT(length.is(ecx));
1004
1005 // Because source is 4-byte aligned in our uses of this function,
1006 // we keep source aligned for the rep_movs call by copying the odd bytes
1007 // at the end of the ranges.
1008 mov(scratch, Operand(source, length, times_1, -4));
1009 mov(Operand(destination, length, times_1, -4), scratch);
1010 mov(scratch, ecx);
1011 shr(ecx, 2);
1012 rep_movs();
1013 and_(Operand(scratch), Immediate(0x3));
1014 add(destination, Operand(scratch));
1015 jmp(&done);
1016
1017 bind(&short_string);
1018 test(length, Operand(length));
1019 j(zero, &done);
1020
1021 bind(&short_loop);
1022 mov_b(scratch, Operand(source, 0));
1023 mov_b(Operand(destination, 0), scratch);
1024 inc(source);
1025 inc(destination);
1026 dec(length);
1027 j(not_zero, &short_loop);
1028
1029 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001030}
1031
Steve Blockd0582a62009-12-15 09:54:21 +00001032
Steve Blocka7e24c12009-10-30 11:49:00 +00001033void MacroAssembler::NegativeZeroTest(Register result,
1034 Register op,
1035 Label* then_label) {
1036 Label ok;
1037 test(result, Operand(result));
1038 j(not_zero, &ok, taken);
1039 test(op, Operand(op));
1040 j(sign, then_label, not_taken);
1041 bind(&ok);
1042}
1043
1044
1045void MacroAssembler::NegativeZeroTest(Register result,
1046 Register op1,
1047 Register op2,
1048 Register scratch,
1049 Label* then_label) {
1050 Label ok;
1051 test(result, Operand(result));
1052 j(not_zero, &ok, taken);
1053 mov(scratch, Operand(op1));
1054 or_(scratch, Operand(op2));
1055 j(sign, then_label, not_taken);
1056 bind(&ok);
1057}
1058
1059
1060void MacroAssembler::TryGetFunctionPrototype(Register function,
1061 Register result,
1062 Register scratch,
1063 Label* miss) {
1064 // Check that the receiver isn't a smi.
1065 test(function, Immediate(kSmiTagMask));
1066 j(zero, miss, not_taken);
1067
1068 // Check that the function really is a function.
1069 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1070 j(not_equal, miss, not_taken);
1071
1072 // Make sure that the function has an instance prototype.
1073 Label non_instance;
1074 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1075 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1076 j(not_zero, &non_instance, not_taken);
1077
1078 // Get the prototype or initial map from the function.
1079 mov(result,
1080 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1081
1082 // If the prototype or initial map is the hole, don't return it and
1083 // simply miss the cache instead. This will allow us to allocate a
1084 // prototype object on-demand in the runtime system.
Steve Block44f0eee2011-05-26 01:26:41 +01001085 cmp(Operand(result), Immediate(isolate()->factory()->the_hole_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001086 j(equal, miss, not_taken);
1087
1088 // If the function does not have an initial map, we're done.
1089 Label done;
1090 CmpObjectType(result, MAP_TYPE, scratch);
1091 j(not_equal, &done);
1092
1093 // Get the prototype from the initial map.
1094 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1095 jmp(&done);
1096
1097 // Non-instance prototype: Fetch prototype from constructor field
1098 // in initial map.
1099 bind(&non_instance);
1100 mov(result, FieldOperand(result, Map::kConstructorOffset));
1101
1102 // All done.
1103 bind(&done);
1104}
1105
1106
1107void MacroAssembler::CallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001108 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001109 call(stub->GetCode(), RelocInfo::CODE_TARGET);
1110}
1111
1112
John Reck59135872010-11-02 12:39:01 -07001113MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001114 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001115 Object* result;
1116 { MaybeObject* maybe_result = stub->TryGetCode();
1117 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001118 }
John Reck59135872010-11-02 12:39:01 -07001119 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001120 return result;
1121}
1122
1123
Steve Blockd0582a62009-12-15 09:54:21 +00001124void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001125 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +00001126 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1127}
1128
1129
John Reck59135872010-11-02 12:39:01 -07001130MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001131 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001132 Object* result;
1133 { MaybeObject* maybe_result = stub->TryGetCode();
1134 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001135 }
John Reck59135872010-11-02 12:39:01 -07001136 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001137 return result;
1138}
1139
1140
Steve Blocka7e24c12009-10-30 11:49:00 +00001141void MacroAssembler::StubReturn(int argc) {
1142 ASSERT(argc >= 1 && generating_stub());
1143 ret((argc - 1) * kPointerSize);
1144}
1145
1146
1147void MacroAssembler::IllegalOperation(int num_arguments) {
1148 if (num_arguments > 0) {
1149 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1150 }
Steve Block44f0eee2011-05-26 01:26:41 +01001151 mov(eax, Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001152}
1153
1154
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001155void MacroAssembler::IndexFromHash(Register hash, Register index) {
1156 // The assert checks that the constants for the maximum number of digits
1157 // for an array index cached in the hash field and the number of bits
1158 // reserved for it does not conflict.
1159 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1160 (1 << String::kArrayIndexValueBits));
1161 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1162 // the low kHashShift bits.
1163 and_(hash, String::kArrayIndexValueMask);
1164 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1165 if (String::kHashShift > kSmiTagSize) {
1166 shr(hash, String::kHashShift - kSmiTagSize);
1167 }
1168 if (!index.is(hash)) {
1169 mov(index, hash);
1170 }
1171}
1172
1173
Steve Blocka7e24c12009-10-30 11:49:00 +00001174void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1175 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1176}
1177
1178
Ben Murdochb0fe1622011-05-05 13:52:32 +01001179void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01001180 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001181 Set(eax, Immediate(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01001182 mov(ebx, Immediate(ExternalReference(function, isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001183 CEntryStub ces(1);
1184 ces.SaveDoubles();
1185 CallStub(&ces);
1186}
1187
1188
John Reck59135872010-11-02 12:39:01 -07001189MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1190 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001191 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1192}
1193
1194
Steve Block44f0eee2011-05-26 01:26:41 +01001195void MacroAssembler::CallRuntime(const Runtime::Function* f,
1196 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001197 // If the expected number of arguments of the runtime function is
1198 // constant, we check that the actual number of arguments match the
1199 // expectation.
1200 if (f->nargs >= 0 && f->nargs != num_arguments) {
1201 IllegalOperation(num_arguments);
1202 return;
1203 }
1204
Leon Clarke4515c472010-02-03 11:58:03 +00001205 // TODO(1236192): Most runtime routines don't need the number of
1206 // arguments passed in because it is constant. At some point we
1207 // should remove this need and make the runtime routine entry code
1208 // smarter.
1209 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001210 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001211 CEntryStub ces(1);
1212 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001213}
1214
1215
Steve Block44f0eee2011-05-26 01:26:41 +01001216MaybeObject* MacroAssembler::TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -07001217 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001218 if (f->nargs >= 0 && f->nargs != num_arguments) {
1219 IllegalOperation(num_arguments);
1220 // Since we did not call the stub, there was no allocation failure.
1221 // Return some non-failure object.
Steve Block44f0eee2011-05-26 01:26:41 +01001222 return isolate()->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +00001223 }
1224
Leon Clarke4515c472010-02-03 11:58:03 +00001225 // TODO(1236192): Most runtime routines don't need the number of
1226 // arguments passed in because it is constant. At some point we
1227 // should remove this need and make the runtime routine entry code
1228 // smarter.
1229 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001230 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001231 CEntryStub ces(1);
1232 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001233}
1234
1235
Ben Murdochbb769b22010-08-11 14:56:33 +01001236void MacroAssembler::CallExternalReference(ExternalReference ref,
1237 int num_arguments) {
1238 mov(eax, Immediate(num_arguments));
1239 mov(ebx, Immediate(ref));
1240
1241 CEntryStub stub(1);
1242 CallStub(&stub);
1243}
1244
1245
Steve Block6ded16b2010-05-10 14:33:55 +01001246void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1247 int num_arguments,
1248 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001249 // TODO(1236192): Most runtime routines don't need the number of
1250 // arguments passed in because it is constant. At some point we
1251 // should remove this need and make the runtime routine entry code
1252 // smarter.
1253 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001254 JumpToExternalReference(ext);
1255}
1256
1257
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001258MaybeObject* MacroAssembler::TryTailCallExternalReference(
1259 const ExternalReference& ext, int num_arguments, int result_size) {
1260 // TODO(1236192): Most runtime routines don't need the number of
1261 // arguments passed in because it is constant. At some point we
1262 // should remove this need and make the runtime routine entry code
1263 // smarter.
1264 Set(eax, Immediate(num_arguments));
1265 return TryJumpToExternalReference(ext);
1266}
1267
1268
Steve Block6ded16b2010-05-10 14:33:55 +01001269void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1270 int num_arguments,
1271 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01001272 TailCallExternalReference(ExternalReference(fid, isolate()),
1273 num_arguments,
1274 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001275}
1276
1277
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001278MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1279 int num_arguments,
1280 int result_size) {
1281 return TryTailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001282 ExternalReference(fid, isolate()), num_arguments, result_size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001283}
1284
1285
Ben Murdochb0fe1622011-05-05 13:52:32 +01001286// If true, a Handle<T> returned by value from a function with cdecl calling
1287// convention will be returned directly as a value of location_ field in a
1288// register eax.
1289// If false, it is returned as a pointer to a preallocated by caller memory
1290// region. Pointer to this region should be passed to a function as an
1291// implicit first argument.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001292#if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001293static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001294#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001295static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001296#endif
1297
1298
1299Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001300 return Operand(
1301 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001302}
1303
1304
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001305void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001306 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001307 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001308 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001309 // space for and pass an out parameter.
1310 } else {
1311 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001312 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001313
John Reck59135872010-11-02 12:39:01 -07001314 // The argument slots are filled as follows:
1315 //
1316 // n + 1: output cell
1317 // n: arg n
1318 // ...
1319 // 1: arg1
1320 // 0: pointer to the output cell
1321 //
1322 // Note that this is one more "argument" than the function expects
1323 // so the out cell will have to be popped explicitly after returning
1324 // from the function. The out cell contains Handle.
John Reck59135872010-11-02 12:39:01 -07001325
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001326 // pointer to out cell.
1327 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1328 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
Steve Block44f0eee2011-05-26 01:26:41 +01001329 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001330 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1331 }
1332 }
1333}
1334
1335
1336MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1337 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001338 ExternalReference next_address =
1339 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001340 ExternalReference limit_address =
1341 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001342 ExternalReference level_address =
1343 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001344
John Reck59135872010-11-02 12:39:01 -07001345 // Allocate HandleScope in callee-save registers.
1346 mov(ebx, Operand::StaticVariable(next_address));
1347 mov(edi, Operand::StaticVariable(limit_address));
1348 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001349
John Reck59135872010-11-02 12:39:01 -07001350 // Call the api function!
1351 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1352
Ben Murdochb0fe1622011-05-05 13:52:32 +01001353 if (!kReturnHandlesDirectly) {
John Reck59135872010-11-02 12:39:01 -07001354 // The returned value is a pointer to the handle holding the result.
1355 // Dereference this to get to the location.
1356 mov(eax, Operand(eax, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001357 }
Steve Blockd0582a62009-12-15 09:54:21 +00001358
John Reck59135872010-11-02 12:39:01 -07001359 Label empty_handle;
1360 Label prologue;
1361 Label promote_scheduled_exception;
1362 Label delete_allocated_handles;
1363 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001364
John Reck59135872010-11-02 12:39:01 -07001365 // Check if the result handle holds 0.
1366 test(eax, Operand(eax));
1367 j(zero, &empty_handle, not_taken);
1368 // It was non-zero. Dereference to get the result value.
1369 mov(eax, Operand(eax, 0));
1370 bind(&prologue);
1371 // No more valid handles (the result handle was the last one). Restore
1372 // previous handle scope.
1373 mov(Operand::StaticVariable(next_address), ebx);
1374 sub(Operand::StaticVariable(level_address), Immediate(1));
1375 Assert(above_equal, "Invalid HandleScope level");
1376 cmp(edi, Operand::StaticVariable(limit_address));
1377 j(not_equal, &delete_allocated_handles, not_taken);
1378 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001379
John Reck59135872010-11-02 12:39:01 -07001380 // Check if the function scheduled an exception.
1381 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01001382 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07001383 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01001384 Immediate(isolate()->factory()->the_hole_value()));
John Reck59135872010-11-02 12:39:01 -07001385 j(not_equal, &promote_scheduled_exception, not_taken);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001386 LeaveApiExitFrame();
1387 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001388 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001389 MaybeObject* result =
1390 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1391 if (result->IsFailure()) {
1392 return result;
1393 }
John Reck59135872010-11-02 12:39:01 -07001394 bind(&empty_handle);
1395 // It was zero; the result is undefined.
Steve Block44f0eee2011-05-26 01:26:41 +01001396 mov(eax, isolate()->factory()->undefined_value());
John Reck59135872010-11-02 12:39:01 -07001397 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001398
John Reck59135872010-11-02 12:39:01 -07001399 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01001400 ExternalReference delete_extensions =
1401 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07001402 bind(&delete_allocated_handles);
1403 mov(Operand::StaticVariable(limit_address), edi);
1404 mov(edi, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001405 mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
1406 mov(eax, Immediate(delete_extensions));
John Reck59135872010-11-02 12:39:01 -07001407 call(Operand(eax));
1408 mov(eax, edi);
1409 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001410
1411 return result;
Steve Blockd0582a62009-12-15 09:54:21 +00001412}
1413
1414
Steve Block6ded16b2010-05-10 14:33:55 +01001415void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 // Set the entry point and jump to the C entry runtime stub.
1417 mov(ebx, Immediate(ext));
1418 CEntryStub ces(1);
1419 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1420}
1421
1422
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001423MaybeObject* MacroAssembler::TryJumpToExternalReference(
1424 const ExternalReference& ext) {
1425 // Set the entry point and jump to the C entry runtime stub.
1426 mov(ebx, Immediate(ext));
1427 CEntryStub ces(1);
1428 return TryTailCallStub(&ces);
1429}
1430
1431
Steve Blocka7e24c12009-10-30 11:49:00 +00001432void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1433 const ParameterCount& actual,
1434 Handle<Code> code_constant,
1435 const Operand& code_operand,
Steve Block44f0eee2011-05-26 01:26:41 +01001436 NearLabel* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001437 InvokeFlag flag,
1438 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001439 bool definitely_matches = false;
1440 Label invoke;
1441 if (expected.is_immediate()) {
1442 ASSERT(actual.is_immediate());
1443 if (expected.immediate() == actual.immediate()) {
1444 definitely_matches = true;
1445 } else {
1446 mov(eax, actual.immediate());
1447 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1448 if (expected.immediate() == sentinel) {
1449 // Don't worry about adapting arguments for builtins that
1450 // don't want that done. Skip adaption code by making it look
1451 // like we have a match between expected and actual number of
1452 // arguments.
1453 definitely_matches = true;
1454 } else {
1455 mov(ebx, expected.immediate());
1456 }
1457 }
1458 } else {
1459 if (actual.is_immediate()) {
1460 // Expected is in register, actual is immediate. This is the
1461 // case when we invoke function values without going through the
1462 // IC mechanism.
1463 cmp(expected.reg(), actual.immediate());
1464 j(equal, &invoke);
1465 ASSERT(expected.reg().is(ebx));
1466 mov(eax, actual.immediate());
1467 } else if (!expected.reg().is(actual.reg())) {
1468 // Both expected and actual are in (different) registers. This
1469 // is the case when we invoke functions using call and apply.
1470 cmp(expected.reg(), Operand(actual.reg()));
1471 j(equal, &invoke);
1472 ASSERT(actual.reg().is(eax));
1473 ASSERT(expected.reg().is(ebx));
1474 }
1475 }
1476
1477 if (!definitely_matches) {
1478 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001479 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001480 if (!code_constant.is_null()) {
1481 mov(edx, Immediate(code_constant));
1482 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1483 } else if (!code_operand.is_reg(edx)) {
1484 mov(edx, code_operand);
1485 }
1486
1487 if (flag == CALL_FUNCTION) {
1488 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001489 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001490 jmp(done);
1491 } else {
1492 jmp(adaptor, RelocInfo::CODE_TARGET);
1493 }
1494 bind(&invoke);
1495 }
1496}
1497
1498
1499void MacroAssembler::InvokeCode(const Operand& code,
1500 const ParameterCount& expected,
1501 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001502 InvokeFlag flag,
1503 PostCallGenerator* post_call_generator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001504 NearLabel done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001505 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1506 &done, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001507 if (flag == CALL_FUNCTION) {
1508 call(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001509 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001510 } else {
1511 ASSERT(flag == JUMP_FUNCTION);
1512 jmp(code);
1513 }
1514 bind(&done);
1515}
1516
1517
1518void MacroAssembler::InvokeCode(Handle<Code> code,
1519 const ParameterCount& expected,
1520 const ParameterCount& actual,
1521 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001522 InvokeFlag flag,
1523 PostCallGenerator* post_call_generator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001524 NearLabel done;
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 Operand dummy(eax);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001526 InvokePrologue(expected, actual, code, dummy, &done,
1527 flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001528 if (flag == CALL_FUNCTION) {
1529 call(code, rmode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001530 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 } else {
1532 ASSERT(flag == JUMP_FUNCTION);
1533 jmp(code, rmode);
1534 }
1535 bind(&done);
1536}
1537
1538
1539void MacroAssembler::InvokeFunction(Register fun,
1540 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001541 InvokeFlag flag,
1542 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001543 ASSERT(fun.is(edi));
1544 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1545 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1546 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001547 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001548
1549 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01001550 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001551 expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001552}
1553
1554
Andrei Popescu402d9372010-02-26 13:31:12 +00001555void MacroAssembler::InvokeFunction(JSFunction* function,
1556 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001557 InvokeFlag flag,
1558 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001559 ASSERT(function->is_compiled());
1560 // Get the function and setup the context.
1561 mov(edi, Immediate(Handle<JSFunction>(function)));
1562 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001563
Andrei Popescu402d9372010-02-26 13:31:12 +00001564 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001565 if (V8::UseCrankshaft()) {
1566 // TODO(kasperl): For now, we always call indirectly through the
1567 // code field in the function to allow recompilation to take effect
1568 // without changing any of the call sites.
1569 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1570 expected, actual, flag, post_call_generator);
1571 } else {
1572 Handle<Code> code(function->code());
1573 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
1574 flag, post_call_generator);
1575 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001576}
1577
1578
Ben Murdochb0fe1622011-05-05 13:52:32 +01001579void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1580 InvokeFlag flag,
1581 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001582 // Calls are not allowed in some stubs.
1583 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1584
1585 // Rely on the assertion to check that the number of provided
1586 // arguments match the expected number of arguments. Fake a
1587 // parameter count to avoid emitting code to do the check.
1588 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01001589 GetBuiltinFunction(edi, id);
1590 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001591 expected, expected, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001592}
1593
Steve Block791712a2010-08-27 10:21:07 +01001594void MacroAssembler::GetBuiltinFunction(Register target,
1595 Builtins::JavaScript id) {
1596 // Load the JavaScript builtin function from the builtins object.
1597 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1598 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1599 mov(target, FieldOperand(target,
1600 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1601}
Steve Blocka7e24c12009-10-30 11:49:00 +00001602
1603void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001604 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00001605 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01001606 GetBuiltinFunction(edi, id);
1607 // Load the code entry point from the function into the target register.
1608 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001609}
1610
1611
Steve Blockd0582a62009-12-15 09:54:21 +00001612void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1613 if (context_chain_length > 0) {
1614 // Move up the chain of contexts to the context containing the slot.
1615 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1616 // Load the function context (which is the incoming, outer context).
1617 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1618 for (int i = 1; i < context_chain_length; i++) {
1619 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1620 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1621 }
Steve Block1e0659c2011-05-24 12:43:12 +01001622 } else {
1623 // Slot is in the current function context. Move it into the
1624 // destination register in case we store into it (the write barrier
1625 // cannot be allowed to destroy the context in esi).
1626 mov(dst, esi);
1627 }
1628
1629 // We should not have found a 'with' context by walking the context chain
1630 // (i.e., the static scope chain and runtime context chain do not agree).
1631 // A variable occurring in such a scope should have slot type LOOKUP and
1632 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01001633 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001634 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1635 Check(equal, "Yo dawg, I heard you liked function contexts "
1636 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00001637 }
1638}
1639
1640
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001641void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1642 // Load the global or builtins object from the current context.
1643 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1644 // Load the global context from the global or builtins object.
1645 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1646 // Load the function from the global context.
1647 mov(function, Operand(function, Context::SlotOffset(index)));
1648}
1649
1650
1651void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1652 Register map) {
1653 // Load the initial map. The global functions all have initial maps.
1654 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001655 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001656 Label ok, fail;
Steve Block44f0eee2011-05-26 01:26:41 +01001657 CheckMap(map, isolate()->factory()->meta_map(), &fail, false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001658 jmp(&ok);
1659 bind(&fail);
1660 Abort("Global functions must have initial map");
1661 bind(&ok);
1662 }
1663}
1664
Steve Blockd0582a62009-12-15 09:54:21 +00001665
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001666// Store the value in register src in the safepoint register stack
1667// slot for register dst.
1668void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
1669 mov(SafepointRegisterSlot(dst), src);
1670}
1671
1672
1673void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
1674 mov(SafepointRegisterSlot(dst), src);
1675}
1676
1677
1678void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
1679 mov(dst, SafepointRegisterSlot(src));
1680}
1681
1682
1683Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
1684 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
1685}
1686
1687
Ben Murdochb0fe1622011-05-05 13:52:32 +01001688int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1689 // The registers are pushed starting with the lowest encoding,
1690 // which means that lowest encodings are furthest away from
1691 // the stack pointer.
1692 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1693 return kNumSafepointRegisters - reg_code - 1;
1694}
1695
1696
Steve Blocka7e24c12009-10-30 11:49:00 +00001697void MacroAssembler::Ret() {
1698 ret(0);
1699}
1700
1701
Steve Block1e0659c2011-05-24 12:43:12 +01001702void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1703 if (is_uint16(bytes_dropped)) {
1704 ret(bytes_dropped);
1705 } else {
1706 pop(scratch);
1707 add(Operand(esp), Immediate(bytes_dropped));
1708 push(scratch);
1709 ret(0);
1710 }
1711}
1712
1713
1714
1715
Leon Clarkee46be812010-01-19 14:06:41 +00001716void MacroAssembler::Drop(int stack_elements) {
1717 if (stack_elements > 0) {
1718 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1719 }
1720}
1721
1722
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001723void MacroAssembler::Move(Register dst, Register src) {
1724 if (!dst.is(src)) {
1725 mov(dst, src);
1726 }
1727}
1728
1729
Leon Clarkee46be812010-01-19 14:06:41 +00001730void MacroAssembler::Move(Register dst, Handle<Object> value) {
1731 mov(dst, value);
1732}
1733
1734
Steve Blocka7e24c12009-10-30 11:49:00 +00001735void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1736 if (FLAG_native_code_counters && counter->Enabled()) {
1737 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1738 }
1739}
1740
1741
1742void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1743 ASSERT(value > 0);
1744 if (FLAG_native_code_counters && counter->Enabled()) {
1745 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1746 if (value == 1) {
1747 inc(operand);
1748 } else {
1749 add(operand, Immediate(value));
1750 }
1751 }
1752}
1753
1754
1755void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1756 ASSERT(value > 0);
1757 if (FLAG_native_code_counters && counter->Enabled()) {
1758 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1759 if (value == 1) {
1760 dec(operand);
1761 } else {
1762 sub(operand, Immediate(value));
1763 }
1764 }
1765}
1766
1767
Leon Clarked91b9f72010-01-27 17:25:45 +00001768void MacroAssembler::IncrementCounter(Condition cc,
1769 StatsCounter* counter,
1770 int value) {
1771 ASSERT(value > 0);
1772 if (FLAG_native_code_counters && counter->Enabled()) {
1773 Label skip;
1774 j(NegateCondition(cc), &skip);
1775 pushfd();
1776 IncrementCounter(counter, value);
1777 popfd();
1778 bind(&skip);
1779 }
1780}
1781
1782
1783void MacroAssembler::DecrementCounter(Condition cc,
1784 StatsCounter* counter,
1785 int value) {
1786 ASSERT(value > 0);
1787 if (FLAG_native_code_counters && counter->Enabled()) {
1788 Label skip;
1789 j(NegateCondition(cc), &skip);
1790 pushfd();
1791 DecrementCounter(counter, value);
1792 popfd();
1793 bind(&skip);
1794 }
1795}
1796
1797
Steve Blocka7e24c12009-10-30 11:49:00 +00001798void MacroAssembler::Assert(Condition cc, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01001799 if (emit_debug_code()) Check(cc, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00001800}
1801
1802
Iain Merrick75681382010-08-19 15:07:18 +01001803void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01001804 if (emit_debug_code()) {
1805 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01001806 Label ok;
1807 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001808 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001809 j(equal, &ok);
1810 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001811 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001812 j(equal, &ok);
1813 Abort("JSObject with fast elements map has slow elements");
1814 bind(&ok);
1815 }
1816}
1817
1818
Steve Blocka7e24c12009-10-30 11:49:00 +00001819void MacroAssembler::Check(Condition cc, const char* msg) {
1820 Label L;
1821 j(cc, &L, taken);
1822 Abort(msg);
1823 // will not return here
1824 bind(&L);
1825}
1826
1827
Steve Block6ded16b2010-05-10 14:33:55 +01001828void MacroAssembler::CheckStackAlignment() {
1829 int frame_alignment = OS::ActivationFrameAlignment();
1830 int frame_alignment_mask = frame_alignment - 1;
1831 if (frame_alignment > kPointerSize) {
1832 ASSERT(IsPowerOf2(frame_alignment));
1833 Label alignment_as_expected;
1834 test(esp, Immediate(frame_alignment_mask));
1835 j(zero, &alignment_as_expected);
1836 // Abort if stack is not aligned.
1837 int3();
1838 bind(&alignment_as_expected);
1839 }
1840}
1841
1842
Steve Blocka7e24c12009-10-30 11:49:00 +00001843void MacroAssembler::Abort(const char* msg) {
1844 // We want to pass the msg string like a smi to avoid GC
1845 // problems, however msg is not guaranteed to be aligned
1846 // properly. Instead, we pass an aligned pointer that is
1847 // a proper v8 smi, but also pass the alignment difference
1848 // from the real pointer as a smi.
1849 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1850 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1851 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1852#ifdef DEBUG
1853 if (msg != NULL) {
1854 RecordComment("Abort message: ");
1855 RecordComment(msg);
1856 }
1857#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001858 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001859 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001860
Steve Blocka7e24c12009-10-30 11:49:00 +00001861 push(eax);
1862 push(Immediate(p0));
1863 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1864 CallRuntime(Runtime::kAbort, 2);
1865 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00001866 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00001867}
1868
1869
Iain Merrick75681382010-08-19 15:07:18 +01001870void MacroAssembler::JumpIfNotNumber(Register reg,
1871 TypeInfo info,
1872 Label* on_not_number) {
Steve Block44f0eee2011-05-26 01:26:41 +01001873 if (emit_debug_code()) AbortIfSmi(reg);
Iain Merrick75681382010-08-19 15:07:18 +01001874 if (!info.IsNumber()) {
1875 cmp(FieldOperand(reg, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001876 isolate()->factory()->heap_number_map());
Iain Merrick75681382010-08-19 15:07:18 +01001877 j(not_equal, on_not_number);
1878 }
1879}
1880
1881
1882void MacroAssembler::ConvertToInt32(Register dst,
1883 Register source,
1884 Register scratch,
1885 TypeInfo info,
1886 Label* on_not_int32) {
Steve Block44f0eee2011-05-26 01:26:41 +01001887 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01001888 AbortIfSmi(source);
1889 AbortIfNotNumber(source);
1890 }
1891 if (info.IsInteger32()) {
1892 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1893 } else {
1894 Label done;
1895 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1896 ASSERT(!scratch.is(source));
1897 if (push_pop) {
1898 push(dst);
1899 scratch = dst;
1900 }
1901 if (scratch.is(no_reg)) scratch = dst;
1902 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1903 cmp(scratch, 0x80000000u);
1904 if (push_pop) {
1905 j(not_equal, &done);
1906 pop(dst);
1907 jmp(on_not_int32);
1908 } else {
1909 j(equal, on_not_int32);
1910 }
1911
1912 bind(&done);
1913 if (push_pop) {
1914 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1915 }
1916 if (!scratch.is(dst)) {
1917 mov(dst, scratch);
1918 }
1919 }
1920}
1921
1922
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001923void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1924 Register scratch,
1925 int power) {
1926 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1927 HeapNumber::kExponentBits));
1928 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1929 movd(dst, Operand(scratch));
1930 psllq(dst, HeapNumber::kMantissaBits);
1931}
1932
1933
Andrei Popescu402d9372010-02-26 13:31:12 +00001934void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1935 Register instance_type,
1936 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01001937 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001938 if (!scratch.is(instance_type)) {
1939 mov(scratch, instance_type);
1940 }
1941 and_(scratch,
1942 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1943 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1944 j(not_equal, failure);
1945}
1946
1947
Leon Clarked91b9f72010-01-27 17:25:45 +00001948void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1949 Register object2,
1950 Register scratch1,
1951 Register scratch2,
1952 Label* failure) {
1953 // Check that both objects are not smis.
1954 ASSERT_EQ(0, kSmiTag);
1955 mov(scratch1, Operand(object1));
1956 and_(scratch1, Operand(object2));
1957 test(scratch1, Immediate(kSmiTagMask));
1958 j(zero, failure);
1959
1960 // Load instance type for both strings.
1961 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1962 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1963 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1964 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1965
1966 // Check that both are flat ascii strings.
1967 const int kFlatAsciiStringMask =
1968 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1969 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1970 // Interleave bits from both instance types and compare them in one check.
1971 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1972 and_(scratch1, kFlatAsciiStringMask);
1973 and_(scratch2, kFlatAsciiStringMask);
1974 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1975 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1976 j(not_equal, failure);
1977}
1978
1979
Steve Block6ded16b2010-05-10 14:33:55 +01001980void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001981 int frame_alignment = OS::ActivationFrameAlignment();
1982 if (frame_alignment != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001983 // Make stack end at alignment and make room for num_arguments words
1984 // and the original value of esp.
1985 mov(scratch, esp);
1986 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
Ben Murdoch8b112d22011-06-08 16:22:53 +01001987 ASSERT(IsPowerOf2(frame_alignment));
1988 and_(esp, -frame_alignment);
Steve Block6ded16b2010-05-10 14:33:55 +01001989 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1990 } else {
1991 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1992 }
1993}
1994
1995
1996void MacroAssembler::CallCFunction(ExternalReference function,
1997 int num_arguments) {
1998 // Trashing eax is ok as it will be the return value.
1999 mov(Operand(eax), Immediate(function));
2000 CallCFunction(eax, num_arguments);
2001}
2002
2003
2004void MacroAssembler::CallCFunction(Register function,
2005 int num_arguments) {
2006 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01002007 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002008 CheckStackAlignment();
2009 }
2010
2011 call(Operand(function));
2012 if (OS::ActivationFrameAlignment() != 0) {
2013 mov(esp, Operand(esp, num_arguments * kPointerSize));
2014 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002015 add(Operand(esp), Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01002016 }
2017}
2018
2019
Steve Blocka7e24c12009-10-30 11:49:00 +00002020CodePatcher::CodePatcher(byte* address, int size)
Ben Murdoch8b112d22011-06-08 16:22:53 +01002021 : address_(address),
2022 size_(size),
2023 masm_(Isolate::Current(), address, size + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002024 // Create a new macro assembler pointing to the address of the code to patch.
2025 // The size is adjusted with kGap on order for the assembler to generate size
2026 // bytes of instructions without failing with buffer size constraints.
2027 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2028}
2029
2030
2031CodePatcher::~CodePatcher() {
2032 // Indicate that code has changed.
2033 CPU::FlushICache(address_, size_);
2034
2035 // Check that the code was patched as expected.
2036 ASSERT(masm_.pc_ == address_ + size_);
2037 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2038}
2039
2040
2041} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002042
2043#endif // V8_TARGET_ARCH_IA32