blob: db26c104257e890f10e7d4db7de74d7e0a571d47 [file] [log] [blame]
ager@chromium.orga1645e22009-09-09 19:27:10 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_IA32)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
34#include "debug.h"
35#include "runtime.h"
36#include "serialize.h"
37
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000041// -------------------------------------------------------------------------
42// MacroAssembler implementation.
43
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044MacroAssembler::MacroAssembler(void* buffer, int size)
45 : Assembler(buffer, size),
kasper.lund7276f142008-07-30 08:49:36 +000046 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000047 allow_stub_calls_(true),
48 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049}
50
51
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000052void MacroAssembler::RecordWriteHelper(Register object,
53 Register addr,
54 Register scratch) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000055 if (FLAG_debug_code) {
56 // Check that the object is not in new space.
57 Label not_in_new_space;
58 InNewSpace(object, scratch, not_equal, &not_in_new_space);
59 Abort("new-space object passed to RecordWriteHelper");
60 bind(&not_in_new_space);
61 }
62
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000063 // Compute the page start address from the heap object pointer, and reuse
64 // the 'object' register for it.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000065 and_(object, ~Page::kPageAlignmentMask);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066
ricow@chromium.org30ce4112010-05-31 10:38:25 +000067 // Compute number of region covering addr. See Page::GetRegionNumberForAddress
68 // method for more details.
69 and_(addr, Page::kPageAlignmentMask);
70 shr(addr, Page::kRegionSizeLog2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071
ricow@chromium.org30ce4112010-05-31 10:38:25 +000072 // Set dirty mark for region.
73 bts(Operand(object, Page::kDirtyFlagOffset), addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074}
75
76
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000077void MacroAssembler::InNewSpace(Register object,
78 Register scratch,
79 Condition cc,
80 Label* branch) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000081 ASSERT(cc == equal || cc == not_equal);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000082 if (Serializer::enabled()) {
83 // Can't do arithmetic on external references if it might get serialized.
84 mov(scratch, Operand(object));
85 // The mask isn't really an address. We load it as an external reference in
86 // case the size of the new space is different between the snapshot maker
87 // and the running system.
88 and_(Operand(scratch), Immediate(ExternalReference::new_space_mask()));
89 cmp(Operand(scratch), Immediate(ExternalReference::new_space_start()));
90 j(cc, branch);
91 } else {
92 int32_t new_space_start = reinterpret_cast<int32_t>(
93 ExternalReference::new_space_start().address());
94 lea(scratch, Operand(object, -new_space_start));
95 and_(scratch, Heap::NewSpaceMask());
96 j(cc, branch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098}
99
100
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000101void MacroAssembler::RecordWrite(Register object,
102 int offset,
103 Register value,
104 Register scratch) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000105 // The compiled code assumes that record write doesn't change the
106 // context register, so we check that none of the clobbered
107 // registers are esi.
108 ASSERT(!object.is(esi) && !value.is(esi) && !scratch.is(esi));
109
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000110 // First, check if a write barrier is even needed. The tests below
111 // catch stores of Smis and stores into young gen.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 Label done;
113
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000114 // Skip barrier if writing a smi.
115 ASSERT_EQ(0, kSmiTag);
116 test(value, Immediate(kSmiTagMask));
117 j(zero, &done);
118
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000119 InNewSpace(object, value, equal, &done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120
whesse@chromium.orge88a9ed2010-04-15 15:07:46 +0000121 // The offset is relative to a tagged or untagged HeapObject pointer,
122 // so either offset or offset + kHeapObjectTag must be a
123 // multiple of kPointerSize.
124 ASSERT(IsAligned(offset, kPointerSize) ||
125 IsAligned(offset + kHeapObjectTag, kPointerSize));
126
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000127 Register dst = scratch;
128 if (offset != 0) {
129 lea(dst, Operand(object, offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000131 // Array access: calculate the destination address in the same manner as
132 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
133 // into an array of words.
134 ASSERT_EQ(1, kSmiTagSize);
135 ASSERT_EQ(0, kSmiTag);
136 lea(dst, Operand(object, dst, times_half_pointer_size,
137 FixedArray::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000139 RecordWriteHelper(object, dst, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140
141 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000142
143 // Clobber all input registers when running with the debug-code flag
144 // turned on to provoke errors.
145 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000146 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
147 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
148 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000149 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150}
151
152
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000153void MacroAssembler::RecordWrite(Register object,
154 Register address,
155 Register value) {
156 // The compiled code assumes that record write doesn't change the
157 // context register, so we check that none of the clobbered
158 // registers are esi.
159 ASSERT(!object.is(esi) && !value.is(esi) && !address.is(esi));
160
161 // First, check if a write barrier is even needed. The tests below
162 // catch stores of Smis and stores into young gen.
163 Label done;
164
165 // Skip barrier if writing a smi.
166 ASSERT_EQ(0, kSmiTag);
167 test(value, Immediate(kSmiTagMask));
168 j(zero, &done);
169
170 InNewSpace(object, value, equal, &done);
171
172 RecordWriteHelper(object, address, value);
173
174 bind(&done);
175
176 // Clobber all input registers when running with the debug-code flag
177 // turned on to provoke errors.
178 if (FLAG_debug_code) {
179 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
180 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
181 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
182 }
183}
184
185
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000186void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
187 cmp(esp,
188 Operand::StaticVariable(ExternalReference::address_of_stack_limit()));
189 j(below, on_stack_overflow);
190}
191
192
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000193#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +0000194void MacroAssembler::DebugBreak() {
195 Set(eax, Immediate(0));
196 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak)));
197 CEntryStub ces(1);
198 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
199}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000200#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000202
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203void MacroAssembler::Set(Register dst, const Immediate& x) {
204 if (x.is_zero()) {
205 xor_(dst, Operand(dst)); // shorter than mov
206 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000207 mov(dst, x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 }
209}
210
211
212void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
213 mov(dst, x);
214}
215
216
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000217void MacroAssembler::CmpObjectType(Register heap_object,
218 InstanceType type,
219 Register map) {
220 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
221 CmpInstanceType(map, type);
222}
223
224
225void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
226 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
227 static_cast<int8_t>(type));
228}
229
230
ager@chromium.org5c838252010-02-19 08:53:10 +0000231void MacroAssembler::CheckMap(Register obj,
232 Handle<Map> map,
233 Label* fail,
234 bool is_heap_object) {
235 if (!is_heap_object) {
236 test(obj, Immediate(kSmiTagMask));
237 j(zero, fail);
238 }
239 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
240 j(not_equal, fail);
241}
242
243
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000244Condition MacroAssembler::IsObjectStringType(Register heap_object,
245 Register map,
246 Register instance_type) {
247 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
248 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
249 ASSERT(kNotStringTag != 0);
250 test(instance_type, Immediate(kIsNotStringMask));
251 return zero;
252}
253
254
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000255void MacroAssembler::IsObjectJSObjectType(Register heap_object,
256 Register map,
257 Register scratch,
258 Label* fail) {
259 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
260 IsInstanceJSObjectType(map, scratch, fail);
261}
262
263
264void MacroAssembler::IsInstanceJSObjectType(Register map,
265 Register scratch,
266 Label* fail) {
267 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
268 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
269 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
270 j(above, fail);
271}
272
273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274void MacroAssembler::FCmp() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000275 if (CpuFeatures::IsSupported(CMOV)) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000276 fucomip();
277 ffree(0);
278 fincstp();
279 } else {
280 fucompp();
281 push(eax);
282 fnstsw_ax();
283 sahf();
284 pop(eax);
285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286}
287
288
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000289void MacroAssembler::AbortIfNotNumber(Register object) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000290 Label ok;
291 test(object, Immediate(kSmiTagMask));
292 j(zero, &ok);
293 cmp(FieldOperand(object, HeapObject::kMapOffset),
294 Factory::heap_number_map());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000295 Assert(equal, "Operand not a number");
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 bind(&ok);
297}
298
299
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000300void MacroAssembler::AbortIfNotSmi(Register object) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000301 test(object, Immediate(kSmiTagMask));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000302 Assert(equal, "Operand is not a smi");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000303}
304
305
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000306void MacroAssembler::AbortIfNotString(Register object) {
307 test(object, Immediate(kSmiTagMask));
308 Assert(not_equal, "Operand is not a string");
309 push(object);
310 mov(object, FieldOperand(object, HeapObject::kMapOffset));
311 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
312 pop(object);
313 Assert(below, "Operand is not a string");
314}
315
316
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000317void MacroAssembler::AbortIfSmi(Register object) {
318 test(object, Immediate(kSmiTagMask));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000319 Assert(not_equal, "Operand is a smi");
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000320}
321
322
ager@chromium.org7c537e22008-10-16 08:43:32 +0000323void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 push(ebp);
325 mov(ebp, Operand(esp));
326 push(esi);
327 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000328 push(Immediate(CodeObject()));
329 if (FLAG_debug_code) {
330 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
331 Check(not_equal, "code object not properly patched");
332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333}
334
335
ager@chromium.org7c537e22008-10-16 08:43:32 +0000336void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 if (FLAG_debug_code) {
338 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
339 Immediate(Smi::FromInt(type)));
340 Check(equal, "stack frame types must match");
341 }
342 leave();
343}
344
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000345
346void MacroAssembler::EnterExitFramePrologue() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000347 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000348 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000349 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
350 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
351 push(ebp);
352 mov(ebp, Operand(esp));
353
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000354 // Reserve room for entry stack pointer and push the code object.
ager@chromium.org236ad962008-09-25 09:45:57 +0000355 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +0000356 push(Immediate(0)); // Saved entry sp, patched before call.
357 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000358
359 // Save the frame pointer and the context in top.
360 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
361 ExternalReference context_address(Top::k_context_address);
362 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
363 mov(Operand::StaticVariable(context_address), esi);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000364}
ager@chromium.org236ad962008-09-25 09:45:57 +0000365
ager@chromium.org236ad962008-09-25 09:45:57 +0000366
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000367void MacroAssembler::EnterExitFrameEpilogue(int argc) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000368 // Reserve space for arguments.
369 sub(Operand(esp), Immediate(argc * kPointerSize));
ager@chromium.org236ad962008-09-25 09:45:57 +0000370
371 // Get the required frame alignment for the OS.
372 static const int kFrameAlignment = OS::ActivationFrameAlignment();
373 if (kFrameAlignment > 0) {
374 ASSERT(IsPowerOf2(kFrameAlignment));
375 and_(esp, -kFrameAlignment);
376 }
377
378 // Patch the saved entry sp.
379 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
380}
381
382
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000383void MacroAssembler::EnterExitFrame() {
384 EnterExitFramePrologue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000385
386 // Setup argc and argv in callee-saved registers.
387 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
388 mov(edi, Operand(eax));
389 lea(esi, Operand(ebp, eax, times_4, offset));
390
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000391 EnterExitFrameEpilogue(2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000392}
393
394
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000395void MacroAssembler::EnterApiExitFrame(int argc) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000396 EnterExitFramePrologue();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000397 EnterExitFrameEpilogue(argc);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000398}
399
400
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000401void MacroAssembler::LeaveExitFrame() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000402 // Get the return address from the stack and restore the frame pointer.
403 mov(ecx, Operand(ebp, 1 * kPointerSize));
404 mov(ebp, Operand(ebp, 0 * kPointerSize));
405
406 // Pop the arguments and the receiver from the caller stack.
407 lea(esp, Operand(esi, 1 * kPointerSize));
408
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000409 // Push the return address to get ready to return.
410 push(ecx);
411
412 LeaveExitFrameEpilogue();
413}
414
415void MacroAssembler::LeaveExitFrameEpilogue() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000416 // Restore current context from top and clear it in debug mode.
417 ExternalReference context_address(Top::k_context_address);
418 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000419#ifdef DEBUG
420 mov(Operand::StaticVariable(context_address), Immediate(0));
421#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000422
ager@chromium.org236ad962008-09-25 09:45:57 +0000423 // Clear the top frame.
424 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
425 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
426}
427
428
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000429void MacroAssembler::LeaveApiExitFrame() {
430 mov(esp, Operand(ebp));
431 pop(ebp);
432
433 LeaveExitFrameEpilogue();
434}
435
436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437void MacroAssembler::PushTryHandler(CodeLocation try_location,
438 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000439 // Adjust this code if not the case.
440 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 // The pc (return address) is already on TOS.
442 if (try_location == IN_JAVASCRIPT) {
443 if (type == TRY_CATCH_HANDLER) {
444 push(Immediate(StackHandler::TRY_CATCH));
445 } else {
446 push(Immediate(StackHandler::TRY_FINALLY));
447 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 } else {
450 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000451 // The frame pointer does not point to a JS frame so we save NULL
452 // for ebp. We expect the code throwing an exception to check ebp
453 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000455 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000457 // Save the current handler as the next handler.
458 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
459 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
461}
462
463
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000464void MacroAssembler::PopTryHandler() {
465 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
466 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
467 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
468}
469
470
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000471void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000472 Register scratch,
473 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000474 Label same_contexts;
475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 ASSERT(!holder_reg.is(scratch));
477
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000478 // Load current lexical context from the stack frame.
479 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
480
481 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 if (FLAG_debug_code) {
483 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000484 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000486 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
488 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000489 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
490
491 // Check the context is a global context.
492 if (FLAG_debug_code) {
493 push(scratch);
494 // Read the first word and compare to global_context_map.
495 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
496 cmp(scratch, Factory::global_context_map());
497 Check(equal, "JSGlobalObject::global_context should be a global context.");
498 pop(scratch);
499 }
500
501 // Check if both contexts are the same.
502 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
503 j(equal, &same_contexts, taken);
504
505 // Compare security tokens, save holder_reg on the stack so we can use it
506 // as a temporary register.
507 //
508 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
509 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 // Check that the security token in the calling global object is
511 // compatible with the security token in the receiving global
512 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000513 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
514
515 // Check the context is a global context.
516 if (FLAG_debug_code) {
517 cmp(holder_reg, Factory::null_value());
518 Check(not_equal, "JSGlobalProxy::context() should not be null.");
519
520 push(holder_reg);
521 // Read the first word and compare to global_context_map(),
522 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
523 cmp(holder_reg, Factory::global_context_map());
524 Check(equal, "JSGlobalObject::global_context should be a global context.");
525 pop(holder_reg);
526 }
527
528 int token_offset = Context::kHeaderSize +
529 Context::SECURITY_TOKEN_INDEX * kPointerSize;
530 mov(scratch, FieldOperand(scratch, token_offset));
531 cmp(scratch, FieldOperand(holder_reg, token_offset));
532 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000534
535 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536}
537
538
ager@chromium.orga1645e22009-09-09 19:27:10 +0000539void MacroAssembler::LoadAllocationTopHelper(Register result,
540 Register result_end,
541 Register scratch,
542 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000543 ExternalReference new_space_allocation_top =
544 ExternalReference::new_space_allocation_top_address();
545
546 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000547 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000548 // No use of scratch if allocation top is provided.
549 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000550#ifdef DEBUG
551 // Assert that result actually contains top on entry.
552 cmp(result, Operand::StaticVariable(new_space_allocation_top));
553 Check(equal, "Unexpected allocation top");
554#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000555 return;
556 }
557
558 // Move address of new object to result. Use scratch register if available.
559 if (scratch.is(no_reg)) {
560 mov(result, Operand::StaticVariable(new_space_allocation_top));
561 } else {
562 ASSERT(!scratch.is(result_end));
563 mov(Operand(scratch), Immediate(new_space_allocation_top));
564 mov(result, Operand(scratch, 0));
565 }
566}
567
568
569void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
570 Register scratch) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000571 if (FLAG_debug_code) {
572 test(result_end, Immediate(kObjectAlignmentMask));
573 Check(zero, "Unaligned allocation in new space");
574 }
575
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000576 ExternalReference new_space_allocation_top =
577 ExternalReference::new_space_allocation_top_address();
578
579 // Update new top. Use scratch if available.
580 if (scratch.is(no_reg)) {
581 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
582 } else {
583 mov(Operand(scratch, 0), result_end);
584 }
585}
586
ager@chromium.orga1645e22009-09-09 19:27:10 +0000587
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000588void MacroAssembler::AllocateInNewSpace(int object_size,
589 Register result,
590 Register result_end,
591 Register scratch,
592 Label* gc_required,
593 AllocationFlags flags) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000594 if (!FLAG_inline_new) {
595 if (FLAG_debug_code) {
596 // Trash the registers to simulate an allocation failure.
597 mov(result, Immediate(0x7091));
598 if (result_end.is_valid()) {
599 mov(result_end, Immediate(0x7191));
600 }
601 if (scratch.is_valid()) {
602 mov(scratch, Immediate(0x7291));
603 }
604 }
605 jmp(gc_required);
606 return;
607 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000608 ASSERT(!result.is(result_end));
609
610 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000611 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000612
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000613 Register top_reg = result_end.is_valid() ? result_end : result;
614
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000615 // Calculate new top and bail out if new space is exhausted.
616 ExternalReference new_space_allocation_limit =
617 ExternalReference::new_space_allocation_limit_address();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000618
619 if (top_reg.is(result)) {
620 add(Operand(top_reg), Immediate(object_size));
621 } else {
622 lea(top_reg, Operand(result, object_size));
623 }
624 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000625 j(above, gc_required, not_taken);
626
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000627 // Update allocation top.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000628 UpdateAllocationTopHelper(top_reg, scratch);
629
630 // Tag result if requested.
631 if (top_reg.is(result)) {
632 if ((flags & TAG_OBJECT) != 0) {
633 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
634 } else {
635 sub(Operand(result), Immediate(object_size));
636 }
637 } else if ((flags & TAG_OBJECT) != 0) {
638 add(Operand(result), Immediate(kHeapObjectTag));
639 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000640}
641
642
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000643void MacroAssembler::AllocateInNewSpace(int header_size,
644 ScaleFactor element_size,
645 Register element_count,
646 Register result,
647 Register result_end,
648 Register scratch,
649 Label* gc_required,
650 AllocationFlags flags) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000651 if (!FLAG_inline_new) {
652 if (FLAG_debug_code) {
653 // Trash the registers to simulate an allocation failure.
654 mov(result, Immediate(0x7091));
655 mov(result_end, Immediate(0x7191));
656 if (scratch.is_valid()) {
657 mov(scratch, Immediate(0x7291));
658 }
659 // Register element_count is not modified by the function.
660 }
661 jmp(gc_required);
662 return;
663 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000664 ASSERT(!result.is(result_end));
665
666 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000667 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000668
669 // Calculate new top and bail out if new space is exhausted.
670 ExternalReference new_space_allocation_limit =
671 ExternalReference::new_space_allocation_limit_address();
672 lea(result_end, Operand(result, element_count, element_size, header_size));
673 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
674 j(above, gc_required);
675
ager@chromium.orga1645e22009-09-09 19:27:10 +0000676 // Tag result if requested.
677 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000678 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000679 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000680
681 // Update allocation top.
682 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000683}
684
685
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000686void MacroAssembler::AllocateInNewSpace(Register object_size,
687 Register result,
688 Register result_end,
689 Register scratch,
690 Label* gc_required,
691 AllocationFlags flags) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000692 if (!FLAG_inline_new) {
693 if (FLAG_debug_code) {
694 // Trash the registers to simulate an allocation failure.
695 mov(result, Immediate(0x7091));
696 mov(result_end, Immediate(0x7191));
697 if (scratch.is_valid()) {
698 mov(scratch, Immediate(0x7291));
699 }
700 // object_size is left unchanged by this function.
701 }
702 jmp(gc_required);
703 return;
704 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000705 ASSERT(!result.is(result_end));
706
707 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000708 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000709
710 // Calculate new top and bail out if new space is exhausted.
711 ExternalReference new_space_allocation_limit =
712 ExternalReference::new_space_allocation_limit_address();
713 if (!object_size.is(result_end)) {
714 mov(result_end, object_size);
715 }
716 add(result_end, Operand(result));
717 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
718 j(above, gc_required, not_taken);
719
ager@chromium.orga1645e22009-09-09 19:27:10 +0000720 // Tag result if requested.
721 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000722 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000723 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000724
725 // Update allocation top.
726 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000727}
728
729
730void MacroAssembler::UndoAllocationInNewSpace(Register object) {
731 ExternalReference new_space_allocation_top =
732 ExternalReference::new_space_allocation_top_address();
733
734 // Make sure the object has no tag before resetting top.
735 and_(Operand(object), Immediate(~kHeapObjectTagMask));
736#ifdef DEBUG
737 cmp(object, Operand::StaticVariable(new_space_allocation_top));
738 Check(below, "Undo allocation of non allocated memory");
739#endif
740 mov(Operand::StaticVariable(new_space_allocation_top), object);
741}
742
743
ager@chromium.org3811b432009-10-28 14:53:37 +0000744void MacroAssembler::AllocateHeapNumber(Register result,
745 Register scratch1,
746 Register scratch2,
747 Label* gc_required) {
748 // Allocate heap number in new space.
749 AllocateInNewSpace(HeapNumber::kSize,
750 result,
751 scratch1,
752 scratch2,
753 gc_required,
754 TAG_OBJECT);
755
756 // Set the map.
757 mov(FieldOperand(result, HeapObject::kMapOffset),
758 Immediate(Factory::heap_number_map()));
759}
760
761
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000762void MacroAssembler::AllocateTwoByteString(Register result,
763 Register length,
764 Register scratch1,
765 Register scratch2,
766 Register scratch3,
767 Label* gc_required) {
768 // Calculate the number of bytes needed for the characters in the string while
769 // observing object alignment.
770 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000771 ASSERT(kShortSize == 2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000772 // scratch1 = length * 2 + kObjectAlignmentMask.
773 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000774 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
775
776 // Allocate two byte string in new space.
777 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
778 times_1,
779 scratch1,
780 result,
781 scratch2,
782 scratch3,
783 gc_required,
784 TAG_OBJECT);
785
786 // Set the map, length and hash field.
787 mov(FieldOperand(result, HeapObject::kMapOffset),
788 Immediate(Factory::string_map()));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000789 mov(scratch1, length);
790 SmiTag(scratch1);
791 mov(FieldOperand(result, String::kLengthOffset), scratch1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000792 mov(FieldOperand(result, String::kHashFieldOffset),
793 Immediate(String::kEmptyHashField));
794}
795
796
797void MacroAssembler::AllocateAsciiString(Register result,
798 Register length,
799 Register scratch1,
800 Register scratch2,
801 Register scratch3,
802 Label* gc_required) {
803 // Calculate the number of bytes needed for the characters in the string while
804 // observing object alignment.
805 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
806 mov(scratch1, length);
807 ASSERT(kCharSize == 1);
808 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
809 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
810
811 // Allocate ascii string in new space.
812 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
813 times_1,
814 scratch1,
815 result,
816 scratch2,
817 scratch3,
818 gc_required,
819 TAG_OBJECT);
820
821 // Set the map, length and hash field.
822 mov(FieldOperand(result, HeapObject::kMapOffset),
823 Immediate(Factory::ascii_string_map()));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000824 mov(scratch1, length);
825 SmiTag(scratch1);
826 mov(FieldOperand(result, String::kLengthOffset), scratch1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000827 mov(FieldOperand(result, String::kHashFieldOffset),
828 Immediate(String::kEmptyHashField));
829}
830
831
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000832void MacroAssembler::AllocateAsciiString(Register result,
833 int length,
834 Register scratch1,
835 Register scratch2,
836 Label* gc_required) {
837 ASSERT(length > 0);
838
839 // Allocate ascii string in new space.
840 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
841 result,
842 scratch1,
843 scratch2,
844 gc_required,
845 TAG_OBJECT);
846
847 // Set the map, length and hash field.
848 mov(FieldOperand(result, HeapObject::kMapOffset),
849 Immediate(Factory::ascii_string_map()));
850 mov(FieldOperand(result, String::kLengthOffset),
851 Immediate(Smi::FromInt(length)));
852 mov(FieldOperand(result, String::kHashFieldOffset),
853 Immediate(String::kEmptyHashField));
854}
855
856
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000857void MacroAssembler::AllocateConsString(Register result,
858 Register scratch1,
859 Register scratch2,
860 Label* gc_required) {
861 // Allocate heap number in new space.
862 AllocateInNewSpace(ConsString::kSize,
863 result,
864 scratch1,
865 scratch2,
866 gc_required,
867 TAG_OBJECT);
868
869 // Set the map. The other fields are left uninitialized.
870 mov(FieldOperand(result, HeapObject::kMapOffset),
871 Immediate(Factory::cons_string_map()));
872}
873
874
875void MacroAssembler::AllocateAsciiConsString(Register result,
876 Register scratch1,
877 Register scratch2,
878 Label* gc_required) {
879 // Allocate heap number in new space.
880 AllocateInNewSpace(ConsString::kSize,
881 result,
882 scratch1,
883 scratch2,
884 gc_required,
885 TAG_OBJECT);
886
887 // Set the map. The other fields are left uninitialized.
888 mov(FieldOperand(result, HeapObject::kMapOffset),
889 Immediate(Factory::cons_ascii_string_map()));
890}
891
892
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000893void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
894 Register result,
895 Register op,
896 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000897 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000898 test(result, Operand(result));
899 ok.Branch(not_zero, taken);
900 test(op, Operand(op));
901 then_target->Branch(sign, not_taken);
902 ok.Bind();
903}
904
905
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906void MacroAssembler::NegativeZeroTest(Register result,
907 Register op,
908 Label* then_label) {
909 Label ok;
910 test(result, Operand(result));
911 j(not_zero, &ok, taken);
912 test(op, Operand(op));
913 j(sign, then_label, not_taken);
914 bind(&ok);
915}
916
917
918void MacroAssembler::NegativeZeroTest(Register result,
919 Register op1,
920 Register op2,
921 Register scratch,
922 Label* then_label) {
923 Label ok;
924 test(result, Operand(result));
925 j(not_zero, &ok, taken);
926 mov(scratch, Operand(op1));
927 or_(scratch, Operand(op2));
928 j(sign, then_label, not_taken);
929 bind(&ok);
930}
931
932
ager@chromium.org7c537e22008-10-16 08:43:32 +0000933void MacroAssembler::TryGetFunctionPrototype(Register function,
934 Register result,
935 Register scratch,
936 Label* miss) {
937 // Check that the receiver isn't a smi.
938 test(function, Immediate(kSmiTagMask));
939 j(zero, miss, not_taken);
940
941 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000942 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000943 j(not_equal, miss, not_taken);
944
945 // Make sure that the function has an instance prototype.
946 Label non_instance;
947 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
948 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
949 j(not_zero, &non_instance, not_taken);
950
951 // Get the prototype or initial map from the function.
952 mov(result,
953 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
954
955 // If the prototype or initial map is the hole, don't return it and
956 // simply miss the cache instead. This will allow us to allocate a
957 // prototype object on-demand in the runtime system.
958 cmp(Operand(result), Immediate(Factory::the_hole_value()));
959 j(equal, miss, not_taken);
960
961 // If the function does not have an initial map, we're done.
962 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000963 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000964 j(not_equal, &done);
965
966 // Get the prototype from the initial map.
967 mov(result, FieldOperand(result, Map::kPrototypeOffset));
968 jmp(&done);
969
970 // Non-instance prototype: Fetch prototype from constructor field
971 // in initial map.
972 bind(&non_instance);
973 mov(result, FieldOperand(result, Map::kConstructorOffset));
974
975 // All done.
976 bind(&done);
977}
978
979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000981 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +0000982 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983}
984
985
lrn@chromium.org303ada72010-10-27 09:33:13 +0000986MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000987 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000988 Object* result;
989 { MaybeObject* maybe_result = stub->TryGetCode();
990 if (!maybe_result->ToObject(&result)) return maybe_result;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000991 }
lrn@chromium.org303ada72010-10-27 09:33:13 +0000992 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000993 return result;
994}
995
996
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000997void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000998 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000999 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1000}
1001
1002
lrn@chromium.org303ada72010-10-27 09:33:13 +00001003MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001004 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001005 Object* result;
1006 { MaybeObject* maybe_result = stub->TryGetCode();
1007 if (!maybe_result->ToObject(&result)) return maybe_result;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001008 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001009 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001010 return result;
1011}
1012
1013
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014void MacroAssembler::StubReturn(int argc) {
1015 ASSERT(argc >= 1 && generating_stub());
1016 ret((argc - 1) * kPointerSize);
1017}
1018
1019
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001020void MacroAssembler::IllegalOperation(int num_arguments) {
1021 if (num_arguments > 0) {
1022 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1023 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001024 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025}
1026
1027
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001028void MacroAssembler::IndexFromHash(Register hash, Register index) {
1029 // The assert checks that the constants for the maximum number of digits
1030 // for an array index cached in the hash field and the number of bits
1031 // reserved for it does not conflict.
1032 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1033 (1 << String::kArrayIndexValueBits));
1034 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1035 // the low kHashShift bits.
1036 and_(hash, String::kArrayIndexValueMask);
1037 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1038 if (String::kHashShift > kSmiTagSize) {
1039 shr(hash, String::kHashShift - kSmiTagSize);
1040 }
1041 if (!index.is(hash)) {
1042 mov(index, hash);
1043 }
1044}
1045
1046
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1048 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1049}
1050
1051
lrn@chromium.org303ada72010-10-27 09:33:13 +00001052MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1053 int num_arguments) {
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001054 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1055}
1056
1057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001059 // If the expected number of arguments of the runtime function is
1060 // constant, we check that the actual number of arguments match the
1061 // expectation.
1062 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001063 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 return;
1065 }
1066
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001067 // TODO(1236192): Most runtime routines don't need the number of
1068 // arguments passed in because it is constant. At some point we
1069 // should remove this need and make the runtime routine entry code
1070 // smarter.
1071 Set(eax, Immediate(num_arguments));
1072 mov(ebx, Immediate(ExternalReference(f)));
1073 CEntryStub ces(1);
1074 CallStub(&ces);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075}
1076
1077
lrn@chromium.org303ada72010-10-27 09:33:13 +00001078MaybeObject* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1079 int num_arguments) {
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001080 if (f->nargs >= 0 && f->nargs != num_arguments) {
1081 IllegalOperation(num_arguments);
1082 // Since we did not call the stub, there was no allocation failure.
1083 // Return some non-failure object.
1084 return Heap::undefined_value();
1085 }
1086
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087 // TODO(1236192): Most runtime routines don't need the number of
1088 // arguments passed in because it is constant. At some point we
1089 // should remove this need and make the runtime routine entry code
1090 // smarter.
1091 Set(eax, Immediate(num_arguments));
1092 mov(ebx, Immediate(ExternalReference(f)));
1093 CEntryStub ces(1);
1094 return TryCallStub(&ces);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001095}
1096
1097
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001098void MacroAssembler::CallExternalReference(ExternalReference ref,
1099 int num_arguments) {
1100 mov(eax, Immediate(num_arguments));
1101 mov(ebx, Immediate(ref));
1102
1103 CEntryStub stub(1);
1104 CallStub(&stub);
1105}
1106
1107
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001108void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1109 int num_arguments,
1110 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001111 // TODO(1236192): Most runtime routines don't need the number of
1112 // arguments passed in because it is constant. At some point we
1113 // should remove this need and make the runtime routine entry code
1114 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001115 Set(eax, Immediate(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001116 JumpToExternalReference(ext);
1117}
1118
1119
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001120MaybeObject* MacroAssembler::TryTailCallExternalReference(
1121 const ExternalReference& ext, int num_arguments, int result_size) {
1122 // TODO(1236192): Most runtime routines don't need the number of
1123 // arguments passed in because it is constant. At some point we
1124 // should remove this need and make the runtime routine entry code
1125 // smarter.
1126 Set(eax, Immediate(num_arguments));
1127 return TryJumpToExternalReference(ext);
1128}
1129
1130
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001131void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1132 int num_arguments,
1133 int result_size) {
1134 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135}
1136
1137
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001138MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1139 int num_arguments,
1140 int result_size) {
1141 return TryTailCallExternalReference(
1142 ExternalReference(fid), num_arguments, result_size);
1143}
1144
1145
lrn@chromium.org303ada72010-10-27 09:33:13 +00001146// If true, a Handle<T> passed by value is passed and returned by
1147// using the location_ field directly. If false, it is passed and
1148// returned as a pointer to a handle.
1149#ifdef USING_BSD_ABI
1150static const bool kPassHandlesDirectly = true;
1151#else
1152static const bool kPassHandlesDirectly = false;
1153#endif
1154
1155
1156Operand ApiParameterOperand(int index) {
1157 return Operand(esp, (index + (kPassHandlesDirectly ? 0 : 1)) * kPointerSize);
1158}
1159
1160
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001161void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001162 if (kPassHandlesDirectly) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001163 EnterApiExitFrame(argc);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001164 // When handles as passed directly we don't have to allocate extra
1165 // space for and pass an out parameter.
1166 } else {
1167 // We allocate two additional slots: return value and pointer to it.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001168 EnterApiExitFrame(argc + 2);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001169
lrn@chromium.org303ada72010-10-27 09:33:13 +00001170 // The argument slots are filled as follows:
1171 //
1172 // n + 1: output cell
1173 // n: arg n
1174 // ...
1175 // 1: arg1
1176 // 0: pointer to the output cell
1177 //
1178 // Note that this is one more "argument" than the function expects
1179 // so the out cell will have to be popped explicitly after returning
1180 // from the function. The out cell contains Handle.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001181
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001182 // pointer to out cell.
1183 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1184 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
1185 if (FLAG_debug_code) {
1186 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1187 }
1188 }
1189}
1190
1191
1192MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1193 int stack_space) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001194 ExternalReference next_address =
1195 ExternalReference::handle_scope_next_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001196 ExternalReference limit_address =
1197 ExternalReference::handle_scope_limit_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001198 ExternalReference level_address =
1199 ExternalReference::handle_scope_level_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001200
lrn@chromium.org303ada72010-10-27 09:33:13 +00001201 // Allocate HandleScope in callee-save registers.
1202 mov(ebx, Operand::StaticVariable(next_address));
1203 mov(edi, Operand::StaticVariable(limit_address));
1204 add(Operand::StaticVariable(level_address), Immediate(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001205
lrn@chromium.org303ada72010-10-27 09:33:13 +00001206 // Call the api function!
1207 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1208
1209 if (!kPassHandlesDirectly) {
1210 // The returned value is a pointer to the handle holding the result.
1211 // Dereference this to get to the location.
1212 mov(eax, Operand(eax, 0));
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001213 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001214
lrn@chromium.org303ada72010-10-27 09:33:13 +00001215 Label empty_handle;
1216 Label prologue;
1217 Label promote_scheduled_exception;
1218 Label delete_allocated_handles;
1219 Label leave_exit_frame;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001220
lrn@chromium.org303ada72010-10-27 09:33:13 +00001221 // Check if the result handle holds 0.
1222 test(eax, Operand(eax));
1223 j(zero, &empty_handle, not_taken);
1224 // It was non-zero. Dereference to get the result value.
1225 mov(eax, Operand(eax, 0));
1226 bind(&prologue);
1227 // No more valid handles (the result handle was the last one). Restore
1228 // previous handle scope.
1229 mov(Operand::StaticVariable(next_address), ebx);
1230 sub(Operand::StaticVariable(level_address), Immediate(1));
1231 Assert(above_equal, "Invalid HandleScope level");
1232 cmp(edi, Operand::StaticVariable(limit_address));
1233 j(not_equal, &delete_allocated_handles, not_taken);
1234 bind(&leave_exit_frame);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001235
lrn@chromium.org303ada72010-10-27 09:33:13 +00001236 // Check if the function scheduled an exception.
1237 ExternalReference scheduled_exception_address =
1238 ExternalReference::scheduled_exception_address();
1239 cmp(Operand::StaticVariable(scheduled_exception_address),
1240 Immediate(Factory::the_hole_value()));
1241 j(not_equal, &promote_scheduled_exception, not_taken);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001242 LeaveApiExitFrame();
1243 ret(stack_space * kPointerSize);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001244 bind(&promote_scheduled_exception);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001245 MaybeObject* result =
1246 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1247 if (result->IsFailure()) {
1248 return result;
1249 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001250 bind(&empty_handle);
1251 // It was zero; the result is undefined.
1252 mov(eax, Factory::undefined_value());
1253 jmp(&prologue);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001254
lrn@chromium.org303ada72010-10-27 09:33:13 +00001255 // HandleScope limit has changed. Delete allocated extensions.
1256 bind(&delete_allocated_handles);
1257 mov(Operand::StaticVariable(limit_address), edi);
1258 mov(edi, eax);
1259 mov(eax, Immediate(ExternalReference::delete_handle_scope_extensions()));
1260 call(Operand(eax));
1261 mov(eax, edi);
1262 jmp(&leave_exit_frame);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001263
1264 return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001265}
1266
1267
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001268void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001270 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001271 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001272 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273}
1274
1275
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001276MaybeObject* MacroAssembler::TryJumpToExternalReference(
1277 const ExternalReference& ext) {
1278 // Set the entry point and jump to the C entry runtime stub.
1279 mov(ebx, Immediate(ext));
1280 CEntryStub ces(1);
1281 return TryTailCallStub(&ces);
1282}
1283
1284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1286 const ParameterCount& actual,
1287 Handle<Code> code_constant,
1288 const Operand& code_operand,
1289 Label* done,
1290 InvokeFlag flag) {
1291 bool definitely_matches = false;
1292 Label invoke;
1293 if (expected.is_immediate()) {
1294 ASSERT(actual.is_immediate());
1295 if (expected.immediate() == actual.immediate()) {
1296 definitely_matches = true;
1297 } else {
1298 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001299 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1300 if (expected.immediate() == sentinel) {
1301 // Don't worry about adapting arguments for builtins that
1302 // don't want that done. Skip adaption code by making it look
1303 // like we have a match between expected and actual number of
1304 // arguments.
1305 definitely_matches = true;
1306 } else {
1307 mov(ebx, expected.immediate());
1308 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 }
1310 } else {
1311 if (actual.is_immediate()) {
1312 // Expected is in register, actual is immediate. This is the
1313 // case when we invoke function values without going through the
1314 // IC mechanism.
1315 cmp(expected.reg(), actual.immediate());
1316 j(equal, &invoke);
1317 ASSERT(expected.reg().is(ebx));
1318 mov(eax, actual.immediate());
1319 } else if (!expected.reg().is(actual.reg())) {
1320 // Both expected and actual are in (different) registers. This
1321 // is the case when we invoke functions using call and apply.
1322 cmp(expected.reg(), Operand(actual.reg()));
1323 j(equal, &invoke);
1324 ASSERT(actual.reg().is(eax));
1325 ASSERT(expected.reg().is(ebx));
1326 }
1327 }
1328
1329 if (!definitely_matches) {
1330 Handle<Code> adaptor =
1331 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1332 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001333 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1335 } else if (!code_operand.is_reg(edx)) {
1336 mov(edx, code_operand);
1337 }
1338
1339 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001340 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 jmp(done);
1342 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001343 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 }
1345 bind(&invoke);
1346 }
1347}
1348
1349
1350void MacroAssembler::InvokeCode(const Operand& code,
1351 const ParameterCount& expected,
1352 const ParameterCount& actual,
1353 InvokeFlag flag) {
1354 Label done;
1355 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1356 if (flag == CALL_FUNCTION) {
1357 call(code);
1358 } else {
1359 ASSERT(flag == JUMP_FUNCTION);
1360 jmp(code);
1361 }
1362 bind(&done);
1363}
1364
1365
1366void MacroAssembler::InvokeCode(Handle<Code> code,
1367 const ParameterCount& expected,
1368 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001369 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 InvokeFlag flag) {
1371 Label done;
1372 Operand dummy(eax);
1373 InvokePrologue(expected, actual, code, dummy, &done, flag);
1374 if (flag == CALL_FUNCTION) {
1375 call(code, rmode);
1376 } else {
1377 ASSERT(flag == JUMP_FUNCTION);
1378 jmp(code, rmode);
1379 }
1380 bind(&done);
1381}
1382
1383
1384void MacroAssembler::InvokeFunction(Register fun,
1385 const ParameterCount& actual,
1386 InvokeFlag flag) {
1387 ASSERT(fun.is(edi));
1388 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1389 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1390 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001391 SmiUntag(ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392
1393 ParameterCount expected(ebx);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001394 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1395 expected, actual, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396}
1397
1398
ager@chromium.org5c838252010-02-19 08:53:10 +00001399void MacroAssembler::InvokeFunction(JSFunction* function,
1400 const ParameterCount& actual,
1401 InvokeFlag flag) {
1402 ASSERT(function->is_compiled());
1403 // Get the function and setup the context.
1404 mov(edi, Immediate(Handle<JSFunction>(function)));
1405 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001406 // Invoke the cached code.
1407 Handle<Code> code(function->code());
1408 ParameterCount expected(function->shared()->formal_parameter_count());
1409 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1410}
1411
1412
1413void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001414 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001415 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416
1417 // Rely on the assertion to check that the number of provided
1418 // arguments match the expected number of arguments. Fake a
1419 // parameter count to avoid emitting code to do the check.
1420 ParameterCount expected(0);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001421 GetBuiltinFunction(edi, id);
1422 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1423 expected, expected, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424}
1425
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001426void MacroAssembler::GetBuiltinFunction(Register target,
1427 Builtins::JavaScript id) {
1428 // Load the JavaScript builtin function from the builtins object.
1429 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1430 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1431 mov(target, FieldOperand(target,
1432 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1433}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434
1435void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001436 ASSERT(!target.is(edi));
ager@chromium.org5c838252010-02-19 08:53:10 +00001437 // Load the JavaScript builtin function from the builtins object.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001438 GetBuiltinFunction(edi, id);
1439 // Load the code entry point from the function into the target register.
1440 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441}
1442
1443
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001444void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1445 if (context_chain_length > 0) {
1446 // Move up the chain of contexts to the context containing the slot.
1447 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1448 // Load the function context (which is the incoming, outer context).
1449 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1450 for (int i = 1; i < context_chain_length; i++) {
1451 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1452 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1453 }
1454 // The context may be an intermediate context, not a function context.
1455 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1456 } else { // Slot is in the current function context.
1457 // The context may be an intermediate context, not a function context.
1458 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1459 }
1460}
1461
1462
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001463void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1464 // Load the global or builtins object from the current context.
1465 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1466 // Load the global context from the global or builtins object.
1467 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1468 // Load the function from the global context.
1469 mov(function, Operand(function, Context::SlotOffset(index)));
1470}
1471
1472
1473void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1474 Register map) {
1475 // Load the initial map. The global functions all have initial maps.
1476 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1477 if (FLAG_debug_code) {
1478 Label ok, fail;
1479 CheckMap(map, Factory::meta_map(), &fail, false);
1480 jmp(&ok);
1481 bind(&fail);
1482 Abort("Global functions must have initial map");
1483 bind(&ok);
1484 }
1485}
1486
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001487
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488void MacroAssembler::Ret() {
1489 ret(0);
1490}
1491
1492
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001493void MacroAssembler::Drop(int stack_elements) {
1494 if (stack_elements > 0) {
1495 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1496 }
1497}
1498
1499
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001500void MacroAssembler::Move(Register dst, Register src) {
1501 if (!dst.is(src)) {
1502 mov(dst, src);
1503 }
1504}
1505
1506
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001507void MacroAssembler::Move(Register dst, Handle<Object> value) {
1508 mov(dst, value);
1509}
1510
1511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1513 if (FLAG_native_code_counters && counter->Enabled()) {
1514 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1515 }
1516}
1517
1518
1519void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1520 ASSERT(value > 0);
1521 if (FLAG_native_code_counters && counter->Enabled()) {
1522 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1523 if (value == 1) {
1524 inc(operand);
1525 } else {
1526 add(operand, Immediate(value));
1527 }
1528 }
1529}
1530
1531
1532void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1533 ASSERT(value > 0);
1534 if (FLAG_native_code_counters && counter->Enabled()) {
1535 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1536 if (value == 1) {
1537 dec(operand);
1538 } else {
1539 sub(operand, Immediate(value));
1540 }
1541 }
1542}
1543
1544
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545void MacroAssembler::IncrementCounter(Condition cc,
1546 StatsCounter* counter,
1547 int value) {
1548 ASSERT(value > 0);
1549 if (FLAG_native_code_counters && counter->Enabled()) {
1550 Label skip;
1551 j(NegateCondition(cc), &skip);
1552 pushfd();
1553 IncrementCounter(counter, value);
1554 popfd();
1555 bind(&skip);
1556 }
1557}
1558
1559
1560void MacroAssembler::DecrementCounter(Condition cc,
1561 StatsCounter* counter,
1562 int value) {
1563 ASSERT(value > 0);
1564 if (FLAG_native_code_counters && counter->Enabled()) {
1565 Label skip;
1566 j(NegateCondition(cc), &skip);
1567 pushfd();
1568 DecrementCounter(counter, value);
1569 popfd();
1570 bind(&skip);
1571 }
1572}
1573
1574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575void MacroAssembler::Assert(Condition cc, const char* msg) {
1576 if (FLAG_debug_code) Check(cc, msg);
1577}
1578
1579
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001580void MacroAssembler::AssertFastElements(Register elements) {
1581 if (FLAG_debug_code) {
1582 Label ok;
1583 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1584 Immediate(Factory::fixed_array_map()));
1585 j(equal, &ok);
1586 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1587 Immediate(Factory::fixed_cow_array_map()));
1588 j(equal, &ok);
1589 Abort("JSObject with fast elements map has slow elements");
1590 bind(&ok);
1591 }
1592}
1593
1594
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595void MacroAssembler::Check(Condition cc, const char* msg) {
1596 Label L;
1597 j(cc, &L, taken);
1598 Abort(msg);
1599 // will not return here
1600 bind(&L);
1601}
1602
1603
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001604void MacroAssembler::CheckStackAlignment() {
1605 int frame_alignment = OS::ActivationFrameAlignment();
1606 int frame_alignment_mask = frame_alignment - 1;
1607 if (frame_alignment > kPointerSize) {
1608 ASSERT(IsPowerOf2(frame_alignment));
1609 Label alignment_as_expected;
1610 test(esp, Immediate(frame_alignment_mask));
1611 j(zero, &alignment_as_expected);
1612 // Abort if stack is not aligned.
1613 int3();
1614 bind(&alignment_as_expected);
1615 }
1616}
1617
1618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619void MacroAssembler::Abort(const char* msg) {
1620 // We want to pass the msg string like a smi to avoid GC
1621 // problems, however msg is not guaranteed to be aligned
1622 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001623 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 // from the real pointer as a smi.
1625 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1626 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1627 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1628#ifdef DEBUG
1629 if (msg != NULL) {
1630 RecordComment("Abort message: ");
1631 RecordComment(msg);
1632 }
1633#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001634 // Disable stub call restrictions to always allow calls to abort.
1635 set_allow_stub_calls(true);
1636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637 push(eax);
1638 push(Immediate(p0));
1639 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1640 CallRuntime(Runtime::kAbort, 2);
1641 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001642 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643}
1644
1645
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001646void MacroAssembler::JumpIfNotNumber(Register reg,
1647 TypeInfo info,
1648 Label* on_not_number) {
1649 if (FLAG_debug_code) AbortIfSmi(reg);
1650 if (!info.IsNumber()) {
1651 cmp(FieldOperand(reg, HeapObject::kMapOffset),
1652 Factory::heap_number_map());
1653 j(not_equal, on_not_number);
1654 }
1655}
1656
1657
1658void MacroAssembler::ConvertToInt32(Register dst,
1659 Register source,
1660 Register scratch,
1661 TypeInfo info,
1662 Label* on_not_int32) {
1663 if (FLAG_debug_code) {
1664 AbortIfSmi(source);
1665 AbortIfNotNumber(source);
1666 }
1667 if (info.IsInteger32()) {
1668 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1669 } else {
1670 Label done;
1671 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1672 ASSERT(!scratch.is(source));
1673 if (push_pop) {
1674 push(dst);
1675 scratch = dst;
1676 }
1677 if (scratch.is(no_reg)) scratch = dst;
1678 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1679 cmp(scratch, 0x80000000u);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001680 if (push_pop) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001681 j(not_equal, &done);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001682 pop(dst);
1683 jmp(on_not_int32);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001684 } else {
1685 j(equal, on_not_int32);
1686 }
1687
1688 bind(&done);
1689 if (push_pop) {
1690 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1691 }
1692 if (!scratch.is(dst)) {
1693 mov(dst, scratch);
1694 }
1695 }
1696}
1697
1698
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001699void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1700 Register scratch,
1701 int power) {
1702 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1703 HeapNumber::kExponentBits));
1704 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1705 movd(dst, Operand(scratch));
1706 psllq(dst, HeapNumber::kMantissaBits);
1707}
1708
1709
ager@chromium.org5c838252010-02-19 08:53:10 +00001710void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1711 Register instance_type,
1712 Register scratch,
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001713 Label* failure) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001714 if (!scratch.is(instance_type)) {
1715 mov(scratch, instance_type);
1716 }
1717 and_(scratch,
1718 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1719 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1720 j(not_equal, failure);
1721}
1722
1723
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001724void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1725 Register object2,
1726 Register scratch1,
1727 Register scratch2,
1728 Label* failure) {
1729 // Check that both objects are not smis.
1730 ASSERT_EQ(0, kSmiTag);
1731 mov(scratch1, Operand(object1));
1732 and_(scratch1, Operand(object2));
1733 test(scratch1, Immediate(kSmiTagMask));
1734 j(zero, failure);
1735
1736 // Load instance type for both strings.
1737 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1738 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1739 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1740 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1741
1742 // Check that both are flat ascii strings.
1743 const int kFlatAsciiStringMask =
1744 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1745 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1746 // Interleave bits from both instance types and compare them in one check.
1747 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1748 and_(scratch1, kFlatAsciiStringMask);
1749 and_(scratch2, kFlatAsciiStringMask);
1750 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1751 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1752 j(not_equal, failure);
1753}
1754
1755
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001756void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1757 int frameAlignment = OS::ActivationFrameAlignment();
1758 if (frameAlignment != 0) {
1759 // Make stack end at alignment and make room for num_arguments words
1760 // and the original value of esp.
1761 mov(scratch, esp);
1762 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1763 ASSERT(IsPowerOf2(frameAlignment));
1764 and_(esp, -frameAlignment);
1765 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1766 } else {
1767 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1768 }
1769}
1770
1771
1772void MacroAssembler::CallCFunction(ExternalReference function,
1773 int num_arguments) {
1774 // Trashing eax is ok as it will be the return value.
1775 mov(Operand(eax), Immediate(function));
1776 CallCFunction(eax, num_arguments);
1777}
1778
1779
1780void MacroAssembler::CallCFunction(Register function,
1781 int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001782 // Check stack alignment.
1783 if (FLAG_debug_code) {
1784 CheckStackAlignment();
1785 }
1786
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001787 call(Operand(function));
1788 if (OS::ActivationFrameAlignment() != 0) {
1789 mov(esp, Operand(esp, num_arguments * kPointerSize));
1790 } else {
1791 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1792 }
1793}
1794
1795
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001797 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001798 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 // The size is adjusted with kGap on order for the assembler to generate size
1800 // bytes of instructions without failing with buffer size constraints.
1801 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1802}
1803
1804
1805CodePatcher::~CodePatcher() {
1806 // Indicate that code has changed.
1807 CPU::FlushICache(address_, size_);
1808
1809 // Check that the code was patched as expected.
1810 ASSERT(masm_.pc_ == address_ + size_);
1811 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1812}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813
1814
1815} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001816
1817#endif // V8_TARGET_ARCH_IA32