blob: 5cba955b3b9fd8c87289617147fe65678fd653f7 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 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
274void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
275 ASSERT(lsb < 32);
276 if (!CpuFeatures::IsSupported(ARMv7)) {
277 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
278 bic(dst, dst, Operand(mask));
279 } else {
280 bfc(dst, lsb, width, cond);
281 }
282}
283
284
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100285void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
286 Condition cond) {
287 if (!CpuFeatures::IsSupported(ARMv7)) {
288 ASSERT(!dst.is(pc) && !src.rm().is(pc));
289 ASSERT((satpos >= 0) && (satpos <= 31));
290
291 // These asserts are required to ensure compatibility with the ARMv7
292 // implementation.
293 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
294 ASSERT(src.rs().is(no_reg));
295
296 Label done;
297 int satval = (1 << satpos) - 1;
298
299 if (cond != al) {
300 b(NegateCondition(cond), &done); // Skip saturate if !condition.
301 }
302 if (!(src.is_reg() && dst.is(src.rm()))) {
303 mov(dst, src);
304 }
305 tst(dst, Operand(~satval));
306 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100307 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100308 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
309 bind(&done);
310 } else {
311 usat(dst, satpos, src, cond);
312 }
313}
314
315
Steve Blocka7e24c12009-10-30 11:49:00 +0000316void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
317 // Empty the const pool.
318 CheckConstPool(true, true);
319 add(pc, pc, Operand(index,
320 LSL,
321 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
322 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
323 nop(); // Jump table alignment.
324 for (int i = 0; i < targets.length(); i++) {
325 b(targets[i]);
326 }
327}
328
329
330void MacroAssembler::LoadRoot(Register destination,
331 Heap::RootListIndex index,
332 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000333 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000334}
335
336
Kristian Monsen25f61362010-05-21 11:50:48 +0100337void MacroAssembler::StoreRoot(Register source,
338 Heap::RootListIndex index,
339 Condition cond) {
340 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
341}
342
343
Steve Block6ded16b2010-05-10 14:33:55 +0100344void MacroAssembler::RecordWriteHelper(Register object,
Steve Block8defd9f2010-07-08 12:39:36 +0100345 Register address,
346 Register scratch) {
Steve Block6ded16b2010-05-10 14:33:55 +0100347 if (FLAG_debug_code) {
348 // Check that the object is not in new space.
349 Label not_in_new_space;
Steve Block8defd9f2010-07-08 12:39:36 +0100350 InNewSpace(object, scratch, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100351 Abort("new-space object passed to RecordWriteHelper");
352 bind(&not_in_new_space);
353 }
Leon Clarke4515c472010-02-03 11:58:03 +0000354
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100355 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100356 Bfc(object, 0, kPageSizeBits);
357
358 // Calculate region number.
Steve Block8defd9f2010-07-08 12:39:36 +0100359 Ubfx(address, address, Page::kRegionSizeLog2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100360 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100362 // Mark region dirty.
Steve Block8defd9f2010-07-08 12:39:36 +0100363 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 mov(ip, Operand(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100365 orr(scratch, scratch, Operand(ip, LSL, address));
366 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100367}
368
369
370void MacroAssembler::InNewSpace(Register object,
371 Register scratch,
372 Condition cc,
373 Label* branch) {
374 ASSERT(cc == eq || cc == ne);
375 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
376 cmp(scratch, Operand(ExternalReference::new_space_start()));
377 b(cc, branch);
378}
379
380
381// Will clobber 4 registers: object, offset, scratch, ip. The
382// register 'object' contains a heap object pointer. The heap object
383// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100384void MacroAssembler::RecordWrite(Register object,
385 Operand offset,
386 Register scratch0,
387 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100388 // The compiled code assumes that record write doesn't change the
389 // context register, so we check that none of the clobbered
390 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100391 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100392
393 Label done;
394
395 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100396 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100397 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100398
Steve Block8defd9f2010-07-08 12:39:36 +0100399 // Add offset into the object.
400 add(scratch0, object, offset);
401
Steve Block6ded16b2010-05-10 14:33:55 +0100402 // Record the actual write.
Steve Block8defd9f2010-07-08 12:39:36 +0100403 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
405 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000406
407 // Clobber all input registers when running with the debug-code flag
408 // turned on to provoke errors.
409 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100410 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100411 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
412 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000413 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000414}
415
416
Steve Block8defd9f2010-07-08 12:39:36 +0100417// Will clobber 4 registers: object, address, scratch, ip. The
418// register 'object' contains a heap object pointer. The heap object
419// tag is shifted away.
420void MacroAssembler::RecordWrite(Register object,
421 Register address,
422 Register scratch) {
423 // The compiled code assumes that record write doesn't change the
424 // context register, so we check that none of the clobbered
425 // registers are cp.
426 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
427
428 Label done;
429
430 // First, test that the object is not in the new space. We cannot set
431 // region marks for new space pages.
432 InNewSpace(object, scratch, eq, &done);
433
434 // Record the actual write.
435 RecordWriteHelper(object, address, scratch);
436
437 bind(&done);
438
439 // Clobber all input registers when running with the debug-code flag
440 // turned on to provoke errors.
441 if (FLAG_debug_code) {
442 mov(object, Operand(BitCast<int32_t>(kZapValue)));
443 mov(address, Operand(BitCast<int32_t>(kZapValue)));
444 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
445 }
446}
447
448
Ben Murdochb0fe1622011-05-05 13:52:32 +0100449// Push and pop all registers that can hold pointers.
450void MacroAssembler::PushSafepointRegisters() {
451 // Safepoints expect a block of contiguous register values starting with r0:
452 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
453 // Safepoints expect a block of kNumSafepointRegisters values on the
454 // stack, so adjust the stack for unsaved registers.
455 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
456 ASSERT(num_unsaved >= 0);
457 sub(sp, sp, Operand(num_unsaved * kPointerSize));
458 stm(db_w, sp, kSafepointSavedRegisters);
459}
460
461
462void MacroAssembler::PopSafepointRegisters() {
463 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
464 ldm(ia_w, sp, kSafepointSavedRegisters);
465 add(sp, sp, Operand(num_unsaved * kPointerSize));
466}
467
468
469int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
470 // The registers are pushed starting with the highest encoding,
471 // which means that lowest encodings are closest to the stack pointer.
472 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
473 return reg_code;
474}
475
476
Leon Clarkef7060e22010-06-03 12:02:55 +0100477void MacroAssembler::Ldrd(Register dst1, Register dst2,
478 const MemOperand& src, Condition cond) {
479 ASSERT(src.rm().is(no_reg));
480 ASSERT(!dst1.is(lr)); // r14.
481 ASSERT_EQ(0, dst1.code() % 2);
482 ASSERT_EQ(dst1.code() + 1, dst2.code());
483
484 // Generate two ldr instructions if ldrd is not available.
485 if (CpuFeatures::IsSupported(ARMv7)) {
486 CpuFeatures::Scope scope(ARMv7);
487 ldrd(dst1, dst2, src, cond);
488 } else {
489 MemOperand src2(src);
490 src2.set_offset(src2.offset() + 4);
491 if (dst1.is(src.rn())) {
492 ldr(dst2, src2, cond);
493 ldr(dst1, src, cond);
494 } else {
495 ldr(dst1, src, cond);
496 ldr(dst2, src2, cond);
497 }
498 }
499}
500
501
502void MacroAssembler::Strd(Register src1, Register src2,
503 const MemOperand& dst, Condition cond) {
504 ASSERT(dst.rm().is(no_reg));
505 ASSERT(!src1.is(lr)); // r14.
506 ASSERT_EQ(0, src1.code() % 2);
507 ASSERT_EQ(src1.code() + 1, src2.code());
508
509 // Generate two str instructions if strd is not available.
510 if (CpuFeatures::IsSupported(ARMv7)) {
511 CpuFeatures::Scope scope(ARMv7);
512 strd(src1, src2, dst, cond);
513 } else {
514 MemOperand dst2(dst);
515 dst2.set_offset(dst2.offset() + 4);
516 str(src1, dst, cond);
517 str(src2, dst2, cond);
518 }
519}
520
521
Ben Murdoch086aeea2011-05-13 15:57:08 +0100522void MacroAssembler::ClearFPSCRBits(uint32_t bits_to_clear, Register scratch) {
523 vmrs(scratch);
524 bic(scratch, scratch, Operand(bits_to_clear));
525 vmsr(scratch);
526}
527
528
Steve Blocka7e24c12009-10-30 11:49:00 +0000529void MacroAssembler::EnterFrame(StackFrame::Type type) {
530 // r0-r3: preserved
531 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
532 mov(ip, Operand(Smi::FromInt(type)));
533 push(ip);
534 mov(ip, Operand(CodeObject()));
535 push(ip);
536 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
537}
538
539
540void MacroAssembler::LeaveFrame(StackFrame::Type type) {
541 // r0: preserved
542 // r1: preserved
543 // r2: preserved
544
545 // Drop the execution stack down to the frame pointer and restore
546 // the caller frame pointer and return address.
547 mov(sp, fp);
548 ldm(ia_w, sp, fp.bit() | lr.bit());
549}
550
551
Ben Murdochb0fe1622011-05-05 13:52:32 +0100552void MacroAssembler::EnterExitFrame(bool save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 // r0 is argc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 // Compute callee's stack pointer before making changes and save it as
555 // ip register so that it is restored as sp register on exit, thereby
556 // popping the args.
557
558 // ip = sp + kPointerSize * #args;
559 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
560
Ben Murdochb0fe1622011-05-05 13:52:32 +0100561 // Compute the argv pointer and keep it in a callee-saved register.
562 sub(r6, ip, Operand(kPointerSize));
563
Steve Block6ded16b2010-05-10 14:33:55 +0100564 // Prepare the stack to be aligned when calling into C. After this point there
565 // are 5 pushes before the call into C, so the stack needs to be aligned after
566 // 5 pushes.
567 int frame_alignment = ActivationFrameAlignment();
568 int frame_alignment_mask = frame_alignment - 1;
569 if (frame_alignment != kPointerSize) {
570 // The following code needs to be more general if this assert does not hold.
571 ASSERT(frame_alignment == 2 * kPointerSize);
572 // With 5 pushes left the frame must be unaligned at this point.
573 mov(r7, Operand(Smi::FromInt(0)));
574 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
575 push(r7, eq); // Push if aligned to make it unaligned.
576 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000577
578 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
579 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000580 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000581
Andrei Popescu402d9372010-02-26 13:31:12 +0000582 mov(ip, Operand(CodeObject()));
583 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000584
585 // Save the frame pointer and the context in top.
586 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
587 str(fp, MemOperand(ip));
588 mov(ip, Operand(ExternalReference(Top::k_context_address)));
589 str(cp, MemOperand(ip));
590
591 // Setup argc and the builtin function in callee-saved registers.
592 mov(r4, Operand(r0));
593 mov(r5, Operand(r1));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100594
595 // Optionally save all double registers.
596 if (save_doubles) {
597 // TODO(regis): Use vstrm instruction.
598 // The stack alignment code above made sp unaligned, so add space for one
599 // more double register and use aligned addresses.
600 ASSERT(kDoubleSize == frame_alignment);
601 // Mark the frame as containing doubles by pushing a non-valid return
602 // address, i.e. 0.
603 ASSERT(ExitFrameConstants::kMarkerOffset == -2 * kPointerSize);
604 mov(ip, Operand(0)); // Marker and alignment word.
605 push(ip);
606 int space = DwVfpRegister::kNumRegisters * kDoubleSize + kPointerSize;
607 sub(sp, sp, Operand(space));
608 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
609 DwVfpRegister reg = DwVfpRegister::from_code(i);
610 vstr(reg, sp, i * kDoubleSize + kPointerSize);
611 }
612 // Note that d0 will be accessible at fp - 2*kPointerSize -
613 // DwVfpRegister::kNumRegisters * kDoubleSize, since the code slot and the
614 // alignment word were pushed after the fp.
615 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000616}
617
618
Steve Block6ded16b2010-05-10 14:33:55 +0100619void MacroAssembler::InitializeNewString(Register string,
620 Register length,
621 Heap::RootListIndex map_index,
622 Register scratch1,
623 Register scratch2) {
624 mov(scratch1, Operand(length, LSL, kSmiTagSize));
625 LoadRoot(scratch2, map_index);
626 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
627 mov(scratch1, Operand(String::kEmptyHashField));
628 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
629 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
630}
631
632
633int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000634#if defined(V8_HOST_ARCH_ARM)
635 // Running on the real platform. Use the alignment as mandated by the local
636 // environment.
637 // Note: This will break if we ever start generating snapshots on one ARM
638 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100639 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000640#else // defined(V8_HOST_ARCH_ARM)
641 // If we are using the simulator then we should always align to the expected
642 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100643 // if the target platform will need alignment, so this is controlled from a
644 // flag.
645 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000646#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000647}
648
649
Ben Murdochb0fe1622011-05-05 13:52:32 +0100650void MacroAssembler::LeaveExitFrame(bool save_doubles) {
651 // Optionally restore all double registers.
652 if (save_doubles) {
653 // TODO(regis): Use vldrm instruction.
654 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
655 DwVfpRegister reg = DwVfpRegister::from_code(i);
656 // Register d15 is just below the marker.
657 const int offset = ExitFrameConstants::kMarkerOffset;
658 vldr(reg, fp, (i - DwVfpRegister::kNumRegisters) * kDoubleSize + offset);
659 }
660 }
661
Steve Blocka7e24c12009-10-30 11:49:00 +0000662 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100663 mov(r3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000664 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
665 str(r3, MemOperand(ip));
666
667 // Restore current context from top and clear it in debug mode.
668 mov(ip, Operand(ExternalReference(Top::k_context_address)));
669 ldr(cp, MemOperand(ip));
670#ifdef DEBUG
671 str(r3, MemOperand(ip));
672#endif
673
674 // Pop the arguments, restore registers, and return.
675 mov(sp, Operand(fp)); // respect ABI stack constraint
676 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
677}
678
679
680void MacroAssembler::InvokePrologue(const ParameterCount& expected,
681 const ParameterCount& actual,
682 Handle<Code> code_constant,
683 Register code_reg,
684 Label* done,
685 InvokeFlag flag) {
686 bool definitely_matches = false;
687 Label regular_invoke;
688
689 // Check whether the expected and actual arguments count match. If not,
690 // setup registers according to contract with ArgumentsAdaptorTrampoline:
691 // r0: actual arguments count
692 // r1: function (passed through to callee)
693 // r2: expected arguments count
694 // r3: callee code entry
695
696 // The code below is made a lot easier because the calling code already sets
697 // up actual and expected registers according to the contract if values are
698 // passed in registers.
699 ASSERT(actual.is_immediate() || actual.reg().is(r0));
700 ASSERT(expected.is_immediate() || expected.reg().is(r2));
701 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
702
703 if (expected.is_immediate()) {
704 ASSERT(actual.is_immediate());
705 if (expected.immediate() == actual.immediate()) {
706 definitely_matches = true;
707 } else {
708 mov(r0, Operand(actual.immediate()));
709 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
710 if (expected.immediate() == sentinel) {
711 // Don't worry about adapting arguments for builtins that
712 // don't want that done. Skip adaption code by making it look
713 // like we have a match between expected and actual number of
714 // arguments.
715 definitely_matches = true;
716 } else {
717 mov(r2, Operand(expected.immediate()));
718 }
719 }
720 } else {
721 if (actual.is_immediate()) {
722 cmp(expected.reg(), Operand(actual.immediate()));
723 b(eq, &regular_invoke);
724 mov(r0, Operand(actual.immediate()));
725 } else {
726 cmp(expected.reg(), Operand(actual.reg()));
727 b(eq, &regular_invoke);
728 }
729 }
730
731 if (!definitely_matches) {
732 if (!code_constant.is_null()) {
733 mov(r3, Operand(code_constant));
734 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
735 }
736
737 Handle<Code> adaptor =
738 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
739 if (flag == CALL_FUNCTION) {
740 Call(adaptor, RelocInfo::CODE_TARGET);
741 b(done);
742 } else {
743 Jump(adaptor, RelocInfo::CODE_TARGET);
744 }
745 bind(&regular_invoke);
746 }
747}
748
749
750void MacroAssembler::InvokeCode(Register code,
751 const ParameterCount& expected,
752 const ParameterCount& actual,
753 InvokeFlag flag) {
754 Label done;
755
756 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
757 if (flag == CALL_FUNCTION) {
758 Call(code);
759 } else {
760 ASSERT(flag == JUMP_FUNCTION);
761 Jump(code);
762 }
763
764 // Continue here if InvokePrologue does handle the invocation due to
765 // mismatched parameter counts.
766 bind(&done);
767}
768
769
770void MacroAssembler::InvokeCode(Handle<Code> code,
771 const ParameterCount& expected,
772 const ParameterCount& actual,
773 RelocInfo::Mode rmode,
774 InvokeFlag flag) {
775 Label done;
776
777 InvokePrologue(expected, actual, code, no_reg, &done, flag);
778 if (flag == CALL_FUNCTION) {
779 Call(code, rmode);
780 } else {
781 Jump(code, rmode);
782 }
783
784 // Continue here if InvokePrologue does handle the invocation due to
785 // mismatched parameter counts.
786 bind(&done);
787}
788
789
790void MacroAssembler::InvokeFunction(Register fun,
791 const ParameterCount& actual,
792 InvokeFlag flag) {
793 // Contract with called JS functions requires that function is passed in r1.
794 ASSERT(fun.is(r1));
795
796 Register expected_reg = r2;
797 Register code_reg = r3;
798
799 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
800 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
801 ldr(expected_reg,
802 FieldMemOperand(code_reg,
803 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100804 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000805 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100806 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000807
808 ParameterCount expected(expected_reg);
809 InvokeCode(code_reg, expected, actual, flag);
810}
811
812
Andrei Popescu402d9372010-02-26 13:31:12 +0000813void MacroAssembler::InvokeFunction(JSFunction* function,
814 const ParameterCount& actual,
815 InvokeFlag flag) {
816 ASSERT(function->is_compiled());
817
818 // Get the function and setup the context.
819 mov(r1, Operand(Handle<JSFunction>(function)));
820 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
821
822 // Invoke the cached code.
823 Handle<Code> code(function->code());
824 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100825 if (V8::UseCrankshaft()) {
826 // TODO(kasperl): For now, we always call indirectly through the
827 // code field in the function to allow recompilation to take effect
828 // without changing any of the call sites.
829 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
830 InvokeCode(r3, expected, actual, flag);
831 } else {
832 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
833 }
834}
835
836
837void MacroAssembler::IsObjectJSObjectType(Register heap_object,
838 Register map,
839 Register scratch,
840 Label* fail) {
841 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
842 IsInstanceJSObjectType(map, scratch, fail);
843}
844
845
846void MacroAssembler::IsInstanceJSObjectType(Register map,
847 Register scratch,
848 Label* fail) {
849 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
850 cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
851 b(lt, fail);
852 cmp(scratch, Operand(LAST_JS_OBJECT_TYPE));
853 b(gt, fail);
854}
855
856
857void MacroAssembler::IsObjectJSStringType(Register object,
858 Register scratch,
859 Label* fail) {
860 ASSERT(kNotStringTag != 0);
861
862 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
863 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
864 tst(scratch, Operand(kIsNotStringMask));
865 b(nz, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +0000866}
867
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100868
Steve Blocka7e24c12009-10-30 11:49:00 +0000869#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000870void MacroAssembler::DebugBreak() {
871 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100872 mov(r0, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000873 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
874 CEntryStub ces(1);
875 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
876}
Steve Blocka7e24c12009-10-30 11:49:00 +0000877#endif
878
879
880void MacroAssembler::PushTryHandler(CodeLocation try_location,
881 HandlerType type) {
882 // Adjust this code if not the case.
883 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
884 // The pc (return address) is passed in register lr.
885 if (try_location == IN_JAVASCRIPT) {
886 if (type == TRY_CATCH_HANDLER) {
887 mov(r3, Operand(StackHandler::TRY_CATCH));
888 } else {
889 mov(r3, Operand(StackHandler::TRY_FINALLY));
890 }
891 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
892 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
893 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
894 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
895 // Save the current handler as the next handler.
896 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
897 ldr(r1, MemOperand(r3));
898 ASSERT(StackHandlerConstants::kNextOffset == 0);
899 push(r1);
900 // Link this handler as the new current one.
901 str(sp, MemOperand(r3));
902 } else {
903 // Must preserve r0-r4, r5-r7 are available.
904 ASSERT(try_location == IN_JS_ENTRY);
905 // The frame pointer does not point to a JS frame so we save NULL
906 // for fp. We expect the code throwing an exception to check fp
907 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100908 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 mov(r6, Operand(StackHandler::ENTRY));
910 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
911 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
912 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
913 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
914 // Save the current handler as the next handler.
915 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
916 ldr(r6, MemOperand(r7));
917 ASSERT(StackHandlerConstants::kNextOffset == 0);
918 push(r6);
919 // Link this handler as the new current one.
920 str(sp, MemOperand(r7));
921 }
922}
923
924
Leon Clarkee46be812010-01-19 14:06:41 +0000925void MacroAssembler::PopTryHandler() {
926 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
927 pop(r1);
928 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
929 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
930 str(r1, MemOperand(ip));
931}
932
933
Steve Blocka7e24c12009-10-30 11:49:00 +0000934void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
935 Register scratch,
936 Label* miss) {
937 Label same_contexts;
938
939 ASSERT(!holder_reg.is(scratch));
940 ASSERT(!holder_reg.is(ip));
941 ASSERT(!scratch.is(ip));
942
943 // Load current lexical context from the stack frame.
944 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
945 // In debug mode, make sure the lexical context is set.
946#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +0100947 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000948 Check(ne, "we should not have an empty lexical context");
949#endif
950
951 // Load the global context of the current context.
952 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
953 ldr(scratch, FieldMemOperand(scratch, offset));
954 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
955
956 // Check the context is a global context.
957 if (FLAG_debug_code) {
958 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
959 // Cannot use ip as a temporary in this verification code. Due to the fact
960 // that ip is clobbered as part of cmp with an object Operand.
961 push(holder_reg); // Temporarily save holder on the stack.
962 // Read the first word and compare to the global_context_map.
963 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
964 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
965 cmp(holder_reg, ip);
966 Check(eq, "JSGlobalObject::global_context should be a global context.");
967 pop(holder_reg); // Restore holder.
968 }
969
970 // Check if both contexts are the same.
971 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
972 cmp(scratch, Operand(ip));
973 b(eq, &same_contexts);
974
975 // Check the context is a global context.
976 if (FLAG_debug_code) {
977 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
978 // Cannot use ip as a temporary in this verification code. Due to the fact
979 // that ip is clobbered as part of cmp with an object Operand.
980 push(holder_reg); // Temporarily save holder on the stack.
981 mov(holder_reg, ip); // Move ip to its holding place.
982 LoadRoot(ip, Heap::kNullValueRootIndex);
983 cmp(holder_reg, ip);
984 Check(ne, "JSGlobalProxy::context() should not be null.");
985
986 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
987 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
988 cmp(holder_reg, ip);
989 Check(eq, "JSGlobalObject::global_context should be a global context.");
990 // Restore ip is not needed. ip is reloaded below.
991 pop(holder_reg); // Restore holder.
992 // Restore ip to holder's context.
993 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
994 }
995
996 // Check that the security token in the calling global object is
997 // compatible with the security token in the receiving global
998 // object.
999 int token_offset = Context::kHeaderSize +
1000 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1001
1002 ldr(scratch, FieldMemOperand(scratch, token_offset));
1003 ldr(ip, FieldMemOperand(ip, token_offset));
1004 cmp(scratch, Operand(ip));
1005 b(ne, miss);
1006
1007 bind(&same_contexts);
1008}
1009
1010
1011void MacroAssembler::AllocateInNewSpace(int object_size,
1012 Register result,
1013 Register scratch1,
1014 Register scratch2,
1015 Label* gc_required,
1016 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001017 if (!FLAG_inline_new) {
1018 if (FLAG_debug_code) {
1019 // Trash the registers to simulate an allocation failure.
1020 mov(result, Operand(0x7091));
1021 mov(scratch1, Operand(0x7191));
1022 mov(scratch2, Operand(0x7291));
1023 }
1024 jmp(gc_required);
1025 return;
1026 }
1027
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001029 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001030 ASSERT(!scratch1.is(scratch2));
1031
Kristian Monsen25f61362010-05-21 11:50:48 +01001032 // Make object size into bytes.
1033 if ((flags & SIZE_IN_WORDS) != 0) {
1034 object_size *= kPointerSize;
1035 }
1036 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1037
Ben Murdochb0fe1622011-05-05 13:52:32 +01001038 // Check relative positions of allocation top and limit addresses.
1039 // The values must be adjacent in memory to allow the use of LDM.
1040 // Also, assert that the registers are numbered such that the values
1041 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001042 ExternalReference new_space_allocation_top =
1043 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001044 ExternalReference new_space_allocation_limit =
1045 ExternalReference::new_space_allocation_limit_address();
1046 intptr_t top =
1047 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1048 intptr_t limit =
1049 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1050 ASSERT((limit - top) == kPointerSize);
1051 ASSERT(result.code() < ip.code());
1052
1053 // Set up allocation top address and object size registers.
1054 Register topaddr = scratch1;
1055 Register obj_size_reg = scratch2;
1056 mov(topaddr, Operand(new_space_allocation_top));
1057 mov(obj_size_reg, Operand(object_size));
1058
1059 // This code stores a temporary value in ip. This is OK, as the code below
1060 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001062 // Load allocation top into result and allocation limit into ip.
1063 ldm(ia, topaddr, result.bit() | ip.bit());
1064 } else {
1065 if (FLAG_debug_code) {
1066 // Assert that result actually contains top on entry. ip is used
1067 // immediately below so this use of ip does not cause difference with
1068 // respect to register content between debug and release mode.
1069 ldr(ip, MemOperand(topaddr));
1070 cmp(result, ip);
1071 Check(eq, "Unexpected allocation top");
1072 }
1073 // Load allocation limit into ip. Result already contains allocation top.
1074 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001075 }
1076
1077 // Calculate new top and bail out if new space is exhausted. Use result
1078 // to calculate the new top.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001079 add(scratch2, result, Operand(obj_size_reg));
1080 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001081 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001082 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001083
Ben Murdochb0fe1622011-05-05 13:52:32 +01001084 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001086 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001087 }
1088}
1089
1090
1091void MacroAssembler::AllocateInNewSpace(Register object_size,
1092 Register result,
1093 Register scratch1,
1094 Register scratch2,
1095 Label* gc_required,
1096 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001097 if (!FLAG_inline_new) {
1098 if (FLAG_debug_code) {
1099 // Trash the registers to simulate an allocation failure.
1100 mov(result, Operand(0x7091));
1101 mov(scratch1, Operand(0x7191));
1102 mov(scratch2, Operand(0x7291));
1103 }
1104 jmp(gc_required);
1105 return;
1106 }
1107
Ben Murdochb0fe1622011-05-05 13:52:32 +01001108 // Assert that the register arguments are different and that none of
1109 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001110 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001111 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001112 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001113 ASSERT(!result.is(ip));
1114 ASSERT(!scratch1.is(ip));
1115 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001116
Ben Murdochb0fe1622011-05-05 13:52:32 +01001117 // Check relative positions of allocation top and limit addresses.
1118 // The values must be adjacent in memory to allow the use of LDM.
1119 // Also, assert that the registers are numbered such that the values
1120 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001121 ExternalReference new_space_allocation_top =
1122 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001123 ExternalReference new_space_allocation_limit =
1124 ExternalReference::new_space_allocation_limit_address();
1125 intptr_t top =
1126 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1127 intptr_t limit =
1128 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1129 ASSERT((limit - top) == kPointerSize);
1130 ASSERT(result.code() < ip.code());
1131
1132 // Set up allocation top address.
1133 Register topaddr = scratch1;
1134 mov(topaddr, Operand(new_space_allocation_top));
1135
1136 // This code stores a temporary value in ip. This is OK, as the code below
1137 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001138 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001139 // Load allocation top into result and allocation limit into ip.
1140 ldm(ia, topaddr, result.bit() | ip.bit());
1141 } else {
1142 if (FLAG_debug_code) {
1143 // Assert that result actually contains top on entry. ip is used
1144 // immediately below so this use of ip does not cause difference with
1145 // respect to register content between debug and release mode.
1146 ldr(ip, MemOperand(topaddr));
1147 cmp(result, ip);
1148 Check(eq, "Unexpected allocation top");
1149 }
1150 // Load allocation limit into ip. Result already contains allocation top.
1151 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001152 }
1153
1154 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001155 // to calculate the new top. Object size may be in words so a shift is
1156 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001157 if ((flags & SIZE_IN_WORDS) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001158 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2));
Kristian Monsen25f61362010-05-21 11:50:48 +01001159 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001160 add(scratch2, result, Operand(object_size));
Kristian Monsen25f61362010-05-21 11:50:48 +01001161 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001162 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001163 b(hi, gc_required);
1164
Steve Blockd0582a62009-12-15 09:54:21 +00001165 // Update allocation top. result temporarily holds the new top.
1166 if (FLAG_debug_code) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001167 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001168 Check(eq, "Unaligned allocation in new space");
1169 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001170 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001171
1172 // Tag object if requested.
1173 if ((flags & TAG_OBJECT) != 0) {
1174 add(result, result, Operand(kHeapObjectTag));
1175 }
1176}
1177
1178
1179void MacroAssembler::UndoAllocationInNewSpace(Register object,
1180 Register scratch) {
1181 ExternalReference new_space_allocation_top =
1182 ExternalReference::new_space_allocation_top_address();
1183
1184 // Make sure the object has no tag before resetting top.
1185 and_(object, object, Operand(~kHeapObjectTagMask));
1186#ifdef DEBUG
1187 // Check that the object un-allocated is below the current top.
1188 mov(scratch, Operand(new_space_allocation_top));
1189 ldr(scratch, MemOperand(scratch));
1190 cmp(object, scratch);
1191 Check(lt, "Undo allocation of non allocated memory");
1192#endif
1193 // Write the address of the object to un-allocate as the current top.
1194 mov(scratch, Operand(new_space_allocation_top));
1195 str(object, MemOperand(scratch));
1196}
1197
1198
Andrei Popescu31002712010-02-23 13:46:05 +00001199void MacroAssembler::AllocateTwoByteString(Register result,
1200 Register length,
1201 Register scratch1,
1202 Register scratch2,
1203 Register scratch3,
1204 Label* gc_required) {
1205 // Calculate the number of bytes needed for the characters in the string while
1206 // observing object alignment.
1207 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1208 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1209 add(scratch1, scratch1,
1210 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001211 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001212
1213 // Allocate two-byte string in new space.
1214 AllocateInNewSpace(scratch1,
1215 result,
1216 scratch2,
1217 scratch3,
1218 gc_required,
1219 TAG_OBJECT);
1220
1221 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001222 InitializeNewString(result,
1223 length,
1224 Heap::kStringMapRootIndex,
1225 scratch1,
1226 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001227}
1228
1229
1230void MacroAssembler::AllocateAsciiString(Register result,
1231 Register length,
1232 Register scratch1,
1233 Register scratch2,
1234 Register scratch3,
1235 Label* gc_required) {
1236 // Calculate the number of bytes needed for the characters in the string while
1237 // observing object alignment.
1238 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1239 ASSERT(kCharSize == 1);
1240 add(scratch1, length,
1241 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001242 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001243
1244 // Allocate ASCII string in new space.
1245 AllocateInNewSpace(scratch1,
1246 result,
1247 scratch2,
1248 scratch3,
1249 gc_required,
1250 TAG_OBJECT);
1251
1252 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001253 InitializeNewString(result,
1254 length,
1255 Heap::kAsciiStringMapRootIndex,
1256 scratch1,
1257 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001258}
1259
1260
1261void MacroAssembler::AllocateTwoByteConsString(Register result,
1262 Register length,
1263 Register scratch1,
1264 Register scratch2,
1265 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001266 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001267 result,
1268 scratch1,
1269 scratch2,
1270 gc_required,
1271 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001272
1273 InitializeNewString(result,
1274 length,
1275 Heap::kConsStringMapRootIndex,
1276 scratch1,
1277 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001278}
1279
1280
1281void MacroAssembler::AllocateAsciiConsString(Register result,
1282 Register length,
1283 Register scratch1,
1284 Register scratch2,
1285 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001286 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001287 result,
1288 scratch1,
1289 scratch2,
1290 gc_required,
1291 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001292
1293 InitializeNewString(result,
1294 length,
1295 Heap::kConsAsciiStringMapRootIndex,
1296 scratch1,
1297 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001298}
1299
1300
Steve Block6ded16b2010-05-10 14:33:55 +01001301void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 Register map,
1303 Register type_reg,
1304 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001305 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 CompareInstanceType(map, type_reg, type);
1307}
1308
1309
1310void MacroAssembler::CompareInstanceType(Register map,
1311 Register type_reg,
1312 InstanceType type) {
1313 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1314 cmp(type_reg, Operand(type));
1315}
1316
1317
Andrei Popescu31002712010-02-23 13:46:05 +00001318void MacroAssembler::CheckMap(Register obj,
1319 Register scratch,
1320 Handle<Map> map,
1321 Label* fail,
1322 bool is_heap_object) {
1323 if (!is_heap_object) {
1324 BranchOnSmi(obj, fail);
1325 }
1326 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1327 mov(ip, Operand(map));
1328 cmp(scratch, ip);
1329 b(ne, fail);
1330}
1331
1332
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001333void MacroAssembler::CheckMap(Register obj,
1334 Register scratch,
1335 Heap::RootListIndex index,
1336 Label* fail,
1337 bool is_heap_object) {
1338 if (!is_heap_object) {
1339 BranchOnSmi(obj, fail);
1340 }
1341 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1342 LoadRoot(ip, index);
1343 cmp(scratch, ip);
1344 b(ne, fail);
1345}
1346
1347
Steve Blocka7e24c12009-10-30 11:49:00 +00001348void MacroAssembler::TryGetFunctionPrototype(Register function,
1349 Register result,
1350 Register scratch,
1351 Label* miss) {
1352 // Check that the receiver isn't a smi.
1353 BranchOnSmi(function, miss);
1354
1355 // Check that the function really is a function. Load map into result reg.
1356 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1357 b(ne, miss);
1358
1359 // Make sure that the function has an instance prototype.
1360 Label non_instance;
1361 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1362 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1363 b(ne, &non_instance);
1364
1365 // Get the prototype or initial map from the function.
1366 ldr(result,
1367 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1368
1369 // If the prototype or initial map is the hole, don't return it and
1370 // simply miss the cache instead. This will allow us to allocate a
1371 // prototype object on-demand in the runtime system.
1372 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1373 cmp(result, ip);
1374 b(eq, miss);
1375
1376 // If the function does not have an initial map, we're done.
1377 Label done;
1378 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1379 b(ne, &done);
1380
1381 // Get the prototype from the initial map.
1382 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1383 jmp(&done);
1384
1385 // Non-instance prototype: Fetch prototype from constructor field
1386 // in initial map.
1387 bind(&non_instance);
1388 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1389
1390 // All done.
1391 bind(&done);
1392}
1393
1394
1395void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1396 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1397 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1398}
1399
1400
Andrei Popescu31002712010-02-23 13:46:05 +00001401void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1402 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1403 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1404}
1405
1406
Steve Blocka7e24c12009-10-30 11:49:00 +00001407void MacroAssembler::IllegalOperation(int num_arguments) {
1408 if (num_arguments > 0) {
1409 add(sp, sp, Operand(num_arguments * kPointerSize));
1410 }
1411 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1412}
1413
1414
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001415void MacroAssembler::IndexFromHash(Register hash, Register index) {
1416 // If the hash field contains an array index pick it out. The assert checks
1417 // that the constants for the maximum number of digits for an array index
1418 // cached in the hash field and the number of bits reserved for it does not
1419 // conflict.
1420 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1421 (1 << String::kArrayIndexValueBits));
1422 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1423 // the low kHashShift bits.
1424 STATIC_ASSERT(kSmiTag == 0);
1425 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1426 mov(index, Operand(hash, LSL, kSmiTagSize));
1427}
1428
1429
Steve Blockd0582a62009-12-15 09:54:21 +00001430void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1431 Register outHighReg,
1432 Register outLowReg) {
1433 // ARMv7 VFP3 instructions to implement integer to double conversion.
1434 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001435 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001436 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001437 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001438}
1439
1440
Steve Block8defd9f2010-07-08 12:39:36 +01001441void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1442 DwVfpRegister result,
1443 Register scratch1,
1444 Register scratch2,
1445 Register heap_number_map,
1446 SwVfpRegister scratch3,
1447 Label* not_number,
1448 ObjectToDoubleFlags flags) {
1449 Label done;
1450 if ((flags & OBJECT_NOT_SMI) == 0) {
1451 Label not_smi;
1452 BranchOnNotSmi(object, &not_smi);
1453 // Remove smi tag and convert to double.
1454 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1455 vmov(scratch3, scratch1);
1456 vcvt_f64_s32(result, scratch3);
1457 b(&done);
1458 bind(&not_smi);
1459 }
1460 // Check for heap number and load double value from it.
1461 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1462 sub(scratch2, object, Operand(kHeapObjectTag));
1463 cmp(scratch1, heap_number_map);
1464 b(ne, not_number);
1465 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1466 // If exponent is all ones the number is either a NaN or +/-Infinity.
1467 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1468 Sbfx(scratch1,
1469 scratch1,
1470 HeapNumber::kExponentShift,
1471 HeapNumber::kExponentBits);
1472 // All-one value sign extend to -1.
1473 cmp(scratch1, Operand(-1));
1474 b(eq, not_number);
1475 }
1476 vldr(result, scratch2, HeapNumber::kValueOffset);
1477 bind(&done);
1478}
1479
1480
1481void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1482 DwVfpRegister value,
1483 Register scratch1,
1484 SwVfpRegister scratch2) {
1485 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1486 vmov(scratch2, scratch1);
1487 vcvt_f64_s32(value, scratch2);
1488}
1489
1490
Iain Merrick9ac36c92010-09-13 15:29:50 +01001491// Tries to get a signed int32 out of a double precision floating point heap
1492// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1493// 32bits signed integer range.
1494void MacroAssembler::ConvertToInt32(Register source,
1495 Register dest,
1496 Register scratch,
1497 Register scratch2,
1498 Label *not_int32) {
1499 if (CpuFeatures::IsSupported(VFP3)) {
1500 CpuFeatures::Scope scope(VFP3);
1501 sub(scratch, source, Operand(kHeapObjectTag));
1502 vldr(d0, scratch, HeapNumber::kValueOffset);
1503 vcvt_s32_f64(s0, d0);
1504 vmov(dest, s0);
1505 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1506 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1507 // range. When substracting one, the minimum signed integer becomes the
1508 // maximun signed integer.
1509 sub(scratch, dest, Operand(1));
1510 cmp(scratch, Operand(LONG_MAX - 1));
1511 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1512 b(ge, not_int32);
1513 } else {
1514 // This code is faster for doubles that are in the ranges -0x7fffffff to
1515 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1516 // the range of signed int32 values that are not Smis. Jumps to the label
1517 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1518 // 0x80000000.0 (excluding the endpoints).
1519 Label right_exponent, done;
1520 // Get exponent word.
1521 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1522 // Get exponent alone in scratch2.
1523 Ubfx(scratch2,
1524 scratch,
1525 HeapNumber::kExponentShift,
1526 HeapNumber::kExponentBits);
1527 // Load dest with zero. We use this either for the final shift or
1528 // for the answer.
1529 mov(dest, Operand(0, RelocInfo::NONE));
1530 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1531 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1532 // the exponent that we are fastest at and also the highest exponent we can
1533 // handle here.
1534 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1535 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1536 // split it up to avoid a constant pool entry. You can't do that in general
1537 // for cmp because of the overflow flag, but we know the exponent is in the
1538 // range 0-2047 so there is no overflow.
1539 int fudge_factor = 0x400;
1540 sub(scratch2, scratch2, Operand(fudge_factor));
1541 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1542 // If we have a match of the int32-but-not-Smi exponent then skip some
1543 // logic.
1544 b(eq, &right_exponent);
1545 // If the exponent is higher than that then go to slow case. This catches
1546 // numbers that don't fit in a signed int32, infinities and NaNs.
1547 b(gt, not_int32);
1548
1549 // We know the exponent is smaller than 30 (biased). If it is less than
1550 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1551 // it rounds to zero.
1552 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1553 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1554 // Dest already has a Smi zero.
1555 b(lt, &done);
1556
1557 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1558 // get how much to shift down.
1559 rsb(dest, scratch2, Operand(30));
1560
1561 bind(&right_exponent);
1562 // Get the top bits of the mantissa.
1563 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1564 // Put back the implicit 1.
1565 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1566 // Shift up the mantissa bits to take up the space the exponent used to
1567 // take. We just orred in the implicit bit so that took care of one and
1568 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1569 // distance.
1570 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1571 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1572 // Put sign in zero flag.
1573 tst(scratch, Operand(HeapNumber::kSignMask));
1574 // Get the second half of the double. For some exponents we don't
1575 // actually need this because the bits get shifted out again, but
1576 // it's probably slower to test than just to do it.
1577 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1578 // Shift down 22 bits to get the last 10 bits.
1579 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1580 // Move down according to the exponent.
1581 mov(dest, Operand(scratch, LSR, dest));
1582 // Fix sign if sign bit was set.
1583 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1584 bind(&done);
1585 }
1586}
1587
1588
Andrei Popescu31002712010-02-23 13:46:05 +00001589void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1590 Register src,
1591 int num_least_bits) {
1592 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001593 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001594 } else {
1595 mov(dst, Operand(src, ASR, kSmiTagSize));
1596 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1597 }
1598}
1599
1600
Steve Blocka7e24c12009-10-30 11:49:00 +00001601void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1602 // All parameters are on the stack. r0 has the return value after call.
1603
1604 // If the expected number of arguments of the runtime function is
1605 // constant, we check that the actual number of arguments match the
1606 // expectation.
1607 if (f->nargs >= 0 && f->nargs != num_arguments) {
1608 IllegalOperation(num_arguments);
1609 return;
1610 }
1611
Leon Clarke4515c472010-02-03 11:58:03 +00001612 // TODO(1236192): Most runtime routines don't need the number of
1613 // arguments passed in because it is constant. At some point we
1614 // should remove this need and make the runtime routine entry code
1615 // smarter.
1616 mov(r0, Operand(num_arguments));
1617 mov(r1, Operand(ExternalReference(f)));
1618 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001619 CallStub(&stub);
1620}
1621
1622
1623void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1624 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1625}
1626
1627
Ben Murdochb0fe1622011-05-05 13:52:32 +01001628void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1629 Runtime::Function* function = Runtime::FunctionForId(id);
1630 mov(r0, Operand(function->nargs));
1631 mov(r1, Operand(ExternalReference(function)));
1632 CEntryStub stub(1);
1633 stub.SaveDoubles();
1634 CallStub(&stub);
1635}
1636
1637
Andrei Popescu402d9372010-02-26 13:31:12 +00001638void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1639 int num_arguments) {
1640 mov(r0, Operand(num_arguments));
1641 mov(r1, Operand(ext));
1642
1643 CEntryStub stub(1);
1644 CallStub(&stub);
1645}
1646
1647
Steve Block6ded16b2010-05-10 14:33:55 +01001648void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1649 int num_arguments,
1650 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001651 // TODO(1236192): Most runtime routines don't need the number of
1652 // arguments passed in because it is constant. At some point we
1653 // should remove this need and make the runtime routine entry code
1654 // smarter.
1655 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001656 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001657}
1658
1659
Steve Block6ded16b2010-05-10 14:33:55 +01001660void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1661 int num_arguments,
1662 int result_size) {
1663 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1664}
1665
1666
1667void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001668#if defined(__thumb__)
1669 // Thumb mode builtin.
1670 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1671#endif
1672 mov(r1, Operand(builtin));
1673 CEntryStub stub(1);
1674 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1675}
1676
1677
Steve Blocka7e24c12009-10-30 11:49:00 +00001678void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1679 InvokeJSFlags flags) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001680 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001681 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001682 Call(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001683 } else {
1684 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001685 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001686 }
1687}
1688
1689
Steve Block791712a2010-08-27 10:21:07 +01001690void MacroAssembler::GetBuiltinFunction(Register target,
1691 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001692 // Load the builtins object into target register.
1693 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1694 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001695 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001696 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01001697 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1698}
1699
1700
1701void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1702 ASSERT(!target.is(r1));
1703 GetBuiltinFunction(r1, id);
1704 // Load the code entry point from the builtins object.
1705 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001706}
1707
1708
1709void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1710 Register scratch1, Register scratch2) {
1711 if (FLAG_native_code_counters && counter->Enabled()) {
1712 mov(scratch1, Operand(value));
1713 mov(scratch2, Operand(ExternalReference(counter)));
1714 str(scratch1, MemOperand(scratch2));
1715 }
1716}
1717
1718
1719void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1720 Register scratch1, Register scratch2) {
1721 ASSERT(value > 0);
1722 if (FLAG_native_code_counters && counter->Enabled()) {
1723 mov(scratch2, Operand(ExternalReference(counter)));
1724 ldr(scratch1, MemOperand(scratch2));
1725 add(scratch1, scratch1, Operand(value));
1726 str(scratch1, MemOperand(scratch2));
1727 }
1728}
1729
1730
1731void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1732 Register scratch1, Register scratch2) {
1733 ASSERT(value > 0);
1734 if (FLAG_native_code_counters && counter->Enabled()) {
1735 mov(scratch2, Operand(ExternalReference(counter)));
1736 ldr(scratch1, MemOperand(scratch2));
1737 sub(scratch1, scratch1, Operand(value));
1738 str(scratch1, MemOperand(scratch2));
1739 }
1740}
1741
1742
1743void MacroAssembler::Assert(Condition cc, const char* msg) {
1744 if (FLAG_debug_code)
1745 Check(cc, msg);
1746}
1747
1748
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001749void MacroAssembler::AssertRegisterIsRoot(Register reg,
1750 Heap::RootListIndex index) {
1751 if (FLAG_debug_code) {
1752 LoadRoot(ip, index);
1753 cmp(reg, ip);
1754 Check(eq, "Register did not match expected root");
1755 }
1756}
1757
1758
Iain Merrick75681382010-08-19 15:07:18 +01001759void MacroAssembler::AssertFastElements(Register elements) {
1760 if (FLAG_debug_code) {
1761 ASSERT(!elements.is(ip));
1762 Label ok;
1763 push(elements);
1764 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
1765 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
1766 cmp(elements, ip);
1767 b(eq, &ok);
1768 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
1769 cmp(elements, ip);
1770 b(eq, &ok);
1771 Abort("JSObject with fast elements map has slow elements");
1772 bind(&ok);
1773 pop(elements);
1774 }
1775}
1776
1777
Steve Blocka7e24c12009-10-30 11:49:00 +00001778void MacroAssembler::Check(Condition cc, const char* msg) {
1779 Label L;
1780 b(cc, &L);
1781 Abort(msg);
1782 // will not return here
1783 bind(&L);
1784}
1785
1786
1787void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01001788 Label abort_start;
1789 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00001790 // We want to pass the msg string like a smi to avoid GC
1791 // problems, however msg is not guaranteed to be aligned
1792 // properly. Instead, we pass an aligned pointer that is
1793 // a proper v8 smi, but also pass the alignment difference
1794 // from the real pointer as a smi.
1795 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1796 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1797 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1798#ifdef DEBUG
1799 if (msg != NULL) {
1800 RecordComment("Abort message: ");
1801 RecordComment(msg);
1802 }
1803#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001804 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001805 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001806
Steve Blocka7e24c12009-10-30 11:49:00 +00001807 mov(r0, Operand(p0));
1808 push(r0);
1809 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1810 push(r0);
1811 CallRuntime(Runtime::kAbort, 2);
1812 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01001813 if (is_const_pool_blocked()) {
1814 // If the calling code cares about the exact number of
1815 // instructions generated, we insert padding here to keep the size
1816 // of the Abort macro constant.
1817 static const int kExpectedAbortInstructions = 10;
1818 int abort_instructions = InstructionsGeneratedSince(&abort_start);
1819 ASSERT(abort_instructions <= kExpectedAbortInstructions);
1820 while (abort_instructions++ < kExpectedAbortInstructions) {
1821 nop();
1822 }
1823 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001824}
1825
1826
Steve Blockd0582a62009-12-15 09:54:21 +00001827void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1828 if (context_chain_length > 0) {
1829 // Move up the chain of contexts to the context containing the slot.
1830 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1831 // Load the function context (which is the incoming, outer context).
1832 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1833 for (int i = 1; i < context_chain_length; i++) {
1834 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1835 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1836 }
1837 // The context may be an intermediate context, not a function context.
1838 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1839 } else { // Slot is in the current function context.
1840 // The context may be an intermediate context, not a function context.
1841 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1842 }
1843}
1844
1845
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001846void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1847 // Load the global or builtins object from the current context.
1848 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1849 // Load the global context from the global or builtins object.
1850 ldr(function, FieldMemOperand(function,
1851 GlobalObject::kGlobalContextOffset));
1852 // Load the function from the global context.
1853 ldr(function, MemOperand(function, Context::SlotOffset(index)));
1854}
1855
1856
1857void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1858 Register map,
1859 Register scratch) {
1860 // Load the initial map. The global functions all have initial maps.
1861 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1862 if (FLAG_debug_code) {
1863 Label ok, fail;
1864 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, false);
1865 b(&ok);
1866 bind(&fail);
1867 Abort("Global functions must have initial map");
1868 bind(&ok);
1869 }
1870}
1871
1872
Andrei Popescu31002712010-02-23 13:46:05 +00001873void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1874 Register reg2,
1875 Label* on_not_both_smi) {
1876 ASSERT_EQ(0, kSmiTag);
1877 tst(reg1, Operand(kSmiTagMask));
1878 tst(reg2, Operand(kSmiTagMask), eq);
1879 b(ne, on_not_both_smi);
1880}
1881
1882
1883void MacroAssembler::JumpIfEitherSmi(Register reg1,
1884 Register reg2,
1885 Label* on_either_smi) {
1886 ASSERT_EQ(0, kSmiTag);
1887 tst(reg1, Operand(kSmiTagMask));
1888 tst(reg2, Operand(kSmiTagMask), ne);
1889 b(eq, on_either_smi);
1890}
1891
1892
Iain Merrick75681382010-08-19 15:07:18 +01001893void MacroAssembler::AbortIfSmi(Register object) {
1894 ASSERT_EQ(0, kSmiTag);
1895 tst(object, Operand(kSmiTagMask));
1896 Assert(ne, "Operand is a smi");
1897}
1898
1899
Leon Clarked91b9f72010-01-27 17:25:45 +00001900void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1901 Register first,
1902 Register second,
1903 Register scratch1,
1904 Register scratch2,
1905 Label* failure) {
1906 // Test that both first and second are sequential ASCII strings.
1907 // Assume that they are non-smis.
1908 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1909 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1910 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1911 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001912
1913 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1914 scratch2,
1915 scratch1,
1916 scratch2,
1917 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001918}
1919
1920void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1921 Register second,
1922 Register scratch1,
1923 Register scratch2,
1924 Label* failure) {
1925 // Check that neither is a smi.
1926 ASSERT_EQ(0, kSmiTag);
1927 and_(scratch1, first, Operand(second));
1928 tst(scratch1, Operand(kSmiTagMask));
1929 b(eq, failure);
1930 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1931 second,
1932 scratch1,
1933 scratch2,
1934 failure);
1935}
1936
Steve Blockd0582a62009-12-15 09:54:21 +00001937
Steve Block6ded16b2010-05-10 14:33:55 +01001938// Allocates a heap number or jumps to the need_gc label if the young space
1939// is full and a scavenge is needed.
1940void MacroAssembler::AllocateHeapNumber(Register result,
1941 Register scratch1,
1942 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001943 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01001944 Label* gc_required) {
1945 // Allocate an object in the heap for the heap number and tag it as a heap
1946 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001947 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001948 result,
1949 scratch1,
1950 scratch2,
1951 gc_required,
1952 TAG_OBJECT);
1953
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001954 // Store heap number map in the allocated object.
1955 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1956 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001957}
1958
1959
Steve Block8defd9f2010-07-08 12:39:36 +01001960void MacroAssembler::AllocateHeapNumberWithValue(Register result,
1961 DwVfpRegister value,
1962 Register scratch1,
1963 Register scratch2,
1964 Register heap_number_map,
1965 Label* gc_required) {
1966 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
1967 sub(scratch1, result, Operand(kHeapObjectTag));
1968 vstr(value, scratch1, HeapNumber::kValueOffset);
1969}
1970
1971
Ben Murdochbb769b22010-08-11 14:56:33 +01001972// Copies a fixed number of fields of heap objects from src to dst.
1973void MacroAssembler::CopyFields(Register dst,
1974 Register src,
1975 RegList temps,
1976 int field_count) {
1977 // At least one bit set in the first 15 registers.
1978 ASSERT((temps & ((1 << 15) - 1)) != 0);
1979 ASSERT((temps & dst.bit()) == 0);
1980 ASSERT((temps & src.bit()) == 0);
1981 // Primitive implementation using only one temporary register.
1982
1983 Register tmp = no_reg;
1984 // Find a temp register in temps list.
1985 for (int i = 0; i < 15; i++) {
1986 if ((temps & (1 << i)) != 0) {
1987 tmp.set_code(i);
1988 break;
1989 }
1990 }
1991 ASSERT(!tmp.is(no_reg));
1992
1993 for (int i = 0; i < field_count; i++) {
1994 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
1995 str(tmp, FieldMemOperand(dst, i * kPointerSize));
1996 }
1997}
1998
1999
Steve Block8defd9f2010-07-08 12:39:36 +01002000void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
2001 Register source, // Input.
2002 Register scratch) {
2003 ASSERT(!zeros.is(source) || !source.is(zeros));
2004 ASSERT(!zeros.is(scratch));
2005 ASSERT(!scratch.is(ip));
2006 ASSERT(!source.is(ip));
2007 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002008#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2009 clz(zeros, source); // This instruction is only supported after ARM5.
2010#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01002011 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01002012 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002013 // Top 16.
2014 tst(scratch, Operand(0xffff0000));
2015 add(zeros, zeros, Operand(16), LeaveCC, eq);
2016 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2017 // Top 8.
2018 tst(scratch, Operand(0xff000000));
2019 add(zeros, zeros, Operand(8), LeaveCC, eq);
2020 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2021 // Top 4.
2022 tst(scratch, Operand(0xf0000000));
2023 add(zeros, zeros, Operand(4), LeaveCC, eq);
2024 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2025 // Top 2.
2026 tst(scratch, Operand(0xc0000000));
2027 add(zeros, zeros, Operand(2), LeaveCC, eq);
2028 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2029 // Top bit.
2030 tst(scratch, Operand(0x80000000u));
2031 add(zeros, zeros, Operand(1), LeaveCC, eq);
2032#endif
2033}
2034
2035
2036void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2037 Register first,
2038 Register second,
2039 Register scratch1,
2040 Register scratch2,
2041 Label* failure) {
2042 int kFlatAsciiStringMask =
2043 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2044 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2045 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2046 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2047 cmp(scratch1, Operand(kFlatAsciiStringTag));
2048 // Ignore second test if first test failed.
2049 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2050 b(ne, failure);
2051}
2052
2053
2054void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
2055 Register scratch,
2056 Label* failure) {
2057 int kFlatAsciiStringMask =
2058 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2059 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2060 and_(scratch, type, Operand(kFlatAsciiStringMask));
2061 cmp(scratch, Operand(kFlatAsciiStringTag));
2062 b(ne, failure);
2063}
2064
2065
2066void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2067 int frame_alignment = ActivationFrameAlignment();
2068 // Up to four simple arguments are passed in registers r0..r3.
2069 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2070 if (frame_alignment > kPointerSize) {
2071 // Make stack end at alignment and make room for num_arguments - 4 words
2072 // and the original value of sp.
2073 mov(scratch, sp);
2074 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
2075 ASSERT(IsPowerOf2(frame_alignment));
2076 and_(sp, sp, Operand(-frame_alignment));
2077 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2078 } else {
2079 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
2080 }
2081}
2082
2083
2084void MacroAssembler::CallCFunction(ExternalReference function,
2085 int num_arguments) {
2086 mov(ip, Operand(function));
2087 CallCFunction(ip, num_arguments);
2088}
2089
2090
2091void MacroAssembler::CallCFunction(Register function, int num_arguments) {
2092 // Make sure that the stack is aligned before calling a C function unless
2093 // running in the simulator. The simulator has its own alignment check which
2094 // provides more information.
2095#if defined(V8_HOST_ARCH_ARM)
2096 if (FLAG_debug_code) {
2097 int frame_alignment = OS::ActivationFrameAlignment();
2098 int frame_alignment_mask = frame_alignment - 1;
2099 if (frame_alignment > kPointerSize) {
2100 ASSERT(IsPowerOf2(frame_alignment));
2101 Label alignment_as_expected;
2102 tst(sp, Operand(frame_alignment_mask));
2103 b(eq, &alignment_as_expected);
2104 // Don't use Check here, as it will call Runtime_Abort possibly
2105 // re-entering here.
2106 stop("Unexpected alignment");
2107 bind(&alignment_as_expected);
2108 }
2109 }
2110#endif
2111
2112 // Just call directly. The function called cannot cause a GC, or
2113 // allow preemption, so the return address in the link register
2114 // stays correct.
2115 Call(function);
2116 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2117 if (OS::ActivationFrameAlignment() > kPointerSize) {
2118 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
2119 } else {
2120 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
2121 }
2122}
2123
2124
Steve Blocka7e24c12009-10-30 11:49:00 +00002125#ifdef ENABLE_DEBUGGER_SUPPORT
2126CodePatcher::CodePatcher(byte* address, int instructions)
2127 : address_(address),
2128 instructions_(instructions),
2129 size_(instructions * Assembler::kInstrSize),
2130 masm_(address, size_ + Assembler::kGap) {
2131 // Create a new macro assembler pointing to the address of the code to patch.
2132 // The size is adjusted with kGap on order for the assembler to generate size
2133 // bytes of instructions without failing with buffer size constraints.
2134 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2135}
2136
2137
2138CodePatcher::~CodePatcher() {
2139 // Indicate that code has changed.
2140 CPU::FlushICache(address_, size_);
2141
2142 // Check that the code was patched as expected.
2143 ASSERT(masm_.pc_ == address_ + size_);
2144 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2145}
2146
2147
2148void CodePatcher::Emit(Instr x) {
2149 masm()->emit(x);
2150}
2151
2152
2153void CodePatcher::Emit(Address addr) {
2154 masm()->emit(reinterpret_cast<Instr>(addr));
2155}
2156#endif // ENABLE_DEBUGGER_SUPPORT
2157
2158
2159} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002160
2161#endif // V8_TARGET_ARCH_ARM