blob: 29e168c51e6aa4147607046d93f43b1e47d75b29 [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
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_ARM)
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
34#include "debug.h"
35#include "runtime.h"
36
37namespace v8 {
38namespace internal {
39
40MacroAssembler::MacroAssembler(void* buffer, int size)
41 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000042 generating_stub_(false),
43 allow_stub_calls_(true),
44 code_object_(Heap::undefined_value()) {
45}
46
47
48// We always generate arm code, never thumb code, even if V8 is compiled to
49// thumb, so we require inter-working support
50#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
51#error "flag -mthumb-interwork missing"
52#endif
53
54
55// We do not support thumb inter-working with an arm architecture not supporting
56// the blx instruction (below v5t). If you know what CPU you are compiling for
57// you can use -march=armv7 or similar.
58#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
59# error "For thumb inter-working we require an architecture which supports blx"
60#endif
61
62
Steve Blocka7e24c12009-10-30 11:49:00 +000063// Using bx does not yield better code, so use it only when required
64#if defined(USE_THUMB_INTERWORK)
65#define USE_BX 1
66#endif
67
68
69void MacroAssembler::Jump(Register target, Condition cond) {
70#if USE_BX
71 bx(target, cond);
72#else
73 mov(pc, Operand(target), LeaveCC, cond);
74#endif
75}
76
77
78void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
79 Condition cond) {
80#if USE_BX
81 mov(ip, Operand(target, rmode), LeaveCC, cond);
82 bx(ip, cond);
83#else
84 mov(pc, Operand(target, rmode), LeaveCC, cond);
85#endif
86}
87
88
89void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
90 Condition cond) {
91 ASSERT(!RelocInfo::IsCodeTarget(rmode));
92 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
93}
94
95
96void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
97 Condition cond) {
98 ASSERT(RelocInfo::IsCodeTarget(rmode));
99 // 'code' is always generated ARM code, never THUMB code
100 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
101}
102
103
104void MacroAssembler::Call(Register target, Condition cond) {
105#if USE_BLX
106 blx(target, cond);
107#else
108 // set lr for return at current pc + 8
109 mov(lr, Operand(pc), LeaveCC, cond);
110 mov(pc, Operand(target), LeaveCC, cond);
111#endif
112}
113
114
115void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
116 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100117#if USE_BLX
118 // On ARMv5 and after the recommended call sequence is:
119 // ldr ip, [pc, #...]
120 // blx ip
121
122 // The two instructions (ldr and blx) could be separated by a constant
123 // pool and the code would still work. The issue comes from the
124 // patching code which expect the ldr to be just above the blx.
125 { BlockConstPoolScope block_const_pool(this);
126 // Statement positions are expected to be recorded when the target
127 // address is loaded. The mov method will automatically record
128 // positions when pc is the target, since this is not the case here
129 // we have to do it explicitly.
130 WriteRecordedPositions();
131
132 mov(ip, Operand(target, rmode), LeaveCC, cond);
133 blx(ip, cond);
134 }
135
136 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
137#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 // Set lr for return at current pc + 8.
139 mov(lr, Operand(pc), LeaveCC, cond);
140 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
141 mov(pc, Operand(target, rmode), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100142
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100144#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000145}
146
147
148void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
149 Condition cond) {
150 ASSERT(!RelocInfo::IsCodeTarget(rmode));
151 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
152}
153
154
155void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
156 Condition cond) {
157 ASSERT(RelocInfo::IsCodeTarget(rmode));
158 // 'code' is always generated ARM code, never THUMB code
159 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
160}
161
162
163void MacroAssembler::Ret(Condition cond) {
164#if USE_BX
165 bx(lr, cond);
166#else
167 mov(pc, Operand(lr), LeaveCC, cond);
168#endif
169}
170
171
Steve Blockd0582a62009-12-15 09:54:21 +0000172void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
173 LoadRoot(ip, Heap::kStackLimitRootIndex);
174 cmp(sp, Operand(ip));
175 b(lo, on_stack_overflow);
176}
177
178
Leon Clarkee46be812010-01-19 14:06:41 +0000179void MacroAssembler::Drop(int count, Condition cond) {
180 if (count > 0) {
181 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
182 }
183}
184
185
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100186void MacroAssembler::Swap(Register reg1,
187 Register reg2,
188 Register scratch,
189 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100190 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100191 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
192 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
193 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100194 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100195 mov(scratch, reg1, LeaveCC, cond);
196 mov(reg1, reg2, LeaveCC, cond);
197 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100198 }
199}
200
201
Leon Clarkee46be812010-01-19 14:06:41 +0000202void MacroAssembler::Call(Label* target) {
203 bl(target);
204}
205
206
207void MacroAssembler::Move(Register dst, Handle<Object> value) {
208 mov(dst, Operand(value));
209}
Steve Blockd0582a62009-12-15 09:54:21 +0000210
211
Steve Block6ded16b2010-05-10 14:33:55 +0100212void MacroAssembler::Move(Register dst, Register src) {
213 if (!dst.is(src)) {
214 mov(dst, src);
215 }
216}
217
218
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100219void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
220 Condition cond) {
221 if (!CpuFeatures::IsSupported(ARMv7) || src2.is_single_instruction()) {
222 and_(dst, src1, src2, LeaveCC, cond);
223 return;
224 }
225 int32_t immediate = src2.immediate();
226 if (immediate == 0) {
227 mov(dst, Operand(0), LeaveCC, cond);
228 return;
229 }
230 if (IsPowerOf2(immediate + 1) && ((immediate & 1) != 0)) {
231 ubfx(dst, src1, 0, WhichPowerOf2(immediate + 1), cond);
232 return;
233 }
234 and_(dst, src1, src2, LeaveCC, cond);
235}
236
237
238void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
239 Condition cond) {
240 ASSERT(lsb < 32);
241 if (!CpuFeatures::IsSupported(ARMv7)) {
242 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
243 and_(dst, src1, Operand(mask), LeaveCC, cond);
244 if (lsb != 0) {
245 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
246 }
247 } else {
248 ubfx(dst, src1, lsb, width, cond);
249 }
250}
251
252
253void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
254 Condition cond) {
255 ASSERT(lsb < 32);
256 if (!CpuFeatures::IsSupported(ARMv7)) {
257 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
258 and_(dst, src1, Operand(mask), LeaveCC, cond);
259 int shift_up = 32 - lsb - width;
260 int shift_down = lsb + shift_up;
261 if (shift_up != 0) {
262 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
263 }
264 if (shift_down != 0) {
265 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
266 }
267 } else {
268 sbfx(dst, src1, lsb, width, cond);
269 }
270}
271
272
273void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
274 ASSERT(lsb < 32);
275 if (!CpuFeatures::IsSupported(ARMv7)) {
276 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
277 bic(dst, dst, Operand(mask));
278 } else {
279 bfc(dst, lsb, width, cond);
280 }
281}
282
283
Steve Blocka7e24c12009-10-30 11:49:00 +0000284void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
285 // Empty the const pool.
286 CheckConstPool(true, true);
287 add(pc, pc, Operand(index,
288 LSL,
289 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
290 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
291 nop(); // Jump table alignment.
292 for (int i = 0; i < targets.length(); i++) {
293 b(targets[i]);
294 }
295}
296
297
298void MacroAssembler::LoadRoot(Register destination,
299 Heap::RootListIndex index,
300 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000301 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000302}
303
304
Kristian Monsen25f61362010-05-21 11:50:48 +0100305void MacroAssembler::StoreRoot(Register source,
306 Heap::RootListIndex index,
307 Condition cond) {
308 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
309}
310
311
Steve Block6ded16b2010-05-10 14:33:55 +0100312void MacroAssembler::RecordWriteHelper(Register object,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100313 Operand offset,
314 Register scratch0,
315 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100316 if (FLAG_debug_code) {
317 // Check that the object is not in new space.
318 Label not_in_new_space;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100319 InNewSpace(object, scratch1, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100320 Abort("new-space object passed to RecordWriteHelper");
321 bind(&not_in_new_space);
322 }
Leon Clarke4515c472010-02-03 11:58:03 +0000323
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100324 // Add offset into the object.
325 add(scratch0, object, offset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100327 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100328 Bfc(object, 0, kPageSizeBits);
329
330 // Calculate region number.
331 Ubfx(scratch0, scratch0, Page::kRegionSizeLog2,
332 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000333
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100334 // Mark region dirty.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100335 ldr(scratch1, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 mov(ip, Operand(1));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100337 orr(scratch1, scratch1, Operand(ip, LSL, scratch0));
338 str(scratch1, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100339}
340
341
342void MacroAssembler::InNewSpace(Register object,
343 Register scratch,
344 Condition cc,
345 Label* branch) {
346 ASSERT(cc == eq || cc == ne);
347 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
348 cmp(scratch, Operand(ExternalReference::new_space_start()));
349 b(cc, branch);
350}
351
352
353// Will clobber 4 registers: object, offset, scratch, ip. The
354// register 'object' contains a heap object pointer. The heap object
355// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100356void MacroAssembler::RecordWrite(Register object,
357 Operand offset,
358 Register scratch0,
359 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100360 // The compiled code assumes that record write doesn't change the
361 // context register, so we check that none of the clobbered
362 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100363 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100364
365 Label done;
366
367 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100368 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100369 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100370
371 // Record the actual write.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100372 RecordWriteHelper(object, offset, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000373
374 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000375
376 // Clobber all input registers when running with the debug-code flag
377 // turned on to provoke errors.
378 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100379 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100380 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
381 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000382 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000383}
384
385
Leon Clarkef7060e22010-06-03 12:02:55 +0100386void MacroAssembler::Ldrd(Register dst1, Register dst2,
387 const MemOperand& src, Condition cond) {
388 ASSERT(src.rm().is(no_reg));
389 ASSERT(!dst1.is(lr)); // r14.
390 ASSERT_EQ(0, dst1.code() % 2);
391 ASSERT_EQ(dst1.code() + 1, dst2.code());
392
393 // Generate two ldr instructions if ldrd is not available.
394 if (CpuFeatures::IsSupported(ARMv7)) {
395 CpuFeatures::Scope scope(ARMv7);
396 ldrd(dst1, dst2, src, cond);
397 } else {
398 MemOperand src2(src);
399 src2.set_offset(src2.offset() + 4);
400 if (dst1.is(src.rn())) {
401 ldr(dst2, src2, cond);
402 ldr(dst1, src, cond);
403 } else {
404 ldr(dst1, src, cond);
405 ldr(dst2, src2, cond);
406 }
407 }
408}
409
410
411void MacroAssembler::Strd(Register src1, Register src2,
412 const MemOperand& dst, Condition cond) {
413 ASSERT(dst.rm().is(no_reg));
414 ASSERT(!src1.is(lr)); // r14.
415 ASSERT_EQ(0, src1.code() % 2);
416 ASSERT_EQ(src1.code() + 1, src2.code());
417
418 // Generate two str instructions if strd is not available.
419 if (CpuFeatures::IsSupported(ARMv7)) {
420 CpuFeatures::Scope scope(ARMv7);
421 strd(src1, src2, dst, cond);
422 } else {
423 MemOperand dst2(dst);
424 dst2.set_offset(dst2.offset() + 4);
425 str(src1, dst, cond);
426 str(src2, dst2, cond);
427 }
428}
429
430
Steve Blocka7e24c12009-10-30 11:49:00 +0000431void MacroAssembler::EnterFrame(StackFrame::Type type) {
432 // r0-r3: preserved
433 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
434 mov(ip, Operand(Smi::FromInt(type)));
435 push(ip);
436 mov(ip, Operand(CodeObject()));
437 push(ip);
438 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
439}
440
441
442void MacroAssembler::LeaveFrame(StackFrame::Type type) {
443 // r0: preserved
444 // r1: preserved
445 // r2: preserved
446
447 // Drop the execution stack down to the frame pointer and restore
448 // the caller frame pointer and return address.
449 mov(sp, fp);
450 ldm(ia_w, sp, fp.bit() | lr.bit());
451}
452
453
Steve Blockd0582a62009-12-15 09:54:21 +0000454void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 // Compute the argv pointer and keep it in a callee-saved register.
456 // r0 is argc.
457 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
458 sub(r6, r6, Operand(kPointerSize));
459
460 // Compute callee's stack pointer before making changes and save it as
461 // ip register so that it is restored as sp register on exit, thereby
462 // popping the args.
463
464 // ip = sp + kPointerSize * #args;
465 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
466
Steve Block6ded16b2010-05-10 14:33:55 +0100467 // Prepare the stack to be aligned when calling into C. After this point there
468 // are 5 pushes before the call into C, so the stack needs to be aligned after
469 // 5 pushes.
470 int frame_alignment = ActivationFrameAlignment();
471 int frame_alignment_mask = frame_alignment - 1;
472 if (frame_alignment != kPointerSize) {
473 // The following code needs to be more general if this assert does not hold.
474 ASSERT(frame_alignment == 2 * kPointerSize);
475 // With 5 pushes left the frame must be unaligned at this point.
476 mov(r7, Operand(Smi::FromInt(0)));
477 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
478 push(r7, eq); // Push if aligned to make it unaligned.
479 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000480
481 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
482 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000483 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000484
Andrei Popescu402d9372010-02-26 13:31:12 +0000485 mov(ip, Operand(CodeObject()));
486 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000487
488 // Save the frame pointer and the context in top.
489 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
490 str(fp, MemOperand(ip));
491 mov(ip, Operand(ExternalReference(Top::k_context_address)));
492 str(cp, MemOperand(ip));
493
494 // Setup argc and the builtin function in callee-saved registers.
495 mov(r4, Operand(r0));
496 mov(r5, Operand(r1));
497
498
499#ifdef ENABLE_DEBUGGER_SUPPORT
500 // Save the state of all registers to the stack from the memory
501 // location. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000502 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000503 // Use sp as base to push.
504 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
505 }
506#endif
507}
508
509
Steve Block6ded16b2010-05-10 14:33:55 +0100510void MacroAssembler::InitializeNewString(Register string,
511 Register length,
512 Heap::RootListIndex map_index,
513 Register scratch1,
514 Register scratch2) {
515 mov(scratch1, Operand(length, LSL, kSmiTagSize));
516 LoadRoot(scratch2, map_index);
517 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
518 mov(scratch1, Operand(String::kEmptyHashField));
519 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
520 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
521}
522
523
524int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000525#if defined(V8_HOST_ARCH_ARM)
526 // Running on the real platform. Use the alignment as mandated by the local
527 // environment.
528 // Note: This will break if we ever start generating snapshots on one ARM
529 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100530 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000531#else // defined(V8_HOST_ARCH_ARM)
532 // If we are using the simulator then we should always align to the expected
533 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100534 // if the target platform will need alignment, so this is controlled from a
535 // flag.
536 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000537#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000538}
539
540
Steve Blockd0582a62009-12-15 09:54:21 +0000541void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000542#ifdef ENABLE_DEBUGGER_SUPPORT
543 // Restore the memory copy of the registers by digging them out from
544 // the stack. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000545 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 // This code intentionally clobbers r2 and r3.
547 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +0000548 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000549 add(r3, fp, Operand(kOffset));
550 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
551 }
552#endif
553
554 // Clear top frame.
555 mov(r3, Operand(0));
556 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
557 str(r3, MemOperand(ip));
558
559 // Restore current context from top and clear it in debug mode.
560 mov(ip, Operand(ExternalReference(Top::k_context_address)));
561 ldr(cp, MemOperand(ip));
562#ifdef DEBUG
563 str(r3, MemOperand(ip));
564#endif
565
566 // Pop the arguments, restore registers, and return.
567 mov(sp, Operand(fp)); // respect ABI stack constraint
568 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
569}
570
571
572void MacroAssembler::InvokePrologue(const ParameterCount& expected,
573 const ParameterCount& actual,
574 Handle<Code> code_constant,
575 Register code_reg,
576 Label* done,
577 InvokeFlag flag) {
578 bool definitely_matches = false;
579 Label regular_invoke;
580
581 // Check whether the expected and actual arguments count match. If not,
582 // setup registers according to contract with ArgumentsAdaptorTrampoline:
583 // r0: actual arguments count
584 // r1: function (passed through to callee)
585 // r2: expected arguments count
586 // r3: callee code entry
587
588 // The code below is made a lot easier because the calling code already sets
589 // up actual and expected registers according to the contract if values are
590 // passed in registers.
591 ASSERT(actual.is_immediate() || actual.reg().is(r0));
592 ASSERT(expected.is_immediate() || expected.reg().is(r2));
593 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
594
595 if (expected.is_immediate()) {
596 ASSERT(actual.is_immediate());
597 if (expected.immediate() == actual.immediate()) {
598 definitely_matches = true;
599 } else {
600 mov(r0, Operand(actual.immediate()));
601 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
602 if (expected.immediate() == sentinel) {
603 // Don't worry about adapting arguments for builtins that
604 // don't want that done. Skip adaption code by making it look
605 // like we have a match between expected and actual number of
606 // arguments.
607 definitely_matches = true;
608 } else {
609 mov(r2, Operand(expected.immediate()));
610 }
611 }
612 } else {
613 if (actual.is_immediate()) {
614 cmp(expected.reg(), Operand(actual.immediate()));
615 b(eq, &regular_invoke);
616 mov(r0, Operand(actual.immediate()));
617 } else {
618 cmp(expected.reg(), Operand(actual.reg()));
619 b(eq, &regular_invoke);
620 }
621 }
622
623 if (!definitely_matches) {
624 if (!code_constant.is_null()) {
625 mov(r3, Operand(code_constant));
626 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
627 }
628
629 Handle<Code> adaptor =
630 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
631 if (flag == CALL_FUNCTION) {
632 Call(adaptor, RelocInfo::CODE_TARGET);
633 b(done);
634 } else {
635 Jump(adaptor, RelocInfo::CODE_TARGET);
636 }
637 bind(&regular_invoke);
638 }
639}
640
641
642void MacroAssembler::InvokeCode(Register code,
643 const ParameterCount& expected,
644 const ParameterCount& actual,
645 InvokeFlag flag) {
646 Label done;
647
648 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
649 if (flag == CALL_FUNCTION) {
650 Call(code);
651 } else {
652 ASSERT(flag == JUMP_FUNCTION);
653 Jump(code);
654 }
655
656 // Continue here if InvokePrologue does handle the invocation due to
657 // mismatched parameter counts.
658 bind(&done);
659}
660
661
662void MacroAssembler::InvokeCode(Handle<Code> code,
663 const ParameterCount& expected,
664 const ParameterCount& actual,
665 RelocInfo::Mode rmode,
666 InvokeFlag flag) {
667 Label done;
668
669 InvokePrologue(expected, actual, code, no_reg, &done, flag);
670 if (flag == CALL_FUNCTION) {
671 Call(code, rmode);
672 } else {
673 Jump(code, rmode);
674 }
675
676 // Continue here if InvokePrologue does handle the invocation due to
677 // mismatched parameter counts.
678 bind(&done);
679}
680
681
682void MacroAssembler::InvokeFunction(Register fun,
683 const ParameterCount& actual,
684 InvokeFlag flag) {
685 // Contract with called JS functions requires that function is passed in r1.
686 ASSERT(fun.is(r1));
687
688 Register expected_reg = r2;
689 Register code_reg = r3;
690
691 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
692 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
693 ldr(expected_reg,
694 FieldMemOperand(code_reg,
695 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100696 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 ldr(code_reg,
698 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
699 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
700
701 ParameterCount expected(expected_reg);
702 InvokeCode(code_reg, expected, actual, flag);
703}
704
705
Andrei Popescu402d9372010-02-26 13:31:12 +0000706void MacroAssembler::InvokeFunction(JSFunction* function,
707 const ParameterCount& actual,
708 InvokeFlag flag) {
709 ASSERT(function->is_compiled());
710
711 // Get the function and setup the context.
712 mov(r1, Operand(Handle<JSFunction>(function)));
713 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
714
715 // Invoke the cached code.
716 Handle<Code> code(function->code());
717 ParameterCount expected(function->shared()->formal_parameter_count());
718 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
719}
720
Steve Blocka7e24c12009-10-30 11:49:00 +0000721#ifdef ENABLE_DEBUGGER_SUPPORT
722void MacroAssembler::SaveRegistersToMemory(RegList regs) {
723 ASSERT((regs & ~kJSCallerSaved) == 0);
724 // Copy the content of registers to memory location.
725 for (int i = 0; i < kNumJSCallerSaved; i++) {
726 int r = JSCallerSavedCode(i);
727 if ((regs & (1 << r)) != 0) {
728 Register reg = { r };
729 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
730 str(reg, MemOperand(ip));
731 }
732 }
733}
734
735
736void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
737 ASSERT((regs & ~kJSCallerSaved) == 0);
738 // Copy the content of memory location to registers.
739 for (int i = kNumJSCallerSaved; --i >= 0;) {
740 int r = JSCallerSavedCode(i);
741 if ((regs & (1 << r)) != 0) {
742 Register reg = { r };
743 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
744 ldr(reg, MemOperand(ip));
745 }
746 }
747}
748
749
750void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
751 RegList regs) {
752 ASSERT((regs & ~kJSCallerSaved) == 0);
753 // Copy the content of the memory location to the stack and adjust base.
754 for (int i = kNumJSCallerSaved; --i >= 0;) {
755 int r = JSCallerSavedCode(i);
756 if ((regs & (1 << r)) != 0) {
757 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
758 ldr(ip, MemOperand(ip));
759 str(ip, MemOperand(base, 4, NegPreIndex));
760 }
761 }
762}
763
764
765void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
766 Register scratch,
767 RegList regs) {
768 ASSERT((regs & ~kJSCallerSaved) == 0);
769 // Copy the content of the stack to the memory location and adjust base.
770 for (int i = 0; i < kNumJSCallerSaved; i++) {
771 int r = JSCallerSavedCode(i);
772 if ((regs & (1 << r)) != 0) {
773 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
774 ldr(scratch, MemOperand(base, 4, PostIndex));
775 str(scratch, MemOperand(ip));
776 }
777 }
778}
Andrei Popescu402d9372010-02-26 13:31:12 +0000779
780
781void MacroAssembler::DebugBreak() {
782 ASSERT(allow_stub_calls());
783 mov(r0, Operand(0));
784 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
785 CEntryStub ces(1);
786 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
787}
Steve Blocka7e24c12009-10-30 11:49:00 +0000788#endif
789
790
791void MacroAssembler::PushTryHandler(CodeLocation try_location,
792 HandlerType type) {
793 // Adjust this code if not the case.
794 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
795 // The pc (return address) is passed in register lr.
796 if (try_location == IN_JAVASCRIPT) {
797 if (type == TRY_CATCH_HANDLER) {
798 mov(r3, Operand(StackHandler::TRY_CATCH));
799 } else {
800 mov(r3, Operand(StackHandler::TRY_FINALLY));
801 }
802 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
803 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
804 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
805 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
806 // Save the current handler as the next handler.
807 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
808 ldr(r1, MemOperand(r3));
809 ASSERT(StackHandlerConstants::kNextOffset == 0);
810 push(r1);
811 // Link this handler as the new current one.
812 str(sp, MemOperand(r3));
813 } else {
814 // Must preserve r0-r4, r5-r7 are available.
815 ASSERT(try_location == IN_JS_ENTRY);
816 // The frame pointer does not point to a JS frame so we save NULL
817 // for fp. We expect the code throwing an exception to check fp
818 // before dereferencing it to restore the context.
819 mov(ip, Operand(0)); // To save a NULL frame pointer.
820 mov(r6, Operand(StackHandler::ENTRY));
821 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
822 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
823 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
824 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
825 // Save the current handler as the next handler.
826 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
827 ldr(r6, MemOperand(r7));
828 ASSERT(StackHandlerConstants::kNextOffset == 0);
829 push(r6);
830 // Link this handler as the new current one.
831 str(sp, MemOperand(r7));
832 }
833}
834
835
Leon Clarkee46be812010-01-19 14:06:41 +0000836void MacroAssembler::PopTryHandler() {
837 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
838 pop(r1);
839 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
840 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
841 str(r1, MemOperand(ip));
842}
843
844
Steve Blocka7e24c12009-10-30 11:49:00 +0000845Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
846 JSObject* holder, Register holder_reg,
847 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100848 int save_at_depth,
Steve Blocka7e24c12009-10-30 11:49:00 +0000849 Label* miss) {
850 // Make sure there's no overlap between scratch and the other
851 // registers.
852 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
853
854 // Keep track of the current object in register reg.
855 Register reg = object_reg;
Steve Block6ded16b2010-05-10 14:33:55 +0100856 int depth = 0;
857
858 if (save_at_depth == depth) {
859 str(reg, MemOperand(sp));
860 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000861
862 // Check the maps in the prototype chain.
863 // Traverse the prototype chain from the object and do map checks.
864 while (object != holder) {
865 depth++;
866
867 // Only global objects and objects that do not require access
868 // checks are allowed in stubs.
869 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
870
871 // Get the map of the current object.
872 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
873 cmp(scratch, Operand(Handle<Map>(object->map())));
874
875 // Branch on the result of the map check.
876 b(ne, miss);
877
878 // Check access rights to the global object. This has to happen
879 // after the map check so that we know that the object is
880 // actually a global object.
881 if (object->IsJSGlobalProxy()) {
882 CheckAccessGlobalProxy(reg, scratch, miss);
883 // Restore scratch register to be the map of the object. In the
884 // new space case below, we load the prototype from the map in
885 // the scratch register.
886 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
887 }
888
889 reg = holder_reg; // from now the object is in holder_reg
890 JSObject* prototype = JSObject::cast(object->GetPrototype());
891 if (Heap::InNewSpace(prototype)) {
892 // The prototype is in new space; we cannot store a reference
893 // to it in the code. Load it from the map.
894 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
895 } else {
896 // The prototype is in old space; load it directly.
897 mov(reg, Operand(Handle<JSObject>(prototype)));
898 }
899
Steve Block6ded16b2010-05-10 14:33:55 +0100900 if (save_at_depth == depth) {
901 str(reg, MemOperand(sp));
902 }
903
Steve Blocka7e24c12009-10-30 11:49:00 +0000904 // Go to the next object in the prototype chain.
905 object = prototype;
906 }
907
908 // Check the holder map.
909 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
910 cmp(scratch, Operand(Handle<Map>(object->map())));
911 b(ne, miss);
912
913 // Log the check depth.
Steve Block6ded16b2010-05-10 14:33:55 +0100914 LOG(IntEvent("check-maps-depth", depth + 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000915
916 // Perform security check for access to the global object and return
917 // the holder register.
918 ASSERT(object == holder);
919 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
920 if (object->IsJSGlobalProxy()) {
921 CheckAccessGlobalProxy(reg, scratch, miss);
922 }
923 return reg;
924}
925
926
927void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
928 Register scratch,
929 Label* miss) {
930 Label same_contexts;
931
932 ASSERT(!holder_reg.is(scratch));
933 ASSERT(!holder_reg.is(ip));
934 ASSERT(!scratch.is(ip));
935
936 // Load current lexical context from the stack frame.
937 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
938 // In debug mode, make sure the lexical context is set.
939#ifdef DEBUG
940 cmp(scratch, Operand(0));
941 Check(ne, "we should not have an empty lexical context");
942#endif
943
944 // Load the global context of the current context.
945 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
946 ldr(scratch, FieldMemOperand(scratch, offset));
947 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
948
949 // Check the context is a global context.
950 if (FLAG_debug_code) {
951 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
952 // Cannot use ip as a temporary in this verification code. Due to the fact
953 // that ip is clobbered as part of cmp with an object Operand.
954 push(holder_reg); // Temporarily save holder on the stack.
955 // Read the first word and compare to the global_context_map.
956 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
957 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
958 cmp(holder_reg, ip);
959 Check(eq, "JSGlobalObject::global_context should be a global context.");
960 pop(holder_reg); // Restore holder.
961 }
962
963 // Check if both contexts are the same.
964 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
965 cmp(scratch, Operand(ip));
966 b(eq, &same_contexts);
967
968 // Check the context is a global context.
969 if (FLAG_debug_code) {
970 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
971 // Cannot use ip as a temporary in this verification code. Due to the fact
972 // that ip is clobbered as part of cmp with an object Operand.
973 push(holder_reg); // Temporarily save holder on the stack.
974 mov(holder_reg, ip); // Move ip to its holding place.
975 LoadRoot(ip, Heap::kNullValueRootIndex);
976 cmp(holder_reg, ip);
977 Check(ne, "JSGlobalProxy::context() should not be null.");
978
979 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
980 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
981 cmp(holder_reg, ip);
982 Check(eq, "JSGlobalObject::global_context should be a global context.");
983 // Restore ip is not needed. ip is reloaded below.
984 pop(holder_reg); // Restore holder.
985 // Restore ip to holder's context.
986 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
987 }
988
989 // Check that the security token in the calling global object is
990 // compatible with the security token in the receiving global
991 // object.
992 int token_offset = Context::kHeaderSize +
993 Context::SECURITY_TOKEN_INDEX * kPointerSize;
994
995 ldr(scratch, FieldMemOperand(scratch, token_offset));
996 ldr(ip, FieldMemOperand(ip, token_offset));
997 cmp(scratch, Operand(ip));
998 b(ne, miss);
999
1000 bind(&same_contexts);
1001}
1002
1003
1004void MacroAssembler::AllocateInNewSpace(int object_size,
1005 Register result,
1006 Register scratch1,
1007 Register scratch2,
1008 Label* gc_required,
1009 AllocationFlags flags) {
1010 ASSERT(!result.is(scratch1));
1011 ASSERT(!scratch1.is(scratch2));
1012
Kristian Monsen25f61362010-05-21 11:50:48 +01001013 // Make object size into bytes.
1014 if ((flags & SIZE_IN_WORDS) != 0) {
1015 object_size *= kPointerSize;
1016 }
1017 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1018
Steve Blocka7e24c12009-10-30 11:49:00 +00001019 // Load address of new object into result and allocation top address into
1020 // scratch1.
1021 ExternalReference new_space_allocation_top =
1022 ExternalReference::new_space_allocation_top_address();
1023 mov(scratch1, Operand(new_space_allocation_top));
1024 if ((flags & RESULT_CONTAINS_TOP) == 0) {
1025 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +00001026 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 // Assert that result actually contains top on entry. scratch2 is used
1028 // immediately below so this use of scratch2 does not cause difference with
1029 // respect to register content between debug and release mode.
1030 ldr(scratch2, MemOperand(scratch1));
1031 cmp(result, scratch2);
1032 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +00001033 }
1034
1035 // Calculate new top and bail out if new space is exhausted. Use result
1036 // to calculate the new top.
1037 ExternalReference new_space_allocation_limit =
1038 ExternalReference::new_space_allocation_limit_address();
1039 mov(scratch2, Operand(new_space_allocation_limit));
1040 ldr(scratch2, MemOperand(scratch2));
Kristian Monsen25f61362010-05-21 11:50:48 +01001041 add(result, result, Operand(object_size));
Steve Blocka7e24c12009-10-30 11:49:00 +00001042 cmp(result, Operand(scratch2));
1043 b(hi, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +00001044 str(result, MemOperand(scratch1));
1045
1046 // Tag and adjust back to start of new object.
1047 if ((flags & TAG_OBJECT) != 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001048 sub(result, result, Operand(object_size - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001049 } else {
Kristian Monsen25f61362010-05-21 11:50:48 +01001050 sub(result, result, Operand(object_size));
Steve Blocka7e24c12009-10-30 11:49:00 +00001051 }
1052}
1053
1054
1055void MacroAssembler::AllocateInNewSpace(Register object_size,
1056 Register result,
1057 Register scratch1,
1058 Register scratch2,
1059 Label* gc_required,
1060 AllocationFlags flags) {
1061 ASSERT(!result.is(scratch1));
1062 ASSERT(!scratch1.is(scratch2));
1063
1064 // Load address of new object into result and allocation top address into
1065 // scratch1.
1066 ExternalReference new_space_allocation_top =
1067 ExternalReference::new_space_allocation_top_address();
1068 mov(scratch1, Operand(new_space_allocation_top));
1069 if ((flags & RESULT_CONTAINS_TOP) == 0) {
1070 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +00001071 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001072 // Assert that result actually contains top on entry. scratch2 is used
1073 // immediately below so this use of scratch2 does not cause difference with
1074 // respect to register content between debug and release mode.
1075 ldr(scratch2, MemOperand(scratch1));
1076 cmp(result, scratch2);
1077 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 }
1079
1080 // Calculate new top and bail out if new space is exhausted. Use result
1081 // to calculate the new top. Object size is in words so a shift is required to
1082 // get the number of bytes
1083 ExternalReference new_space_allocation_limit =
1084 ExternalReference::new_space_allocation_limit_address();
1085 mov(scratch2, Operand(new_space_allocation_limit));
1086 ldr(scratch2, MemOperand(scratch2));
Kristian Monsen25f61362010-05-21 11:50:48 +01001087 if ((flags & SIZE_IN_WORDS) != 0) {
1088 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1089 } else {
1090 add(result, result, Operand(object_size));
1091 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001092 cmp(result, Operand(scratch2));
1093 b(hi, gc_required);
1094
Steve Blockd0582a62009-12-15 09:54:21 +00001095 // Update allocation top. result temporarily holds the new top.
1096 if (FLAG_debug_code) {
1097 tst(result, Operand(kObjectAlignmentMask));
1098 Check(eq, "Unaligned allocation in new space");
1099 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001100 str(result, MemOperand(scratch1));
1101
1102 // Adjust back to start of new object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001103 if ((flags & SIZE_IN_WORDS) != 0) {
1104 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1105 } else {
1106 sub(result, result, Operand(object_size));
1107 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001108
1109 // Tag object if requested.
1110 if ((flags & TAG_OBJECT) != 0) {
1111 add(result, result, Operand(kHeapObjectTag));
1112 }
1113}
1114
1115
1116void MacroAssembler::UndoAllocationInNewSpace(Register object,
1117 Register scratch) {
1118 ExternalReference new_space_allocation_top =
1119 ExternalReference::new_space_allocation_top_address();
1120
1121 // Make sure the object has no tag before resetting top.
1122 and_(object, object, Operand(~kHeapObjectTagMask));
1123#ifdef DEBUG
1124 // Check that the object un-allocated is below the current top.
1125 mov(scratch, Operand(new_space_allocation_top));
1126 ldr(scratch, MemOperand(scratch));
1127 cmp(object, scratch);
1128 Check(lt, "Undo allocation of non allocated memory");
1129#endif
1130 // Write the address of the object to un-allocate as the current top.
1131 mov(scratch, Operand(new_space_allocation_top));
1132 str(object, MemOperand(scratch));
1133}
1134
1135
Andrei Popescu31002712010-02-23 13:46:05 +00001136void MacroAssembler::AllocateTwoByteString(Register result,
1137 Register length,
1138 Register scratch1,
1139 Register scratch2,
1140 Register scratch3,
1141 Label* gc_required) {
1142 // Calculate the number of bytes needed for the characters in the string while
1143 // observing object alignment.
1144 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1145 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1146 add(scratch1, scratch1,
1147 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001148 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001149
1150 // Allocate two-byte string in new space.
1151 AllocateInNewSpace(scratch1,
1152 result,
1153 scratch2,
1154 scratch3,
1155 gc_required,
1156 TAG_OBJECT);
1157
1158 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001159 InitializeNewString(result,
1160 length,
1161 Heap::kStringMapRootIndex,
1162 scratch1,
1163 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001164}
1165
1166
1167void MacroAssembler::AllocateAsciiString(Register result,
1168 Register length,
1169 Register scratch1,
1170 Register scratch2,
1171 Register scratch3,
1172 Label* gc_required) {
1173 // Calculate the number of bytes needed for the characters in the string while
1174 // observing object alignment.
1175 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1176 ASSERT(kCharSize == 1);
1177 add(scratch1, length,
1178 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001179 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001180
1181 // Allocate ASCII string in new space.
1182 AllocateInNewSpace(scratch1,
1183 result,
1184 scratch2,
1185 scratch3,
1186 gc_required,
1187 TAG_OBJECT);
1188
1189 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001190 InitializeNewString(result,
1191 length,
1192 Heap::kAsciiStringMapRootIndex,
1193 scratch1,
1194 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001195}
1196
1197
1198void MacroAssembler::AllocateTwoByteConsString(Register result,
1199 Register length,
1200 Register scratch1,
1201 Register scratch2,
1202 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001203 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001204 result,
1205 scratch1,
1206 scratch2,
1207 gc_required,
1208 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001209
1210 InitializeNewString(result,
1211 length,
1212 Heap::kConsStringMapRootIndex,
1213 scratch1,
1214 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001215}
1216
1217
1218void MacroAssembler::AllocateAsciiConsString(Register result,
1219 Register length,
1220 Register scratch1,
1221 Register scratch2,
1222 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001223 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001224 result,
1225 scratch1,
1226 scratch2,
1227 gc_required,
1228 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001229
1230 InitializeNewString(result,
1231 length,
1232 Heap::kConsAsciiStringMapRootIndex,
1233 scratch1,
1234 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001235}
1236
1237
Steve Block6ded16b2010-05-10 14:33:55 +01001238void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 Register map,
1240 Register type_reg,
1241 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001242 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001243 CompareInstanceType(map, type_reg, type);
1244}
1245
1246
1247void MacroAssembler::CompareInstanceType(Register map,
1248 Register type_reg,
1249 InstanceType type) {
1250 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1251 cmp(type_reg, Operand(type));
1252}
1253
1254
Andrei Popescu31002712010-02-23 13:46:05 +00001255void MacroAssembler::CheckMap(Register obj,
1256 Register scratch,
1257 Handle<Map> map,
1258 Label* fail,
1259 bool is_heap_object) {
1260 if (!is_heap_object) {
1261 BranchOnSmi(obj, fail);
1262 }
1263 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1264 mov(ip, Operand(map));
1265 cmp(scratch, ip);
1266 b(ne, fail);
1267}
1268
1269
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001270void MacroAssembler::CheckMap(Register obj,
1271 Register scratch,
1272 Heap::RootListIndex index,
1273 Label* fail,
1274 bool is_heap_object) {
1275 if (!is_heap_object) {
1276 BranchOnSmi(obj, fail);
1277 }
1278 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1279 LoadRoot(ip, index);
1280 cmp(scratch, ip);
1281 b(ne, fail);
1282}
1283
1284
Steve Blocka7e24c12009-10-30 11:49:00 +00001285void MacroAssembler::TryGetFunctionPrototype(Register function,
1286 Register result,
1287 Register scratch,
1288 Label* miss) {
1289 // Check that the receiver isn't a smi.
1290 BranchOnSmi(function, miss);
1291
1292 // Check that the function really is a function. Load map into result reg.
1293 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1294 b(ne, miss);
1295
1296 // Make sure that the function has an instance prototype.
1297 Label non_instance;
1298 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1299 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1300 b(ne, &non_instance);
1301
1302 // Get the prototype or initial map from the function.
1303 ldr(result,
1304 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1305
1306 // If the prototype or initial map is the hole, don't return it and
1307 // simply miss the cache instead. This will allow us to allocate a
1308 // prototype object on-demand in the runtime system.
1309 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1310 cmp(result, ip);
1311 b(eq, miss);
1312
1313 // If the function does not have an initial map, we're done.
1314 Label done;
1315 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1316 b(ne, &done);
1317
1318 // Get the prototype from the initial map.
1319 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1320 jmp(&done);
1321
1322 // Non-instance prototype: Fetch prototype from constructor field
1323 // in initial map.
1324 bind(&non_instance);
1325 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1326
1327 // All done.
1328 bind(&done);
1329}
1330
1331
1332void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1333 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1334 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1335}
1336
1337
Andrei Popescu31002712010-02-23 13:46:05 +00001338void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1339 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1340 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1341}
1342
1343
Steve Blocka7e24c12009-10-30 11:49:00 +00001344void MacroAssembler::StubReturn(int argc) {
1345 ASSERT(argc >= 1 && generating_stub());
Andrei Popescu31002712010-02-23 13:46:05 +00001346 if (argc > 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 add(sp, sp, Operand((argc - 1) * kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +00001348 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001349 Ret();
1350}
1351
1352
1353void MacroAssembler::IllegalOperation(int num_arguments) {
1354 if (num_arguments > 0) {
1355 add(sp, sp, Operand(num_arguments * kPointerSize));
1356 }
1357 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1358}
1359
1360
Steve Blockd0582a62009-12-15 09:54:21 +00001361void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1362 Register outHighReg,
1363 Register outLowReg) {
1364 // ARMv7 VFP3 instructions to implement integer to double conversion.
1365 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001366 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001367 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001368 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001369}
1370
1371
Andrei Popescu31002712010-02-23 13:46:05 +00001372void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1373 Register src,
1374 int num_least_bits) {
1375 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001376 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001377 } else {
1378 mov(dst, Operand(src, ASR, kSmiTagSize));
1379 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1380 }
1381}
1382
1383
Steve Blocka7e24c12009-10-30 11:49:00 +00001384void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1385 // All parameters are on the stack. r0 has the return value after call.
1386
1387 // If the expected number of arguments of the runtime function is
1388 // constant, we check that the actual number of arguments match the
1389 // expectation.
1390 if (f->nargs >= 0 && f->nargs != num_arguments) {
1391 IllegalOperation(num_arguments);
1392 return;
1393 }
1394
Leon Clarke4515c472010-02-03 11:58:03 +00001395 // TODO(1236192): Most runtime routines don't need the number of
1396 // arguments passed in because it is constant. At some point we
1397 // should remove this need and make the runtime routine entry code
1398 // smarter.
1399 mov(r0, Operand(num_arguments));
1400 mov(r1, Operand(ExternalReference(f)));
1401 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001402 CallStub(&stub);
1403}
1404
1405
1406void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1407 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1408}
1409
1410
Andrei Popescu402d9372010-02-26 13:31:12 +00001411void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1412 int num_arguments) {
1413 mov(r0, Operand(num_arguments));
1414 mov(r1, Operand(ext));
1415
1416 CEntryStub stub(1);
1417 CallStub(&stub);
1418}
1419
1420
Steve Block6ded16b2010-05-10 14:33:55 +01001421void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1422 int num_arguments,
1423 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 // TODO(1236192): Most runtime routines don't need the number of
1425 // arguments passed in because it is constant. At some point we
1426 // should remove this need and make the runtime routine entry code
1427 // smarter.
1428 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001429 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001430}
1431
1432
Steve Block6ded16b2010-05-10 14:33:55 +01001433void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1434 int num_arguments,
1435 int result_size) {
1436 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1437}
1438
1439
1440void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001441#if defined(__thumb__)
1442 // Thumb mode builtin.
1443 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1444#endif
1445 mov(r1, Operand(builtin));
1446 CEntryStub stub(1);
1447 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1448}
1449
1450
Steve Blocka7e24c12009-10-30 11:49:00 +00001451void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1452 InvokeJSFlags flags) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001453 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001454 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001455 Call(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001456 } else {
1457 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001458 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001459 }
1460}
1461
1462
1463void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001464 ASSERT(!target.is(r1));
1465
1466 // Load the builtins object into target register.
1467 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1468 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1469
Andrei Popescu402d9372010-02-26 13:31:12 +00001470 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001471 ldr(r1, FieldMemOperand(target,
1472 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1473
1474 // Load the code entry point from the builtins object.
1475 ldr(target, FieldMemOperand(target,
1476 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1477 if (FLAG_debug_code) {
1478 // Make sure the code objects in the builtins object and in the
1479 // builtin function are the same.
1480 push(r1);
1481 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1482 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1483 cmp(r1, target);
1484 Assert(eq, "Builtin code object changed");
1485 pop(r1);
1486 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
1488}
1489
1490
1491void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1492 Register scratch1, Register scratch2) {
1493 if (FLAG_native_code_counters && counter->Enabled()) {
1494 mov(scratch1, Operand(value));
1495 mov(scratch2, Operand(ExternalReference(counter)));
1496 str(scratch1, MemOperand(scratch2));
1497 }
1498}
1499
1500
1501void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1502 Register scratch1, Register scratch2) {
1503 ASSERT(value > 0);
1504 if (FLAG_native_code_counters && counter->Enabled()) {
1505 mov(scratch2, Operand(ExternalReference(counter)));
1506 ldr(scratch1, MemOperand(scratch2));
1507 add(scratch1, scratch1, Operand(value));
1508 str(scratch1, MemOperand(scratch2));
1509 }
1510}
1511
1512
1513void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1514 Register scratch1, Register scratch2) {
1515 ASSERT(value > 0);
1516 if (FLAG_native_code_counters && counter->Enabled()) {
1517 mov(scratch2, Operand(ExternalReference(counter)));
1518 ldr(scratch1, MemOperand(scratch2));
1519 sub(scratch1, scratch1, Operand(value));
1520 str(scratch1, MemOperand(scratch2));
1521 }
1522}
1523
1524
1525void MacroAssembler::Assert(Condition cc, const char* msg) {
1526 if (FLAG_debug_code)
1527 Check(cc, msg);
1528}
1529
1530
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001531void MacroAssembler::AssertRegisterIsRoot(Register reg,
1532 Heap::RootListIndex index) {
1533 if (FLAG_debug_code) {
1534 LoadRoot(ip, index);
1535 cmp(reg, ip);
1536 Check(eq, "Register did not match expected root");
1537 }
1538}
1539
1540
Steve Blocka7e24c12009-10-30 11:49:00 +00001541void MacroAssembler::Check(Condition cc, const char* msg) {
1542 Label L;
1543 b(cc, &L);
1544 Abort(msg);
1545 // will not return here
1546 bind(&L);
1547}
1548
1549
1550void MacroAssembler::Abort(const char* msg) {
1551 // We want to pass the msg string like a smi to avoid GC
1552 // problems, however msg is not guaranteed to be aligned
1553 // properly. Instead, we pass an aligned pointer that is
1554 // a proper v8 smi, but also pass the alignment difference
1555 // from the real pointer as a smi.
1556 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1557 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1558 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1559#ifdef DEBUG
1560 if (msg != NULL) {
1561 RecordComment("Abort message: ");
1562 RecordComment(msg);
1563 }
1564#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001565 // Disable stub call restrictions to always allow calls to abort.
1566 set_allow_stub_calls(true);
1567
Steve Blocka7e24c12009-10-30 11:49:00 +00001568 mov(r0, Operand(p0));
1569 push(r0);
1570 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1571 push(r0);
1572 CallRuntime(Runtime::kAbort, 2);
1573 // will not return here
1574}
1575
1576
Steve Blockd0582a62009-12-15 09:54:21 +00001577void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1578 if (context_chain_length > 0) {
1579 // Move up the chain of contexts to the context containing the slot.
1580 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1581 // Load the function context (which is the incoming, outer context).
1582 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1583 for (int i = 1; i < context_chain_length; i++) {
1584 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1585 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1586 }
1587 // The context may be an intermediate context, not a function context.
1588 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1589 } else { // Slot is in the current function context.
1590 // The context may be an intermediate context, not a function context.
1591 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1592 }
1593}
1594
1595
Andrei Popescu31002712010-02-23 13:46:05 +00001596void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1597 Register reg2,
1598 Label* on_not_both_smi) {
1599 ASSERT_EQ(0, kSmiTag);
1600 tst(reg1, Operand(kSmiTagMask));
1601 tst(reg2, Operand(kSmiTagMask), eq);
1602 b(ne, on_not_both_smi);
1603}
1604
1605
1606void MacroAssembler::JumpIfEitherSmi(Register reg1,
1607 Register reg2,
1608 Label* on_either_smi) {
1609 ASSERT_EQ(0, kSmiTag);
1610 tst(reg1, Operand(kSmiTagMask));
1611 tst(reg2, Operand(kSmiTagMask), ne);
1612 b(eq, on_either_smi);
1613}
1614
1615
Leon Clarked91b9f72010-01-27 17:25:45 +00001616void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1617 Register first,
1618 Register second,
1619 Register scratch1,
1620 Register scratch2,
1621 Label* failure) {
1622 // Test that both first and second are sequential ASCII strings.
1623 // Assume that they are non-smis.
1624 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1625 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1626 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1627 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001628
1629 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1630 scratch2,
1631 scratch1,
1632 scratch2,
1633 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001634}
1635
1636void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1637 Register second,
1638 Register scratch1,
1639 Register scratch2,
1640 Label* failure) {
1641 // Check that neither is a smi.
1642 ASSERT_EQ(0, kSmiTag);
1643 and_(scratch1, first, Operand(second));
1644 tst(scratch1, Operand(kSmiTagMask));
1645 b(eq, failure);
1646 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1647 second,
1648 scratch1,
1649 scratch2,
1650 failure);
1651}
1652
Steve Blockd0582a62009-12-15 09:54:21 +00001653
Steve Block6ded16b2010-05-10 14:33:55 +01001654// Allocates a heap number or jumps to the need_gc label if the young space
1655// is full and a scavenge is needed.
1656void MacroAssembler::AllocateHeapNumber(Register result,
1657 Register scratch1,
1658 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001659 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01001660 Label* gc_required) {
1661 // Allocate an object in the heap for the heap number and tag it as a heap
1662 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001663 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001664 result,
1665 scratch1,
1666 scratch2,
1667 gc_required,
1668 TAG_OBJECT);
1669
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001670 // Store heap number map in the allocated object.
1671 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1672 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001673}
1674
1675
1676void MacroAssembler::CountLeadingZeros(Register source,
1677 Register scratch,
1678 Register zeros) {
1679#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1680 clz(zeros, source); // This instruction is only supported after ARM5.
1681#else
1682 mov(zeros, Operand(0));
1683 mov(scratch, source);
1684 // Top 16.
1685 tst(scratch, Operand(0xffff0000));
1686 add(zeros, zeros, Operand(16), LeaveCC, eq);
1687 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1688 // Top 8.
1689 tst(scratch, Operand(0xff000000));
1690 add(zeros, zeros, Operand(8), LeaveCC, eq);
1691 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1692 // Top 4.
1693 tst(scratch, Operand(0xf0000000));
1694 add(zeros, zeros, Operand(4), LeaveCC, eq);
1695 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1696 // Top 2.
1697 tst(scratch, Operand(0xc0000000));
1698 add(zeros, zeros, Operand(2), LeaveCC, eq);
1699 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1700 // Top bit.
1701 tst(scratch, Operand(0x80000000u));
1702 add(zeros, zeros, Operand(1), LeaveCC, eq);
1703#endif
1704}
1705
1706
1707void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1708 Register first,
1709 Register second,
1710 Register scratch1,
1711 Register scratch2,
1712 Label* failure) {
1713 int kFlatAsciiStringMask =
1714 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1715 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1716 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1717 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1718 cmp(scratch1, Operand(kFlatAsciiStringTag));
1719 // Ignore second test if first test failed.
1720 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1721 b(ne, failure);
1722}
1723
1724
1725void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1726 Register scratch,
1727 Label* failure) {
1728 int kFlatAsciiStringMask =
1729 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1730 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1731 and_(scratch, type, Operand(kFlatAsciiStringMask));
1732 cmp(scratch, Operand(kFlatAsciiStringTag));
1733 b(ne, failure);
1734}
1735
1736
1737void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1738 int frame_alignment = ActivationFrameAlignment();
1739 // Up to four simple arguments are passed in registers r0..r3.
1740 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1741 if (frame_alignment > kPointerSize) {
1742 // Make stack end at alignment and make room for num_arguments - 4 words
1743 // and the original value of sp.
1744 mov(scratch, sp);
1745 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
1746 ASSERT(IsPowerOf2(frame_alignment));
1747 and_(sp, sp, Operand(-frame_alignment));
1748 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1749 } else {
1750 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1751 }
1752}
1753
1754
1755void MacroAssembler::CallCFunction(ExternalReference function,
1756 int num_arguments) {
1757 mov(ip, Operand(function));
1758 CallCFunction(ip, num_arguments);
1759}
1760
1761
1762void MacroAssembler::CallCFunction(Register function, int num_arguments) {
1763 // Make sure that the stack is aligned before calling a C function unless
1764 // running in the simulator. The simulator has its own alignment check which
1765 // provides more information.
1766#if defined(V8_HOST_ARCH_ARM)
1767 if (FLAG_debug_code) {
1768 int frame_alignment = OS::ActivationFrameAlignment();
1769 int frame_alignment_mask = frame_alignment - 1;
1770 if (frame_alignment > kPointerSize) {
1771 ASSERT(IsPowerOf2(frame_alignment));
1772 Label alignment_as_expected;
1773 tst(sp, Operand(frame_alignment_mask));
1774 b(eq, &alignment_as_expected);
1775 // Don't use Check here, as it will call Runtime_Abort possibly
1776 // re-entering here.
1777 stop("Unexpected alignment");
1778 bind(&alignment_as_expected);
1779 }
1780 }
1781#endif
1782
1783 // Just call directly. The function called cannot cause a GC, or
1784 // allow preemption, so the return address in the link register
1785 // stays correct.
1786 Call(function);
1787 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1788 if (OS::ActivationFrameAlignment() > kPointerSize) {
1789 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1790 } else {
1791 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1792 }
1793}
1794
1795
Steve Blocka7e24c12009-10-30 11:49:00 +00001796#ifdef ENABLE_DEBUGGER_SUPPORT
1797CodePatcher::CodePatcher(byte* address, int instructions)
1798 : address_(address),
1799 instructions_(instructions),
1800 size_(instructions * Assembler::kInstrSize),
1801 masm_(address, size_ + Assembler::kGap) {
1802 // Create a new macro assembler pointing to the address of the code to patch.
1803 // The size is adjusted with kGap on order for the assembler to generate size
1804 // bytes of instructions without failing with buffer size constraints.
1805 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1806}
1807
1808
1809CodePatcher::~CodePatcher() {
1810 // Indicate that code has changed.
1811 CPU::FlushICache(address_, size_);
1812
1813 // Check that the code was patched as expected.
1814 ASSERT(masm_.pc_ == address_ + size_);
1815 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1816}
1817
1818
1819void CodePatcher::Emit(Instr x) {
1820 masm()->emit(x);
1821}
1822
1823
1824void CodePatcher::Emit(Address addr) {
1825 masm()->emit(reinterpret_cast<Instr>(addr));
1826}
1827#endif // ENABLE_DEBUGGER_SUPPORT
1828
1829
1830} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001831
1832#endif // V8_TARGET_ARCH_ARM