blob: 03f726ca9bcd6b9708ce28861e60c6490c4c91ab [file] [log] [blame]
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001// Copyright 2011 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
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +000077void MacroAssembler::RecordWrite(Register object,
78 int offset,
79 Register value,
80 Register scratch) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000081 // First, check if a write barrier is even needed. The tests below
82 // catch stores of Smis and stores into young gen.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000083 NearLabel done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000085 // Skip barrier if writing a smi.
86 ASSERT_EQ(0, kSmiTag);
87 test(value, Immediate(kSmiTagMask));
88 j(zero, &done);
89
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000090 InNewSpace(object, value, equal, &done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
whesse@chromium.orge88a9ed2010-04-15 15:07:46 +000092 // The offset is relative to a tagged or untagged HeapObject pointer,
93 // so either offset or offset + kHeapObjectTag must be a
94 // multiple of kPointerSize.
95 ASSERT(IsAligned(offset, kPointerSize) ||
96 IsAligned(offset + kHeapObjectTag, kPointerSize));
97
ricow@chromium.org30ce4112010-05-31 10:38:25 +000098 Register dst = scratch;
99 if (offset != 0) {
100 lea(dst, Operand(object, offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000102 // Array access: calculate the destination address in the same manner as
103 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
104 // into an array of words.
105 ASSERT_EQ(1, kSmiTagSize);
106 ASSERT_EQ(0, kSmiTag);
107 lea(dst, Operand(object, dst, times_half_pointer_size,
108 FixedArray::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000110 RecordWriteHelper(object, dst, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111
112 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000113
114 // Clobber all input registers when running with the debug-code flag
115 // turned on to provoke errors.
116 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000117 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
118 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
119 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000120 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121}
122
123
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000124void MacroAssembler::RecordWrite(Register object,
125 Register address,
126 Register value) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000127 // First, check if a write barrier is even needed. The tests below
128 // catch stores of Smis and stores into young gen.
129 Label done;
130
131 // Skip barrier if writing a smi.
132 ASSERT_EQ(0, kSmiTag);
133 test(value, Immediate(kSmiTagMask));
134 j(zero, &done);
135
136 InNewSpace(object, value, equal, &done);
137
138 RecordWriteHelper(object, address, value);
139
140 bind(&done);
141
142 // Clobber all input registers when running with the debug-code flag
143 // turned on to provoke errors.
144 if (FLAG_debug_code) {
145 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
146 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
147 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
148 }
149}
150
151
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000152#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +0000153void MacroAssembler::DebugBreak() {
154 Set(eax, Immediate(0));
155 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak)));
156 CEntryStub ces(1);
157 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
158}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000159#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162void MacroAssembler::Set(Register dst, const Immediate& x) {
163 if (x.is_zero()) {
164 xor_(dst, Operand(dst)); // shorter than mov
165 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000166 mov(dst, x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 }
168}
169
170
171void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
172 mov(dst, x);
173}
174
175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000176void MacroAssembler::CmpObjectType(Register heap_object,
177 InstanceType type,
178 Register map) {
179 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
180 CmpInstanceType(map, type);
181}
182
183
184void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
185 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
186 static_cast<int8_t>(type));
187}
188
189
ager@chromium.org5c838252010-02-19 08:53:10 +0000190void MacroAssembler::CheckMap(Register obj,
191 Handle<Map> map,
192 Label* fail,
193 bool is_heap_object) {
194 if (!is_heap_object) {
195 test(obj, Immediate(kSmiTagMask));
196 j(zero, fail);
197 }
198 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
199 j(not_equal, fail);
200}
201
202
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000203Condition MacroAssembler::IsObjectStringType(Register heap_object,
204 Register map,
205 Register instance_type) {
206 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
207 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
208 ASSERT(kNotStringTag != 0);
209 test(instance_type, Immediate(kIsNotStringMask));
210 return zero;
211}
212
213
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000214void MacroAssembler::IsObjectJSObjectType(Register heap_object,
215 Register map,
216 Register scratch,
217 Label* fail) {
218 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
219 IsInstanceJSObjectType(map, scratch, fail);
220}
221
222
223void MacroAssembler::IsInstanceJSObjectType(Register map,
224 Register scratch,
225 Label* fail) {
226 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
227 sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE));
228 cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE);
229 j(above, fail);
230}
231
232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233void MacroAssembler::FCmp() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000234 if (CpuFeatures::IsSupported(CMOV)) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000235 fucomip();
236 ffree(0);
237 fincstp();
238 } else {
239 fucompp();
240 push(eax);
241 fnstsw_ax();
242 sahf();
243 pop(eax);
244 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245}
246
247
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000248void MacroAssembler::AbortIfNotNumber(Register object) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000249 Label ok;
250 test(object, Immediate(kSmiTagMask));
251 j(zero, &ok);
252 cmp(FieldOperand(object, HeapObject::kMapOffset),
253 Factory::heap_number_map());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000254 Assert(equal, "Operand not a number");
ager@chromium.org5c838252010-02-19 08:53:10 +0000255 bind(&ok);
256}
257
258
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000259void MacroAssembler::AbortIfNotSmi(Register object) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000260 test(object, Immediate(kSmiTagMask));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000261 Assert(equal, "Operand is not a smi");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000262}
263
264
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000265void MacroAssembler::AbortIfNotString(Register object) {
266 test(object, Immediate(kSmiTagMask));
267 Assert(not_equal, "Operand is not a string");
268 push(object);
269 mov(object, FieldOperand(object, HeapObject::kMapOffset));
270 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
271 pop(object);
272 Assert(below, "Operand is not a string");
273}
274
275
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000276void MacroAssembler::AbortIfSmi(Register object) {
277 test(object, Immediate(kSmiTagMask));
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000278 Assert(not_equal, "Operand is a smi");
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000279}
280
281
ager@chromium.org7c537e22008-10-16 08:43:32 +0000282void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 push(ebp);
284 mov(ebp, Operand(esp));
285 push(esi);
286 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000287 push(Immediate(CodeObject()));
288 if (FLAG_debug_code) {
289 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
290 Check(not_equal, "code object not properly patched");
291 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292}
293
294
ager@chromium.org7c537e22008-10-16 08:43:32 +0000295void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 if (FLAG_debug_code) {
297 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
298 Immediate(Smi::FromInt(type)));
299 Check(equal, "stack frame types must match");
300 }
301 leave();
302}
303
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000304
305void MacroAssembler::EnterExitFramePrologue() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000306 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000307 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000308 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
309 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
310 push(ebp);
311 mov(ebp, Operand(esp));
312
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000313 // Reserve room for entry stack pointer and push the code object.
ager@chromium.org236ad962008-09-25 09:45:57 +0000314 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +0000315 push(Immediate(0)); // Saved entry sp, patched before call.
316 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000317
318 // Save the frame pointer and the context in top.
319 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
320 ExternalReference context_address(Top::k_context_address);
321 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
322 mov(Operand::StaticVariable(context_address), esi);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000323}
ager@chromium.org236ad962008-09-25 09:45:57 +0000324
ager@chromium.org236ad962008-09-25 09:45:57 +0000325
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000326void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
327 // Optionally save all XMM registers.
328 if (save_doubles) {
329 CpuFeatures::Scope scope(SSE2);
330 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
331 sub(Operand(esp), Immediate(space));
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000332 const int offset = -2 * kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000333 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
334 XMMRegister reg = XMMRegister::from_code(i);
335 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
336 }
337 } else {
338 sub(Operand(esp), Immediate(argc * kPointerSize));
339 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000340
341 // Get the required frame alignment for the OS.
342 static const int kFrameAlignment = OS::ActivationFrameAlignment();
343 if (kFrameAlignment > 0) {
344 ASSERT(IsPowerOf2(kFrameAlignment));
345 and_(esp, -kFrameAlignment);
346 }
347
348 // Patch the saved entry sp.
349 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
350}
351
352
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000353void MacroAssembler::EnterExitFrame(bool save_doubles) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000354 EnterExitFramePrologue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000355
356 // Setup argc and argv in callee-saved registers.
357 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
358 mov(edi, Operand(eax));
359 lea(esi, Operand(ebp, eax, times_4, offset));
360
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000361 EnterExitFrameEpilogue(2, save_doubles);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000362}
363
364
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000365void MacroAssembler::EnterApiExitFrame(int argc) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000366 EnterExitFramePrologue();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367 EnterExitFrameEpilogue(argc, false);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000368}
369
370
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371void MacroAssembler::LeaveExitFrame(bool save_doubles) {
372 // Optionally restore all XMM registers.
373 if (save_doubles) {
374 CpuFeatures::Scope scope(SSE2);
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000375 const int offset = -2 * kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
377 XMMRegister reg = XMMRegister::from_code(i);
378 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
379 }
380 }
381
ager@chromium.org236ad962008-09-25 09:45:57 +0000382 // Get the return address from the stack and restore the frame pointer.
383 mov(ecx, Operand(ebp, 1 * kPointerSize));
384 mov(ebp, Operand(ebp, 0 * kPointerSize));
385
386 // Pop the arguments and the receiver from the caller stack.
387 lea(esp, Operand(esi, 1 * kPointerSize));
388
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000389 // Push the return address to get ready to return.
390 push(ecx);
391
392 LeaveExitFrameEpilogue();
393}
394
395void MacroAssembler::LeaveExitFrameEpilogue() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000396 // Restore current context from top and clear it in debug mode.
397 ExternalReference context_address(Top::k_context_address);
398 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000399#ifdef DEBUG
400 mov(Operand::StaticVariable(context_address), Immediate(0));
401#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000402
ager@chromium.org236ad962008-09-25 09:45:57 +0000403 // Clear the top frame.
404 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
405 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
406}
407
408
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000409void MacroAssembler::LeaveApiExitFrame() {
410 mov(esp, Operand(ebp));
411 pop(ebp);
412
413 LeaveExitFrameEpilogue();
414}
415
416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417void MacroAssembler::PushTryHandler(CodeLocation try_location,
418 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000419 // Adjust this code if not the case.
420 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 // The pc (return address) is already on TOS.
422 if (try_location == IN_JAVASCRIPT) {
423 if (type == TRY_CATCH_HANDLER) {
424 push(Immediate(StackHandler::TRY_CATCH));
425 } else {
426 push(Immediate(StackHandler::TRY_FINALLY));
427 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 } else {
430 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000431 // The frame pointer does not point to a JS frame so we save NULL
432 // for ebp. We expect the code throwing an exception to check ebp
433 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000435 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000437 // Save the current handler as the next handler.
438 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
439 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
441}
442
443
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000444void MacroAssembler::PopTryHandler() {
445 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
446 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
447 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
448}
449
450
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000451void MacroAssembler::Throw(Register value) {
452 // Adjust this code if not the case.
453 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
454
455 // eax must hold the exception.
456 if (!value.is(eax)) {
457 mov(eax, value);
458 }
459
460 // Drop the sp to the top of the handler.
461 ExternalReference handler_address(Top::k_handler_address);
462 mov(esp, Operand::StaticVariable(handler_address));
463
464 // Restore next handler and frame pointer, discard handler state.
465 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
466 pop(Operand::StaticVariable(handler_address));
467 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
468 pop(ebp);
469 pop(edx); // Remove state.
470
471 // Before returning we restore the context from the frame pointer if
472 // not NULL. The frame pointer is NULL in the exception handler of
473 // a JS entry frame.
474 Set(esi, Immediate(0)); // Tentatively set context pointer to NULL.
475 NearLabel skip;
476 cmp(ebp, 0);
477 j(equal, &skip, not_taken);
478 mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
479 bind(&skip);
480
481 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
482 ret(0);
483}
484
485
486void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
487 Register value) {
488 // Adjust this code if not the case.
489 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
490
491 // eax must hold the exception.
492 if (!value.is(eax)) {
493 mov(eax, value);
494 }
495
496 // Drop sp to the top stack handler.
497 ExternalReference handler_address(Top::k_handler_address);
498 mov(esp, Operand::StaticVariable(handler_address));
499
500 // Unwind the handlers until the ENTRY handler is found.
501 NearLabel loop, done;
502 bind(&loop);
503 // Load the type of the current stack handler.
504 const int kStateOffset = StackHandlerConstants::kStateOffset;
505 cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
506 j(equal, &done);
507 // Fetch the next handler in the list.
508 const int kNextOffset = StackHandlerConstants::kNextOffset;
509 mov(esp, Operand(esp, kNextOffset));
510 jmp(&loop);
511 bind(&done);
512
513 // Set the top handler address to next handler past the current ENTRY handler.
514 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
515 pop(Operand::StaticVariable(handler_address));
516
517 if (type == OUT_OF_MEMORY) {
518 // Set external caught exception to false.
519 ExternalReference external_caught(Top::k_external_caught_exception_address);
520 mov(eax, false);
521 mov(Operand::StaticVariable(external_caught), eax);
522
523 // Set pending exception and eax to out of memory exception.
524 ExternalReference pending_exception(Top::k_pending_exception_address);
525 mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
526 mov(Operand::StaticVariable(pending_exception), eax);
527 }
528
529 // Clear the context pointer.
530 Set(esi, Immediate(0));
531
532 // Restore fp from handler and discard handler state.
533 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
534 pop(ebp);
535 pop(edx); // State.
536
537 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
538 ret(0);
539}
540
541
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000542void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000543 Register scratch,
544 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000545 Label same_contexts;
546
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 ASSERT(!holder_reg.is(scratch));
548
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000549 // Load current lexical context from the stack frame.
550 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
551
552 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 if (FLAG_debug_code) {
554 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000555 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000557 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
559 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000560 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
561
562 // Check the context is a global context.
563 if (FLAG_debug_code) {
564 push(scratch);
565 // Read the first word and compare to global_context_map.
566 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
567 cmp(scratch, Factory::global_context_map());
568 Check(equal, "JSGlobalObject::global_context should be a global context.");
569 pop(scratch);
570 }
571
572 // Check if both contexts are the same.
573 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
574 j(equal, &same_contexts, taken);
575
576 // Compare security tokens, save holder_reg on the stack so we can use it
577 // as a temporary register.
578 //
579 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
580 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 // Check that the security token in the calling global object is
582 // compatible with the security token in the receiving global
583 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000584 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
585
586 // Check the context is a global context.
587 if (FLAG_debug_code) {
588 cmp(holder_reg, Factory::null_value());
589 Check(not_equal, "JSGlobalProxy::context() should not be null.");
590
591 push(holder_reg);
592 // Read the first word and compare to global_context_map(),
593 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
594 cmp(holder_reg, Factory::global_context_map());
595 Check(equal, "JSGlobalObject::global_context should be a global context.");
596 pop(holder_reg);
597 }
598
599 int token_offset = Context::kHeaderSize +
600 Context::SECURITY_TOKEN_INDEX * kPointerSize;
601 mov(scratch, FieldOperand(scratch, token_offset));
602 cmp(scratch, FieldOperand(holder_reg, token_offset));
603 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000605
606 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607}
608
609
ager@chromium.orga1645e22009-09-09 19:27:10 +0000610void MacroAssembler::LoadAllocationTopHelper(Register result,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000611 Register scratch,
612 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000613 ExternalReference new_space_allocation_top =
614 ExternalReference::new_space_allocation_top_address();
615
616 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000617 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000618 // No use of scratch if allocation top is provided.
619 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000620#ifdef DEBUG
621 // Assert that result actually contains top on entry.
622 cmp(result, Operand::StaticVariable(new_space_allocation_top));
623 Check(equal, "Unexpected allocation top");
624#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000625 return;
626 }
627
628 // Move address of new object to result. Use scratch register if available.
629 if (scratch.is(no_reg)) {
630 mov(result, Operand::StaticVariable(new_space_allocation_top));
631 } else {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000632 mov(Operand(scratch), Immediate(new_space_allocation_top));
633 mov(result, Operand(scratch, 0));
634 }
635}
636
637
638void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
639 Register scratch) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000640 if (FLAG_debug_code) {
641 test(result_end, Immediate(kObjectAlignmentMask));
642 Check(zero, "Unaligned allocation in new space");
643 }
644
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000645 ExternalReference new_space_allocation_top =
646 ExternalReference::new_space_allocation_top_address();
647
648 // Update new top. Use scratch if available.
649 if (scratch.is(no_reg)) {
650 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
651 } else {
652 mov(Operand(scratch, 0), result_end);
653 }
654}
655
ager@chromium.orga1645e22009-09-09 19:27:10 +0000656
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000657void MacroAssembler::AllocateInNewSpace(int object_size,
658 Register result,
659 Register result_end,
660 Register scratch,
661 Label* gc_required,
662 AllocationFlags flags) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000663 if (!FLAG_inline_new) {
664 if (FLAG_debug_code) {
665 // Trash the registers to simulate an allocation failure.
666 mov(result, Immediate(0x7091));
667 if (result_end.is_valid()) {
668 mov(result_end, Immediate(0x7191));
669 }
670 if (scratch.is_valid()) {
671 mov(scratch, Immediate(0x7291));
672 }
673 }
674 jmp(gc_required);
675 return;
676 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000677 ASSERT(!result.is(result_end));
678
679 // Load address of new object into result.
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000680 LoadAllocationTopHelper(result, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000681
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000682 Register top_reg = result_end.is_valid() ? result_end : result;
683
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000684 // Calculate new top and bail out if new space is exhausted.
685 ExternalReference new_space_allocation_limit =
686 ExternalReference::new_space_allocation_limit_address();
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000687
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000688 if (!top_reg.is(result)) {
689 mov(top_reg, result);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000690 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000691 add(Operand(top_reg), Immediate(object_size));
692 j(carry, gc_required, not_taken);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000693 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000694 j(above, gc_required, not_taken);
695
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000696 // Update allocation top.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000697 UpdateAllocationTopHelper(top_reg, scratch);
698
699 // Tag result if requested.
700 if (top_reg.is(result)) {
701 if ((flags & TAG_OBJECT) != 0) {
702 sub(Operand(result), Immediate(object_size - kHeapObjectTag));
703 } else {
704 sub(Operand(result), Immediate(object_size));
705 }
706 } else if ((flags & TAG_OBJECT) != 0) {
707 add(Operand(result), Immediate(kHeapObjectTag));
708 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000709}
710
711
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000712void MacroAssembler::AllocateInNewSpace(int header_size,
713 ScaleFactor element_size,
714 Register element_count,
715 Register result,
716 Register result_end,
717 Register scratch,
718 Label* gc_required,
719 AllocationFlags flags) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000720 if (!FLAG_inline_new) {
721 if (FLAG_debug_code) {
722 // Trash the registers to simulate an allocation failure.
723 mov(result, Immediate(0x7091));
724 mov(result_end, Immediate(0x7191));
725 if (scratch.is_valid()) {
726 mov(scratch, Immediate(0x7291));
727 }
728 // Register element_count is not modified by the function.
729 }
730 jmp(gc_required);
731 return;
732 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000733 ASSERT(!result.is(result_end));
734
735 // Load address of new object into result.
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000736 LoadAllocationTopHelper(result, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000737
738 // Calculate new top and bail out if new space is exhausted.
739 ExternalReference new_space_allocation_limit =
740 ExternalReference::new_space_allocation_limit_address();
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000741
742 // We assume that element_count*element_size + header_size does not
743 // overflow.
744 lea(result_end, Operand(element_count, element_size, header_size));
745 add(result_end, Operand(result));
746 j(carry, gc_required);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000747 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
748 j(above, gc_required);
749
ager@chromium.orga1645e22009-09-09 19:27:10 +0000750 // Tag result if requested.
751 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000752 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000753 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000754
755 // Update allocation top.
756 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000757}
758
759
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000760void MacroAssembler::AllocateInNewSpace(Register object_size,
761 Register result,
762 Register result_end,
763 Register scratch,
764 Label* gc_required,
765 AllocationFlags flags) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000766 if (!FLAG_inline_new) {
767 if (FLAG_debug_code) {
768 // Trash the registers to simulate an allocation failure.
769 mov(result, Immediate(0x7091));
770 mov(result_end, Immediate(0x7191));
771 if (scratch.is_valid()) {
772 mov(scratch, Immediate(0x7291));
773 }
774 // object_size is left unchanged by this function.
775 }
776 jmp(gc_required);
777 return;
778 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000779 ASSERT(!result.is(result_end));
780
781 // Load address of new object into result.
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000782 LoadAllocationTopHelper(result, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000783
784 // Calculate new top and bail out if new space is exhausted.
785 ExternalReference new_space_allocation_limit =
786 ExternalReference::new_space_allocation_limit_address();
787 if (!object_size.is(result_end)) {
788 mov(result_end, object_size);
789 }
790 add(result_end, Operand(result));
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000791 j(carry, gc_required, not_taken);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000792 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
793 j(above, gc_required, not_taken);
794
ager@chromium.orga1645e22009-09-09 19:27:10 +0000795 // Tag result if requested.
796 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000797 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000798 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000799
800 // Update allocation top.
801 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000802}
803
804
805void MacroAssembler::UndoAllocationInNewSpace(Register object) {
806 ExternalReference new_space_allocation_top =
807 ExternalReference::new_space_allocation_top_address();
808
809 // Make sure the object has no tag before resetting top.
810 and_(Operand(object), Immediate(~kHeapObjectTagMask));
811#ifdef DEBUG
812 cmp(object, Operand::StaticVariable(new_space_allocation_top));
813 Check(below, "Undo allocation of non allocated memory");
814#endif
815 mov(Operand::StaticVariable(new_space_allocation_top), object);
816}
817
818
ager@chromium.org3811b432009-10-28 14:53:37 +0000819void MacroAssembler::AllocateHeapNumber(Register result,
820 Register scratch1,
821 Register scratch2,
822 Label* gc_required) {
823 // Allocate heap number in new space.
824 AllocateInNewSpace(HeapNumber::kSize,
825 result,
826 scratch1,
827 scratch2,
828 gc_required,
829 TAG_OBJECT);
830
831 // Set the map.
832 mov(FieldOperand(result, HeapObject::kMapOffset),
833 Immediate(Factory::heap_number_map()));
834}
835
836
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000837void MacroAssembler::AllocateTwoByteString(Register result,
838 Register length,
839 Register scratch1,
840 Register scratch2,
841 Register scratch3,
842 Label* gc_required) {
843 // Calculate the number of bytes needed for the characters in the string while
844 // observing object alignment.
845 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000846 ASSERT(kShortSize == 2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000847 // scratch1 = length * 2 + kObjectAlignmentMask.
848 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000849 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
850
851 // Allocate two byte string in new space.
852 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
853 times_1,
854 scratch1,
855 result,
856 scratch2,
857 scratch3,
858 gc_required,
859 TAG_OBJECT);
860
861 // Set the map, length and hash field.
862 mov(FieldOperand(result, HeapObject::kMapOffset),
863 Immediate(Factory::string_map()));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000864 mov(scratch1, length);
865 SmiTag(scratch1);
866 mov(FieldOperand(result, String::kLengthOffset), scratch1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000867 mov(FieldOperand(result, String::kHashFieldOffset),
868 Immediate(String::kEmptyHashField));
869}
870
871
872void MacroAssembler::AllocateAsciiString(Register result,
873 Register length,
874 Register scratch1,
875 Register scratch2,
876 Register scratch3,
877 Label* gc_required) {
878 // Calculate the number of bytes needed for the characters in the string while
879 // observing object alignment.
880 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
881 mov(scratch1, length);
882 ASSERT(kCharSize == 1);
883 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
884 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
885
886 // Allocate ascii string in new space.
887 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
888 times_1,
889 scratch1,
890 result,
891 scratch2,
892 scratch3,
893 gc_required,
894 TAG_OBJECT);
895
896 // Set the map, length and hash field.
897 mov(FieldOperand(result, HeapObject::kMapOffset),
898 Immediate(Factory::ascii_string_map()));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000899 mov(scratch1, length);
900 SmiTag(scratch1);
901 mov(FieldOperand(result, String::kLengthOffset), scratch1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000902 mov(FieldOperand(result, String::kHashFieldOffset),
903 Immediate(String::kEmptyHashField));
904}
905
906
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000907void MacroAssembler::AllocateAsciiString(Register result,
908 int length,
909 Register scratch1,
910 Register scratch2,
911 Label* gc_required) {
912 ASSERT(length > 0);
913
914 // Allocate ascii string in new space.
915 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
916 result,
917 scratch1,
918 scratch2,
919 gc_required,
920 TAG_OBJECT);
921
922 // Set the map, length and hash field.
923 mov(FieldOperand(result, HeapObject::kMapOffset),
924 Immediate(Factory::ascii_string_map()));
925 mov(FieldOperand(result, String::kLengthOffset),
926 Immediate(Smi::FromInt(length)));
927 mov(FieldOperand(result, String::kHashFieldOffset),
928 Immediate(String::kEmptyHashField));
929}
930
931
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000932void MacroAssembler::AllocateConsString(Register result,
933 Register scratch1,
934 Register scratch2,
935 Label* gc_required) {
936 // Allocate heap number in new space.
937 AllocateInNewSpace(ConsString::kSize,
938 result,
939 scratch1,
940 scratch2,
941 gc_required,
942 TAG_OBJECT);
943
944 // Set the map. The other fields are left uninitialized.
945 mov(FieldOperand(result, HeapObject::kMapOffset),
946 Immediate(Factory::cons_string_map()));
947}
948
949
950void MacroAssembler::AllocateAsciiConsString(Register result,
951 Register scratch1,
952 Register scratch2,
953 Label* gc_required) {
954 // Allocate heap number in new space.
955 AllocateInNewSpace(ConsString::kSize,
956 result,
957 scratch1,
958 scratch2,
959 gc_required,
960 TAG_OBJECT);
961
962 // Set the map. The other fields are left uninitialized.
963 mov(FieldOperand(result, HeapObject::kMapOffset),
964 Immediate(Factory::cons_ascii_string_map()));
965}
966
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000967
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000968// Copy memory, byte-by-byte, from source to destination. Not optimized for
969// long or aligned copies. The contents of scratch and length are destroyed.
970// Source and destination are incremented by length.
971// Many variants of movsb, loop unrolling, word moves, and indexed operands
972// have been tried here already, and this is fastest.
973// A simpler loop is faster on small copies, but 30% slower on large ones.
974// The cld() instruction must have been emitted, to set the direction flag(),
975// before calling this function.
976void MacroAssembler::CopyBytes(Register source,
977 Register destination,
978 Register length,
979 Register scratch) {
980 Label loop, done, short_string, short_loop;
981 // Experimentation shows that the short string loop is faster if length < 10.
982 cmp(Operand(length), Immediate(10));
983 j(less_equal, &short_string);
984
985 ASSERT(source.is(esi));
986 ASSERT(destination.is(edi));
987 ASSERT(length.is(ecx));
988
989 // Because source is 4-byte aligned in our uses of this function,
990 // we keep source aligned for the rep_movs call by copying the odd bytes
991 // at the end of the ranges.
992 mov(scratch, Operand(source, length, times_1, -4));
993 mov(Operand(destination, length, times_1, -4), scratch);
994 mov(scratch, ecx);
995 shr(ecx, 2);
996 rep_movs();
997 and_(Operand(scratch), Immediate(0x3));
998 add(destination, Operand(scratch));
999 jmp(&done);
1000
1001 bind(&short_string);
1002 test(length, Operand(length));
1003 j(zero, &done);
1004
1005 bind(&short_loop);
1006 mov_b(scratch, Operand(source, 0));
1007 mov_b(Operand(destination, 0), scratch);
1008 inc(source);
1009 inc(destination);
1010 dec(length);
1011 j(not_zero, &short_loop);
1012
1013 bind(&done);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001014}
1015
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001016
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001017void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
1018 Register result,
1019 Register op,
1020 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001021 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001022 test(result, Operand(result));
1023 ok.Branch(not_zero, taken);
1024 test(op, Operand(op));
1025 then_target->Branch(sign, not_taken);
1026 ok.Bind();
1027}
1028
1029
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030void MacroAssembler::NegativeZeroTest(Register result,
1031 Register op,
1032 Label* then_label) {
1033 Label ok;
1034 test(result, Operand(result));
1035 j(not_zero, &ok, taken);
1036 test(op, Operand(op));
1037 j(sign, then_label, not_taken);
1038 bind(&ok);
1039}
1040
1041
1042void MacroAssembler::NegativeZeroTest(Register result,
1043 Register op1,
1044 Register op2,
1045 Register scratch,
1046 Label* then_label) {
1047 Label ok;
1048 test(result, Operand(result));
1049 j(not_zero, &ok, taken);
1050 mov(scratch, Operand(op1));
1051 or_(scratch, Operand(op2));
1052 j(sign, then_label, not_taken);
1053 bind(&ok);
1054}
1055
1056
ager@chromium.org7c537e22008-10-16 08:43:32 +00001057void MacroAssembler::TryGetFunctionPrototype(Register function,
1058 Register result,
1059 Register scratch,
1060 Label* miss) {
1061 // Check that the receiver isn't a smi.
1062 test(function, Immediate(kSmiTagMask));
1063 j(zero, miss, not_taken);
1064
1065 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001066 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001067 j(not_equal, miss, not_taken);
1068
1069 // Make sure that the function has an instance prototype.
1070 Label non_instance;
1071 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1072 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1073 j(not_zero, &non_instance, not_taken);
1074
1075 // Get the prototype or initial map from the function.
1076 mov(result,
1077 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1078
1079 // If the prototype or initial map is the hole, don't return it and
1080 // simply miss the cache instead. This will allow us to allocate a
1081 // prototype object on-demand in the runtime system.
1082 cmp(Operand(result), Immediate(Factory::the_hole_value()));
1083 j(equal, miss, not_taken);
1084
1085 // If the function does not have an initial map, we're done.
1086 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001087 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001088 j(not_equal, &done);
1089
1090 // Get the prototype from the initial map.
1091 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1092 jmp(&done);
1093
1094 // Non-instance prototype: Fetch prototype from constructor field
1095 // in initial map.
1096 bind(&non_instance);
1097 mov(result, FieldOperand(result, Map::kConstructorOffset));
1098
1099 // All done.
1100 bind(&done);
1101}
1102
1103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001105 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +00001106 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107}
1108
1109
lrn@chromium.org303ada72010-10-27 09:33:13 +00001110MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001111 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001112 Object* result;
1113 { MaybeObject* maybe_result = stub->TryGetCode();
1114 if (!maybe_result->ToObject(&result)) return maybe_result;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001115 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001116 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001117 return result;
1118}
1119
1120
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001121void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001122 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001123 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1124}
1125
1126
lrn@chromium.org303ada72010-10-27 09:33:13 +00001127MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001128 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001129 Object* result;
1130 { MaybeObject* maybe_result = stub->TryGetCode();
1131 if (!maybe_result->ToObject(&result)) return maybe_result;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001132 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001133 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001134 return result;
1135}
1136
1137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138void MacroAssembler::StubReturn(int argc) {
1139 ASSERT(argc >= 1 && generating_stub());
1140 ret((argc - 1) * kPointerSize);
1141}
1142
1143
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001144void MacroAssembler::IllegalOperation(int num_arguments) {
1145 if (num_arguments > 0) {
1146 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1147 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001148 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149}
1150
1151
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001152void MacroAssembler::IndexFromHash(Register hash, Register index) {
1153 // The assert checks that the constants for the maximum number of digits
1154 // for an array index cached in the hash field and the number of bits
1155 // reserved for it does not conflict.
1156 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1157 (1 << String::kArrayIndexValueBits));
1158 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1159 // the low kHashShift bits.
1160 and_(hash, String::kArrayIndexValueMask);
1161 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1162 if (String::kHashShift > kSmiTagSize) {
1163 shr(hash, String::kHashShift - kSmiTagSize);
1164 }
1165 if (!index.is(hash)) {
1166 mov(index, hash);
1167 }
1168}
1169
1170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1172 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1173}
1174
1175
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001176void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1177 Runtime::Function* function = Runtime::FunctionForId(id);
1178 Set(eax, Immediate(function->nargs));
1179 mov(ebx, Immediate(ExternalReference(function)));
1180 CEntryStub ces(1);
1181 ces.SaveDoubles();
1182 CallStub(&ces);
1183}
1184
1185
lrn@chromium.org303ada72010-10-27 09:33:13 +00001186MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1187 int num_arguments) {
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001188 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1189}
1190
1191
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001193 // If the expected number of arguments of the runtime function is
1194 // constant, we check that the actual number of arguments match the
1195 // expectation.
1196 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001197 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 return;
1199 }
1200
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001201 // TODO(1236192): Most runtime routines don't need the number of
1202 // arguments passed in because it is constant. At some point we
1203 // should remove this need and make the runtime routine entry code
1204 // smarter.
1205 Set(eax, Immediate(num_arguments));
1206 mov(ebx, Immediate(ExternalReference(f)));
1207 CEntryStub ces(1);
1208 CallStub(&ces);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209}
1210
1211
lrn@chromium.org303ada72010-10-27 09:33:13 +00001212MaybeObject* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1213 int num_arguments) {
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001214 if (f->nargs >= 0 && f->nargs != num_arguments) {
1215 IllegalOperation(num_arguments);
1216 // Since we did not call the stub, there was no allocation failure.
1217 // Return some non-failure object.
1218 return Heap::undefined_value();
1219 }
1220
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221 // TODO(1236192): Most runtime routines don't need the number of
1222 // arguments passed in because it is constant. At some point we
1223 // should remove this need and make the runtime routine entry code
1224 // smarter.
1225 Set(eax, Immediate(num_arguments));
1226 mov(ebx, Immediate(ExternalReference(f)));
1227 CEntryStub ces(1);
1228 return TryCallStub(&ces);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001229}
1230
1231
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001232void MacroAssembler::CallExternalReference(ExternalReference ref,
1233 int num_arguments) {
1234 mov(eax, Immediate(num_arguments));
1235 mov(ebx, Immediate(ref));
1236
1237 CEntryStub stub(1);
1238 CallStub(&stub);
1239}
1240
1241
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001242void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1243 int num_arguments,
1244 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001245 // TODO(1236192): Most runtime routines don't need the number of
1246 // arguments passed in because it is constant. At some point we
1247 // should remove this need and make the runtime routine entry code
1248 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001249 Set(eax, Immediate(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001250 JumpToExternalReference(ext);
1251}
1252
1253
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001254MaybeObject* MacroAssembler::TryTailCallExternalReference(
1255 const ExternalReference& ext, int num_arguments, int result_size) {
1256 // TODO(1236192): Most runtime routines don't need the number of
1257 // arguments passed in because it is constant. At some point we
1258 // should remove this need and make the runtime routine entry code
1259 // smarter.
1260 Set(eax, Immediate(num_arguments));
1261 return TryJumpToExternalReference(ext);
1262}
1263
1264
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001265void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1266 int num_arguments,
1267 int result_size) {
1268 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269}
1270
1271
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001272MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid,
1273 int num_arguments,
1274 int result_size) {
1275 return TryTailCallExternalReference(
1276 ExternalReference(fid), num_arguments, result_size);
1277}
1278
1279
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001280// If true, a Handle<T> returned by value from a function with cdecl calling
1281// convention will be returned directly as a value of location_ field in a
1282// register eax.
1283// If false, it is returned as a pointer to a preallocated by caller memory
1284// region. Pointer to this region should be passed to a function as an
1285// implicit first argument.
1286#if defined(USING_BSD_ABI) || defined(__MINGW32__)
1287static const bool kReturnHandlesDirectly = true;
lrn@chromium.org303ada72010-10-27 09:33:13 +00001288#else
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001289static const bool kReturnHandlesDirectly = false;
lrn@chromium.org303ada72010-10-27 09:33:13 +00001290#endif
1291
1292
1293Operand ApiParameterOperand(int index) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001294 return Operand(
1295 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001296}
1297
1298
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001299void MacroAssembler::PrepareCallApiFunction(int argc, Register scratch) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001300 if (kReturnHandlesDirectly) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001301 EnterApiExitFrame(argc);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001302 // When handles are returned directly we don't have to allocate extra
lrn@chromium.org303ada72010-10-27 09:33:13 +00001303 // space for and pass an out parameter.
1304 } else {
1305 // We allocate two additional slots: return value and pointer to it.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001306 EnterApiExitFrame(argc + 2);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001307
lrn@chromium.org303ada72010-10-27 09:33:13 +00001308 // The argument slots are filled as follows:
1309 //
1310 // n + 1: output cell
1311 // n: arg n
1312 // ...
1313 // 1: arg1
1314 // 0: pointer to the output cell
1315 //
1316 // Note that this is one more "argument" than the function expects
1317 // so the out cell will have to be popped explicitly after returning
1318 // from the function. The out cell contains Handle.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001319
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001320 // pointer to out cell.
1321 lea(scratch, Operand(esp, (argc + 1) * kPointerSize));
1322 mov(Operand(esp, 0 * kPointerSize), scratch); // output.
1323 if (FLAG_debug_code) {
1324 mov(Operand(esp, (argc + 1) * kPointerSize), Immediate(0)); // out cell.
1325 }
1326 }
1327}
1328
1329
1330MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function,
1331 int stack_space) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001332 ExternalReference next_address =
1333 ExternalReference::handle_scope_next_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001334 ExternalReference limit_address =
1335 ExternalReference::handle_scope_limit_address();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001336 ExternalReference level_address =
1337 ExternalReference::handle_scope_level_address();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001338
lrn@chromium.org303ada72010-10-27 09:33:13 +00001339 // Allocate HandleScope in callee-save registers.
1340 mov(ebx, Operand::StaticVariable(next_address));
1341 mov(edi, Operand::StaticVariable(limit_address));
1342 add(Operand::StaticVariable(level_address), Immediate(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001343
lrn@chromium.org303ada72010-10-27 09:33:13 +00001344 // Call the api function!
1345 call(function->address(), RelocInfo::RUNTIME_ENTRY);
1346
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001347 if (!kReturnHandlesDirectly) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001348 // The returned value is a pointer to the handle holding the result.
1349 // Dereference this to get to the location.
1350 mov(eax, Operand(eax, 0));
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001351 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001352
lrn@chromium.org303ada72010-10-27 09:33:13 +00001353 Label empty_handle;
1354 Label prologue;
1355 Label promote_scheduled_exception;
1356 Label delete_allocated_handles;
1357 Label leave_exit_frame;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001358
lrn@chromium.org303ada72010-10-27 09:33:13 +00001359 // Check if the result handle holds 0.
1360 test(eax, Operand(eax));
1361 j(zero, &empty_handle, not_taken);
1362 // It was non-zero. Dereference to get the result value.
1363 mov(eax, Operand(eax, 0));
1364 bind(&prologue);
1365 // No more valid handles (the result handle was the last one). Restore
1366 // previous handle scope.
1367 mov(Operand::StaticVariable(next_address), ebx);
1368 sub(Operand::StaticVariable(level_address), Immediate(1));
1369 Assert(above_equal, "Invalid HandleScope level");
1370 cmp(edi, Operand::StaticVariable(limit_address));
1371 j(not_equal, &delete_allocated_handles, not_taken);
1372 bind(&leave_exit_frame);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001373
lrn@chromium.org303ada72010-10-27 09:33:13 +00001374 // Check if the function scheduled an exception.
1375 ExternalReference scheduled_exception_address =
1376 ExternalReference::scheduled_exception_address();
1377 cmp(Operand::StaticVariable(scheduled_exception_address),
ager@chromium.org378b34e2011-01-28 08:04:38 +00001378 Immediate(Factory::the_hole_value()));
lrn@chromium.org303ada72010-10-27 09:33:13 +00001379 j(not_equal, &promote_scheduled_exception, not_taken);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001380 LeaveApiExitFrame();
1381 ret(stack_space * kPointerSize);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001382 bind(&promote_scheduled_exception);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001383 MaybeObject* result =
1384 TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1385 if (result->IsFailure()) {
1386 return result;
1387 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001388 bind(&empty_handle);
1389 // It was zero; the result is undefined.
1390 mov(eax, Factory::undefined_value());
1391 jmp(&prologue);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001392
lrn@chromium.org303ada72010-10-27 09:33:13 +00001393 // HandleScope limit has changed. Delete allocated extensions.
1394 bind(&delete_allocated_handles);
1395 mov(Operand::StaticVariable(limit_address), edi);
1396 mov(edi, eax);
1397 mov(eax, Immediate(ExternalReference::delete_handle_scope_extensions()));
1398 call(Operand(eax));
1399 mov(eax, edi);
1400 jmp(&leave_exit_frame);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001401
1402 return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001403}
1404
1405
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001406void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001408 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001409 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001410 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411}
1412
1413
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001414MaybeObject* MacroAssembler::TryJumpToExternalReference(
1415 const ExternalReference& ext) {
1416 // Set the entry point and jump to the C entry runtime stub.
1417 mov(ebx, Immediate(ext));
1418 CEntryStub ces(1);
1419 return TryTailCallStub(&ces);
1420}
1421
1422
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1424 const ParameterCount& actual,
1425 Handle<Code> code_constant,
1426 const Operand& code_operand,
1427 Label* done,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001428 InvokeFlag flag,
1429 PostCallGenerator* post_call_generator) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 bool definitely_matches = false;
1431 Label invoke;
1432 if (expected.is_immediate()) {
1433 ASSERT(actual.is_immediate());
1434 if (expected.immediate() == actual.immediate()) {
1435 definitely_matches = true;
1436 } else {
1437 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001438 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1439 if (expected.immediate() == sentinel) {
1440 // Don't worry about adapting arguments for builtins that
1441 // don't want that done. Skip adaption code by making it look
1442 // like we have a match between expected and actual number of
1443 // arguments.
1444 definitely_matches = true;
1445 } else {
1446 mov(ebx, expected.immediate());
1447 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 }
1449 } else {
1450 if (actual.is_immediate()) {
1451 // Expected is in register, actual is immediate. This is the
1452 // case when we invoke function values without going through the
1453 // IC mechanism.
1454 cmp(expected.reg(), actual.immediate());
1455 j(equal, &invoke);
1456 ASSERT(expected.reg().is(ebx));
1457 mov(eax, actual.immediate());
1458 } else if (!expected.reg().is(actual.reg())) {
1459 // Both expected and actual are in (different) registers. This
1460 // is the case when we invoke functions using call and apply.
1461 cmp(expected.reg(), Operand(actual.reg()));
1462 j(equal, &invoke);
1463 ASSERT(actual.reg().is(eax));
1464 ASSERT(expected.reg().is(ebx));
1465 }
1466 }
1467
1468 if (!definitely_matches) {
1469 Handle<Code> adaptor =
1470 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1471 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001472 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1474 } else if (!code_operand.is_reg(edx)) {
1475 mov(edx, code_operand);
1476 }
1477
1478 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001479 call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001480 if (post_call_generator != NULL) post_call_generator->Generate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 jmp(done);
1482 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001483 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 }
1485 bind(&invoke);
1486 }
1487}
1488
1489
1490void MacroAssembler::InvokeCode(const Operand& code,
1491 const ParameterCount& expected,
1492 const ParameterCount& actual,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001493 InvokeFlag flag,
1494 PostCallGenerator* post_call_generator) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001496 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1497 &done, flag, post_call_generator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 if (flag == CALL_FUNCTION) {
1499 call(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001500 if (post_call_generator != NULL) post_call_generator->Generate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 } else {
1502 ASSERT(flag == JUMP_FUNCTION);
1503 jmp(code);
1504 }
1505 bind(&done);
1506}
1507
1508
1509void MacroAssembler::InvokeCode(Handle<Code> code,
1510 const ParameterCount& expected,
1511 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001512 RelocInfo::Mode rmode,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001513 InvokeFlag flag,
1514 PostCallGenerator* post_call_generator) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515 Label done;
1516 Operand dummy(eax);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001517 InvokePrologue(expected, actual, code, dummy, &done,
1518 flag, post_call_generator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519 if (flag == CALL_FUNCTION) {
1520 call(code, rmode);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001521 if (post_call_generator != NULL) post_call_generator->Generate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522 } else {
1523 ASSERT(flag == JUMP_FUNCTION);
1524 jmp(code, rmode);
1525 }
1526 bind(&done);
1527}
1528
1529
1530void MacroAssembler::InvokeFunction(Register fun,
1531 const ParameterCount& actual,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001532 InvokeFlag flag,
1533 PostCallGenerator* post_call_generator) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 ASSERT(fun.is(edi));
1535 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1536 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1537 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001538 SmiUntag(ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539
1540 ParameterCount expected(ebx);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001541 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001542 expected, actual, flag, post_call_generator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543}
1544
1545
ager@chromium.org5c838252010-02-19 08:53:10 +00001546void MacroAssembler::InvokeFunction(JSFunction* function,
1547 const ParameterCount& actual,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001548 InvokeFlag flag,
1549 PostCallGenerator* post_call_generator) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001550 ASSERT(function->is_compiled());
1551 // Get the function and setup the context.
1552 mov(edi, Immediate(Handle<JSFunction>(function)));
1553 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001554
ager@chromium.org5c838252010-02-19 08:53:10 +00001555 ParameterCount expected(function->shared()->formal_parameter_count());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001556 if (V8::UseCrankshaft()) {
1557 // TODO(kasperl): For now, we always call indirectly through the
1558 // code field in the function to allow recompilation to take effect
1559 // without changing any of the call sites.
1560 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
1561 expected, actual, flag, post_call_generator);
1562 } else {
1563 Handle<Code> code(function->code());
1564 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET,
1565 flag, post_call_generator);
1566 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001567}
1568
1569
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001570void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1571 InvokeFlag flag,
1572 PostCallGenerator* post_call_generator) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001573 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001574 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575
1576 // Rely on the assertion to check that the number of provided
1577 // arguments match the expected number of arguments. Fake a
1578 // parameter count to avoid emitting code to do the check.
1579 ParameterCount expected(0);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001580 GetBuiltinFunction(edi, id);
1581 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001582 expected, expected, flag, post_call_generator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583}
1584
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001585void MacroAssembler::GetBuiltinFunction(Register target,
1586 Builtins::JavaScript id) {
1587 // Load the JavaScript builtin function from the builtins object.
1588 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1589 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
1590 mov(target, FieldOperand(target,
1591 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1592}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593
1594void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001595 ASSERT(!target.is(edi));
ager@chromium.org5c838252010-02-19 08:53:10 +00001596 // Load the JavaScript builtin function from the builtins object.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001597 GetBuiltinFunction(edi, id);
1598 // Load the code entry point from the function into the target register.
1599 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600}
1601
1602
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001603void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1604 if (context_chain_length > 0) {
1605 // Move up the chain of contexts to the context containing the slot.
1606 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1607 // Load the function context (which is the incoming, outer context).
1608 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1609 for (int i = 1; i < context_chain_length; i++) {
1610 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1611 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1612 }
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001613 } else {
1614 // Slot is in the current function context. Move it into the
1615 // destination register in case we store into it (the write barrier
1616 // cannot be allowed to destroy the context in esi).
1617 mov(dst, esi);
1618 }
1619
1620 // We should not have found a 'with' context by walking the context chain
1621 // (i.e., the static scope chain and runtime context chain do not agree).
1622 // A variable occurring in such a scope should have slot type LOOKUP and
1623 // not CONTEXT.
1624 if (FLAG_debug_code) {
1625 cmp(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1626 Check(equal, "Yo dawg, I heard you liked function contexts "
1627 "so I put function contexts in all your contexts");
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001628 }
1629}
1630
1631
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001632void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1633 // Load the global or builtins object from the current context.
1634 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1635 // Load the global context from the global or builtins object.
1636 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
1637 // Load the function from the global context.
1638 mov(function, Operand(function, Context::SlotOffset(index)));
1639}
1640
1641
1642void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1643 Register map) {
1644 // Load the initial map. The global functions all have initial maps.
1645 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1646 if (FLAG_debug_code) {
1647 Label ok, fail;
1648 CheckMap(map, Factory::meta_map(), &fail, false);
1649 jmp(&ok);
1650 bind(&fail);
1651 Abort("Global functions must have initial map");
1652 bind(&ok);
1653 }
1654}
1655
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001656
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001657int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
1658 // The registers are pushed starting with the lowest encoding,
1659 // which means that lowest encodings are furthest away from
1660 // the stack pointer.
1661 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
1662 return kNumSafepointRegisters - reg_code - 1;
1663}
1664
1665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666void MacroAssembler::Ret() {
1667 ret(0);
1668}
1669
1670
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001671void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
1672 if (is_uint16(bytes_dropped)) {
1673 ret(bytes_dropped);
1674 } else {
1675 pop(scratch);
1676 add(Operand(esp), Immediate(bytes_dropped));
1677 push(scratch);
1678 ret(0);
1679 }
1680}
1681
1682
1683
1684
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001685void MacroAssembler::Drop(int stack_elements) {
1686 if (stack_elements > 0) {
1687 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1688 }
1689}
1690
1691
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001692void MacroAssembler::Move(Register dst, Register src) {
1693 if (!dst.is(src)) {
1694 mov(dst, src);
1695 }
1696}
1697
1698
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001699void MacroAssembler::Move(Register dst, Handle<Object> value) {
1700 mov(dst, value);
1701}
1702
1703
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1705 if (FLAG_native_code_counters && counter->Enabled()) {
1706 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1707 }
1708}
1709
1710
1711void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1712 ASSERT(value > 0);
1713 if (FLAG_native_code_counters && counter->Enabled()) {
1714 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1715 if (value == 1) {
1716 inc(operand);
1717 } else {
1718 add(operand, Immediate(value));
1719 }
1720 }
1721}
1722
1723
1724void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1725 ASSERT(value > 0);
1726 if (FLAG_native_code_counters && counter->Enabled()) {
1727 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1728 if (value == 1) {
1729 dec(operand);
1730 } else {
1731 sub(operand, Immediate(value));
1732 }
1733 }
1734}
1735
1736
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001737void MacroAssembler::IncrementCounter(Condition cc,
1738 StatsCounter* counter,
1739 int value) {
1740 ASSERT(value > 0);
1741 if (FLAG_native_code_counters && counter->Enabled()) {
1742 Label skip;
1743 j(NegateCondition(cc), &skip);
1744 pushfd();
1745 IncrementCounter(counter, value);
1746 popfd();
1747 bind(&skip);
1748 }
1749}
1750
1751
1752void MacroAssembler::DecrementCounter(Condition cc,
1753 StatsCounter* counter,
1754 int value) {
1755 ASSERT(value > 0);
1756 if (FLAG_native_code_counters && counter->Enabled()) {
1757 Label skip;
1758 j(NegateCondition(cc), &skip);
1759 pushfd();
1760 DecrementCounter(counter, value);
1761 popfd();
1762 bind(&skip);
1763 }
1764}
1765
1766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767void MacroAssembler::Assert(Condition cc, const char* msg) {
1768 if (FLAG_debug_code) Check(cc, msg);
1769}
1770
1771
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001772void MacroAssembler::AssertFastElements(Register elements) {
1773 if (FLAG_debug_code) {
1774 Label ok;
1775 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1776 Immediate(Factory::fixed_array_map()));
1777 j(equal, &ok);
1778 cmp(FieldOperand(elements, HeapObject::kMapOffset),
1779 Immediate(Factory::fixed_cow_array_map()));
1780 j(equal, &ok);
1781 Abort("JSObject with fast elements map has slow elements");
1782 bind(&ok);
1783 }
1784}
1785
1786
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787void MacroAssembler::Check(Condition cc, const char* msg) {
1788 Label L;
1789 j(cc, &L, taken);
1790 Abort(msg);
1791 // will not return here
1792 bind(&L);
1793}
1794
1795
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001796void MacroAssembler::CheckStackAlignment() {
1797 int frame_alignment = OS::ActivationFrameAlignment();
1798 int frame_alignment_mask = frame_alignment - 1;
1799 if (frame_alignment > kPointerSize) {
1800 ASSERT(IsPowerOf2(frame_alignment));
1801 Label alignment_as_expected;
1802 test(esp, Immediate(frame_alignment_mask));
1803 j(zero, &alignment_as_expected);
1804 // Abort if stack is not aligned.
1805 int3();
1806 bind(&alignment_as_expected);
1807 }
1808}
1809
1810
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811void MacroAssembler::Abort(const char* msg) {
1812 // We want to pass the msg string like a smi to avoid GC
1813 // problems, however msg is not guaranteed to be aligned
1814 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001815 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 // from the real pointer as a smi.
1817 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1818 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1819 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1820#ifdef DEBUG
1821 if (msg != NULL) {
1822 RecordComment("Abort message: ");
1823 RecordComment(msg);
1824 }
1825#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001826 // Disable stub call restrictions to always allow calls to abort.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001827 AllowStubCallsScope allow_scope(this, true);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 push(eax);
1830 push(Immediate(p0));
1831 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1832 CallRuntime(Runtime::kAbort, 2);
1833 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001834 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835}
1836
1837
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001838void MacroAssembler::JumpIfNotNumber(Register reg,
1839 TypeInfo info,
1840 Label* on_not_number) {
1841 if (FLAG_debug_code) AbortIfSmi(reg);
1842 if (!info.IsNumber()) {
1843 cmp(FieldOperand(reg, HeapObject::kMapOffset),
1844 Factory::heap_number_map());
1845 j(not_equal, on_not_number);
1846 }
1847}
1848
1849
1850void MacroAssembler::ConvertToInt32(Register dst,
1851 Register source,
1852 Register scratch,
1853 TypeInfo info,
1854 Label* on_not_int32) {
1855 if (FLAG_debug_code) {
1856 AbortIfSmi(source);
1857 AbortIfNotNumber(source);
1858 }
1859 if (info.IsInteger32()) {
1860 cvttsd2si(dst, FieldOperand(source, HeapNumber::kValueOffset));
1861 } else {
1862 Label done;
1863 bool push_pop = (scratch.is(no_reg) && dst.is(source));
1864 ASSERT(!scratch.is(source));
1865 if (push_pop) {
1866 push(dst);
1867 scratch = dst;
1868 }
1869 if (scratch.is(no_reg)) scratch = dst;
1870 cvttsd2si(scratch, FieldOperand(source, HeapNumber::kValueOffset));
1871 cmp(scratch, 0x80000000u);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001872 if (push_pop) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001873 j(not_equal, &done);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001874 pop(dst);
1875 jmp(on_not_int32);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001876 } else {
1877 j(equal, on_not_int32);
1878 }
1879
1880 bind(&done);
1881 if (push_pop) {
1882 add(Operand(esp), Immediate(kPointerSize)); // Pop.
1883 }
1884 if (!scratch.is(dst)) {
1885 mov(dst, scratch);
1886 }
1887 }
1888}
1889
1890
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001891void MacroAssembler::LoadPowerOf2(XMMRegister dst,
1892 Register scratch,
1893 int power) {
1894 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
1895 HeapNumber::kExponentBits));
1896 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
1897 movd(dst, Operand(scratch));
1898 psllq(dst, HeapNumber::kMantissaBits);
1899}
1900
1901
ager@chromium.org5c838252010-02-19 08:53:10 +00001902void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1903 Register instance_type,
1904 Register scratch,
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001905 Label* failure) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001906 if (!scratch.is(instance_type)) {
1907 mov(scratch, instance_type);
1908 }
1909 and_(scratch,
1910 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1911 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1912 j(not_equal, failure);
1913}
1914
1915
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001916void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1917 Register object2,
1918 Register scratch1,
1919 Register scratch2,
1920 Label* failure) {
1921 // Check that both objects are not smis.
1922 ASSERT_EQ(0, kSmiTag);
1923 mov(scratch1, Operand(object1));
1924 and_(scratch1, Operand(object2));
1925 test(scratch1, Immediate(kSmiTagMask));
1926 j(zero, failure);
1927
1928 // Load instance type for both strings.
1929 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1930 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1931 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1932 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1933
1934 // Check that both are flat ascii strings.
1935 const int kFlatAsciiStringMask =
1936 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1937 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1938 // Interleave bits from both instance types and compare them in one check.
1939 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1940 and_(scratch1, kFlatAsciiStringMask);
1941 and_(scratch2, kFlatAsciiStringMask);
1942 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1943 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1944 j(not_equal, failure);
1945}
1946
1947
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001948void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1949 int frameAlignment = OS::ActivationFrameAlignment();
1950 if (frameAlignment != 0) {
1951 // Make stack end at alignment and make room for num_arguments words
1952 // and the original value of esp.
1953 mov(scratch, esp);
1954 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1955 ASSERT(IsPowerOf2(frameAlignment));
1956 and_(esp, -frameAlignment);
1957 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1958 } else {
1959 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1960 }
1961}
1962
1963
1964void MacroAssembler::CallCFunction(ExternalReference function,
1965 int num_arguments) {
1966 // Trashing eax is ok as it will be the return value.
1967 mov(Operand(eax), Immediate(function));
1968 CallCFunction(eax, num_arguments);
1969}
1970
1971
1972void MacroAssembler::CallCFunction(Register function,
1973 int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001974 // Check stack alignment.
1975 if (FLAG_debug_code) {
1976 CheckStackAlignment();
1977 }
1978
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001979 call(Operand(function));
1980 if (OS::ActivationFrameAlignment() != 0) {
1981 mov(esp, Operand(esp, num_arguments * kPointerSize));
1982 } else {
1983 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1984 }
1985}
1986
1987
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001989 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001990 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 // The size is adjusted with kGap on order for the assembler to generate size
1992 // bytes of instructions without failing with buffer size constraints.
1993 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1994}
1995
1996
1997CodePatcher::~CodePatcher() {
1998 // Indicate that code has changed.
1999 CPU::FlushICache(address_, size_);
2000
2001 // Check that the code was patched as expected.
2002 ASSERT(masm_.pc_ == address_ + size_);
2003 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2004}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005
2006
2007} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002008
2009#endif // V8_TARGET_ARCH_IA32