blob: 61aadf7ebd432734239813468e60e7373ee8509d [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
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000892// All registers must be distinct. Only current_string needs valid contents
893// on entry. All registers may be invalid on exit. result_operand is
894// unchanged, padding_chars is updated correctly.
895void MacroAssembler::AppendStringToTopOfNewSpace(
896 Register current_string, // Tagged pointer to string to copy.
897 Register current_string_length,
898 Register result_pos,
899 Register scratch,
900 Register new_padding_chars,
901 Operand operand_result,
902 Operand operand_padding_chars,
903 Label* bailout) {
904 mov(current_string_length,
905 FieldOperand(current_string, String::kLengthOffset));
906 shr(current_string_length, 1);
907 sub(current_string_length, operand_padding_chars);
908 mov(new_padding_chars, current_string_length);
909 add(Operand(current_string_length), Immediate(kObjectAlignmentMask));
910 and_(Operand(current_string_length), Immediate(~kObjectAlignmentMask));
911 sub(new_padding_chars, Operand(current_string_length));
912 neg(new_padding_chars);
913 // We need an allocation even if current_string_length is 0, to fetch
914 // result_pos. Consider using a faster fetch of result_pos in that case.
915 AllocateInNewSpace(current_string_length, result_pos, scratch, no_reg,
916 bailout, NO_ALLOCATION_FLAGS);
917 sub(result_pos, operand_padding_chars);
918 mov(operand_padding_chars, new_padding_chars);
919
920 Register scratch_2 = new_padding_chars; // Used to compute total length.
921 // Copy string to the end of result.
922 mov(current_string_length,
923 FieldOperand(current_string, String::kLengthOffset));
924 mov(scratch, operand_result);
925 mov(scratch_2, current_string_length);
926 add(scratch_2, FieldOperand(scratch, String::kLengthOffset));
927 mov(FieldOperand(scratch, String::kLengthOffset), scratch_2);
928 shr(current_string_length, 1);
929 lea(current_string,
930 FieldOperand(current_string, SeqAsciiString::kHeaderSize));
931 // Loop condition: while (--current_string_length >= 0).
932 Label copy_loop;
933 Label copy_loop_entry;
934 jmp(&copy_loop_entry);
935 bind(&copy_loop);
936 mov_b(scratch, Operand(current_string, current_string_length, times_1, 0));
937 mov_b(Operand(result_pos, current_string_length, times_1, 0), scratch);
938 bind(&copy_loop_entry);
939 sub(Operand(current_string_length), Immediate(1));
940 j(greater_equal, &copy_loop);
941}
942
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000943
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000944void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
945 Register result,
946 Register op,
947 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000948 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000949 test(result, Operand(result));
950 ok.Branch(not_zero, taken);
951 test(op, Operand(op));
952 then_target->Branch(sign, not_taken);
953 ok.Bind();
954}
955
956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957void MacroAssembler::NegativeZeroTest(Register result,
958 Register op,
959 Label* then_label) {
960 Label ok;
961 test(result, Operand(result));
962 j(not_zero, &ok, taken);
963 test(op, Operand(op));
964 j(sign, then_label, not_taken);
965 bind(&ok);
966}
967
968
969void MacroAssembler::NegativeZeroTest(Register result,
970 Register op1,
971 Register op2,
972 Register scratch,
973 Label* then_label) {
974 Label ok;
975 test(result, Operand(result));
976 j(not_zero, &ok, taken);
977 mov(scratch, Operand(op1));
978 or_(scratch, Operand(op2));
979 j(sign, then_label, not_taken);
980 bind(&ok);
981}
982
983
ager@chromium.org7c537e22008-10-16 08:43:32 +0000984void MacroAssembler::TryGetFunctionPrototype(Register function,
985 Register result,
986 Register scratch,
987 Label* miss) {
988 // Check that the receiver isn't a smi.
989 test(function, Immediate(kSmiTagMask));
990 j(zero, miss, not_taken);
991
992 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000993 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000994 j(not_equal, miss, not_taken);
995
996 // Make sure that the function has an instance prototype.
997 Label non_instance;
998 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
999 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1000 j(not_zero, &non_instance, not_taken);
1001
1002 // Get the prototype or initial map from the function.
1003 mov(result,
1004 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1005
1006 // If the prototype or initial map is the hole, don't return it and
1007 // simply miss the cache instead. This will allow us to allocate a
1008 // prototype object on-demand in the runtime system.
1009 cmp(Operand(result), Immediate(Factory::the_hole_value()));
1010 j(equal, miss, not_taken);
1011
1012 // If the function does not have an initial map, we're done.
1013 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001014 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001015 j(not_equal, &done);
1016
1017 // Get the prototype from the initial map.
1018 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1019 jmp(&done);
1020
1021 // Non-instance prototype: Fetch prototype from constructor field
1022 // in initial map.
1023 bind(&non_instance);
1024 mov(result, FieldOperand(result, Map::kConstructorOffset));
1025
1026 // All done.
1027 bind(&done);
1028}
1029
1030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001032 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +00001033 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034}
1035
1036
lrn@chromium.org303ada72010-10-27 09:33:13 +00001037MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001038 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001039 Object* result;
1040 { MaybeObject* maybe_result = stub->TryGetCode();
1041 if (!maybe_result->ToObject(&result)) return maybe_result;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001042 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001043 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001044 return result;
1045}
1046
1047
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001048void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001049 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001050 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1051}
1052
1053
lrn@chromium.org303ada72010-10-27 09:33:13 +00001054MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001055 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001056 Object* result;
1057 { MaybeObject* maybe_result = stub->TryGetCode();
1058 if (!maybe_result->ToObject(&result)) return maybe_result;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001059 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001060 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001061 return result;
1062}
1063
1064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065void MacroAssembler::StubReturn(int argc) {
1066 ASSERT(argc >= 1 && generating_stub());
1067 ret((argc - 1) * kPointerSize);
1068}
1069
1070
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001071void MacroAssembler::IllegalOperation(int num_arguments) {
1072 if (num_arguments > 0) {
1073 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1074 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001075 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076}
1077
1078
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001079void MacroAssembler::IndexFromHash(Register hash, Register index) {
1080 // The assert checks that the constants for the maximum number of digits
1081 // for an array index cached in the hash field and the number of bits
1082 // reserved for it does not conflict.
1083 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1084 (1 << String::kArrayIndexValueBits));
1085 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1086 // the low kHashShift bits.
1087 and_(hash, String::kArrayIndexValueMask);
1088 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1089 if (String::kHashShift > kSmiTagSize) {
1090 shr(hash, String::kHashShift - kSmiTagSize);
1091 }
1092 if (!index.is(hash)) {
1093 mov(index, hash);
1094 }
1095}
1096
1097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1099 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1100}
1101
1102
lrn@chromium.org303ada72010-10-27 09:33:13 +00001103MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1104 int num_arguments) {
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001105 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1106}
1107
1108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001110 // If the expected number of arguments of the runtime function is
1111 // constant, we check that the actual number of arguments match the
1112 // expectation.
1113 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001114 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 return;
1116 }
1117
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001118 // TODO(1236192): Most runtime routines don't need the number of
1119 // arguments passed in because it is constant. At some point we
1120 // should remove this need and make the runtime routine entry code
1121 // smarter.
1122 Set(eax, Immediate(num_arguments));
1123 mov(ebx, Immediate(ExternalReference(f)));
1124 CEntryStub ces(1);
1125 CallStub(&ces);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126}
1127
1128
lrn@chromium.org303ada72010-10-27 09:33:13 +00001129MaybeObject* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1130 int num_arguments) {
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001131 if (f->nargs >= 0 && f->nargs != num_arguments) {
1132 IllegalOperation(num_arguments);
1133 // Since we did not call the stub, there was no allocation failure.
1134 // Return some non-failure object.
1135 return Heap::undefined_value();
1136 }
1137
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001138 // TODO(1236192): Most runtime routines don't need the number of
1139 // arguments passed in because it is constant. At some point we
1140 // should remove this need and make the runtime routine entry code
1141 // smarter.
1142 Set(eax, Immediate(num_arguments));
1143 mov(ebx, Immediate(ExternalReference(f)));
1144 CEntryStub ces(1);
1145 return TryCallStub(&ces);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001146}
1147
1148
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001149void MacroAssembler::CallExternalReference(ExternalReference ref,
1150 int num_arguments) {
1151 mov(eax, Immediate(num_arguments));
1152 mov(ebx, Immediate(ref));
1153
1154 CEntryStub stub(1);
1155 CallStub(&stub);
1156}
1157
1158
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001159void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1160 int num_arguments,
1161 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001162 // TODO(1236192): Most runtime routines don't need the number of
1163 // arguments passed in because it is constant. At some point we
1164 // should remove this need and make the runtime routine entry code
1165 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001166 Set(eax, Immediate(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001167 JumpToExternalReference(ext);
1168}
1169
1170
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001171MaybeObject* MacroAssembler::TryTailCallExternalReference(
1172 const ExternalReference& ext, int num_arguments, int result_size) {
1173 // TODO(1236192): Most runtime routines don't need the number of
1174 // arguments passed in because it is constant. At some point we
1175 // should remove this need and make the runtime routine entry code
1176 // smarter.
1177 Set(eax, Immediate(num_arguments));
1178 return TryJumpToExternalReference(ext);
1179}
1180
1181
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001182void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1183 int num_arguments,
1184 int result_size) {
1185 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186}
1187
1188
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001189MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1190 int num_arguments,
1191 int result_size) {
1192 return TryTailCallExternalReference(
1193 ExternalReference(fid), num_arguments, result_size);
1194}
1195
1196
lrn@chromium.org303ada72010-10-27 09:33:13 +00001197// If true, a Handle<T> passed by value is passed and returned by
1198// using the location_ field directly. If false, it is passed and
1199// returned as a pointer to a handle.
1200#ifdef USING_BSD_ABI
1201static const bool kPassHandlesDirectly = true;
1202#else
1203static const bool kPassHandlesDirectly = false;
1204#endif
1205
1206
1207Operand ApiParameterOperand(int index) {
1208 return Operand(esp, (index + (kPassHandlesDirectly ? 0 : 1)) * kPointerSize);
1209}
1210
1211
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001212void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001213 if (kPassHandlesDirectly) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001214 EnterApiExitFrame(argc);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001215 // When handles as passed directly we don't have to allocate extra
1216 // space for and pass an out parameter.
1217 } else {
1218 // We allocate two additional slots: return value and pointer to it.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001219 EnterApiExitFrame(argc + 2);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001220
lrn@chromium.org303ada72010-10-27 09:33:13 +00001221 // The argument slots are filled as follows:
1222 //
1223 // n + 1: output cell
1224 // n: arg n
1225 // ...
1226 // 1: arg1
1227 // 0: pointer to the output cell
1228 //
1229 // Note that this is one more "argument" than the function expects
1230 // so the out cell will have to be popped explicitly after returning
1231 // from the function. The out cell contains Handle.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001232
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001233 // pointer to out cell.
1234 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1235 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
1236 if (FLAG_debug_code) {
1237 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1238 }
1239 }
1240}
1241
1242
1243MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1244 int stack_space) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001245 ExternalReference next_address =
1246 ExternalReference::handle_scope_next_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001247 ExternalReference limit_address =
1248 ExternalReference::handle_scope_limit_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001249 ExternalReference level_address =
1250 ExternalReference::handle_scope_level_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001251
lrn@chromium.org303ada72010-10-27 09:33:13 +00001252 // Allocate HandleScope in callee-save registers.
1253 mov(ebx, Operand::StaticVariable(next_address));
1254 mov(edi, Operand::StaticVariable(limit_address));
1255 add(Operand::StaticVariable(level_address), Immediate(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001256
lrn@chromium.org303ada72010-10-27 09:33:13 +00001257 // Call the api function!
1258 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1259
1260 if (!kPassHandlesDirectly) {
1261 // The returned value is a pointer to the handle holding the result.
1262 // Dereference this to get to the location.
1263 mov(eax, Operand(eax, 0));
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001264 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001265
lrn@chromium.org303ada72010-10-27 09:33:13 +00001266 Label empty_handle;
1267 Label prologue;
1268 Label promote_scheduled_exception;
1269 Label delete_allocated_handles;
1270 Label leave_exit_frame;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001271
lrn@chromium.org303ada72010-10-27 09:33:13 +00001272 // Check if the result handle holds 0.
1273 test(eax, Operand(eax));
1274 j(zero, &empty_handle, not_taken);
1275 // It was non-zero. Dereference to get the result value.
1276 mov(eax, Operand(eax, 0));
1277 bind(&prologue);
1278 // No more valid handles (the result handle was the last one). Restore
1279 // previous handle scope.
1280 mov(Operand::StaticVariable(next_address), ebx);
1281 sub(Operand::StaticVariable(level_address), Immediate(1));
1282 Assert(above_equal, "Invalid HandleScope level");
1283 cmp(edi, Operand::StaticVariable(limit_address));
1284 j(not_equal, &delete_allocated_handles, not_taken);
1285 bind(&leave_exit_frame);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001286
lrn@chromium.org303ada72010-10-27 09:33:13 +00001287 // Check if the function scheduled an exception.
1288 ExternalReference scheduled_exception_address =
1289 ExternalReference::scheduled_exception_address();
1290 cmp(Operand::StaticVariable(scheduled_exception_address),
1291 Immediate(Factory::the_hole_value()));
1292 j(not_equal, &promote_scheduled_exception, not_taken);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001293 LeaveApiExitFrame();
1294 ret(stack_space * kPointerSize);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001295 bind(&promote_scheduled_exception);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001296 MaybeObject* result =
1297 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1298 if (result->IsFailure()) {
1299 return result;
1300 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001301 bind(&empty_handle);
1302 // It was zero; the result is undefined.
1303 mov(eax, Factory::undefined_value());
1304 jmp(&prologue);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001305
lrn@chromium.org303ada72010-10-27 09:33:13 +00001306 // HandleScope limit has changed. Delete allocated extensions.
1307 bind(&delete_allocated_handles);
1308 mov(Operand::StaticVariable(limit_address), edi);
1309 mov(edi, eax);
1310 mov(eax, Immediate(ExternalReference::delete_handle_scope_extensions()));
1311 call(Operand(eax));
1312 mov(eax, edi);
1313 jmp(&leave_exit_frame);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001314
1315 return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001316}
1317
1318
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001319void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001321 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001322 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001323 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324}
1325
1326
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001327MaybeObject* MacroAssembler::TryJumpToExternalReference(
1328 const ExternalReference& ext) {
1329 // Set the entry point and jump to the C entry runtime stub.
1330 mov(ebx, Immediate(ext));
1331 CEntryStub ces(1);
1332 return TryTailCallStub(&ces);
1333}
1334
1335
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1337 const ParameterCount& actual,
1338 Handle<Code> code_constant,
1339 const Operand& code_operand,
1340 Label* done,
1341 InvokeFlag flag) {
1342 bool definitely_matches = false;
1343 Label invoke;
1344 if (expected.is_immediate()) {
1345 ASSERT(actual.is_immediate());
1346 if (expected.immediate() == actual.immediate()) {
1347 definitely_matches = true;
1348 } else {
1349 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001350 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1351 if (expected.immediate() == sentinel) {
1352 // Don't worry about adapting arguments for builtins that
1353 // don't want that done. Skip adaption code by making it look
1354 // like we have a match between expected and actual number of
1355 // arguments.
1356 definitely_matches = true;
1357 } else {
1358 mov(ebx, expected.immediate());
1359 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 }
1361 } else {
1362 if (actual.is_immediate()) {
1363 // Expected is in register, actual is immediate. This is the
1364 // case when we invoke function values without going through the
1365 // IC mechanism.
1366 cmp(expected.reg(), actual.immediate());
1367 j(equal, &invoke);
1368 ASSERT(expected.reg().is(ebx));
1369 mov(eax, actual.immediate());
1370 } else if (!expected.reg().is(actual.reg())) {
1371 // Both expected and actual are in (different) registers. This
1372 // is the case when we invoke functions using call and apply.
1373 cmp(expected.reg(), Operand(actual.reg()));
1374 j(equal, &invoke);
1375 ASSERT(actual.reg().is(eax));
1376 ASSERT(expected.reg().is(ebx));
1377 }
1378 }
1379
1380 if (!definitely_matches) {
1381 Handle<Code> adaptor =
1382 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1383 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001384 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1386 } else if (!code_operand.is_reg(edx)) {
1387 mov(edx, code_operand);
1388 }
1389
1390 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001391 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 jmp(done);
1393 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001394 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 }
1396 bind(&invoke);
1397 }
1398}
1399
1400
1401void MacroAssembler::InvokeCode(const Operand& code,
1402 const ParameterCount& expected,
1403 const ParameterCount& actual,
1404 InvokeFlag flag) {
1405 Label done;
1406 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1407 if (flag == CALL_FUNCTION) {
1408 call(code);
1409 } else {
1410 ASSERT(flag == JUMP_FUNCTION);
1411 jmp(code);
1412 }
1413 bind(&done);
1414}
1415
1416
1417void MacroAssembler::InvokeCode(Handle<Code> code,
1418 const ParameterCount& expected,
1419 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001420 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 InvokeFlag flag) {
1422 Label done;
1423 Operand dummy(eax);
1424 InvokePrologue(expected, actual, code, dummy, &done, flag);
1425 if (flag == CALL_FUNCTION) {
1426 call(code, rmode);
1427 } else {
1428 ASSERT(flag == JUMP_FUNCTION);
1429 jmp(code, rmode);
1430 }
1431 bind(&done);
1432}
1433
1434
1435void MacroAssembler::InvokeFunction(Register fun,
1436 const ParameterCount& actual,
1437 InvokeFlag flag) {
1438 ASSERT(fun.is(edi));
1439 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1440 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1441 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001442 SmiUntag(ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443
1444 ParameterCount expected(ebx);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001445 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1446 expected, actual, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447}
1448
1449
ager@chromium.org5c838252010-02-19 08:53:10 +00001450void MacroAssembler::InvokeFunction(JSFunction* function,
1451 const ParameterCount& actual,
1452 InvokeFlag flag) {
1453 ASSERT(function->is_compiled());
1454 // Get the function and setup the context.
1455 mov(edi, Immediate(Handle<JSFunction>(function)));
1456 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +00001457 // Invoke the cached code.
1458 Handle<Code> code(function->code());
1459 ParameterCount expected(function->shared()->formal_parameter_count());
1460 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1461}
1462
1463
1464void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001466 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467
1468 // Rely on the assertion to check that the number of provided
1469 // arguments match the expected number of arguments. Fake a
1470 // parameter count to avoid emitting code to do the check.
1471 ParameterCount expected(0);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001472 GetBuiltinFunction(edi, id);
1473 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1474 expected, expected, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475}
1476
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001477void MacroAssembler::GetBuiltinFunction(Register target,
1478 Builtins::JavaScript id) {
1479 // Load the JavaScript builtin function from the builtins object.
1480 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1481 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1482 mov(target, FieldOperand(target,
1483 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1484}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485
1486void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001487 ASSERT(!target.is(edi));
ager@chromium.org5c838252010-02-19 08:53:10 +00001488 // Load the JavaScript builtin function from the builtins object.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001489 GetBuiltinFunction(edi, id);
1490 // Load the code entry point from the function into the target register.
1491 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492}
1493
1494
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001495void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1496 if (context_chain_length > 0) {
1497 // Move up the chain of contexts to the context containing the slot.
1498 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1499 // Load the function context (which is the incoming, outer context).
1500 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1501 for (int i = 1; i < context_chain_length; i++) {
1502 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1503 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1504 }
1505 // The context may be an intermediate context, not a function context.
1506 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1507 } else { // Slot is in the current function context.
1508 // The context may be an intermediate context, not a function context.
1509 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1510 }
1511}
1512
1513
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001514void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1515 // Load the global or builtins object from the current context.
1516 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1517 // Load the global context from the global or builtins object.
1518 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1519 // Load the function from the global context.
1520 mov(function, Operand(function, Context::SlotOffset(index)));
1521}
1522
1523
1524void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1525 Register map) {
1526 // Load the initial map. The global functions all have initial maps.
1527 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1528 if (FLAG_debug_code) {
1529 Label ok, fail;
1530 CheckMap(map, Factory::meta_map(), &fail, false);
1531 jmp(&ok);
1532 bind(&fail);
1533 Abort("Global functions must have initial map");
1534 bind(&ok);
1535 }
1536}
1537
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539void MacroAssembler::Ret() {
1540 ret(0);
1541}
1542
1543
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001544void MacroAssembler::Drop(int stack_elements) {
1545 if (stack_elements > 0) {
1546 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1547 }
1548}
1549
1550
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001551void MacroAssembler::Move(Register dst, Register src) {
1552 if (!dst.is(src)) {
1553 mov(dst, src);
1554 }
1555}
1556
1557
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001558void MacroAssembler::Move(Register dst, Handle<Object> value) {
1559 mov(dst, value);
1560}
1561
1562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1564 if (FLAG_native_code_counters && counter->Enabled()) {
1565 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1566 }
1567}
1568
1569
1570void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1571 ASSERT(value > 0);
1572 if (FLAG_native_code_counters && counter->Enabled()) {
1573 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1574 if (value == 1) {
1575 inc(operand);
1576 } else {
1577 add(operand, Immediate(value));
1578 }
1579 }
1580}
1581
1582
1583void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1584 ASSERT(value > 0);
1585 if (FLAG_native_code_counters && counter->Enabled()) {
1586 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1587 if (value == 1) {
1588 dec(operand);
1589 } else {
1590 sub(operand, Immediate(value));
1591 }
1592 }
1593}
1594
1595
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001596void MacroAssembler::IncrementCounter(Condition cc,
1597 StatsCounter* counter,
1598 int value) {
1599 ASSERT(value > 0);
1600 if (FLAG_native_code_counters && counter->Enabled()) {
1601 Label skip;
1602 j(NegateCondition(cc), &skip);
1603 pushfd();
1604 IncrementCounter(counter, value);
1605 popfd();
1606 bind(&skip);
1607 }
1608}
1609
1610
1611void MacroAssembler::DecrementCounter(Condition cc,
1612 StatsCounter* counter,
1613 int value) {
1614 ASSERT(value > 0);
1615 if (FLAG_native_code_counters && counter->Enabled()) {
1616 Label skip;
1617 j(NegateCondition(cc), &skip);
1618 pushfd();
1619 DecrementCounter(counter, value);
1620 popfd();
1621 bind(&skip);
1622 }
1623}
1624
1625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626void MacroAssembler::Assert(Condition cc, const char* msg) {
1627 if (FLAG_debug_code) Check(cc, msg);
1628}
1629
1630
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001631void MacroAssembler::AssertFastElements(Register elements) {
1632 if (FLAG_debug_code) {
1633 Label ok;
1634 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1635 Immediate(Factory::fixed_array_map()));
1636 j(equal, &ok);
1637 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1638 Immediate(Factory::fixed_cow_array_map()));
1639 j(equal, &ok);
1640 Abort("JSObject with fast elements map has slow elements");
1641 bind(&ok);
1642 }
1643}
1644
1645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646void MacroAssembler::Check(Condition cc, const char* msg) {
1647 Label L;
1648 j(cc, &L, taken);
1649 Abort(msg);
1650 // will not return here
1651 bind(&L);
1652}
1653
1654
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001655void MacroAssembler::CheckStackAlignment() {
1656 int frame_alignment = OS::ActivationFrameAlignment();
1657 int frame_alignment_mask = frame_alignment - 1;
1658 if (frame_alignment > kPointerSize) {
1659 ASSERT(IsPowerOf2(frame_alignment));
1660 Label alignment_as_expected;
1661 test(esp, Immediate(frame_alignment_mask));
1662 j(zero, &alignment_as_expected);
1663 // Abort if stack is not aligned.
1664 int3();
1665 bind(&alignment_as_expected);
1666 }
1667}
1668
1669
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670void MacroAssembler::Abort(const char* msg) {
1671 // We want to pass the msg string like a smi to avoid GC
1672 // problems, however msg is not guaranteed to be aligned
1673 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001674 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 // from the real pointer as a smi.
1676 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1677 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1678 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1679#ifdef DEBUG
1680 if (msg != NULL) {
1681 RecordComment("Abort message: ");
1682 RecordComment(msg);
1683 }
1684#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001685 // Disable stub call restrictions to always allow calls to abort.
1686 set_allow_stub_calls(true);
1687
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 push(eax);
1689 push(Immediate(p0));
1690 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1691 CallRuntime(Runtime::kAbort, 2);
1692 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001693 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694}
1695
1696
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001697void MacroAssembler::JumpIfNotNumber(Register reg,
1698 TypeInfo info,
1699 Label* on_not_number) {
1700 if (FLAG_debug_code) AbortIfSmi(reg);
1701 if (!info.IsNumber()) {
1702 cmp(FieldOperand(reg, HeapObject::kMapOffset),
1703 Factory::heap_number_map());
1704 j(not_equal, on_not_number);
1705 }
1706}
1707
1708
1709void MacroAssembler::ConvertToInt32(Register dst,
1710 Register source,
1711 Register scratch,
1712 TypeInfo info,
1713 Label* on_not_int32) {
1714 if (FLAG_debug_code) {
1715 AbortIfSmi(source);
1716 AbortIfNotNumber(source);
1717 }
1718 if (info.IsInteger32()) {
1719 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1720 } else {
1721 Label done;
1722 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1723 ASSERT(!scratch.is(source));
1724 if (push_pop) {
1725 push(dst);
1726 scratch = dst;
1727 }
1728 if (scratch.is(no_reg)) scratch = dst;
1729 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1730 cmp(scratch, 0x80000000u);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001731 if (push_pop) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001732 j(not_equal, &done);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001733 pop(dst);
1734 jmp(on_not_int32);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001735 } else {
1736 j(equal, on_not_int32);
1737 }
1738
1739 bind(&done);
1740 if (push_pop) {
1741 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1742 }
1743 if (!scratch.is(dst)) {
1744 mov(dst, scratch);
1745 }
1746 }
1747}
1748
1749
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001750void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1751 Register scratch,
1752 int power) {
1753 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1754 HeapNumber::kExponentBits));
1755 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1756 movd(dst, Operand(scratch));
1757 psllq(dst, HeapNumber::kMantissaBits);
1758}
1759
1760
ager@chromium.org5c838252010-02-19 08:53:10 +00001761void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1762 Register instance_type,
1763 Register scratch,
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001764 Label* failure) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001765 if (!scratch.is(instance_type)) {
1766 mov(scratch, instance_type);
1767 }
1768 and_(scratch,
1769 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1770 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1771 j(not_equal, failure);
1772}
1773
1774
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001775void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1776 Register object2,
1777 Register scratch1,
1778 Register scratch2,
1779 Label* failure) {
1780 // Check that both objects are not smis.
1781 ASSERT_EQ(0, kSmiTag);
1782 mov(scratch1, Operand(object1));
1783 and_(scratch1, Operand(object2));
1784 test(scratch1, Immediate(kSmiTagMask));
1785 j(zero, failure);
1786
1787 // Load instance type for both strings.
1788 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1789 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1790 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1791 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1792
1793 // Check that both are flat ascii strings.
1794 const int kFlatAsciiStringMask =
1795 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1796 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1797 // Interleave bits from both instance types and compare them in one check.
1798 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1799 and_(scratch1, kFlatAsciiStringMask);
1800 and_(scratch2, kFlatAsciiStringMask);
1801 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1802 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1803 j(not_equal, failure);
1804}
1805
1806
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001807void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1808 int frameAlignment = OS::ActivationFrameAlignment();
1809 if (frameAlignment != 0) {
1810 // Make stack end at alignment and make room for num_arguments words
1811 // and the original value of esp.
1812 mov(scratch, esp);
1813 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1814 ASSERT(IsPowerOf2(frameAlignment));
1815 and_(esp, -frameAlignment);
1816 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1817 } else {
1818 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1819 }
1820}
1821
1822
1823void MacroAssembler::CallCFunction(ExternalReference function,
1824 int num_arguments) {
1825 // Trashing eax is ok as it will be the return value.
1826 mov(Operand(eax), Immediate(function));
1827 CallCFunction(eax, num_arguments);
1828}
1829
1830
1831void MacroAssembler::CallCFunction(Register function,
1832 int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001833 // Check stack alignment.
1834 if (FLAG_debug_code) {
1835 CheckStackAlignment();
1836 }
1837
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001838 call(Operand(function));
1839 if (OS::ActivationFrameAlignment() != 0) {
1840 mov(esp, Operand(esp, num_arguments * kPointerSize));
1841 } else {
1842 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1843 }
1844}
1845
1846
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001848 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001849 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 // The size is adjusted with kGap on order for the assembler to generate size
1851 // bytes of instructions without failing with buffer size constraints.
1852 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1853}
1854
1855
1856CodePatcher::~CodePatcher() {
1857 // Indicate that code has changed.
1858 CPU::FlushICache(address_, size_);
1859
1860 // Check that the code was patched as expected.
1861 ASSERT(masm_.pc_ == address_ + size_);
1862 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1863}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864
1865
1866} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001867
1868#endif // V8_TARGET_ARCH_IA32