blob: ac2895efafec5fe23bd85506001e2c3b896ebc96 [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),
44 unresolved_(0),
kasper.lund7276f142008-07-30 08:49:36 +000045 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000046 allow_stub_calls_(true),
47 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048}
49
50
51static void RecordWriteHelper(MacroAssembler* masm,
52 Register object,
53 Register addr,
54 Register scratch) {
55 Label fast;
56
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000057 // Compute the page start address from the heap object pointer, and reuse
58 // the 'object' register for it.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059 masm->and_(object, ~Page::kPageAlignmentMask);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000060 Register page_start = object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000062 // Compute the bit addr in the remembered set/index of the pointer in the
63 // page. Reuse 'addr' as pointer_offset.
64 masm->sub(addr, Operand(page_start));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065 masm->shr(addr, kObjectAlignmentBits);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000066 Register pointer_offset = addr;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067
68 // If the bit offset lies beyond the normal remembered set range, it is in
69 // the extra remembered set area of a large object.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000070 masm->cmp(pointer_offset, Page::kPageSize / kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071 masm->j(less, &fast);
72
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000073 // Adjust 'page_start' so that addressing using 'pointer_offset' hits the
74 // extra remembered set after the large object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000076 // Find the length of the large object (FixedArray).
77 masm->mov(scratch, Operand(page_start, Page::kObjectStartOffset
78 + FixedArray::kLengthOffset));
79 Register array_length = scratch;
80
81 // Extra remembered set starts right after the large object (a FixedArray), at
82 // page_start + kObjectStartOffset + objectSize
83 // where objectSize is FixedArray::kHeaderSize + kPointerSize * array_length.
84 // Add the delta between the end of the normal RSet and the start of the
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000085 // extra RSet to 'page_start', so that addressing the bit using
86 // 'pointer_offset' hits the extra RSet words.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000087 masm->lea(page_start,
88 Operand(page_start, array_length, times_pointer_size,
89 Page::kObjectStartOffset + FixedArray::kHeaderSize
90 - Page::kRSetEndOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
92 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
93 // to limit code size. We should probably evaluate this decision by
94 // measuring the performance of an equivalent implementation using
95 // "simpler" instructions
96 masm->bind(&fast);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +000097 masm->bts(Operand(page_start, Page::kRSetOffset), pointer_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098}
99
100
101class RecordWriteStub : public CodeStub {
102 public:
103 RecordWriteStub(Register object, Register addr, Register scratch)
104 : object_(object), addr_(addr), scratch_(scratch) { }
105
106 void Generate(MacroAssembler* masm);
107
108 private:
109 Register object_;
110 Register addr_;
111 Register scratch_;
112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113#ifdef DEBUG
114 void Print() {
115 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
116 object_.code(), addr_.code(), scratch_.code());
117 }
118#endif
119
120 // Minor key encoding in 12 bits of three registers (object, address and
121 // scratch) OOOOAAAASSSS.
122 class ScratchBits: public BitField<uint32_t, 0, 4> {};
123 class AddressBits: public BitField<uint32_t, 4, 4> {};
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000124 class ObjectBits: public BitField<uint32_t, 8, 4> {};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
126 Major MajorKey() { return RecordWrite; }
127
128 int MinorKey() {
129 // Encode the registers.
130 return ObjectBits::encode(object_.code()) |
131 AddressBits::encode(addr_.code()) |
132 ScratchBits::encode(scratch_.code());
133 }
134};
135
136
137void RecordWriteStub::Generate(MacroAssembler* masm) {
138 RecordWriteHelper(masm, object_, addr_, scratch_);
139 masm->ret(0);
140}
141
142
143// Set the remembered set bit for [object+offset].
144// object is the object being stored into, value is the object being stored.
145// If offset is zero, then the scratch register contains the array index into
146// the elements array represented as a Smi.
147// All registers are clobbered by the operation.
148void MacroAssembler::RecordWrite(Register object, int offset,
149 Register value, Register scratch) {
150 // First, check if a remembered set write is even needed. The tests below
151 // catch stores of Smis and stores into young gen (which does not have space
152 // for the remembered set bits.
153 Label done;
154
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000155 // Skip barrier if writing a smi.
156 ASSERT_EQ(0, kSmiTag);
157 test(value, Immediate(kSmiTagMask));
158 j(zero, &done);
159
160 if (Serializer::enabled()) {
161 // Can't do arithmetic on external references if it might get serialized.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 mov(value, Operand(object));
163 and_(value, Heap::NewSpaceMask());
164 cmp(Operand(value), Immediate(ExternalReference::new_space_start()));
165 j(equal, &done);
166 } else {
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000167 int32_t new_space_start = reinterpret_cast<int32_t>(
168 ExternalReference::new_space_start().address());
169 lea(value, Operand(object, -new_space_start));
170 and_(value, Heap::NewSpaceMask());
171 j(equal, &done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 }
173
174 if ((offset > 0) && (offset < Page::kMaxHeapObjectSize)) {
175 // Compute the bit offset in the remembered set, leave it in 'value'.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000176 lea(value, Operand(object, offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 and_(value, Page::kPageAlignmentMask);
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000178 shr(value, kPointerSizeLog2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179
180 // Compute the page address from the heap object pointer, leave it in
181 // 'object'.
182 and_(object, ~Page::kPageAlignmentMask);
183
184 // NOTE: For now, we use the bit-test-and-set (bts) x86 instruction
185 // to limit code size. We should probably evaluate this decision by
186 // measuring the performance of an equivalent implementation using
187 // "simpler" instructions
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000188 bts(Operand(object, Page::kRSetOffset), value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 } else {
190 Register dst = scratch;
191 if (offset != 0) {
192 lea(dst, Operand(object, offset));
193 } else {
194 // array access: calculate the destination address in the same manner as
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000195 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
196 // into an array of words.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000197 ASSERT_EQ(1, kSmiTagSize);
198 ASSERT_EQ(0, kSmiTag);
199 lea(dst, Operand(object, dst, times_half_pointer_size,
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000200 FixedArray::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 }
202 // If we are already generating a shared stub, not inlining the
203 // record write code isn't going to save us any memory.
204 if (generating_stub()) {
205 RecordWriteHelper(this, object, dst, value);
206 } else {
207 RecordWriteStub stub(object, dst, value);
208 CallStub(&stub);
209 }
210 }
211
212 bind(&done);
213}
214
215
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000216void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
217 cmp(esp,
218 Operand::StaticVariable(ExternalReference::address_of_stack_limit()));
219 j(below, on_stack_overflow);
220}
221
222
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000223#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224void MacroAssembler::SaveRegistersToMemory(RegList regs) {
225 ASSERT((regs & ~kJSCallerSaved) == 0);
226 // Copy the content of registers to memory location.
227 for (int i = 0; i < kNumJSCallerSaved; i++) {
228 int r = JSCallerSavedCode(i);
229 if ((regs & (1 << r)) != 0) {
230 Register reg = { r };
231 ExternalReference reg_addr =
232 ExternalReference(Debug_Address::Register(i));
233 mov(Operand::StaticVariable(reg_addr), reg);
234 }
235 }
236}
237
238
239void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
240 ASSERT((regs & ~kJSCallerSaved) == 0);
241 // Copy the content of memory location to registers.
242 for (int i = kNumJSCallerSaved; --i >= 0;) {
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(reg, Operand::StaticVariable(reg_addr));
249 }
250 }
251}
252
253
254void MacroAssembler::PushRegistersFromMemory(RegList regs) {
255 ASSERT((regs & ~kJSCallerSaved) == 0);
256 // Push the content of the memory location to the stack.
257 for (int i = 0; i < kNumJSCallerSaved; i++) {
258 int r = JSCallerSavedCode(i);
259 if ((regs & (1 << r)) != 0) {
260 ExternalReference reg_addr =
261 ExternalReference(Debug_Address::Register(i));
262 push(Operand::StaticVariable(reg_addr));
263 }
264 }
265}
266
267
268void MacroAssembler::PopRegistersToMemory(RegList regs) {
269 ASSERT((regs & ~kJSCallerSaved) == 0);
270 // Pop the content from the stack to the memory location.
271 for (int i = kNumJSCallerSaved; --i >= 0;) {
272 int r = JSCallerSavedCode(i);
273 if ((regs & (1 << r)) != 0) {
274 ExternalReference reg_addr =
275 ExternalReference(Debug_Address::Register(i));
276 pop(Operand::StaticVariable(reg_addr));
277 }
278 }
279}
280
281
282void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
283 Register scratch,
284 RegList regs) {
285 ASSERT((regs & ~kJSCallerSaved) == 0);
286 // Copy the content of the stack to the memory location and adjust base.
287 for (int i = kNumJSCallerSaved; --i >= 0;) {
288 int r = JSCallerSavedCode(i);
289 if ((regs & (1 << r)) != 0) {
290 mov(scratch, Operand(base, 0));
291 ExternalReference reg_addr =
292 ExternalReference(Debug_Address::Register(i));
293 mov(Operand::StaticVariable(reg_addr), scratch);
294 lea(base, Operand(base, kPointerSize));
295 }
296 }
297}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000298#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299
300void MacroAssembler::Set(Register dst, const Immediate& x) {
301 if (x.is_zero()) {
302 xor_(dst, Operand(dst)); // shorter than mov
303 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000304 mov(dst, x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 }
306}
307
308
309void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
310 mov(dst, x);
311}
312
313
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314void MacroAssembler::CmpObjectType(Register heap_object,
315 InstanceType type,
316 Register map) {
317 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
318 CmpInstanceType(map, type);
319}
320
321
322void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
323 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
324 static_cast<int8_t>(type));
325}
326
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328void MacroAssembler::FCmp() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000329 if (CpuFeatures::IsSupported(CMOV)) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000330 fucomip();
331 ffree(0);
332 fincstp();
333 } else {
334 fucompp();
335 push(eax);
336 fnstsw_ax();
337 sahf();
338 pop(eax);
339 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340}
341
342
ager@chromium.org7c537e22008-10-16 08:43:32 +0000343void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 push(ebp);
345 mov(ebp, Operand(esp));
346 push(esi);
347 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000348 push(Immediate(CodeObject()));
349 if (FLAG_debug_code) {
350 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
351 Check(not_equal, "code object not properly patched");
352 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353}
354
355
ager@chromium.org7c537e22008-10-16 08:43:32 +0000356void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 if (FLAG_debug_code) {
358 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
359 Immediate(Smi::FromInt(type)));
360 Check(equal, "stack frame types must match");
361 }
362 leave();
363}
364
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000365void MacroAssembler::EnterExitFramePrologue(ExitFrame::Mode mode) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000366 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000367 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000368 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
369 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
370 push(ebp);
371 mov(ebp, Operand(esp));
372
373 // Reserve room for entry stack pointer and push the debug marker.
374 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
375 push(Immediate(0)); // saved entry sp, patched before call
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000376 if (mode == ExitFrame::MODE_DEBUG) {
377 push(Immediate(0));
378 } else {
379 push(Immediate(CodeObject()));
380 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000381
382 // Save the frame pointer and the context in top.
383 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
384 ExternalReference context_address(Top::k_context_address);
385 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
386 mov(Operand::StaticVariable(context_address), esi);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000387}
ager@chromium.org236ad962008-09-25 09:45:57 +0000388
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000389void MacroAssembler::EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000390#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000391 // Save the state of all registers to the stack from the memory
392 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000393 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000394 // TODO(1243899): This should be symmetric to
395 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
396 // correct here, but computed for the other call. Very error
397 // prone! FIX THIS. Actually there are deeper problems with
398 // register saving than this asymmetry (see the bug report
399 // associated with this issue).
400 PushRegistersFromMemory(kJSCallerSaved);
401 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000402#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000403
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000404 // Reserve space for arguments.
405 sub(Operand(esp), Immediate(argc * kPointerSize));
ager@chromium.org236ad962008-09-25 09:45:57 +0000406
407 // Get the required frame alignment for the OS.
408 static const int kFrameAlignment = OS::ActivationFrameAlignment();
409 if (kFrameAlignment > 0) {
410 ASSERT(IsPowerOf2(kFrameAlignment));
411 and_(esp, -kFrameAlignment);
412 }
413
414 // Patch the saved entry sp.
415 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
416}
417
418
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000419void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
420 EnterExitFramePrologue(mode);
421
422 // Setup argc and argv in callee-saved registers.
423 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
424 mov(edi, Operand(eax));
425 lea(esi, Operand(ebp, eax, times_4, offset));
426
427 EnterExitFrameEpilogue(mode, 2);
428}
429
430
431void MacroAssembler::EnterApiExitFrame(ExitFrame::Mode mode,
432 int stack_space,
433 int argc) {
434 EnterExitFramePrologue(mode);
435
436 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
437 lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset));
438
439 EnterExitFrameEpilogue(mode, argc);
440}
441
442
443void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000444#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000445 // Restore the memory copy of the registers by digging them out from
446 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000447 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000448 // It's okay to clobber register ebx below because we don't need
449 // the function pointer after this.
450 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000451 int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000452 lea(ebx, Operand(ebp, kOffset));
453 CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved);
454 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000455#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000456
457 // Get the return address from the stack and restore the frame pointer.
458 mov(ecx, Operand(ebp, 1 * kPointerSize));
459 mov(ebp, Operand(ebp, 0 * kPointerSize));
460
461 // Pop the arguments and the receiver from the caller stack.
462 lea(esp, Operand(esi, 1 * kPointerSize));
463
464 // Restore current context from top and clear it in debug mode.
465 ExternalReference context_address(Top::k_context_address);
466 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000467#ifdef DEBUG
468 mov(Operand::StaticVariable(context_address), Immediate(0));
469#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000470
471 // Push the return address to get ready to return.
472 push(ecx);
473
474 // Clear the top frame.
475 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
476 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
477}
478
479
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480void MacroAssembler::PushTryHandler(CodeLocation try_location,
481 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000482 // Adjust this code if not the case.
483 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 // The pc (return address) is already on TOS.
485 if (try_location == IN_JAVASCRIPT) {
486 if (type == TRY_CATCH_HANDLER) {
487 push(Immediate(StackHandler::TRY_CATCH));
488 } else {
489 push(Immediate(StackHandler::TRY_FINALLY));
490 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 } else {
493 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000494 // The frame pointer does not point to a JS frame so we save NULL
495 // for ebp. We expect the code throwing an exception to check ebp
496 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000498 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000500 // Save the current handler as the next handler.
501 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
502 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
504}
505
506
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000507void MacroAssembler::PopTryHandler() {
508 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
509 pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
510 add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
515 JSObject* holder, Register holder_reg,
516 Register scratch,
517 Label* miss) {
518 // Make sure there's no overlap between scratch and the other
519 // registers.
520 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
521
522 // Keep track of the current object in register reg.
523 Register reg = object_reg;
524 int depth = 1;
525
526 // Check the maps in the prototype chain.
527 // Traverse the prototype chain from the object and do map checks.
528 while (object != holder) {
529 depth++;
530
531 // Only global objects and objects that do not require access
532 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000533 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534
535 JSObject* prototype = JSObject::cast(object->GetPrototype());
536 if (Heap::InNewSpace(prototype)) {
537 // Get the map of the current object.
538 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
539 cmp(Operand(scratch), Immediate(Handle<Map>(object->map())));
540 // Branch on the result of the map check.
541 j(not_equal, miss, not_taken);
542 // Check access rights to the global object. This has to happen
543 // after the map check so that we know that the object is
544 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000545 if (object->IsJSGlobalProxy()) {
546 CheckAccessGlobalProxy(reg, scratch, miss);
547
548 // Restore scratch register to be the map of the object.
549 // We load the prototype from the map in the scratch register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
551 }
552 // The prototype is in new space; we cannot store a reference
553 // to it in the code. Load it from the map.
554 reg = holder_reg; // from now the object is in holder_reg
555 mov(reg, FieldOperand(scratch, Map::kPrototypeOffset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 } else {
558 // Check the map of the current object.
559 cmp(FieldOperand(reg, HeapObject::kMapOffset),
560 Immediate(Handle<Map>(object->map())));
561 // Branch on the result of the map check.
562 j(not_equal, miss, not_taken);
563 // Check access rights to the global object. This has to happen
564 // after the map check so that we know that the object is
565 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000566 if (object->IsJSGlobalProxy()) {
567 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 }
569 // The prototype is in old space; load it directly.
570 reg = holder_reg; // from now the object is in holder_reg
571 mov(reg, Handle<JSObject>(prototype));
572 }
573
574 // Go to the next object in the prototype chain.
575 object = prototype;
576 }
577
578 // Check the holder map.
579 cmp(FieldOperand(reg, HeapObject::kMapOffset),
580 Immediate(Handle<Map>(holder->map())));
581 j(not_equal, miss, not_taken);
582
583 // Log the check depth.
584 LOG(IntEvent("check-maps-depth", depth));
585
586 // Perform security check for access to the global object and return
587 // the holder register.
588 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000589 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
590 if (object->IsJSGlobalProxy()) {
591 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 }
593 return reg;
594}
595
596
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000597void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000598 Register scratch,
599 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000600 Label same_contexts;
601
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 ASSERT(!holder_reg.is(scratch));
603
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000604 // Load current lexical context from the stack frame.
605 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
606
607 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 if (FLAG_debug_code) {
609 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000610 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000612 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
614 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000615 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
616
617 // Check the context is a global context.
618 if (FLAG_debug_code) {
619 push(scratch);
620 // Read the first word and compare to global_context_map.
621 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
622 cmp(scratch, Factory::global_context_map());
623 Check(equal, "JSGlobalObject::global_context should be a global context.");
624 pop(scratch);
625 }
626
627 // Check if both contexts are the same.
628 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
629 j(equal, &same_contexts, taken);
630
631 // Compare security tokens, save holder_reg on the stack so we can use it
632 // as a temporary register.
633 //
634 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
635 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 // Check that the security token in the calling global object is
637 // compatible with the security token in the receiving global
638 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000639 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
640
641 // Check the context is a global context.
642 if (FLAG_debug_code) {
643 cmp(holder_reg, Factory::null_value());
644 Check(not_equal, "JSGlobalProxy::context() should not be null.");
645
646 push(holder_reg);
647 // Read the first word and compare to global_context_map(),
648 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
649 cmp(holder_reg, Factory::global_context_map());
650 Check(equal, "JSGlobalObject::global_context should be a global context.");
651 pop(holder_reg);
652 }
653
654 int token_offset = Context::kHeaderSize +
655 Context::SECURITY_TOKEN_INDEX * kPointerSize;
656 mov(scratch, FieldOperand(scratch, token_offset));
657 cmp(scratch, FieldOperand(holder_reg, token_offset));
658 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000660
661 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662}
663
664
ager@chromium.orga1645e22009-09-09 19:27:10 +0000665void MacroAssembler::LoadAllocationTopHelper(Register result,
666 Register result_end,
667 Register scratch,
668 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000669 ExternalReference new_space_allocation_top =
670 ExternalReference::new_space_allocation_top_address();
671
672 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000673 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000674 // No use of scratch if allocation top is provided.
675 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000676#ifdef DEBUG
677 // Assert that result actually contains top on entry.
678 cmp(result, Operand::StaticVariable(new_space_allocation_top));
679 Check(equal, "Unexpected allocation top");
680#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000681 return;
682 }
683
684 // Move address of new object to result. Use scratch register if available.
685 if (scratch.is(no_reg)) {
686 mov(result, Operand::StaticVariable(new_space_allocation_top));
687 } else {
688 ASSERT(!scratch.is(result_end));
689 mov(Operand(scratch), Immediate(new_space_allocation_top));
690 mov(result, Operand(scratch, 0));
691 }
692}
693
694
695void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
696 Register scratch) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000697 if (FLAG_debug_code) {
698 test(result_end, Immediate(kObjectAlignmentMask));
699 Check(zero, "Unaligned allocation in new space");
700 }
701
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000702 ExternalReference new_space_allocation_top =
703 ExternalReference::new_space_allocation_top_address();
704
705 // Update new top. Use scratch if available.
706 if (scratch.is(no_reg)) {
707 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
708 } else {
709 mov(Operand(scratch, 0), result_end);
710 }
711}
712
ager@chromium.orga1645e22009-09-09 19:27:10 +0000713
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000714void MacroAssembler::AllocateInNewSpace(int object_size,
715 Register result,
716 Register result_end,
717 Register scratch,
718 Label* gc_required,
719 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000720 ASSERT(!result.is(result_end));
721
722 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000723 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000724
725 // Calculate new top and bail out if new space is exhausted.
726 ExternalReference new_space_allocation_limit =
727 ExternalReference::new_space_allocation_limit_address();
728 lea(result_end, Operand(result, object_size));
729 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
730 j(above, gc_required, not_taken);
731
732 // Update allocation top.
733 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000734
735 // Tag result if requested.
736 if ((flags & TAG_OBJECT) != 0) {
737 or_(Operand(result), Immediate(kHeapObjectTag));
738 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000739}
740
741
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000742void MacroAssembler::AllocateInNewSpace(int header_size,
743 ScaleFactor element_size,
744 Register element_count,
745 Register result,
746 Register result_end,
747 Register scratch,
748 Label* gc_required,
749 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000750 ASSERT(!result.is(result_end));
751
752 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000753 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000754
755 // Calculate new top and bail out if new space is exhausted.
756 ExternalReference new_space_allocation_limit =
757 ExternalReference::new_space_allocation_limit_address();
758 lea(result_end, Operand(result, element_count, element_size, header_size));
759 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
760 j(above, gc_required);
761
762 // Update allocation top.
763 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000764
765 // Tag result if requested.
766 if ((flags & TAG_OBJECT) != 0) {
767 or_(Operand(result), Immediate(kHeapObjectTag));
768 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000769}
770
771
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000772void MacroAssembler::AllocateInNewSpace(Register object_size,
773 Register result,
774 Register result_end,
775 Register scratch,
776 Label* gc_required,
777 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000778 ASSERT(!result.is(result_end));
779
780 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000781 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000782
783 // Calculate new top and bail out if new space is exhausted.
784 ExternalReference new_space_allocation_limit =
785 ExternalReference::new_space_allocation_limit_address();
786 if (!object_size.is(result_end)) {
787 mov(result_end, object_size);
788 }
789 add(result_end, Operand(result));
790 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
791 j(above, gc_required, not_taken);
792
793 // Update allocation top.
794 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000795
796 // Tag result if requested.
797 if ((flags & TAG_OBJECT) != 0) {
798 or_(Operand(result), Immediate(kHeapObjectTag));
799 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000800}
801
802
803void MacroAssembler::UndoAllocationInNewSpace(Register object) {
804 ExternalReference new_space_allocation_top =
805 ExternalReference::new_space_allocation_top_address();
806
807 // Make sure the object has no tag before resetting top.
808 and_(Operand(object), Immediate(~kHeapObjectTagMask));
809#ifdef DEBUG
810 cmp(object, Operand::StaticVariable(new_space_allocation_top));
811 Check(below, "Undo allocation of non allocated memory");
812#endif
813 mov(Operand::StaticVariable(new_space_allocation_top), object);
814}
815
816
ager@chromium.org3811b432009-10-28 14:53:37 +0000817void MacroAssembler::AllocateHeapNumber(Register result,
818 Register scratch1,
819 Register scratch2,
820 Label* gc_required) {
821 // Allocate heap number in new space.
822 AllocateInNewSpace(HeapNumber::kSize,
823 result,
824 scratch1,
825 scratch2,
826 gc_required,
827 TAG_OBJECT);
828
829 // Set the map.
830 mov(FieldOperand(result, HeapObject::kMapOffset),
831 Immediate(Factory::heap_number_map()));
832}
833
834
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000835void MacroAssembler::AllocateTwoByteString(Register result,
836 Register length,
837 Register scratch1,
838 Register scratch2,
839 Register scratch3,
840 Label* gc_required) {
841 // Calculate the number of bytes needed for the characters in the string while
842 // observing object alignment.
843 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000844 ASSERT(kShortSize == 2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000845 // scratch1 = length * 2 + kObjectAlignmentMask.
846 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000847 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
848
849 // Allocate two byte string in new space.
850 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
851 times_1,
852 scratch1,
853 result,
854 scratch2,
855 scratch3,
856 gc_required,
857 TAG_OBJECT);
858
859 // Set the map, length and hash field.
860 mov(FieldOperand(result, HeapObject::kMapOffset),
861 Immediate(Factory::string_map()));
862 mov(FieldOperand(result, String::kLengthOffset), length);
863 mov(FieldOperand(result, String::kHashFieldOffset),
864 Immediate(String::kEmptyHashField));
865}
866
867
868void MacroAssembler::AllocateAsciiString(Register result,
869 Register length,
870 Register scratch1,
871 Register scratch2,
872 Register scratch3,
873 Label* gc_required) {
874 // Calculate the number of bytes needed for the characters in the string while
875 // observing object alignment.
876 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
877 mov(scratch1, length);
878 ASSERT(kCharSize == 1);
879 add(Operand(scratch1), Immediate(kObjectAlignmentMask));
880 and_(Operand(scratch1), Immediate(~kObjectAlignmentMask));
881
882 // Allocate ascii string in new space.
883 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
884 times_1,
885 scratch1,
886 result,
887 scratch2,
888 scratch3,
889 gc_required,
890 TAG_OBJECT);
891
892 // Set the map, length and hash field.
893 mov(FieldOperand(result, HeapObject::kMapOffset),
894 Immediate(Factory::ascii_string_map()));
895 mov(FieldOperand(result, String::kLengthOffset), length);
896 mov(FieldOperand(result, String::kHashFieldOffset),
897 Immediate(String::kEmptyHashField));
898}
899
900
901void MacroAssembler::AllocateConsString(Register result,
902 Register scratch1,
903 Register scratch2,
904 Label* gc_required) {
905 // Allocate heap number in new space.
906 AllocateInNewSpace(ConsString::kSize,
907 result,
908 scratch1,
909 scratch2,
910 gc_required,
911 TAG_OBJECT);
912
913 // Set the map. The other fields are left uninitialized.
914 mov(FieldOperand(result, HeapObject::kMapOffset),
915 Immediate(Factory::cons_string_map()));
916}
917
918
919void MacroAssembler::AllocateAsciiConsString(Register result,
920 Register scratch1,
921 Register scratch2,
922 Label* gc_required) {
923 // Allocate heap number in new space.
924 AllocateInNewSpace(ConsString::kSize,
925 result,
926 scratch1,
927 scratch2,
928 gc_required,
929 TAG_OBJECT);
930
931 // Set the map. The other fields are left uninitialized.
932 mov(FieldOperand(result, HeapObject::kMapOffset),
933 Immediate(Factory::cons_ascii_string_map()));
934}
935
936
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000937void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
938 Register result,
939 Register op,
940 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000941 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000942 test(result, Operand(result));
943 ok.Branch(not_zero, taken);
944 test(op, Operand(op));
945 then_target->Branch(sign, not_taken);
946 ok.Bind();
947}
948
949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950void MacroAssembler::NegativeZeroTest(Register result,
951 Register op,
952 Label* then_label) {
953 Label ok;
954 test(result, Operand(result));
955 j(not_zero, &ok, taken);
956 test(op, Operand(op));
957 j(sign, then_label, not_taken);
958 bind(&ok);
959}
960
961
962void MacroAssembler::NegativeZeroTest(Register result,
963 Register op1,
964 Register op2,
965 Register scratch,
966 Label* then_label) {
967 Label ok;
968 test(result, Operand(result));
969 j(not_zero, &ok, taken);
970 mov(scratch, Operand(op1));
971 or_(scratch, Operand(op2));
972 j(sign, then_label, not_taken);
973 bind(&ok);
974}
975
976
ager@chromium.org7c537e22008-10-16 08:43:32 +0000977void MacroAssembler::TryGetFunctionPrototype(Register function,
978 Register result,
979 Register scratch,
980 Label* miss) {
981 // Check that the receiver isn't a smi.
982 test(function, Immediate(kSmiTagMask));
983 j(zero, miss, not_taken);
984
985 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000986 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000987 j(not_equal, miss, not_taken);
988
989 // Make sure that the function has an instance prototype.
990 Label non_instance;
991 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
992 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
993 j(not_zero, &non_instance, not_taken);
994
995 // Get the prototype or initial map from the function.
996 mov(result,
997 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
998
999 // If the prototype or initial map is the hole, don't return it and
1000 // simply miss the cache instead. This will allow us to allocate a
1001 // prototype object on-demand in the runtime system.
1002 cmp(Operand(result), Immediate(Factory::the_hole_value()));
1003 j(equal, miss, not_taken);
1004
1005 // If the function does not have an initial map, we're done.
1006 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001007 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001008 j(not_equal, &done);
1009
1010 // Get the prototype from the initial map.
1011 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1012 jmp(&done);
1013
1014 // Non-instance prototype: Fetch prototype from constructor field
1015 // in initial map.
1016 bind(&non_instance);
1017 mov(result, FieldOperand(result, Map::kConstructorOffset));
1018
1019 // All done.
1020 bind(&done);
1021}
1022
1023
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024void MacroAssembler::CallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001025 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
ager@chromium.org236ad962008-09-25 09:45:57 +00001026 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027}
1028
1029
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001030Object* MacroAssembler::TryCallStub(CodeStub* stub) {
1031 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1032 Object* result = stub->TryGetCode();
1033 if (!result->IsFailure()) {
1034 call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1035 }
1036 return result;
1037}
1038
1039
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001040void MacroAssembler::TailCallStub(CodeStub* stub) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001041 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001042 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1043}
1044
1045
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001046Object* MacroAssembler::TryTailCallStub(CodeStub* stub) {
1047 ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs.
1048 Object* result = stub->TryGetCode();
1049 if (!result->IsFailure()) {
1050 jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET);
1051 }
1052 return result;
1053}
1054
1055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056void MacroAssembler::StubReturn(int argc) {
1057 ASSERT(argc >= 1 && generating_stub());
1058 ret((argc - 1) * kPointerSize);
1059}
1060
1061
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001062void MacroAssembler::IllegalOperation(int num_arguments) {
1063 if (num_arguments > 0) {
1064 add(Operand(esp), Immediate(num_arguments * kPointerSize));
1065 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001066 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067}
1068
1069
1070void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1071 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1072}
1073
1074
1075void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001076 // If the expected number of arguments of the runtime function is
1077 // constant, we check that the actual number of arguments match the
1078 // expectation.
1079 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001080 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 return;
1082 }
1083
mads.s.ager31e71382008-08-13 09:32:07 +00001084 Runtime::FunctionId function_id =
1085 static_cast<Runtime::FunctionId>(f->stub_id);
1086 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 CallStub(&stub);
1088}
1089
1090
mads.s.ager31e71382008-08-13 09:32:07 +00001091void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001092 int num_arguments,
1093 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001094 // TODO(1236192): Most runtime routines don't need the number of
1095 // arguments passed in because it is constant. At some point we
1096 // should remove this need and make the runtime routine entry code
1097 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001098 Set(eax, Immediate(num_arguments));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001099 JumpToRuntime(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100}
1101
1102
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001103void MacroAssembler::PushHandleScope(Register scratch) {
1104 // Push the number of extensions, smi-tagged so the gc will ignore it.
1105 ExternalReference extensions_address =
1106 ExternalReference::handle_scope_extensions_address();
1107 mov(scratch, Operand::StaticVariable(extensions_address));
1108 ASSERT_EQ(0, kSmiTag);
1109 shl(scratch, kSmiTagSize);
1110 push(scratch);
1111 mov(Operand::StaticVariable(extensions_address), Immediate(0));
1112 // Push next and limit pointers which will be wordsize aligned and
1113 // hence automatically smi tagged.
1114 ExternalReference next_address =
1115 ExternalReference::handle_scope_next_address();
1116 push(Operand::StaticVariable(next_address));
1117 ExternalReference limit_address =
1118 ExternalReference::handle_scope_limit_address();
1119 push(Operand::StaticVariable(limit_address));
1120}
1121
1122
1123void MacroAssembler::PopHandleScope(Register saved, Register scratch) {
1124 ExternalReference extensions_address =
1125 ExternalReference::handle_scope_extensions_address();
1126 Label write_back;
1127 mov(scratch, Operand::StaticVariable(extensions_address));
1128 cmp(Operand(scratch), Immediate(0));
1129 j(equal, &write_back);
1130 // Calling a runtime function messes with registers so we save and
1131 // restore any one we're asked not to change
1132 if (saved.is_valid()) push(saved);
1133 CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
1134 if (saved.is_valid()) pop(saved);
1135
1136 bind(&write_back);
1137 ExternalReference limit_address =
1138 ExternalReference::handle_scope_limit_address();
1139 pop(Operand::StaticVariable(limit_address));
1140 ExternalReference next_address =
1141 ExternalReference::handle_scope_next_address();
1142 pop(Operand::StaticVariable(next_address));
1143 pop(scratch);
1144 shr(scratch, kSmiTagSize);
1145 mov(Operand::StaticVariable(extensions_address), scratch);
1146}
1147
1148
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001149void MacroAssembler::JumpToRuntime(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001151 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001152 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001153 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154}
1155
1156
1157void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1158 const ParameterCount& actual,
1159 Handle<Code> code_constant,
1160 const Operand& code_operand,
1161 Label* done,
1162 InvokeFlag flag) {
1163 bool definitely_matches = false;
1164 Label invoke;
1165 if (expected.is_immediate()) {
1166 ASSERT(actual.is_immediate());
1167 if (expected.immediate() == actual.immediate()) {
1168 definitely_matches = true;
1169 } else {
1170 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001171 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1172 if (expected.immediate() == sentinel) {
1173 // Don't worry about adapting arguments for builtins that
1174 // don't want that done. Skip adaption code by making it look
1175 // like we have a match between expected and actual number of
1176 // arguments.
1177 definitely_matches = true;
1178 } else {
1179 mov(ebx, expected.immediate());
1180 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 }
1182 } else {
1183 if (actual.is_immediate()) {
1184 // Expected is in register, actual is immediate. This is the
1185 // case when we invoke function values without going through the
1186 // IC mechanism.
1187 cmp(expected.reg(), actual.immediate());
1188 j(equal, &invoke);
1189 ASSERT(expected.reg().is(ebx));
1190 mov(eax, actual.immediate());
1191 } else if (!expected.reg().is(actual.reg())) {
1192 // Both expected and actual are in (different) registers. This
1193 // is the case when we invoke functions using call and apply.
1194 cmp(expected.reg(), Operand(actual.reg()));
1195 j(equal, &invoke);
1196 ASSERT(actual.reg().is(eax));
1197 ASSERT(expected.reg().is(ebx));
1198 }
1199 }
1200
1201 if (!definitely_matches) {
1202 Handle<Code> adaptor =
1203 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1204 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001205 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1207 } else if (!code_operand.is_reg(edx)) {
1208 mov(edx, code_operand);
1209 }
1210
1211 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001212 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 jmp(done);
1214 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001215 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 }
1217 bind(&invoke);
1218 }
1219}
1220
1221
1222void MacroAssembler::InvokeCode(const Operand& code,
1223 const ParameterCount& expected,
1224 const ParameterCount& actual,
1225 InvokeFlag flag) {
1226 Label done;
1227 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1228 if (flag == CALL_FUNCTION) {
1229 call(code);
1230 } else {
1231 ASSERT(flag == JUMP_FUNCTION);
1232 jmp(code);
1233 }
1234 bind(&done);
1235}
1236
1237
1238void MacroAssembler::InvokeCode(Handle<Code> code,
1239 const ParameterCount& expected,
1240 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001241 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242 InvokeFlag flag) {
1243 Label done;
1244 Operand dummy(eax);
1245 InvokePrologue(expected, actual, code, dummy, &done, flag);
1246 if (flag == CALL_FUNCTION) {
1247 call(code, rmode);
1248 } else {
1249 ASSERT(flag == JUMP_FUNCTION);
1250 jmp(code, rmode);
1251 }
1252 bind(&done);
1253}
1254
1255
1256void MacroAssembler::InvokeFunction(Register fun,
1257 const ParameterCount& actual,
1258 InvokeFlag flag) {
1259 ASSERT(fun.is(edi));
1260 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1261 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1262 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1263 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1264 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1265
1266 ParameterCount expected(ebx);
1267 InvokeCode(Operand(edx), expected, actual, flag);
1268}
1269
1270
1271void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
1272 bool resolved;
1273 Handle<Code> code = ResolveBuiltin(id, &resolved);
1274
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001275 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001276 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277
1278 // Rely on the assertion to check that the number of provided
1279 // arguments match the expected number of arguments. Fake a
1280 // parameter count to avoid emitting code to do the check.
1281 ParameterCount expected(0);
ager@chromium.org236ad962008-09-25 09:45:57 +00001282 InvokeCode(Handle<Code>(code), expected, expected,
1283 RelocInfo::CODE_TARGET, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284
1285 const char* name = Builtins::GetName(id);
1286 int argc = Builtins::GetArgumentsCount(id);
1287
1288 if (!resolved) {
1289 uint32_t flags =
1290 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001291 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1293 unresolved_.Add(entry);
1294 }
1295}
1296
1297
1298void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1299 bool resolved;
1300 Handle<Code> code = ResolveBuiltin(id, &resolved);
1301
1302 const char* name = Builtins::GetName(id);
1303 int argc = Builtins::GetArgumentsCount(id);
1304
1305 mov(Operand(target), Immediate(code));
1306 if (!resolved) {
1307 uint32_t flags =
1308 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001309 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1311 unresolved_.Add(entry);
1312 }
1313 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
1314}
1315
1316
1317Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1318 bool* resolved) {
1319 // Move the builtin function into the temporary function slot by
1320 // reading it from the builtins object. NOTE: We should be able to
1321 // reduce this to two instructions by putting the function table in
1322 // the global object instead of the "builtins" object and by using a
1323 // real register for the function.
1324 mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1325 mov(edx, FieldOperand(edx, GlobalObject::kBuiltinsOffset));
1326 int builtins_offset =
1327 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1328 mov(edi, FieldOperand(edx, builtins_offset));
1329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001331 return Builtins::GetCode(id, resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332}
1333
1334
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001335void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1336 if (context_chain_length > 0) {
1337 // Move up the chain of contexts to the context containing the slot.
1338 mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX)));
1339 // Load the function context (which is the incoming, outer context).
1340 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1341 for (int i = 1; i < context_chain_length; i++) {
1342 mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1343 mov(dst, FieldOperand(dst, JSFunction::kContextOffset));
1344 }
1345 // The context may be an intermediate context, not a function context.
1346 mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1347 } else { // Slot is in the current function context.
1348 // The context may be an intermediate context, not a function context.
1349 mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1350 }
1351}
1352
1353
1354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355void MacroAssembler::Ret() {
1356 ret(0);
1357}
1358
1359
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001360void MacroAssembler::Drop(int stack_elements) {
1361 if (stack_elements > 0) {
1362 add(Operand(esp), Immediate(stack_elements * kPointerSize));
1363 }
1364}
1365
1366
1367void MacroAssembler::Move(Register dst, Handle<Object> value) {
1368 mov(dst, value);
1369}
1370
1371
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1373 if (FLAG_native_code_counters && counter->Enabled()) {
1374 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1375 }
1376}
1377
1378
1379void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1380 ASSERT(value > 0);
1381 if (FLAG_native_code_counters && counter->Enabled()) {
1382 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1383 if (value == 1) {
1384 inc(operand);
1385 } else {
1386 add(operand, Immediate(value));
1387 }
1388 }
1389}
1390
1391
1392void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1393 ASSERT(value > 0);
1394 if (FLAG_native_code_counters && counter->Enabled()) {
1395 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1396 if (value == 1) {
1397 dec(operand);
1398 } else {
1399 sub(operand, Immediate(value));
1400 }
1401 }
1402}
1403
1404
1405void MacroAssembler::Assert(Condition cc, const char* msg) {
1406 if (FLAG_debug_code) Check(cc, msg);
1407}
1408
1409
1410void MacroAssembler::Check(Condition cc, const char* msg) {
1411 Label L;
1412 j(cc, &L, taken);
1413 Abort(msg);
1414 // will not return here
1415 bind(&L);
1416}
1417
1418
1419void MacroAssembler::Abort(const char* msg) {
1420 // We want to pass the msg string like a smi to avoid GC
1421 // problems, however msg is not guaranteed to be aligned
1422 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001423 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 // from the real pointer as a smi.
1425 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1426 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1427 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1428#ifdef DEBUG
1429 if (msg != NULL) {
1430 RecordComment("Abort message: ");
1431 RecordComment(msg);
1432 }
1433#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001434 // Disable stub call restrictions to always allow calls to abort.
1435 set_allow_stub_calls(true);
1436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 push(eax);
1438 push(Immediate(p0));
1439 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1440 CallRuntime(Runtime::kAbort, 2);
1441 // will not return here
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001442 int3();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443}
1444
1445
1446CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001447 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001448 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 // The size is adjusted with kGap on order for the assembler to generate size
1450 // bytes of instructions without failing with buffer size constraints.
1451 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1452}
1453
1454
1455CodePatcher::~CodePatcher() {
1456 // Indicate that code has changed.
1457 CPU::FlushICache(address_, size_);
1458
1459 // Check that the code was patched as expected.
1460 ASSERT(masm_.pc_ == address_ + size_);
1461 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1462}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463
1464
1465} } // namespace v8::internal