blob: c465c4dce51f0808f191941fc6fdab2478b3767d [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
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
33#include "runtime.h"
34#include "serialize.h"
35
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000039// -------------------------------------------------------------------------
40// MacroAssembler implementation.
41
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042MacroAssembler::MacroAssembler(void* buffer, int size)
43 : Assembler(buffer, size),
kasper.lund7276f142008-07-30 08:49:36 +000044 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000045 allow_stub_calls_(true),
46 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047}
48
49
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000050void MacroAssembler::RecordWriteHelper(Register object,
51 Register addr,
52 Register scratch) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053 Label fast;
54
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000055 // Compute the page start address from the heap object pointer, and reuse
56 // the 'object' register for it.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000057 and_(object, ~Page::kPageAlignmentMask);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000058 Register page_start = object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000060 // Compute the bit addr in the remembered set/index of the pointer in the
61 // page. Reuse 'addr' as pointer_offset.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000062 sub(addr, Operand(page_start));
63 shr(addr, kObjectAlignmentBits);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000064 Register pointer_offset = addr;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065
66 // If the bit offset lies beyond the normal remembered set range, it is in
67 // the extra remembered set area of a large object.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000068 cmp(pointer_offset, Page::kPageSize / kPointerSize);
69 j(less, &fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000071 // Adjust 'page_start' so that addressing using 'pointer_offset' hits the
72 // extra remembered set after the large object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000074 // Find the length of the large object (FixedArray).
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000075 mov(scratch, Operand(page_start, Page::kObjectStartOffset
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000076 + FixedArray::kLengthOffset));
77 Register array_length = scratch;
78
79 // Extra remembered set starts right after the large object (a FixedArray), at
80 // page_start + kObjectStartOffset + objectSize
81 // where objectSize is FixedArray::kHeaderSize + kPointerSize * array_length.
82 // Add the delta between the end of the normal RSet and the start of the
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000083 // extra RSet to 'page_start', so that addressing the bit using
84 // 'pointer_offset' hits the extra RSet words.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000085 lea(page_start,
86 Operand(page_start, array_length, times_pointer_size,
87 Page::kObjectStartOffset + FixedArray::kHeaderSize
88 - Page::kRSetEndOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089
90 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
91 // to limit code size. We should probably evaluate this decision by
92 // measuring the performance of an equivalent implementation using
93 // "simpler" instructions
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000094 bind(&fast);
95 bts(Operand(page_start, Page::kRSetOffset), pointer_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096}
97
98
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000099void MacroAssembler::InNewSpace(Register object,
100 Register scratch,
101 Condition cc,
102 Label* branch) {
103 if (Serializer::enabled()) {
104 // Can't do arithmetic on external references if it might get serialized.
105 mov(scratch, Operand(object));
106 // The mask isn't really an address. We load it as an external reference in
107 // case the size of the new space is different between the snapshot maker
108 // and the running system.
109 and_(Operand(scratch), Immediate(ExternalReference::new_space_mask()));
110 cmp(Operand(scratch), Immediate(ExternalReference::new_space_start()));
111 j(cc, branch);
112 } else {
113 int32_t new_space_start = reinterpret_cast<int32_t>(
114 ExternalReference::new_space_start().address());
115 lea(scratch, Operand(object, -new_space_start));
116 and_(scratch, Heap::NewSpaceMask());
117 j(cc, branch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119}
120
121
122// Set the remembered set bit for [object+offset].
123// object is the object being stored into, value is the object being stored.
124// If offset is zero, then the scratch register contains the array index into
125// the elements array represented as a Smi.
126// All registers are clobbered by the operation.
127void MacroAssembler::RecordWrite(Register object, int offset,
128 Register value, Register scratch) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000129 // The compiled code assumes that record write doesn't change the
130 // context register, so we check that none of the clobbered
131 // registers are esi.
132 ASSERT(!object.is(esi) && !value.is(esi) && !scratch.is(esi));
133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 // First, check if a remembered set write is even needed. The tests below
135 // catch stores of Smis and stores into young gen (which does not have space
136 // for the remembered set bits.
137 Label done;
138
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000139 // Skip barrier if writing a smi.
140 ASSERT_EQ(0, kSmiTag);
141 test(value, Immediate(kSmiTagMask));
142 j(zero, &done);
143
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000144 InNewSpace(object, value, equal, &done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145
whesse@chromium.orge88a9ed2010-04-15 15:07:46 +0000146 // The offset is relative to a tagged or untagged HeapObject pointer,
147 // so either offset or offset + kHeapObjectTag must be a
148 // multiple of kPointerSize.
149 ASSERT(IsAligned(offset, kPointerSize) ||
150 IsAligned(offset + kHeapObjectTag, kPointerSize));
151
152 // We use optimized write barrier code if the word being written to is not in
153 // a large object chunk or is in the first page of a large object chunk.
154 // We make sure that an offset is inside the right limits whether it is
155 // tagged or untagged.
156 if ((offset > 0) && (offset < Page::kMaxHeapObjectSize - kHeapObjectTag)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 // Compute the bit offset in the remembered set, leave it in 'value'.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000158 lea(value, Operand(object, offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 and_(value, Page::kPageAlignmentMask);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000160 shr(value, kPointerSizeLog2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161
162 // Compute the page address from the heap object pointer, leave it in
163 // 'object'.
164 and_(object, ~Page::kPageAlignmentMask);
165
166 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
167 // to limit code size. We should probably evaluate this decision by
168 // measuring the performance of an equivalent implementation using
169 // "simpler" instructions
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000170 bts(Operand(object, Page::kRSetOffset), value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 } else {
172 Register dst = scratch;
173 if (offset != 0) {
174 lea(dst, Operand(object, offset));
175 } else {
176 // array access: calculate the destination address in the same manner as
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000177 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
178 // into an array of words.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000179 ASSERT_EQ(1, kSmiTagSize);
180 ASSERT_EQ(0, kSmiTag);
181 lea(dst, Operand(object, dst, times_half_pointer_size,
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000182 FixedArray::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 }
184 // If we are already generating a shared stub, not inlining the
185 // record write code isn't going to save us any memory.
186 if (generating_stub()) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000187 RecordWriteHelper(object, dst, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 } else {
189 RecordWriteStub stub(object, dst, value);
190 CallStub(&stub);
191 }
192 }
193
194 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000195
196 // Clobber all input registers when running with the debug-code flag
197 // turned on to provoke errors.
198 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000199 mov(object, Immediate(BitCast<int32_t>(kZapValue)));
200 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
201 mov(scratch, Immediate(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000202 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203}
204
205
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000206void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
207 cmp(esp,
208 Operand::StaticVariable(ExternalReference::address_of_stack_limit()));
209 j(below, on_stack_overflow);
210}
211
212
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000213#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214void MacroAssembler::SaveRegistersToMemory(RegList regs) {
215 ASSERT((regs & ~kJSCallerSaved) == 0);
216 // Copy the content of registers to memory location.
217 for (int i = 0; i < kNumJSCallerSaved; i++) {
218 int r = JSCallerSavedCode(i);
219 if ((regs & (1 << r)) != 0) {
220 Register reg = { r };
221 ExternalReference reg_addr =
222 ExternalReference(Debug_Address::Register(i));
223 mov(Operand::StaticVariable(reg_addr), reg);
224 }
225 }
226}
227
228
229void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
230 ASSERT((regs & ~kJSCallerSaved) == 0);
231 // Copy the content of memory location to registers.
232 for (int i = kNumJSCallerSaved; --i >= 0;) {
233 int r = JSCallerSavedCode(i);
234 if ((regs & (1 << r)) != 0) {
235 Register reg = { r };
236 ExternalReference reg_addr =
237 ExternalReference(Debug_Address::Register(i));
238 mov(reg, Operand::StaticVariable(reg_addr));
239 }
240 }
241}
242
243
244void MacroAssembler::PushRegistersFromMemory(RegList regs) {
245 ASSERT((regs & ~kJSCallerSaved) == 0);
246 // Push the content of the memory location to the stack.
247 for (int i = 0; i < kNumJSCallerSaved; i++) {
248 int r = JSCallerSavedCode(i);
249 if ((regs & (1 << r)) != 0) {
250 ExternalReference reg_addr =
251 ExternalReference(Debug_Address::Register(i));
252 push(Operand::StaticVariable(reg_addr));
253 }
254 }
255}
256
257
258void MacroAssembler::PopRegistersToMemory(RegList regs) {
259 ASSERT((regs & ~kJSCallerSaved) == 0);
260 // Pop the content from the stack to the memory location.
261 for (int i = kNumJSCallerSaved; --i >= 0;) {
262 int r = JSCallerSavedCode(i);
263 if ((regs & (1 << r)) != 0) {
264 ExternalReference reg_addr =
265 ExternalReference(Debug_Address::Register(i));
266 pop(Operand::StaticVariable(reg_addr));
267 }
268 }
269}
270
271
272void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
273 Register scratch,
274 RegList regs) {
275 ASSERT((regs & ~kJSCallerSaved) == 0);
276 // Copy the content of the stack to the memory location and adjust base.
277 for (int i = kNumJSCallerSaved; --i >= 0;) {
278 int r = JSCallerSavedCode(i);
279 if ((regs & (1 << r)) != 0) {
280 mov(scratch, Operand(base, 0));
281 ExternalReference reg_addr =
282 ExternalReference(Debug_Address::Register(i));
283 mov(Operand::StaticVariable(reg_addr), scratch);
284 lea(base, Operand(base, kPointerSize));
285 }
286 }
287}
ager@chromium.org5c838252010-02-19 08:53:10 +0000288
289void MacroAssembler::DebugBreak() {
290 Set(eax, Immediate(0));
291 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak)));
292 CEntryStub ces(1);
293 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
294}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000295#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296
297void MacroAssembler::Set(Register dst, const Immediate& x) {
298 if (x.is_zero()) {
299 xor_(dst, Operand(dst)); // shorter than mov
300 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000301 mov(dst, x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 }
303}
304
305
306void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
307 mov(dst, x);
308}
309
310
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000311void MacroAssembler::CmpObjectType(Register heap_object,
312 InstanceType type,
313 Register map) {
314 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
315 CmpInstanceType(map, type);
316}
317
318
319void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
320 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
321 static_cast<int8_t>(type));
322}
323
324
ager@chromium.org5c838252010-02-19 08:53:10 +0000325void MacroAssembler::CheckMap(Register obj,
326 Handle<Map> map,
327 Label* fail,
328 bool is_heap_object) {
329 if (!is_heap_object) {
330 test(obj, Immediate(kSmiTagMask));
331 j(zero, fail);
332 }
333 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
334 j(not_equal, fail);
335}
336
337
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000338Condition MacroAssembler::IsObjectStringType(Register heap_object,
339 Register map,
340 Register instance_type) {
341 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
342 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
343 ASSERT(kNotStringTag != 0);
344 test(instance_type, Immediate(kIsNotStringMask));
345 return zero;
346}
347
348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349void MacroAssembler::FCmp() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000350 if (CpuFeatures::IsSupported(CMOV)) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000351 fucomip();
352 ffree(0);
353 fincstp();
354 } else {
355 fucompp();
356 push(eax);
357 fnstsw_ax();
358 sahf();
359 pop(eax);
360 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361}
362
363
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000364void MacroAssembler::AbortIfNotNumber(Register object) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000365 Label ok;
366 test(object, Immediate(kSmiTagMask));
367 j(zero, &ok);
368 cmp(FieldOperand(object, HeapObject::kMapOffset),
369 Factory::heap_number_map());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000370 Assert(equal, "Operand not a number");
ager@chromium.org5c838252010-02-19 08:53:10 +0000371 bind(&ok);
372}
373
374
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000375void MacroAssembler::AbortIfNotSmi(Register object) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000376 test(object, Immediate(kSmiTagMask));
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000377 Assert(equal, "Operand not a smi");
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000378}
379
380
ager@chromium.org7c537e22008-10-16 08:43:32 +0000381void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 push(ebp);
383 mov(ebp, Operand(esp));
384 push(esi);
385 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000386 push(Immediate(CodeObject()));
387 if (FLAG_debug_code) {
388 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
389 Check(not_equal, "code object not properly patched");
390 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391}
392
393
ager@chromium.org7c537e22008-10-16 08:43:32 +0000394void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 if (FLAG_debug_code) {
396 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
397 Immediate(Smi::FromInt(type)));
398 Check(equal, "stack frame types must match");
399 }
400 leave();
401}
402
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000403void MacroAssembler::EnterExitFramePrologue(ExitFrame::Mode mode) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000404 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000405 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000406 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
407 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
408 push(ebp);
409 mov(ebp, Operand(esp));
410
411 // Reserve room for entry stack pointer and push the debug marker.
412 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +0000413 push(Immediate(0)); // Saved entry sp, patched before call.
414 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000415
416 // Save the frame pointer and the context in top.
417 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
418 ExternalReference context_address(Top::k_context_address);
419 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
420 mov(Operand::StaticVariable(context_address), esi);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000421}
ager@chromium.org236ad962008-09-25 09:45:57 +0000422
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000423void MacroAssembler::EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000424#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000425 // Save the state of all registers to the stack from the memory
426 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000427 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000428 // TODO(1243899): This should be symmetric to
429 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
430 // correct here, but computed for the other call. Very error
431 // prone! FIX THIS. Actually there are deeper problems with
432 // register saving than this asymmetry (see the bug report
433 // associated with this issue).
434 PushRegistersFromMemory(kJSCallerSaved);
435 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000436#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000437
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000438 // Reserve space for arguments.
439 sub(Operand(esp), Immediate(argc * kPointerSize));
ager@chromium.org236ad962008-09-25 09:45:57 +0000440
441 // Get the required frame alignment for the OS.
442 static const int kFrameAlignment = OS::ActivationFrameAlignment();
443 if (kFrameAlignment > 0) {
444 ASSERT(IsPowerOf2(kFrameAlignment));
445 and_(esp, -kFrameAlignment);
446 }
447
448 // Patch the saved entry sp.
449 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
450}
451
452
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000453void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
454 EnterExitFramePrologue(mode);
455
456 // Setup argc and argv in callee-saved registers.
457 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
458 mov(edi, Operand(eax));
459 lea(esi, Operand(ebp, eax, times_4, offset));
460
461 EnterExitFrameEpilogue(mode, 2);
462}
463
464
465void MacroAssembler::EnterApiExitFrame(ExitFrame::Mode mode,
466 int stack_space,
467 int argc) {
468 EnterExitFramePrologue(mode);
469
470 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
471 lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset));
472
473 EnterExitFrameEpilogue(mode, argc);
474}
475
476
477void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000478#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000479 // Restore the memory copy of the registers by digging them out from
480 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000481 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000482 // It's okay to clobber register ebx below because we don't need
483 // the function pointer after this.
484 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000485 int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000486 lea(ebx, Operand(ebp, kOffset));
487 CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved);
488 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000489#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000490
491 // Get the return address from the stack and restore the frame pointer.
492 mov(ecx, Operand(ebp, 1 * kPointerSize));
493 mov(ebp, Operand(ebp, 0 * kPointerSize));
494
495 // Pop the arguments and the receiver from the caller stack.
496 lea(esp, Operand(esi, 1 * kPointerSize));
497
498 // Restore current context from top and clear it in debug mode.
499 ExternalReference context_address(Top::k_context_address);
500 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000501#ifdef DEBUG
502 mov(Operand::StaticVariable(context_address), Immediate(0));
503#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000504
505 // Push the return address to get ready to return.
506 push(ecx);
507
508 // Clear the top frame.
509 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
510 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514void MacroAssembler::PushTryHandler(CodeLocation try_location,
515 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000516 // Adjust this code if not the case.
517 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 // The pc (return address) is already on TOS.
519 if (try_location == IN_JAVASCRIPT) {
520 if (type == TRY_CATCH_HANDLER) {
521 push(Immediate(StackHandler::TRY_CATCH));
522 } else {
523 push(Immediate(StackHandler::TRY_FINALLY));
524 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 } else {
527 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000528 // The frame pointer does not point to a JS frame so we save NULL
529 // for ebp. We expect the code throwing an exception to check ebp
530 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000532 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000534 // Save the current handler as the next handler.
535 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
536 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
538}
539
540
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000541void MacroAssembler::PopTryHandler() {
542 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
543 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
544 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
545}
546
547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
549 JSObject* holder, Register holder_reg,
550 Register scratch,
ager@chromium.org5c838252010-02-19 08:53:10 +0000551 int save_at_depth,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 Label* miss) {
553 // Make sure there's no overlap between scratch and the other
554 // registers.
555 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
556
557 // Keep track of the current object in register reg.
558 Register reg = object_reg;
ager@chromium.org5c838252010-02-19 08:53:10 +0000559 int depth = 0;
560
561 if (save_at_depth == depth) {
562 mov(Operand(esp, kPointerSize), object_reg);
563 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564
565 // Check the maps in the prototype chain.
566 // Traverse the prototype chain from the object and do map checks.
567 while (object != holder) {
568 depth++;
569
570 // Only global objects and objects that do not require access
571 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000572 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573
574 JSObject* prototype = JSObject::cast(object->GetPrototype());
575 if (Heap::InNewSpace(prototype)) {
576 // Get the map of the current object.
577 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
578 cmp(Operand(scratch), Immediate(Handle<Map>(object->map())));
579 // Branch on the result of the map check.
580 j(not_equal, miss, not_taken);
581 // Check access rights to the global object. This has to happen
582 // after the map check so that we know that the object is
583 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000584 if (object->IsJSGlobalProxy()) {
585 CheckAccessGlobalProxy(reg, scratch, miss);
586
587 // Restore scratch register to be the map of the object.
588 // We load the prototype from the map in the scratch register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
590 }
591 // The prototype is in new space; we cannot store a reference
592 // to it in the code. Load it from the map.
593 reg = holder_reg; // from now the object is in holder_reg
594 mov(reg, FieldOperand(scratch, Map::kPrototypeOffset));
595 } else {
596 // Check the map of the current object.
597 cmp(FieldOperand(reg, HeapObject::kMapOffset),
598 Immediate(Handle<Map>(object->map())));
599 // Branch on the result of the map check.
600 j(not_equal, miss, not_taken);
601 // Check access rights to the global object. This has to happen
602 // after the map check so that we know that the object is
603 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000604 if (object->IsJSGlobalProxy()) {
605 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 }
607 // The prototype is in old space; load it directly.
608 reg = holder_reg; // from now the object is in holder_reg
609 mov(reg, Handle<JSObject>(prototype));
610 }
611
ager@chromium.org5c838252010-02-19 08:53:10 +0000612 if (save_at_depth == depth) {
613 mov(Operand(esp, kPointerSize), reg);
614 }
615
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 // Go to the next object in the prototype chain.
617 object = prototype;
618 }
619
620 // Check the holder map.
621 cmp(FieldOperand(reg, HeapObject::kMapOffset),
622 Immediate(Handle<Map>(holder->map())));
623 j(not_equal, miss, not_taken);
624
625 // Log the check depth.
ager@chromium.org5c838252010-02-19 08:53:10 +0000626 LOG(IntEvent("check-maps-depth", depth + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
628 // Perform security check for access to the global object and return
629 // the holder register.
630 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000631 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
632 if (object->IsJSGlobalProxy()) {
633 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 }
635 return reg;
636}
637
638
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000639void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000640 Register scratch,
641 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000642 Label same_contexts;
643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 ASSERT(!holder_reg.is(scratch));
645
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000646 // Load current lexical context from the stack frame.
647 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
648
649 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 if (FLAG_debug_code) {
651 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000652 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000654 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
656 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000657 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
658
659 // Check the context is a global context.
660 if (FLAG_debug_code) {
661 push(scratch);
662 // Read the first word and compare to global_context_map.
663 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
664 cmp(scratch, Factory::global_context_map());
665 Check(equal, "JSGlobalObject::global_context should be a global context.");
666 pop(scratch);
667 }
668
669 // Check if both contexts are the same.
670 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
671 j(equal, &same_contexts, taken);
672
673 // Compare security tokens, save holder_reg on the stack so we can use it
674 // as a temporary register.
675 //
676 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
677 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 // Check that the security token in the calling global object is
679 // compatible with the security token in the receiving global
680 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000681 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
682
683 // Check the context is a global context.
684 if (FLAG_debug_code) {
685 cmp(holder_reg, Factory::null_value());
686 Check(not_equal, "JSGlobalProxy::context() should not be null.");
687
688 push(holder_reg);
689 // Read the first word and compare to global_context_map(),
690 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
691 cmp(holder_reg, Factory::global_context_map());
692 Check(equal, "JSGlobalObject::global_context should be a global context.");
693 pop(holder_reg);
694 }
695
696 int token_offset = Context::kHeaderSize +
697 Context::SECURITY_TOKEN_INDEX * kPointerSize;
698 mov(scratch, FieldOperand(scratch, token_offset));
699 cmp(scratch, FieldOperand(holder_reg, token_offset));
700 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000702
703 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704}
705
706
ager@chromium.orga1645e22009-09-09 19:27:10 +0000707void MacroAssembler::LoadAllocationTopHelper(Register result,
708 Register result_end,
709 Register scratch,
710 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000711 ExternalReference new_space_allocation_top =
712 ExternalReference::new_space_allocation_top_address();
713
714 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000715 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000716 // No use of scratch if allocation top is provided.
717 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000718#ifdef DEBUG
719 // Assert that result actually contains top on entry.
720 cmp(result, Operand::StaticVariable(new_space_allocation_top));
721 Check(equal, "Unexpected allocation top");
722#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000723 return;
724 }
725
726 // Move address of new object to result. Use scratch register if available.
727 if (scratch.is(no_reg)) {
728 mov(result, Operand::StaticVariable(new_space_allocation_top));
729 } else {
730 ASSERT(!scratch.is(result_end));
731 mov(Operand(scratch), Immediate(new_space_allocation_top));
732 mov(result, Operand(scratch, 0));
733 }
734}
735
736
737void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
738 Register scratch) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000739 if (FLAG_debug_code) {
740 test(result_end, Immediate(kObjectAlignmentMask));
741 Check(zero, "Unaligned allocation in new space");
742 }
743
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000744 ExternalReference new_space_allocation_top =
745 ExternalReference::new_space_allocation_top_address();
746
747 // Update new top. Use scratch if available.
748 if (scratch.is(no_reg)) {
749 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
750 } else {
751 mov(Operand(scratch, 0), result_end);
752 }
753}
754
ager@chromium.orga1645e22009-09-09 19:27:10 +0000755
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000756void MacroAssembler::AllocateInNewSpace(int object_size,
757 Register result,
758 Register result_end,
759 Register scratch,
760 Label* gc_required,
761 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000762 ASSERT(!result.is(result_end));
763
764 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000765 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000766
767 // Calculate new top and bail out if new space is exhausted.
768 ExternalReference new_space_allocation_limit =
769 ExternalReference::new_space_allocation_limit_address();
770 lea(result_end, Operand(result, object_size));
771 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
772 j(above, gc_required, not_taken);
773
ager@chromium.orga1645e22009-09-09 19:27:10 +0000774 // Tag result if requested.
775 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000776 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000777 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000778
779 // Update allocation top.
780 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000781}
782
783
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000784void MacroAssembler::AllocateInNewSpace(int header_size,
785 ScaleFactor element_size,
786 Register element_count,
787 Register result,
788 Register result_end,
789 Register scratch,
790 Label* gc_required,
791 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000792 ASSERT(!result.is(result_end));
793
794 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000795 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000796
797 // Calculate new top and bail out if new space is exhausted.
798 ExternalReference new_space_allocation_limit =
799 ExternalReference::new_space_allocation_limit_address();
800 lea(result_end, Operand(result, element_count, element_size, header_size));
801 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
802 j(above, gc_required);
803
ager@chromium.orga1645e22009-09-09 19:27:10 +0000804 // Tag result if requested.
805 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000806 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000807 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000808
809 // Update allocation top.
810 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000811}
812
813
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000814void MacroAssembler::AllocateInNewSpace(Register object_size,
815 Register result,
816 Register result_end,
817 Register scratch,
818 Label* gc_required,
819 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000820 ASSERT(!result.is(result_end));
821
822 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000823 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000824
825 // Calculate new top and bail out if new space is exhausted.
826 ExternalReference new_space_allocation_limit =
827 ExternalReference::new_space_allocation_limit_address();
828 if (!object_size.is(result_end)) {
829 mov(result_end, object_size);
830 }
831 add(result_end, Operand(result));
832 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
833 j(above, gc_required, not_taken);
834
ager@chromium.orga1645e22009-09-09 19:27:10 +0000835 // Tag result if requested.
836 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000837 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000838 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000839
840 // Update allocation top.
841 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000842}
843
844
845void MacroAssembler::UndoAllocationInNewSpace(Register object) {
846 ExternalReference new_space_allocation_top =
847 ExternalReference::new_space_allocation_top_address();
848
849 // Make sure the object has no tag before resetting top.
850 and_(Operand(object), Immediate(~kHeapObjectTagMask));
851#ifdef DEBUG
852 cmp(object, Operand::StaticVariable(new_space_allocation_top));
853 Check(below, "Undo allocation of non allocated memory");
854#endif
855 mov(Operand::StaticVariable(new_space_allocation_top), object);
856}
857
858
ager@chromium.org3811b432009-10-28 14:53:37 +0000859void MacroAssembler::AllocateHeapNumber(Register result,
860 Register scratch1,
861 Register scratch2,
862 Label* gc_required) {
863 // Allocate heap number in new space.
864 AllocateInNewSpace(HeapNumber::kSize,
865 result,
866 scratch1,
867 scratch2,
868 gc_required,
869 TAG_OBJECT);
870
871 // Set the map.
872 mov(FieldOperand(result, HeapObject::kMapOffset),
873 Immediate(Factory::heap_number_map()));
874}
875
876
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000877void MacroAssembler::AllocateTwoByteString(Register result,
878 Register length,
879 Register scratch1,
880 Register scratch2,
881 Register scratch3,
882 Label* gc_required) {
883 // Calculate the number of bytes needed for the characters in the string while
884 // observing object alignment.
885 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000886 ASSERT(kShortSize == 2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000887 // scratch1 = length * 2 + kObjectAlignmentMask.
888 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000889 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
890
891 // Allocate two byte string in new space.
892 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
893 times_1,
894 scratch1,
895 result,
896 scratch2,
897 scratch3,
898 gc_required,
899 TAG_OBJECT);
900
901 // Set the map, length and hash field.
902 mov(FieldOperand(result, HeapObject::kMapOffset),
903 Immediate(Factory::string_map()));
904 mov(FieldOperand(result, String::kLengthOffset), length);
905 mov(FieldOperand(result, String::kHashFieldOffset),
906 Immediate(String::kEmptyHashField));
907}
908
909
910void MacroAssembler::AllocateAsciiString(Register result,
911 Register length,
912 Register scratch1,
913 Register scratch2,
914 Register scratch3,
915 Label* gc_required) {
916 // Calculate the number of bytes needed for the characters in the string while
917 // observing object alignment.
918 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
919 mov(scratch1, length);
920 ASSERT(kCharSize == 1);
921 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
922 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
923
924 // Allocate ascii string in new space.
925 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
926 times_1,
927 scratch1,
928 result,
929 scratch2,
930 scratch3,
931 gc_required,
932 TAG_OBJECT);
933
934 // Set the map, length and hash field.
935 mov(FieldOperand(result, HeapObject::kMapOffset),
936 Immediate(Factory::ascii_string_map()));
937 mov(FieldOperand(result, String::kLengthOffset), length);
938 mov(FieldOperand(result, String::kHashFieldOffset),
939 Immediate(String::kEmptyHashField));
940}
941
942
943void MacroAssembler::AllocateConsString(Register result,
944 Register scratch1,
945 Register scratch2,
946 Label* gc_required) {
947 // Allocate heap number in new space.
948 AllocateInNewSpace(ConsString::kSize,
949 result,
950 scratch1,
951 scratch2,
952 gc_required,
953 TAG_OBJECT);
954
955 // Set the map. The other fields are left uninitialized.
956 mov(FieldOperand(result, HeapObject::kMapOffset),
957 Immediate(Factory::cons_string_map()));
958}
959
960
961void MacroAssembler::AllocateAsciiConsString(Register result,
962 Register scratch1,
963 Register scratch2,
964 Label* gc_required) {
965 // Allocate heap number in new space.
966 AllocateInNewSpace(ConsString::kSize,
967 result,
968 scratch1,
969 scratch2,
970 gc_required,
971 TAG_OBJECT);
972
973 // Set the map. The other fields are left uninitialized.
974 mov(FieldOperand(result, HeapObject::kMapOffset),
975 Immediate(Factory::cons_ascii_string_map()));
976}
977
978
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000979void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
980 Register result,
981 Register op,
982 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000983 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000984 test(result, Operand(result));
985 ok.Branch(not_zero, taken);
986 test(op, Operand(op));
987 then_target->Branch(sign, not_taken);
988 ok.Bind();
989}
990
991
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992void MacroAssembler::NegativeZeroTest(Register result,
993 Register op,
994 Label* then_label) {
995 Label ok;
996 test(result, Operand(result));
997 j(not_zero, &ok, taken);
998 test(op, Operand(op));
999 j(sign, then_label, not_taken);
1000 bind(&ok);
1001}
1002
1003
1004void MacroAssembler::NegativeZeroTest(Register result,
1005 Register op1,
1006 Register op2,
1007 Register scratch,
1008 Label* then_label) {
1009 Label ok;
1010 test(result, Operand(result));
1011 j(not_zero, &ok, taken);
1012 mov(scratch, Operand(op1));
1013 or_(scratch, Operand(op2));
1014 j(sign, then_label, not_taken);
1015 bind(&ok);
1016}
1017
1018
ager@chromium.org7c537e22008-10-16 08:43:32 +00001019void MacroAssembler::TryGetFunctionPrototype(Register function,
1020 Register result,
1021 Register scratch,
1022 Label* miss) {
1023 // Check that the receiver isn't a smi.
1024 test(function, Immediate(kSmiTagMask));
1025 j(zero, miss, not_taken);
1026
1027 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001029 j(not_equal, miss, not_taken);
1030
1031 // Make sure that the function has an instance prototype.
1032 Label non_instance;
1033 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1034 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1035 j(not_zero, &non_instance, not_taken);
1036
1037 // Get the prototype or initial map from the function.
1038 mov(result,
1039 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1040
1041 // If the prototype or initial map is the hole, don't return it and
1042 // simply miss the cache instead. This will allow us to allocate a
1043 // prototype object on-demand in the runtime system.
1044 cmp(Operand(result), Immediate(Factory::the_hole_value()));
1045 j(equal, miss, not_taken);
1046
1047 // If the function does not have an initial map, we're done.
1048 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001049 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001050 j(not_equal, &done);
1051
1052 // Get the prototype from the initial map.
1053 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1054 jmp(&done);
1055
1056 // Non-instance prototype: Fetch prototype from constructor field
1057 // in initial map.
1058 bind(&non_instance);
1059 mov(result, FieldOperand(result, Map::kConstructorOffset));
1060
1061 // All done.
1062 bind(&done);
1063}
1064
1065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001067 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +00001068 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069}
1070
1071
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001072Object* MacroAssembler::TryCallStub(CodeStub* stub) {
1073 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1074 Object* result = stub->TryGetCode();
1075 if (!result->IsFailure()) {
1076 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1077 }
1078 return result;
1079}
1080
1081
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001082void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001083 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001084 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1085}
1086
1087
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001088Object* MacroAssembler::TryTailCallStub(CodeStub* stub) {
1089 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1090 Object* result = stub->TryGetCode();
1091 if (!result->IsFailure()) {
1092 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1093 }
1094 return result;
1095}
1096
1097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098void MacroAssembler::StubReturn(int argc) {
1099 ASSERT(argc >= 1 && generating_stub());
1100 ret((argc - 1) * kPointerSize);
1101}
1102
1103
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001104void MacroAssembler::IllegalOperation(int num_arguments) {
1105 if (num_arguments > 0) {
1106 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1107 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001108 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109}
1110
1111
1112void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1113 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1114}
1115
1116
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001117Object* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1118 int num_arguments) {
1119 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1120}
1121
1122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001124 // If the expected number of arguments of the runtime function is
1125 // constant, we check that the actual number of arguments match the
1126 // expectation.
1127 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001128 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 return;
1130 }
1131
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132 // TODO(1236192): Most runtime routines don't need the number of
1133 // arguments passed in because it is constant. At some point we
1134 // should remove this need and make the runtime routine entry code
1135 // smarter.
1136 Set(eax, Immediate(num_arguments));
1137 mov(ebx, Immediate(ExternalReference(f)));
1138 CEntryStub ces(1);
1139 CallStub(&ces);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140}
1141
1142
ager@chromium.org5c838252010-02-19 08:53:10 +00001143void MacroAssembler::CallExternalReference(ExternalReference ref,
1144 int num_arguments) {
1145 mov(eax, Immediate(num_arguments));
1146 mov(ebx, Immediate(ref));
1147
1148 CEntryStub stub(1);
1149 CallStub(&stub);
1150}
1151
1152
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001153Object* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1154 int num_arguments) {
1155 if (f->nargs >= 0 && f->nargs != num_arguments) {
1156 IllegalOperation(num_arguments);
1157 // Since we did not call the stub, there was no allocation failure.
1158 // Return some non-failure object.
1159 return Heap::undefined_value();
1160 }
1161
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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.
1166 Set(eax, Immediate(num_arguments));
1167 mov(ebx, Immediate(ExternalReference(f)));
1168 CEntryStub ces(1);
1169 return TryCallStub(&ces);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001170}
1171
1172
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001173void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1174 int num_arguments,
1175 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001176 // TODO(1236192): Most runtime routines don't need the number of
1177 // arguments passed in because it is constant. At some point we
1178 // should remove this need and make the runtime routine entry code
1179 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001180 Set(eax, Immediate(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001181 JumpToExternalReference(ext);
1182}
1183
1184
1185void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1186 int num_arguments,
1187 int result_size) {
1188 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189}
1190
1191
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001192void MacroAssembler::PushHandleScope(Register scratch) {
1193 // Push the number of extensions, smi-tagged so the gc will ignore it.
1194 ExternalReference extensions_address =
1195 ExternalReference::handle_scope_extensions_address();
1196 mov(scratch, Operand::StaticVariable(extensions_address));
1197 ASSERT_EQ(0, kSmiTag);
1198 shl(scratch, kSmiTagSize);
1199 push(scratch);
1200 mov(Operand::StaticVariable(extensions_address), Immediate(0));
1201 // Push next and limit pointers which will be wordsize aligned and
1202 // hence automatically smi tagged.
1203 ExternalReference next_address =
1204 ExternalReference::handle_scope_next_address();
1205 push(Operand::StaticVariable(next_address));
1206 ExternalReference limit_address =
1207 ExternalReference::handle_scope_limit_address();
1208 push(Operand::StaticVariable(limit_address));
1209}
1210
1211
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001212Object* MacroAssembler::PopHandleScopeHelper(Register saved,
1213 Register scratch,
1214 bool gc_allowed) {
1215 Object* result = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001216 ExternalReference extensions_address =
1217 ExternalReference::handle_scope_extensions_address();
1218 Label write_back;
1219 mov(scratch, Operand::StaticVariable(extensions_address));
1220 cmp(Operand(scratch), Immediate(0));
1221 j(equal, &write_back);
1222 // Calling a runtime function messes with registers so we save and
1223 // restore any one we're asked not to change
1224 if (saved.is_valid()) push(saved);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001225 if (gc_allowed) {
1226 CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1227 } else {
1228 result = TryCallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1229 if (result->IsFailure()) return result;
1230 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001231 if (saved.is_valid()) pop(saved);
1232
1233 bind(&write_back);
1234 ExternalReference limit_address =
1235 ExternalReference::handle_scope_limit_address();
1236 pop(Operand::StaticVariable(limit_address));
1237 ExternalReference next_address =
1238 ExternalReference::handle_scope_next_address();
1239 pop(Operand::StaticVariable(next_address));
1240 pop(scratch);
1241 shr(scratch, kSmiTagSize);
1242 mov(Operand::StaticVariable(extensions_address), scratch);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001243
1244 return result;
1245}
1246
1247
1248void MacroAssembler::PopHandleScope(Register saved, Register scratch) {
1249 PopHandleScopeHelper(saved, scratch, true);
1250}
1251
1252
1253Object* MacroAssembler::TryPopHandleScope(Register saved, Register scratch) {
1254 return PopHandleScopeHelper(saved, scratch, false);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001255}
1256
1257
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001258void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001260 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001261 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001262 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263}
1264
1265
1266void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1267 const ParameterCount& actual,
1268 Handle<Code> code_constant,
1269 const Operand& code_operand,
1270 Label* done,
1271 InvokeFlag flag) {
1272 bool definitely_matches = false;
1273 Label invoke;
1274 if (expected.is_immediate()) {
1275 ASSERT(actual.is_immediate());
1276 if (expected.immediate() == actual.immediate()) {
1277 definitely_matches = true;
1278 } else {
1279 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001280 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1281 if (expected.immediate() == sentinel) {
1282 // Don't worry about adapting arguments for builtins that
1283 // don't want that done. Skip adaption code by making it look
1284 // like we have a match between expected and actual number of
1285 // arguments.
1286 definitely_matches = true;
1287 } else {
1288 mov(ebx, expected.immediate());
1289 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 }
1291 } else {
1292 if (actual.is_immediate()) {
1293 // Expected is in register, actual is immediate. This is the
1294 // case when we invoke function values without going through the
1295 // IC mechanism.
1296 cmp(expected.reg(), actual.immediate());
1297 j(equal, &invoke);
1298 ASSERT(expected.reg().is(ebx));
1299 mov(eax, actual.immediate());
1300 } else if (!expected.reg().is(actual.reg())) {
1301 // Both expected and actual are in (different) registers. This
1302 // is the case when we invoke functions using call and apply.
1303 cmp(expected.reg(), Operand(actual.reg()));
1304 j(equal, &invoke);
1305 ASSERT(actual.reg().is(eax));
1306 ASSERT(expected.reg().is(ebx));
1307 }
1308 }
1309
1310 if (!definitely_matches) {
1311 Handle<Code> adaptor =
1312 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1313 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001314 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1316 } else if (!code_operand.is_reg(edx)) {
1317 mov(edx, code_operand);
1318 }
1319
1320 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001321 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 jmp(done);
1323 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001324 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 }
1326 bind(&invoke);
1327 }
1328}
1329
1330
1331void MacroAssembler::InvokeCode(const Operand& code,
1332 const ParameterCount& expected,
1333 const ParameterCount& actual,
1334 InvokeFlag flag) {
1335 Label done;
1336 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1337 if (flag == CALL_FUNCTION) {
1338 call(code);
1339 } else {
1340 ASSERT(flag == JUMP_FUNCTION);
1341 jmp(code);
1342 }
1343 bind(&done);
1344}
1345
1346
1347void MacroAssembler::InvokeCode(Handle<Code> code,
1348 const ParameterCount& expected,
1349 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001350 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 InvokeFlag flag) {
1352 Label done;
1353 Operand dummy(eax);
1354 InvokePrologue(expected, actual, code, dummy, &done, flag);
1355 if (flag == CALL_FUNCTION) {
1356 call(code, rmode);
1357 } else {
1358 ASSERT(flag == JUMP_FUNCTION);
1359 jmp(code, rmode);
1360 }
1361 bind(&done);
1362}
1363
1364
1365void MacroAssembler::InvokeFunction(Register fun,
1366 const ParameterCount& actual,
1367 InvokeFlag flag) {
1368 ASSERT(fun.is(edi));
1369 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1370 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1371 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1372 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1373 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1374
1375 ParameterCount expected(ebx);
1376 InvokeCode(Operand(edx), expected, actual, flag);
1377}
1378
1379
ager@chromium.org5c838252010-02-19 08:53:10 +00001380void MacroAssembler::InvokeFunction(JSFunction* function,
1381 const ParameterCount& actual,
1382 InvokeFlag flag) {
1383 ASSERT(function->is_compiled());
1384 // Get the function and setup the context.
1385 mov(edi, Immediate(Handle<JSFunction>(function)));
1386 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387
ager@chromium.org5c838252010-02-19 08:53:10 +00001388 // Invoke the cached code.
1389 Handle<Code> code(function->code());
1390 ParameterCount expected(function->shared()->formal_parameter_count());
1391 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1392}
1393
1394
1395void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001396 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001397 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398
1399 // Rely on the assertion to check that the number of provided
1400 // arguments match the expected number of arguments. Fake a
1401 // parameter count to avoid emitting code to do the check.
1402 ParameterCount expected(0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001403 GetBuiltinEntry(edx, id);
1404 InvokeCode(Operand(edx), expected, expected, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405}
1406
1407
1408void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001409 // Load the JavaScript builtin function from the builtins object.
1410 mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1411 mov(edi, FieldOperand(edi, GlobalObject::kBuiltinsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 int builtins_offset =
1413 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00001414 mov(edi, FieldOperand(edi, builtins_offset));
1415 // Load the code entry point from the function into the target register.
1416 mov(target, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1417 mov(target, FieldOperand(target, SharedFunctionInfo::kCodeOffset));
1418 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419}
1420
1421
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001422void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1423 if (context_chain_length > 0) {
1424 // Move up the chain of contexts to the context containing the slot.
1425 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1426 // Load the function context (which is the incoming, outer context).
1427 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1428 for (int i = 1; i < context_chain_length; i++) {
1429 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1430 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1431 }
1432 // The context may be an intermediate context, not a function context.
1433 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1434 } else { // Slot is in the current function context.
1435 // The context may be an intermediate context, not a function context.
1436 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1437 }
1438}
1439
1440
1441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442void MacroAssembler::Ret() {
1443 ret(0);
1444}
1445
1446
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001447void MacroAssembler::Drop(int stack_elements) {
1448 if (stack_elements > 0) {
1449 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1450 }
1451}
1452
1453
1454void MacroAssembler::Move(Register dst, Handle<Object> value) {
1455 mov(dst, value);
1456}
1457
1458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1460 if (FLAG_native_code_counters && counter->Enabled()) {
1461 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1462 }
1463}
1464
1465
1466void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1467 ASSERT(value > 0);
1468 if (FLAG_native_code_counters && counter->Enabled()) {
1469 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1470 if (value == 1) {
1471 inc(operand);
1472 } else {
1473 add(operand, Immediate(value));
1474 }
1475 }
1476}
1477
1478
1479void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1480 ASSERT(value > 0);
1481 if (FLAG_native_code_counters && counter->Enabled()) {
1482 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1483 if (value == 1) {
1484 dec(operand);
1485 } else {
1486 sub(operand, Immediate(value));
1487 }
1488 }
1489}
1490
1491
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492void MacroAssembler::IncrementCounter(Condition cc,
1493 StatsCounter* counter,
1494 int value) {
1495 ASSERT(value > 0);
1496 if (FLAG_native_code_counters && counter->Enabled()) {
1497 Label skip;
1498 j(NegateCondition(cc), &skip);
1499 pushfd();
1500 IncrementCounter(counter, value);
1501 popfd();
1502 bind(&skip);
1503 }
1504}
1505
1506
1507void MacroAssembler::DecrementCounter(Condition cc,
1508 StatsCounter* counter,
1509 int value) {
1510 ASSERT(value > 0);
1511 if (FLAG_native_code_counters && counter->Enabled()) {
1512 Label skip;
1513 j(NegateCondition(cc), &skip);
1514 pushfd();
1515 DecrementCounter(counter, value);
1516 popfd();
1517 bind(&skip);
1518 }
1519}
1520
1521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522void MacroAssembler::Assert(Condition cc, const char* msg) {
1523 if (FLAG_debug_code) Check(cc, msg);
1524}
1525
1526
1527void MacroAssembler::Check(Condition cc, const char* msg) {
1528 Label L;
1529 j(cc, &L, taken);
1530 Abort(msg);
1531 // will not return here
1532 bind(&L);
1533}
1534
1535
1536void MacroAssembler::Abort(const char* msg) {
1537 // We want to pass the msg string like a smi to avoid GC
1538 // problems, however msg is not guaranteed to be aligned
1539 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001540 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 // from the real pointer as a smi.
1542 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1543 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1544 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1545#ifdef DEBUG
1546 if (msg != NULL) {
1547 RecordComment("Abort message: ");
1548 RecordComment(msg);
1549 }
1550#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001551 // Disable stub call restrictions to always allow calls to abort.
1552 set_allow_stub_calls(true);
1553
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 push(eax);
1555 push(Immediate(p0));
1556 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1557 CallRuntime(Runtime::kAbort, 2);
1558 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001559 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560}
1561
1562
ager@chromium.org5c838252010-02-19 08:53:10 +00001563void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1564 Register instance_type,
1565 Register scratch,
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001566 Label* failure) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001567 if (!scratch.is(instance_type)) {
1568 mov(scratch, instance_type);
1569 }
1570 and_(scratch,
1571 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1572 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1573 j(not_equal, failure);
1574}
1575
1576
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001577void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1578 Register object2,
1579 Register scratch1,
1580 Register scratch2,
1581 Label* failure) {
1582 // Check that both objects are not smis.
1583 ASSERT_EQ(0, kSmiTag);
1584 mov(scratch1, Operand(object1));
1585 and_(scratch1, Operand(object2));
1586 test(scratch1, Immediate(kSmiTagMask));
1587 j(zero, failure);
1588
1589 // Load instance type for both strings.
1590 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1591 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1592 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1593 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1594
1595 // Check that both are flat ascii strings.
1596 const int kFlatAsciiStringMask =
1597 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1598 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1599 // Interleave bits from both instance types and compare them in one check.
1600 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1601 and_(scratch1, kFlatAsciiStringMask);
1602 and_(scratch2, kFlatAsciiStringMask);
1603 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1604 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1605 j(not_equal, failure);
1606}
1607
1608
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001609void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1610 int frameAlignment = OS::ActivationFrameAlignment();
1611 if (frameAlignment != 0) {
1612 // Make stack end at alignment and make room for num_arguments words
1613 // and the original value of esp.
1614 mov(scratch, esp);
1615 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1616 ASSERT(IsPowerOf2(frameAlignment));
1617 and_(esp, -frameAlignment);
1618 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1619 } else {
1620 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1621 }
1622}
1623
1624
1625void MacroAssembler::CallCFunction(ExternalReference function,
1626 int num_arguments) {
1627 // Trashing eax is ok as it will be the return value.
1628 mov(Operand(eax), Immediate(function));
1629 CallCFunction(eax, num_arguments);
1630}
1631
1632
1633void MacroAssembler::CallCFunction(Register function,
1634 int num_arguments) {
1635 call(Operand(function));
1636 if (OS::ActivationFrameAlignment() != 0) {
1637 mov(esp, Operand(esp, num_arguments * kPointerSize));
1638 } else {
1639 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1640 }
1641}
1642
1643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001645 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001646 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647 // The size is adjusted with kGap on order for the assembler to generate size
1648 // bytes of instructions without failing with buffer size constraints.
1649 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1650}
1651
1652
1653CodePatcher::~CodePatcher() {
1654 // Indicate that code has changed.
1655 CPU::FlushICache(address_, size_);
1656
1657 // Check that the code was patched as expected.
1658 ASSERT(masm_.pc_ == address_ + size_);
1659 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1660}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661
1662
1663} } // namespace v8::internal