blob: 4fc3b03abf1caced7e8009593a8c24aaeee1cdb4 [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"
Ben Murdoch8b112d22011-06-08 16:22:53 +010035#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "debug.h"
37#include "runtime.h"
38
39namespace v8 {
40namespace internal {
41
Ben Murdoch8b112d22011-06-08 16:22:53 +010042MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
43 : Assembler(arg_isolate, buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000044 generating_stub_(false),
Ben Murdoch592a9fc2012-03-05 11:04:45 +000045 allow_stub_calls_(true),
46 has_frame_(false) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010047 if (isolate() != NULL) {
48 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
49 isolate());
50 }
Steve Blocka7e24c12009-10-30 11:49:00 +000051}
52
53
54// We always generate arm code, never thumb code, even if V8 is compiled to
55// thumb, so we require inter-working support
56#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
57#error "flag -mthumb-interwork missing"
58#endif
59
60
61// We do not support thumb inter-working with an arm architecture not supporting
62// the blx instruction (below v5t). If you know what CPU you are compiling for
63// you can use -march=armv7 or similar.
64#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
65# error "For thumb inter-working we require an architecture which supports blx"
66#endif
67
68
Steve Blocka7e24c12009-10-30 11:49:00 +000069// Using bx does not yield better code, so use it only when required
70#if defined(USE_THUMB_INTERWORK)
71#define USE_BX 1
72#endif
73
74
75void MacroAssembler::Jump(Register target, Condition cond) {
76#if USE_BX
77 bx(target, cond);
78#else
79 mov(pc, Operand(target), LeaveCC, cond);
80#endif
81}
82
83
84void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
85 Condition cond) {
86#if USE_BX
Ben Murdoch257744e2011-11-30 15:57:28 +000087 mov(ip, Operand(target, rmode));
Steve Blocka7e24c12009-10-30 11:49:00 +000088 bx(ip, cond);
89#else
90 mov(pc, Operand(target, rmode), LeaveCC, cond);
91#endif
92}
93
94
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000095void MacroAssembler::Jump(Address target, RelocInfo::Mode rmode,
Steve Blocka7e24c12009-10-30 11:49:00 +000096 Condition cond) {
97 ASSERT(!RelocInfo::IsCodeTarget(rmode));
98 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
99}
100
101
102void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
103 Condition cond) {
104 ASSERT(RelocInfo::IsCodeTarget(rmode));
105 // 'code' is always generated ARM code, never THUMB code
106 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
107}
108
109
Steve Block44f0eee2011-05-26 01:26:41 +0100110int MacroAssembler::CallSize(Register target, Condition cond) {
111#if USE_BLX
112 return kInstrSize;
113#else
114 return 2 * kInstrSize;
115#endif
116}
117
118
Steve Blocka7e24c12009-10-30 11:49:00 +0000119void MacroAssembler::Call(Register target, Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100120 // Block constant pool for the call instruction sequence.
121 BlockConstPoolScope block_const_pool(this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000122 Label start;
123 bind(&start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000124#if USE_BLX
125 blx(target, cond);
126#else
127 // set lr for return at current pc + 8
128 mov(lr, Operand(pc), LeaveCC, cond);
129 mov(pc, Operand(target), LeaveCC, cond);
130#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000131 ASSERT_EQ(CallSize(target, cond), SizeOfCodeGeneratedSince(&start));
Steve Blocka7e24c12009-10-30 11:49:00 +0000132}
133
134
Steve Block44f0eee2011-05-26 01:26:41 +0100135int MacroAssembler::CallSize(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000136 Address target, RelocInfo::Mode rmode, Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100137 int size = 2 * kInstrSize;
138 Instr mov_instr = cond | MOV | LeaveCC;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000139 intptr_t immediate = reinterpret_cast<intptr_t>(target);
140 if (!Operand(immediate, rmode).is_single_instruction(mov_instr)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100141 size += kInstrSize;
142 }
143 return size;
144}
145
146
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000147void MacroAssembler::Call(Address target,
Ben Murdoch257744e2011-11-30 15:57:28 +0000148 RelocInfo::Mode rmode,
149 Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100150 // Block constant pool for the call instruction sequence.
151 BlockConstPoolScope block_const_pool(this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000152 Label start;
153 bind(&start);
Steve Block6ded16b2010-05-10 14:33:55 +0100154#if USE_BLX
155 // On ARMv5 and after the recommended call sequence is:
156 // ldr ip, [pc, #...]
157 // blx ip
158
Steve Block44f0eee2011-05-26 01:26:41 +0100159 // Statement positions are expected to be recorded when the target
160 // address is loaded. The mov method will automatically record
161 // positions when pc is the target, since this is not the case here
162 // we have to do it explicitly.
163 positions_recorder()->WriteRecordedPositions();
Steve Block6ded16b2010-05-10 14:33:55 +0100164
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000165 mov(ip, Operand(reinterpret_cast<int32_t>(target), rmode));
Steve Block44f0eee2011-05-26 01:26:41 +0100166 blx(ip, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100167
168 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
169#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 // Set lr for return at current pc + 8.
171 mov(lr, Operand(pc), LeaveCC, cond);
172 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000173 mov(pc, Operand(reinterpret_cast<int32_t>(target), rmode), LeaveCC, cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100175#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000176 ASSERT_EQ(CallSize(target, rmode, cond), SizeOfCodeGeneratedSince(&start));
Steve Blocka7e24c12009-10-30 11:49:00 +0000177}
178
179
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000180int MacroAssembler::CallSize(Handle<Code> code,
181 RelocInfo::Mode rmode,
182 unsigned ast_id,
183 Condition cond) {
184 return CallSize(reinterpret_cast<Address>(code.location()), rmode, cond);
Ben Murdoch257744e2011-11-30 15:57:28 +0000185}
186
187
188void MacroAssembler::Call(Handle<Code> code,
189 RelocInfo::Mode rmode,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000190 unsigned ast_id,
Ben Murdoch257744e2011-11-30 15:57:28 +0000191 Condition cond) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000192 Label start;
193 bind(&start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 ASSERT(RelocInfo::IsCodeTarget(rmode));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000195 if (rmode == RelocInfo::CODE_TARGET && ast_id != kNoASTId) {
196 SetRecordedAstId(ast_id);
197 rmode = RelocInfo::CODE_TARGET_WITH_ID;
198 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 // 'code' is always generated ARM code, never THUMB code
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000200 Call(reinterpret_cast<Address>(code.location()), rmode, cond);
201 ASSERT_EQ(CallSize(code, rmode, ast_id, cond),
202 SizeOfCodeGeneratedSince(&start));
Steve Blocka7e24c12009-10-30 11:49:00 +0000203}
204
205
206void MacroAssembler::Ret(Condition cond) {
207#if USE_BX
208 bx(lr, cond);
209#else
210 mov(pc, Operand(lr), LeaveCC, cond);
211#endif
212}
213
214
Leon Clarkee46be812010-01-19 14:06:41 +0000215void MacroAssembler::Drop(int count, Condition cond) {
216 if (count > 0) {
217 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
218 }
219}
220
221
Ben Murdochb0fe1622011-05-05 13:52:32 +0100222void MacroAssembler::Ret(int drop, Condition cond) {
223 Drop(drop, cond);
224 Ret(cond);
225}
226
227
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100228void MacroAssembler::Swap(Register reg1,
229 Register reg2,
230 Register scratch,
231 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100232 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100233 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
234 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
235 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100236 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100237 mov(scratch, reg1, LeaveCC, cond);
238 mov(reg1, reg2, LeaveCC, cond);
239 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100240 }
241}
242
243
Leon Clarkee46be812010-01-19 14:06:41 +0000244void MacroAssembler::Call(Label* target) {
245 bl(target);
246}
247
248
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000249void MacroAssembler::Push(Handle<Object> handle) {
250 mov(ip, Operand(handle));
251 push(ip);
252}
253
254
Leon Clarkee46be812010-01-19 14:06:41 +0000255void MacroAssembler::Move(Register dst, Handle<Object> value) {
256 mov(dst, Operand(value));
257}
Steve Blockd0582a62009-12-15 09:54:21 +0000258
259
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000260void MacroAssembler::Move(Register dst, Register src, Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100261 if (!dst.is(src)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000262 mov(dst, src, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100263 }
264}
265
266
Ben Murdoch257744e2011-11-30 15:57:28 +0000267void MacroAssembler::Move(DoubleRegister dst, DoubleRegister src) {
268 ASSERT(CpuFeatures::IsSupported(VFP3));
269 CpuFeatures::Scope scope(VFP3);
270 if (!dst.is(src)) {
271 vmov(dst, src);
272 }
273}
274
275
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100276void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
277 Condition cond) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800278 if (!src2.is_reg() &&
279 !src2.must_use_constant_pool() &&
280 src2.immediate() == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100281 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800282
283 } else if (!src2.is_single_instruction() &&
284 !src2.must_use_constant_pool() &&
Ben Murdoch8b112d22011-06-08 16:22:53 +0100285 CpuFeatures::IsSupported(ARMv7) &&
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800286 IsPowerOf2(src2.immediate() + 1)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000287 ubfx(dst, src1, 0,
288 WhichPowerOf2(static_cast<uint32_t>(src2.immediate()) + 1), cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800289
290 } else {
291 and_(dst, src1, src2, LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100292 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100293}
294
295
296void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
297 Condition cond) {
298 ASSERT(lsb < 32);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100299 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100300 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
301 and_(dst, src1, Operand(mask), LeaveCC, cond);
302 if (lsb != 0) {
303 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
304 }
305 } else {
306 ubfx(dst, src1, lsb, width, cond);
307 }
308}
309
310
311void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
312 Condition cond) {
313 ASSERT(lsb < 32);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100314 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100315 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
316 and_(dst, src1, Operand(mask), LeaveCC, cond);
317 int shift_up = 32 - lsb - width;
318 int shift_down = lsb + shift_up;
319 if (shift_up != 0) {
320 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
321 }
322 if (shift_down != 0) {
323 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
324 }
325 } else {
326 sbfx(dst, src1, lsb, width, cond);
327 }
328}
329
330
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100331void MacroAssembler::Bfi(Register dst,
332 Register src,
333 Register scratch,
334 int lsb,
335 int width,
336 Condition cond) {
337 ASSERT(0 <= lsb && lsb < 32);
338 ASSERT(0 <= width && width < 32);
339 ASSERT(lsb + width < 32);
340 ASSERT(!scratch.is(dst));
341 if (width == 0) return;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100342 if (!CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100343 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
344 bic(dst, dst, Operand(mask));
345 and_(scratch, src, Operand((1 << width) - 1));
346 mov(scratch, Operand(scratch, LSL, lsb));
347 orr(dst, dst, scratch);
348 } else {
349 bfi(dst, src, lsb, width, cond);
350 }
351}
352
353
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100354void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
355 ASSERT(lsb < 32);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100356 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100357 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
358 bic(dst, dst, Operand(mask));
359 } else {
360 bfc(dst, lsb, width, cond);
361 }
362}
363
364
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100365void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
366 Condition cond) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100367 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100368 ASSERT(!dst.is(pc) && !src.rm().is(pc));
369 ASSERT((satpos >= 0) && (satpos <= 31));
370
371 // These asserts are required to ensure compatibility with the ARMv7
372 // implementation.
373 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
374 ASSERT(src.rs().is(no_reg));
375
376 Label done;
377 int satval = (1 << satpos) - 1;
378
379 if (cond != al) {
380 b(NegateCondition(cond), &done); // Skip saturate if !condition.
381 }
382 if (!(src.is_reg() && dst.is(src.rm()))) {
383 mov(dst, src);
384 }
385 tst(dst, Operand(~satval));
386 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100387 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100388 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
389 bind(&done);
390 } else {
391 usat(dst, satpos, src, cond);
392 }
393}
394
395
Steve Blocka7e24c12009-10-30 11:49:00 +0000396void MacroAssembler::LoadRoot(Register destination,
397 Heap::RootListIndex index,
398 Condition cond) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000399 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000400}
401
402
Kristian Monsen25f61362010-05-21 11:50:48 +0100403void MacroAssembler::StoreRoot(Register source,
404 Heap::RootListIndex index,
405 Condition cond) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000406 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100407}
408
409
410void MacroAssembler::InNewSpace(Register object,
411 Register scratch,
Steve Block1e0659c2011-05-24 12:43:12 +0100412 Condition cond,
Steve Block6ded16b2010-05-10 14:33:55 +0100413 Label* branch) {
Steve Block1e0659c2011-05-24 12:43:12 +0100414 ASSERT(cond == eq || cond == ne);
Steve Block44f0eee2011-05-26 01:26:41 +0100415 and_(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
416 cmp(scratch, Operand(ExternalReference::new_space_start(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +0100417 b(cond, branch);
Steve Block6ded16b2010-05-10 14:33:55 +0100418}
419
420
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000421void MacroAssembler::RecordWriteField(
422 Register object,
423 int offset,
424 Register value,
425 Register dst,
426 LinkRegisterStatus lr_status,
427 SaveFPRegsMode save_fp,
428 RememberedSetAction remembered_set_action,
429 SmiCheck smi_check) {
430 // First, check if a write barrier is even needed. The tests below
431 // catch stores of Smis.
Steve Block6ded16b2010-05-10 14:33:55 +0100432 Label done;
433
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000434 // Skip barrier if writing a smi.
435 if (smi_check == INLINE_SMI_CHECK) {
436 JumpIfSmi(value, &done);
437 }
Steve Block6ded16b2010-05-10 14:33:55 +0100438
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000439 // Although the object register is tagged, the offset is relative to the start
440 // of the object, so so offset must be a multiple of kPointerSize.
441 ASSERT(IsAligned(offset, kPointerSize));
Steve Block8defd9f2010-07-08 12:39:36 +0100442
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000443 add(dst, object, Operand(offset - kHeapObjectTag));
444 if (emit_debug_code()) {
445 Label ok;
446 tst(dst, Operand((1 << kPointerSizeLog2) - 1));
447 b(eq, &ok);
448 stop("Unaligned cell in write barrier");
449 bind(&ok);
450 }
451
452 RecordWrite(object,
453 dst,
454 value,
455 lr_status,
456 save_fp,
457 remembered_set_action,
458 OMIT_SMI_CHECK);
Steve Blocka7e24c12009-10-30 11:49:00 +0000459
460 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000461
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000462 // Clobber clobbered input registers when running with the debug-code flag
Leon Clarke4515c472010-02-03 11:58:03 +0000463 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100464 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000465 mov(value, Operand(BitCast<int32_t>(kZapValue + 4)));
466 mov(dst, Operand(BitCast<int32_t>(kZapValue + 8)));
Leon Clarke4515c472010-02-03 11:58:03 +0000467 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000468}
469
470
Steve Block8defd9f2010-07-08 12:39:36 +0100471// Will clobber 4 registers: object, address, scratch, ip. The
472// register 'object' contains a heap object pointer. The heap object
473// tag is shifted away.
474void MacroAssembler::RecordWrite(Register object,
475 Register address,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000476 Register value,
477 LinkRegisterStatus lr_status,
478 SaveFPRegsMode fp_mode,
479 RememberedSetAction remembered_set_action,
480 SmiCheck smi_check) {
Steve Block8defd9f2010-07-08 12:39:36 +0100481 // The compiled code assumes that record write doesn't change the
482 // context register, so we check that none of the clobbered
483 // registers are cp.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000484 ASSERT(!address.is(cp) && !value.is(cp));
485
486 if (FLAG_debug_code) {
487 Label ok;
488 ldr(ip, MemOperand(address));
489 cmp(ip, value);
490 b(eq, &ok);
491 stop("Wrong address or value passed to RecordWrite");
492 bind(&ok);
493 }
Steve Block8defd9f2010-07-08 12:39:36 +0100494
495 Label done;
496
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000497 if (smi_check == INLINE_SMI_CHECK) {
498 ASSERT_EQ(0, kSmiTag);
499 tst(value, Operand(kSmiTagMask));
500 b(eq, &done);
501 }
502
503 CheckPageFlag(value,
504 value, // Used as scratch.
505 MemoryChunk::kPointersToHereAreInterestingMask,
506 eq,
507 &done);
508 CheckPageFlag(object,
509 value, // Used as scratch.
510 MemoryChunk::kPointersFromHereAreInterestingMask,
511 eq,
512 &done);
Steve Block8defd9f2010-07-08 12:39:36 +0100513
514 // Record the actual write.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000515 if (lr_status == kLRHasNotBeenSaved) {
516 push(lr);
517 }
518 RecordWriteStub stub(object, value, address, remembered_set_action, fp_mode);
519 CallStub(&stub);
520 if (lr_status == kLRHasNotBeenSaved) {
521 pop(lr);
522 }
Steve Block8defd9f2010-07-08 12:39:36 +0100523
524 bind(&done);
525
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000526 // Clobber clobbered registers when running with the debug-code flag
Steve Block8defd9f2010-07-08 12:39:36 +0100527 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100528 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000529 mov(address, Operand(BitCast<int32_t>(kZapValue + 12)));
530 mov(value, Operand(BitCast<int32_t>(kZapValue + 16)));
531 }
532}
533
534
535void MacroAssembler::RememberedSetHelper(Register object, // For debug tests.
536 Register address,
537 Register scratch,
538 SaveFPRegsMode fp_mode,
539 RememberedSetFinalAction and_then) {
540 Label done;
541 if (FLAG_debug_code) {
542 Label ok;
543 JumpIfNotInNewSpace(object, scratch, &ok);
544 stop("Remembered set pointer is in new space");
545 bind(&ok);
546 }
547 // Load store buffer top.
548 ExternalReference store_buffer =
549 ExternalReference::store_buffer_top(isolate());
550 mov(ip, Operand(store_buffer));
551 ldr(scratch, MemOperand(ip));
552 // Store pointer to buffer and increment buffer top.
553 str(address, MemOperand(scratch, kPointerSize, PostIndex));
554 // Write back new top of buffer.
555 str(scratch, MemOperand(ip));
556 // Call stub on end of buffer.
557 // Check for end of buffer.
558 tst(scratch, Operand(StoreBuffer::kStoreBufferOverflowBit));
559 if (and_then == kFallThroughAtEnd) {
560 b(eq, &done);
561 } else {
562 ASSERT(and_then == kReturnAtEnd);
563 Ret(eq);
564 }
565 push(lr);
566 StoreBufferOverflowStub store_buffer_overflow =
567 StoreBufferOverflowStub(fp_mode);
568 CallStub(&store_buffer_overflow);
569 pop(lr);
570 bind(&done);
571 if (and_then == kReturnAtEnd) {
572 Ret();
Steve Block8defd9f2010-07-08 12:39:36 +0100573 }
574}
575
576
Ben Murdochb0fe1622011-05-05 13:52:32 +0100577// Push and pop all registers that can hold pointers.
578void MacroAssembler::PushSafepointRegisters() {
579 // Safepoints expect a block of contiguous register values starting with r0:
580 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
581 // Safepoints expect a block of kNumSafepointRegisters values on the
582 // stack, so adjust the stack for unsaved registers.
583 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
584 ASSERT(num_unsaved >= 0);
585 sub(sp, sp, Operand(num_unsaved * kPointerSize));
586 stm(db_w, sp, kSafepointSavedRegisters);
587}
588
589
590void MacroAssembler::PopSafepointRegisters() {
591 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
592 ldm(ia_w, sp, kSafepointSavedRegisters);
593 add(sp, sp, Operand(num_unsaved * kPointerSize));
594}
595
596
Ben Murdochb8e0da22011-05-16 14:20:40 +0100597void MacroAssembler::PushSafepointRegistersAndDoubles() {
598 PushSafepointRegisters();
599 sub(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
600 kDoubleSize));
601 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
602 vstr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
603 }
604}
605
606
607void MacroAssembler::PopSafepointRegistersAndDoubles() {
608 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
609 vldr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
610 }
611 add(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
612 kDoubleSize));
613 PopSafepointRegisters();
614}
615
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100616void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register src,
617 Register dst) {
618 str(src, SafepointRegistersAndDoublesSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100619}
620
621
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100622void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
623 str(src, SafepointRegisterSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100624}
625
626
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100627void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
628 ldr(dst, SafepointRegisterSlot(src));
Steve Block1e0659c2011-05-24 12:43:12 +0100629}
630
631
Ben Murdochb0fe1622011-05-05 13:52:32 +0100632int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
633 // The registers are pushed starting with the highest encoding,
634 // which means that lowest encodings are closest to the stack pointer.
635 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
636 return reg_code;
637}
638
639
Steve Block1e0659c2011-05-24 12:43:12 +0100640MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
641 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
642}
643
644
645MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
646 // General purpose registers are pushed last on the stack.
647 int doubles_size = DwVfpRegister::kNumAllocatableRegisters * kDoubleSize;
648 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
649 return MemOperand(sp, doubles_size + register_offset);
650}
651
652
Leon Clarkef7060e22010-06-03 12:02:55 +0100653void MacroAssembler::Ldrd(Register dst1, Register dst2,
654 const MemOperand& src, Condition cond) {
655 ASSERT(src.rm().is(no_reg));
656 ASSERT(!dst1.is(lr)); // r14.
657 ASSERT_EQ(0, dst1.code() % 2);
658 ASSERT_EQ(dst1.code() + 1, dst2.code());
659
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000660 // V8 does not use this addressing mode, so the fallback code
661 // below doesn't support it yet.
662 ASSERT((src.am() != PreIndex) && (src.am() != NegPreIndex));
663
Leon Clarkef7060e22010-06-03 12:02:55 +0100664 // Generate two ldr instructions if ldrd is not available.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100665 if (CpuFeatures::IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100666 CpuFeatures::Scope scope(ARMv7);
667 ldrd(dst1, dst2, src, cond);
668 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000669 if ((src.am() == Offset) || (src.am() == NegOffset)) {
670 MemOperand src2(src);
671 src2.set_offset(src2.offset() + 4);
672 if (dst1.is(src.rn())) {
673 ldr(dst2, src2, cond);
674 ldr(dst1, src, cond);
675 } else {
676 ldr(dst1, src, cond);
677 ldr(dst2, src2, cond);
678 }
679 } else { // PostIndex or NegPostIndex.
680 ASSERT((src.am() == PostIndex) || (src.am() == NegPostIndex));
681 if (dst1.is(src.rn())) {
682 ldr(dst2, MemOperand(src.rn(), 4, Offset), cond);
683 ldr(dst1, src, cond);
684 } else {
685 MemOperand src2(src);
686 src2.set_offset(src2.offset() - 4);
687 ldr(dst1, MemOperand(src.rn(), 4, PostIndex), cond);
688 ldr(dst2, src2, cond);
689 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100690 }
691 }
692}
693
694
695void MacroAssembler::Strd(Register src1, Register src2,
696 const MemOperand& dst, Condition cond) {
697 ASSERT(dst.rm().is(no_reg));
698 ASSERT(!src1.is(lr)); // r14.
699 ASSERT_EQ(0, src1.code() % 2);
700 ASSERT_EQ(src1.code() + 1, src2.code());
701
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000702 // V8 does not use this addressing mode, so the fallback code
703 // below doesn't support it yet.
704 ASSERT((dst.am() != PreIndex) && (dst.am() != NegPreIndex));
705
Leon Clarkef7060e22010-06-03 12:02:55 +0100706 // Generate two str instructions if strd is not available.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100707 if (CpuFeatures::IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100708 CpuFeatures::Scope scope(ARMv7);
709 strd(src1, src2, dst, cond);
710 } else {
711 MemOperand dst2(dst);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000712 if ((dst.am() == Offset) || (dst.am() == NegOffset)) {
713 dst2.set_offset(dst2.offset() + 4);
714 str(src1, dst, cond);
715 str(src2, dst2, cond);
716 } else { // PostIndex or NegPostIndex.
717 ASSERT((dst.am() == PostIndex) || (dst.am() == NegPostIndex));
718 dst2.set_offset(dst2.offset() - 4);
719 str(src1, MemOperand(dst.rn(), 4, PostIndex), cond);
720 str(src2, dst2, cond);
721 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100722 }
723}
724
725
Ben Murdochb8e0da22011-05-16 14:20:40 +0100726void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
727 const Register scratch,
728 const Condition cond) {
729 vmrs(scratch, cond);
730 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
731 vmsr(scratch, cond);
732}
733
734
735void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
736 const DwVfpRegister src2,
737 const Condition cond) {
738 // Compare and move FPSCR flags to the normal condition flags.
739 VFPCompareAndLoadFlags(src1, src2, pc, cond);
740}
741
742void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
743 const double src2,
744 const Condition cond) {
745 // Compare and move FPSCR flags to the normal condition flags.
746 VFPCompareAndLoadFlags(src1, src2, pc, cond);
747}
748
749
750void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
751 const DwVfpRegister src2,
752 const Register fpscr_flags,
753 const Condition cond) {
754 // Compare and load FPSCR.
755 vcmp(src1, src2, cond);
756 vmrs(fpscr_flags, cond);
757}
758
759void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
760 const double src2,
761 const Register fpscr_flags,
762 const Condition cond) {
763 // Compare and load FPSCR.
764 vcmp(src1, src2, cond);
765 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100766}
767
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000768void MacroAssembler::Vmov(const DwVfpRegister dst,
769 const double imm,
770 const Condition cond) {
771 ASSERT(CpuFeatures::IsEnabled(VFP3));
772 static const DoubleRepresentation minus_zero(-0.0);
773 static const DoubleRepresentation zero(0.0);
774 DoubleRepresentation value(imm);
775 // Handle special values first.
776 if (value.bits == zero.bits) {
777 vmov(dst, kDoubleRegZero, cond);
778 } else if (value.bits == minus_zero.bits) {
779 vneg(dst, kDoubleRegZero, cond);
780 } else {
781 vmov(dst, imm, cond);
782 }
783}
784
Ben Murdoch086aeea2011-05-13 15:57:08 +0100785
Steve Blocka7e24c12009-10-30 11:49:00 +0000786void MacroAssembler::EnterFrame(StackFrame::Type type) {
787 // r0-r3: preserved
788 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
789 mov(ip, Operand(Smi::FromInt(type)));
790 push(ip);
791 mov(ip, Operand(CodeObject()));
792 push(ip);
793 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
794}
795
796
797void MacroAssembler::LeaveFrame(StackFrame::Type type) {
798 // r0: preserved
799 // r1: preserved
800 // r2: preserved
801
802 // Drop the execution stack down to the frame pointer and restore
803 // the caller frame pointer and return address.
804 mov(sp, fp);
805 ldm(ia_w, sp, fp.bit() | lr.bit());
806}
807
808
Steve Block1e0659c2011-05-24 12:43:12 +0100809void MacroAssembler::EnterExitFrame(bool save_doubles, int stack_space) {
810 // Setup the frame structure on the stack.
811 ASSERT_EQ(2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
812 ASSERT_EQ(1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
813 ASSERT_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
814 Push(lr, fp);
Andrei Popescu402d9372010-02-26 13:31:12 +0000815 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Block1e0659c2011-05-24 12:43:12 +0100816 // Reserve room for saved entry sp and code object.
817 sub(sp, sp, Operand(2 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100818 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100819 mov(ip, Operand(0));
820 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
821 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000822 mov(ip, Operand(CodeObject()));
Steve Block1e0659c2011-05-24 12:43:12 +0100823 str(ip, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000824
825 // Save the frame pointer and the context in top.
Ben Murdoch589d6972011-11-30 16:04:58 +0000826 mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000827 str(fp, MemOperand(ip));
Ben Murdoch589d6972011-11-30 16:04:58 +0000828 mov(ip, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000829 str(cp, MemOperand(ip));
830
Ben Murdochb0fe1622011-05-05 13:52:32 +0100831 // Optionally save all double registers.
832 if (save_doubles) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100833 DwVfpRegister first = d0;
834 DwVfpRegister last =
835 DwVfpRegister::from_code(DwVfpRegister::kNumRegisters - 1);
836 vstm(db_w, sp, first, last);
Steve Block1e0659c2011-05-24 12:43:12 +0100837 // Note that d0 will be accessible at
838 // fp - 2 * kPointerSize - DwVfpRegister::kNumRegisters * kDoubleSize,
839 // since the sp slot and code slot were pushed after the fp.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100840 }
Steve Block1e0659c2011-05-24 12:43:12 +0100841
842 // Reserve place for the return address and stack space and align the frame
843 // preparing for calling the runtime function.
844 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
845 sub(sp, sp, Operand((stack_space + 1) * kPointerSize));
846 if (frame_alignment > 0) {
847 ASSERT(IsPowerOf2(frame_alignment));
848 and_(sp, sp, Operand(-frame_alignment));
849 }
850
851 // Set the exit frame sp value to point just before the return address
852 // location.
853 add(ip, sp, Operand(kPointerSize));
854 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000855}
856
857
Steve Block6ded16b2010-05-10 14:33:55 +0100858void MacroAssembler::InitializeNewString(Register string,
859 Register length,
860 Heap::RootListIndex map_index,
861 Register scratch1,
862 Register scratch2) {
863 mov(scratch1, Operand(length, LSL, kSmiTagSize));
864 LoadRoot(scratch2, map_index);
865 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
866 mov(scratch1, Operand(String::kEmptyHashField));
867 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
868 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
869}
870
871
872int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000873#if defined(V8_HOST_ARCH_ARM)
874 // Running on the real platform. Use the alignment as mandated by the local
875 // environment.
876 // Note: This will break if we ever start generating snapshots on one ARM
877 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100878 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000879#else // defined(V8_HOST_ARCH_ARM)
880 // If we are using the simulator then we should always align to the expected
881 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100882 // if the target platform will need alignment, so this is controlled from a
883 // flag.
884 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000885#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000886}
887
888
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100889void MacroAssembler::LeaveExitFrame(bool save_doubles,
890 Register argument_count) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100891 // Optionally restore all double registers.
892 if (save_doubles) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100893 // Calculate the stack location of the saved doubles and restore them.
894 const int offset = 2 * kPointerSize;
895 sub(r3, fp, Operand(offset + DwVfpRegister::kNumRegisters * kDoubleSize));
896 DwVfpRegister first = d0;
897 DwVfpRegister last =
898 DwVfpRegister::from_code(DwVfpRegister::kNumRegisters - 1);
899 vldm(ia, r3, first, last);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100900 }
901
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100903 mov(r3, Operand(0, RelocInfo::NONE));
Ben Murdoch589d6972011-11-30 16:04:58 +0000904 mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000905 str(r3, MemOperand(ip));
906
907 // Restore current context from top and clear it in debug mode.
Ben Murdoch589d6972011-11-30 16:04:58 +0000908 mov(ip, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 ldr(cp, MemOperand(ip));
910#ifdef DEBUG
911 str(r3, MemOperand(ip));
912#endif
913
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100914 // Tear down the exit frame, pop the arguments, and return.
Steve Block1e0659c2011-05-24 12:43:12 +0100915 mov(sp, Operand(fp));
916 ldm(ia_w, sp, fp.bit() | lr.bit());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100917 if (argument_count.is_valid()) {
918 add(sp, sp, Operand(argument_count, LSL, kPointerSizeLog2));
919 }
920}
921
922void MacroAssembler::GetCFunctionDoubleResult(const DoubleRegister dst) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000923 if (use_eabi_hardfloat()) {
924 Move(dst, d0);
925 } else {
926 vmov(dst, r0, r1);
927 }
928}
929
930
931void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
932 // This macro takes the dst register to make the code more readable
933 // at the call sites. However, the dst register has to be r5 to
934 // follow the calling convention which requires the call type to be
935 // in r5.
936 ASSERT(dst.is(r5));
937 if (call_kind == CALL_AS_FUNCTION) {
938 mov(dst, Operand(Smi::FromInt(1)));
939 } else {
940 mov(dst, Operand(Smi::FromInt(0)));
941 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000942}
943
944
945void MacroAssembler::InvokePrologue(const ParameterCount& expected,
946 const ParameterCount& actual,
947 Handle<Code> code_constant,
948 Register code_reg,
949 Label* done,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100950 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000951 const CallWrapper& call_wrapper,
952 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000953 bool definitely_matches = false;
954 Label regular_invoke;
955
956 // Check whether the expected and actual arguments count match. If not,
957 // setup registers according to contract with ArgumentsAdaptorTrampoline:
958 // r0: actual arguments count
959 // r1: function (passed through to callee)
960 // r2: expected arguments count
961 // r3: callee code entry
962
963 // The code below is made a lot easier because the calling code already sets
964 // up actual and expected registers according to the contract if values are
965 // passed in registers.
966 ASSERT(actual.is_immediate() || actual.reg().is(r0));
967 ASSERT(expected.is_immediate() || expected.reg().is(r2));
968 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
969
970 if (expected.is_immediate()) {
971 ASSERT(actual.is_immediate());
972 if (expected.immediate() == actual.immediate()) {
973 definitely_matches = true;
974 } else {
975 mov(r0, Operand(actual.immediate()));
976 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
977 if (expected.immediate() == sentinel) {
978 // Don't worry about adapting arguments for builtins that
979 // don't want that done. Skip adaption code by making it look
980 // like we have a match between expected and actual number of
981 // arguments.
982 definitely_matches = true;
983 } else {
984 mov(r2, Operand(expected.immediate()));
985 }
986 }
987 } else {
988 if (actual.is_immediate()) {
989 cmp(expected.reg(), Operand(actual.immediate()));
990 b(eq, &regular_invoke);
991 mov(r0, Operand(actual.immediate()));
992 } else {
993 cmp(expected.reg(), Operand(actual.reg()));
994 b(eq, &regular_invoke);
995 }
996 }
997
998 if (!definitely_matches) {
999 if (!code_constant.is_null()) {
1000 mov(r3, Operand(code_constant));
1001 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
1002 }
1003
1004 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001005 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001006 if (flag == CALL_FUNCTION) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001007 call_wrapper.BeforeCall(CallSize(adaptor));
Ben Murdoch257744e2011-11-30 15:57:28 +00001008 SetCallKind(r5, call_kind);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001009 Call(adaptor);
Ben Murdoch257744e2011-11-30 15:57:28 +00001010 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00001011 b(done);
1012 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001013 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 Jump(adaptor, RelocInfo::CODE_TARGET);
1015 }
1016 bind(&regular_invoke);
1017 }
1018}
1019
1020
1021void MacroAssembler::InvokeCode(Register code,
1022 const ParameterCount& expected,
1023 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001024 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001025 const CallWrapper& call_wrapper,
1026 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001027 // You can't call a function without a valid frame.
1028 ASSERT(flag == JUMP_FUNCTION || has_frame());
1029
Steve Blocka7e24c12009-10-30 11:49:00 +00001030 Label done;
1031
Ben Murdochb8e0da22011-05-16 14:20:40 +01001032 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001033 call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001034 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001035 call_wrapper.BeforeCall(CallSize(code));
1036 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001037 Call(code);
Ben Murdoch257744e2011-11-30 15:57:28 +00001038 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00001039 } else {
1040 ASSERT(flag == JUMP_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00001041 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001042 Jump(code);
1043 }
1044
1045 // Continue here if InvokePrologue does handle the invocation due to
1046 // mismatched parameter counts.
1047 bind(&done);
1048}
1049
1050
1051void MacroAssembler::InvokeCode(Handle<Code> code,
1052 const ParameterCount& expected,
1053 const ParameterCount& actual,
1054 RelocInfo::Mode rmode,
Ben Murdoch257744e2011-11-30 15:57:28 +00001055 InvokeFlag flag,
1056 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001057 // You can't call a function without a valid frame.
1058 ASSERT(flag == JUMP_FUNCTION || has_frame());
1059
Steve Blocka7e24c12009-10-30 11:49:00 +00001060 Label done;
1061
Ben Murdoch257744e2011-11-30 15:57:28 +00001062 InvokePrologue(expected, actual, code, no_reg, &done, flag,
1063 NullCallWrapper(), call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 if (flag == CALL_FUNCTION) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001065 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001066 Call(code, rmode);
1067 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001068 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 Jump(code, rmode);
1070 }
1071
1072 // Continue here if InvokePrologue does handle the invocation due to
1073 // mismatched parameter counts.
1074 bind(&done);
1075}
1076
1077
1078void MacroAssembler::InvokeFunction(Register fun,
1079 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001080 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001081 const CallWrapper& call_wrapper,
1082 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001083 // You can't call a function without a valid frame.
1084 ASSERT(flag == JUMP_FUNCTION || has_frame());
1085
Steve Blocka7e24c12009-10-30 11:49:00 +00001086 // Contract with called JS functions requires that function is passed in r1.
1087 ASSERT(fun.is(r1));
1088
1089 Register expected_reg = r2;
1090 Register code_reg = r3;
1091
1092 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1093 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1094 ldr(expected_reg,
1095 FieldMemOperand(code_reg,
1096 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001097 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001098 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +01001099 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001100
1101 ParameterCount expected(expected_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00001102 InvokeCode(code_reg, expected, actual, flag, call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001103}
1104
1105
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001106void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
Andrei Popescu402d9372010-02-26 13:31:12 +00001107 const ParameterCount& actual,
Ben Murdoch257744e2011-11-30 15:57:28 +00001108 InvokeFlag flag,
1109 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001110 // You can't call a function without a valid frame.
1111 ASSERT(flag == JUMP_FUNCTION || has_frame());
Andrei Popescu402d9372010-02-26 13:31:12 +00001112
1113 // Get the function and setup the context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001114 mov(r1, Operand(function));
Andrei Popescu402d9372010-02-26 13:31:12 +00001115 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1116
Andrei Popescu402d9372010-02-26 13:31:12 +00001117 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001118 // We call indirectly through the code field in the function to
1119 // allow recompilation to take effect without changing any of the
1120 // call sites.
1121 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1122 InvokeCode(r3, expected, actual, flag, NullCallWrapper(), call_kind);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001123}
1124
1125
1126void MacroAssembler::IsObjectJSObjectType(Register heap_object,
1127 Register map,
1128 Register scratch,
1129 Label* fail) {
1130 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
1131 IsInstanceJSObjectType(map, scratch, fail);
1132}
1133
1134
1135void MacroAssembler::IsInstanceJSObjectType(Register map,
1136 Register scratch,
1137 Label* fail) {
1138 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001139 cmp(scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001140 b(lt, fail);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001141 cmp(scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001142 b(gt, fail);
1143}
1144
1145
1146void MacroAssembler::IsObjectJSStringType(Register object,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001147 Register scratch,
1148 Label* fail) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001149 ASSERT(kNotStringTag != 0);
1150
1151 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
1152 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1153 tst(scratch, Operand(kIsNotStringMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001154 b(ne, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +00001155}
1156
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001157
Steve Blocka7e24c12009-10-30 11:49:00 +00001158#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +00001159void MacroAssembler::DebugBreak() {
Iain Merrick9ac36c92010-09-13 15:29:50 +01001160 mov(r0, Operand(0, RelocInfo::NONE));
Steve Block44f0eee2011-05-26 01:26:41 +01001161 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +00001162 CEntryStub ces(1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001163 ASSERT(AllowThisStubCall(&ces));
Andrei Popescu402d9372010-02-26 13:31:12 +00001164 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
1165}
Steve Blocka7e24c12009-10-30 11:49:00 +00001166#endif
1167
1168
1169void MacroAssembler::PushTryHandler(CodeLocation try_location,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001170 HandlerType type,
1171 int handler_index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001172 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001173 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1174 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001175 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1176 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1177 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1178 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001179
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001180 // For the JSEntry handler, we must preserve r0-r4, r5-r7 are available.
1181 // We will build up the handler from the bottom by pushing on the stack.
1182 // First compute the state.
1183 unsigned state = StackHandler::OffsetField::encode(handler_index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001184 if (try_location == IN_JAVASCRIPT) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001185 state |= (type == TRY_CATCH_HANDLER)
1186 ? StackHandler::KindField::encode(StackHandler::TRY_CATCH)
1187 : StackHandler::KindField::encode(StackHandler::TRY_FINALLY);
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00001189 ASSERT(try_location == IN_JS_ENTRY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001190 state |= StackHandler::KindField::encode(StackHandler::ENTRY);
Steve Blocka7e24c12009-10-30 11:49:00 +00001191 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001192
1193 // Set up the code object (r5) and the state (r6) for pushing.
1194 mov(r5, Operand(CodeObject()));
1195 mov(r6, Operand(state));
1196
1197 // Push the frame pointer, context, state, and code object.
1198 if (try_location == IN_JAVASCRIPT) {
1199 stm(db_w, sp, r5.bit() | r6.bit() | cp.bit() | fp.bit());
1200 } else {
1201 mov(r7, Operand(Smi::FromInt(0))); // Indicates no context.
1202 mov(ip, Operand(0, RelocInfo::NONE)); // NULL frame pointer.
1203 stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | ip.bit());
1204 }
1205
1206 // Link the current handler as the next handler.
1207 mov(r6, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1208 ldr(r5, MemOperand(r6));
1209 push(r5);
1210 // Set this new handler as the current one.
1211 str(sp, MemOperand(r6));
Steve Blocka7e24c12009-10-30 11:49:00 +00001212}
1213
1214
Leon Clarkee46be812010-01-19 14:06:41 +00001215void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001216 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Leon Clarkee46be812010-01-19 14:06:41 +00001217 pop(r1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001218 mov(ip, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +00001219 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1220 str(r1, MemOperand(ip));
1221}
1222
1223
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001224void MacroAssembler::JumpToHandlerEntry() {
1225 // Compute the handler entry address and jump to it. The handler table is
1226 // a fixed array of (smi-tagged) code offsets.
1227 // r0 = exception, r1 = code object, r2 = state.
1228 ldr(r3, FieldMemOperand(r1, Code::kHandlerTableOffset)); // Handler table.
1229 add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1230 mov(r2, Operand(r2, LSR, StackHandler::kKindWidth)); // Handler index.
1231 ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2)); // Smi-tagged offset.
1232 add(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag)); // Code start.
1233 add(pc, r1, Operand(r2, ASR, kSmiTagSize)); // Jump.
1234}
1235
1236
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001237void MacroAssembler::Throw(Register value) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001238 // Adjust this code if not the case.
1239 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001240 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1241 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1242 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1243 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1244 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
1245
1246 // The exception is expected in r0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001247 if (!value.is(r0)) {
1248 mov(r0, value);
1249 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001250 // Drop the stack pointer to the top of the top handler.
Ben Murdoch589d6972011-11-30 16:04:58 +00001251 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001252 ldr(sp, MemOperand(r3));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001253 // Restore the next handler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001254 pop(r2);
1255 str(r2, MemOperand(r3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001256
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001257 // Get the code object (r1) and state (r2). Restore the context and frame
1258 // pointer.
1259 ldm(ia_w, sp, r1.bit() | r2.bit() | cp.bit() | fp.bit());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001260
1261 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001262 // (kind == ENTRY) == (fp == 0) == (cp == 0), so we could test either fp
1263 // or cp.
1264 tst(cp, cp);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001265 str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1266
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001267 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001268}
1269
1270
1271void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
1272 Register value) {
1273 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001274 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1275 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001276 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1277 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1278 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1279 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001280
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001281 // The exception is expected in r0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001282 if (type == OUT_OF_MEMORY) {
1283 // Set external caught exception to false.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001284 ExternalReference external_caught(Isolate::kExternalCaughtExceptionAddress,
1285 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001286 mov(r0, Operand(false, RelocInfo::NONE));
1287 mov(r2, Operand(external_caught));
1288 str(r0, MemOperand(r2));
1289
1290 // Set pending exception and r0 to out of memory exception.
1291 Failure* out_of_memory = Failure::OutOfMemoryException();
1292 mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
Ben Murdoch589d6972011-11-30 16:04:58 +00001293 mov(r2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01001294 isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001295 str(r0, MemOperand(r2));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001296 } else if (!value.is(r0)) {
1297 mov(r0, value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001298 }
1299
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001300 // Drop the stack pointer to the top of the top stack handler.
1301 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1302 ldr(sp, MemOperand(r3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001303
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001304 // Unwind the handlers until the ENTRY handler is found.
1305 Label fetch_next, check_kind;
1306 jmp(&check_kind);
1307 bind(&fetch_next);
1308 ldr(sp, MemOperand(sp, StackHandlerConstants::kNextOffset));
1309
1310 bind(&check_kind);
1311 STATIC_ASSERT(StackHandler::ENTRY == 0);
1312 ldr(r2, MemOperand(sp, StackHandlerConstants::kStateOffset));
1313 tst(r2, Operand(StackHandler::KindField::kMask));
1314 b(ne, &fetch_next);
1315
1316 // Set the top handler address to next handler past the top ENTRY handler.
1317 pop(r2);
1318 str(r2, MemOperand(r3));
1319 // Get the code object (r1) and state (r2). Clear the context and frame
1320 // pointer (0 was saved in the handler).
1321 ldm(ia_w, sp, r1.bit() | r2.bit() | cp.bit() | fp.bit());
1322
1323 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001324}
1325
1326
Steve Blocka7e24c12009-10-30 11:49:00 +00001327void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1328 Register scratch,
1329 Label* miss) {
1330 Label same_contexts;
1331
1332 ASSERT(!holder_reg.is(scratch));
1333 ASSERT(!holder_reg.is(ip));
1334 ASSERT(!scratch.is(ip));
1335
1336 // Load current lexical context from the stack frame.
1337 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1338 // In debug mode, make sure the lexical context is set.
1339#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001340 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001341 Check(ne, "we should not have an empty lexical context");
1342#endif
1343
1344 // Load the global context of the current context.
1345 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1346 ldr(scratch, FieldMemOperand(scratch, offset));
1347 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1348
1349 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001350 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001351 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1352 // Cannot use ip as a temporary in this verification code. Due to the fact
1353 // that ip is clobbered as part of cmp with an object Operand.
1354 push(holder_reg); // Temporarily save holder on the stack.
1355 // Read the first word and compare to the global_context_map.
1356 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1357 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1358 cmp(holder_reg, ip);
1359 Check(eq, "JSGlobalObject::global_context should be a global context.");
1360 pop(holder_reg); // Restore holder.
1361 }
1362
1363 // Check if both contexts are the same.
1364 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1365 cmp(scratch, Operand(ip));
1366 b(eq, &same_contexts);
1367
1368 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001369 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1371 // Cannot use ip as a temporary in this verification code. Due to the fact
1372 // that ip is clobbered as part of cmp with an object Operand.
1373 push(holder_reg); // Temporarily save holder on the stack.
1374 mov(holder_reg, ip); // Move ip to its holding place.
1375 LoadRoot(ip, Heap::kNullValueRootIndex);
1376 cmp(holder_reg, ip);
1377 Check(ne, "JSGlobalProxy::context() should not be null.");
1378
1379 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1380 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1381 cmp(holder_reg, ip);
1382 Check(eq, "JSGlobalObject::global_context should be a global context.");
1383 // Restore ip is not needed. ip is reloaded below.
1384 pop(holder_reg); // Restore holder.
1385 // Restore ip to holder's context.
1386 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1387 }
1388
1389 // Check that the security token in the calling global object is
1390 // compatible with the security token in the receiving global
1391 // object.
1392 int token_offset = Context::kHeaderSize +
1393 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1394
1395 ldr(scratch, FieldMemOperand(scratch, token_offset));
1396 ldr(ip, FieldMemOperand(ip, token_offset));
1397 cmp(scratch, Operand(ip));
1398 b(ne, miss);
1399
1400 bind(&same_contexts);
1401}
1402
1403
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001404void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1405 Register elements,
1406 Register key,
1407 Register result,
1408 Register t0,
1409 Register t1,
1410 Register t2) {
1411 // Register use:
1412 //
1413 // elements - holds the slow-case elements of the receiver on entry.
1414 // Unchanged unless 'result' is the same register.
1415 //
1416 // key - holds the smi key on entry.
1417 // Unchanged unless 'result' is the same register.
1418 //
1419 // result - holds the result on exit if the load succeeded.
1420 // Allowed to be the same as 'key' or 'result'.
1421 // Unchanged on bailout so 'key' or 'result' can be used
1422 // in further computation.
1423 //
1424 // Scratch registers:
1425 //
1426 // t0 - holds the untagged key on entry and holds the hash once computed.
1427 //
1428 // t1 - used to hold the capacity mask of the dictionary
1429 //
1430 // t2 - used for the index into the dictionary.
1431 Label done;
1432
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001433 // Compute the hash code from the untagged key. This must be kept in sync
1434 // with ComputeIntegerHash in utils.h.
1435 //
1436 // hash = ~hash + (hash << 15);
1437 mvn(t1, Operand(t0));
1438 add(t0, t1, Operand(t0, LSL, 15));
1439 // hash = hash ^ (hash >> 12);
1440 eor(t0, t0, Operand(t0, LSR, 12));
1441 // hash = hash + (hash << 2);
1442 add(t0, t0, Operand(t0, LSL, 2));
1443 // hash = hash ^ (hash >> 4);
1444 eor(t0, t0, Operand(t0, LSR, 4));
1445 // hash = hash * 2057;
1446 mov(t1, Operand(2057));
1447 mul(t0, t0, t1);
1448 // hash = hash ^ (hash >> 16);
1449 eor(t0, t0, Operand(t0, LSR, 16));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001450
1451 // Compute the capacity mask.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001452 ldr(t1, FieldMemOperand(elements, NumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001453 mov(t1, Operand(t1, ASR, kSmiTagSize)); // convert smi to int
1454 sub(t1, t1, Operand(1));
1455
1456 // Generate an unrolled loop that performs a few probes before giving up.
1457 static const int kProbes = 4;
1458 for (int i = 0; i < kProbes; i++) {
1459 // Use t2 for index calculations and keep the hash intact in t0.
1460 mov(t2, t0);
1461 // Compute the masked index: (hash + i + i * i) & mask.
1462 if (i > 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001463 add(t2, t2, Operand(NumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001464 }
1465 and_(t2, t2, Operand(t1));
1466
1467 // Scale the index by multiplying by the element size.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001468 ASSERT(NumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001469 add(t2, t2, Operand(t2, LSL, 1)); // t2 = t2 * 3
1470
1471 // Check if the key is identical to the name.
1472 add(t2, elements, Operand(t2, LSL, kPointerSizeLog2));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001473 ldr(ip, FieldMemOperand(t2, NumberDictionary::kElementsStartOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001474 cmp(key, Operand(ip));
1475 if (i != kProbes - 1) {
1476 b(eq, &done);
1477 } else {
1478 b(ne, miss);
1479 }
1480 }
1481
1482 bind(&done);
1483 // Check that the value is a normal property.
1484 // t2: elements + (index * kPointerSize)
1485 const int kDetailsOffset =
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001486 NumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001487 ldr(t1, FieldMemOperand(t2, kDetailsOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +00001488 tst(t1, Operand(Smi::FromInt(PropertyDetails::TypeField::kMask)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001489 b(ne, miss);
1490
1491 // Get the value at the masked, scaled index and return.
1492 const int kValueOffset =
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001493 NumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001494 ldr(result, FieldMemOperand(t2, kValueOffset));
1495}
1496
1497
Steve Blocka7e24c12009-10-30 11:49:00 +00001498void MacroAssembler::AllocateInNewSpace(int object_size,
1499 Register result,
1500 Register scratch1,
1501 Register scratch2,
1502 Label* gc_required,
1503 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001504 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001505 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001506 // Trash the registers to simulate an allocation failure.
1507 mov(result, Operand(0x7091));
1508 mov(scratch1, Operand(0x7191));
1509 mov(scratch2, Operand(0x7291));
1510 }
1511 jmp(gc_required);
1512 return;
1513 }
1514
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001516 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 ASSERT(!scratch1.is(scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001518 ASSERT(!scratch1.is(ip));
1519 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001520
Kristian Monsen25f61362010-05-21 11:50:48 +01001521 // Make object size into bytes.
1522 if ((flags & SIZE_IN_WORDS) != 0) {
1523 object_size *= kPointerSize;
1524 }
1525 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1526
Ben Murdochb0fe1622011-05-05 13:52:32 +01001527 // Check relative positions of allocation top and limit addresses.
1528 // The values must be adjacent in memory to allow the use of LDM.
1529 // Also, assert that the registers are numbered such that the values
1530 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001532 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001533 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001534 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001535 intptr_t top =
1536 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1537 intptr_t limit =
1538 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1539 ASSERT((limit - top) == kPointerSize);
1540 ASSERT(result.code() < ip.code());
1541
1542 // Set up allocation top address and object size registers.
1543 Register topaddr = scratch1;
1544 Register obj_size_reg = scratch2;
1545 mov(topaddr, Operand(new_space_allocation_top));
1546 mov(obj_size_reg, Operand(object_size));
1547
1548 // This code stores a temporary value in ip. This is OK, as the code below
1549 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001550 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001551 // Load allocation top into result and allocation limit into ip.
1552 ldm(ia, topaddr, result.bit() | ip.bit());
1553 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001554 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001555 // Assert that result actually contains top on entry. ip is used
1556 // immediately below so this use of ip does not cause difference with
1557 // respect to register content between debug and release mode.
1558 ldr(ip, MemOperand(topaddr));
1559 cmp(result, ip);
1560 Check(eq, "Unexpected allocation top");
1561 }
1562 // Load allocation limit into ip. Result already contains allocation top.
1563 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001564 }
1565
1566 // Calculate new top and bail out if new space is exhausted. Use result
1567 // to calculate the new top.
Steve Block1e0659c2011-05-24 12:43:12 +01001568 add(scratch2, result, Operand(obj_size_reg), SetCC);
1569 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001570 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001571 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001572 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001573
Ben Murdochb0fe1622011-05-05 13:52:32 +01001574 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001575 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001576 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001577 }
1578}
1579
1580
1581void MacroAssembler::AllocateInNewSpace(Register object_size,
1582 Register result,
1583 Register scratch1,
1584 Register scratch2,
1585 Label* gc_required,
1586 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001587 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001588 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001589 // Trash the registers to simulate an allocation failure.
1590 mov(result, Operand(0x7091));
1591 mov(scratch1, Operand(0x7191));
1592 mov(scratch2, Operand(0x7291));
1593 }
1594 jmp(gc_required);
1595 return;
1596 }
1597
Ben Murdochb0fe1622011-05-05 13:52:32 +01001598 // Assert that the register arguments are different and that none of
1599 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001600 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001601 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001602 ASSERT(!scratch1.is(scratch2));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001603 ASSERT(!object_size.is(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001604 ASSERT(!result.is(ip));
1605 ASSERT(!scratch1.is(ip));
1606 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001607
Ben Murdochb0fe1622011-05-05 13:52:32 +01001608 // Check relative positions of allocation top and limit addresses.
1609 // The values must be adjacent in memory to allow the use of LDM.
1610 // Also, assert that the registers are numbered such that the values
1611 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001612 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001613 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001614 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001615 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001616 intptr_t top =
1617 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1618 intptr_t limit =
1619 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1620 ASSERT((limit - top) == kPointerSize);
1621 ASSERT(result.code() < ip.code());
1622
1623 // Set up allocation top address.
1624 Register topaddr = scratch1;
1625 mov(topaddr, Operand(new_space_allocation_top));
1626
1627 // This code stores a temporary value in ip. This is OK, as the code below
1628 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001629 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001630 // Load allocation top into result and allocation limit into ip.
1631 ldm(ia, topaddr, result.bit() | ip.bit());
1632 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001633 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001634 // Assert that result actually contains top on entry. ip is used
1635 // immediately below so this use of ip does not cause difference with
1636 // respect to register content between debug and release mode.
1637 ldr(ip, MemOperand(topaddr));
1638 cmp(result, ip);
1639 Check(eq, "Unexpected allocation top");
1640 }
1641 // Load allocation limit into ip. Result already contains allocation top.
1642 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001643 }
1644
1645 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001646 // to calculate the new top. Object size may be in words so a shift is
1647 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001648 if ((flags & SIZE_IN_WORDS) != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001649 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001650 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001651 add(scratch2, result, Operand(object_size), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001652 }
Steve Block1e0659c2011-05-24 12:43:12 +01001653 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001654 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001655 b(hi, gc_required);
1656
Steve Blockd0582a62009-12-15 09:54:21 +00001657 // Update allocation top. result temporarily holds the new top.
Steve Block44f0eee2011-05-26 01:26:41 +01001658 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001659 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001660 Check(eq, "Unaligned allocation in new space");
1661 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001662 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001663
1664 // Tag object if requested.
1665 if ((flags & TAG_OBJECT) != 0) {
1666 add(result, result, Operand(kHeapObjectTag));
1667 }
1668}
1669
1670
1671void MacroAssembler::UndoAllocationInNewSpace(Register object,
1672 Register scratch) {
1673 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001674 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001675
1676 // Make sure the object has no tag before resetting top.
1677 and_(object, object, Operand(~kHeapObjectTagMask));
1678#ifdef DEBUG
1679 // Check that the object un-allocated is below the current top.
1680 mov(scratch, Operand(new_space_allocation_top));
1681 ldr(scratch, MemOperand(scratch));
1682 cmp(object, scratch);
1683 Check(lt, "Undo allocation of non allocated memory");
1684#endif
1685 // Write the address of the object to un-allocate as the current top.
1686 mov(scratch, Operand(new_space_allocation_top));
1687 str(object, MemOperand(scratch));
1688}
1689
1690
Andrei Popescu31002712010-02-23 13:46:05 +00001691void MacroAssembler::AllocateTwoByteString(Register result,
1692 Register length,
1693 Register scratch1,
1694 Register scratch2,
1695 Register scratch3,
1696 Label* gc_required) {
1697 // Calculate the number of bytes needed for the characters in the string while
1698 // observing object alignment.
1699 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1700 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1701 add(scratch1, scratch1,
1702 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001703 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001704
1705 // Allocate two-byte string in new space.
1706 AllocateInNewSpace(scratch1,
1707 result,
1708 scratch2,
1709 scratch3,
1710 gc_required,
1711 TAG_OBJECT);
1712
1713 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001714 InitializeNewString(result,
1715 length,
1716 Heap::kStringMapRootIndex,
1717 scratch1,
1718 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001719}
1720
1721
1722void MacroAssembler::AllocateAsciiString(Register result,
1723 Register length,
1724 Register scratch1,
1725 Register scratch2,
1726 Register scratch3,
1727 Label* gc_required) {
1728 // Calculate the number of bytes needed for the characters in the string while
1729 // observing object alignment.
1730 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1731 ASSERT(kCharSize == 1);
1732 add(scratch1, length,
1733 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001734 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001735
1736 // Allocate ASCII string in new space.
1737 AllocateInNewSpace(scratch1,
1738 result,
1739 scratch2,
1740 scratch3,
1741 gc_required,
1742 TAG_OBJECT);
1743
1744 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001745 InitializeNewString(result,
1746 length,
1747 Heap::kAsciiStringMapRootIndex,
1748 scratch1,
1749 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001750}
1751
1752
1753void MacroAssembler::AllocateTwoByteConsString(Register result,
1754 Register length,
1755 Register scratch1,
1756 Register scratch2,
1757 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001758 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001759 result,
1760 scratch1,
1761 scratch2,
1762 gc_required,
1763 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001764
1765 InitializeNewString(result,
1766 length,
1767 Heap::kConsStringMapRootIndex,
1768 scratch1,
1769 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001770}
1771
1772
1773void MacroAssembler::AllocateAsciiConsString(Register result,
1774 Register length,
1775 Register scratch1,
1776 Register scratch2,
1777 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001778 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001779 result,
1780 scratch1,
1781 scratch2,
1782 gc_required,
1783 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001784
1785 InitializeNewString(result,
1786 length,
1787 Heap::kConsAsciiStringMapRootIndex,
1788 scratch1,
1789 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001790}
1791
1792
Ben Murdoch589d6972011-11-30 16:04:58 +00001793void MacroAssembler::AllocateTwoByteSlicedString(Register result,
1794 Register length,
1795 Register scratch1,
1796 Register scratch2,
1797 Label* gc_required) {
1798 AllocateInNewSpace(SlicedString::kSize,
1799 result,
1800 scratch1,
1801 scratch2,
1802 gc_required,
1803 TAG_OBJECT);
1804
1805 InitializeNewString(result,
1806 length,
1807 Heap::kSlicedStringMapRootIndex,
1808 scratch1,
1809 scratch2);
1810}
1811
1812
1813void MacroAssembler::AllocateAsciiSlicedString(Register result,
1814 Register length,
1815 Register scratch1,
1816 Register scratch2,
1817 Label* gc_required) {
1818 AllocateInNewSpace(SlicedString::kSize,
1819 result,
1820 scratch1,
1821 scratch2,
1822 gc_required,
1823 TAG_OBJECT);
1824
1825 InitializeNewString(result,
1826 length,
1827 Heap::kSlicedAsciiStringMapRootIndex,
1828 scratch1,
1829 scratch2);
1830}
1831
1832
Steve Block6ded16b2010-05-10 14:33:55 +01001833void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001834 Register map,
1835 Register type_reg,
1836 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001837 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001838 CompareInstanceType(map, type_reg, type);
1839}
1840
1841
1842void MacroAssembler::CompareInstanceType(Register map,
1843 Register type_reg,
1844 InstanceType type) {
1845 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1846 cmp(type_reg, Operand(type));
1847}
1848
1849
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001850void MacroAssembler::CompareRoot(Register obj,
1851 Heap::RootListIndex index) {
1852 ASSERT(!obj.is(ip));
1853 LoadRoot(ip, index);
1854 cmp(obj, ip);
1855}
1856
1857
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001858void MacroAssembler::CheckFastElements(Register map,
1859 Register scratch,
1860 Label* fail) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001861 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
1862 STATIC_ASSERT(FAST_ELEMENTS == 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001863 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1864 cmp(scratch, Operand(Map::kMaximumBitField2FastElementValue));
1865 b(hi, fail);
1866}
1867
1868
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001869void MacroAssembler::CheckFastObjectElements(Register map,
1870 Register scratch,
1871 Label* fail) {
1872 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
1873 STATIC_ASSERT(FAST_ELEMENTS == 1);
1874 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1875 cmp(scratch, Operand(Map::kMaximumBitField2FastSmiOnlyElementValue));
1876 b(ls, fail);
1877 cmp(scratch, Operand(Map::kMaximumBitField2FastElementValue));
1878 b(hi, fail);
1879}
1880
1881
1882void MacroAssembler::CheckFastSmiOnlyElements(Register map,
1883 Register scratch,
1884 Label* fail) {
1885 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
1886 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1887 cmp(scratch, Operand(Map::kMaximumBitField2FastSmiOnlyElementValue));
1888 b(hi, fail);
1889}
1890
1891
1892void MacroAssembler::StoreNumberToDoubleElements(Register value_reg,
1893 Register key_reg,
1894 Register receiver_reg,
1895 Register elements_reg,
1896 Register scratch1,
1897 Register scratch2,
1898 Register scratch3,
1899 Register scratch4,
1900 Label* fail) {
1901 Label smi_value, maybe_nan, have_double_value, is_nan, done;
1902 Register mantissa_reg = scratch2;
1903 Register exponent_reg = scratch3;
1904
1905 // Handle smi values specially.
1906 JumpIfSmi(value_reg, &smi_value);
1907
1908 // Ensure that the object is a heap number
1909 CheckMap(value_reg,
1910 scratch1,
1911 isolate()->factory()->heap_number_map(),
1912 fail,
1913 DONT_DO_SMI_CHECK);
1914
1915 // Check for nan: all NaN values have a value greater (signed) than 0x7ff00000
1916 // in the exponent.
1917 mov(scratch1, Operand(kNaNOrInfinityLowerBoundUpper32));
1918 ldr(exponent_reg, FieldMemOperand(value_reg, HeapNumber::kExponentOffset));
1919 cmp(exponent_reg, scratch1);
1920 b(ge, &maybe_nan);
1921
1922 ldr(mantissa_reg, FieldMemOperand(value_reg, HeapNumber::kMantissaOffset));
1923
1924 bind(&have_double_value);
1925 add(scratch1, elements_reg,
1926 Operand(key_reg, LSL, kDoubleSizeLog2 - kSmiTagSize));
1927 str(mantissa_reg, FieldMemOperand(scratch1, FixedDoubleArray::kHeaderSize));
1928 uint32_t offset = FixedDoubleArray::kHeaderSize + sizeof(kHoleNanLower32);
1929 str(exponent_reg, FieldMemOperand(scratch1, offset));
1930 jmp(&done);
1931
1932 bind(&maybe_nan);
1933 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
1934 // it's an Infinity, and the non-NaN code path applies.
1935 b(gt, &is_nan);
1936 ldr(mantissa_reg, FieldMemOperand(value_reg, HeapNumber::kMantissaOffset));
1937 cmp(mantissa_reg, Operand(0));
1938 b(eq, &have_double_value);
1939 bind(&is_nan);
1940 // Load canonical NaN for storing into the double array.
1941 uint64_t nan_int64 = BitCast<uint64_t>(
1942 FixedDoubleArray::canonical_not_the_hole_nan_as_double());
1943 mov(mantissa_reg, Operand(static_cast<uint32_t>(nan_int64)));
1944 mov(exponent_reg, Operand(static_cast<uint32_t>(nan_int64 >> 32)));
1945 jmp(&have_double_value);
1946
1947 bind(&smi_value);
1948 add(scratch1, elements_reg,
1949 Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag));
1950 add(scratch1, scratch1,
1951 Operand(key_reg, LSL, kDoubleSizeLog2 - kSmiTagSize));
1952 // scratch1 is now effective address of the double element
1953
1954 FloatingPointHelper::Destination destination;
1955 if (CpuFeatures::IsSupported(VFP3)) {
1956 destination = FloatingPointHelper::kVFPRegisters;
1957 } else {
1958 destination = FloatingPointHelper::kCoreRegisters;
1959 }
1960
1961 Register untagged_value = receiver_reg;
1962 SmiUntag(untagged_value, value_reg);
1963 FloatingPointHelper::ConvertIntToDouble(this,
1964 untagged_value,
1965 destination,
1966 d0,
1967 mantissa_reg,
1968 exponent_reg,
1969 scratch4,
1970 s2);
1971 if (destination == FloatingPointHelper::kVFPRegisters) {
1972 CpuFeatures::Scope scope(VFP3);
1973 vstr(d0, scratch1, 0);
1974 } else {
1975 str(mantissa_reg, MemOperand(scratch1, 0));
1976 str(exponent_reg, MemOperand(scratch1, Register::kSizeInBytes));
1977 }
1978 bind(&done);
1979}
1980
1981
Andrei Popescu31002712010-02-23 13:46:05 +00001982void MacroAssembler::CheckMap(Register obj,
1983 Register scratch,
1984 Handle<Map> map,
1985 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00001986 SmiCheckType smi_check_type) {
1987 if (smi_check_type == DO_SMI_CHECK) {
Steve Block1e0659c2011-05-24 12:43:12 +01001988 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +00001989 }
1990 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1991 mov(ip, Operand(map));
1992 cmp(scratch, ip);
1993 b(ne, fail);
1994}
1995
1996
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001997void MacroAssembler::CheckMap(Register obj,
1998 Register scratch,
1999 Heap::RootListIndex index,
2000 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00002001 SmiCheckType smi_check_type) {
2002 if (smi_check_type == DO_SMI_CHECK) {
Steve Block1e0659c2011-05-24 12:43:12 +01002003 JumpIfSmi(obj, fail);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002004 }
2005 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2006 LoadRoot(ip, index);
2007 cmp(scratch, ip);
2008 b(ne, fail);
2009}
2010
2011
Ben Murdoch257744e2011-11-30 15:57:28 +00002012void MacroAssembler::DispatchMap(Register obj,
2013 Register scratch,
2014 Handle<Map> map,
2015 Handle<Code> success,
2016 SmiCheckType smi_check_type) {
2017 Label fail;
2018 if (smi_check_type == DO_SMI_CHECK) {
2019 JumpIfSmi(obj, &fail);
2020 }
2021 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2022 mov(ip, Operand(map));
2023 cmp(scratch, ip);
2024 Jump(success, RelocInfo::CODE_TARGET, eq);
2025 bind(&fail);
2026}
2027
2028
Steve Blocka7e24c12009-10-30 11:49:00 +00002029void MacroAssembler::TryGetFunctionPrototype(Register function,
2030 Register result,
2031 Register scratch,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002032 Label* miss,
2033 bool miss_on_bound_function) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 // Check that the receiver isn't a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002035 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00002036
2037 // Check that the function really is a function. Load map into result reg.
2038 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
2039 b(ne, miss);
2040
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002041 if (miss_on_bound_function) {
2042 ldr(scratch,
2043 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
2044 ldr(scratch,
2045 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
2046 tst(scratch,
2047 Operand(Smi::FromInt(1 << SharedFunctionInfo::kBoundFunction)));
2048 b(ne, miss);
2049 }
2050
Steve Blocka7e24c12009-10-30 11:49:00 +00002051 // Make sure that the function has an instance prototype.
2052 Label non_instance;
2053 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
2054 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
2055 b(ne, &non_instance);
2056
2057 // Get the prototype or initial map from the function.
2058 ldr(result,
2059 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2060
2061 // If the prototype or initial map is the hole, don't return it and
2062 // simply miss the cache instead. This will allow us to allocate a
2063 // prototype object on-demand in the runtime system.
2064 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2065 cmp(result, ip);
2066 b(eq, miss);
2067
2068 // If the function does not have an initial map, we're done.
2069 Label done;
2070 CompareObjectType(result, scratch, scratch, MAP_TYPE);
2071 b(ne, &done);
2072
2073 // Get the prototype from the initial map.
2074 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
2075 jmp(&done);
2076
2077 // Non-instance prototype: Fetch prototype from constructor field
2078 // in initial map.
2079 bind(&non_instance);
2080 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
2081
2082 // All done.
2083 bind(&done);
2084}
2085
2086
2087void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002088 ASSERT(AllowThisStubCall(stub)); // Stub calls are not allowed in some stubs.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002089 Call(stub->GetCode(), RelocInfo::CODE_TARGET, kNoASTId, cond);
Steve Blocka7e24c12009-10-30 11:49:00 +00002090}
2091
2092
Andrei Popescu31002712010-02-23 13:46:05 +00002093void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002094 ASSERT(allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe());
Andrei Popescu31002712010-02-23 13:46:05 +00002095 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
2096}
2097
2098
Steve Block1e0659c2011-05-24 12:43:12 +01002099static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
2100 return ref0.address() - ref1.address();
2101}
2102
2103
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002104void MacroAssembler::CallApiFunctionAndReturn(ExternalReference function,
2105 int stack_space) {
Steve Block1e0659c2011-05-24 12:43:12 +01002106 ExternalReference next_address =
2107 ExternalReference::handle_scope_next_address();
2108 const int kNextOffset = 0;
2109 const int kLimitOffset = AddressOffset(
2110 ExternalReference::handle_scope_limit_address(),
2111 next_address);
2112 const int kLevelOffset = AddressOffset(
2113 ExternalReference::handle_scope_level_address(),
2114 next_address);
2115
2116 // Allocate HandleScope in callee-save registers.
2117 mov(r7, Operand(next_address));
2118 ldr(r4, MemOperand(r7, kNextOffset));
2119 ldr(r5, MemOperand(r7, kLimitOffset));
2120 ldr(r6, MemOperand(r7, kLevelOffset));
2121 add(r6, r6, Operand(1));
2122 str(r6, MemOperand(r7, kLevelOffset));
2123
2124 // Native call returns to the DirectCEntry stub which redirects to the
2125 // return address pushed on stack (could have moved after GC).
2126 // DirectCEntry stub itself is generated early and never moves.
2127 DirectCEntryStub stub;
2128 stub.GenerateCall(this, function);
2129
2130 Label promote_scheduled_exception;
2131 Label delete_allocated_handles;
2132 Label leave_exit_frame;
2133
2134 // If result is non-zero, dereference to get the result value
2135 // otherwise set it to undefined.
2136 cmp(r0, Operand(0));
2137 LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
2138 ldr(r0, MemOperand(r0), ne);
2139
2140 // No more valid handles (the result handle was the last one). Restore
2141 // previous handle scope.
2142 str(r4, MemOperand(r7, kNextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002143 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002144 ldr(r1, MemOperand(r7, kLevelOffset));
2145 cmp(r1, r6);
2146 Check(eq, "Unexpected level after return from api call");
2147 }
2148 sub(r6, r6, Operand(1));
2149 str(r6, MemOperand(r7, kLevelOffset));
2150 ldr(ip, MemOperand(r7, kLimitOffset));
2151 cmp(r5, ip);
2152 b(ne, &delete_allocated_handles);
2153
2154 // Check if the function scheduled an exception.
2155 bind(&leave_exit_frame);
2156 LoadRoot(r4, Heap::kTheHoleValueRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01002157 mov(ip, Operand(ExternalReference::scheduled_exception_address(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +01002158 ldr(r5, MemOperand(ip));
2159 cmp(r4, r5);
2160 b(ne, &promote_scheduled_exception);
2161
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002162 // LeaveExitFrame expects unwind space to be in a register.
Steve Block1e0659c2011-05-24 12:43:12 +01002163 mov(r4, Operand(stack_space));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002164 LeaveExitFrame(false, r4);
2165 mov(pc, lr);
Steve Block1e0659c2011-05-24 12:43:12 +01002166
2167 bind(&promote_scheduled_exception);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002168 TailCallExternalReference(
2169 ExternalReference(Runtime::kPromoteScheduledException, isolate()),
2170 0,
2171 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002172
2173 // HandleScope limit has changed. Delete allocated extensions.
2174 bind(&delete_allocated_handles);
2175 str(r5, MemOperand(r7, kLimitOffset));
2176 mov(r4, r0);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002177 PrepareCallCFunction(1, r5);
2178 mov(r0, Operand(ExternalReference::isolate_address()));
Steve Block44f0eee2011-05-26 01:26:41 +01002179 CallCFunction(
Ben Murdoch8b112d22011-06-08 16:22:53 +01002180 ExternalReference::delete_handle_scope_extensions(isolate()), 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002181 mov(r0, r4);
2182 jmp(&leave_exit_frame);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002183}
Steve Block1e0659c2011-05-24 12:43:12 +01002184
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002185
2186bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
2187 if (!has_frame_ && stub->SometimesSetsUpAFrame()) return false;
2188 return allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe();
Steve Block1e0659c2011-05-24 12:43:12 +01002189}
2190
2191
Steve Blocka7e24c12009-10-30 11:49:00 +00002192void MacroAssembler::IllegalOperation(int num_arguments) {
2193 if (num_arguments > 0) {
2194 add(sp, sp, Operand(num_arguments * kPointerSize));
2195 }
2196 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2197}
2198
2199
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002200void MacroAssembler::IndexFromHash(Register hash, Register index) {
2201 // If the hash field contains an array index pick it out. The assert checks
2202 // that the constants for the maximum number of digits for an array index
2203 // cached in the hash field and the number of bits reserved for it does not
2204 // conflict.
2205 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
2206 (1 << String::kArrayIndexValueBits));
2207 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
2208 // the low kHashShift bits.
2209 STATIC_ASSERT(kSmiTag == 0);
2210 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
2211 mov(index, Operand(hash, LSL, kSmiTagSize));
2212}
2213
2214
Steve Blockd0582a62009-12-15 09:54:21 +00002215void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
2216 Register outHighReg,
2217 Register outLowReg) {
2218 // ARMv7 VFP3 instructions to implement integer to double conversion.
2219 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00002220 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01002221 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00002222 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00002223}
2224
2225
Steve Block8defd9f2010-07-08 12:39:36 +01002226void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
2227 DwVfpRegister result,
2228 Register scratch1,
2229 Register scratch2,
2230 Register heap_number_map,
2231 SwVfpRegister scratch3,
2232 Label* not_number,
2233 ObjectToDoubleFlags flags) {
2234 Label done;
2235 if ((flags & OBJECT_NOT_SMI) == 0) {
2236 Label not_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01002237 JumpIfNotSmi(object, &not_smi);
Steve Block8defd9f2010-07-08 12:39:36 +01002238 // Remove smi tag and convert to double.
2239 mov(scratch1, Operand(object, ASR, kSmiTagSize));
2240 vmov(scratch3, scratch1);
2241 vcvt_f64_s32(result, scratch3);
2242 b(&done);
2243 bind(&not_smi);
2244 }
2245 // Check for heap number and load double value from it.
2246 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
2247 sub(scratch2, object, Operand(kHeapObjectTag));
2248 cmp(scratch1, heap_number_map);
2249 b(ne, not_number);
2250 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
2251 // If exponent is all ones the number is either a NaN or +/-Infinity.
2252 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
2253 Sbfx(scratch1,
2254 scratch1,
2255 HeapNumber::kExponentShift,
2256 HeapNumber::kExponentBits);
2257 // All-one value sign extend to -1.
2258 cmp(scratch1, Operand(-1));
2259 b(eq, not_number);
2260 }
2261 vldr(result, scratch2, HeapNumber::kValueOffset);
2262 bind(&done);
2263}
2264
2265
2266void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
2267 DwVfpRegister value,
2268 Register scratch1,
2269 SwVfpRegister scratch2) {
2270 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
2271 vmov(scratch2, scratch1);
2272 vcvt_f64_s32(value, scratch2);
2273}
2274
2275
Iain Merrick9ac36c92010-09-13 15:29:50 +01002276// Tries to get a signed int32 out of a double precision floating point heap
2277// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
2278// 32bits signed integer range.
2279void MacroAssembler::ConvertToInt32(Register source,
2280 Register dest,
2281 Register scratch,
2282 Register scratch2,
Steve Block1e0659c2011-05-24 12:43:12 +01002283 DwVfpRegister double_scratch,
Iain Merrick9ac36c92010-09-13 15:29:50 +01002284 Label *not_int32) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002285 if (CpuFeatures::IsSupported(VFP3)) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01002286 CpuFeatures::Scope scope(VFP3);
2287 sub(scratch, source, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01002288 vldr(double_scratch, scratch, HeapNumber::kValueOffset);
2289 vcvt_s32_f64(double_scratch.low(), double_scratch);
2290 vmov(dest, double_scratch.low());
Iain Merrick9ac36c92010-09-13 15:29:50 +01002291 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
2292 // maximun (0x7fffffff) signed 32bits integer when the double is out of
2293 // range. When substracting one, the minimum signed integer becomes the
2294 // maximun signed integer.
2295 sub(scratch, dest, Operand(1));
2296 cmp(scratch, Operand(LONG_MAX - 1));
2297 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
2298 b(ge, not_int32);
2299 } else {
2300 // This code is faster for doubles that are in the ranges -0x7fffffff to
2301 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
2302 // the range of signed int32 values that are not Smis. Jumps to the label
2303 // 'not_int32' if the double isn't in the range -0x80000000.0 to
2304 // 0x80000000.0 (excluding the endpoints).
2305 Label right_exponent, done;
2306 // Get exponent word.
2307 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
2308 // Get exponent alone in scratch2.
2309 Ubfx(scratch2,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002310 scratch,
2311 HeapNumber::kExponentShift,
2312 HeapNumber::kExponentBits);
Iain Merrick9ac36c92010-09-13 15:29:50 +01002313 // Load dest with zero. We use this either for the final shift or
2314 // for the answer.
2315 mov(dest, Operand(0, RelocInfo::NONE));
2316 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
2317 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
2318 // the exponent that we are fastest at and also the highest exponent we can
2319 // handle here.
2320 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
2321 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
2322 // split it up to avoid a constant pool entry. You can't do that in general
2323 // for cmp because of the overflow flag, but we know the exponent is in the
2324 // range 0-2047 so there is no overflow.
2325 int fudge_factor = 0x400;
2326 sub(scratch2, scratch2, Operand(fudge_factor));
2327 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
2328 // If we have a match of the int32-but-not-Smi exponent then skip some
2329 // logic.
2330 b(eq, &right_exponent);
2331 // If the exponent is higher than that then go to slow case. This catches
2332 // numbers that don't fit in a signed int32, infinities and NaNs.
2333 b(gt, not_int32);
2334
2335 // We know the exponent is smaller than 30 (biased). If it is less than
2336 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
2337 // it rounds to zero.
2338 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
2339 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
2340 // Dest already has a Smi zero.
2341 b(lt, &done);
2342
2343 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
2344 // get how much to shift down.
2345 rsb(dest, scratch2, Operand(30));
2346
2347 bind(&right_exponent);
2348 // Get the top bits of the mantissa.
2349 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
2350 // Put back the implicit 1.
2351 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
2352 // Shift up the mantissa bits to take up the space the exponent used to
2353 // take. We just orred in the implicit bit so that took care of one and
2354 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
2355 // distance.
2356 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
2357 mov(scratch2, Operand(scratch2, LSL, shift_distance));
2358 // Put sign in zero flag.
2359 tst(scratch, Operand(HeapNumber::kSignMask));
2360 // Get the second half of the double. For some exponents we don't
2361 // actually need this because the bits get shifted out again, but
2362 // it's probably slower to test than just to do it.
2363 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
2364 // Shift down 22 bits to get the last 10 bits.
2365 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
2366 // Move down according to the exponent.
2367 mov(dest, Operand(scratch, LSR, dest));
2368 // Fix sign if sign bit was set.
2369 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
2370 bind(&done);
2371 }
2372}
2373
2374
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002375void MacroAssembler::EmitVFPTruncate(VFPRoundingMode rounding_mode,
2376 SwVfpRegister result,
2377 DwVfpRegister double_input,
2378 Register scratch1,
2379 Register scratch2,
2380 CheckForInexactConversion check_inexact) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002381 ASSERT(CpuFeatures::IsSupported(VFP3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002382 CpuFeatures::Scope scope(VFP3);
2383 Register prev_fpscr = scratch1;
2384 Register scratch = scratch2;
2385
2386 int32_t check_inexact_conversion =
2387 (check_inexact == kCheckForInexactConversion) ? kVFPInexactExceptionBit : 0;
2388
2389 // Set custom FPCSR:
2390 // - Set rounding mode.
2391 // - Clear vfp cumulative exception flags.
2392 // - Make sure Flush-to-zero mode control bit is unset.
2393 vmrs(prev_fpscr);
2394 bic(scratch,
2395 prev_fpscr,
2396 Operand(kVFPExceptionMask |
2397 check_inexact_conversion |
2398 kVFPRoundingModeMask |
2399 kVFPFlushToZeroMask));
2400 // 'Round To Nearest' is encoded by 0b00 so no bits need to be set.
2401 if (rounding_mode != kRoundToNearest) {
2402 orr(scratch, scratch, Operand(rounding_mode));
2403 }
2404 vmsr(scratch);
2405
2406 // Convert the argument to an integer.
2407 vcvt_s32_f64(result,
2408 double_input,
2409 (rounding_mode == kRoundToZero) ? kDefaultRoundToZero
2410 : kFPSCRRounding);
2411
2412 // Retrieve FPSCR.
2413 vmrs(scratch);
2414 // Restore FPSCR.
2415 vmsr(prev_fpscr);
2416 // Check for vfp exceptions.
2417 tst(scratch, Operand(kVFPExceptionMask | check_inexact_conversion));
2418}
2419
2420
Steve Block44f0eee2011-05-26 01:26:41 +01002421void MacroAssembler::EmitOutOfInt32RangeTruncate(Register result,
2422 Register input_high,
2423 Register input_low,
2424 Register scratch) {
2425 Label done, normal_exponent, restore_sign;
2426
2427 // Extract the biased exponent in result.
2428 Ubfx(result,
2429 input_high,
2430 HeapNumber::kExponentShift,
2431 HeapNumber::kExponentBits);
2432
2433 // Check for Infinity and NaNs, which should return 0.
2434 cmp(result, Operand(HeapNumber::kExponentMask));
2435 mov(result, Operand(0), LeaveCC, eq);
2436 b(eq, &done);
2437
2438 // Express exponent as delta to (number of mantissa bits + 31).
2439 sub(result,
2440 result,
2441 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31),
2442 SetCC);
2443
2444 // If the delta is strictly positive, all bits would be shifted away,
2445 // which means that we can return 0.
2446 b(le, &normal_exponent);
2447 mov(result, Operand(0));
2448 b(&done);
2449
2450 bind(&normal_exponent);
2451 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
2452 // Calculate shift.
2453 add(scratch, result, Operand(kShiftBase + HeapNumber::kMantissaBits), SetCC);
2454
2455 // Save the sign.
2456 Register sign = result;
2457 result = no_reg;
2458 and_(sign, input_high, Operand(HeapNumber::kSignMask));
2459
2460 // Set the implicit 1 before the mantissa part in input_high.
2461 orr(input_high,
2462 input_high,
2463 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
2464 // Shift the mantissa bits to the correct position.
2465 // We don't need to clear non-mantissa bits as they will be shifted away.
2466 // If they weren't, it would mean that the answer is in the 32bit range.
2467 mov(input_high, Operand(input_high, LSL, scratch));
2468
2469 // Replace the shifted bits with bits from the lower mantissa word.
2470 Label pos_shift, shift_done;
2471 rsb(scratch, scratch, Operand(32), SetCC);
2472 b(&pos_shift, ge);
2473
2474 // Negate scratch.
2475 rsb(scratch, scratch, Operand(0));
2476 mov(input_low, Operand(input_low, LSL, scratch));
2477 b(&shift_done);
2478
2479 bind(&pos_shift);
2480 mov(input_low, Operand(input_low, LSR, scratch));
2481
2482 bind(&shift_done);
2483 orr(input_high, input_high, Operand(input_low));
2484 // Restore sign if necessary.
2485 cmp(sign, Operand(0));
2486 result = sign;
2487 sign = no_reg;
2488 rsb(result, input_high, Operand(0), LeaveCC, ne);
2489 mov(result, input_high, LeaveCC, eq);
2490 bind(&done);
2491}
2492
2493
2494void MacroAssembler::EmitECMATruncate(Register result,
2495 DwVfpRegister double_input,
2496 SwVfpRegister single_scratch,
2497 Register scratch,
2498 Register input_high,
2499 Register input_low) {
2500 CpuFeatures::Scope scope(VFP3);
2501 ASSERT(!input_high.is(result));
2502 ASSERT(!input_low.is(result));
2503 ASSERT(!input_low.is(input_high));
2504 ASSERT(!scratch.is(result) &&
2505 !scratch.is(input_high) &&
2506 !scratch.is(input_low));
2507 ASSERT(!single_scratch.is(double_input.low()) &&
2508 !single_scratch.is(double_input.high()));
2509
2510 Label done;
2511
2512 // Clear cumulative exception flags.
2513 ClearFPSCRBits(kVFPExceptionMask, scratch);
2514 // Try a conversion to a signed integer.
2515 vcvt_s32_f64(single_scratch, double_input);
2516 vmov(result, single_scratch);
2517 // Retrieve he FPSCR.
2518 vmrs(scratch);
2519 // Check for overflow and NaNs.
2520 tst(scratch, Operand(kVFPOverflowExceptionBit |
2521 kVFPUnderflowExceptionBit |
2522 kVFPInvalidOpExceptionBit));
2523 // If we had no exceptions we are done.
2524 b(eq, &done);
2525
2526 // Load the double value and perform a manual truncation.
2527 vmov(input_low, input_high, double_input);
2528 EmitOutOfInt32RangeTruncate(result,
2529 input_high,
2530 input_low,
2531 scratch);
2532 bind(&done);
2533}
2534
2535
Andrei Popescu31002712010-02-23 13:46:05 +00002536void MacroAssembler::GetLeastBitsFromSmi(Register dst,
2537 Register src,
2538 int num_least_bits) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002539 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002540 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00002541 } else {
2542 mov(dst, Operand(src, ASR, kSmiTagSize));
2543 and_(dst, dst, Operand((1 << num_least_bits) - 1));
2544 }
2545}
2546
2547
Steve Block1e0659c2011-05-24 12:43:12 +01002548void MacroAssembler::GetLeastBitsFromInt32(Register dst,
2549 Register src,
2550 int num_least_bits) {
2551 and_(dst, src, Operand((1 << num_least_bits) - 1));
2552}
2553
2554
Steve Block44f0eee2011-05-26 01:26:41 +01002555void MacroAssembler::CallRuntime(const Runtime::Function* f,
2556 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002557 // All parameters are on the stack. r0 has the return value after call.
2558
2559 // If the expected number of arguments of the runtime function is
2560 // constant, we check that the actual number of arguments match the
2561 // expectation.
2562 if (f->nargs >= 0 && f->nargs != num_arguments) {
2563 IllegalOperation(num_arguments);
2564 return;
2565 }
2566
Leon Clarke4515c472010-02-03 11:58:03 +00002567 // TODO(1236192): Most runtime routines don't need the number of
2568 // arguments passed in because it is constant. At some point we
2569 // should remove this need and make the runtime routine entry code
2570 // smarter.
2571 mov(r0, Operand(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01002572 mov(r1, Operand(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00002573 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002574 CallStub(&stub);
2575}
2576
2577
2578void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
2579 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
2580}
2581
2582
Ben Murdochb0fe1622011-05-05 13:52:32 +01002583void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01002584 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002585 mov(r0, Operand(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01002586 mov(r1, Operand(ExternalReference(function, isolate())));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002587 CEntryStub stub(1, kSaveFPRegs);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002588 CallStub(&stub);
2589}
2590
2591
Andrei Popescu402d9372010-02-26 13:31:12 +00002592void MacroAssembler::CallExternalReference(const ExternalReference& ext,
2593 int num_arguments) {
2594 mov(r0, Operand(num_arguments));
2595 mov(r1, Operand(ext));
2596
2597 CEntryStub stub(1);
2598 CallStub(&stub);
2599}
2600
2601
Steve Block6ded16b2010-05-10 14:33:55 +01002602void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2603 int num_arguments,
2604 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002605 // TODO(1236192): Most runtime routines don't need the number of
2606 // arguments passed in because it is constant. At some point we
2607 // should remove this need and make the runtime routine entry code
2608 // smarter.
2609 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002610 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00002611}
2612
2613
Steve Block6ded16b2010-05-10 14:33:55 +01002614void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2615 int num_arguments,
2616 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002617 TailCallExternalReference(ExternalReference(fid, isolate()),
2618 num_arguments,
2619 result_size);
Steve Block6ded16b2010-05-10 14:33:55 +01002620}
2621
2622
2623void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002624#if defined(__thumb__)
2625 // Thumb mode builtin.
2626 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2627#endif
2628 mov(r1, Operand(builtin));
2629 CEntryStub stub(1);
2630 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2631}
2632
2633
Steve Blocka7e24c12009-10-30 11:49:00 +00002634void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +00002635 InvokeFlag flag,
2636 const CallWrapper& call_wrapper) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002637 // You can't call a builtin without a valid frame.
2638 ASSERT(flag == JUMP_FUNCTION || has_frame());
2639
Andrei Popescu402d9372010-02-26 13:31:12 +00002640 GetBuiltinEntry(r2, id);
Ben Murdoch257744e2011-11-30 15:57:28 +00002641 if (flag == CALL_FUNCTION) {
2642 call_wrapper.BeforeCall(CallSize(r2));
2643 SetCallKind(r5, CALL_AS_METHOD);
Andrei Popescu402d9372010-02-26 13:31:12 +00002644 Call(r2);
Ben Murdoch257744e2011-11-30 15:57:28 +00002645 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00002646 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002647 ASSERT(flag == JUMP_FUNCTION);
2648 SetCallKind(r5, CALL_AS_METHOD);
Andrei Popescu402d9372010-02-26 13:31:12 +00002649 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002650 }
2651}
2652
2653
Steve Block791712a2010-08-27 10:21:07 +01002654void MacroAssembler::GetBuiltinFunction(Register target,
2655 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01002656 // Load the builtins object into target register.
2657 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2658 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00002659 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01002660 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01002661 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2662}
2663
2664
2665void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2666 ASSERT(!target.is(r1));
2667 GetBuiltinFunction(r1, id);
2668 // Load the code entry point from the builtins object.
2669 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002670}
2671
2672
2673void MacroAssembler::SetCounter(StatsCounter* counter, int value,
2674 Register scratch1, Register scratch2) {
2675 if (FLAG_native_code_counters && counter->Enabled()) {
2676 mov(scratch1, Operand(value));
2677 mov(scratch2, Operand(ExternalReference(counter)));
2678 str(scratch1, MemOperand(scratch2));
2679 }
2680}
2681
2682
2683void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2684 Register scratch1, Register scratch2) {
2685 ASSERT(value > 0);
2686 if (FLAG_native_code_counters && counter->Enabled()) {
2687 mov(scratch2, Operand(ExternalReference(counter)));
2688 ldr(scratch1, MemOperand(scratch2));
2689 add(scratch1, scratch1, Operand(value));
2690 str(scratch1, MemOperand(scratch2));
2691 }
2692}
2693
2694
2695void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2696 Register scratch1, Register scratch2) {
2697 ASSERT(value > 0);
2698 if (FLAG_native_code_counters && counter->Enabled()) {
2699 mov(scratch2, Operand(ExternalReference(counter)));
2700 ldr(scratch1, MemOperand(scratch2));
2701 sub(scratch1, scratch1, Operand(value));
2702 str(scratch1, MemOperand(scratch2));
2703 }
2704}
2705
2706
Steve Block1e0659c2011-05-24 12:43:12 +01002707void MacroAssembler::Assert(Condition cond, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01002708 if (emit_debug_code())
Steve Block1e0659c2011-05-24 12:43:12 +01002709 Check(cond, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00002710}
2711
2712
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002713void MacroAssembler::AssertRegisterIsRoot(Register reg,
2714 Heap::RootListIndex index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002715 if (emit_debug_code()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002716 LoadRoot(ip, index);
2717 cmp(reg, ip);
2718 Check(eq, "Register did not match expected root");
2719 }
2720}
2721
2722
Iain Merrick75681382010-08-19 15:07:18 +01002723void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002724 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01002725 ASSERT(!elements.is(ip));
2726 Label ok;
2727 push(elements);
2728 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
2729 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2730 cmp(elements, ip);
2731 b(eq, &ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002732 LoadRoot(ip, Heap::kFixedDoubleArrayMapRootIndex);
2733 cmp(elements, ip);
2734 b(eq, &ok);
Iain Merrick75681382010-08-19 15:07:18 +01002735 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2736 cmp(elements, ip);
2737 b(eq, &ok);
2738 Abort("JSObject with fast elements map has slow elements");
2739 bind(&ok);
2740 pop(elements);
2741 }
2742}
2743
2744
Steve Block1e0659c2011-05-24 12:43:12 +01002745void MacroAssembler::Check(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002746 Label L;
Steve Block1e0659c2011-05-24 12:43:12 +01002747 b(cond, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00002748 Abort(msg);
2749 // will not return here
2750 bind(&L);
2751}
2752
2753
2754void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01002755 Label abort_start;
2756 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00002757 // We want to pass the msg string like a smi to avoid GC
2758 // problems, however msg is not guaranteed to be aligned
2759 // properly. Instead, we pass an aligned pointer that is
2760 // a proper v8 smi, but also pass the alignment difference
2761 // from the real pointer as a smi.
2762 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2763 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2764 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2765#ifdef DEBUG
2766 if (msg != NULL) {
2767 RecordComment("Abort message: ");
2768 RecordComment(msg);
2769 }
2770#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002771
Steve Blocka7e24c12009-10-30 11:49:00 +00002772 mov(r0, Operand(p0));
2773 push(r0);
2774 mov(r0, Operand(Smi::FromInt(p1 - p0)));
2775 push(r0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002776 // Disable stub call restrictions to always allow calls to abort.
2777 if (!has_frame_) {
2778 // We don't actually want to generate a pile of code for this, so just
2779 // claim there is a stack frame, without generating one.
2780 FrameScope scope(this, StackFrame::NONE);
2781 CallRuntime(Runtime::kAbort, 2);
2782 } else {
2783 CallRuntime(Runtime::kAbort, 2);
2784 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002785 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01002786 if (is_const_pool_blocked()) {
2787 // If the calling code cares about the exact number of
2788 // instructions generated, we insert padding here to keep the size
2789 // of the Abort macro constant.
2790 static const int kExpectedAbortInstructions = 10;
2791 int abort_instructions = InstructionsGeneratedSince(&abort_start);
2792 ASSERT(abort_instructions <= kExpectedAbortInstructions);
2793 while (abort_instructions++ < kExpectedAbortInstructions) {
2794 nop();
2795 }
2796 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002797}
2798
2799
Steve Blockd0582a62009-12-15 09:54:21 +00002800void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2801 if (context_chain_length > 0) {
2802 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002803 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002804 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002805 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002806 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002807 } else {
2808 // Slot is in the current function context. Move it into the
2809 // destination register in case we store into it (the write barrier
2810 // cannot be allowed to destroy the context in esi).
2811 mov(dst, cp);
2812 }
Steve Blockd0582a62009-12-15 09:54:21 +00002813}
2814
2815
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002816void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2817 // Load the global or builtins object from the current context.
2818 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2819 // Load the global context from the global or builtins object.
2820 ldr(function, FieldMemOperand(function,
2821 GlobalObject::kGlobalContextOffset));
2822 // Load the function from the global context.
2823 ldr(function, MemOperand(function, Context::SlotOffset(index)));
2824}
2825
2826
2827void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2828 Register map,
2829 Register scratch) {
2830 // Load the initial map. The global functions all have initial maps.
2831 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002832 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002833 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00002834 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002835 b(&ok);
2836 bind(&fail);
2837 Abort("Global functions must have initial map");
2838 bind(&ok);
2839 }
2840}
2841
2842
Steve Block1e0659c2011-05-24 12:43:12 +01002843void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
2844 Register reg,
2845 Register scratch,
2846 Label* not_power_of_two_or_zero) {
2847 sub(scratch, reg, Operand(1), SetCC);
2848 b(mi, not_power_of_two_or_zero);
2849 tst(scratch, reg);
2850 b(ne, not_power_of_two_or_zero);
2851}
2852
2853
Steve Block44f0eee2011-05-26 01:26:41 +01002854void MacroAssembler::JumpIfNotPowerOfTwoOrZeroAndNeg(
2855 Register reg,
2856 Register scratch,
2857 Label* zero_and_neg,
2858 Label* not_power_of_two) {
2859 sub(scratch, reg, Operand(1), SetCC);
2860 b(mi, zero_and_neg);
2861 tst(scratch, reg);
2862 b(ne, not_power_of_two);
2863}
2864
2865
Andrei Popescu31002712010-02-23 13:46:05 +00002866void MacroAssembler::JumpIfNotBothSmi(Register reg1,
2867 Register reg2,
2868 Label* on_not_both_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002869 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002870 tst(reg1, Operand(kSmiTagMask));
2871 tst(reg2, Operand(kSmiTagMask), eq);
2872 b(ne, on_not_both_smi);
2873}
2874
2875
2876void MacroAssembler::JumpIfEitherSmi(Register reg1,
2877 Register reg2,
2878 Label* on_either_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002879 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002880 tst(reg1, Operand(kSmiTagMask));
2881 tst(reg2, Operand(kSmiTagMask), ne);
2882 b(eq, on_either_smi);
2883}
2884
2885
Iain Merrick75681382010-08-19 15:07:18 +01002886void MacroAssembler::AbortIfSmi(Register object) {
Steve Block1e0659c2011-05-24 12:43:12 +01002887 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +01002888 tst(object, Operand(kSmiTagMask));
2889 Assert(ne, "Operand is a smi");
2890}
2891
2892
Steve Block1e0659c2011-05-24 12:43:12 +01002893void MacroAssembler::AbortIfNotSmi(Register object) {
2894 STATIC_ASSERT(kSmiTag == 0);
2895 tst(object, Operand(kSmiTagMask));
2896 Assert(eq, "Operand is not smi");
2897}
2898
2899
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002900void MacroAssembler::AbortIfNotString(Register object) {
2901 STATIC_ASSERT(kSmiTag == 0);
2902 tst(object, Operand(kSmiTagMask));
2903 Assert(ne, "Operand is not a string");
2904 push(object);
2905 ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2906 CompareInstanceType(object, object, FIRST_NONSTRING_TYPE);
2907 pop(object);
2908 Assert(lo, "Operand is not a string");
2909}
2910
2911
2912
Steve Block1e0659c2011-05-24 12:43:12 +01002913void MacroAssembler::AbortIfNotRootValue(Register src,
2914 Heap::RootListIndex root_value_index,
2915 const char* message) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002916 CompareRoot(src, root_value_index);
Steve Block1e0659c2011-05-24 12:43:12 +01002917 Assert(eq, message);
2918}
2919
2920
2921void MacroAssembler::JumpIfNotHeapNumber(Register object,
2922 Register heap_number_map,
2923 Register scratch,
2924 Label* on_not_heap_number) {
2925 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2926 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2927 cmp(scratch, heap_number_map);
2928 b(ne, on_not_heap_number);
2929}
2930
2931
Leon Clarked91b9f72010-01-27 17:25:45 +00002932void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
2933 Register first,
2934 Register second,
2935 Register scratch1,
2936 Register scratch2,
2937 Label* failure) {
2938 // Test that both first and second are sequential ASCII strings.
2939 // Assume that they are non-smis.
2940 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
2941 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
2942 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
2943 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002944
2945 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
2946 scratch2,
2947 scratch1,
2948 scratch2,
2949 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002950}
2951
2952void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
2953 Register second,
2954 Register scratch1,
2955 Register scratch2,
2956 Label* failure) {
2957 // Check that neither is a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002958 STATIC_ASSERT(kSmiTag == 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002959 and_(scratch1, first, Operand(second));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002960 JumpIfSmi(scratch1, failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002961 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
2962 second,
2963 scratch1,
2964 scratch2,
2965 failure);
2966}
2967
Steve Blockd0582a62009-12-15 09:54:21 +00002968
Steve Block6ded16b2010-05-10 14:33:55 +01002969// Allocates a heap number or jumps to the need_gc label if the young space
2970// is full and a scavenge is needed.
2971void MacroAssembler::AllocateHeapNumber(Register result,
2972 Register scratch1,
2973 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002974 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01002975 Label* gc_required) {
2976 // Allocate an object in the heap for the heap number and tag it as a heap
2977 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01002978 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01002979 result,
2980 scratch1,
2981 scratch2,
2982 gc_required,
2983 TAG_OBJECT);
2984
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002985 // Store heap number map in the allocated object.
2986 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2987 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002988}
2989
2990
Steve Block8defd9f2010-07-08 12:39:36 +01002991void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2992 DwVfpRegister value,
2993 Register scratch1,
2994 Register scratch2,
2995 Register heap_number_map,
2996 Label* gc_required) {
2997 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
2998 sub(scratch1, result, Operand(kHeapObjectTag));
2999 vstr(value, scratch1, HeapNumber::kValueOffset);
3000}
3001
3002
Ben Murdochbb769b22010-08-11 14:56:33 +01003003// Copies a fixed number of fields of heap objects from src to dst.
3004void MacroAssembler::CopyFields(Register dst,
3005 Register src,
3006 RegList temps,
3007 int field_count) {
3008 // At least one bit set in the first 15 registers.
3009 ASSERT((temps & ((1 << 15) - 1)) != 0);
3010 ASSERT((temps & dst.bit()) == 0);
3011 ASSERT((temps & src.bit()) == 0);
3012 // Primitive implementation using only one temporary register.
3013
3014 Register tmp = no_reg;
3015 // Find a temp register in temps list.
3016 for (int i = 0; i < 15; i++) {
3017 if ((temps & (1 << i)) != 0) {
3018 tmp.set_code(i);
3019 break;
3020 }
3021 }
3022 ASSERT(!tmp.is(no_reg));
3023
3024 for (int i = 0; i < field_count; i++) {
3025 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
3026 str(tmp, FieldMemOperand(dst, i * kPointerSize));
3027 }
3028}
3029
3030
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003031void MacroAssembler::CopyBytes(Register src,
3032 Register dst,
3033 Register length,
3034 Register scratch) {
3035 Label align_loop, align_loop_1, word_loop, byte_loop, byte_loop_1, done;
3036
3037 // Align src before copying in word size chunks.
3038 bind(&align_loop);
3039 cmp(length, Operand(0));
3040 b(eq, &done);
3041 bind(&align_loop_1);
3042 tst(src, Operand(kPointerSize - 1));
3043 b(eq, &word_loop);
3044 ldrb(scratch, MemOperand(src, 1, PostIndex));
3045 strb(scratch, MemOperand(dst, 1, PostIndex));
3046 sub(length, length, Operand(1), SetCC);
3047 b(ne, &byte_loop_1);
3048
3049 // Copy bytes in word size chunks.
3050 bind(&word_loop);
Steve Block44f0eee2011-05-26 01:26:41 +01003051 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003052 tst(src, Operand(kPointerSize - 1));
3053 Assert(eq, "Expecting alignment for CopyBytes");
3054 }
3055 cmp(length, Operand(kPointerSize));
3056 b(lt, &byte_loop);
3057 ldr(scratch, MemOperand(src, kPointerSize, PostIndex));
3058#if CAN_USE_UNALIGNED_ACCESSES
3059 str(scratch, MemOperand(dst, kPointerSize, PostIndex));
3060#else
3061 strb(scratch, MemOperand(dst, 1, PostIndex));
3062 mov(scratch, Operand(scratch, LSR, 8));
3063 strb(scratch, MemOperand(dst, 1, PostIndex));
3064 mov(scratch, Operand(scratch, LSR, 8));
3065 strb(scratch, MemOperand(dst, 1, PostIndex));
3066 mov(scratch, Operand(scratch, LSR, 8));
3067 strb(scratch, MemOperand(dst, 1, PostIndex));
3068#endif
3069 sub(length, length, Operand(kPointerSize));
3070 b(&word_loop);
3071
3072 // Copy the last bytes if any left.
3073 bind(&byte_loop);
3074 cmp(length, Operand(0));
3075 b(eq, &done);
3076 bind(&byte_loop_1);
3077 ldrb(scratch, MemOperand(src, 1, PostIndex));
3078 strb(scratch, MemOperand(dst, 1, PostIndex));
3079 sub(length, length, Operand(1), SetCC);
3080 b(ne, &byte_loop_1);
3081 bind(&done);
3082}
3083
3084
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003085void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
3086 Register end_offset,
3087 Register filler) {
3088 Label loop, entry;
3089 b(&entry);
3090 bind(&loop);
3091 str(filler, MemOperand(start_offset, kPointerSize, PostIndex));
3092 bind(&entry);
3093 cmp(start_offset, end_offset);
3094 b(lt, &loop);
3095}
3096
3097
Steve Block8defd9f2010-07-08 12:39:36 +01003098void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
3099 Register source, // Input.
3100 Register scratch) {
Steve Block1e0659c2011-05-24 12:43:12 +01003101 ASSERT(!zeros.is(source) || !source.is(scratch));
Steve Block8defd9f2010-07-08 12:39:36 +01003102 ASSERT(!zeros.is(scratch));
3103 ASSERT(!scratch.is(ip));
3104 ASSERT(!source.is(ip));
3105 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01003106#ifdef CAN_USE_ARMV5_INSTRUCTIONS
3107 clz(zeros, source); // This instruction is only supported after ARM5.
3108#else
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003109 // Order of the next two lines is important: zeros register
3110 // can be the same as source register.
Steve Block8defd9f2010-07-08 12:39:36 +01003111 Move(scratch, source);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003112 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block6ded16b2010-05-10 14:33:55 +01003113 // Top 16.
3114 tst(scratch, Operand(0xffff0000));
3115 add(zeros, zeros, Operand(16), LeaveCC, eq);
3116 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
3117 // Top 8.
3118 tst(scratch, Operand(0xff000000));
3119 add(zeros, zeros, Operand(8), LeaveCC, eq);
3120 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
3121 // Top 4.
3122 tst(scratch, Operand(0xf0000000));
3123 add(zeros, zeros, Operand(4), LeaveCC, eq);
3124 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
3125 // Top 2.
3126 tst(scratch, Operand(0xc0000000));
3127 add(zeros, zeros, Operand(2), LeaveCC, eq);
3128 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
3129 // Top bit.
3130 tst(scratch, Operand(0x80000000u));
3131 add(zeros, zeros, Operand(1), LeaveCC, eq);
3132#endif
3133}
3134
3135
3136void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
3137 Register first,
3138 Register second,
3139 Register scratch1,
3140 Register scratch2,
3141 Label* failure) {
3142 int kFlatAsciiStringMask =
3143 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3144 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3145 and_(scratch1, first, Operand(kFlatAsciiStringMask));
3146 and_(scratch2, second, Operand(kFlatAsciiStringMask));
3147 cmp(scratch1, Operand(kFlatAsciiStringTag));
3148 // Ignore second test if first test failed.
3149 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
3150 b(ne, failure);
3151}
3152
3153
3154void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
3155 Register scratch,
3156 Label* failure) {
3157 int kFlatAsciiStringMask =
3158 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3159 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3160 and_(scratch, type, Operand(kFlatAsciiStringMask));
3161 cmp(scratch, Operand(kFlatAsciiStringTag));
3162 b(ne, failure);
3163}
3164
Steve Block44f0eee2011-05-26 01:26:41 +01003165static const int kRegisterPassedArguments = 4;
Steve Block6ded16b2010-05-10 14:33:55 +01003166
Steve Block44f0eee2011-05-26 01:26:41 +01003167
Ben Murdoch257744e2011-11-30 15:57:28 +00003168int MacroAssembler::CalculateStackPassedWords(int num_reg_arguments,
3169 int num_double_arguments) {
3170 int stack_passed_words = 0;
3171 if (use_eabi_hardfloat()) {
3172 // In the hard floating point calling convention, we can use
3173 // all double registers to pass doubles.
3174 if (num_double_arguments > DoubleRegister::kNumRegisters) {
3175 stack_passed_words +=
3176 2 * (num_double_arguments - DoubleRegister::kNumRegisters);
3177 }
3178 } else {
3179 // In the soft floating point calling convention, every double
3180 // argument is passed using two registers.
3181 num_reg_arguments += 2 * num_double_arguments;
3182 }
Steve Block6ded16b2010-05-10 14:33:55 +01003183 // Up to four simple arguments are passed in registers r0..r3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003184 if (num_reg_arguments > kRegisterPassedArguments) {
3185 stack_passed_words += num_reg_arguments - kRegisterPassedArguments;
3186 }
3187 return stack_passed_words;
3188}
3189
3190
3191void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
3192 int num_double_arguments,
3193 Register scratch) {
3194 int frame_alignment = ActivationFrameAlignment();
3195 int stack_passed_arguments = CalculateStackPassedWords(
3196 num_reg_arguments, num_double_arguments);
Steve Block6ded16b2010-05-10 14:33:55 +01003197 if (frame_alignment > kPointerSize) {
3198 // Make stack end at alignment and make room for num_arguments - 4 words
3199 // and the original value of sp.
3200 mov(scratch, sp);
3201 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
3202 ASSERT(IsPowerOf2(frame_alignment));
3203 and_(sp, sp, Operand(-frame_alignment));
3204 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
3205 } else {
3206 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
3207 }
3208}
3209
3210
Ben Murdoch257744e2011-11-30 15:57:28 +00003211void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
3212 Register scratch) {
3213 PrepareCallCFunction(num_reg_arguments, 0, scratch);
3214}
3215
3216
3217void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg) {
3218 if (use_eabi_hardfloat()) {
3219 Move(d0, dreg);
3220 } else {
3221 vmov(r0, r1, dreg);
3222 }
3223}
3224
3225
3226void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg1,
3227 DoubleRegister dreg2) {
3228 if (use_eabi_hardfloat()) {
3229 if (dreg2.is(d0)) {
3230 ASSERT(!dreg1.is(d1));
3231 Move(d1, dreg2);
3232 Move(d0, dreg1);
3233 } else {
3234 Move(d0, dreg1);
3235 Move(d1, dreg2);
3236 }
3237 } else {
3238 vmov(r0, r1, dreg1);
3239 vmov(r2, r3, dreg2);
3240 }
3241}
3242
3243
3244void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg,
3245 Register reg) {
3246 if (use_eabi_hardfloat()) {
3247 Move(d0, dreg);
3248 Move(r0, reg);
3249 } else {
3250 Move(r2, reg);
3251 vmov(r0, r1, dreg);
3252 }
3253}
3254
3255
3256void MacroAssembler::CallCFunction(ExternalReference function,
3257 int num_reg_arguments,
3258 int num_double_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003259 mov(ip, Operand(function));
3260 CallCFunctionHelper(ip, num_reg_arguments, num_double_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +00003261}
3262
3263
3264void MacroAssembler::CallCFunction(Register function,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003265 int num_reg_arguments,
3266 int num_double_arguments) {
3267 CallCFunctionHelper(function, num_reg_arguments, num_double_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +00003268}
3269
3270
Steve Block6ded16b2010-05-10 14:33:55 +01003271void MacroAssembler::CallCFunction(ExternalReference function,
3272 int num_arguments) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003273 CallCFunction(function, num_arguments, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003274}
3275
Ben Murdoch257744e2011-11-30 15:57:28 +00003276
Steve Block44f0eee2011-05-26 01:26:41 +01003277void MacroAssembler::CallCFunction(Register function,
Steve Block44f0eee2011-05-26 01:26:41 +01003278 int num_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003279 CallCFunction(function, num_arguments, 0);
Steve Block6ded16b2010-05-10 14:33:55 +01003280}
3281
3282
Steve Block44f0eee2011-05-26 01:26:41 +01003283void MacroAssembler::CallCFunctionHelper(Register function,
Ben Murdoch257744e2011-11-30 15:57:28 +00003284 int num_reg_arguments,
3285 int num_double_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003286 ASSERT(has_frame());
Steve Block6ded16b2010-05-10 14:33:55 +01003287 // Make sure that the stack is aligned before calling a C function unless
3288 // running in the simulator. The simulator has its own alignment check which
3289 // provides more information.
3290#if defined(V8_HOST_ARCH_ARM)
Steve Block44f0eee2011-05-26 01:26:41 +01003291 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01003292 int frame_alignment = OS::ActivationFrameAlignment();
3293 int frame_alignment_mask = frame_alignment - 1;
3294 if (frame_alignment > kPointerSize) {
3295 ASSERT(IsPowerOf2(frame_alignment));
3296 Label alignment_as_expected;
3297 tst(sp, Operand(frame_alignment_mask));
3298 b(eq, &alignment_as_expected);
3299 // Don't use Check here, as it will call Runtime_Abort possibly
3300 // re-entering here.
3301 stop("Unexpected alignment");
3302 bind(&alignment_as_expected);
3303 }
3304 }
3305#endif
3306
3307 // Just call directly. The function called cannot cause a GC, or
3308 // allow preemption, so the return address in the link register
3309 // stays correct.
3310 Call(function);
Ben Murdoch257744e2011-11-30 15:57:28 +00003311 int stack_passed_arguments = CalculateStackPassedWords(
3312 num_reg_arguments, num_double_arguments);
3313 if (ActivationFrameAlignment() > kPointerSize) {
Steve Block6ded16b2010-05-10 14:33:55 +01003314 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
3315 } else {
3316 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
3317 }
3318}
3319
3320
Steve Block1e0659c2011-05-24 12:43:12 +01003321void MacroAssembler::GetRelocatedValueLocation(Register ldr_location,
3322 Register result) {
3323 const uint32_t kLdrOffsetMask = (1 << 12) - 1;
3324 const int32_t kPCRegOffset = 2 * kPointerSize;
3325 ldr(result, MemOperand(ldr_location));
Steve Block44f0eee2011-05-26 01:26:41 +01003326 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003327 // Check that the instruction is a ldr reg, [pc + offset] .
3328 and_(result, result, Operand(kLdrPCPattern));
3329 cmp(result, Operand(kLdrPCPattern));
3330 Check(eq, "The instruction to patch should be a load from pc.");
3331 // Result was clobbered. Restore it.
3332 ldr(result, MemOperand(ldr_location));
3333 }
3334 // Get the address of the constant.
3335 and_(result, result, Operand(kLdrOffsetMask));
3336 add(result, ldr_location, Operand(result));
3337 add(result, result, Operand(kPCRegOffset));
3338}
3339
3340
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003341void MacroAssembler::CheckPageFlag(
3342 Register object,
3343 Register scratch,
3344 int mask,
3345 Condition cc,
3346 Label* condition_met) {
3347 and_(scratch, object, Operand(~Page::kPageAlignmentMask));
3348 ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
3349 tst(scratch, Operand(mask));
3350 b(cc, condition_met);
3351}
3352
3353
3354void MacroAssembler::JumpIfBlack(Register object,
3355 Register scratch0,
3356 Register scratch1,
3357 Label* on_black) {
3358 HasColor(object, scratch0, scratch1, on_black, 1, 0); // kBlackBitPattern.
3359 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
3360}
3361
3362
3363void MacroAssembler::HasColor(Register object,
3364 Register bitmap_scratch,
3365 Register mask_scratch,
3366 Label* has_color,
3367 int first_bit,
3368 int second_bit) {
3369 ASSERT(!AreAliased(object, bitmap_scratch, mask_scratch, no_reg));
3370
3371 GetMarkBits(object, bitmap_scratch, mask_scratch);
3372
3373 Label other_color, word_boundary;
3374 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3375 tst(ip, Operand(mask_scratch));
3376 b(first_bit == 1 ? eq : ne, &other_color);
3377 // Shift left 1 by adding.
3378 add(mask_scratch, mask_scratch, Operand(mask_scratch), SetCC);
3379 b(eq, &word_boundary);
3380 tst(ip, Operand(mask_scratch));
3381 b(second_bit == 1 ? ne : eq, has_color);
3382 jmp(&other_color);
3383
3384 bind(&word_boundary);
3385 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize));
3386 tst(ip, Operand(1));
3387 b(second_bit == 1 ? ne : eq, has_color);
3388 bind(&other_color);
3389}
3390
3391
3392// Detect some, but not all, common pointer-free objects. This is used by the
3393// incremental write barrier which doesn't care about oddballs (they are always
3394// marked black immediately so this code is not hit).
3395void MacroAssembler::JumpIfDataObject(Register value,
3396 Register scratch,
3397 Label* not_data_object) {
3398 Label is_data_object;
3399 ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
3400 CompareRoot(scratch, Heap::kHeapNumberMapRootIndex);
3401 b(eq, &is_data_object);
3402 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3403 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
3404 // If it's a string and it's not a cons string then it's an object containing
3405 // no GC pointers.
3406 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
3407 tst(scratch, Operand(kIsIndirectStringMask | kIsNotStringMask));
3408 b(ne, not_data_object);
3409 bind(&is_data_object);
3410}
3411
3412
3413void MacroAssembler::GetMarkBits(Register addr_reg,
3414 Register bitmap_reg,
3415 Register mask_reg) {
3416 ASSERT(!AreAliased(addr_reg, bitmap_reg, mask_reg, no_reg));
3417 and_(bitmap_reg, addr_reg, Operand(~Page::kPageAlignmentMask));
3418 Ubfx(mask_reg, addr_reg, kPointerSizeLog2, Bitmap::kBitsPerCellLog2);
3419 const int kLowBits = kPointerSizeLog2 + Bitmap::kBitsPerCellLog2;
3420 Ubfx(ip, addr_reg, kLowBits, kPageSizeBits - kLowBits);
3421 add(bitmap_reg, bitmap_reg, Operand(ip, LSL, kPointerSizeLog2));
3422 mov(ip, Operand(1));
3423 mov(mask_reg, Operand(ip, LSL, mask_reg));
3424}
3425
3426
3427void MacroAssembler::EnsureNotWhite(
3428 Register value,
3429 Register bitmap_scratch,
3430 Register mask_scratch,
3431 Register load_scratch,
3432 Label* value_is_white_and_not_data) {
3433 ASSERT(!AreAliased(value, bitmap_scratch, mask_scratch, ip));
3434 GetMarkBits(value, bitmap_scratch, mask_scratch);
3435
3436 // If the value is black or grey we don't need to do anything.
3437 ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0);
3438 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
3439 ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0);
3440 ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
3441
3442 Label done;
3443
3444 // Since both black and grey have a 1 in the first position and white does
3445 // not have a 1 there we only need to check one bit.
3446 ldr(load_scratch, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3447 tst(mask_scratch, load_scratch);
3448 b(ne, &done);
3449
3450 if (FLAG_debug_code) {
3451 // Check for impossible bit pattern.
3452 Label ok;
3453 // LSL may overflow, making the check conservative.
3454 tst(load_scratch, Operand(mask_scratch, LSL, 1));
3455 b(eq, &ok);
3456 stop("Impossible marking bit pattern");
3457 bind(&ok);
3458 }
3459
3460 // Value is white. We check whether it is data that doesn't need scanning.
3461 // Currently only checks for HeapNumber and non-cons strings.
3462 Register map = load_scratch; // Holds map while checking type.
3463 Register length = load_scratch; // Holds length of object after testing type.
3464 Label is_data_object;
3465
3466 // Check for heap-number
3467 ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
3468 CompareRoot(map, Heap::kHeapNumberMapRootIndex);
3469 mov(length, Operand(HeapNumber::kSize), LeaveCC, eq);
3470 b(eq, &is_data_object);
3471
3472 // Check for strings.
3473 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3474 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
3475 // If it's a string and it's not a cons string then it's an object containing
3476 // no GC pointers.
3477 Register instance_type = load_scratch;
3478 ldrb(instance_type, FieldMemOperand(map, Map::kInstanceTypeOffset));
3479 tst(instance_type, Operand(kIsIndirectStringMask | kIsNotStringMask));
3480 b(ne, value_is_white_and_not_data);
3481 // It's a non-indirect (non-cons and non-slice) string.
3482 // If it's external, the length is just ExternalString::kSize.
3483 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
3484 // External strings are the only ones with the kExternalStringTag bit
3485 // set.
3486 ASSERT_EQ(0, kSeqStringTag & kExternalStringTag);
3487 ASSERT_EQ(0, kConsStringTag & kExternalStringTag);
3488 tst(instance_type, Operand(kExternalStringTag));
3489 mov(length, Operand(ExternalString::kSize), LeaveCC, ne);
3490 b(ne, &is_data_object);
3491
3492 // Sequential string, either ASCII or UC16.
3493 // For ASCII (char-size of 1) we shift the smi tag away to get the length.
3494 // For UC16 (char-size of 2) we just leave the smi tag in place, thereby
3495 // getting the length multiplied by 2.
3496 ASSERT(kAsciiStringTag == 4 && kStringEncodingMask == 4);
3497 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3498 ldr(ip, FieldMemOperand(value, String::kLengthOffset));
3499 tst(instance_type, Operand(kStringEncodingMask));
3500 mov(ip, Operand(ip, LSR, 1), LeaveCC, ne);
3501 add(length, ip, Operand(SeqString::kHeaderSize + kObjectAlignmentMask));
3502 and_(length, length, Operand(~kObjectAlignmentMask));
3503
3504 bind(&is_data_object);
3505 // Value is a data object, and it is white. Mark it black. Since we know
3506 // that the object is white we can make it black by flipping one bit.
3507 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3508 orr(ip, ip, Operand(mask_scratch));
3509 str(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3510
3511 and_(bitmap_scratch, bitmap_scratch, Operand(~Page::kPageAlignmentMask));
3512 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3513 add(ip, ip, Operand(length));
3514 str(ip, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3515
3516 bind(&done);
3517}
3518
3519
Ben Murdoch257744e2011-11-30 15:57:28 +00003520void MacroAssembler::ClampUint8(Register output_reg, Register input_reg) {
3521 Usat(output_reg, 8, Operand(input_reg));
3522}
3523
3524
3525void MacroAssembler::ClampDoubleToUint8(Register result_reg,
3526 DoubleRegister input_reg,
3527 DoubleRegister temp_double_reg) {
3528 Label above_zero;
3529 Label done;
3530 Label in_bounds;
3531
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003532 Vmov(temp_double_reg, 0.0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003533 VFPCompareAndSetFlags(input_reg, temp_double_reg);
3534 b(gt, &above_zero);
3535
3536 // Double value is less than zero, NaN or Inf, return 0.
3537 mov(result_reg, Operand(0));
3538 b(al, &done);
3539
3540 // Double value is >= 255, return 255.
3541 bind(&above_zero);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003542 Vmov(temp_double_reg, 255.0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003543 VFPCompareAndSetFlags(input_reg, temp_double_reg);
3544 b(le, &in_bounds);
3545 mov(result_reg, Operand(255));
3546 b(al, &done);
3547
3548 // In 0-255 range, round and truncate.
3549 bind(&in_bounds);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003550 Vmov(temp_double_reg, 0.5);
Ben Murdoch257744e2011-11-30 15:57:28 +00003551 vadd(temp_double_reg, input_reg, temp_double_reg);
3552 vcvt_u32_f64(s0, temp_double_reg);
3553 vmov(result_reg, s0);
3554 bind(&done);
3555}
3556
3557
3558void MacroAssembler::LoadInstanceDescriptors(Register map,
3559 Register descriptors) {
3560 ldr(descriptors,
3561 FieldMemOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
3562 Label not_smi;
3563 JumpIfNotSmi(descriptors, &not_smi);
3564 mov(descriptors, Operand(FACTORY->empty_descriptor_array()));
3565 bind(&not_smi);
3566}
3567
3568
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003569bool AreAliased(Register r1, Register r2, Register r3, Register r4) {
3570 if (r1.is(r2)) return true;
3571 if (r1.is(r3)) return true;
3572 if (r1.is(r4)) return true;
3573 if (r2.is(r3)) return true;
3574 if (r2.is(r4)) return true;
3575 if (r3.is(r4)) return true;
3576 return false;
3577}
3578
3579
Steve Blocka7e24c12009-10-30 11:49:00 +00003580CodePatcher::CodePatcher(byte* address, int instructions)
3581 : address_(address),
3582 instructions_(instructions),
3583 size_(instructions * Assembler::kInstrSize),
Ben Murdoch8b112d22011-06-08 16:22:53 +01003584 masm_(Isolate::Current(), address, size_ + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003585 // Create a new macro assembler pointing to the address of the code to patch.
3586 // The size is adjusted with kGap on order for the assembler to generate size
3587 // bytes of instructions without failing with buffer size constraints.
3588 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
3589}
3590
3591
3592CodePatcher::~CodePatcher() {
3593 // Indicate that code has changed.
3594 CPU::FlushICache(address_, size_);
3595
3596 // Check that the code was patched as expected.
3597 ASSERT(masm_.pc_ == address_ + size_);
3598 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
3599}
3600
3601
Steve Block1e0659c2011-05-24 12:43:12 +01003602void CodePatcher::Emit(Instr instr) {
3603 masm()->emit(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003604}
3605
3606
3607void CodePatcher::Emit(Address addr) {
3608 masm()->emit(reinterpret_cast<Instr>(addr));
3609}
Steve Block1e0659c2011-05-24 12:43:12 +01003610
3611
3612void CodePatcher::EmitCondition(Condition cond) {
3613 Instr instr = Assembler::instr_at(masm_.pc_);
3614 instr = (instr & ~kCondMask) | cond;
3615 masm_.emit(instr);
3616}
Steve Blocka7e24c12009-10-30 11:49:00 +00003617
3618
3619} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01003620
3621#endif // V8_TARGET_ARCH_ARM