blob: d431f6a9f77516ef355227fbcf4ae8fb06fd2c78 [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
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.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800132 positions_recorder()->WriteRecordedPositions();
Steve Block6ded16b2010-05-10 14:33:55 +0100133
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
Leon Clarkee46be812010-01-19 14:06:41 +0000174void MacroAssembler::Drop(int count, Condition cond) {
175 if (count > 0) {
176 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
177 }
178}
179
180
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181void MacroAssembler::Ret(int drop, Condition cond) {
182 Drop(drop, cond);
183 Ret(cond);
184}
185
186
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100187void MacroAssembler::Swap(Register reg1,
188 Register reg2,
189 Register scratch,
190 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100191 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100192 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
193 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
194 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100195 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196 mov(scratch, reg1, LeaveCC, cond);
197 mov(reg1, reg2, LeaveCC, cond);
198 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100199 }
200}
201
202
Leon Clarkee46be812010-01-19 14:06:41 +0000203void MacroAssembler::Call(Label* target) {
204 bl(target);
205}
206
207
208void MacroAssembler::Move(Register dst, Handle<Object> value) {
209 mov(dst, Operand(value));
210}
Steve Blockd0582a62009-12-15 09:54:21 +0000211
212
Steve Block6ded16b2010-05-10 14:33:55 +0100213void MacroAssembler::Move(Register dst, Register src) {
214 if (!dst.is(src)) {
215 mov(dst, src);
216 }
217}
218
219
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100220void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
221 Condition cond) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800222 if (!src2.is_reg() &&
223 !src2.must_use_constant_pool() &&
224 src2.immediate() == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100225 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800226
227 } else if (!src2.is_single_instruction() &&
228 !src2.must_use_constant_pool() &&
229 CpuFeatures::IsSupported(ARMv7) &&
230 IsPowerOf2(src2.immediate() + 1)) {
231 ubfx(dst, src1, 0, WhichPowerOf2(src2.immediate() + 1), cond);
232
233 } else {
234 and_(dst, src1, src2, LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100235 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100236}
237
238
239void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
240 Condition cond) {
241 ASSERT(lsb < 32);
242 if (!CpuFeatures::IsSupported(ARMv7)) {
243 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
244 and_(dst, src1, Operand(mask), LeaveCC, cond);
245 if (lsb != 0) {
246 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
247 }
248 } else {
249 ubfx(dst, src1, lsb, width, cond);
250 }
251}
252
253
254void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
255 Condition cond) {
256 ASSERT(lsb < 32);
257 if (!CpuFeatures::IsSupported(ARMv7)) {
258 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
259 and_(dst, src1, Operand(mask), LeaveCC, cond);
260 int shift_up = 32 - lsb - width;
261 int shift_down = lsb + shift_up;
262 if (shift_up != 0) {
263 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
264 }
265 if (shift_down != 0) {
266 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
267 }
268 } else {
269 sbfx(dst, src1, lsb, width, cond);
270 }
271}
272
273
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100274void MacroAssembler::Bfi(Register dst,
275 Register src,
276 Register scratch,
277 int lsb,
278 int width,
279 Condition cond) {
280 ASSERT(0 <= lsb && lsb < 32);
281 ASSERT(0 <= width && width < 32);
282 ASSERT(lsb + width < 32);
283 ASSERT(!scratch.is(dst));
284 if (width == 0) return;
285 if (!CpuFeatures::IsSupported(ARMv7)) {
286 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
287 bic(dst, dst, Operand(mask));
288 and_(scratch, src, Operand((1 << width) - 1));
289 mov(scratch, Operand(scratch, LSL, lsb));
290 orr(dst, dst, scratch);
291 } else {
292 bfi(dst, src, lsb, width, cond);
293 }
294}
295
296
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100297void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
298 ASSERT(lsb < 32);
299 if (!CpuFeatures::IsSupported(ARMv7)) {
300 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
301 bic(dst, dst, Operand(mask));
302 } else {
303 bfc(dst, lsb, width, cond);
304 }
305}
306
307
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100308void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
309 Condition cond) {
310 if (!CpuFeatures::IsSupported(ARMv7)) {
311 ASSERT(!dst.is(pc) && !src.rm().is(pc));
312 ASSERT((satpos >= 0) && (satpos <= 31));
313
314 // These asserts are required to ensure compatibility with the ARMv7
315 // implementation.
316 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
317 ASSERT(src.rs().is(no_reg));
318
319 Label done;
320 int satval = (1 << satpos) - 1;
321
322 if (cond != al) {
323 b(NegateCondition(cond), &done); // Skip saturate if !condition.
324 }
325 if (!(src.is_reg() && dst.is(src.rm()))) {
326 mov(dst, src);
327 }
328 tst(dst, Operand(~satval));
329 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100330 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100331 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
332 bind(&done);
333 } else {
334 usat(dst, satpos, src, cond);
335 }
336}
337
338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
340 // Empty the const pool.
341 CheckConstPool(true, true);
342 add(pc, pc, Operand(index,
343 LSL,
Steve Block1e0659c2011-05-24 12:43:12 +0100344 Instruction::kInstrSizeLog2 - kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
346 nop(); // Jump table alignment.
347 for (int i = 0; i < targets.length(); i++) {
348 b(targets[i]);
349 }
350}
351
352
353void MacroAssembler::LoadRoot(Register destination,
354 Heap::RootListIndex index,
355 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000356 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000357}
358
359
Kristian Monsen25f61362010-05-21 11:50:48 +0100360void MacroAssembler::StoreRoot(Register source,
361 Heap::RootListIndex index,
362 Condition cond) {
363 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
364}
365
366
Steve Block6ded16b2010-05-10 14:33:55 +0100367void MacroAssembler::RecordWriteHelper(Register object,
Steve Block8defd9f2010-07-08 12:39:36 +0100368 Register address,
369 Register scratch) {
Steve Block6ded16b2010-05-10 14:33:55 +0100370 if (FLAG_debug_code) {
371 // Check that the object is not in new space.
372 Label not_in_new_space;
Steve Block8defd9f2010-07-08 12:39:36 +0100373 InNewSpace(object, scratch, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100374 Abort("new-space object passed to RecordWriteHelper");
375 bind(&not_in_new_space);
376 }
Leon Clarke4515c472010-02-03 11:58:03 +0000377
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100378 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100379 Bfc(object, 0, kPageSizeBits);
380
381 // Calculate region number.
Steve Block8defd9f2010-07-08 12:39:36 +0100382 Ubfx(address, address, Page::kRegionSizeLog2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100383 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000384
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100385 // Mark region dirty.
Steve Block8defd9f2010-07-08 12:39:36 +0100386 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 mov(ip, Operand(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100388 orr(scratch, scratch, Operand(ip, LSL, address));
389 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100390}
391
392
393void MacroAssembler::InNewSpace(Register object,
394 Register scratch,
Steve Block1e0659c2011-05-24 12:43:12 +0100395 Condition cond,
Steve Block6ded16b2010-05-10 14:33:55 +0100396 Label* branch) {
Steve Block1e0659c2011-05-24 12:43:12 +0100397 ASSERT(cond == eq || cond == ne);
Steve Block6ded16b2010-05-10 14:33:55 +0100398 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
399 cmp(scratch, Operand(ExternalReference::new_space_start()));
Steve Block1e0659c2011-05-24 12:43:12 +0100400 b(cond, branch);
Steve Block6ded16b2010-05-10 14:33:55 +0100401}
402
403
404// Will clobber 4 registers: object, offset, scratch, ip. The
405// register 'object' contains a heap object pointer. The heap object
406// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100407void MacroAssembler::RecordWrite(Register object,
408 Operand offset,
409 Register scratch0,
410 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100411 // The compiled code assumes that record write doesn't change the
412 // context register, so we check that none of the clobbered
413 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100414 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100415
416 Label done;
417
418 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100419 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100420 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100421
Steve Block8defd9f2010-07-08 12:39:36 +0100422 // Add offset into the object.
423 add(scratch0, object, offset);
424
Steve Block6ded16b2010-05-10 14:33:55 +0100425 // Record the actual write.
Steve Block8defd9f2010-07-08 12:39:36 +0100426 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000427
428 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000429
430 // Clobber all input registers when running with the debug-code flag
431 // turned on to provoke errors.
432 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100433 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100434 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
435 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000436 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000437}
438
439
Steve Block8defd9f2010-07-08 12:39:36 +0100440// Will clobber 4 registers: object, address, scratch, ip. The
441// register 'object' contains a heap object pointer. The heap object
442// tag is shifted away.
443void MacroAssembler::RecordWrite(Register object,
444 Register address,
445 Register scratch) {
446 // The compiled code assumes that record write doesn't change the
447 // context register, so we check that none of the clobbered
448 // registers are cp.
449 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
450
451 Label done;
452
453 // First, test that the object is not in the new space. We cannot set
454 // region marks for new space pages.
455 InNewSpace(object, scratch, eq, &done);
456
457 // Record the actual write.
458 RecordWriteHelper(object, address, scratch);
459
460 bind(&done);
461
462 // Clobber all input registers when running with the debug-code flag
463 // turned on to provoke errors.
464 if (FLAG_debug_code) {
465 mov(object, Operand(BitCast<int32_t>(kZapValue)));
466 mov(address, Operand(BitCast<int32_t>(kZapValue)));
467 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
468 }
469}
470
471
Ben Murdochb0fe1622011-05-05 13:52:32 +0100472// Push and pop all registers that can hold pointers.
473void MacroAssembler::PushSafepointRegisters() {
474 // Safepoints expect a block of contiguous register values starting with r0:
475 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
476 // Safepoints expect a block of kNumSafepointRegisters values on the
477 // stack, so adjust the stack for unsaved registers.
478 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
479 ASSERT(num_unsaved >= 0);
480 sub(sp, sp, Operand(num_unsaved * kPointerSize));
481 stm(db_w, sp, kSafepointSavedRegisters);
482}
483
484
485void MacroAssembler::PopSafepointRegisters() {
486 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
487 ldm(ia_w, sp, kSafepointSavedRegisters);
488 add(sp, sp, Operand(num_unsaved * kPointerSize));
489}
490
491
Ben Murdochb8e0da22011-05-16 14:20:40 +0100492void MacroAssembler::PushSafepointRegistersAndDoubles() {
493 PushSafepointRegisters();
494 sub(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
495 kDoubleSize));
496 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
497 vstr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
498 }
499}
500
501
502void MacroAssembler::PopSafepointRegistersAndDoubles() {
503 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
504 vldr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
505 }
506 add(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
507 kDoubleSize));
508 PopSafepointRegisters();
509}
510
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100511void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register src,
512 Register dst) {
513 str(src, SafepointRegistersAndDoublesSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100514}
515
516
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100517void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
518 str(src, SafepointRegisterSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100519}
520
521
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100522void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
523 ldr(dst, SafepointRegisterSlot(src));
Steve Block1e0659c2011-05-24 12:43:12 +0100524}
525
526
Ben Murdochb0fe1622011-05-05 13:52:32 +0100527int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
528 // The registers are pushed starting with the highest encoding,
529 // which means that lowest encodings are closest to the stack pointer.
530 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
531 return reg_code;
532}
533
534
Steve Block1e0659c2011-05-24 12:43:12 +0100535MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
536 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
537}
538
539
540MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
541 // General purpose registers are pushed last on the stack.
542 int doubles_size = DwVfpRegister::kNumAllocatableRegisters * kDoubleSize;
543 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
544 return MemOperand(sp, doubles_size + register_offset);
545}
546
547
Leon Clarkef7060e22010-06-03 12:02:55 +0100548void MacroAssembler::Ldrd(Register dst1, Register dst2,
549 const MemOperand& src, Condition cond) {
550 ASSERT(src.rm().is(no_reg));
551 ASSERT(!dst1.is(lr)); // r14.
552 ASSERT_EQ(0, dst1.code() % 2);
553 ASSERT_EQ(dst1.code() + 1, dst2.code());
554
555 // Generate two ldr instructions if ldrd is not available.
556 if (CpuFeatures::IsSupported(ARMv7)) {
557 CpuFeatures::Scope scope(ARMv7);
558 ldrd(dst1, dst2, src, cond);
559 } else {
560 MemOperand src2(src);
561 src2.set_offset(src2.offset() + 4);
562 if (dst1.is(src.rn())) {
563 ldr(dst2, src2, cond);
564 ldr(dst1, src, cond);
565 } else {
566 ldr(dst1, src, cond);
567 ldr(dst2, src2, cond);
568 }
569 }
570}
571
572
573void MacroAssembler::Strd(Register src1, Register src2,
574 const MemOperand& dst, Condition cond) {
575 ASSERT(dst.rm().is(no_reg));
576 ASSERT(!src1.is(lr)); // r14.
577 ASSERT_EQ(0, src1.code() % 2);
578 ASSERT_EQ(src1.code() + 1, src2.code());
579
580 // Generate two str instructions if strd is not available.
581 if (CpuFeatures::IsSupported(ARMv7)) {
582 CpuFeatures::Scope scope(ARMv7);
583 strd(src1, src2, dst, cond);
584 } else {
585 MemOperand dst2(dst);
586 dst2.set_offset(dst2.offset() + 4);
587 str(src1, dst, cond);
588 str(src2, dst2, cond);
589 }
590}
591
592
Ben Murdochb8e0da22011-05-16 14:20:40 +0100593void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
594 const Register scratch,
595 const Condition cond) {
596 vmrs(scratch, cond);
597 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
598 vmsr(scratch, cond);
599}
600
601
602void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
603 const DwVfpRegister src2,
604 const Condition cond) {
605 // Compare and move FPSCR flags to the normal condition flags.
606 VFPCompareAndLoadFlags(src1, src2, pc, cond);
607}
608
609void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
610 const double src2,
611 const Condition cond) {
612 // Compare and move FPSCR flags to the normal condition flags.
613 VFPCompareAndLoadFlags(src1, src2, pc, cond);
614}
615
616
617void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
618 const DwVfpRegister src2,
619 const Register fpscr_flags,
620 const Condition cond) {
621 // Compare and load FPSCR.
622 vcmp(src1, src2, cond);
623 vmrs(fpscr_flags, cond);
624}
625
626void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
627 const double src2,
628 const Register fpscr_flags,
629 const Condition cond) {
630 // Compare and load FPSCR.
631 vcmp(src1, src2, cond);
632 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100633}
634
635
Steve Blocka7e24c12009-10-30 11:49:00 +0000636void MacroAssembler::EnterFrame(StackFrame::Type type) {
637 // r0-r3: preserved
638 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
639 mov(ip, Operand(Smi::FromInt(type)));
640 push(ip);
641 mov(ip, Operand(CodeObject()));
642 push(ip);
643 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
644}
645
646
647void MacroAssembler::LeaveFrame(StackFrame::Type type) {
648 // r0: preserved
649 // r1: preserved
650 // r2: preserved
651
652 // Drop the execution stack down to the frame pointer and restore
653 // the caller frame pointer and return address.
654 mov(sp, fp);
655 ldm(ia_w, sp, fp.bit() | lr.bit());
656}
657
658
Steve Block1e0659c2011-05-24 12:43:12 +0100659void MacroAssembler::EnterExitFrame(bool save_doubles, int stack_space) {
660 // Setup the frame structure on the stack.
661 ASSERT_EQ(2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
662 ASSERT_EQ(1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
663 ASSERT_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
664 Push(lr, fp);
Andrei Popescu402d9372010-02-26 13:31:12 +0000665 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Block1e0659c2011-05-24 12:43:12 +0100666 // Reserve room for saved entry sp and code object.
667 sub(sp, sp, Operand(2 * kPointerSize));
668 if (FLAG_debug_code) {
669 mov(ip, Operand(0));
670 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
671 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000672 mov(ip, Operand(CodeObject()));
Steve Block1e0659c2011-05-24 12:43:12 +0100673 str(ip, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000674
675 // Save the frame pointer and the context in top.
676 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
677 str(fp, MemOperand(ip));
678 mov(ip, Operand(ExternalReference(Top::k_context_address)));
679 str(cp, MemOperand(ip));
680
Ben Murdochb0fe1622011-05-05 13:52:32 +0100681 // Optionally save all double registers.
682 if (save_doubles) {
Steve Block1e0659c2011-05-24 12:43:12 +0100683 sub(sp, sp, Operand(DwVfpRegister::kNumRegisters * kDoubleSize));
684 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100685 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
686 DwVfpRegister reg = DwVfpRegister::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100687 vstr(reg, fp, offset - ((i + 1) * kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100688 }
Steve Block1e0659c2011-05-24 12:43:12 +0100689 // Note that d0 will be accessible at
690 // fp - 2 * kPointerSize - DwVfpRegister::kNumRegisters * kDoubleSize,
691 // since the sp slot and code slot were pushed after the fp.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100692 }
Steve Block1e0659c2011-05-24 12:43:12 +0100693
694 // Reserve place for the return address and stack space and align the frame
695 // preparing for calling the runtime function.
696 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
697 sub(sp, sp, Operand((stack_space + 1) * kPointerSize));
698 if (frame_alignment > 0) {
699 ASSERT(IsPowerOf2(frame_alignment));
700 and_(sp, sp, Operand(-frame_alignment));
701 }
702
703 // Set the exit frame sp value to point just before the return address
704 // location.
705 add(ip, sp, Operand(kPointerSize));
706 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000707}
708
709
Steve Block6ded16b2010-05-10 14:33:55 +0100710void MacroAssembler::InitializeNewString(Register string,
711 Register length,
712 Heap::RootListIndex map_index,
713 Register scratch1,
714 Register scratch2) {
715 mov(scratch1, Operand(length, LSL, kSmiTagSize));
716 LoadRoot(scratch2, map_index);
717 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
718 mov(scratch1, Operand(String::kEmptyHashField));
719 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
720 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
721}
722
723
724int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000725#if defined(V8_HOST_ARCH_ARM)
726 // Running on the real platform. Use the alignment as mandated by the local
727 // environment.
728 // Note: This will break if we ever start generating snapshots on one ARM
729 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100730 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000731#else // defined(V8_HOST_ARCH_ARM)
732 // If we are using the simulator then we should always align to the expected
733 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100734 // if the target platform will need alignment, so this is controlled from a
735 // flag.
736 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000737#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000738}
739
740
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100741void MacroAssembler::LeaveExitFrame(bool save_doubles,
742 Register argument_count) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100743 // Optionally restore all double registers.
744 if (save_doubles) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100745 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
746 DwVfpRegister reg = DwVfpRegister::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100747 const int offset = -2 * kPointerSize;
748 vldr(reg, fp, offset - ((i + 1) * kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100749 }
750 }
751
Steve Blocka7e24c12009-10-30 11:49:00 +0000752 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100753 mov(r3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000754 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
755 str(r3, MemOperand(ip));
756
757 // Restore current context from top and clear it in debug mode.
758 mov(ip, Operand(ExternalReference(Top::k_context_address)));
759 ldr(cp, MemOperand(ip));
760#ifdef DEBUG
761 str(r3, MemOperand(ip));
762#endif
763
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100764 // Tear down the exit frame, pop the arguments, and return.
Steve Block1e0659c2011-05-24 12:43:12 +0100765 mov(sp, Operand(fp));
766 ldm(ia_w, sp, fp.bit() | lr.bit());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100767 if (argument_count.is_valid()) {
768 add(sp, sp, Operand(argument_count, LSL, kPointerSizeLog2));
769 }
770}
771
772void MacroAssembler::GetCFunctionDoubleResult(const DoubleRegister dst) {
773#if !defined(USE_ARM_EABI)
774 UNREACHABLE();
775#else
776 vmov(dst, r0, r1);
777#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000778}
779
780
781void MacroAssembler::InvokePrologue(const ParameterCount& expected,
782 const ParameterCount& actual,
783 Handle<Code> code_constant,
784 Register code_reg,
785 Label* done,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100786 InvokeFlag flag,
787 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 bool definitely_matches = false;
789 Label regular_invoke;
790
791 // Check whether the expected and actual arguments count match. If not,
792 // setup registers according to contract with ArgumentsAdaptorTrampoline:
793 // r0: actual arguments count
794 // r1: function (passed through to callee)
795 // r2: expected arguments count
796 // r3: callee code entry
797
798 // The code below is made a lot easier because the calling code already sets
799 // up actual and expected registers according to the contract if values are
800 // passed in registers.
801 ASSERT(actual.is_immediate() || actual.reg().is(r0));
802 ASSERT(expected.is_immediate() || expected.reg().is(r2));
803 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
804
805 if (expected.is_immediate()) {
806 ASSERT(actual.is_immediate());
807 if (expected.immediate() == actual.immediate()) {
808 definitely_matches = true;
809 } else {
810 mov(r0, Operand(actual.immediate()));
811 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
812 if (expected.immediate() == sentinel) {
813 // Don't worry about adapting arguments for builtins that
814 // don't want that done. Skip adaption code by making it look
815 // like we have a match between expected and actual number of
816 // arguments.
817 definitely_matches = true;
818 } else {
819 mov(r2, Operand(expected.immediate()));
820 }
821 }
822 } else {
823 if (actual.is_immediate()) {
824 cmp(expected.reg(), Operand(actual.immediate()));
825 b(eq, &regular_invoke);
826 mov(r0, Operand(actual.immediate()));
827 } else {
828 cmp(expected.reg(), Operand(actual.reg()));
829 b(eq, &regular_invoke);
830 }
831 }
832
833 if (!definitely_matches) {
834 if (!code_constant.is_null()) {
835 mov(r3, Operand(code_constant));
836 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
837 }
838
839 Handle<Code> adaptor =
840 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
841 if (flag == CALL_FUNCTION) {
842 Call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100843 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 b(done);
845 } else {
846 Jump(adaptor, RelocInfo::CODE_TARGET);
847 }
848 bind(&regular_invoke);
849 }
850}
851
852
853void MacroAssembler::InvokeCode(Register code,
854 const ParameterCount& expected,
855 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100856 InvokeFlag flag,
857 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 Label done;
859
Ben Murdochb8e0da22011-05-16 14:20:40 +0100860 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
861 post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000862 if (flag == CALL_FUNCTION) {
863 Call(code);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100864 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 } else {
866 ASSERT(flag == JUMP_FUNCTION);
867 Jump(code);
868 }
869
870 // Continue here if InvokePrologue does handle the invocation due to
871 // mismatched parameter counts.
872 bind(&done);
873}
874
875
876void MacroAssembler::InvokeCode(Handle<Code> code,
877 const ParameterCount& expected,
878 const ParameterCount& actual,
879 RelocInfo::Mode rmode,
880 InvokeFlag flag) {
881 Label done;
882
883 InvokePrologue(expected, actual, code, no_reg, &done, flag);
884 if (flag == CALL_FUNCTION) {
885 Call(code, rmode);
886 } else {
887 Jump(code, rmode);
888 }
889
890 // Continue here if InvokePrologue does handle the invocation due to
891 // mismatched parameter counts.
892 bind(&done);
893}
894
895
896void MacroAssembler::InvokeFunction(Register fun,
897 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100898 InvokeFlag flag,
899 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000900 // Contract with called JS functions requires that function is passed in r1.
901 ASSERT(fun.is(r1));
902
903 Register expected_reg = r2;
904 Register code_reg = r3;
905
906 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
907 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
908 ldr(expected_reg,
909 FieldMemOperand(code_reg,
910 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100911 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000912 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100913 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000914
915 ParameterCount expected(expected_reg);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100916 InvokeCode(code_reg, expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000917}
918
919
Andrei Popescu402d9372010-02-26 13:31:12 +0000920void MacroAssembler::InvokeFunction(JSFunction* function,
921 const ParameterCount& actual,
922 InvokeFlag flag) {
923 ASSERT(function->is_compiled());
924
925 // Get the function and setup the context.
926 mov(r1, Operand(Handle<JSFunction>(function)));
927 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
928
929 // Invoke the cached code.
930 Handle<Code> code(function->code());
931 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100932 if (V8::UseCrankshaft()) {
933 // TODO(kasperl): For now, we always call indirectly through the
934 // code field in the function to allow recompilation to take effect
935 // without changing any of the call sites.
936 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
937 InvokeCode(r3, expected, actual, flag);
938 } else {
939 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
940 }
941}
942
943
944void MacroAssembler::IsObjectJSObjectType(Register heap_object,
945 Register map,
946 Register scratch,
947 Label* fail) {
948 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
949 IsInstanceJSObjectType(map, scratch, fail);
950}
951
952
953void MacroAssembler::IsInstanceJSObjectType(Register map,
954 Register scratch,
955 Label* fail) {
956 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
957 cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
958 b(lt, fail);
959 cmp(scratch, Operand(LAST_JS_OBJECT_TYPE));
960 b(gt, fail);
961}
962
963
964void MacroAssembler::IsObjectJSStringType(Register object,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100965 Register scratch,
966 Label* fail) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100967 ASSERT(kNotStringTag != 0);
968
969 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
970 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
971 tst(scratch, Operand(kIsNotStringMask));
Steve Block1e0659c2011-05-24 12:43:12 +0100972 b(ne, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +0000973}
974
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100975
Steve Blocka7e24c12009-10-30 11:49:00 +0000976#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000977void MacroAssembler::DebugBreak() {
978 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100979 mov(r0, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000980 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
981 CEntryStub ces(1);
982 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
983}
Steve Blocka7e24c12009-10-30 11:49:00 +0000984#endif
985
986
987void MacroAssembler::PushTryHandler(CodeLocation try_location,
988 HandlerType type) {
989 // Adjust this code if not the case.
990 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
991 // The pc (return address) is passed in register lr.
992 if (try_location == IN_JAVASCRIPT) {
993 if (type == TRY_CATCH_HANDLER) {
994 mov(r3, Operand(StackHandler::TRY_CATCH));
995 } else {
996 mov(r3, Operand(StackHandler::TRY_FINALLY));
997 }
998 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
999 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
1000 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1001 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
1002 // Save the current handler as the next handler.
1003 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1004 ldr(r1, MemOperand(r3));
1005 ASSERT(StackHandlerConstants::kNextOffset == 0);
1006 push(r1);
1007 // Link this handler as the new current one.
1008 str(sp, MemOperand(r3));
1009 } else {
1010 // Must preserve r0-r4, r5-r7 are available.
1011 ASSERT(try_location == IN_JS_ENTRY);
1012 // The frame pointer does not point to a JS frame so we save NULL
1013 // for fp. We expect the code throwing an exception to check fp
1014 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001015 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 mov(r6, Operand(StackHandler::ENTRY));
1017 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
1018 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
1019 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1020 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
1021 // Save the current handler as the next handler.
1022 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
1023 ldr(r6, MemOperand(r7));
1024 ASSERT(StackHandlerConstants::kNextOffset == 0);
1025 push(r6);
1026 // Link this handler as the new current one.
1027 str(sp, MemOperand(r7));
1028 }
1029}
1030
1031
Leon Clarkee46be812010-01-19 14:06:41 +00001032void MacroAssembler::PopTryHandler() {
1033 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
1034 pop(r1);
1035 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
1036 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1037 str(r1, MemOperand(ip));
1038}
1039
1040
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001041void MacroAssembler::Throw(Register value) {
1042 // r0 is expected to hold the exception.
1043 if (!value.is(r0)) {
1044 mov(r0, value);
1045 }
1046
1047 // Adjust this code if not the case.
1048 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1049
1050 // Drop the sp to the top of the handler.
1051 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1052 ldr(sp, MemOperand(r3));
1053
1054 // Restore the next handler and frame pointer, discard handler state.
1055 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1056 pop(r2);
1057 str(r2, MemOperand(r3));
1058 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
1059 ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
1060
1061 // Before returning we restore the context from the frame pointer if
1062 // not NULL. The frame pointer is NULL in the exception handler of a
1063 // JS entry frame.
1064 cmp(fp, Operand(0, RelocInfo::NONE));
1065 // Set cp to NULL if fp is NULL.
1066 mov(cp, Operand(0, RelocInfo::NONE), LeaveCC, eq);
1067 // Restore cp otherwise.
1068 ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1069#ifdef DEBUG
1070 if (FLAG_debug_code) {
1071 mov(lr, Operand(pc));
1072 }
1073#endif
1074 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1075 pop(pc);
1076}
1077
1078
1079void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
1080 Register value) {
1081 // Adjust this code if not the case.
1082 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1083
1084 // r0 is expected to hold the exception.
1085 if (!value.is(r0)) {
1086 mov(r0, value);
1087 }
1088
1089 // Drop sp to the top stack handler.
1090 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1091 ldr(sp, MemOperand(r3));
1092
1093 // Unwind the handlers until the ENTRY handler is found.
1094 Label loop, done;
1095 bind(&loop);
1096 // Load the type of the current stack handler.
1097 const int kStateOffset = StackHandlerConstants::kStateOffset;
1098 ldr(r2, MemOperand(sp, kStateOffset));
1099 cmp(r2, Operand(StackHandler::ENTRY));
1100 b(eq, &done);
1101 // Fetch the next handler in the list.
1102 const int kNextOffset = StackHandlerConstants::kNextOffset;
1103 ldr(sp, MemOperand(sp, kNextOffset));
1104 jmp(&loop);
1105 bind(&done);
1106
1107 // Set the top handler address to next handler past the current ENTRY handler.
1108 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1109 pop(r2);
1110 str(r2, MemOperand(r3));
1111
1112 if (type == OUT_OF_MEMORY) {
1113 // Set external caught exception to false.
1114 ExternalReference external_caught(Top::k_external_caught_exception_address);
1115 mov(r0, Operand(false, RelocInfo::NONE));
1116 mov(r2, Operand(external_caught));
1117 str(r0, MemOperand(r2));
1118
1119 // Set pending exception and r0 to out of memory exception.
1120 Failure* out_of_memory = Failure::OutOfMemoryException();
1121 mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1122 mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
1123 str(r0, MemOperand(r2));
1124 }
1125
1126 // Stack layout at this point. See also StackHandlerConstants.
1127 // sp -> state (ENTRY)
1128 // fp
1129 // lr
1130
1131 // Discard handler state (r2 is not used) and restore frame pointer.
1132 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
1133 ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
1134 // Before returning we restore the context from the frame pointer if
1135 // not NULL. The frame pointer is NULL in the exception handler of a
1136 // JS entry frame.
1137 cmp(fp, Operand(0, RelocInfo::NONE));
1138 // Set cp to NULL if fp is NULL.
1139 mov(cp, Operand(0, RelocInfo::NONE), LeaveCC, eq);
1140 // Restore cp otherwise.
1141 ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1142#ifdef DEBUG
1143 if (FLAG_debug_code) {
1144 mov(lr, Operand(pc));
1145 }
1146#endif
1147 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1148 pop(pc);
1149}
1150
1151
Steve Blocka7e24c12009-10-30 11:49:00 +00001152void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1153 Register scratch,
1154 Label* miss) {
1155 Label same_contexts;
1156
1157 ASSERT(!holder_reg.is(scratch));
1158 ASSERT(!holder_reg.is(ip));
1159 ASSERT(!scratch.is(ip));
1160
1161 // Load current lexical context from the stack frame.
1162 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1163 // In debug mode, make sure the lexical context is set.
1164#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001165 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001166 Check(ne, "we should not have an empty lexical context");
1167#endif
1168
1169 // Load the global context of the current context.
1170 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1171 ldr(scratch, FieldMemOperand(scratch, offset));
1172 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1173
1174 // Check the context is a global context.
1175 if (FLAG_debug_code) {
1176 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1177 // Cannot use ip as a temporary in this verification code. Due to the fact
1178 // that ip is clobbered as part of cmp with an object Operand.
1179 push(holder_reg); // Temporarily save holder on the stack.
1180 // Read the first word and compare to the global_context_map.
1181 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1182 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1183 cmp(holder_reg, ip);
1184 Check(eq, "JSGlobalObject::global_context should be a global context.");
1185 pop(holder_reg); // Restore holder.
1186 }
1187
1188 // Check if both contexts are the same.
1189 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1190 cmp(scratch, Operand(ip));
1191 b(eq, &same_contexts);
1192
1193 // Check the context is a global context.
1194 if (FLAG_debug_code) {
1195 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1196 // Cannot use ip as a temporary in this verification code. Due to the fact
1197 // that ip is clobbered as part of cmp with an object Operand.
1198 push(holder_reg); // Temporarily save holder on the stack.
1199 mov(holder_reg, ip); // Move ip to its holding place.
1200 LoadRoot(ip, Heap::kNullValueRootIndex);
1201 cmp(holder_reg, ip);
1202 Check(ne, "JSGlobalProxy::context() should not be null.");
1203
1204 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1205 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1206 cmp(holder_reg, ip);
1207 Check(eq, "JSGlobalObject::global_context should be a global context.");
1208 // Restore ip is not needed. ip is reloaded below.
1209 pop(holder_reg); // Restore holder.
1210 // Restore ip to holder's context.
1211 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1212 }
1213
1214 // Check that the security token in the calling global object is
1215 // compatible with the security token in the receiving global
1216 // object.
1217 int token_offset = Context::kHeaderSize +
1218 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1219
1220 ldr(scratch, FieldMemOperand(scratch, token_offset));
1221 ldr(ip, FieldMemOperand(ip, token_offset));
1222 cmp(scratch, Operand(ip));
1223 b(ne, miss);
1224
1225 bind(&same_contexts);
1226}
1227
1228
1229void MacroAssembler::AllocateInNewSpace(int object_size,
1230 Register result,
1231 Register scratch1,
1232 Register scratch2,
1233 Label* gc_required,
1234 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001235 if (!FLAG_inline_new) {
1236 if (FLAG_debug_code) {
1237 // Trash the registers to simulate an allocation failure.
1238 mov(result, Operand(0x7091));
1239 mov(scratch1, Operand(0x7191));
1240 mov(scratch2, Operand(0x7291));
1241 }
1242 jmp(gc_required);
1243 return;
1244 }
1245
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001247 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001248 ASSERT(!scratch1.is(scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001249 ASSERT(!scratch1.is(ip));
1250 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001251
Kristian Monsen25f61362010-05-21 11:50:48 +01001252 // Make object size into bytes.
1253 if ((flags & SIZE_IN_WORDS) != 0) {
1254 object_size *= kPointerSize;
1255 }
1256 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1257
Ben Murdochb0fe1622011-05-05 13:52:32 +01001258 // Check relative positions of allocation top and limit addresses.
1259 // The values must be adjacent in memory to allow the use of LDM.
1260 // Also, assert that the registers are numbered such that the values
1261 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001262 ExternalReference new_space_allocation_top =
1263 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001264 ExternalReference new_space_allocation_limit =
1265 ExternalReference::new_space_allocation_limit_address();
1266 intptr_t top =
1267 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1268 intptr_t limit =
1269 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1270 ASSERT((limit - top) == kPointerSize);
1271 ASSERT(result.code() < ip.code());
1272
1273 // Set up allocation top address and object size registers.
1274 Register topaddr = scratch1;
1275 Register obj_size_reg = scratch2;
1276 mov(topaddr, Operand(new_space_allocation_top));
1277 mov(obj_size_reg, Operand(object_size));
1278
1279 // This code stores a temporary value in ip. This is OK, as the code below
1280 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001281 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001282 // Load allocation top into result and allocation limit into ip.
1283 ldm(ia, topaddr, result.bit() | ip.bit());
1284 } else {
1285 if (FLAG_debug_code) {
1286 // Assert that result actually contains top on entry. ip is used
1287 // immediately below so this use of ip does not cause difference with
1288 // respect to register content between debug and release mode.
1289 ldr(ip, MemOperand(topaddr));
1290 cmp(result, ip);
1291 Check(eq, "Unexpected allocation top");
1292 }
1293 // Load allocation limit into ip. Result already contains allocation top.
1294 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001295 }
1296
1297 // Calculate new top and bail out if new space is exhausted. Use result
1298 // to calculate the new top.
Steve Block1e0659c2011-05-24 12:43:12 +01001299 add(scratch2, result, Operand(obj_size_reg), SetCC);
1300 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001301 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001303 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001304
Ben Murdochb0fe1622011-05-05 13:52:32 +01001305 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001307 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001308 }
1309}
1310
1311
1312void MacroAssembler::AllocateInNewSpace(Register object_size,
1313 Register result,
1314 Register scratch1,
1315 Register scratch2,
1316 Label* gc_required,
1317 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001318 if (!FLAG_inline_new) {
1319 if (FLAG_debug_code) {
1320 // Trash the registers to simulate an allocation failure.
1321 mov(result, Operand(0x7091));
1322 mov(scratch1, Operand(0x7191));
1323 mov(scratch2, Operand(0x7291));
1324 }
1325 jmp(gc_required);
1326 return;
1327 }
1328
Ben Murdochb0fe1622011-05-05 13:52:32 +01001329 // Assert that the register arguments are different and that none of
1330 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001331 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001332 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001333 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001334 ASSERT(!result.is(ip));
1335 ASSERT(!scratch1.is(ip));
1336 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001337
Ben Murdochb0fe1622011-05-05 13:52:32 +01001338 // Check relative positions of allocation top and limit addresses.
1339 // The values must be adjacent in memory to allow the use of LDM.
1340 // Also, assert that the registers are numbered such that the values
1341 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001342 ExternalReference new_space_allocation_top =
1343 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001344 ExternalReference new_space_allocation_limit =
1345 ExternalReference::new_space_allocation_limit_address();
1346 intptr_t top =
1347 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1348 intptr_t limit =
1349 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1350 ASSERT((limit - top) == kPointerSize);
1351 ASSERT(result.code() < ip.code());
1352
1353 // Set up allocation top address.
1354 Register topaddr = scratch1;
1355 mov(topaddr, Operand(new_space_allocation_top));
1356
1357 // This code stores a temporary value in ip. This is OK, as the code below
1358 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001359 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001360 // Load allocation top into result and allocation limit into ip.
1361 ldm(ia, topaddr, result.bit() | ip.bit());
1362 } else {
1363 if (FLAG_debug_code) {
1364 // Assert that result actually contains top on entry. ip is used
1365 // immediately below so this use of ip does not cause difference with
1366 // respect to register content between debug and release mode.
1367 ldr(ip, MemOperand(topaddr));
1368 cmp(result, ip);
1369 Check(eq, "Unexpected allocation top");
1370 }
1371 // Load allocation limit into ip. Result already contains allocation top.
1372 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001373 }
1374
1375 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001376 // to calculate the new top. Object size may be in words so a shift is
1377 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001378 if ((flags & SIZE_IN_WORDS) != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001379 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001380 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001381 add(scratch2, result, Operand(object_size), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001382 }
Steve Block1e0659c2011-05-24 12:43:12 +01001383 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001384 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001385 b(hi, gc_required);
1386
Steve Blockd0582a62009-12-15 09:54:21 +00001387 // Update allocation top. result temporarily holds the new top.
1388 if (FLAG_debug_code) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001389 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001390 Check(eq, "Unaligned allocation in new space");
1391 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001392 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001393
1394 // Tag object if requested.
1395 if ((flags & TAG_OBJECT) != 0) {
1396 add(result, result, Operand(kHeapObjectTag));
1397 }
1398}
1399
1400
1401void MacroAssembler::UndoAllocationInNewSpace(Register object,
1402 Register scratch) {
1403 ExternalReference new_space_allocation_top =
1404 ExternalReference::new_space_allocation_top_address();
1405
1406 // Make sure the object has no tag before resetting top.
1407 and_(object, object, Operand(~kHeapObjectTagMask));
1408#ifdef DEBUG
1409 // Check that the object un-allocated is below the current top.
1410 mov(scratch, Operand(new_space_allocation_top));
1411 ldr(scratch, MemOperand(scratch));
1412 cmp(object, scratch);
1413 Check(lt, "Undo allocation of non allocated memory");
1414#endif
1415 // Write the address of the object to un-allocate as the current top.
1416 mov(scratch, Operand(new_space_allocation_top));
1417 str(object, MemOperand(scratch));
1418}
1419
1420
Andrei Popescu31002712010-02-23 13:46:05 +00001421void MacroAssembler::AllocateTwoByteString(Register result,
1422 Register length,
1423 Register scratch1,
1424 Register scratch2,
1425 Register scratch3,
1426 Label* gc_required) {
1427 // Calculate the number of bytes needed for the characters in the string while
1428 // observing object alignment.
1429 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1430 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1431 add(scratch1, scratch1,
1432 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001433 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001434
1435 // Allocate two-byte string in new space.
1436 AllocateInNewSpace(scratch1,
1437 result,
1438 scratch2,
1439 scratch3,
1440 gc_required,
1441 TAG_OBJECT);
1442
1443 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001444 InitializeNewString(result,
1445 length,
1446 Heap::kStringMapRootIndex,
1447 scratch1,
1448 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001449}
1450
1451
1452void MacroAssembler::AllocateAsciiString(Register result,
1453 Register length,
1454 Register scratch1,
1455 Register scratch2,
1456 Register scratch3,
1457 Label* gc_required) {
1458 // Calculate the number of bytes needed for the characters in the string while
1459 // observing object alignment.
1460 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1461 ASSERT(kCharSize == 1);
1462 add(scratch1, length,
1463 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001464 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001465
1466 // Allocate ASCII string in new space.
1467 AllocateInNewSpace(scratch1,
1468 result,
1469 scratch2,
1470 scratch3,
1471 gc_required,
1472 TAG_OBJECT);
1473
1474 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001475 InitializeNewString(result,
1476 length,
1477 Heap::kAsciiStringMapRootIndex,
1478 scratch1,
1479 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001480}
1481
1482
1483void MacroAssembler::AllocateTwoByteConsString(Register result,
1484 Register length,
1485 Register scratch1,
1486 Register scratch2,
1487 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001488 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001489 result,
1490 scratch1,
1491 scratch2,
1492 gc_required,
1493 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001494
1495 InitializeNewString(result,
1496 length,
1497 Heap::kConsStringMapRootIndex,
1498 scratch1,
1499 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001500}
1501
1502
1503void MacroAssembler::AllocateAsciiConsString(Register result,
1504 Register length,
1505 Register scratch1,
1506 Register scratch2,
1507 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001508 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001509 result,
1510 scratch1,
1511 scratch2,
1512 gc_required,
1513 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001514
1515 InitializeNewString(result,
1516 length,
1517 Heap::kConsAsciiStringMapRootIndex,
1518 scratch1,
1519 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001520}
1521
1522
Steve Block6ded16b2010-05-10 14:33:55 +01001523void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001524 Register map,
1525 Register type_reg,
1526 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001527 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001528 CompareInstanceType(map, type_reg, type);
1529}
1530
1531
1532void MacroAssembler::CompareInstanceType(Register map,
1533 Register type_reg,
1534 InstanceType type) {
1535 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1536 cmp(type_reg, Operand(type));
1537}
1538
1539
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001540void MacroAssembler::CompareRoot(Register obj,
1541 Heap::RootListIndex index) {
1542 ASSERT(!obj.is(ip));
1543 LoadRoot(ip, index);
1544 cmp(obj, ip);
1545}
1546
1547
Andrei Popescu31002712010-02-23 13:46:05 +00001548void MacroAssembler::CheckMap(Register obj,
1549 Register scratch,
1550 Handle<Map> map,
1551 Label* fail,
1552 bool is_heap_object) {
1553 if (!is_heap_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001554 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +00001555 }
1556 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1557 mov(ip, Operand(map));
1558 cmp(scratch, ip);
1559 b(ne, fail);
1560}
1561
1562
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001563void MacroAssembler::CheckMap(Register obj,
1564 Register scratch,
1565 Heap::RootListIndex index,
1566 Label* fail,
1567 bool is_heap_object) {
1568 if (!is_heap_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001569 JumpIfSmi(obj, fail);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001570 }
1571 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1572 LoadRoot(ip, index);
1573 cmp(scratch, ip);
1574 b(ne, fail);
1575}
1576
1577
Steve Blocka7e24c12009-10-30 11:49:00 +00001578void MacroAssembler::TryGetFunctionPrototype(Register function,
1579 Register result,
1580 Register scratch,
1581 Label* miss) {
1582 // Check that the receiver isn't a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01001583 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001584
1585 // Check that the function really is a function. Load map into result reg.
1586 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1587 b(ne, miss);
1588
1589 // Make sure that the function has an instance prototype.
1590 Label non_instance;
1591 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1592 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1593 b(ne, &non_instance);
1594
1595 // Get the prototype or initial map from the function.
1596 ldr(result,
1597 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1598
1599 // If the prototype or initial map is the hole, don't return it and
1600 // simply miss the cache instead. This will allow us to allocate a
1601 // prototype object on-demand in the runtime system.
1602 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1603 cmp(result, ip);
1604 b(eq, miss);
1605
1606 // If the function does not have an initial map, we're done.
1607 Label done;
1608 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1609 b(ne, &done);
1610
1611 // Get the prototype from the initial map.
1612 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1613 jmp(&done);
1614
1615 // Non-instance prototype: Fetch prototype from constructor field
1616 // in initial map.
1617 bind(&non_instance);
1618 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1619
1620 // All done.
1621 bind(&done);
1622}
1623
1624
1625void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
Steve Block1e0659c2011-05-24 12:43:12 +01001626 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001627 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1628}
1629
1630
Andrei Popescu31002712010-02-23 13:46:05 +00001631void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
Steve Block1e0659c2011-05-24 12:43:12 +01001632 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Andrei Popescu31002712010-02-23 13:46:05 +00001633 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1634}
1635
1636
Steve Block1e0659c2011-05-24 12:43:12 +01001637MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub, Condition cond) {
1638 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
1639 Object* result;
1640 { MaybeObject* maybe_result = stub->TryGetCode();
1641 if (!maybe_result->ToObject(&result)) return maybe_result;
1642 }
1643 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1644 return result;
1645}
1646
1647
1648static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
1649 return ref0.address() - ref1.address();
1650}
1651
1652
1653MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001654 ExternalReference function, int stack_space) {
Steve Block1e0659c2011-05-24 12:43:12 +01001655 ExternalReference next_address =
1656 ExternalReference::handle_scope_next_address();
1657 const int kNextOffset = 0;
1658 const int kLimitOffset = AddressOffset(
1659 ExternalReference::handle_scope_limit_address(),
1660 next_address);
1661 const int kLevelOffset = AddressOffset(
1662 ExternalReference::handle_scope_level_address(),
1663 next_address);
1664
1665 // Allocate HandleScope in callee-save registers.
1666 mov(r7, Operand(next_address));
1667 ldr(r4, MemOperand(r7, kNextOffset));
1668 ldr(r5, MemOperand(r7, kLimitOffset));
1669 ldr(r6, MemOperand(r7, kLevelOffset));
1670 add(r6, r6, Operand(1));
1671 str(r6, MemOperand(r7, kLevelOffset));
1672
1673 // Native call returns to the DirectCEntry stub which redirects to the
1674 // return address pushed on stack (could have moved after GC).
1675 // DirectCEntry stub itself is generated early and never moves.
1676 DirectCEntryStub stub;
1677 stub.GenerateCall(this, function);
1678
1679 Label promote_scheduled_exception;
1680 Label delete_allocated_handles;
1681 Label leave_exit_frame;
1682
1683 // If result is non-zero, dereference to get the result value
1684 // otherwise set it to undefined.
1685 cmp(r0, Operand(0));
1686 LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1687 ldr(r0, MemOperand(r0), ne);
1688
1689 // No more valid handles (the result handle was the last one). Restore
1690 // previous handle scope.
1691 str(r4, MemOperand(r7, kNextOffset));
1692 if (FLAG_debug_code) {
1693 ldr(r1, MemOperand(r7, kLevelOffset));
1694 cmp(r1, r6);
1695 Check(eq, "Unexpected level after return from api call");
1696 }
1697 sub(r6, r6, Operand(1));
1698 str(r6, MemOperand(r7, kLevelOffset));
1699 ldr(ip, MemOperand(r7, kLimitOffset));
1700 cmp(r5, ip);
1701 b(ne, &delete_allocated_handles);
1702
1703 // Check if the function scheduled an exception.
1704 bind(&leave_exit_frame);
1705 LoadRoot(r4, Heap::kTheHoleValueRootIndex);
1706 mov(ip, Operand(ExternalReference::scheduled_exception_address()));
1707 ldr(r5, MemOperand(ip));
1708 cmp(r4, r5);
1709 b(ne, &promote_scheduled_exception);
1710
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001711 // LeaveExitFrame expects unwind space to be in a register.
Steve Block1e0659c2011-05-24 12:43:12 +01001712 mov(r4, Operand(stack_space));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001713 LeaveExitFrame(false, r4);
1714 mov(pc, lr);
Steve Block1e0659c2011-05-24 12:43:12 +01001715
1716 bind(&promote_scheduled_exception);
1717 MaybeObject* result = TryTailCallExternalReference(
1718 ExternalReference(Runtime::kPromoteScheduledException), 0, 1);
1719 if (result->IsFailure()) {
1720 return result;
1721 }
1722
1723 // HandleScope limit has changed. Delete allocated extensions.
1724 bind(&delete_allocated_handles);
1725 str(r5, MemOperand(r7, kLimitOffset));
1726 mov(r4, r0);
1727 PrepareCallCFunction(0, r5);
1728 CallCFunction(ExternalReference::delete_handle_scope_extensions(), 0);
1729 mov(r0, r4);
1730 jmp(&leave_exit_frame);
1731
1732 return result;
1733}
1734
1735
Steve Blocka7e24c12009-10-30 11:49:00 +00001736void MacroAssembler::IllegalOperation(int num_arguments) {
1737 if (num_arguments > 0) {
1738 add(sp, sp, Operand(num_arguments * kPointerSize));
1739 }
1740 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1741}
1742
1743
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001744void MacroAssembler::IndexFromHash(Register hash, Register index) {
1745 // If the hash field contains an array index pick it out. The assert checks
1746 // that the constants for the maximum number of digits for an array index
1747 // cached in the hash field and the number of bits reserved for it does not
1748 // conflict.
1749 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1750 (1 << String::kArrayIndexValueBits));
1751 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1752 // the low kHashShift bits.
1753 STATIC_ASSERT(kSmiTag == 0);
1754 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1755 mov(index, Operand(hash, LSL, kSmiTagSize));
1756}
1757
1758
Steve Blockd0582a62009-12-15 09:54:21 +00001759void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1760 Register outHighReg,
1761 Register outLowReg) {
1762 // ARMv7 VFP3 instructions to implement integer to double conversion.
1763 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001764 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001765 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001766 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001767}
1768
1769
Steve Block8defd9f2010-07-08 12:39:36 +01001770void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1771 DwVfpRegister result,
1772 Register scratch1,
1773 Register scratch2,
1774 Register heap_number_map,
1775 SwVfpRegister scratch3,
1776 Label* not_number,
1777 ObjectToDoubleFlags flags) {
1778 Label done;
1779 if ((flags & OBJECT_NOT_SMI) == 0) {
1780 Label not_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01001781 JumpIfNotSmi(object, &not_smi);
Steve Block8defd9f2010-07-08 12:39:36 +01001782 // Remove smi tag and convert to double.
1783 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1784 vmov(scratch3, scratch1);
1785 vcvt_f64_s32(result, scratch3);
1786 b(&done);
1787 bind(&not_smi);
1788 }
1789 // Check for heap number and load double value from it.
1790 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1791 sub(scratch2, object, Operand(kHeapObjectTag));
1792 cmp(scratch1, heap_number_map);
1793 b(ne, not_number);
1794 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1795 // If exponent is all ones the number is either a NaN or +/-Infinity.
1796 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1797 Sbfx(scratch1,
1798 scratch1,
1799 HeapNumber::kExponentShift,
1800 HeapNumber::kExponentBits);
1801 // All-one value sign extend to -1.
1802 cmp(scratch1, Operand(-1));
1803 b(eq, not_number);
1804 }
1805 vldr(result, scratch2, HeapNumber::kValueOffset);
1806 bind(&done);
1807}
1808
1809
1810void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1811 DwVfpRegister value,
1812 Register scratch1,
1813 SwVfpRegister scratch2) {
1814 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1815 vmov(scratch2, scratch1);
1816 vcvt_f64_s32(value, scratch2);
1817}
1818
1819
Iain Merrick9ac36c92010-09-13 15:29:50 +01001820// Tries to get a signed int32 out of a double precision floating point heap
1821// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1822// 32bits signed integer range.
1823void MacroAssembler::ConvertToInt32(Register source,
1824 Register dest,
1825 Register scratch,
1826 Register scratch2,
Steve Block1e0659c2011-05-24 12:43:12 +01001827 DwVfpRegister double_scratch,
Iain Merrick9ac36c92010-09-13 15:29:50 +01001828 Label *not_int32) {
1829 if (CpuFeatures::IsSupported(VFP3)) {
1830 CpuFeatures::Scope scope(VFP3);
1831 sub(scratch, source, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01001832 vldr(double_scratch, scratch, HeapNumber::kValueOffset);
1833 vcvt_s32_f64(double_scratch.low(), double_scratch);
1834 vmov(dest, double_scratch.low());
Iain Merrick9ac36c92010-09-13 15:29:50 +01001835 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1836 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1837 // range. When substracting one, the minimum signed integer becomes the
1838 // maximun signed integer.
1839 sub(scratch, dest, Operand(1));
1840 cmp(scratch, Operand(LONG_MAX - 1));
1841 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1842 b(ge, not_int32);
1843 } else {
1844 // This code is faster for doubles that are in the ranges -0x7fffffff to
1845 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1846 // the range of signed int32 values that are not Smis. Jumps to the label
1847 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1848 // 0x80000000.0 (excluding the endpoints).
1849 Label right_exponent, done;
1850 // Get exponent word.
1851 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1852 // Get exponent alone in scratch2.
1853 Ubfx(scratch2,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001854 scratch,
1855 HeapNumber::kExponentShift,
1856 HeapNumber::kExponentBits);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001857 // Load dest with zero. We use this either for the final shift or
1858 // for the answer.
1859 mov(dest, Operand(0, RelocInfo::NONE));
1860 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1861 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1862 // the exponent that we are fastest at and also the highest exponent we can
1863 // handle here.
1864 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1865 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1866 // split it up to avoid a constant pool entry. You can't do that in general
1867 // for cmp because of the overflow flag, but we know the exponent is in the
1868 // range 0-2047 so there is no overflow.
1869 int fudge_factor = 0x400;
1870 sub(scratch2, scratch2, Operand(fudge_factor));
1871 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1872 // If we have a match of the int32-but-not-Smi exponent then skip some
1873 // logic.
1874 b(eq, &right_exponent);
1875 // If the exponent is higher than that then go to slow case. This catches
1876 // numbers that don't fit in a signed int32, infinities and NaNs.
1877 b(gt, not_int32);
1878
1879 // We know the exponent is smaller than 30 (biased). If it is less than
1880 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1881 // it rounds to zero.
1882 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1883 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1884 // Dest already has a Smi zero.
1885 b(lt, &done);
1886
1887 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1888 // get how much to shift down.
1889 rsb(dest, scratch2, Operand(30));
1890
1891 bind(&right_exponent);
1892 // Get the top bits of the mantissa.
1893 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1894 // Put back the implicit 1.
1895 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1896 // Shift up the mantissa bits to take up the space the exponent used to
1897 // take. We just orred in the implicit bit so that took care of one and
1898 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1899 // distance.
1900 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1901 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1902 // Put sign in zero flag.
1903 tst(scratch, Operand(HeapNumber::kSignMask));
1904 // Get the second half of the double. For some exponents we don't
1905 // actually need this because the bits get shifted out again, but
1906 // it's probably slower to test than just to do it.
1907 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1908 // Shift down 22 bits to get the last 10 bits.
1909 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1910 // Move down according to the exponent.
1911 mov(dest, Operand(scratch, LSR, dest));
1912 // Fix sign if sign bit was set.
1913 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1914 bind(&done);
1915 }
1916}
1917
1918
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001919void MacroAssembler::EmitVFPTruncate(VFPRoundingMode rounding_mode,
1920 SwVfpRegister result,
1921 DwVfpRegister double_input,
1922 Register scratch1,
1923 Register scratch2,
1924 CheckForInexactConversion check_inexact) {
1925 ASSERT(CpuFeatures::IsSupported(VFP3));
1926 CpuFeatures::Scope scope(VFP3);
1927 Register prev_fpscr = scratch1;
1928 Register scratch = scratch2;
1929
1930 int32_t check_inexact_conversion =
1931 (check_inexact == kCheckForInexactConversion) ? kVFPInexactExceptionBit : 0;
1932
1933 // Set custom FPCSR:
1934 // - Set rounding mode.
1935 // - Clear vfp cumulative exception flags.
1936 // - Make sure Flush-to-zero mode control bit is unset.
1937 vmrs(prev_fpscr);
1938 bic(scratch,
1939 prev_fpscr,
1940 Operand(kVFPExceptionMask |
1941 check_inexact_conversion |
1942 kVFPRoundingModeMask |
1943 kVFPFlushToZeroMask));
1944 // 'Round To Nearest' is encoded by 0b00 so no bits need to be set.
1945 if (rounding_mode != kRoundToNearest) {
1946 orr(scratch, scratch, Operand(rounding_mode));
1947 }
1948 vmsr(scratch);
1949
1950 // Convert the argument to an integer.
1951 vcvt_s32_f64(result,
1952 double_input,
1953 (rounding_mode == kRoundToZero) ? kDefaultRoundToZero
1954 : kFPSCRRounding);
1955
1956 // Retrieve FPSCR.
1957 vmrs(scratch);
1958 // Restore FPSCR.
1959 vmsr(prev_fpscr);
1960 // Check for vfp exceptions.
1961 tst(scratch, Operand(kVFPExceptionMask | check_inexact_conversion));
1962}
1963
1964
Andrei Popescu31002712010-02-23 13:46:05 +00001965void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1966 Register src,
1967 int num_least_bits) {
1968 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001969 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001970 } else {
1971 mov(dst, Operand(src, ASR, kSmiTagSize));
1972 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1973 }
1974}
1975
1976
Steve Block1e0659c2011-05-24 12:43:12 +01001977void MacroAssembler::GetLeastBitsFromInt32(Register dst,
1978 Register src,
1979 int num_least_bits) {
1980 and_(dst, src, Operand((1 << num_least_bits) - 1));
1981}
1982
1983
Steve Blocka7e24c12009-10-30 11:49:00 +00001984void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1985 // All parameters are on the stack. r0 has the return value after call.
1986
1987 // If the expected number of arguments of the runtime function is
1988 // constant, we check that the actual number of arguments match the
1989 // expectation.
1990 if (f->nargs >= 0 && f->nargs != num_arguments) {
1991 IllegalOperation(num_arguments);
1992 return;
1993 }
1994
Leon Clarke4515c472010-02-03 11:58:03 +00001995 // TODO(1236192): Most runtime routines don't need the number of
1996 // arguments passed in because it is constant. At some point we
1997 // should remove this need and make the runtime routine entry code
1998 // smarter.
1999 mov(r0, Operand(num_arguments));
2000 mov(r1, Operand(ExternalReference(f)));
2001 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002002 CallStub(&stub);
2003}
2004
2005
2006void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
2007 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
2008}
2009
2010
Ben Murdochb0fe1622011-05-05 13:52:32 +01002011void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
2012 Runtime::Function* function = Runtime::FunctionForId(id);
2013 mov(r0, Operand(function->nargs));
2014 mov(r1, Operand(ExternalReference(function)));
2015 CEntryStub stub(1);
2016 stub.SaveDoubles();
2017 CallStub(&stub);
2018}
2019
2020
Andrei Popescu402d9372010-02-26 13:31:12 +00002021void MacroAssembler::CallExternalReference(const ExternalReference& ext,
2022 int num_arguments) {
2023 mov(r0, Operand(num_arguments));
2024 mov(r1, Operand(ext));
2025
2026 CEntryStub stub(1);
2027 CallStub(&stub);
2028}
2029
2030
Steve Block6ded16b2010-05-10 14:33:55 +01002031void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2032 int num_arguments,
2033 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 // TODO(1236192): Most runtime routines don't need the number of
2035 // arguments passed in because it is constant. At some point we
2036 // should remove this need and make the runtime routine entry code
2037 // smarter.
2038 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002039 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00002040}
2041
2042
Steve Block1e0659c2011-05-24 12:43:12 +01002043MaybeObject* MacroAssembler::TryTailCallExternalReference(
2044 const ExternalReference& ext, int num_arguments, int result_size) {
2045 // TODO(1236192): Most runtime routines don't need the number of
2046 // arguments passed in because it is constant. At some point we
2047 // should remove this need and make the runtime routine entry code
2048 // smarter.
2049 mov(r0, Operand(num_arguments));
2050 return TryJumpToExternalReference(ext);
2051}
2052
2053
Steve Block6ded16b2010-05-10 14:33:55 +01002054void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2055 int num_arguments,
2056 int result_size) {
2057 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
2058}
2059
2060
2061void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002062#if defined(__thumb__)
2063 // Thumb mode builtin.
2064 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2065#endif
2066 mov(r1, Operand(builtin));
2067 CEntryStub stub(1);
2068 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2069}
2070
2071
Steve Block1e0659c2011-05-24 12:43:12 +01002072MaybeObject* MacroAssembler::TryJumpToExternalReference(
2073 const ExternalReference& builtin) {
2074#if defined(__thumb__)
2075 // Thumb mode builtin.
2076 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2077#endif
2078 mov(r1, Operand(builtin));
2079 CEntryStub stub(1);
2080 return TryTailCallStub(&stub);
2081}
2082
2083
Steve Blocka7e24c12009-10-30 11:49:00 +00002084void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdochb8e0da22011-05-16 14:20:40 +01002085 InvokeJSFlags flags,
2086 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002087 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00002088 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002089 Call(r2);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002090 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 } else {
2092 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00002093 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002094 }
2095}
2096
2097
Steve Block791712a2010-08-27 10:21:07 +01002098void MacroAssembler::GetBuiltinFunction(Register target,
2099 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01002100 // Load the builtins object into target register.
2101 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2102 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00002103 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01002104 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01002105 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2106}
2107
2108
2109void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2110 ASSERT(!target.is(r1));
2111 GetBuiltinFunction(r1, id);
2112 // Load the code entry point from the builtins object.
2113 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002114}
2115
2116
2117void MacroAssembler::SetCounter(StatsCounter* counter, int value,
2118 Register scratch1, Register scratch2) {
2119 if (FLAG_native_code_counters && counter->Enabled()) {
2120 mov(scratch1, Operand(value));
2121 mov(scratch2, Operand(ExternalReference(counter)));
2122 str(scratch1, MemOperand(scratch2));
2123 }
2124}
2125
2126
2127void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2128 Register scratch1, Register scratch2) {
2129 ASSERT(value > 0);
2130 if (FLAG_native_code_counters && counter->Enabled()) {
2131 mov(scratch2, Operand(ExternalReference(counter)));
2132 ldr(scratch1, MemOperand(scratch2));
2133 add(scratch1, scratch1, Operand(value));
2134 str(scratch1, MemOperand(scratch2));
2135 }
2136}
2137
2138
2139void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2140 Register scratch1, Register scratch2) {
2141 ASSERT(value > 0);
2142 if (FLAG_native_code_counters && counter->Enabled()) {
2143 mov(scratch2, Operand(ExternalReference(counter)));
2144 ldr(scratch1, MemOperand(scratch2));
2145 sub(scratch1, scratch1, Operand(value));
2146 str(scratch1, MemOperand(scratch2));
2147 }
2148}
2149
2150
Steve Block1e0659c2011-05-24 12:43:12 +01002151void MacroAssembler::Assert(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002152 if (FLAG_debug_code)
Steve Block1e0659c2011-05-24 12:43:12 +01002153 Check(cond, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00002154}
2155
2156
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002157void MacroAssembler::AssertRegisterIsRoot(Register reg,
2158 Heap::RootListIndex index) {
2159 if (FLAG_debug_code) {
2160 LoadRoot(ip, index);
2161 cmp(reg, ip);
2162 Check(eq, "Register did not match expected root");
2163 }
2164}
2165
2166
Iain Merrick75681382010-08-19 15:07:18 +01002167void MacroAssembler::AssertFastElements(Register elements) {
2168 if (FLAG_debug_code) {
2169 ASSERT(!elements.is(ip));
2170 Label ok;
2171 push(elements);
2172 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
2173 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2174 cmp(elements, ip);
2175 b(eq, &ok);
2176 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2177 cmp(elements, ip);
2178 b(eq, &ok);
2179 Abort("JSObject with fast elements map has slow elements");
2180 bind(&ok);
2181 pop(elements);
2182 }
2183}
2184
2185
Steve Block1e0659c2011-05-24 12:43:12 +01002186void MacroAssembler::Check(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002187 Label L;
Steve Block1e0659c2011-05-24 12:43:12 +01002188 b(cond, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00002189 Abort(msg);
2190 // will not return here
2191 bind(&L);
2192}
2193
2194
2195void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01002196 Label abort_start;
2197 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00002198 // We want to pass the msg string like a smi to avoid GC
2199 // problems, however msg is not guaranteed to be aligned
2200 // properly. Instead, we pass an aligned pointer that is
2201 // a proper v8 smi, but also pass the alignment difference
2202 // from the real pointer as a smi.
2203 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2204 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2205 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2206#ifdef DEBUG
2207 if (msg != NULL) {
2208 RecordComment("Abort message: ");
2209 RecordComment(msg);
2210 }
2211#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002212 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01002213 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00002214
Steve Blocka7e24c12009-10-30 11:49:00 +00002215 mov(r0, Operand(p0));
2216 push(r0);
2217 mov(r0, Operand(Smi::FromInt(p1 - p0)));
2218 push(r0);
2219 CallRuntime(Runtime::kAbort, 2);
2220 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01002221 if (is_const_pool_blocked()) {
2222 // If the calling code cares about the exact number of
2223 // instructions generated, we insert padding here to keep the size
2224 // of the Abort macro constant.
2225 static const int kExpectedAbortInstructions = 10;
2226 int abort_instructions = InstructionsGeneratedSince(&abort_start);
2227 ASSERT(abort_instructions <= kExpectedAbortInstructions);
2228 while (abort_instructions++ < kExpectedAbortInstructions) {
2229 nop();
2230 }
2231 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002232}
2233
2234
Steve Blockd0582a62009-12-15 09:54:21 +00002235void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2236 if (context_chain_length > 0) {
2237 // Move up the chain of contexts to the context containing the slot.
2238 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
2239 // Load the function context (which is the incoming, outer context).
2240 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
2241 for (int i = 1; i < context_chain_length; i++) {
2242 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
2243 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
2244 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002245 } else {
2246 // Slot is in the current function context. Move it into the
2247 // destination register in case we store into it (the write barrier
2248 // cannot be allowed to destroy the context in esi).
2249 mov(dst, cp);
2250 }
2251
2252 // We should not have found a 'with' context by walking the context chain
2253 // (i.e., the static scope chain and runtime context chain do not agree).
2254 // A variable occurring in such a scope should have slot type LOOKUP and
2255 // not CONTEXT.
2256 if (FLAG_debug_code) {
2257 ldr(ip, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2258 cmp(dst, ip);
2259 Check(eq, "Yo dawg, I heard you liked function contexts "
2260 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00002261 }
2262}
2263
2264
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002265void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2266 // Load the global or builtins object from the current context.
2267 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2268 // Load the global context from the global or builtins object.
2269 ldr(function, FieldMemOperand(function,
2270 GlobalObject::kGlobalContextOffset));
2271 // Load the function from the global context.
2272 ldr(function, MemOperand(function, Context::SlotOffset(index)));
2273}
2274
2275
2276void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2277 Register map,
2278 Register scratch) {
2279 // Load the initial map. The global functions all have initial maps.
2280 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2281 if (FLAG_debug_code) {
2282 Label ok, fail;
2283 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, false);
2284 b(&ok);
2285 bind(&fail);
2286 Abort("Global functions must have initial map");
2287 bind(&ok);
2288 }
2289}
2290
2291
Steve Block1e0659c2011-05-24 12:43:12 +01002292void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
2293 Register reg,
2294 Register scratch,
2295 Label* not_power_of_two_or_zero) {
2296 sub(scratch, reg, Operand(1), SetCC);
2297 b(mi, not_power_of_two_or_zero);
2298 tst(scratch, reg);
2299 b(ne, not_power_of_two_or_zero);
2300}
2301
2302
Andrei Popescu31002712010-02-23 13:46:05 +00002303void MacroAssembler::JumpIfNotBothSmi(Register reg1,
2304 Register reg2,
2305 Label* on_not_both_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002306 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002307 tst(reg1, Operand(kSmiTagMask));
2308 tst(reg2, Operand(kSmiTagMask), eq);
2309 b(ne, on_not_both_smi);
2310}
2311
2312
2313void MacroAssembler::JumpIfEitherSmi(Register reg1,
2314 Register reg2,
2315 Label* on_either_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002316 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002317 tst(reg1, Operand(kSmiTagMask));
2318 tst(reg2, Operand(kSmiTagMask), ne);
2319 b(eq, on_either_smi);
2320}
2321
2322
Iain Merrick75681382010-08-19 15:07:18 +01002323void MacroAssembler::AbortIfSmi(Register object) {
Steve Block1e0659c2011-05-24 12:43:12 +01002324 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +01002325 tst(object, Operand(kSmiTagMask));
2326 Assert(ne, "Operand is a smi");
2327}
2328
2329
Steve Block1e0659c2011-05-24 12:43:12 +01002330void MacroAssembler::AbortIfNotSmi(Register object) {
2331 STATIC_ASSERT(kSmiTag == 0);
2332 tst(object, Operand(kSmiTagMask));
2333 Assert(eq, "Operand is not smi");
2334}
2335
2336
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002337void MacroAssembler::AbortIfNotString(Register object) {
2338 STATIC_ASSERT(kSmiTag == 0);
2339 tst(object, Operand(kSmiTagMask));
2340 Assert(ne, "Operand is not a string");
2341 push(object);
2342 ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2343 CompareInstanceType(object, object, FIRST_NONSTRING_TYPE);
2344 pop(object);
2345 Assert(lo, "Operand is not a string");
2346}
2347
2348
2349
Steve Block1e0659c2011-05-24 12:43:12 +01002350void MacroAssembler::AbortIfNotRootValue(Register src,
2351 Heap::RootListIndex root_value_index,
2352 const char* message) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002353 CompareRoot(src, root_value_index);
Steve Block1e0659c2011-05-24 12:43:12 +01002354 Assert(eq, message);
2355}
2356
2357
2358void MacroAssembler::JumpIfNotHeapNumber(Register object,
2359 Register heap_number_map,
2360 Register scratch,
2361 Label* on_not_heap_number) {
2362 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2363 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2364 cmp(scratch, heap_number_map);
2365 b(ne, on_not_heap_number);
2366}
2367
2368
Leon Clarked91b9f72010-01-27 17:25:45 +00002369void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
2370 Register first,
2371 Register second,
2372 Register scratch1,
2373 Register scratch2,
2374 Label* failure) {
2375 // Test that both first and second are sequential ASCII strings.
2376 // Assume that they are non-smis.
2377 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
2378 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
2379 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
2380 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002381
2382 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
2383 scratch2,
2384 scratch1,
2385 scratch2,
2386 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002387}
2388
2389void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
2390 Register second,
2391 Register scratch1,
2392 Register scratch2,
2393 Label* failure) {
2394 // Check that neither is a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002395 STATIC_ASSERT(kSmiTag == 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002396 and_(scratch1, first, Operand(second));
2397 tst(scratch1, Operand(kSmiTagMask));
2398 b(eq, failure);
2399 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
2400 second,
2401 scratch1,
2402 scratch2,
2403 failure);
2404}
2405
Steve Blockd0582a62009-12-15 09:54:21 +00002406
Steve Block6ded16b2010-05-10 14:33:55 +01002407// Allocates a heap number or jumps to the need_gc label if the young space
2408// is full and a scavenge is needed.
2409void MacroAssembler::AllocateHeapNumber(Register result,
2410 Register scratch1,
2411 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002412 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01002413 Label* gc_required) {
2414 // Allocate an object in the heap for the heap number and tag it as a heap
2415 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01002416 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01002417 result,
2418 scratch1,
2419 scratch2,
2420 gc_required,
2421 TAG_OBJECT);
2422
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002423 // Store heap number map in the allocated object.
2424 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2425 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002426}
2427
2428
Steve Block8defd9f2010-07-08 12:39:36 +01002429void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2430 DwVfpRegister value,
2431 Register scratch1,
2432 Register scratch2,
2433 Register heap_number_map,
2434 Label* gc_required) {
2435 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
2436 sub(scratch1, result, Operand(kHeapObjectTag));
2437 vstr(value, scratch1, HeapNumber::kValueOffset);
2438}
2439
2440
Ben Murdochbb769b22010-08-11 14:56:33 +01002441// Copies a fixed number of fields of heap objects from src to dst.
2442void MacroAssembler::CopyFields(Register dst,
2443 Register src,
2444 RegList temps,
2445 int field_count) {
2446 // At least one bit set in the first 15 registers.
2447 ASSERT((temps & ((1 << 15) - 1)) != 0);
2448 ASSERT((temps & dst.bit()) == 0);
2449 ASSERT((temps & src.bit()) == 0);
2450 // Primitive implementation using only one temporary register.
2451
2452 Register tmp = no_reg;
2453 // Find a temp register in temps list.
2454 for (int i = 0; i < 15; i++) {
2455 if ((temps & (1 << i)) != 0) {
2456 tmp.set_code(i);
2457 break;
2458 }
2459 }
2460 ASSERT(!tmp.is(no_reg));
2461
2462 for (int i = 0; i < field_count; i++) {
2463 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
2464 str(tmp, FieldMemOperand(dst, i * kPointerSize));
2465 }
2466}
2467
2468
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002469void MacroAssembler::CopyBytes(Register src,
2470 Register dst,
2471 Register length,
2472 Register scratch) {
2473 Label align_loop, align_loop_1, word_loop, byte_loop, byte_loop_1, done;
2474
2475 // Align src before copying in word size chunks.
2476 bind(&align_loop);
2477 cmp(length, Operand(0));
2478 b(eq, &done);
2479 bind(&align_loop_1);
2480 tst(src, Operand(kPointerSize - 1));
2481 b(eq, &word_loop);
2482 ldrb(scratch, MemOperand(src, 1, PostIndex));
2483 strb(scratch, MemOperand(dst, 1, PostIndex));
2484 sub(length, length, Operand(1), SetCC);
2485 b(ne, &byte_loop_1);
2486
2487 // Copy bytes in word size chunks.
2488 bind(&word_loop);
2489 if (FLAG_debug_code) {
2490 tst(src, Operand(kPointerSize - 1));
2491 Assert(eq, "Expecting alignment for CopyBytes");
2492 }
2493 cmp(length, Operand(kPointerSize));
2494 b(lt, &byte_loop);
2495 ldr(scratch, MemOperand(src, kPointerSize, PostIndex));
2496#if CAN_USE_UNALIGNED_ACCESSES
2497 str(scratch, MemOperand(dst, kPointerSize, PostIndex));
2498#else
2499 strb(scratch, MemOperand(dst, 1, PostIndex));
2500 mov(scratch, Operand(scratch, LSR, 8));
2501 strb(scratch, MemOperand(dst, 1, PostIndex));
2502 mov(scratch, Operand(scratch, LSR, 8));
2503 strb(scratch, MemOperand(dst, 1, PostIndex));
2504 mov(scratch, Operand(scratch, LSR, 8));
2505 strb(scratch, MemOperand(dst, 1, PostIndex));
2506#endif
2507 sub(length, length, Operand(kPointerSize));
2508 b(&word_loop);
2509
2510 // Copy the last bytes if any left.
2511 bind(&byte_loop);
2512 cmp(length, Operand(0));
2513 b(eq, &done);
2514 bind(&byte_loop_1);
2515 ldrb(scratch, MemOperand(src, 1, PostIndex));
2516 strb(scratch, MemOperand(dst, 1, PostIndex));
2517 sub(length, length, Operand(1), SetCC);
2518 b(ne, &byte_loop_1);
2519 bind(&done);
2520}
2521
2522
Steve Block8defd9f2010-07-08 12:39:36 +01002523void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
2524 Register source, // Input.
2525 Register scratch) {
Steve Block1e0659c2011-05-24 12:43:12 +01002526 ASSERT(!zeros.is(source) || !source.is(scratch));
Steve Block8defd9f2010-07-08 12:39:36 +01002527 ASSERT(!zeros.is(scratch));
2528 ASSERT(!scratch.is(ip));
2529 ASSERT(!source.is(ip));
2530 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002531#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2532 clz(zeros, source); // This instruction is only supported after ARM5.
2533#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01002534 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01002535 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002536 // Top 16.
2537 tst(scratch, Operand(0xffff0000));
2538 add(zeros, zeros, Operand(16), LeaveCC, eq);
2539 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2540 // Top 8.
2541 tst(scratch, Operand(0xff000000));
2542 add(zeros, zeros, Operand(8), LeaveCC, eq);
2543 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2544 // Top 4.
2545 tst(scratch, Operand(0xf0000000));
2546 add(zeros, zeros, Operand(4), LeaveCC, eq);
2547 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2548 // Top 2.
2549 tst(scratch, Operand(0xc0000000));
2550 add(zeros, zeros, Operand(2), LeaveCC, eq);
2551 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2552 // Top bit.
2553 tst(scratch, Operand(0x80000000u));
2554 add(zeros, zeros, Operand(1), LeaveCC, eq);
2555#endif
2556}
2557
2558
2559void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2560 Register first,
2561 Register second,
2562 Register scratch1,
2563 Register scratch2,
2564 Label* failure) {
2565 int kFlatAsciiStringMask =
2566 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2567 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2568 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2569 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2570 cmp(scratch1, Operand(kFlatAsciiStringTag));
2571 // Ignore second test if first test failed.
2572 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2573 b(ne, failure);
2574}
2575
2576
2577void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
2578 Register scratch,
2579 Label* failure) {
2580 int kFlatAsciiStringMask =
2581 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2582 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2583 and_(scratch, type, Operand(kFlatAsciiStringMask));
2584 cmp(scratch, Operand(kFlatAsciiStringTag));
2585 b(ne, failure);
2586}
2587
2588
2589void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2590 int frame_alignment = ActivationFrameAlignment();
2591 // Up to four simple arguments are passed in registers r0..r3.
2592 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2593 if (frame_alignment > kPointerSize) {
2594 // Make stack end at alignment and make room for num_arguments - 4 words
2595 // and the original value of sp.
2596 mov(scratch, sp);
2597 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
2598 ASSERT(IsPowerOf2(frame_alignment));
2599 and_(sp, sp, Operand(-frame_alignment));
2600 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2601 } else {
2602 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
2603 }
2604}
2605
2606
2607void MacroAssembler::CallCFunction(ExternalReference function,
2608 int num_arguments) {
2609 mov(ip, Operand(function));
2610 CallCFunction(ip, num_arguments);
2611}
2612
2613
2614void MacroAssembler::CallCFunction(Register function, int num_arguments) {
2615 // Make sure that the stack is aligned before calling a C function unless
2616 // running in the simulator. The simulator has its own alignment check which
2617 // provides more information.
2618#if defined(V8_HOST_ARCH_ARM)
2619 if (FLAG_debug_code) {
2620 int frame_alignment = OS::ActivationFrameAlignment();
2621 int frame_alignment_mask = frame_alignment - 1;
2622 if (frame_alignment > kPointerSize) {
2623 ASSERT(IsPowerOf2(frame_alignment));
2624 Label alignment_as_expected;
2625 tst(sp, Operand(frame_alignment_mask));
2626 b(eq, &alignment_as_expected);
2627 // Don't use Check here, as it will call Runtime_Abort possibly
2628 // re-entering here.
2629 stop("Unexpected alignment");
2630 bind(&alignment_as_expected);
2631 }
2632 }
2633#endif
2634
2635 // Just call directly. The function called cannot cause a GC, or
2636 // allow preemption, so the return address in the link register
2637 // stays correct.
2638 Call(function);
2639 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2640 if (OS::ActivationFrameAlignment() > kPointerSize) {
2641 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
2642 } else {
2643 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
2644 }
2645}
2646
2647
Steve Block1e0659c2011-05-24 12:43:12 +01002648void MacroAssembler::GetRelocatedValueLocation(Register ldr_location,
2649 Register result) {
2650 const uint32_t kLdrOffsetMask = (1 << 12) - 1;
2651 const int32_t kPCRegOffset = 2 * kPointerSize;
2652 ldr(result, MemOperand(ldr_location));
2653 if (FLAG_debug_code) {
2654 // Check that the instruction is a ldr reg, [pc + offset] .
2655 and_(result, result, Operand(kLdrPCPattern));
2656 cmp(result, Operand(kLdrPCPattern));
2657 Check(eq, "The instruction to patch should be a load from pc.");
2658 // Result was clobbered. Restore it.
2659 ldr(result, MemOperand(ldr_location));
2660 }
2661 // Get the address of the constant.
2662 and_(result, result, Operand(kLdrOffsetMask));
2663 add(result, ldr_location, Operand(result));
2664 add(result, result, Operand(kPCRegOffset));
2665}
2666
2667
Steve Blocka7e24c12009-10-30 11:49:00 +00002668CodePatcher::CodePatcher(byte* address, int instructions)
2669 : address_(address),
2670 instructions_(instructions),
2671 size_(instructions * Assembler::kInstrSize),
2672 masm_(address, size_ + Assembler::kGap) {
2673 // Create a new macro assembler pointing to the address of the code to patch.
2674 // The size is adjusted with kGap on order for the assembler to generate size
2675 // bytes of instructions without failing with buffer size constraints.
2676 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2677}
2678
2679
2680CodePatcher::~CodePatcher() {
2681 // Indicate that code has changed.
2682 CPU::FlushICache(address_, size_);
2683
2684 // Check that the code was patched as expected.
2685 ASSERT(masm_.pc_ == address_ + size_);
2686 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2687}
2688
2689
Steve Block1e0659c2011-05-24 12:43:12 +01002690void CodePatcher::Emit(Instr instr) {
2691 masm()->emit(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002692}
2693
2694
2695void CodePatcher::Emit(Address addr) {
2696 masm()->emit(reinterpret_cast<Instr>(addr));
2697}
Steve Block1e0659c2011-05-24 12:43:12 +01002698
2699
2700void CodePatcher::EmitCondition(Condition cond) {
2701 Instr instr = Assembler::instr_at(masm_.pc_);
2702 instr = (instr & ~kCondMask) | cond;
2703 masm_.emit(instr);
2704}
Steve Blocka7e24c12009-10-30 11:49:00 +00002705
2706
2707} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002708
2709#endif // V8_TARGET_ARCH_ARM