blob: 4a131465902fb76eaa86248c1feadbd5c37ecfe1 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Iain Merrick9ac36c92010-09-13 15:29:50 +010028#include <limits.h> // For LONG_MIN, LONG_MAX.
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#include "v8.h"
31
Leon Clarkef7060e22010-06-03 12:02:55 +010032#if defined(V8_TARGET_ARCH_ARM)
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "bootstrapper.h"
35#include "codegen-inl.h"
36#include "debug.h"
37#include "runtime.h"
38
39namespace v8 {
40namespace internal {
41
42MacroAssembler::MacroAssembler(void* buffer, int size)
43 : Assembler(buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000044 generating_stub_(false),
45 allow_stub_calls_(true),
46 code_object_(Heap::undefined_value()) {
47}
48
49
50// We always generate arm code, never thumb code, even if V8 is compiled to
51// thumb, so we require inter-working support
52#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
53#error "flag -mthumb-interwork missing"
54#endif
55
56
57// We do not support thumb inter-working with an arm architecture not supporting
58// the blx instruction (below v5t). If you know what CPU you are compiling for
59// you can use -march=armv7 or similar.
60#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
61# error "For thumb inter-working we require an architecture which supports blx"
62#endif
63
64
Steve Blocka7e24c12009-10-30 11:49:00 +000065// Using bx does not yield better code, so use it only when required
66#if defined(USE_THUMB_INTERWORK)
67#define USE_BX 1
68#endif
69
70
71void MacroAssembler::Jump(Register target, Condition cond) {
72#if USE_BX
73 bx(target, cond);
74#else
75 mov(pc, Operand(target), LeaveCC, cond);
76#endif
77}
78
79
80void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
81 Condition cond) {
82#if USE_BX
83 mov(ip, Operand(target, rmode), LeaveCC, cond);
84 bx(ip, cond);
85#else
86 mov(pc, Operand(target, rmode), LeaveCC, cond);
87#endif
88}
89
90
91void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
92 Condition cond) {
93 ASSERT(!RelocInfo::IsCodeTarget(rmode));
94 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
95}
96
97
98void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
99 Condition cond) {
100 ASSERT(RelocInfo::IsCodeTarget(rmode));
101 // 'code' is always generated ARM code, never THUMB code
102 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
103}
104
105
106void MacroAssembler::Call(Register target, Condition cond) {
107#if USE_BLX
108 blx(target, cond);
109#else
110 // set lr for return at current pc + 8
111 mov(lr, Operand(pc), LeaveCC, cond);
112 mov(pc, Operand(target), LeaveCC, cond);
113#endif
114}
115
116
117void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
118 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100119#if USE_BLX
120 // On ARMv5 and after the recommended call sequence is:
121 // ldr ip, [pc, #...]
122 // blx ip
123
124 // The two instructions (ldr and blx) could be separated by a constant
125 // pool and the code would still work. The issue comes from the
126 // patching code which expect the ldr to be just above the blx.
127 { BlockConstPoolScope block_const_pool(this);
128 // Statement positions are expected to be recorded when the target
129 // address is loaded. The mov method will automatically record
130 // positions when pc is the target, since this is not the case here
131 // we have to do it explicitly.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800132 positions_recorder()->WriteRecordedPositions();
Steve Block6ded16b2010-05-10 14:33:55 +0100133
134 mov(ip, Operand(target, rmode), LeaveCC, cond);
135 blx(ip, cond);
136 }
137
138 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
139#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 // Set lr for return at current pc + 8.
141 mov(lr, Operand(pc), LeaveCC, cond);
142 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
143 mov(pc, Operand(target, rmode), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100146#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000147}
148
149
150void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
151 Condition cond) {
152 ASSERT(!RelocInfo::IsCodeTarget(rmode));
153 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
154}
155
156
157void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
158 Condition cond) {
159 ASSERT(RelocInfo::IsCodeTarget(rmode));
160 // 'code' is always generated ARM code, never THUMB code
161 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
162}
163
164
165void MacroAssembler::Ret(Condition cond) {
166#if USE_BX
167 bx(lr, cond);
168#else
169 mov(pc, Operand(lr), LeaveCC, cond);
170#endif
171}
172
173
Leon Clarkee46be812010-01-19 14:06:41 +0000174void MacroAssembler::Drop(int count, Condition cond) {
175 if (count > 0) {
176 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
177 }
178}
179
180
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181void MacroAssembler::Ret(int drop, Condition cond) {
182 Drop(drop, cond);
183 Ret(cond);
184}
185
186
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100187void MacroAssembler::Swap(Register reg1,
188 Register reg2,
189 Register scratch,
190 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100191 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100192 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
193 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
194 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100195 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196 mov(scratch, reg1, LeaveCC, cond);
197 mov(reg1, reg2, LeaveCC, cond);
198 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100199 }
200}
201
202
Leon Clarkee46be812010-01-19 14:06:41 +0000203void MacroAssembler::Call(Label* target) {
204 bl(target);
205}
206
207
208void MacroAssembler::Move(Register dst, Handle<Object> value) {
209 mov(dst, Operand(value));
210}
Steve Blockd0582a62009-12-15 09:54:21 +0000211
212
Steve Block6ded16b2010-05-10 14:33:55 +0100213void MacroAssembler::Move(Register dst, Register src) {
214 if (!dst.is(src)) {
215 mov(dst, src);
216 }
217}
218
219
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100220void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
221 Condition cond) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800222 if (!src2.is_reg() &&
223 !src2.must_use_constant_pool() &&
224 src2.immediate() == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100225 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800226
227 } else if (!src2.is_single_instruction() &&
228 !src2.must_use_constant_pool() &&
229 CpuFeatures::IsSupported(ARMv7) &&
230 IsPowerOf2(src2.immediate() + 1)) {
231 ubfx(dst, src1, 0, WhichPowerOf2(src2.immediate() + 1), cond);
232
233 } else {
234 and_(dst, src1, src2, LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100235 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100236}
237
238
239void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
240 Condition cond) {
241 ASSERT(lsb < 32);
242 if (!CpuFeatures::IsSupported(ARMv7)) {
243 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
244 and_(dst, src1, Operand(mask), LeaveCC, cond);
245 if (lsb != 0) {
246 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
247 }
248 } else {
249 ubfx(dst, src1, lsb, width, cond);
250 }
251}
252
253
254void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
255 Condition cond) {
256 ASSERT(lsb < 32);
257 if (!CpuFeatures::IsSupported(ARMv7)) {
258 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
259 and_(dst, src1, Operand(mask), LeaveCC, cond);
260 int shift_up = 32 - lsb - width;
261 int shift_down = lsb + shift_up;
262 if (shift_up != 0) {
263 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
264 }
265 if (shift_down != 0) {
266 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
267 }
268 } else {
269 sbfx(dst, src1, lsb, width, cond);
270 }
271}
272
273
274void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
275 ASSERT(lsb < 32);
276 if (!CpuFeatures::IsSupported(ARMv7)) {
277 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
278 bic(dst, dst, Operand(mask));
279 } else {
280 bfc(dst, lsb, width, cond);
281 }
282}
283
284
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100285void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
286 Condition cond) {
287 if (!CpuFeatures::IsSupported(ARMv7)) {
288 ASSERT(!dst.is(pc) && !src.rm().is(pc));
289 ASSERT((satpos >= 0) && (satpos <= 31));
290
291 // These asserts are required to ensure compatibility with the ARMv7
292 // implementation.
293 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
294 ASSERT(src.rs().is(no_reg));
295
296 Label done;
297 int satval = (1 << satpos) - 1;
298
299 if (cond != al) {
300 b(NegateCondition(cond), &done); // Skip saturate if !condition.
301 }
302 if (!(src.is_reg() && dst.is(src.rm()))) {
303 mov(dst, src);
304 }
305 tst(dst, Operand(~satval));
306 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100307 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100308 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
309 bind(&done);
310 } else {
311 usat(dst, satpos, src, cond);
312 }
313}
314
315
Steve Blocka7e24c12009-10-30 11:49:00 +0000316void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
317 // Empty the const pool.
318 CheckConstPool(true, true);
319 add(pc, pc, Operand(index,
320 LSL,
321 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
322 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
323 nop(); // Jump table alignment.
324 for (int i = 0; i < targets.length(); i++) {
325 b(targets[i]);
326 }
327}
328
329
330void MacroAssembler::LoadRoot(Register destination,
331 Heap::RootListIndex index,
332 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000333 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000334}
335
336
Kristian Monsen25f61362010-05-21 11:50:48 +0100337void MacroAssembler::StoreRoot(Register source,
338 Heap::RootListIndex index,
339 Condition cond) {
340 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
341}
342
343
Steve Block6ded16b2010-05-10 14:33:55 +0100344void MacroAssembler::RecordWriteHelper(Register object,
Steve Block8defd9f2010-07-08 12:39:36 +0100345 Register address,
346 Register scratch) {
Steve Block6ded16b2010-05-10 14:33:55 +0100347 if (FLAG_debug_code) {
348 // Check that the object is not in new space.
349 Label not_in_new_space;
Steve Block8defd9f2010-07-08 12:39:36 +0100350 InNewSpace(object, scratch, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100351 Abort("new-space object passed to RecordWriteHelper");
352 bind(&not_in_new_space);
353 }
Leon Clarke4515c472010-02-03 11:58:03 +0000354
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100355 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100356 Bfc(object, 0, kPageSizeBits);
357
358 // Calculate region number.
Steve Block8defd9f2010-07-08 12:39:36 +0100359 Ubfx(address, address, Page::kRegionSizeLog2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100360 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100362 // Mark region dirty.
Steve Block8defd9f2010-07-08 12:39:36 +0100363 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 mov(ip, Operand(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100365 orr(scratch, scratch, Operand(ip, LSL, address));
366 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100367}
368
369
370void MacroAssembler::InNewSpace(Register object,
371 Register scratch,
372 Condition cc,
373 Label* branch) {
374 ASSERT(cc == eq || cc == ne);
375 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
376 cmp(scratch, Operand(ExternalReference::new_space_start()));
377 b(cc, branch);
378}
379
380
381// Will clobber 4 registers: object, offset, scratch, ip. The
382// register 'object' contains a heap object pointer. The heap object
383// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100384void MacroAssembler::RecordWrite(Register object,
385 Operand offset,
386 Register scratch0,
387 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100388 // The compiled code assumes that record write doesn't change the
389 // context register, so we check that none of the clobbered
390 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100391 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100392
393 Label done;
394
395 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100396 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100397 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100398
Steve Block8defd9f2010-07-08 12:39:36 +0100399 // Add offset into the object.
400 add(scratch0, object, offset);
401
Steve Block6ded16b2010-05-10 14:33:55 +0100402 // Record the actual write.
Steve Block8defd9f2010-07-08 12:39:36 +0100403 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
405 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000406
407 // Clobber all input registers when running with the debug-code flag
408 // turned on to provoke errors.
409 if (FLAG_debug_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100410 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100411 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
412 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000413 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000414}
415
416
Steve Block8defd9f2010-07-08 12:39:36 +0100417// Will clobber 4 registers: object, address, scratch, ip. The
418// register 'object' contains a heap object pointer. The heap object
419// tag is shifted away.
420void MacroAssembler::RecordWrite(Register object,
421 Register address,
422 Register scratch) {
423 // The compiled code assumes that record write doesn't change the
424 // context register, so we check that none of the clobbered
425 // registers are cp.
426 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
427
428 Label done;
429
430 // First, test that the object is not in the new space. We cannot set
431 // region marks for new space pages.
432 InNewSpace(object, scratch, eq, &done);
433
434 // Record the actual write.
435 RecordWriteHelper(object, address, scratch);
436
437 bind(&done);
438
439 // Clobber all input registers when running with the debug-code flag
440 // turned on to provoke errors.
441 if (FLAG_debug_code) {
442 mov(object, Operand(BitCast<int32_t>(kZapValue)));
443 mov(address, Operand(BitCast<int32_t>(kZapValue)));
444 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
445 }
446}
447
448
Ben Murdochb0fe1622011-05-05 13:52:32 +0100449// Push and pop all registers that can hold pointers.
450void MacroAssembler::PushSafepointRegisters() {
451 // Safepoints expect a block of contiguous register values starting with r0:
452 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
453 // Safepoints expect a block of kNumSafepointRegisters values on the
454 // stack, so adjust the stack for unsaved registers.
455 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
456 ASSERT(num_unsaved >= 0);
457 sub(sp, sp, Operand(num_unsaved * kPointerSize));
458 stm(db_w, sp, kSafepointSavedRegisters);
459}
460
461
462void MacroAssembler::PopSafepointRegisters() {
463 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
464 ldm(ia_w, sp, kSafepointSavedRegisters);
465 add(sp, sp, Operand(num_unsaved * kPointerSize));
466}
467
468
469int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
470 // The registers are pushed starting with the highest encoding,
471 // which means that lowest encodings are closest to the stack pointer.
472 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
473 return reg_code;
474}
475
476
Leon Clarkef7060e22010-06-03 12:02:55 +0100477void MacroAssembler::Ldrd(Register dst1, Register dst2,
478 const MemOperand& src, Condition cond) {
479 ASSERT(src.rm().is(no_reg));
480 ASSERT(!dst1.is(lr)); // r14.
481 ASSERT_EQ(0, dst1.code() % 2);
482 ASSERT_EQ(dst1.code() + 1, dst2.code());
483
484 // Generate two ldr instructions if ldrd is not available.
485 if (CpuFeatures::IsSupported(ARMv7)) {
486 CpuFeatures::Scope scope(ARMv7);
487 ldrd(dst1, dst2, src, cond);
488 } else {
489 MemOperand src2(src);
490 src2.set_offset(src2.offset() + 4);
491 if (dst1.is(src.rn())) {
492 ldr(dst2, src2, cond);
493 ldr(dst1, src, cond);
494 } else {
495 ldr(dst1, src, cond);
496 ldr(dst2, src2, cond);
497 }
498 }
499}
500
501
502void MacroAssembler::Strd(Register src1, Register src2,
503 const MemOperand& dst, Condition cond) {
504 ASSERT(dst.rm().is(no_reg));
505 ASSERT(!src1.is(lr)); // r14.
506 ASSERT_EQ(0, src1.code() % 2);
507 ASSERT_EQ(src1.code() + 1, src2.code());
508
509 // Generate two str instructions if strd is not available.
510 if (CpuFeatures::IsSupported(ARMv7)) {
511 CpuFeatures::Scope scope(ARMv7);
512 strd(src1, src2, dst, cond);
513 } else {
514 MemOperand dst2(dst);
515 dst2.set_offset(dst2.offset() + 4);
516 str(src1, dst, cond);
517 str(src2, dst2, cond);
518 }
519}
520
521
Steve Blocka7e24c12009-10-30 11:49:00 +0000522void MacroAssembler::EnterFrame(StackFrame::Type type) {
523 // r0-r3: preserved
524 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
525 mov(ip, Operand(Smi::FromInt(type)));
526 push(ip);
527 mov(ip, Operand(CodeObject()));
528 push(ip);
529 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
530}
531
532
533void MacroAssembler::LeaveFrame(StackFrame::Type type) {
534 // r0: preserved
535 // r1: preserved
536 // r2: preserved
537
538 // Drop the execution stack down to the frame pointer and restore
539 // the caller frame pointer and return address.
540 mov(sp, fp);
541 ldm(ia_w, sp, fp.bit() | lr.bit());
542}
543
544
Ben Murdochb0fe1622011-05-05 13:52:32 +0100545void MacroAssembler::EnterExitFrame(bool save_doubles) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 // r0 is argc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000547 // Compute callee's stack pointer before making changes and save it as
548 // ip register so that it is restored as sp register on exit, thereby
549 // popping the args.
550
551 // ip = sp + kPointerSize * #args;
552 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
553
Ben Murdochb0fe1622011-05-05 13:52:32 +0100554 // Compute the argv pointer and keep it in a callee-saved register.
555 sub(r6, ip, Operand(kPointerSize));
556
Steve Block6ded16b2010-05-10 14:33:55 +0100557 // Prepare the stack to be aligned when calling into C. After this point there
558 // are 5 pushes before the call into C, so the stack needs to be aligned after
559 // 5 pushes.
560 int frame_alignment = ActivationFrameAlignment();
561 int frame_alignment_mask = frame_alignment - 1;
562 if (frame_alignment != kPointerSize) {
563 // The following code needs to be more general if this assert does not hold.
564 ASSERT(frame_alignment == 2 * kPointerSize);
565 // With 5 pushes left the frame must be unaligned at this point.
566 mov(r7, Operand(Smi::FromInt(0)));
567 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
568 push(r7, eq); // Push if aligned to make it unaligned.
569 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000570
571 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
572 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
Andrei Popescu402d9372010-02-26 13:31:12 +0000573 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
Andrei Popescu402d9372010-02-26 13:31:12 +0000575 mov(ip, Operand(CodeObject()));
576 push(ip); // Accessed from ExitFrame::code_slot.
Steve Blocka7e24c12009-10-30 11:49:00 +0000577
578 // Save the frame pointer and the context in top.
579 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
580 str(fp, MemOperand(ip));
581 mov(ip, Operand(ExternalReference(Top::k_context_address)));
582 str(cp, MemOperand(ip));
583
584 // Setup argc and the builtin function in callee-saved registers.
585 mov(r4, Operand(r0));
586 mov(r5, Operand(r1));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100587
588 // Optionally save all double registers.
589 if (save_doubles) {
590 // TODO(regis): Use vstrm instruction.
591 // The stack alignment code above made sp unaligned, so add space for one
592 // more double register and use aligned addresses.
593 ASSERT(kDoubleSize == frame_alignment);
594 // Mark the frame as containing doubles by pushing a non-valid return
595 // address, i.e. 0.
596 ASSERT(ExitFrameConstants::kMarkerOffset == -2 * kPointerSize);
597 mov(ip, Operand(0)); // Marker and alignment word.
598 push(ip);
599 int space = DwVfpRegister::kNumRegisters * kDoubleSize + kPointerSize;
600 sub(sp, sp, Operand(space));
601 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
602 DwVfpRegister reg = DwVfpRegister::from_code(i);
603 vstr(reg, sp, i * kDoubleSize + kPointerSize);
604 }
605 // Note that d0 will be accessible at fp - 2*kPointerSize -
606 // DwVfpRegister::kNumRegisters * kDoubleSize, since the code slot and the
607 // alignment word were pushed after the fp.
608 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000609}
610
611
Steve Block6ded16b2010-05-10 14:33:55 +0100612void MacroAssembler::InitializeNewString(Register string,
613 Register length,
614 Heap::RootListIndex map_index,
615 Register scratch1,
616 Register scratch2) {
617 mov(scratch1, Operand(length, LSL, kSmiTagSize));
618 LoadRoot(scratch2, map_index);
619 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
620 mov(scratch1, Operand(String::kEmptyHashField));
621 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
622 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
623}
624
625
626int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000627#if defined(V8_HOST_ARCH_ARM)
628 // Running on the real platform. Use the alignment as mandated by the local
629 // environment.
630 // Note: This will break if we ever start generating snapshots on one ARM
631 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100632 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000633#else // defined(V8_HOST_ARCH_ARM)
634 // If we are using the simulator then we should always align to the expected
635 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100636 // if the target platform will need alignment, so this is controlled from a
637 // flag.
638 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000639#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000640}
641
642
Ben Murdochb0fe1622011-05-05 13:52:32 +0100643void MacroAssembler::LeaveExitFrame(bool save_doubles) {
644 // Optionally restore all double registers.
645 if (save_doubles) {
646 // TODO(regis): Use vldrm instruction.
647 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
648 DwVfpRegister reg = DwVfpRegister::from_code(i);
649 // Register d15 is just below the marker.
650 const int offset = ExitFrameConstants::kMarkerOffset;
651 vldr(reg, fp, (i - DwVfpRegister::kNumRegisters) * kDoubleSize + offset);
652 }
653 }
654
Steve Blocka7e24c12009-10-30 11:49:00 +0000655 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100656 mov(r3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000657 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
658 str(r3, MemOperand(ip));
659
660 // Restore current context from top and clear it in debug mode.
661 mov(ip, Operand(ExternalReference(Top::k_context_address)));
662 ldr(cp, MemOperand(ip));
663#ifdef DEBUG
664 str(r3, MemOperand(ip));
665#endif
666
667 // Pop the arguments, restore registers, and return.
668 mov(sp, Operand(fp)); // respect ABI stack constraint
669 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
670}
671
672
673void MacroAssembler::InvokePrologue(const ParameterCount& expected,
674 const ParameterCount& actual,
675 Handle<Code> code_constant,
676 Register code_reg,
677 Label* done,
678 InvokeFlag flag) {
679 bool definitely_matches = false;
680 Label regular_invoke;
681
682 // Check whether the expected and actual arguments count match. If not,
683 // setup registers according to contract with ArgumentsAdaptorTrampoline:
684 // r0: actual arguments count
685 // r1: function (passed through to callee)
686 // r2: expected arguments count
687 // r3: callee code entry
688
689 // The code below is made a lot easier because the calling code already sets
690 // up actual and expected registers according to the contract if values are
691 // passed in registers.
692 ASSERT(actual.is_immediate() || actual.reg().is(r0));
693 ASSERT(expected.is_immediate() || expected.reg().is(r2));
694 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
695
696 if (expected.is_immediate()) {
697 ASSERT(actual.is_immediate());
698 if (expected.immediate() == actual.immediate()) {
699 definitely_matches = true;
700 } else {
701 mov(r0, Operand(actual.immediate()));
702 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
703 if (expected.immediate() == sentinel) {
704 // Don't worry about adapting arguments for builtins that
705 // don't want that done. Skip adaption code by making it look
706 // like we have a match between expected and actual number of
707 // arguments.
708 definitely_matches = true;
709 } else {
710 mov(r2, Operand(expected.immediate()));
711 }
712 }
713 } else {
714 if (actual.is_immediate()) {
715 cmp(expected.reg(), Operand(actual.immediate()));
716 b(eq, &regular_invoke);
717 mov(r0, Operand(actual.immediate()));
718 } else {
719 cmp(expected.reg(), Operand(actual.reg()));
720 b(eq, &regular_invoke);
721 }
722 }
723
724 if (!definitely_matches) {
725 if (!code_constant.is_null()) {
726 mov(r3, Operand(code_constant));
727 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
728 }
729
730 Handle<Code> adaptor =
731 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
732 if (flag == CALL_FUNCTION) {
733 Call(adaptor, RelocInfo::CODE_TARGET);
734 b(done);
735 } else {
736 Jump(adaptor, RelocInfo::CODE_TARGET);
737 }
738 bind(&regular_invoke);
739 }
740}
741
742
743void MacroAssembler::InvokeCode(Register code,
744 const ParameterCount& expected,
745 const ParameterCount& actual,
746 InvokeFlag flag) {
747 Label done;
748
749 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
750 if (flag == CALL_FUNCTION) {
751 Call(code);
752 } else {
753 ASSERT(flag == JUMP_FUNCTION);
754 Jump(code);
755 }
756
757 // Continue here if InvokePrologue does handle the invocation due to
758 // mismatched parameter counts.
759 bind(&done);
760}
761
762
763void MacroAssembler::InvokeCode(Handle<Code> code,
764 const ParameterCount& expected,
765 const ParameterCount& actual,
766 RelocInfo::Mode rmode,
767 InvokeFlag flag) {
768 Label done;
769
770 InvokePrologue(expected, actual, code, no_reg, &done, flag);
771 if (flag == CALL_FUNCTION) {
772 Call(code, rmode);
773 } else {
774 Jump(code, rmode);
775 }
776
777 // Continue here if InvokePrologue does handle the invocation due to
778 // mismatched parameter counts.
779 bind(&done);
780}
781
782
783void MacroAssembler::InvokeFunction(Register fun,
784 const ParameterCount& actual,
785 InvokeFlag flag) {
786 // Contract with called JS functions requires that function is passed in r1.
787 ASSERT(fun.is(r1));
788
789 Register expected_reg = r2;
790 Register code_reg = r3;
791
792 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
793 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
794 ldr(expected_reg,
795 FieldMemOperand(code_reg,
796 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100797 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000798 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100799 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000800
801 ParameterCount expected(expected_reg);
802 InvokeCode(code_reg, expected, actual, flag);
803}
804
805
Andrei Popescu402d9372010-02-26 13:31:12 +0000806void MacroAssembler::InvokeFunction(JSFunction* function,
807 const ParameterCount& actual,
808 InvokeFlag flag) {
809 ASSERT(function->is_compiled());
810
811 // Get the function and setup the context.
812 mov(r1, Operand(Handle<JSFunction>(function)));
813 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
814
815 // Invoke the cached code.
816 Handle<Code> code(function->code());
817 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100818 if (V8::UseCrankshaft()) {
819 // TODO(kasperl): For now, we always call indirectly through the
820 // code field in the function to allow recompilation to take effect
821 // without changing any of the call sites.
822 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
823 InvokeCode(r3, expected, actual, flag);
824 } else {
825 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
826 }
827}
828
829
830void MacroAssembler::IsObjectJSObjectType(Register heap_object,
831 Register map,
832 Register scratch,
833 Label* fail) {
834 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
835 IsInstanceJSObjectType(map, scratch, fail);
836}
837
838
839void MacroAssembler::IsInstanceJSObjectType(Register map,
840 Register scratch,
841 Label* fail) {
842 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
843 cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
844 b(lt, fail);
845 cmp(scratch, Operand(LAST_JS_OBJECT_TYPE));
846 b(gt, fail);
847}
848
849
850void MacroAssembler::IsObjectJSStringType(Register object,
851 Register scratch,
852 Label* fail) {
853 ASSERT(kNotStringTag != 0);
854
855 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
856 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
857 tst(scratch, Operand(kIsNotStringMask));
858 b(nz, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +0000859}
860
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100861
Steve Blocka7e24c12009-10-30 11:49:00 +0000862#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +0000863void MacroAssembler::DebugBreak() {
864 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100865 mov(r0, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000866 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
867 CEntryStub ces(1);
868 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
869}
Steve Blocka7e24c12009-10-30 11:49:00 +0000870#endif
871
872
873void MacroAssembler::PushTryHandler(CodeLocation try_location,
874 HandlerType type) {
875 // Adjust this code if not the case.
876 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
877 // The pc (return address) is passed in register lr.
878 if (try_location == IN_JAVASCRIPT) {
879 if (type == TRY_CATCH_HANDLER) {
880 mov(r3, Operand(StackHandler::TRY_CATCH));
881 } else {
882 mov(r3, Operand(StackHandler::TRY_FINALLY));
883 }
884 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
885 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
886 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
887 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
888 // Save the current handler as the next handler.
889 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
890 ldr(r1, MemOperand(r3));
891 ASSERT(StackHandlerConstants::kNextOffset == 0);
892 push(r1);
893 // Link this handler as the new current one.
894 str(sp, MemOperand(r3));
895 } else {
896 // Must preserve r0-r4, r5-r7 are available.
897 ASSERT(try_location == IN_JS_ENTRY);
898 // The frame pointer does not point to a JS frame so we save NULL
899 // for fp. We expect the code throwing an exception to check fp
900 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100901 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 mov(r6, Operand(StackHandler::ENTRY));
903 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
904 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
905 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
906 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
907 // Save the current handler as the next handler.
908 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
909 ldr(r6, MemOperand(r7));
910 ASSERT(StackHandlerConstants::kNextOffset == 0);
911 push(r6);
912 // Link this handler as the new current one.
913 str(sp, MemOperand(r7));
914 }
915}
916
917
Leon Clarkee46be812010-01-19 14:06:41 +0000918void MacroAssembler::PopTryHandler() {
919 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
920 pop(r1);
921 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
922 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
923 str(r1, MemOperand(ip));
924}
925
926
Steve Blocka7e24c12009-10-30 11:49:00 +0000927void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
928 Register scratch,
929 Label* miss) {
930 Label same_contexts;
931
932 ASSERT(!holder_reg.is(scratch));
933 ASSERT(!holder_reg.is(ip));
934 ASSERT(!scratch.is(ip));
935
936 // Load current lexical context from the stack frame.
937 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
938 // In debug mode, make sure the lexical context is set.
939#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +0100940 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 Check(ne, "we should not have an empty lexical context");
942#endif
943
944 // Load the global context of the current context.
945 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
946 ldr(scratch, FieldMemOperand(scratch, offset));
947 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
948
949 // Check the context is a global context.
950 if (FLAG_debug_code) {
951 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
952 // Cannot use ip as a temporary in this verification code. Due to the fact
953 // that ip is clobbered as part of cmp with an object Operand.
954 push(holder_reg); // Temporarily save holder on the stack.
955 // Read the first word and compare to the global_context_map.
956 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
957 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
958 cmp(holder_reg, ip);
959 Check(eq, "JSGlobalObject::global_context should be a global context.");
960 pop(holder_reg); // Restore holder.
961 }
962
963 // Check if both contexts are the same.
964 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
965 cmp(scratch, Operand(ip));
966 b(eq, &same_contexts);
967
968 // Check the context is a global context.
969 if (FLAG_debug_code) {
970 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
971 // Cannot use ip as a temporary in this verification code. Due to the fact
972 // that ip is clobbered as part of cmp with an object Operand.
973 push(holder_reg); // Temporarily save holder on the stack.
974 mov(holder_reg, ip); // Move ip to its holding place.
975 LoadRoot(ip, Heap::kNullValueRootIndex);
976 cmp(holder_reg, ip);
977 Check(ne, "JSGlobalProxy::context() should not be null.");
978
979 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
980 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
981 cmp(holder_reg, ip);
982 Check(eq, "JSGlobalObject::global_context should be a global context.");
983 // Restore ip is not needed. ip is reloaded below.
984 pop(holder_reg); // Restore holder.
985 // Restore ip to holder's context.
986 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
987 }
988
989 // Check that the security token in the calling global object is
990 // compatible with the security token in the receiving global
991 // object.
992 int token_offset = Context::kHeaderSize +
993 Context::SECURITY_TOKEN_INDEX * kPointerSize;
994
995 ldr(scratch, FieldMemOperand(scratch, token_offset));
996 ldr(ip, FieldMemOperand(ip, token_offset));
997 cmp(scratch, Operand(ip));
998 b(ne, miss);
999
1000 bind(&same_contexts);
1001}
1002
1003
1004void MacroAssembler::AllocateInNewSpace(int object_size,
1005 Register result,
1006 Register scratch1,
1007 Register scratch2,
1008 Label* gc_required,
1009 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001010 if (!FLAG_inline_new) {
1011 if (FLAG_debug_code) {
1012 // Trash the registers to simulate an allocation failure.
1013 mov(result, Operand(0x7091));
1014 mov(scratch1, Operand(0x7191));
1015 mov(scratch2, Operand(0x7291));
1016 }
1017 jmp(gc_required);
1018 return;
1019 }
1020
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001022 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001023 ASSERT(!scratch1.is(scratch2));
1024
Kristian Monsen25f61362010-05-21 11:50:48 +01001025 // Make object size into bytes.
1026 if ((flags & SIZE_IN_WORDS) != 0) {
1027 object_size *= kPointerSize;
1028 }
1029 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1030
Ben Murdochb0fe1622011-05-05 13:52:32 +01001031 // Check relative positions of allocation top and limit addresses.
1032 // The values must be adjacent in memory to allow the use of LDM.
1033 // Also, assert that the registers are numbered such that the values
1034 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001035 ExternalReference new_space_allocation_top =
1036 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001037 ExternalReference new_space_allocation_limit =
1038 ExternalReference::new_space_allocation_limit_address();
1039 intptr_t top =
1040 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1041 intptr_t limit =
1042 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1043 ASSERT((limit - top) == kPointerSize);
1044 ASSERT(result.code() < ip.code());
1045
1046 // Set up allocation top address and object size registers.
1047 Register topaddr = scratch1;
1048 Register obj_size_reg = scratch2;
1049 mov(topaddr, Operand(new_space_allocation_top));
1050 mov(obj_size_reg, Operand(object_size));
1051
1052 // This code stores a temporary value in ip. This is OK, as the code below
1053 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001054 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001055 // Load allocation top into result and allocation limit into ip.
1056 ldm(ia, topaddr, result.bit() | ip.bit());
1057 } else {
1058 if (FLAG_debug_code) {
1059 // Assert that result actually contains top on entry. ip is used
1060 // immediately below so this use of ip does not cause difference with
1061 // respect to register content between debug and release mode.
1062 ldr(ip, MemOperand(topaddr));
1063 cmp(result, ip);
1064 Check(eq, "Unexpected allocation top");
1065 }
1066 // Load allocation limit into ip. Result already contains allocation top.
1067 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 }
1069
1070 // Calculate new top and bail out if new space is exhausted. Use result
1071 // to calculate the new top.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001072 add(scratch2, result, Operand(obj_size_reg));
1073 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001075 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001076
Ben Murdochb0fe1622011-05-05 13:52:32 +01001077 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001079 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001080 }
1081}
1082
1083
1084void MacroAssembler::AllocateInNewSpace(Register object_size,
1085 Register result,
1086 Register scratch1,
1087 Register scratch2,
1088 Label* gc_required,
1089 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001090 if (!FLAG_inline_new) {
1091 if (FLAG_debug_code) {
1092 // Trash the registers to simulate an allocation failure.
1093 mov(result, Operand(0x7091));
1094 mov(scratch1, Operand(0x7191));
1095 mov(scratch2, Operand(0x7291));
1096 }
1097 jmp(gc_required);
1098 return;
1099 }
1100
Ben Murdochb0fe1622011-05-05 13:52:32 +01001101 // Assert that the register arguments are different and that none of
1102 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001103 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001104 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001106 ASSERT(!result.is(ip));
1107 ASSERT(!scratch1.is(ip));
1108 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001109
Ben Murdochb0fe1622011-05-05 13:52:32 +01001110 // Check relative positions of allocation top and limit addresses.
1111 // The values must be adjacent in memory to allow the use of LDM.
1112 // Also, assert that the registers are numbered such that the values
1113 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001114 ExternalReference new_space_allocation_top =
1115 ExternalReference::new_space_allocation_top_address();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001116 ExternalReference new_space_allocation_limit =
1117 ExternalReference::new_space_allocation_limit_address();
1118 intptr_t top =
1119 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1120 intptr_t limit =
1121 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1122 ASSERT((limit - top) == kPointerSize);
1123 ASSERT(result.code() < ip.code());
1124
1125 // Set up allocation top address.
1126 Register topaddr = scratch1;
1127 mov(topaddr, Operand(new_space_allocation_top));
1128
1129 // This code stores a temporary value in ip. This is OK, as the code below
1130 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001131 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001132 // Load allocation top into result and allocation limit into ip.
1133 ldm(ia, topaddr, result.bit() | ip.bit());
1134 } else {
1135 if (FLAG_debug_code) {
1136 // Assert that result actually contains top on entry. ip is used
1137 // immediately below so this use of ip does not cause difference with
1138 // respect to register content between debug and release mode.
1139 ldr(ip, MemOperand(topaddr));
1140 cmp(result, ip);
1141 Check(eq, "Unexpected allocation top");
1142 }
1143 // Load allocation limit into ip. Result already contains allocation top.
1144 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 }
1146
1147 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001148 // to calculate the new top. Object size may be in words so a shift is
1149 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001150 if ((flags & SIZE_IN_WORDS) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001151 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2));
Kristian Monsen25f61362010-05-21 11:50:48 +01001152 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001153 add(scratch2, result, Operand(object_size));
Kristian Monsen25f61362010-05-21 11:50:48 +01001154 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001155 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 b(hi, gc_required);
1157
Steve Blockd0582a62009-12-15 09:54:21 +00001158 // Update allocation top. result temporarily holds the new top.
1159 if (FLAG_debug_code) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001160 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001161 Check(eq, "Unaligned allocation in new space");
1162 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001163 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001164
1165 // Tag object if requested.
1166 if ((flags & TAG_OBJECT) != 0) {
1167 add(result, result, Operand(kHeapObjectTag));
1168 }
1169}
1170
1171
1172void MacroAssembler::UndoAllocationInNewSpace(Register object,
1173 Register scratch) {
1174 ExternalReference new_space_allocation_top =
1175 ExternalReference::new_space_allocation_top_address();
1176
1177 // Make sure the object has no tag before resetting top.
1178 and_(object, object, Operand(~kHeapObjectTagMask));
1179#ifdef DEBUG
1180 // Check that the object un-allocated is below the current top.
1181 mov(scratch, Operand(new_space_allocation_top));
1182 ldr(scratch, MemOperand(scratch));
1183 cmp(object, scratch);
1184 Check(lt, "Undo allocation of non allocated memory");
1185#endif
1186 // Write the address of the object to un-allocate as the current top.
1187 mov(scratch, Operand(new_space_allocation_top));
1188 str(object, MemOperand(scratch));
1189}
1190
1191
Andrei Popescu31002712010-02-23 13:46:05 +00001192void MacroAssembler::AllocateTwoByteString(Register result,
1193 Register length,
1194 Register scratch1,
1195 Register scratch2,
1196 Register scratch3,
1197 Label* gc_required) {
1198 // Calculate the number of bytes needed for the characters in the string while
1199 // observing object alignment.
1200 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1201 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1202 add(scratch1, scratch1,
1203 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001204 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001205
1206 // Allocate two-byte string in new space.
1207 AllocateInNewSpace(scratch1,
1208 result,
1209 scratch2,
1210 scratch3,
1211 gc_required,
1212 TAG_OBJECT);
1213
1214 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001215 InitializeNewString(result,
1216 length,
1217 Heap::kStringMapRootIndex,
1218 scratch1,
1219 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001220}
1221
1222
1223void MacroAssembler::AllocateAsciiString(Register result,
1224 Register length,
1225 Register scratch1,
1226 Register scratch2,
1227 Register scratch3,
1228 Label* gc_required) {
1229 // Calculate the number of bytes needed for the characters in the string while
1230 // observing object alignment.
1231 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1232 ASSERT(kCharSize == 1);
1233 add(scratch1, length,
1234 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001235 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001236
1237 // Allocate ASCII string in new space.
1238 AllocateInNewSpace(scratch1,
1239 result,
1240 scratch2,
1241 scratch3,
1242 gc_required,
1243 TAG_OBJECT);
1244
1245 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001246 InitializeNewString(result,
1247 length,
1248 Heap::kAsciiStringMapRootIndex,
1249 scratch1,
1250 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001251}
1252
1253
1254void MacroAssembler::AllocateTwoByteConsString(Register result,
1255 Register length,
1256 Register scratch1,
1257 Register scratch2,
1258 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001259 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001260 result,
1261 scratch1,
1262 scratch2,
1263 gc_required,
1264 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001265
1266 InitializeNewString(result,
1267 length,
1268 Heap::kConsStringMapRootIndex,
1269 scratch1,
1270 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001271}
1272
1273
1274void MacroAssembler::AllocateAsciiConsString(Register result,
1275 Register length,
1276 Register scratch1,
1277 Register scratch2,
1278 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001279 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001280 result,
1281 scratch1,
1282 scratch2,
1283 gc_required,
1284 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001285
1286 InitializeNewString(result,
1287 length,
1288 Heap::kConsAsciiStringMapRootIndex,
1289 scratch1,
1290 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001291}
1292
1293
Steve Block6ded16b2010-05-10 14:33:55 +01001294void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001295 Register map,
1296 Register type_reg,
1297 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001298 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001299 CompareInstanceType(map, type_reg, type);
1300}
1301
1302
1303void MacroAssembler::CompareInstanceType(Register map,
1304 Register type_reg,
1305 InstanceType type) {
1306 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1307 cmp(type_reg, Operand(type));
1308}
1309
1310
Andrei Popescu31002712010-02-23 13:46:05 +00001311void MacroAssembler::CheckMap(Register obj,
1312 Register scratch,
1313 Handle<Map> map,
1314 Label* fail,
1315 bool is_heap_object) {
1316 if (!is_heap_object) {
1317 BranchOnSmi(obj, fail);
1318 }
1319 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1320 mov(ip, Operand(map));
1321 cmp(scratch, ip);
1322 b(ne, fail);
1323}
1324
1325
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001326void MacroAssembler::CheckMap(Register obj,
1327 Register scratch,
1328 Heap::RootListIndex index,
1329 Label* fail,
1330 bool is_heap_object) {
1331 if (!is_heap_object) {
1332 BranchOnSmi(obj, fail);
1333 }
1334 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1335 LoadRoot(ip, index);
1336 cmp(scratch, ip);
1337 b(ne, fail);
1338}
1339
1340
Steve Blocka7e24c12009-10-30 11:49:00 +00001341void MacroAssembler::TryGetFunctionPrototype(Register function,
1342 Register result,
1343 Register scratch,
1344 Label* miss) {
1345 // Check that the receiver isn't a smi.
1346 BranchOnSmi(function, miss);
1347
1348 // Check that the function really is a function. Load map into result reg.
1349 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1350 b(ne, miss);
1351
1352 // Make sure that the function has an instance prototype.
1353 Label non_instance;
1354 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1355 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1356 b(ne, &non_instance);
1357
1358 // Get the prototype or initial map from the function.
1359 ldr(result,
1360 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1361
1362 // If the prototype or initial map is the hole, don't return it and
1363 // simply miss the cache instead. This will allow us to allocate a
1364 // prototype object on-demand in the runtime system.
1365 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1366 cmp(result, ip);
1367 b(eq, miss);
1368
1369 // If the function does not have an initial map, we're done.
1370 Label done;
1371 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1372 b(ne, &done);
1373
1374 // Get the prototype from the initial map.
1375 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1376 jmp(&done);
1377
1378 // Non-instance prototype: Fetch prototype from constructor field
1379 // in initial map.
1380 bind(&non_instance);
1381 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1382
1383 // All done.
1384 bind(&done);
1385}
1386
1387
1388void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1389 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1390 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1391}
1392
1393
Andrei Popescu31002712010-02-23 13:46:05 +00001394void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1395 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1396 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1397}
1398
1399
Steve Blocka7e24c12009-10-30 11:49:00 +00001400void MacroAssembler::IllegalOperation(int num_arguments) {
1401 if (num_arguments > 0) {
1402 add(sp, sp, Operand(num_arguments * kPointerSize));
1403 }
1404 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1405}
1406
1407
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001408void MacroAssembler::IndexFromHash(Register hash, Register index) {
1409 // If the hash field contains an array index pick it out. The assert checks
1410 // that the constants for the maximum number of digits for an array index
1411 // cached in the hash field and the number of bits reserved for it does not
1412 // conflict.
1413 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1414 (1 << String::kArrayIndexValueBits));
1415 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1416 // the low kHashShift bits.
1417 STATIC_ASSERT(kSmiTag == 0);
1418 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1419 mov(index, Operand(hash, LSL, kSmiTagSize));
1420}
1421
1422
Steve Blockd0582a62009-12-15 09:54:21 +00001423void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1424 Register outHighReg,
1425 Register outLowReg) {
1426 // ARMv7 VFP3 instructions to implement integer to double conversion.
1427 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001428 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001429 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001430 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001431}
1432
1433
Steve Block8defd9f2010-07-08 12:39:36 +01001434void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1435 DwVfpRegister result,
1436 Register scratch1,
1437 Register scratch2,
1438 Register heap_number_map,
1439 SwVfpRegister scratch3,
1440 Label* not_number,
1441 ObjectToDoubleFlags flags) {
1442 Label done;
1443 if ((flags & OBJECT_NOT_SMI) == 0) {
1444 Label not_smi;
1445 BranchOnNotSmi(object, &not_smi);
1446 // Remove smi tag and convert to double.
1447 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1448 vmov(scratch3, scratch1);
1449 vcvt_f64_s32(result, scratch3);
1450 b(&done);
1451 bind(&not_smi);
1452 }
1453 // Check for heap number and load double value from it.
1454 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1455 sub(scratch2, object, Operand(kHeapObjectTag));
1456 cmp(scratch1, heap_number_map);
1457 b(ne, not_number);
1458 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1459 // If exponent is all ones the number is either a NaN or +/-Infinity.
1460 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1461 Sbfx(scratch1,
1462 scratch1,
1463 HeapNumber::kExponentShift,
1464 HeapNumber::kExponentBits);
1465 // All-one value sign extend to -1.
1466 cmp(scratch1, Operand(-1));
1467 b(eq, not_number);
1468 }
1469 vldr(result, scratch2, HeapNumber::kValueOffset);
1470 bind(&done);
1471}
1472
1473
1474void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1475 DwVfpRegister value,
1476 Register scratch1,
1477 SwVfpRegister scratch2) {
1478 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1479 vmov(scratch2, scratch1);
1480 vcvt_f64_s32(value, scratch2);
1481}
1482
1483
Iain Merrick9ac36c92010-09-13 15:29:50 +01001484// Tries to get a signed int32 out of a double precision floating point heap
1485// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1486// 32bits signed integer range.
1487void MacroAssembler::ConvertToInt32(Register source,
1488 Register dest,
1489 Register scratch,
1490 Register scratch2,
1491 Label *not_int32) {
1492 if (CpuFeatures::IsSupported(VFP3)) {
1493 CpuFeatures::Scope scope(VFP3);
1494 sub(scratch, source, Operand(kHeapObjectTag));
1495 vldr(d0, scratch, HeapNumber::kValueOffset);
1496 vcvt_s32_f64(s0, d0);
1497 vmov(dest, s0);
1498 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1499 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1500 // range. When substracting one, the minimum signed integer becomes the
1501 // maximun signed integer.
1502 sub(scratch, dest, Operand(1));
1503 cmp(scratch, Operand(LONG_MAX - 1));
1504 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1505 b(ge, not_int32);
1506 } else {
1507 // This code is faster for doubles that are in the ranges -0x7fffffff to
1508 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1509 // the range of signed int32 values that are not Smis. Jumps to the label
1510 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1511 // 0x80000000.0 (excluding the endpoints).
1512 Label right_exponent, done;
1513 // Get exponent word.
1514 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1515 // Get exponent alone in scratch2.
1516 Ubfx(scratch2,
1517 scratch,
1518 HeapNumber::kExponentShift,
1519 HeapNumber::kExponentBits);
1520 // Load dest with zero. We use this either for the final shift or
1521 // for the answer.
1522 mov(dest, Operand(0, RelocInfo::NONE));
1523 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1524 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1525 // the exponent that we are fastest at and also the highest exponent we can
1526 // handle here.
1527 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1528 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1529 // split it up to avoid a constant pool entry. You can't do that in general
1530 // for cmp because of the overflow flag, but we know the exponent is in the
1531 // range 0-2047 so there is no overflow.
1532 int fudge_factor = 0x400;
1533 sub(scratch2, scratch2, Operand(fudge_factor));
1534 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1535 // If we have a match of the int32-but-not-Smi exponent then skip some
1536 // logic.
1537 b(eq, &right_exponent);
1538 // If the exponent is higher than that then go to slow case. This catches
1539 // numbers that don't fit in a signed int32, infinities and NaNs.
1540 b(gt, not_int32);
1541
1542 // We know the exponent is smaller than 30 (biased). If it is less than
1543 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1544 // it rounds to zero.
1545 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1546 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1547 // Dest already has a Smi zero.
1548 b(lt, &done);
1549
1550 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1551 // get how much to shift down.
1552 rsb(dest, scratch2, Operand(30));
1553
1554 bind(&right_exponent);
1555 // Get the top bits of the mantissa.
1556 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1557 // Put back the implicit 1.
1558 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1559 // Shift up the mantissa bits to take up the space the exponent used to
1560 // take. We just orred in the implicit bit so that took care of one and
1561 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1562 // distance.
1563 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1564 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1565 // Put sign in zero flag.
1566 tst(scratch, Operand(HeapNumber::kSignMask));
1567 // Get the second half of the double. For some exponents we don't
1568 // actually need this because the bits get shifted out again, but
1569 // it's probably slower to test than just to do it.
1570 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1571 // Shift down 22 bits to get the last 10 bits.
1572 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1573 // Move down according to the exponent.
1574 mov(dest, Operand(scratch, LSR, dest));
1575 // Fix sign if sign bit was set.
1576 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1577 bind(&done);
1578 }
1579}
1580
1581
Andrei Popescu31002712010-02-23 13:46:05 +00001582void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1583 Register src,
1584 int num_least_bits) {
1585 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001586 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00001587 } else {
1588 mov(dst, Operand(src, ASR, kSmiTagSize));
1589 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1590 }
1591}
1592
1593
Steve Blocka7e24c12009-10-30 11:49:00 +00001594void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1595 // All parameters are on the stack. r0 has the return value after call.
1596
1597 // If the expected number of arguments of the runtime function is
1598 // constant, we check that the actual number of arguments match the
1599 // expectation.
1600 if (f->nargs >= 0 && f->nargs != num_arguments) {
1601 IllegalOperation(num_arguments);
1602 return;
1603 }
1604
Leon Clarke4515c472010-02-03 11:58:03 +00001605 // TODO(1236192): Most runtime routines don't need the number of
1606 // arguments passed in because it is constant. At some point we
1607 // should remove this need and make the runtime routine entry code
1608 // smarter.
1609 mov(r0, Operand(num_arguments));
1610 mov(r1, Operand(ExternalReference(f)));
1611 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001612 CallStub(&stub);
1613}
1614
1615
1616void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1617 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1618}
1619
1620
Ben Murdochb0fe1622011-05-05 13:52:32 +01001621void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1622 Runtime::Function* function = Runtime::FunctionForId(id);
1623 mov(r0, Operand(function->nargs));
1624 mov(r1, Operand(ExternalReference(function)));
1625 CEntryStub stub(1);
1626 stub.SaveDoubles();
1627 CallStub(&stub);
1628}
1629
1630
Andrei Popescu402d9372010-02-26 13:31:12 +00001631void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1632 int num_arguments) {
1633 mov(r0, Operand(num_arguments));
1634 mov(r1, Operand(ext));
1635
1636 CEntryStub stub(1);
1637 CallStub(&stub);
1638}
1639
1640
Steve Block6ded16b2010-05-10 14:33:55 +01001641void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1642 int num_arguments,
1643 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001644 // TODO(1236192): Most runtime routines don't need the number of
1645 // arguments passed in because it is constant. At some point we
1646 // should remove this need and make the runtime routine entry code
1647 // smarter.
1648 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01001649 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00001650}
1651
1652
Steve Block6ded16b2010-05-10 14:33:55 +01001653void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1654 int num_arguments,
1655 int result_size) {
1656 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1657}
1658
1659
1660void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001661#if defined(__thumb__)
1662 // Thumb mode builtin.
1663 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1664#endif
1665 mov(r1, Operand(builtin));
1666 CEntryStub stub(1);
1667 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1668}
1669
1670
Steve Blocka7e24c12009-10-30 11:49:00 +00001671void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1672 InvokeJSFlags flags) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001673 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00001674 if (flags == CALL_JS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001675 Call(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001676 } else {
1677 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00001678 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 }
1680}
1681
1682
Steve Block791712a2010-08-27 10:21:07 +01001683void MacroAssembler::GetBuiltinFunction(Register target,
1684 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01001685 // Load the builtins object into target register.
1686 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1687 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001688 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01001689 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01001690 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1691}
1692
1693
1694void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1695 ASSERT(!target.is(r1));
1696 GetBuiltinFunction(r1, id);
1697 // Load the code entry point from the builtins object.
1698 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001699}
1700
1701
1702void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1703 Register scratch1, Register scratch2) {
1704 if (FLAG_native_code_counters && counter->Enabled()) {
1705 mov(scratch1, Operand(value));
1706 mov(scratch2, Operand(ExternalReference(counter)));
1707 str(scratch1, MemOperand(scratch2));
1708 }
1709}
1710
1711
1712void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1713 Register scratch1, Register scratch2) {
1714 ASSERT(value > 0);
1715 if (FLAG_native_code_counters && counter->Enabled()) {
1716 mov(scratch2, Operand(ExternalReference(counter)));
1717 ldr(scratch1, MemOperand(scratch2));
1718 add(scratch1, scratch1, Operand(value));
1719 str(scratch1, MemOperand(scratch2));
1720 }
1721}
1722
1723
1724void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1725 Register scratch1, Register scratch2) {
1726 ASSERT(value > 0);
1727 if (FLAG_native_code_counters && counter->Enabled()) {
1728 mov(scratch2, Operand(ExternalReference(counter)));
1729 ldr(scratch1, MemOperand(scratch2));
1730 sub(scratch1, scratch1, Operand(value));
1731 str(scratch1, MemOperand(scratch2));
1732 }
1733}
1734
1735
1736void MacroAssembler::Assert(Condition cc, const char* msg) {
1737 if (FLAG_debug_code)
1738 Check(cc, msg);
1739}
1740
1741
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001742void MacroAssembler::AssertRegisterIsRoot(Register reg,
1743 Heap::RootListIndex index) {
1744 if (FLAG_debug_code) {
1745 LoadRoot(ip, index);
1746 cmp(reg, ip);
1747 Check(eq, "Register did not match expected root");
1748 }
1749}
1750
1751
Iain Merrick75681382010-08-19 15:07:18 +01001752void MacroAssembler::AssertFastElements(Register elements) {
1753 if (FLAG_debug_code) {
1754 ASSERT(!elements.is(ip));
1755 Label ok;
1756 push(elements);
1757 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
1758 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
1759 cmp(elements, ip);
1760 b(eq, &ok);
1761 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
1762 cmp(elements, ip);
1763 b(eq, &ok);
1764 Abort("JSObject with fast elements map has slow elements");
1765 bind(&ok);
1766 pop(elements);
1767 }
1768}
1769
1770
Steve Blocka7e24c12009-10-30 11:49:00 +00001771void MacroAssembler::Check(Condition cc, const char* msg) {
1772 Label L;
1773 b(cc, &L);
1774 Abort(msg);
1775 // will not return here
1776 bind(&L);
1777}
1778
1779
1780void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01001781 Label abort_start;
1782 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00001783 // We want to pass the msg string like a smi to avoid GC
1784 // problems, however msg is not guaranteed to be aligned
1785 // properly. Instead, we pass an aligned pointer that is
1786 // a proper v8 smi, but also pass the alignment difference
1787 // from the real pointer as a smi.
1788 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1789 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1790 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1791#ifdef DEBUG
1792 if (msg != NULL) {
1793 RecordComment("Abort message: ");
1794 RecordComment(msg);
1795 }
1796#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001797 // Disable stub call restrictions to always allow calls to abort.
1798 set_allow_stub_calls(true);
1799
Steve Blocka7e24c12009-10-30 11:49:00 +00001800 mov(r0, Operand(p0));
1801 push(r0);
1802 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1803 push(r0);
1804 CallRuntime(Runtime::kAbort, 2);
1805 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01001806 if (is_const_pool_blocked()) {
1807 // If the calling code cares about the exact number of
1808 // instructions generated, we insert padding here to keep the size
1809 // of the Abort macro constant.
1810 static const int kExpectedAbortInstructions = 10;
1811 int abort_instructions = InstructionsGeneratedSince(&abort_start);
1812 ASSERT(abort_instructions <= kExpectedAbortInstructions);
1813 while (abort_instructions++ < kExpectedAbortInstructions) {
1814 nop();
1815 }
1816 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001817}
1818
1819
Steve Blockd0582a62009-12-15 09:54:21 +00001820void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1821 if (context_chain_length > 0) {
1822 // Move up the chain of contexts to the context containing the slot.
1823 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1824 // Load the function context (which is the incoming, outer context).
1825 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1826 for (int i = 1; i < context_chain_length; i++) {
1827 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1828 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1829 }
1830 // The context may be an intermediate context, not a function context.
1831 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1832 } else { // Slot is in the current function context.
1833 // The context may be an intermediate context, not a function context.
1834 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1835 }
1836}
1837
1838
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001839void MacroAssembler::LoadGlobalFunction(int index, Register function) {
1840 // Load the global or builtins object from the current context.
1841 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1842 // Load the global context from the global or builtins object.
1843 ldr(function, FieldMemOperand(function,
1844 GlobalObject::kGlobalContextOffset));
1845 // Load the function from the global context.
1846 ldr(function, MemOperand(function, Context::SlotOffset(index)));
1847}
1848
1849
1850void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
1851 Register map,
1852 Register scratch) {
1853 // Load the initial map. The global functions all have initial maps.
1854 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1855 if (FLAG_debug_code) {
1856 Label ok, fail;
1857 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, false);
1858 b(&ok);
1859 bind(&fail);
1860 Abort("Global functions must have initial map");
1861 bind(&ok);
1862 }
1863}
1864
1865
Andrei Popescu31002712010-02-23 13:46:05 +00001866void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1867 Register reg2,
1868 Label* on_not_both_smi) {
1869 ASSERT_EQ(0, kSmiTag);
1870 tst(reg1, Operand(kSmiTagMask));
1871 tst(reg2, Operand(kSmiTagMask), eq);
1872 b(ne, on_not_both_smi);
1873}
1874
1875
1876void MacroAssembler::JumpIfEitherSmi(Register reg1,
1877 Register reg2,
1878 Label* on_either_smi) {
1879 ASSERT_EQ(0, kSmiTag);
1880 tst(reg1, Operand(kSmiTagMask));
1881 tst(reg2, Operand(kSmiTagMask), ne);
1882 b(eq, on_either_smi);
1883}
1884
1885
Iain Merrick75681382010-08-19 15:07:18 +01001886void MacroAssembler::AbortIfSmi(Register object) {
1887 ASSERT_EQ(0, kSmiTag);
1888 tst(object, Operand(kSmiTagMask));
1889 Assert(ne, "Operand is a smi");
1890}
1891
1892
Leon Clarked91b9f72010-01-27 17:25:45 +00001893void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1894 Register first,
1895 Register second,
1896 Register scratch1,
1897 Register scratch2,
1898 Label* failure) {
1899 // Test that both first and second are sequential ASCII strings.
1900 // Assume that they are non-smis.
1901 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1902 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1903 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1904 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001905
1906 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1907 scratch2,
1908 scratch1,
1909 scratch2,
1910 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00001911}
1912
1913void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1914 Register second,
1915 Register scratch1,
1916 Register scratch2,
1917 Label* failure) {
1918 // Check that neither is a smi.
1919 ASSERT_EQ(0, kSmiTag);
1920 and_(scratch1, first, Operand(second));
1921 tst(scratch1, Operand(kSmiTagMask));
1922 b(eq, failure);
1923 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1924 second,
1925 scratch1,
1926 scratch2,
1927 failure);
1928}
1929
Steve Blockd0582a62009-12-15 09:54:21 +00001930
Steve Block6ded16b2010-05-10 14:33:55 +01001931// Allocates a heap number or jumps to the need_gc label if the young space
1932// is full and a scavenge is needed.
1933void MacroAssembler::AllocateHeapNumber(Register result,
1934 Register scratch1,
1935 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001936 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01001937 Label* gc_required) {
1938 // Allocate an object in the heap for the heap number and tag it as a heap
1939 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01001940 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001941 result,
1942 scratch1,
1943 scratch2,
1944 gc_required,
1945 TAG_OBJECT);
1946
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001947 // Store heap number map in the allocated object.
1948 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1949 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001950}
1951
1952
Steve Block8defd9f2010-07-08 12:39:36 +01001953void MacroAssembler::AllocateHeapNumberWithValue(Register result,
1954 DwVfpRegister value,
1955 Register scratch1,
1956 Register scratch2,
1957 Register heap_number_map,
1958 Label* gc_required) {
1959 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
1960 sub(scratch1, result, Operand(kHeapObjectTag));
1961 vstr(value, scratch1, HeapNumber::kValueOffset);
1962}
1963
1964
Ben Murdochbb769b22010-08-11 14:56:33 +01001965// Copies a fixed number of fields of heap objects from src to dst.
1966void MacroAssembler::CopyFields(Register dst,
1967 Register src,
1968 RegList temps,
1969 int field_count) {
1970 // At least one bit set in the first 15 registers.
1971 ASSERT((temps & ((1 << 15) - 1)) != 0);
1972 ASSERT((temps & dst.bit()) == 0);
1973 ASSERT((temps & src.bit()) == 0);
1974 // Primitive implementation using only one temporary register.
1975
1976 Register tmp = no_reg;
1977 // Find a temp register in temps list.
1978 for (int i = 0; i < 15; i++) {
1979 if ((temps & (1 << i)) != 0) {
1980 tmp.set_code(i);
1981 break;
1982 }
1983 }
1984 ASSERT(!tmp.is(no_reg));
1985
1986 for (int i = 0; i < field_count; i++) {
1987 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
1988 str(tmp, FieldMemOperand(dst, i * kPointerSize));
1989 }
1990}
1991
1992
Steve Block8defd9f2010-07-08 12:39:36 +01001993void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
1994 Register source, // Input.
1995 Register scratch) {
1996 ASSERT(!zeros.is(source) || !source.is(zeros));
1997 ASSERT(!zeros.is(scratch));
1998 ASSERT(!scratch.is(ip));
1999 ASSERT(!source.is(ip));
2000 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002001#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2002 clz(zeros, source); // This instruction is only supported after ARM5.
2003#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01002004 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01002005 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002006 // Top 16.
2007 tst(scratch, Operand(0xffff0000));
2008 add(zeros, zeros, Operand(16), LeaveCC, eq);
2009 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2010 // Top 8.
2011 tst(scratch, Operand(0xff000000));
2012 add(zeros, zeros, Operand(8), LeaveCC, eq);
2013 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2014 // Top 4.
2015 tst(scratch, Operand(0xf0000000));
2016 add(zeros, zeros, Operand(4), LeaveCC, eq);
2017 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2018 // Top 2.
2019 tst(scratch, Operand(0xc0000000));
2020 add(zeros, zeros, Operand(2), LeaveCC, eq);
2021 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2022 // Top bit.
2023 tst(scratch, Operand(0x80000000u));
2024 add(zeros, zeros, Operand(1), LeaveCC, eq);
2025#endif
2026}
2027
2028
2029void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2030 Register first,
2031 Register second,
2032 Register scratch1,
2033 Register scratch2,
2034 Label* failure) {
2035 int kFlatAsciiStringMask =
2036 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2037 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2038 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2039 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2040 cmp(scratch1, Operand(kFlatAsciiStringTag));
2041 // Ignore second test if first test failed.
2042 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2043 b(ne, failure);
2044}
2045
2046
2047void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
2048 Register scratch,
2049 Label* failure) {
2050 int kFlatAsciiStringMask =
2051 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2052 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2053 and_(scratch, type, Operand(kFlatAsciiStringMask));
2054 cmp(scratch, Operand(kFlatAsciiStringTag));
2055 b(ne, failure);
2056}
2057
2058
2059void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2060 int frame_alignment = ActivationFrameAlignment();
2061 // Up to four simple arguments are passed in registers r0..r3.
2062 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2063 if (frame_alignment > kPointerSize) {
2064 // Make stack end at alignment and make room for num_arguments - 4 words
2065 // and the original value of sp.
2066 mov(scratch, sp);
2067 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
2068 ASSERT(IsPowerOf2(frame_alignment));
2069 and_(sp, sp, Operand(-frame_alignment));
2070 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2071 } else {
2072 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
2073 }
2074}
2075
2076
2077void MacroAssembler::CallCFunction(ExternalReference function,
2078 int num_arguments) {
2079 mov(ip, Operand(function));
2080 CallCFunction(ip, num_arguments);
2081}
2082
2083
2084void MacroAssembler::CallCFunction(Register function, int num_arguments) {
2085 // Make sure that the stack is aligned before calling a C function unless
2086 // running in the simulator. The simulator has its own alignment check which
2087 // provides more information.
2088#if defined(V8_HOST_ARCH_ARM)
2089 if (FLAG_debug_code) {
2090 int frame_alignment = OS::ActivationFrameAlignment();
2091 int frame_alignment_mask = frame_alignment - 1;
2092 if (frame_alignment > kPointerSize) {
2093 ASSERT(IsPowerOf2(frame_alignment));
2094 Label alignment_as_expected;
2095 tst(sp, Operand(frame_alignment_mask));
2096 b(eq, &alignment_as_expected);
2097 // Don't use Check here, as it will call Runtime_Abort possibly
2098 // re-entering here.
2099 stop("Unexpected alignment");
2100 bind(&alignment_as_expected);
2101 }
2102 }
2103#endif
2104
2105 // Just call directly. The function called cannot cause a GC, or
2106 // allow preemption, so the return address in the link register
2107 // stays correct.
2108 Call(function);
2109 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
2110 if (OS::ActivationFrameAlignment() > kPointerSize) {
2111 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
2112 } else {
2113 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
2114 }
2115}
2116
2117
Steve Blocka7e24c12009-10-30 11:49:00 +00002118#ifdef ENABLE_DEBUGGER_SUPPORT
2119CodePatcher::CodePatcher(byte* address, int instructions)
2120 : address_(address),
2121 instructions_(instructions),
2122 size_(instructions * Assembler::kInstrSize),
2123 masm_(address, size_ + Assembler::kGap) {
2124 // Create a new macro assembler pointing to the address of the code to patch.
2125 // The size is adjusted with kGap on order for the assembler to generate size
2126 // bytes of instructions without failing with buffer size constraints.
2127 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2128}
2129
2130
2131CodePatcher::~CodePatcher() {
2132 // Indicate that code has changed.
2133 CPU::FlushICache(address_, size_);
2134
2135 // Check that the code was patched as expected.
2136 ASSERT(masm_.pc_ == address_ + size_);
2137 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2138}
2139
2140
2141void CodePatcher::Emit(Instr x) {
2142 masm()->emit(x);
2143}
2144
2145
2146void CodePatcher::Emit(Address addr) {
2147 masm()->emit(reinterpret_cast<Instr>(addr));
2148}
2149#endif // ENABLE_DEBUGGER_SUPPORT
2150
2151
2152} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002153
2154#endif // V8_TARGET_ARCH_ARM