blob: 0b6e7b33e0999d8a3940d21bd9ad42980508adcc [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
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100284void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
285 Condition cond) {
286 if (!CpuFeatures::IsSupported(ARMv7)) {
287 ASSERT(!dst.is(pc) && !src.rm().is(pc));
288 ASSERT((satpos >= 0) && (satpos <= 31));
289
290 // These asserts are required to ensure compatibility with the ARMv7
291 // implementation.
292 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
293 ASSERT(src.rs().is(no_reg));
294
295 Label done;
296 int satval = (1 << satpos) - 1;
297
298 if (cond != al) {
299 b(NegateCondition(cond), &done); // Skip saturate if !condition.
300 }
301 if (!(src.is_reg() && dst.is(src.rm()))) {
302 mov(dst, src);
303 }
304 tst(dst, Operand(~satval));
305 b(eq, &done);
306 mov(dst, Operand(0), LeaveCC, mi); // 0 if negative.
307 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
308 bind(&done);
309 } else {
310 usat(dst, satpos, src, cond);
311 }
312}
313
314
Steve Blocka7e24c12009-10-30 11:49:00 +0000315void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
316 // Empty the const pool.
317 CheckConstPool(true, true);
318 add(pc, pc, Operand(index,
319 LSL,
320 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
321 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
322 nop(); // Jump table alignment.
323 for (int i = 0; i < targets.length(); i++) {
324 b(targets[i]);
325 }
326}
327
328
329void MacroAssembler::LoadRoot(Register destination,
330 Heap::RootListIndex index,
331 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000332 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000333}
334
335
Kristian Monsen25f61362010-05-21 11:50:48 +0100336void MacroAssembler::StoreRoot(Register source,
337 Heap::RootListIndex index,
338 Condition cond) {
339 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
340}
341
342
Steve Block6ded16b2010-05-10 14:33:55 +0100343void MacroAssembler::RecordWriteHelper(Register object,
Steve Block8defd9f2010-07-08 12:39:36 +0100344 Register address,
345 Register scratch) {
Steve Block6ded16b2010-05-10 14:33:55 +0100346 if (FLAG_debug_code) {
347 // Check that the object is not in new space.
348 Label not_in_new_space;
Steve Block8defd9f2010-07-08 12:39:36 +0100349 InNewSpace(object, scratch, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100350 Abort("new-space object passed to RecordWriteHelper");
351 bind(&not_in_new_space);
352 }
Leon Clarke4515c472010-02-03 11:58:03 +0000353
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100354 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100355 Bfc(object, 0, kPageSizeBits);
356
357 // Calculate region number.
Steve Block8defd9f2010-07-08 12:39:36 +0100358 Ubfx(address, address, Page::kRegionSizeLog2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100359 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100361 // Mark region dirty.
Steve Block8defd9f2010-07-08 12:39:36 +0100362 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 mov(ip, Operand(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100364 orr(scratch, scratch, Operand(ip, LSL, address));
365 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100366}
367
368
369void MacroAssembler::InNewSpace(Register object,
370 Register scratch,
371 Condition cc,
372 Label* branch) {
373 ASSERT(cc == eq || cc == ne);
374 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
375 cmp(scratch, Operand(ExternalReference::new_space_start()));
376 b(cc, branch);
377}
378
379
380// Will clobber 4 registers: object, offset, scratch, ip. The
381// register 'object' contains a heap object pointer. The heap object
382// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100383void MacroAssembler::RecordWrite(Register object,
384 Operand offset,
385 Register scratch0,
386 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100387 // The compiled code assumes that record write doesn't change the
388 // context register, so we check that none of the clobbered
389 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100390 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100391
392 Label done;
393
394 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100395 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100396 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100397
Steve Block8defd9f2010-07-08 12:39:36 +0100398 // Add offset into the object.
399 add(scratch0, object, offset);
400
Steve Block6ded16b2010-05-10 14:33:55 +0100401 // Record the actual write.
Steve Block8defd9f2010-07-08 12:39:36 +0100402 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000403
404 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000405
406 // Clobber all input registers when running with the debug-code flag
407 // turned on to provoke errors.
408 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100409 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100410 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
411 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000412 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000413}
414
415
Steve Block8defd9f2010-07-08 12:39:36 +0100416// Will clobber 4 registers: object, address, scratch, ip. The
417// register 'object' contains a heap object pointer. The heap object
418// tag is shifted away.
419void MacroAssembler::RecordWrite(Register object,
420 Register address,
421 Register scratch) {
422 // The compiled code assumes that record write doesn't change the
423 // context register, so we check that none of the clobbered
424 // registers are cp.
425 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
426
427 Label done;
428
429 // First, test that the object is not in the new space. We cannot set
430 // region marks for new space pages.
431 InNewSpace(object, scratch, eq, &done);
432
433 // Record the actual write.
434 RecordWriteHelper(object, address, scratch);
435
436 bind(&done);
437
438 // Clobber all input registers when running with the debug-code flag
439 // turned on to provoke errors.
440 if (FLAG_debug_code) {
441 mov(object, Operand(BitCast<int32_t>(kZapValue)));
442 mov(address, Operand(BitCast<int32_t>(kZapValue)));
443 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
444 }
445}
446
447
Leon Clarkef7060e22010-06-03 12:02:55 +0100448void MacroAssembler::Ldrd(Register dst1, Register dst2,
449 const MemOperand& src, Condition cond) {
450 ASSERT(src.rm().is(no_reg));
451 ASSERT(!dst1.is(lr)); // r14.
452 ASSERT_EQ(0, dst1.code() % 2);
453 ASSERT_EQ(dst1.code() + 1, dst2.code());
454
455 // Generate two ldr instructions if ldrd is not available.
456 if (CpuFeatures::IsSupported(ARMv7)) {
457 CpuFeatures::Scope scope(ARMv7);
458 ldrd(dst1, dst2, src, cond);
459 } else {
460 MemOperand src2(src);
461 src2.set_offset(src2.offset() + 4);
462 if (dst1.is(src.rn())) {
463 ldr(dst2, src2, cond);
464 ldr(dst1, src, cond);
465 } else {
466 ldr(dst1, src, cond);
467 ldr(dst2, src2, cond);
468 }
469 }
470}
471
472
473void MacroAssembler::Strd(Register src1, Register src2,
474 const MemOperand& dst, Condition cond) {
475 ASSERT(dst.rm().is(no_reg));
476 ASSERT(!src1.is(lr)); // r14.
477 ASSERT_EQ(0, src1.code() % 2);
478 ASSERT_EQ(src1.code() + 1, src2.code());
479
480 // Generate two str instructions if strd is not available.
481 if (CpuFeatures::IsSupported(ARMv7)) {
482 CpuFeatures::Scope scope(ARMv7);
483 strd(src1, src2, dst, cond);
484 } else {
485 MemOperand dst2(dst);
486 dst2.set_offset(dst2.offset() + 4);
487 str(src1, dst, cond);
488 str(src2, dst2, cond);
489 }
490}
491
492
Steve Blocka7e24c12009-10-30 11:49:00 +0000493void MacroAssembler::EnterFrame(StackFrame::Type type) {
494 // r0-r3: preserved
495 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
496 mov(ip, Operand(Smi::FromInt(type)));
497 push(ip);
498 mov(ip, Operand(CodeObject()));
499 push(ip);
500 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
501}
502
503
504void MacroAssembler::LeaveFrame(StackFrame::Type type) {
505 // r0: preserved
506 // r1: preserved
507 // r2: preserved
508
509 // Drop the execution stack down to the frame pointer and restore
510 // the caller frame pointer and return address.
511 mov(sp, fp);
512 ldm(ia_w, sp, fp.bit() | lr.bit());
513}
514
515
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100516void MacroAssembler::EnterExitFrame() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 // Compute the argv pointer and keep it in a callee-saved register.
518 // r0 is argc.
519 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
520 sub(r6, r6, Operand(kPointerSize));
521
522 // Compute callee's stack pointer before making changes and save it as
523 // ip register so that it is restored as sp register on exit, thereby
524 // popping the args.
525
526 // ip = sp + kPointerSize * #args;
527 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
528
Steve Block6ded16b2010-05-10 14:33:55 +0100529 // Prepare the stack to be aligned when calling into C. After this point there
530 // are 5 pushes before the call into C, so the stack needs to be aligned after
531 // 5 pushes.
532 int frame_alignment = ActivationFrameAlignment();
533 int frame_alignment_mask = frame_alignment - 1;
534 if (frame_alignment != kPointerSize) {
535 // The following code needs to be more general if this assert does not hold.
536 ASSERT(frame_alignment == 2 * kPointerSize);
537 // With 5 pushes left the frame must be unaligned at this point.
538 mov(r7, Operand(Smi::FromInt(0)));
539 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
540 push(r7, eq); // Push if aligned to make it unaligned.
541 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000542
543 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
544 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000545 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
Andrei Popescu402d9372010-02-26 13:31:12 +0000547 mov(ip, Operand(CodeObject()));
548 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000549
550 // Save the frame pointer and the context in top.
551 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
552 str(fp, MemOperand(ip));
553 mov(ip, Operand(ExternalReference(Top::k_context_address)));
554 str(cp, MemOperand(ip));
555
556 // Setup argc and the builtin function in callee-saved registers.
557 mov(r4, Operand(r0));
558 mov(r5, Operand(r1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000559}
560
561
Steve Block6ded16b2010-05-10 14:33:55 +0100562void MacroAssembler::InitializeNewString(Register string,
563 Register length,
564 Heap::RootListIndex map_index,
565 Register scratch1,
566 Register scratch2) {
567 mov(scratch1, Operand(length, LSL, kSmiTagSize));
568 LoadRoot(scratch2, map_index);
569 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
570 mov(scratch1, Operand(String::kEmptyHashField));
571 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
572 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
573}
574
575
576int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000577#if defined(V8_HOST_ARCH_ARM)
578 // Running on the real platform. Use the alignment as mandated by the local
579 // environment.
580 // Note: This will break if we ever start generating snapshots on one ARM
581 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100582 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000583#else // defined(V8_HOST_ARCH_ARM)
584 // If we are using the simulator then we should always align to the expected
585 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100586 // if the target platform will need alignment, so this is controlled from a
587 // flag.
588 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000589#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000590}
591
592
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100593void MacroAssembler::LeaveExitFrame() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 // Clear top frame.
595 mov(r3, Operand(0));
596 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
597 str(r3, MemOperand(ip));
598
599 // Restore current context from top and clear it in debug mode.
600 mov(ip, Operand(ExternalReference(Top::k_context_address)));
601 ldr(cp, MemOperand(ip));
602#ifdef DEBUG
603 str(r3, MemOperand(ip));
604#endif
605
606 // Pop the arguments, restore registers, and return.
607 mov(sp, Operand(fp)); // respect ABI stack constraint
608 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
609}
610
611
612void MacroAssembler::InvokePrologue(const ParameterCount& expected,
613 const ParameterCount& actual,
614 Handle<Code> code_constant,
615 Register code_reg,
616 Label* done,
617 InvokeFlag flag) {
618 bool definitely_matches = false;
619 Label regular_invoke;
620
621 // Check whether the expected and actual arguments count match. If not,
622 // setup registers according to contract with ArgumentsAdaptorTrampoline:
623 // r0: actual arguments count
624 // r1: function (passed through to callee)
625 // r2: expected arguments count
626 // r3: callee code entry
627
628 // The code below is made a lot easier because the calling code already sets
629 // up actual and expected registers according to the contract if values are
630 // passed in registers.
631 ASSERT(actual.is_immediate() || actual.reg().is(r0));
632 ASSERT(expected.is_immediate() || expected.reg().is(r2));
633 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
634
635 if (expected.is_immediate()) {
636 ASSERT(actual.is_immediate());
637 if (expected.immediate() == actual.immediate()) {
638 definitely_matches = true;
639 } else {
640 mov(r0, Operand(actual.immediate()));
641 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
642 if (expected.immediate() == sentinel) {
643 // Don't worry about adapting arguments for builtins that
644 // don't want that done. Skip adaption code by making it look
645 // like we have a match between expected and actual number of
646 // arguments.
647 definitely_matches = true;
648 } else {
649 mov(r2, Operand(expected.immediate()));
650 }
651 }
652 } else {
653 if (actual.is_immediate()) {
654 cmp(expected.reg(), Operand(actual.immediate()));
655 b(eq, &regular_invoke);
656 mov(r0, Operand(actual.immediate()));
657 } else {
658 cmp(expected.reg(), Operand(actual.reg()));
659 b(eq, &regular_invoke);
660 }
661 }
662
663 if (!definitely_matches) {
664 if (!code_constant.is_null()) {
665 mov(r3, Operand(code_constant));
666 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
667 }
668
669 Handle<Code> adaptor =
670 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
671 if (flag == CALL_FUNCTION) {
672 Call(adaptor, RelocInfo::CODE_TARGET);
673 b(done);
674 } else {
675 Jump(adaptor, RelocInfo::CODE_TARGET);
676 }
677 bind(&regular_invoke);
678 }
679}
680
681
682void MacroAssembler::InvokeCode(Register code,
683 const ParameterCount& expected,
684 const ParameterCount& actual,
685 InvokeFlag flag) {
686 Label done;
687
688 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
689 if (flag == CALL_FUNCTION) {
690 Call(code);
691 } else {
692 ASSERT(flag == JUMP_FUNCTION);
693 Jump(code);
694 }
695
696 // Continue here if InvokePrologue does handle the invocation due to
697 // mismatched parameter counts.
698 bind(&done);
699}
700
701
702void MacroAssembler::InvokeCode(Handle<Code> code,
703 const ParameterCount& expected,
704 const ParameterCount& actual,
705 RelocInfo::Mode rmode,
706 InvokeFlag flag) {
707 Label done;
708
709 InvokePrologue(expected, actual, code, no_reg, &done, flag);
710 if (flag == CALL_FUNCTION) {
711 Call(code, rmode);
712 } else {
713 Jump(code, rmode);
714 }
715
716 // Continue here if InvokePrologue does handle the invocation due to
717 // mismatched parameter counts.
718 bind(&done);
719}
720
721
722void MacroAssembler::InvokeFunction(Register fun,
723 const ParameterCount& actual,
724 InvokeFlag flag) {
725 // Contract with called JS functions requires that function is passed in r1.
726 ASSERT(fun.is(r1));
727
728 Register expected_reg = r2;
729 Register code_reg = r3;
730
731 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
732 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
733 ldr(expected_reg,
734 FieldMemOperand(code_reg,
735 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100736 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000737 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100738 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000739
740 ParameterCount expected(expected_reg);
741 InvokeCode(code_reg, expected, actual, flag);
742}
743
744
Andrei Popescu402d9372010-02-26 13:31:12 +0000745void MacroAssembler::InvokeFunction(JSFunction* function,
746 const ParameterCount& actual,
747 InvokeFlag flag) {
748 ASSERT(function->is_compiled());
749
750 // Get the function and setup the context.
751 mov(r1, Operand(Handle<JSFunction>(function)));
752 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
753
754 // Invoke the cached code.
755 Handle<Code> code(function->code());
756 ParameterCount expected(function->shared()->formal_parameter_count());
757 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
758}
759
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100760
Steve Blocka7e24c12009-10-30 11:49:00 +0000761#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000762void MacroAssembler::DebugBreak() {
763 ASSERT(allow_stub_calls());
764 mov(r0, Operand(0));
765 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
766 CEntryStub ces(1);
767 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
768}
Steve Blocka7e24c12009-10-30 11:49:00 +0000769#endif
770
771
772void MacroAssembler::PushTryHandler(CodeLocation try_location,
773 HandlerType type) {
774 // Adjust this code if not the case.
775 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
776 // The pc (return address) is passed in register lr.
777 if (try_location == IN_JAVASCRIPT) {
778 if (type == TRY_CATCH_HANDLER) {
779 mov(r3, Operand(StackHandler::TRY_CATCH));
780 } else {
781 mov(r3, Operand(StackHandler::TRY_FINALLY));
782 }
783 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
784 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
785 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
786 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
787 // Save the current handler as the next handler.
788 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
789 ldr(r1, MemOperand(r3));
790 ASSERT(StackHandlerConstants::kNextOffset == 0);
791 push(r1);
792 // Link this handler as the new current one.
793 str(sp, MemOperand(r3));
794 } else {
795 // Must preserve r0-r4, r5-r7 are available.
796 ASSERT(try_location == IN_JS_ENTRY);
797 // The frame pointer does not point to a JS frame so we save NULL
798 // for fp. We expect the code throwing an exception to check fp
799 // before dereferencing it to restore the context.
800 mov(ip, Operand(0)); // To save a NULL frame pointer.
801 mov(r6, Operand(StackHandler::ENTRY));
802 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
803 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
804 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
805 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
806 // Save the current handler as the next handler.
807 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
808 ldr(r6, MemOperand(r7));
809 ASSERT(StackHandlerConstants::kNextOffset == 0);
810 push(r6);
811 // Link this handler as the new current one.
812 str(sp, MemOperand(r7));
813 }
814}
815
816
Leon Clarkee46be812010-01-19 14:06:41 +0000817void MacroAssembler::PopTryHandler() {
818 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
819 pop(r1);
820 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
821 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
822 str(r1, MemOperand(ip));
823}
824
825
Steve Blocka7e24c12009-10-30 11:49:00 +0000826void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
827 Register scratch,
828 Label* miss) {
829 Label same_contexts;
830
831 ASSERT(!holder_reg.is(scratch));
832 ASSERT(!holder_reg.is(ip));
833 ASSERT(!scratch.is(ip));
834
835 // Load current lexical context from the stack frame.
836 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
837 // In debug mode, make sure the lexical context is set.
838#ifdef DEBUG
839 cmp(scratch, Operand(0));
840 Check(ne, "we should not have an empty lexical context");
841#endif
842
843 // Load the global context of the current context.
844 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
845 ldr(scratch, FieldMemOperand(scratch, offset));
846 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
847
848 // Check the context is a global context.
849 if (FLAG_debug_code) {
850 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
851 // Cannot use ip as a temporary in this verification code. Due to the fact
852 // that ip is clobbered as part of cmp with an object Operand.
853 push(holder_reg); // Temporarily save holder on the stack.
854 // Read the first word and compare to the global_context_map.
855 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
856 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
857 cmp(holder_reg, ip);
858 Check(eq, "JSGlobalObject::global_context should be a global context.");
859 pop(holder_reg); // Restore holder.
860 }
861
862 // Check if both contexts are the same.
863 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
864 cmp(scratch, Operand(ip));
865 b(eq, &same_contexts);
866
867 // Check the context is a global context.
868 if (FLAG_debug_code) {
869 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
870 // Cannot use ip as a temporary in this verification code. Due to the fact
871 // that ip is clobbered as part of cmp with an object Operand.
872 push(holder_reg); // Temporarily save holder on the stack.
873 mov(holder_reg, ip); // Move ip to its holding place.
874 LoadRoot(ip, Heap::kNullValueRootIndex);
875 cmp(holder_reg, ip);
876 Check(ne, "JSGlobalProxy::context() should not be null.");
877
878 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
879 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
880 cmp(holder_reg, ip);
881 Check(eq, "JSGlobalObject::global_context should be a global context.");
882 // Restore ip is not needed. ip is reloaded below.
883 pop(holder_reg); // Restore holder.
884 // Restore ip to holder's context.
885 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
886 }
887
888 // Check that the security token in the calling global object is
889 // compatible with the security token in the receiving global
890 // object.
891 int token_offset = Context::kHeaderSize +
892 Context::SECURITY_TOKEN_INDEX * kPointerSize;
893
894 ldr(scratch, FieldMemOperand(scratch, token_offset));
895 ldr(ip, FieldMemOperand(ip, token_offset));
896 cmp(scratch, Operand(ip));
897 b(ne, miss);
898
899 bind(&same_contexts);
900}
901
902
903void MacroAssembler::AllocateInNewSpace(int object_size,
904 Register result,
905 Register scratch1,
906 Register scratch2,
907 Label* gc_required,
908 AllocationFlags flags) {
909 ASSERT(!result.is(scratch1));
910 ASSERT(!scratch1.is(scratch2));
911
Kristian Monsen25f61362010-05-21 11:50:48 +0100912 // Make object size into bytes.
913 if ((flags & SIZE_IN_WORDS) != 0) {
914 object_size *= kPointerSize;
915 }
916 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
917
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 // Load address of new object into result and allocation top address into
919 // scratch1.
920 ExternalReference new_space_allocation_top =
921 ExternalReference::new_space_allocation_top_address();
922 mov(scratch1, Operand(new_space_allocation_top));
923 if ((flags & RESULT_CONTAINS_TOP) == 0) {
924 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000925 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000926 // Assert that result actually contains top on entry. scratch2 is used
927 // immediately below so this use of scratch2 does not cause difference with
928 // respect to register content between debug and release mode.
929 ldr(scratch2, MemOperand(scratch1));
930 cmp(result, scratch2);
931 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000932 }
933
934 // Calculate new top and bail out if new space is exhausted. Use result
935 // to calculate the new top.
936 ExternalReference new_space_allocation_limit =
937 ExternalReference::new_space_allocation_limit_address();
938 mov(scratch2, Operand(new_space_allocation_limit));
939 ldr(scratch2, MemOperand(scratch2));
Kristian Monsen25f61362010-05-21 11:50:48 +0100940 add(result, result, Operand(object_size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 cmp(result, Operand(scratch2));
942 b(hi, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 str(result, MemOperand(scratch1));
944
945 // Tag and adjust back to start of new object.
946 if ((flags & TAG_OBJECT) != 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100947 sub(result, result, Operand(object_size - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000948 } else {
Kristian Monsen25f61362010-05-21 11:50:48 +0100949 sub(result, result, Operand(object_size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000950 }
951}
952
953
954void MacroAssembler::AllocateInNewSpace(Register object_size,
955 Register result,
956 Register scratch1,
957 Register scratch2,
958 Label* gc_required,
959 AllocationFlags flags) {
960 ASSERT(!result.is(scratch1));
961 ASSERT(!scratch1.is(scratch2));
962
963 // Load address of new object into result and allocation top address into
964 // scratch1.
965 ExternalReference new_space_allocation_top =
966 ExternalReference::new_space_allocation_top_address();
967 mov(scratch1, Operand(new_space_allocation_top));
968 if ((flags & RESULT_CONTAINS_TOP) == 0) {
969 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000970 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000971 // Assert that result actually contains top on entry. scratch2 is used
972 // immediately below so this use of scratch2 does not cause difference with
973 // respect to register content between debug and release mode.
974 ldr(scratch2, MemOperand(scratch1));
975 cmp(result, scratch2);
976 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 }
978
979 // Calculate new top and bail out if new space is exhausted. Use result
980 // to calculate the new top. Object size is in words so a shift is required to
981 // get the number of bytes
982 ExternalReference new_space_allocation_limit =
983 ExternalReference::new_space_allocation_limit_address();
984 mov(scratch2, Operand(new_space_allocation_limit));
985 ldr(scratch2, MemOperand(scratch2));
Kristian Monsen25f61362010-05-21 11:50:48 +0100986 if ((flags & SIZE_IN_WORDS) != 0) {
987 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
988 } else {
989 add(result, result, Operand(object_size));
990 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000991 cmp(result, Operand(scratch2));
992 b(hi, gc_required);
993
Steve Blockd0582a62009-12-15 09:54:21 +0000994 // Update allocation top. result temporarily holds the new top.
995 if (FLAG_debug_code) {
996 tst(result, Operand(kObjectAlignmentMask));
997 Check(eq, "Unaligned allocation in new space");
998 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000999 str(result, MemOperand(scratch1));
1000
1001 // Adjust back to start of new object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001002 if ((flags & SIZE_IN_WORDS) != 0) {
1003 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1004 } else {
1005 sub(result, result, Operand(object_size));
1006 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001007
1008 // Tag object if requested.
1009 if ((flags & TAG_OBJECT) != 0) {
1010 add(result, result, Operand(kHeapObjectTag));
1011 }
1012}
1013
1014
1015void MacroAssembler::UndoAllocationInNewSpace(Register object,
1016 Register scratch) {
1017 ExternalReference new_space_allocation_top =
1018 ExternalReference::new_space_allocation_top_address();
1019
1020 // Make sure the object has no tag before resetting top.
1021 and_(object, object, Operand(~kHeapObjectTagMask));
1022#ifdef DEBUG
1023 // Check that the object un-allocated is below the current top.
1024 mov(scratch, Operand(new_space_allocation_top));
1025 ldr(scratch, MemOperand(scratch));
1026 cmp(object, scratch);
1027 Check(lt, "Undo allocation of non allocated memory");
1028#endif
1029 // Write the address of the object to un-allocate as the current top.
1030 mov(scratch, Operand(new_space_allocation_top));
1031 str(object, MemOperand(scratch));
1032}
1033
1034
Andrei Popescu31002712010-02-23 13:46:05 +00001035void MacroAssembler::AllocateTwoByteString(Register result,
1036 Register length,
1037 Register scratch1,
1038 Register scratch2,
1039 Register scratch3,
1040 Label* gc_required) {
1041 // Calculate the number of bytes needed for the characters in the string while
1042 // observing object alignment.
1043 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1044 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1045 add(scratch1, scratch1,
1046 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001047 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001048
1049 // Allocate two-byte string in new space.
1050 AllocateInNewSpace(scratch1,
1051 result,
1052 scratch2,
1053 scratch3,
1054 gc_required,
1055 TAG_OBJECT);
1056
1057 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001058 InitializeNewString(result,
1059 length,
1060 Heap::kStringMapRootIndex,
1061 scratch1,
1062 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001063}
1064
1065
1066void MacroAssembler::AllocateAsciiString(Register result,
1067 Register length,
1068 Register scratch1,
1069 Register scratch2,
1070 Register scratch3,
1071 Label* gc_required) {
1072 // Calculate the number of bytes needed for the characters in the string while
1073 // observing object alignment.
1074 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1075 ASSERT(kCharSize == 1);
1076 add(scratch1, length,
1077 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001078 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001079
1080 // Allocate ASCII string in new space.
1081 AllocateInNewSpace(scratch1,
1082 result,
1083 scratch2,
1084 scratch3,
1085 gc_required,
1086 TAG_OBJECT);
1087
1088 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001089 InitializeNewString(result,
1090 length,
1091 Heap::kAsciiStringMapRootIndex,
1092 scratch1,
1093 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001094}
1095
1096
1097void MacroAssembler::AllocateTwoByteConsString(Register result,
1098 Register length,
1099 Register scratch1,
1100 Register scratch2,
1101 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001102 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001103 result,
1104 scratch1,
1105 scratch2,
1106 gc_required,
1107 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001108
1109 InitializeNewString(result,
1110 length,
1111 Heap::kConsStringMapRootIndex,
1112 scratch1,
1113 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001114}
1115
1116
1117void MacroAssembler::AllocateAsciiConsString(Register result,
1118 Register length,
1119 Register scratch1,
1120 Register scratch2,
1121 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001122 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001123 result,
1124 scratch1,
1125 scratch2,
1126 gc_required,
1127 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001128
1129 InitializeNewString(result,
1130 length,
1131 Heap::kConsAsciiStringMapRootIndex,
1132 scratch1,
1133 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001134}
1135
1136
Steve Block6ded16b2010-05-10 14:33:55 +01001137void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001138 Register map,
1139 Register type_reg,
1140 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001141 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001142 CompareInstanceType(map, type_reg, type);
1143}
1144
1145
1146void MacroAssembler::CompareInstanceType(Register map,
1147 Register type_reg,
1148 InstanceType type) {
1149 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1150 cmp(type_reg, Operand(type));
1151}
1152
1153
Andrei Popescu31002712010-02-23 13:46:05 +00001154void MacroAssembler::CheckMap(Register obj,
1155 Register scratch,
1156 Handle<Map> map,
1157 Label* fail,
1158 bool is_heap_object) {
1159 if (!is_heap_object) {
1160 BranchOnSmi(obj, fail);
1161 }
1162 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1163 mov(ip, Operand(map));
1164 cmp(scratch, ip);
1165 b(ne, fail);
1166}
1167
1168
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001169void MacroAssembler::CheckMap(Register obj,
1170 Register scratch,
1171 Heap::RootListIndex index,
1172 Label* fail,
1173 bool is_heap_object) {
1174 if (!is_heap_object) {
1175 BranchOnSmi(obj, fail);
1176 }
1177 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1178 LoadRoot(ip, index);
1179 cmp(scratch, ip);
1180 b(ne, fail);
1181}
1182
1183
Steve Blocka7e24c12009-10-30 11:49:00 +00001184void MacroAssembler::TryGetFunctionPrototype(Register function,
1185 Register result,
1186 Register scratch,
1187 Label* miss) {
1188 // Check that the receiver isn't a smi.
1189 BranchOnSmi(function, miss);
1190
1191 // Check that the function really is a function. Load map into result reg.
1192 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1193 b(ne, miss);
1194
1195 // Make sure that the function has an instance prototype.
1196 Label non_instance;
1197 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1198 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1199 b(ne, &non_instance);
1200
1201 // Get the prototype or initial map from the function.
1202 ldr(result,
1203 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1204
1205 // If the prototype or initial map is the hole, don't return it and
1206 // simply miss the cache instead. This will allow us to allocate a
1207 // prototype object on-demand in the runtime system.
1208 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1209 cmp(result, ip);
1210 b(eq, miss);
1211
1212 // If the function does not have an initial map, we're done.
1213 Label done;
1214 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1215 b(ne, &done);
1216
1217 // Get the prototype from the initial map.
1218 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1219 jmp(&done);
1220
1221 // Non-instance prototype: Fetch prototype from constructor field
1222 // in initial map.
1223 bind(&non_instance);
1224 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1225
1226 // All done.
1227 bind(&done);
1228}
1229
1230
1231void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1232 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1233 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1234}
1235
1236
Andrei Popescu31002712010-02-23 13:46:05 +00001237void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1238 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1239 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1240}
1241
1242
Leon Clarkeac952652010-07-15 11:15:24 +01001243void MacroAssembler::StubReturn(int argc, Condition cond) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001244 ASSERT(argc >= 1 && generating_stub());
Andrei Popescu31002712010-02-23 13:46:05 +00001245 if (argc > 1) {
Leon Clarkeac952652010-07-15 11:15:24 +01001246 add(sp, sp, Operand((argc - 1) * kPointerSize), LeaveCC, cond);
Andrei Popescu31002712010-02-23 13:46:05 +00001247 }
Leon Clarkeac952652010-07-15 11:15:24 +01001248 Ret(cond);
Steve Blocka7e24c12009-10-30 11:49:00 +00001249}
1250
1251
1252void MacroAssembler::IllegalOperation(int num_arguments) {
1253 if (num_arguments > 0) {
1254 add(sp, sp, Operand(num_arguments * kPointerSize));
1255 }
1256 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1257}
1258
1259
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001260void MacroAssembler::IndexFromHash(Register hash, Register index) {
1261 // If the hash field contains an array index pick it out. The assert checks
1262 // that the constants for the maximum number of digits for an array index
1263 // cached in the hash field and the number of bits reserved for it does not
1264 // conflict.
1265 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1266 (1 << String::kArrayIndexValueBits));
1267 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1268 // the low kHashShift bits.
1269 STATIC_ASSERT(kSmiTag == 0);
1270 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1271 mov(index, Operand(hash, LSL, kSmiTagSize));
1272}
1273
1274
Steve Blockd0582a62009-12-15 09:54:21 +00001275void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1276 Register outHighReg,
1277 Register outLowReg) {
1278 // ARMv7 VFP3 instructions to implement integer to double conversion.
1279 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001280 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001281 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001282 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001283}
1284
1285
Steve Block8defd9f2010-07-08 12:39:36 +01001286void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1287 DwVfpRegister result,
1288 Register scratch1,
1289 Register scratch2,
1290 Register heap_number_map,
1291 SwVfpRegister scratch3,
1292 Label* not_number,
1293 ObjectToDoubleFlags flags) {
1294 Label done;
1295 if ((flags & OBJECT_NOT_SMI) == 0) {
1296 Label not_smi;
1297 BranchOnNotSmi(object, &not_smi);
1298 // Remove smi tag and convert to double.
1299 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1300 vmov(scratch3, scratch1);
1301 vcvt_f64_s32(result, scratch3);
1302 b(&done);
1303 bind(&not_smi);
1304 }
1305 // Check for heap number and load double value from it.
1306 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1307 sub(scratch2, object, Operand(kHeapObjectTag));
1308 cmp(scratch1, heap_number_map);
1309 b(ne, not_number);
1310 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1311 // If exponent is all ones the number is either a NaN or +/-Infinity.
1312 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1313 Sbfx(scratch1,
1314 scratch1,
1315 HeapNumber::kExponentShift,
1316 HeapNumber::kExponentBits);
1317 // All-one value sign extend to -1.
1318 cmp(scratch1, Operand(-1));
1319 b(eq, not_number);
1320 }
1321 vldr(result, scratch2, HeapNumber::kValueOffset);
1322 bind(&done);
1323}
1324
1325
1326void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1327 DwVfpRegister value,
1328 Register scratch1,
1329 SwVfpRegister scratch2) {
1330 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1331 vmov(scratch2, scratch1);
1332 vcvt_f64_s32(value, scratch2);
1333}
1334
1335
Andrei Popescu31002712010-02-23 13:46:05 +00001336void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1337 Register src,
1338 int num_least_bits) {
1339 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001340 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001341 } else {
1342 mov(dst, Operand(src, ASR, kSmiTagSize));
1343 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1344 }
1345}
1346
1347
Steve Blocka7e24c12009-10-30 11:49:00 +00001348void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1349 // All parameters are on the stack. r0 has the return value after call.
1350
1351 // If the expected number of arguments of the runtime function is
1352 // constant, we check that the actual number of arguments match the
1353 // expectation.
1354 if (f->nargs >= 0 && f->nargs != num_arguments) {
1355 IllegalOperation(num_arguments);
1356 return;
1357 }
1358
Leon Clarke4515c472010-02-03 11:58:03 +00001359 // TODO(1236192): Most runtime routines don't need the number of
1360 // arguments passed in because it is constant. At some point we
1361 // should remove this need and make the runtime routine entry code
1362 // smarter.
1363 mov(r0, Operand(num_arguments));
1364 mov(r1, Operand(ExternalReference(f)));
1365 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001366 CallStub(&stub);
1367}
1368
1369
1370void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1371 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1372}
1373
1374
Andrei Popescu402d9372010-02-26 13:31:12 +00001375void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1376 int num_arguments) {
1377 mov(r0, Operand(num_arguments));
1378 mov(r1, Operand(ext));
1379
1380 CEntryStub stub(1);
1381 CallStub(&stub);
1382}
1383
1384
Steve Block6ded16b2010-05-10 14:33:55 +01001385void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1386 int num_arguments,
1387 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 // TODO(1236192): Most runtime routines don't need the number of
1389 // arguments passed in because it is constant. At some point we
1390 // should remove this need and make the runtime routine entry code
1391 // smarter.
1392 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001393 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001394}
1395
1396
Steve Block6ded16b2010-05-10 14:33:55 +01001397void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1398 int num_arguments,
1399 int result_size) {
1400 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1401}
1402
1403
1404void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001405#if defined(__thumb__)
1406 // Thumb mode builtin.
1407 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1408#endif
1409 mov(r1, Operand(builtin));
1410 CEntryStub stub(1);
1411 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1412}
1413
1414
Steve Blocka7e24c12009-10-30 11:49:00 +00001415void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1416 InvokeJSFlags flags) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001417 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001419 Call(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 } else {
1421 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001422 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 }
1424}
1425
1426
Steve Block791712a2010-08-27 10:21:07 +01001427void MacroAssembler::GetBuiltinFunction(Register target,
1428 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001429 // Load the builtins object into target register.
1430 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1431 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001432 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001433 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01001434 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1435}
1436
1437
1438void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1439 ASSERT(!target.is(r1));
1440 GetBuiltinFunction(r1, id);
1441 // Load the code entry point from the builtins object.
1442 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001443}
1444
1445
1446void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1447 Register scratch1, Register scratch2) {
1448 if (FLAG_native_code_counters && counter->Enabled()) {
1449 mov(scratch1, Operand(value));
1450 mov(scratch2, Operand(ExternalReference(counter)));
1451 str(scratch1, MemOperand(scratch2));
1452 }
1453}
1454
1455
1456void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1457 Register scratch1, Register scratch2) {
1458 ASSERT(value > 0);
1459 if (FLAG_native_code_counters && counter->Enabled()) {
1460 mov(scratch2, Operand(ExternalReference(counter)));
1461 ldr(scratch1, MemOperand(scratch2));
1462 add(scratch1, scratch1, Operand(value));
1463 str(scratch1, MemOperand(scratch2));
1464 }
1465}
1466
1467
1468void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1469 Register scratch1, Register scratch2) {
1470 ASSERT(value > 0);
1471 if (FLAG_native_code_counters && counter->Enabled()) {
1472 mov(scratch2, Operand(ExternalReference(counter)));
1473 ldr(scratch1, MemOperand(scratch2));
1474 sub(scratch1, scratch1, Operand(value));
1475 str(scratch1, MemOperand(scratch2));
1476 }
1477}
1478
1479
1480void MacroAssembler::Assert(Condition cc, const char* msg) {
1481 if (FLAG_debug_code)
1482 Check(cc, msg);
1483}
1484
1485
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001486void MacroAssembler::AssertRegisterIsRoot(Register reg,
1487 Heap::RootListIndex index) {
1488 if (FLAG_debug_code) {
1489 LoadRoot(ip, index);
1490 cmp(reg, ip);
1491 Check(eq, "Register did not match expected root");
1492 }
1493}
1494
1495
Iain Merrick75681382010-08-19 15:07:18 +01001496void MacroAssembler::AssertFastElements(Register elements) {
1497 if (FLAG_debug_code) {
1498 ASSERT(!elements.is(ip));
1499 Label ok;
1500 push(elements);
1501 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
1502 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
1503 cmp(elements, ip);
1504 b(eq, &ok);
1505 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
1506 cmp(elements, ip);
1507 b(eq, &ok);
1508 Abort("JSObject with fast elements map has slow elements");
1509 bind(&ok);
1510 pop(elements);
1511 }
1512}
1513
1514
Steve Blocka7e24c12009-10-30 11:49:00 +00001515void MacroAssembler::Check(Condition cc, const char* msg) {
1516 Label L;
1517 b(cc, &L);
1518 Abort(msg);
1519 // will not return here
1520 bind(&L);
1521}
1522
1523
1524void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01001525 Label abort_start;
1526 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00001527 // We want to pass the msg string like a smi to avoid GC
1528 // problems, however msg is not guaranteed to be aligned
1529 // properly. Instead, we pass an aligned pointer that is
1530 // a proper v8 smi, but also pass the alignment difference
1531 // from the real pointer as a smi.
1532 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1533 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1534 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1535#ifdef DEBUG
1536 if (msg != NULL) {
1537 RecordComment("Abort message: ");
1538 RecordComment(msg);
1539 }
1540#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001541 // Disable stub call restrictions to always allow calls to abort.
1542 set_allow_stub_calls(true);
1543
Steve Blocka7e24c12009-10-30 11:49:00 +00001544 mov(r0, Operand(p0));
1545 push(r0);
1546 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1547 push(r0);
1548 CallRuntime(Runtime::kAbort, 2);
1549 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01001550 if (is_const_pool_blocked()) {
1551 // If the calling code cares about the exact number of
1552 // instructions generated, we insert padding here to keep the size
1553 // of the Abort macro constant.
1554 static const int kExpectedAbortInstructions = 10;
1555 int abort_instructions = InstructionsGeneratedSince(&abort_start);
1556 ASSERT(abort_instructions <= kExpectedAbortInstructions);
1557 while (abort_instructions++ < kExpectedAbortInstructions) {
1558 nop();
1559 }
1560 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001561}
1562
1563
Steve Blockd0582a62009-12-15 09:54:21 +00001564void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1565 if (context_chain_length > 0) {
1566 // Move up the chain of contexts to the context containing the slot.
1567 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1568 // Load the function context (which is the incoming, outer context).
1569 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1570 for (int i = 1; i < context_chain_length; i++) {
1571 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1572 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1573 }
1574 // The context may be an intermediate context, not a function context.
1575 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1576 } else { // Slot is in the current function context.
1577 // The context may be an intermediate context, not a function context.
1578 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1579 }
1580}
1581
1582
Andrei Popescu31002712010-02-23 13:46:05 +00001583void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1584 Register reg2,
1585 Label* on_not_both_smi) {
1586 ASSERT_EQ(0, kSmiTag);
1587 tst(reg1, Operand(kSmiTagMask));
1588 tst(reg2, Operand(kSmiTagMask), eq);
1589 b(ne, on_not_both_smi);
1590}
1591
1592
1593void MacroAssembler::JumpIfEitherSmi(Register reg1,
1594 Register reg2,
1595 Label* on_either_smi) {
1596 ASSERT_EQ(0, kSmiTag);
1597 tst(reg1, Operand(kSmiTagMask));
1598 tst(reg2, Operand(kSmiTagMask), ne);
1599 b(eq, on_either_smi);
1600}
1601
1602
Iain Merrick75681382010-08-19 15:07:18 +01001603void MacroAssembler::AbortIfSmi(Register object) {
1604 ASSERT_EQ(0, kSmiTag);
1605 tst(object, Operand(kSmiTagMask));
1606 Assert(ne, "Operand is a smi");
1607}
1608
1609
Leon Clarked91b9f72010-01-27 17:25:45 +00001610void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1611 Register first,
1612 Register second,
1613 Register scratch1,
1614 Register scratch2,
1615 Label* failure) {
1616 // Test that both first and second are sequential ASCII strings.
1617 // Assume that they are non-smis.
1618 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1619 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1620 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1621 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001622
1623 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1624 scratch2,
1625 scratch1,
1626 scratch2,
1627 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001628}
1629
1630void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1631 Register second,
1632 Register scratch1,
1633 Register scratch2,
1634 Label* failure) {
1635 // Check that neither is a smi.
1636 ASSERT_EQ(0, kSmiTag);
1637 and_(scratch1, first, Operand(second));
1638 tst(scratch1, Operand(kSmiTagMask));
1639 b(eq, failure);
1640 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1641 second,
1642 scratch1,
1643 scratch2,
1644 failure);
1645}
1646
Steve Blockd0582a62009-12-15 09:54:21 +00001647
Steve Block6ded16b2010-05-10 14:33:55 +01001648// Allocates a heap number or jumps to the need_gc label if the young space
1649// is full and a scavenge is needed.
1650void MacroAssembler::AllocateHeapNumber(Register result,
1651 Register scratch1,
1652 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001653 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01001654 Label* gc_required) {
1655 // Allocate an object in the heap for the heap number and tag it as a heap
1656 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001657 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001658 result,
1659 scratch1,
1660 scratch2,
1661 gc_required,
1662 TAG_OBJECT);
1663
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001664 // Store heap number map in the allocated object.
1665 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1666 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001667}
1668
1669
Steve Block8defd9f2010-07-08 12:39:36 +01001670void MacroAssembler::AllocateHeapNumberWithValue(Register result,
1671 DwVfpRegister value,
1672 Register scratch1,
1673 Register scratch2,
1674 Register heap_number_map,
1675 Label* gc_required) {
1676 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
1677 sub(scratch1, result, Operand(kHeapObjectTag));
1678 vstr(value, scratch1, HeapNumber::kValueOffset);
1679}
1680
1681
Ben Murdochbb769b22010-08-11 14:56:33 +01001682// Copies a fixed number of fields of heap objects from src to dst.
1683void MacroAssembler::CopyFields(Register dst,
1684 Register src,
1685 RegList temps,
1686 int field_count) {
1687 // At least one bit set in the first 15 registers.
1688 ASSERT((temps & ((1 << 15) - 1)) != 0);
1689 ASSERT((temps & dst.bit()) == 0);
1690 ASSERT((temps & src.bit()) == 0);
1691 // Primitive implementation using only one temporary register.
1692
1693 Register tmp = no_reg;
1694 // Find a temp register in temps list.
1695 for (int i = 0; i < 15; i++) {
1696 if ((temps & (1 << i)) != 0) {
1697 tmp.set_code(i);
1698 break;
1699 }
1700 }
1701 ASSERT(!tmp.is(no_reg));
1702
1703 for (int i = 0; i < field_count; i++) {
1704 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
1705 str(tmp, FieldMemOperand(dst, i * kPointerSize));
1706 }
1707}
1708
1709
Steve Block8defd9f2010-07-08 12:39:36 +01001710void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
1711 Register source, // Input.
1712 Register scratch) {
1713 ASSERT(!zeros.is(source) || !source.is(zeros));
1714 ASSERT(!zeros.is(scratch));
1715 ASSERT(!scratch.is(ip));
1716 ASSERT(!source.is(ip));
1717 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01001718#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1719 clz(zeros, source); // This instruction is only supported after ARM5.
1720#else
1721 mov(zeros, Operand(0));
Steve Block8defd9f2010-07-08 12:39:36 +01001722 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01001723 // Top 16.
1724 tst(scratch, Operand(0xffff0000));
1725 add(zeros, zeros, Operand(16), LeaveCC, eq);
1726 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1727 // Top 8.
1728 tst(scratch, Operand(0xff000000));
1729 add(zeros, zeros, Operand(8), LeaveCC, eq);
1730 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1731 // Top 4.
1732 tst(scratch, Operand(0xf0000000));
1733 add(zeros, zeros, Operand(4), LeaveCC, eq);
1734 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1735 // Top 2.
1736 tst(scratch, Operand(0xc0000000));
1737 add(zeros, zeros, Operand(2), LeaveCC, eq);
1738 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1739 // Top bit.
1740 tst(scratch, Operand(0x80000000u));
1741 add(zeros, zeros, Operand(1), LeaveCC, eq);
1742#endif
1743}
1744
1745
1746void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1747 Register first,
1748 Register second,
1749 Register scratch1,
1750 Register scratch2,
1751 Label* failure) {
1752 int kFlatAsciiStringMask =
1753 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1754 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1755 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1756 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1757 cmp(scratch1, Operand(kFlatAsciiStringTag));
1758 // Ignore second test if first test failed.
1759 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1760 b(ne, failure);
1761}
1762
1763
1764void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1765 Register scratch,
1766 Label* failure) {
1767 int kFlatAsciiStringMask =
1768 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1769 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1770 and_(scratch, type, Operand(kFlatAsciiStringMask));
1771 cmp(scratch, Operand(kFlatAsciiStringTag));
1772 b(ne, failure);
1773}
1774
1775
1776void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1777 int frame_alignment = ActivationFrameAlignment();
1778 // Up to four simple arguments are passed in registers r0..r3.
1779 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1780 if (frame_alignment > kPointerSize) {
1781 // Make stack end at alignment and make room for num_arguments - 4 words
1782 // and the original value of sp.
1783 mov(scratch, sp);
1784 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
1785 ASSERT(IsPowerOf2(frame_alignment));
1786 and_(sp, sp, Operand(-frame_alignment));
1787 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1788 } else {
1789 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1790 }
1791}
1792
1793
1794void MacroAssembler::CallCFunction(ExternalReference function,
1795 int num_arguments) {
1796 mov(ip, Operand(function));
1797 CallCFunction(ip, num_arguments);
1798}
1799
1800
1801void MacroAssembler::CallCFunction(Register function, int num_arguments) {
1802 // Make sure that the stack is aligned before calling a C function unless
1803 // running in the simulator. The simulator has its own alignment check which
1804 // provides more information.
1805#if defined(V8_HOST_ARCH_ARM)
1806 if (FLAG_debug_code) {
1807 int frame_alignment = OS::ActivationFrameAlignment();
1808 int frame_alignment_mask = frame_alignment - 1;
1809 if (frame_alignment > kPointerSize) {
1810 ASSERT(IsPowerOf2(frame_alignment));
1811 Label alignment_as_expected;
1812 tst(sp, Operand(frame_alignment_mask));
1813 b(eq, &alignment_as_expected);
1814 // Don't use Check here, as it will call Runtime_Abort possibly
1815 // re-entering here.
1816 stop("Unexpected alignment");
1817 bind(&alignment_as_expected);
1818 }
1819 }
1820#endif
1821
1822 // Just call directly. The function called cannot cause a GC, or
1823 // allow preemption, so the return address in the link register
1824 // stays correct.
1825 Call(function);
1826 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1827 if (OS::ActivationFrameAlignment() > kPointerSize) {
1828 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1829 } else {
1830 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1831 }
1832}
1833
1834
Steve Blocka7e24c12009-10-30 11:49:00 +00001835#ifdef ENABLE_DEBUGGER_SUPPORT
1836CodePatcher::CodePatcher(byte* address, int instructions)
1837 : address_(address),
1838 instructions_(instructions),
1839 size_(instructions * Assembler::kInstrSize),
1840 masm_(address, size_ + Assembler::kGap) {
1841 // Create a new macro assembler pointing to the address of the code to patch.
1842 // The size is adjusted with kGap on order for the assembler to generate size
1843 // bytes of instructions without failing with buffer size constraints.
1844 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1845}
1846
1847
1848CodePatcher::~CodePatcher() {
1849 // Indicate that code has changed.
1850 CPU::FlushICache(address_, size_);
1851
1852 // Check that the code was patched as expected.
1853 ASSERT(masm_.pc_ == address_ + size_);
1854 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1855}
1856
1857
1858void CodePatcher::Emit(Instr x) {
1859 masm()->emit(x);
1860}
1861
1862
1863void CodePatcher::Emit(Address addr) {
1864 masm()->emit(reinterpret_cast<Instr>(addr));
1865}
1866#endif // ENABLE_DEBUGGER_SUPPORT
1867
1868
1869} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001870
1871#endif // V8_TARGET_ARCH_ARM