blob: 45e24fa73705681645472aff19e7c9d181791c01 [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
50static void RecordWriteHelper(MacroAssembler* masm,
51 Register object,
52 Register addr,
53 Register scratch) {
54 Label fast;
55
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000056 // Compute the page start address from the heap object pointer, and reuse
57 // the 'object' register for it.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 masm->and_(object, ~Page::kPageAlignmentMask);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000059 Register page_start = object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000061 // Compute the bit addr in the remembered set/index of the pointer in the
62 // page. Reuse 'addr' as pointer_offset.
63 masm->sub(addr, Operand(page_start));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064 masm->shr(addr, kObjectAlignmentBits);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000065 Register pointer_offset = addr;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066
67 // If the bit offset lies beyond the normal remembered set range, it is in
68 // the extra remembered set area of a large object.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000069 masm->cmp(pointer_offset, Page::kPageSize / kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070 masm->j(less, &fast);
71
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000072 // Adjust 'page_start' so that addressing using 'pointer_offset' hits the
73 // extra remembered set after the large object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000075 // Find the length of the large object (FixedArray).
76 masm->mov(scratch, Operand(page_start, Page::kObjectStartOffset
77 + FixedArray::kLengthOffset));
78 Register array_length = scratch;
79
80 // Extra remembered set starts right after the large object (a FixedArray), at
81 // page_start + kObjectStartOffset + objectSize
82 // where objectSize is FixedArray::kHeaderSize + kPointerSize * array_length.
83 // Add the delta between the end of the normal RSet and the start of the
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000084 // extra RSet to 'page_start', so that addressing the bit using
85 // 'pointer_offset' hits the extra RSet words.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000086 masm->lea(page_start,
87 Operand(page_start, array_length, times_pointer_size,
88 Page::kObjectStartOffset + FixedArray::kHeaderSize
89 - Page::kRSetEndOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090
91 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
92 // to limit code size. We should probably evaluate this decision by
93 // measuring the performance of an equivalent implementation using
94 // "simpler" instructions
95 masm->bind(&fast);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000096 masm->bts(Operand(page_start, Page::kRSetOffset), pointer_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097}
98
99
100class RecordWriteStub : public CodeStub {
101 public:
102 RecordWriteStub(Register object, Register addr, Register scratch)
103 : object_(object), addr_(addr), scratch_(scratch) { }
104
105 void Generate(MacroAssembler* masm);
106
107 private:
108 Register object_;
109 Register addr_;
110 Register scratch_;
111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112#ifdef DEBUG
113 void Print() {
114 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
115 object_.code(), addr_.code(), scratch_.code());
116 }
117#endif
118
119 // Minor key encoding in 12 bits of three registers (object, address and
120 // scratch) OOOOAAAASSSS.
121 class ScratchBits: public BitField<uint32_t, 0, 4> {};
122 class AddressBits: public BitField<uint32_t, 4, 4> {};
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000123 class ObjectBits: public BitField<uint32_t, 8, 4> {};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
125 Major MajorKey() { return RecordWrite; }
126
127 int MinorKey() {
128 // Encode the registers.
129 return ObjectBits::encode(object_.code()) |
130 AddressBits::encode(addr_.code()) |
131 ScratchBits::encode(scratch_.code());
132 }
133};
134
135
136void RecordWriteStub::Generate(MacroAssembler* masm) {
137 RecordWriteHelper(masm, object_, addr_, scratch_);
138 masm->ret(0);
139}
140
141
142// Set the remembered set bit for [object+offset].
143// object is the object being stored into, value is the object being stored.
144// If offset is zero, then the scratch register contains the array index into
145// the elements array represented as a Smi.
146// All registers are clobbered by the operation.
147void MacroAssembler::RecordWrite(Register object, int offset,
148 Register value, Register scratch) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000149 // The compiled code assumes that record write doesn't change the
150 // context register, so we check that none of the clobbered
151 // registers are esi.
152 ASSERT(!object.is(esi) && !value.is(esi) && !scratch.is(esi));
153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 // First, check if a remembered set write is even needed. The tests below
155 // catch stores of Smis and stores into young gen (which does not have space
156 // for the remembered set bits.
157 Label done;
158
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000159 // Skip barrier if writing a smi.
160 ASSERT_EQ(0, kSmiTag);
161 test(value, Immediate(kSmiTagMask));
162 j(zero, &done);
163
164 if (Serializer::enabled()) {
165 // Can't do arithmetic on external references if it might get serialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 mov(value, Operand(object));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000167 // The mask isn't really an address. We load it as an external reference in
168 // case the size of the new space is different between the snapshot maker
169 // and the running system.
170 and_(Operand(value), Immediate(ExternalReference::new_space_mask()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 cmp(Operand(value), Immediate(ExternalReference::new_space_start()));
172 j(equal, &done);
173 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000174 int32_t new_space_start = reinterpret_cast<int32_t>(
175 ExternalReference::new_space_start().address());
176 lea(value, Operand(object, -new_space_start));
177 and_(value, Heap::NewSpaceMask());
178 j(equal, &done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 }
180
181 if ((offset > 0) && (offset < Page::kMaxHeapObjectSize)) {
182 // Compute the bit offset in the remembered set, leave it in 'value'.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000183 lea(value, Operand(object, offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 and_(value, Page::kPageAlignmentMask);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000185 shr(value, kPointerSizeLog2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186
187 // Compute the page address from the heap object pointer, leave it in
188 // 'object'.
189 and_(object, ~Page::kPageAlignmentMask);
190
191 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
192 // to limit code size. We should probably evaluate this decision by
193 // measuring the performance of an equivalent implementation using
194 // "simpler" instructions
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000195 bts(Operand(object, Page::kRSetOffset), value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 } else {
197 Register dst = scratch;
198 if (offset != 0) {
199 lea(dst, Operand(object, offset));
200 } else {
201 // array access: calculate the destination address in the same manner as
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000202 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
203 // into an array of words.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000204 ASSERT_EQ(1, kSmiTagSize);
205 ASSERT_EQ(0, kSmiTag);
206 lea(dst, Operand(object, dst, times_half_pointer_size,
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000207 FixedArray::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 }
209 // If we are already generating a shared stub, not inlining the
210 // record write code isn't going to save us any memory.
211 if (generating_stub()) {
212 RecordWriteHelper(this, object, dst, value);
213 } else {
214 RecordWriteStub stub(object, dst, value);
215 CallStub(&stub);
216 }
217 }
218
219 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000220
221 // Clobber all input registers when running with the debug-code flag
222 // turned on to provoke errors.
223 if (FLAG_debug_code) {
224 mov(object, Immediate(bit_cast<int32_t>(kZapValue)));
225 mov(value, Immediate(bit_cast<int32_t>(kZapValue)));
226 mov(scratch, Immediate(bit_cast<int32_t>(kZapValue)));
227 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228}
229
230
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000231void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
232 cmp(esp,
233 Operand::StaticVariable(ExternalReference::address_of_stack_limit()));
234 j(below, on_stack_overflow);
235}
236
237
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000238#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239void MacroAssembler::SaveRegistersToMemory(RegList regs) {
240 ASSERT((regs & ~kJSCallerSaved) == 0);
241 // Copy the content of registers to memory location.
242 for (int i = 0; i < kNumJSCallerSaved; i++) {
243 int r = JSCallerSavedCode(i);
244 if ((regs & (1 << r)) != 0) {
245 Register reg = { r };
246 ExternalReference reg_addr =
247 ExternalReference(Debug_Address::Register(i));
248 mov(Operand::StaticVariable(reg_addr), reg);
249 }
250 }
251}
252
253
254void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
255 ASSERT((regs & ~kJSCallerSaved) == 0);
256 // Copy the content of memory location to registers.
257 for (int i = kNumJSCallerSaved; --i >= 0;) {
258 int r = JSCallerSavedCode(i);
259 if ((regs & (1 << r)) != 0) {
260 Register reg = { r };
261 ExternalReference reg_addr =
262 ExternalReference(Debug_Address::Register(i));
263 mov(reg, Operand::StaticVariable(reg_addr));
264 }
265 }
266}
267
268
269void MacroAssembler::PushRegistersFromMemory(RegList regs) {
270 ASSERT((regs & ~kJSCallerSaved) == 0);
271 // Push the content of the memory location to the stack.
272 for (int i = 0; i < kNumJSCallerSaved; i++) {
273 int r = JSCallerSavedCode(i);
274 if ((regs & (1 << r)) != 0) {
275 ExternalReference reg_addr =
276 ExternalReference(Debug_Address::Register(i));
277 push(Operand::StaticVariable(reg_addr));
278 }
279 }
280}
281
282
283void MacroAssembler::PopRegistersToMemory(RegList regs) {
284 ASSERT((regs & ~kJSCallerSaved) == 0);
285 // Pop the content from the stack to the memory location.
286 for (int i = kNumJSCallerSaved; --i >= 0;) {
287 int r = JSCallerSavedCode(i);
288 if ((regs & (1 << r)) != 0) {
289 ExternalReference reg_addr =
290 ExternalReference(Debug_Address::Register(i));
291 pop(Operand::StaticVariable(reg_addr));
292 }
293 }
294}
295
296
297void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
298 Register scratch,
299 RegList regs) {
300 ASSERT((regs & ~kJSCallerSaved) == 0);
301 // Copy the content of the stack to the memory location and adjust base.
302 for (int i = kNumJSCallerSaved; --i >= 0;) {
303 int r = JSCallerSavedCode(i);
304 if ((regs & (1 << r)) != 0) {
305 mov(scratch, Operand(base, 0));
306 ExternalReference reg_addr =
307 ExternalReference(Debug_Address::Register(i));
308 mov(Operand::StaticVariable(reg_addr), scratch);
309 lea(base, Operand(base, kPointerSize));
310 }
311 }
312}
ager@chromium.org5c838252010-02-19 08:53:10 +0000313
314void MacroAssembler::DebugBreak() {
315 Set(eax, Immediate(0));
316 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak)));
317 CEntryStub ces(1);
318 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
319}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000320#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321
322void MacroAssembler::Set(Register dst, const Immediate& x) {
323 if (x.is_zero()) {
324 xor_(dst, Operand(dst)); // shorter than mov
325 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000326 mov(dst, x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 }
328}
329
330
331void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
332 mov(dst, x);
333}
334
335
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336void MacroAssembler::CmpObjectType(Register heap_object,
337 InstanceType type,
338 Register map) {
339 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
340 CmpInstanceType(map, type);
341}
342
343
344void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
345 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
346 static_cast<int8_t>(type));
347}
348
349
ager@chromium.org5c838252010-02-19 08:53:10 +0000350void MacroAssembler::CheckMap(Register obj,
351 Handle<Map> map,
352 Label* fail,
353 bool is_heap_object) {
354 if (!is_heap_object) {
355 test(obj, Immediate(kSmiTagMask));
356 j(zero, fail);
357 }
358 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
359 j(not_equal, fail);
360}
361
362
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000363Condition MacroAssembler::IsObjectStringType(Register heap_object,
364 Register map,
365 Register instance_type) {
366 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
367 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
368 ASSERT(kNotStringTag != 0);
369 test(instance_type, Immediate(kIsNotStringMask));
370 return zero;
371}
372
373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374void MacroAssembler::FCmp() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000375 if (CpuFeatures::IsSupported(CMOV)) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000376 fucomip();
377 ffree(0);
378 fincstp();
379 } else {
380 fucompp();
381 push(eax);
382 fnstsw_ax();
383 sahf();
384 pop(eax);
385 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386}
387
388
ager@chromium.org5c838252010-02-19 08:53:10 +0000389void MacroAssembler::AbortIfNotNumber(Register object, const char* msg) {
390 Label ok;
391 test(object, Immediate(kSmiTagMask));
392 j(zero, &ok);
393 cmp(FieldOperand(object, HeapObject::kMapOffset),
394 Factory::heap_number_map());
395 Assert(equal, msg);
396 bind(&ok);
397}
398
399
ager@chromium.org7c537e22008-10-16 08:43:32 +0000400void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 push(ebp);
402 mov(ebp, Operand(esp));
403 push(esi);
404 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000405 push(Immediate(CodeObject()));
406 if (FLAG_debug_code) {
407 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
408 Check(not_equal, "code object not properly patched");
409 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410}
411
412
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414 if (FLAG_debug_code) {
415 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
416 Immediate(Smi::FromInt(type)));
417 Check(equal, "stack frame types must match");
418 }
419 leave();
420}
421
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000422void MacroAssembler::EnterExitFramePrologue(ExitFrame::Mode mode) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000423 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000424 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000425 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
426 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
427 push(ebp);
428 mov(ebp, Operand(esp));
429
430 // Reserve room for entry stack pointer and push the debug marker.
431 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +0000432 push(Immediate(0)); // Saved entry sp, patched before call.
433 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000434
435 // Save the frame pointer and the context in top.
436 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
437 ExternalReference context_address(Top::k_context_address);
438 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
439 mov(Operand::StaticVariable(context_address), esi);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000440}
ager@chromium.org236ad962008-09-25 09:45:57 +0000441
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000442void MacroAssembler::EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000443#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000444 // Save the state of all registers to the stack from the memory
445 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000446 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000447 // TODO(1243899): This should be symmetric to
448 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
449 // correct here, but computed for the other call. Very error
450 // prone! FIX THIS. Actually there are deeper problems with
451 // register saving than this asymmetry (see the bug report
452 // associated with this issue).
453 PushRegistersFromMemory(kJSCallerSaved);
454 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000455#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000456
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000457 // Reserve space for arguments.
458 sub(Operand(esp), Immediate(argc * kPointerSize));
ager@chromium.org236ad962008-09-25 09:45:57 +0000459
460 // Get the required frame alignment for the OS.
461 static const int kFrameAlignment = OS::ActivationFrameAlignment();
462 if (kFrameAlignment > 0) {
463 ASSERT(IsPowerOf2(kFrameAlignment));
464 and_(esp, -kFrameAlignment);
465 }
466
467 // Patch the saved entry sp.
468 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
469}
470
471
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000472void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
473 EnterExitFramePrologue(mode);
474
475 // Setup argc and argv in callee-saved registers.
476 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
477 mov(edi, Operand(eax));
478 lea(esi, Operand(ebp, eax, times_4, offset));
479
480 EnterExitFrameEpilogue(mode, 2);
481}
482
483
484void MacroAssembler::EnterApiExitFrame(ExitFrame::Mode mode,
485 int stack_space,
486 int argc) {
487 EnterExitFramePrologue(mode);
488
489 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
490 lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset));
491
492 EnterExitFrameEpilogue(mode, argc);
493}
494
495
496void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000497#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000498 // Restore the memory copy of the registers by digging them out from
499 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000500 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000501 // It's okay to clobber register ebx below because we don't need
502 // the function pointer after this.
503 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000504 int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000505 lea(ebx, Operand(ebp, kOffset));
506 CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved);
507 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000508#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000509
510 // Get the return address from the stack and restore the frame pointer.
511 mov(ecx, Operand(ebp, 1 * kPointerSize));
512 mov(ebp, Operand(ebp, 0 * kPointerSize));
513
514 // Pop the arguments and the receiver from the caller stack.
515 lea(esp, Operand(esi, 1 * kPointerSize));
516
517 // Restore current context from top and clear it in debug mode.
518 ExternalReference context_address(Top::k_context_address);
519 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000520#ifdef DEBUG
521 mov(Operand::StaticVariable(context_address), Immediate(0));
522#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000523
524 // Push the return address to get ready to return.
525 push(ecx);
526
527 // Clear the top frame.
528 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
529 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
530}
531
532
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533void MacroAssembler::PushTryHandler(CodeLocation try_location,
534 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000535 // Adjust this code if not the case.
536 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 // The pc (return address) is already on TOS.
538 if (try_location == IN_JAVASCRIPT) {
539 if (type == TRY_CATCH_HANDLER) {
540 push(Immediate(StackHandler::TRY_CATCH));
541 } else {
542 push(Immediate(StackHandler::TRY_FINALLY));
543 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 } else {
546 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000547 // The frame pointer does not point to a JS frame so we save NULL
548 // for ebp. We expect the code throwing an exception to check ebp
549 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000551 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000553 // Save the current handler as the next handler.
554 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
555 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
557}
558
559
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000560void MacroAssembler::PopTryHandler() {
561 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
562 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
563 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
564}
565
566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
568 JSObject* holder, Register holder_reg,
569 Register scratch,
ager@chromium.org5c838252010-02-19 08:53:10 +0000570 int save_at_depth,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 Label* miss) {
572 // Make sure there's no overlap between scratch and the other
573 // registers.
574 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
575
576 // Keep track of the current object in register reg.
577 Register reg = object_reg;
ager@chromium.org5c838252010-02-19 08:53:10 +0000578 int depth = 0;
579
580 if (save_at_depth == depth) {
581 mov(Operand(esp, kPointerSize), object_reg);
582 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583
584 // Check the maps in the prototype chain.
585 // Traverse the prototype chain from the object and do map checks.
586 while (object != holder) {
587 depth++;
588
589 // Only global objects and objects that do not require access
590 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000591 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592
593 JSObject* prototype = JSObject::cast(object->GetPrototype());
594 if (Heap::InNewSpace(prototype)) {
595 // Get the map of the current object.
596 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
597 cmp(Operand(scratch), Immediate(Handle<Map>(object->map())));
598 // Branch on the result of the map check.
599 j(not_equal, miss, not_taken);
600 // Check access rights to the global object. This has to happen
601 // after the map check so that we know that the object is
602 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000603 if (object->IsJSGlobalProxy()) {
604 CheckAccessGlobalProxy(reg, scratch, miss);
605
606 // Restore scratch register to be the map of the object.
607 // We load the prototype from the map in the scratch register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
609 }
610 // The prototype is in new space; we cannot store a reference
611 // to it in the code. Load it from the map.
612 reg = holder_reg; // from now the object is in holder_reg
613 mov(reg, FieldOperand(scratch, Map::kPrototypeOffset));
614 } else {
615 // Check the map of the current object.
616 cmp(FieldOperand(reg, HeapObject::kMapOffset),
617 Immediate(Handle<Map>(object->map())));
618 // Branch on the result of the map check.
619 j(not_equal, miss, not_taken);
620 // Check access rights to the global object. This has to happen
621 // after the map check so that we know that the object is
622 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000623 if (object->IsJSGlobalProxy()) {
624 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625 }
626 // The prototype is in old space; load it directly.
627 reg = holder_reg; // from now the object is in holder_reg
628 mov(reg, Handle<JSObject>(prototype));
629 }
630
ager@chromium.org5c838252010-02-19 08:53:10 +0000631 if (save_at_depth == depth) {
632 mov(Operand(esp, kPointerSize), reg);
633 }
634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 // Go to the next object in the prototype chain.
636 object = prototype;
637 }
638
639 // Check the holder map.
640 cmp(FieldOperand(reg, HeapObject::kMapOffset),
641 Immediate(Handle<Map>(holder->map())));
642 j(not_equal, miss, not_taken);
643
644 // Log the check depth.
ager@chromium.org5c838252010-02-19 08:53:10 +0000645 LOG(IntEvent("check-maps-depth", depth + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
647 // Perform security check for access to the global object and return
648 // the holder register.
649 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000650 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
651 if (object->IsJSGlobalProxy()) {
652 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 }
654 return reg;
655}
656
657
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000658void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000659 Register scratch,
660 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000661 Label same_contexts;
662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 ASSERT(!holder_reg.is(scratch));
664
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000665 // Load current lexical context from the stack frame.
666 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
667
668 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 if (FLAG_debug_code) {
670 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000671 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000673 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
675 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000676 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
677
678 // Check the context is a global context.
679 if (FLAG_debug_code) {
680 push(scratch);
681 // Read the first word and compare to global_context_map.
682 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
683 cmp(scratch, Factory::global_context_map());
684 Check(equal, "JSGlobalObject::global_context should be a global context.");
685 pop(scratch);
686 }
687
688 // Check if both contexts are the same.
689 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
690 j(equal, &same_contexts, taken);
691
692 // Compare security tokens, save holder_reg on the stack so we can use it
693 // as a temporary register.
694 //
695 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
696 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 // Check that the security token in the calling global object is
698 // compatible with the security token in the receiving global
699 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000700 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
701
702 // Check the context is a global context.
703 if (FLAG_debug_code) {
704 cmp(holder_reg, Factory::null_value());
705 Check(not_equal, "JSGlobalProxy::context() should not be null.");
706
707 push(holder_reg);
708 // Read the first word and compare to global_context_map(),
709 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
710 cmp(holder_reg, Factory::global_context_map());
711 Check(equal, "JSGlobalObject::global_context should be a global context.");
712 pop(holder_reg);
713 }
714
715 int token_offset = Context::kHeaderSize +
716 Context::SECURITY_TOKEN_INDEX * kPointerSize;
717 mov(scratch, FieldOperand(scratch, token_offset));
718 cmp(scratch, FieldOperand(holder_reg, token_offset));
719 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000721
722 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723}
724
725
ager@chromium.orga1645e22009-09-09 19:27:10 +0000726void MacroAssembler::LoadAllocationTopHelper(Register result,
727 Register result_end,
728 Register scratch,
729 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000730 ExternalReference new_space_allocation_top =
731 ExternalReference::new_space_allocation_top_address();
732
733 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000734 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000735 // No use of scratch if allocation top is provided.
736 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000737#ifdef DEBUG
738 // Assert that result actually contains top on entry.
739 cmp(result, Operand::StaticVariable(new_space_allocation_top));
740 Check(equal, "Unexpected allocation top");
741#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000742 return;
743 }
744
745 // Move address of new object to result. Use scratch register if available.
746 if (scratch.is(no_reg)) {
747 mov(result, Operand::StaticVariable(new_space_allocation_top));
748 } else {
749 ASSERT(!scratch.is(result_end));
750 mov(Operand(scratch), Immediate(new_space_allocation_top));
751 mov(result, Operand(scratch, 0));
752 }
753}
754
755
756void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
757 Register scratch) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000758 if (FLAG_debug_code) {
759 test(result_end, Immediate(kObjectAlignmentMask));
760 Check(zero, "Unaligned allocation in new space");
761 }
762
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000763 ExternalReference new_space_allocation_top =
764 ExternalReference::new_space_allocation_top_address();
765
766 // Update new top. Use scratch if available.
767 if (scratch.is(no_reg)) {
768 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
769 } else {
770 mov(Operand(scratch, 0), result_end);
771 }
772}
773
ager@chromium.orga1645e22009-09-09 19:27:10 +0000774
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000775void MacroAssembler::AllocateInNewSpace(int object_size,
776 Register result,
777 Register result_end,
778 Register scratch,
779 Label* gc_required,
780 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000781 ASSERT(!result.is(result_end));
782
783 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000784 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000785
786 // Calculate new top and bail out if new space is exhausted.
787 ExternalReference new_space_allocation_limit =
788 ExternalReference::new_space_allocation_limit_address();
789 lea(result_end, Operand(result, object_size));
790 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
791 j(above, gc_required, not_taken);
792
ager@chromium.orga1645e22009-09-09 19:27:10 +0000793 // Tag result if requested.
794 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000795 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000796 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000797
798 // Update allocation top.
799 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000800}
801
802
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000803void MacroAssembler::AllocateInNewSpace(int header_size,
804 ScaleFactor element_size,
805 Register element_count,
806 Register result,
807 Register result_end,
808 Register scratch,
809 Label* gc_required,
810 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000811 ASSERT(!result.is(result_end));
812
813 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000814 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000815
816 // Calculate new top and bail out if new space is exhausted.
817 ExternalReference new_space_allocation_limit =
818 ExternalReference::new_space_allocation_limit_address();
819 lea(result_end, Operand(result, element_count, element_size, header_size));
820 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
821 j(above, gc_required);
822
ager@chromium.orga1645e22009-09-09 19:27:10 +0000823 // Tag result if requested.
824 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000825 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000826 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000827
828 // Update allocation top.
829 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000830}
831
832
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000833void MacroAssembler::AllocateInNewSpace(Register object_size,
834 Register result,
835 Register result_end,
836 Register scratch,
837 Label* gc_required,
838 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000839 ASSERT(!result.is(result_end));
840
841 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000842 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000843
844 // Calculate new top and bail out if new space is exhausted.
845 ExternalReference new_space_allocation_limit =
846 ExternalReference::new_space_allocation_limit_address();
847 if (!object_size.is(result_end)) {
848 mov(result_end, object_size);
849 }
850 add(result_end, Operand(result));
851 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
852 j(above, gc_required, not_taken);
853
ager@chromium.orga1645e22009-09-09 19:27:10 +0000854 // Tag result if requested.
855 if ((flags & TAG_OBJECT) != 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000856 lea(result, Operand(result, kHeapObjectTag));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000857 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000858
859 // Update allocation top.
860 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000861}
862
863
864void MacroAssembler::UndoAllocationInNewSpace(Register object) {
865 ExternalReference new_space_allocation_top =
866 ExternalReference::new_space_allocation_top_address();
867
868 // Make sure the object has no tag before resetting top.
869 and_(Operand(object), Immediate(~kHeapObjectTagMask));
870#ifdef DEBUG
871 cmp(object, Operand::StaticVariable(new_space_allocation_top));
872 Check(below, "Undo allocation of non allocated memory");
873#endif
874 mov(Operand::StaticVariable(new_space_allocation_top), object);
875}
876
877
ager@chromium.org3811b432009-10-28 14:53:37 +0000878void MacroAssembler::AllocateHeapNumber(Register result,
879 Register scratch1,
880 Register scratch2,
881 Label* gc_required) {
882 // Allocate heap number in new space.
883 AllocateInNewSpace(HeapNumber::kSize,
884 result,
885 scratch1,
886 scratch2,
887 gc_required,
888 TAG_OBJECT);
889
890 // Set the map.
891 mov(FieldOperand(result, HeapObject::kMapOffset),
892 Immediate(Factory::heap_number_map()));
893}
894
895
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000896void MacroAssembler::AllocateTwoByteString(Register result,
897 Register length,
898 Register scratch1,
899 Register scratch2,
900 Register scratch3,
901 Label* gc_required) {
902 // Calculate the number of bytes needed for the characters in the string while
903 // observing object alignment.
904 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000905 ASSERT(kShortSize == 2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000906 // scratch1 = length * 2 + kObjectAlignmentMask.
907 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000908 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
909
910 // Allocate two byte string in new space.
911 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
912 times_1,
913 scratch1,
914 result,
915 scratch2,
916 scratch3,
917 gc_required,
918 TAG_OBJECT);
919
920 // Set the map, length and hash field.
921 mov(FieldOperand(result, HeapObject::kMapOffset),
922 Immediate(Factory::string_map()));
923 mov(FieldOperand(result, String::kLengthOffset), length);
924 mov(FieldOperand(result, String::kHashFieldOffset),
925 Immediate(String::kEmptyHashField));
926}
927
928
929void MacroAssembler::AllocateAsciiString(Register result,
930 Register length,
931 Register scratch1,
932 Register scratch2,
933 Register scratch3,
934 Label* gc_required) {
935 // Calculate the number of bytes needed for the characters in the string while
936 // observing object alignment.
937 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
938 mov(scratch1, length);
939 ASSERT(kCharSize == 1);
940 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
941 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
942
943 // Allocate ascii string in new space.
944 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
945 times_1,
946 scratch1,
947 result,
948 scratch2,
949 scratch3,
950 gc_required,
951 TAG_OBJECT);
952
953 // Set the map, length and hash field.
954 mov(FieldOperand(result, HeapObject::kMapOffset),
955 Immediate(Factory::ascii_string_map()));
956 mov(FieldOperand(result, String::kLengthOffset), length);
957 mov(FieldOperand(result, String::kHashFieldOffset),
958 Immediate(String::kEmptyHashField));
959}
960
961
962void MacroAssembler::AllocateConsString(Register result,
963 Register scratch1,
964 Register scratch2,
965 Label* gc_required) {
966 // Allocate heap number in new space.
967 AllocateInNewSpace(ConsString::kSize,
968 result,
969 scratch1,
970 scratch2,
971 gc_required,
972 TAG_OBJECT);
973
974 // Set the map. The other fields are left uninitialized.
975 mov(FieldOperand(result, HeapObject::kMapOffset),
976 Immediate(Factory::cons_string_map()));
977}
978
979
980void MacroAssembler::AllocateAsciiConsString(Register result,
981 Register scratch1,
982 Register scratch2,
983 Label* gc_required) {
984 // Allocate heap number in new space.
985 AllocateInNewSpace(ConsString::kSize,
986 result,
987 scratch1,
988 scratch2,
989 gc_required,
990 TAG_OBJECT);
991
992 // Set the map. The other fields are left uninitialized.
993 mov(FieldOperand(result, HeapObject::kMapOffset),
994 Immediate(Factory::cons_ascii_string_map()));
995}
996
997
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000998void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
999 Register result,
1000 Register op,
1001 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001002 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001003 test(result, Operand(result));
1004 ok.Branch(not_zero, taken);
1005 test(op, Operand(op));
1006 then_target->Branch(sign, not_taken);
1007 ok.Bind();
1008}
1009
1010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011void MacroAssembler::NegativeZeroTest(Register result,
1012 Register op,
1013 Label* then_label) {
1014 Label ok;
1015 test(result, Operand(result));
1016 j(not_zero, &ok, taken);
1017 test(op, Operand(op));
1018 j(sign, then_label, not_taken);
1019 bind(&ok);
1020}
1021
1022
1023void MacroAssembler::NegativeZeroTest(Register result,
1024 Register op1,
1025 Register op2,
1026 Register scratch,
1027 Label* then_label) {
1028 Label ok;
1029 test(result, Operand(result));
1030 j(not_zero, &ok, taken);
1031 mov(scratch, Operand(op1));
1032 or_(scratch, Operand(op2));
1033 j(sign, then_label, not_taken);
1034 bind(&ok);
1035}
1036
1037
ager@chromium.org7c537e22008-10-16 08:43:32 +00001038void MacroAssembler::TryGetFunctionPrototype(Register function,
1039 Register result,
1040 Register scratch,
1041 Label* miss) {
1042 // Check that the receiver isn't a smi.
1043 test(function, Immediate(kSmiTagMask));
1044 j(zero, miss, not_taken);
1045
1046 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001047 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001048 j(not_equal, miss, not_taken);
1049
1050 // Make sure that the function has an instance prototype.
1051 Label non_instance;
1052 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1053 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1054 j(not_zero, &non_instance, not_taken);
1055
1056 // Get the prototype or initial map from the function.
1057 mov(result,
1058 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1059
1060 // If the prototype or initial map is the hole, don't return it and
1061 // simply miss the cache instead. This will allow us to allocate a
1062 // prototype object on-demand in the runtime system.
1063 cmp(Operand(result), Immediate(Factory::the_hole_value()));
1064 j(equal, miss, not_taken);
1065
1066 // If the function does not have an initial map, we're done.
1067 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001068 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001069 j(not_equal, &done);
1070
1071 // Get the prototype from the initial map.
1072 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1073 jmp(&done);
1074
1075 // Non-instance prototype: Fetch prototype from constructor field
1076 // in initial map.
1077 bind(&non_instance);
1078 mov(result, FieldOperand(result, Map::kConstructorOffset));
1079
1080 // All done.
1081 bind(&done);
1082}
1083
1084
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001086 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +00001087 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088}
1089
1090
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001091Object* MacroAssembler::TryCallStub(CodeStub* stub) {
1092 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1093 Object* result = stub->TryGetCode();
1094 if (!result->IsFailure()) {
1095 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1096 }
1097 return result;
1098}
1099
1100
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001101void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001102 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001103 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1104}
1105
1106
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001107Object* MacroAssembler::TryTailCallStub(CodeStub* stub) {
1108 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1109 Object* result = stub->TryGetCode();
1110 if (!result->IsFailure()) {
1111 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1112 }
1113 return result;
1114}
1115
1116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117void MacroAssembler::StubReturn(int argc) {
1118 ASSERT(argc >= 1 && generating_stub());
1119 ret((argc - 1) * kPointerSize);
1120}
1121
1122
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001123void MacroAssembler::IllegalOperation(int num_arguments) {
1124 if (num_arguments > 0) {
1125 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1126 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001127 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128}
1129
1130
1131void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1132 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1133}
1134
1135
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001136Object* MacroAssembler::TryCallRuntime(Runtime::FunctionId id,
1137 int num_arguments) {
1138 return TryCallRuntime(Runtime::FunctionForId(id), num_arguments);
1139}
1140
1141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001143 // If the expected number of arguments of the runtime function is
1144 // constant, we check that the actual number of arguments match the
1145 // expectation.
1146 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001147 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 return;
1149 }
1150
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151 // TODO(1236192): Most runtime routines don't need the number of
1152 // arguments passed in because it is constant. At some point we
1153 // should remove this need and make the runtime routine entry code
1154 // smarter.
1155 Set(eax, Immediate(num_arguments));
1156 mov(ebx, Immediate(ExternalReference(f)));
1157 CEntryStub ces(1);
1158 CallStub(&ces);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159}
1160
1161
ager@chromium.org5c838252010-02-19 08:53:10 +00001162void MacroAssembler::CallExternalReference(ExternalReference ref,
1163 int num_arguments) {
1164 mov(eax, Immediate(num_arguments));
1165 mov(ebx, Immediate(ref));
1166
1167 CEntryStub stub(1);
1168 CallStub(&stub);
1169}
1170
1171
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001172Object* MacroAssembler::TryCallRuntime(Runtime::Function* f,
1173 int num_arguments) {
1174 if (f->nargs >= 0 && f->nargs != num_arguments) {
1175 IllegalOperation(num_arguments);
1176 // Since we did not call the stub, there was no allocation failure.
1177 // Return some non-failure object.
1178 return Heap::undefined_value();
1179 }
1180
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001181 // TODO(1236192): Most runtime routines don't need the number of
1182 // arguments passed in because it is constant. At some point we
1183 // should remove this need and make the runtime routine entry code
1184 // smarter.
1185 Set(eax, Immediate(num_arguments));
1186 mov(ebx, Immediate(ExternalReference(f)));
1187 CEntryStub ces(1);
1188 return TryCallStub(&ces);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001189}
1190
1191
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001192void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1193 int num_arguments,
1194 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001195 // TODO(1236192): Most runtime routines don't need the number of
1196 // arguments passed in because it is constant. At some point we
1197 // should remove this need and make the runtime routine entry code
1198 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001199 Set(eax, Immediate(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001200 JumpToExternalReference(ext);
1201}
1202
1203
1204void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1205 int num_arguments,
1206 int result_size) {
1207 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208}
1209
1210
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001211void MacroAssembler::PushHandleScope(Register scratch) {
1212 // Push the number of extensions, smi-tagged so the gc will ignore it.
1213 ExternalReference extensions_address =
1214 ExternalReference::handle_scope_extensions_address();
1215 mov(scratch, Operand::StaticVariable(extensions_address));
1216 ASSERT_EQ(0, kSmiTag);
1217 shl(scratch, kSmiTagSize);
1218 push(scratch);
1219 mov(Operand::StaticVariable(extensions_address), Immediate(0));
1220 // Push next and limit pointers which will be wordsize aligned and
1221 // hence automatically smi tagged.
1222 ExternalReference next_address =
1223 ExternalReference::handle_scope_next_address();
1224 push(Operand::StaticVariable(next_address));
1225 ExternalReference limit_address =
1226 ExternalReference::handle_scope_limit_address();
1227 push(Operand::StaticVariable(limit_address));
1228}
1229
1230
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001231Object* MacroAssembler::PopHandleScopeHelper(Register saved,
1232 Register scratch,
1233 bool gc_allowed) {
1234 Object* result = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001235 ExternalReference extensions_address =
1236 ExternalReference::handle_scope_extensions_address();
1237 Label write_back;
1238 mov(scratch, Operand::StaticVariable(extensions_address));
1239 cmp(Operand(scratch), Immediate(0));
1240 j(equal, &write_back);
1241 // Calling a runtime function messes with registers so we save and
1242 // restore any one we're asked not to change
1243 if (saved.is_valid()) push(saved);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001244 if (gc_allowed) {
1245 CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1246 } else {
1247 result = TryCallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1248 if (result->IsFailure()) return result;
1249 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001250 if (saved.is_valid()) pop(saved);
1251
1252 bind(&write_back);
1253 ExternalReference limit_address =
1254 ExternalReference::handle_scope_limit_address();
1255 pop(Operand::StaticVariable(limit_address));
1256 ExternalReference next_address =
1257 ExternalReference::handle_scope_next_address();
1258 pop(Operand::StaticVariable(next_address));
1259 pop(scratch);
1260 shr(scratch, kSmiTagSize);
1261 mov(Operand::StaticVariable(extensions_address), scratch);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001262
1263 return result;
1264}
1265
1266
1267void MacroAssembler::PopHandleScope(Register saved, Register scratch) {
1268 PopHandleScopeHelper(saved, scratch, true);
1269}
1270
1271
1272Object* MacroAssembler::TryPopHandleScope(Register saved, Register scratch) {
1273 return PopHandleScopeHelper(saved, scratch, false);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001274}
1275
1276
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001277void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001279 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001280 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001281 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282}
1283
1284
1285void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1286 const ParameterCount& actual,
1287 Handle<Code> code_constant,
1288 const Operand& code_operand,
1289 Label* done,
1290 InvokeFlag flag) {
1291 bool definitely_matches = false;
1292 Label invoke;
1293 if (expected.is_immediate()) {
1294 ASSERT(actual.is_immediate());
1295 if (expected.immediate() == actual.immediate()) {
1296 definitely_matches = true;
1297 } else {
1298 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001299 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1300 if (expected.immediate() == sentinel) {
1301 // Don't worry about adapting arguments for builtins that
1302 // don't want that done. Skip adaption code by making it look
1303 // like we have a match between expected and actual number of
1304 // arguments.
1305 definitely_matches = true;
1306 } else {
1307 mov(ebx, expected.immediate());
1308 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 }
1310 } else {
1311 if (actual.is_immediate()) {
1312 // Expected is in register, actual is immediate. This is the
1313 // case when we invoke function values without going through the
1314 // IC mechanism.
1315 cmp(expected.reg(), actual.immediate());
1316 j(equal, &invoke);
1317 ASSERT(expected.reg().is(ebx));
1318 mov(eax, actual.immediate());
1319 } else if (!expected.reg().is(actual.reg())) {
1320 // Both expected and actual are in (different) registers. This
1321 // is the case when we invoke functions using call and apply.
1322 cmp(expected.reg(), Operand(actual.reg()));
1323 j(equal, &invoke);
1324 ASSERT(actual.reg().is(eax));
1325 ASSERT(expected.reg().is(ebx));
1326 }
1327 }
1328
1329 if (!definitely_matches) {
1330 Handle<Code> adaptor =
1331 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1332 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001333 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1335 } else if (!code_operand.is_reg(edx)) {
1336 mov(edx, code_operand);
1337 }
1338
1339 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001340 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 jmp(done);
1342 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001343 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 }
1345 bind(&invoke);
1346 }
1347}
1348
1349
1350void MacroAssembler::InvokeCode(const Operand& code,
1351 const ParameterCount& expected,
1352 const ParameterCount& actual,
1353 InvokeFlag flag) {
1354 Label done;
1355 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1356 if (flag == CALL_FUNCTION) {
1357 call(code);
1358 } else {
1359 ASSERT(flag == JUMP_FUNCTION);
1360 jmp(code);
1361 }
1362 bind(&done);
1363}
1364
1365
1366void MacroAssembler::InvokeCode(Handle<Code> code,
1367 const ParameterCount& expected,
1368 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001369 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 InvokeFlag flag) {
1371 Label done;
1372 Operand dummy(eax);
1373 InvokePrologue(expected, actual, code, dummy, &done, flag);
1374 if (flag == CALL_FUNCTION) {
1375 call(code, rmode);
1376 } else {
1377 ASSERT(flag == JUMP_FUNCTION);
1378 jmp(code, rmode);
1379 }
1380 bind(&done);
1381}
1382
1383
1384void MacroAssembler::InvokeFunction(Register fun,
1385 const ParameterCount& actual,
1386 InvokeFlag flag) {
1387 ASSERT(fun.is(edi));
1388 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1389 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1390 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1391 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1392 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1393
1394 ParameterCount expected(ebx);
1395 InvokeCode(Operand(edx), expected, actual, flag);
1396}
1397
1398
ager@chromium.org5c838252010-02-19 08:53:10 +00001399void MacroAssembler::InvokeFunction(JSFunction* function,
1400 const ParameterCount& actual,
1401 InvokeFlag flag) {
1402 ASSERT(function->is_compiled());
1403 // Get the function and setup the context.
1404 mov(edi, Immediate(Handle<JSFunction>(function)));
1405 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406
ager@chromium.org5c838252010-02-19 08:53:10 +00001407 // Invoke the cached code.
1408 Handle<Code> code(function->code());
1409 ParameterCount expected(function->shared()->formal_parameter_count());
1410 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1411}
1412
1413
1414void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001415 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001416 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417
1418 // Rely on the assertion to check that the number of provided
1419 // arguments match the expected number of arguments. Fake a
1420 // parameter count to avoid emitting code to do the check.
1421 ParameterCount expected(0);
ager@chromium.org5c838252010-02-19 08:53:10 +00001422 GetBuiltinEntry(edx, id);
1423 InvokeCode(Operand(edx), expected, expected, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424}
1425
1426
1427void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001428 // Load the JavaScript builtin function from the builtins object.
1429 mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1430 mov(edi, FieldOperand(edi, GlobalObject::kBuiltinsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 int builtins_offset =
1432 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00001433 mov(edi, FieldOperand(edi, builtins_offset));
1434 // Load the code entry point from the function into the target register.
1435 mov(target, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1436 mov(target, FieldOperand(target, SharedFunctionInfo::kCodeOffset));
1437 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438}
1439
1440
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001441void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1442 if (context_chain_length > 0) {
1443 // Move up the chain of contexts to the context containing the slot.
1444 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1445 // Load the function context (which is the incoming, outer context).
1446 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1447 for (int i = 1; i < context_chain_length; i++) {
1448 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1449 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1450 }
1451 // The context may be an intermediate context, not a function context.
1452 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1453 } else { // Slot is in the current function context.
1454 // The context may be an intermediate context, not a function context.
1455 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1456 }
1457}
1458
1459
1460
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461void MacroAssembler::Ret() {
1462 ret(0);
1463}
1464
1465
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001466void MacroAssembler::Drop(int stack_elements) {
1467 if (stack_elements > 0) {
1468 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1469 }
1470}
1471
1472
1473void MacroAssembler::Move(Register dst, Handle<Object> value) {
1474 mov(dst, value);
1475}
1476
1477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1479 if (FLAG_native_code_counters && counter->Enabled()) {
1480 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1481 }
1482}
1483
1484
1485void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1486 ASSERT(value > 0);
1487 if (FLAG_native_code_counters && counter->Enabled()) {
1488 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1489 if (value == 1) {
1490 inc(operand);
1491 } else {
1492 add(operand, Immediate(value));
1493 }
1494 }
1495}
1496
1497
1498void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1499 ASSERT(value > 0);
1500 if (FLAG_native_code_counters && counter->Enabled()) {
1501 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1502 if (value == 1) {
1503 dec(operand);
1504 } else {
1505 sub(operand, Immediate(value));
1506 }
1507 }
1508}
1509
1510
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511void MacroAssembler::IncrementCounter(Condition cc,
1512 StatsCounter* counter,
1513 int value) {
1514 ASSERT(value > 0);
1515 if (FLAG_native_code_counters && counter->Enabled()) {
1516 Label skip;
1517 j(NegateCondition(cc), &skip);
1518 pushfd();
1519 IncrementCounter(counter, value);
1520 popfd();
1521 bind(&skip);
1522 }
1523}
1524
1525
1526void MacroAssembler::DecrementCounter(Condition cc,
1527 StatsCounter* counter,
1528 int value) {
1529 ASSERT(value > 0);
1530 if (FLAG_native_code_counters && counter->Enabled()) {
1531 Label skip;
1532 j(NegateCondition(cc), &skip);
1533 pushfd();
1534 DecrementCounter(counter, value);
1535 popfd();
1536 bind(&skip);
1537 }
1538}
1539
1540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541void MacroAssembler::Assert(Condition cc, const char* msg) {
1542 if (FLAG_debug_code) Check(cc, msg);
1543}
1544
1545
1546void MacroAssembler::Check(Condition cc, const char* msg) {
1547 Label L;
1548 j(cc, &L, taken);
1549 Abort(msg);
1550 // will not return here
1551 bind(&L);
1552}
1553
1554
1555void MacroAssembler::Abort(const char* msg) {
1556 // We want to pass the msg string like a smi to avoid GC
1557 // problems, however msg is not guaranteed to be aligned
1558 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001559 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 // from the real pointer as a smi.
1561 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1562 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1563 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1564#ifdef DEBUG
1565 if (msg != NULL) {
1566 RecordComment("Abort message: ");
1567 RecordComment(msg);
1568 }
1569#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001570 // Disable stub call restrictions to always allow calls to abort.
1571 set_allow_stub_calls(true);
1572
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 push(eax);
1574 push(Immediate(p0));
1575 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1576 CallRuntime(Runtime::kAbort, 2);
1577 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001578 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579}
1580
1581
ager@chromium.org5c838252010-02-19 08:53:10 +00001582void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1583 Register instance_type,
1584 Register scratch,
1585 Label *failure) {
1586 if (!scratch.is(instance_type)) {
1587 mov(scratch, instance_type);
1588 }
1589 and_(scratch,
1590 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
1591 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
1592 j(not_equal, failure);
1593}
1594
1595
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001596void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
1597 Register object2,
1598 Register scratch1,
1599 Register scratch2,
1600 Label* failure) {
1601 // Check that both objects are not smis.
1602 ASSERT_EQ(0, kSmiTag);
1603 mov(scratch1, Operand(object1));
1604 and_(scratch1, Operand(object2));
1605 test(scratch1, Immediate(kSmiTagMask));
1606 j(zero, failure);
1607
1608 // Load instance type for both strings.
1609 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
1610 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
1611 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1612 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1613
1614 // Check that both are flat ascii strings.
1615 const int kFlatAsciiStringMask =
1616 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1617 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1618 // Interleave bits from both instance types and compare them in one check.
1619 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1620 and_(scratch1, kFlatAsciiStringMask);
1621 and_(scratch2, kFlatAsciiStringMask);
1622 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1623 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
1624 j(not_equal, failure);
1625}
1626
1627
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001628void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1629 int frameAlignment = OS::ActivationFrameAlignment();
1630 if (frameAlignment != 0) {
1631 // Make stack end at alignment and make room for num_arguments words
1632 // and the original value of esp.
1633 mov(scratch, esp);
1634 sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1635 ASSERT(IsPowerOf2(frameAlignment));
1636 and_(esp, -frameAlignment);
1637 mov(Operand(esp, num_arguments * kPointerSize), scratch);
1638 } else {
1639 sub(Operand(esp), Immediate(num_arguments * kPointerSize));
1640 }
1641}
1642
1643
1644void MacroAssembler::CallCFunction(ExternalReference function,
1645 int num_arguments) {
1646 // Trashing eax is ok as it will be the return value.
1647 mov(Operand(eax), Immediate(function));
1648 CallCFunction(eax, num_arguments);
1649}
1650
1651
1652void MacroAssembler::CallCFunction(Register function,
1653 int num_arguments) {
1654 call(Operand(function));
1655 if (OS::ActivationFrameAlignment() != 0) {
1656 mov(esp, Operand(esp, num_arguments * kPointerSize));
1657 } else {
1658 add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1659 }
1660}
1661
1662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001664 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001665 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 // The size is adjusted with kGap on order for the assembler to generate size
1667 // bytes of instructions without failing with buffer size constraints.
1668 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1669}
1670
1671
1672CodePatcher::~CodePatcher() {
1673 // Indicate that code has changed.
1674 CPU::FlushICache(address_, size_);
1675
1676 // Check that the code was patched as expected.
1677 ASSERT(masm_.pc_ == address_ + size_);
1678 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1679}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
1681
1682} } // namespace v8::internal