blob: 35544312f1c6771bc4044c7d63a7749b2108c966 [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
Iain Merrick9ac36c92010-09-13 15:29:50 +010028#include <limits.h> // For LONG_MIN, LONG_MAX.
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#include "v8.h"
31
Leon Clarkef7060e22010-06-03 12:02:55 +010032#if defined(V8_TARGET_ARCH_ARM)
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "bootstrapper.h"
35#include "codegen-inl.h"
36#include "debug.h"
37#include "runtime.h"
38
39namespace v8 {
40namespace internal {
41
42MacroAssembler::MacroAssembler(void* buffer, int size)
43 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000044 generating_stub_(false),
45 allow_stub_calls_(true),
46 code_object_(Heap::undefined_value()) {
47}
48
49
50// We always generate arm code, never thumb code, even if V8 is compiled to
51// thumb, so we require inter-working support
52#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
53#error "flag -mthumb-interwork missing"
54#endif
55
56
57// We do not support thumb inter-working with an arm architecture not supporting
58// the blx instruction (below v5t). If you know what CPU you are compiling for
59// you can use -march=armv7 or similar.
60#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
61# error "For thumb inter-working we require an architecture which supports blx"
62#endif
63
64
Steve Blocka7e24c12009-10-30 11:49:00 +000065// Using bx does not yield better code, so use it only when required
66#if defined(USE_THUMB_INTERWORK)
67#define USE_BX 1
68#endif
69
70
71void MacroAssembler::Jump(Register target, Condition cond) {
72#if USE_BX
73 bx(target, cond);
74#else
75 mov(pc, Operand(target), LeaveCC, cond);
76#endif
77}
78
79
80void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
81 Condition cond) {
82#if USE_BX
83 mov(ip, Operand(target, rmode), LeaveCC, cond);
84 bx(ip, cond);
85#else
86 mov(pc, Operand(target, rmode), LeaveCC, cond);
87#endif
88}
89
90
91void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
92 Condition cond) {
93 ASSERT(!RelocInfo::IsCodeTarget(rmode));
94 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
95}
96
97
98void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
99 Condition cond) {
100 ASSERT(RelocInfo::IsCodeTarget(rmode));
101 // 'code' is always generated ARM code, never THUMB code
102 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
103}
104
105
106void MacroAssembler::Call(Register target, Condition cond) {
107#if USE_BLX
108 blx(target, cond);
109#else
110 // set lr for return at current pc + 8
111 mov(lr, Operand(pc), LeaveCC, cond);
112 mov(pc, Operand(target), LeaveCC, cond);
113#endif
114}
115
116
117void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
118 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100119#if USE_BLX
120 // On ARMv5 and after the recommended call sequence is:
121 // ldr ip, [pc, #...]
122 // blx ip
123
124 // The two instructions (ldr and blx) could be separated by a constant
125 // pool and the code would still work. The issue comes from the
126 // patching code which expect the ldr to be just above the blx.
127 { BlockConstPoolScope block_const_pool(this);
128 // Statement positions are expected to be recorded when the target
129 // address is loaded. The mov method will automatically record
130 // positions when pc is the target, since this is not the case here
131 // we have to do it explicitly.
132 WriteRecordedPositions();
133
134 mov(ip, Operand(target, rmode), LeaveCC, cond);
135 blx(ip, cond);
136 }
137
138 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
139#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 // Set lr for return at current pc + 8.
141 mov(lr, Operand(pc), LeaveCC, cond);
142 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
143 mov(pc, Operand(target, rmode), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100146#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000147}
148
149
150void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
151 Condition cond) {
152 ASSERT(!RelocInfo::IsCodeTarget(rmode));
153 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
154}
155
156
157void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
158 Condition cond) {
159 ASSERT(RelocInfo::IsCodeTarget(rmode));
160 // 'code' is always generated ARM code, never THUMB code
161 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
162}
163
164
165void MacroAssembler::Ret(Condition cond) {
166#if USE_BX
167 bx(lr, cond);
168#else
169 mov(pc, Operand(lr), LeaveCC, cond);
170#endif
171}
172
173
Steve Blockd0582a62009-12-15 09:54:21 +0000174void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
175 LoadRoot(ip, Heap::kStackLimitRootIndex);
176 cmp(sp, Operand(ip));
177 b(lo, on_stack_overflow);
178}
179
180
Leon Clarkee46be812010-01-19 14:06:41 +0000181void MacroAssembler::Drop(int count, Condition cond) {
182 if (count > 0) {
183 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
184 }
185}
186
187
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100188void MacroAssembler::Swap(Register reg1,
189 Register reg2,
190 Register scratch,
191 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100192 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100193 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
194 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
195 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100196 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100197 mov(scratch, reg1, LeaveCC, cond);
198 mov(reg1, reg2, LeaveCC, cond);
199 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100200 }
201}
202
203
Leon Clarkee46be812010-01-19 14:06:41 +0000204void MacroAssembler::Call(Label* target) {
205 bl(target);
206}
207
208
209void MacroAssembler::Move(Register dst, Handle<Object> value) {
210 mov(dst, Operand(value));
211}
Steve Blockd0582a62009-12-15 09:54:21 +0000212
213
Steve Block6ded16b2010-05-10 14:33:55 +0100214void MacroAssembler::Move(Register dst, Register src) {
215 if (!dst.is(src)) {
216 mov(dst, src);
217 }
218}
219
220
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100221void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
222 Condition cond) {
223 if (!CpuFeatures::IsSupported(ARMv7) || src2.is_single_instruction()) {
224 and_(dst, src1, src2, LeaveCC, cond);
225 return;
226 }
227 int32_t immediate = src2.immediate();
228 if (immediate == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100229 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100230 return;
231 }
232 if (IsPowerOf2(immediate + 1) && ((immediate & 1) != 0)) {
233 ubfx(dst, src1, 0, WhichPowerOf2(immediate + 1), cond);
234 return;
235 }
236 and_(dst, src1, src2, LeaveCC, cond);
237}
238
239
240void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
241 Condition cond) {
242 ASSERT(lsb < 32);
243 if (!CpuFeatures::IsSupported(ARMv7)) {
244 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
245 and_(dst, src1, Operand(mask), LeaveCC, cond);
246 if (lsb != 0) {
247 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
248 }
249 } else {
250 ubfx(dst, src1, lsb, width, cond);
251 }
252}
253
254
255void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
256 Condition cond) {
257 ASSERT(lsb < 32);
258 if (!CpuFeatures::IsSupported(ARMv7)) {
259 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
260 and_(dst, src1, Operand(mask), LeaveCC, cond);
261 int shift_up = 32 - lsb - width;
262 int shift_down = lsb + shift_up;
263 if (shift_up != 0) {
264 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
265 }
266 if (shift_down != 0) {
267 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
268 }
269 } else {
270 sbfx(dst, src1, lsb, width, cond);
271 }
272}
273
274
275void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
276 ASSERT(lsb < 32);
277 if (!CpuFeatures::IsSupported(ARMv7)) {
278 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
279 bic(dst, dst, Operand(mask));
280 } else {
281 bfc(dst, lsb, width, cond);
282 }
283}
284
285
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100286void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
287 Condition cond) {
288 if (!CpuFeatures::IsSupported(ARMv7)) {
289 ASSERT(!dst.is(pc) && !src.rm().is(pc));
290 ASSERT((satpos >= 0) && (satpos <= 31));
291
292 // These asserts are required to ensure compatibility with the ARMv7
293 // implementation.
294 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
295 ASSERT(src.rs().is(no_reg));
296
297 Label done;
298 int satval = (1 << satpos) - 1;
299
300 if (cond != al) {
301 b(NegateCondition(cond), &done); // Skip saturate if !condition.
302 }
303 if (!(src.is_reg() && dst.is(src.rm()))) {
304 mov(dst, src);
305 }
306 tst(dst, Operand(~satval));
307 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100308 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100309 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
310 bind(&done);
311 } else {
312 usat(dst, satpos, src, cond);
313 }
314}
315
316
Steve Blocka7e24c12009-10-30 11:49:00 +0000317void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
318 // Empty the const pool.
319 CheckConstPool(true, true);
320 add(pc, pc, Operand(index,
321 LSL,
322 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
323 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
324 nop(); // Jump table alignment.
325 for (int i = 0; i < targets.length(); i++) {
326 b(targets[i]);
327 }
328}
329
330
331void MacroAssembler::LoadRoot(Register destination,
332 Heap::RootListIndex index,
333 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000334 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000335}
336
337
Kristian Monsen25f61362010-05-21 11:50:48 +0100338void MacroAssembler::StoreRoot(Register source,
339 Heap::RootListIndex index,
340 Condition cond) {
341 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
342}
343
344
Steve Block6ded16b2010-05-10 14:33:55 +0100345void MacroAssembler::RecordWriteHelper(Register object,
Steve Block8defd9f2010-07-08 12:39:36 +0100346 Register address,
347 Register scratch) {
Steve Block6ded16b2010-05-10 14:33:55 +0100348 if (FLAG_debug_code) {
349 // Check that the object is not in new space.
350 Label not_in_new_space;
Steve Block8defd9f2010-07-08 12:39:36 +0100351 InNewSpace(object, scratch, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100352 Abort("new-space object passed to RecordWriteHelper");
353 bind(&not_in_new_space);
354 }
Leon Clarke4515c472010-02-03 11:58:03 +0000355
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100356 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100357 Bfc(object, 0, kPageSizeBits);
358
359 // Calculate region number.
Steve Block8defd9f2010-07-08 12:39:36 +0100360 Ubfx(address, address, Page::kRegionSizeLog2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100361 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000362
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100363 // Mark region dirty.
Steve Block8defd9f2010-07-08 12:39:36 +0100364 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 mov(ip, Operand(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100366 orr(scratch, scratch, Operand(ip, LSL, address));
367 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100368}
369
370
371void MacroAssembler::InNewSpace(Register object,
372 Register scratch,
373 Condition cc,
374 Label* branch) {
375 ASSERT(cc == eq || cc == ne);
376 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
377 cmp(scratch, Operand(ExternalReference::new_space_start()));
378 b(cc, branch);
379}
380
381
382// Will clobber 4 registers: object, offset, scratch, ip. The
383// register 'object' contains a heap object pointer. The heap object
384// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100385void MacroAssembler::RecordWrite(Register object,
386 Operand offset,
387 Register scratch0,
388 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100389 // The compiled code assumes that record write doesn't change the
390 // context register, so we check that none of the clobbered
391 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100392 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100393
394 Label done;
395
396 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100397 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100398 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100399
Steve Block8defd9f2010-07-08 12:39:36 +0100400 // Add offset into the object.
401 add(scratch0, object, offset);
402
Steve Block6ded16b2010-05-10 14:33:55 +0100403 // Record the actual write.
Steve Block8defd9f2010-07-08 12:39:36 +0100404 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000405
406 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000407
408 // Clobber all input registers when running with the debug-code flag
409 // turned on to provoke errors.
410 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100411 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100412 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
413 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000414 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000415}
416
417
Steve Block8defd9f2010-07-08 12:39:36 +0100418// Will clobber 4 registers: object, address, scratch, ip. The
419// register 'object' contains a heap object pointer. The heap object
420// tag is shifted away.
421void MacroAssembler::RecordWrite(Register object,
422 Register address,
423 Register scratch) {
424 // The compiled code assumes that record write doesn't change the
425 // context register, so we check that none of the clobbered
426 // registers are cp.
427 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
428
429 Label done;
430
431 // First, test that the object is not in the new space. We cannot set
432 // region marks for new space pages.
433 InNewSpace(object, scratch, eq, &done);
434
435 // Record the actual write.
436 RecordWriteHelper(object, address, scratch);
437
438 bind(&done);
439
440 // Clobber all input registers when running with the debug-code flag
441 // turned on to provoke errors.
442 if (FLAG_debug_code) {
443 mov(object, Operand(BitCast<int32_t>(kZapValue)));
444 mov(address, Operand(BitCast<int32_t>(kZapValue)));
445 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
446 }
447}
448
449
Leon Clarkef7060e22010-06-03 12:02:55 +0100450void MacroAssembler::Ldrd(Register dst1, Register dst2,
451 const MemOperand& src, Condition cond) {
452 ASSERT(src.rm().is(no_reg));
453 ASSERT(!dst1.is(lr)); // r14.
454 ASSERT_EQ(0, dst1.code() % 2);
455 ASSERT_EQ(dst1.code() + 1, dst2.code());
456
457 // Generate two ldr instructions if ldrd is not available.
458 if (CpuFeatures::IsSupported(ARMv7)) {
459 CpuFeatures::Scope scope(ARMv7);
460 ldrd(dst1, dst2, src, cond);
461 } else {
462 MemOperand src2(src);
463 src2.set_offset(src2.offset() + 4);
464 if (dst1.is(src.rn())) {
465 ldr(dst2, src2, cond);
466 ldr(dst1, src, cond);
467 } else {
468 ldr(dst1, src, cond);
469 ldr(dst2, src2, cond);
470 }
471 }
472}
473
474
475void MacroAssembler::Strd(Register src1, Register src2,
476 const MemOperand& dst, Condition cond) {
477 ASSERT(dst.rm().is(no_reg));
478 ASSERT(!src1.is(lr)); // r14.
479 ASSERT_EQ(0, src1.code() % 2);
480 ASSERT_EQ(src1.code() + 1, src2.code());
481
482 // Generate two str instructions if strd is not available.
483 if (CpuFeatures::IsSupported(ARMv7)) {
484 CpuFeatures::Scope scope(ARMv7);
485 strd(src1, src2, dst, cond);
486 } else {
487 MemOperand dst2(dst);
488 dst2.set_offset(dst2.offset() + 4);
489 str(src1, dst, cond);
490 str(src2, dst2, cond);
491 }
492}
493
494
Steve Blocka7e24c12009-10-30 11:49:00 +0000495void MacroAssembler::EnterFrame(StackFrame::Type type) {
496 // r0-r3: preserved
497 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
498 mov(ip, Operand(Smi::FromInt(type)));
499 push(ip);
500 mov(ip, Operand(CodeObject()));
501 push(ip);
502 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
503}
504
505
506void MacroAssembler::LeaveFrame(StackFrame::Type type) {
507 // r0: preserved
508 // r1: preserved
509 // r2: preserved
510
511 // Drop the execution stack down to the frame pointer and restore
512 // the caller frame pointer and return address.
513 mov(sp, fp);
514 ldm(ia_w, sp, fp.bit() | lr.bit());
515}
516
517
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100518void MacroAssembler::EnterExitFrame() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 // Compute the argv pointer and keep it in a callee-saved register.
520 // r0 is argc.
521 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
522 sub(r6, r6, Operand(kPointerSize));
523
524 // Compute callee's stack pointer before making changes and save it as
525 // ip register so that it is restored as sp register on exit, thereby
526 // popping the args.
527
528 // ip = sp + kPointerSize * #args;
529 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
530
Steve Block6ded16b2010-05-10 14:33:55 +0100531 // Prepare the stack to be aligned when calling into C. After this point there
532 // are 5 pushes before the call into C, so the stack needs to be aligned after
533 // 5 pushes.
534 int frame_alignment = ActivationFrameAlignment();
535 int frame_alignment_mask = frame_alignment - 1;
536 if (frame_alignment != kPointerSize) {
537 // The following code needs to be more general if this assert does not hold.
538 ASSERT(frame_alignment == 2 * kPointerSize);
539 // With 5 pushes left the frame must be unaligned at this point.
540 mov(r7, Operand(Smi::FromInt(0)));
541 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
542 push(r7, eq); // Push if aligned to make it unaligned.
543 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000544
545 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
546 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000547 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000548
Andrei Popescu402d9372010-02-26 13:31:12 +0000549 mov(ip, Operand(CodeObject()));
550 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000551
552 // Save the frame pointer and the context in top.
553 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
554 str(fp, MemOperand(ip));
555 mov(ip, Operand(ExternalReference(Top::k_context_address)));
556 str(cp, MemOperand(ip));
557
558 // Setup argc and the builtin function in callee-saved registers.
559 mov(r4, Operand(r0));
560 mov(r5, Operand(r1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000561}
562
563
Steve Block6ded16b2010-05-10 14:33:55 +0100564void MacroAssembler::InitializeNewString(Register string,
565 Register length,
566 Heap::RootListIndex map_index,
567 Register scratch1,
568 Register scratch2) {
569 mov(scratch1, Operand(length, LSL, kSmiTagSize));
570 LoadRoot(scratch2, map_index);
571 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
572 mov(scratch1, Operand(String::kEmptyHashField));
573 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
574 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
575}
576
577
578int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000579#if defined(V8_HOST_ARCH_ARM)
580 // Running on the real platform. Use the alignment as mandated by the local
581 // environment.
582 // Note: This will break if we ever start generating snapshots on one ARM
583 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100584 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000585#else // defined(V8_HOST_ARCH_ARM)
586 // If we are using the simulator then we should always align to the expected
587 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100588 // if the target platform will need alignment, so this is controlled from a
589 // flag.
590 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000591#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000592}
593
594
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100595void MacroAssembler::LeaveExitFrame() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100597 mov(r3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
599 str(r3, MemOperand(ip));
600
601 // Restore current context from top and clear it in debug mode.
602 mov(ip, Operand(ExternalReference(Top::k_context_address)));
603 ldr(cp, MemOperand(ip));
604#ifdef DEBUG
605 str(r3, MemOperand(ip));
606#endif
607
608 // Pop the arguments, restore registers, and return.
609 mov(sp, Operand(fp)); // respect ABI stack constraint
610 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
611}
612
613
614void MacroAssembler::InvokePrologue(const ParameterCount& expected,
615 const ParameterCount& actual,
616 Handle<Code> code_constant,
617 Register code_reg,
618 Label* done,
619 InvokeFlag flag) {
620 bool definitely_matches = false;
621 Label regular_invoke;
622
623 // Check whether the expected and actual arguments count match. If not,
624 // setup registers according to contract with ArgumentsAdaptorTrampoline:
625 // r0: actual arguments count
626 // r1: function (passed through to callee)
627 // r2: expected arguments count
628 // r3: callee code entry
629
630 // The code below is made a lot easier because the calling code already sets
631 // up actual and expected registers according to the contract if values are
632 // passed in registers.
633 ASSERT(actual.is_immediate() || actual.reg().is(r0));
634 ASSERT(expected.is_immediate() || expected.reg().is(r2));
635 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
636
637 if (expected.is_immediate()) {
638 ASSERT(actual.is_immediate());
639 if (expected.immediate() == actual.immediate()) {
640 definitely_matches = true;
641 } else {
642 mov(r0, Operand(actual.immediate()));
643 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
644 if (expected.immediate() == sentinel) {
645 // Don't worry about adapting arguments for builtins that
646 // don't want that done. Skip adaption code by making it look
647 // like we have a match between expected and actual number of
648 // arguments.
649 definitely_matches = true;
650 } else {
651 mov(r2, Operand(expected.immediate()));
652 }
653 }
654 } else {
655 if (actual.is_immediate()) {
656 cmp(expected.reg(), Operand(actual.immediate()));
657 b(eq, &regular_invoke);
658 mov(r0, Operand(actual.immediate()));
659 } else {
660 cmp(expected.reg(), Operand(actual.reg()));
661 b(eq, &regular_invoke);
662 }
663 }
664
665 if (!definitely_matches) {
666 if (!code_constant.is_null()) {
667 mov(r3, Operand(code_constant));
668 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
669 }
670
671 Handle<Code> adaptor =
672 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
673 if (flag == CALL_FUNCTION) {
674 Call(adaptor, RelocInfo::CODE_TARGET);
675 b(done);
676 } else {
677 Jump(adaptor, RelocInfo::CODE_TARGET);
678 }
679 bind(&regular_invoke);
680 }
681}
682
683
684void MacroAssembler::InvokeCode(Register code,
685 const ParameterCount& expected,
686 const ParameterCount& actual,
687 InvokeFlag flag) {
688 Label done;
689
690 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
691 if (flag == CALL_FUNCTION) {
692 Call(code);
693 } else {
694 ASSERT(flag == JUMP_FUNCTION);
695 Jump(code);
696 }
697
698 // Continue here if InvokePrologue does handle the invocation due to
699 // mismatched parameter counts.
700 bind(&done);
701}
702
703
704void MacroAssembler::InvokeCode(Handle<Code> code,
705 const ParameterCount& expected,
706 const ParameterCount& actual,
707 RelocInfo::Mode rmode,
708 InvokeFlag flag) {
709 Label done;
710
711 InvokePrologue(expected, actual, code, no_reg, &done, flag);
712 if (flag == CALL_FUNCTION) {
713 Call(code, rmode);
714 } else {
715 Jump(code, rmode);
716 }
717
718 // Continue here if InvokePrologue does handle the invocation due to
719 // mismatched parameter counts.
720 bind(&done);
721}
722
723
724void MacroAssembler::InvokeFunction(Register fun,
725 const ParameterCount& actual,
726 InvokeFlag flag) {
727 // Contract with called JS functions requires that function is passed in r1.
728 ASSERT(fun.is(r1));
729
730 Register expected_reg = r2;
731 Register code_reg = r3;
732
733 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
734 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
735 ldr(expected_reg,
736 FieldMemOperand(code_reg,
737 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100738 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100740 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000741
742 ParameterCount expected(expected_reg);
743 InvokeCode(code_reg, expected, actual, flag);
744}
745
746
Andrei Popescu402d9372010-02-26 13:31:12 +0000747void MacroAssembler::InvokeFunction(JSFunction* function,
748 const ParameterCount& actual,
749 InvokeFlag flag) {
750 ASSERT(function->is_compiled());
751
752 // Get the function and setup the context.
753 mov(r1, Operand(Handle<JSFunction>(function)));
754 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
755
756 // Invoke the cached code.
757 Handle<Code> code(function->code());
758 ParameterCount expected(function->shared()->formal_parameter_count());
759 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
760}
761
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000764void MacroAssembler::DebugBreak() {
765 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100766 mov(r0, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000767 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
768 CEntryStub ces(1);
769 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
770}
Steve Blocka7e24c12009-10-30 11:49:00 +0000771#endif
772
773
774void MacroAssembler::PushTryHandler(CodeLocation try_location,
775 HandlerType type) {
776 // Adjust this code if not the case.
777 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
778 // The pc (return address) is passed in register lr.
779 if (try_location == IN_JAVASCRIPT) {
780 if (type == TRY_CATCH_HANDLER) {
781 mov(r3, Operand(StackHandler::TRY_CATCH));
782 } else {
783 mov(r3, Operand(StackHandler::TRY_FINALLY));
784 }
785 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
786 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
787 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
788 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
789 // Save the current handler as the next handler.
790 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
791 ldr(r1, MemOperand(r3));
792 ASSERT(StackHandlerConstants::kNextOffset == 0);
793 push(r1);
794 // Link this handler as the new current one.
795 str(sp, MemOperand(r3));
796 } else {
797 // Must preserve r0-r4, r5-r7 are available.
798 ASSERT(try_location == IN_JS_ENTRY);
799 // The frame pointer does not point to a JS frame so we save NULL
800 // for fp. We expect the code throwing an exception to check fp
801 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100802 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000803 mov(r6, Operand(StackHandler::ENTRY));
804 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
805 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
806 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
807 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
808 // Save the current handler as the next handler.
809 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
810 ldr(r6, MemOperand(r7));
811 ASSERT(StackHandlerConstants::kNextOffset == 0);
812 push(r6);
813 // Link this handler as the new current one.
814 str(sp, MemOperand(r7));
815 }
816}
817
818
Leon Clarkee46be812010-01-19 14:06:41 +0000819void MacroAssembler::PopTryHandler() {
820 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
821 pop(r1);
822 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
823 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
824 str(r1, MemOperand(ip));
825}
826
827
Steve Blocka7e24c12009-10-30 11:49:00 +0000828void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
829 Register scratch,
830 Label* miss) {
831 Label same_contexts;
832
833 ASSERT(!holder_reg.is(scratch));
834 ASSERT(!holder_reg.is(ip));
835 ASSERT(!scratch.is(ip));
836
837 // Load current lexical context from the stack frame.
838 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
839 // In debug mode, make sure the lexical context is set.
840#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +0100841 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 Check(ne, "we should not have an empty lexical context");
843#endif
844
845 // Load the global context of the current context.
846 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
847 ldr(scratch, FieldMemOperand(scratch, offset));
848 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
849
850 // Check the context is a global context.
851 if (FLAG_debug_code) {
852 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
853 // Cannot use ip as a temporary in this verification code. Due to the fact
854 // that ip is clobbered as part of cmp with an object Operand.
855 push(holder_reg); // Temporarily save holder on the stack.
856 // Read the first word and compare to the global_context_map.
857 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
858 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
859 cmp(holder_reg, ip);
860 Check(eq, "JSGlobalObject::global_context should be a global context.");
861 pop(holder_reg); // Restore holder.
862 }
863
864 // Check if both contexts are the same.
865 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
866 cmp(scratch, Operand(ip));
867 b(eq, &same_contexts);
868
869 // Check the context is a global context.
870 if (FLAG_debug_code) {
871 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
872 // Cannot use ip as a temporary in this verification code. Due to the fact
873 // that ip is clobbered as part of cmp with an object Operand.
874 push(holder_reg); // Temporarily save holder on the stack.
875 mov(holder_reg, ip); // Move ip to its holding place.
876 LoadRoot(ip, Heap::kNullValueRootIndex);
877 cmp(holder_reg, ip);
878 Check(ne, "JSGlobalProxy::context() should not be null.");
879
880 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
881 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
882 cmp(holder_reg, ip);
883 Check(eq, "JSGlobalObject::global_context should be a global context.");
884 // Restore ip is not needed. ip is reloaded below.
885 pop(holder_reg); // Restore holder.
886 // Restore ip to holder's context.
887 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
888 }
889
890 // Check that the security token in the calling global object is
891 // compatible with the security token in the receiving global
892 // object.
893 int token_offset = Context::kHeaderSize +
894 Context::SECURITY_TOKEN_INDEX * kPointerSize;
895
896 ldr(scratch, FieldMemOperand(scratch, token_offset));
897 ldr(ip, FieldMemOperand(ip, token_offset));
898 cmp(scratch, Operand(ip));
899 b(ne, miss);
900
901 bind(&same_contexts);
902}
903
904
905void MacroAssembler::AllocateInNewSpace(int object_size,
906 Register result,
907 Register scratch1,
908 Register scratch2,
909 Label* gc_required,
910 AllocationFlags flags) {
911 ASSERT(!result.is(scratch1));
912 ASSERT(!scratch1.is(scratch2));
913
Kristian Monsen25f61362010-05-21 11:50:48 +0100914 // Make object size into bytes.
915 if ((flags & SIZE_IN_WORDS) != 0) {
916 object_size *= kPointerSize;
917 }
918 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
919
Steve Blocka7e24c12009-10-30 11:49:00 +0000920 // Load address of new object into result and allocation top address into
921 // scratch1.
922 ExternalReference new_space_allocation_top =
923 ExternalReference::new_space_allocation_top_address();
924 mov(scratch1, Operand(new_space_allocation_top));
925 if ((flags & RESULT_CONTAINS_TOP) == 0) {
926 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000927 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000928 // Assert that result actually contains top on entry. scratch2 is used
929 // immediately below so this use of scratch2 does not cause difference with
930 // respect to register content between debug and release mode.
931 ldr(scratch2, MemOperand(scratch1));
932 cmp(result, scratch2);
933 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000934 }
935
936 // Calculate new top and bail out if new space is exhausted. Use result
937 // to calculate the new top.
938 ExternalReference new_space_allocation_limit =
939 ExternalReference::new_space_allocation_limit_address();
940 mov(scratch2, Operand(new_space_allocation_limit));
941 ldr(scratch2, MemOperand(scratch2));
Kristian Monsen25f61362010-05-21 11:50:48 +0100942 add(result, result, Operand(object_size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 cmp(result, Operand(scratch2));
944 b(hi, gc_required);
Steve Blocka7e24c12009-10-30 11:49:00 +0000945 str(result, MemOperand(scratch1));
946
947 // Tag and adjust back to start of new object.
948 if ((flags & TAG_OBJECT) != 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100949 sub(result, result, Operand(object_size - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000950 } else {
Kristian Monsen25f61362010-05-21 11:50:48 +0100951 sub(result, result, Operand(object_size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000952 }
953}
954
955
956void MacroAssembler::AllocateInNewSpace(Register object_size,
957 Register result,
958 Register scratch1,
959 Register scratch2,
960 Label* gc_required,
961 AllocationFlags flags) {
962 ASSERT(!result.is(scratch1));
963 ASSERT(!scratch1.is(scratch2));
964
965 // Load address of new object into result and allocation top address into
966 // scratch1.
967 ExternalReference new_space_allocation_top =
968 ExternalReference::new_space_allocation_top_address();
969 mov(scratch1, Operand(new_space_allocation_top));
970 if ((flags & RESULT_CONTAINS_TOP) == 0) {
971 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000972 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000973 // Assert that result actually contains top on entry. scratch2 is used
974 // immediately below so this use of scratch2 does not cause difference with
975 // respect to register content between debug and release mode.
976 ldr(scratch2, MemOperand(scratch1));
977 cmp(result, scratch2);
978 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000979 }
980
981 // Calculate new top and bail out if new space is exhausted. Use result
982 // to calculate the new top. Object size is in words so a shift is required to
983 // get the number of bytes
984 ExternalReference new_space_allocation_limit =
985 ExternalReference::new_space_allocation_limit_address();
986 mov(scratch2, Operand(new_space_allocation_limit));
987 ldr(scratch2, MemOperand(scratch2));
Kristian Monsen25f61362010-05-21 11:50:48 +0100988 if ((flags & SIZE_IN_WORDS) != 0) {
989 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
990 } else {
991 add(result, result, Operand(object_size));
992 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 cmp(result, Operand(scratch2));
994 b(hi, gc_required);
995
Steve Blockd0582a62009-12-15 09:54:21 +0000996 // Update allocation top. result temporarily holds the new top.
997 if (FLAG_debug_code) {
998 tst(result, Operand(kObjectAlignmentMask));
999 Check(eq, "Unaligned allocation in new space");
1000 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 str(result, MemOperand(scratch1));
1002
1003 // Adjust back to start of new object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001004 if ((flags & SIZE_IN_WORDS) != 0) {
1005 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1006 } else {
1007 sub(result, result, Operand(object_size));
1008 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001009
1010 // Tag object if requested.
1011 if ((flags & TAG_OBJECT) != 0) {
1012 add(result, result, Operand(kHeapObjectTag));
1013 }
1014}
1015
1016
1017void MacroAssembler::UndoAllocationInNewSpace(Register object,
1018 Register scratch) {
1019 ExternalReference new_space_allocation_top =
1020 ExternalReference::new_space_allocation_top_address();
1021
1022 // Make sure the object has no tag before resetting top.
1023 and_(object, object, Operand(~kHeapObjectTagMask));
1024#ifdef DEBUG
1025 // Check that the object un-allocated is below the current top.
1026 mov(scratch, Operand(new_space_allocation_top));
1027 ldr(scratch, MemOperand(scratch));
1028 cmp(object, scratch);
1029 Check(lt, "Undo allocation of non allocated memory");
1030#endif
1031 // Write the address of the object to un-allocate as the current top.
1032 mov(scratch, Operand(new_space_allocation_top));
1033 str(object, MemOperand(scratch));
1034}
1035
1036
Andrei Popescu31002712010-02-23 13:46:05 +00001037void MacroAssembler::AllocateTwoByteString(Register result,
1038 Register length,
1039 Register scratch1,
1040 Register scratch2,
1041 Register scratch3,
1042 Label* gc_required) {
1043 // Calculate the number of bytes needed for the characters in the string while
1044 // observing object alignment.
1045 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1046 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1047 add(scratch1, scratch1,
1048 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001049 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001050
1051 // Allocate two-byte string in new space.
1052 AllocateInNewSpace(scratch1,
1053 result,
1054 scratch2,
1055 scratch3,
1056 gc_required,
1057 TAG_OBJECT);
1058
1059 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001060 InitializeNewString(result,
1061 length,
1062 Heap::kStringMapRootIndex,
1063 scratch1,
1064 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001065}
1066
1067
1068void MacroAssembler::AllocateAsciiString(Register result,
1069 Register length,
1070 Register scratch1,
1071 Register scratch2,
1072 Register scratch3,
1073 Label* gc_required) {
1074 // Calculate the number of bytes needed for the characters in the string while
1075 // observing object alignment.
1076 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1077 ASSERT(kCharSize == 1);
1078 add(scratch1, length,
1079 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001080 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001081
1082 // Allocate ASCII string in new space.
1083 AllocateInNewSpace(scratch1,
1084 result,
1085 scratch2,
1086 scratch3,
1087 gc_required,
1088 TAG_OBJECT);
1089
1090 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001091 InitializeNewString(result,
1092 length,
1093 Heap::kAsciiStringMapRootIndex,
1094 scratch1,
1095 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001096}
1097
1098
1099void MacroAssembler::AllocateTwoByteConsString(Register result,
1100 Register length,
1101 Register scratch1,
1102 Register scratch2,
1103 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001104 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001105 result,
1106 scratch1,
1107 scratch2,
1108 gc_required,
1109 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001110
1111 InitializeNewString(result,
1112 length,
1113 Heap::kConsStringMapRootIndex,
1114 scratch1,
1115 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001116}
1117
1118
1119void MacroAssembler::AllocateAsciiConsString(Register result,
1120 Register length,
1121 Register scratch1,
1122 Register scratch2,
1123 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001124 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001125 result,
1126 scratch1,
1127 scratch2,
1128 gc_required,
1129 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001130
1131 InitializeNewString(result,
1132 length,
1133 Heap::kConsAsciiStringMapRootIndex,
1134 scratch1,
1135 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001136}
1137
1138
Steve Block6ded16b2010-05-10 14:33:55 +01001139void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001140 Register map,
1141 Register type_reg,
1142 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001143 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001144 CompareInstanceType(map, type_reg, type);
1145}
1146
1147
1148void MacroAssembler::CompareInstanceType(Register map,
1149 Register type_reg,
1150 InstanceType type) {
1151 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1152 cmp(type_reg, Operand(type));
1153}
1154
1155
Andrei Popescu31002712010-02-23 13:46:05 +00001156void MacroAssembler::CheckMap(Register obj,
1157 Register scratch,
1158 Handle<Map> map,
1159 Label* fail,
1160 bool is_heap_object) {
1161 if (!is_heap_object) {
1162 BranchOnSmi(obj, fail);
1163 }
1164 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1165 mov(ip, Operand(map));
1166 cmp(scratch, ip);
1167 b(ne, fail);
1168}
1169
1170
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001171void MacroAssembler::CheckMap(Register obj,
1172 Register scratch,
1173 Heap::RootListIndex index,
1174 Label* fail,
1175 bool is_heap_object) {
1176 if (!is_heap_object) {
1177 BranchOnSmi(obj, fail);
1178 }
1179 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1180 LoadRoot(ip, index);
1181 cmp(scratch, ip);
1182 b(ne, fail);
1183}
1184
1185
Steve Blocka7e24c12009-10-30 11:49:00 +00001186void MacroAssembler::TryGetFunctionPrototype(Register function,
1187 Register result,
1188 Register scratch,
1189 Label* miss) {
1190 // Check that the receiver isn't a smi.
1191 BranchOnSmi(function, miss);
1192
1193 // Check that the function really is a function. Load map into result reg.
1194 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1195 b(ne, miss);
1196
1197 // Make sure that the function has an instance prototype.
1198 Label non_instance;
1199 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1200 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1201 b(ne, &non_instance);
1202
1203 // Get the prototype or initial map from the function.
1204 ldr(result,
1205 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1206
1207 // If the prototype or initial map is the hole, don't return it and
1208 // simply miss the cache instead. This will allow us to allocate a
1209 // prototype object on-demand in the runtime system.
1210 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1211 cmp(result, ip);
1212 b(eq, miss);
1213
1214 // If the function does not have an initial map, we're done.
1215 Label done;
1216 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1217 b(ne, &done);
1218
1219 // Get the prototype from the initial map.
1220 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1221 jmp(&done);
1222
1223 // Non-instance prototype: Fetch prototype from constructor field
1224 // in initial map.
1225 bind(&non_instance);
1226 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1227
1228 // All done.
1229 bind(&done);
1230}
1231
1232
1233void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1234 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1235 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1236}
1237
1238
Andrei Popescu31002712010-02-23 13:46:05 +00001239void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1240 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1241 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1242}
1243
1244
Leon Clarkeac952652010-07-15 11:15:24 +01001245void MacroAssembler::StubReturn(int argc, Condition cond) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 ASSERT(argc >= 1 && generating_stub());
Andrei Popescu31002712010-02-23 13:46:05 +00001247 if (argc > 1) {
Leon Clarkeac952652010-07-15 11:15:24 +01001248 add(sp, sp, Operand((argc - 1) * kPointerSize), LeaveCC, cond);
Andrei Popescu31002712010-02-23 13:46:05 +00001249 }
Leon Clarkeac952652010-07-15 11:15:24 +01001250 Ret(cond);
Steve Blocka7e24c12009-10-30 11:49:00 +00001251}
1252
1253
1254void MacroAssembler::IllegalOperation(int num_arguments) {
1255 if (num_arguments > 0) {
1256 add(sp, sp, Operand(num_arguments * kPointerSize));
1257 }
1258 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1259}
1260
1261
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001262void MacroAssembler::IndexFromHash(Register hash, Register index) {
1263 // If the hash field contains an array index pick it out. The assert checks
1264 // that the constants for the maximum number of digits for an array index
1265 // cached in the hash field and the number of bits reserved for it does not
1266 // conflict.
1267 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1268 (1 << String::kArrayIndexValueBits));
1269 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1270 // the low kHashShift bits.
1271 STATIC_ASSERT(kSmiTag == 0);
1272 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1273 mov(index, Operand(hash, LSL, kSmiTagSize));
1274}
1275
1276
Steve Blockd0582a62009-12-15 09:54:21 +00001277void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1278 Register outHighReg,
1279 Register outLowReg) {
1280 // ARMv7 VFP3 instructions to implement integer to double conversion.
1281 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001282 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001283 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001284 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001285}
1286
1287
Steve Block8defd9f2010-07-08 12:39:36 +01001288void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1289 DwVfpRegister result,
1290 Register scratch1,
1291 Register scratch2,
1292 Register heap_number_map,
1293 SwVfpRegister scratch3,
1294 Label* not_number,
1295 ObjectToDoubleFlags flags) {
1296 Label done;
1297 if ((flags & OBJECT_NOT_SMI) == 0) {
1298 Label not_smi;
1299 BranchOnNotSmi(object, &not_smi);
1300 // Remove smi tag and convert to double.
1301 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1302 vmov(scratch3, scratch1);
1303 vcvt_f64_s32(result, scratch3);
1304 b(&done);
1305 bind(&not_smi);
1306 }
1307 // Check for heap number and load double value from it.
1308 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1309 sub(scratch2, object, Operand(kHeapObjectTag));
1310 cmp(scratch1, heap_number_map);
1311 b(ne, not_number);
1312 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1313 // If exponent is all ones the number is either a NaN or +/-Infinity.
1314 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1315 Sbfx(scratch1,
1316 scratch1,
1317 HeapNumber::kExponentShift,
1318 HeapNumber::kExponentBits);
1319 // All-one value sign extend to -1.
1320 cmp(scratch1, Operand(-1));
1321 b(eq, not_number);
1322 }
1323 vldr(result, scratch2, HeapNumber::kValueOffset);
1324 bind(&done);
1325}
1326
1327
1328void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1329 DwVfpRegister value,
1330 Register scratch1,
1331 SwVfpRegister scratch2) {
1332 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1333 vmov(scratch2, scratch1);
1334 vcvt_f64_s32(value, scratch2);
1335}
1336
1337
Iain Merrick9ac36c92010-09-13 15:29:50 +01001338// Tries to get a signed int32 out of a double precision floating point heap
1339// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1340// 32bits signed integer range.
1341void MacroAssembler::ConvertToInt32(Register source,
1342 Register dest,
1343 Register scratch,
1344 Register scratch2,
1345 Label *not_int32) {
1346 if (CpuFeatures::IsSupported(VFP3)) {
1347 CpuFeatures::Scope scope(VFP3);
1348 sub(scratch, source, Operand(kHeapObjectTag));
1349 vldr(d0, scratch, HeapNumber::kValueOffset);
1350 vcvt_s32_f64(s0, d0);
1351 vmov(dest, s0);
1352 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1353 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1354 // range. When substracting one, the minimum signed integer becomes the
1355 // maximun signed integer.
1356 sub(scratch, dest, Operand(1));
1357 cmp(scratch, Operand(LONG_MAX - 1));
1358 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1359 b(ge, not_int32);
1360 } else {
1361 // This code is faster for doubles that are in the ranges -0x7fffffff to
1362 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1363 // the range of signed int32 values that are not Smis. Jumps to the label
1364 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1365 // 0x80000000.0 (excluding the endpoints).
1366 Label right_exponent, done;
1367 // Get exponent word.
1368 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1369 // Get exponent alone in scratch2.
1370 Ubfx(scratch2,
1371 scratch,
1372 HeapNumber::kExponentShift,
1373 HeapNumber::kExponentBits);
1374 // Load dest with zero. We use this either for the final shift or
1375 // for the answer.
1376 mov(dest, Operand(0, RelocInfo::NONE));
1377 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1378 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1379 // the exponent that we are fastest at and also the highest exponent we can
1380 // handle here.
1381 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1382 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1383 // split it up to avoid a constant pool entry. You can't do that in general
1384 // for cmp because of the overflow flag, but we know the exponent is in the
1385 // range 0-2047 so there is no overflow.
1386 int fudge_factor = 0x400;
1387 sub(scratch2, scratch2, Operand(fudge_factor));
1388 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1389 // If we have a match of the int32-but-not-Smi exponent then skip some
1390 // logic.
1391 b(eq, &right_exponent);
1392 // If the exponent is higher than that then go to slow case. This catches
1393 // numbers that don't fit in a signed int32, infinities and NaNs.
1394 b(gt, not_int32);
1395
1396 // We know the exponent is smaller than 30 (biased). If it is less than
1397 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1398 // it rounds to zero.
1399 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1400 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1401 // Dest already has a Smi zero.
1402 b(lt, &done);
1403
1404 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1405 // get how much to shift down.
1406 rsb(dest, scratch2, Operand(30));
1407
1408 bind(&right_exponent);
1409 // Get the top bits of the mantissa.
1410 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1411 // Put back the implicit 1.
1412 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1413 // Shift up the mantissa bits to take up the space the exponent used to
1414 // take. We just orred in the implicit bit so that took care of one and
1415 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1416 // distance.
1417 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1418 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1419 // Put sign in zero flag.
1420 tst(scratch, Operand(HeapNumber::kSignMask));
1421 // Get the second half of the double. For some exponents we don't
1422 // actually need this because the bits get shifted out again, but
1423 // it's probably slower to test than just to do it.
1424 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1425 // Shift down 22 bits to get the last 10 bits.
1426 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1427 // Move down according to the exponent.
1428 mov(dest, Operand(scratch, LSR, dest));
1429 // Fix sign if sign bit was set.
1430 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1431 bind(&done);
1432 }
1433}
1434
1435
Andrei Popescu31002712010-02-23 13:46:05 +00001436void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1437 Register src,
1438 int num_least_bits) {
1439 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001440 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001441 } else {
1442 mov(dst, Operand(src, ASR, kSmiTagSize));
1443 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1444 }
1445}
1446
1447
Steve Blocka7e24c12009-10-30 11:49:00 +00001448void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1449 // All parameters are on the stack. r0 has the return value after call.
1450
1451 // If the expected number of arguments of the runtime function is
1452 // constant, we check that the actual number of arguments match the
1453 // expectation.
1454 if (f->nargs >= 0 && f->nargs != num_arguments) {
1455 IllegalOperation(num_arguments);
1456 return;
1457 }
1458
Leon Clarke4515c472010-02-03 11:58:03 +00001459 // TODO(1236192): Most runtime routines don't need the number of
1460 // arguments passed in because it is constant. At some point we
1461 // should remove this need and make the runtime routine entry code
1462 // smarter.
1463 mov(r0, Operand(num_arguments));
1464 mov(r1, Operand(ExternalReference(f)));
1465 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001466 CallStub(&stub);
1467}
1468
1469
1470void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1471 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1472}
1473
1474
Andrei Popescu402d9372010-02-26 13:31:12 +00001475void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1476 int num_arguments) {
1477 mov(r0, Operand(num_arguments));
1478 mov(r1, Operand(ext));
1479
1480 CEntryStub stub(1);
1481 CallStub(&stub);
1482}
1483
1484
Steve Block6ded16b2010-05-10 14:33:55 +01001485void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1486 int num_arguments,
1487 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001488 // TODO(1236192): Most runtime routines don't need the number of
1489 // arguments passed in because it is constant. At some point we
1490 // should remove this need and make the runtime routine entry code
1491 // smarter.
1492 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001493 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001494}
1495
1496
Steve Block6ded16b2010-05-10 14:33:55 +01001497void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1498 int num_arguments,
1499 int result_size) {
1500 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1501}
1502
1503
1504void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001505#if defined(__thumb__)
1506 // Thumb mode builtin.
1507 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1508#endif
1509 mov(r1, Operand(builtin));
1510 CEntryStub stub(1);
1511 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1512}
1513
1514
Steve Blocka7e24c12009-10-30 11:49:00 +00001515void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1516 InvokeJSFlags flags) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001517 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001518 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001519 Call(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001520 } else {
1521 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001522 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001523 }
1524}
1525
1526
Steve Block791712a2010-08-27 10:21:07 +01001527void MacroAssembler::GetBuiltinFunction(Register target,
1528 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001529 // Load the builtins object into target register.
1530 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1531 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001532 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001533 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01001534 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1535}
1536
1537
1538void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1539 ASSERT(!target.is(r1));
1540 GetBuiltinFunction(r1, id);
1541 // Load the code entry point from the builtins object.
1542 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001543}
1544
1545
1546void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1547 Register scratch1, Register scratch2) {
1548 if (FLAG_native_code_counters && counter->Enabled()) {
1549 mov(scratch1, Operand(value));
1550 mov(scratch2, Operand(ExternalReference(counter)));
1551 str(scratch1, MemOperand(scratch2));
1552 }
1553}
1554
1555
1556void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1557 Register scratch1, Register scratch2) {
1558 ASSERT(value > 0);
1559 if (FLAG_native_code_counters && counter->Enabled()) {
1560 mov(scratch2, Operand(ExternalReference(counter)));
1561 ldr(scratch1, MemOperand(scratch2));
1562 add(scratch1, scratch1, Operand(value));
1563 str(scratch1, MemOperand(scratch2));
1564 }
1565}
1566
1567
1568void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1569 Register scratch1, Register scratch2) {
1570 ASSERT(value > 0);
1571 if (FLAG_native_code_counters && counter->Enabled()) {
1572 mov(scratch2, Operand(ExternalReference(counter)));
1573 ldr(scratch1, MemOperand(scratch2));
1574 sub(scratch1, scratch1, Operand(value));
1575 str(scratch1, MemOperand(scratch2));
1576 }
1577}
1578
1579
1580void MacroAssembler::Assert(Condition cc, const char* msg) {
1581 if (FLAG_debug_code)
1582 Check(cc, msg);
1583}
1584
1585
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001586void MacroAssembler::AssertRegisterIsRoot(Register reg,
1587 Heap::RootListIndex index) {
1588 if (FLAG_debug_code) {
1589 LoadRoot(ip, index);
1590 cmp(reg, ip);
1591 Check(eq, "Register did not match expected root");
1592 }
1593}
1594
1595
Iain Merrick75681382010-08-19 15:07:18 +01001596void MacroAssembler::AssertFastElements(Register elements) {
1597 if (FLAG_debug_code) {
1598 ASSERT(!elements.is(ip));
1599 Label ok;
1600 push(elements);
1601 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
1602 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
1603 cmp(elements, ip);
1604 b(eq, &ok);
1605 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
1606 cmp(elements, ip);
1607 b(eq, &ok);
1608 Abort("JSObject with fast elements map has slow elements");
1609 bind(&ok);
1610 pop(elements);
1611 }
1612}
1613
1614
Steve Blocka7e24c12009-10-30 11:49:00 +00001615void MacroAssembler::Check(Condition cc, const char* msg) {
1616 Label L;
1617 b(cc, &L);
1618 Abort(msg);
1619 // will not return here
1620 bind(&L);
1621}
1622
1623
1624void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01001625 Label abort_start;
1626 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00001627 // We want to pass the msg string like a smi to avoid GC
1628 // problems, however msg is not guaranteed to be aligned
1629 // properly. Instead, we pass an aligned pointer that is
1630 // a proper v8 smi, but also pass the alignment difference
1631 // from the real pointer as a smi.
1632 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1633 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1634 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1635#ifdef DEBUG
1636 if (msg != NULL) {
1637 RecordComment("Abort message: ");
1638 RecordComment(msg);
1639 }
1640#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001641 // Disable stub call restrictions to always allow calls to abort.
1642 set_allow_stub_calls(true);
1643
Steve Blocka7e24c12009-10-30 11:49:00 +00001644 mov(r0, Operand(p0));
1645 push(r0);
1646 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1647 push(r0);
1648 CallRuntime(Runtime::kAbort, 2);
1649 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01001650 if (is_const_pool_blocked()) {
1651 // If the calling code cares about the exact number of
1652 // instructions generated, we insert padding here to keep the size
1653 // of the Abort macro constant.
1654 static const int kExpectedAbortInstructions = 10;
1655 int abort_instructions = InstructionsGeneratedSince(&abort_start);
1656 ASSERT(abort_instructions <= kExpectedAbortInstructions);
1657 while (abort_instructions++ < kExpectedAbortInstructions) {
1658 nop();
1659 }
1660 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001661}
1662
1663
Steve Blockd0582a62009-12-15 09:54:21 +00001664void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1665 if (context_chain_length > 0) {
1666 // Move up the chain of contexts to the context containing the slot.
1667 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1668 // Load the function context (which is the incoming, outer context).
1669 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1670 for (int i = 1; i < context_chain_length; i++) {
1671 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1672 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1673 }
1674 // The context may be an intermediate context, not a function context.
1675 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1676 } else { // Slot is in the current function context.
1677 // The context may be an intermediate context, not a function context.
1678 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1679 }
1680}
1681
1682
Andrei Popescu31002712010-02-23 13:46:05 +00001683void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1684 Register reg2,
1685 Label* on_not_both_smi) {
1686 ASSERT_EQ(0, kSmiTag);
1687 tst(reg1, Operand(kSmiTagMask));
1688 tst(reg2, Operand(kSmiTagMask), eq);
1689 b(ne, on_not_both_smi);
1690}
1691
1692
1693void MacroAssembler::JumpIfEitherSmi(Register reg1,
1694 Register reg2,
1695 Label* on_either_smi) {
1696 ASSERT_EQ(0, kSmiTag);
1697 tst(reg1, Operand(kSmiTagMask));
1698 tst(reg2, Operand(kSmiTagMask), ne);
1699 b(eq, on_either_smi);
1700}
1701
1702
Iain Merrick75681382010-08-19 15:07:18 +01001703void MacroAssembler::AbortIfSmi(Register object) {
1704 ASSERT_EQ(0, kSmiTag);
1705 tst(object, Operand(kSmiTagMask));
1706 Assert(ne, "Operand is a smi");
1707}
1708
1709
Leon Clarked91b9f72010-01-27 17:25:45 +00001710void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1711 Register first,
1712 Register second,
1713 Register scratch1,
1714 Register scratch2,
1715 Label* failure) {
1716 // Test that both first and second are sequential ASCII strings.
1717 // Assume that they are non-smis.
1718 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1719 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1720 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1721 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001722
1723 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1724 scratch2,
1725 scratch1,
1726 scratch2,
1727 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001728}
1729
1730void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1731 Register second,
1732 Register scratch1,
1733 Register scratch2,
1734 Label* failure) {
1735 // Check that neither is a smi.
1736 ASSERT_EQ(0, kSmiTag);
1737 and_(scratch1, first, Operand(second));
1738 tst(scratch1, Operand(kSmiTagMask));
1739 b(eq, failure);
1740 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1741 second,
1742 scratch1,
1743 scratch2,
1744 failure);
1745}
1746
Steve Blockd0582a62009-12-15 09:54:21 +00001747
Steve Block6ded16b2010-05-10 14:33:55 +01001748// Allocates a heap number or jumps to the need_gc label if the young space
1749// is full and a scavenge is needed.
1750void MacroAssembler::AllocateHeapNumber(Register result,
1751 Register scratch1,
1752 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001753 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01001754 Label* gc_required) {
1755 // Allocate an object in the heap for the heap number and tag it as a heap
1756 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001757 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001758 result,
1759 scratch1,
1760 scratch2,
1761 gc_required,
1762 TAG_OBJECT);
1763
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001764 // Store heap number map in the allocated object.
1765 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1766 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001767}
1768
1769
Steve Block8defd9f2010-07-08 12:39:36 +01001770void MacroAssembler::AllocateHeapNumberWithValue(Register result,
1771 DwVfpRegister value,
1772 Register scratch1,
1773 Register scratch2,
1774 Register heap_number_map,
1775 Label* gc_required) {
1776 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
1777 sub(scratch1, result, Operand(kHeapObjectTag));
1778 vstr(value, scratch1, HeapNumber::kValueOffset);
1779}
1780
1781
Ben Murdochbb769b22010-08-11 14:56:33 +01001782// Copies a fixed number of fields of heap objects from src to dst.
1783void MacroAssembler::CopyFields(Register dst,
1784 Register src,
1785 RegList temps,
1786 int field_count) {
1787 // At least one bit set in the first 15 registers.
1788 ASSERT((temps & ((1 << 15) - 1)) != 0);
1789 ASSERT((temps & dst.bit()) == 0);
1790 ASSERT((temps & src.bit()) == 0);
1791 // Primitive implementation using only one temporary register.
1792
1793 Register tmp = no_reg;
1794 // Find a temp register in temps list.
1795 for (int i = 0; i < 15; i++) {
1796 if ((temps & (1 << i)) != 0) {
1797 tmp.set_code(i);
1798 break;
1799 }
1800 }
1801 ASSERT(!tmp.is(no_reg));
1802
1803 for (int i = 0; i < field_count; i++) {
1804 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
1805 str(tmp, FieldMemOperand(dst, i * kPointerSize));
1806 }
1807}
1808
1809
Steve Block8defd9f2010-07-08 12:39:36 +01001810void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
1811 Register source, // Input.
1812 Register scratch) {
1813 ASSERT(!zeros.is(source) || !source.is(zeros));
1814 ASSERT(!zeros.is(scratch));
1815 ASSERT(!scratch.is(ip));
1816 ASSERT(!source.is(ip));
1817 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01001818#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1819 clz(zeros, source); // This instruction is only supported after ARM5.
1820#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01001821 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01001822 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01001823 // Top 16.
1824 tst(scratch, Operand(0xffff0000));
1825 add(zeros, zeros, Operand(16), LeaveCC, eq);
1826 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1827 // Top 8.
1828 tst(scratch, Operand(0xff000000));
1829 add(zeros, zeros, Operand(8), LeaveCC, eq);
1830 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1831 // Top 4.
1832 tst(scratch, Operand(0xf0000000));
1833 add(zeros, zeros, Operand(4), LeaveCC, eq);
1834 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1835 // Top 2.
1836 tst(scratch, Operand(0xc0000000));
1837 add(zeros, zeros, Operand(2), LeaveCC, eq);
1838 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1839 // Top bit.
1840 tst(scratch, Operand(0x80000000u));
1841 add(zeros, zeros, Operand(1), LeaveCC, eq);
1842#endif
1843}
1844
1845
1846void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1847 Register first,
1848 Register second,
1849 Register scratch1,
1850 Register scratch2,
1851 Label* failure) {
1852 int kFlatAsciiStringMask =
1853 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1854 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1855 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1856 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1857 cmp(scratch1, Operand(kFlatAsciiStringTag));
1858 // Ignore second test if first test failed.
1859 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1860 b(ne, failure);
1861}
1862
1863
1864void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1865 Register scratch,
1866 Label* failure) {
1867 int kFlatAsciiStringMask =
1868 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1869 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1870 and_(scratch, type, Operand(kFlatAsciiStringMask));
1871 cmp(scratch, Operand(kFlatAsciiStringTag));
1872 b(ne, failure);
1873}
1874
1875
1876void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
1877 int frame_alignment = ActivationFrameAlignment();
1878 // Up to four simple arguments are passed in registers r0..r3.
1879 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1880 if (frame_alignment > kPointerSize) {
1881 // Make stack end at alignment and make room for num_arguments - 4 words
1882 // and the original value of sp.
1883 mov(scratch, sp);
1884 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
1885 ASSERT(IsPowerOf2(frame_alignment));
1886 and_(sp, sp, Operand(-frame_alignment));
1887 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1888 } else {
1889 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1890 }
1891}
1892
1893
1894void MacroAssembler::CallCFunction(ExternalReference function,
1895 int num_arguments) {
1896 mov(ip, Operand(function));
1897 CallCFunction(ip, num_arguments);
1898}
1899
1900
1901void MacroAssembler::CallCFunction(Register function, int num_arguments) {
1902 // Make sure that the stack is aligned before calling a C function unless
1903 // running in the simulator. The simulator has its own alignment check which
1904 // provides more information.
1905#if defined(V8_HOST_ARCH_ARM)
1906 if (FLAG_debug_code) {
1907 int frame_alignment = OS::ActivationFrameAlignment();
1908 int frame_alignment_mask = frame_alignment - 1;
1909 if (frame_alignment > kPointerSize) {
1910 ASSERT(IsPowerOf2(frame_alignment));
1911 Label alignment_as_expected;
1912 tst(sp, Operand(frame_alignment_mask));
1913 b(eq, &alignment_as_expected);
1914 // Don't use Check here, as it will call Runtime_Abort possibly
1915 // re-entering here.
1916 stop("Unexpected alignment");
1917 bind(&alignment_as_expected);
1918 }
1919 }
1920#endif
1921
1922 // Just call directly. The function called cannot cause a GC, or
1923 // allow preemption, so the return address in the link register
1924 // stays correct.
1925 Call(function);
1926 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1927 if (OS::ActivationFrameAlignment() > kPointerSize) {
1928 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1929 } else {
1930 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1931 }
1932}
1933
1934
Steve Blocka7e24c12009-10-30 11:49:00 +00001935#ifdef ENABLE_DEBUGGER_SUPPORT
1936CodePatcher::CodePatcher(byte* address, int instructions)
1937 : address_(address),
1938 instructions_(instructions),
1939 size_(instructions * Assembler::kInstrSize),
1940 masm_(address, size_ + Assembler::kGap) {
1941 // Create a new macro assembler pointing to the address of the code to patch.
1942 // The size is adjusted with kGap on order for the assembler to generate size
1943 // bytes of instructions without failing with buffer size constraints.
1944 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1945}
1946
1947
1948CodePatcher::~CodePatcher() {
1949 // Indicate that code has changed.
1950 CPU::FlushICache(address_, size_);
1951
1952 // Check that the code was patched as expected.
1953 ASSERT(masm_.pc_ == address_ + size_);
1954 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1955}
1956
1957
1958void CodePatcher::Emit(Instr x) {
1959 masm()->emit(x);
1960}
1961
1962
1963void CodePatcher::Emit(Address addr) {
1964 masm()->emit(reinterpret_cast<Instr>(addr));
1965}
1966#endif // ENABLE_DEBUGGER_SUPPORT
1967
1968
1969} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001970
1971#endif // V8_TARGET_ARCH_ARM