blob: 1028b0e69f3e8dbba88fee6e7ac635a0c6ff954b [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
Ben Murdochb8e0da22011-05-16 14:20:40 +0100469void MacroAssembler::PushSafepointRegistersAndDoubles() {
470 PushSafepointRegisters();
471 sub(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
472 kDoubleSize));
473 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
474 vstr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
475 }
476}
477
478
479void MacroAssembler::PopSafepointRegistersAndDoubles() {
480 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
481 vldr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
482 }
483 add(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
484 kDoubleSize));
485 PopSafepointRegisters();
486}
487
Ben Murdochb0fe1622011-05-05 13:52:32 +0100488int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
489 // The registers are pushed starting with the highest encoding,
490 // which means that lowest encodings are closest to the stack pointer.
491 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
492 return reg_code;
493}
494
495
Leon Clarkef7060e22010-06-03 12:02:55 +0100496void MacroAssembler::Ldrd(Register dst1, Register dst2,
497 const MemOperand& src, Condition cond) {
498 ASSERT(src.rm().is(no_reg));
499 ASSERT(!dst1.is(lr)); // r14.
500 ASSERT_EQ(0, dst1.code() % 2);
501 ASSERT_EQ(dst1.code() + 1, dst2.code());
502
503 // Generate two ldr instructions if ldrd is not available.
504 if (CpuFeatures::IsSupported(ARMv7)) {
505 CpuFeatures::Scope scope(ARMv7);
506 ldrd(dst1, dst2, src, cond);
507 } else {
508 MemOperand src2(src);
509 src2.set_offset(src2.offset() + 4);
510 if (dst1.is(src.rn())) {
511 ldr(dst2, src2, cond);
512 ldr(dst1, src, cond);
513 } else {
514 ldr(dst1, src, cond);
515 ldr(dst2, src2, cond);
516 }
517 }
518}
519
520
521void MacroAssembler::Strd(Register src1, Register src2,
522 const MemOperand& dst, Condition cond) {
523 ASSERT(dst.rm().is(no_reg));
524 ASSERT(!src1.is(lr)); // r14.
525 ASSERT_EQ(0, src1.code() % 2);
526 ASSERT_EQ(src1.code() + 1, src2.code());
527
528 // Generate two str instructions if strd is not available.
529 if (CpuFeatures::IsSupported(ARMv7)) {
530 CpuFeatures::Scope scope(ARMv7);
531 strd(src1, src2, dst, cond);
532 } else {
533 MemOperand dst2(dst);
534 dst2.set_offset(dst2.offset() + 4);
535 str(src1, dst, cond);
536 str(src2, dst2, cond);
537 }
538}
539
540
Ben Murdochb8e0da22011-05-16 14:20:40 +0100541void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
542 const Register scratch,
543 const Condition cond) {
544 vmrs(scratch, cond);
545 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
546 vmsr(scratch, cond);
547}
548
549
550void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
551 const DwVfpRegister src2,
552 const Condition cond) {
553 // Compare and move FPSCR flags to the normal condition flags.
554 VFPCompareAndLoadFlags(src1, src2, pc, cond);
555}
556
557void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
558 const double src2,
559 const Condition cond) {
560 // Compare and move FPSCR flags to the normal condition flags.
561 VFPCompareAndLoadFlags(src1, src2, pc, cond);
562}
563
564
565void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
566 const DwVfpRegister src2,
567 const Register fpscr_flags,
568 const Condition cond) {
569 // Compare and load FPSCR.
570 vcmp(src1, src2, cond);
571 vmrs(fpscr_flags, cond);
572}
573
574void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
575 const double src2,
576 const Register fpscr_flags,
577 const Condition cond) {
578 // Compare and load FPSCR.
579 vcmp(src1, src2, cond);
580 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100581}
582
583
Steve Blocka7e24c12009-10-30 11:49:00 +0000584void MacroAssembler::EnterFrame(StackFrame::Type type) {
585 // r0-r3: preserved
586 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
587 mov(ip, Operand(Smi::FromInt(type)));
588 push(ip);
589 mov(ip, Operand(CodeObject()));
590 push(ip);
591 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
592}
593
594
595void MacroAssembler::LeaveFrame(StackFrame::Type type) {
596 // r0: preserved
597 // r1: preserved
598 // r2: preserved
599
600 // Drop the execution stack down to the frame pointer and restore
601 // the caller frame pointer and return address.
602 mov(sp, fp);
603 ldm(ia_w, sp, fp.bit() | lr.bit());
604}
605
606
Ben Murdochb0fe1622011-05-05 13:52:32 +0100607void MacroAssembler::EnterExitFrame(bool save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000608 // r0 is argc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 // Compute callee's stack pointer before making changes and save it as
610 // ip register so that it is restored as sp register on exit, thereby
611 // popping the args.
612
613 // ip = sp + kPointerSize * #args;
614 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
615
Ben Murdochb0fe1622011-05-05 13:52:32 +0100616 // Compute the argv pointer and keep it in a callee-saved register.
617 sub(r6, ip, Operand(kPointerSize));
618
Steve Block6ded16b2010-05-10 14:33:55 +0100619 // Prepare the stack to be aligned when calling into C. After this point there
620 // are 5 pushes before the call into C, so the stack needs to be aligned after
621 // 5 pushes.
622 int frame_alignment = ActivationFrameAlignment();
623 int frame_alignment_mask = frame_alignment - 1;
624 if (frame_alignment != kPointerSize) {
625 // The following code needs to be more general if this assert does not hold.
626 ASSERT(frame_alignment == 2 * kPointerSize);
627 // With 5 pushes left the frame must be unaligned at this point.
628 mov(r7, Operand(Smi::FromInt(0)));
629 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
630 push(r7, eq); // Push if aligned to make it unaligned.
631 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000632
633 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
634 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000635 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000636
Andrei Popescu402d9372010-02-26 13:31:12 +0000637 mov(ip, Operand(CodeObject()));
638 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000639
640 // Save the frame pointer and the context in top.
641 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
642 str(fp, MemOperand(ip));
643 mov(ip, Operand(ExternalReference(Top::k_context_address)));
644 str(cp, MemOperand(ip));
645
646 // Setup argc and the builtin function in callee-saved registers.
647 mov(r4, Operand(r0));
648 mov(r5, Operand(r1));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100649
650 // Optionally save all double registers.
651 if (save_doubles) {
652 // TODO(regis): Use vstrm instruction.
653 // The stack alignment code above made sp unaligned, so add space for one
654 // more double register and use aligned addresses.
655 ASSERT(kDoubleSize == frame_alignment);
656 // Mark the frame as containing doubles by pushing a non-valid return
657 // address, i.e. 0.
658 ASSERT(ExitFrameConstants::kMarkerOffset == -2 * kPointerSize);
659 mov(ip, Operand(0)); // Marker and alignment word.
660 push(ip);
661 int space = DwVfpRegister::kNumRegisters * kDoubleSize + kPointerSize;
662 sub(sp, sp, Operand(space));
663 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
664 DwVfpRegister reg = DwVfpRegister::from_code(i);
665 vstr(reg, sp, i * kDoubleSize + kPointerSize);
666 }
667 // Note that d0 will be accessible at fp - 2*kPointerSize -
668 // DwVfpRegister::kNumRegisters * kDoubleSize, since the code slot and the
669 // alignment word were pushed after the fp.
670 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000671}
672
673
Steve Block6ded16b2010-05-10 14:33:55 +0100674void MacroAssembler::InitializeNewString(Register string,
675 Register length,
676 Heap::RootListIndex map_index,
677 Register scratch1,
678 Register scratch2) {
679 mov(scratch1, Operand(length, LSL, kSmiTagSize));
680 LoadRoot(scratch2, map_index);
681 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
682 mov(scratch1, Operand(String::kEmptyHashField));
683 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
684 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
685}
686
687
688int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000689#if defined(V8_HOST_ARCH_ARM)
690 // Running on the real platform. Use the alignment as mandated by the local
691 // environment.
692 // Note: This will break if we ever start generating snapshots on one ARM
693 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100694 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000695#else // defined(V8_HOST_ARCH_ARM)
696 // If we are using the simulator then we should always align to the expected
697 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100698 // if the target platform will need alignment, so this is controlled from a
699 // flag.
700 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000701#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000702}
703
704
Ben Murdochb0fe1622011-05-05 13:52:32 +0100705void MacroAssembler::LeaveExitFrame(bool save_doubles) {
706 // Optionally restore all double registers.
707 if (save_doubles) {
708 // TODO(regis): Use vldrm instruction.
709 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
710 DwVfpRegister reg = DwVfpRegister::from_code(i);
711 // Register d15 is just below the marker.
712 const int offset = ExitFrameConstants::kMarkerOffset;
713 vldr(reg, fp, (i - DwVfpRegister::kNumRegisters) * kDoubleSize + offset);
714 }
715 }
716
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100718 mov(r3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000719 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
720 str(r3, MemOperand(ip));
721
722 // Restore current context from top and clear it in debug mode.
723 mov(ip, Operand(ExternalReference(Top::k_context_address)));
724 ldr(cp, MemOperand(ip));
725#ifdef DEBUG
726 str(r3, MemOperand(ip));
727#endif
728
729 // Pop the arguments, restore registers, and return.
730 mov(sp, Operand(fp)); // respect ABI stack constraint
731 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
732}
733
734
735void MacroAssembler::InvokePrologue(const ParameterCount& expected,
736 const ParameterCount& actual,
737 Handle<Code> code_constant,
738 Register code_reg,
739 Label* done,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100740 InvokeFlag flag,
741 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000742 bool definitely_matches = false;
743 Label regular_invoke;
744
745 // Check whether the expected and actual arguments count match. If not,
746 // setup registers according to contract with ArgumentsAdaptorTrampoline:
747 // r0: actual arguments count
748 // r1: function (passed through to callee)
749 // r2: expected arguments count
750 // r3: callee code entry
751
752 // The code below is made a lot easier because the calling code already sets
753 // up actual and expected registers according to the contract if values are
754 // passed in registers.
755 ASSERT(actual.is_immediate() || actual.reg().is(r0));
756 ASSERT(expected.is_immediate() || expected.reg().is(r2));
757 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
758
759 if (expected.is_immediate()) {
760 ASSERT(actual.is_immediate());
761 if (expected.immediate() == actual.immediate()) {
762 definitely_matches = true;
763 } else {
764 mov(r0, Operand(actual.immediate()));
765 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
766 if (expected.immediate() == sentinel) {
767 // Don't worry about adapting arguments for builtins that
768 // don't want that done. Skip adaption code by making it look
769 // like we have a match between expected and actual number of
770 // arguments.
771 definitely_matches = true;
772 } else {
773 mov(r2, Operand(expected.immediate()));
774 }
775 }
776 } else {
777 if (actual.is_immediate()) {
778 cmp(expected.reg(), Operand(actual.immediate()));
779 b(eq, &regular_invoke);
780 mov(r0, Operand(actual.immediate()));
781 } else {
782 cmp(expected.reg(), Operand(actual.reg()));
783 b(eq, &regular_invoke);
784 }
785 }
786
787 if (!definitely_matches) {
788 if (!code_constant.is_null()) {
789 mov(r3, Operand(code_constant));
790 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
791 }
792
793 Handle<Code> adaptor =
794 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
795 if (flag == CALL_FUNCTION) {
796 Call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100797 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000798 b(done);
799 } else {
800 Jump(adaptor, RelocInfo::CODE_TARGET);
801 }
802 bind(&regular_invoke);
803 }
804}
805
806
807void MacroAssembler::InvokeCode(Register code,
808 const ParameterCount& expected,
809 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100810 InvokeFlag flag,
811 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 Label done;
813
Ben Murdochb8e0da22011-05-16 14:20:40 +0100814 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
815 post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000816 if (flag == CALL_FUNCTION) {
817 Call(code);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100818 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 } else {
820 ASSERT(flag == JUMP_FUNCTION);
821 Jump(code);
822 }
823
824 // Continue here if InvokePrologue does handle the invocation due to
825 // mismatched parameter counts.
826 bind(&done);
827}
828
829
830void MacroAssembler::InvokeCode(Handle<Code> code,
831 const ParameterCount& expected,
832 const ParameterCount& actual,
833 RelocInfo::Mode rmode,
834 InvokeFlag flag) {
835 Label done;
836
837 InvokePrologue(expected, actual, code, no_reg, &done, flag);
838 if (flag == CALL_FUNCTION) {
839 Call(code, rmode);
840 } else {
841 Jump(code, rmode);
842 }
843
844 // Continue here if InvokePrologue does handle the invocation due to
845 // mismatched parameter counts.
846 bind(&done);
847}
848
849
850void MacroAssembler::InvokeFunction(Register fun,
851 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100852 InvokeFlag flag,
853 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 // Contract with called JS functions requires that function is passed in r1.
855 ASSERT(fun.is(r1));
856
857 Register expected_reg = r2;
858 Register code_reg = r3;
859
860 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
861 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
862 ldr(expected_reg,
863 FieldMemOperand(code_reg,
864 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100865 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100867 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000868
869 ParameterCount expected(expected_reg);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100870 InvokeCode(code_reg, expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000871}
872
873
Andrei Popescu402d9372010-02-26 13:31:12 +0000874void MacroAssembler::InvokeFunction(JSFunction* function,
875 const ParameterCount& actual,
876 InvokeFlag flag) {
877 ASSERT(function->is_compiled());
878
879 // Get the function and setup the context.
880 mov(r1, Operand(Handle<JSFunction>(function)));
881 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
882
883 // Invoke the cached code.
884 Handle<Code> code(function->code());
885 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100886 if (V8::UseCrankshaft()) {
887 // TODO(kasperl): For now, we always call indirectly through the
888 // code field in the function to allow recompilation to take effect
889 // without changing any of the call sites.
890 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
891 InvokeCode(r3, expected, actual, flag);
892 } else {
893 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
894 }
895}
896
897
898void MacroAssembler::IsObjectJSObjectType(Register heap_object,
899 Register map,
900 Register scratch,
901 Label* fail) {
902 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
903 IsInstanceJSObjectType(map, scratch, fail);
904}
905
906
907void MacroAssembler::IsInstanceJSObjectType(Register map,
908 Register scratch,
909 Label* fail) {
910 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
911 cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
912 b(lt, fail);
913 cmp(scratch, Operand(LAST_JS_OBJECT_TYPE));
914 b(gt, fail);
915}
916
917
918void MacroAssembler::IsObjectJSStringType(Register object,
919 Register scratch,
920 Label* fail) {
921 ASSERT(kNotStringTag != 0);
922
923 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
924 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
925 tst(scratch, Operand(kIsNotStringMask));
926 b(nz, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +0000927}
928
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100929
Steve Blocka7e24c12009-10-30 11:49:00 +0000930#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000931void MacroAssembler::DebugBreak() {
932 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100933 mov(r0, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000934 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
935 CEntryStub ces(1);
936 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
937}
Steve Blocka7e24c12009-10-30 11:49:00 +0000938#endif
939
940
941void MacroAssembler::PushTryHandler(CodeLocation try_location,
942 HandlerType type) {
943 // Adjust this code if not the case.
944 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
945 // The pc (return address) is passed in register lr.
946 if (try_location == IN_JAVASCRIPT) {
947 if (type == TRY_CATCH_HANDLER) {
948 mov(r3, Operand(StackHandler::TRY_CATCH));
949 } else {
950 mov(r3, Operand(StackHandler::TRY_FINALLY));
951 }
952 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
953 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
954 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
955 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
956 // Save the current handler as the next handler.
957 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
958 ldr(r1, MemOperand(r3));
959 ASSERT(StackHandlerConstants::kNextOffset == 0);
960 push(r1);
961 // Link this handler as the new current one.
962 str(sp, MemOperand(r3));
963 } else {
964 // Must preserve r0-r4, r5-r7 are available.
965 ASSERT(try_location == IN_JS_ENTRY);
966 // The frame pointer does not point to a JS frame so we save NULL
967 // for fp. We expect the code throwing an exception to check fp
968 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100969 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000970 mov(r6, Operand(StackHandler::ENTRY));
971 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
972 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
973 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
974 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
975 // Save the current handler as the next handler.
976 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
977 ldr(r6, MemOperand(r7));
978 ASSERT(StackHandlerConstants::kNextOffset == 0);
979 push(r6);
980 // Link this handler as the new current one.
981 str(sp, MemOperand(r7));
982 }
983}
984
985
Leon Clarkee46be812010-01-19 14:06:41 +0000986void MacroAssembler::PopTryHandler() {
987 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
988 pop(r1);
989 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
990 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
991 str(r1, MemOperand(ip));
992}
993
994
Steve Blocka7e24c12009-10-30 11:49:00 +0000995void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
996 Register scratch,
997 Label* miss) {
998 Label same_contexts;
999
1000 ASSERT(!holder_reg.is(scratch));
1001 ASSERT(!holder_reg.is(ip));
1002 ASSERT(!scratch.is(ip));
1003
1004 // Load current lexical context from the stack frame.
1005 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1006 // In debug mode, make sure the lexical context is set.
1007#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001008 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001009 Check(ne, "we should not have an empty lexical context");
1010#endif
1011
1012 // Load the global context of the current context.
1013 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1014 ldr(scratch, FieldMemOperand(scratch, offset));
1015 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1016
1017 // Check the context is a global context.
1018 if (FLAG_debug_code) {
1019 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1020 // Cannot use ip as a temporary in this verification code. Due to the fact
1021 // that ip is clobbered as part of cmp with an object Operand.
1022 push(holder_reg); // Temporarily save holder on the stack.
1023 // Read the first word and compare to the global_context_map.
1024 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1025 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1026 cmp(holder_reg, ip);
1027 Check(eq, "JSGlobalObject::global_context should be a global context.");
1028 pop(holder_reg); // Restore holder.
1029 }
1030
1031 // Check if both contexts are the same.
1032 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1033 cmp(scratch, Operand(ip));
1034 b(eq, &same_contexts);
1035
1036 // Check the context is a global context.
1037 if (FLAG_debug_code) {
1038 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1039 // Cannot use ip as a temporary in this verification code. Due to the fact
1040 // that ip is clobbered as part of cmp with an object Operand.
1041 push(holder_reg); // Temporarily save holder on the stack.
1042 mov(holder_reg, ip); // Move ip to its holding place.
1043 LoadRoot(ip, Heap::kNullValueRootIndex);
1044 cmp(holder_reg, ip);
1045 Check(ne, "JSGlobalProxy::context() should not be null.");
1046
1047 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1048 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1049 cmp(holder_reg, ip);
1050 Check(eq, "JSGlobalObject::global_context should be a global context.");
1051 // Restore ip is not needed. ip is reloaded below.
1052 pop(holder_reg); // Restore holder.
1053 // Restore ip to holder's context.
1054 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1055 }
1056
1057 // Check that the security token in the calling global object is
1058 // compatible with the security token in the receiving global
1059 // object.
1060 int token_offset = Context::kHeaderSize +
1061 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1062
1063 ldr(scratch, FieldMemOperand(scratch, token_offset));
1064 ldr(ip, FieldMemOperand(ip, token_offset));
1065 cmp(scratch, Operand(ip));
1066 b(ne, miss);
1067
1068 bind(&same_contexts);
1069}
1070
1071
1072void MacroAssembler::AllocateInNewSpace(int object_size,
1073 Register result,
1074 Register scratch1,
1075 Register scratch2,
1076 Label* gc_required,
1077 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001078 if (!FLAG_inline_new) {
1079 if (FLAG_debug_code) {
1080 // Trash the registers to simulate an allocation failure.
1081 mov(result, Operand(0x7091));
1082 mov(scratch1, Operand(0x7191));
1083 mov(scratch2, Operand(0x7291));
1084 }
1085 jmp(gc_required);
1086 return;
1087 }
1088
Steve Blocka7e24c12009-10-30 11:49:00 +00001089 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001090 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 ASSERT(!scratch1.is(scratch2));
1092
Kristian Monsen25f61362010-05-21 11:50:48 +01001093 // Make object size into bytes.
1094 if ((flags & SIZE_IN_WORDS) != 0) {
1095 object_size *= kPointerSize;
1096 }
1097 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1098
Ben Murdochb0fe1622011-05-05 13:52:32 +01001099 // Check relative positions of allocation top and limit addresses.
1100 // The values must be adjacent in memory to allow the use of LDM.
1101 // Also, assert that the registers are numbered such that the values
1102 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001103 ExternalReference new_space_allocation_top =
1104 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001105 ExternalReference new_space_allocation_limit =
1106 ExternalReference::new_space_allocation_limit_address();
1107 intptr_t top =
1108 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1109 intptr_t limit =
1110 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1111 ASSERT((limit - top) == kPointerSize);
1112 ASSERT(result.code() < ip.code());
1113
1114 // Set up allocation top address and object size registers.
1115 Register topaddr = scratch1;
1116 Register obj_size_reg = scratch2;
1117 mov(topaddr, Operand(new_space_allocation_top));
1118 mov(obj_size_reg, Operand(object_size));
1119
1120 // This code stores a temporary value in ip. This is OK, as the code below
1121 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001123 // Load allocation top into result and allocation limit into ip.
1124 ldm(ia, topaddr, result.bit() | ip.bit());
1125 } else {
1126 if (FLAG_debug_code) {
1127 // Assert that result actually contains top on entry. ip is used
1128 // immediately below so this use of ip does not cause difference with
1129 // respect to register content between debug and release mode.
1130 ldr(ip, MemOperand(topaddr));
1131 cmp(result, ip);
1132 Check(eq, "Unexpected allocation top");
1133 }
1134 // Load allocation limit into ip. Result already contains allocation top.
1135 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 }
1137
1138 // Calculate new top and bail out if new space is exhausted. Use result
1139 // to calculate the new top.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001140 add(scratch2, result, Operand(obj_size_reg));
1141 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001142 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001143 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001144
Ben Murdochb0fe1622011-05-05 13:52:32 +01001145 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001147 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 }
1149}
1150
1151
1152void MacroAssembler::AllocateInNewSpace(Register object_size,
1153 Register result,
1154 Register scratch1,
1155 Register scratch2,
1156 Label* gc_required,
1157 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001158 if (!FLAG_inline_new) {
1159 if (FLAG_debug_code) {
1160 // Trash the registers to simulate an allocation failure.
1161 mov(result, Operand(0x7091));
1162 mov(scratch1, Operand(0x7191));
1163 mov(scratch2, Operand(0x7291));
1164 }
1165 jmp(gc_required);
1166 return;
1167 }
1168
Ben Murdochb0fe1622011-05-05 13:52:32 +01001169 // Assert that the register arguments are different and that none of
1170 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001172 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001173 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001174 ASSERT(!result.is(ip));
1175 ASSERT(!scratch1.is(ip));
1176 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001177
Ben Murdochb0fe1622011-05-05 13:52:32 +01001178 // Check relative positions of allocation top and limit addresses.
1179 // The values must be adjacent in memory to allow the use of LDM.
1180 // Also, assert that the registers are numbered such that the values
1181 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001182 ExternalReference new_space_allocation_top =
1183 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001184 ExternalReference new_space_allocation_limit =
1185 ExternalReference::new_space_allocation_limit_address();
1186 intptr_t top =
1187 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1188 intptr_t limit =
1189 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1190 ASSERT((limit - top) == kPointerSize);
1191 ASSERT(result.code() < ip.code());
1192
1193 // Set up allocation top address.
1194 Register topaddr = scratch1;
1195 mov(topaddr, Operand(new_space_allocation_top));
1196
1197 // This code stores a temporary value in ip. This is OK, as the code below
1198 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001199 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001200 // Load allocation top into result and allocation limit into ip.
1201 ldm(ia, topaddr, result.bit() | ip.bit());
1202 } else {
1203 if (FLAG_debug_code) {
1204 // Assert that result actually contains top on entry. ip is used
1205 // immediately below so this use of ip does not cause difference with
1206 // respect to register content between debug and release mode.
1207 ldr(ip, MemOperand(topaddr));
1208 cmp(result, ip);
1209 Check(eq, "Unexpected allocation top");
1210 }
1211 // Load allocation limit into ip. Result already contains allocation top.
1212 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 }
1214
1215 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001216 // to calculate the new top. Object size may be in words so a shift is
1217 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001218 if ((flags & SIZE_IN_WORDS) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001219 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2));
Kristian Monsen25f61362010-05-21 11:50:48 +01001220 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001221 add(scratch2, result, Operand(object_size));
Kristian Monsen25f61362010-05-21 11:50:48 +01001222 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001223 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 b(hi, gc_required);
1225
Steve Blockd0582a62009-12-15 09:54:21 +00001226 // Update allocation top. result temporarily holds the new top.
1227 if (FLAG_debug_code) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001228 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001229 Check(eq, "Unaligned allocation in new space");
1230 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001231 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001232
1233 // Tag object if requested.
1234 if ((flags & TAG_OBJECT) != 0) {
1235 add(result, result, Operand(kHeapObjectTag));
1236 }
1237}
1238
1239
1240void MacroAssembler::UndoAllocationInNewSpace(Register object,
1241 Register scratch) {
1242 ExternalReference new_space_allocation_top =
1243 ExternalReference::new_space_allocation_top_address();
1244
1245 // Make sure the object has no tag before resetting top.
1246 and_(object, object, Operand(~kHeapObjectTagMask));
1247#ifdef DEBUG
1248 // Check that the object un-allocated is below the current top.
1249 mov(scratch, Operand(new_space_allocation_top));
1250 ldr(scratch, MemOperand(scratch));
1251 cmp(object, scratch);
1252 Check(lt, "Undo allocation of non allocated memory");
1253#endif
1254 // Write the address of the object to un-allocate as the current top.
1255 mov(scratch, Operand(new_space_allocation_top));
1256 str(object, MemOperand(scratch));
1257}
1258
1259
Andrei Popescu31002712010-02-23 13:46:05 +00001260void MacroAssembler::AllocateTwoByteString(Register result,
1261 Register length,
1262 Register scratch1,
1263 Register scratch2,
1264 Register scratch3,
1265 Label* gc_required) {
1266 // Calculate the number of bytes needed for the characters in the string while
1267 // observing object alignment.
1268 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1269 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1270 add(scratch1, scratch1,
1271 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001272 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001273
1274 // Allocate two-byte string in new space.
1275 AllocateInNewSpace(scratch1,
1276 result,
1277 scratch2,
1278 scratch3,
1279 gc_required,
1280 TAG_OBJECT);
1281
1282 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001283 InitializeNewString(result,
1284 length,
1285 Heap::kStringMapRootIndex,
1286 scratch1,
1287 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001288}
1289
1290
1291void MacroAssembler::AllocateAsciiString(Register result,
1292 Register length,
1293 Register scratch1,
1294 Register scratch2,
1295 Register scratch3,
1296 Label* gc_required) {
1297 // Calculate the number of bytes needed for the characters in the string while
1298 // observing object alignment.
1299 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1300 ASSERT(kCharSize == 1);
1301 add(scratch1, length,
1302 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001303 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001304
1305 // Allocate ASCII string in new space.
1306 AllocateInNewSpace(scratch1,
1307 result,
1308 scratch2,
1309 scratch3,
1310 gc_required,
1311 TAG_OBJECT);
1312
1313 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001314 InitializeNewString(result,
1315 length,
1316 Heap::kAsciiStringMapRootIndex,
1317 scratch1,
1318 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001319}
1320
1321
1322void MacroAssembler::AllocateTwoByteConsString(Register result,
1323 Register length,
1324 Register scratch1,
1325 Register scratch2,
1326 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001327 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001328 result,
1329 scratch1,
1330 scratch2,
1331 gc_required,
1332 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001333
1334 InitializeNewString(result,
1335 length,
1336 Heap::kConsStringMapRootIndex,
1337 scratch1,
1338 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001339}
1340
1341
1342void MacroAssembler::AllocateAsciiConsString(Register result,
1343 Register length,
1344 Register scratch1,
1345 Register scratch2,
1346 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001347 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001348 result,
1349 scratch1,
1350 scratch2,
1351 gc_required,
1352 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001353
1354 InitializeNewString(result,
1355 length,
1356 Heap::kConsAsciiStringMapRootIndex,
1357 scratch1,
1358 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001359}
1360
1361
Steve Block6ded16b2010-05-10 14:33:55 +01001362void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001363 Register map,
1364 Register type_reg,
1365 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001366 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001367 CompareInstanceType(map, type_reg, type);
1368}
1369
1370
1371void MacroAssembler::CompareInstanceType(Register map,
1372 Register type_reg,
1373 InstanceType type) {
1374 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1375 cmp(type_reg, Operand(type));
1376}
1377
1378
Andrei Popescu31002712010-02-23 13:46:05 +00001379void MacroAssembler::CheckMap(Register obj,
1380 Register scratch,
1381 Handle<Map> map,
1382 Label* fail,
1383 bool is_heap_object) {
1384 if (!is_heap_object) {
1385 BranchOnSmi(obj, fail);
1386 }
1387 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1388 mov(ip, Operand(map));
1389 cmp(scratch, ip);
1390 b(ne, fail);
1391}
1392
1393
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001394void MacroAssembler::CheckMap(Register obj,
1395 Register scratch,
1396 Heap::RootListIndex index,
1397 Label* fail,
1398 bool is_heap_object) {
1399 if (!is_heap_object) {
1400 BranchOnSmi(obj, fail);
1401 }
1402 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1403 LoadRoot(ip, index);
1404 cmp(scratch, ip);
1405 b(ne, fail);
1406}
1407
1408
Steve Blocka7e24c12009-10-30 11:49:00 +00001409void MacroAssembler::TryGetFunctionPrototype(Register function,
1410 Register result,
1411 Register scratch,
1412 Label* miss) {
1413 // Check that the receiver isn't a smi.
1414 BranchOnSmi(function, miss);
1415
1416 // Check that the function really is a function. Load map into result reg.
1417 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1418 b(ne, miss);
1419
1420 // Make sure that the function has an instance prototype.
1421 Label non_instance;
1422 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1423 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1424 b(ne, &non_instance);
1425
1426 // Get the prototype or initial map from the function.
1427 ldr(result,
1428 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1429
1430 // If the prototype or initial map is the hole, don't return it and
1431 // simply miss the cache instead. This will allow us to allocate a
1432 // prototype object on-demand in the runtime system.
1433 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1434 cmp(result, ip);
1435 b(eq, miss);
1436
1437 // If the function does not have an initial map, we're done.
1438 Label done;
1439 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1440 b(ne, &done);
1441
1442 // Get the prototype from the initial map.
1443 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1444 jmp(&done);
1445
1446 // Non-instance prototype: Fetch prototype from constructor field
1447 // in initial map.
1448 bind(&non_instance);
1449 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1450
1451 // All done.
1452 bind(&done);
1453}
1454
1455
1456void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1457 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1458 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1459}
1460
1461
Andrei Popescu31002712010-02-23 13:46:05 +00001462void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1463 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1464 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1465}
1466
1467
Steve Blocka7e24c12009-10-30 11:49:00 +00001468void MacroAssembler::IllegalOperation(int num_arguments) {
1469 if (num_arguments > 0) {
1470 add(sp, sp, Operand(num_arguments * kPointerSize));
1471 }
1472 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1473}
1474
1475
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001476void MacroAssembler::IndexFromHash(Register hash, Register index) {
1477 // If the hash field contains an array index pick it out. The assert checks
1478 // that the constants for the maximum number of digits for an array index
1479 // cached in the hash field and the number of bits reserved for it does not
1480 // conflict.
1481 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1482 (1 << String::kArrayIndexValueBits));
1483 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1484 // the low kHashShift bits.
1485 STATIC_ASSERT(kSmiTag == 0);
1486 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1487 mov(index, Operand(hash, LSL, kSmiTagSize));
1488}
1489
1490
Steve Blockd0582a62009-12-15 09:54:21 +00001491void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1492 Register outHighReg,
1493 Register outLowReg) {
1494 // ARMv7 VFP3 instructions to implement integer to double conversion.
1495 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001496 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001497 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001498 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001499}
1500
1501
Steve Block8defd9f2010-07-08 12:39:36 +01001502void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1503 DwVfpRegister result,
1504 Register scratch1,
1505 Register scratch2,
1506 Register heap_number_map,
1507 SwVfpRegister scratch3,
1508 Label* not_number,
1509 ObjectToDoubleFlags flags) {
1510 Label done;
1511 if ((flags & OBJECT_NOT_SMI) == 0) {
1512 Label not_smi;
1513 BranchOnNotSmi(object, &not_smi);
1514 // Remove smi tag and convert to double.
1515 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1516 vmov(scratch3, scratch1);
1517 vcvt_f64_s32(result, scratch3);
1518 b(&done);
1519 bind(&not_smi);
1520 }
1521 // Check for heap number and load double value from it.
1522 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1523 sub(scratch2, object, Operand(kHeapObjectTag));
1524 cmp(scratch1, heap_number_map);
1525 b(ne, not_number);
1526 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1527 // If exponent is all ones the number is either a NaN or +/-Infinity.
1528 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1529 Sbfx(scratch1,
1530 scratch1,
1531 HeapNumber::kExponentShift,
1532 HeapNumber::kExponentBits);
1533 // All-one value sign extend to -1.
1534 cmp(scratch1, Operand(-1));
1535 b(eq, not_number);
1536 }
1537 vldr(result, scratch2, HeapNumber::kValueOffset);
1538 bind(&done);
1539}
1540
1541
1542void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1543 DwVfpRegister value,
1544 Register scratch1,
1545 SwVfpRegister scratch2) {
1546 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1547 vmov(scratch2, scratch1);
1548 vcvt_f64_s32(value, scratch2);
1549}
1550
1551
Iain Merrick9ac36c92010-09-13 15:29:50 +01001552// Tries to get a signed int32 out of a double precision floating point heap
1553// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1554// 32bits signed integer range.
1555void MacroAssembler::ConvertToInt32(Register source,
1556 Register dest,
1557 Register scratch,
1558 Register scratch2,
1559 Label *not_int32) {
1560 if (CpuFeatures::IsSupported(VFP3)) {
1561 CpuFeatures::Scope scope(VFP3);
1562 sub(scratch, source, Operand(kHeapObjectTag));
1563 vldr(d0, scratch, HeapNumber::kValueOffset);
1564 vcvt_s32_f64(s0, d0);
1565 vmov(dest, s0);
1566 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1567 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1568 // range. When substracting one, the minimum signed integer becomes the
1569 // maximun signed integer.
1570 sub(scratch, dest, Operand(1));
1571 cmp(scratch, Operand(LONG_MAX - 1));
1572 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1573 b(ge, not_int32);
1574 } else {
1575 // This code is faster for doubles that are in the ranges -0x7fffffff to
1576 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1577 // the range of signed int32 values that are not Smis. Jumps to the label
1578 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1579 // 0x80000000.0 (excluding the endpoints).
1580 Label right_exponent, done;
1581 // Get exponent word.
1582 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1583 // Get exponent alone in scratch2.
1584 Ubfx(scratch2,
1585 scratch,
1586 HeapNumber::kExponentShift,
1587 HeapNumber::kExponentBits);
1588 // Load dest with zero. We use this either for the final shift or
1589 // for the answer.
1590 mov(dest, Operand(0, RelocInfo::NONE));
1591 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1592 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1593 // the exponent that we are fastest at and also the highest exponent we can
1594 // handle here.
1595 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1596 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1597 // split it up to avoid a constant pool entry. You can't do that in general
1598 // for cmp because of the overflow flag, but we know the exponent is in the
1599 // range 0-2047 so there is no overflow.
1600 int fudge_factor = 0x400;
1601 sub(scratch2, scratch2, Operand(fudge_factor));
1602 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1603 // If we have a match of the int32-but-not-Smi exponent then skip some
1604 // logic.
1605 b(eq, &right_exponent);
1606 // If the exponent is higher than that then go to slow case. This catches
1607 // numbers that don't fit in a signed int32, infinities and NaNs.
1608 b(gt, not_int32);
1609
1610 // We know the exponent is smaller than 30 (biased). If it is less than
1611 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1612 // it rounds to zero.
1613 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1614 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1615 // Dest already has a Smi zero.
1616 b(lt, &done);
1617
1618 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1619 // get how much to shift down.
1620 rsb(dest, scratch2, Operand(30));
1621
1622 bind(&right_exponent);
1623 // Get the top bits of the mantissa.
1624 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1625 // Put back the implicit 1.
1626 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1627 // Shift up the mantissa bits to take up the space the exponent used to
1628 // take. We just orred in the implicit bit so that took care of one and
1629 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1630 // distance.
1631 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1632 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1633 // Put sign in zero flag.
1634 tst(scratch, Operand(HeapNumber::kSignMask));
1635 // Get the second half of the double. For some exponents we don't
1636 // actually need this because the bits get shifted out again, but
1637 // it's probably slower to test than just to do it.
1638 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1639 // Shift down 22 bits to get the last 10 bits.
1640 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1641 // Move down according to the exponent.
1642 mov(dest, Operand(scratch, LSR, dest));
1643 // Fix sign if sign bit was set.
1644 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1645 bind(&done);
1646 }
1647}
1648
1649
Andrei Popescu31002712010-02-23 13:46:05 +00001650void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1651 Register src,
1652 int num_least_bits) {
1653 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001654 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001655 } else {
1656 mov(dst, Operand(src, ASR, kSmiTagSize));
1657 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1658 }
1659}
1660
1661
Steve Blocka7e24c12009-10-30 11:49:00 +00001662void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1663 // All parameters are on the stack. r0 has the return value after call.
1664
1665 // If the expected number of arguments of the runtime function is
1666 // constant, we check that the actual number of arguments match the
1667 // expectation.
1668 if (f->nargs >= 0 && f->nargs != num_arguments) {
1669 IllegalOperation(num_arguments);
1670 return;
1671 }
1672
Leon Clarke4515c472010-02-03 11:58:03 +00001673 // TODO(1236192): Most runtime routines don't need the number of
1674 // arguments passed in because it is constant. At some point we
1675 // should remove this need and make the runtime routine entry code
1676 // smarter.
1677 mov(r0, Operand(num_arguments));
1678 mov(r1, Operand(ExternalReference(f)));
1679 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001680 CallStub(&stub);
1681}
1682
1683
1684void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1685 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1686}
1687
1688
Ben Murdochb0fe1622011-05-05 13:52:32 +01001689void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1690 Runtime::Function* function = Runtime::FunctionForId(id);
1691 mov(r0, Operand(function->nargs));
1692 mov(r1, Operand(ExternalReference(function)));
1693 CEntryStub stub(1);
1694 stub.SaveDoubles();
1695 CallStub(&stub);
1696}
1697
1698
Andrei Popescu402d9372010-02-26 13:31:12 +00001699void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1700 int num_arguments) {
1701 mov(r0, Operand(num_arguments));
1702 mov(r1, Operand(ext));
1703
1704 CEntryStub stub(1);
1705 CallStub(&stub);
1706}
1707
1708
Steve Block6ded16b2010-05-10 14:33:55 +01001709void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1710 int num_arguments,
1711 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001712 // TODO(1236192): Most runtime routines don't need the number of
1713 // arguments passed in because it is constant. At some point we
1714 // should remove this need and make the runtime routine entry code
1715 // smarter.
1716 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001717 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001718}
1719
1720
Steve Block6ded16b2010-05-10 14:33:55 +01001721void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1722 int num_arguments,
1723 int result_size) {
1724 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1725}
1726
1727
1728void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001729#if defined(__thumb__)
1730 // Thumb mode builtin.
1731 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1732#endif
1733 mov(r1, Operand(builtin));
1734 CEntryStub stub(1);
1735 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1736}
1737
1738
Steve Blocka7e24c12009-10-30 11:49:00 +00001739void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001740 InvokeJSFlags flags,
1741 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001742 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001743 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001744 Call(r2);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001745 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001746 } else {
1747 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001748 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001749 }
1750}
1751
1752
Steve Block791712a2010-08-27 10:21:07 +01001753void MacroAssembler::GetBuiltinFunction(Register target,
1754 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001755 // Load the builtins object into target register.
1756 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1757 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001758 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001759 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01001760 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1761}
1762
1763
1764void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1765 ASSERT(!target.is(r1));
1766 GetBuiltinFunction(r1, id);
1767 // Load the code entry point from the builtins object.
1768 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001769}
1770
1771
1772void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1773 Register scratch1, Register scratch2) {
1774 if (FLAG_native_code_counters && counter->Enabled()) {
1775 mov(scratch1, Operand(value));
1776 mov(scratch2, Operand(ExternalReference(counter)));
1777 str(scratch1, MemOperand(scratch2));
1778 }
1779}
1780
1781
1782void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1783 Register scratch1, Register scratch2) {
1784 ASSERT(value > 0);
1785 if (FLAG_native_code_counters && counter->Enabled()) {
1786 mov(scratch2, Operand(ExternalReference(counter)));
1787 ldr(scratch1, MemOperand(scratch2));
1788 add(scratch1, scratch1, Operand(value));
1789 str(scratch1, MemOperand(scratch2));
1790 }
1791}
1792
1793
1794void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1795 Register scratch1, Register scratch2) {
1796 ASSERT(value > 0);
1797 if (FLAG_native_code_counters && counter->Enabled()) {
1798 mov(scratch2, Operand(ExternalReference(counter)));
1799 ldr(scratch1, MemOperand(scratch2));
1800 sub(scratch1, scratch1, Operand(value));
1801 str(scratch1, MemOperand(scratch2));
1802 }
1803}
1804
1805
1806void MacroAssembler::Assert(Condition cc, const char* msg) {
1807 if (FLAG_debug_code)
1808 Check(cc, msg);
1809}
1810
1811
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001812void MacroAssembler::AssertRegisterIsRoot(Register reg,
1813 Heap::RootListIndex index) {
1814 if (FLAG_debug_code) {
1815 LoadRoot(ip, index);
1816 cmp(reg, ip);
1817 Check(eq, "Register did not match expected root");
1818 }
1819}
1820
1821
Iain Merrick75681382010-08-19 15:07:18 +01001822void MacroAssembler::AssertFastElements(Register elements) {
1823 if (FLAG_debug_code) {
1824 ASSERT(!elements.is(ip));
1825 Label ok;
1826 push(elements);
1827 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
1828 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
1829 cmp(elements, ip);
1830 b(eq, &ok);
1831 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
1832 cmp(elements, ip);
1833 b(eq, &ok);
1834 Abort("JSObject with fast elements map has slow elements");
1835 bind(&ok);
1836 pop(elements);
1837 }
1838}
1839
1840
Steve Blocka7e24c12009-10-30 11:49:00 +00001841void MacroAssembler::Check(Condition cc, const char* msg) {
1842 Label L;
1843 b(cc, &L);
1844 Abort(msg);
1845 // will not return here
1846 bind(&L);
1847}
1848
1849
1850void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01001851 Label abort_start;
1852 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00001853 // We want to pass the msg string like a smi to avoid GC
1854 // problems, however msg is not guaranteed to be aligned
1855 // properly. Instead, we pass an aligned pointer that is
1856 // a proper v8 smi, but also pass the alignment difference
1857 // from the real pointer as a smi.
1858 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1859 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1860 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1861#ifdef DEBUG
1862 if (msg != NULL) {
1863 RecordComment("Abort message: ");
1864 RecordComment(msg);
1865 }
1866#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001867 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001868 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00001869
Steve Blocka7e24c12009-10-30 11:49:00 +00001870 mov(r0, Operand(p0));
1871 push(r0);
1872 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1873 push(r0);
1874 CallRuntime(Runtime::kAbort, 2);
1875 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01001876 if (is_const_pool_blocked()) {
1877 // If the calling code cares about the exact number of
1878 // instructions generated, we insert padding here to keep the size
1879 // of the Abort macro constant.
1880 static const int kExpectedAbortInstructions = 10;
1881 int abort_instructions = InstructionsGeneratedSince(&abort_start);
1882 ASSERT(abort_instructions <= kExpectedAbortInstructions);
1883 while (abort_instructions++ < kExpectedAbortInstructions) {
1884 nop();
1885 }
1886 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001887}
1888
1889
Steve Blockd0582a62009-12-15 09:54:21 +00001890void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1891 if (context_chain_length > 0) {
1892 // Move up the chain of contexts to the context containing the slot.
1893 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1894 // Load the function context (which is the incoming, outer context).
1895 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1896 for (int i = 1; i < context_chain_length; i++) {
1897 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1898 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1899 }
1900 // The context may be an intermediate context, not a function context.
1901 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1902 } else { // Slot is in the current function context.
1903 // The context may be an intermediate context, not a function context.
1904 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1905 }
1906}
1907
1908
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001909void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1910 // Load the global or builtins object from the current context.
1911 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1912 // Load the global context from the global or builtins object.
1913 ldr(function, FieldMemOperand(function,
1914 GlobalObject::kGlobalContextOffset));
1915 // Load the function from the global context.
1916 ldr(function, MemOperand(function, Context::SlotOffset(index)));
1917}
1918
1919
1920void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1921 Register map,
1922 Register scratch) {
1923 // Load the initial map. The global functions all have initial maps.
1924 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1925 if (FLAG_debug_code) {
1926 Label ok, fail;
1927 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, false);
1928 b(&ok);
1929 bind(&fail);
1930 Abort("Global functions must have initial map");
1931 bind(&ok);
1932 }
1933}
1934
1935
Andrei Popescu31002712010-02-23 13:46:05 +00001936void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1937 Register reg2,
1938 Label* on_not_both_smi) {
1939 ASSERT_EQ(0, kSmiTag);
1940 tst(reg1, Operand(kSmiTagMask));
1941 tst(reg2, Operand(kSmiTagMask), eq);
1942 b(ne, on_not_both_smi);
1943}
1944
1945
1946void MacroAssembler::JumpIfEitherSmi(Register reg1,
1947 Register reg2,
1948 Label* on_either_smi) {
1949 ASSERT_EQ(0, kSmiTag);
1950 tst(reg1, Operand(kSmiTagMask));
1951 tst(reg2, Operand(kSmiTagMask), ne);
1952 b(eq, on_either_smi);
1953}
1954
1955
Iain Merrick75681382010-08-19 15:07:18 +01001956void MacroAssembler::AbortIfSmi(Register object) {
1957 ASSERT_EQ(0, kSmiTag);
1958 tst(object, Operand(kSmiTagMask));
1959 Assert(ne, "Operand is a smi");
1960}
1961
1962
Leon Clarked91b9f72010-01-27 17:25:45 +00001963void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1964 Register first,
1965 Register second,
1966 Register scratch1,
1967 Register scratch2,
1968 Label* failure) {
1969 // Test that both first and second are sequential ASCII strings.
1970 // Assume that they are non-smis.
1971 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1972 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1973 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1974 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001975
1976 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1977 scratch2,
1978 scratch1,
1979 scratch2,
1980 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001981}
1982
1983void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1984 Register second,
1985 Register scratch1,
1986 Register scratch2,
1987 Label* failure) {
1988 // Check that neither is a smi.
1989 ASSERT_EQ(0, kSmiTag);
1990 and_(scratch1, first, Operand(second));
1991 tst(scratch1, Operand(kSmiTagMask));
1992 b(eq, failure);
1993 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1994 second,
1995 scratch1,
1996 scratch2,
1997 failure);
1998}
1999
Steve Blockd0582a62009-12-15 09:54:21 +00002000
Steve Block6ded16b2010-05-10 14:33:55 +01002001// Allocates a heap number or jumps to the need_gc label if the young space
2002// is full and a scavenge is needed.
2003void MacroAssembler::AllocateHeapNumber(Register result,
2004 Register scratch1,
2005 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002006 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01002007 Label* gc_required) {
2008 // Allocate an object in the heap for the heap number and tag it as a heap
2009 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01002010 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01002011 result,
2012 scratch1,
2013 scratch2,
2014 gc_required,
2015 TAG_OBJECT);
2016
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002017 // Store heap number map in the allocated object.
2018 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2019 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002020}
2021
2022
Steve Block8defd9f2010-07-08 12:39:36 +01002023void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2024 DwVfpRegister value,
2025 Register scratch1,
2026 Register scratch2,
2027 Register heap_number_map,
2028 Label* gc_required) {
2029 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
2030 sub(scratch1, result, Operand(kHeapObjectTag));
2031 vstr(value, scratch1, HeapNumber::kValueOffset);
2032}
2033
2034
Ben Murdochbb769b22010-08-11 14:56:33 +01002035// Copies a fixed number of fields of heap objects from src to dst.
2036void MacroAssembler::CopyFields(Register dst,
2037 Register src,
2038 RegList temps,
2039 int field_count) {
2040 // At least one bit set in the first 15 registers.
2041 ASSERT((temps & ((1 << 15) - 1)) != 0);
2042 ASSERT((temps & dst.bit()) == 0);
2043 ASSERT((temps & src.bit()) == 0);
2044 // Primitive implementation using only one temporary register.
2045
2046 Register tmp = no_reg;
2047 // Find a temp register in temps list.
2048 for (int i = 0; i < 15; i++) {
2049 if ((temps & (1 << i)) != 0) {
2050 tmp.set_code(i);
2051 break;
2052 }
2053 }
2054 ASSERT(!tmp.is(no_reg));
2055
2056 for (int i = 0; i < field_count; i++) {
2057 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
2058 str(tmp, FieldMemOperand(dst, i * kPointerSize));
2059 }
2060}
2061
2062
Steve Block8defd9f2010-07-08 12:39:36 +01002063void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
2064 Register source, // Input.
2065 Register scratch) {
2066 ASSERT(!zeros.is(source) || !source.is(zeros));
2067 ASSERT(!zeros.is(scratch));
2068 ASSERT(!scratch.is(ip));
2069 ASSERT(!source.is(ip));
2070 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002071#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2072 clz(zeros, source); // This instruction is only supported after ARM5.
2073#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01002074 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01002075 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002076 // Top 16.
2077 tst(scratch, Operand(0xffff0000));
2078 add(zeros, zeros, Operand(16), LeaveCC, eq);
2079 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2080 // Top 8.
2081 tst(scratch, Operand(0xff000000));
2082 add(zeros, zeros, Operand(8), LeaveCC, eq);
2083 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2084 // Top 4.
2085 tst(scratch, Operand(0xf0000000));
2086 add(zeros, zeros, Operand(4), LeaveCC, eq);
2087 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2088 // Top 2.
2089 tst(scratch, Operand(0xc0000000));
2090 add(zeros, zeros, Operand(2), LeaveCC, eq);
2091 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2092 // Top bit.
2093 tst(scratch, Operand(0x80000000u));
2094 add(zeros, zeros, Operand(1), LeaveCC, eq);
2095#endif
2096}
2097
2098
2099void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2100 Register first,
2101 Register second,
2102 Register scratch1,
2103 Register scratch2,
2104 Label* failure) {
2105 int kFlatAsciiStringMask =
2106 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2107 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2108 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2109 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2110 cmp(scratch1, Operand(kFlatAsciiStringTag));
2111 // Ignore second test if first test failed.
2112 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2113 b(ne, failure);
2114}
2115
2116
2117void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
2118 Register scratch,
2119 Label* failure) {
2120 int kFlatAsciiStringMask =
2121 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2122 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2123 and_(scratch, type, Operand(kFlatAsciiStringMask));
2124 cmp(scratch, Operand(kFlatAsciiStringTag));
2125 b(ne, failure);
2126}
2127
2128
2129void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2130 int frame_alignment = ActivationFrameAlignment();
2131 // Up to four simple arguments are passed in registers r0..r3.
2132 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2133 if (frame_alignment > kPointerSize) {
2134 // Make stack end at alignment and make room for num_arguments - 4 words
2135 // and the original value of sp.
2136 mov(scratch, sp);
2137 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
2138 ASSERT(IsPowerOf2(frame_alignment));
2139 and_(sp, sp, Operand(-frame_alignment));
2140 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2141 } else {
2142 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
2143 }
2144}
2145
2146
2147void MacroAssembler::CallCFunction(ExternalReference function,
2148 int num_arguments) {
2149 mov(ip, Operand(function));
2150 CallCFunction(ip, num_arguments);
2151}
2152
2153
2154void MacroAssembler::CallCFunction(Register function, int num_arguments) {
2155 // Make sure that the stack is aligned before calling a C function unless
2156 // running in the simulator. The simulator has its own alignment check which
2157 // provides more information.
2158#if defined(V8_HOST_ARCH_ARM)
2159 if (FLAG_debug_code) {
2160 int frame_alignment = OS::ActivationFrameAlignment();
2161 int frame_alignment_mask = frame_alignment - 1;
2162 if (frame_alignment > kPointerSize) {
2163 ASSERT(IsPowerOf2(frame_alignment));
2164 Label alignment_as_expected;
2165 tst(sp, Operand(frame_alignment_mask));
2166 b(eq, &alignment_as_expected);
2167 // Don't use Check here, as it will call Runtime_Abort possibly
2168 // re-entering here.
2169 stop("Unexpected alignment");
2170 bind(&alignment_as_expected);
2171 }
2172 }
2173#endif
2174
2175 // Just call directly. The function called cannot cause a GC, or
2176 // allow preemption, so the return address in the link register
2177 // stays correct.
2178 Call(function);
2179 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2180 if (OS::ActivationFrameAlignment() > kPointerSize) {
2181 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
2182 } else {
2183 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
2184 }
2185}
2186
2187
Steve Blocka7e24c12009-10-30 11:49:00 +00002188#ifdef ENABLE_DEBUGGER_SUPPORT
2189CodePatcher::CodePatcher(byte* address, int instructions)
2190 : address_(address),
2191 instructions_(instructions),
2192 size_(instructions * Assembler::kInstrSize),
2193 masm_(address, size_ + Assembler::kGap) {
2194 // Create a new macro assembler pointing to the address of the code to patch.
2195 // The size is adjusted with kGap on order for the assembler to generate size
2196 // bytes of instructions without failing with buffer size constraints.
2197 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2198}
2199
2200
2201CodePatcher::~CodePatcher() {
2202 // Indicate that code has changed.
2203 CPU::FlushICache(address_, size_);
2204
2205 // Check that the code was patched as expected.
2206 ASSERT(masm_.pc_ == address_ + size_);
2207 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2208}
2209
2210
2211void CodePatcher::Emit(Instr x) {
2212 masm()->emit(x);
2213}
2214
2215
2216void CodePatcher::Emit(Address addr) {
2217 masm()->emit(reinterpret_cast<Instr>(addr));
2218}
2219#endif // ENABLE_DEBUGGER_SUPPORT
2220
2221
2222} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002223
2224#endif // V8_TARGET_ARCH_ARM