blob: 6e66b6e85fc548ed15d6365e9931e3dd9193d7c3 [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.
Ben Murdoch257744e2011-11-30 15:57:28 +000076 // Bit tests with a memory operand should be avoided on Intel processors,
77 // as they usually have long latency and multiple uops. We load the bit base
78 // operand to a register at first and store it back after bit set.
79 mov(scratch, Operand(object, Page::kDirtyFlagOffset));
80 bts(Operand(scratch), addr);
81 mov(Operand(object, Page::kDirtyFlagOffset), scratch);
82}
83
84
85void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
86 XMMRegister scratch_reg,
87 Register result_reg) {
88 Label done;
89 ExternalReference zero_ref = ExternalReference::address_of_zero();
90 movdbl(scratch_reg, Operand::StaticVariable(zero_ref));
91 Set(result_reg, Immediate(0));
92 ucomisd(input_reg, scratch_reg);
93 j(below, &done, Label::kNear);
94 ExternalReference half_ref = ExternalReference::address_of_one_half();
95 movdbl(scratch_reg, Operand::StaticVariable(half_ref));
96 addsd(scratch_reg, input_reg);
97 cvttsd2si(result_reg, Operand(scratch_reg));
98 test(result_reg, Immediate(0xFFFFFF00));
99 j(zero, &done, Label::kNear);
100 Set(result_reg, Immediate(255));
101 bind(&done);
102}
103
104
105void MacroAssembler::ClampUint8(Register reg) {
106 Label done;
107 test(reg, Immediate(0xFFFFFF00));
108 j(zero, &done, Label::kNear);
109 setcc(negative, reg); // 1 if negative, 0 if positive.
110 dec_b(reg); // 0 if negative, 255 if positive.
111 bind(&done);
112}
113
114
115void MacroAssembler::InNewSpace(Register object,
116 Register scratch,
117 Condition cc,
118 Label* branch,
119 Label::Distance branch_near) {
120 ASSERT(cc == equal || cc == not_equal);
121 if (Serializer::enabled()) {
122 // Can't do arithmetic on external references if it might get serialized.
123 mov(scratch, Operand(object));
124 // The mask isn't really an address. We load it as an external reference in
125 // case the size of the new space is different between the snapshot maker
126 // and the running system.
127 and_(Operand(scratch),
128 Immediate(ExternalReference::new_space_mask(isolate())));
129 cmp(Operand(scratch),
130 Immediate(ExternalReference::new_space_start(isolate())));
131 j(cc, branch, branch_near);
132 } else {
133 int32_t new_space_start = reinterpret_cast<int32_t>(
134 ExternalReference::new_space_start(isolate()).address());
135 lea(scratch, Operand(object, -new_space_start));
136 and_(scratch, isolate()->heap()->NewSpaceMask());
137 j(cc, branch, branch_near);
138 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000139}
140
141
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100142void MacroAssembler::RecordWrite(Register object,
143 int offset,
144 Register value,
145 Register scratch) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100146 // First, check if a write barrier is even needed. The tests below
147 // catch stores of Smis and stores into young gen.
Ben Murdoch257744e2011-11-30 15:57:28 +0000148 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 // Skip barrier if writing a smi.
151 ASSERT_EQ(0, kSmiTag);
152 test(value, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000153 j(zero, &done, Label::kNear);
Steve Blocka7e24c12009-10-30 11:49:00 +0000154
Ben Murdoch257744e2011-11-30 15:57:28 +0000155 InNewSpace(object, value, equal, &done, Label::kNear);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
Steve Block6ded16b2010-05-10 14:33:55 +0100157 // The offset is relative to a tagged or untagged HeapObject pointer,
158 // so either offset or offset + kHeapObjectTag must be a
159 // multiple of kPointerSize.
160 ASSERT(IsAligned(offset, kPointerSize) ||
161 IsAligned(offset + kHeapObjectTag, kPointerSize));
162
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100163 Register dst = scratch;
164 if (offset != 0) {
165 lea(dst, Operand(object, offset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100167 // Array access: calculate the destination address in the same manner as
168 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
169 // into an array of words.
170 ASSERT_EQ(1, kSmiTagSize);
171 ASSERT_EQ(0, kSmiTag);
172 lea(dst, Operand(object, dst, times_half_pointer_size,
173 FixedArray::kHeaderSize - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100175 RecordWriteHelper(object, dst, value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
177 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000178
179 // Clobber all input registers when running with the debug-code flag
180 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100181 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100182 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
183 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
184 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000185 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000186}
187
188
Steve Block8defd9f2010-07-08 12:39:36 +0100189void MacroAssembler::RecordWrite(Register object,
190 Register address,
191 Register value) {
Steve Block8defd9f2010-07-08 12:39:36 +0100192 // First, check if a write barrier is even needed. The tests below
193 // catch stores of Smis and stores into young gen.
194 Label done;
195
196 // Skip barrier if writing a smi.
197 ASSERT_EQ(0, kSmiTag);
198 test(value, Immediate(kSmiTagMask));
199 j(zero, &done);
200
201 InNewSpace(object, value, equal, &done);
202
203 RecordWriteHelper(object, address, value);
204
205 bind(&done);
206
207 // Clobber all input registers when running with the debug-code flag
208 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100209 if (emit_debug_code()) {
Steve Block8defd9f2010-07-08 12:39:36 +0100210 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
211 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
212 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
213 }
214}
215
216
Steve Blocka7e24c12009-10-30 11:49:00 +0000217#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000218void MacroAssembler::DebugBreak() {
219 Set(eax, Immediate(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100220 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +0000221 CEntryStub ces(1);
222 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
223}
Steve Blocka7e24c12009-10-30 11:49:00 +0000224#endif
225
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100226
Steve Blocka7e24c12009-10-30 11:49:00 +0000227void MacroAssembler::Set(Register dst, const Immediate& x) {
228 if (x.is_zero()) {
Steve Block053d10c2011-06-13 19:13:29 +0100229 xor_(dst, Operand(dst)); // Shorter than mov.
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 } else {
231 mov(dst, x);
232 }
233}
234
235
236void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
237 mov(dst, x);
238}
239
240
Steve Block053d10c2011-06-13 19:13:29 +0100241bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
242 static const int kMaxImmediateBits = 17;
243 if (x.rmode_ != RelocInfo::NONE) return false;
244 return !is_intn(x.x_, kMaxImmediateBits);
245}
246
247
248void MacroAssembler::SafeSet(Register dst, const Immediate& x) {
249 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
250 Set(dst, Immediate(x.x_ ^ jit_cookie()));
251 xor_(dst, jit_cookie());
252 } else {
253 Set(dst, x);
254 }
255}
256
257
258void MacroAssembler::SafePush(const Immediate& x) {
259 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
260 push(Immediate(x.x_ ^ jit_cookie()));
261 xor_(Operand(esp, 0), Immediate(jit_cookie()));
262 } else {
263 push(x);
264 }
265}
266
267
Steve Blocka7e24c12009-10-30 11:49:00 +0000268void MacroAssembler::CmpObjectType(Register heap_object,
269 InstanceType type,
270 Register map) {
271 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
272 CmpInstanceType(map, type);
273}
274
275
276void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
277 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
278 static_cast<int8_t>(type));
279}
280
281
Andrei Popescu31002712010-02-23 13:46:05 +0000282void MacroAssembler::CheckMap(Register obj,
283 Handle<Map> map,
284 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +0000285 SmiCheckType smi_check_type) {
286 if (smi_check_type == DO_SMI_CHECK) {
287 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +0000288 }
289 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
290 j(not_equal, fail);
291}
292
293
Ben Murdoch257744e2011-11-30 15:57:28 +0000294void MacroAssembler::DispatchMap(Register obj,
295 Handle<Map> map,
296 Handle<Code> success,
297 SmiCheckType smi_check_type) {
298 Label fail;
299 if (smi_check_type == DO_SMI_CHECK) {
300 JumpIfSmi(obj, &fail);
301 }
302 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
303 j(equal, success);
304
305 bind(&fail);
306}
307
308
Leon Clarkee46be812010-01-19 14:06:41 +0000309Condition MacroAssembler::IsObjectStringType(Register heap_object,
310 Register map,
311 Register instance_type) {
312 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
313 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
314 ASSERT(kNotStringTag != 0);
315 test(instance_type, Immediate(kIsNotStringMask));
316 return zero;
317}
318
319
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100320void MacroAssembler::IsObjectJSObjectType(Register heap_object,
321 Register map,
322 Register scratch,
323 Label* fail) {
324 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
325 IsInstanceJSObjectType(map, scratch, fail);
326}
327
328
329void MacroAssembler::IsInstanceJSObjectType(Register map,
330 Register scratch,
331 Label* fail) {
332 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
333 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
334 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
335 j(above, fail);
336}
337
338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339void MacroAssembler::FCmp() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100340 if (CpuFeatures::IsSupported(CMOV)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000341 fucomip();
342 ffree(0);
343 fincstp();
344 } else {
345 fucompp();
346 push(eax);
347 fnstsw_ax();
348 sahf();
349 pop(eax);
350 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000351}
352
353
Steve Block6ded16b2010-05-10 14:33:55 +0100354void MacroAssembler::AbortIfNotNumber(Register object) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000355 Label ok;
356 test(object, Immediate(kSmiTagMask));
357 j(zero, &ok);
358 cmp(FieldOperand(object, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100359 isolate()->factory()->heap_number_map());
Steve Block6ded16b2010-05-10 14:33:55 +0100360 Assert(equal, "Operand not a number");
Andrei Popescu402d9372010-02-26 13:31:12 +0000361 bind(&ok);
362}
363
364
Steve Block6ded16b2010-05-10 14:33:55 +0100365void MacroAssembler::AbortIfNotSmi(Register object) {
366 test(object, Immediate(kSmiTagMask));
Iain Merrick75681382010-08-19 15:07:18 +0100367 Assert(equal, "Operand is not a smi");
368}
369
370
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100371void MacroAssembler::AbortIfNotString(Register object) {
372 test(object, Immediate(kSmiTagMask));
373 Assert(not_equal, "Operand is not a string");
374 push(object);
375 mov(object, FieldOperand(object, HeapObject::kMapOffset));
376 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
377 pop(object);
378 Assert(below, "Operand is not a string");
379}
380
381
Iain Merrick75681382010-08-19 15:07:18 +0100382void MacroAssembler::AbortIfSmi(Register object) {
383 test(object, Immediate(kSmiTagMask));
384 Assert(not_equal, "Operand is a smi");
Steve Block6ded16b2010-05-10 14:33:55 +0100385}
386
387
Steve Blocka7e24c12009-10-30 11:49:00 +0000388void MacroAssembler::EnterFrame(StackFrame::Type type) {
389 push(ebp);
390 mov(ebp, Operand(esp));
391 push(esi);
392 push(Immediate(Smi::FromInt(type)));
393 push(Immediate(CodeObject()));
Steve Block44f0eee2011-05-26 01:26:41 +0100394 if (emit_debug_code()) {
395 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 Check(not_equal, "code object not properly patched");
397 }
398}
399
400
401void MacroAssembler::LeaveFrame(StackFrame::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100402 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
404 Immediate(Smi::FromInt(type)));
405 Check(equal, "stack frame types must match");
406 }
407 leave();
408}
409
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100410
411void MacroAssembler::EnterExitFramePrologue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 // Setup the frame structure on the stack.
413 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
414 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
415 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
416 push(ebp);
417 mov(ebp, Operand(esp));
418
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100419 // Reserve room for entry stack pointer and push the code object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000421 push(Immediate(0)); // Saved entry sp, patched before call.
422 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000423
424 // Save the frame pointer and the context in top.
Steve Block44f0eee2011-05-26 01:26:41 +0100425 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
426 isolate());
427 ExternalReference context_address(Isolate::k_context_address,
428 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
430 mov(Operand::StaticVariable(context_address), esi);
Steve Blockd0582a62009-12-15 09:54:21 +0000431}
Steve Blocka7e24c12009-10-30 11:49:00 +0000432
Steve Blocka7e24c12009-10-30 11:49:00 +0000433
Ben Murdochb0fe1622011-05-05 13:52:32 +0100434void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
435 // Optionally save all XMM registers.
436 if (save_doubles) {
437 CpuFeatures::Scope scope(SSE2);
438 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
439 sub(Operand(esp), Immediate(space));
Steve Block1e0659c2011-05-24 12:43:12 +0100440 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100441 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
442 XMMRegister reg = XMMRegister::from_code(i);
443 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
444 }
445 } else {
446 sub(Operand(esp), Immediate(argc * kPointerSize));
447 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000448
449 // Get the required frame alignment for the OS.
Steve Block44f0eee2011-05-26 01:26:41 +0100450 const int kFrameAlignment = OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 if (kFrameAlignment > 0) {
452 ASSERT(IsPowerOf2(kFrameAlignment));
453 and_(esp, -kFrameAlignment);
454 }
455
456 // Patch the saved entry sp.
457 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
458}
459
460
Ben Murdochb0fe1622011-05-05 13:52:32 +0100461void MacroAssembler::EnterExitFrame(bool save_doubles) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100462 EnterExitFramePrologue();
Steve Blockd0582a62009-12-15 09:54:21 +0000463
464 // Setup argc and argv in callee-saved registers.
465 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
466 mov(edi, Operand(eax));
467 lea(esi, Operand(ebp, eax, times_4, offset));
468
Steve Block44f0eee2011-05-26 01:26:41 +0100469 // Reserve space for argc, argv and isolate.
470 EnterExitFrameEpilogue(3, save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000471}
472
473
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800474void MacroAssembler::EnterApiExitFrame(int argc) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100475 EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100476 EnterExitFrameEpilogue(argc, false);
Steve Blockd0582a62009-12-15 09:54:21 +0000477}
478
479
Ben Murdochb0fe1622011-05-05 13:52:32 +0100480void MacroAssembler::LeaveExitFrame(bool save_doubles) {
481 // Optionally restore all XMM registers.
482 if (save_doubles) {
483 CpuFeatures::Scope scope(SSE2);
Steve Block1e0659c2011-05-24 12:43:12 +0100484 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100485 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
486 XMMRegister reg = XMMRegister::from_code(i);
487 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
488 }
489 }
490
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 // Get the return address from the stack and restore the frame pointer.
492 mov(ecx, Operand(ebp, 1 * kPointerSize));
493 mov(ebp, Operand(ebp, 0 * kPointerSize));
494
495 // Pop the arguments and the receiver from the caller stack.
496 lea(esp, Operand(esi, 1 * kPointerSize));
497
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800498 // Push the return address to get ready to return.
499 push(ecx);
500
501 LeaveExitFrameEpilogue();
502}
503
504void MacroAssembler::LeaveExitFrameEpilogue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000505 // Restore current context from top and clear it in debug mode.
Steve Block44f0eee2011-05-26 01:26:41 +0100506 ExternalReference context_address(Isolate::k_context_address, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 mov(esi, Operand::StaticVariable(context_address));
508#ifdef DEBUG
509 mov(Operand::StaticVariable(context_address), Immediate(0));
510#endif
511
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 // Clear the top frame.
Steve Block44f0eee2011-05-26 01:26:41 +0100513 ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address,
514 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
516}
517
518
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800519void MacroAssembler::LeaveApiExitFrame() {
520 mov(esp, Operand(ebp));
521 pop(ebp);
522
523 LeaveExitFrameEpilogue();
524}
525
526
Steve Blocka7e24c12009-10-30 11:49:00 +0000527void MacroAssembler::PushTryHandler(CodeLocation try_location,
528 HandlerType type) {
529 // Adjust this code if not the case.
530 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
531 // The pc (return address) is already on TOS.
532 if (try_location == IN_JAVASCRIPT) {
533 if (type == TRY_CATCH_HANDLER) {
534 push(Immediate(StackHandler::TRY_CATCH));
535 } else {
536 push(Immediate(StackHandler::TRY_FINALLY));
537 }
538 push(ebp);
539 } else {
540 ASSERT(try_location == IN_JS_ENTRY);
541 // The frame pointer does not point to a JS frame so we save NULL
542 // for ebp. We expect the code throwing an exception to check ebp
543 // before dereferencing it to restore the context.
544 push(Immediate(StackHandler::ENTRY));
545 push(Immediate(0)); // NULL frame pointer.
546 }
547 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100548 push(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
549 isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 // Link this handler as the new current one.
Steve Block44f0eee2011-05-26 01:26:41 +0100551 mov(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
552 isolate())),
553 esp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000554}
555
556
Leon Clarkee46be812010-01-19 14:06:41 +0000557void MacroAssembler::PopTryHandler() {
558 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
Steve Block44f0eee2011-05-26 01:26:41 +0100559 pop(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address,
560 isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +0000561 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
562}
563
564
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100565void MacroAssembler::Throw(Register value) {
566 // Adjust this code if not the case.
567 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
568
569 // eax must hold the exception.
570 if (!value.is(eax)) {
571 mov(eax, value);
572 }
573
574 // Drop the sp to the top of the handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100575 ExternalReference handler_address(Isolate::k_handler_address,
576 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100577 mov(esp, Operand::StaticVariable(handler_address));
578
579 // Restore next handler and frame pointer, discard handler state.
580 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
581 pop(Operand::StaticVariable(handler_address));
582 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
583 pop(ebp);
584 pop(edx); // Remove state.
585
586 // Before returning we restore the context from the frame pointer if
587 // not NULL. The frame pointer is NULL in the exception handler of
588 // a JS entry frame.
589 Set(esi, Immediate(0)); // Tentatively set context pointer to NULL.
Ben Murdoch257744e2011-11-30 15:57:28 +0000590 Label skip;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100591 cmp(ebp, 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000592 j(equal, &skip, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100593 mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
594 bind(&skip);
595
596 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
597 ret(0);
598}
599
600
601void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
602 Register value) {
603 // Adjust this code if not the case.
604 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
605
606 // eax must hold the exception.
607 if (!value.is(eax)) {
608 mov(eax, value);
609 }
610
611 // Drop sp to the top stack handler.
Steve Block44f0eee2011-05-26 01:26:41 +0100612 ExternalReference handler_address(Isolate::k_handler_address,
613 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100614 mov(esp, Operand::StaticVariable(handler_address));
615
616 // Unwind the handlers until the ENTRY handler is found.
Ben Murdoch257744e2011-11-30 15:57:28 +0000617 Label loop, done;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100618 bind(&loop);
619 // Load the type of the current stack handler.
620 const int kStateOffset = StackHandlerConstants::kStateOffset;
621 cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
Ben Murdoch257744e2011-11-30 15:57:28 +0000622 j(equal, &done, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100623 // Fetch the next handler in the list.
624 const int kNextOffset = StackHandlerConstants::kNextOffset;
625 mov(esp, Operand(esp, kNextOffset));
626 jmp(&loop);
627 bind(&done);
628
629 // Set the top handler address to next handler past the current ENTRY handler.
630 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
631 pop(Operand::StaticVariable(handler_address));
632
633 if (type == OUT_OF_MEMORY) {
634 // Set external caught exception to false.
Steve Block44f0eee2011-05-26 01:26:41 +0100635 ExternalReference external_caught(
636 Isolate::k_external_caught_exception_address,
637 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100638 mov(eax, false);
639 mov(Operand::StaticVariable(external_caught), eax);
640
641 // Set pending exception and eax to out of memory exception.
Steve Block44f0eee2011-05-26 01:26:41 +0100642 ExternalReference pending_exception(Isolate::k_pending_exception_address,
643 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100644 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
645 mov(Operand::StaticVariable(pending_exception), eax);
646 }
647
648 // Clear the context pointer.
649 Set(esi, Immediate(0));
650
651 // Restore fp from handler and discard handler state.
652 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
653 pop(ebp);
654 pop(edx); // State.
655
656 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
657 ret(0);
658}
659
660
Steve Blocka7e24c12009-10-30 11:49:00 +0000661void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
662 Register scratch,
663 Label* miss) {
664 Label same_contexts;
665
666 ASSERT(!holder_reg.is(scratch));
667
668 // Load current lexical context from the stack frame.
669 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
670
671 // When generating debug code, make sure the lexical context is set.
Steve Block44f0eee2011-05-26 01:26:41 +0100672 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000673 cmp(Operand(scratch), Immediate(0));
674 Check(not_equal, "we should not have an empty lexical context");
675 }
676 // Load the global context of the current context.
677 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
678 mov(scratch, FieldOperand(scratch, offset));
679 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
680
681 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100682 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000683 push(scratch);
684 // Read the first word and compare to global_context_map.
685 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100686 cmp(scratch, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000687 Check(equal, "JSGlobalObject::global_context should be a global context.");
688 pop(scratch);
689 }
690
691 // Check if both contexts are the same.
692 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000693 j(equal, &same_contexts);
Steve Blocka7e24c12009-10-30 11:49:00 +0000694
695 // Compare security tokens, save holder_reg on the stack so we can use it
696 // as a temporary register.
697 //
698 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
699 push(holder_reg);
700 // Check that the security token in the calling global object is
701 // compatible with the security token in the receiving global
702 // object.
703 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
704
705 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +0100706 if (emit_debug_code()) {
707 cmp(holder_reg, isolate()->factory()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 Check(not_equal, "JSGlobalProxy::context() should not be null.");
709
710 push(holder_reg);
711 // Read the first word and compare to global_context_map(),
712 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100713 cmp(holder_reg, isolate()->factory()->global_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000714 Check(equal, "JSGlobalObject::global_context should be a global context.");
715 pop(holder_reg);
716 }
717
718 int token_offset = Context::kHeaderSize +
719 Context::SECURITY_TOKEN_INDEX * kPointerSize;
720 mov(scratch, FieldOperand(scratch, token_offset));
721 cmp(scratch, FieldOperand(holder_reg, token_offset));
722 pop(holder_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +0000723 j(not_equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +0000724
725 bind(&same_contexts);
726}
727
728
729void MacroAssembler::LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 Register scratch,
731 AllocationFlags flags) {
732 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100733 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000734
735 // Just return if allocation top is already known.
736 if ((flags & RESULT_CONTAINS_TOP) != 0) {
737 // No use of scratch if allocation top is provided.
738 ASSERT(scratch.is(no_reg));
739#ifdef DEBUG
740 // Assert that result actually contains top on entry.
741 cmp(result, Operand::StaticVariable(new_space_allocation_top));
742 Check(equal, "Unexpected allocation top");
743#endif
744 return;
745 }
746
747 // Move address of new object to result. Use scratch register if available.
748 if (scratch.is(no_reg)) {
749 mov(result, Operand::StaticVariable(new_space_allocation_top));
750 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000751 mov(Operand(scratch), Immediate(new_space_allocation_top));
752 mov(result, Operand(scratch, 0));
753 }
754}
755
756
757void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
758 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +0100759 if (emit_debug_code()) {
Steve Blockd0582a62009-12-15 09:54:21 +0000760 test(result_end, Immediate(kObjectAlignmentMask));
761 Check(zero, "Unaligned allocation in new space");
762 }
763
Steve Blocka7e24c12009-10-30 11:49:00 +0000764 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100765 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000766
767 // Update new top. Use scratch if available.
768 if (scratch.is(no_reg)) {
769 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
770 } else {
771 mov(Operand(scratch, 0), result_end);
772 }
773}
774
775
776void MacroAssembler::AllocateInNewSpace(int 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 if (result_end.is_valid()) {
787 mov(result_end, Immediate(0x7191));
788 }
789 if (scratch.is_valid()) {
790 mov(scratch, Immediate(0x7291));
791 }
792 }
793 jmp(gc_required);
794 return;
795 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 ASSERT(!result.is(result_end));
797
798 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800799 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000800
Ben Murdochbb769b22010-08-11 14:56:33 +0100801 Register top_reg = result_end.is_valid() ? result_end : result;
802
Steve Blocka7e24c12009-10-30 11:49:00 +0000803 // Calculate new top and bail out if new space is exhausted.
804 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100805 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochbb769b22010-08-11 14:56:33 +0100806
Steve Block1e0659c2011-05-24 12:43:12 +0100807 if (!top_reg.is(result)) {
808 mov(top_reg, result);
Ben Murdochbb769b22010-08-11 14:56:33 +0100809 }
Steve Block1e0659c2011-05-24 12:43:12 +0100810 add(Operand(top_reg), Immediate(object_size));
Ben Murdoch257744e2011-11-30 15:57:28 +0000811 j(carry, gc_required);
Ben Murdochbb769b22010-08-11 14:56:33 +0100812 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +0000813 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000814
Leon Clarkee46be812010-01-19 14:06:41 +0000815 // Update allocation top.
Ben Murdochbb769b22010-08-11 14:56:33 +0100816 UpdateAllocationTopHelper(top_reg, scratch);
817
818 // Tag result if requested.
819 if (top_reg.is(result)) {
820 if ((flags & TAG_OBJECT) != 0) {
821 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
822 } else {
823 sub(Operand(result), Immediate(object_size));
824 }
825 } else if ((flags & TAG_OBJECT) != 0) {
826 add(Operand(result), Immediate(kHeapObjectTag));
827 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000828}
829
830
831void MacroAssembler::AllocateInNewSpace(int header_size,
832 ScaleFactor element_size,
833 Register element_count,
834 Register result,
835 Register result_end,
836 Register scratch,
837 Label* gc_required,
838 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700839 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100840 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700841 // Trash the registers to simulate an allocation failure.
842 mov(result, Immediate(0x7091));
843 mov(result_end, Immediate(0x7191));
844 if (scratch.is_valid()) {
845 mov(scratch, Immediate(0x7291));
846 }
847 // Register element_count is not modified by the function.
848 }
849 jmp(gc_required);
850 return;
851 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000852 ASSERT(!result.is(result_end));
853
854 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800855 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000856
857 // Calculate new top and bail out if new space is exhausted.
858 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100859 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100860
861 // We assume that element_count*element_size + header_size does not
862 // overflow.
863 lea(result_end, Operand(element_count, element_size, header_size));
864 add(result_end, Operand(result));
865 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
867 j(above, gc_required);
868
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 // Tag result if requested.
870 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000871 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 }
Leon Clarkee46be812010-01-19 14:06:41 +0000873
874 // Update allocation top.
875 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000876}
877
878
879void MacroAssembler::AllocateInNewSpace(Register object_size,
880 Register result,
881 Register result_end,
882 Register scratch,
883 Label* gc_required,
884 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -0700885 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +0100886 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -0700887 // Trash the registers to simulate an allocation failure.
888 mov(result, Immediate(0x7091));
889 mov(result_end, Immediate(0x7191));
890 if (scratch.is_valid()) {
891 mov(scratch, Immediate(0x7291));
892 }
893 // object_size is left unchanged by this function.
894 }
895 jmp(gc_required);
896 return;
897 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000898 ASSERT(!result.is(result_end));
899
900 // Load address of new object into result.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800901 LoadAllocationTopHelper(result, scratch, flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000902
903 // Calculate new top and bail out if new space is exhausted.
904 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +0100905 ExternalReference::new_space_allocation_limit_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 if (!object_size.is(result_end)) {
907 mov(result_end, object_size);
908 }
909 add(result_end, Operand(result));
Ben Murdoch257744e2011-11-30 15:57:28 +0000910 j(carry, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000911 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +0000912 j(above, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000913
Steve Blocka7e24c12009-10-30 11:49:00 +0000914 // Tag result if requested.
915 if ((flags & TAG_OBJECT) != 0) {
Leon Clarkee46be812010-01-19 14:06:41 +0000916 lea(result, Operand(result, kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000917 }
Leon Clarkee46be812010-01-19 14:06:41 +0000918
919 // Update allocation top.
920 UpdateAllocationTopHelper(result_end, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000921}
922
923
924void MacroAssembler::UndoAllocationInNewSpace(Register object) {
925 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +0100926 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000927
928 // Make sure the object has no tag before resetting top.
929 and_(Operand(object), Immediate(~kHeapObjectTagMask));
930#ifdef DEBUG
931 cmp(object, Operand::StaticVariable(new_space_allocation_top));
932 Check(below, "Undo allocation of non allocated memory");
933#endif
934 mov(Operand::StaticVariable(new_space_allocation_top), object);
935}
936
937
Steve Block3ce2e202009-11-05 08:53:23 +0000938void MacroAssembler::AllocateHeapNumber(Register result,
939 Register scratch1,
940 Register scratch2,
941 Label* gc_required) {
942 // Allocate heap number in new space.
943 AllocateInNewSpace(HeapNumber::kSize,
944 result,
945 scratch1,
946 scratch2,
947 gc_required,
948 TAG_OBJECT);
949
950 // Set the map.
951 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100952 Immediate(isolate()->factory()->heap_number_map()));
Steve Block3ce2e202009-11-05 08:53:23 +0000953}
954
955
Steve Blockd0582a62009-12-15 09:54:21 +0000956void MacroAssembler::AllocateTwoByteString(Register result,
957 Register length,
958 Register scratch1,
959 Register scratch2,
960 Register scratch3,
961 Label* gc_required) {
962 // Calculate the number of bytes needed for the characters in the string while
963 // observing object alignment.
964 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Steve Blockd0582a62009-12-15 09:54:21 +0000965 ASSERT(kShortSize == 2);
Leon Clarkee46be812010-01-19 14:06:41 +0000966 // scratch1 = length * 2 + kObjectAlignmentMask.
967 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +0000968 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
969
970 // Allocate two byte string in new space.
971 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
972 times_1,
973 scratch1,
974 result,
975 scratch2,
976 scratch3,
977 gc_required,
978 TAG_OBJECT);
979
980 // Set the map, length and hash field.
981 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +0100982 Immediate(isolate()->factory()->string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +0100983 mov(scratch1, length);
984 SmiTag(scratch1);
985 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +0000986 mov(FieldOperand(result, String::kHashFieldOffset),
987 Immediate(String::kEmptyHashField));
988}
989
990
991void MacroAssembler::AllocateAsciiString(Register result,
992 Register length,
993 Register scratch1,
994 Register scratch2,
995 Register scratch3,
996 Label* gc_required) {
997 // Calculate the number of bytes needed for the characters in the string while
998 // observing object alignment.
999 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1000 mov(scratch1, length);
1001 ASSERT(kCharSize == 1);
1002 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
1003 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
1004
1005 // Allocate ascii string in new space.
1006 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
1007 times_1,
1008 scratch1,
1009 result,
1010 scratch2,
1011 scratch3,
1012 gc_required,
1013 TAG_OBJECT);
1014
1015 // Set the map, length and hash field.
1016 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001017 Immediate(isolate()->factory()->ascii_string_map()));
Steve Block6ded16b2010-05-10 14:33:55 +01001018 mov(scratch1, length);
1019 SmiTag(scratch1);
1020 mov(FieldOperand(result, String::kLengthOffset), scratch1);
Steve Blockd0582a62009-12-15 09:54:21 +00001021 mov(FieldOperand(result, String::kHashFieldOffset),
1022 Immediate(String::kEmptyHashField));
1023}
1024
1025
Iain Merrick9ac36c92010-09-13 15:29:50 +01001026void MacroAssembler::AllocateAsciiString(Register result,
1027 int length,
1028 Register scratch1,
1029 Register scratch2,
1030 Label* gc_required) {
1031 ASSERT(length > 0);
1032
1033 // Allocate ascii string in new space.
1034 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
1035 result,
1036 scratch1,
1037 scratch2,
1038 gc_required,
1039 TAG_OBJECT);
1040
1041 // Set the map, length and hash field.
1042 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001043 Immediate(isolate()->factory()->ascii_string_map()));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001044 mov(FieldOperand(result, String::kLengthOffset),
1045 Immediate(Smi::FromInt(length)));
1046 mov(FieldOperand(result, String::kHashFieldOffset),
1047 Immediate(String::kEmptyHashField));
1048}
1049
1050
Steve Blockd0582a62009-12-15 09:54:21 +00001051void MacroAssembler::AllocateConsString(Register result,
1052 Register scratch1,
1053 Register scratch2,
1054 Label* gc_required) {
1055 // Allocate heap number in new space.
1056 AllocateInNewSpace(ConsString::kSize,
1057 result,
1058 scratch1,
1059 scratch2,
1060 gc_required,
1061 TAG_OBJECT);
1062
1063 // Set the map. The other fields are left uninitialized.
1064 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001065 Immediate(isolate()->factory()->cons_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001066}
1067
1068
1069void MacroAssembler::AllocateAsciiConsString(Register result,
1070 Register scratch1,
1071 Register scratch2,
1072 Label* gc_required) {
1073 // Allocate heap number in new space.
1074 AllocateInNewSpace(ConsString::kSize,
1075 result,
1076 scratch1,
1077 scratch2,
1078 gc_required,
1079 TAG_OBJECT);
1080
1081 // Set the map. The other fields are left uninitialized.
1082 mov(FieldOperand(result, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001083 Immediate(isolate()->factory()->cons_ascii_string_map()));
Steve Blockd0582a62009-12-15 09:54:21 +00001084}
1085
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001086
Ben Murdochb8e0da22011-05-16 14:20:40 +01001087// Copy memory, byte-by-byte, from source to destination. Not optimized for
1088// long or aligned copies. The contents of scratch and length are destroyed.
1089// Source and destination are incremented by length.
1090// Many variants of movsb, loop unrolling, word moves, and indexed operands
1091// have been tried here already, and this is fastest.
1092// A simpler loop is faster on small copies, but 30% slower on large ones.
1093// The cld() instruction must have been emitted, to set the direction flag(),
1094// before calling this function.
1095void MacroAssembler::CopyBytes(Register source,
1096 Register destination,
1097 Register length,
1098 Register scratch) {
1099 Label loop, done, short_string, short_loop;
1100 // Experimentation shows that the short string loop is faster if length < 10.
1101 cmp(Operand(length), Immediate(10));
1102 j(less_equal, &short_string);
1103
1104 ASSERT(source.is(esi));
1105 ASSERT(destination.is(edi));
1106 ASSERT(length.is(ecx));
1107
1108 // Because source is 4-byte aligned in our uses of this function,
1109 // we keep source aligned for the rep_movs call by copying the odd bytes
1110 // at the end of the ranges.
1111 mov(scratch, Operand(source, length, times_1, -4));
1112 mov(Operand(destination, length, times_1, -4), scratch);
1113 mov(scratch, ecx);
1114 shr(ecx, 2);
1115 rep_movs();
1116 and_(Operand(scratch), Immediate(0x3));
1117 add(destination, Operand(scratch));
1118 jmp(&done);
1119
1120 bind(&short_string);
1121 test(length, Operand(length));
1122 j(zero, &done);
1123
1124 bind(&short_loop);
1125 mov_b(scratch, Operand(source, 0));
1126 mov_b(Operand(destination, 0), scratch);
1127 inc(source);
1128 inc(destination);
1129 dec(length);
1130 j(not_zero, &short_loop);
1131
1132 bind(&done);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001133}
1134
Steve Blockd0582a62009-12-15 09:54:21 +00001135
Steve Blocka7e24c12009-10-30 11:49:00 +00001136void MacroAssembler::NegativeZeroTest(Register result,
1137 Register op,
1138 Label* then_label) {
1139 Label ok;
1140 test(result, Operand(result));
Ben Murdoch257744e2011-11-30 15:57:28 +00001141 j(not_zero, &ok);
Steve Blocka7e24c12009-10-30 11:49:00 +00001142 test(op, Operand(op));
Ben Murdoch257744e2011-11-30 15:57:28 +00001143 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001144 bind(&ok);
1145}
1146
1147
1148void MacroAssembler::NegativeZeroTest(Register result,
1149 Register op1,
1150 Register op2,
1151 Register scratch,
1152 Label* then_label) {
1153 Label ok;
1154 test(result, Operand(result));
Ben Murdoch257744e2011-11-30 15:57:28 +00001155 j(not_zero, &ok);
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 mov(scratch, Operand(op1));
1157 or_(scratch, Operand(op2));
Ben Murdoch257744e2011-11-30 15:57:28 +00001158 j(sign, then_label);
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 bind(&ok);
1160}
1161
1162
1163void MacroAssembler::TryGetFunctionPrototype(Register function,
1164 Register result,
1165 Register scratch,
1166 Label* miss) {
1167 // Check that the receiver isn't a smi.
1168 test(function, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00001169 j(zero, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001170
1171 // Check that the function really is a function.
1172 CmpObjectType(function, JS_FUNCTION_TYPE, result);
Ben Murdoch257744e2011-11-30 15:57:28 +00001173 j(not_equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001174
1175 // Make sure that the function has an instance prototype.
1176 Label non_instance;
1177 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1178 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
Ben Murdoch257744e2011-11-30 15:57:28 +00001179 j(not_zero, &non_instance);
Steve Blocka7e24c12009-10-30 11:49:00 +00001180
1181 // Get the prototype or initial map from the function.
1182 mov(result,
1183 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1184
1185 // If the prototype or initial map is the hole, don't return it and
1186 // simply miss the cache instead. This will allow us to allocate a
1187 // prototype object on-demand in the runtime system.
Steve Block44f0eee2011-05-26 01:26:41 +01001188 cmp(Operand(result), Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001189 j(equal, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001190
1191 // If the function does not have an initial map, we're done.
1192 Label done;
1193 CmpObjectType(result, MAP_TYPE, scratch);
1194 j(not_equal, &done);
1195
1196 // Get the prototype from the initial map.
1197 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1198 jmp(&done);
1199
1200 // Non-instance prototype: Fetch prototype from constructor field
1201 // in initial map.
1202 bind(&non_instance);
1203 mov(result, FieldOperand(result, Map::kConstructorOffset));
1204
1205 // All done.
1206 bind(&done);
1207}
1208
1209
Ben Murdoch257744e2011-11-30 15:57:28 +00001210void MacroAssembler::CallStub(CodeStub* stub, unsigned ast_id) {
Leon Clarkee46be812010-01-19 14:06:41 +00001211 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Ben Murdoch257744e2011-11-30 15:57:28 +00001212 call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001213}
1214
1215
John Reck59135872010-11-02 12:39:01 -07001216MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001217 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001218 Object* result;
1219 { MaybeObject* maybe_result = stub->TryGetCode();
1220 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001221 }
John Reck59135872010-11-02 12:39:01 -07001222 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001223 return result;
1224}
1225
1226
Steve Blockd0582a62009-12-15 09:54:21 +00001227void MacroAssembler::TailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001228 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
Steve Blockd0582a62009-12-15 09:54:21 +00001229 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1230}
1231
1232
John Reck59135872010-11-02 12:39:01 -07001233MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
Leon Clarkee46be812010-01-19 14:06:41 +00001234 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
John Reck59135872010-11-02 12:39:01 -07001235 Object* result;
1236 { MaybeObject* maybe_result = stub->TryGetCode();
1237 if (!maybe_result->ToObject(&result)) return maybe_result;
Leon Clarkee46be812010-01-19 14:06:41 +00001238 }
John Reck59135872010-11-02 12:39:01 -07001239 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
Leon Clarkee46be812010-01-19 14:06:41 +00001240 return result;
1241}
1242
1243
Steve Blocka7e24c12009-10-30 11:49:00 +00001244void MacroAssembler::StubReturn(int argc) {
1245 ASSERT(argc >= 1 && generating_stub());
1246 ret((argc - 1) * kPointerSize);
1247}
1248
1249
1250void MacroAssembler::IllegalOperation(int num_arguments) {
1251 if (num_arguments > 0) {
1252 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1253 }
Steve Block44f0eee2011-05-26 01:26:41 +01001254 mov(eax, Immediate(isolate()->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001255}
1256
1257
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001258void MacroAssembler::IndexFromHash(Register hash, Register index) {
1259 // The assert checks that the constants for the maximum number of digits
1260 // for an array index cached in the hash field and the number of bits
1261 // reserved for it does not conflict.
1262 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1263 (1 << String::kArrayIndexValueBits));
1264 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1265 // the low kHashShift bits.
1266 and_(hash, String::kArrayIndexValueMask);
1267 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1268 if (String::kHashShift > kSmiTagSize) {
1269 shr(hash, String::kHashShift - kSmiTagSize);
1270 }
1271 if (!index.is(hash)) {
1272 mov(index, hash);
1273 }
1274}
1275
1276
Steve Blocka7e24c12009-10-30 11:49:00 +00001277void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1278 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1279}
1280
1281
Ben Murdochb0fe1622011-05-05 13:52:32 +01001282void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01001283 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001284 Set(eax, Immediate(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01001285 mov(ebx, Immediate(ExternalReference(function, isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001286 CEntryStub ces(1);
1287 ces.SaveDoubles();
1288 CallStub(&ces);
1289}
1290
1291
John Reck59135872010-11-02 12:39:01 -07001292MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1293 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001294 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1295}
1296
1297
Steve Block44f0eee2011-05-26 01:26:41 +01001298void MacroAssembler::CallRuntime(const Runtime::Function* f,
1299 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001300 // If the expected number of arguments of the runtime function is
1301 // constant, we check that the actual number of arguments match the
1302 // expectation.
1303 if (f->nargs >= 0 && f->nargs != num_arguments) {
1304 IllegalOperation(num_arguments);
1305 return;
1306 }
1307
Leon Clarke4515c472010-02-03 11:58:03 +00001308 // TODO(1236192): Most runtime routines don't need the number of
1309 // arguments passed in because it is constant. At some point we
1310 // should remove this need and make the runtime routine entry code
1311 // smarter.
1312 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001313 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001314 CEntryStub ces(1);
1315 CallStub(&ces);
Steve Blocka7e24c12009-10-30 11:49:00 +00001316}
1317
1318
Steve Block44f0eee2011-05-26 01:26:41 +01001319MaybeObject* MacroAssembler::TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -07001320 int num_arguments) {
Leon Clarkee46be812010-01-19 14:06:41 +00001321 if (f->nargs >= 0 && f->nargs != num_arguments) {
1322 IllegalOperation(num_arguments);
1323 // Since we did not call the stub, there was no allocation failure.
1324 // Return some non-failure object.
Steve Block44f0eee2011-05-26 01:26:41 +01001325 return isolate()->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +00001326 }
1327
Leon Clarke4515c472010-02-03 11:58:03 +00001328 // TODO(1236192): Most runtime routines don't need the number of
1329 // arguments passed in because it is constant. At some point we
1330 // should remove this need and make the runtime routine entry code
1331 // smarter.
1332 Set(eax, Immediate(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01001333 mov(ebx, Immediate(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00001334 CEntryStub ces(1);
1335 return TryCallStub(&ces);
Leon Clarkee46be812010-01-19 14:06:41 +00001336}
1337
1338
Ben Murdochbb769b22010-08-11 14:56:33 +01001339void MacroAssembler::CallExternalReference(ExternalReference ref,
1340 int num_arguments) {
1341 mov(eax, Immediate(num_arguments));
1342 mov(ebx, Immediate(ref));
1343
1344 CEntryStub stub(1);
1345 CallStub(&stub);
1346}
1347
1348
Steve Block6ded16b2010-05-10 14:33:55 +01001349void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1350 int num_arguments,
1351 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001352 // TODO(1236192): Most runtime routines don't need the number of
1353 // arguments passed in because it is constant. At some point we
1354 // should remove this need and make the runtime routine entry code
1355 // smarter.
1356 Set(eax, Immediate(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001357 JumpToExternalReference(ext);
1358}
1359
1360
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001361MaybeObject* MacroAssembler::TryTailCallExternalReference(
1362 const ExternalReference& ext, int num_arguments, int result_size) {
1363 // TODO(1236192): Most runtime routines don't need the number of
1364 // arguments passed in because it is constant. At some point we
1365 // should remove this need and make the runtime routine entry code
1366 // smarter.
1367 Set(eax, Immediate(num_arguments));
1368 return TryJumpToExternalReference(ext);
1369}
1370
1371
Steve Block6ded16b2010-05-10 14:33:55 +01001372void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1373 int num_arguments,
1374 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01001375 TailCallExternalReference(ExternalReference(fid, isolate()),
1376 num_arguments,
1377 result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001378}
1379
1380
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001381MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1382 int num_arguments,
1383 int result_size) {
1384 return TryTailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001385 ExternalReference(fid, isolate()), num_arguments, result_size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001386}
1387
1388
Ben Murdochb0fe1622011-05-05 13:52:32 +01001389// If true, a Handle<T> returned by value from a function with cdecl calling
1390// convention will be returned directly as a value of location_ field in a
1391// register eax.
1392// If false, it is returned as a pointer to a preallocated by caller memory
1393// region. Pointer to this region should be passed to a function as an
1394// implicit first argument.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001395#if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
Ben Murdochb0fe1622011-05-05 13:52:32 +01001396static const bool kReturnHandlesDirectly = true;
John Reck59135872010-11-02 12:39:01 -07001397#else
Ben Murdochb0fe1622011-05-05 13:52:32 +01001398static const bool kReturnHandlesDirectly = false;
John Reck59135872010-11-02 12:39:01 -07001399#endif
1400
1401
1402Operand ApiParameterOperand(int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001403 return Operand(
1404 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001405}
1406
1407
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001408void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001409 if (kReturnHandlesDirectly) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001410 EnterApiExitFrame(argc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001411 // When handles are returned directly we don't have to allocate extra
John Reck59135872010-11-02 12:39:01 -07001412 // space for and pass an out parameter.
1413 } else {
1414 // We allocate two additional slots: return value and pointer to it.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001415 EnterApiExitFrame(argc + 2);
John Reck59135872010-11-02 12:39:01 -07001416
John Reck59135872010-11-02 12:39:01 -07001417 // The argument slots are filled as follows:
1418 //
1419 // n + 1: output cell
1420 // n: arg n
1421 // ...
1422 // 1: arg1
1423 // 0: pointer to the output cell
1424 //
1425 // Note that this is one more "argument" than the function expects
1426 // so the out cell will have to be popped explicitly after returning
1427 // from the function. The out cell contains Handle.
John Reck59135872010-11-02 12:39:01 -07001428
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001429 // pointer to out cell.
1430 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1431 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
Steve Block44f0eee2011-05-26 01:26:41 +01001432 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001433 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1434 }
1435 }
1436}
1437
1438
1439MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1440 int stack_space) {
Steve Blockd0582a62009-12-15 09:54:21 +00001441 ExternalReference next_address =
1442 ExternalReference::handle_scope_next_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001443 ExternalReference limit_address =
1444 ExternalReference::handle_scope_limit_address();
John Reck59135872010-11-02 12:39:01 -07001445 ExternalReference level_address =
1446 ExternalReference::handle_scope_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +00001447
John Reck59135872010-11-02 12:39:01 -07001448 // Allocate HandleScope in callee-save registers.
1449 mov(ebx, Operand::StaticVariable(next_address));
1450 mov(edi, Operand::StaticVariable(limit_address));
1451 add(Operand::StaticVariable(level_address), Immediate(1));
Steve Blockd0582a62009-12-15 09:54:21 +00001452
John Reck59135872010-11-02 12:39:01 -07001453 // Call the api function!
1454 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1455
Ben Murdochb0fe1622011-05-05 13:52:32 +01001456 if (!kReturnHandlesDirectly) {
John Reck59135872010-11-02 12:39:01 -07001457 // The returned value is a pointer to the handle holding the result.
1458 // Dereference this to get to the location.
1459 mov(eax, Operand(eax, 0));
Leon Clarkee46be812010-01-19 14:06:41 +00001460 }
Steve Blockd0582a62009-12-15 09:54:21 +00001461
John Reck59135872010-11-02 12:39:01 -07001462 Label empty_handle;
1463 Label prologue;
1464 Label promote_scheduled_exception;
1465 Label delete_allocated_handles;
1466 Label leave_exit_frame;
Leon Clarkee46be812010-01-19 14:06:41 +00001467
John Reck59135872010-11-02 12:39:01 -07001468 // Check if the result handle holds 0.
1469 test(eax, Operand(eax));
Ben Murdoch257744e2011-11-30 15:57:28 +00001470 j(zero, &empty_handle);
John Reck59135872010-11-02 12:39:01 -07001471 // It was non-zero. Dereference to get the result value.
1472 mov(eax, Operand(eax, 0));
1473 bind(&prologue);
1474 // No more valid handles (the result handle was the last one). Restore
1475 // previous handle scope.
1476 mov(Operand::StaticVariable(next_address), ebx);
1477 sub(Operand::StaticVariable(level_address), Immediate(1));
1478 Assert(above_equal, "Invalid HandleScope level");
1479 cmp(edi, Operand::StaticVariable(limit_address));
Ben Murdoch257744e2011-11-30 15:57:28 +00001480 j(not_equal, &delete_allocated_handles);
John Reck59135872010-11-02 12:39:01 -07001481 bind(&leave_exit_frame);
Leon Clarkee46be812010-01-19 14:06:41 +00001482
John Reck59135872010-11-02 12:39:01 -07001483 // Check if the function scheduled an exception.
1484 ExternalReference scheduled_exception_address =
Steve Block44f0eee2011-05-26 01:26:41 +01001485 ExternalReference::scheduled_exception_address(isolate());
John Reck59135872010-11-02 12:39:01 -07001486 cmp(Operand::StaticVariable(scheduled_exception_address),
Steve Block44f0eee2011-05-26 01:26:41 +01001487 Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001488 j(not_equal, &promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001489 LeaveApiExitFrame();
1490 ret(stack_space * kPointerSize);
John Reck59135872010-11-02 12:39:01 -07001491 bind(&promote_scheduled_exception);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001492 MaybeObject* result =
1493 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1494 if (result->IsFailure()) {
1495 return result;
1496 }
John Reck59135872010-11-02 12:39:01 -07001497 bind(&empty_handle);
1498 // It was zero; the result is undefined.
Steve Block44f0eee2011-05-26 01:26:41 +01001499 mov(eax, isolate()->factory()->undefined_value());
John Reck59135872010-11-02 12:39:01 -07001500 jmp(&prologue);
Leon Clarkee46be812010-01-19 14:06:41 +00001501
John Reck59135872010-11-02 12:39:01 -07001502 // HandleScope limit has changed. Delete allocated extensions.
Steve Block44f0eee2011-05-26 01:26:41 +01001503 ExternalReference delete_extensions =
1504 ExternalReference::delete_handle_scope_extensions(isolate());
John Reck59135872010-11-02 12:39:01 -07001505 bind(&delete_allocated_handles);
1506 mov(Operand::StaticVariable(limit_address), edi);
1507 mov(edi, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001508 mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
1509 mov(eax, Immediate(delete_extensions));
John Reck59135872010-11-02 12:39:01 -07001510 call(Operand(eax));
1511 mov(eax, edi);
1512 jmp(&leave_exit_frame);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001513
1514 return result;
Steve Blockd0582a62009-12-15 09:54:21 +00001515}
1516
1517
Steve Block6ded16b2010-05-10 14:33:55 +01001518void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001519 // Set the entry point and jump to the C entry runtime stub.
1520 mov(ebx, Immediate(ext));
1521 CEntryStub ces(1);
1522 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1523}
1524
1525
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001526MaybeObject* MacroAssembler::TryJumpToExternalReference(
1527 const ExternalReference& ext) {
1528 // Set the entry point and jump to the C entry runtime stub.
1529 mov(ebx, Immediate(ext));
1530 CEntryStub ces(1);
1531 return TryTailCallStub(&ces);
1532}
1533
1534
Ben Murdoch257744e2011-11-30 15:57:28 +00001535void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
1536 // This macro takes the dst register to make the code more readable
1537 // at the call sites. However, the dst register has to be ecx to
1538 // follow the calling convention which requires the call type to be
1539 // in ecx.
1540 ASSERT(dst.is(ecx));
1541 if (call_kind == CALL_AS_FUNCTION) {
1542 // Set to some non-zero smi by updating the least significant
1543 // byte.
1544 mov_b(Operand(dst), 1 << kSmiTagSize);
1545 } else {
1546 // Set to smi zero by clearing the register.
1547 xor_(dst, Operand(dst));
1548 }
1549}
1550
1551
Steve Blocka7e24c12009-10-30 11:49:00 +00001552void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1553 const ParameterCount& actual,
1554 Handle<Code> code_constant,
1555 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +00001556 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001557 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001558 Label::Distance done_near,
1559 const CallWrapper& call_wrapper,
1560 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001561 bool definitely_matches = false;
1562 Label invoke;
1563 if (expected.is_immediate()) {
1564 ASSERT(actual.is_immediate());
1565 if (expected.immediate() == actual.immediate()) {
1566 definitely_matches = true;
1567 } else {
1568 mov(eax, actual.immediate());
1569 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1570 if (expected.immediate() == sentinel) {
1571 // Don't worry about adapting arguments for builtins that
1572 // don't want that done. Skip adaption code by making it look
1573 // like we have a match between expected and actual number of
1574 // arguments.
1575 definitely_matches = true;
1576 } else {
1577 mov(ebx, expected.immediate());
1578 }
1579 }
1580 } else {
1581 if (actual.is_immediate()) {
1582 // Expected is in register, actual is immediate. This is the
1583 // case when we invoke function values without going through the
1584 // IC mechanism.
1585 cmp(expected.reg(), actual.immediate());
1586 j(equal, &invoke);
1587 ASSERT(expected.reg().is(ebx));
1588 mov(eax, actual.immediate());
1589 } else if (!expected.reg().is(actual.reg())) {
1590 // Both expected and actual are in (different) registers. This
1591 // is the case when we invoke functions using call and apply.
1592 cmp(expected.reg(), Operand(actual.reg()));
1593 j(equal, &invoke);
1594 ASSERT(actual.reg().is(eax));
1595 ASSERT(expected.reg().is(ebx));
1596 }
1597 }
1598
1599 if (!definitely_matches) {
1600 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001601 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001602 if (!code_constant.is_null()) {
1603 mov(edx, Immediate(code_constant));
1604 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1605 } else if (!code_operand.is_reg(edx)) {
1606 mov(edx, code_operand);
1607 }
1608
1609 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001610 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
1611 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001612 call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdoch257744e2011-11-30 15:57:28 +00001613 call_wrapper.AfterCall();
1614 jmp(done, done_near);
Steve Blocka7e24c12009-10-30 11:49:00 +00001615 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001616 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001617 jmp(adaptor, RelocInfo::CODE_TARGET);
1618 }
1619 bind(&invoke);
1620 }
1621}
1622
1623
1624void MacroAssembler::InvokeCode(const Operand& code,
1625 const ParameterCount& expected,
1626 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001627 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001628 const CallWrapper& call_wrapper,
1629 CallKind call_kind) {
1630 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001631 InvokePrologue(expected, actual, Handle<Code>::null(), code,
Ben Murdoch257744e2011-11-30 15:57:28 +00001632 &done, flag, Label::kNear, call_wrapper,
1633 call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001634 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001635 call_wrapper.BeforeCall(CallSize(code));
1636 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001637 call(code);
Ben Murdoch257744e2011-11-30 15:57:28 +00001638 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00001639 } else {
1640 ASSERT(flag == JUMP_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00001641 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001642 jmp(code);
1643 }
1644 bind(&done);
1645}
1646
1647
1648void MacroAssembler::InvokeCode(Handle<Code> code,
1649 const ParameterCount& expected,
1650 const ParameterCount& actual,
1651 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001652 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001653 const CallWrapper& call_wrapper,
1654 CallKind call_kind) {
1655 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +00001656 Operand dummy(eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00001657 InvokePrologue(expected, actual, code, dummy, &done, flag, Label::kNear,
1658 call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001659 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001660 call_wrapper.BeforeCall(CallSize(code, rmode));
1661 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001662 call(code, rmode);
Ben Murdoch257744e2011-11-30 15:57:28 +00001663 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00001664 } else {
1665 ASSERT(flag == JUMP_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00001666 SetCallKind(ecx, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001667 jmp(code, rmode);
1668 }
1669 bind(&done);
1670}
1671
1672
1673void MacroAssembler::InvokeFunction(Register fun,
1674 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001675 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001676 const CallWrapper& call_wrapper,
1677 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 ASSERT(fun.is(edi));
1679 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1680 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1681 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001682 SmiUntag(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001683
1684 ParameterCount expected(ebx);
Steve Block791712a2010-08-27 10:21:07 +01001685 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00001686 expected, actual, flag, call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001687}
1688
1689
Andrei Popescu402d9372010-02-26 13:31:12 +00001690void MacroAssembler::InvokeFunction(JSFunction* function,
1691 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001692 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001693 const CallWrapper& call_wrapper,
1694 CallKind call_kind) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001695 ASSERT(function->is_compiled());
1696 // Get the function and setup the context.
1697 mov(edi, Immediate(Handle<JSFunction>(function)));
1698 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001699
Andrei Popescu402d9372010-02-26 13:31:12 +00001700 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001701 if (V8::UseCrankshaft()) {
1702 // TODO(kasperl): For now, we always call indirectly through the
1703 // code field in the function to allow recompilation to take effect
1704 // without changing any of the call sites.
1705 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00001706 expected, actual, flag, call_wrapper, call_kind);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001707 } else {
1708 Handle<Code> code(function->code());
1709 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
Ben Murdoch257744e2011-11-30 15:57:28 +00001710 flag, call_wrapper, call_kind);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001711 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001712}
1713
1714
Ben Murdochb0fe1622011-05-05 13:52:32 +01001715void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1716 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001717 const CallWrapper& call_wrapper) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001718 // Calls are not allowed in some stubs.
1719 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
1720
1721 // Rely on the assertion to check that the number of provided
1722 // arguments match the expected number of arguments. Fake a
1723 // parameter count to avoid emitting code to do the check.
1724 ParameterCount expected(0);
Steve Block791712a2010-08-27 10:21:07 +01001725 GetBuiltinFunction(edi, id);
1726 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00001727 expected, expected, flag, call_wrapper, CALL_AS_METHOD);
Steve Blocka7e24c12009-10-30 11:49:00 +00001728}
1729
Steve Block791712a2010-08-27 10:21:07 +01001730void MacroAssembler::GetBuiltinFunction(Register target,
1731 Builtins::JavaScript id) {
1732 // Load the JavaScript builtin function from the builtins object.
1733 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1734 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1735 mov(target, FieldOperand(target,
1736 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1737}
Steve Blocka7e24c12009-10-30 11:49:00 +00001738
1739void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001740 ASSERT(!target.is(edi));
Andrei Popescu402d9372010-02-26 13:31:12 +00001741 // Load the JavaScript builtin function from the builtins object.
Steve Block791712a2010-08-27 10:21:07 +01001742 GetBuiltinFunction(edi, id);
1743 // Load the code entry point from the function into the target register.
1744 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001745}
1746
1747
Steve Blockd0582a62009-12-15 09:54:21 +00001748void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1749 if (context_chain_length > 0) {
1750 // Move up the chain of contexts to the context containing the slot.
1751 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1752 // Load the function context (which is the incoming, outer context).
1753 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1754 for (int i = 1; i < context_chain_length; i++) {
1755 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1756 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1757 }
Steve Block1e0659c2011-05-24 12:43:12 +01001758 } else {
1759 // Slot is in the current function context. Move it into the
1760 // destination register in case we store into it (the write barrier
1761 // cannot be allowed to destroy the context in esi).
1762 mov(dst, esi);
1763 }
1764
1765 // We should not have found a 'with' context by walking the context chain
1766 // (i.e., the static scope chain and runtime context chain do not agree).
1767 // A variable occurring in such a scope should have slot type LOOKUP and
1768 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01001769 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001770 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1771 Check(equal, "Yo dawg, I heard you liked function contexts "
1772 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00001773 }
1774}
1775
1776
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001777void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1778 // Load the global or builtins object from the current context.
1779 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1780 // Load the global context from the global or builtins object.
1781 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1782 // Load the function from the global context.
1783 mov(function, Operand(function, Context::SlotOffset(index)));
1784}
1785
1786
1787void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1788 Register map) {
1789 // Load the initial map. The global functions all have initial maps.
1790 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001791 if (emit_debug_code()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001792 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00001793 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001794 jmp(&ok);
1795 bind(&fail);
1796 Abort("Global functions must have initial map");
1797 bind(&ok);
1798 }
1799}
1800
Steve Blockd0582a62009-12-15 09:54:21 +00001801
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001802// Store the value in register src in the safepoint register stack
1803// slot for register dst.
1804void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
1805 mov(SafepointRegisterSlot(dst), src);
1806}
1807
1808
1809void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
1810 mov(SafepointRegisterSlot(dst), src);
1811}
1812
1813
1814void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
1815 mov(dst, SafepointRegisterSlot(src));
1816}
1817
1818
1819Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
1820 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
1821}
1822
1823
Ben Murdochb0fe1622011-05-05 13:52:32 +01001824int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1825 // The registers are pushed starting with the lowest encoding,
1826 // which means that lowest encodings are furthest away from
1827 // the stack pointer.
1828 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1829 return kNumSafepointRegisters - reg_code - 1;
1830}
1831
1832
Steve Blocka7e24c12009-10-30 11:49:00 +00001833void MacroAssembler::Ret() {
1834 ret(0);
1835}
1836
1837
Steve Block1e0659c2011-05-24 12:43:12 +01001838void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1839 if (is_uint16(bytes_dropped)) {
1840 ret(bytes_dropped);
1841 } else {
1842 pop(scratch);
1843 add(Operand(esp), Immediate(bytes_dropped));
1844 push(scratch);
1845 ret(0);
1846 }
1847}
1848
1849
1850
1851
Leon Clarkee46be812010-01-19 14:06:41 +00001852void MacroAssembler::Drop(int stack_elements) {
1853 if (stack_elements > 0) {
1854 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1855 }
1856}
1857
1858
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001859void MacroAssembler::Move(Register dst, Register src) {
1860 if (!dst.is(src)) {
1861 mov(dst, src);
1862 }
1863}
1864
1865
Leon Clarkee46be812010-01-19 14:06:41 +00001866void MacroAssembler::Move(Register dst, Handle<Object> value) {
1867 mov(dst, value);
1868}
1869
1870
Steve Blocka7e24c12009-10-30 11:49:00 +00001871void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1872 if (FLAG_native_code_counters && counter->Enabled()) {
1873 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1874 }
1875}
1876
1877
1878void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1879 ASSERT(value > 0);
1880 if (FLAG_native_code_counters && counter->Enabled()) {
1881 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1882 if (value == 1) {
1883 inc(operand);
1884 } else {
1885 add(operand, Immediate(value));
1886 }
1887 }
1888}
1889
1890
1891void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1892 ASSERT(value > 0);
1893 if (FLAG_native_code_counters && counter->Enabled()) {
1894 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1895 if (value == 1) {
1896 dec(operand);
1897 } else {
1898 sub(operand, Immediate(value));
1899 }
1900 }
1901}
1902
1903
Leon Clarked91b9f72010-01-27 17:25:45 +00001904void MacroAssembler::IncrementCounter(Condition cc,
1905 StatsCounter* counter,
1906 int value) {
1907 ASSERT(value > 0);
1908 if (FLAG_native_code_counters && counter->Enabled()) {
1909 Label skip;
1910 j(NegateCondition(cc), &skip);
1911 pushfd();
1912 IncrementCounter(counter, value);
1913 popfd();
1914 bind(&skip);
1915 }
1916}
1917
1918
1919void MacroAssembler::DecrementCounter(Condition cc,
1920 StatsCounter* counter,
1921 int value) {
1922 ASSERT(value > 0);
1923 if (FLAG_native_code_counters && counter->Enabled()) {
1924 Label skip;
1925 j(NegateCondition(cc), &skip);
1926 pushfd();
1927 DecrementCounter(counter, value);
1928 popfd();
1929 bind(&skip);
1930 }
1931}
1932
1933
Steve Blocka7e24c12009-10-30 11:49:00 +00001934void MacroAssembler::Assert(Condition cc, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01001935 if (emit_debug_code()) Check(cc, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00001936}
1937
1938
Iain Merrick75681382010-08-19 15:07:18 +01001939void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01001940 if (emit_debug_code()) {
1941 Factory* factory = isolate()->factory();
Iain Merrick75681382010-08-19 15:07:18 +01001942 Label ok;
1943 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001944 Immediate(factory->fixed_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001945 j(equal, &ok);
1946 cmp(FieldOperand(elements, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01001947 Immediate(factory->fixed_cow_array_map()));
Iain Merrick75681382010-08-19 15:07:18 +01001948 j(equal, &ok);
1949 Abort("JSObject with fast elements map has slow elements");
1950 bind(&ok);
1951 }
1952}
1953
1954
Steve Blocka7e24c12009-10-30 11:49:00 +00001955void MacroAssembler::Check(Condition cc, const char* msg) {
1956 Label L;
Ben Murdoch257744e2011-11-30 15:57:28 +00001957 j(cc, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00001958 Abort(msg);
1959 // will not return here
1960 bind(&L);
1961}
1962
1963
Steve Block6ded16b2010-05-10 14:33:55 +01001964void MacroAssembler::CheckStackAlignment() {
1965 int frame_alignment = OS::ActivationFrameAlignment();
1966 int frame_alignment_mask = frame_alignment - 1;
1967 if (frame_alignment > kPointerSize) {
1968 ASSERT(IsPowerOf2(frame_alignment));
1969 Label alignment_as_expected;
1970 test(esp, Immediate(frame_alignment_mask));
1971 j(zero, &alignment_as_expected);
1972 // Abort if stack is not aligned.
1973 int3();
1974 bind(&alignment_as_expected);
1975 }
1976}
1977
1978
Steve Blocka7e24c12009-10-30 11:49:00 +00001979void MacroAssembler::Abort(const char* msg) {
1980 // We want to pass the msg string like a smi to avoid GC
1981 // problems, however msg is not guaranteed to be aligned
1982 // properly. Instead, we pass an aligned pointer that is
1983 // a proper v8 smi, but also pass the alignment difference
1984 // from the real pointer as a smi.
1985 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1986 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1987 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1988#ifdef DEBUG
1989 if (msg != NULL) {
1990 RecordComment("Abort message: ");
1991 RecordComment(msg);
1992 }
1993#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001994 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001995 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001996
Steve Blocka7e24c12009-10-30 11:49:00 +00001997 push(eax);
1998 push(Immediate(p0));
1999 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
2000 CallRuntime(Runtime::kAbort, 2);
2001 // will not return here
Steve Blockd0582a62009-12-15 09:54:21 +00002002 int3();
Steve Blocka7e24c12009-10-30 11:49:00 +00002003}
2004
2005
Ben Murdoch257744e2011-11-30 15:57:28 +00002006void MacroAssembler::LoadInstanceDescriptors(Register map,
2007 Register descriptors) {
2008 mov(descriptors,
2009 FieldOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
2010 Label not_smi;
2011 JumpIfNotSmi(descriptors, &not_smi);
2012 mov(descriptors, isolate()->factory()->empty_descriptor_array());
2013 bind(&not_smi);
Iain Merrick75681382010-08-19 15:07:18 +01002014}
2015
2016
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002017void MacroAssembler::LoadPowerOf2(XMMRegister dst,
2018 Register scratch,
2019 int power) {
2020 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
2021 HeapNumber::kExponentBits));
2022 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
2023 movd(dst, Operand(scratch));
2024 psllq(dst, HeapNumber::kMantissaBits);
2025}
2026
2027
Andrei Popescu402d9372010-02-26 13:31:12 +00002028void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
2029 Register instance_type,
2030 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +01002031 Label* failure) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002032 if (!scratch.is(instance_type)) {
2033 mov(scratch, instance_type);
2034 }
2035 and_(scratch,
2036 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2037 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
2038 j(not_equal, failure);
2039}
2040
2041
Leon Clarked91b9f72010-01-27 17:25:45 +00002042void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
2043 Register object2,
2044 Register scratch1,
2045 Register scratch2,
2046 Label* failure) {
2047 // Check that both objects are not smis.
2048 ASSERT_EQ(0, kSmiTag);
2049 mov(scratch1, Operand(object1));
2050 and_(scratch1, Operand(object2));
2051 test(scratch1, Immediate(kSmiTagMask));
2052 j(zero, failure);
2053
2054 // Load instance type for both strings.
2055 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2056 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2057 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2058 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2059
2060 // Check that both are flat ascii strings.
2061 const int kFlatAsciiStringMask =
2062 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2063 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2064 // Interleave bits from both instance types and compare them in one check.
2065 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
2066 and_(scratch1, kFlatAsciiStringMask);
2067 and_(scratch2, kFlatAsciiStringMask);
2068 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
2069 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
2070 j(not_equal, failure);
2071}
2072
2073
Steve Block6ded16b2010-05-10 14:33:55 +01002074void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002075 int frame_alignment = OS::ActivationFrameAlignment();
2076 if (frame_alignment != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01002077 // Make stack end at alignment and make room for num_arguments words
2078 // and the original value of esp.
2079 mov(scratch, esp);
2080 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
Ben Murdoch8b112d22011-06-08 16:22:53 +01002081 ASSERT(IsPowerOf2(frame_alignment));
2082 and_(esp, -frame_alignment);
Steve Block6ded16b2010-05-10 14:33:55 +01002083 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2084 } else {
2085 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
2086 }
2087}
2088
2089
2090void MacroAssembler::CallCFunction(ExternalReference function,
2091 int num_arguments) {
2092 // Trashing eax is ok as it will be the return value.
2093 mov(Operand(eax), Immediate(function));
2094 CallCFunction(eax, num_arguments);
2095}
2096
2097
2098void MacroAssembler::CallCFunction(Register function,
2099 int num_arguments) {
2100 // Check stack alignment.
Steve Block44f0eee2011-05-26 01:26:41 +01002101 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002102 CheckStackAlignment();
2103 }
2104
2105 call(Operand(function));
2106 if (OS::ActivationFrameAlignment() != 0) {
2107 mov(esp, Operand(esp, num_arguments * kPointerSize));
2108 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002109 add(Operand(esp), Immediate(num_arguments * kPointerSize));
Steve Block6ded16b2010-05-10 14:33:55 +01002110 }
2111}
2112
2113
Steve Blocka7e24c12009-10-30 11:49:00 +00002114CodePatcher::CodePatcher(byte* address, int size)
Ben Murdoch8b112d22011-06-08 16:22:53 +01002115 : address_(address),
2116 size_(size),
2117 masm_(Isolate::Current(), address, size + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002118 // Create a new macro assembler pointing to the address of the code to patch.
2119 // The size is adjusted with kGap on order for the assembler to generate size
2120 // bytes of instructions without failing with buffer size constraints.
2121 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2122}
2123
2124
2125CodePatcher::~CodePatcher() {
2126 // Indicate that code has changed.
2127 CPU::FlushICache(address_, size_);
2128
2129 // Check that the code was patched as expected.
2130 ASSERT(masm_.pc_ == address_ + size_);
2131 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2132}
2133
2134
2135} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002136
2137#endif // V8_TARGET_ARCH_IA32