blob: 754b74abefbed6e72019348d73a3e9700f775cb6 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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() {
322 fcompp();
323 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.org18ad94b2009-09-02 08:22:29 +0000623void MacroAssembler::LoadAllocationTopHelper(
624 Register result,
625 Register result_end,
626 Register scratch,
627 bool result_contains_top_on_entry) {
628 ExternalReference new_space_allocation_top =
629 ExternalReference::new_space_allocation_top_address();
630
631 // Just return if allocation top is already known.
632 if (result_contains_top_on_entry) {
633 // No use of scratch if allocation top is provided.
634 ASSERT(scratch.is(no_reg));
635 return;
636 }
637
638 // Move address of new object to result. Use scratch register if available.
639 if (scratch.is(no_reg)) {
640 mov(result, Operand::StaticVariable(new_space_allocation_top));
641 } else {
642 ASSERT(!scratch.is(result_end));
643 mov(Operand(scratch), Immediate(new_space_allocation_top));
644 mov(result, Operand(scratch, 0));
645 }
646}
647
648
649void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
650 Register scratch) {
651 ExternalReference new_space_allocation_top =
652 ExternalReference::new_space_allocation_top_address();
653
654 // Update new top. Use scratch if available.
655 if (scratch.is(no_reg)) {
656 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
657 } else {
658 mov(Operand(scratch, 0), result_end);
659 }
660}
661
662void MacroAssembler::AllocateObjectInNewSpace(
663 int object_size,
664 Register result,
665 Register result_end,
666 Register scratch,
667 Label* gc_required,
668 bool result_contains_top_on_entry) {
669 ASSERT(!result.is(result_end));
670
671 // Load address of new object into result.
672 LoadAllocationTopHelper(result,
673 result_end,
674 scratch,
675 result_contains_top_on_entry);
676
677 // Calculate new top and bail out if new space is exhausted.
678 ExternalReference new_space_allocation_limit =
679 ExternalReference::new_space_allocation_limit_address();
680 lea(result_end, Operand(result, object_size));
681 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
682 j(above, gc_required, not_taken);
683
684 // Update allocation top.
685 UpdateAllocationTopHelper(result_end, scratch);
686}
687
688
689void MacroAssembler::AllocateObjectInNewSpace(
690 int header_size,
691 ScaleFactor element_size,
692 Register element_count,
693 Register result,
694 Register result_end,
695 Register scratch,
696 Label* gc_required,
697 bool result_contains_top_on_entry) {
698 ASSERT(!result.is(result_end));
699
700 // Load address of new object into result.
701 LoadAllocationTopHelper(result,
702 result_end,
703 scratch,
704 result_contains_top_on_entry);
705
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, element_count, element_size, header_size));
710 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
711 j(above, gc_required);
712
713 // Update allocation top.
714 UpdateAllocationTopHelper(result_end, scratch);
715}
716
717
718void MacroAssembler::AllocateObjectInNewSpace(
719 Register object_size,
720 Register result,
721 Register result_end,
722 Register scratch,
723 Label* gc_required,
724 bool result_contains_top_on_entry) {
725 ASSERT(!result.is(result_end));
726
727 // Load address of new object into result.
728 LoadAllocationTopHelper(result,
729 result_end,
730 scratch,
731 result_contains_top_on_entry);
732
733
734 // Calculate new top and bail out if new space is exhausted.
735 ExternalReference new_space_allocation_limit =
736 ExternalReference::new_space_allocation_limit_address();
737 if (!object_size.is(result_end)) {
738 mov(result_end, object_size);
739 }
740 add(result_end, Operand(result));
741 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
742 j(above, gc_required, not_taken);
743
744 // Update allocation top.
745 UpdateAllocationTopHelper(result_end, scratch);
746}
747
748
749void MacroAssembler::UndoAllocationInNewSpace(Register object) {
750 ExternalReference new_space_allocation_top =
751 ExternalReference::new_space_allocation_top_address();
752
753 // Make sure the object has no tag before resetting top.
754 and_(Operand(object), Immediate(~kHeapObjectTagMask));
755#ifdef DEBUG
756 cmp(object, Operand::StaticVariable(new_space_allocation_top));
757 Check(below, "Undo allocation of non allocated memory");
758#endif
759 mov(Operand::StaticVariable(new_space_allocation_top), object);
760}
761
762
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000763void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
764 Register result,
765 Register op,
766 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000767 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000768 test(result, Operand(result));
769 ok.Branch(not_zero, taken);
770 test(op, Operand(op));
771 then_target->Branch(sign, not_taken);
772 ok.Bind();
773}
774
775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776void MacroAssembler::NegativeZeroTest(Register result,
777 Register op,
778 Label* then_label) {
779 Label ok;
780 test(result, Operand(result));
781 j(not_zero, &ok, taken);
782 test(op, Operand(op));
783 j(sign, then_label, not_taken);
784 bind(&ok);
785}
786
787
788void MacroAssembler::NegativeZeroTest(Register result,
789 Register op1,
790 Register op2,
791 Register scratch,
792 Label* then_label) {
793 Label ok;
794 test(result, Operand(result));
795 j(not_zero, &ok, taken);
796 mov(scratch, Operand(op1));
797 or_(scratch, Operand(op2));
798 j(sign, then_label, not_taken);
799 bind(&ok);
800}
801
802
ager@chromium.org7c537e22008-10-16 08:43:32 +0000803void MacroAssembler::TryGetFunctionPrototype(Register function,
804 Register result,
805 Register scratch,
806 Label* miss) {
807 // Check that the receiver isn't a smi.
808 test(function, Immediate(kSmiTagMask));
809 j(zero, miss, not_taken);
810
811 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000812 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000813 j(not_equal, miss, not_taken);
814
815 // Make sure that the function has an instance prototype.
816 Label non_instance;
817 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
818 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
819 j(not_zero, &non_instance, not_taken);
820
821 // Get the prototype or initial map from the function.
822 mov(result,
823 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
824
825 // If the prototype or initial map is the hole, don't return it and
826 // simply miss the cache instead. This will allow us to allocate a
827 // prototype object on-demand in the runtime system.
828 cmp(Operand(result), Immediate(Factory::the_hole_value()));
829 j(equal, miss, not_taken);
830
831 // If the function does not have an initial map, we're done.
832 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000833 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000834 j(not_equal, &done);
835
836 // Get the prototype from the initial map.
837 mov(result, FieldOperand(result, Map::kPrototypeOffset));
838 jmp(&done);
839
840 // Non-instance prototype: Fetch prototype from constructor field
841 // in initial map.
842 bind(&non_instance);
843 mov(result, FieldOperand(result, Map::kConstructorOffset));
844
845 // All done.
846 bind(&done);
847}
848
849
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000851 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000852 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853}
854
855
856void MacroAssembler::StubReturn(int argc) {
857 ASSERT(argc >= 1 && generating_stub());
858 ret((argc - 1) * kPointerSize);
859}
860
861
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000862void MacroAssembler::IllegalOperation(int num_arguments) {
863 if (num_arguments > 0) {
864 add(Operand(esp), Immediate(num_arguments * kPointerSize));
865 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000866 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867}
868
869
870void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
871 CallRuntime(Runtime::FunctionForId(id), num_arguments);
872}
873
874
875void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000876 // If the expected number of arguments of the runtime function is
877 // constant, we check that the actual number of arguments match the
878 // expectation.
879 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000880 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 return;
882 }
883
mads.s.ager31e71382008-08-13 09:32:07 +0000884 Runtime::FunctionId function_id =
885 static_cast<Runtime::FunctionId>(f->stub_id);
886 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 CallStub(&stub);
888}
889
890
mads.s.ager31e71382008-08-13 09:32:07 +0000891void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
892 int num_arguments) {
893 // TODO(1236192): Most runtime routines don't need the number of
894 // arguments passed in because it is constant. At some point we
895 // should remove this need and make the runtime routine entry code
896 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000897 Set(eax, Immediate(num_arguments));
mads.s.ager31e71382008-08-13 09:32:07 +0000898 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899}
900
901
902void MacroAssembler::JumpToBuiltin(const ExternalReference& ext) {
903 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000904 mov(ebx, Immediate(ext));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 CEntryStub ces;
ager@chromium.org236ad962008-09-25 09:45:57 +0000906 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907}
908
909
910void MacroAssembler::InvokePrologue(const ParameterCount& expected,
911 const ParameterCount& actual,
912 Handle<Code> code_constant,
913 const Operand& code_operand,
914 Label* done,
915 InvokeFlag flag) {
916 bool definitely_matches = false;
917 Label invoke;
918 if (expected.is_immediate()) {
919 ASSERT(actual.is_immediate());
920 if (expected.immediate() == actual.immediate()) {
921 definitely_matches = true;
922 } else {
923 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000924 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
925 if (expected.immediate() == sentinel) {
926 // Don't worry about adapting arguments for builtins that
927 // don't want that done. Skip adaption code by making it look
928 // like we have a match between expected and actual number of
929 // arguments.
930 definitely_matches = true;
931 } else {
932 mov(ebx, expected.immediate());
933 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 }
935 } else {
936 if (actual.is_immediate()) {
937 // Expected is in register, actual is immediate. This is the
938 // case when we invoke function values without going through the
939 // IC mechanism.
940 cmp(expected.reg(), actual.immediate());
941 j(equal, &invoke);
942 ASSERT(expected.reg().is(ebx));
943 mov(eax, actual.immediate());
944 } else if (!expected.reg().is(actual.reg())) {
945 // Both expected and actual are in (different) registers. This
946 // is the case when we invoke functions using call and apply.
947 cmp(expected.reg(), Operand(actual.reg()));
948 j(equal, &invoke);
949 ASSERT(actual.reg().is(eax));
950 ASSERT(expected.reg().is(ebx));
951 }
952 }
953
954 if (!definitely_matches) {
955 Handle<Code> adaptor =
956 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
957 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000958 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
960 } else if (!code_operand.is_reg(edx)) {
961 mov(edx, code_operand);
962 }
963
964 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000965 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 jmp(done);
967 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000968 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 }
970 bind(&invoke);
971 }
972}
973
974
975void MacroAssembler::InvokeCode(const Operand& code,
976 const ParameterCount& expected,
977 const ParameterCount& actual,
978 InvokeFlag flag) {
979 Label done;
980 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
981 if (flag == CALL_FUNCTION) {
982 call(code);
983 } else {
984 ASSERT(flag == JUMP_FUNCTION);
985 jmp(code);
986 }
987 bind(&done);
988}
989
990
991void MacroAssembler::InvokeCode(Handle<Code> code,
992 const ParameterCount& expected,
993 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000994 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 InvokeFlag flag) {
996 Label done;
997 Operand dummy(eax);
998 InvokePrologue(expected, actual, code, dummy, &done, flag);
999 if (flag == CALL_FUNCTION) {
1000 call(code, rmode);
1001 } else {
1002 ASSERT(flag == JUMP_FUNCTION);
1003 jmp(code, rmode);
1004 }
1005 bind(&done);
1006}
1007
1008
1009void MacroAssembler::InvokeFunction(Register fun,
1010 const ParameterCount& actual,
1011 InvokeFlag flag) {
1012 ASSERT(fun.is(edi));
1013 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1014 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1015 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1016 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
1017 lea(edx, FieldOperand(edx, Code::kHeaderSize));
1018
1019 ParameterCount expected(ebx);
1020 InvokeCode(Operand(edx), expected, actual, flag);
1021}
1022
1023
1024void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
1025 bool resolved;
1026 Handle<Code> code = ResolveBuiltin(id, &resolved);
1027
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +00001029 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030
1031 // Rely on the assertion to check that the number of provided
1032 // arguments match the expected number of arguments. Fake a
1033 // parameter count to avoid emitting code to do the check.
1034 ParameterCount expected(0);
ager@chromium.org236ad962008-09-25 09:45:57 +00001035 InvokeCode(Handle<Code>(code), expected, expected,
1036 RelocInfo::CODE_TARGET, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037
1038 const char* name = Builtins::GetName(id);
1039 int argc = Builtins::GetArgumentsCount(id);
1040
1041 if (!resolved) {
1042 uint32_t flags =
1043 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001044 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
1045 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1047 unresolved_.Add(entry);
1048 }
1049}
1050
1051
1052void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1053 bool resolved;
1054 Handle<Code> code = ResolveBuiltin(id, &resolved);
1055
1056 const char* name = Builtins::GetName(id);
1057 int argc = Builtins::GetArgumentsCount(id);
1058
1059 mov(Operand(target), Immediate(code));
1060 if (!resolved) {
1061 uint32_t flags =
1062 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001063 Bootstrapper::FixupFlagsIsPCRelative::encode(false) |
1064 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
1066 unresolved_.Add(entry);
1067 }
1068 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
1069}
1070
1071
1072Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1073 bool* resolved) {
1074 // Move the builtin function into the temporary function slot by
1075 // reading it from the builtins object. NOTE: We should be able to
1076 // reduce this to two instructions by putting the function table in
1077 // the global object instead of the "builtins" object and by using a
1078 // real register for the function.
1079 mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1080 mov(edx, FieldOperand(edx, GlobalObject::kBuiltinsOffset));
1081 int builtins_offset =
1082 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1083 mov(edi, FieldOperand(edx, builtins_offset));
1084
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001086 return Builtins::GetCode(id, resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087}
1088
1089
1090void MacroAssembler::Ret() {
1091 ret(0);
1092}
1093
1094
1095void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
1096 if (FLAG_native_code_counters && counter->Enabled()) {
1097 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
1098 }
1099}
1100
1101
1102void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
1103 ASSERT(value > 0);
1104 if (FLAG_native_code_counters && counter->Enabled()) {
1105 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1106 if (value == 1) {
1107 inc(operand);
1108 } else {
1109 add(operand, Immediate(value));
1110 }
1111 }
1112}
1113
1114
1115void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
1116 ASSERT(value > 0);
1117 if (FLAG_native_code_counters && counter->Enabled()) {
1118 Operand operand = Operand::StaticVariable(ExternalReference(counter));
1119 if (value == 1) {
1120 dec(operand);
1121 } else {
1122 sub(operand, Immediate(value));
1123 }
1124 }
1125}
1126
1127
1128void MacroAssembler::Assert(Condition cc, const char* msg) {
1129 if (FLAG_debug_code) Check(cc, msg);
1130}
1131
1132
1133void MacroAssembler::Check(Condition cc, const char* msg) {
1134 Label L;
1135 j(cc, &L, taken);
1136 Abort(msg);
1137 // will not return here
1138 bind(&L);
1139}
1140
1141
1142void MacroAssembler::Abort(const char* msg) {
1143 // We want to pass the msg string like a smi to avoid GC
1144 // problems, however msg is not guaranteed to be aligned
1145 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001146 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 // from the real pointer as a smi.
1148 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1149 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1150 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1151#ifdef DEBUG
1152 if (msg != NULL) {
1153 RecordComment("Abort message: ");
1154 RecordComment(msg);
1155 }
1156#endif
1157 push(eax);
1158 push(Immediate(p0));
1159 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1160 CallRuntime(Runtime::kAbort, 2);
1161 // will not return here
1162}
1163
1164
1165CodePatcher::CodePatcher(byte* address, int size)
1166 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001167 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 // The size is adjusted with kGap on order for the assembler to generate size
1169 // bytes of instructions without failing with buffer size constraints.
1170 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1171}
1172
1173
1174CodePatcher::~CodePatcher() {
1175 // Indicate that code has changed.
1176 CPU::FlushICache(address_, size_);
1177
1178 // Check that the code was patched as expected.
1179 ASSERT(masm_.pc_ == address_ + size_);
1180 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1181}
1182
1183
1184} } // namespace v8::internal