blob: 7782aa95c6eb6878d941642b17c3757f0faeeee6 [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
85 // extra RSet to 'object', so that addressing the bit using 'pointer_offset'
86 // hits the extra RSet words.
87 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
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000623void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen,
624 Register result,
625 Register op,
626 JumpTarget* then_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000627 JumpTarget ok;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000628 test(result, Operand(result));
629 ok.Branch(not_zero, taken);
630 test(op, Operand(op));
631 then_target->Branch(sign, not_taken);
632 ok.Bind();
633}
634
635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636void MacroAssembler::NegativeZeroTest(Register result,
637 Register op,
638 Label* then_label) {
639 Label ok;
640 test(result, Operand(result));
641 j(not_zero, &ok, taken);
642 test(op, Operand(op));
643 j(sign, then_label, not_taken);
644 bind(&ok);
645}
646
647
648void MacroAssembler::NegativeZeroTest(Register result,
649 Register op1,
650 Register op2,
651 Register scratch,
652 Label* then_label) {
653 Label ok;
654 test(result, Operand(result));
655 j(not_zero, &ok, taken);
656 mov(scratch, Operand(op1));
657 or_(scratch, Operand(op2));
658 j(sign, then_label, not_taken);
659 bind(&ok);
660}
661
662
ager@chromium.org7c537e22008-10-16 08:43:32 +0000663void MacroAssembler::TryGetFunctionPrototype(Register function,
664 Register result,
665 Register scratch,
666 Label* miss) {
667 // Check that the receiver isn't a smi.
668 test(function, Immediate(kSmiTagMask));
669 j(zero, miss, not_taken);
670
671 // Check that the function really is a function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000672 CmpObjectType(function, JS_FUNCTION_TYPE, result);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000673 j(not_equal, miss, not_taken);
674
675 // Make sure that the function has an instance prototype.
676 Label non_instance;
677 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
678 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
679 j(not_zero, &non_instance, not_taken);
680
681 // Get the prototype or initial map from the function.
682 mov(result,
683 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
684
685 // If the prototype or initial map is the hole, don't return it and
686 // simply miss the cache instead. This will allow us to allocate a
687 // prototype object on-demand in the runtime system.
688 cmp(Operand(result), Immediate(Factory::the_hole_value()));
689 j(equal, miss, not_taken);
690
691 // If the function does not have an initial map, we're done.
692 Label done;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000693 CmpObjectType(result, MAP_TYPE, scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000694 j(not_equal, &done);
695
696 // Get the prototype from the initial map.
697 mov(result, FieldOperand(result, Map::kPrototypeOffset));
698 jmp(&done);
699
700 // Non-instance prototype: Fetch prototype from constructor field
701 // in initial map.
702 bind(&non_instance);
703 mov(result, FieldOperand(result, Map::kConstructorOffset));
704
705 // All done.
706 bind(&done);
707}
708
709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710void MacroAssembler::CallStub(CodeStub* stub) {
kasper.lund7276f142008-07-30 08:49:36 +0000711 ASSERT(allow_stub_calls()); // calls are not allowed in some stubs
ager@chromium.org236ad962008-09-25 09:45:57 +0000712 call(stub->GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713}
714
715
716void MacroAssembler::StubReturn(int argc) {
717 ASSERT(argc >= 1 && generating_stub());
718 ret((argc - 1) * kPointerSize);
719}
720
721
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000722void MacroAssembler::IllegalOperation(int num_arguments) {
723 if (num_arguments > 0) {
724 add(Operand(esp), Immediate(num_arguments * kPointerSize));
725 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000726 mov(eax, Immediate(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727}
728
729
730void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
731 CallRuntime(Runtime::FunctionForId(id), num_arguments);
732}
733
734
735void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000736 // If the expected number of arguments of the runtime function is
737 // constant, we check that the actual number of arguments match the
738 // expectation.
739 if (f->nargs >= 0 && f->nargs != num_arguments) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000740 IllegalOperation(num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 return;
742 }
743
mads.s.ager31e71382008-08-13 09:32:07 +0000744 Runtime::FunctionId function_id =
745 static_cast<Runtime::FunctionId>(f->stub_id);
746 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 CallStub(&stub);
748}
749
750
mads.s.ager31e71382008-08-13 09:32:07 +0000751void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
752 int num_arguments) {
753 // TODO(1236192): Most runtime routines don't need the number of
754 // arguments passed in because it is constant. At some point we
755 // should remove this need and make the runtime routine entry code
756 // smarter.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000757 Set(eax, Immediate(num_arguments));
mads.s.ager31e71382008-08-13 09:32:07 +0000758 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759}
760
761
762void MacroAssembler::JumpToBuiltin(const ExternalReference& ext) {
763 // Set the entry point and jump to the C entry runtime stub.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000764 mov(ebx, Immediate(ext));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 CEntryStub ces;
ager@chromium.org236ad962008-09-25 09:45:57 +0000766 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767}
768
769
770void MacroAssembler::InvokePrologue(const ParameterCount& expected,
771 const ParameterCount& actual,
772 Handle<Code> code_constant,
773 const Operand& code_operand,
774 Label* done,
775 InvokeFlag flag) {
776 bool definitely_matches = false;
777 Label invoke;
778 if (expected.is_immediate()) {
779 ASSERT(actual.is_immediate());
780 if (expected.immediate() == actual.immediate()) {
781 definitely_matches = true;
782 } else {
783 mov(eax, actual.immediate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000784 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
785 if (expected.immediate() == sentinel) {
786 // Don't worry about adapting arguments for builtins that
787 // don't want that done. Skip adaption code by making it look
788 // like we have a match between expected and actual number of
789 // arguments.
790 definitely_matches = true;
791 } else {
792 mov(ebx, expected.immediate());
793 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 }
795 } else {
796 if (actual.is_immediate()) {
797 // Expected is in register, actual is immediate. This is the
798 // case when we invoke function values without going through the
799 // IC mechanism.
800 cmp(expected.reg(), actual.immediate());
801 j(equal, &invoke);
802 ASSERT(expected.reg().is(ebx));
803 mov(eax, actual.immediate());
804 } else if (!expected.reg().is(actual.reg())) {
805 // Both expected and actual are in (different) registers. This
806 // is the case when we invoke functions using call and apply.
807 cmp(expected.reg(), Operand(actual.reg()));
808 j(equal, &invoke);
809 ASSERT(actual.reg().is(eax));
810 ASSERT(expected.reg().is(ebx));
811 }
812 }
813
814 if (!definitely_matches) {
815 Handle<Code> adaptor =
816 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
817 if (!code_constant.is_null()) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000818 mov(edx, Immediate(code_constant));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
820 } else if (!code_operand.is_reg(edx)) {
821 mov(edx, code_operand);
822 }
823
824 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000825 call(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 jmp(done);
827 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000828 jmp(adaptor, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 }
830 bind(&invoke);
831 }
832}
833
834
835void MacroAssembler::InvokeCode(const Operand& code,
836 const ParameterCount& expected,
837 const ParameterCount& actual,
838 InvokeFlag flag) {
839 Label done;
840 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
841 if (flag == CALL_FUNCTION) {
842 call(code);
843 } else {
844 ASSERT(flag == JUMP_FUNCTION);
845 jmp(code);
846 }
847 bind(&done);
848}
849
850
851void MacroAssembler::InvokeCode(Handle<Code> code,
852 const ParameterCount& expected,
853 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000854 RelocInfo::Mode rmode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 InvokeFlag flag) {
856 Label done;
857 Operand dummy(eax);
858 InvokePrologue(expected, actual, code, dummy, &done, flag);
859 if (flag == CALL_FUNCTION) {
860 call(code, rmode);
861 } else {
862 ASSERT(flag == JUMP_FUNCTION);
863 jmp(code, rmode);
864 }
865 bind(&done);
866}
867
868
869void MacroAssembler::InvokeFunction(Register fun,
870 const ParameterCount& actual,
871 InvokeFlag flag) {
872 ASSERT(fun.is(edi));
873 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
874 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
875 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
876 mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
877 lea(edx, FieldOperand(edx, Code::kHeaderSize));
878
879 ParameterCount expected(ebx);
880 InvokeCode(Operand(edx), expected, actual, flag);
881}
882
883
884void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) {
885 bool resolved;
886 Handle<Code> code = ResolveBuiltin(id, &resolved);
887
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000888 // Calls are not allowed in some stubs.
kasper.lund7276f142008-07-30 08:49:36 +0000889 ASSERT(flag == JUMP_FUNCTION || allow_stub_calls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
891 // Rely on the assertion to check that the number of provided
892 // arguments match the expected number of arguments. Fake a
893 // parameter count to avoid emitting code to do the check.
894 ParameterCount expected(0);
ager@chromium.org236ad962008-09-25 09:45:57 +0000895 InvokeCode(Handle<Code>(code), expected, expected,
896 RelocInfo::CODE_TARGET, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897
898 const char* name = Builtins::GetName(id);
899 int argc = Builtins::GetArgumentsCount(id);
900
901 if (!resolved) {
902 uint32_t flags =
903 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000904 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
905 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
907 unresolved_.Add(entry);
908 }
909}
910
911
912void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
913 bool resolved;
914 Handle<Code> code = ResolveBuiltin(id, &resolved);
915
916 const char* name = Builtins::GetName(id);
917 int argc = Builtins::GetArgumentsCount(id);
918
919 mov(Operand(target), Immediate(code));
920 if (!resolved) {
921 uint32_t flags =
922 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000923 Bootstrapper::FixupFlagsIsPCRelative::encode(false) |
924 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 Unresolved entry = { pc_offset() - sizeof(int32_t), flags, name };
926 unresolved_.Add(entry);
927 }
928 add(Operand(target), Immediate(Code::kHeaderSize - kHeapObjectTag));
929}
930
931
932Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
933 bool* resolved) {
934 // Move the builtin function into the temporary function slot by
935 // reading it from the builtins object. NOTE: We should be able to
936 // reduce this to two instructions by putting the function table in
937 // the global object instead of the "builtins" object and by using a
938 // real register for the function.
939 mov(edx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
940 mov(edx, FieldOperand(edx, GlobalObject::kBuiltinsOffset));
941 int builtins_offset =
942 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
943 mov(edi, FieldOperand(edx, builtins_offset));
944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000946 return Builtins::GetCode(id, resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947}
948
949
950void MacroAssembler::Ret() {
951 ret(0);
952}
953
954
955void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
956 if (FLAG_native_code_counters && counter->Enabled()) {
957 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
958 }
959}
960
961
962void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
963 ASSERT(value > 0);
964 if (FLAG_native_code_counters && counter->Enabled()) {
965 Operand operand = Operand::StaticVariable(ExternalReference(counter));
966 if (value == 1) {
967 inc(operand);
968 } else {
969 add(operand, Immediate(value));
970 }
971 }
972}
973
974
975void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
976 ASSERT(value > 0);
977 if (FLAG_native_code_counters && counter->Enabled()) {
978 Operand operand = Operand::StaticVariable(ExternalReference(counter));
979 if (value == 1) {
980 dec(operand);
981 } else {
982 sub(operand, Immediate(value));
983 }
984 }
985}
986
987
988void MacroAssembler::Assert(Condition cc, const char* msg) {
989 if (FLAG_debug_code) Check(cc, msg);
990}
991
992
993void MacroAssembler::Check(Condition cc, const char* msg) {
994 Label L;
995 j(cc, &L, taken);
996 Abort(msg);
997 // will not return here
998 bind(&L);
999}
1000
1001
1002void MacroAssembler::Abort(const char* msg) {
1003 // We want to pass the msg string like a smi to avoid GC
1004 // problems, however msg is not guaranteed to be aligned
1005 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001006 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 // from the real pointer as a smi.
1008 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1009 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1010 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1011#ifdef DEBUG
1012 if (msg != NULL) {
1013 RecordComment("Abort message: ");
1014 RecordComment(msg);
1015 }
1016#endif
1017 push(eax);
1018 push(Immediate(p0));
1019 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
1020 CallRuntime(Runtime::kAbort, 2);
1021 // will not return here
1022}
1023
1024
1025CodePatcher::CodePatcher(byte* address, int size)
1026 : address_(address), size_(size), masm_(address, size + Assembler::kGap) {
ager@chromium.org32912102009-01-16 10:38:43 +00001027 // Create a new macro assembler pointing to the address of the code to patch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 // The size is adjusted with kGap on order for the assembler to generate size
1029 // bytes of instructions without failing with buffer size constraints.
1030 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1031}
1032
1033
1034CodePatcher::~CodePatcher() {
1035 // Indicate that code has changed.
1036 CPU::FlushICache(address_, size_);
1037
1038 // Check that the code was patched as expected.
1039 ASSERT(masm_.pc_ == address_ + size_);
1040 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1041}
1042
1043
1044} } // namespace v8::internal