blob: a3b214972736c7abd508469aa515e72550daecd1 [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.org4af710e2009-09-15 12:20:11 +0000322 fucompp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 push(eax);
324 fnstsw_ax();
325 sahf();
326 pop(eax);
327}
328
329
ager@chromium.org7c537e22008-10-16 08:43:32 +0000330void MacroAssembler::EnterFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 push(ebp);
332 mov(ebp, Operand(esp));
333 push(esi);
334 push(Immediate(Smi::FromInt(type)));
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000335 push(Immediate(CodeObject()));
336 if (FLAG_debug_code) {
337 cmp(Operand(esp, 0), Immediate(Factory::undefined_value()));
338 Check(not_equal, "code object not properly patched");
339 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340}
341
342
ager@chromium.org7c537e22008-10-16 08:43:32 +0000343void MacroAssembler::LeaveFrame(StackFrame::Type type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 if (FLAG_debug_code) {
345 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
346 Immediate(Smi::FromInt(type)));
347 Check(equal, "stack frame types must match");
348 }
349 leave();
350}
351
352
ager@chromium.org236ad962008-09-25 09:45:57 +0000353void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
354 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
355
356 // Setup the frame structure on the stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000357 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
ager@chromium.org236ad962008-09-25 09:45:57 +0000358 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
359 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
360 push(ebp);
361 mov(ebp, Operand(esp));
362
363 // Reserve room for entry stack pointer and push the debug marker.
364 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
365 push(Immediate(0)); // saved entry sp, patched before call
366 push(Immediate(type == StackFrame::EXIT_DEBUG ? 1 : 0));
367
368 // Save the frame pointer and the context in top.
369 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
370 ExternalReference context_address(Top::k_context_address);
371 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
372 mov(Operand::StaticVariable(context_address), esi);
373
374 // Setup argc and argv in callee-saved registers.
375 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
376 mov(edi, Operand(eax));
377 lea(esi, Operand(ebp, eax, times_4, offset));
378
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000379#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000380 // Save the state of all registers to the stack from the memory
381 // location. This is needed to allow nested break points.
382 if (type == StackFrame::EXIT_DEBUG) {
383 // TODO(1243899): This should be symmetric to
384 // CopyRegistersFromStackToMemory() but it isn't! esp is assumed
385 // correct here, but computed for the other call. Very error
386 // prone! FIX THIS. Actually there are deeper problems with
387 // register saving than this asymmetry (see the bug report
388 // associated with this issue).
389 PushRegistersFromMemory(kJSCallerSaved);
390 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000391#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000392
393 // Reserve space for two arguments: argc and argv.
394 sub(Operand(esp), Immediate(2 * kPointerSize));
395
396 // Get the required frame alignment for the OS.
397 static const int kFrameAlignment = OS::ActivationFrameAlignment();
398 if (kFrameAlignment > 0) {
399 ASSERT(IsPowerOf2(kFrameAlignment));
400 and_(esp, -kFrameAlignment);
401 }
402
403 // Patch the saved entry sp.
404 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
405}
406
407
408void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000409#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000410 // Restore the memory copy of the registers by digging them out from
411 // the stack. This is needed to allow nested break points.
412 if (type == StackFrame::EXIT_DEBUG) {
413 // It's okay to clobber register ebx below because we don't need
414 // the function pointer after this.
415 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
416 int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
417 lea(ebx, Operand(ebp, kOffset));
418 CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved);
419 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000420#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000421
422 // Get the return address from the stack and restore the frame pointer.
423 mov(ecx, Operand(ebp, 1 * kPointerSize));
424 mov(ebp, Operand(ebp, 0 * kPointerSize));
425
426 // Pop the arguments and the receiver from the caller stack.
427 lea(esp, Operand(esi, 1 * kPointerSize));
428
429 // Restore current context from top and clear it in debug mode.
430 ExternalReference context_address(Top::k_context_address);
431 mov(esi, Operand::StaticVariable(context_address));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000432#ifdef DEBUG
433 mov(Operand::StaticVariable(context_address), Immediate(0));
434#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000435
436 // Push the return address to get ready to return.
437 push(ecx);
438
439 // Clear the top frame.
440 ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address);
441 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
442}
443
444
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445void MacroAssembler::PushTryHandler(CodeLocation try_location,
446 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000447 // Adjust this code if not the case.
448 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 // The pc (return address) is already on TOS.
450 if (try_location == IN_JAVASCRIPT) {
451 if (type == TRY_CATCH_HANDLER) {
452 push(Immediate(StackHandler::TRY_CATCH));
453 } else {
454 push(Immediate(StackHandler::TRY_FINALLY));
455 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456 push(ebp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 } else {
458 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000459 // The frame pointer does not point to a JS frame so we save NULL
460 // for ebp. We expect the code throwing an exception to check ebp
461 // before dereferencing it to restore the context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 push(Immediate(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000463 push(Immediate(0)); // NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000465 // Save the current handler as the next handler.
466 push(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
467 // Link this handler as the new current one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp);
469}
470
471
472Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
473 JSObject* holder, Register holder_reg,
474 Register scratch,
475 Label* miss) {
476 // Make sure there's no overlap between scratch and the other
477 // registers.
478 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
479
480 // Keep track of the current object in register reg.
481 Register reg = object_reg;
482 int depth = 1;
483
484 // Check the maps in the prototype chain.
485 // Traverse the prototype chain from the object and do map checks.
486 while (object != holder) {
487 depth++;
488
489 // Only global objects and objects that do not require access
490 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000491 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
493 JSObject* prototype = JSObject::cast(object->GetPrototype());
494 if (Heap::InNewSpace(prototype)) {
495 // Get the map of the current object.
496 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
497 cmp(Operand(scratch), Immediate(Handle<Map>(object->map())));
498 // Branch on the result of the map check.
499 j(not_equal, miss, not_taken);
500 // Check access rights to the global object. This has to happen
501 // after the map check so that we know that the object is
502 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000503 if (object->IsJSGlobalProxy()) {
504 CheckAccessGlobalProxy(reg, scratch, miss);
505
506 // Restore scratch register to be the map of the object.
507 // We load the prototype from the map in the scratch register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
509 }
510 // The prototype is in new space; we cannot store a reference
511 // to it in the code. Load it from the map.
512 reg = holder_reg; // from now the object is in holder_reg
513 mov(reg, FieldOperand(scratch, Map::kPrototypeOffset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000514
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 } else {
516 // Check the map of the current object.
517 cmp(FieldOperand(reg, HeapObject::kMapOffset),
518 Immediate(Handle<Map>(object->map())));
519 // Branch on the result of the map check.
520 j(not_equal, miss, not_taken);
521 // Check access rights to the global object. This has to happen
522 // after the map check so that we know that the object is
523 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000524 if (object->IsJSGlobalProxy()) {
525 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 }
527 // The prototype is in old space; load it directly.
528 reg = holder_reg; // from now the object is in holder_reg
529 mov(reg, Handle<JSObject>(prototype));
530 }
531
532 // Go to the next object in the prototype chain.
533 object = prototype;
534 }
535
536 // Check the holder map.
537 cmp(FieldOperand(reg, HeapObject::kMapOffset),
538 Immediate(Handle<Map>(holder->map())));
539 j(not_equal, miss, not_taken);
540
541 // Log the check depth.
542 LOG(IntEvent("check-maps-depth", depth));
543
544 // Perform security check for access to the global object and return
545 // the holder register.
546 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000547 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
548 if (object->IsJSGlobalProxy()) {
549 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 }
551 return reg;
552}
553
554
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000555void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
ager@chromium.orge2902be2009-06-08 12:21:35 +0000556 Register scratch,
557 Label* miss) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000558 Label same_contexts;
559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 ASSERT(!holder_reg.is(scratch));
561
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000562 // Load current lexical context from the stack frame.
563 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
564
565 // When generating debug code, make sure the lexical context is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 if (FLAG_debug_code) {
567 cmp(Operand(scratch), Immediate(0));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000568 Check(not_equal, "we should not have an empty lexical context");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000570 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
572 mov(scratch, FieldOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000573 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
574
575 // Check the context is a global context.
576 if (FLAG_debug_code) {
577 push(scratch);
578 // Read the first word and compare to global_context_map.
579 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
580 cmp(scratch, Factory::global_context_map());
581 Check(equal, "JSGlobalObject::global_context should be a global context.");
582 pop(scratch);
583 }
584
585 // Check if both contexts are the same.
586 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
587 j(equal, &same_contexts, taken);
588
589 // Compare security tokens, save holder_reg on the stack so we can use it
590 // as a temporary register.
591 //
592 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
593 push(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 // Check that the security token in the calling global object is
595 // compatible with the security token in the receiving global
596 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000597 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
598
599 // Check the context is a global context.
600 if (FLAG_debug_code) {
601 cmp(holder_reg, Factory::null_value());
602 Check(not_equal, "JSGlobalProxy::context() should not be null.");
603
604 push(holder_reg);
605 // Read the first word and compare to global_context_map(),
606 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
607 cmp(holder_reg, Factory::global_context_map());
608 Check(equal, "JSGlobalObject::global_context should be a global context.");
609 pop(holder_reg);
610 }
611
612 int token_offset = Context::kHeaderSize +
613 Context::SECURITY_TOKEN_INDEX * kPointerSize;
614 mov(scratch, FieldOperand(scratch, token_offset));
615 cmp(scratch, FieldOperand(holder_reg, token_offset));
616 pop(holder_reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 j(not_equal, miss, not_taken);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000618
619 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620}
621
622
ager@chromium.orga1645e22009-09-09 19:27:10 +0000623void MacroAssembler::LoadAllocationTopHelper(Register result,
624 Register result_end,
625 Register scratch,
626 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000627 ExternalReference new_space_allocation_top =
628 ExternalReference::new_space_allocation_top_address();
629
630 // Just return if allocation top is already known.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000631 if ((flags & RESULT_CONTAINS_TOP) != 0) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000632 // No use of scratch if allocation top is provided.
633 ASSERT(scratch.is(no_reg));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000634#ifdef DEBUG
635 // Assert that result actually contains top on entry.
636 cmp(result, Operand::StaticVariable(new_space_allocation_top));
637 Check(equal, "Unexpected allocation top");
638#endif
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000639 return;
640 }
641
642 // Move address of new object to result. Use scratch register if available.
643 if (scratch.is(no_reg)) {
644 mov(result, Operand::StaticVariable(new_space_allocation_top));
645 } else {
646 ASSERT(!scratch.is(result_end));
647 mov(Operand(scratch), Immediate(new_space_allocation_top));
648 mov(result, Operand(scratch, 0));
649 }
650}
651
652
653void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
654 Register scratch) {
655 ExternalReference new_space_allocation_top =
656 ExternalReference::new_space_allocation_top_address();
657
658 // Update new top. Use scratch if available.
659 if (scratch.is(no_reg)) {
660 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
661 } else {
662 mov(Operand(scratch, 0), result_end);
663 }
664}
665
ager@chromium.orga1645e22009-09-09 19:27:10 +0000666
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000667void MacroAssembler::AllocateInNewSpace(int object_size,
668 Register result,
669 Register result_end,
670 Register scratch,
671 Label* gc_required,
672 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000673 ASSERT(!result.is(result_end));
674
675 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000676 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000677
678 // Calculate new top and bail out if new space is exhausted.
679 ExternalReference new_space_allocation_limit =
680 ExternalReference::new_space_allocation_limit_address();
681 lea(result_end, Operand(result, object_size));
682 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
683 j(above, gc_required, not_taken);
684
685 // Update allocation top.
686 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000687
688 // Tag result if requested.
689 if ((flags & TAG_OBJECT) != 0) {
690 or_(Operand(result), Immediate(kHeapObjectTag));
691 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000692}
693
694
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000695void MacroAssembler::AllocateInNewSpace(int header_size,
696 ScaleFactor element_size,
697 Register element_count,
698 Register result,
699 Register result_end,
700 Register scratch,
701 Label* gc_required,
702 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000703 ASSERT(!result.is(result_end));
704
705 // Load address of new object into result.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000706 LoadAllocationTopHelper(result, result_end, scratch, flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000707
708 // Calculate new top and bail out if new space is exhausted.
709 ExternalReference new_space_allocation_limit =
710 ExternalReference::new_space_allocation_limit_address();
711 lea(result_end, Operand(result, element_count, element_size, header_size));
712 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
713 j(above, gc_required);
714
715 // Update allocation top.
716 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000717
718 // Tag result if requested.
719 if ((flags & TAG_OBJECT) != 0) {
720 or_(Operand(result), Immediate(kHeapObjectTag));
721 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000722}
723
724
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000725void MacroAssembler::AllocateInNewSpace(Register object_size,
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 if (!object_size.is(result_end)) {
740 mov(result_end, object_size);
741 }
742 add(result_end, Operand(result));
743 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
744 j(above, gc_required, not_taken);
745
746 // Update allocation top.
747 UpdateAllocationTopHelper(result_end, scratch);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000748
749 // Tag result if requested.
750 if ((flags & TAG_OBJECT) != 0) {
751 or_(Operand(result), Immediate(kHeapObjectTag));
752 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000753}
754
755
756void MacroAssembler::UndoAllocationInNewSpace(Register object) {
757 ExternalReference new_space_allocation_top =
758 ExternalReference::new_space_allocation_top_address();
759
760 // Make sure the object has no tag before resetting top.
761 and_(Operand(object), Immediate(~kHeapObjectTagMask));
762#ifdef DEBUG
763 cmp(object, Operand::StaticVariable(new_space_allocation_top));
764 Check(below, "Undo allocation of non allocated memory");
765#endif
766 mov(Operand::StaticVariable(new_space_allocation_top), object);
767}
768
769
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000770void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
771 Register result,
772 Register op,
773 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000774 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000775 test(result, Operand(result));
776 ok.Branch(not_zero, taken);
777 test(op, Operand(op));
778 then_target->Branch(sign, not_taken);
779 ok.Bind();
780}
781
782
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783void MacroAssembler::NegativeZeroTest(Register result,
784 Register op,
785 Label* then_label) {
786 Label ok;
787 test(result, Operand(result));
788 j(not_zero, &ok, taken);
789 test(op, Operand(op));
790 j(sign, then_label, not_taken);
791 bind(&ok);
792}
793
794
795void MacroAssembler::NegativeZeroTest(Register result,
796 Register op1,
797 Register op2,
798 Register scratch,
799 Label* then_label) {
800 Label ok;
801 test(result, Operand(result));
802 j(not_zero, &ok, taken);
803 mov(scratch, Operand(op1));
804 or_(scratch, Operand(op2));
805 j(sign, then_label, not_taken);
806 bind(&ok);
807}
808
809
ager@chromium.org7c537e22008-10-16 08:43:32 +0000810void MacroAssembler::TryGetFunctionPrototype(Register function,
811 Register result,
812 Register scratch,
813 Label* miss) {
814 // Check that the receiver isn't a smi.
815 test(function, Immediate(kSmiTagMask));
816 j(zero, miss, not_taken);
817
818 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000819 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000820 j(not_equal, miss, not_taken);
821
822 // Make sure that the function has an instance prototype.
823 Label non_instance;
824 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
825 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
826 j(not_zero, &non_instance, not_taken);
827
828 // Get the prototype or initial map from the function.
829 mov(result,
830 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
831
832 // If the prototype or initial map is the hole, don't return it and
833 // simply miss the cache instead. This will allow us to allocate a
834 // prototype object on-demand in the runtime system.
835 cmp(Operand(result), Immediate(Factory::the_hole_value()));
836 j(equal, miss, not_taken);
837
838 // If the function does not have an initial map, we're done.
839 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000840 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000841 j(not_equal, &done);
842
843 // Get the prototype from the initial map.
844 mov(result, FieldOperand(result, Map::kPrototypeOffset));
845 jmp(&done);
846
847 // Non-instance prototype: Fetch prototype from constructor field
848 // in initial map.
849 bind(&non_instance);
850 mov(result, FieldOperand(result, Map::kConstructorOffset));
851
852 // All done.
853 bind(&done);
854}
855
856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000858 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000859 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860}
861
862
863void MacroAssembler::StubReturn(int argc) {
864 ASSERT(argc >= 1 && generating_stub());
865 ret((argc - 1) * kPointerSize);
866}
867
868
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000869void MacroAssembler::IllegalOperation(int num_arguments) {
870 if (num_arguments > 0) {
871 add(Operand(esp), Immediate(num_arguments * kPointerSize));
872 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000873 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874}
875
876
877void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
878 CallRuntime(Runtime::FunctionForId(id), num_arguments);
879}
880
881
882void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000883 // If the expected number of arguments of the runtime function is
884 // constant, we check that the actual number of arguments match the
885 // expectation.
886 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000887 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 return;
889 }
890
mads.s.ager31e71382008-08-13 09:32:07 +0000891 Runtime::FunctionId function_id =
892 static_cast<Runtime::FunctionId>(f->stub_id);
893 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 CallStub(&stub);
895}
896
897
mads.s.ager31e71382008-08-13 09:32:07 +0000898void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000899 int num_arguments,
900 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +0000901 // TODO(1236192): Most runtime routines don't need the number of
902 // arguments passed in because it is constant. At some point we
903 // should remove this need and make the runtime routine entry code
904 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000905 Set(eax, Immediate(num_arguments));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000906 JumpToRuntime(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907}
908
909
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000910void MacroAssembler::JumpToRuntime(const ExternalReference& ext) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000912 mov(ebx, Immediate(ext));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000913 CEntryStub ces(1);
ager@chromium.org236ad962008-09-25 09:45:57 +0000914 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915}
916
917
918void MacroAssembler::InvokePrologue(const ParameterCount& expected,
919 const ParameterCount& actual,
920 Handle<Code> code_constant,
921 const Operand& code_operand,
922 Label* done,
923 InvokeFlag flag) {
924 bool definitely_matches = false;
925 Label invoke;
926 if (expected.is_immediate()) {
927 ASSERT(actual.is_immediate());
928 if (expected.immediate() == actual.immediate()) {
929 definitely_matches = true;
930 } else {
931 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000932 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
933 if (expected.immediate() == sentinel) {
934 // Don't worry about adapting arguments for builtins that
935 // don't want that done. Skip adaption code by making it look
936 // like we have a match between expected and actual number of
937 // arguments.
938 definitely_matches = true;
939 } else {
940 mov(ebx, expected.immediate());
941 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 }
943 } else {
944 if (actual.is_immediate()) {
945 // Expected is in register, actual is immediate. This is the
946 // case when we invoke function values without going through the
947 // IC mechanism.
948 cmp(expected.reg(), actual.immediate());
949 j(equal, &invoke);
950 ASSERT(expected.reg().is(ebx));
951 mov(eax, actual.immediate());
952 } else if (!expected.reg().is(actual.reg())) {
953 // Both expected and actual are in (different) registers. This
954 // is the case when we invoke functions using call and apply.
955 cmp(expected.reg(), Operand(actual.reg()));
956 j(equal, &invoke);
957 ASSERT(actual.reg().is(eax));
958 ASSERT(expected.reg().is(ebx));
959 }
960 }
961
962 if (!definitely_matches) {
963 Handle<Code> adaptor =
964 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
965 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000966 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
968 } else if (!code_operand.is_reg(edx)) {
969 mov(edx, code_operand);
970 }
971
972 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000973 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 jmp(done);
975 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000976 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 }
978 bind(&invoke);
979 }
980}
981
982
983void MacroAssembler::InvokeCode(const Operand& code,
984 const ParameterCount& expected,
985 const ParameterCount& actual,
986 InvokeFlag flag) {
987 Label done;
988 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
989 if (flag == CALL_FUNCTION) {
990 call(code);
991 } else {
992 ASSERT(flag == JUMP_FUNCTION);
993 jmp(code);
994 }
995 bind(&done);
996}
997
998
999void MacroAssembler::InvokeCode(Handle<Code> code,
1000 const ParameterCount& expected,
1001 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +00001002 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 InvokeFlag flag) {
1004 Label done;
1005 Operand dummy(eax);
1006 InvokePrologue(expected, actual, code, dummy, &done, flag);
1007 if (flag == CALL_FUNCTION) {
1008 call(code, rmode);
1009 } else {
1010 ASSERT(flag == JUMP_FUNCTION);
1011 jmp(code, rmode);
1012 }
1013 bind(&done);
1014}
1015
1016
1017void MacroAssembler::InvokeFunction(Register fun,
1018 const ParameterCount& actual,
1019 InvokeFlag flag) {
1020 ASSERT(fun.is(edi));
1021 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1022 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1023 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1024 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1025 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1026
1027 ParameterCount expected(ebx);
1028 InvokeCode(Operand(edx), expected, actual, flag);
1029}
1030
1031
1032void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
1033 bool resolved;
1034 Handle<Code> code = ResolveBuiltin(id, &resolved);
1035
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001036 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001037 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038
1039 // Rely on the assertion to check that the number of provided
1040 // arguments match the expected number of arguments. Fake a
1041 // parameter count to avoid emitting code to do the check.
1042 ParameterCount expected(0);
ager@chromium.org236ad962008-09-25 09:45:57 +00001043 InvokeCode(Handle<Code>(code), expected, expected,
1044 RelocInfo::CODE_TARGET, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045
1046 const char* name = Builtins::GetName(id);
1047 int argc = Builtins::GetArgumentsCount(id);
1048
1049 if (!resolved) {
1050 uint32_t flags =
1051 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001052 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1054 unresolved_.Add(entry);
1055 }
1056}
1057
1058
1059void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1060 bool resolved;
1061 Handle<Code> code = ResolveBuiltin(id, &resolved);
1062
1063 const char* name = Builtins::GetName(id);
1064 int argc = Builtins::GetArgumentsCount(id);
1065
1066 mov(Operand(target), Immediate(code));
1067 if (!resolved) {
1068 uint32_t flags =
1069 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001070 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1072 unresolved_.Add(entry);
1073 }
1074 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
1075}
1076
1077
1078Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1079 bool* resolved) {
1080 // Move the builtin function into the temporary function slot by
1081 // reading it from the builtins object. NOTE: We should be able to
1082 // reduce this to two instructions by putting the function table in
1083 // the global object instead of the "builtins" object and by using a
1084 // real register for the function.
1085 mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1086 mov(edx, FieldOperand(edx, GlobalObject::kBuiltinsOffset));
1087 int builtins_offset =
1088 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1089 mov(edi, FieldOperand(edx, builtins_offset));
1090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001092 return Builtins::GetCode(id, resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093}
1094
1095
1096void MacroAssembler::Ret() {
1097 ret(0);
1098}
1099
1100
1101void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1102 if (FLAG_native_code_counters && counter->Enabled()) {
1103 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1104 }
1105}
1106
1107
1108void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1109 ASSERT(value > 0);
1110 if (FLAG_native_code_counters && counter->Enabled()) {
1111 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1112 if (value == 1) {
1113 inc(operand);
1114 } else {
1115 add(operand, Immediate(value));
1116 }
1117 }
1118}
1119
1120
1121void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1122 ASSERT(value > 0);
1123 if (FLAG_native_code_counters && counter->Enabled()) {
1124 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1125 if (value == 1) {
1126 dec(operand);
1127 } else {
1128 sub(operand, Immediate(value));
1129 }
1130 }
1131}
1132
1133
1134void MacroAssembler::Assert(Condition cc, const char* msg) {
1135 if (FLAG_debug_code) Check(cc, msg);
1136}
1137
1138
1139void MacroAssembler::Check(Condition cc, const char* msg) {
1140 Label L;
1141 j(cc, &L, taken);
1142 Abort(msg);
1143 // will not return here
1144 bind(&L);
1145}
1146
1147
1148void MacroAssembler::Abort(const char* msg) {
1149 // We want to pass the msg string like a smi to avoid GC
1150 // problems, however msg is not guaranteed to be aligned
1151 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001152 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 // from the real pointer as a smi.
1154 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1155 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1156 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1157#ifdef DEBUG
1158 if (msg != NULL) {
1159 RecordComment("Abort message: ");
1160 RecordComment(msg);
1161 }
1162#endif
1163 push(eax);
1164 push(Immediate(p0));
1165 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1166 CallRuntime(Runtime::kAbort, 2);
1167 // will not return here
1168}
1169
1170
1171CodePatcher::CodePatcher(byte* address, int size)
ager@chromium.orga1645e22009-09-09 19:27:10 +00001172 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001173 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 // The size is adjusted with kGap on order for the assembler to generate size
1175 // bytes of instructions without failing with buffer size constraints.
1176 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1177}
1178
1179
1180CodePatcher::~CodePatcher() {
1181 // Indicate that code has changed.
1182 CPU::FlushICache(address_, size_);
1183
1184 // Check that the code was patched as expected.
1185 ASSERT(masm_.pc_ == address_ + size_);
1186 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1187}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188
1189
1190} } // namespace v8::internal