blob: d97f04b71cc00ed78f39bab232b7c21e2233a982 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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
35namespace v8 {
36namespace internal {
37
38MacroAssembler::MacroAssembler(void* buffer, int size)
39 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000040 generating_stub_(false),
41 allow_stub_calls_(true),
42 code_object_(Heap::undefined_value()) {
43}
44
45
46// We always generate arm code, never thumb code, even if V8 is compiled to
47// thumb, so we require inter-working support
48#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
49#error "flag -mthumb-interwork missing"
50#endif
51
52
53// We do not support thumb inter-working with an arm architecture not supporting
54// the blx instruction (below v5t). If you know what CPU you are compiling for
55// you can use -march=armv7 or similar.
56#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
57# error "For thumb inter-working we require an architecture which supports blx"
58#endif
59
60
Steve Blocka7e24c12009-10-30 11:49:00 +000061// Using bx does not yield better code, so use it only when required
62#if defined(USE_THUMB_INTERWORK)
63#define USE_BX 1
64#endif
65
66
67void MacroAssembler::Jump(Register target, Condition cond) {
68#if USE_BX
69 bx(target, cond);
70#else
71 mov(pc, Operand(target), LeaveCC, cond);
72#endif
73}
74
75
76void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
77 Condition cond) {
78#if USE_BX
79 mov(ip, Operand(target, rmode), LeaveCC, cond);
80 bx(ip, cond);
81#else
82 mov(pc, Operand(target, rmode), LeaveCC, cond);
83#endif
84}
85
86
87void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
88 Condition cond) {
89 ASSERT(!RelocInfo::IsCodeTarget(rmode));
90 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
91}
92
93
94void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
95 Condition cond) {
96 ASSERT(RelocInfo::IsCodeTarget(rmode));
97 // 'code' is always generated ARM code, never THUMB code
98 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
99}
100
101
102void MacroAssembler::Call(Register target, Condition cond) {
103#if USE_BLX
104 blx(target, cond);
105#else
106 // set lr for return at current pc + 8
107 mov(lr, Operand(pc), LeaveCC, cond);
108 mov(pc, Operand(target), LeaveCC, cond);
109#endif
110}
111
112
113void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
114 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100115#if USE_BLX
116 // On ARMv5 and after the recommended call sequence is:
117 // ldr ip, [pc, #...]
118 // blx ip
119
120 // The two instructions (ldr and blx) could be separated by a constant
121 // pool and the code would still work. The issue comes from the
122 // patching code which expect the ldr to be just above the blx.
123 { BlockConstPoolScope block_const_pool(this);
124 // Statement positions are expected to be recorded when the target
125 // address is loaded. The mov method will automatically record
126 // positions when pc is the target, since this is not the case here
127 // we have to do it explicitly.
128 WriteRecordedPositions();
129
130 mov(ip, Operand(target, rmode), LeaveCC, cond);
131 blx(ip, cond);
132 }
133
134 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
135#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 // Set lr for return at current pc + 8.
137 mov(lr, Operand(pc), LeaveCC, cond);
138 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
139 mov(pc, Operand(target, rmode), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100140
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100142#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000143}
144
145
146void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
147 Condition cond) {
148 ASSERT(!RelocInfo::IsCodeTarget(rmode));
149 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
150}
151
152
153void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
154 Condition cond) {
155 ASSERT(RelocInfo::IsCodeTarget(rmode));
156 // 'code' is always generated ARM code, never THUMB code
157 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
158}
159
160
161void MacroAssembler::Ret(Condition cond) {
162#if USE_BX
163 bx(lr, cond);
164#else
165 mov(pc, Operand(lr), LeaveCC, cond);
166#endif
167}
168
169
Steve Blockd0582a62009-12-15 09:54:21 +0000170void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
171 LoadRoot(ip, Heap::kStackLimitRootIndex);
172 cmp(sp, Operand(ip));
173 b(lo, on_stack_overflow);
174}
175
176
Leon Clarkee46be812010-01-19 14:06:41 +0000177void MacroAssembler::Drop(int count, Condition cond) {
178 if (count > 0) {
179 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
180 }
181}
182
183
Steve Block6ded16b2010-05-10 14:33:55 +0100184void MacroAssembler::Swap(Register reg1, Register reg2, Register scratch) {
185 if (scratch.is(no_reg)) {
186 eor(reg1, reg1, Operand(reg2));
187 eor(reg2, reg2, Operand(reg1));
188 eor(reg1, reg1, Operand(reg2));
189 } else {
190 mov(scratch, reg1);
191 mov(reg1, reg2);
192 mov(reg2, scratch);
193 }
194}
195
196
Leon Clarkee46be812010-01-19 14:06:41 +0000197void MacroAssembler::Call(Label* target) {
198 bl(target);
199}
200
201
202void MacroAssembler::Move(Register dst, Handle<Object> value) {
203 mov(dst, Operand(value));
204}
Steve Blockd0582a62009-12-15 09:54:21 +0000205
206
Steve Block6ded16b2010-05-10 14:33:55 +0100207void MacroAssembler::Move(Register dst, Register src) {
208 if (!dst.is(src)) {
209 mov(dst, src);
210 }
211}
212
213
Steve Blocka7e24c12009-10-30 11:49:00 +0000214void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
215 // Empty the const pool.
216 CheckConstPool(true, true);
217 add(pc, pc, Operand(index,
218 LSL,
219 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
220 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
221 nop(); // Jump table alignment.
222 for (int i = 0; i < targets.length(); i++) {
223 b(targets[i]);
224 }
225}
226
227
228void MacroAssembler::LoadRoot(Register destination,
229 Heap::RootListIndex index,
230 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000231 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000232}
233
234
Steve Block6ded16b2010-05-10 14:33:55 +0100235void MacroAssembler::RecordWriteHelper(Register object,
236 Register offset,
237 Register scratch) {
238 if (FLAG_debug_code) {
239 // Check that the object is not in new space.
240 Label not_in_new_space;
241 InNewSpace(object, scratch, ne, &not_in_new_space);
242 Abort("new-space object passed to RecordWriteHelper");
243 bind(&not_in_new_space);
244 }
Leon Clarke4515c472010-02-03 11:58:03 +0000245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 // This is how much we shift the remembered set bit offset to get the
247 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
248 // shift right 5) and then multiply by kIntSize (4, shift left 2).
249 const int kRSetWordShift = 3;
250
Steve Block6ded16b2010-05-10 14:33:55 +0100251 Label fast;
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253 // Compute the bit offset in the remembered set.
254 // object: heap object pointer (with tag)
255 // offset: offset to store location from the object
256 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
257 and_(scratch, object, Operand(ip)); // offset into page of the object
258 add(offset, scratch, Operand(offset)); // add offset into the object
259 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
260
261 // Compute the page address from the heap object pointer.
262 // object: heap object pointer (with tag)
263 // offset: bit offset of store position in the remembered set
264 bic(object, object, Operand(ip));
265
266 // If the bit offset lies beyond the normal remembered set range, it is in
267 // the extra remembered set area of a large object.
268 // object: page start
269 // offset: bit offset of store position in the remembered set
270 cmp(offset, Operand(Page::kPageSize / kPointerSize));
271 b(lt, &fast);
272
273 // Adjust the bit offset to be relative to the start of the extra
274 // remembered set and the start address to be the address of the extra
275 // remembered set.
276 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
277 // Load the array length into 'scratch' and multiply by four to get the
278 // size in bytes of the elements.
279 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
280 + FixedArray::kLengthOffset));
281 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
282 // Add the page header (including remembered set), array header, and array
283 // body size to the page address.
284 add(object, object, Operand(Page::kObjectStartOffset
285 + FixedArray::kHeaderSize));
286 add(object, object, Operand(scratch));
287
288 bind(&fast);
289 // Get address of the rset word.
290 // object: start of the remembered set (page start for the fast case)
291 // offset: bit offset of store position in the remembered set
292 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
293 add(object, object, Operand(scratch, LSR, kRSetWordShift));
294 // Get bit offset in the rset word.
295 // object: address of remembered set word
296 // offset: bit offset of store position
297 and_(offset, offset, Operand(kBitsPerInt - 1));
298
299 ldr(scratch, MemOperand(object));
300 mov(ip, Operand(1));
301 orr(scratch, scratch, Operand(ip, LSL, offset));
302 str(scratch, MemOperand(object));
Steve Block6ded16b2010-05-10 14:33:55 +0100303}
304
305
306void MacroAssembler::InNewSpace(Register object,
307 Register scratch,
308 Condition cc,
309 Label* branch) {
310 ASSERT(cc == eq || cc == ne);
311 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
312 cmp(scratch, Operand(ExternalReference::new_space_start()));
313 b(cc, branch);
314}
315
316
317// Will clobber 4 registers: object, offset, scratch, ip. The
318// register 'object' contains a heap object pointer. The heap object
319// tag is shifted away.
320void MacroAssembler::RecordWrite(Register object, Register offset,
321 Register scratch) {
322 // The compiled code assumes that record write doesn't change the
323 // context register, so we check that none of the clobbered
324 // registers are cp.
325 ASSERT(!object.is(cp) && !offset.is(cp) && !scratch.is(cp));
326
327 Label done;
328
329 // First, test that the object is not in the new space. We cannot set
330 // remembered set bits in the new space.
331 InNewSpace(object, scratch, eq, &done);
332
333 // Record the actual write.
334 RecordWriteHelper(object, offset, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
336 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000337
338 // Clobber all input registers when running with the debug-code flag
339 // turned on to provoke errors.
340 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100341 mov(object, Operand(BitCast<int32_t>(kZapValue)));
342 mov(offset, Operand(BitCast<int32_t>(kZapValue)));
343 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000344 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000345}
346
347
348void MacroAssembler::EnterFrame(StackFrame::Type type) {
349 // r0-r3: preserved
350 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
351 mov(ip, Operand(Smi::FromInt(type)));
352 push(ip);
353 mov(ip, Operand(CodeObject()));
354 push(ip);
355 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
356}
357
358
359void MacroAssembler::LeaveFrame(StackFrame::Type type) {
360 // r0: preserved
361 // r1: preserved
362 // r2: preserved
363
364 // Drop the execution stack down to the frame pointer and restore
365 // the caller frame pointer and return address.
366 mov(sp, fp);
367 ldm(ia_w, sp, fp.bit() | lr.bit());
368}
369
370
Steve Blockd0582a62009-12-15 09:54:21 +0000371void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 // Compute the argv pointer and keep it in a callee-saved register.
373 // r0 is argc.
374 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
375 sub(r6, r6, Operand(kPointerSize));
376
377 // Compute callee's stack pointer before making changes and save it as
378 // ip register so that it is restored as sp register on exit, thereby
379 // popping the args.
380
381 // ip = sp + kPointerSize * #args;
382 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
383
Steve Block6ded16b2010-05-10 14:33:55 +0100384 // Prepare the stack to be aligned when calling into C. After this point there
385 // are 5 pushes before the call into C, so the stack needs to be aligned after
386 // 5 pushes.
387 int frame_alignment = ActivationFrameAlignment();
388 int frame_alignment_mask = frame_alignment - 1;
389 if (frame_alignment != kPointerSize) {
390 // The following code needs to be more general if this assert does not hold.
391 ASSERT(frame_alignment == 2 * kPointerSize);
392 // With 5 pushes left the frame must be unaligned at this point.
393 mov(r7, Operand(Smi::FromInt(0)));
394 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
395 push(r7, eq); // Push if aligned to make it unaligned.
396 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000397
398 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
399 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000400 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000401
Andrei Popescu402d9372010-02-26 13:31:12 +0000402 mov(ip, Operand(CodeObject()));
403 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
405 // Save the frame pointer and the context in top.
406 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
407 str(fp, MemOperand(ip));
408 mov(ip, Operand(ExternalReference(Top::k_context_address)));
409 str(cp, MemOperand(ip));
410
411 // Setup argc and the builtin function in callee-saved registers.
412 mov(r4, Operand(r0));
413 mov(r5, Operand(r1));
414
415
416#ifdef ENABLE_DEBUGGER_SUPPORT
417 // Save the state of all registers to the stack from the memory
418 // location. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000419 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 // Use sp as base to push.
421 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
422 }
423#endif
424}
425
426
Steve Block6ded16b2010-05-10 14:33:55 +0100427void MacroAssembler::InitializeNewString(Register string,
428 Register length,
429 Heap::RootListIndex map_index,
430 Register scratch1,
431 Register scratch2) {
432 mov(scratch1, Operand(length, LSL, kSmiTagSize));
433 LoadRoot(scratch2, map_index);
434 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
435 mov(scratch1, Operand(String::kEmptyHashField));
436 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
437 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
438}
439
440
441int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000442#if defined(V8_HOST_ARCH_ARM)
443 // Running on the real platform. Use the alignment as mandated by the local
444 // environment.
445 // Note: This will break if we ever start generating snapshots on one ARM
446 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100447 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000448#else // defined(V8_HOST_ARCH_ARM)
449 // If we are using the simulator then we should always align to the expected
450 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100451 // if the target platform will need alignment, so this is controlled from a
452 // flag.
453 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000454#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000455}
456
457
Steve Blockd0582a62009-12-15 09:54:21 +0000458void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000459#ifdef ENABLE_DEBUGGER_SUPPORT
460 // Restore the memory copy of the registers by digging them out from
461 // the stack. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000462 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 // This code intentionally clobbers r2 and r3.
464 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +0000465 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 add(r3, fp, Operand(kOffset));
467 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
468 }
469#endif
470
471 // Clear top frame.
472 mov(r3, Operand(0));
473 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
474 str(r3, MemOperand(ip));
475
476 // Restore current context from top and clear it in debug mode.
477 mov(ip, Operand(ExternalReference(Top::k_context_address)));
478 ldr(cp, MemOperand(ip));
479#ifdef DEBUG
480 str(r3, MemOperand(ip));
481#endif
482
483 // Pop the arguments, restore registers, and return.
484 mov(sp, Operand(fp)); // respect ABI stack constraint
485 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
486}
487
488
489void MacroAssembler::InvokePrologue(const ParameterCount& expected,
490 const ParameterCount& actual,
491 Handle<Code> code_constant,
492 Register code_reg,
493 Label* done,
494 InvokeFlag flag) {
495 bool definitely_matches = false;
496 Label regular_invoke;
497
498 // Check whether the expected and actual arguments count match. If not,
499 // setup registers according to contract with ArgumentsAdaptorTrampoline:
500 // r0: actual arguments count
501 // r1: function (passed through to callee)
502 // r2: expected arguments count
503 // r3: callee code entry
504
505 // The code below is made a lot easier because the calling code already sets
506 // up actual and expected registers according to the contract if values are
507 // passed in registers.
508 ASSERT(actual.is_immediate() || actual.reg().is(r0));
509 ASSERT(expected.is_immediate() || expected.reg().is(r2));
510 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
511
512 if (expected.is_immediate()) {
513 ASSERT(actual.is_immediate());
514 if (expected.immediate() == actual.immediate()) {
515 definitely_matches = true;
516 } else {
517 mov(r0, Operand(actual.immediate()));
518 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
519 if (expected.immediate() == sentinel) {
520 // Don't worry about adapting arguments for builtins that
521 // don't want that done. Skip adaption code by making it look
522 // like we have a match between expected and actual number of
523 // arguments.
524 definitely_matches = true;
525 } else {
526 mov(r2, Operand(expected.immediate()));
527 }
528 }
529 } else {
530 if (actual.is_immediate()) {
531 cmp(expected.reg(), Operand(actual.immediate()));
532 b(eq, &regular_invoke);
533 mov(r0, Operand(actual.immediate()));
534 } else {
535 cmp(expected.reg(), Operand(actual.reg()));
536 b(eq, &regular_invoke);
537 }
538 }
539
540 if (!definitely_matches) {
541 if (!code_constant.is_null()) {
542 mov(r3, Operand(code_constant));
543 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
544 }
545
546 Handle<Code> adaptor =
547 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
548 if (flag == CALL_FUNCTION) {
549 Call(adaptor, RelocInfo::CODE_TARGET);
550 b(done);
551 } else {
552 Jump(adaptor, RelocInfo::CODE_TARGET);
553 }
554 bind(&regular_invoke);
555 }
556}
557
558
559void MacroAssembler::InvokeCode(Register code,
560 const ParameterCount& expected,
561 const ParameterCount& actual,
562 InvokeFlag flag) {
563 Label done;
564
565 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
566 if (flag == CALL_FUNCTION) {
567 Call(code);
568 } else {
569 ASSERT(flag == JUMP_FUNCTION);
570 Jump(code);
571 }
572
573 // Continue here if InvokePrologue does handle the invocation due to
574 // mismatched parameter counts.
575 bind(&done);
576}
577
578
579void MacroAssembler::InvokeCode(Handle<Code> code,
580 const ParameterCount& expected,
581 const ParameterCount& actual,
582 RelocInfo::Mode rmode,
583 InvokeFlag flag) {
584 Label done;
585
586 InvokePrologue(expected, actual, code, no_reg, &done, flag);
587 if (flag == CALL_FUNCTION) {
588 Call(code, rmode);
589 } else {
590 Jump(code, rmode);
591 }
592
593 // Continue here if InvokePrologue does handle the invocation due to
594 // mismatched parameter counts.
595 bind(&done);
596}
597
598
599void MacroAssembler::InvokeFunction(Register fun,
600 const ParameterCount& actual,
601 InvokeFlag flag) {
602 // Contract with called JS functions requires that function is passed in r1.
603 ASSERT(fun.is(r1));
604
605 Register expected_reg = r2;
606 Register code_reg = r3;
607
608 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
609 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
610 ldr(expected_reg,
611 FieldMemOperand(code_reg,
612 SharedFunctionInfo::kFormalParameterCountOffset));
613 ldr(code_reg,
614 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
615 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
616
617 ParameterCount expected(expected_reg);
618 InvokeCode(code_reg, expected, actual, flag);
619}
620
621
Andrei Popescu402d9372010-02-26 13:31:12 +0000622void MacroAssembler::InvokeFunction(JSFunction* function,
623 const ParameterCount& actual,
624 InvokeFlag flag) {
625 ASSERT(function->is_compiled());
626
627 // Get the function and setup the context.
628 mov(r1, Operand(Handle<JSFunction>(function)));
629 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
630
631 // Invoke the cached code.
632 Handle<Code> code(function->code());
633 ParameterCount expected(function->shared()->formal_parameter_count());
634 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
635}
636
Steve Blocka7e24c12009-10-30 11:49:00 +0000637#ifdef ENABLE_DEBUGGER_SUPPORT
638void MacroAssembler::SaveRegistersToMemory(RegList regs) {
639 ASSERT((regs & ~kJSCallerSaved) == 0);
640 // Copy the content of registers to memory location.
641 for (int i = 0; i < kNumJSCallerSaved; i++) {
642 int r = JSCallerSavedCode(i);
643 if ((regs & (1 << r)) != 0) {
644 Register reg = { r };
645 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
646 str(reg, MemOperand(ip));
647 }
648 }
649}
650
651
652void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
653 ASSERT((regs & ~kJSCallerSaved) == 0);
654 // Copy the content of memory location to registers.
655 for (int i = kNumJSCallerSaved; --i >= 0;) {
656 int r = JSCallerSavedCode(i);
657 if ((regs & (1 << r)) != 0) {
658 Register reg = { r };
659 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
660 ldr(reg, MemOperand(ip));
661 }
662 }
663}
664
665
666void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
667 RegList regs) {
668 ASSERT((regs & ~kJSCallerSaved) == 0);
669 // Copy the content of the memory location to the stack and adjust base.
670 for (int i = kNumJSCallerSaved; --i >= 0;) {
671 int r = JSCallerSavedCode(i);
672 if ((regs & (1 << r)) != 0) {
673 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
674 ldr(ip, MemOperand(ip));
675 str(ip, MemOperand(base, 4, NegPreIndex));
676 }
677 }
678}
679
680
681void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
682 Register scratch,
683 RegList regs) {
684 ASSERT((regs & ~kJSCallerSaved) == 0);
685 // Copy the content of the stack to the memory location and adjust base.
686 for (int i = 0; i < kNumJSCallerSaved; i++) {
687 int r = JSCallerSavedCode(i);
688 if ((regs & (1 << r)) != 0) {
689 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
690 ldr(scratch, MemOperand(base, 4, PostIndex));
691 str(scratch, MemOperand(ip));
692 }
693 }
694}
Andrei Popescu402d9372010-02-26 13:31:12 +0000695
696
697void MacroAssembler::DebugBreak() {
698 ASSERT(allow_stub_calls());
699 mov(r0, Operand(0));
700 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
701 CEntryStub ces(1);
702 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
703}
Steve Blocka7e24c12009-10-30 11:49:00 +0000704#endif
705
706
707void MacroAssembler::PushTryHandler(CodeLocation try_location,
708 HandlerType type) {
709 // Adjust this code if not the case.
710 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
711 // The pc (return address) is passed in register lr.
712 if (try_location == IN_JAVASCRIPT) {
713 if (type == TRY_CATCH_HANDLER) {
714 mov(r3, Operand(StackHandler::TRY_CATCH));
715 } else {
716 mov(r3, Operand(StackHandler::TRY_FINALLY));
717 }
718 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
719 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
720 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
721 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
722 // Save the current handler as the next handler.
723 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
724 ldr(r1, MemOperand(r3));
725 ASSERT(StackHandlerConstants::kNextOffset == 0);
726 push(r1);
727 // Link this handler as the new current one.
728 str(sp, MemOperand(r3));
729 } else {
730 // Must preserve r0-r4, r5-r7 are available.
731 ASSERT(try_location == IN_JS_ENTRY);
732 // The frame pointer does not point to a JS frame so we save NULL
733 // for fp. We expect the code throwing an exception to check fp
734 // before dereferencing it to restore the context.
735 mov(ip, Operand(0)); // To save a NULL frame pointer.
736 mov(r6, Operand(StackHandler::ENTRY));
737 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
738 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
739 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
740 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
741 // Save the current handler as the next handler.
742 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
743 ldr(r6, MemOperand(r7));
744 ASSERT(StackHandlerConstants::kNextOffset == 0);
745 push(r6);
746 // Link this handler as the new current one.
747 str(sp, MemOperand(r7));
748 }
749}
750
751
Leon Clarkee46be812010-01-19 14:06:41 +0000752void MacroAssembler::PopTryHandler() {
753 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
754 pop(r1);
755 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
756 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
757 str(r1, MemOperand(ip));
758}
759
760
Steve Blocka7e24c12009-10-30 11:49:00 +0000761Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
762 JSObject* holder, Register holder_reg,
763 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100764 int save_at_depth,
Steve Blocka7e24c12009-10-30 11:49:00 +0000765 Label* miss) {
766 // Make sure there's no overlap between scratch and the other
767 // registers.
768 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
769
770 // Keep track of the current object in register reg.
771 Register reg = object_reg;
Steve Block6ded16b2010-05-10 14:33:55 +0100772 int depth = 0;
773
774 if (save_at_depth == depth) {
775 str(reg, MemOperand(sp));
776 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000777
778 // Check the maps in the prototype chain.
779 // Traverse the prototype chain from the object and do map checks.
780 while (object != holder) {
781 depth++;
782
783 // Only global objects and objects that do not require access
784 // checks are allowed in stubs.
785 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
786
787 // Get the map of the current object.
788 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
789 cmp(scratch, Operand(Handle<Map>(object->map())));
790
791 // Branch on the result of the map check.
792 b(ne, miss);
793
794 // Check access rights to the global object. This has to happen
795 // after the map check so that we know that the object is
796 // actually a global object.
797 if (object->IsJSGlobalProxy()) {
798 CheckAccessGlobalProxy(reg, scratch, miss);
799 // Restore scratch register to be the map of the object. In the
800 // new space case below, we load the prototype from the map in
801 // the scratch register.
802 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
803 }
804
805 reg = holder_reg; // from now the object is in holder_reg
806 JSObject* prototype = JSObject::cast(object->GetPrototype());
807 if (Heap::InNewSpace(prototype)) {
808 // The prototype is in new space; we cannot store a reference
809 // to it in the code. Load it from the map.
810 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
811 } else {
812 // The prototype is in old space; load it directly.
813 mov(reg, Operand(Handle<JSObject>(prototype)));
814 }
815
Steve Block6ded16b2010-05-10 14:33:55 +0100816 if (save_at_depth == depth) {
817 str(reg, MemOperand(sp));
818 }
819
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 // Go to the next object in the prototype chain.
821 object = prototype;
822 }
823
824 // Check the holder map.
825 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
826 cmp(scratch, Operand(Handle<Map>(object->map())));
827 b(ne, miss);
828
829 // Log the check depth.
Steve Block6ded16b2010-05-10 14:33:55 +0100830 LOG(IntEvent("check-maps-depth", depth + 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000831
832 // Perform security check for access to the global object and return
833 // the holder register.
834 ASSERT(object == holder);
835 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
836 if (object->IsJSGlobalProxy()) {
837 CheckAccessGlobalProxy(reg, scratch, miss);
838 }
839 return reg;
840}
841
842
843void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
844 Register scratch,
845 Label* miss) {
846 Label same_contexts;
847
848 ASSERT(!holder_reg.is(scratch));
849 ASSERT(!holder_reg.is(ip));
850 ASSERT(!scratch.is(ip));
851
852 // Load current lexical context from the stack frame.
853 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
854 // In debug mode, make sure the lexical context is set.
855#ifdef DEBUG
856 cmp(scratch, Operand(0));
857 Check(ne, "we should not have an empty lexical context");
858#endif
859
860 // Load the global context of the current context.
861 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
862 ldr(scratch, FieldMemOperand(scratch, offset));
863 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
864
865 // Check the context is a global context.
866 if (FLAG_debug_code) {
867 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
868 // Cannot use ip as a temporary in this verification code. Due to the fact
869 // that ip is clobbered as part of cmp with an object Operand.
870 push(holder_reg); // Temporarily save holder on the stack.
871 // Read the first word and compare to the global_context_map.
872 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
873 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
874 cmp(holder_reg, ip);
875 Check(eq, "JSGlobalObject::global_context should be a global context.");
876 pop(holder_reg); // Restore holder.
877 }
878
879 // Check if both contexts are the same.
880 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
881 cmp(scratch, Operand(ip));
882 b(eq, &same_contexts);
883
884 // Check the context is a global context.
885 if (FLAG_debug_code) {
886 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
887 // Cannot use ip as a temporary in this verification code. Due to the fact
888 // that ip is clobbered as part of cmp with an object Operand.
889 push(holder_reg); // Temporarily save holder on the stack.
890 mov(holder_reg, ip); // Move ip to its holding place.
891 LoadRoot(ip, Heap::kNullValueRootIndex);
892 cmp(holder_reg, ip);
893 Check(ne, "JSGlobalProxy::context() should not be null.");
894
895 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
896 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
897 cmp(holder_reg, ip);
898 Check(eq, "JSGlobalObject::global_context should be a global context.");
899 // Restore ip is not needed. ip is reloaded below.
900 pop(holder_reg); // Restore holder.
901 // Restore ip to holder's context.
902 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
903 }
904
905 // Check that the security token in the calling global object is
906 // compatible with the security token in the receiving global
907 // object.
908 int token_offset = Context::kHeaderSize +
909 Context::SECURITY_TOKEN_INDEX * kPointerSize;
910
911 ldr(scratch, FieldMemOperand(scratch, token_offset));
912 ldr(ip, FieldMemOperand(ip, token_offset));
913 cmp(scratch, Operand(ip));
914 b(ne, miss);
915
916 bind(&same_contexts);
917}
918
919
920void MacroAssembler::AllocateInNewSpace(int object_size,
921 Register result,
922 Register scratch1,
923 Register scratch2,
924 Label* gc_required,
925 AllocationFlags flags) {
926 ASSERT(!result.is(scratch1));
927 ASSERT(!scratch1.is(scratch2));
928
929 // Load address of new object into result and allocation top address into
930 // scratch1.
931 ExternalReference new_space_allocation_top =
932 ExternalReference::new_space_allocation_top_address();
933 mov(scratch1, Operand(new_space_allocation_top));
934 if ((flags & RESULT_CONTAINS_TOP) == 0) {
935 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000936 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000937 // Assert that result actually contains top on entry. scratch2 is used
938 // immediately below so this use of scratch2 does not cause difference with
939 // respect to register content between debug and release mode.
940 ldr(scratch2, MemOperand(scratch1));
941 cmp(result, scratch2);
942 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 }
944
945 // Calculate new top and bail out if new space is exhausted. Use result
946 // to calculate the new top.
947 ExternalReference new_space_allocation_limit =
948 ExternalReference::new_space_allocation_limit_address();
949 mov(scratch2, Operand(new_space_allocation_limit));
950 ldr(scratch2, MemOperand(scratch2));
951 add(result, result, Operand(object_size * kPointerSize));
952 cmp(result, Operand(scratch2));
953 b(hi, gc_required);
954
Steve Blockd0582a62009-12-15 09:54:21 +0000955 // Update allocation top. result temporarily holds the new top.
956 if (FLAG_debug_code) {
957 tst(result, Operand(kObjectAlignmentMask));
958 Check(eq, "Unaligned allocation in new space");
959 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000960 str(result, MemOperand(scratch1));
961
962 // Tag and adjust back to start of new object.
963 if ((flags & TAG_OBJECT) != 0) {
964 sub(result, result, Operand((object_size * kPointerSize) -
965 kHeapObjectTag));
966 } else {
967 sub(result, result, Operand(object_size * kPointerSize));
968 }
969}
970
971
972void MacroAssembler::AllocateInNewSpace(Register object_size,
973 Register result,
974 Register scratch1,
975 Register scratch2,
976 Label* gc_required,
977 AllocationFlags flags) {
978 ASSERT(!result.is(scratch1));
979 ASSERT(!scratch1.is(scratch2));
980
981 // Load address of new object into result and allocation top address into
982 // scratch1.
983 ExternalReference new_space_allocation_top =
984 ExternalReference::new_space_allocation_top_address();
985 mov(scratch1, Operand(new_space_allocation_top));
986 if ((flags & RESULT_CONTAINS_TOP) == 0) {
987 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000988 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000989 // Assert that result actually contains top on entry. scratch2 is used
990 // immediately below so this use of scratch2 does not cause difference with
991 // respect to register content between debug and release mode.
992 ldr(scratch2, MemOperand(scratch1));
993 cmp(result, scratch2);
994 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 }
996
997 // Calculate new top and bail out if new space is exhausted. Use result
998 // to calculate the new top. Object size is in words so a shift is required to
999 // get the number of bytes
1000 ExternalReference new_space_allocation_limit =
1001 ExternalReference::new_space_allocation_limit_address();
1002 mov(scratch2, Operand(new_space_allocation_limit));
1003 ldr(scratch2, MemOperand(scratch2));
1004 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1005 cmp(result, Operand(scratch2));
1006 b(hi, gc_required);
1007
Steve Blockd0582a62009-12-15 09:54:21 +00001008 // Update allocation top. result temporarily holds the new top.
1009 if (FLAG_debug_code) {
1010 tst(result, Operand(kObjectAlignmentMask));
1011 Check(eq, "Unaligned allocation in new space");
1012 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 str(result, MemOperand(scratch1));
1014
1015 // Adjust back to start of new object.
1016 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1017
1018 // Tag object if requested.
1019 if ((flags & TAG_OBJECT) != 0) {
1020 add(result, result, Operand(kHeapObjectTag));
1021 }
1022}
1023
1024
1025void MacroAssembler::UndoAllocationInNewSpace(Register object,
1026 Register scratch) {
1027 ExternalReference new_space_allocation_top =
1028 ExternalReference::new_space_allocation_top_address();
1029
1030 // Make sure the object has no tag before resetting top.
1031 and_(object, object, Operand(~kHeapObjectTagMask));
1032#ifdef DEBUG
1033 // Check that the object un-allocated is below the current top.
1034 mov(scratch, Operand(new_space_allocation_top));
1035 ldr(scratch, MemOperand(scratch));
1036 cmp(object, scratch);
1037 Check(lt, "Undo allocation of non allocated memory");
1038#endif
1039 // Write the address of the object to un-allocate as the current top.
1040 mov(scratch, Operand(new_space_allocation_top));
1041 str(object, MemOperand(scratch));
1042}
1043
1044
Andrei Popescu31002712010-02-23 13:46:05 +00001045void MacroAssembler::AllocateTwoByteString(Register result,
1046 Register length,
1047 Register scratch1,
1048 Register scratch2,
1049 Register scratch3,
1050 Label* gc_required) {
1051 // Calculate the number of bytes needed for the characters in the string while
1052 // observing object alignment.
1053 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1054 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1055 add(scratch1, scratch1,
1056 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
1057 // AllocateInNewSpace expects the size in words, so we can round down
1058 // to kObjectAlignment and divide by kPointerSize in the same shift.
1059 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
1060 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
1061
1062 // Allocate two-byte string in new space.
1063 AllocateInNewSpace(scratch1,
1064 result,
1065 scratch2,
1066 scratch3,
1067 gc_required,
1068 TAG_OBJECT);
1069
1070 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001071 InitializeNewString(result,
1072 length,
1073 Heap::kStringMapRootIndex,
1074 scratch1,
1075 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001076}
1077
1078
1079void MacroAssembler::AllocateAsciiString(Register result,
1080 Register length,
1081 Register scratch1,
1082 Register scratch2,
1083 Register scratch3,
1084 Label* gc_required) {
1085 // Calculate the number of bytes needed for the characters in the string while
1086 // observing object alignment.
1087 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1088 ASSERT(kCharSize == 1);
1089 add(scratch1, length,
1090 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
1091 // AllocateInNewSpace expects the size in words, so we can round down
1092 // to kObjectAlignment and divide by kPointerSize in the same shift.
1093 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
1094 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
1095
1096 // Allocate ASCII string in new space.
1097 AllocateInNewSpace(scratch1,
1098 result,
1099 scratch2,
1100 scratch3,
1101 gc_required,
1102 TAG_OBJECT);
1103
1104 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001105 InitializeNewString(result,
1106 length,
1107 Heap::kAsciiStringMapRootIndex,
1108 scratch1,
1109 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001110}
1111
1112
1113void MacroAssembler::AllocateTwoByteConsString(Register result,
1114 Register length,
1115 Register scratch1,
1116 Register scratch2,
1117 Label* gc_required) {
1118 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1119 result,
1120 scratch1,
1121 scratch2,
1122 gc_required,
1123 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001124
1125 InitializeNewString(result,
1126 length,
1127 Heap::kConsStringMapRootIndex,
1128 scratch1,
1129 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001130}
1131
1132
1133void MacroAssembler::AllocateAsciiConsString(Register result,
1134 Register length,
1135 Register scratch1,
1136 Register scratch2,
1137 Label* gc_required) {
1138 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1139 result,
1140 scratch1,
1141 scratch2,
1142 gc_required,
1143 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001144
1145 InitializeNewString(result,
1146 length,
1147 Heap::kConsAsciiStringMapRootIndex,
1148 scratch1,
1149 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001150}
1151
1152
Steve Block6ded16b2010-05-10 14:33:55 +01001153void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001154 Register map,
1155 Register type_reg,
1156 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001157 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001158 CompareInstanceType(map, type_reg, type);
1159}
1160
1161
1162void MacroAssembler::CompareInstanceType(Register map,
1163 Register type_reg,
1164 InstanceType type) {
1165 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1166 cmp(type_reg, Operand(type));
1167}
1168
1169
Andrei Popescu31002712010-02-23 13:46:05 +00001170void MacroAssembler::CheckMap(Register obj,
1171 Register scratch,
1172 Handle<Map> map,
1173 Label* fail,
1174 bool is_heap_object) {
1175 if (!is_heap_object) {
1176 BranchOnSmi(obj, fail);
1177 }
1178 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1179 mov(ip, Operand(map));
1180 cmp(scratch, ip);
1181 b(ne, fail);
1182}
1183
1184
Steve Blocka7e24c12009-10-30 11:49:00 +00001185void MacroAssembler::TryGetFunctionPrototype(Register function,
1186 Register result,
1187 Register scratch,
1188 Label* miss) {
1189 // Check that the receiver isn't a smi.
1190 BranchOnSmi(function, miss);
1191
1192 // Check that the function really is a function. Load map into result reg.
1193 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1194 b(ne, miss);
1195
1196 // Make sure that the function has an instance prototype.
1197 Label non_instance;
1198 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1199 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1200 b(ne, &non_instance);
1201
1202 // Get the prototype or initial map from the function.
1203 ldr(result,
1204 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1205
1206 // If the prototype or initial map is the hole, don't return it and
1207 // simply miss the cache instead. This will allow us to allocate a
1208 // prototype object on-demand in the runtime system.
1209 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1210 cmp(result, ip);
1211 b(eq, miss);
1212
1213 // If the function does not have an initial map, we're done.
1214 Label done;
1215 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1216 b(ne, &done);
1217
1218 // Get the prototype from the initial map.
1219 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1220 jmp(&done);
1221
1222 // Non-instance prototype: Fetch prototype from constructor field
1223 // in initial map.
1224 bind(&non_instance);
1225 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1226
1227 // All done.
1228 bind(&done);
1229}
1230
1231
1232void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1233 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1234 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1235}
1236
1237
Andrei Popescu31002712010-02-23 13:46:05 +00001238void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1239 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1240 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1241}
1242
1243
Steve Blocka7e24c12009-10-30 11:49:00 +00001244void MacroAssembler::StubReturn(int argc) {
1245 ASSERT(argc >= 1 && generating_stub());
Andrei Popescu31002712010-02-23 13:46:05 +00001246 if (argc > 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001247 add(sp, sp, Operand((argc - 1) * kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +00001248 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001249 Ret();
1250}
1251
1252
1253void MacroAssembler::IllegalOperation(int num_arguments) {
1254 if (num_arguments > 0) {
1255 add(sp, sp, Operand(num_arguments * kPointerSize));
1256 }
1257 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1258}
1259
1260
Steve Blockd0582a62009-12-15 09:54:21 +00001261void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1262 Register outHighReg,
1263 Register outLowReg) {
1264 // ARMv7 VFP3 instructions to implement integer to double conversion.
1265 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001266 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001267 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001268 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001269}
1270
1271
Andrei Popescu31002712010-02-23 13:46:05 +00001272void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1273 Register src,
1274 int num_least_bits) {
1275 if (CpuFeatures::IsSupported(ARMv7)) {
1276 ubfx(dst, src, Operand(kSmiTagSize), Operand(num_least_bits - 1));
1277 } else {
1278 mov(dst, Operand(src, ASR, kSmiTagSize));
1279 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1280 }
1281}
1282
1283
Steve Blocka7e24c12009-10-30 11:49:00 +00001284void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1285 // All parameters are on the stack. r0 has the return value after call.
1286
1287 // If the expected number of arguments of the runtime function is
1288 // constant, we check that the actual number of arguments match the
1289 // expectation.
1290 if (f->nargs >= 0 && f->nargs != num_arguments) {
1291 IllegalOperation(num_arguments);
1292 return;
1293 }
1294
Leon Clarke4515c472010-02-03 11:58:03 +00001295 // TODO(1236192): Most runtime routines don't need the number of
1296 // arguments passed in because it is constant. At some point we
1297 // should remove this need and make the runtime routine entry code
1298 // smarter.
1299 mov(r0, Operand(num_arguments));
1300 mov(r1, Operand(ExternalReference(f)));
1301 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 CallStub(&stub);
1303}
1304
1305
1306void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1307 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1308}
1309
1310
Andrei Popescu402d9372010-02-26 13:31:12 +00001311void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1312 int num_arguments) {
1313 mov(r0, Operand(num_arguments));
1314 mov(r1, Operand(ext));
1315
1316 CEntryStub stub(1);
1317 CallStub(&stub);
1318}
1319
1320
Steve Block6ded16b2010-05-10 14:33:55 +01001321void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1322 int num_arguments,
1323 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001324 // TODO(1236192): Most runtime routines don't need the number of
1325 // arguments passed in because it is constant. At some point we
1326 // should remove this need and make the runtime routine entry code
1327 // smarter.
1328 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001329 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001330}
1331
1332
Steve Block6ded16b2010-05-10 14:33:55 +01001333void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1334 int num_arguments,
1335 int result_size) {
1336 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1337}
1338
1339
1340void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001341#if defined(__thumb__)
1342 // Thumb mode builtin.
1343 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1344#endif
1345 mov(r1, Operand(builtin));
1346 CEntryStub stub(1);
1347 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1348}
1349
1350
Steve Blocka7e24c12009-10-30 11:49:00 +00001351void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1352 InvokeJSFlags flags) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001353 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001355 Call(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001356 } else {
1357 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001358 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001359 }
1360}
1361
1362
1363void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001364 ASSERT(!target.is(r1));
1365
1366 // Load the builtins object into target register.
1367 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1368 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1369
Andrei Popescu402d9372010-02-26 13:31:12 +00001370 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001371 ldr(r1, FieldMemOperand(target,
1372 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1373
1374 // Load the code entry point from the builtins object.
1375 ldr(target, FieldMemOperand(target,
1376 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1377 if (FLAG_debug_code) {
1378 // Make sure the code objects in the builtins object and in the
1379 // builtin function are the same.
1380 push(r1);
1381 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1382 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1383 cmp(r1, target);
1384 Assert(eq, "Builtin code object changed");
1385 pop(r1);
1386 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001387 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
1388}
1389
1390
1391void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1392 Register scratch1, Register scratch2) {
1393 if (FLAG_native_code_counters && counter->Enabled()) {
1394 mov(scratch1, Operand(value));
1395 mov(scratch2, Operand(ExternalReference(counter)));
1396 str(scratch1, MemOperand(scratch2));
1397 }
1398}
1399
1400
1401void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1402 Register scratch1, Register scratch2) {
1403 ASSERT(value > 0);
1404 if (FLAG_native_code_counters && counter->Enabled()) {
1405 mov(scratch2, Operand(ExternalReference(counter)));
1406 ldr(scratch1, MemOperand(scratch2));
1407 add(scratch1, scratch1, Operand(value));
1408 str(scratch1, MemOperand(scratch2));
1409 }
1410}
1411
1412
1413void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1414 Register scratch1, Register scratch2) {
1415 ASSERT(value > 0);
1416 if (FLAG_native_code_counters && counter->Enabled()) {
1417 mov(scratch2, Operand(ExternalReference(counter)));
1418 ldr(scratch1, MemOperand(scratch2));
1419 sub(scratch1, scratch1, Operand(value));
1420 str(scratch1, MemOperand(scratch2));
1421 }
1422}
1423
1424
1425void MacroAssembler::Assert(Condition cc, const char* msg) {
1426 if (FLAG_debug_code)
1427 Check(cc, msg);
1428}
1429
1430
1431void MacroAssembler::Check(Condition cc, const char* msg) {
1432 Label L;
1433 b(cc, &L);
1434 Abort(msg);
1435 // will not return here
1436 bind(&L);
1437}
1438
1439
1440void MacroAssembler::Abort(const char* msg) {
1441 // We want to pass the msg string like a smi to avoid GC
1442 // problems, however msg is not guaranteed to be aligned
1443 // properly. Instead, we pass an aligned pointer that is
1444 // a proper v8 smi, but also pass the alignment difference
1445 // from the real pointer as a smi.
1446 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1447 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1448 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1449#ifdef DEBUG
1450 if (msg != NULL) {
1451 RecordComment("Abort message: ");
1452 RecordComment(msg);
1453 }
1454#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001455 // Disable stub call restrictions to always allow calls to abort.
1456 set_allow_stub_calls(true);
1457
Steve Blocka7e24c12009-10-30 11:49:00 +00001458 mov(r0, Operand(p0));
1459 push(r0);
1460 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1461 push(r0);
1462 CallRuntime(Runtime::kAbort, 2);
1463 // will not return here
1464}
1465
1466
Steve Blockd0582a62009-12-15 09:54:21 +00001467void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1468 if (context_chain_length > 0) {
1469 // Move up the chain of contexts to the context containing the slot.
1470 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1471 // Load the function context (which is the incoming, outer context).
1472 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1473 for (int i = 1; i < context_chain_length; i++) {
1474 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1475 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1476 }
1477 // The context may be an intermediate context, not a function context.
1478 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1479 } else { // Slot is in the current function context.
1480 // The context may be an intermediate context, not a function context.
1481 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1482 }
1483}
1484
1485
Andrei Popescu31002712010-02-23 13:46:05 +00001486void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1487 Register reg2,
1488 Label* on_not_both_smi) {
1489 ASSERT_EQ(0, kSmiTag);
1490 tst(reg1, Operand(kSmiTagMask));
1491 tst(reg2, Operand(kSmiTagMask), eq);
1492 b(ne, on_not_both_smi);
1493}
1494
1495
1496void MacroAssembler::JumpIfEitherSmi(Register reg1,
1497 Register reg2,
1498 Label* on_either_smi) {
1499 ASSERT_EQ(0, kSmiTag);
1500 tst(reg1, Operand(kSmiTagMask));
1501 tst(reg2, Operand(kSmiTagMask), ne);
1502 b(eq, on_either_smi);
1503}
1504
1505
Leon Clarked91b9f72010-01-27 17:25:45 +00001506void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1507 Register first,
1508 Register second,
1509 Register scratch1,
1510 Register scratch2,
1511 Label* failure) {
1512 // Test that both first and second are sequential ASCII strings.
1513 // Assume that they are non-smis.
1514 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1515 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1516 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1517 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001518
1519 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1520 scratch2,
1521 scratch1,
1522 scratch2,
1523 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001524}
1525
1526void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1527 Register second,
1528 Register scratch1,
1529 Register scratch2,
1530 Label* failure) {
1531 // Check that neither is a smi.
1532 ASSERT_EQ(0, kSmiTag);
1533 and_(scratch1, first, Operand(second));
1534 tst(scratch1, Operand(kSmiTagMask));
1535 b(eq, failure);
1536 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1537 second,
1538 scratch1,
1539 scratch2,
1540 failure);
1541}
1542
Steve Blockd0582a62009-12-15 09:54:21 +00001543
Steve Block6ded16b2010-05-10 14:33:55 +01001544// Allocates a heap number or jumps to the need_gc label if the young space
1545// is full and a scavenge is needed.
1546void MacroAssembler::AllocateHeapNumber(Register result,
1547 Register scratch1,
1548 Register scratch2,
1549 Label* gc_required) {
1550 // Allocate an object in the heap for the heap number and tag it as a heap
1551 // object.
1552 AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
1553 result,
1554 scratch1,
1555 scratch2,
1556 gc_required,
1557 TAG_OBJECT);
1558
1559 // Get heap number map and store it in the allocated object.
1560 LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
1561 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1562}
1563
1564
1565void MacroAssembler::CountLeadingZeros(Register source,
1566 Register scratch,
1567 Register zeros) {
1568#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1569 clz(zeros, source); // This instruction is only supported after ARM5.
1570#else
1571 mov(zeros, Operand(0));
1572 mov(scratch, source);
1573 // Top 16.
1574 tst(scratch, Operand(0xffff0000));
1575 add(zeros, zeros, Operand(16), LeaveCC, eq);
1576 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1577 // Top 8.
1578 tst(scratch, Operand(0xff000000));
1579 add(zeros, zeros, Operand(8), LeaveCC, eq);
1580 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1581 // Top 4.
1582 tst(scratch, Operand(0xf0000000));
1583 add(zeros, zeros, Operand(4), LeaveCC, eq);
1584 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1585 // Top 2.
1586 tst(scratch, Operand(0xc0000000));
1587 add(zeros, zeros, Operand(2), LeaveCC, eq);
1588 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1589 // Top bit.
1590 tst(scratch, Operand(0x80000000u));
1591 add(zeros, zeros, Operand(1), LeaveCC, eq);
1592#endif
1593}
1594
1595
1596void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1597 Register first,
1598 Register second,
1599 Register scratch1,
1600 Register scratch2,
1601 Label* failure) {
1602 int kFlatAsciiStringMask =
1603 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1604 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1605 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1606 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1607 cmp(scratch1, Operand(kFlatAsciiStringTag));
1608 // Ignore second test if first test failed.
1609 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1610 b(ne, failure);
1611}
1612
1613
1614void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1615 Register scratch,
1616 Label* failure) {
1617 int kFlatAsciiStringMask =
1618 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1619 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1620 and_(scratch, type, Operand(kFlatAsciiStringMask));
1621 cmp(scratch, Operand(kFlatAsciiStringTag));
1622 b(ne, failure);
1623}
1624
1625
1626void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1627 int frame_alignment = ActivationFrameAlignment();
1628 // Up to four simple arguments are passed in registers r0..r3.
1629 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1630 if (frame_alignment > kPointerSize) {
1631 // Make stack end at alignment and make room for num_arguments - 4 words
1632 // and the original value of sp.
1633 mov(scratch, sp);
1634 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
1635 ASSERT(IsPowerOf2(frame_alignment));
1636 and_(sp, sp, Operand(-frame_alignment));
1637 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1638 } else {
1639 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1640 }
1641}
1642
1643
1644void MacroAssembler::CallCFunction(ExternalReference function,
1645 int num_arguments) {
1646 mov(ip, Operand(function));
1647 CallCFunction(ip, num_arguments);
1648}
1649
1650
1651void MacroAssembler::CallCFunction(Register function, int num_arguments) {
1652 // Make sure that the stack is aligned before calling a C function unless
1653 // running in the simulator. The simulator has its own alignment check which
1654 // provides more information.
1655#if defined(V8_HOST_ARCH_ARM)
1656 if (FLAG_debug_code) {
1657 int frame_alignment = OS::ActivationFrameAlignment();
1658 int frame_alignment_mask = frame_alignment - 1;
1659 if (frame_alignment > kPointerSize) {
1660 ASSERT(IsPowerOf2(frame_alignment));
1661 Label alignment_as_expected;
1662 tst(sp, Operand(frame_alignment_mask));
1663 b(eq, &alignment_as_expected);
1664 // Don't use Check here, as it will call Runtime_Abort possibly
1665 // re-entering here.
1666 stop("Unexpected alignment");
1667 bind(&alignment_as_expected);
1668 }
1669 }
1670#endif
1671
1672 // Just call directly. The function called cannot cause a GC, or
1673 // allow preemption, so the return address in the link register
1674 // stays correct.
1675 Call(function);
1676 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1677 if (OS::ActivationFrameAlignment() > kPointerSize) {
1678 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1679 } else {
1680 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1681 }
1682}
1683
1684
Steve Blocka7e24c12009-10-30 11:49:00 +00001685#ifdef ENABLE_DEBUGGER_SUPPORT
1686CodePatcher::CodePatcher(byte* address, int instructions)
1687 : address_(address),
1688 instructions_(instructions),
1689 size_(instructions * Assembler::kInstrSize),
1690 masm_(address, size_ + Assembler::kGap) {
1691 // Create a new macro assembler pointing to the address of the code to patch.
1692 // The size is adjusted with kGap on order for the assembler to generate size
1693 // bytes of instructions without failing with buffer size constraints.
1694 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1695}
1696
1697
1698CodePatcher::~CodePatcher() {
1699 // Indicate that code has changed.
1700 CPU::FlushICache(address_, size_);
1701
1702 // Check that the code was patched as expected.
1703 ASSERT(masm_.pc_ == address_ + size_);
1704 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1705}
1706
1707
1708void CodePatcher::Emit(Instr x) {
1709 masm()->emit(x);
1710}
1711
1712
1713void CodePatcher::Emit(Address addr) {
1714 masm()->emit(reinterpret_cast<Instr>(addr));
1715}
1716#endif // ENABLE_DEBUGGER_SUPPORT
1717
1718
1719} } // namespace v8::internal