blob: 3a1a8b6a6f1a3510edd2b7c8d6a3582ae9bede81 [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),
Steve Block44f0eee2011-05-26 01:26:41 +010046 code_object_(HEAP->undefined_value()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000047}
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
Steve Block44f0eee2011-05-26 01:26:41 +0100106int MacroAssembler::CallSize(Register target, Condition cond) {
107#if USE_BLX
108 return kInstrSize;
109#else
110 return 2 * kInstrSize;
111#endif
112}
113
114
Steve Blocka7e24c12009-10-30 11:49:00 +0000115void MacroAssembler::Call(Register target, Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100116 // Block constant pool for the call instruction sequence.
117 BlockConstPoolScope block_const_pool(this);
118#ifdef DEBUG
119 int pre_position = pc_offset();
120#endif
121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122#if USE_BLX
123 blx(target, cond);
124#else
125 // set lr for return at current pc + 8
126 mov(lr, Operand(pc), LeaveCC, cond);
127 mov(pc, Operand(target), LeaveCC, cond);
128#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100129
130#ifdef DEBUG
131 int post_position = pc_offset();
132 CHECK_EQ(pre_position + CallSize(target, cond), post_position);
133#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000134}
135
136
Steve Block44f0eee2011-05-26 01:26:41 +0100137int MacroAssembler::CallSize(
138 intptr_t target, RelocInfo::Mode rmode, Condition cond) {
139 int size = 2 * kInstrSize;
140 Instr mov_instr = cond | MOV | LeaveCC;
141 if (!Operand(target, rmode).is_single_instruction(mov_instr)) {
142 size += kInstrSize;
143 }
144 return size;
145}
146
147
148void MacroAssembler::Call(
149 intptr_t target, RelocInfo::Mode rmode, Condition cond) {
150 // Block constant pool for the call instruction sequence.
151 BlockConstPoolScope block_const_pool(this);
152#ifdef DEBUG
153 int pre_position = pc_offset();
154#endif
155
Steve Block6ded16b2010-05-10 14:33:55 +0100156#if USE_BLX
157 // On ARMv5 and after the recommended call sequence is:
158 // ldr ip, [pc, #...]
159 // blx ip
160
Steve Block44f0eee2011-05-26 01:26:41 +0100161 // Statement positions are expected to be recorded when the target
162 // address is loaded. The mov method will automatically record
163 // positions when pc is the target, since this is not the case here
164 // we have to do it explicitly.
165 positions_recorder()->WriteRecordedPositions();
Steve Block6ded16b2010-05-10 14:33:55 +0100166
Steve Block44f0eee2011-05-26 01:26:41 +0100167 mov(ip, Operand(target, rmode), LeaveCC, cond);
168 blx(ip, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100169
170 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
171#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 // Set lr for return at current pc + 8.
173 mov(lr, Operand(pc), LeaveCC, cond);
174 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
175 mov(pc, Operand(target, rmode), LeaveCC, cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100177#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100178
179#ifdef DEBUG
180 int post_position = pc_offset();
181 CHECK_EQ(pre_position + CallSize(target, rmode, cond), post_position);
182#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000183}
184
185
Steve Block44f0eee2011-05-26 01:26:41 +0100186int MacroAssembler::CallSize(
187 byte* target, RelocInfo::Mode rmode, Condition cond) {
188 return CallSize(reinterpret_cast<intptr_t>(target), rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000189}
190
191
Steve Block44f0eee2011-05-26 01:26:41 +0100192void MacroAssembler::Call(
193 byte* target, RelocInfo::Mode rmode, Condition cond) {
194#ifdef DEBUG
195 int pre_position = pc_offset();
196#endif
197
198 ASSERT(!RelocInfo::IsCodeTarget(rmode));
199 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
200
201#ifdef DEBUG
202 int post_position = pc_offset();
203 CHECK_EQ(pre_position + CallSize(target, rmode, cond), post_position);
204#endif
205}
206
207
208int MacroAssembler::CallSize(
209 Handle<Code> code, RelocInfo::Mode rmode, Condition cond) {
210 return CallSize(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
211}
212
213
214void MacroAssembler::Call(
215 Handle<Code> code, RelocInfo::Mode rmode, Condition cond) {
216#ifdef DEBUG
217 int pre_position = pc_offset();
218#endif
219
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 ASSERT(RelocInfo::IsCodeTarget(rmode));
221 // 'code' is always generated ARM code, never THUMB code
222 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
Steve Block44f0eee2011-05-26 01:26:41 +0100223
224#ifdef DEBUG
225 int post_position = pc_offset();
226 CHECK_EQ(pre_position + CallSize(code, rmode, cond), post_position);
227#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000228}
229
230
231void MacroAssembler::Ret(Condition cond) {
232#if USE_BX
233 bx(lr, cond);
234#else
235 mov(pc, Operand(lr), LeaveCC, cond);
236#endif
237}
238
239
Leon Clarkee46be812010-01-19 14:06:41 +0000240void MacroAssembler::Drop(int count, Condition cond) {
241 if (count > 0) {
242 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
243 }
244}
245
246
Ben Murdochb0fe1622011-05-05 13:52:32 +0100247void MacroAssembler::Ret(int drop, Condition cond) {
248 Drop(drop, cond);
249 Ret(cond);
250}
251
252
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100253void MacroAssembler::Swap(Register reg1,
254 Register reg2,
255 Register scratch,
256 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100257 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100258 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
259 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
260 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100261 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100262 mov(scratch, reg1, LeaveCC, cond);
263 mov(reg1, reg2, LeaveCC, cond);
264 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100265 }
266}
267
268
Leon Clarkee46be812010-01-19 14:06:41 +0000269void MacroAssembler::Call(Label* target) {
270 bl(target);
271}
272
273
274void MacroAssembler::Move(Register dst, Handle<Object> value) {
275 mov(dst, Operand(value));
276}
Steve Blockd0582a62009-12-15 09:54:21 +0000277
278
Steve Block6ded16b2010-05-10 14:33:55 +0100279void MacroAssembler::Move(Register dst, Register src) {
280 if (!dst.is(src)) {
281 mov(dst, src);
282 }
283}
284
285
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100286void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
287 Condition cond) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800288 if (!src2.is_reg() &&
289 !src2.must_use_constant_pool() &&
290 src2.immediate() == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100291 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800292
293 } else if (!src2.is_single_instruction() &&
294 !src2.must_use_constant_pool() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100295 Isolate::Current()->cpu_features()->IsSupported(ARMv7) &&
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800296 IsPowerOf2(src2.immediate() + 1)) {
297 ubfx(dst, src1, 0, WhichPowerOf2(src2.immediate() + 1), cond);
298
299 } else {
300 and_(dst, src1, src2, LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100301 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100302}
303
304
305void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
306 Condition cond) {
307 ASSERT(lsb < 32);
Steve Block44f0eee2011-05-26 01:26:41 +0100308 if (!Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100309 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
310 and_(dst, src1, Operand(mask), LeaveCC, cond);
311 if (lsb != 0) {
312 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
313 }
314 } else {
315 ubfx(dst, src1, lsb, width, cond);
316 }
317}
318
319
320void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
321 Condition cond) {
322 ASSERT(lsb < 32);
Steve Block44f0eee2011-05-26 01:26:41 +0100323 if (!Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100324 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
325 and_(dst, src1, Operand(mask), LeaveCC, cond);
326 int shift_up = 32 - lsb - width;
327 int shift_down = lsb + shift_up;
328 if (shift_up != 0) {
329 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
330 }
331 if (shift_down != 0) {
332 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
333 }
334 } else {
335 sbfx(dst, src1, lsb, width, cond);
336 }
337}
338
339
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100340void MacroAssembler::Bfi(Register dst,
341 Register src,
342 Register scratch,
343 int lsb,
344 int width,
345 Condition cond) {
346 ASSERT(0 <= lsb && lsb < 32);
347 ASSERT(0 <= width && width < 32);
348 ASSERT(lsb + width < 32);
349 ASSERT(!scratch.is(dst));
350 if (width == 0) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100351 if (!Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100352 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
353 bic(dst, dst, Operand(mask));
354 and_(scratch, src, Operand((1 << width) - 1));
355 mov(scratch, Operand(scratch, LSL, lsb));
356 orr(dst, dst, scratch);
357 } else {
358 bfi(dst, src, lsb, width, cond);
359 }
360}
361
362
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100363void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
364 ASSERT(lsb < 32);
Steve Block44f0eee2011-05-26 01:26:41 +0100365 if (!Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100366 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
367 bic(dst, dst, Operand(mask));
368 } else {
369 bfc(dst, lsb, width, cond);
370 }
371}
372
373
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100374void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
375 Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100376 if (!Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100377 ASSERT(!dst.is(pc) && !src.rm().is(pc));
378 ASSERT((satpos >= 0) && (satpos <= 31));
379
380 // These asserts are required to ensure compatibility with the ARMv7
381 // implementation.
382 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
383 ASSERT(src.rs().is(no_reg));
384
385 Label done;
386 int satval = (1 << satpos) - 1;
387
388 if (cond != al) {
389 b(NegateCondition(cond), &done); // Skip saturate if !condition.
390 }
391 if (!(src.is_reg() && dst.is(src.rm()))) {
392 mov(dst, src);
393 }
394 tst(dst, Operand(~satval));
395 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100396 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100397 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
398 bind(&done);
399 } else {
400 usat(dst, satpos, src, cond);
401 }
402}
403
404
Steve Blocka7e24c12009-10-30 11:49:00 +0000405void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
406 // Empty the const pool.
407 CheckConstPool(true, true);
408 add(pc, pc, Operand(index,
409 LSL,
Steve Block1e0659c2011-05-24 12:43:12 +0100410 Instruction::kInstrSizeLog2 - kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
412 nop(); // Jump table alignment.
413 for (int i = 0; i < targets.length(); i++) {
414 b(targets[i]);
415 }
416}
417
418
419void MacroAssembler::LoadRoot(Register destination,
420 Heap::RootListIndex index,
421 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000422 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000423}
424
425
Kristian Monsen25f61362010-05-21 11:50:48 +0100426void MacroAssembler::StoreRoot(Register source,
427 Heap::RootListIndex index,
428 Condition cond) {
429 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
430}
431
432
Steve Block6ded16b2010-05-10 14:33:55 +0100433void MacroAssembler::RecordWriteHelper(Register object,
Steve Block8defd9f2010-07-08 12:39:36 +0100434 Register address,
435 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +0100436 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100437 // Check that the object is not in new space.
438 Label not_in_new_space;
Steve Block8defd9f2010-07-08 12:39:36 +0100439 InNewSpace(object, scratch, ne, &not_in_new_space);
Steve Block6ded16b2010-05-10 14:33:55 +0100440 Abort("new-space object passed to RecordWriteHelper");
441 bind(&not_in_new_space);
442 }
Leon Clarke4515c472010-02-03 11:58:03 +0000443
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100444 // Calculate page address.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100445 Bfc(object, 0, kPageSizeBits);
446
447 // Calculate region number.
Steve Block8defd9f2010-07-08 12:39:36 +0100448 Ubfx(address, address, Page::kRegionSizeLog2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100449 kPageSizeBits - Page::kRegionSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100451 // Mark region dirty.
Steve Block8defd9f2010-07-08 12:39:36 +0100452 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000453 mov(ip, Operand(1));
Steve Block8defd9f2010-07-08 12:39:36 +0100454 orr(scratch, scratch, Operand(ip, LSL, address));
455 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100456}
457
458
459void MacroAssembler::InNewSpace(Register object,
460 Register scratch,
Steve Block1e0659c2011-05-24 12:43:12 +0100461 Condition cond,
Steve Block6ded16b2010-05-10 14:33:55 +0100462 Label* branch) {
Steve Block1e0659c2011-05-24 12:43:12 +0100463 ASSERT(cond == eq || cond == ne);
Steve Block44f0eee2011-05-26 01:26:41 +0100464 and_(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
465 cmp(scratch, Operand(ExternalReference::new_space_start(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +0100466 b(cond, branch);
Steve Block6ded16b2010-05-10 14:33:55 +0100467}
468
469
470// Will clobber 4 registers: object, offset, scratch, ip. The
471// register 'object' contains a heap object pointer. The heap object
472// tag is shifted away.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100473void MacroAssembler::RecordWrite(Register object,
474 Operand offset,
475 Register scratch0,
476 Register scratch1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100477 // The compiled code assumes that record write doesn't change the
478 // context register, so we check that none of the clobbered
479 // registers are cp.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100480 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
Steve Block6ded16b2010-05-10 14:33:55 +0100481
482 Label done;
483
484 // First, test that the object is not in the new space. We cannot set
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100485 // region marks for new space pages.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100486 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100487
Steve Block8defd9f2010-07-08 12:39:36 +0100488 // Add offset into the object.
489 add(scratch0, object, offset);
490
Steve Block6ded16b2010-05-10 14:33:55 +0100491 // Record the actual write.
Steve Block8defd9f2010-07-08 12:39:36 +0100492 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000493
494 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000495
496 // Clobber all input registers when running with the debug-code flag
497 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100498 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100499 mov(object, Operand(BitCast<int32_t>(kZapValue)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100500 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
501 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000502 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000503}
504
505
Steve Block8defd9f2010-07-08 12:39:36 +0100506// Will clobber 4 registers: object, address, scratch, ip. The
507// register 'object' contains a heap object pointer. The heap object
508// tag is shifted away.
509void MacroAssembler::RecordWrite(Register object,
510 Register address,
511 Register scratch) {
512 // The compiled code assumes that record write doesn't change the
513 // context register, so we check that none of the clobbered
514 // registers are cp.
515 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
516
517 Label done;
518
519 // First, test that the object is not in the new space. We cannot set
520 // region marks for new space pages.
521 InNewSpace(object, scratch, eq, &done);
522
523 // Record the actual write.
524 RecordWriteHelper(object, address, scratch);
525
526 bind(&done);
527
528 // Clobber all input registers when running with the debug-code flag
529 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100530 if (emit_debug_code()) {
Steve Block8defd9f2010-07-08 12:39:36 +0100531 mov(object, Operand(BitCast<int32_t>(kZapValue)));
532 mov(address, Operand(BitCast<int32_t>(kZapValue)));
533 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
534 }
535}
536
537
Ben Murdochb0fe1622011-05-05 13:52:32 +0100538// Push and pop all registers that can hold pointers.
539void MacroAssembler::PushSafepointRegisters() {
540 // Safepoints expect a block of contiguous register values starting with r0:
541 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
542 // Safepoints expect a block of kNumSafepointRegisters values on the
543 // stack, so adjust the stack for unsaved registers.
544 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
545 ASSERT(num_unsaved >= 0);
546 sub(sp, sp, Operand(num_unsaved * kPointerSize));
547 stm(db_w, sp, kSafepointSavedRegisters);
548}
549
550
551void MacroAssembler::PopSafepointRegisters() {
552 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
553 ldm(ia_w, sp, kSafepointSavedRegisters);
554 add(sp, sp, Operand(num_unsaved * kPointerSize));
555}
556
557
Ben Murdochb8e0da22011-05-16 14:20:40 +0100558void MacroAssembler::PushSafepointRegistersAndDoubles() {
559 PushSafepointRegisters();
560 sub(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
561 kDoubleSize));
562 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
563 vstr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
564 }
565}
566
567
568void MacroAssembler::PopSafepointRegistersAndDoubles() {
569 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
570 vldr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
571 }
572 add(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
573 kDoubleSize));
574 PopSafepointRegisters();
575}
576
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100577void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register src,
578 Register dst) {
579 str(src, SafepointRegistersAndDoublesSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100580}
581
582
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100583void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
584 str(src, SafepointRegisterSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100585}
586
587
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100588void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
589 ldr(dst, SafepointRegisterSlot(src));
Steve Block1e0659c2011-05-24 12:43:12 +0100590}
591
592
Ben Murdochb0fe1622011-05-05 13:52:32 +0100593int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
594 // The registers are pushed starting with the highest encoding,
595 // which means that lowest encodings are closest to the stack pointer.
596 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
597 return reg_code;
598}
599
600
Steve Block1e0659c2011-05-24 12:43:12 +0100601MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
602 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
603}
604
605
606MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
607 // General purpose registers are pushed last on the stack.
608 int doubles_size = DwVfpRegister::kNumAllocatableRegisters * kDoubleSize;
609 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
610 return MemOperand(sp, doubles_size + register_offset);
611}
612
613
Leon Clarkef7060e22010-06-03 12:02:55 +0100614void MacroAssembler::Ldrd(Register dst1, Register dst2,
615 const MemOperand& src, Condition cond) {
616 ASSERT(src.rm().is(no_reg));
617 ASSERT(!dst1.is(lr)); // r14.
618 ASSERT_EQ(0, dst1.code() % 2);
619 ASSERT_EQ(dst1.code() + 1, dst2.code());
620
621 // Generate two ldr instructions if ldrd is not available.
Steve Block44f0eee2011-05-26 01:26:41 +0100622 if (Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100623 CpuFeatures::Scope scope(ARMv7);
624 ldrd(dst1, dst2, src, cond);
625 } else {
626 MemOperand src2(src);
627 src2.set_offset(src2.offset() + 4);
628 if (dst1.is(src.rn())) {
629 ldr(dst2, src2, cond);
630 ldr(dst1, src, cond);
631 } else {
632 ldr(dst1, src, cond);
633 ldr(dst2, src2, cond);
634 }
635 }
636}
637
638
639void MacroAssembler::Strd(Register src1, Register src2,
640 const MemOperand& dst, Condition cond) {
641 ASSERT(dst.rm().is(no_reg));
642 ASSERT(!src1.is(lr)); // r14.
643 ASSERT_EQ(0, src1.code() % 2);
644 ASSERT_EQ(src1.code() + 1, src2.code());
645
646 // Generate two str instructions if strd is not available.
Steve Block44f0eee2011-05-26 01:26:41 +0100647 if (Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100648 CpuFeatures::Scope scope(ARMv7);
649 strd(src1, src2, dst, cond);
650 } else {
651 MemOperand dst2(dst);
652 dst2.set_offset(dst2.offset() + 4);
653 str(src1, dst, cond);
654 str(src2, dst2, cond);
655 }
656}
657
658
Ben Murdochb8e0da22011-05-16 14:20:40 +0100659void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
660 const Register scratch,
661 const Condition cond) {
662 vmrs(scratch, cond);
663 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
664 vmsr(scratch, cond);
665}
666
667
668void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
669 const DwVfpRegister src2,
670 const Condition cond) {
671 // Compare and move FPSCR flags to the normal condition flags.
672 VFPCompareAndLoadFlags(src1, src2, pc, cond);
673}
674
675void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
676 const double src2,
677 const Condition cond) {
678 // Compare and move FPSCR flags to the normal condition flags.
679 VFPCompareAndLoadFlags(src1, src2, pc, cond);
680}
681
682
683void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
684 const DwVfpRegister src2,
685 const Register fpscr_flags,
686 const Condition cond) {
687 // Compare and load FPSCR.
688 vcmp(src1, src2, cond);
689 vmrs(fpscr_flags, cond);
690}
691
692void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
693 const double src2,
694 const Register fpscr_flags,
695 const Condition cond) {
696 // Compare and load FPSCR.
697 vcmp(src1, src2, cond);
698 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100699}
700
701
Steve Blocka7e24c12009-10-30 11:49:00 +0000702void MacroAssembler::EnterFrame(StackFrame::Type type) {
703 // r0-r3: preserved
704 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
705 mov(ip, Operand(Smi::FromInt(type)));
706 push(ip);
707 mov(ip, Operand(CodeObject()));
708 push(ip);
709 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
710}
711
712
713void MacroAssembler::LeaveFrame(StackFrame::Type type) {
714 // r0: preserved
715 // r1: preserved
716 // r2: preserved
717
718 // Drop the execution stack down to the frame pointer and restore
719 // the caller frame pointer and return address.
720 mov(sp, fp);
721 ldm(ia_w, sp, fp.bit() | lr.bit());
722}
723
724
Steve Block1e0659c2011-05-24 12:43:12 +0100725void MacroAssembler::EnterExitFrame(bool save_doubles, int stack_space) {
726 // Setup the frame structure on the stack.
727 ASSERT_EQ(2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
728 ASSERT_EQ(1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
729 ASSERT_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
730 Push(lr, fp);
Andrei Popescu402d9372010-02-26 13:31:12 +0000731 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Block1e0659c2011-05-24 12:43:12 +0100732 // Reserve room for saved entry sp and code object.
733 sub(sp, sp, Operand(2 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100734 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100735 mov(ip, Operand(0));
736 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
737 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000738 mov(ip, Operand(CodeObject()));
Steve Block1e0659c2011-05-24 12:43:12 +0100739 str(ip, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000740
741 // Save the frame pointer and the context in top.
Steve Block44f0eee2011-05-26 01:26:41 +0100742 mov(ip, Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 str(fp, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +0100744 mov(ip, Operand(ExternalReference(Isolate::k_context_address, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000745 str(cp, MemOperand(ip));
746
Ben Murdochb0fe1622011-05-05 13:52:32 +0100747 // Optionally save all double registers.
748 if (save_doubles) {
Steve Block1e0659c2011-05-24 12:43:12 +0100749 sub(sp, sp, Operand(DwVfpRegister::kNumRegisters * kDoubleSize));
750 const int offset = -2 * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100751 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
752 DwVfpRegister reg = DwVfpRegister::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100753 vstr(reg, fp, offset - ((i + 1) * kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100754 }
Steve Block1e0659c2011-05-24 12:43:12 +0100755 // Note that d0 will be accessible at
756 // fp - 2 * kPointerSize - DwVfpRegister::kNumRegisters * kDoubleSize,
757 // since the sp slot and code slot were pushed after the fp.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100758 }
Steve Block1e0659c2011-05-24 12:43:12 +0100759
760 // Reserve place for the return address and stack space and align the frame
761 // preparing for calling the runtime function.
762 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
763 sub(sp, sp, Operand((stack_space + 1) * kPointerSize));
764 if (frame_alignment > 0) {
765 ASSERT(IsPowerOf2(frame_alignment));
766 and_(sp, sp, Operand(-frame_alignment));
767 }
768
769 // Set the exit frame sp value to point just before the return address
770 // location.
771 add(ip, sp, Operand(kPointerSize));
772 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000773}
774
775
Steve Block6ded16b2010-05-10 14:33:55 +0100776void MacroAssembler::InitializeNewString(Register string,
777 Register length,
778 Heap::RootListIndex map_index,
779 Register scratch1,
780 Register scratch2) {
781 mov(scratch1, Operand(length, LSL, kSmiTagSize));
782 LoadRoot(scratch2, map_index);
783 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
784 mov(scratch1, Operand(String::kEmptyHashField));
785 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
786 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
787}
788
789
790int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000791#if defined(V8_HOST_ARCH_ARM)
792 // Running on the real platform. Use the alignment as mandated by the local
793 // environment.
794 // Note: This will break if we ever start generating snapshots on one ARM
795 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100796 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000797#else // defined(V8_HOST_ARCH_ARM)
798 // If we are using the simulator then we should always align to the expected
799 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100800 // if the target platform will need alignment, so this is controlled from a
801 // flag.
802 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000803#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000804}
805
806
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100807void MacroAssembler::LeaveExitFrame(bool save_doubles,
808 Register argument_count) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100809 // Optionally restore all double registers.
810 if (save_doubles) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100811 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
812 DwVfpRegister reg = DwVfpRegister::from_code(i);
Steve Block1e0659c2011-05-24 12:43:12 +0100813 const int offset = -2 * kPointerSize;
814 vldr(reg, fp, offset - ((i + 1) * kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100815 }
816 }
817
Steve Blocka7e24c12009-10-30 11:49:00 +0000818 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100819 mov(r3, Operand(0, RelocInfo::NONE));
Steve Block44f0eee2011-05-26 01:26:41 +0100820 mov(ip, Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000821 str(r3, MemOperand(ip));
822
823 // Restore current context from top and clear it in debug mode.
Steve Block44f0eee2011-05-26 01:26:41 +0100824 mov(ip, Operand(ExternalReference(Isolate::k_context_address, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 ldr(cp, MemOperand(ip));
826#ifdef DEBUG
827 str(r3, MemOperand(ip));
828#endif
829
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100830 // Tear down the exit frame, pop the arguments, and return.
Steve Block1e0659c2011-05-24 12:43:12 +0100831 mov(sp, Operand(fp));
832 ldm(ia_w, sp, fp.bit() | lr.bit());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100833 if (argument_count.is_valid()) {
834 add(sp, sp, Operand(argument_count, LSL, kPointerSizeLog2));
835 }
836}
837
838void MacroAssembler::GetCFunctionDoubleResult(const DoubleRegister dst) {
839#if !defined(USE_ARM_EABI)
840 UNREACHABLE();
841#else
842 vmov(dst, r0, r1);
843#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000844}
845
846
847void MacroAssembler::InvokePrologue(const ParameterCount& expected,
848 const ParameterCount& actual,
849 Handle<Code> code_constant,
850 Register code_reg,
851 Label* done,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100852 InvokeFlag flag,
Steve Block44f0eee2011-05-26 01:26:41 +0100853 CallWrapper* call_wrapper) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 bool definitely_matches = false;
855 Label regular_invoke;
856
857 // Check whether the expected and actual arguments count match. If not,
858 // setup registers according to contract with ArgumentsAdaptorTrampoline:
859 // r0: actual arguments count
860 // r1: function (passed through to callee)
861 // r2: expected arguments count
862 // r3: callee code entry
863
864 // The code below is made a lot easier because the calling code already sets
865 // up actual and expected registers according to the contract if values are
866 // passed in registers.
867 ASSERT(actual.is_immediate() || actual.reg().is(r0));
868 ASSERT(expected.is_immediate() || expected.reg().is(r2));
869 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
870
871 if (expected.is_immediate()) {
872 ASSERT(actual.is_immediate());
873 if (expected.immediate() == actual.immediate()) {
874 definitely_matches = true;
875 } else {
876 mov(r0, Operand(actual.immediate()));
877 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
878 if (expected.immediate() == sentinel) {
879 // Don't worry about adapting arguments for builtins that
880 // don't want that done. Skip adaption code by making it look
881 // like we have a match between expected and actual number of
882 // arguments.
883 definitely_matches = true;
884 } else {
885 mov(r2, Operand(expected.immediate()));
886 }
887 }
888 } else {
889 if (actual.is_immediate()) {
890 cmp(expected.reg(), Operand(actual.immediate()));
891 b(eq, &regular_invoke);
892 mov(r0, Operand(actual.immediate()));
893 } else {
894 cmp(expected.reg(), Operand(actual.reg()));
895 b(eq, &regular_invoke);
896 }
897 }
898
899 if (!definitely_matches) {
900 if (!code_constant.is_null()) {
901 mov(r3, Operand(code_constant));
902 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
903 }
904
905 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +0100906 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +0000907 if (flag == CALL_FUNCTION) {
Steve Block44f0eee2011-05-26 01:26:41 +0100908 if (call_wrapper != NULL) {
909 call_wrapper->BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
910 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000911 Call(adaptor, RelocInfo::CODE_TARGET);
Steve Block44f0eee2011-05-26 01:26:41 +0100912 if (call_wrapper != NULL) call_wrapper->AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +0000913 b(done);
914 } else {
915 Jump(adaptor, RelocInfo::CODE_TARGET);
916 }
917 bind(&regular_invoke);
918 }
919}
920
921
922void MacroAssembler::InvokeCode(Register code,
923 const ParameterCount& expected,
924 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100925 InvokeFlag flag,
Steve Block44f0eee2011-05-26 01:26:41 +0100926 CallWrapper* call_wrapper) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000927 Label done;
928
Ben Murdochb8e0da22011-05-16 14:20:40 +0100929 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
Steve Block44f0eee2011-05-26 01:26:41 +0100930 call_wrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 if (flag == CALL_FUNCTION) {
Steve Block44f0eee2011-05-26 01:26:41 +0100932 if (call_wrapper != NULL) call_wrapper->BeforeCall(CallSize(code));
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 Call(code);
Steve Block44f0eee2011-05-26 01:26:41 +0100934 if (call_wrapper != NULL) call_wrapper->AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +0000935 } else {
936 ASSERT(flag == JUMP_FUNCTION);
937 Jump(code);
938 }
939
940 // Continue here if InvokePrologue does handle the invocation due to
941 // mismatched parameter counts.
942 bind(&done);
943}
944
945
946void MacroAssembler::InvokeCode(Handle<Code> code,
947 const ParameterCount& expected,
948 const ParameterCount& actual,
949 RelocInfo::Mode rmode,
950 InvokeFlag flag) {
951 Label done;
952
953 InvokePrologue(expected, actual, code, no_reg, &done, flag);
954 if (flag == CALL_FUNCTION) {
955 Call(code, rmode);
956 } else {
957 Jump(code, rmode);
958 }
959
960 // Continue here if InvokePrologue does handle the invocation due to
961 // mismatched parameter counts.
962 bind(&done);
963}
964
965
966void MacroAssembler::InvokeFunction(Register fun,
967 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100968 InvokeFlag flag,
Steve Block44f0eee2011-05-26 01:26:41 +0100969 CallWrapper* call_wrapper) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000970 // Contract with called JS functions requires that function is passed in r1.
971 ASSERT(fun.is(r1));
972
973 Register expected_reg = r2;
974 Register code_reg = r3;
975
976 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
977 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
978 ldr(expected_reg,
979 FieldMemOperand(code_reg,
980 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100981 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000982 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +0100983 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000984
985 ParameterCount expected(expected_reg);
Steve Block44f0eee2011-05-26 01:26:41 +0100986 InvokeCode(code_reg, expected, actual, flag, call_wrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +0000987}
988
989
Andrei Popescu402d9372010-02-26 13:31:12 +0000990void MacroAssembler::InvokeFunction(JSFunction* function,
991 const ParameterCount& actual,
992 InvokeFlag flag) {
993 ASSERT(function->is_compiled());
994
995 // Get the function and setup the context.
996 mov(r1, Operand(Handle<JSFunction>(function)));
997 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
998
999 // Invoke the cached code.
1000 Handle<Code> code(function->code());
1001 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001002 if (V8::UseCrankshaft()) {
1003 // TODO(kasperl): For now, we always call indirectly through the
1004 // code field in the function to allow recompilation to take effect
1005 // without changing any of the call sites.
1006 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1007 InvokeCode(r3, expected, actual, flag);
1008 } else {
1009 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
1010 }
1011}
1012
1013
1014void MacroAssembler::IsObjectJSObjectType(Register heap_object,
1015 Register map,
1016 Register scratch,
1017 Label* fail) {
1018 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
1019 IsInstanceJSObjectType(map, scratch, fail);
1020}
1021
1022
1023void MacroAssembler::IsInstanceJSObjectType(Register map,
1024 Register scratch,
1025 Label* fail) {
1026 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
1027 cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1028 b(lt, fail);
1029 cmp(scratch, Operand(LAST_JS_OBJECT_TYPE));
1030 b(gt, fail);
1031}
1032
1033
1034void MacroAssembler::IsObjectJSStringType(Register object,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001035 Register scratch,
1036 Label* fail) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001037 ASSERT(kNotStringTag != 0);
1038
1039 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
1040 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1041 tst(scratch, Operand(kIsNotStringMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001042 b(ne, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +00001043}
1044
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001045
Steve Blocka7e24c12009-10-30 11:49:00 +00001046#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +00001047void MacroAssembler::DebugBreak() {
1048 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +01001049 mov(r0, Operand(0, RelocInfo::NONE));
Steve Block44f0eee2011-05-26 01:26:41 +01001050 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +00001051 CEntryStub ces(1);
1052 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
1053}
Steve Blocka7e24c12009-10-30 11:49:00 +00001054#endif
1055
1056
1057void MacroAssembler::PushTryHandler(CodeLocation try_location,
1058 HandlerType type) {
1059 // Adjust this code if not the case.
1060 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1061 // The pc (return address) is passed in register lr.
1062 if (try_location == IN_JAVASCRIPT) {
1063 if (type == TRY_CATCH_HANDLER) {
1064 mov(r3, Operand(StackHandler::TRY_CATCH));
1065 } else {
1066 mov(r3, Operand(StackHandler::TRY_FINALLY));
1067 }
1068 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
1069 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
1070 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1071 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
1072 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +01001073 mov(r3, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 ldr(r1, MemOperand(r3));
1075 ASSERT(StackHandlerConstants::kNextOffset == 0);
1076 push(r1);
1077 // Link this handler as the new current one.
1078 str(sp, MemOperand(r3));
1079 } else {
1080 // Must preserve r0-r4, r5-r7 are available.
1081 ASSERT(try_location == IN_JS_ENTRY);
1082 // The frame pointer does not point to a JS frame so we save NULL
1083 // for fp. We expect the code throwing an exception to check fp
1084 // before dereferencing it to restore the context.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001085 mov(ip, Operand(0, RelocInfo::NONE)); // To save a NULL frame pointer.
Steve Blocka7e24c12009-10-30 11:49:00 +00001086 mov(r6, Operand(StackHandler::ENTRY));
1087 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
1088 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
1089 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1090 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
1091 // Save the current handler as the next handler.
Steve Block44f0eee2011-05-26 01:26:41 +01001092 mov(r7, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +00001093 ldr(r6, MemOperand(r7));
1094 ASSERT(StackHandlerConstants::kNextOffset == 0);
1095 push(r6);
1096 // Link this handler as the new current one.
1097 str(sp, MemOperand(r7));
1098 }
1099}
1100
1101
Leon Clarkee46be812010-01-19 14:06:41 +00001102void MacroAssembler::PopTryHandler() {
1103 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
1104 pop(r1);
Steve Block44f0eee2011-05-26 01:26:41 +01001105 mov(ip, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +00001106 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1107 str(r1, MemOperand(ip));
1108}
1109
1110
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001111void MacroAssembler::Throw(Register value) {
1112 // r0 is expected to hold the exception.
1113 if (!value.is(r0)) {
1114 mov(r0, value);
1115 }
1116
1117 // Adjust this code if not the case.
1118 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1119
1120 // Drop the sp to the top of the handler.
Steve Block44f0eee2011-05-26 01:26:41 +01001121 mov(r3, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001122 ldr(sp, MemOperand(r3));
1123
1124 // Restore the next handler and frame pointer, discard handler state.
1125 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1126 pop(r2);
1127 str(r2, MemOperand(r3));
1128 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
1129 ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
1130
1131 // Before returning we restore the context from the frame pointer if
1132 // not NULL. The frame pointer is NULL in the exception handler of a
1133 // JS entry frame.
1134 cmp(fp, Operand(0, RelocInfo::NONE));
1135 // Set cp to NULL if fp is NULL.
1136 mov(cp, Operand(0, RelocInfo::NONE), LeaveCC, eq);
1137 // Restore cp otherwise.
1138 ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1139#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +01001140 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001141 mov(lr, Operand(pc));
1142 }
1143#endif
1144 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1145 pop(pc);
1146}
1147
1148
1149void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
1150 Register value) {
1151 // Adjust this code if not the case.
1152 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
1153
1154 // r0 is expected to hold the exception.
1155 if (!value.is(r0)) {
1156 mov(r0, value);
1157 }
1158
1159 // Drop sp to the top stack handler.
Steve Block44f0eee2011-05-26 01:26:41 +01001160 mov(r3, Operand(ExternalReference(Isolate::k_handler_address, isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001161 ldr(sp, MemOperand(r3));
1162
1163 // Unwind the handlers until the ENTRY handler is found.
1164 Label loop, done;
1165 bind(&loop);
1166 // Load the type of the current stack handler.
1167 const int kStateOffset = StackHandlerConstants::kStateOffset;
1168 ldr(r2, MemOperand(sp, kStateOffset));
1169 cmp(r2, Operand(StackHandler::ENTRY));
1170 b(eq, &done);
1171 // Fetch the next handler in the list.
1172 const int kNextOffset = StackHandlerConstants::kNextOffset;
1173 ldr(sp, MemOperand(sp, kNextOffset));
1174 jmp(&loop);
1175 bind(&done);
1176
1177 // Set the top handler address to next handler past the current ENTRY handler.
1178 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1179 pop(r2);
1180 str(r2, MemOperand(r3));
1181
1182 if (type == OUT_OF_MEMORY) {
1183 // Set external caught exception to false.
Steve Block44f0eee2011-05-26 01:26:41 +01001184 ExternalReference external_caught(
1185 Isolate::k_external_caught_exception_address, isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001186 mov(r0, Operand(false, RelocInfo::NONE));
1187 mov(r2, Operand(external_caught));
1188 str(r0, MemOperand(r2));
1189
1190 // Set pending exception and r0 to out of memory exception.
1191 Failure* out_of_memory = Failure::OutOfMemoryException();
1192 mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
Steve Block44f0eee2011-05-26 01:26:41 +01001193 mov(r2, Operand(ExternalReference(Isolate::k_pending_exception_address,
1194 isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001195 str(r0, MemOperand(r2));
1196 }
1197
1198 // Stack layout at this point. See also StackHandlerConstants.
1199 // sp -> state (ENTRY)
1200 // fp
1201 // lr
1202
1203 // Discard handler state (r2 is not used) and restore frame pointer.
1204 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
1205 ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
1206 // Before returning we restore the context from the frame pointer if
1207 // not NULL. The frame pointer is NULL in the exception handler of a
1208 // JS entry frame.
1209 cmp(fp, Operand(0, RelocInfo::NONE));
1210 // Set cp to NULL if fp is NULL.
1211 mov(cp, Operand(0, RelocInfo::NONE), LeaveCC, eq);
1212 // Restore cp otherwise.
1213 ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1214#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +01001215 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001216 mov(lr, Operand(pc));
1217 }
1218#endif
1219 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
1220 pop(pc);
1221}
1222
1223
Steve Blocka7e24c12009-10-30 11:49:00 +00001224void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1225 Register scratch,
1226 Label* miss) {
1227 Label same_contexts;
1228
1229 ASSERT(!holder_reg.is(scratch));
1230 ASSERT(!holder_reg.is(ip));
1231 ASSERT(!scratch.is(ip));
1232
1233 // Load current lexical context from the stack frame.
1234 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1235 // In debug mode, make sure the lexical context is set.
1236#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001237 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001238 Check(ne, "we should not have an empty lexical context");
1239#endif
1240
1241 // Load the global context of the current context.
1242 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1243 ldr(scratch, FieldMemOperand(scratch, offset));
1244 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1245
1246 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001247 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001248 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1249 // Cannot use ip as a temporary in this verification code. Due to the fact
1250 // that ip is clobbered as part of cmp with an object Operand.
1251 push(holder_reg); // Temporarily save holder on the stack.
1252 // Read the first word and compare to the global_context_map.
1253 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1254 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1255 cmp(holder_reg, ip);
1256 Check(eq, "JSGlobalObject::global_context should be a global context.");
1257 pop(holder_reg); // Restore holder.
1258 }
1259
1260 // Check if both contexts are the same.
1261 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1262 cmp(scratch, Operand(ip));
1263 b(eq, &same_contexts);
1264
1265 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001266 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1268 // Cannot use ip as a temporary in this verification code. Due to the fact
1269 // that ip is clobbered as part of cmp with an object Operand.
1270 push(holder_reg); // Temporarily save holder on the stack.
1271 mov(holder_reg, ip); // Move ip to its holding place.
1272 LoadRoot(ip, Heap::kNullValueRootIndex);
1273 cmp(holder_reg, ip);
1274 Check(ne, "JSGlobalProxy::context() should not be null.");
1275
1276 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1277 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1278 cmp(holder_reg, ip);
1279 Check(eq, "JSGlobalObject::global_context should be a global context.");
1280 // Restore ip is not needed. ip is reloaded below.
1281 pop(holder_reg); // Restore holder.
1282 // Restore ip to holder's context.
1283 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1284 }
1285
1286 // Check that the security token in the calling global object is
1287 // compatible with the security token in the receiving global
1288 // object.
1289 int token_offset = Context::kHeaderSize +
1290 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1291
1292 ldr(scratch, FieldMemOperand(scratch, token_offset));
1293 ldr(ip, FieldMemOperand(ip, token_offset));
1294 cmp(scratch, Operand(ip));
1295 b(ne, miss);
1296
1297 bind(&same_contexts);
1298}
1299
1300
1301void MacroAssembler::AllocateInNewSpace(int object_size,
1302 Register result,
1303 Register scratch1,
1304 Register scratch2,
1305 Label* gc_required,
1306 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001307 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001308 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001309 // Trash the registers to simulate an allocation failure.
1310 mov(result, Operand(0x7091));
1311 mov(scratch1, Operand(0x7191));
1312 mov(scratch2, Operand(0x7291));
1313 }
1314 jmp(gc_required);
1315 return;
1316 }
1317
Steve Blocka7e24c12009-10-30 11:49:00 +00001318 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001319 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001320 ASSERT(!scratch1.is(scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001321 ASSERT(!scratch1.is(ip));
1322 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001323
Kristian Monsen25f61362010-05-21 11:50:48 +01001324 // Make object size into bytes.
1325 if ((flags & SIZE_IN_WORDS) != 0) {
1326 object_size *= kPointerSize;
1327 }
1328 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1329
Ben Murdochb0fe1622011-05-05 13:52:32 +01001330 // Check relative positions of allocation top and limit addresses.
1331 // The values must be adjacent in memory to allow the use of LDM.
1332 // Also, assert that the registers are numbered such that the values
1333 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001334 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001335 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001336 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001337 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001338 intptr_t top =
1339 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1340 intptr_t limit =
1341 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1342 ASSERT((limit - top) == kPointerSize);
1343 ASSERT(result.code() < ip.code());
1344
1345 // Set up allocation top address and object size registers.
1346 Register topaddr = scratch1;
1347 Register obj_size_reg = scratch2;
1348 mov(topaddr, Operand(new_space_allocation_top));
1349 mov(obj_size_reg, Operand(object_size));
1350
1351 // This code stores a temporary value in ip. This is OK, as the code below
1352 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001353 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001354 // Load allocation top into result and allocation limit into ip.
1355 ldm(ia, topaddr, result.bit() | ip.bit());
1356 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001357 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001358 // Assert that result actually contains top on entry. ip is used
1359 // immediately below so this use of ip does not cause difference with
1360 // respect to register content between debug and release mode.
1361 ldr(ip, MemOperand(topaddr));
1362 cmp(result, ip);
1363 Check(eq, "Unexpected allocation top");
1364 }
1365 // Load allocation limit into ip. Result already contains allocation top.
1366 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001367 }
1368
1369 // Calculate new top and bail out if new space is exhausted. Use result
1370 // to calculate the new top.
Steve Block1e0659c2011-05-24 12:43:12 +01001371 add(scratch2, result, Operand(obj_size_reg), SetCC);
1372 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001373 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001374 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001375 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001376
Ben Murdochb0fe1622011-05-05 13:52:32 +01001377 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001379 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001380 }
1381}
1382
1383
1384void MacroAssembler::AllocateInNewSpace(Register object_size,
1385 Register result,
1386 Register scratch1,
1387 Register scratch2,
1388 Label* gc_required,
1389 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001390 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001391 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001392 // Trash the registers to simulate an allocation failure.
1393 mov(result, Operand(0x7091));
1394 mov(scratch1, Operand(0x7191));
1395 mov(scratch2, Operand(0x7291));
1396 }
1397 jmp(gc_required);
1398 return;
1399 }
1400
Ben Murdochb0fe1622011-05-05 13:52:32 +01001401 // Assert that the register arguments are different and that none of
1402 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001404 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001405 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001406 ASSERT(!result.is(ip));
1407 ASSERT(!scratch1.is(ip));
1408 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001409
Ben Murdochb0fe1622011-05-05 13:52:32 +01001410 // Check relative positions of allocation top and limit addresses.
1411 // The values must be adjacent in memory to allow the use of LDM.
1412 // Also, assert that the registers are numbered such that the values
1413 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001414 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001415 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001416 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001417 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001418 intptr_t top =
1419 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1420 intptr_t limit =
1421 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1422 ASSERT((limit - top) == kPointerSize);
1423 ASSERT(result.code() < ip.code());
1424
1425 // Set up allocation top address.
1426 Register topaddr = scratch1;
1427 mov(topaddr, Operand(new_space_allocation_top));
1428
1429 // This code stores a temporary value in ip. This is OK, as the code below
1430 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001431 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001432 // Load allocation top into result and allocation limit into ip.
1433 ldm(ia, topaddr, result.bit() | ip.bit());
1434 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001435 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001436 // Assert that result actually contains top on entry. ip is used
1437 // immediately below so this use of ip does not cause difference with
1438 // respect to register content between debug and release mode.
1439 ldr(ip, MemOperand(topaddr));
1440 cmp(result, ip);
1441 Check(eq, "Unexpected allocation top");
1442 }
1443 // Load allocation limit into ip. Result already contains allocation top.
1444 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001445 }
1446
1447 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001448 // to calculate the new top. Object size may be in words so a shift is
1449 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001450 if ((flags & SIZE_IN_WORDS) != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001451 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001452 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001453 add(scratch2, result, Operand(object_size), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001454 }
Steve Block1e0659c2011-05-24 12:43:12 +01001455 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001456 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001457 b(hi, gc_required);
1458
Steve Blockd0582a62009-12-15 09:54:21 +00001459 // Update allocation top. result temporarily holds the new top.
Steve Block44f0eee2011-05-26 01:26:41 +01001460 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001461 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001462 Check(eq, "Unaligned allocation in new space");
1463 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001464 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001465
1466 // Tag object if requested.
1467 if ((flags & TAG_OBJECT) != 0) {
1468 add(result, result, Operand(kHeapObjectTag));
1469 }
1470}
1471
1472
1473void MacroAssembler::UndoAllocationInNewSpace(Register object,
1474 Register scratch) {
1475 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001476 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001477
1478 // Make sure the object has no tag before resetting top.
1479 and_(object, object, Operand(~kHeapObjectTagMask));
1480#ifdef DEBUG
1481 // Check that the object un-allocated is below the current top.
1482 mov(scratch, Operand(new_space_allocation_top));
1483 ldr(scratch, MemOperand(scratch));
1484 cmp(object, scratch);
1485 Check(lt, "Undo allocation of non allocated memory");
1486#endif
1487 // Write the address of the object to un-allocate as the current top.
1488 mov(scratch, Operand(new_space_allocation_top));
1489 str(object, MemOperand(scratch));
1490}
1491
1492
Andrei Popescu31002712010-02-23 13:46:05 +00001493void MacroAssembler::AllocateTwoByteString(Register result,
1494 Register length,
1495 Register scratch1,
1496 Register scratch2,
1497 Register scratch3,
1498 Label* gc_required) {
1499 // Calculate the number of bytes needed for the characters in the string while
1500 // observing object alignment.
1501 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1502 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1503 add(scratch1, scratch1,
1504 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001505 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001506
1507 // Allocate two-byte string in new space.
1508 AllocateInNewSpace(scratch1,
1509 result,
1510 scratch2,
1511 scratch3,
1512 gc_required,
1513 TAG_OBJECT);
1514
1515 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001516 InitializeNewString(result,
1517 length,
1518 Heap::kStringMapRootIndex,
1519 scratch1,
1520 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001521}
1522
1523
1524void MacroAssembler::AllocateAsciiString(Register result,
1525 Register length,
1526 Register scratch1,
1527 Register scratch2,
1528 Register scratch3,
1529 Label* gc_required) {
1530 // Calculate the number of bytes needed for the characters in the string while
1531 // observing object alignment.
1532 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1533 ASSERT(kCharSize == 1);
1534 add(scratch1, length,
1535 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001536 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001537
1538 // Allocate ASCII string in new space.
1539 AllocateInNewSpace(scratch1,
1540 result,
1541 scratch2,
1542 scratch3,
1543 gc_required,
1544 TAG_OBJECT);
1545
1546 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001547 InitializeNewString(result,
1548 length,
1549 Heap::kAsciiStringMapRootIndex,
1550 scratch1,
1551 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001552}
1553
1554
1555void MacroAssembler::AllocateTwoByteConsString(Register result,
1556 Register length,
1557 Register scratch1,
1558 Register scratch2,
1559 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001560 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001561 result,
1562 scratch1,
1563 scratch2,
1564 gc_required,
1565 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001566
1567 InitializeNewString(result,
1568 length,
1569 Heap::kConsStringMapRootIndex,
1570 scratch1,
1571 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001572}
1573
1574
1575void MacroAssembler::AllocateAsciiConsString(Register result,
1576 Register length,
1577 Register scratch1,
1578 Register scratch2,
1579 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001580 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001581 result,
1582 scratch1,
1583 scratch2,
1584 gc_required,
1585 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001586
1587 InitializeNewString(result,
1588 length,
1589 Heap::kConsAsciiStringMapRootIndex,
1590 scratch1,
1591 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001592}
1593
1594
Steve Block6ded16b2010-05-10 14:33:55 +01001595void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001596 Register map,
1597 Register type_reg,
1598 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001599 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001600 CompareInstanceType(map, type_reg, type);
1601}
1602
1603
1604void MacroAssembler::CompareInstanceType(Register map,
1605 Register type_reg,
1606 InstanceType type) {
1607 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1608 cmp(type_reg, Operand(type));
1609}
1610
1611
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001612void MacroAssembler::CompareRoot(Register obj,
1613 Heap::RootListIndex index) {
1614 ASSERT(!obj.is(ip));
1615 LoadRoot(ip, index);
1616 cmp(obj, ip);
1617}
1618
1619
Andrei Popescu31002712010-02-23 13:46:05 +00001620void MacroAssembler::CheckMap(Register obj,
1621 Register scratch,
1622 Handle<Map> map,
1623 Label* fail,
1624 bool is_heap_object) {
1625 if (!is_heap_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001626 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +00001627 }
1628 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1629 mov(ip, Operand(map));
1630 cmp(scratch, ip);
1631 b(ne, fail);
1632}
1633
1634
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001635void MacroAssembler::CheckMap(Register obj,
1636 Register scratch,
1637 Heap::RootListIndex index,
1638 Label* fail,
1639 bool is_heap_object) {
1640 if (!is_heap_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001641 JumpIfSmi(obj, fail);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001642 }
1643 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1644 LoadRoot(ip, index);
1645 cmp(scratch, ip);
1646 b(ne, fail);
1647}
1648
1649
Steve Blocka7e24c12009-10-30 11:49:00 +00001650void MacroAssembler::TryGetFunctionPrototype(Register function,
1651 Register result,
1652 Register scratch,
1653 Label* miss) {
1654 // Check that the receiver isn't a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01001655 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001656
1657 // Check that the function really is a function. Load map into result reg.
1658 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1659 b(ne, miss);
1660
1661 // Make sure that the function has an instance prototype.
1662 Label non_instance;
1663 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1664 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1665 b(ne, &non_instance);
1666
1667 // Get the prototype or initial map from the function.
1668 ldr(result,
1669 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1670
1671 // If the prototype or initial map is the hole, don't return it and
1672 // simply miss the cache instead. This will allow us to allocate a
1673 // prototype object on-demand in the runtime system.
1674 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1675 cmp(result, ip);
1676 b(eq, miss);
1677
1678 // If the function does not have an initial map, we're done.
1679 Label done;
1680 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1681 b(ne, &done);
1682
1683 // Get the prototype from the initial map.
1684 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1685 jmp(&done);
1686
1687 // Non-instance prototype: Fetch prototype from constructor field
1688 // in initial map.
1689 bind(&non_instance);
1690 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1691
1692 // All done.
1693 bind(&done);
1694}
1695
1696
1697void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
Steve Block1e0659c2011-05-24 12:43:12 +01001698 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00001699 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1700}
1701
1702
Andrei Popescu31002712010-02-23 13:46:05 +00001703void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
Steve Block1e0659c2011-05-24 12:43:12 +01001704 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Andrei Popescu31002712010-02-23 13:46:05 +00001705 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1706}
1707
1708
Steve Block1e0659c2011-05-24 12:43:12 +01001709MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub, Condition cond) {
1710 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
1711 Object* result;
1712 { MaybeObject* maybe_result = stub->TryGetCode();
1713 if (!maybe_result->ToObject(&result)) return maybe_result;
1714 }
1715 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1716 return result;
1717}
1718
1719
1720static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
1721 return ref0.address() - ref1.address();
1722}
1723
1724
1725MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001726 ExternalReference function, int stack_space) {
Steve Block1e0659c2011-05-24 12:43:12 +01001727 ExternalReference next_address =
1728 ExternalReference::handle_scope_next_address();
1729 const int kNextOffset = 0;
1730 const int kLimitOffset = AddressOffset(
1731 ExternalReference::handle_scope_limit_address(),
1732 next_address);
1733 const int kLevelOffset = AddressOffset(
1734 ExternalReference::handle_scope_level_address(),
1735 next_address);
1736
1737 // Allocate HandleScope in callee-save registers.
1738 mov(r7, Operand(next_address));
1739 ldr(r4, MemOperand(r7, kNextOffset));
1740 ldr(r5, MemOperand(r7, kLimitOffset));
1741 ldr(r6, MemOperand(r7, kLevelOffset));
1742 add(r6, r6, Operand(1));
1743 str(r6, MemOperand(r7, kLevelOffset));
1744
1745 // Native call returns to the DirectCEntry stub which redirects to the
1746 // return address pushed on stack (could have moved after GC).
1747 // DirectCEntry stub itself is generated early and never moves.
1748 DirectCEntryStub stub;
1749 stub.GenerateCall(this, function);
1750
1751 Label promote_scheduled_exception;
1752 Label delete_allocated_handles;
1753 Label leave_exit_frame;
1754
1755 // If result is non-zero, dereference to get the result value
1756 // otherwise set it to undefined.
1757 cmp(r0, Operand(0));
1758 LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1759 ldr(r0, MemOperand(r0), ne);
1760
1761 // No more valid handles (the result handle was the last one). Restore
1762 // previous handle scope.
1763 str(r4, MemOperand(r7, kNextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001764 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001765 ldr(r1, MemOperand(r7, kLevelOffset));
1766 cmp(r1, r6);
1767 Check(eq, "Unexpected level after return from api call");
1768 }
1769 sub(r6, r6, Operand(1));
1770 str(r6, MemOperand(r7, kLevelOffset));
1771 ldr(ip, MemOperand(r7, kLimitOffset));
1772 cmp(r5, ip);
1773 b(ne, &delete_allocated_handles);
1774
1775 // Check if the function scheduled an exception.
1776 bind(&leave_exit_frame);
1777 LoadRoot(r4, Heap::kTheHoleValueRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01001778 mov(ip, Operand(ExternalReference::scheduled_exception_address(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +01001779 ldr(r5, MemOperand(ip));
1780 cmp(r4, r5);
1781 b(ne, &promote_scheduled_exception);
1782
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001783 // LeaveExitFrame expects unwind space to be in a register.
Steve Block1e0659c2011-05-24 12:43:12 +01001784 mov(r4, Operand(stack_space));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001785 LeaveExitFrame(false, r4);
1786 mov(pc, lr);
Steve Block1e0659c2011-05-24 12:43:12 +01001787
1788 bind(&promote_scheduled_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01001789 MaybeObject* result
1790 = TryTailCallExternalReference(
1791 ExternalReference(Runtime::kPromoteScheduledException, isolate()),
1792 0,
1793 1);
Steve Block1e0659c2011-05-24 12:43:12 +01001794 if (result->IsFailure()) {
1795 return result;
1796 }
1797
1798 // HandleScope limit has changed. Delete allocated extensions.
1799 bind(&delete_allocated_handles);
1800 str(r5, MemOperand(r7, kLimitOffset));
1801 mov(r4, r0);
1802 PrepareCallCFunction(0, r5);
Steve Block44f0eee2011-05-26 01:26:41 +01001803 CallCFunction(
1804 ExternalReference::delete_handle_scope_extensions(isolate()), 0);
Steve Block1e0659c2011-05-24 12:43:12 +01001805 mov(r0, r4);
1806 jmp(&leave_exit_frame);
1807
1808 return result;
1809}
1810
1811
Steve Blocka7e24c12009-10-30 11:49:00 +00001812void MacroAssembler::IllegalOperation(int num_arguments) {
1813 if (num_arguments > 0) {
1814 add(sp, sp, Operand(num_arguments * kPointerSize));
1815 }
1816 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1817}
1818
1819
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001820void MacroAssembler::IndexFromHash(Register hash, Register index) {
1821 // If the hash field contains an array index pick it out. The assert checks
1822 // that the constants for the maximum number of digits for an array index
1823 // cached in the hash field and the number of bits reserved for it does not
1824 // conflict.
1825 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1826 (1 << String::kArrayIndexValueBits));
1827 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1828 // the low kHashShift bits.
1829 STATIC_ASSERT(kSmiTag == 0);
1830 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
1831 mov(index, Operand(hash, LSL, kSmiTagSize));
1832}
1833
1834
Steve Blockd0582a62009-12-15 09:54:21 +00001835void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1836 Register outHighReg,
1837 Register outLowReg) {
1838 // ARMv7 VFP3 instructions to implement integer to double conversion.
1839 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001840 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01001841 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00001842 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001843}
1844
1845
Steve Block8defd9f2010-07-08 12:39:36 +01001846void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1847 DwVfpRegister result,
1848 Register scratch1,
1849 Register scratch2,
1850 Register heap_number_map,
1851 SwVfpRegister scratch3,
1852 Label* not_number,
1853 ObjectToDoubleFlags flags) {
1854 Label done;
1855 if ((flags & OBJECT_NOT_SMI) == 0) {
1856 Label not_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01001857 JumpIfNotSmi(object, &not_smi);
Steve Block8defd9f2010-07-08 12:39:36 +01001858 // Remove smi tag and convert to double.
1859 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1860 vmov(scratch3, scratch1);
1861 vcvt_f64_s32(result, scratch3);
1862 b(&done);
1863 bind(&not_smi);
1864 }
1865 // Check for heap number and load double value from it.
1866 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1867 sub(scratch2, object, Operand(kHeapObjectTag));
1868 cmp(scratch1, heap_number_map);
1869 b(ne, not_number);
1870 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1871 // If exponent is all ones the number is either a NaN or +/-Infinity.
1872 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1873 Sbfx(scratch1,
1874 scratch1,
1875 HeapNumber::kExponentShift,
1876 HeapNumber::kExponentBits);
1877 // All-one value sign extend to -1.
1878 cmp(scratch1, Operand(-1));
1879 b(eq, not_number);
1880 }
1881 vldr(result, scratch2, HeapNumber::kValueOffset);
1882 bind(&done);
1883}
1884
1885
1886void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1887 DwVfpRegister value,
1888 Register scratch1,
1889 SwVfpRegister scratch2) {
1890 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1891 vmov(scratch2, scratch1);
1892 vcvt_f64_s32(value, scratch2);
1893}
1894
1895
Iain Merrick9ac36c92010-09-13 15:29:50 +01001896// Tries to get a signed int32 out of a double precision floating point heap
1897// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
1898// 32bits signed integer range.
1899void MacroAssembler::ConvertToInt32(Register source,
1900 Register dest,
1901 Register scratch,
1902 Register scratch2,
Steve Block1e0659c2011-05-24 12:43:12 +01001903 DwVfpRegister double_scratch,
Iain Merrick9ac36c92010-09-13 15:29:50 +01001904 Label *not_int32) {
Steve Block44f0eee2011-05-26 01:26:41 +01001905 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01001906 CpuFeatures::Scope scope(VFP3);
1907 sub(scratch, source, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01001908 vldr(double_scratch, scratch, HeapNumber::kValueOffset);
1909 vcvt_s32_f64(double_scratch.low(), double_scratch);
1910 vmov(dest, double_scratch.low());
Iain Merrick9ac36c92010-09-13 15:29:50 +01001911 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
1912 // maximun (0x7fffffff) signed 32bits integer when the double is out of
1913 // range. When substracting one, the minimum signed integer becomes the
1914 // maximun signed integer.
1915 sub(scratch, dest, Operand(1));
1916 cmp(scratch, Operand(LONG_MAX - 1));
1917 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
1918 b(ge, not_int32);
1919 } else {
1920 // This code is faster for doubles that are in the ranges -0x7fffffff to
1921 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
1922 // the range of signed int32 values that are not Smis. Jumps to the label
1923 // 'not_int32' if the double isn't in the range -0x80000000.0 to
1924 // 0x80000000.0 (excluding the endpoints).
1925 Label right_exponent, done;
1926 // Get exponent word.
1927 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1928 // Get exponent alone in scratch2.
1929 Ubfx(scratch2,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001930 scratch,
1931 HeapNumber::kExponentShift,
1932 HeapNumber::kExponentBits);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001933 // Load dest with zero. We use this either for the final shift or
1934 // for the answer.
1935 mov(dest, Operand(0, RelocInfo::NONE));
1936 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1937 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1938 // the exponent that we are fastest at and also the highest exponent we can
1939 // handle here.
1940 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1941 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1942 // split it up to avoid a constant pool entry. You can't do that in general
1943 // for cmp because of the overflow flag, but we know the exponent is in the
1944 // range 0-2047 so there is no overflow.
1945 int fudge_factor = 0x400;
1946 sub(scratch2, scratch2, Operand(fudge_factor));
1947 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1948 // If we have a match of the int32-but-not-Smi exponent then skip some
1949 // logic.
1950 b(eq, &right_exponent);
1951 // If the exponent is higher than that then go to slow case. This catches
1952 // numbers that don't fit in a signed int32, infinities and NaNs.
1953 b(gt, not_int32);
1954
1955 // We know the exponent is smaller than 30 (biased). If it is less than
1956 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1957 // it rounds to zero.
1958 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1959 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1960 // Dest already has a Smi zero.
1961 b(lt, &done);
1962
1963 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1964 // get how much to shift down.
1965 rsb(dest, scratch2, Operand(30));
1966
1967 bind(&right_exponent);
1968 // Get the top bits of the mantissa.
1969 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1970 // Put back the implicit 1.
1971 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1972 // Shift up the mantissa bits to take up the space the exponent used to
1973 // take. We just orred in the implicit bit so that took care of one and
1974 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1975 // distance.
1976 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1977 mov(scratch2, Operand(scratch2, LSL, shift_distance));
1978 // Put sign in zero flag.
1979 tst(scratch, Operand(HeapNumber::kSignMask));
1980 // Get the second half of the double. For some exponents we don't
1981 // actually need this because the bits get shifted out again, but
1982 // it's probably slower to test than just to do it.
1983 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1984 // Shift down 22 bits to get the last 10 bits.
1985 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1986 // Move down according to the exponent.
1987 mov(dest, Operand(scratch, LSR, dest));
1988 // Fix sign if sign bit was set.
1989 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1990 bind(&done);
1991 }
1992}
1993
1994
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001995void MacroAssembler::EmitVFPTruncate(VFPRoundingMode rounding_mode,
1996 SwVfpRegister result,
1997 DwVfpRegister double_input,
1998 Register scratch1,
1999 Register scratch2,
2000 CheckForInexactConversion check_inexact) {
Steve Block44f0eee2011-05-26 01:26:41 +01002001 ASSERT(Isolate::Current()->cpu_features()->IsSupported(VFP3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002002 CpuFeatures::Scope scope(VFP3);
2003 Register prev_fpscr = scratch1;
2004 Register scratch = scratch2;
2005
2006 int32_t check_inexact_conversion =
2007 (check_inexact == kCheckForInexactConversion) ? kVFPInexactExceptionBit : 0;
2008
2009 // Set custom FPCSR:
2010 // - Set rounding mode.
2011 // - Clear vfp cumulative exception flags.
2012 // - Make sure Flush-to-zero mode control bit is unset.
2013 vmrs(prev_fpscr);
2014 bic(scratch,
2015 prev_fpscr,
2016 Operand(kVFPExceptionMask |
2017 check_inexact_conversion |
2018 kVFPRoundingModeMask |
2019 kVFPFlushToZeroMask));
2020 // 'Round To Nearest' is encoded by 0b00 so no bits need to be set.
2021 if (rounding_mode != kRoundToNearest) {
2022 orr(scratch, scratch, Operand(rounding_mode));
2023 }
2024 vmsr(scratch);
2025
2026 // Convert the argument to an integer.
2027 vcvt_s32_f64(result,
2028 double_input,
2029 (rounding_mode == kRoundToZero) ? kDefaultRoundToZero
2030 : kFPSCRRounding);
2031
2032 // Retrieve FPSCR.
2033 vmrs(scratch);
2034 // Restore FPSCR.
2035 vmsr(prev_fpscr);
2036 // Check for vfp exceptions.
2037 tst(scratch, Operand(kVFPExceptionMask | check_inexact_conversion));
2038}
2039
2040
Steve Block44f0eee2011-05-26 01:26:41 +01002041void MacroAssembler::EmitOutOfInt32RangeTruncate(Register result,
2042 Register input_high,
2043 Register input_low,
2044 Register scratch) {
2045 Label done, normal_exponent, restore_sign;
2046
2047 // Extract the biased exponent in result.
2048 Ubfx(result,
2049 input_high,
2050 HeapNumber::kExponentShift,
2051 HeapNumber::kExponentBits);
2052
2053 // Check for Infinity and NaNs, which should return 0.
2054 cmp(result, Operand(HeapNumber::kExponentMask));
2055 mov(result, Operand(0), LeaveCC, eq);
2056 b(eq, &done);
2057
2058 // Express exponent as delta to (number of mantissa bits + 31).
2059 sub(result,
2060 result,
2061 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31),
2062 SetCC);
2063
2064 // If the delta is strictly positive, all bits would be shifted away,
2065 // which means that we can return 0.
2066 b(le, &normal_exponent);
2067 mov(result, Operand(0));
2068 b(&done);
2069
2070 bind(&normal_exponent);
2071 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
2072 // Calculate shift.
2073 add(scratch, result, Operand(kShiftBase + HeapNumber::kMantissaBits), SetCC);
2074
2075 // Save the sign.
2076 Register sign = result;
2077 result = no_reg;
2078 and_(sign, input_high, Operand(HeapNumber::kSignMask));
2079
2080 // Set the implicit 1 before the mantissa part in input_high.
2081 orr(input_high,
2082 input_high,
2083 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
2084 // Shift the mantissa bits to the correct position.
2085 // We don't need to clear non-mantissa bits as they will be shifted away.
2086 // If they weren't, it would mean that the answer is in the 32bit range.
2087 mov(input_high, Operand(input_high, LSL, scratch));
2088
2089 // Replace the shifted bits with bits from the lower mantissa word.
2090 Label pos_shift, shift_done;
2091 rsb(scratch, scratch, Operand(32), SetCC);
2092 b(&pos_shift, ge);
2093
2094 // Negate scratch.
2095 rsb(scratch, scratch, Operand(0));
2096 mov(input_low, Operand(input_low, LSL, scratch));
2097 b(&shift_done);
2098
2099 bind(&pos_shift);
2100 mov(input_low, Operand(input_low, LSR, scratch));
2101
2102 bind(&shift_done);
2103 orr(input_high, input_high, Operand(input_low));
2104 // Restore sign if necessary.
2105 cmp(sign, Operand(0));
2106 result = sign;
2107 sign = no_reg;
2108 rsb(result, input_high, Operand(0), LeaveCC, ne);
2109 mov(result, input_high, LeaveCC, eq);
2110 bind(&done);
2111}
2112
2113
2114void MacroAssembler::EmitECMATruncate(Register result,
2115 DwVfpRegister double_input,
2116 SwVfpRegister single_scratch,
2117 Register scratch,
2118 Register input_high,
2119 Register input_low) {
2120 CpuFeatures::Scope scope(VFP3);
2121 ASSERT(!input_high.is(result));
2122 ASSERT(!input_low.is(result));
2123 ASSERT(!input_low.is(input_high));
2124 ASSERT(!scratch.is(result) &&
2125 !scratch.is(input_high) &&
2126 !scratch.is(input_low));
2127 ASSERT(!single_scratch.is(double_input.low()) &&
2128 !single_scratch.is(double_input.high()));
2129
2130 Label done;
2131
2132 // Clear cumulative exception flags.
2133 ClearFPSCRBits(kVFPExceptionMask, scratch);
2134 // Try a conversion to a signed integer.
2135 vcvt_s32_f64(single_scratch, double_input);
2136 vmov(result, single_scratch);
2137 // Retrieve he FPSCR.
2138 vmrs(scratch);
2139 // Check for overflow and NaNs.
2140 tst(scratch, Operand(kVFPOverflowExceptionBit |
2141 kVFPUnderflowExceptionBit |
2142 kVFPInvalidOpExceptionBit));
2143 // If we had no exceptions we are done.
2144 b(eq, &done);
2145
2146 // Load the double value and perform a manual truncation.
2147 vmov(input_low, input_high, double_input);
2148 EmitOutOfInt32RangeTruncate(result,
2149 input_high,
2150 input_low,
2151 scratch);
2152 bind(&done);
2153}
2154
2155
Andrei Popescu31002712010-02-23 13:46:05 +00002156void MacroAssembler::GetLeastBitsFromSmi(Register dst,
2157 Register src,
2158 int num_least_bits) {
Steve Block44f0eee2011-05-26 01:26:41 +01002159 if (Isolate::Current()->cpu_features()->IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002160 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00002161 } else {
2162 mov(dst, Operand(src, ASR, kSmiTagSize));
2163 and_(dst, dst, Operand((1 << num_least_bits) - 1));
2164 }
2165}
2166
2167
Steve Block1e0659c2011-05-24 12:43:12 +01002168void MacroAssembler::GetLeastBitsFromInt32(Register dst,
2169 Register src,
2170 int num_least_bits) {
2171 and_(dst, src, Operand((1 << num_least_bits) - 1));
2172}
2173
2174
Steve Block44f0eee2011-05-26 01:26:41 +01002175void MacroAssembler::CallRuntime(const Runtime::Function* f,
2176 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002177 // All parameters are on the stack. r0 has the return value after call.
2178
2179 // If the expected number of arguments of the runtime function is
2180 // constant, we check that the actual number of arguments match the
2181 // expectation.
2182 if (f->nargs >= 0 && f->nargs != num_arguments) {
2183 IllegalOperation(num_arguments);
2184 return;
2185 }
2186
Leon Clarke4515c472010-02-03 11:58:03 +00002187 // TODO(1236192): Most runtime routines don't need the number of
2188 // arguments passed in because it is constant. At some point we
2189 // should remove this need and make the runtime routine entry code
2190 // smarter.
2191 mov(r0, Operand(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01002192 mov(r1, Operand(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00002193 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002194 CallStub(&stub);
2195}
2196
2197
2198void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
2199 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
2200}
2201
2202
Ben Murdochb0fe1622011-05-05 13:52:32 +01002203void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01002204 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002205 mov(r0, Operand(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01002206 mov(r1, Operand(ExternalReference(function, isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002207 CEntryStub stub(1);
2208 stub.SaveDoubles();
2209 CallStub(&stub);
2210}
2211
2212
Andrei Popescu402d9372010-02-26 13:31:12 +00002213void MacroAssembler::CallExternalReference(const ExternalReference& ext,
2214 int num_arguments) {
2215 mov(r0, Operand(num_arguments));
2216 mov(r1, Operand(ext));
2217
2218 CEntryStub stub(1);
2219 CallStub(&stub);
2220}
2221
2222
Steve Block6ded16b2010-05-10 14:33:55 +01002223void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2224 int num_arguments,
2225 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002226 // TODO(1236192): Most runtime routines don't need the number of
2227 // arguments passed in because it is constant. At some point we
2228 // should remove this need and make the runtime routine entry code
2229 // smarter.
2230 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002231 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00002232}
2233
2234
Steve Block1e0659c2011-05-24 12:43:12 +01002235MaybeObject* MacroAssembler::TryTailCallExternalReference(
2236 const ExternalReference& ext, int num_arguments, int result_size) {
2237 // TODO(1236192): Most runtime routines don't need the number of
2238 // arguments passed in because it is constant. At some point we
2239 // should remove this need and make the runtime routine entry code
2240 // smarter.
2241 mov(r0, Operand(num_arguments));
2242 return TryJumpToExternalReference(ext);
2243}
2244
2245
Steve Block6ded16b2010-05-10 14:33:55 +01002246void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2247 int num_arguments,
2248 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002249 TailCallExternalReference(ExternalReference(fid, isolate()),
2250 num_arguments,
2251 result_size);
Steve Block6ded16b2010-05-10 14:33:55 +01002252}
2253
2254
2255void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002256#if defined(__thumb__)
2257 // Thumb mode builtin.
2258 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2259#endif
2260 mov(r1, Operand(builtin));
2261 CEntryStub stub(1);
2262 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2263}
2264
2265
Steve Block1e0659c2011-05-24 12:43:12 +01002266MaybeObject* MacroAssembler::TryJumpToExternalReference(
2267 const ExternalReference& builtin) {
2268#if defined(__thumb__)
2269 // Thumb mode builtin.
2270 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2271#endif
2272 mov(r1, Operand(builtin));
2273 CEntryStub stub(1);
2274 return TryTailCallStub(&stub);
2275}
2276
2277
Steve Blocka7e24c12009-10-30 11:49:00 +00002278void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdochb8e0da22011-05-16 14:20:40 +01002279 InvokeJSFlags flags,
Steve Block44f0eee2011-05-26 01:26:41 +01002280 CallWrapper* call_wrapper) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002281 GetBuiltinEntry(r2, id);
Steve Blocka7e24c12009-10-30 11:49:00 +00002282 if (flags == CALL_JS) {
Steve Block44f0eee2011-05-26 01:26:41 +01002283 if (call_wrapper != NULL) call_wrapper->BeforeCall(CallSize(r2));
Andrei Popescu402d9372010-02-26 13:31:12 +00002284 Call(r2);
Steve Block44f0eee2011-05-26 01:26:41 +01002285 if (call_wrapper != NULL) call_wrapper->AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00002286 } else {
2287 ASSERT(flags == JUMP_JS);
Andrei Popescu402d9372010-02-26 13:31:12 +00002288 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002289 }
2290}
2291
2292
Steve Block791712a2010-08-27 10:21:07 +01002293void MacroAssembler::GetBuiltinFunction(Register target,
2294 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01002295 // Load the builtins object into target register.
2296 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2297 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00002298 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01002299 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01002300 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2301}
2302
2303
2304void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2305 ASSERT(!target.is(r1));
2306 GetBuiltinFunction(r1, id);
2307 // Load the code entry point from the builtins object.
2308 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002309}
2310
2311
2312void MacroAssembler::SetCounter(StatsCounter* counter, int value,
2313 Register scratch1, Register scratch2) {
2314 if (FLAG_native_code_counters && counter->Enabled()) {
2315 mov(scratch1, Operand(value));
2316 mov(scratch2, Operand(ExternalReference(counter)));
2317 str(scratch1, MemOperand(scratch2));
2318 }
2319}
2320
2321
2322void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2323 Register scratch1, Register scratch2) {
2324 ASSERT(value > 0);
2325 if (FLAG_native_code_counters && counter->Enabled()) {
2326 mov(scratch2, Operand(ExternalReference(counter)));
2327 ldr(scratch1, MemOperand(scratch2));
2328 add(scratch1, scratch1, Operand(value));
2329 str(scratch1, MemOperand(scratch2));
2330 }
2331}
2332
2333
2334void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2335 Register scratch1, Register scratch2) {
2336 ASSERT(value > 0);
2337 if (FLAG_native_code_counters && counter->Enabled()) {
2338 mov(scratch2, Operand(ExternalReference(counter)));
2339 ldr(scratch1, MemOperand(scratch2));
2340 sub(scratch1, scratch1, Operand(value));
2341 str(scratch1, MemOperand(scratch2));
2342 }
2343}
2344
2345
Steve Block1e0659c2011-05-24 12:43:12 +01002346void MacroAssembler::Assert(Condition cond, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01002347 if (emit_debug_code())
Steve Block1e0659c2011-05-24 12:43:12 +01002348 Check(cond, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00002349}
2350
2351
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002352void MacroAssembler::AssertRegisterIsRoot(Register reg,
2353 Heap::RootListIndex index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002354 if (emit_debug_code()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002355 LoadRoot(ip, index);
2356 cmp(reg, ip);
2357 Check(eq, "Register did not match expected root");
2358 }
2359}
2360
2361
Iain Merrick75681382010-08-19 15:07:18 +01002362void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002363 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01002364 ASSERT(!elements.is(ip));
2365 Label ok;
2366 push(elements);
2367 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
2368 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2369 cmp(elements, ip);
2370 b(eq, &ok);
2371 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2372 cmp(elements, ip);
2373 b(eq, &ok);
2374 Abort("JSObject with fast elements map has slow elements");
2375 bind(&ok);
2376 pop(elements);
2377 }
2378}
2379
2380
Steve Block1e0659c2011-05-24 12:43:12 +01002381void MacroAssembler::Check(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002382 Label L;
Steve Block1e0659c2011-05-24 12:43:12 +01002383 b(cond, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00002384 Abort(msg);
2385 // will not return here
2386 bind(&L);
2387}
2388
2389
2390void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01002391 Label abort_start;
2392 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00002393 // We want to pass the msg string like a smi to avoid GC
2394 // problems, however msg is not guaranteed to be aligned
2395 // properly. Instead, we pass an aligned pointer that is
2396 // a proper v8 smi, but also pass the alignment difference
2397 // from the real pointer as a smi.
2398 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2399 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2400 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2401#ifdef DEBUG
2402 if (msg != NULL) {
2403 RecordComment("Abort message: ");
2404 RecordComment(msg);
2405 }
2406#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002407 // Disable stub call restrictions to always allow calls to abort.
Ben Murdoch086aeea2011-05-13 15:57:08 +01002408 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00002409
Steve Blocka7e24c12009-10-30 11:49:00 +00002410 mov(r0, Operand(p0));
2411 push(r0);
2412 mov(r0, Operand(Smi::FromInt(p1 - p0)));
2413 push(r0);
2414 CallRuntime(Runtime::kAbort, 2);
2415 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01002416 if (is_const_pool_blocked()) {
2417 // If the calling code cares about the exact number of
2418 // instructions generated, we insert padding here to keep the size
2419 // of the Abort macro constant.
2420 static const int kExpectedAbortInstructions = 10;
2421 int abort_instructions = InstructionsGeneratedSince(&abort_start);
2422 ASSERT(abort_instructions <= kExpectedAbortInstructions);
2423 while (abort_instructions++ < kExpectedAbortInstructions) {
2424 nop();
2425 }
2426 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002427}
2428
2429
Steve Blockd0582a62009-12-15 09:54:21 +00002430void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2431 if (context_chain_length > 0) {
2432 // Move up the chain of contexts to the context containing the slot.
2433 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
2434 // Load the function context (which is the incoming, outer context).
2435 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
2436 for (int i = 1; i < context_chain_length; i++) {
2437 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
2438 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
2439 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002440 } else {
2441 // Slot is in the current function context. Move it into the
2442 // destination register in case we store into it (the write barrier
2443 // cannot be allowed to destroy the context in esi).
2444 mov(dst, cp);
2445 }
2446
2447 // We should not have found a 'with' context by walking the context chain
2448 // (i.e., the static scope chain and runtime context chain do not agree).
2449 // A variable occurring in such a scope should have slot type LOOKUP and
2450 // not CONTEXT.
Steve Block44f0eee2011-05-26 01:26:41 +01002451 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002452 ldr(ip, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
2453 cmp(dst, ip);
2454 Check(eq, "Yo dawg, I heard you liked function contexts "
2455 "so I put function contexts in all your contexts");
Steve Blockd0582a62009-12-15 09:54:21 +00002456 }
2457}
2458
2459
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002460void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2461 // Load the global or builtins object from the current context.
2462 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2463 // Load the global context from the global or builtins object.
2464 ldr(function, FieldMemOperand(function,
2465 GlobalObject::kGlobalContextOffset));
2466 // Load the function from the global context.
2467 ldr(function, MemOperand(function, Context::SlotOffset(index)));
2468}
2469
2470
2471void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2472 Register map,
2473 Register scratch) {
2474 // Load the initial map. The global functions all have initial maps.
2475 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002476 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002477 Label ok, fail;
2478 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, false);
2479 b(&ok);
2480 bind(&fail);
2481 Abort("Global functions must have initial map");
2482 bind(&ok);
2483 }
2484}
2485
2486
Steve Block1e0659c2011-05-24 12:43:12 +01002487void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
2488 Register reg,
2489 Register scratch,
2490 Label* not_power_of_two_or_zero) {
2491 sub(scratch, reg, Operand(1), SetCC);
2492 b(mi, not_power_of_two_or_zero);
2493 tst(scratch, reg);
2494 b(ne, not_power_of_two_or_zero);
2495}
2496
2497
Steve Block44f0eee2011-05-26 01:26:41 +01002498void MacroAssembler::JumpIfNotPowerOfTwoOrZeroAndNeg(
2499 Register reg,
2500 Register scratch,
2501 Label* zero_and_neg,
2502 Label* not_power_of_two) {
2503 sub(scratch, reg, Operand(1), SetCC);
2504 b(mi, zero_and_neg);
2505 tst(scratch, reg);
2506 b(ne, not_power_of_two);
2507}
2508
2509
Andrei Popescu31002712010-02-23 13:46:05 +00002510void MacroAssembler::JumpIfNotBothSmi(Register reg1,
2511 Register reg2,
2512 Label* on_not_both_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002513 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002514 tst(reg1, Operand(kSmiTagMask));
2515 tst(reg2, Operand(kSmiTagMask), eq);
2516 b(ne, on_not_both_smi);
2517}
2518
2519
2520void MacroAssembler::JumpIfEitherSmi(Register reg1,
2521 Register reg2,
2522 Label* on_either_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002523 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002524 tst(reg1, Operand(kSmiTagMask));
2525 tst(reg2, Operand(kSmiTagMask), ne);
2526 b(eq, on_either_smi);
2527}
2528
2529
Iain Merrick75681382010-08-19 15:07:18 +01002530void MacroAssembler::AbortIfSmi(Register object) {
Steve Block1e0659c2011-05-24 12:43:12 +01002531 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +01002532 tst(object, Operand(kSmiTagMask));
2533 Assert(ne, "Operand is a smi");
2534}
2535
2536
Steve Block1e0659c2011-05-24 12:43:12 +01002537void MacroAssembler::AbortIfNotSmi(Register object) {
2538 STATIC_ASSERT(kSmiTag == 0);
2539 tst(object, Operand(kSmiTagMask));
2540 Assert(eq, "Operand is not smi");
2541}
2542
2543
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002544void MacroAssembler::AbortIfNotString(Register object) {
2545 STATIC_ASSERT(kSmiTag == 0);
2546 tst(object, Operand(kSmiTagMask));
2547 Assert(ne, "Operand is not a string");
2548 push(object);
2549 ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2550 CompareInstanceType(object, object, FIRST_NONSTRING_TYPE);
2551 pop(object);
2552 Assert(lo, "Operand is not a string");
2553}
2554
2555
2556
Steve Block1e0659c2011-05-24 12:43:12 +01002557void MacroAssembler::AbortIfNotRootValue(Register src,
2558 Heap::RootListIndex root_value_index,
2559 const char* message) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002560 CompareRoot(src, root_value_index);
Steve Block1e0659c2011-05-24 12:43:12 +01002561 Assert(eq, message);
2562}
2563
2564
2565void MacroAssembler::JumpIfNotHeapNumber(Register object,
2566 Register heap_number_map,
2567 Register scratch,
2568 Label* on_not_heap_number) {
2569 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2570 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2571 cmp(scratch, heap_number_map);
2572 b(ne, on_not_heap_number);
2573}
2574
2575
Leon Clarked91b9f72010-01-27 17:25:45 +00002576void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
2577 Register first,
2578 Register second,
2579 Register scratch1,
2580 Register scratch2,
2581 Label* failure) {
2582 // Test that both first and second are sequential ASCII strings.
2583 // Assume that they are non-smis.
2584 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
2585 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
2586 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
2587 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002588
2589 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
2590 scratch2,
2591 scratch1,
2592 scratch2,
2593 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002594}
2595
2596void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
2597 Register second,
2598 Register scratch1,
2599 Register scratch2,
2600 Label* failure) {
2601 // Check that neither is a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002602 STATIC_ASSERT(kSmiTag == 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002603 and_(scratch1, first, Operand(second));
2604 tst(scratch1, Operand(kSmiTagMask));
2605 b(eq, failure);
2606 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
2607 second,
2608 scratch1,
2609 scratch2,
2610 failure);
2611}
2612
Steve Blockd0582a62009-12-15 09:54:21 +00002613
Steve Block6ded16b2010-05-10 14:33:55 +01002614// Allocates a heap number or jumps to the need_gc label if the young space
2615// is full and a scavenge is needed.
2616void MacroAssembler::AllocateHeapNumber(Register result,
2617 Register scratch1,
2618 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002619 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01002620 Label* gc_required) {
2621 // Allocate an object in the heap for the heap number and tag it as a heap
2622 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01002623 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01002624 result,
2625 scratch1,
2626 scratch2,
2627 gc_required,
2628 TAG_OBJECT);
2629
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002630 // Store heap number map in the allocated object.
2631 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2632 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002633}
2634
2635
Steve Block8defd9f2010-07-08 12:39:36 +01002636void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2637 DwVfpRegister value,
2638 Register scratch1,
2639 Register scratch2,
2640 Register heap_number_map,
2641 Label* gc_required) {
2642 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
2643 sub(scratch1, result, Operand(kHeapObjectTag));
2644 vstr(value, scratch1, HeapNumber::kValueOffset);
2645}
2646
2647
Ben Murdochbb769b22010-08-11 14:56:33 +01002648// Copies a fixed number of fields of heap objects from src to dst.
2649void MacroAssembler::CopyFields(Register dst,
2650 Register src,
2651 RegList temps,
2652 int field_count) {
2653 // At least one bit set in the first 15 registers.
2654 ASSERT((temps & ((1 << 15) - 1)) != 0);
2655 ASSERT((temps & dst.bit()) == 0);
2656 ASSERT((temps & src.bit()) == 0);
2657 // Primitive implementation using only one temporary register.
2658
2659 Register tmp = no_reg;
2660 // Find a temp register in temps list.
2661 for (int i = 0; i < 15; i++) {
2662 if ((temps & (1 << i)) != 0) {
2663 tmp.set_code(i);
2664 break;
2665 }
2666 }
2667 ASSERT(!tmp.is(no_reg));
2668
2669 for (int i = 0; i < field_count; i++) {
2670 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
2671 str(tmp, FieldMemOperand(dst, i * kPointerSize));
2672 }
2673}
2674
2675
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002676void MacroAssembler::CopyBytes(Register src,
2677 Register dst,
2678 Register length,
2679 Register scratch) {
2680 Label align_loop, align_loop_1, word_loop, byte_loop, byte_loop_1, done;
2681
2682 // Align src before copying in word size chunks.
2683 bind(&align_loop);
2684 cmp(length, Operand(0));
2685 b(eq, &done);
2686 bind(&align_loop_1);
2687 tst(src, Operand(kPointerSize - 1));
2688 b(eq, &word_loop);
2689 ldrb(scratch, MemOperand(src, 1, PostIndex));
2690 strb(scratch, MemOperand(dst, 1, PostIndex));
2691 sub(length, length, Operand(1), SetCC);
2692 b(ne, &byte_loop_1);
2693
2694 // Copy bytes in word size chunks.
2695 bind(&word_loop);
Steve Block44f0eee2011-05-26 01:26:41 +01002696 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002697 tst(src, Operand(kPointerSize - 1));
2698 Assert(eq, "Expecting alignment for CopyBytes");
2699 }
2700 cmp(length, Operand(kPointerSize));
2701 b(lt, &byte_loop);
2702 ldr(scratch, MemOperand(src, kPointerSize, PostIndex));
2703#if CAN_USE_UNALIGNED_ACCESSES
2704 str(scratch, MemOperand(dst, kPointerSize, PostIndex));
2705#else
2706 strb(scratch, MemOperand(dst, 1, PostIndex));
2707 mov(scratch, Operand(scratch, LSR, 8));
2708 strb(scratch, MemOperand(dst, 1, PostIndex));
2709 mov(scratch, Operand(scratch, LSR, 8));
2710 strb(scratch, MemOperand(dst, 1, PostIndex));
2711 mov(scratch, Operand(scratch, LSR, 8));
2712 strb(scratch, MemOperand(dst, 1, PostIndex));
2713#endif
2714 sub(length, length, Operand(kPointerSize));
2715 b(&word_loop);
2716
2717 // Copy the last bytes if any left.
2718 bind(&byte_loop);
2719 cmp(length, Operand(0));
2720 b(eq, &done);
2721 bind(&byte_loop_1);
2722 ldrb(scratch, MemOperand(src, 1, PostIndex));
2723 strb(scratch, MemOperand(dst, 1, PostIndex));
2724 sub(length, length, Operand(1), SetCC);
2725 b(ne, &byte_loop_1);
2726 bind(&done);
2727}
2728
2729
Steve Block8defd9f2010-07-08 12:39:36 +01002730void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
2731 Register source, // Input.
2732 Register scratch) {
Steve Block1e0659c2011-05-24 12:43:12 +01002733 ASSERT(!zeros.is(source) || !source.is(scratch));
Steve Block8defd9f2010-07-08 12:39:36 +01002734 ASSERT(!zeros.is(scratch));
2735 ASSERT(!scratch.is(ip));
2736 ASSERT(!source.is(ip));
2737 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002738#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2739 clz(zeros, source); // This instruction is only supported after ARM5.
2740#else
Iain Merrick9ac36c92010-09-13 15:29:50 +01002741 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block8defd9f2010-07-08 12:39:36 +01002742 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002743 // Top 16.
2744 tst(scratch, Operand(0xffff0000));
2745 add(zeros, zeros, Operand(16), LeaveCC, eq);
2746 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2747 // Top 8.
2748 tst(scratch, Operand(0xff000000));
2749 add(zeros, zeros, Operand(8), LeaveCC, eq);
2750 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2751 // Top 4.
2752 tst(scratch, Operand(0xf0000000));
2753 add(zeros, zeros, Operand(4), LeaveCC, eq);
2754 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2755 // Top 2.
2756 tst(scratch, Operand(0xc0000000));
2757 add(zeros, zeros, Operand(2), LeaveCC, eq);
2758 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2759 // Top bit.
2760 tst(scratch, Operand(0x80000000u));
2761 add(zeros, zeros, Operand(1), LeaveCC, eq);
2762#endif
2763}
2764
2765
2766void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2767 Register first,
2768 Register second,
2769 Register scratch1,
2770 Register scratch2,
2771 Label* failure) {
2772 int kFlatAsciiStringMask =
2773 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2774 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2775 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2776 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2777 cmp(scratch1, Operand(kFlatAsciiStringTag));
2778 // Ignore second test if first test failed.
2779 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2780 b(ne, failure);
2781}
2782
2783
2784void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
2785 Register scratch,
2786 Label* failure) {
2787 int kFlatAsciiStringMask =
2788 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2789 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2790 and_(scratch, type, Operand(kFlatAsciiStringMask));
2791 cmp(scratch, Operand(kFlatAsciiStringTag));
2792 b(ne, failure);
2793}
2794
Steve Block44f0eee2011-05-26 01:26:41 +01002795static const int kRegisterPassedArguments = 4;
Steve Block6ded16b2010-05-10 14:33:55 +01002796
2797void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2798 int frame_alignment = ActivationFrameAlignment();
Steve Block44f0eee2011-05-26 01:26:41 +01002799
2800 // Reserve space for Isolate address which is always passed as last parameter
2801 num_arguments += 1;
2802
Steve Block6ded16b2010-05-10 14:33:55 +01002803 // Up to four simple arguments are passed in registers r0..r3.
Steve Block44f0eee2011-05-26 01:26:41 +01002804 int stack_passed_arguments = (num_arguments <= kRegisterPassedArguments) ?
2805 0 : num_arguments - kRegisterPassedArguments;
Steve Block6ded16b2010-05-10 14:33:55 +01002806 if (frame_alignment > kPointerSize) {
2807 // Make stack end at alignment and make room for num_arguments - 4 words
2808 // and the original value of sp.
2809 mov(scratch, sp);
2810 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
2811 ASSERT(IsPowerOf2(frame_alignment));
2812 and_(sp, sp, Operand(-frame_alignment));
2813 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2814 } else {
2815 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
2816 }
2817}
2818
2819
2820void MacroAssembler::CallCFunction(ExternalReference function,
2821 int num_arguments) {
Steve Block44f0eee2011-05-26 01:26:41 +01002822 CallCFunctionHelper(no_reg, function, ip, num_arguments);
2823}
2824
2825void MacroAssembler::CallCFunction(Register function,
2826 Register scratch,
2827 int num_arguments) {
2828 CallCFunctionHelper(function,
2829 ExternalReference::the_hole_value_location(isolate()),
2830 scratch,
2831 num_arguments);
Steve Block6ded16b2010-05-10 14:33:55 +01002832}
2833
2834
Steve Block44f0eee2011-05-26 01:26:41 +01002835void MacroAssembler::CallCFunctionHelper(Register function,
2836 ExternalReference function_reference,
2837 Register scratch,
2838 int num_arguments) {
2839 // Push Isolate address as the last argument.
2840 if (num_arguments < kRegisterPassedArguments) {
2841 Register arg_to_reg[] = {r0, r1, r2, r3};
2842 Register r = arg_to_reg[num_arguments];
2843 mov(r, Operand(ExternalReference::isolate_address()));
2844 } else {
2845 int stack_passed_arguments = num_arguments - kRegisterPassedArguments;
2846 // Push Isolate address on the stack after the arguments.
2847 mov(scratch, Operand(ExternalReference::isolate_address()));
2848 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
2849 }
2850 num_arguments += 1;
2851
Steve Block6ded16b2010-05-10 14:33:55 +01002852 // Make sure that the stack is aligned before calling a C function unless
2853 // running in the simulator. The simulator has its own alignment check which
2854 // provides more information.
2855#if defined(V8_HOST_ARCH_ARM)
Steve Block44f0eee2011-05-26 01:26:41 +01002856 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01002857 int frame_alignment = OS::ActivationFrameAlignment();
2858 int frame_alignment_mask = frame_alignment - 1;
2859 if (frame_alignment > kPointerSize) {
2860 ASSERT(IsPowerOf2(frame_alignment));
2861 Label alignment_as_expected;
2862 tst(sp, Operand(frame_alignment_mask));
2863 b(eq, &alignment_as_expected);
2864 // Don't use Check here, as it will call Runtime_Abort possibly
2865 // re-entering here.
2866 stop("Unexpected alignment");
2867 bind(&alignment_as_expected);
2868 }
2869 }
2870#endif
2871
2872 // Just call directly. The function called cannot cause a GC, or
2873 // allow preemption, so the return address in the link register
2874 // stays correct.
Steve Block44f0eee2011-05-26 01:26:41 +01002875 if (function.is(no_reg)) {
2876 mov(scratch, Operand(function_reference));
2877 function = scratch;
2878 }
Steve Block6ded16b2010-05-10 14:33:55 +01002879 Call(function);
Steve Block44f0eee2011-05-26 01:26:41 +01002880 int stack_passed_arguments = (num_arguments <= kRegisterPassedArguments) ?
2881 0 : num_arguments - kRegisterPassedArguments;
Steve Block6ded16b2010-05-10 14:33:55 +01002882 if (OS::ActivationFrameAlignment() > kPointerSize) {
2883 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
2884 } else {
2885 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
2886 }
2887}
2888
2889
Steve Block1e0659c2011-05-24 12:43:12 +01002890void MacroAssembler::GetRelocatedValueLocation(Register ldr_location,
2891 Register result) {
2892 const uint32_t kLdrOffsetMask = (1 << 12) - 1;
2893 const int32_t kPCRegOffset = 2 * kPointerSize;
2894 ldr(result, MemOperand(ldr_location));
Steve Block44f0eee2011-05-26 01:26:41 +01002895 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002896 // Check that the instruction is a ldr reg, [pc + offset] .
2897 and_(result, result, Operand(kLdrPCPattern));
2898 cmp(result, Operand(kLdrPCPattern));
2899 Check(eq, "The instruction to patch should be a load from pc.");
2900 // Result was clobbered. Restore it.
2901 ldr(result, MemOperand(ldr_location));
2902 }
2903 // Get the address of the constant.
2904 and_(result, result, Operand(kLdrOffsetMask));
2905 add(result, ldr_location, Operand(result));
2906 add(result, result, Operand(kPCRegOffset));
2907}
2908
2909
Steve Blocka7e24c12009-10-30 11:49:00 +00002910CodePatcher::CodePatcher(byte* address, int instructions)
2911 : address_(address),
2912 instructions_(instructions),
2913 size_(instructions * Assembler::kInstrSize),
2914 masm_(address, size_ + Assembler::kGap) {
2915 // Create a new macro assembler pointing to the address of the code to patch.
2916 // The size is adjusted with kGap on order for the assembler to generate size
2917 // bytes of instructions without failing with buffer size constraints.
2918 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2919}
2920
2921
2922CodePatcher::~CodePatcher() {
2923 // Indicate that code has changed.
2924 CPU::FlushICache(address_, size_);
2925
2926 // Check that the code was patched as expected.
2927 ASSERT(masm_.pc_ == address_ + size_);
2928 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2929}
2930
2931
Steve Block1e0659c2011-05-24 12:43:12 +01002932void CodePatcher::Emit(Instr instr) {
2933 masm()->emit(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002934}
2935
2936
2937void CodePatcher::Emit(Address addr) {
2938 masm()->emit(reinterpret_cast<Instr>(addr));
2939}
Steve Block1e0659c2011-05-24 12:43:12 +01002940
2941
2942void CodePatcher::EmitCondition(Condition cond) {
2943 Instr instr = Assembler::instr_at(masm_.pc_);
2944 instr = (instr & ~kCondMask) | cond;
2945 masm_.emit(instr);
2946}
Steve Blocka7e24c12009-10-30 11:49:00 +00002947
2948
2949} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01002950
2951#endif // V8_TARGET_ARCH_ARM