blob: a62f74b746567551a9a9af3102f440b9a13f9316 [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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000395void MacroAssembler::EnterApiExitFrame(int stack_space,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000396 int argc) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000397 EnterExitFramePrologue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000398
399 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
400 lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset));
401
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000402 EnterExitFrameEpilogue(argc);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000403}
404
405
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000406void MacroAssembler::LeaveExitFrame() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000407 // Get the return address from the stack and restore the frame pointer.
408 mov(ecx, Operand(ebp, 1 * kPointerSize));
409 mov(ebp, Operand(ebp, 0 * kPointerSize));
410
411 // Pop the arguments and the receiver from the caller stack.
412 lea(esp, Operand(esi, 1 * kPointerSize));
413
414 // Restore current context from top and clear it in debug mode.
415 ExternalReference context_address(Top::k_context_address);
416 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000417#ifdef DEBUG
418 mov(Operand::StaticVariable(context_address), Immediate(0));
419#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000420
421 // Push the return address to get ready to return.
422 push(ecx);
423
424 // Clear the top frame.
425 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
426 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
427}
428
429
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430void MacroAssembler::PushTryHandler(CodeLocation try_location,
431 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000432 // Adjust this code if not the case.
433 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 // The pc (return address) is already on TOS.
435 if (try_location == IN_JAVASCRIPT) {
436 if (type == TRY_CATCH_HANDLER) {
437 push(Immediate(StackHandler::TRY_CATCH));
438 } else {
439 push(Immediate(StackHandler::TRY_FINALLY));
440 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 } else {
443 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000444 // The frame pointer does not point to a JS frame so we save NULL
445 // for ebp. We expect the code throwing an exception to check ebp
446 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000448 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000450 // Save the current handler as the next handler.
451 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
452 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
454}
455
456
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000457void MacroAssembler::PopTryHandler() {
458 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
459 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
460 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
461}
462
463
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000464void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000465 Register scratch,
466 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000467 Label same_contexts;
468
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469 ASSERT(!holder_reg.is(scratch));
470
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000471 // Load current lexical context from the stack frame.
472 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
473
474 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 if (FLAG_debug_code) {
476 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000477 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000479 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
481 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000482 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
483
484 // Check the context is a global context.
485 if (FLAG_debug_code) {
486 push(scratch);
487 // Read the first word and compare to global_context_map.
488 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
489 cmp(scratch, Factory::global_context_map());
490 Check(equal, "JSGlobalObject::global_context should be a global context.");
491 pop(scratch);
492 }
493
494 // Check if both contexts are the same.
495 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
496 j(equal, &same_contexts, taken);
497
498 // Compare security tokens, save holder_reg on the stack so we can use it
499 // as a temporary register.
500 //
501 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
502 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 // Check that the security token in the calling global object is
504 // compatible with the security token in the receiving global
505 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000506 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
507
508 // Check the context is a global context.
509 if (FLAG_debug_code) {
510 cmp(holder_reg, Factory::null_value());
511 Check(not_equal, "JSGlobalProxy::context() should not be null.");
512
513 push(holder_reg);
514 // Read the first word and compare to global_context_map(),
515 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
516 cmp(holder_reg, Factory::global_context_map());
517 Check(equal, "JSGlobalObject::global_context should be a global context.");
518 pop(holder_reg);
519 }
520
521 int token_offset = Context::kHeaderSize +
522 Context::SECURITY_TOKEN_INDEX * kPointerSize;
523 mov(scratch, FieldOperand(scratch, token_offset));
524 cmp(scratch, FieldOperand(holder_reg, token_offset));
525 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000527
528 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529}
530
531
ager@chromium.orga1645e22009-09-09 19:27:10 +0000532void MacroAssembler::LoadAllocationTopHelper(Register result,
533 Register result_end,
534 Register scratch,
535 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000536 ExternalReference new_space_allocation_top =
537 ExternalReference::new_space_allocation_top_address();
538
539 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000540 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000541 // No use of scratch if allocation top is provided.
542 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000543#ifdef DEBUG
544 // Assert that result actually contains top on entry.
545 cmp(result, Operand::StaticVariable(new_space_allocation_top));
546 Check(equal, "Unexpected allocation top");
547#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000548 return;
549 }
550
551 // Move address of new object to result. Use scratch register if available.
552 if (scratch.is(no_reg)) {
553 mov(result, Operand::StaticVariable(new_space_allocation_top));
554 } else {
555 ASSERT(!scratch.is(result_end));
556 mov(Operand(scratch), Immediate(new_space_allocation_top));
557 mov(result, Operand(scratch, 0));
558 }
559}
560
561
562void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
563 Register scratch) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000564 if (FLAG_debug_code) {
565 test(result_end, Immediate(kObjectAlignmentMask));
566 Check(zero, "Unaligned allocation in new space");
567 }
568
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000569 ExternalReference new_space_allocation_top =
570 ExternalReference::new_space_allocation_top_address();
571
572 // Update new top. Use scratch if available.
573 if (scratch.is(no_reg)) {
574 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
575 } else {
576 mov(Operand(scratch, 0), result_end);
577 }
578}
579
ager@chromium.orga1645e22009-09-09 19:27:10 +0000580
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000581void MacroAssembler::AllocateInNewSpace(int object_size,
582 Register result,
583 Register result_end,
584 Register scratch,
585 Label* gc_required,
586 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000587 ASSERT(!result.is(result_end));
588
589 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000590 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000591
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000592 Register top_reg = result_end.is_valid() ? result_end : result;
593
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000594 // Calculate new top and bail out if new space is exhausted.
595 ExternalReference new_space_allocation_limit =
596 ExternalReference::new_space_allocation_limit_address();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000597
598 if (top_reg.is(result)) {
599 add(Operand(top_reg), Immediate(object_size));
600 } else {
601 lea(top_reg, Operand(result, object_size));
602 }
603 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000604 j(above, gc_required, not_taken);
605
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000606 // Update allocation top.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000607 UpdateAllocationTopHelper(top_reg, scratch);
608
609 // Tag result if requested.
610 if (top_reg.is(result)) {
611 if ((flags & TAG_OBJECT) != 0) {
612 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
613 } else {
614 sub(Operand(result), Immediate(object_size));
615 }
616 } else if ((flags & TAG_OBJECT) != 0) {
617 add(Operand(result), Immediate(kHeapObjectTag));
618 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000619}
620
621
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000622void MacroAssembler::AllocateInNewSpace(int header_size,
623 ScaleFactor element_size,
624 Register element_count,
625 Register result,
626 Register result_end,
627 Register scratch,
628 Label* gc_required,
629 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000630 ASSERT(!result.is(result_end));
631
632 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000633 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000634
635 // Calculate new top and bail out if new space is exhausted.
636 ExternalReference new_space_allocation_limit =
637 ExternalReference::new_space_allocation_limit_address();
638 lea(result_end, Operand(result, element_count, element_size, header_size));
639 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
640 j(above, gc_required);
641
ager@chromium.orga1645e22009-09-09 19:27:10 +0000642 // Tag result if requested.
643 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000644 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000645 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000646
647 // Update allocation top.
648 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000649}
650
651
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000652void MacroAssembler::AllocateInNewSpace(Register object_size,
653 Register result,
654 Register result_end,
655 Register scratch,
656 Label* gc_required,
657 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000658 ASSERT(!result.is(result_end));
659
660 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000661 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000662
663 // Calculate new top and bail out if new space is exhausted.
664 ExternalReference new_space_allocation_limit =
665 ExternalReference::new_space_allocation_limit_address();
666 if (!object_size.is(result_end)) {
667 mov(result_end, object_size);
668 }
669 add(result_end, Operand(result));
670 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
671 j(above, gc_required, not_taken);
672
ager@chromium.orga1645e22009-09-09 19:27:10 +0000673 // Tag result if requested.
674 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000675 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000676 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000677
678 // Update allocation top.
679 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000680}
681
682
683void MacroAssembler::UndoAllocationInNewSpace(Register object) {
684 ExternalReference new_space_allocation_top =
685 ExternalReference::new_space_allocation_top_address();
686
687 // Make sure the object has no tag before resetting top.
688 and_(Operand(object), Immediate(~kHeapObjectTagMask));
689#ifdef DEBUG
690 cmp(object, Operand::StaticVariable(new_space_allocation_top));
691 Check(below, "Undo allocation of non allocated memory");
692#endif
693 mov(Operand::StaticVariable(new_space_allocation_top), object);
694}
695
696
ager@chromium.org3811b432009-10-28 14:53:37 +0000697void MacroAssembler::AllocateHeapNumber(Register result,
698 Register scratch1,
699 Register scratch2,
700 Label* gc_required) {
701 // Allocate heap number in new space.
702 AllocateInNewSpace(HeapNumber::kSize,
703 result,
704 scratch1,
705 scratch2,
706 gc_required,
707 TAG_OBJECT);
708
709 // Set the map.
710 mov(FieldOperand(result, HeapObject::kMapOffset),
711 Immediate(Factory::heap_number_map()));
712}
713
714
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000715void MacroAssembler::AllocateTwoByteString(Register result,
716 Register length,
717 Register scratch1,
718 Register scratch2,
719 Register scratch3,
720 Label* gc_required) {
721 // Calculate the number of bytes needed for the characters in the string while
722 // observing object alignment.
723 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000724 ASSERT(kShortSize == 2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000725 // scratch1 = length * 2 + kObjectAlignmentMask.
726 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000727 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
728
729 // Allocate two byte string in new space.
730 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
731 times_1,
732 scratch1,
733 result,
734 scratch2,
735 scratch3,
736 gc_required,
737 TAG_OBJECT);
738
739 // Set the map, length and hash field.
740 mov(FieldOperand(result, HeapObject::kMapOffset),
741 Immediate(Factory::string_map()));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000742 mov(scratch1, length);
743 SmiTag(scratch1);
744 mov(FieldOperand(result, String::kLengthOffset), scratch1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000745 mov(FieldOperand(result, String::kHashFieldOffset),
746 Immediate(String::kEmptyHashField));
747}
748
749
750void MacroAssembler::AllocateAsciiString(Register result,
751 Register length,
752 Register scratch1,
753 Register scratch2,
754 Register scratch3,
755 Label* gc_required) {
756 // Calculate the number of bytes needed for the characters in the string while
757 // observing object alignment.
758 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
759 mov(scratch1, length);
760 ASSERT(kCharSize == 1);
761 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
762 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
763
764 // Allocate ascii string in new space.
765 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
766 times_1,
767 scratch1,
768 result,
769 scratch2,
770 scratch3,
771 gc_required,
772 TAG_OBJECT);
773
774 // Set the map, length and hash field.
775 mov(FieldOperand(result, HeapObject::kMapOffset),
776 Immediate(Factory::ascii_string_map()));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000777 mov(scratch1, length);
778 SmiTag(scratch1);
779 mov(FieldOperand(result, String::kLengthOffset), scratch1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000780 mov(FieldOperand(result, String::kHashFieldOffset),
781 Immediate(String::kEmptyHashField));
782}
783
784
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000785void MacroAssembler::AllocateAsciiString(Register result,
786 int length,
787 Register scratch1,
788 Register scratch2,
789 Label* gc_required) {
790 ASSERT(length > 0);
791
792 // Allocate ascii string in new space.
793 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
794 result,
795 scratch1,
796 scratch2,
797 gc_required,
798 TAG_OBJECT);
799
800 // Set the map, length and hash field.
801 mov(FieldOperand(result, HeapObject::kMapOffset),
802 Immediate(Factory::ascii_string_map()));
803 mov(FieldOperand(result, String::kLengthOffset),
804 Immediate(Smi::FromInt(length)));
805 mov(FieldOperand(result, String::kHashFieldOffset),
806 Immediate(String::kEmptyHashField));
807}
808
809
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000810void MacroAssembler::AllocateConsString(Register result,
811 Register scratch1,
812 Register scratch2,
813 Label* gc_required) {
814 // Allocate heap number in new space.
815 AllocateInNewSpace(ConsString::kSize,
816 result,
817 scratch1,
818 scratch2,
819 gc_required,
820 TAG_OBJECT);
821
822 // Set the map. The other fields are left uninitialized.
823 mov(FieldOperand(result, HeapObject::kMapOffset),
824 Immediate(Factory::cons_string_map()));
825}
826
827
828void MacroAssembler::AllocateAsciiConsString(Register result,
829 Register scratch1,
830 Register scratch2,
831 Label* gc_required) {
832 // Allocate heap number in new space.
833 AllocateInNewSpace(ConsString::kSize,
834 result,
835 scratch1,
836 scratch2,
837 gc_required,
838 TAG_OBJECT);
839
840 // Set the map. The other fields are left uninitialized.
841 mov(FieldOperand(result, HeapObject::kMapOffset),
842 Immediate(Factory::cons_ascii_string_map()));
843}
844
845
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000846void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
847 Register result,
848 Register op,
849 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000850 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000851 test(result, Operand(result));
852 ok.Branch(not_zero, taken);
853 test(op, Operand(op));
854 then_target->Branch(sign, not_taken);
855 ok.Bind();
856}
857
858
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859void MacroAssembler::NegativeZeroTest(Register result,
860 Register op,
861 Label* then_label) {
862 Label ok;
863 test(result, Operand(result));
864 j(not_zero, &ok, taken);
865 test(op, Operand(op));
866 j(sign, then_label, not_taken);
867 bind(&ok);
868}
869
870
871void MacroAssembler::NegativeZeroTest(Register result,
872 Register op1,
873 Register op2,
874 Register scratch,
875 Label* then_label) {
876 Label ok;
877 test(result, Operand(result));
878 j(not_zero, &ok, taken);
879 mov(scratch, Operand(op1));
880 or_(scratch, Operand(op2));
881 j(sign, then_label, not_taken);
882 bind(&ok);
883}
884
885
ager@chromium.org7c537e22008-10-16 08:43:32 +0000886void MacroAssembler::TryGetFunctionPrototype(Register function,
887 Register result,
888 Register scratch,
889 Label* miss) {
890 // Check that the receiver isn't a smi.
891 test(function, Immediate(kSmiTagMask));
892 j(zero, miss, not_taken);
893
894 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000895 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000896 j(not_equal, miss, not_taken);
897
898 // Make sure that the function has an instance prototype.
899 Label non_instance;
900 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
901 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
902 j(not_zero, &non_instance, not_taken);
903
904 // Get the prototype or initial map from the function.
905 mov(result,
906 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
907
908 // If the prototype or initial map is the hole, don't return it and
909 // simply miss the cache instead. This will allow us to allocate a
910 // prototype object on-demand in the runtime system.
911 cmp(Operand(result), Immediate(Factory::the_hole_value()));
912 j(equal, miss, not_taken);
913
914 // If the function does not have an initial map, we're done.
915 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000916 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000917 j(not_equal, &done);
918
919 // Get the prototype from the initial map.
920 mov(result, FieldOperand(result, Map::kPrototypeOffset));
921 jmp(&done);
922
923 // Non-instance prototype: Fetch prototype from constructor field
924 // in initial map.
925 bind(&non_instance);
926 mov(result, FieldOperand(result, Map::kConstructorOffset));
927
928 // All done.
929 bind(&done);
930}
931
932
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000934 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +0000935 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936}
937
938
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000939Object* MacroAssembler::TryCallStub(CodeStub* stub) {
940 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
941 Object* result = stub->TryGetCode();
942 if (!result->IsFailure()) {
943 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
944 }
945 return result;
946}
947
948
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000949void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000950 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000951 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
952}
953
954
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000955Object* MacroAssembler::TryTailCallStub(CodeStub* stub) {
956 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
957 Object* result = stub->TryGetCode();
958 if (!result->IsFailure()) {
959 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
960 }
961 return result;
962}
963
964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965void MacroAssembler::StubReturn(int argc) {
966 ASSERT(argc >= 1 && generating_stub());
967 ret((argc - 1) * kPointerSize);
968}
969
970
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000971void MacroAssembler::IllegalOperation(int num_arguments) {
972 if (num_arguments > 0) {
973 add(Operand(esp), Immediate(num_arguments * kPointerSize));
974 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000975 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976}
977
978
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000979void MacroAssembler::IndexFromHash(Register hash, Register index) {
980 // The assert checks that the constants for the maximum number of digits
981 // for an array index cached in the hash field and the number of bits
982 // reserved for it does not conflict.
983 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
984 (1 << String::kArrayIndexValueBits));
985 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
986 // the low kHashShift bits.
987 and_(hash, String::kArrayIndexValueMask);
988 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
989 if (String::kHashShift > kSmiTagSize) {
990 shr(hash, String::kHashShift - kSmiTagSize);
991 }
992 if (!index.is(hash)) {
993 mov(index, hash);
994 }
995}
996
997
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
999 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1000}
1001
1002
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001003Object* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1004 int num_arguments) {
1005 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1006}
1007
1008
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001010 // If the expected number of arguments of the runtime function is
1011 // constant, we check that the actual number of arguments match the
1012 // expectation.
1013 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001014 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 return;
1016 }
1017
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018 // TODO(1236192): Most runtime routines don't need the number of
1019 // arguments passed in because it is constant. At some point we
1020 // should remove this need and make the runtime routine entry code
1021 // smarter.
1022 Set(eax, Immediate(num_arguments));
1023 mov(ebx, Immediate(ExternalReference(f)));
1024 CEntryStub ces(1);
1025 CallStub(&ces);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026}
1027
1028
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001029Object* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1030 int num_arguments) {
1031 if (f->nargs >= 0 && f->nargs != num_arguments) {
1032 IllegalOperation(num_arguments);
1033 // Since we did not call the stub, there was no allocation failure.
1034 // Return some non-failure object.
1035 return Heap::undefined_value();
1036 }
1037
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001038 // TODO(1236192): Most runtime routines don't need the number of
1039 // arguments passed in because it is constant. At some point we
1040 // should remove this need and make the runtime routine entry code
1041 // smarter.
1042 Set(eax, Immediate(num_arguments));
1043 mov(ebx, Immediate(ExternalReference(f)));
1044 CEntryStub ces(1);
1045 return TryCallStub(&ces);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001046}
1047
1048
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001049void MacroAssembler::CallExternalReference(ExternalReference ref,
1050 int num_arguments) {
1051 mov(eax, Immediate(num_arguments));
1052 mov(ebx, Immediate(ref));
1053
1054 CEntryStub stub(1);
1055 CallStub(&stub);
1056}
1057
1058
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001059void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1060 int num_arguments,
1061 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001062 // TODO(1236192): Most runtime routines don't need the number of
1063 // arguments passed in because it is constant. At some point we
1064 // should remove this need and make the runtime routine entry code
1065 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001066 Set(eax, Immediate(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001067 JumpToExternalReference(ext);
1068}
1069
1070
1071void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1072 int num_arguments,
1073 int result_size) {
1074 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075}
1076
1077
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001078void MacroAssembler::PushHandleScope(Register scratch) {
1079 // Push the number of extensions, smi-tagged so the gc will ignore it.
1080 ExternalReference extensions_address =
1081 ExternalReference::handle_scope_extensions_address();
1082 mov(scratch, Operand::StaticVariable(extensions_address));
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001083 SmiTag(scratch);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001084 push(scratch);
1085 mov(Operand::StaticVariable(extensions_address), Immediate(0));
1086 // Push next and limit pointers which will be wordsize aligned and
1087 // hence automatically smi tagged.
1088 ExternalReference next_address =
1089 ExternalReference::handle_scope_next_address();
1090 push(Operand::StaticVariable(next_address));
1091 ExternalReference limit_address =
1092 ExternalReference::handle_scope_limit_address();
1093 push(Operand::StaticVariable(limit_address));
1094}
1095
1096
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001097Object* MacroAssembler::PopHandleScopeHelper(Register saved,
1098 Register scratch,
1099 bool gc_allowed) {
1100 Object* result = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001101 ExternalReference extensions_address =
1102 ExternalReference::handle_scope_extensions_address();
1103 Label write_back;
1104 mov(scratch, Operand::StaticVariable(extensions_address));
1105 cmp(Operand(scratch), Immediate(0));
1106 j(equal, &write_back);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001107 push(saved);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001108 if (gc_allowed) {
1109 CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1110 } else {
1111 result = TryCallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1112 if (result->IsFailure()) return result;
1113 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001114 pop(saved);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001115
1116 bind(&write_back);
1117 ExternalReference limit_address =
1118 ExternalReference::handle_scope_limit_address();
1119 pop(Operand::StaticVariable(limit_address));
1120 ExternalReference next_address =
1121 ExternalReference::handle_scope_next_address();
1122 pop(Operand::StaticVariable(next_address));
1123 pop(scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001124 SmiUntag(scratch);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001125 mov(Operand::StaticVariable(extensions_address), scratch);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001126
1127 return result;
1128}
1129
1130
1131void MacroAssembler::PopHandleScope(Register saved, Register scratch) {
1132 PopHandleScopeHelper(saved, scratch, true);
1133}
1134
1135
1136Object* MacroAssembler::TryPopHandleScope(Register saved, Register scratch) {
1137 return PopHandleScopeHelper(saved, scratch, false);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001138}
1139
1140
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001141void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001143 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001144 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001145 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146}
1147
1148
1149void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1150 const ParameterCount& actual,
1151 Handle<Code> code_constant,
1152 const Operand& code_operand,
1153 Label* done,
1154 InvokeFlag flag) {
1155 bool definitely_matches = false;
1156 Label invoke;
1157 if (expected.is_immediate()) {
1158 ASSERT(actual.is_immediate());
1159 if (expected.immediate() == actual.immediate()) {
1160 definitely_matches = true;
1161 } else {
1162 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001163 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1164 if (expected.immediate() == sentinel) {
1165 // Don't worry about adapting arguments for builtins that
1166 // don't want that done. Skip adaption code by making it look
1167 // like we have a match between expected and actual number of
1168 // arguments.
1169 definitely_matches = true;
1170 } else {
1171 mov(ebx, expected.immediate());
1172 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 }
1174 } else {
1175 if (actual.is_immediate()) {
1176 // Expected is in register, actual is immediate. This is the
1177 // case when we invoke function values without going through the
1178 // IC mechanism.
1179 cmp(expected.reg(), actual.immediate());
1180 j(equal, &invoke);
1181 ASSERT(expected.reg().is(ebx));
1182 mov(eax, actual.immediate());
1183 } else if (!expected.reg().is(actual.reg())) {
1184 // Both expected and actual are in (different) registers. This
1185 // is the case when we invoke functions using call and apply.
1186 cmp(expected.reg(), Operand(actual.reg()));
1187 j(equal, &invoke);
1188 ASSERT(actual.reg().is(eax));
1189 ASSERT(expected.reg().is(ebx));
1190 }
1191 }
1192
1193 if (!definitely_matches) {
1194 Handle<Code> adaptor =
1195 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1196 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001197 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1199 } else if (!code_operand.is_reg(edx)) {
1200 mov(edx, code_operand);
1201 }
1202
1203 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001204 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 jmp(done);
1206 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001207 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 }
1209 bind(&invoke);
1210 }
1211}
1212
1213
1214void MacroAssembler::InvokeCode(const Operand& code,
1215 const ParameterCount& expected,
1216 const ParameterCount& actual,
1217 InvokeFlag flag) {
1218 Label done;
1219 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1220 if (flag == CALL_FUNCTION) {
1221 call(code);
1222 } else {
1223 ASSERT(flag == JUMP_FUNCTION);
1224 jmp(code);
1225 }
1226 bind(&done);
1227}
1228
1229
1230void MacroAssembler::InvokeCode(Handle<Code> code,
1231 const ParameterCount& expected,
1232 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001233 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 InvokeFlag flag) {
1235 Label done;
1236 Operand dummy(eax);
1237 InvokePrologue(expected, actual, code, dummy, &done, flag);
1238 if (flag == CALL_FUNCTION) {
1239 call(code, rmode);
1240 } else {
1241 ASSERT(flag == JUMP_FUNCTION);
1242 jmp(code, rmode);
1243 }
1244 bind(&done);
1245}
1246
1247
1248void MacroAssembler::InvokeFunction(Register fun,
1249 const ParameterCount& actual,
1250 InvokeFlag flag) {
1251 ASSERT(fun.is(edi));
1252 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1253 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1254 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001255 SmiUntag(ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256
1257 ParameterCount expected(ebx);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001258 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1259 expected, actual, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260}
1261
1262
ager@chromium.org5c838252010-02-19 08:53:10 +00001263void MacroAssembler::InvokeFunction(JSFunction* function,
1264 const ParameterCount& actual,
1265 InvokeFlag flag) {
1266 ASSERT(function->is_compiled());
1267 // Get the function and setup the context.
1268 mov(edi, Immediate(Handle<JSFunction>(function)));
1269 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001270 // Invoke the cached code.
1271 Handle<Code> code(function->code());
1272 ParameterCount expected(function->shared()->formal_parameter_count());
1273 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1274}
1275
1276
1277void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001278 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001279 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
1281 // Rely on the assertion to check that the number of provided
1282 // arguments match the expected number of arguments. Fake a
1283 // parameter count to avoid emitting code to do the check.
1284 ParameterCount expected(0);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001285 GetBuiltinFunction(edi, id);
1286 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1287 expected, expected, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288}
1289
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001290void MacroAssembler::GetBuiltinFunction(Register target,
1291 Builtins::JavaScript id) {
1292 // Load the JavaScript builtin function from the builtins object.
1293 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1294 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1295 mov(target, FieldOperand(target,
1296 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1297}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298
1299void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001300 ASSERT(!target.is(edi));
ager@chromium.org5c838252010-02-19 08:53:10 +00001301 // Load the JavaScript builtin function from the builtins object.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001302 GetBuiltinFunction(edi, id);
1303 // Load the code entry point from the function into the target register.
1304 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305}
1306
1307
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001308void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1309 if (context_chain_length > 0) {
1310 // Move up the chain of contexts to the context containing the slot.
1311 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1312 // Load the function context (which is the incoming, outer context).
1313 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1314 for (int i = 1; i < context_chain_length; i++) {
1315 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1316 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1317 }
1318 // The context may be an intermediate context, not a function context.
1319 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1320 } else { // Slot is in the current function context.
1321 // The context may be an intermediate context, not a function context.
1322 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1323 }
1324}
1325
1326
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001327void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1328 // Load the global or builtins object from the current context.
1329 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1330 // Load the global context from the global or builtins object.
1331 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1332 // Load the function from the global context.
1333 mov(function, Operand(function, Context::SlotOffset(index)));
1334}
1335
1336
1337void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1338 Register map) {
1339 // Load the initial map. The global functions all have initial maps.
1340 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1341 if (FLAG_debug_code) {
1342 Label ok, fail;
1343 CheckMap(map, Factory::meta_map(), &fail, false);
1344 jmp(&ok);
1345 bind(&fail);
1346 Abort("Global functions must have initial map");
1347 bind(&ok);
1348 }
1349}
1350
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352void MacroAssembler::Ret() {
1353 ret(0);
1354}
1355
1356
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001357void MacroAssembler::Drop(int stack_elements) {
1358 if (stack_elements > 0) {
1359 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1360 }
1361}
1362
1363
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001364void MacroAssembler::Move(Register dst, Register src) {
1365 if (!dst.is(src)) {
1366 mov(dst, src);
1367 }
1368}
1369
1370
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001371void MacroAssembler::Move(Register dst, Handle<Object> value) {
1372 mov(dst, value);
1373}
1374
1375
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1377 if (FLAG_native_code_counters && counter->Enabled()) {
1378 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1379 }
1380}
1381
1382
1383void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1384 ASSERT(value > 0);
1385 if (FLAG_native_code_counters && counter->Enabled()) {
1386 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1387 if (value == 1) {
1388 inc(operand);
1389 } else {
1390 add(operand, Immediate(value));
1391 }
1392 }
1393}
1394
1395
1396void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1397 ASSERT(value > 0);
1398 if (FLAG_native_code_counters && counter->Enabled()) {
1399 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1400 if (value == 1) {
1401 dec(operand);
1402 } else {
1403 sub(operand, Immediate(value));
1404 }
1405 }
1406}
1407
1408
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001409void MacroAssembler::IncrementCounter(Condition cc,
1410 StatsCounter* counter,
1411 int value) {
1412 ASSERT(value > 0);
1413 if (FLAG_native_code_counters && counter->Enabled()) {
1414 Label skip;
1415 j(NegateCondition(cc), &skip);
1416 pushfd();
1417 IncrementCounter(counter, value);
1418 popfd();
1419 bind(&skip);
1420 }
1421}
1422
1423
1424void MacroAssembler::DecrementCounter(Condition cc,
1425 StatsCounter* counter,
1426 int value) {
1427 ASSERT(value > 0);
1428 if (FLAG_native_code_counters && counter->Enabled()) {
1429 Label skip;
1430 j(NegateCondition(cc), &skip);
1431 pushfd();
1432 DecrementCounter(counter, value);
1433 popfd();
1434 bind(&skip);
1435 }
1436}
1437
1438
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439void MacroAssembler::Assert(Condition cc, const char* msg) {
1440 if (FLAG_debug_code) Check(cc, msg);
1441}
1442
1443
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001444void MacroAssembler::AssertFastElements(Register elements) {
1445 if (FLAG_debug_code) {
1446 Label ok;
1447 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1448 Immediate(Factory::fixed_array_map()));
1449 j(equal, &ok);
1450 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1451 Immediate(Factory::fixed_cow_array_map()));
1452 j(equal, &ok);
1453 Abort("JSObject with fast elements map has slow elements");
1454 bind(&ok);
1455 }
1456}
1457
1458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459void MacroAssembler::Check(Condition cc, const char* msg) {
1460 Label L;
1461 j(cc, &L, taken);
1462 Abort(msg);
1463 // will not return here
1464 bind(&L);
1465}
1466
1467
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001468void MacroAssembler::CheckStackAlignment() {
1469 int frame_alignment = OS::ActivationFrameAlignment();
1470 int frame_alignment_mask = frame_alignment - 1;
1471 if (frame_alignment > kPointerSize) {
1472 ASSERT(IsPowerOf2(frame_alignment));
1473 Label alignment_as_expected;
1474 test(esp, Immediate(frame_alignment_mask));
1475 j(zero, &alignment_as_expected);
1476 // Abort if stack is not aligned.
1477 int3();
1478 bind(&alignment_as_expected);
1479 }
1480}
1481
1482
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483void MacroAssembler::Abort(const char* msg) {
1484 // We want to pass the msg string like a smi to avoid GC
1485 // problems, however msg is not guaranteed to be aligned
1486 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001487 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 // from the real pointer as a smi.
1489 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1490 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1491 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1492#ifdef DEBUG
1493 if (msg != NULL) {
1494 RecordComment("Abort message: ");
1495 RecordComment(msg);
1496 }
1497#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001498 // Disable stub call restrictions to always allow calls to abort.
1499 set_allow_stub_calls(true);
1500
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 push(eax);
1502 push(Immediate(p0));
1503 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1504 CallRuntime(Runtime::kAbort, 2);
1505 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001506 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507}
1508
1509
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001510void MacroAssembler::JumpIfNotNumber(Register reg,
1511 TypeInfo info,
1512 Label* on_not_number) {
1513 if (FLAG_debug_code) AbortIfSmi(reg);
1514 if (!info.IsNumber()) {
1515 cmp(FieldOperand(reg, HeapObject::kMapOffset),
1516 Factory::heap_number_map());
1517 j(not_equal, on_not_number);
1518 }
1519}
1520
1521
1522void MacroAssembler::ConvertToInt32(Register dst,
1523 Register source,
1524 Register scratch,
1525 TypeInfo info,
1526 Label* on_not_int32) {
1527 if (FLAG_debug_code) {
1528 AbortIfSmi(source);
1529 AbortIfNotNumber(source);
1530 }
1531 if (info.IsInteger32()) {
1532 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1533 } else {
1534 Label done;
1535 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1536 ASSERT(!scratch.is(source));
1537 if (push_pop) {
1538 push(dst);
1539 scratch = dst;
1540 }
1541 if (scratch.is(no_reg)) scratch = dst;
1542 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1543 cmp(scratch, 0x80000000u);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001544 if (push_pop) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001545 j(not_equal, &done);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001546 pop(dst);
1547 jmp(on_not_int32);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001548 } else {
1549 j(equal, on_not_int32);
1550 }
1551
1552 bind(&done);
1553 if (push_pop) {
1554 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1555 }
1556 if (!scratch.is(dst)) {
1557 mov(dst, scratch);
1558 }
1559 }
1560}
1561
1562
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001563void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1564 Register scratch,
1565 int power) {
1566 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1567 HeapNumber::kExponentBits));
1568 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1569 movd(dst, Operand(scratch));
1570 psllq(dst, HeapNumber::kMantissaBits);
1571}
1572
1573
ager@chromium.org5c838252010-02-19 08:53:10 +00001574void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1575 Register instance_type,
1576 Register scratch,
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001577 Label* failure) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001578 if (!scratch.is(instance_type)) {
1579 mov(scratch, instance_type);
1580 }
1581 and_(scratch,
1582 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1583 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1584 j(not_equal, failure);
1585}
1586
1587
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001588void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1589 Register object2,
1590 Register scratch1,
1591 Register scratch2,
1592 Label* failure) {
1593 // Check that both objects are not smis.
1594 ASSERT_EQ(0, kSmiTag);
1595 mov(scratch1, Operand(object1));
1596 and_(scratch1, Operand(object2));
1597 test(scratch1, Immediate(kSmiTagMask));
1598 j(zero, failure);
1599
1600 // Load instance type for both strings.
1601 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1602 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1603 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1604 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1605
1606 // Check that both are flat ascii strings.
1607 const int kFlatAsciiStringMask =
1608 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1609 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1610 // Interleave bits from both instance types and compare them in one check.
1611 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1612 and_(scratch1, kFlatAsciiStringMask);
1613 and_(scratch2, kFlatAsciiStringMask);
1614 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1615 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1616 j(not_equal, failure);
1617}
1618
1619
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001620void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1621 int frameAlignment = OS::ActivationFrameAlignment();
1622 if (frameAlignment != 0) {
1623 // Make stack end at alignment and make room for num_arguments words
1624 // and the original value of esp.
1625 mov(scratch, esp);
1626 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1627 ASSERT(IsPowerOf2(frameAlignment));
1628 and_(esp, -frameAlignment);
1629 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1630 } else {
1631 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1632 }
1633}
1634
1635
1636void MacroAssembler::CallCFunction(ExternalReference function,
1637 int num_arguments) {
1638 // Trashing eax is ok as it will be the return value.
1639 mov(Operand(eax), Immediate(function));
1640 CallCFunction(eax, num_arguments);
1641}
1642
1643
1644void MacroAssembler::CallCFunction(Register function,
1645 int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001646 // Check stack alignment.
1647 if (FLAG_debug_code) {
1648 CheckStackAlignment();
1649 }
1650
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001651 call(Operand(function));
1652 if (OS::ActivationFrameAlignment() != 0) {
1653 mov(esp, Operand(esp, num_arguments * kPointerSize));
1654 } else {
1655 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1656 }
1657}
1658
1659
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001661 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001662 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 // The size is adjusted with kGap on order for the assembler to generate size
1664 // bytes of instructions without failing with buffer size constraints.
1665 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1666}
1667
1668
1669CodePatcher::~CodePatcher() {
1670 // Indicate that code has changed.
1671 CPU::FlushICache(address_, size_);
1672
1673 // Check that the code was patched as expected.
1674 ASSERT(masm_.pc_ == address_ + size_);
1675 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1676}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677
1678
1679} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001680
1681#endif // V8_TARGET_ARCH_IA32