blob: 010433e163045013615104e5ce3ab69751252411 [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
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000216#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217void MacroAssembler::SaveRegistersToMemory(RegList regs) {
218 ASSERT((regs & ~kJSCallerSaved) == 0);
219 // Copy the content of registers to memory location.
220 for (int i = 0; i < kNumJSCallerSaved; i++) {
221 int r = JSCallerSavedCode(i);
222 if ((regs & (1 << r)) != 0) {
223 Register reg = { r };
224 ExternalReference reg_addr =
225 ExternalReference(Debug_Address::Register(i));
226 mov(Operand::StaticVariable(reg_addr), reg);
227 }
228 }
229}
230
231
232void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
233 ASSERT((regs & ~kJSCallerSaved) == 0);
234 // Copy the content of memory location to registers.
235 for (int i = kNumJSCallerSaved; --i >= 0;) {
236 int r = JSCallerSavedCode(i);
237 if ((regs & (1 << r)) != 0) {
238 Register reg = { r };
239 ExternalReference reg_addr =
240 ExternalReference(Debug_Address::Register(i));
241 mov(reg, Operand::StaticVariable(reg_addr));
242 }
243 }
244}
245
246
247void MacroAssembler::PushRegistersFromMemory(RegList regs) {
248 ASSERT((regs & ~kJSCallerSaved) == 0);
249 // Push the content of the memory location to the stack.
250 for (int i = 0; i < kNumJSCallerSaved; i++) {
251 int r = JSCallerSavedCode(i);
252 if ((regs & (1 << r)) != 0) {
253 ExternalReference reg_addr =
254 ExternalReference(Debug_Address::Register(i));
255 push(Operand::StaticVariable(reg_addr));
256 }
257 }
258}
259
260
261void MacroAssembler::PopRegistersToMemory(RegList regs) {
262 ASSERT((regs & ~kJSCallerSaved) == 0);
263 // Pop the content from the stack to the memory location.
264 for (int i = kNumJSCallerSaved; --i >= 0;) {
265 int r = JSCallerSavedCode(i);
266 if ((regs & (1 << r)) != 0) {
267 ExternalReference reg_addr =
268 ExternalReference(Debug_Address::Register(i));
269 pop(Operand::StaticVariable(reg_addr));
270 }
271 }
272}
273
274
275void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
276 Register scratch,
277 RegList regs) {
278 ASSERT((regs & ~kJSCallerSaved) == 0);
279 // Copy the content of the stack to the memory location and adjust base.
280 for (int i = kNumJSCallerSaved; --i >= 0;) {
281 int r = JSCallerSavedCode(i);
282 if ((regs & (1 << r)) != 0) {
283 mov(scratch, Operand(base, 0));
284 ExternalReference reg_addr =
285 ExternalReference(Debug_Address::Register(i));
286 mov(Operand::StaticVariable(reg_addr), scratch);
287 lea(base, Operand(base, kPointerSize));
288 }
289 }
290}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000291#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292
293void MacroAssembler::Set(Register dst, const Immediate& x) {
294 if (x.is_zero()) {
295 xor_(dst, Operand(dst)); // shorter than mov
296 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000297 mov(dst, x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 }
299}
300
301
302void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
303 mov(dst, x);
304}
305
306
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307void MacroAssembler::CmpObjectType(Register heap_object,
308 InstanceType type,
309 Register map) {
310 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
311 CmpInstanceType(map, type);
312}
313
314
315void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
316 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
317 static_cast<int8_t>(type));
318}
319
320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321void MacroAssembler::FCmp() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000322 if (CpuFeatures::IsSupported(CMOV)) {
ager@chromium.org3811b432009-10-28 14:53:37 +0000323 fucomip();
324 ffree(0);
325 fincstp();
326 } else {
327 fucompp();
328 push(eax);
329 fnstsw_ax();
330 sahf();
331 pop(eax);
332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333}
334
335
ager@chromium.org7c537e22008-10-16 08:43:32 +0000336void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 push(ebp);
338 mov(ebp, Operand(esp));
339 push(esi);
340 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000341 push(Immediate(CodeObject()));
342 if (FLAG_debug_code) {
343 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
344 Check(not_equal, "code object not properly patched");
345 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346}
347
348
ager@chromium.org7c537e22008-10-16 08:43:32 +0000349void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 if (FLAG_debug_code) {
351 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
352 Immediate(Smi::FromInt(type)));
353 Check(equal, "stack frame types must match");
354 }
355 leave();
356}
357
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000358void MacroAssembler::EnterExitFramePrologue(ExitFrame::Mode mode) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000359 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000360 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000361 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
362 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
363 push(ebp);
364 mov(ebp, Operand(esp));
365
366 // Reserve room for entry stack pointer and push the debug marker.
367 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
368 push(Immediate(0)); // saved entry sp, patched before call
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000369 if (mode == ExitFrame::MODE_DEBUG) {
370 push(Immediate(0));
371 } else {
372 push(Immediate(CodeObject()));
373 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000374
375 // Save the frame pointer and the context in top.
376 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
377 ExternalReference context_address(Top::k_context_address);
378 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
379 mov(Operand::StaticVariable(context_address), esi);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000380}
ager@chromium.org236ad962008-09-25 09:45:57 +0000381
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000382void MacroAssembler::EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000383#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000384 // Save the state of all registers to the stack from the memory
385 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000386 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000387 // TODO(1243899): This should be symmetric to
388 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
389 // correct here, but computed for the other call. Very error
390 // prone! FIX THIS. Actually there are deeper problems with
391 // register saving than this asymmetry (see the bug report
392 // associated with this issue).
393 PushRegistersFromMemory(kJSCallerSaved);
394 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000395#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000396
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000397 // Reserve space for arguments.
398 sub(Operand(esp), Immediate(argc * kPointerSize));
ager@chromium.org236ad962008-09-25 09:45:57 +0000399
400 // Get the required frame alignment for the OS.
401 static const int kFrameAlignment = OS::ActivationFrameAlignment();
402 if (kFrameAlignment > 0) {
403 ASSERT(IsPowerOf2(kFrameAlignment));
404 and_(esp, -kFrameAlignment);
405 }
406
407 // Patch the saved entry sp.
408 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
409}
410
411
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000412void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
413 EnterExitFramePrologue(mode);
414
415 // Setup argc and argv in callee-saved registers.
416 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
417 mov(edi, Operand(eax));
418 lea(esi, Operand(ebp, eax, times_4, offset));
419
420 EnterExitFrameEpilogue(mode, 2);
421}
422
423
424void MacroAssembler::EnterApiExitFrame(ExitFrame::Mode mode,
425 int stack_space,
426 int argc) {
427 EnterExitFramePrologue(mode);
428
429 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
430 lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset));
431
432 EnterExitFrameEpilogue(mode, argc);
433}
434
435
436void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000437#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000438 // Restore the memory copy of the registers by digging them out from
439 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000440 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000441 // It's okay to clobber register ebx below because we don't need
442 // the function pointer after this.
443 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000444 int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000445 lea(ebx, Operand(ebp, kOffset));
446 CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved);
447 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000448#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000449
450 // Get the return address from the stack and restore the frame pointer.
451 mov(ecx, Operand(ebp, 1 * kPointerSize));
452 mov(ebp, Operand(ebp, 0 * kPointerSize));
453
454 // Pop the arguments and the receiver from the caller stack.
455 lea(esp, Operand(esi, 1 * kPointerSize));
456
457 // Restore current context from top and clear it in debug mode.
458 ExternalReference context_address(Top::k_context_address);
459 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000460#ifdef DEBUG
461 mov(Operand::StaticVariable(context_address), Immediate(0));
462#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000463
464 // Push the return address to get ready to return.
465 push(ecx);
466
467 // Clear the top frame.
468 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
469 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
470}
471
472
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473void MacroAssembler::PushTryHandler(CodeLocation try_location,
474 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000475 // Adjust this code if not the case.
476 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 // The pc (return address) is already on TOS.
478 if (try_location == IN_JAVASCRIPT) {
479 if (type == TRY_CATCH_HANDLER) {
480 push(Immediate(StackHandler::TRY_CATCH));
481 } else {
482 push(Immediate(StackHandler::TRY_FINALLY));
483 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 } else {
486 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000487 // The frame pointer does not point to a JS frame so we save NULL
488 // for ebp. We expect the code throwing an exception to check ebp
489 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000491 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000493 // Save the current handler as the next handler.
494 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
495 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
497}
498
499
500Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
501 JSObject* holder, Register holder_reg,
502 Register scratch,
503 Label* miss) {
504 // Make sure there's no overlap between scratch and the other
505 // registers.
506 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
507
508 // Keep track of the current object in register reg.
509 Register reg = object_reg;
510 int depth = 1;
511
512 // Check the maps in the prototype chain.
513 // Traverse the prototype chain from the object and do map checks.
514 while (object != holder) {
515 depth++;
516
517 // Only global objects and objects that do not require access
518 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000519 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
521 JSObject* prototype = JSObject::cast(object->GetPrototype());
522 if (Heap::InNewSpace(prototype)) {
523 // Get the map of the current object.
524 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
525 cmp(Operand(scratch), Immediate(Handle<Map>(object->map())));
526 // Branch on the result of the map check.
527 j(not_equal, miss, not_taken);
528 // Check access rights to the global object. This has to happen
529 // after the map check so that we know that the object is
530 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000531 if (object->IsJSGlobalProxy()) {
532 CheckAccessGlobalProxy(reg, scratch, miss);
533
534 // Restore scratch register to be the map of the object.
535 // We load the prototype from the map in the scratch register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
537 }
538 // The prototype is in new space; we cannot store a reference
539 // to it in the code. Load it from the map.
540 reg = holder_reg; // from now the object is in holder_reg
541 mov(reg, FieldOperand(scratch, Map::kPrototypeOffset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000542
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 } else {
544 // Check the map of the current object.
545 cmp(FieldOperand(reg, HeapObject::kMapOffset),
546 Immediate(Handle<Map>(object->map())));
547 // Branch on the result of the map check.
548 j(not_equal, miss, not_taken);
549 // Check access rights to the global object. This has to happen
550 // after the map check so that we know that the object is
551 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000552 if (object->IsJSGlobalProxy()) {
553 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 }
555 // The prototype is in old space; load it directly.
556 reg = holder_reg; // from now the object is in holder_reg
557 mov(reg, Handle<JSObject>(prototype));
558 }
559
560 // Go to the next object in the prototype chain.
561 object = prototype;
562 }
563
564 // Check the holder map.
565 cmp(FieldOperand(reg, HeapObject::kMapOffset),
566 Immediate(Handle<Map>(holder->map())));
567 j(not_equal, miss, not_taken);
568
569 // Log the check depth.
570 LOG(IntEvent("check-maps-depth", depth));
571
572 // Perform security check for access to the global object and return
573 // the holder register.
574 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000575 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
576 if (object->IsJSGlobalProxy()) {
577 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 }
579 return reg;
580}
581
582
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000583void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000584 Register scratch,
585 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000586 Label same_contexts;
587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 ASSERT(!holder_reg.is(scratch));
589
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000590 // Load current lexical context from the stack frame.
591 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
592
593 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 if (FLAG_debug_code) {
595 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000596 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000598 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
600 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000601 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
602
603 // Check the context is a global context.
604 if (FLAG_debug_code) {
605 push(scratch);
606 // Read the first word and compare to global_context_map.
607 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
608 cmp(scratch, Factory::global_context_map());
609 Check(equal, "JSGlobalObject::global_context should be a global context.");
610 pop(scratch);
611 }
612
613 // Check if both contexts are the same.
614 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
615 j(equal, &same_contexts, taken);
616
617 // Compare security tokens, save holder_reg on the stack so we can use it
618 // as a temporary register.
619 //
620 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
621 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 // Check that the security token in the calling global object is
623 // compatible with the security token in the receiving global
624 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000625 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
626
627 // Check the context is a global context.
628 if (FLAG_debug_code) {
629 cmp(holder_reg, Factory::null_value());
630 Check(not_equal, "JSGlobalProxy::context() should not be null.");
631
632 push(holder_reg);
633 // Read the first word and compare to global_context_map(),
634 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
635 cmp(holder_reg, Factory::global_context_map());
636 Check(equal, "JSGlobalObject::global_context should be a global context.");
637 pop(holder_reg);
638 }
639
640 int token_offset = Context::kHeaderSize +
641 Context::SECURITY_TOKEN_INDEX * kPointerSize;
642 mov(scratch, FieldOperand(scratch, token_offset));
643 cmp(scratch, FieldOperand(holder_reg, token_offset));
644 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000646
647 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648}
649
650
ager@chromium.orga1645e22009-09-09 19:27:10 +0000651void MacroAssembler::LoadAllocationTopHelper(Register result,
652 Register result_end,
653 Register scratch,
654 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000655 ExternalReference new_space_allocation_top =
656 ExternalReference::new_space_allocation_top_address();
657
658 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000659 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000660 // No use of scratch if allocation top is provided.
661 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000662#ifdef DEBUG
663 // Assert that result actually contains top on entry.
664 cmp(result, Operand::StaticVariable(new_space_allocation_top));
665 Check(equal, "Unexpected allocation top");
666#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000667 return;
668 }
669
670 // Move address of new object to result. Use scratch register if available.
671 if (scratch.is(no_reg)) {
672 mov(result, Operand::StaticVariable(new_space_allocation_top));
673 } else {
674 ASSERT(!scratch.is(result_end));
675 mov(Operand(scratch), Immediate(new_space_allocation_top));
676 mov(result, Operand(scratch, 0));
677 }
678}
679
680
681void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
682 Register scratch) {
683 ExternalReference new_space_allocation_top =
684 ExternalReference::new_space_allocation_top_address();
685
686 // Update new top. Use scratch if available.
687 if (scratch.is(no_reg)) {
688 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
689 } else {
690 mov(Operand(scratch, 0), result_end);
691 }
692}
693
ager@chromium.orga1645e22009-09-09 19:27:10 +0000694
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000695void MacroAssembler::AllocateInNewSpace(int object_size,
696 Register result,
697 Register result_end,
698 Register scratch,
699 Label* gc_required,
700 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000701 ASSERT(!result.is(result_end));
702
703 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000704 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000705
706 // Calculate new top and bail out if new space is exhausted.
707 ExternalReference new_space_allocation_limit =
708 ExternalReference::new_space_allocation_limit_address();
709 lea(result_end, Operand(result, object_size));
710 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
711 j(above, gc_required, not_taken);
712
713 // Update allocation top.
714 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000715
716 // Tag result if requested.
717 if ((flags & TAG_OBJECT) != 0) {
718 or_(Operand(result), Immediate(kHeapObjectTag));
719 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000720}
721
722
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000723void MacroAssembler::AllocateInNewSpace(int header_size,
724 ScaleFactor element_size,
725 Register element_count,
726 Register result,
727 Register result_end,
728 Register scratch,
729 Label* gc_required,
730 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000731 ASSERT(!result.is(result_end));
732
733 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000734 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000735
736 // Calculate new top and bail out if new space is exhausted.
737 ExternalReference new_space_allocation_limit =
738 ExternalReference::new_space_allocation_limit_address();
739 lea(result_end, Operand(result, element_count, element_size, header_size));
740 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
741 j(above, gc_required);
742
743 // Update allocation top.
744 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000745
746 // Tag result if requested.
747 if ((flags & TAG_OBJECT) != 0) {
748 or_(Operand(result), Immediate(kHeapObjectTag));
749 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000750}
751
752
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000753void MacroAssembler::AllocateInNewSpace(Register object_size,
754 Register result,
755 Register result_end,
756 Register scratch,
757 Label* gc_required,
758 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000759 ASSERT(!result.is(result_end));
760
761 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000762 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000763
764 // Calculate new top and bail out if new space is exhausted.
765 ExternalReference new_space_allocation_limit =
766 ExternalReference::new_space_allocation_limit_address();
767 if (!object_size.is(result_end)) {
768 mov(result_end, object_size);
769 }
770 add(result_end, Operand(result));
771 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
772 j(above, gc_required, not_taken);
773
774 // Update allocation top.
775 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000776
777 // Tag result if requested.
778 if ((flags & TAG_OBJECT) != 0) {
779 or_(Operand(result), Immediate(kHeapObjectTag));
780 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000781}
782
783
784void MacroAssembler::UndoAllocationInNewSpace(Register object) {
785 ExternalReference new_space_allocation_top =
786 ExternalReference::new_space_allocation_top_address();
787
788 // Make sure the object has no tag before resetting top.
789 and_(Operand(object), Immediate(~kHeapObjectTagMask));
790#ifdef DEBUG
791 cmp(object, Operand::StaticVariable(new_space_allocation_top));
792 Check(below, "Undo allocation of non allocated memory");
793#endif
794 mov(Operand::StaticVariable(new_space_allocation_top), object);
795}
796
797
ager@chromium.org3811b432009-10-28 14:53:37 +0000798void MacroAssembler::AllocateHeapNumber(Register result,
799 Register scratch1,
800 Register scratch2,
801 Label* gc_required) {
802 // Allocate heap number in new space.
803 AllocateInNewSpace(HeapNumber::kSize,
804 result,
805 scratch1,
806 scratch2,
807 gc_required,
808 TAG_OBJECT);
809
810 // Set the map.
811 mov(FieldOperand(result, HeapObject::kMapOffset),
812 Immediate(Factory::heap_number_map()));
813}
814
815
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000816void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
817 Register result,
818 Register op,
819 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000820 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000821 test(result, Operand(result));
822 ok.Branch(not_zero, taken);
823 test(op, Operand(op));
824 then_target->Branch(sign, not_taken);
825 ok.Bind();
826}
827
828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829void MacroAssembler::NegativeZeroTest(Register result,
830 Register op,
831 Label* then_label) {
832 Label ok;
833 test(result, Operand(result));
834 j(not_zero, &ok, taken);
835 test(op, Operand(op));
836 j(sign, then_label, not_taken);
837 bind(&ok);
838}
839
840
841void MacroAssembler::NegativeZeroTest(Register result,
842 Register op1,
843 Register op2,
844 Register scratch,
845 Label* then_label) {
846 Label ok;
847 test(result, Operand(result));
848 j(not_zero, &ok, taken);
849 mov(scratch, Operand(op1));
850 or_(scratch, Operand(op2));
851 j(sign, then_label, not_taken);
852 bind(&ok);
853}
854
855
ager@chromium.org7c537e22008-10-16 08:43:32 +0000856void MacroAssembler::TryGetFunctionPrototype(Register function,
857 Register result,
858 Register scratch,
859 Label* miss) {
860 // Check that the receiver isn't a smi.
861 test(function, Immediate(kSmiTagMask));
862 j(zero, miss, not_taken);
863
864 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000865 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000866 j(not_equal, miss, not_taken);
867
868 // Make sure that the function has an instance prototype.
869 Label non_instance;
870 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
871 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
872 j(not_zero, &non_instance, not_taken);
873
874 // Get the prototype or initial map from the function.
875 mov(result,
876 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
877
878 // If the prototype or initial map is the hole, don't return it and
879 // simply miss the cache instead. This will allow us to allocate a
880 // prototype object on-demand in the runtime system.
881 cmp(Operand(result), Immediate(Factory::the_hole_value()));
882 j(equal, miss, not_taken);
883
884 // If the function does not have an initial map, we're done.
885 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000886 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000887 j(not_equal, &done);
888
889 // Get the prototype from the initial map.
890 mov(result, FieldOperand(result, Map::kPrototypeOffset));
891 jmp(&done);
892
893 // Non-instance prototype: Fetch prototype from constructor field
894 // in initial map.
895 bind(&non_instance);
896 mov(result, FieldOperand(result, Map::kConstructorOffset));
897
898 // All done.
899 bind(&done);
900}
901
902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000904 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000905 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906}
907
908
909void MacroAssembler::StubReturn(int argc) {
910 ASSERT(argc >= 1 && generating_stub());
911 ret((argc - 1) * kPointerSize);
912}
913
914
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000915void MacroAssembler::IllegalOperation(int num_arguments) {
916 if (num_arguments > 0) {
917 add(Operand(esp), Immediate(num_arguments * kPointerSize));
918 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000919 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920}
921
922
923void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
924 CallRuntime(Runtime::FunctionForId(id), num_arguments);
925}
926
927
928void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000929 // If the expected number of arguments of the runtime function is
930 // constant, we check that the actual number of arguments match the
931 // expectation.
932 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000933 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 return;
935 }
936
mads.s.ager31e71382008-08-13 09:32:07 +0000937 Runtime::FunctionId function_id =
938 static_cast<Runtime::FunctionId>(f->stub_id);
939 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 CallStub(&stub);
941}
942
943
mads.s.ager31e71382008-08-13 09:32:07 +0000944void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000945 int num_arguments,
946 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +0000947 // TODO(1236192): Most runtime routines don't need the number of
948 // arguments passed in because it is constant. At some point we
949 // should remove this need and make the runtime routine entry code
950 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000951 Set(eax, Immediate(num_arguments));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000952 JumpToRuntime(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953}
954
955
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000956void MacroAssembler::PushHandleScope(Register scratch) {
957 // Push the number of extensions, smi-tagged so the gc will ignore it.
958 ExternalReference extensions_address =
959 ExternalReference::handle_scope_extensions_address();
960 mov(scratch, Operand::StaticVariable(extensions_address));
961 ASSERT_EQ(0, kSmiTag);
962 shl(scratch, kSmiTagSize);
963 push(scratch);
964 mov(Operand::StaticVariable(extensions_address), Immediate(0));
965 // Push next and limit pointers which will be wordsize aligned and
966 // hence automatically smi tagged.
967 ExternalReference next_address =
968 ExternalReference::handle_scope_next_address();
969 push(Operand::StaticVariable(next_address));
970 ExternalReference limit_address =
971 ExternalReference::handle_scope_limit_address();
972 push(Operand::StaticVariable(limit_address));
973}
974
975
976void MacroAssembler::PopHandleScope(Register saved, Register scratch) {
977 ExternalReference extensions_address =
978 ExternalReference::handle_scope_extensions_address();
979 Label write_back;
980 mov(scratch, Operand::StaticVariable(extensions_address));
981 cmp(Operand(scratch), Immediate(0));
982 j(equal, &write_back);
983 // Calling a runtime function messes with registers so we save and
984 // restore any one we're asked not to change
985 if (saved.is_valid()) push(saved);
986 CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0);
987 if (saved.is_valid()) pop(saved);
988
989 bind(&write_back);
990 ExternalReference limit_address =
991 ExternalReference::handle_scope_limit_address();
992 pop(Operand::StaticVariable(limit_address));
993 ExternalReference next_address =
994 ExternalReference::handle_scope_next_address();
995 pop(Operand::StaticVariable(next_address));
996 pop(scratch);
997 shr(scratch, kSmiTagSize);
998 mov(Operand::StaticVariable(extensions_address), scratch);
999}
1000
1001
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001002void MacroAssembler::JumpToRuntime(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001004 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001005 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001006 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007}
1008
1009
1010void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1011 const ParameterCount& actual,
1012 Handle<Code> code_constant,
1013 const Operand& code_operand,
1014 Label* done,
1015 InvokeFlag flag) {
1016 bool definitely_matches = false;
1017 Label invoke;
1018 if (expected.is_immediate()) {
1019 ASSERT(actual.is_immediate());
1020 if (expected.immediate() == actual.immediate()) {
1021 definitely_matches = true;
1022 } else {
1023 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001024 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1025 if (expected.immediate() == sentinel) {
1026 // Don't worry about adapting arguments for builtins that
1027 // don't want that done. Skip adaption code by making it look
1028 // like we have a match between expected and actual number of
1029 // arguments.
1030 definitely_matches = true;
1031 } else {
1032 mov(ebx, expected.immediate());
1033 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 }
1035 } else {
1036 if (actual.is_immediate()) {
1037 // Expected is in register, actual is immediate. This is the
1038 // case when we invoke function values without going through the
1039 // IC mechanism.
1040 cmp(expected.reg(), actual.immediate());
1041 j(equal, &invoke);
1042 ASSERT(expected.reg().is(ebx));
1043 mov(eax, actual.immediate());
1044 } else if (!expected.reg().is(actual.reg())) {
1045 // Both expected and actual are in (different) registers. This
1046 // is the case when we invoke functions using call and apply.
1047 cmp(expected.reg(), Operand(actual.reg()));
1048 j(equal, &invoke);
1049 ASSERT(actual.reg().is(eax));
1050 ASSERT(expected.reg().is(ebx));
1051 }
1052 }
1053
1054 if (!definitely_matches) {
1055 Handle<Code> adaptor =
1056 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1057 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001058 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
1060 } else if (!code_operand.is_reg(edx)) {
1061 mov(edx, code_operand);
1062 }
1063
1064 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001065 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 jmp(done);
1067 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00001068 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069 }
1070 bind(&invoke);
1071 }
1072}
1073
1074
1075void MacroAssembler::InvokeCode(const Operand& code,
1076 const ParameterCount& expected,
1077 const ParameterCount& actual,
1078 InvokeFlag flag) {
1079 Label done;
1080 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
1081 if (flag == CALL_FUNCTION) {
1082 call(code);
1083 } else {
1084 ASSERT(flag == JUMP_FUNCTION);
1085 jmp(code);
1086 }
1087 bind(&done);
1088}
1089
1090
1091void MacroAssembler::InvokeCode(Handle<Code> code,
1092 const ParameterCount& expected,
1093 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001094 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 InvokeFlag flag) {
1096 Label done;
1097 Operand dummy(eax);
1098 InvokePrologue(expected, actual, code, dummy, &done, flag);
1099 if (flag == CALL_FUNCTION) {
1100 call(code, rmode);
1101 } else {
1102 ASSERT(flag == JUMP_FUNCTION);
1103 jmp(code, rmode);
1104 }
1105 bind(&done);
1106}
1107
1108
1109void MacroAssembler::InvokeFunction(Register fun,
1110 const ParameterCount& actual,
1111 InvokeFlag flag) {
1112 ASSERT(fun.is(edi));
1113 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1114 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1115 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1116 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1117 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1118
1119 ParameterCount expected(ebx);
1120 InvokeCode(Operand(edx), expected, actual, flag);
1121}
1122
1123
1124void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
1125 bool resolved;
1126 Handle<Code> code = ResolveBuiltin(id, &resolved);
1127
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001128 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001129 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130
1131 // Rely on the assertion to check that the number of provided
1132 // arguments match the expected number of arguments. Fake a
1133 // parameter count to avoid emitting code to do the check.
1134 ParameterCount expected(0);
ager@chromium.org236ad962008-09-25 09:45:57 +00001135 InvokeCode(Handle<Code>(code), expected, expected,
1136 RelocInfo::CODE_TARGET, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137
1138 const char* name = Builtins::GetName(id);
1139 int argc = Builtins::GetArgumentsCount(id);
1140
1141 if (!resolved) {
1142 uint32_t flags =
1143 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001144 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1146 unresolved_.Add(entry);
1147 }
1148}
1149
1150
1151void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1152 bool resolved;
1153 Handle<Code> code = ResolveBuiltin(id, &resolved);
1154
1155 const char* name = Builtins::GetName(id);
1156 int argc = Builtins::GetArgumentsCount(id);
1157
1158 mov(Operand(target), Immediate(code));
1159 if (!resolved) {
1160 uint32_t flags =
1161 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001162 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1164 unresolved_.Add(entry);
1165 }
1166 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
1167}
1168
1169
1170Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1171 bool* resolved) {
1172 // Move the builtin function into the temporary function slot by
1173 // reading it from the builtins object. NOTE: We should be able to
1174 // reduce this to two instructions by putting the function table in
1175 // the global object instead of the "builtins" object and by using a
1176 // real register for the function.
1177 mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1178 mov(edx, FieldOperand(edx, GlobalObject::kBuiltinsOffset));
1179 int builtins_offset =
1180 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1181 mov(edi, FieldOperand(edx, builtins_offset));
1182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001184 return Builtins::GetCode(id, resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185}
1186
1187
1188void MacroAssembler::Ret() {
1189 ret(0);
1190}
1191
1192
1193void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1194 if (FLAG_native_code_counters && counter->Enabled()) {
1195 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1196 }
1197}
1198
1199
1200void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1201 ASSERT(value > 0);
1202 if (FLAG_native_code_counters && counter->Enabled()) {
1203 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1204 if (value == 1) {
1205 inc(operand);
1206 } else {
1207 add(operand, Immediate(value));
1208 }
1209 }
1210}
1211
1212
1213void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1214 ASSERT(value > 0);
1215 if (FLAG_native_code_counters && counter->Enabled()) {
1216 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1217 if (value == 1) {
1218 dec(operand);
1219 } else {
1220 sub(operand, Immediate(value));
1221 }
1222 }
1223}
1224
1225
1226void MacroAssembler::Assert(Condition cc, const char* msg) {
1227 if (FLAG_debug_code) Check(cc, msg);
1228}
1229
1230
1231void MacroAssembler::Check(Condition cc, const char* msg) {
1232 Label L;
1233 j(cc, &L, taken);
1234 Abort(msg);
1235 // will not return here
1236 bind(&L);
1237}
1238
1239
1240void MacroAssembler::Abort(const char* msg) {
1241 // We want to pass the msg string like a smi to avoid GC
1242 // problems, however msg is not guaranteed to be aligned
1243 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001244 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 // from the real pointer as a smi.
1246 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1247 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1248 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1249#ifdef DEBUG
1250 if (msg != NULL) {
1251 RecordComment("Abort message: ");
1252 RecordComment(msg);
1253 }
1254#endif
1255 push(eax);
1256 push(Immediate(p0));
1257 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1258 CallRuntime(Runtime::kAbort, 2);
1259 // will not return here
1260}
1261
1262
1263CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001264 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001265 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 // The size is adjusted with kGap on order for the assembler to generate size
1267 // bytes of instructions without failing with buffer size constraints.
1268 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1269}
1270
1271
1272CodePatcher::~CodePatcher() {
1273 // Indicate that code has changed.
1274 CPU::FlushICache(address_, size_);
1275
1276 // Check that the code was patched as expected.
1277 ASSERT(masm_.pc_ == address_ + size_);
1278 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1279}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
1281
1282} } // namespace v8::internal