blob: 5d8df1afac12bab369d3c32ecb1e9e4b5941c61b [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Iain Merrick9ac36c92010-09-13 15:29:50 +010028#include <limits.h> // For LONG_MIN, LONG_MAX.
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#include "v8.h"
31
Leon Clarkef7060e22010-06-03 12:02:55 +010032#if defined(V8_TARGET_ARCH_ARM)
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "bootstrapper.h"
35#include "codegen-inl.h"
36#include "debug.h"
37#include "runtime.h"
38
39namespace v8 {
40namespace internal {
41
42MacroAssembler::MacroAssembler(void* buffer, int size)
43 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000044 generating_stub_(false),
45 allow_stub_calls_(true),
46 code_object_(Heap::undefined_value()) {
47}
48
49
50// We always generate arm code, never thumb code, even if V8 is compiled to
51// thumb, so we require inter-working support
52#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
53#error "flag -mthumb-interwork missing"
54#endif
55
56
57// We do not support thumb inter-working with an arm architecture not supporting
58// the blx instruction (below v5t). If you know what CPU you are compiling for
59// you can use -march=armv7 or similar.
60#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
61# error "For thumb inter-working we require an architecture which supports blx"
62#endif
63
64
Steve Blocka7e24c12009-10-30 11:49:00 +000065// Using bx does not yield better code, so use it only when required
66#if defined(USE_THUMB_INTERWORK)
67#define USE_BX 1
68#endif
69
70
71void MacroAssembler::Jump(Register target, Condition cond) {
72#if USE_BX
73 bx(target, cond);
74#else
75 mov(pc, Operand(target), LeaveCC, cond);
76#endif
77}
78
79
80void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
81 Condition cond) {
82#if USE_BX
83 mov(ip, Operand(target, rmode), LeaveCC, cond);
84 bx(ip, cond);
85#else
86 mov(pc, Operand(target, rmode), LeaveCC, cond);
87#endif
88}
89
90
91void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
92 Condition cond) {
93 ASSERT(!RelocInfo::IsCodeTarget(rmode));
94 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
95}
96
97
98void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
99 Condition cond) {
100 ASSERT(RelocInfo::IsCodeTarget(rmode));
101 // 'code' is always generated ARM code, never THUMB code
102 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
103}
104
105
106void MacroAssembler::Call(Register target, Condition cond) {
107#if USE_BLX
108 blx(target, cond);
109#else
110 // set lr for return at current pc + 8
111 mov(lr, Operand(pc), LeaveCC, cond);
112 mov(pc, Operand(target), LeaveCC, cond);
113#endif
114}
115
116
117void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
118 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100119#if USE_BLX
120 // On ARMv5 and after the recommended call sequence is:
121 // ldr ip, [pc, #...]
122 // blx ip
123
124 // The two instructions (ldr and blx) could be separated by a constant
125 // pool and the code would still work. The issue comes from the
126 // patching code which expect the ldr to be just above the blx.
127 { BlockConstPoolScope block_const_pool(this);
128 // Statement positions are expected to be recorded when the target
129 // address is loaded. The mov method will automatically record
130 // positions when pc is the target, since this is not the case here
131 // we have to do it explicitly.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800132 positions_recorder()->WriteRecordedPositions();
Steve Block6ded16b2010-05-10 14:33:55 +0100133
134 mov(ip, Operand(target, rmode), LeaveCC, cond);
135 blx(ip, cond);
136 }
137
138 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
139#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 // Set lr for return at current pc + 8.
141 mov(lr, Operand(pc), LeaveCC, cond);
142 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
143 mov(pc, Operand(target, rmode), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100146#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000147}
148
149
150void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
151 Condition cond) {
152 ASSERT(!RelocInfo::IsCodeTarget(rmode));
153 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
154}
155
156
157void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
158 Condition cond) {
159 ASSERT(RelocInfo::IsCodeTarget(rmode));
160 // 'code' is always generated ARM code, never THUMB code
161 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
162}
163
164
165void MacroAssembler::Ret(Condition cond) {
166#if USE_BX
167 bx(lr, cond);
168#else
169 mov(pc, Operand(lr), LeaveCC, cond);
170#endif
171}
172
173
Leon Clarkee46be812010-01-19 14:06:41 +0000174void MacroAssembler::Drop(int count, Condition cond) {
175 if (count > 0) {
176 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
177 }
178}
179
180
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181void MacroAssembler::Ret(int drop, Condition cond) {
182 Drop(drop, cond);
183 Ret(cond);
184}
185
186
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100187void MacroAssembler::Swap(Register reg1,
188 Register reg2,
189 Register scratch,
190 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100191 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100192 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
193 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
194 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100195 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196 mov(scratch, reg1, LeaveCC, cond);
197 mov(reg1, reg2, LeaveCC, cond);
198 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100199 }
200}
201
202
Leon Clarkee46be812010-01-19 14:06:41 +0000203void MacroAssembler::Call(Label* target) {
204 bl(target);
205}
206
207
208void MacroAssembler::Move(Register dst, Handle<Object> value) {
209 mov(dst, Operand(value));
210}
Steve Blockd0582a62009-12-15 09:54:21 +0000211
212
Steve Block6ded16b2010-05-10 14:33:55 +0100213void MacroAssembler::Move(Register dst, Register src) {
214 if (!dst.is(src)) {
215 mov(dst, src);
216 }
217}
218
219
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100220void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
221 Condition cond) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800222 if (!src2.is_reg() &&
223 !src2.must_use_constant_pool() &&
224 src2.immediate() == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100225 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800226
227 } else if (!src2.is_single_instruction() &&
228 !src2.must_use_constant_pool() &&
229 CpuFeatures::IsSupported(ARMv7) &&
230 IsPowerOf2(src2.immediate() + 1)) {
231 ubfx(dst, src1, 0, WhichPowerOf2(src2.immediate() + 1), cond);
232
233 } else {
234 and_(dst, src1, src2, LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100235 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100236}
237
238
239void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
240 Condition cond) {
241 ASSERT(lsb < 32);
242 if (!CpuFeatures::IsSupported(ARMv7)) {
243 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
244 and_(dst, src1, Operand(mask), LeaveCC, cond);
245 if (lsb != 0) {
246 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
247 }
248 } else {
249 ubfx(dst, src1, lsb, width, cond);
250 }
251}
252
253
254void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
255 Condition cond) {
256 ASSERT(lsb < 32);
257 if (!CpuFeatures::IsSupported(ARMv7)) {
258 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
259 and_(dst, src1, Operand(mask), LeaveCC, cond);
260 int shift_up = 32 - lsb - width;
261 int shift_down = lsb + shift_up;
262 if (shift_up != 0) {
263 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
264 }
265 if (shift_down != 0) {
266 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
267 }
268 } else {
269 sbfx(dst, src1, lsb, width, cond);
270 }
271}
272
273
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,
Steve Block1e0659c2011-05-24 12:43:12 +0100321 Instruction::kInstrSizeLog2 - kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 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,
Steve Block1e0659c2011-05-24 12:43:12 +0100372 Condition cond,
Steve Block6ded16b2010-05-10 14:33:55 +0100373 Label* branch) {
Steve Block1e0659c2011-05-24 12:43:12 +0100374 ASSERT(cond == eq || cond == ne);
Steve Block6ded16b2010-05-10 14:33:55 +0100375 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
376 cmp(scratch, Operand(ExternalReference::new_space_start()));
Steve Block1e0659c2011-05-24 12:43:12 +0100377 b(cond, branch);
Steve Block6ded16b2010-05-10 14:33:55 +0100378}
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
Steve Block1e0659c2011-05-24 12:43:12 +0100488void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register reg) {
489 str(reg, SafepointRegistersAndDoublesSlot(reg));
490}
491
492
493void MacroAssembler::StoreToSafepointRegisterSlot(Register reg) {
494 str(reg, SafepointRegisterSlot(reg));
495}
496
497
498void MacroAssembler::LoadFromSafepointRegisterSlot(Register reg) {
499 ldr(reg, SafepointRegisterSlot(reg));
500}
501
502
Ben Murdochb0fe1622011-05-05 13:52:32 +0100503int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
504 // The registers are pushed starting with the highest encoding,
505 // which means that lowest encodings are closest to the stack pointer.
506 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
507 return reg_code;
508}
509
510
Steve Block1e0659c2011-05-24 12:43:12 +0100511MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
512 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
513}
514
515
516MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
517 // General purpose registers are pushed last on the stack.
518 int doubles_size = DwVfpRegister::kNumAllocatableRegisters * kDoubleSize;
519 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
520 return MemOperand(sp, doubles_size + register_offset);
521}
522
523
Leon Clarkef7060e22010-06-03 12:02:55 +0100524void MacroAssembler::Ldrd(Register dst1, Register dst2,
525 const MemOperand& src, Condition cond) {
526 ASSERT(src.rm().is(no_reg));
527 ASSERT(!dst1.is(lr)); // r14.
528 ASSERT_EQ(0, dst1.code() % 2);
529 ASSERT_EQ(dst1.code() + 1, dst2.code());
530
531 // Generate two ldr instructions if ldrd is not available.
532 if (CpuFeatures::IsSupported(ARMv7)) {
533 CpuFeatures::Scope scope(ARMv7);
534 ldrd(dst1, dst2, src, cond);
535 } else {
536 MemOperand src2(src);
537 src2.set_offset(src2.offset() + 4);
538 if (dst1.is(src.rn())) {
539 ldr(dst2, src2, cond);
540 ldr(dst1, src, cond);
541 } else {
542 ldr(dst1, src, cond);
543 ldr(dst2, src2, cond);
544 }
545 }
546}
547
548
549void MacroAssembler::Strd(Register src1, Register src2,
550 const MemOperand& dst, Condition cond) {
551 ASSERT(dst.rm().is(no_reg));
552 ASSERT(!src1.is(lr)); // r14.
553 ASSERT_EQ(0, src1.code() % 2);
554 ASSERT_EQ(src1.code() + 1, src2.code());
555
556 // Generate two str instructions if strd is not available.
557 if (CpuFeatures::IsSupported(ARMv7)) {
558 CpuFeatures::Scope scope(ARMv7);
559 strd(src1, src2, dst, cond);
560 } else {
561 MemOperand dst2(dst);
562 dst2.set_offset(dst2.offset() + 4);
563 str(src1, dst, cond);
564 str(src2, dst2, cond);
565 }
566}
567
568
Ben Murdochb8e0da22011-05-16 14:20:40 +0100569void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
570 const Register scratch,
571 const Condition cond) {
572 vmrs(scratch, cond);
573 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
574 vmsr(scratch, cond);
575}
576
577
578void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
579 const DwVfpRegister src2,
580 const Condition cond) {
581 // Compare and move FPSCR flags to the normal condition flags.
582 VFPCompareAndLoadFlags(src1, src2, pc, cond);
583}
584
585void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
586 const double src2,
587 const Condition cond) {
588 // Compare and move FPSCR flags to the normal condition flags.
589 VFPCompareAndLoadFlags(src1, src2, pc, cond);
590}
591
592
593void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
594 const DwVfpRegister src2,
595 const Register fpscr_flags,
596 const Condition cond) {
597 // Compare and load FPSCR.
598 vcmp(src1, src2, cond);
599 vmrs(fpscr_flags, cond);
600}
601
602void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
603 const double src2,
604 const Register fpscr_flags,
605 const Condition cond) {
606 // Compare and load FPSCR.
607 vcmp(src1, src2, cond);
608 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100609}
610
611
Steve Blocka7e24c12009-10-30 11:49:00 +0000612void MacroAssembler::EnterFrame(StackFrame::Type type) {
613 // r0-r3: preserved
614 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
615 mov(ip, Operand(Smi::FromInt(type)));
616 push(ip);
617 mov(ip, Operand(CodeObject()));
618 push(ip);
619 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
620}
621
622
623void MacroAssembler::LeaveFrame(StackFrame::Type type) {
624 // r0: preserved
625 // r1: preserved
626 // r2: preserved
627
628 // Drop the execution stack down to the frame pointer and restore
629 // the caller frame pointer and return address.
630 mov(sp, fp);
631 ldm(ia_w, sp, fp.bit() | lr.bit());
632}
633
634
Steve Block1e0659c2011-05-24 12:43:12 +0100635void MacroAssembler::EnterExitFrame(bool save_doubles, int stack_space) {
636 // Setup the frame structure on the stack.
637 ASSERT_EQ(2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
638 ASSERT_EQ(1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
639 ASSERT_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
640 Push(lr, fp);
Andrei Popescu402d9372010-02-26 13:31:12 +0000641 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Block1e0659c2011-05-24 12:43:12 +0100642 // Reserve room for saved entry sp and code object.
643 sub(sp, sp, Operand(2 * kPointerSize));
644 if (FLAG_debug_code) {
645 mov(ip, Operand(0));
646 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
647 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000648 mov(ip, Operand(CodeObject()));
Steve Block1e0659c2011-05-24 12:43:12 +0100649 str(ip, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000650
651 // Save the frame pointer and the context in top.
652 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
653 str(fp, MemOperand(ip));
654 mov(ip, Operand(ExternalReference(Top::k_context_address)));
655 str(cp, MemOperand(ip));
656
Ben Murdochb0fe1622011-05-05 13:52:32 +0100657 // Optionally save all double registers.
658 if (save_doubles) {
Steve Block1e0659c2011-05-24 12:43:12 +0100659 sub(sp, sp, Operand(DwVfpRegister::kNumRegisters * kDoubleSize));
660 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100661 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
662 DwVfpRegister reg = DwVfpRegister::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100663 vstr(reg, fp, offset - ((i + 1) * kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664 }
Steve Block1e0659c2011-05-24 12:43:12 +0100665 // Note that d0 will be accessible at
666 // fp - 2 * kPointerSize - DwVfpRegister::kNumRegisters * kDoubleSize,
667 // since the sp slot and code slot were pushed after the fp.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100668 }
Steve Block1e0659c2011-05-24 12:43:12 +0100669
670 // Reserve place for the return address and stack space and align the frame
671 // preparing for calling the runtime function.
672 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
673 sub(sp, sp, Operand((stack_space + 1) * kPointerSize));
674 if (frame_alignment > 0) {
675 ASSERT(IsPowerOf2(frame_alignment));
676 and_(sp, sp, Operand(-frame_alignment));
677 }
678
679 // Set the exit frame sp value to point just before the return address
680 // location.
681 add(ip, sp, Operand(kPointerSize));
682 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000683}
684
685
Steve Block6ded16b2010-05-10 14:33:55 +0100686void MacroAssembler::InitializeNewString(Register string,
687 Register length,
688 Heap::RootListIndex map_index,
689 Register scratch1,
690 Register scratch2) {
691 mov(scratch1, Operand(length, LSL, kSmiTagSize));
692 LoadRoot(scratch2, map_index);
693 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
694 mov(scratch1, Operand(String::kEmptyHashField));
695 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
696 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
697}
698
699
700int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000701#if defined(V8_HOST_ARCH_ARM)
702 // Running on the real platform. Use the alignment as mandated by the local
703 // environment.
704 // Note: This will break if we ever start generating snapshots on one ARM
705 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100706 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000707#else // defined(V8_HOST_ARCH_ARM)
708 // If we are using the simulator then we should always align to the expected
709 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100710 // if the target platform will need alignment, so this is controlled from a
711 // flag.
712 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000713#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000714}
715
716
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717void MacroAssembler::LeaveExitFrame(bool save_doubles) {
718 // Optionally restore all double registers.
719 if (save_doubles) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100720 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
721 DwVfpRegister reg = DwVfpRegister::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100722 const int offset = -2 * kPointerSize;
723 vldr(reg, fp, offset - ((i + 1) * kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100724 }
725 }
726
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100728 mov(r3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000729 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
730 str(r3, MemOperand(ip));
731
732 // Restore current context from top and clear it in debug mode.
733 mov(ip, Operand(ExternalReference(Top::k_context_address)));
734 ldr(cp, MemOperand(ip));
735#ifdef DEBUG
736 str(r3, MemOperand(ip));
737#endif
738
Steve Block1e0659c2011-05-24 12:43:12 +0100739 // Tear down the exit frame, pop the arguments, and return. Callee-saved
740 // register r4 still holds argc.
741 mov(sp, Operand(fp));
742 ldm(ia_w, sp, fp.bit() | lr.bit());
743 add(sp, sp, Operand(r4, LSL, kPointerSizeLog2));
744 mov(pc, lr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000745}
746
747
748void MacroAssembler::InvokePrologue(const ParameterCount& expected,
749 const ParameterCount& actual,
750 Handle<Code> code_constant,
751 Register code_reg,
752 Label* done,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100753 InvokeFlag flag,
754 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 bool definitely_matches = false;
756 Label regular_invoke;
757
758 // Check whether the expected and actual arguments count match. If not,
759 // setup registers according to contract with ArgumentsAdaptorTrampoline:
760 // r0: actual arguments count
761 // r1: function (passed through to callee)
762 // r2: expected arguments count
763 // r3: callee code entry
764
765 // The code below is made a lot easier because the calling code already sets
766 // up actual and expected registers according to the contract if values are
767 // passed in registers.
768 ASSERT(actual.is_immediate() || actual.reg().is(r0));
769 ASSERT(expected.is_immediate() || expected.reg().is(r2));
770 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
771
772 if (expected.is_immediate()) {
773 ASSERT(actual.is_immediate());
774 if (expected.immediate() == actual.immediate()) {
775 definitely_matches = true;
776 } else {
777 mov(r0, Operand(actual.immediate()));
778 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
779 if (expected.immediate() == sentinel) {
780 // Don't worry about adapting arguments for builtins that
781 // don't want that done. Skip adaption code by making it look
782 // like we have a match between expected and actual number of
783 // arguments.
784 definitely_matches = true;
785 } else {
786 mov(r2, Operand(expected.immediate()));
787 }
788 }
789 } else {
790 if (actual.is_immediate()) {
791 cmp(expected.reg(), Operand(actual.immediate()));
792 b(eq, &regular_invoke);
793 mov(r0, Operand(actual.immediate()));
794 } else {
795 cmp(expected.reg(), Operand(actual.reg()));
796 b(eq, &regular_invoke);
797 }
798 }
799
800 if (!definitely_matches) {
801 if (!code_constant.is_null()) {
802 mov(r3, Operand(code_constant));
803 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
804 }
805
806 Handle<Code> adaptor =
807 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
808 if (flag == CALL_FUNCTION) {
809 Call(adaptor, RelocInfo::CODE_TARGET);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100810 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 b(done);
812 } else {
813 Jump(adaptor, RelocInfo::CODE_TARGET);
814 }
815 bind(&regular_invoke);
816 }
817}
818
819
820void MacroAssembler::InvokeCode(Register code,
821 const ParameterCount& expected,
822 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100823 InvokeFlag flag,
824 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 Label done;
826
Ben Murdochb8e0da22011-05-16 14:20:40 +0100827 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
828 post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000829 if (flag == CALL_FUNCTION) {
830 Call(code);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100831 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000832 } else {
833 ASSERT(flag == JUMP_FUNCTION);
834 Jump(code);
835 }
836
837 // Continue here if InvokePrologue does handle the invocation due to
838 // mismatched parameter counts.
839 bind(&done);
840}
841
842
843void MacroAssembler::InvokeCode(Handle<Code> code,
844 const ParameterCount& expected,
845 const ParameterCount& actual,
846 RelocInfo::Mode rmode,
847 InvokeFlag flag) {
848 Label done;
849
850 InvokePrologue(expected, actual, code, no_reg, &done, flag);
851 if (flag == CALL_FUNCTION) {
852 Call(code, rmode);
853 } else {
854 Jump(code, rmode);
855 }
856
857 // Continue here if InvokePrologue does handle the invocation due to
858 // mismatched parameter counts.
859 bind(&done);
860}
861
862
863void MacroAssembler::InvokeFunction(Register fun,
864 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100865 InvokeFlag flag,
866 PostCallGenerator* post_call_generator) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 // Contract with called JS functions requires that function is passed in r1.
868 ASSERT(fun.is(r1));
869
870 Register expected_reg = r2;
871 Register code_reg = r3;
872
873 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
874 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
875 ldr(expected_reg,
876 FieldMemOperand(code_reg,
877 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100878 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000879 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100880 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000881
882 ParameterCount expected(expected_reg);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100883 InvokeCode(code_reg, expected, actual, flag, post_call_generator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000884}
885
886
Andrei Popescu402d9372010-02-26 13:31:12 +0000887void MacroAssembler::InvokeFunction(JSFunction* function,
888 const ParameterCount& actual,
889 InvokeFlag flag) {
890 ASSERT(function->is_compiled());
891
892 // Get the function and setup the context.
893 mov(r1, Operand(Handle<JSFunction>(function)));
894 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
895
896 // Invoke the cached code.
897 Handle<Code> code(function->code());
898 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100899 if (V8::UseCrankshaft()) {
900 // TODO(kasperl): For now, we always call indirectly through the
901 // code field in the function to allow recompilation to take effect
902 // without changing any of the call sites.
903 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
904 InvokeCode(r3, expected, actual, flag);
905 } else {
906 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
907 }
908}
909
910
911void MacroAssembler::IsObjectJSObjectType(Register heap_object,
912 Register map,
913 Register scratch,
914 Label* fail) {
915 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
916 IsInstanceJSObjectType(map, scratch, fail);
917}
918
919
920void MacroAssembler::IsInstanceJSObjectType(Register map,
921 Register scratch,
922 Label* fail) {
923 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
924 cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
925 b(lt, fail);
926 cmp(scratch, Operand(LAST_JS_OBJECT_TYPE));
927 b(gt, fail);
928}
929
930
931void MacroAssembler::IsObjectJSStringType(Register object,
932 Register scratch,
933 Label* fail) {
934 ASSERT(kNotStringTag != 0);
935
936 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
937 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
938 tst(scratch, Operand(kIsNotStringMask));
Steve Block1e0659c2011-05-24 12:43:12 +0100939 b(ne, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +0000940}
941
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100942
Steve Blocka7e24c12009-10-30 11:49:00 +0000943#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000944void MacroAssembler::DebugBreak() {
945 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100946 mov(r0, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000947 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
948 CEntryStub ces(1);
949 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
950}
Steve Blocka7e24c12009-10-30 11:49:00 +0000951#endif
952
953
954void MacroAssembler::PushTryHandler(CodeLocation try_location,
955 HandlerType type) {
956 // Adjust this code if not the case.
957 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
958 // The pc (return address) is passed in register lr.
959 if (try_location == IN_JAVASCRIPT) {
960 if (type == TRY_CATCH_HANDLER) {
961 mov(r3, Operand(StackHandler::TRY_CATCH));
962 } else {
963 mov(r3, Operand(StackHandler::TRY_FINALLY));
964 }
965 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
966 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
967 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
968 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
969 // Save the current handler as the next handler.
970 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
971 ldr(r1, MemOperand(r3));
972 ASSERT(StackHandlerConstants::kNextOffset == 0);
973 push(r1);
974 // Link this handler as the new current one.
975 str(sp, MemOperand(r3));
976 } else {
977 // Must preserve r0-r4, r5-r7 are available.
978 ASSERT(try_location == IN_JS_ENTRY);
979 // The frame pointer does not point to a JS frame so we save NULL
980 // for fp. We expect the code throwing an exception to check fp
981 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100982 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000983 mov(r6, Operand(StackHandler::ENTRY));
984 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
985 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
986 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
987 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
988 // Save the current handler as the next handler.
989 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
990 ldr(r6, MemOperand(r7));
991 ASSERT(StackHandlerConstants::kNextOffset == 0);
992 push(r6);
993 // Link this handler as the new current one.
994 str(sp, MemOperand(r7));
995 }
996}
997
998
Leon Clarkee46be812010-01-19 14:06:41 +0000999void MacroAssembler::PopTryHandler() {
1000 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
1001 pop(r1);
1002 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
1003 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1004 str(r1, MemOperand(ip));
1005}
1006
1007
Steve Blocka7e24c12009-10-30 11:49:00 +00001008void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1009 Register scratch,
1010 Label* miss) {
1011 Label same_contexts;
1012
1013 ASSERT(!holder_reg.is(scratch));
1014 ASSERT(!holder_reg.is(ip));
1015 ASSERT(!scratch.is(ip));
1016
1017 // Load current lexical context from the stack frame.
1018 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1019 // In debug mode, make sure the lexical context is set.
1020#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001021 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001022 Check(ne, "we should not have an empty lexical context");
1023#endif
1024
1025 // Load the global context of the current context.
1026 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1027 ldr(scratch, FieldMemOperand(scratch, offset));
1028 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1029
1030 // Check the context is a global context.
1031 if (FLAG_debug_code) {
1032 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1033 // Cannot use ip as a temporary in this verification code. Due to the fact
1034 // that ip is clobbered as part of cmp with an object Operand.
1035 push(holder_reg); // Temporarily save holder on the stack.
1036 // Read the first word and compare to the global_context_map.
1037 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1038 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1039 cmp(holder_reg, ip);
1040 Check(eq, "JSGlobalObject::global_context should be a global context.");
1041 pop(holder_reg); // Restore holder.
1042 }
1043
1044 // Check if both contexts are the same.
1045 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1046 cmp(scratch, Operand(ip));
1047 b(eq, &same_contexts);
1048
1049 // Check the context is a global context.
1050 if (FLAG_debug_code) {
1051 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1052 // Cannot use ip as a temporary in this verification code. Due to the fact
1053 // that ip is clobbered as part of cmp with an object Operand.
1054 push(holder_reg); // Temporarily save holder on the stack.
1055 mov(holder_reg, ip); // Move ip to its holding place.
1056 LoadRoot(ip, Heap::kNullValueRootIndex);
1057 cmp(holder_reg, ip);
1058 Check(ne, "JSGlobalProxy::context() should not be null.");
1059
1060 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1061 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1062 cmp(holder_reg, ip);
1063 Check(eq, "JSGlobalObject::global_context should be a global context.");
1064 // Restore ip is not needed. ip is reloaded below.
1065 pop(holder_reg); // Restore holder.
1066 // Restore ip to holder's context.
1067 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1068 }
1069
1070 // Check that the security token in the calling global object is
1071 // compatible with the security token in the receiving global
1072 // object.
1073 int token_offset = Context::kHeaderSize +
1074 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1075
1076 ldr(scratch, FieldMemOperand(scratch, token_offset));
1077 ldr(ip, FieldMemOperand(ip, token_offset));
1078 cmp(scratch, Operand(ip));
1079 b(ne, miss);
1080
1081 bind(&same_contexts);
1082}
1083
1084
1085void MacroAssembler::AllocateInNewSpace(int object_size,
1086 Register result,
1087 Register scratch1,
1088 Register scratch2,
1089 Label* gc_required,
1090 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001091 if (!FLAG_inline_new) {
1092 if (FLAG_debug_code) {
1093 // Trash the registers to simulate an allocation failure.
1094 mov(result, Operand(0x7091));
1095 mov(scratch1, Operand(0x7191));
1096 mov(scratch2, Operand(0x7291));
1097 }
1098 jmp(gc_required);
1099 return;
1100 }
1101
Steve Blocka7e24c12009-10-30 11:49:00 +00001102 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001103 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001104 ASSERT(!scratch1.is(scratch2));
1105
Kristian Monsen25f61362010-05-21 11:50:48 +01001106 // Make object size into bytes.
1107 if ((flags & SIZE_IN_WORDS) != 0) {
1108 object_size *= kPointerSize;
1109 }
1110 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1111
Ben Murdochb0fe1622011-05-05 13:52:32 +01001112 // Check relative positions of allocation top and limit addresses.
1113 // The values must be adjacent in memory to allow the use of LDM.
1114 // Also, assert that the registers are numbered such that the values
1115 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001116 ExternalReference new_space_allocation_top =
1117 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001118 ExternalReference new_space_allocation_limit =
1119 ExternalReference::new_space_allocation_limit_address();
1120 intptr_t top =
1121 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1122 intptr_t limit =
1123 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1124 ASSERT((limit - top) == kPointerSize);
1125 ASSERT(result.code() < ip.code());
1126
1127 // Set up allocation top address and object size registers.
1128 Register topaddr = scratch1;
1129 Register obj_size_reg = scratch2;
1130 mov(topaddr, Operand(new_space_allocation_top));
1131 mov(obj_size_reg, Operand(object_size));
1132
1133 // This code stores a temporary value in ip. This is OK, as the code below
1134 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001135 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001136 // Load allocation top into result and allocation limit into ip.
1137 ldm(ia, topaddr, result.bit() | ip.bit());
1138 } else {
1139 if (FLAG_debug_code) {
1140 // Assert that result actually contains top on entry. ip is used
1141 // immediately below so this use of ip does not cause difference with
1142 // respect to register content between debug and release mode.
1143 ldr(ip, MemOperand(topaddr));
1144 cmp(result, ip);
1145 Check(eq, "Unexpected allocation top");
1146 }
1147 // Load allocation limit into ip. Result already contains allocation top.
1148 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001149 }
1150
1151 // Calculate new top and bail out if new space is exhausted. Use result
1152 // to calculate the new top.
Steve Block1e0659c2011-05-24 12:43:12 +01001153 add(scratch2, result, Operand(obj_size_reg), SetCC);
1154 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001155 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001157 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001158
Ben Murdochb0fe1622011-05-05 13:52:32 +01001159 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001160 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001161 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001162 }
1163}
1164
1165
1166void MacroAssembler::AllocateInNewSpace(Register object_size,
1167 Register result,
1168 Register scratch1,
1169 Register scratch2,
1170 Label* gc_required,
1171 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001172 if (!FLAG_inline_new) {
1173 if (FLAG_debug_code) {
1174 // Trash the registers to simulate an allocation failure.
1175 mov(result, Operand(0x7091));
1176 mov(scratch1, Operand(0x7191));
1177 mov(scratch2, Operand(0x7291));
1178 }
1179 jmp(gc_required);
1180 return;
1181 }
1182
Ben Murdochb0fe1622011-05-05 13:52:32 +01001183 // Assert that the register arguments are different and that none of
1184 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001185 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001186 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001187 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001188 ASSERT(!result.is(ip));
1189 ASSERT(!scratch1.is(ip));
1190 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001191
Ben Murdochb0fe1622011-05-05 13:52:32 +01001192 // Check relative positions of allocation top and limit addresses.
1193 // The values must be adjacent in memory to allow the use of LDM.
1194 // Also, assert that the registers are numbered such that the values
1195 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001196 ExternalReference new_space_allocation_top =
1197 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001198 ExternalReference new_space_allocation_limit =
1199 ExternalReference::new_space_allocation_limit_address();
1200 intptr_t top =
1201 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1202 intptr_t limit =
1203 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1204 ASSERT((limit - top) == kPointerSize);
1205 ASSERT(result.code() < ip.code());
1206
1207 // Set up allocation top address.
1208 Register topaddr = scratch1;
1209 mov(topaddr, Operand(new_space_allocation_top));
1210
1211 // This code stores a temporary value in ip. This is OK, as the code below
1212 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001214 // Load allocation top into result and allocation limit into ip.
1215 ldm(ia, topaddr, result.bit() | ip.bit());
1216 } else {
1217 if (FLAG_debug_code) {
1218 // Assert that result actually contains top on entry. ip is used
1219 // immediately below so this use of ip does not cause difference with
1220 // respect to register content between debug and release mode.
1221 ldr(ip, MemOperand(topaddr));
1222 cmp(result, ip);
1223 Check(eq, "Unexpected allocation top");
1224 }
1225 // Load allocation limit into ip. Result already contains allocation top.
1226 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001227 }
1228
1229 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001230 // to calculate the new top. Object size may be in words so a shift is
1231 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001232 if ((flags & SIZE_IN_WORDS) != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001233 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001234 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001235 add(scratch2, result, Operand(object_size), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001236 }
Steve Block1e0659c2011-05-24 12:43:12 +01001237 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001238 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 b(hi, gc_required);
1240
Steve Blockd0582a62009-12-15 09:54:21 +00001241 // Update allocation top. result temporarily holds the new top.
1242 if (FLAG_debug_code) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001243 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001244 Check(eq, "Unaligned allocation in new space");
1245 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001246 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001247
1248 // Tag object if requested.
1249 if ((flags & TAG_OBJECT) != 0) {
1250 add(result, result, Operand(kHeapObjectTag));
1251 }
1252}
1253
1254
1255void MacroAssembler::UndoAllocationInNewSpace(Register object,
1256 Register scratch) {
1257 ExternalReference new_space_allocation_top =
1258 ExternalReference::new_space_allocation_top_address();
1259
1260 // Make sure the object has no tag before resetting top.
1261 and_(object, object, Operand(~kHeapObjectTagMask));
1262#ifdef DEBUG
1263 // Check that the object un-allocated is below the current top.
1264 mov(scratch, Operand(new_space_allocation_top));
1265 ldr(scratch, MemOperand(scratch));
1266 cmp(object, scratch);
1267 Check(lt, "Undo allocation of non allocated memory");
1268#endif
1269 // Write the address of the object to un-allocate as the current top.
1270 mov(scratch, Operand(new_space_allocation_top));
1271 str(object, MemOperand(scratch));
1272}
1273
1274
Andrei Popescu31002712010-02-23 13:46:05 +00001275void MacroAssembler::AllocateTwoByteString(Register result,
1276 Register length,
1277 Register scratch1,
1278 Register scratch2,
1279 Register scratch3,
1280 Label* gc_required) {
1281 // Calculate the number of bytes needed for the characters in the string while
1282 // observing object alignment.
1283 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1284 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1285 add(scratch1, scratch1,
1286 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001287 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001288
1289 // Allocate two-byte string in new space.
1290 AllocateInNewSpace(scratch1,
1291 result,
1292 scratch2,
1293 scratch3,
1294 gc_required,
1295 TAG_OBJECT);
1296
1297 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001298 InitializeNewString(result,
1299 length,
1300 Heap::kStringMapRootIndex,
1301 scratch1,
1302 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001303}
1304
1305
1306void MacroAssembler::AllocateAsciiString(Register result,
1307 Register length,
1308 Register scratch1,
1309 Register scratch2,
1310 Register scratch3,
1311 Label* gc_required) {
1312 // Calculate the number of bytes needed for the characters in the string while
1313 // observing object alignment.
1314 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1315 ASSERT(kCharSize == 1);
1316 add(scratch1, length,
1317 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001318 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001319
1320 // Allocate ASCII string in new space.
1321 AllocateInNewSpace(scratch1,
1322 result,
1323 scratch2,
1324 scratch3,
1325 gc_required,
1326 TAG_OBJECT);
1327
1328 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001329 InitializeNewString(result,
1330 length,
1331 Heap::kAsciiStringMapRootIndex,
1332 scratch1,
1333 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001334}
1335
1336
1337void MacroAssembler::AllocateTwoByteConsString(Register result,
1338 Register length,
1339 Register scratch1,
1340 Register scratch2,
1341 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001342 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001343 result,
1344 scratch1,
1345 scratch2,
1346 gc_required,
1347 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001348
1349 InitializeNewString(result,
1350 length,
1351 Heap::kConsStringMapRootIndex,
1352 scratch1,
1353 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001354}
1355
1356
1357void MacroAssembler::AllocateAsciiConsString(Register result,
1358 Register length,
1359 Register scratch1,
1360 Register scratch2,
1361 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001362 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001363 result,
1364 scratch1,
1365 scratch2,
1366 gc_required,
1367 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001368
1369 InitializeNewString(result,
1370 length,
1371 Heap::kConsAsciiStringMapRootIndex,
1372 scratch1,
1373 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001374}
1375
1376
Steve Block6ded16b2010-05-10 14:33:55 +01001377void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 Register map,
1379 Register type_reg,
1380 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001381 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 CompareInstanceType(map, type_reg, type);
1383}
1384
1385
1386void MacroAssembler::CompareInstanceType(Register map,
1387 Register type_reg,
1388 InstanceType type) {
1389 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1390 cmp(type_reg, Operand(type));
1391}
1392
1393
Andrei Popescu31002712010-02-23 13:46:05 +00001394void MacroAssembler::CheckMap(Register obj,
1395 Register scratch,
1396 Handle<Map> map,
1397 Label* fail,
1398 bool is_heap_object) {
1399 if (!is_heap_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001400 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +00001401 }
1402 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1403 mov(ip, Operand(map));
1404 cmp(scratch, ip);
1405 b(ne, fail);
1406}
1407
1408
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001409void MacroAssembler::CheckMap(Register obj,
1410 Register scratch,
1411 Heap::RootListIndex index,
1412 Label* fail,
1413 bool is_heap_object) {
1414 if (!is_heap_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001415 JumpIfSmi(obj, fail);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001416 }
1417 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1418 LoadRoot(ip, index);
1419 cmp(scratch, ip);
1420 b(ne, fail);
1421}
1422
1423
Steve Blocka7e24c12009-10-30 11:49:00 +00001424void MacroAssembler::TryGetFunctionPrototype(Register function,
1425 Register result,
1426 Register scratch,
1427 Label* miss) {
1428 // Check that the receiver isn't a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01001429 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001430
1431 // Check that the function really is a function. Load map into result reg.
1432 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1433 b(ne, miss);
1434
1435 // Make sure that the function has an instance prototype.
1436 Label non_instance;
1437 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1438 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1439 b(ne, &non_instance);
1440
1441 // Get the prototype or initial map from the function.
1442 ldr(result,
1443 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1444
1445 // If the prototype or initial map is the hole, don't return it and
1446 // simply miss the cache instead. This will allow us to allocate a
1447 // prototype object on-demand in the runtime system.
1448 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1449 cmp(result, ip);
1450 b(eq, miss);
1451
1452 // If the function does not have an initial map, we're done.
1453 Label done;
1454 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1455 b(ne, &done);
1456
1457 // Get the prototype from the initial map.
1458 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1459 jmp(&done);
1460
1461 // Non-instance prototype: Fetch prototype from constructor field
1462 // in initial map.
1463 bind(&non_instance);
1464 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1465
1466 // All done.
1467 bind(&done);
1468}
1469
1470
1471void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
Steve Block1e0659c2011-05-24 12:43:12 +01001472 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001473 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1474}
1475
1476
Andrei Popescu31002712010-02-23 13:46:05 +00001477void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
Steve Block1e0659c2011-05-24 12:43:12 +01001478 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Andrei Popescu31002712010-02-23 13:46:05 +00001479 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1480}
1481
1482
Steve Block1e0659c2011-05-24 12:43:12 +01001483MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub, Condition cond) {
1484 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
1485 Object* result;
1486 { MaybeObject* maybe_result = stub->TryGetCode();
1487 if (!maybe_result->ToObject(&result)) return maybe_result;
1488 }
1489 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1490 return result;
1491}
1492
1493
1494static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
1495 return ref0.address() - ref1.address();
1496}
1497
1498
1499MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(
1500 ApiFunction* function, int stack_space) {
1501 ExternalReference next_address =
1502 ExternalReference::handle_scope_next_address();
1503 const int kNextOffset = 0;
1504 const int kLimitOffset = AddressOffset(
1505 ExternalReference::handle_scope_limit_address(),
1506 next_address);
1507 const int kLevelOffset = AddressOffset(
1508 ExternalReference::handle_scope_level_address(),
1509 next_address);
1510
1511 // Allocate HandleScope in callee-save registers.
1512 mov(r7, Operand(next_address));
1513 ldr(r4, MemOperand(r7, kNextOffset));
1514 ldr(r5, MemOperand(r7, kLimitOffset));
1515 ldr(r6, MemOperand(r7, kLevelOffset));
1516 add(r6, r6, Operand(1));
1517 str(r6, MemOperand(r7, kLevelOffset));
1518
1519 // Native call returns to the DirectCEntry stub which redirects to the
1520 // return address pushed on stack (could have moved after GC).
1521 // DirectCEntry stub itself is generated early and never moves.
1522 DirectCEntryStub stub;
1523 stub.GenerateCall(this, function);
1524
1525 Label promote_scheduled_exception;
1526 Label delete_allocated_handles;
1527 Label leave_exit_frame;
1528
1529 // If result is non-zero, dereference to get the result value
1530 // otherwise set it to undefined.
1531 cmp(r0, Operand(0));
1532 LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1533 ldr(r0, MemOperand(r0), ne);
1534
1535 // No more valid handles (the result handle was the last one). Restore
1536 // previous handle scope.
1537 str(r4, MemOperand(r7, kNextOffset));
1538 if (FLAG_debug_code) {
1539 ldr(r1, MemOperand(r7, kLevelOffset));
1540 cmp(r1, r6);
1541 Check(eq, "Unexpected level after return from api call");
1542 }
1543 sub(r6, r6, Operand(1));
1544 str(r6, MemOperand(r7, kLevelOffset));
1545 ldr(ip, MemOperand(r7, kLimitOffset));
1546 cmp(r5, ip);
1547 b(ne, &delete_allocated_handles);
1548
1549 // Check if the function scheduled an exception.
1550 bind(&leave_exit_frame);
1551 LoadRoot(r4, Heap::kTheHoleValueRootIndex);
1552 mov(ip, Operand(ExternalReference::scheduled_exception_address()));
1553 ldr(r5, MemOperand(ip));
1554 cmp(r4, r5);
1555 b(ne, &promote_scheduled_exception);
1556
1557 // LeaveExitFrame expects unwind space to be in r4.
1558 mov(r4, Operand(stack_space));
1559 LeaveExitFrame(false);
1560
1561 bind(&promote_scheduled_exception);
1562 MaybeObject* result = TryTailCallExternalReference(
1563 ExternalReference(Runtime::kPromoteScheduledException), 0, 1);
1564 if (result->IsFailure()) {
1565 return result;
1566 }
1567
1568 // HandleScope limit has changed. Delete allocated extensions.
1569 bind(&delete_allocated_handles);
1570 str(r5, MemOperand(r7, kLimitOffset));
1571 mov(r4, r0);
1572 PrepareCallCFunction(0, r5);
1573 CallCFunction(ExternalReference::delete_handle_scope_extensions(), 0);
1574 mov(r0, r4);
1575 jmp(&leave_exit_frame);
1576
1577 return result;
1578}
1579
1580
Steve Blocka7e24c12009-10-30 11:49:00 +00001581void MacroAssembler::IllegalOperation(int num_arguments) {
1582 if (num_arguments > 0) {
1583 add(sp, sp, Operand(num_arguments * kPointerSize));
1584 }
1585 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1586}
1587
1588
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001589void MacroAssembler::IndexFromHash(Register hash, Register index) {
1590 // If the hash field contains an array index pick it out. The assert checks
1591 // that the constants for the maximum number of digits for an array index
1592 // cached in the hash field and the number of bits reserved for it does not
1593 // conflict.
1594 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1595 (1 << String::kArrayIndexValueBits));
1596 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1597 // the low kHashShift bits.
1598 STATIC_ASSERT(kSmiTag == 0);
1599 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1600 mov(index, Operand(hash, LSL, kSmiTagSize));
1601}
1602
1603
Steve Blockd0582a62009-12-15 09:54:21 +00001604void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1605 Register outHighReg,
1606 Register outLowReg) {
1607 // ARMv7 VFP3 instructions to implement integer to double conversion.
1608 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001609 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001610 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001611 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001612}
1613
1614
Steve Block8defd9f2010-07-08 12:39:36 +01001615void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1616 DwVfpRegister result,
1617 Register scratch1,
1618 Register scratch2,
1619 Register heap_number_map,
1620 SwVfpRegister scratch3,
1621 Label* not_number,
1622 ObjectToDoubleFlags flags) {
1623 Label done;
1624 if ((flags & OBJECT_NOT_SMI) == 0) {
1625 Label not_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01001626 JumpIfNotSmi(object, &not_smi);
Steve Block8defd9f2010-07-08 12:39:36 +01001627 // Remove smi tag and convert to double.
1628 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1629 vmov(scratch3, scratch1);
1630 vcvt_f64_s32(result, scratch3);
1631 b(&done);
1632 bind(&not_smi);
1633 }
1634 // Check for heap number and load double value from it.
1635 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1636 sub(scratch2, object, Operand(kHeapObjectTag));
1637 cmp(scratch1, heap_number_map);
1638 b(ne, not_number);
1639 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1640 // If exponent is all ones the number is either a NaN or +/-Infinity.
1641 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1642 Sbfx(scratch1,
1643 scratch1,
1644 HeapNumber::kExponentShift,
1645 HeapNumber::kExponentBits);
1646 // All-one value sign extend to -1.
1647 cmp(scratch1, Operand(-1));
1648 b(eq, not_number);
1649 }
1650 vldr(result, scratch2, HeapNumber::kValueOffset);
1651 bind(&done);
1652}
1653
1654
1655void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1656 DwVfpRegister value,
1657 Register scratch1,
1658 SwVfpRegister scratch2) {
1659 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1660 vmov(scratch2, scratch1);
1661 vcvt_f64_s32(value, scratch2);
1662}
1663
1664
Iain Merrick9ac36c92010-09-13 15:29:50 +01001665// Tries to get a signed int32 out of a double precision floating point heap
1666// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1667// 32bits signed integer range.
1668void MacroAssembler::ConvertToInt32(Register source,
1669 Register dest,
1670 Register scratch,
1671 Register scratch2,
Steve Block1e0659c2011-05-24 12:43:12 +01001672 DwVfpRegister double_scratch,
Iain Merrick9ac36c92010-09-13 15:29:50 +01001673 Label *not_int32) {
1674 if (CpuFeatures::IsSupported(VFP3)) {
1675 CpuFeatures::Scope scope(VFP3);
1676 sub(scratch, source, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01001677 vldr(double_scratch, scratch, HeapNumber::kValueOffset);
1678 vcvt_s32_f64(double_scratch.low(), double_scratch);
1679 vmov(dest, double_scratch.low());
Iain Merrick9ac36c92010-09-13 15:29:50 +01001680 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1681 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1682 // range. When substracting one, the minimum signed integer becomes the
1683 // maximun signed integer.
1684 sub(scratch, dest, Operand(1));
1685 cmp(scratch, Operand(LONG_MAX - 1));
1686 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1687 b(ge, not_int32);
1688 } else {
1689 // This code is faster for doubles that are in the ranges -0x7fffffff to
1690 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1691 // the range of signed int32 values that are not Smis. Jumps to the label
1692 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1693 // 0x80000000.0 (excluding the endpoints).
1694 Label right_exponent, done;
1695 // Get exponent word.
1696 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1697 // Get exponent alone in scratch2.
1698 Ubfx(scratch2,
1699 scratch,
1700 HeapNumber::kExponentShift,
1701 HeapNumber::kExponentBits);
1702 // Load dest with zero. We use this either for the final shift or
1703 // for the answer.
1704 mov(dest, Operand(0, RelocInfo::NONE));
1705 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1706 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1707 // the exponent that we are fastest at and also the highest exponent we can
1708 // handle here.
1709 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1710 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1711 // split it up to avoid a constant pool entry. You can't do that in general
1712 // for cmp because of the overflow flag, but we know the exponent is in the
1713 // range 0-2047 so there is no overflow.
1714 int fudge_factor = 0x400;
1715 sub(scratch2, scratch2, Operand(fudge_factor));
1716 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1717 // If we have a match of the int32-but-not-Smi exponent then skip some
1718 // logic.
1719 b(eq, &right_exponent);
1720 // If the exponent is higher than that then go to slow case. This catches
1721 // numbers that don't fit in a signed int32, infinities and NaNs.
1722 b(gt, not_int32);
1723
1724 // We know the exponent is smaller than 30 (biased). If it is less than
1725 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1726 // it rounds to zero.
1727 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1728 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1729 // Dest already has a Smi zero.
1730 b(lt, &done);
1731
1732 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1733 // get how much to shift down.
1734 rsb(dest, scratch2, Operand(30));
1735
1736 bind(&right_exponent);
1737 // Get the top bits of the mantissa.
1738 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1739 // Put back the implicit 1.
1740 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1741 // Shift up the mantissa bits to take up the space the exponent used to
1742 // take. We just orred in the implicit bit so that took care of one and
1743 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1744 // distance.
1745 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1746 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1747 // Put sign in zero flag.
1748 tst(scratch, Operand(HeapNumber::kSignMask));
1749 // Get the second half of the double. For some exponents we don't
1750 // actually need this because the bits get shifted out again, but
1751 // it's probably slower to test than just to do it.
1752 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1753 // Shift down 22 bits to get the last 10 bits.
1754 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1755 // Move down according to the exponent.
1756 mov(dest, Operand(scratch, LSR, dest));
1757 // Fix sign if sign bit was set.
1758 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1759 bind(&done);
1760 }
1761}
1762
1763
Andrei Popescu31002712010-02-23 13:46:05 +00001764void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1765 Register src,
1766 int num_least_bits) {
1767 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001768 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001769 } else {
1770 mov(dst, Operand(src, ASR, kSmiTagSize));
1771 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1772 }
1773}
1774
1775
Steve Block1e0659c2011-05-24 12:43:12 +01001776void MacroAssembler::GetLeastBitsFromInt32(Register dst,
1777 Register src,
1778 int num_least_bits) {
1779 and_(dst, src, Operand((1 << num_least_bits) - 1));
1780}
1781
1782
Steve Blocka7e24c12009-10-30 11:49:00 +00001783void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1784 // All parameters are on the stack. r0 has the return value after call.
1785
1786 // If the expected number of arguments of the runtime function is
1787 // constant, we check that the actual number of arguments match the
1788 // expectation.
1789 if (f->nargs >= 0 && f->nargs != num_arguments) {
1790 IllegalOperation(num_arguments);
1791 return;
1792 }
1793
Leon Clarke4515c472010-02-03 11:58:03 +00001794 // TODO(1236192): Most runtime routines don't need the number of
1795 // arguments passed in because it is constant. At some point we
1796 // should remove this need and make the runtime routine entry code
1797 // smarter.
1798 mov(r0, Operand(num_arguments));
1799 mov(r1, Operand(ExternalReference(f)));
1800 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001801 CallStub(&stub);
1802}
1803
1804
1805void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1806 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1807}
1808
1809
Ben Murdochb0fe1622011-05-05 13:52:32 +01001810void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1811 Runtime::Function* function = Runtime::FunctionForId(id);
1812 mov(r0, Operand(function->nargs));
1813 mov(r1, Operand(ExternalReference(function)));
1814 CEntryStub stub(1);
1815 stub.SaveDoubles();
1816 CallStub(&stub);
1817}
1818
1819
Andrei Popescu402d9372010-02-26 13:31:12 +00001820void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1821 int num_arguments) {
1822 mov(r0, Operand(num_arguments));
1823 mov(r1, Operand(ext));
1824
1825 CEntryStub stub(1);
1826 CallStub(&stub);
1827}
1828
1829
Steve Block6ded16b2010-05-10 14:33:55 +01001830void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1831 int num_arguments,
1832 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001833 // TODO(1236192): Most runtime routines don't need the number of
1834 // arguments passed in because it is constant. At some point we
1835 // should remove this need and make the runtime routine entry code
1836 // smarter.
1837 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001838 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001839}
1840
1841
Steve Block1e0659c2011-05-24 12:43:12 +01001842MaybeObject* MacroAssembler::TryTailCallExternalReference(
1843 const ExternalReference& ext, int num_arguments, int result_size) {
1844 // TODO(1236192): Most runtime routines don't need the number of
1845 // arguments passed in because it is constant. At some point we
1846 // should remove this need and make the runtime routine entry code
1847 // smarter.
1848 mov(r0, Operand(num_arguments));
1849 return TryJumpToExternalReference(ext);
1850}
1851
1852
Steve Block6ded16b2010-05-10 14:33:55 +01001853void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1854 int num_arguments,
1855 int result_size) {
1856 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1857}
1858
1859
1860void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001861#if defined(__thumb__)
1862 // Thumb mode builtin.
1863 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1864#endif
1865 mov(r1, Operand(builtin));
1866 CEntryStub stub(1);
1867 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1868}
1869
1870
Steve Block1e0659c2011-05-24 12:43:12 +01001871MaybeObject* MacroAssembler::TryJumpToExternalReference(
1872 const ExternalReference& builtin) {
1873#if defined(__thumb__)
1874 // Thumb mode builtin.
1875 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1876#endif
1877 mov(r1, Operand(builtin));
1878 CEntryStub stub(1);
1879 return TryTailCallStub(&stub);
1880}
1881
1882
Steve Blocka7e24c12009-10-30 11:49:00 +00001883void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001884 InvokeJSFlags flags,
1885 PostCallGenerator* post_call_generator) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001886 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001887 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001888 Call(r2);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001889 if (post_call_generator != NULL) post_call_generator->Generate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001890 } else {
1891 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001892 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001893 }
1894}
1895
1896
Steve Block791712a2010-08-27 10:21:07 +01001897void MacroAssembler::GetBuiltinFunction(Register target,
1898 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001899 // Load the builtins object into target register.
1900 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1901 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001902 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001903 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01001904 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1905}
1906
1907
1908void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1909 ASSERT(!target.is(r1));
1910 GetBuiltinFunction(r1, id);
1911 // Load the code entry point from the builtins object.
1912 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001913}
1914
1915
1916void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1917 Register scratch1, Register scratch2) {
1918 if (FLAG_native_code_counters && counter->Enabled()) {
1919 mov(scratch1, Operand(value));
1920 mov(scratch2, Operand(ExternalReference(counter)));
1921 str(scratch1, MemOperand(scratch2));
1922 }
1923}
1924
1925
1926void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1927 Register scratch1, Register scratch2) {
1928 ASSERT(value > 0);
1929 if (FLAG_native_code_counters && counter->Enabled()) {
1930 mov(scratch2, Operand(ExternalReference(counter)));
1931 ldr(scratch1, MemOperand(scratch2));
1932 add(scratch1, scratch1, Operand(value));
1933 str(scratch1, MemOperand(scratch2));
1934 }
1935}
1936
1937
1938void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1939 Register scratch1, Register scratch2) {
1940 ASSERT(value > 0);
1941 if (FLAG_native_code_counters && counter->Enabled()) {
1942 mov(scratch2, Operand(ExternalReference(counter)));
1943 ldr(scratch1, MemOperand(scratch2));
1944 sub(scratch1, scratch1, Operand(value));
1945 str(scratch1, MemOperand(scratch2));
1946 }
1947}
1948
1949
Steve Block1e0659c2011-05-24 12:43:12 +01001950void MacroAssembler::Assert(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001951 if (FLAG_debug_code)
Steve Block1e0659c2011-05-24 12:43:12 +01001952 Check(cond, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00001953}
1954
1955
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001956void MacroAssembler::AssertRegisterIsRoot(Register reg,
1957 Heap::RootListIndex index) {
1958 if (FLAG_debug_code) {
1959 LoadRoot(ip, index);
1960 cmp(reg, ip);
1961 Check(eq, "Register did not match expected root");
1962 }
1963}
1964
1965
Iain Merrick75681382010-08-19 15:07:18 +01001966void MacroAssembler::AssertFastElements(Register elements) {
1967 if (FLAG_debug_code) {
1968 ASSERT(!elements.is(ip));
1969 Label ok;
1970 push(elements);
1971 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
1972 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
1973 cmp(elements, ip);
1974 b(eq, &ok);
1975 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
1976 cmp(elements, ip);
1977 b(eq, &ok);
1978 Abort("JSObject with fast elements map has slow elements");
1979 bind(&ok);
1980 pop(elements);
1981 }
1982}
1983
1984
Steve Block1e0659c2011-05-24 12:43:12 +01001985void MacroAssembler::Check(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001986 Label L;
Steve Block1e0659c2011-05-24 12:43:12 +01001987 b(cond, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00001988 Abort(msg);
1989 // will not return here
1990 bind(&L);
1991}
1992
1993
1994void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01001995 Label abort_start;
1996 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00001997 // We want to pass the msg string like a smi to avoid GC
1998 // problems, however msg is not guaranteed to be aligned
1999 // properly. Instead, we pass an aligned pointer that is
2000 // a proper v8 smi, but also pass the alignment difference
2001 // from the real pointer as a smi.
2002 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2003 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2004 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2005#ifdef DEBUG
2006 if (msg != NULL) {
2007 RecordComment("Abort message: ");
2008 RecordComment(msg);
2009 }
2010#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002011 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01002012 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00002013
Steve Blocka7e24c12009-10-30 11:49:00 +00002014 mov(r0, Operand(p0));
2015 push(r0);
2016 mov(r0, Operand(Smi::FromInt(p1 - p0)));
2017 push(r0);
2018 CallRuntime(Runtime::kAbort, 2);
2019 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01002020 if (is_const_pool_blocked()) {
2021 // If the calling code cares about the exact number of
2022 // instructions generated, we insert padding here to keep the size
2023 // of the Abort macro constant.
2024 static const int kExpectedAbortInstructions = 10;
2025 int abort_instructions = InstructionsGeneratedSince(&abort_start);
2026 ASSERT(abort_instructions <= kExpectedAbortInstructions);
2027 while (abort_instructions++ < kExpectedAbortInstructions) {
2028 nop();
2029 }
2030 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002031}
2032
2033
Steve Blockd0582a62009-12-15 09:54:21 +00002034void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2035 if (context_chain_length > 0) {
2036 // Move up the chain of contexts to the context containing the slot.
2037 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
2038 // Load the function context (which is the incoming, outer context).
2039 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
2040 for (int i = 1; i < context_chain_length; i++) {
2041 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
2042 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
2043 }
2044 // The context may be an intermediate context, not a function context.
2045 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2046 } else { // Slot is in the current function context.
2047 // The context may be an intermediate context, not a function context.
2048 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2049 }
2050}
2051
2052
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002053void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2054 // Load the global or builtins object from the current context.
2055 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2056 // Load the global context from the global or builtins object.
2057 ldr(function, FieldMemOperand(function,
2058 GlobalObject::kGlobalContextOffset));
2059 // Load the function from the global context.
2060 ldr(function, MemOperand(function, Context::SlotOffset(index)));
2061}
2062
2063
2064void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2065 Register map,
2066 Register scratch) {
2067 // Load the initial map. The global functions all have initial maps.
2068 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2069 if (FLAG_debug_code) {
2070 Label ok, fail;
2071 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, false);
2072 b(&ok);
2073 bind(&fail);
2074 Abort("Global functions must have initial map");
2075 bind(&ok);
2076 }
2077}
2078
2079
Steve Block1e0659c2011-05-24 12:43:12 +01002080void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
2081 Register reg,
2082 Register scratch,
2083 Label* not_power_of_two_or_zero) {
2084 sub(scratch, reg, Operand(1), SetCC);
2085 b(mi, not_power_of_two_or_zero);
2086 tst(scratch, reg);
2087 b(ne, not_power_of_two_or_zero);
2088}
2089
2090
Andrei Popescu31002712010-02-23 13:46:05 +00002091void MacroAssembler::JumpIfNotBothSmi(Register reg1,
2092 Register reg2,
2093 Label* on_not_both_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002094 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002095 tst(reg1, Operand(kSmiTagMask));
2096 tst(reg2, Operand(kSmiTagMask), eq);
2097 b(ne, on_not_both_smi);
2098}
2099
2100
2101void MacroAssembler::JumpIfEitherSmi(Register reg1,
2102 Register reg2,
2103 Label* on_either_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002104 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002105 tst(reg1, Operand(kSmiTagMask));
2106 tst(reg2, Operand(kSmiTagMask), ne);
2107 b(eq, on_either_smi);
2108}
2109
2110
Iain Merrick75681382010-08-19 15:07:18 +01002111void MacroAssembler::AbortIfSmi(Register object) {
Steve Block1e0659c2011-05-24 12:43:12 +01002112 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +01002113 tst(object, Operand(kSmiTagMask));
2114 Assert(ne, "Operand is a smi");
2115}
2116
2117
Steve Block1e0659c2011-05-24 12:43:12 +01002118void MacroAssembler::AbortIfNotSmi(Register object) {
2119 STATIC_ASSERT(kSmiTag == 0);
2120 tst(object, Operand(kSmiTagMask));
2121 Assert(eq, "Operand is not smi");
2122}
2123
2124
2125void MacroAssembler::AbortIfNotRootValue(Register src,
2126 Heap::RootListIndex root_value_index,
2127 const char* message) {
2128 ASSERT(!src.is(ip));
2129 LoadRoot(ip, root_value_index);
2130 cmp(src, ip);
2131 Assert(eq, message);
2132}
2133
2134
2135void MacroAssembler::JumpIfNotHeapNumber(Register object,
2136 Register heap_number_map,
2137 Register scratch,
2138 Label* on_not_heap_number) {
2139 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2140 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2141 cmp(scratch, heap_number_map);
2142 b(ne, on_not_heap_number);
2143}
2144
2145
Leon Clarked91b9f72010-01-27 17:25:45 +00002146void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
2147 Register first,
2148 Register second,
2149 Register scratch1,
2150 Register scratch2,
2151 Label* failure) {
2152 // Test that both first and second are sequential ASCII strings.
2153 // Assume that they are non-smis.
2154 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
2155 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
2156 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
2157 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002158
2159 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
2160 scratch2,
2161 scratch1,
2162 scratch2,
2163 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002164}
2165
2166void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
2167 Register second,
2168 Register scratch1,
2169 Register scratch2,
2170 Label* failure) {
2171 // Check that neither is a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002172 STATIC_ASSERT(kSmiTag == 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002173 and_(scratch1, first, Operand(second));
2174 tst(scratch1, Operand(kSmiTagMask));
2175 b(eq, failure);
2176 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
2177 second,
2178 scratch1,
2179 scratch2,
2180 failure);
2181}
2182
Steve Blockd0582a62009-12-15 09:54:21 +00002183
Steve Block6ded16b2010-05-10 14:33:55 +01002184// Allocates a heap number or jumps to the need_gc label if the young space
2185// is full and a scavenge is needed.
2186void MacroAssembler::AllocateHeapNumber(Register result,
2187 Register scratch1,
2188 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002189 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01002190 Label* gc_required) {
2191 // Allocate an object in the heap for the heap number and tag it as a heap
2192 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01002193 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01002194 result,
2195 scratch1,
2196 scratch2,
2197 gc_required,
2198 TAG_OBJECT);
2199
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002200 // Store heap number map in the allocated object.
2201 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2202 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002203}
2204
2205
Steve Block8defd9f2010-07-08 12:39:36 +01002206void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2207 DwVfpRegister value,
2208 Register scratch1,
2209 Register scratch2,
2210 Register heap_number_map,
2211 Label* gc_required) {
2212 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
2213 sub(scratch1, result, Operand(kHeapObjectTag));
2214 vstr(value, scratch1, HeapNumber::kValueOffset);
2215}
2216
2217
Ben Murdochbb769b22010-08-11 14:56:33 +01002218// Copies a fixed number of fields of heap objects from src to dst.
2219void MacroAssembler::CopyFields(Register dst,
2220 Register src,
2221 RegList temps,
2222 int field_count) {
2223 // At least one bit set in the first 15 registers.
2224 ASSERT((temps & ((1 << 15) - 1)) != 0);
2225 ASSERT((temps & dst.bit()) == 0);
2226 ASSERT((temps & src.bit()) == 0);
2227 // Primitive implementation using only one temporary register.
2228
2229 Register tmp = no_reg;
2230 // Find a temp register in temps list.
2231 for (int i = 0; i < 15; i++) {
2232 if ((temps & (1 << i)) != 0) {
2233 tmp.set_code(i);
2234 break;
2235 }
2236 }
2237 ASSERT(!tmp.is(no_reg));
2238
2239 for (int i = 0; i < field_count; i++) {
2240 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
2241 str(tmp, FieldMemOperand(dst, i * kPointerSize));
2242 }
2243}
2244
2245
Steve Block8defd9f2010-07-08 12:39:36 +01002246void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
2247 Register source, // Input.
2248 Register scratch) {
Steve Block1e0659c2011-05-24 12:43:12 +01002249 ASSERT(!zeros.is(source) || !source.is(scratch));
Steve Block8defd9f2010-07-08 12:39:36 +01002250 ASSERT(!zeros.is(scratch));
2251 ASSERT(!scratch.is(ip));
2252 ASSERT(!source.is(ip));
2253 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002254#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2255 clz(zeros, source); // This instruction is only supported after ARM5.
2256#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01002257 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01002258 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002259 // Top 16.
2260 tst(scratch, Operand(0xffff0000));
2261 add(zeros, zeros, Operand(16), LeaveCC, eq);
2262 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2263 // Top 8.
2264 tst(scratch, Operand(0xff000000));
2265 add(zeros, zeros, Operand(8), LeaveCC, eq);
2266 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2267 // Top 4.
2268 tst(scratch, Operand(0xf0000000));
2269 add(zeros, zeros, Operand(4), LeaveCC, eq);
2270 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2271 // Top 2.
2272 tst(scratch, Operand(0xc0000000));
2273 add(zeros, zeros, Operand(2), LeaveCC, eq);
2274 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2275 // Top bit.
2276 tst(scratch, Operand(0x80000000u));
2277 add(zeros, zeros, Operand(1), LeaveCC, eq);
2278#endif
2279}
2280
2281
2282void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2283 Register first,
2284 Register second,
2285 Register scratch1,
2286 Register scratch2,
2287 Label* failure) {
2288 int kFlatAsciiStringMask =
2289 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2290 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2291 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2292 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2293 cmp(scratch1, Operand(kFlatAsciiStringTag));
2294 // Ignore second test if first test failed.
2295 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2296 b(ne, failure);
2297}
2298
2299
2300void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
2301 Register scratch,
2302 Label* failure) {
2303 int kFlatAsciiStringMask =
2304 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2305 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2306 and_(scratch, type, Operand(kFlatAsciiStringMask));
2307 cmp(scratch, Operand(kFlatAsciiStringTag));
2308 b(ne, failure);
2309}
2310
2311
2312void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2313 int frame_alignment = ActivationFrameAlignment();
2314 // Up to four simple arguments are passed in registers r0..r3.
2315 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2316 if (frame_alignment > kPointerSize) {
2317 // Make stack end at alignment and make room for num_arguments - 4 words
2318 // and the original value of sp.
2319 mov(scratch, sp);
2320 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
2321 ASSERT(IsPowerOf2(frame_alignment));
2322 and_(sp, sp, Operand(-frame_alignment));
2323 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2324 } else {
2325 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
2326 }
2327}
2328
2329
2330void MacroAssembler::CallCFunction(ExternalReference function,
2331 int num_arguments) {
2332 mov(ip, Operand(function));
2333 CallCFunction(ip, num_arguments);
2334}
2335
2336
2337void MacroAssembler::CallCFunction(Register function, int num_arguments) {
2338 // Make sure that the stack is aligned before calling a C function unless
2339 // running in the simulator. The simulator has its own alignment check which
2340 // provides more information.
2341#if defined(V8_HOST_ARCH_ARM)
2342 if (FLAG_debug_code) {
2343 int frame_alignment = OS::ActivationFrameAlignment();
2344 int frame_alignment_mask = frame_alignment - 1;
2345 if (frame_alignment > kPointerSize) {
2346 ASSERT(IsPowerOf2(frame_alignment));
2347 Label alignment_as_expected;
2348 tst(sp, Operand(frame_alignment_mask));
2349 b(eq, &alignment_as_expected);
2350 // Don't use Check here, as it will call Runtime_Abort possibly
2351 // re-entering here.
2352 stop("Unexpected alignment");
2353 bind(&alignment_as_expected);
2354 }
2355 }
2356#endif
2357
2358 // Just call directly. The function called cannot cause a GC, or
2359 // allow preemption, so the return address in the link register
2360 // stays correct.
2361 Call(function);
2362 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2363 if (OS::ActivationFrameAlignment() > kPointerSize) {
2364 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
2365 } else {
2366 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
2367 }
2368}
2369
2370
Steve Block1e0659c2011-05-24 12:43:12 +01002371void MacroAssembler::GetRelocatedValueLocation(Register ldr_location,
2372 Register result) {
2373 const uint32_t kLdrOffsetMask = (1 << 12) - 1;
2374 const int32_t kPCRegOffset = 2 * kPointerSize;
2375 ldr(result, MemOperand(ldr_location));
2376 if (FLAG_debug_code) {
2377 // Check that the instruction is a ldr reg, [pc + offset] .
2378 and_(result, result, Operand(kLdrPCPattern));
2379 cmp(result, Operand(kLdrPCPattern));
2380 Check(eq, "The instruction to patch should be a load from pc.");
2381 // Result was clobbered. Restore it.
2382 ldr(result, MemOperand(ldr_location));
2383 }
2384 // Get the address of the constant.
2385 and_(result, result, Operand(kLdrOffsetMask));
2386 add(result, ldr_location, Operand(result));
2387 add(result, result, Operand(kPCRegOffset));
2388}
2389
2390
Steve Blocka7e24c12009-10-30 11:49:00 +00002391CodePatcher::CodePatcher(byte* address, int instructions)
2392 : address_(address),
2393 instructions_(instructions),
2394 size_(instructions * Assembler::kInstrSize),
2395 masm_(address, size_ + Assembler::kGap) {
2396 // Create a new macro assembler pointing to the address of the code to patch.
2397 // The size is adjusted with kGap on order for the assembler to generate size
2398 // bytes of instructions without failing with buffer size constraints.
2399 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2400}
2401
2402
2403CodePatcher::~CodePatcher() {
2404 // Indicate that code has changed.
2405 CPU::FlushICache(address_, size_);
2406
2407 // Check that the code was patched as expected.
2408 ASSERT(masm_.pc_ == address_ + size_);
2409 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2410}
2411
2412
Steve Block1e0659c2011-05-24 12:43:12 +01002413void CodePatcher::Emit(Instr instr) {
2414 masm()->emit(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002415}
2416
2417
2418void CodePatcher::Emit(Address addr) {
2419 masm()->emit(reinterpret_cast<Instr>(addr));
2420}
Steve Block1e0659c2011-05-24 12:43:12 +01002421
2422
2423void CodePatcher::EmitCondition(Condition cond) {
2424 Instr instr = Assembler::instr_at(masm_.pc_);
2425 instr = (instr & ~kCondMask) | cond;
2426 masm_.emit(instr);
2427}
Steve Blocka7e24c12009-10-30 11:49:00 +00002428
2429
2430} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002431
2432#endif // V8_TARGET_ARCH_ARM