blob: 9894ff202e6d1b87e98b20ce60268ff336ae3273 [file] [log] [blame]
Ben Murdochc7cc0282012-03-05 14:35:55 +00001// Copyright 2012 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 Murdochc7cc0282012-03-05 14:35:55 +0000399 ldr(destination, MemOperand(kRootRegister, 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 Murdochc7cc0282012-03-05 14:35:55 +0000406 str(source, MemOperand(kRootRegister, index << kPointerSizeLog2), cond);
407}
408
409
410void MacroAssembler::LoadHeapObject(Register result,
411 Handle<HeapObject> object) {
412 if (isolate()->heap()->InNewSpace(*object)) {
413 Handle<JSGlobalPropertyCell> cell =
414 isolate()->factory()->NewJSGlobalPropertyCell(object);
415 mov(result, Operand(cell));
416 ldr(result, FieldMemOperand(result, JSGlobalPropertyCell::kValueOffset));
417 } else {
418 mov(result, Operand(object));
419 }
Steve Block6ded16b2010-05-10 14:33:55 +0100420}
421
422
423void MacroAssembler::InNewSpace(Register object,
424 Register scratch,
Steve Block1e0659c2011-05-24 12:43:12 +0100425 Condition cond,
Steve Block6ded16b2010-05-10 14:33:55 +0100426 Label* branch) {
Steve Block1e0659c2011-05-24 12:43:12 +0100427 ASSERT(cond == eq || cond == ne);
Steve Block44f0eee2011-05-26 01:26:41 +0100428 and_(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
429 cmp(scratch, Operand(ExternalReference::new_space_start(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +0100430 b(cond, branch);
Steve Block6ded16b2010-05-10 14:33:55 +0100431}
432
433
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000434void MacroAssembler::RecordWriteField(
435 Register object,
436 int offset,
437 Register value,
438 Register dst,
439 LinkRegisterStatus lr_status,
440 SaveFPRegsMode save_fp,
441 RememberedSetAction remembered_set_action,
442 SmiCheck smi_check) {
443 // First, check if a write barrier is even needed. The tests below
444 // catch stores of Smis.
Steve Block6ded16b2010-05-10 14:33:55 +0100445 Label done;
446
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000447 // Skip barrier if writing a smi.
448 if (smi_check == INLINE_SMI_CHECK) {
449 JumpIfSmi(value, &done);
450 }
Steve Block6ded16b2010-05-10 14:33:55 +0100451
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000452 // Although the object register is tagged, the offset is relative to the start
453 // of the object, so so offset must be a multiple of kPointerSize.
454 ASSERT(IsAligned(offset, kPointerSize));
Steve Block8defd9f2010-07-08 12:39:36 +0100455
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000456 add(dst, object, Operand(offset - kHeapObjectTag));
457 if (emit_debug_code()) {
458 Label ok;
459 tst(dst, Operand((1 << kPointerSizeLog2) - 1));
460 b(eq, &ok);
461 stop("Unaligned cell in write barrier");
462 bind(&ok);
463 }
464
465 RecordWrite(object,
466 dst,
467 value,
468 lr_status,
469 save_fp,
470 remembered_set_action,
471 OMIT_SMI_CHECK);
Steve Blocka7e24c12009-10-30 11:49:00 +0000472
473 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000474
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000475 // Clobber clobbered input registers when running with the debug-code flag
Leon Clarke4515c472010-02-03 11:58:03 +0000476 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100477 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000478 mov(value, Operand(BitCast<int32_t>(kZapValue + 4)));
479 mov(dst, Operand(BitCast<int32_t>(kZapValue + 8)));
Leon Clarke4515c472010-02-03 11:58:03 +0000480 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000481}
482
483
Steve Block8defd9f2010-07-08 12:39:36 +0100484// Will clobber 4 registers: object, address, scratch, ip. The
485// register 'object' contains a heap object pointer. The heap object
486// tag is shifted away.
487void MacroAssembler::RecordWrite(Register object,
488 Register address,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000489 Register value,
490 LinkRegisterStatus lr_status,
491 SaveFPRegsMode fp_mode,
492 RememberedSetAction remembered_set_action,
493 SmiCheck smi_check) {
Steve Block8defd9f2010-07-08 12:39:36 +0100494 // The compiled code assumes that record write doesn't change the
495 // context register, so we check that none of the clobbered
496 // registers are cp.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000497 ASSERT(!address.is(cp) && !value.is(cp));
498
Ben Murdochc7cc0282012-03-05 14:35:55 +0000499 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000500 ldr(ip, MemOperand(address));
501 cmp(ip, value);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000502 Check(eq, "Wrong address or value passed to RecordWrite");
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000503 }
Steve Block8defd9f2010-07-08 12:39:36 +0100504
505 Label done;
506
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000507 if (smi_check == INLINE_SMI_CHECK) {
508 ASSERT_EQ(0, kSmiTag);
509 tst(value, Operand(kSmiTagMask));
510 b(eq, &done);
511 }
512
513 CheckPageFlag(value,
514 value, // Used as scratch.
515 MemoryChunk::kPointersToHereAreInterestingMask,
516 eq,
517 &done);
518 CheckPageFlag(object,
519 value, // Used as scratch.
520 MemoryChunk::kPointersFromHereAreInterestingMask,
521 eq,
522 &done);
Steve Block8defd9f2010-07-08 12:39:36 +0100523
524 // Record the actual write.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000525 if (lr_status == kLRHasNotBeenSaved) {
526 push(lr);
527 }
528 RecordWriteStub stub(object, value, address, remembered_set_action, fp_mode);
529 CallStub(&stub);
530 if (lr_status == kLRHasNotBeenSaved) {
531 pop(lr);
532 }
Steve Block8defd9f2010-07-08 12:39:36 +0100533
534 bind(&done);
535
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000536 // Clobber clobbered registers when running with the debug-code flag
Steve Block8defd9f2010-07-08 12:39:36 +0100537 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100538 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000539 mov(address, Operand(BitCast<int32_t>(kZapValue + 12)));
540 mov(value, Operand(BitCast<int32_t>(kZapValue + 16)));
541 }
542}
543
544
545void MacroAssembler::RememberedSetHelper(Register object, // For debug tests.
546 Register address,
547 Register scratch,
548 SaveFPRegsMode fp_mode,
549 RememberedSetFinalAction and_then) {
550 Label done;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000551 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000552 Label ok;
553 JumpIfNotInNewSpace(object, scratch, &ok);
554 stop("Remembered set pointer is in new space");
555 bind(&ok);
556 }
557 // Load store buffer top.
558 ExternalReference store_buffer =
559 ExternalReference::store_buffer_top(isolate());
560 mov(ip, Operand(store_buffer));
561 ldr(scratch, MemOperand(ip));
562 // Store pointer to buffer and increment buffer top.
563 str(address, MemOperand(scratch, kPointerSize, PostIndex));
564 // Write back new top of buffer.
565 str(scratch, MemOperand(ip));
566 // Call stub on end of buffer.
567 // Check for end of buffer.
568 tst(scratch, Operand(StoreBuffer::kStoreBufferOverflowBit));
569 if (and_then == kFallThroughAtEnd) {
570 b(eq, &done);
571 } else {
572 ASSERT(and_then == kReturnAtEnd);
573 Ret(eq);
574 }
575 push(lr);
576 StoreBufferOverflowStub store_buffer_overflow =
577 StoreBufferOverflowStub(fp_mode);
578 CallStub(&store_buffer_overflow);
579 pop(lr);
580 bind(&done);
581 if (and_then == kReturnAtEnd) {
582 Ret();
Steve Block8defd9f2010-07-08 12:39:36 +0100583 }
584}
585
586
Ben Murdochb0fe1622011-05-05 13:52:32 +0100587// Push and pop all registers that can hold pointers.
588void MacroAssembler::PushSafepointRegisters() {
589 // Safepoints expect a block of contiguous register values starting with r0:
590 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
591 // Safepoints expect a block of kNumSafepointRegisters values on the
592 // stack, so adjust the stack for unsaved registers.
593 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
594 ASSERT(num_unsaved >= 0);
595 sub(sp, sp, Operand(num_unsaved * kPointerSize));
596 stm(db_w, sp, kSafepointSavedRegisters);
597}
598
599
600void MacroAssembler::PopSafepointRegisters() {
601 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
602 ldm(ia_w, sp, kSafepointSavedRegisters);
603 add(sp, sp, Operand(num_unsaved * kPointerSize));
604}
605
606
Ben Murdochb8e0da22011-05-16 14:20:40 +0100607void MacroAssembler::PushSafepointRegistersAndDoubles() {
608 PushSafepointRegisters();
609 sub(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
610 kDoubleSize));
611 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
612 vstr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
613 }
614}
615
616
617void MacroAssembler::PopSafepointRegistersAndDoubles() {
618 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
619 vldr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
620 }
621 add(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
622 kDoubleSize));
623 PopSafepointRegisters();
624}
625
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100626void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register src,
627 Register dst) {
628 str(src, SafepointRegistersAndDoublesSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100629}
630
631
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100632void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
633 str(src, SafepointRegisterSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100634}
635
636
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100637void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
638 ldr(dst, SafepointRegisterSlot(src));
Steve Block1e0659c2011-05-24 12:43:12 +0100639}
640
641
Ben Murdochb0fe1622011-05-05 13:52:32 +0100642int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
643 // The registers are pushed starting with the highest encoding,
644 // which means that lowest encodings are closest to the stack pointer.
645 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
646 return reg_code;
647}
648
649
Steve Block1e0659c2011-05-24 12:43:12 +0100650MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
651 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
652}
653
654
655MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
656 // General purpose registers are pushed last on the stack.
657 int doubles_size = DwVfpRegister::kNumAllocatableRegisters * kDoubleSize;
658 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
659 return MemOperand(sp, doubles_size + register_offset);
660}
661
662
Leon Clarkef7060e22010-06-03 12:02:55 +0100663void MacroAssembler::Ldrd(Register dst1, Register dst2,
664 const MemOperand& src, Condition cond) {
665 ASSERT(src.rm().is(no_reg));
666 ASSERT(!dst1.is(lr)); // r14.
667 ASSERT_EQ(0, dst1.code() % 2);
668 ASSERT_EQ(dst1.code() + 1, dst2.code());
669
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000670 // V8 does not use this addressing mode, so the fallback code
671 // below doesn't support it yet.
672 ASSERT((src.am() != PreIndex) && (src.am() != NegPreIndex));
673
Leon Clarkef7060e22010-06-03 12:02:55 +0100674 // Generate two ldr instructions if ldrd is not available.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100675 if (CpuFeatures::IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100676 CpuFeatures::Scope scope(ARMv7);
677 ldrd(dst1, dst2, src, cond);
678 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000679 if ((src.am() == Offset) || (src.am() == NegOffset)) {
680 MemOperand src2(src);
681 src2.set_offset(src2.offset() + 4);
682 if (dst1.is(src.rn())) {
683 ldr(dst2, src2, cond);
684 ldr(dst1, src, cond);
685 } else {
686 ldr(dst1, src, cond);
687 ldr(dst2, src2, cond);
688 }
689 } else { // PostIndex or NegPostIndex.
690 ASSERT((src.am() == PostIndex) || (src.am() == NegPostIndex));
691 if (dst1.is(src.rn())) {
692 ldr(dst2, MemOperand(src.rn(), 4, Offset), cond);
693 ldr(dst1, src, cond);
694 } else {
695 MemOperand src2(src);
696 src2.set_offset(src2.offset() - 4);
697 ldr(dst1, MemOperand(src.rn(), 4, PostIndex), cond);
698 ldr(dst2, src2, cond);
699 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100700 }
701 }
702}
703
704
705void MacroAssembler::Strd(Register src1, Register src2,
706 const MemOperand& dst, Condition cond) {
707 ASSERT(dst.rm().is(no_reg));
708 ASSERT(!src1.is(lr)); // r14.
709 ASSERT_EQ(0, src1.code() % 2);
710 ASSERT_EQ(src1.code() + 1, src2.code());
711
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000712 // V8 does not use this addressing mode, so the fallback code
713 // below doesn't support it yet.
714 ASSERT((dst.am() != PreIndex) && (dst.am() != NegPreIndex));
715
Leon Clarkef7060e22010-06-03 12:02:55 +0100716 // Generate two str instructions if strd is not available.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100717 if (CpuFeatures::IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100718 CpuFeatures::Scope scope(ARMv7);
719 strd(src1, src2, dst, cond);
720 } else {
721 MemOperand dst2(dst);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000722 if ((dst.am() == Offset) || (dst.am() == NegOffset)) {
723 dst2.set_offset(dst2.offset() + 4);
724 str(src1, dst, cond);
725 str(src2, dst2, cond);
726 } else { // PostIndex or NegPostIndex.
727 ASSERT((dst.am() == PostIndex) || (dst.am() == NegPostIndex));
728 dst2.set_offset(dst2.offset() - 4);
729 str(src1, MemOperand(dst.rn(), 4, PostIndex), cond);
730 str(src2, dst2, cond);
731 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100732 }
733}
734
735
Ben Murdochb8e0da22011-05-16 14:20:40 +0100736void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
737 const Register scratch,
738 const Condition cond) {
739 vmrs(scratch, cond);
740 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
741 vmsr(scratch, cond);
742}
743
744
745void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
746 const DwVfpRegister src2,
747 const Condition cond) {
748 // Compare and move FPSCR flags to the normal condition flags.
749 VFPCompareAndLoadFlags(src1, src2, pc, cond);
750}
751
752void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
753 const double src2,
754 const Condition cond) {
755 // Compare and move FPSCR flags to the normal condition flags.
756 VFPCompareAndLoadFlags(src1, src2, pc, cond);
757}
758
759
760void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
761 const DwVfpRegister src2,
762 const Register fpscr_flags,
763 const Condition cond) {
764 // Compare and load FPSCR.
765 vcmp(src1, src2, cond);
766 vmrs(fpscr_flags, cond);
767}
768
769void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
770 const double src2,
771 const Register fpscr_flags,
772 const Condition cond) {
773 // Compare and load FPSCR.
774 vcmp(src1, src2, cond);
775 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100776}
777
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000778void MacroAssembler::Vmov(const DwVfpRegister dst,
779 const double imm,
780 const Condition cond) {
781 ASSERT(CpuFeatures::IsEnabled(VFP3));
782 static const DoubleRepresentation minus_zero(-0.0);
783 static const DoubleRepresentation zero(0.0);
784 DoubleRepresentation value(imm);
785 // Handle special values first.
786 if (value.bits == zero.bits) {
787 vmov(dst, kDoubleRegZero, cond);
788 } else if (value.bits == minus_zero.bits) {
789 vneg(dst, kDoubleRegZero, cond);
790 } else {
791 vmov(dst, imm, cond);
792 }
793}
794
Ben Murdoch086aeea2011-05-13 15:57:08 +0100795
Steve Blocka7e24c12009-10-30 11:49:00 +0000796void MacroAssembler::EnterFrame(StackFrame::Type type) {
797 // r0-r3: preserved
798 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
799 mov(ip, Operand(Smi::FromInt(type)));
800 push(ip);
801 mov(ip, Operand(CodeObject()));
802 push(ip);
803 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
804}
805
806
807void MacroAssembler::LeaveFrame(StackFrame::Type type) {
808 // r0: preserved
809 // r1: preserved
810 // r2: preserved
811
812 // Drop the execution stack down to the frame pointer and restore
813 // the caller frame pointer and return address.
814 mov(sp, fp);
815 ldm(ia_w, sp, fp.bit() | lr.bit());
816}
817
818
Steve Block1e0659c2011-05-24 12:43:12 +0100819void MacroAssembler::EnterExitFrame(bool save_doubles, int stack_space) {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000820 // Set up the frame structure on the stack.
Steve Block1e0659c2011-05-24 12:43:12 +0100821 ASSERT_EQ(2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
822 ASSERT_EQ(1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
823 ASSERT_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
824 Push(lr, fp);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000825 mov(fp, Operand(sp)); // Set up new frame pointer.
Steve Block1e0659c2011-05-24 12:43:12 +0100826 // Reserve room for saved entry sp and code object.
827 sub(sp, sp, Operand(2 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100828 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100829 mov(ip, Operand(0));
830 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
831 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000832 mov(ip, Operand(CodeObject()));
Steve Block1e0659c2011-05-24 12:43:12 +0100833 str(ip, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000834
835 // Save the frame pointer and the context in top.
Ben Murdoch589d6972011-11-30 16:04:58 +0000836 mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000837 str(fp, MemOperand(ip));
Ben Murdoch589d6972011-11-30 16:04:58 +0000838 mov(ip, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000839 str(cp, MemOperand(ip));
840
Ben Murdochb0fe1622011-05-05 13:52:32 +0100841 // Optionally save all double registers.
842 if (save_doubles) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100843 DwVfpRegister first = d0;
844 DwVfpRegister last =
845 DwVfpRegister::from_code(DwVfpRegister::kNumRegisters - 1);
846 vstm(db_w, sp, first, last);
Steve Block1e0659c2011-05-24 12:43:12 +0100847 // Note that d0 will be accessible at
848 // fp - 2 * kPointerSize - DwVfpRegister::kNumRegisters * kDoubleSize,
849 // since the sp slot and code slot were pushed after the fp.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100850 }
Steve Block1e0659c2011-05-24 12:43:12 +0100851
852 // Reserve place for the return address and stack space and align the frame
853 // preparing for calling the runtime function.
854 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
855 sub(sp, sp, Operand((stack_space + 1) * kPointerSize));
856 if (frame_alignment > 0) {
857 ASSERT(IsPowerOf2(frame_alignment));
858 and_(sp, sp, Operand(-frame_alignment));
859 }
860
861 // Set the exit frame sp value to point just before the return address
862 // location.
863 add(ip, sp, Operand(kPointerSize));
864 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000865}
866
867
Steve Block6ded16b2010-05-10 14:33:55 +0100868void MacroAssembler::InitializeNewString(Register string,
869 Register length,
870 Heap::RootListIndex map_index,
871 Register scratch1,
872 Register scratch2) {
873 mov(scratch1, Operand(length, LSL, kSmiTagSize));
874 LoadRoot(scratch2, map_index);
875 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
876 mov(scratch1, Operand(String::kEmptyHashField));
877 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
878 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
879}
880
881
882int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000883#if defined(V8_HOST_ARCH_ARM)
884 // Running on the real platform. Use the alignment as mandated by the local
885 // environment.
886 // Note: This will break if we ever start generating snapshots on one ARM
887 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100888 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000889#else // defined(V8_HOST_ARCH_ARM)
890 // If we are using the simulator then we should always align to the expected
891 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100892 // if the target platform will need alignment, so this is controlled from a
893 // flag.
894 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000895#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000896}
897
898
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100899void MacroAssembler::LeaveExitFrame(bool save_doubles,
900 Register argument_count) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100901 // Optionally restore all double registers.
902 if (save_doubles) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100903 // Calculate the stack location of the saved doubles and restore them.
904 const int offset = 2 * kPointerSize;
905 sub(r3, fp, Operand(offset + DwVfpRegister::kNumRegisters * kDoubleSize));
906 DwVfpRegister first = d0;
907 DwVfpRegister last =
908 DwVfpRegister::from_code(DwVfpRegister::kNumRegisters - 1);
909 vldm(ia, r3, first, last);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100910 }
911
Steve Blocka7e24c12009-10-30 11:49:00 +0000912 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100913 mov(r3, Operand(0, RelocInfo::NONE));
Ben Murdoch589d6972011-11-30 16:04:58 +0000914 mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000915 str(r3, MemOperand(ip));
916
917 // Restore current context from top and clear it in debug mode.
Ben Murdoch589d6972011-11-30 16:04:58 +0000918 mov(ip, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000919 ldr(cp, MemOperand(ip));
920#ifdef DEBUG
921 str(r3, MemOperand(ip));
922#endif
923
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100924 // Tear down the exit frame, pop the arguments, and return.
Steve Block1e0659c2011-05-24 12:43:12 +0100925 mov(sp, Operand(fp));
926 ldm(ia_w, sp, fp.bit() | lr.bit());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100927 if (argument_count.is_valid()) {
928 add(sp, sp, Operand(argument_count, LSL, kPointerSizeLog2));
929 }
930}
931
932void MacroAssembler::GetCFunctionDoubleResult(const DoubleRegister dst) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000933 if (use_eabi_hardfloat()) {
934 Move(dst, d0);
935 } else {
936 vmov(dst, r0, r1);
937 }
938}
939
940
941void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
942 // This macro takes the dst register to make the code more readable
943 // at the call sites. However, the dst register has to be r5 to
944 // follow the calling convention which requires the call type to be
945 // in r5.
946 ASSERT(dst.is(r5));
947 if (call_kind == CALL_AS_FUNCTION) {
948 mov(dst, Operand(Smi::FromInt(1)));
949 } else {
950 mov(dst, Operand(Smi::FromInt(0)));
951 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000952}
953
954
955void MacroAssembler::InvokePrologue(const ParameterCount& expected,
956 const ParameterCount& actual,
957 Handle<Code> code_constant,
958 Register code_reg,
959 Label* done,
Ben Murdochc7cc0282012-03-05 14:35:55 +0000960 bool* definitely_mismatches,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100961 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000962 const CallWrapper& call_wrapper,
963 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000964 bool definitely_matches = false;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000965 *definitely_mismatches = false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000966 Label regular_invoke;
967
968 // Check whether the expected and actual arguments count match. If not,
969 // setup registers according to contract with ArgumentsAdaptorTrampoline:
970 // r0: actual arguments count
971 // r1: function (passed through to callee)
972 // r2: expected arguments count
973 // r3: callee code entry
974
975 // The code below is made a lot easier because the calling code already sets
976 // up actual and expected registers according to the contract if values are
977 // passed in registers.
978 ASSERT(actual.is_immediate() || actual.reg().is(r0));
979 ASSERT(expected.is_immediate() || expected.reg().is(r2));
980 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
981
982 if (expected.is_immediate()) {
983 ASSERT(actual.is_immediate());
984 if (expected.immediate() == actual.immediate()) {
985 definitely_matches = true;
986 } else {
987 mov(r0, Operand(actual.immediate()));
988 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
989 if (expected.immediate() == sentinel) {
990 // Don't worry about adapting arguments for builtins that
991 // don't want that done. Skip adaption code by making it look
992 // like we have a match between expected and actual number of
993 // arguments.
994 definitely_matches = true;
995 } else {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000996 *definitely_mismatches = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 mov(r2, Operand(expected.immediate()));
998 }
999 }
1000 } else {
1001 if (actual.is_immediate()) {
1002 cmp(expected.reg(), Operand(actual.immediate()));
1003 b(eq, &regular_invoke);
1004 mov(r0, Operand(actual.immediate()));
1005 } else {
1006 cmp(expected.reg(), Operand(actual.reg()));
1007 b(eq, &regular_invoke);
1008 }
1009 }
1010
1011 if (!definitely_matches) {
1012 if (!code_constant.is_null()) {
1013 mov(r3, Operand(code_constant));
1014 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
1015 }
1016
1017 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +01001018 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +00001019 if (flag == CALL_FUNCTION) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001020 call_wrapper.BeforeCall(CallSize(adaptor));
Ben Murdoch257744e2011-11-30 15:57:28 +00001021 SetCallKind(r5, call_kind);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001022 Call(adaptor);
Ben Murdoch257744e2011-11-30 15:57:28 +00001023 call_wrapper.AfterCall();
Ben Murdochc7cc0282012-03-05 14:35:55 +00001024 if (!*definitely_mismatches) {
1025 b(done);
1026 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001028 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 Jump(adaptor, RelocInfo::CODE_TARGET);
1030 }
1031 bind(&regular_invoke);
1032 }
1033}
1034
1035
1036void MacroAssembler::InvokeCode(Register code,
1037 const ParameterCount& expected,
1038 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001039 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001040 const CallWrapper& call_wrapper,
1041 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001042 // You can't call a function without a valid frame.
1043 ASSERT(flag == JUMP_FUNCTION || has_frame());
1044
Steve Blocka7e24c12009-10-30 11:49:00 +00001045 Label done;
Ben Murdochc7cc0282012-03-05 14:35:55 +00001046 bool definitely_mismatches = false;
1047 InvokePrologue(expected, actual, Handle<Code>::null(), code,
1048 &done, &definitely_mismatches, flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001049 call_wrapper, call_kind);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001050 if (!definitely_mismatches) {
1051 if (flag == CALL_FUNCTION) {
1052 call_wrapper.BeforeCall(CallSize(code));
1053 SetCallKind(r5, call_kind);
1054 Call(code);
1055 call_wrapper.AfterCall();
1056 } else {
1057 ASSERT(flag == JUMP_FUNCTION);
1058 SetCallKind(r5, call_kind);
1059 Jump(code);
1060 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001061
Ben Murdochc7cc0282012-03-05 14:35:55 +00001062 // Continue here if InvokePrologue does handle the invocation due to
1063 // mismatched parameter counts.
1064 bind(&done);
1065 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001066}
1067
1068
1069void MacroAssembler::InvokeCode(Handle<Code> code,
1070 const ParameterCount& expected,
1071 const ParameterCount& actual,
1072 RelocInfo::Mode rmode,
Ben Murdoch257744e2011-11-30 15:57:28 +00001073 InvokeFlag flag,
1074 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001075 // You can't call a function without a valid frame.
1076 ASSERT(flag == JUMP_FUNCTION || has_frame());
1077
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 Label done;
Ben Murdochc7cc0282012-03-05 14:35:55 +00001079 bool definitely_mismatches = false;
1080 InvokePrologue(expected, actual, code, no_reg,
1081 &done, &definitely_mismatches, flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001082 NullCallWrapper(), call_kind);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001083 if (!definitely_mismatches) {
1084 if (flag == CALL_FUNCTION) {
1085 SetCallKind(r5, call_kind);
1086 Call(code, rmode);
1087 } else {
1088 SetCallKind(r5, call_kind);
1089 Jump(code, rmode);
1090 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001091
Ben Murdochc7cc0282012-03-05 14:35:55 +00001092 // Continue here if InvokePrologue does handle the invocation due to
1093 // mismatched parameter counts.
1094 bind(&done);
1095 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001096}
1097
1098
1099void MacroAssembler::InvokeFunction(Register fun,
1100 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001101 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001102 const CallWrapper& call_wrapper,
1103 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001104 // You can't call a function without a valid frame.
1105 ASSERT(flag == JUMP_FUNCTION || has_frame());
1106
Steve Blocka7e24c12009-10-30 11:49:00 +00001107 // Contract with called JS functions requires that function is passed in r1.
1108 ASSERT(fun.is(r1));
1109
1110 Register expected_reg = r2;
1111 Register code_reg = r3;
1112
1113 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1114 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1115 ldr(expected_reg,
1116 FieldMemOperand(code_reg,
1117 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001118 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001119 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +01001120 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001121
1122 ParameterCount expected(expected_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00001123 InvokeCode(code_reg, expected, actual, flag, call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001124}
1125
1126
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001127void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
Andrei Popescu402d9372010-02-26 13:31:12 +00001128 const ParameterCount& actual,
Ben Murdoch257744e2011-11-30 15:57:28 +00001129 InvokeFlag flag,
Ben Murdochc7cc0282012-03-05 14:35:55 +00001130 const CallWrapper& call_wrapper,
Ben Murdoch257744e2011-11-30 15:57:28 +00001131 CallKind call_kind) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001132 // You can't call a function without a valid frame.
1133 ASSERT(flag == JUMP_FUNCTION || has_frame());
Andrei Popescu402d9372010-02-26 13:31:12 +00001134
1135 // Get the function and setup the context.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001136 LoadHeapObject(r1, function);
Andrei Popescu402d9372010-02-26 13:31:12 +00001137 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1138
Andrei Popescu402d9372010-02-26 13:31:12 +00001139 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001140 // We call indirectly through the code field in the function to
1141 // allow recompilation to take effect without changing any of the
1142 // call sites.
1143 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001144 InvokeCode(r3, expected, actual, flag, call_wrapper, call_kind);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001145}
1146
1147
1148void MacroAssembler::IsObjectJSObjectType(Register heap_object,
1149 Register map,
1150 Register scratch,
1151 Label* fail) {
1152 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
1153 IsInstanceJSObjectType(map, scratch, fail);
1154}
1155
1156
1157void MacroAssembler::IsInstanceJSObjectType(Register map,
1158 Register scratch,
1159 Label* fail) {
1160 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001161 cmp(scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001162 b(lt, fail);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001163 cmp(scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001164 b(gt, fail);
1165}
1166
1167
1168void MacroAssembler::IsObjectJSStringType(Register object,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001169 Register scratch,
1170 Label* fail) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001171 ASSERT(kNotStringTag != 0);
1172
1173 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
1174 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1175 tst(scratch, Operand(kIsNotStringMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001176 b(ne, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +00001177}
1178
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001179
Steve Blocka7e24c12009-10-30 11:49:00 +00001180#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +00001181void MacroAssembler::DebugBreak() {
Iain Merrick9ac36c92010-09-13 15:29:50 +01001182 mov(r0, Operand(0, RelocInfo::NONE));
Steve Block44f0eee2011-05-26 01:26:41 +01001183 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +00001184 CEntryStub ces(1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001185 ASSERT(AllowThisStubCall(&ces));
Andrei Popescu402d9372010-02-26 13:31:12 +00001186 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
1187}
Steve Blocka7e24c12009-10-30 11:49:00 +00001188#endif
1189
1190
1191void MacroAssembler::PushTryHandler(CodeLocation try_location,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001192 HandlerType type,
1193 int handler_index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001194 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001195 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1196 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001197 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1198 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1199 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1200 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001201
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001202 // For the JSEntry handler, we must preserve r0-r4, r5-r7 are available.
1203 // We will build up the handler from the bottom by pushing on the stack.
1204 // First compute the state.
1205 unsigned state = StackHandler::OffsetField::encode(handler_index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 if (try_location == IN_JAVASCRIPT) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001207 state |= (type == TRY_CATCH_HANDLER)
1208 ? StackHandler::KindField::encode(StackHandler::TRY_CATCH)
1209 : StackHandler::KindField::encode(StackHandler::TRY_FINALLY);
Steve Blocka7e24c12009-10-30 11:49:00 +00001210 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00001211 ASSERT(try_location == IN_JS_ENTRY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001212 state |= StackHandler::KindField::encode(StackHandler::ENTRY);
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001214
1215 // Set up the code object (r5) and the state (r6) for pushing.
1216 mov(r5, Operand(CodeObject()));
1217 mov(r6, Operand(state));
1218
1219 // Push the frame pointer, context, state, and code object.
1220 if (try_location == IN_JAVASCRIPT) {
1221 stm(db_w, sp, r5.bit() | r6.bit() | cp.bit() | fp.bit());
1222 } else {
1223 mov(r7, Operand(Smi::FromInt(0))); // Indicates no context.
1224 mov(ip, Operand(0, RelocInfo::NONE)); // NULL frame pointer.
1225 stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | ip.bit());
1226 }
1227
1228 // Link the current handler as the next handler.
1229 mov(r6, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1230 ldr(r5, MemOperand(r6));
1231 push(r5);
1232 // Set this new handler as the current one.
1233 str(sp, MemOperand(r6));
Steve Blocka7e24c12009-10-30 11:49:00 +00001234}
1235
1236
Leon Clarkee46be812010-01-19 14:06:41 +00001237void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001238 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Leon Clarkee46be812010-01-19 14:06:41 +00001239 pop(r1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001240 mov(ip, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +00001241 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1242 str(r1, MemOperand(ip));
1243}
1244
1245
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001246void MacroAssembler::JumpToHandlerEntry() {
1247 // Compute the handler entry address and jump to it. The handler table is
1248 // a fixed array of (smi-tagged) code offsets.
1249 // r0 = exception, r1 = code object, r2 = state.
1250 ldr(r3, FieldMemOperand(r1, Code::kHandlerTableOffset)); // Handler table.
1251 add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1252 mov(r2, Operand(r2, LSR, StackHandler::kKindWidth)); // Handler index.
1253 ldr(r2, MemOperand(r3, r2, LSL, kPointerSizeLog2)); // Smi-tagged offset.
1254 add(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag)); // Code start.
1255 add(pc, r1, Operand(r2, ASR, kSmiTagSize)); // Jump.
1256}
1257
1258
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001259void MacroAssembler::Throw(Register value) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001260 // Adjust this code if not the case.
1261 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001262 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
1263 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1264 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1265 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1266 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
1267
1268 // The exception is expected in r0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001269 if (!value.is(r0)) {
1270 mov(r0, value);
1271 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001272 // Drop the stack pointer to the top of the top handler.
Ben Murdoch589d6972011-11-30 16:04:58 +00001273 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001274 ldr(sp, MemOperand(r3));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001275 // Restore the next handler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001276 pop(r2);
1277 str(r2, MemOperand(r3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001278
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001279 // Get the code object (r1) and state (r2). Restore the context and frame
1280 // pointer.
1281 ldm(ia_w, sp, r1.bit() | r2.bit() | cp.bit() | fp.bit());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001282
1283 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001284 // (kind == ENTRY) == (fp == 0) == (cp == 0), so we could test either fp
1285 // or cp.
1286 tst(cp, cp);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001287 str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1288
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001289 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001290}
1291
1292
1293void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
1294 Register value) {
1295 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001296 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1297 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001298 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
1299 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
1300 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
1301 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001302
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001303 // The exception is expected in r0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001304 if (type == OUT_OF_MEMORY) {
1305 // Set external caught exception to false.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001306 ExternalReference external_caught(Isolate::kExternalCaughtExceptionAddress,
1307 isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001308 mov(r0, Operand(false, RelocInfo::NONE));
1309 mov(r2, Operand(external_caught));
1310 str(r0, MemOperand(r2));
1311
1312 // Set pending exception and r0 to out of memory exception.
1313 Failure* out_of_memory = Failure::OutOfMemoryException();
1314 mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
Ben Murdoch589d6972011-11-30 16:04:58 +00001315 mov(r2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01001316 isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001317 str(r0, MemOperand(r2));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001318 } else if (!value.is(r0)) {
1319 mov(r0, value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001320 }
1321
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001322 // Drop the stack pointer to the top of the top stack handler.
1323 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1324 ldr(sp, MemOperand(r3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001325
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001326 // Unwind the handlers until the ENTRY handler is found.
1327 Label fetch_next, check_kind;
1328 jmp(&check_kind);
1329 bind(&fetch_next);
1330 ldr(sp, MemOperand(sp, StackHandlerConstants::kNextOffset));
1331
1332 bind(&check_kind);
1333 STATIC_ASSERT(StackHandler::ENTRY == 0);
1334 ldr(r2, MemOperand(sp, StackHandlerConstants::kStateOffset));
1335 tst(r2, Operand(StackHandler::KindField::kMask));
1336 b(ne, &fetch_next);
1337
1338 // Set the top handler address to next handler past the top ENTRY handler.
1339 pop(r2);
1340 str(r2, MemOperand(r3));
1341 // Get the code object (r1) and state (r2). Clear the context and frame
1342 // pointer (0 was saved in the handler).
1343 ldm(ia_w, sp, r1.bit() | r2.bit() | cp.bit() | fp.bit());
1344
1345 JumpToHandlerEntry();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001346}
1347
1348
Steve Blocka7e24c12009-10-30 11:49:00 +00001349void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1350 Register scratch,
1351 Label* miss) {
1352 Label same_contexts;
1353
1354 ASSERT(!holder_reg.is(scratch));
1355 ASSERT(!holder_reg.is(ip));
1356 ASSERT(!scratch.is(ip));
1357
1358 // Load current lexical context from the stack frame.
1359 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1360 // In debug mode, make sure the lexical context is set.
1361#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001362 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001363 Check(ne, "we should not have an empty lexical context");
1364#endif
1365
1366 // Load the global context of the current context.
1367 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1368 ldr(scratch, FieldMemOperand(scratch, offset));
1369 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1370
1371 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001372 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001373 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1374 // Cannot use ip as a temporary in this verification code. Due to the fact
1375 // that ip is clobbered as part of cmp with an object Operand.
1376 push(holder_reg); // Temporarily save holder on the stack.
1377 // Read the first word and compare to the global_context_map.
1378 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1379 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1380 cmp(holder_reg, ip);
1381 Check(eq, "JSGlobalObject::global_context should be a global context.");
1382 pop(holder_reg); // Restore holder.
1383 }
1384
1385 // Check if both contexts are the same.
1386 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1387 cmp(scratch, Operand(ip));
1388 b(eq, &same_contexts);
1389
1390 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001391 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001392 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1393 // Cannot use ip as a temporary in this verification code. Due to the fact
1394 // that ip is clobbered as part of cmp with an object Operand.
1395 push(holder_reg); // Temporarily save holder on the stack.
1396 mov(holder_reg, ip); // Move ip to its holding place.
1397 LoadRoot(ip, Heap::kNullValueRootIndex);
1398 cmp(holder_reg, ip);
1399 Check(ne, "JSGlobalProxy::context() should not be null.");
1400
1401 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1402 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1403 cmp(holder_reg, ip);
1404 Check(eq, "JSGlobalObject::global_context should be a global context.");
1405 // Restore ip is not needed. ip is reloaded below.
1406 pop(holder_reg); // Restore holder.
1407 // Restore ip to holder's context.
1408 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1409 }
1410
1411 // Check that the security token in the calling global object is
1412 // compatible with the security token in the receiving global
1413 // object.
1414 int token_offset = Context::kHeaderSize +
1415 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1416
1417 ldr(scratch, FieldMemOperand(scratch, token_offset));
1418 ldr(ip, FieldMemOperand(ip, token_offset));
1419 cmp(scratch, Operand(ip));
1420 b(ne, miss);
1421
1422 bind(&same_contexts);
1423}
1424
1425
Ben Murdochc7cc0282012-03-05 14:35:55 +00001426void MacroAssembler::GetNumberHash(Register t0, Register scratch) {
1427 // First of all we assign the hash seed to scratch.
1428 LoadRoot(scratch, Heap::kHashSeedRootIndex);
1429 SmiUntag(scratch);
1430
1431 // Xor original key with a seed.
1432 eor(t0, t0, Operand(scratch));
1433
1434 // Compute the hash code from the untagged key. This must be kept in sync
1435 // with ComputeIntegerHash in utils.h.
1436 //
1437 // hash = ~hash + (hash << 15);
1438 mvn(scratch, Operand(t0));
1439 add(t0, scratch, Operand(t0, LSL, 15));
1440 // hash = hash ^ (hash >> 12);
1441 eor(t0, t0, Operand(t0, LSR, 12));
1442 // hash = hash + (hash << 2);
1443 add(t0, t0, Operand(t0, LSL, 2));
1444 // hash = hash ^ (hash >> 4);
1445 eor(t0, t0, Operand(t0, LSR, 4));
1446 // hash = hash * 2057;
1447 mov(scratch, Operand(t0, LSL, 11));
1448 add(t0, t0, Operand(t0, LSL, 3));
1449 add(t0, t0, scratch);
1450 // hash = hash ^ (hash >> 16);
1451 eor(t0, t0, Operand(t0, LSR, 16));
1452}
1453
1454
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001455void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1456 Register elements,
1457 Register key,
1458 Register result,
1459 Register t0,
1460 Register t1,
1461 Register t2) {
1462 // Register use:
1463 //
1464 // elements - holds the slow-case elements of the receiver on entry.
1465 // Unchanged unless 'result' is the same register.
1466 //
1467 // key - holds the smi key on entry.
1468 // Unchanged unless 'result' is the same register.
1469 //
1470 // result - holds the result on exit if the load succeeded.
1471 // Allowed to be the same as 'key' or 'result'.
1472 // Unchanged on bailout so 'key' or 'result' can be used
1473 // in further computation.
1474 //
1475 // Scratch registers:
1476 //
1477 // t0 - holds the untagged key on entry and holds the hash once computed.
1478 //
1479 // t1 - used to hold the capacity mask of the dictionary
1480 //
1481 // t2 - used for the index into the dictionary.
1482 Label done;
1483
Ben Murdochc7cc0282012-03-05 14:35:55 +00001484 GetNumberHash(t0, t1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001485
1486 // Compute the capacity mask.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001487 ldr(t1, FieldMemOperand(elements, SeededNumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001488 mov(t1, Operand(t1, ASR, kSmiTagSize)); // convert smi to int
1489 sub(t1, t1, Operand(1));
1490
1491 // Generate an unrolled loop that performs a few probes before giving up.
1492 static const int kProbes = 4;
1493 for (int i = 0; i < kProbes; i++) {
1494 // Use t2 for index calculations and keep the hash intact in t0.
1495 mov(t2, t0);
1496 // Compute the masked index: (hash + i + i * i) & mask.
1497 if (i > 0) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001498 add(t2, t2, Operand(SeededNumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001499 }
1500 and_(t2, t2, Operand(t1));
1501
1502 // Scale the index by multiplying by the element size.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001503 ASSERT(SeededNumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001504 add(t2, t2, Operand(t2, LSL, 1)); // t2 = t2 * 3
1505
1506 // Check if the key is identical to the name.
1507 add(t2, elements, Operand(t2, LSL, kPointerSizeLog2));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001508 ldr(ip, FieldMemOperand(t2, SeededNumberDictionary::kElementsStartOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001509 cmp(key, Operand(ip));
1510 if (i != kProbes - 1) {
1511 b(eq, &done);
1512 } else {
1513 b(ne, miss);
1514 }
1515 }
1516
1517 bind(&done);
1518 // Check that the value is a normal property.
1519 // t2: elements + (index * kPointerSize)
1520 const int kDetailsOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001521 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001522 ldr(t1, FieldMemOperand(t2, kDetailsOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +00001523 tst(t1, Operand(Smi::FromInt(PropertyDetails::TypeField::kMask)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001524 b(ne, miss);
1525
1526 // Get the value at the masked, scaled index and return.
1527 const int kValueOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001528 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001529 ldr(result, FieldMemOperand(t2, kValueOffset));
1530}
1531
1532
Steve Blocka7e24c12009-10-30 11:49:00 +00001533void MacroAssembler::AllocateInNewSpace(int object_size,
1534 Register result,
1535 Register scratch1,
1536 Register scratch2,
1537 Label* gc_required,
1538 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001539 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001540 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001541 // Trash the registers to simulate an allocation failure.
1542 mov(result, Operand(0x7091));
1543 mov(scratch1, Operand(0x7191));
1544 mov(scratch2, Operand(0x7291));
1545 }
1546 jmp(gc_required);
1547 return;
1548 }
1549
Steve Blocka7e24c12009-10-30 11:49:00 +00001550 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001551 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001552 ASSERT(!scratch1.is(scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001553 ASSERT(!scratch1.is(ip));
1554 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001555
Kristian Monsen25f61362010-05-21 11:50:48 +01001556 // Make object size into bytes.
1557 if ((flags & SIZE_IN_WORDS) != 0) {
1558 object_size *= kPointerSize;
1559 }
1560 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1561
Ben Murdochb0fe1622011-05-05 13:52:32 +01001562 // Check relative positions of allocation top and limit addresses.
1563 // The values must be adjacent in memory to allow the use of LDM.
1564 // Also, assert that the registers are numbered such that the values
1565 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001566 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001567 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001568 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001569 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001570 intptr_t top =
1571 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1572 intptr_t limit =
1573 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1574 ASSERT((limit - top) == kPointerSize);
1575 ASSERT(result.code() < ip.code());
1576
1577 // Set up allocation top address and object size registers.
1578 Register topaddr = scratch1;
1579 Register obj_size_reg = scratch2;
1580 mov(topaddr, Operand(new_space_allocation_top));
1581 mov(obj_size_reg, Operand(object_size));
1582
1583 // This code stores a temporary value in ip. This is OK, as the code below
1584 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001585 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001586 // Load allocation top into result and allocation limit into ip.
1587 ldm(ia, topaddr, result.bit() | ip.bit());
1588 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001589 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001590 // Assert that result actually contains top on entry. ip is used
1591 // immediately below so this use of ip does not cause difference with
1592 // respect to register content between debug and release mode.
1593 ldr(ip, MemOperand(topaddr));
1594 cmp(result, ip);
1595 Check(eq, "Unexpected allocation top");
1596 }
1597 // Load allocation limit into ip. Result already contains allocation top.
1598 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001599 }
1600
1601 // Calculate new top and bail out if new space is exhausted. Use result
1602 // to calculate the new top.
Steve Block1e0659c2011-05-24 12:43:12 +01001603 add(scratch2, result, Operand(obj_size_reg), SetCC);
1604 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001605 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001606 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001607 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001608
Ben Murdochb0fe1622011-05-05 13:52:32 +01001609 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001610 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001611 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001612 }
1613}
1614
1615
1616void MacroAssembler::AllocateInNewSpace(Register object_size,
1617 Register result,
1618 Register scratch1,
1619 Register scratch2,
1620 Label* gc_required,
1621 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001622 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001623 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001624 // Trash the registers to simulate an allocation failure.
1625 mov(result, Operand(0x7091));
1626 mov(scratch1, Operand(0x7191));
1627 mov(scratch2, Operand(0x7291));
1628 }
1629 jmp(gc_required);
1630 return;
1631 }
1632
Ben Murdochb0fe1622011-05-05 13:52:32 +01001633 // Assert that the register arguments are different and that none of
1634 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001635 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001636 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001637 ASSERT(!scratch1.is(scratch2));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001638 ASSERT(!object_size.is(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001639 ASSERT(!result.is(ip));
1640 ASSERT(!scratch1.is(ip));
1641 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001642
Ben Murdochb0fe1622011-05-05 13:52:32 +01001643 // Check relative positions of allocation top and limit addresses.
1644 // The values must be adjacent in memory to allow the use of LDM.
1645 // Also, assert that the registers are numbered such that the values
1646 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001647 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001648 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001649 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001650 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001651 intptr_t top =
1652 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1653 intptr_t limit =
1654 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1655 ASSERT((limit - top) == kPointerSize);
1656 ASSERT(result.code() < ip.code());
1657
1658 // Set up allocation top address.
1659 Register topaddr = scratch1;
1660 mov(topaddr, Operand(new_space_allocation_top));
1661
1662 // This code stores a temporary value in ip. This is OK, as the code below
1663 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001664 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001665 // Load allocation top into result and allocation limit into ip.
1666 ldm(ia, topaddr, result.bit() | ip.bit());
1667 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001668 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001669 // Assert that result actually contains top on entry. ip is used
1670 // immediately below so this use of ip does not cause difference with
1671 // respect to register content between debug and release mode.
1672 ldr(ip, MemOperand(topaddr));
1673 cmp(result, ip);
1674 Check(eq, "Unexpected allocation top");
1675 }
1676 // Load allocation limit into ip. Result already contains allocation top.
1677 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 }
1679
1680 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001681 // to calculate the new top. Object size may be in words so a shift is
1682 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001683 if ((flags & SIZE_IN_WORDS) != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001684 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001685 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001686 add(scratch2, result, Operand(object_size), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001687 }
Steve Block1e0659c2011-05-24 12:43:12 +01001688 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001689 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001690 b(hi, gc_required);
1691
Steve Blockd0582a62009-12-15 09:54:21 +00001692 // Update allocation top. result temporarily holds the new top.
Steve Block44f0eee2011-05-26 01:26:41 +01001693 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001694 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001695 Check(eq, "Unaligned allocation in new space");
1696 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001697 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001698
1699 // Tag object if requested.
1700 if ((flags & TAG_OBJECT) != 0) {
1701 add(result, result, Operand(kHeapObjectTag));
1702 }
1703}
1704
1705
1706void MacroAssembler::UndoAllocationInNewSpace(Register object,
1707 Register scratch) {
1708 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001709 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001710
1711 // Make sure the object has no tag before resetting top.
1712 and_(object, object, Operand(~kHeapObjectTagMask));
1713#ifdef DEBUG
1714 // Check that the object un-allocated is below the current top.
1715 mov(scratch, Operand(new_space_allocation_top));
1716 ldr(scratch, MemOperand(scratch));
1717 cmp(object, scratch);
1718 Check(lt, "Undo allocation of non allocated memory");
1719#endif
1720 // Write the address of the object to un-allocate as the current top.
1721 mov(scratch, Operand(new_space_allocation_top));
1722 str(object, MemOperand(scratch));
1723}
1724
1725
Andrei Popescu31002712010-02-23 13:46:05 +00001726void MacroAssembler::AllocateTwoByteString(Register result,
1727 Register length,
1728 Register scratch1,
1729 Register scratch2,
1730 Register scratch3,
1731 Label* gc_required) {
1732 // Calculate the number of bytes needed for the characters in the string while
1733 // observing object alignment.
1734 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1735 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1736 add(scratch1, scratch1,
1737 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001738 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001739
1740 // Allocate two-byte string in new space.
1741 AllocateInNewSpace(scratch1,
1742 result,
1743 scratch2,
1744 scratch3,
1745 gc_required,
1746 TAG_OBJECT);
1747
1748 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001749 InitializeNewString(result,
1750 length,
1751 Heap::kStringMapRootIndex,
1752 scratch1,
1753 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001754}
1755
1756
1757void MacroAssembler::AllocateAsciiString(Register result,
1758 Register length,
1759 Register scratch1,
1760 Register scratch2,
1761 Register scratch3,
1762 Label* gc_required) {
1763 // Calculate the number of bytes needed for the characters in the string while
1764 // observing object alignment.
1765 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1766 ASSERT(kCharSize == 1);
1767 add(scratch1, length,
1768 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001769 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001770
1771 // Allocate ASCII string in new space.
1772 AllocateInNewSpace(scratch1,
1773 result,
1774 scratch2,
1775 scratch3,
1776 gc_required,
1777 TAG_OBJECT);
1778
1779 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001780 InitializeNewString(result,
1781 length,
1782 Heap::kAsciiStringMapRootIndex,
1783 scratch1,
1784 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001785}
1786
1787
1788void MacroAssembler::AllocateTwoByteConsString(Register result,
1789 Register length,
1790 Register scratch1,
1791 Register scratch2,
1792 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001793 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001794 result,
1795 scratch1,
1796 scratch2,
1797 gc_required,
1798 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001799
1800 InitializeNewString(result,
1801 length,
1802 Heap::kConsStringMapRootIndex,
1803 scratch1,
1804 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001805}
1806
1807
1808void MacroAssembler::AllocateAsciiConsString(Register result,
1809 Register length,
1810 Register scratch1,
1811 Register scratch2,
1812 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001813 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001814 result,
1815 scratch1,
1816 scratch2,
1817 gc_required,
1818 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001819
1820 InitializeNewString(result,
1821 length,
1822 Heap::kConsAsciiStringMapRootIndex,
1823 scratch1,
1824 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001825}
1826
1827
Ben Murdoch589d6972011-11-30 16:04:58 +00001828void MacroAssembler::AllocateTwoByteSlicedString(Register result,
1829 Register length,
1830 Register scratch1,
1831 Register scratch2,
1832 Label* gc_required) {
1833 AllocateInNewSpace(SlicedString::kSize,
1834 result,
1835 scratch1,
1836 scratch2,
1837 gc_required,
1838 TAG_OBJECT);
1839
1840 InitializeNewString(result,
1841 length,
1842 Heap::kSlicedStringMapRootIndex,
1843 scratch1,
1844 scratch2);
1845}
1846
1847
1848void MacroAssembler::AllocateAsciiSlicedString(Register result,
1849 Register length,
1850 Register scratch1,
1851 Register scratch2,
1852 Label* gc_required) {
1853 AllocateInNewSpace(SlicedString::kSize,
1854 result,
1855 scratch1,
1856 scratch2,
1857 gc_required,
1858 TAG_OBJECT);
1859
1860 InitializeNewString(result,
1861 length,
1862 Heap::kSlicedAsciiStringMapRootIndex,
1863 scratch1,
1864 scratch2);
1865}
1866
1867
Steve Block6ded16b2010-05-10 14:33:55 +01001868void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001869 Register map,
1870 Register type_reg,
1871 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001872 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001873 CompareInstanceType(map, type_reg, type);
1874}
1875
1876
1877void MacroAssembler::CompareInstanceType(Register map,
1878 Register type_reg,
1879 InstanceType type) {
1880 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1881 cmp(type_reg, Operand(type));
1882}
1883
1884
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001885void MacroAssembler::CompareRoot(Register obj,
1886 Heap::RootListIndex index) {
1887 ASSERT(!obj.is(ip));
1888 LoadRoot(ip, index);
1889 cmp(obj, ip);
1890}
1891
1892
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001893void MacroAssembler::CheckFastElements(Register map,
1894 Register scratch,
1895 Label* fail) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001896 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
1897 STATIC_ASSERT(FAST_ELEMENTS == 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001898 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1899 cmp(scratch, Operand(Map::kMaximumBitField2FastElementValue));
1900 b(hi, fail);
1901}
1902
1903
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001904void MacroAssembler::CheckFastObjectElements(Register map,
1905 Register scratch,
1906 Label* fail) {
1907 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
1908 STATIC_ASSERT(FAST_ELEMENTS == 1);
1909 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1910 cmp(scratch, Operand(Map::kMaximumBitField2FastSmiOnlyElementValue));
1911 b(ls, fail);
1912 cmp(scratch, Operand(Map::kMaximumBitField2FastElementValue));
1913 b(hi, fail);
1914}
1915
1916
1917void MacroAssembler::CheckFastSmiOnlyElements(Register map,
1918 Register scratch,
1919 Label* fail) {
1920 STATIC_ASSERT(FAST_SMI_ONLY_ELEMENTS == 0);
1921 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1922 cmp(scratch, Operand(Map::kMaximumBitField2FastSmiOnlyElementValue));
1923 b(hi, fail);
1924}
1925
1926
1927void MacroAssembler::StoreNumberToDoubleElements(Register value_reg,
1928 Register key_reg,
1929 Register receiver_reg,
1930 Register elements_reg,
1931 Register scratch1,
1932 Register scratch2,
1933 Register scratch3,
1934 Register scratch4,
1935 Label* fail) {
1936 Label smi_value, maybe_nan, have_double_value, is_nan, done;
1937 Register mantissa_reg = scratch2;
1938 Register exponent_reg = scratch3;
1939
1940 // Handle smi values specially.
1941 JumpIfSmi(value_reg, &smi_value);
1942
1943 // Ensure that the object is a heap number
1944 CheckMap(value_reg,
1945 scratch1,
1946 isolate()->factory()->heap_number_map(),
1947 fail,
1948 DONT_DO_SMI_CHECK);
1949
1950 // Check for nan: all NaN values have a value greater (signed) than 0x7ff00000
1951 // in the exponent.
1952 mov(scratch1, Operand(kNaNOrInfinityLowerBoundUpper32));
1953 ldr(exponent_reg, FieldMemOperand(value_reg, HeapNumber::kExponentOffset));
1954 cmp(exponent_reg, scratch1);
1955 b(ge, &maybe_nan);
1956
1957 ldr(mantissa_reg, FieldMemOperand(value_reg, HeapNumber::kMantissaOffset));
1958
1959 bind(&have_double_value);
1960 add(scratch1, elements_reg,
1961 Operand(key_reg, LSL, kDoubleSizeLog2 - kSmiTagSize));
1962 str(mantissa_reg, FieldMemOperand(scratch1, FixedDoubleArray::kHeaderSize));
1963 uint32_t offset = FixedDoubleArray::kHeaderSize + sizeof(kHoleNanLower32);
1964 str(exponent_reg, FieldMemOperand(scratch1, offset));
1965 jmp(&done);
1966
1967 bind(&maybe_nan);
1968 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
1969 // it's an Infinity, and the non-NaN code path applies.
1970 b(gt, &is_nan);
1971 ldr(mantissa_reg, FieldMemOperand(value_reg, HeapNumber::kMantissaOffset));
1972 cmp(mantissa_reg, Operand(0));
1973 b(eq, &have_double_value);
1974 bind(&is_nan);
1975 // Load canonical NaN for storing into the double array.
1976 uint64_t nan_int64 = BitCast<uint64_t>(
1977 FixedDoubleArray::canonical_not_the_hole_nan_as_double());
1978 mov(mantissa_reg, Operand(static_cast<uint32_t>(nan_int64)));
1979 mov(exponent_reg, Operand(static_cast<uint32_t>(nan_int64 >> 32)));
1980 jmp(&have_double_value);
1981
1982 bind(&smi_value);
1983 add(scratch1, elements_reg,
1984 Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag));
1985 add(scratch1, scratch1,
1986 Operand(key_reg, LSL, kDoubleSizeLog2 - kSmiTagSize));
1987 // scratch1 is now effective address of the double element
1988
1989 FloatingPointHelper::Destination destination;
1990 if (CpuFeatures::IsSupported(VFP3)) {
1991 destination = FloatingPointHelper::kVFPRegisters;
1992 } else {
1993 destination = FloatingPointHelper::kCoreRegisters;
1994 }
1995
1996 Register untagged_value = receiver_reg;
1997 SmiUntag(untagged_value, value_reg);
1998 FloatingPointHelper::ConvertIntToDouble(this,
1999 untagged_value,
2000 destination,
2001 d0,
2002 mantissa_reg,
2003 exponent_reg,
2004 scratch4,
2005 s2);
2006 if (destination == FloatingPointHelper::kVFPRegisters) {
2007 CpuFeatures::Scope scope(VFP3);
2008 vstr(d0, scratch1, 0);
2009 } else {
2010 str(mantissa_reg, MemOperand(scratch1, 0));
2011 str(exponent_reg, MemOperand(scratch1, Register::kSizeInBytes));
2012 }
2013 bind(&done);
2014}
2015
2016
Ben Murdochc7cc0282012-03-05 14:35:55 +00002017void MacroAssembler::CompareMap(Register obj,
2018 Register scratch,
2019 Handle<Map> map,
2020 Label* early_success,
2021 CompareMapMode mode) {
2022 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2023 cmp(scratch, Operand(map));
2024 if (mode == ALLOW_ELEMENT_TRANSITION_MAPS) {
2025 Map* transitioned_fast_element_map(
2026 map->LookupElementsTransitionMap(FAST_ELEMENTS, NULL));
2027 ASSERT(transitioned_fast_element_map == NULL ||
2028 map->elements_kind() != FAST_ELEMENTS);
2029 if (transitioned_fast_element_map != NULL) {
2030 b(eq, early_success);
2031 cmp(scratch, Operand(Handle<Map>(transitioned_fast_element_map)));
2032 }
2033
2034 Map* transitioned_double_map(
2035 map->LookupElementsTransitionMap(FAST_DOUBLE_ELEMENTS, NULL));
2036 ASSERT(transitioned_double_map == NULL ||
2037 map->elements_kind() == FAST_SMI_ONLY_ELEMENTS);
2038 if (transitioned_double_map != NULL) {
2039 b(eq, early_success);
2040 cmp(scratch, Operand(Handle<Map>(transitioned_double_map)));
2041 }
2042 }
2043}
2044
2045
Andrei Popescu31002712010-02-23 13:46:05 +00002046void MacroAssembler::CheckMap(Register obj,
2047 Register scratch,
2048 Handle<Map> map,
2049 Label* fail,
Ben Murdochc7cc0282012-03-05 14:35:55 +00002050 SmiCheckType smi_check_type,
2051 CompareMapMode mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002052 if (smi_check_type == DO_SMI_CHECK) {
Steve Block1e0659c2011-05-24 12:43:12 +01002053 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +00002054 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00002055
2056 Label success;
2057 CompareMap(obj, scratch, map, &success, mode);
Andrei Popescu31002712010-02-23 13:46:05 +00002058 b(ne, fail);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002059 bind(&success);
Andrei Popescu31002712010-02-23 13:46:05 +00002060}
2061
2062
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002063void MacroAssembler::CheckMap(Register obj,
2064 Register scratch,
2065 Heap::RootListIndex index,
2066 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00002067 SmiCheckType smi_check_type) {
2068 if (smi_check_type == DO_SMI_CHECK) {
Steve Block1e0659c2011-05-24 12:43:12 +01002069 JumpIfSmi(obj, fail);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002070 }
2071 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2072 LoadRoot(ip, index);
2073 cmp(scratch, ip);
2074 b(ne, fail);
2075}
2076
2077
Ben Murdoch257744e2011-11-30 15:57:28 +00002078void MacroAssembler::DispatchMap(Register obj,
2079 Register scratch,
2080 Handle<Map> map,
2081 Handle<Code> success,
2082 SmiCheckType smi_check_type) {
2083 Label fail;
2084 if (smi_check_type == DO_SMI_CHECK) {
2085 JumpIfSmi(obj, &fail);
2086 }
2087 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
2088 mov(ip, Operand(map));
2089 cmp(scratch, ip);
2090 Jump(success, RelocInfo::CODE_TARGET, eq);
2091 bind(&fail);
2092}
2093
2094
Steve Blocka7e24c12009-10-30 11:49:00 +00002095void MacroAssembler::TryGetFunctionPrototype(Register function,
2096 Register result,
2097 Register scratch,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002098 Label* miss,
2099 bool miss_on_bound_function) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002100 // Check that the receiver isn't a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002101 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00002102
2103 // Check that the function really is a function. Load map into result reg.
2104 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
2105 b(ne, miss);
2106
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002107 if (miss_on_bound_function) {
2108 ldr(scratch,
2109 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
2110 ldr(scratch,
2111 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
2112 tst(scratch,
2113 Operand(Smi::FromInt(1 << SharedFunctionInfo::kBoundFunction)));
2114 b(ne, miss);
2115 }
2116
Steve Blocka7e24c12009-10-30 11:49:00 +00002117 // Make sure that the function has an instance prototype.
2118 Label non_instance;
2119 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
2120 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
2121 b(ne, &non_instance);
2122
2123 // Get the prototype or initial map from the function.
2124 ldr(result,
2125 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2126
2127 // If the prototype or initial map is the hole, don't return it and
2128 // simply miss the cache instead. This will allow us to allocate a
2129 // prototype object on-demand in the runtime system.
2130 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2131 cmp(result, ip);
2132 b(eq, miss);
2133
2134 // If the function does not have an initial map, we're done.
2135 Label done;
2136 CompareObjectType(result, scratch, scratch, MAP_TYPE);
2137 b(ne, &done);
2138
2139 // Get the prototype from the initial map.
2140 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
2141 jmp(&done);
2142
2143 // Non-instance prototype: Fetch prototype from constructor field
2144 // in initial map.
2145 bind(&non_instance);
2146 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
2147
2148 // All done.
2149 bind(&done);
2150}
2151
2152
2153void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002154 ASSERT(AllowThisStubCall(stub)); // Stub calls are not allowed in some stubs.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002155 Call(stub->GetCode(), RelocInfo::CODE_TARGET, kNoASTId, cond);
Steve Blocka7e24c12009-10-30 11:49:00 +00002156}
2157
2158
Andrei Popescu31002712010-02-23 13:46:05 +00002159void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002160 ASSERT(allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe());
Andrei Popescu31002712010-02-23 13:46:05 +00002161 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
2162}
2163
2164
Steve Block1e0659c2011-05-24 12:43:12 +01002165static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
2166 return ref0.address() - ref1.address();
2167}
2168
2169
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002170void MacroAssembler::CallApiFunctionAndReturn(ExternalReference function,
2171 int stack_space) {
Steve Block1e0659c2011-05-24 12:43:12 +01002172 ExternalReference next_address =
2173 ExternalReference::handle_scope_next_address();
2174 const int kNextOffset = 0;
2175 const int kLimitOffset = AddressOffset(
2176 ExternalReference::handle_scope_limit_address(),
2177 next_address);
2178 const int kLevelOffset = AddressOffset(
2179 ExternalReference::handle_scope_level_address(),
2180 next_address);
2181
2182 // Allocate HandleScope in callee-save registers.
2183 mov(r7, Operand(next_address));
2184 ldr(r4, MemOperand(r7, kNextOffset));
2185 ldr(r5, MemOperand(r7, kLimitOffset));
2186 ldr(r6, MemOperand(r7, kLevelOffset));
2187 add(r6, r6, Operand(1));
2188 str(r6, MemOperand(r7, kLevelOffset));
2189
2190 // Native call returns to the DirectCEntry stub which redirects to the
2191 // return address pushed on stack (could have moved after GC).
2192 // DirectCEntry stub itself is generated early and never moves.
2193 DirectCEntryStub stub;
2194 stub.GenerateCall(this, function);
2195
2196 Label promote_scheduled_exception;
2197 Label delete_allocated_handles;
2198 Label leave_exit_frame;
2199
2200 // If result is non-zero, dereference to get the result value
2201 // otherwise set it to undefined.
2202 cmp(r0, Operand(0));
2203 LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
2204 ldr(r0, MemOperand(r0), ne);
2205
2206 // No more valid handles (the result handle was the last one). Restore
2207 // previous handle scope.
2208 str(r4, MemOperand(r7, kNextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002209 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002210 ldr(r1, MemOperand(r7, kLevelOffset));
2211 cmp(r1, r6);
2212 Check(eq, "Unexpected level after return from api call");
2213 }
2214 sub(r6, r6, Operand(1));
2215 str(r6, MemOperand(r7, kLevelOffset));
2216 ldr(ip, MemOperand(r7, kLimitOffset));
2217 cmp(r5, ip);
2218 b(ne, &delete_allocated_handles);
2219
2220 // Check if the function scheduled an exception.
2221 bind(&leave_exit_frame);
2222 LoadRoot(r4, Heap::kTheHoleValueRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01002223 mov(ip, Operand(ExternalReference::scheduled_exception_address(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +01002224 ldr(r5, MemOperand(ip));
2225 cmp(r4, r5);
2226 b(ne, &promote_scheduled_exception);
2227
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002228 // LeaveExitFrame expects unwind space to be in a register.
Steve Block1e0659c2011-05-24 12:43:12 +01002229 mov(r4, Operand(stack_space));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002230 LeaveExitFrame(false, r4);
2231 mov(pc, lr);
Steve Block1e0659c2011-05-24 12:43:12 +01002232
2233 bind(&promote_scheduled_exception);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002234 TailCallExternalReference(
2235 ExternalReference(Runtime::kPromoteScheduledException, isolate()),
2236 0,
2237 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002238
2239 // HandleScope limit has changed. Delete allocated extensions.
2240 bind(&delete_allocated_handles);
2241 str(r5, MemOperand(r7, kLimitOffset));
2242 mov(r4, r0);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002243 PrepareCallCFunction(1, r5);
2244 mov(r0, Operand(ExternalReference::isolate_address()));
Steve Block44f0eee2011-05-26 01:26:41 +01002245 CallCFunction(
Ben Murdoch8b112d22011-06-08 16:22:53 +01002246 ExternalReference::delete_handle_scope_extensions(isolate()), 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002247 mov(r0, r4);
2248 jmp(&leave_exit_frame);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002249}
Steve Block1e0659c2011-05-24 12:43:12 +01002250
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002251
2252bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
2253 if (!has_frame_ && stub->SometimesSetsUpAFrame()) return false;
2254 return allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe();
Steve Block1e0659c2011-05-24 12:43:12 +01002255}
2256
2257
Steve Blocka7e24c12009-10-30 11:49:00 +00002258void MacroAssembler::IllegalOperation(int num_arguments) {
2259 if (num_arguments > 0) {
2260 add(sp, sp, Operand(num_arguments * kPointerSize));
2261 }
2262 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2263}
2264
2265
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002266void MacroAssembler::IndexFromHash(Register hash, Register index) {
2267 // If the hash field contains an array index pick it out. The assert checks
2268 // that the constants for the maximum number of digits for an array index
2269 // cached in the hash field and the number of bits reserved for it does not
2270 // conflict.
2271 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
2272 (1 << String::kArrayIndexValueBits));
2273 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
2274 // the low kHashShift bits.
2275 STATIC_ASSERT(kSmiTag == 0);
2276 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
2277 mov(index, Operand(hash, LSL, kSmiTagSize));
2278}
2279
2280
Steve Blockd0582a62009-12-15 09:54:21 +00002281void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
2282 Register outHighReg,
2283 Register outLowReg) {
2284 // ARMv7 VFP3 instructions to implement integer to double conversion.
2285 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00002286 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01002287 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00002288 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00002289}
2290
2291
Steve Block8defd9f2010-07-08 12:39:36 +01002292void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
2293 DwVfpRegister result,
2294 Register scratch1,
2295 Register scratch2,
2296 Register heap_number_map,
2297 SwVfpRegister scratch3,
2298 Label* not_number,
2299 ObjectToDoubleFlags flags) {
2300 Label done;
2301 if ((flags & OBJECT_NOT_SMI) == 0) {
2302 Label not_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01002303 JumpIfNotSmi(object, &not_smi);
Steve Block8defd9f2010-07-08 12:39:36 +01002304 // Remove smi tag and convert to double.
2305 mov(scratch1, Operand(object, ASR, kSmiTagSize));
2306 vmov(scratch3, scratch1);
2307 vcvt_f64_s32(result, scratch3);
2308 b(&done);
2309 bind(&not_smi);
2310 }
2311 // Check for heap number and load double value from it.
2312 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
2313 sub(scratch2, object, Operand(kHeapObjectTag));
2314 cmp(scratch1, heap_number_map);
2315 b(ne, not_number);
2316 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
2317 // If exponent is all ones the number is either a NaN or +/-Infinity.
2318 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
2319 Sbfx(scratch1,
2320 scratch1,
2321 HeapNumber::kExponentShift,
2322 HeapNumber::kExponentBits);
2323 // All-one value sign extend to -1.
2324 cmp(scratch1, Operand(-1));
2325 b(eq, not_number);
2326 }
2327 vldr(result, scratch2, HeapNumber::kValueOffset);
2328 bind(&done);
2329}
2330
2331
2332void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
2333 DwVfpRegister value,
2334 Register scratch1,
2335 SwVfpRegister scratch2) {
2336 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
2337 vmov(scratch2, scratch1);
2338 vcvt_f64_s32(value, scratch2);
2339}
2340
2341
Iain Merrick9ac36c92010-09-13 15:29:50 +01002342// Tries to get a signed int32 out of a double precision floating point heap
2343// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
2344// 32bits signed integer range.
2345void MacroAssembler::ConvertToInt32(Register source,
2346 Register dest,
2347 Register scratch,
2348 Register scratch2,
Steve Block1e0659c2011-05-24 12:43:12 +01002349 DwVfpRegister double_scratch,
Iain Merrick9ac36c92010-09-13 15:29:50 +01002350 Label *not_int32) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002351 if (CpuFeatures::IsSupported(VFP3)) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01002352 CpuFeatures::Scope scope(VFP3);
2353 sub(scratch, source, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01002354 vldr(double_scratch, scratch, HeapNumber::kValueOffset);
2355 vcvt_s32_f64(double_scratch.low(), double_scratch);
2356 vmov(dest, double_scratch.low());
Iain Merrick9ac36c92010-09-13 15:29:50 +01002357 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
2358 // maximun (0x7fffffff) signed 32bits integer when the double is out of
2359 // range. When substracting one, the minimum signed integer becomes the
2360 // maximun signed integer.
2361 sub(scratch, dest, Operand(1));
2362 cmp(scratch, Operand(LONG_MAX - 1));
2363 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
2364 b(ge, not_int32);
2365 } else {
2366 // This code is faster for doubles that are in the ranges -0x7fffffff to
2367 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
2368 // the range of signed int32 values that are not Smis. Jumps to the label
2369 // 'not_int32' if the double isn't in the range -0x80000000.0 to
2370 // 0x80000000.0 (excluding the endpoints).
2371 Label right_exponent, done;
2372 // Get exponent word.
2373 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
2374 // Get exponent alone in scratch2.
2375 Ubfx(scratch2,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002376 scratch,
2377 HeapNumber::kExponentShift,
2378 HeapNumber::kExponentBits);
Iain Merrick9ac36c92010-09-13 15:29:50 +01002379 // Load dest with zero. We use this either for the final shift or
2380 // for the answer.
2381 mov(dest, Operand(0, RelocInfo::NONE));
2382 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
2383 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
2384 // the exponent that we are fastest at and also the highest exponent we can
2385 // handle here.
2386 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
2387 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
2388 // split it up to avoid a constant pool entry. You can't do that in general
2389 // for cmp because of the overflow flag, but we know the exponent is in the
2390 // range 0-2047 so there is no overflow.
2391 int fudge_factor = 0x400;
2392 sub(scratch2, scratch2, Operand(fudge_factor));
2393 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
2394 // If we have a match of the int32-but-not-Smi exponent then skip some
2395 // logic.
2396 b(eq, &right_exponent);
2397 // If the exponent is higher than that then go to slow case. This catches
2398 // numbers that don't fit in a signed int32, infinities and NaNs.
2399 b(gt, not_int32);
2400
2401 // We know the exponent is smaller than 30 (biased). If it is less than
Ben Murdochc7cc0282012-03-05 14:35:55 +00002402 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, i.e.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002403 // it rounds to zero.
2404 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
2405 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
2406 // Dest already has a Smi zero.
2407 b(lt, &done);
2408
2409 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
2410 // get how much to shift down.
2411 rsb(dest, scratch2, Operand(30));
2412
2413 bind(&right_exponent);
2414 // Get the top bits of the mantissa.
2415 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
2416 // Put back the implicit 1.
2417 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
2418 // Shift up the mantissa bits to take up the space the exponent used to
2419 // take. We just orred in the implicit bit so that took care of one and
2420 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
2421 // distance.
2422 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
2423 mov(scratch2, Operand(scratch2, LSL, shift_distance));
2424 // Put sign in zero flag.
2425 tst(scratch, Operand(HeapNumber::kSignMask));
2426 // Get the second half of the double. For some exponents we don't
2427 // actually need this because the bits get shifted out again, but
2428 // it's probably slower to test than just to do it.
2429 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
2430 // Shift down 22 bits to get the last 10 bits.
2431 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
2432 // Move down according to the exponent.
2433 mov(dest, Operand(scratch, LSR, dest));
2434 // Fix sign if sign bit was set.
2435 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
2436 bind(&done);
2437 }
2438}
2439
2440
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002441void MacroAssembler::EmitVFPTruncate(VFPRoundingMode rounding_mode,
2442 SwVfpRegister result,
2443 DwVfpRegister double_input,
2444 Register scratch1,
2445 Register scratch2,
2446 CheckForInexactConversion check_inexact) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002447 ASSERT(CpuFeatures::IsSupported(VFP3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002448 CpuFeatures::Scope scope(VFP3);
2449 Register prev_fpscr = scratch1;
2450 Register scratch = scratch2;
2451
2452 int32_t check_inexact_conversion =
2453 (check_inexact == kCheckForInexactConversion) ? kVFPInexactExceptionBit : 0;
2454
2455 // Set custom FPCSR:
2456 // - Set rounding mode.
2457 // - Clear vfp cumulative exception flags.
2458 // - Make sure Flush-to-zero mode control bit is unset.
2459 vmrs(prev_fpscr);
2460 bic(scratch,
2461 prev_fpscr,
2462 Operand(kVFPExceptionMask |
2463 check_inexact_conversion |
2464 kVFPRoundingModeMask |
2465 kVFPFlushToZeroMask));
2466 // 'Round To Nearest' is encoded by 0b00 so no bits need to be set.
2467 if (rounding_mode != kRoundToNearest) {
2468 orr(scratch, scratch, Operand(rounding_mode));
2469 }
2470 vmsr(scratch);
2471
2472 // Convert the argument to an integer.
2473 vcvt_s32_f64(result,
2474 double_input,
2475 (rounding_mode == kRoundToZero) ? kDefaultRoundToZero
2476 : kFPSCRRounding);
2477
2478 // Retrieve FPSCR.
2479 vmrs(scratch);
2480 // Restore FPSCR.
2481 vmsr(prev_fpscr);
2482 // Check for vfp exceptions.
2483 tst(scratch, Operand(kVFPExceptionMask | check_inexact_conversion));
2484}
2485
2486
Steve Block44f0eee2011-05-26 01:26:41 +01002487void MacroAssembler::EmitOutOfInt32RangeTruncate(Register result,
2488 Register input_high,
2489 Register input_low,
2490 Register scratch) {
2491 Label done, normal_exponent, restore_sign;
2492
2493 // Extract the biased exponent in result.
2494 Ubfx(result,
2495 input_high,
2496 HeapNumber::kExponentShift,
2497 HeapNumber::kExponentBits);
2498
2499 // Check for Infinity and NaNs, which should return 0.
2500 cmp(result, Operand(HeapNumber::kExponentMask));
2501 mov(result, Operand(0), LeaveCC, eq);
2502 b(eq, &done);
2503
2504 // Express exponent as delta to (number of mantissa bits + 31).
2505 sub(result,
2506 result,
2507 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31),
2508 SetCC);
2509
2510 // If the delta is strictly positive, all bits would be shifted away,
2511 // which means that we can return 0.
2512 b(le, &normal_exponent);
2513 mov(result, Operand(0));
2514 b(&done);
2515
2516 bind(&normal_exponent);
2517 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
2518 // Calculate shift.
2519 add(scratch, result, Operand(kShiftBase + HeapNumber::kMantissaBits), SetCC);
2520
2521 // Save the sign.
2522 Register sign = result;
2523 result = no_reg;
2524 and_(sign, input_high, Operand(HeapNumber::kSignMask));
2525
2526 // Set the implicit 1 before the mantissa part in input_high.
2527 orr(input_high,
2528 input_high,
2529 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
2530 // Shift the mantissa bits to the correct position.
2531 // We don't need to clear non-mantissa bits as they will be shifted away.
2532 // If they weren't, it would mean that the answer is in the 32bit range.
2533 mov(input_high, Operand(input_high, LSL, scratch));
2534
2535 // Replace the shifted bits with bits from the lower mantissa word.
2536 Label pos_shift, shift_done;
2537 rsb(scratch, scratch, Operand(32), SetCC);
2538 b(&pos_shift, ge);
2539
2540 // Negate scratch.
2541 rsb(scratch, scratch, Operand(0));
2542 mov(input_low, Operand(input_low, LSL, scratch));
2543 b(&shift_done);
2544
2545 bind(&pos_shift);
2546 mov(input_low, Operand(input_low, LSR, scratch));
2547
2548 bind(&shift_done);
2549 orr(input_high, input_high, Operand(input_low));
2550 // Restore sign if necessary.
2551 cmp(sign, Operand(0));
2552 result = sign;
2553 sign = no_reg;
2554 rsb(result, input_high, Operand(0), LeaveCC, ne);
2555 mov(result, input_high, LeaveCC, eq);
2556 bind(&done);
2557}
2558
2559
2560void MacroAssembler::EmitECMATruncate(Register result,
2561 DwVfpRegister double_input,
2562 SwVfpRegister single_scratch,
2563 Register scratch,
2564 Register input_high,
2565 Register input_low) {
2566 CpuFeatures::Scope scope(VFP3);
2567 ASSERT(!input_high.is(result));
2568 ASSERT(!input_low.is(result));
2569 ASSERT(!input_low.is(input_high));
2570 ASSERT(!scratch.is(result) &&
2571 !scratch.is(input_high) &&
2572 !scratch.is(input_low));
2573 ASSERT(!single_scratch.is(double_input.low()) &&
2574 !single_scratch.is(double_input.high()));
2575
2576 Label done;
2577
2578 // Clear cumulative exception flags.
2579 ClearFPSCRBits(kVFPExceptionMask, scratch);
2580 // Try a conversion to a signed integer.
2581 vcvt_s32_f64(single_scratch, double_input);
2582 vmov(result, single_scratch);
2583 // Retrieve he FPSCR.
2584 vmrs(scratch);
2585 // Check for overflow and NaNs.
2586 tst(scratch, Operand(kVFPOverflowExceptionBit |
2587 kVFPUnderflowExceptionBit |
2588 kVFPInvalidOpExceptionBit));
2589 // If we had no exceptions we are done.
2590 b(eq, &done);
2591
2592 // Load the double value and perform a manual truncation.
2593 vmov(input_low, input_high, double_input);
2594 EmitOutOfInt32RangeTruncate(result,
2595 input_high,
2596 input_low,
2597 scratch);
2598 bind(&done);
2599}
2600
2601
Andrei Popescu31002712010-02-23 13:46:05 +00002602void MacroAssembler::GetLeastBitsFromSmi(Register dst,
2603 Register src,
2604 int num_least_bits) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002605 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002606 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00002607 } else {
2608 mov(dst, Operand(src, ASR, kSmiTagSize));
2609 and_(dst, dst, Operand((1 << num_least_bits) - 1));
2610 }
2611}
2612
2613
Steve Block1e0659c2011-05-24 12:43:12 +01002614void MacroAssembler::GetLeastBitsFromInt32(Register dst,
2615 Register src,
2616 int num_least_bits) {
2617 and_(dst, src, Operand((1 << num_least_bits) - 1));
2618}
2619
2620
Steve Block44f0eee2011-05-26 01:26:41 +01002621void MacroAssembler::CallRuntime(const Runtime::Function* f,
2622 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002623 // All parameters are on the stack. r0 has the return value after call.
2624
2625 // If the expected number of arguments of the runtime function is
2626 // constant, we check that the actual number of arguments match the
2627 // expectation.
2628 if (f->nargs >= 0 && f->nargs != num_arguments) {
2629 IllegalOperation(num_arguments);
2630 return;
2631 }
2632
Leon Clarke4515c472010-02-03 11:58:03 +00002633 // TODO(1236192): Most runtime routines don't need the number of
2634 // arguments passed in because it is constant. At some point we
2635 // should remove this need and make the runtime routine entry code
2636 // smarter.
2637 mov(r0, Operand(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01002638 mov(r1, Operand(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00002639 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002640 CallStub(&stub);
2641}
2642
2643
2644void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
2645 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
2646}
2647
2648
Ben Murdochb0fe1622011-05-05 13:52:32 +01002649void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01002650 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002651 mov(r0, Operand(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01002652 mov(r1, Operand(ExternalReference(function, isolate())));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002653 CEntryStub stub(1, kSaveFPRegs);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002654 CallStub(&stub);
2655}
2656
2657
Andrei Popescu402d9372010-02-26 13:31:12 +00002658void MacroAssembler::CallExternalReference(const ExternalReference& ext,
2659 int num_arguments) {
2660 mov(r0, Operand(num_arguments));
2661 mov(r1, Operand(ext));
2662
2663 CEntryStub stub(1);
2664 CallStub(&stub);
2665}
2666
2667
Steve Block6ded16b2010-05-10 14:33:55 +01002668void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2669 int num_arguments,
2670 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002671 // TODO(1236192): Most runtime routines don't need the number of
2672 // arguments passed in because it is constant. At some point we
2673 // should remove this need and make the runtime routine entry code
2674 // smarter.
2675 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002676 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00002677}
2678
2679
Steve Block6ded16b2010-05-10 14:33:55 +01002680void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2681 int num_arguments,
2682 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002683 TailCallExternalReference(ExternalReference(fid, isolate()),
2684 num_arguments,
2685 result_size);
Steve Block6ded16b2010-05-10 14:33:55 +01002686}
2687
2688
2689void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002690#if defined(__thumb__)
2691 // Thumb mode builtin.
2692 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2693#endif
2694 mov(r1, Operand(builtin));
2695 CEntryStub stub(1);
2696 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2697}
2698
2699
Steve Blocka7e24c12009-10-30 11:49:00 +00002700void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +00002701 InvokeFlag flag,
2702 const CallWrapper& call_wrapper) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002703 // You can't call a builtin without a valid frame.
2704 ASSERT(flag == JUMP_FUNCTION || has_frame());
2705
Andrei Popescu402d9372010-02-26 13:31:12 +00002706 GetBuiltinEntry(r2, id);
Ben Murdoch257744e2011-11-30 15:57:28 +00002707 if (flag == CALL_FUNCTION) {
2708 call_wrapper.BeforeCall(CallSize(r2));
2709 SetCallKind(r5, CALL_AS_METHOD);
Andrei Popescu402d9372010-02-26 13:31:12 +00002710 Call(r2);
Ben Murdoch257744e2011-11-30 15:57:28 +00002711 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00002712 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002713 ASSERT(flag == JUMP_FUNCTION);
2714 SetCallKind(r5, CALL_AS_METHOD);
Andrei Popescu402d9372010-02-26 13:31:12 +00002715 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002716 }
2717}
2718
2719
Steve Block791712a2010-08-27 10:21:07 +01002720void MacroAssembler::GetBuiltinFunction(Register target,
2721 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01002722 // Load the builtins object into target register.
2723 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2724 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00002725 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01002726 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01002727 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2728}
2729
2730
2731void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2732 ASSERT(!target.is(r1));
2733 GetBuiltinFunction(r1, id);
2734 // Load the code entry point from the builtins object.
2735 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002736}
2737
2738
2739void MacroAssembler::SetCounter(StatsCounter* counter, int value,
2740 Register scratch1, Register scratch2) {
2741 if (FLAG_native_code_counters && counter->Enabled()) {
2742 mov(scratch1, Operand(value));
2743 mov(scratch2, Operand(ExternalReference(counter)));
2744 str(scratch1, MemOperand(scratch2));
2745 }
2746}
2747
2748
2749void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2750 Register scratch1, Register scratch2) {
2751 ASSERT(value > 0);
2752 if (FLAG_native_code_counters && counter->Enabled()) {
2753 mov(scratch2, Operand(ExternalReference(counter)));
2754 ldr(scratch1, MemOperand(scratch2));
2755 add(scratch1, scratch1, Operand(value));
2756 str(scratch1, MemOperand(scratch2));
2757 }
2758}
2759
2760
2761void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2762 Register scratch1, Register scratch2) {
2763 ASSERT(value > 0);
2764 if (FLAG_native_code_counters && counter->Enabled()) {
2765 mov(scratch2, Operand(ExternalReference(counter)));
2766 ldr(scratch1, MemOperand(scratch2));
2767 sub(scratch1, scratch1, Operand(value));
2768 str(scratch1, MemOperand(scratch2));
2769 }
2770}
2771
2772
Steve Block1e0659c2011-05-24 12:43:12 +01002773void MacroAssembler::Assert(Condition cond, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01002774 if (emit_debug_code())
Steve Block1e0659c2011-05-24 12:43:12 +01002775 Check(cond, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00002776}
2777
2778
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002779void MacroAssembler::AssertRegisterIsRoot(Register reg,
2780 Heap::RootListIndex index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002781 if (emit_debug_code()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002782 LoadRoot(ip, index);
2783 cmp(reg, ip);
2784 Check(eq, "Register did not match expected root");
2785 }
2786}
2787
2788
Iain Merrick75681382010-08-19 15:07:18 +01002789void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002790 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01002791 ASSERT(!elements.is(ip));
2792 Label ok;
2793 push(elements);
2794 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
2795 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2796 cmp(elements, ip);
2797 b(eq, &ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002798 LoadRoot(ip, Heap::kFixedDoubleArrayMapRootIndex);
2799 cmp(elements, ip);
2800 b(eq, &ok);
Iain Merrick75681382010-08-19 15:07:18 +01002801 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2802 cmp(elements, ip);
2803 b(eq, &ok);
2804 Abort("JSObject with fast elements map has slow elements");
2805 bind(&ok);
2806 pop(elements);
2807 }
2808}
2809
2810
Steve Block1e0659c2011-05-24 12:43:12 +01002811void MacroAssembler::Check(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002812 Label L;
Steve Block1e0659c2011-05-24 12:43:12 +01002813 b(cond, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00002814 Abort(msg);
2815 // will not return here
2816 bind(&L);
2817}
2818
2819
2820void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01002821 Label abort_start;
2822 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00002823 // We want to pass the msg string like a smi to avoid GC
2824 // problems, however msg is not guaranteed to be aligned
2825 // properly. Instead, we pass an aligned pointer that is
2826 // a proper v8 smi, but also pass the alignment difference
2827 // from the real pointer as a smi.
2828 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2829 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2830 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2831#ifdef DEBUG
2832 if (msg != NULL) {
2833 RecordComment("Abort message: ");
2834 RecordComment(msg);
2835 }
2836#endif
Steve Blockd0582a62009-12-15 09:54:21 +00002837
Steve Blocka7e24c12009-10-30 11:49:00 +00002838 mov(r0, Operand(p0));
2839 push(r0);
2840 mov(r0, Operand(Smi::FromInt(p1 - p0)));
2841 push(r0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002842 // Disable stub call restrictions to always allow calls to abort.
2843 if (!has_frame_) {
2844 // We don't actually want to generate a pile of code for this, so just
2845 // claim there is a stack frame, without generating one.
2846 FrameScope scope(this, StackFrame::NONE);
2847 CallRuntime(Runtime::kAbort, 2);
2848 } else {
2849 CallRuntime(Runtime::kAbort, 2);
2850 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002851 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01002852 if (is_const_pool_blocked()) {
2853 // If the calling code cares about the exact number of
2854 // instructions generated, we insert padding here to keep the size
2855 // of the Abort macro constant.
2856 static const int kExpectedAbortInstructions = 10;
2857 int abort_instructions = InstructionsGeneratedSince(&abort_start);
2858 ASSERT(abort_instructions <= kExpectedAbortInstructions);
2859 while (abort_instructions++ < kExpectedAbortInstructions) {
2860 nop();
2861 }
2862 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002863}
2864
2865
Steve Blockd0582a62009-12-15 09:54:21 +00002866void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2867 if (context_chain_length > 0) {
2868 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002869 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002870 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002871 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002872 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002873 } else {
2874 // Slot is in the current function context. Move it into the
2875 // destination register in case we store into it (the write barrier
2876 // cannot be allowed to destroy the context in esi).
2877 mov(dst, cp);
2878 }
Steve Blockd0582a62009-12-15 09:54:21 +00002879}
2880
2881
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002882void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2883 // Load the global or builtins object from the current context.
2884 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2885 // Load the global context from the global or builtins object.
2886 ldr(function, FieldMemOperand(function,
2887 GlobalObject::kGlobalContextOffset));
2888 // Load the function from the global context.
2889 ldr(function, MemOperand(function, Context::SlotOffset(index)));
2890}
2891
2892
2893void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2894 Register map,
2895 Register scratch) {
2896 // Load the initial map. The global functions all have initial maps.
2897 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002898 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002899 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00002900 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002901 b(&ok);
2902 bind(&fail);
2903 Abort("Global functions must have initial map");
2904 bind(&ok);
2905 }
2906}
2907
2908
Steve Block1e0659c2011-05-24 12:43:12 +01002909void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
2910 Register reg,
2911 Register scratch,
2912 Label* not_power_of_two_or_zero) {
2913 sub(scratch, reg, Operand(1), SetCC);
2914 b(mi, not_power_of_two_or_zero);
2915 tst(scratch, reg);
2916 b(ne, not_power_of_two_or_zero);
2917}
2918
2919
Steve Block44f0eee2011-05-26 01:26:41 +01002920void MacroAssembler::JumpIfNotPowerOfTwoOrZeroAndNeg(
2921 Register reg,
2922 Register scratch,
2923 Label* zero_and_neg,
2924 Label* not_power_of_two) {
2925 sub(scratch, reg, Operand(1), SetCC);
2926 b(mi, zero_and_neg);
2927 tst(scratch, reg);
2928 b(ne, not_power_of_two);
2929}
2930
2931
Andrei Popescu31002712010-02-23 13:46:05 +00002932void MacroAssembler::JumpIfNotBothSmi(Register reg1,
2933 Register reg2,
2934 Label* on_not_both_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002935 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002936 tst(reg1, Operand(kSmiTagMask));
2937 tst(reg2, Operand(kSmiTagMask), eq);
2938 b(ne, on_not_both_smi);
2939}
2940
2941
2942void MacroAssembler::JumpIfEitherSmi(Register reg1,
2943 Register reg2,
2944 Label* on_either_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002945 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002946 tst(reg1, Operand(kSmiTagMask));
2947 tst(reg2, Operand(kSmiTagMask), ne);
2948 b(eq, on_either_smi);
2949}
2950
2951
Iain Merrick75681382010-08-19 15:07:18 +01002952void MacroAssembler::AbortIfSmi(Register object) {
Steve Block1e0659c2011-05-24 12:43:12 +01002953 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +01002954 tst(object, Operand(kSmiTagMask));
2955 Assert(ne, "Operand is a smi");
2956}
2957
2958
Steve Block1e0659c2011-05-24 12:43:12 +01002959void MacroAssembler::AbortIfNotSmi(Register object) {
2960 STATIC_ASSERT(kSmiTag == 0);
2961 tst(object, Operand(kSmiTagMask));
2962 Assert(eq, "Operand is not smi");
2963}
2964
2965
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002966void MacroAssembler::AbortIfNotString(Register object) {
2967 STATIC_ASSERT(kSmiTag == 0);
2968 tst(object, Operand(kSmiTagMask));
2969 Assert(ne, "Operand is not a string");
2970 push(object);
2971 ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2972 CompareInstanceType(object, object, FIRST_NONSTRING_TYPE);
2973 pop(object);
2974 Assert(lo, "Operand is not a string");
2975}
2976
2977
2978
Steve Block1e0659c2011-05-24 12:43:12 +01002979void MacroAssembler::AbortIfNotRootValue(Register src,
2980 Heap::RootListIndex root_value_index,
2981 const char* message) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002982 CompareRoot(src, root_value_index);
Steve Block1e0659c2011-05-24 12:43:12 +01002983 Assert(eq, message);
2984}
2985
2986
2987void MacroAssembler::JumpIfNotHeapNumber(Register object,
2988 Register heap_number_map,
2989 Register scratch,
2990 Label* on_not_heap_number) {
2991 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2992 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2993 cmp(scratch, heap_number_map);
2994 b(ne, on_not_heap_number);
2995}
2996
2997
Leon Clarked91b9f72010-01-27 17:25:45 +00002998void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
2999 Register first,
3000 Register second,
3001 Register scratch1,
3002 Register scratch2,
3003 Label* failure) {
3004 // Test that both first and second are sequential ASCII strings.
3005 // Assume that they are non-smis.
3006 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
3007 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
3008 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3009 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01003010
3011 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
3012 scratch2,
3013 scratch1,
3014 scratch2,
3015 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00003016}
3017
3018void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
3019 Register second,
3020 Register scratch1,
3021 Register scratch2,
3022 Label* failure) {
3023 // Check that neither is a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01003024 STATIC_ASSERT(kSmiTag == 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00003025 and_(scratch1, first, Operand(second));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003026 JumpIfSmi(scratch1, failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00003027 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
3028 second,
3029 scratch1,
3030 scratch2,
3031 failure);
3032}
3033
Steve Blockd0582a62009-12-15 09:54:21 +00003034
Steve Block6ded16b2010-05-10 14:33:55 +01003035// Allocates a heap number or jumps to the need_gc label if the young space
3036// is full and a scavenge is needed.
3037void MacroAssembler::AllocateHeapNumber(Register result,
3038 Register scratch1,
3039 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003040 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01003041 Label* gc_required) {
3042 // Allocate an object in the heap for the heap number and tag it as a heap
3043 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01003044 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01003045 result,
3046 scratch1,
3047 scratch2,
3048 gc_required,
3049 TAG_OBJECT);
3050
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003051 // Store heap number map in the allocated object.
3052 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
3053 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01003054}
3055
3056
Steve Block8defd9f2010-07-08 12:39:36 +01003057void MacroAssembler::AllocateHeapNumberWithValue(Register result,
3058 DwVfpRegister value,
3059 Register scratch1,
3060 Register scratch2,
3061 Register heap_number_map,
3062 Label* gc_required) {
3063 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
3064 sub(scratch1, result, Operand(kHeapObjectTag));
3065 vstr(value, scratch1, HeapNumber::kValueOffset);
3066}
3067
3068
Ben Murdochbb769b22010-08-11 14:56:33 +01003069// Copies a fixed number of fields of heap objects from src to dst.
3070void MacroAssembler::CopyFields(Register dst,
3071 Register src,
3072 RegList temps,
3073 int field_count) {
3074 // At least one bit set in the first 15 registers.
3075 ASSERT((temps & ((1 << 15) - 1)) != 0);
3076 ASSERT((temps & dst.bit()) == 0);
3077 ASSERT((temps & src.bit()) == 0);
3078 // Primitive implementation using only one temporary register.
3079
3080 Register tmp = no_reg;
3081 // Find a temp register in temps list.
3082 for (int i = 0; i < 15; i++) {
3083 if ((temps & (1 << i)) != 0) {
3084 tmp.set_code(i);
3085 break;
3086 }
3087 }
3088 ASSERT(!tmp.is(no_reg));
3089
3090 for (int i = 0; i < field_count; i++) {
3091 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
3092 str(tmp, FieldMemOperand(dst, i * kPointerSize));
3093 }
3094}
3095
3096
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003097void MacroAssembler::CopyBytes(Register src,
3098 Register dst,
3099 Register length,
3100 Register scratch) {
3101 Label align_loop, align_loop_1, word_loop, byte_loop, byte_loop_1, done;
3102
3103 // Align src before copying in word size chunks.
3104 bind(&align_loop);
3105 cmp(length, Operand(0));
3106 b(eq, &done);
3107 bind(&align_loop_1);
3108 tst(src, Operand(kPointerSize - 1));
3109 b(eq, &word_loop);
3110 ldrb(scratch, MemOperand(src, 1, PostIndex));
3111 strb(scratch, MemOperand(dst, 1, PostIndex));
3112 sub(length, length, Operand(1), SetCC);
3113 b(ne, &byte_loop_1);
3114
3115 // Copy bytes in word size chunks.
3116 bind(&word_loop);
Steve Block44f0eee2011-05-26 01:26:41 +01003117 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003118 tst(src, Operand(kPointerSize - 1));
3119 Assert(eq, "Expecting alignment for CopyBytes");
3120 }
3121 cmp(length, Operand(kPointerSize));
3122 b(lt, &byte_loop);
3123 ldr(scratch, MemOperand(src, kPointerSize, PostIndex));
3124#if CAN_USE_UNALIGNED_ACCESSES
3125 str(scratch, MemOperand(dst, kPointerSize, PostIndex));
3126#else
3127 strb(scratch, MemOperand(dst, 1, PostIndex));
3128 mov(scratch, Operand(scratch, LSR, 8));
3129 strb(scratch, MemOperand(dst, 1, PostIndex));
3130 mov(scratch, Operand(scratch, LSR, 8));
3131 strb(scratch, MemOperand(dst, 1, PostIndex));
3132 mov(scratch, Operand(scratch, LSR, 8));
3133 strb(scratch, MemOperand(dst, 1, PostIndex));
3134#endif
3135 sub(length, length, Operand(kPointerSize));
3136 b(&word_loop);
3137
3138 // Copy the last bytes if any left.
3139 bind(&byte_loop);
3140 cmp(length, Operand(0));
3141 b(eq, &done);
3142 bind(&byte_loop_1);
3143 ldrb(scratch, MemOperand(src, 1, PostIndex));
3144 strb(scratch, MemOperand(dst, 1, PostIndex));
3145 sub(length, length, Operand(1), SetCC);
3146 b(ne, &byte_loop_1);
3147 bind(&done);
3148}
3149
3150
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003151void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
3152 Register end_offset,
3153 Register filler) {
3154 Label loop, entry;
3155 b(&entry);
3156 bind(&loop);
3157 str(filler, MemOperand(start_offset, kPointerSize, PostIndex));
3158 bind(&entry);
3159 cmp(start_offset, end_offset);
3160 b(lt, &loop);
3161}
3162
3163
Steve Block8defd9f2010-07-08 12:39:36 +01003164void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
3165 Register source, // Input.
3166 Register scratch) {
Steve Block1e0659c2011-05-24 12:43:12 +01003167 ASSERT(!zeros.is(source) || !source.is(scratch));
Steve Block8defd9f2010-07-08 12:39:36 +01003168 ASSERT(!zeros.is(scratch));
3169 ASSERT(!scratch.is(ip));
3170 ASSERT(!source.is(ip));
3171 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01003172#ifdef CAN_USE_ARMV5_INSTRUCTIONS
3173 clz(zeros, source); // This instruction is only supported after ARM5.
3174#else
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003175 // Order of the next two lines is important: zeros register
3176 // can be the same as source register.
Steve Block8defd9f2010-07-08 12:39:36 +01003177 Move(scratch, source);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003178 mov(zeros, Operand(0, RelocInfo::NONE));
Steve Block6ded16b2010-05-10 14:33:55 +01003179 // Top 16.
3180 tst(scratch, Operand(0xffff0000));
3181 add(zeros, zeros, Operand(16), LeaveCC, eq);
3182 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
3183 // Top 8.
3184 tst(scratch, Operand(0xff000000));
3185 add(zeros, zeros, Operand(8), LeaveCC, eq);
3186 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
3187 // Top 4.
3188 tst(scratch, Operand(0xf0000000));
3189 add(zeros, zeros, Operand(4), LeaveCC, eq);
3190 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
3191 // Top 2.
3192 tst(scratch, Operand(0xc0000000));
3193 add(zeros, zeros, Operand(2), LeaveCC, eq);
3194 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
3195 // Top bit.
3196 tst(scratch, Operand(0x80000000u));
3197 add(zeros, zeros, Operand(1), LeaveCC, eq);
3198#endif
3199}
3200
3201
3202void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
3203 Register first,
3204 Register second,
3205 Register scratch1,
3206 Register scratch2,
3207 Label* failure) {
3208 int kFlatAsciiStringMask =
3209 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3210 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3211 and_(scratch1, first, Operand(kFlatAsciiStringMask));
3212 and_(scratch2, second, Operand(kFlatAsciiStringMask));
3213 cmp(scratch1, Operand(kFlatAsciiStringTag));
3214 // Ignore second test if first test failed.
3215 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
3216 b(ne, failure);
3217}
3218
3219
3220void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
3221 Register scratch,
3222 Label* failure) {
3223 int kFlatAsciiStringMask =
3224 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3225 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3226 and_(scratch, type, Operand(kFlatAsciiStringMask));
3227 cmp(scratch, Operand(kFlatAsciiStringTag));
3228 b(ne, failure);
3229}
3230
Steve Block44f0eee2011-05-26 01:26:41 +01003231static const int kRegisterPassedArguments = 4;
Steve Block6ded16b2010-05-10 14:33:55 +01003232
Steve Block44f0eee2011-05-26 01:26:41 +01003233
Ben Murdoch257744e2011-11-30 15:57:28 +00003234int MacroAssembler::CalculateStackPassedWords(int num_reg_arguments,
3235 int num_double_arguments) {
3236 int stack_passed_words = 0;
3237 if (use_eabi_hardfloat()) {
3238 // In the hard floating point calling convention, we can use
3239 // all double registers to pass doubles.
3240 if (num_double_arguments > DoubleRegister::kNumRegisters) {
3241 stack_passed_words +=
3242 2 * (num_double_arguments - DoubleRegister::kNumRegisters);
3243 }
3244 } else {
3245 // In the soft floating point calling convention, every double
3246 // argument is passed using two registers.
3247 num_reg_arguments += 2 * num_double_arguments;
3248 }
Steve Block6ded16b2010-05-10 14:33:55 +01003249 // Up to four simple arguments are passed in registers r0..r3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003250 if (num_reg_arguments > kRegisterPassedArguments) {
3251 stack_passed_words += num_reg_arguments - kRegisterPassedArguments;
3252 }
3253 return stack_passed_words;
3254}
3255
3256
3257void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
3258 int num_double_arguments,
3259 Register scratch) {
3260 int frame_alignment = ActivationFrameAlignment();
3261 int stack_passed_arguments = CalculateStackPassedWords(
3262 num_reg_arguments, num_double_arguments);
Steve Block6ded16b2010-05-10 14:33:55 +01003263 if (frame_alignment > kPointerSize) {
3264 // Make stack end at alignment and make room for num_arguments - 4 words
3265 // and the original value of sp.
3266 mov(scratch, sp);
3267 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
3268 ASSERT(IsPowerOf2(frame_alignment));
3269 and_(sp, sp, Operand(-frame_alignment));
3270 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
3271 } else {
3272 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
3273 }
3274}
3275
3276
Ben Murdoch257744e2011-11-30 15:57:28 +00003277void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
3278 Register scratch) {
3279 PrepareCallCFunction(num_reg_arguments, 0, scratch);
3280}
3281
3282
3283void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg) {
3284 if (use_eabi_hardfloat()) {
3285 Move(d0, dreg);
3286 } else {
3287 vmov(r0, r1, dreg);
3288 }
3289}
3290
3291
3292void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg1,
3293 DoubleRegister dreg2) {
3294 if (use_eabi_hardfloat()) {
3295 if (dreg2.is(d0)) {
3296 ASSERT(!dreg1.is(d1));
3297 Move(d1, dreg2);
3298 Move(d0, dreg1);
3299 } else {
3300 Move(d0, dreg1);
3301 Move(d1, dreg2);
3302 }
3303 } else {
3304 vmov(r0, r1, dreg1);
3305 vmov(r2, r3, dreg2);
3306 }
3307}
3308
3309
3310void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg,
3311 Register reg) {
3312 if (use_eabi_hardfloat()) {
3313 Move(d0, dreg);
3314 Move(r0, reg);
3315 } else {
3316 Move(r2, reg);
3317 vmov(r0, r1, dreg);
3318 }
3319}
3320
3321
3322void MacroAssembler::CallCFunction(ExternalReference function,
3323 int num_reg_arguments,
3324 int num_double_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003325 mov(ip, Operand(function));
3326 CallCFunctionHelper(ip, num_reg_arguments, num_double_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +00003327}
3328
3329
3330void MacroAssembler::CallCFunction(Register function,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003331 int num_reg_arguments,
3332 int num_double_arguments) {
3333 CallCFunctionHelper(function, num_reg_arguments, num_double_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +00003334}
3335
3336
Steve Block6ded16b2010-05-10 14:33:55 +01003337void MacroAssembler::CallCFunction(ExternalReference function,
3338 int num_arguments) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003339 CallCFunction(function, num_arguments, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003340}
3341
Ben Murdoch257744e2011-11-30 15:57:28 +00003342
Steve Block44f0eee2011-05-26 01:26:41 +01003343void MacroAssembler::CallCFunction(Register function,
Steve Block44f0eee2011-05-26 01:26:41 +01003344 int num_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003345 CallCFunction(function, num_arguments, 0);
Steve Block6ded16b2010-05-10 14:33:55 +01003346}
3347
3348
Steve Block44f0eee2011-05-26 01:26:41 +01003349void MacroAssembler::CallCFunctionHelper(Register function,
Ben Murdoch257744e2011-11-30 15:57:28 +00003350 int num_reg_arguments,
3351 int num_double_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003352 ASSERT(has_frame());
Steve Block6ded16b2010-05-10 14:33:55 +01003353 // Make sure that the stack is aligned before calling a C function unless
3354 // running in the simulator. The simulator has its own alignment check which
3355 // provides more information.
3356#if defined(V8_HOST_ARCH_ARM)
Steve Block44f0eee2011-05-26 01:26:41 +01003357 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01003358 int frame_alignment = OS::ActivationFrameAlignment();
3359 int frame_alignment_mask = frame_alignment - 1;
3360 if (frame_alignment > kPointerSize) {
3361 ASSERT(IsPowerOf2(frame_alignment));
3362 Label alignment_as_expected;
3363 tst(sp, Operand(frame_alignment_mask));
3364 b(eq, &alignment_as_expected);
3365 // Don't use Check here, as it will call Runtime_Abort possibly
3366 // re-entering here.
3367 stop("Unexpected alignment");
3368 bind(&alignment_as_expected);
3369 }
3370 }
3371#endif
3372
3373 // Just call directly. The function called cannot cause a GC, or
3374 // allow preemption, so the return address in the link register
3375 // stays correct.
3376 Call(function);
Ben Murdoch257744e2011-11-30 15:57:28 +00003377 int stack_passed_arguments = CalculateStackPassedWords(
3378 num_reg_arguments, num_double_arguments);
3379 if (ActivationFrameAlignment() > kPointerSize) {
Steve Block6ded16b2010-05-10 14:33:55 +01003380 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
3381 } else {
3382 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
3383 }
3384}
3385
3386
Steve Block1e0659c2011-05-24 12:43:12 +01003387void MacroAssembler::GetRelocatedValueLocation(Register ldr_location,
3388 Register result) {
3389 const uint32_t kLdrOffsetMask = (1 << 12) - 1;
3390 const int32_t kPCRegOffset = 2 * kPointerSize;
3391 ldr(result, MemOperand(ldr_location));
Steve Block44f0eee2011-05-26 01:26:41 +01003392 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003393 // Check that the instruction is a ldr reg, [pc + offset] .
3394 and_(result, result, Operand(kLdrPCPattern));
3395 cmp(result, Operand(kLdrPCPattern));
3396 Check(eq, "The instruction to patch should be a load from pc.");
3397 // Result was clobbered. Restore it.
3398 ldr(result, MemOperand(ldr_location));
3399 }
3400 // Get the address of the constant.
3401 and_(result, result, Operand(kLdrOffsetMask));
3402 add(result, ldr_location, Operand(result));
3403 add(result, result, Operand(kPCRegOffset));
3404}
3405
3406
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003407void MacroAssembler::CheckPageFlag(
3408 Register object,
3409 Register scratch,
3410 int mask,
3411 Condition cc,
3412 Label* condition_met) {
3413 and_(scratch, object, Operand(~Page::kPageAlignmentMask));
3414 ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
3415 tst(scratch, Operand(mask));
3416 b(cc, condition_met);
3417}
3418
3419
3420void MacroAssembler::JumpIfBlack(Register object,
3421 Register scratch0,
3422 Register scratch1,
3423 Label* on_black) {
3424 HasColor(object, scratch0, scratch1, on_black, 1, 0); // kBlackBitPattern.
3425 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
3426}
3427
3428
3429void MacroAssembler::HasColor(Register object,
3430 Register bitmap_scratch,
3431 Register mask_scratch,
3432 Label* has_color,
3433 int first_bit,
3434 int second_bit) {
3435 ASSERT(!AreAliased(object, bitmap_scratch, mask_scratch, no_reg));
3436
3437 GetMarkBits(object, bitmap_scratch, mask_scratch);
3438
3439 Label other_color, word_boundary;
3440 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3441 tst(ip, Operand(mask_scratch));
3442 b(first_bit == 1 ? eq : ne, &other_color);
3443 // Shift left 1 by adding.
3444 add(mask_scratch, mask_scratch, Operand(mask_scratch), SetCC);
3445 b(eq, &word_boundary);
3446 tst(ip, Operand(mask_scratch));
3447 b(second_bit == 1 ? ne : eq, has_color);
3448 jmp(&other_color);
3449
3450 bind(&word_boundary);
3451 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize));
3452 tst(ip, Operand(1));
3453 b(second_bit == 1 ? ne : eq, has_color);
3454 bind(&other_color);
3455}
3456
3457
3458// Detect some, but not all, common pointer-free objects. This is used by the
3459// incremental write barrier which doesn't care about oddballs (they are always
3460// marked black immediately so this code is not hit).
3461void MacroAssembler::JumpIfDataObject(Register value,
3462 Register scratch,
3463 Label* not_data_object) {
3464 Label is_data_object;
3465 ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
3466 CompareRoot(scratch, Heap::kHeapNumberMapRootIndex);
3467 b(eq, &is_data_object);
3468 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3469 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
3470 // If it's a string and it's not a cons string then it's an object containing
3471 // no GC pointers.
3472 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
3473 tst(scratch, Operand(kIsIndirectStringMask | kIsNotStringMask));
3474 b(ne, not_data_object);
3475 bind(&is_data_object);
3476}
3477
3478
3479void MacroAssembler::GetMarkBits(Register addr_reg,
3480 Register bitmap_reg,
3481 Register mask_reg) {
3482 ASSERT(!AreAliased(addr_reg, bitmap_reg, mask_reg, no_reg));
3483 and_(bitmap_reg, addr_reg, Operand(~Page::kPageAlignmentMask));
3484 Ubfx(mask_reg, addr_reg, kPointerSizeLog2, Bitmap::kBitsPerCellLog2);
3485 const int kLowBits = kPointerSizeLog2 + Bitmap::kBitsPerCellLog2;
3486 Ubfx(ip, addr_reg, kLowBits, kPageSizeBits - kLowBits);
3487 add(bitmap_reg, bitmap_reg, Operand(ip, LSL, kPointerSizeLog2));
3488 mov(ip, Operand(1));
3489 mov(mask_reg, Operand(ip, LSL, mask_reg));
3490}
3491
3492
3493void MacroAssembler::EnsureNotWhite(
3494 Register value,
3495 Register bitmap_scratch,
3496 Register mask_scratch,
3497 Register load_scratch,
3498 Label* value_is_white_and_not_data) {
3499 ASSERT(!AreAliased(value, bitmap_scratch, mask_scratch, ip));
3500 GetMarkBits(value, bitmap_scratch, mask_scratch);
3501
3502 // If the value is black or grey we don't need to do anything.
3503 ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0);
3504 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
3505 ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0);
3506 ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
3507
3508 Label done;
3509
3510 // Since both black and grey have a 1 in the first position and white does
3511 // not have a 1 there we only need to check one bit.
3512 ldr(load_scratch, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3513 tst(mask_scratch, load_scratch);
3514 b(ne, &done);
3515
Ben Murdochc7cc0282012-03-05 14:35:55 +00003516 if (emit_debug_code()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003517 // Check for impossible bit pattern.
3518 Label ok;
3519 // LSL may overflow, making the check conservative.
3520 tst(load_scratch, Operand(mask_scratch, LSL, 1));
3521 b(eq, &ok);
3522 stop("Impossible marking bit pattern");
3523 bind(&ok);
3524 }
3525
3526 // Value is white. We check whether it is data that doesn't need scanning.
3527 // Currently only checks for HeapNumber and non-cons strings.
3528 Register map = load_scratch; // Holds map while checking type.
3529 Register length = load_scratch; // Holds length of object after testing type.
3530 Label is_data_object;
3531
3532 // Check for heap-number
3533 ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
3534 CompareRoot(map, Heap::kHeapNumberMapRootIndex);
3535 mov(length, Operand(HeapNumber::kSize), LeaveCC, eq);
3536 b(eq, &is_data_object);
3537
3538 // Check for strings.
3539 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3540 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
3541 // If it's a string and it's not a cons string then it's an object containing
3542 // no GC pointers.
3543 Register instance_type = load_scratch;
3544 ldrb(instance_type, FieldMemOperand(map, Map::kInstanceTypeOffset));
3545 tst(instance_type, Operand(kIsIndirectStringMask | kIsNotStringMask));
3546 b(ne, value_is_white_and_not_data);
3547 // It's a non-indirect (non-cons and non-slice) string.
3548 // If it's external, the length is just ExternalString::kSize.
3549 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
3550 // External strings are the only ones with the kExternalStringTag bit
3551 // set.
3552 ASSERT_EQ(0, kSeqStringTag & kExternalStringTag);
3553 ASSERT_EQ(0, kConsStringTag & kExternalStringTag);
3554 tst(instance_type, Operand(kExternalStringTag));
3555 mov(length, Operand(ExternalString::kSize), LeaveCC, ne);
3556 b(ne, &is_data_object);
3557
3558 // Sequential string, either ASCII or UC16.
3559 // For ASCII (char-size of 1) we shift the smi tag away to get the length.
3560 // For UC16 (char-size of 2) we just leave the smi tag in place, thereby
3561 // getting the length multiplied by 2.
3562 ASSERT(kAsciiStringTag == 4 && kStringEncodingMask == 4);
3563 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3564 ldr(ip, FieldMemOperand(value, String::kLengthOffset));
3565 tst(instance_type, Operand(kStringEncodingMask));
3566 mov(ip, Operand(ip, LSR, 1), LeaveCC, ne);
3567 add(length, ip, Operand(SeqString::kHeaderSize + kObjectAlignmentMask));
3568 and_(length, length, Operand(~kObjectAlignmentMask));
3569
3570 bind(&is_data_object);
3571 // Value is a data object, and it is white. Mark it black. Since we know
3572 // that the object is white we can make it black by flipping one bit.
3573 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3574 orr(ip, ip, Operand(mask_scratch));
3575 str(ip, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
3576
3577 and_(bitmap_scratch, bitmap_scratch, Operand(~Page::kPageAlignmentMask));
3578 ldr(ip, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3579 add(ip, ip, Operand(length));
3580 str(ip, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3581
3582 bind(&done);
3583}
3584
3585
Ben Murdoch257744e2011-11-30 15:57:28 +00003586void MacroAssembler::ClampUint8(Register output_reg, Register input_reg) {
3587 Usat(output_reg, 8, Operand(input_reg));
3588}
3589
3590
3591void MacroAssembler::ClampDoubleToUint8(Register result_reg,
3592 DoubleRegister input_reg,
3593 DoubleRegister temp_double_reg) {
3594 Label above_zero;
3595 Label done;
3596 Label in_bounds;
3597
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003598 Vmov(temp_double_reg, 0.0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003599 VFPCompareAndSetFlags(input_reg, temp_double_reg);
3600 b(gt, &above_zero);
3601
3602 // Double value is less than zero, NaN or Inf, return 0.
3603 mov(result_reg, Operand(0));
3604 b(al, &done);
3605
3606 // Double value is >= 255, return 255.
3607 bind(&above_zero);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003608 Vmov(temp_double_reg, 255.0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003609 VFPCompareAndSetFlags(input_reg, temp_double_reg);
3610 b(le, &in_bounds);
3611 mov(result_reg, Operand(255));
3612 b(al, &done);
3613
3614 // In 0-255 range, round and truncate.
3615 bind(&in_bounds);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003616 Vmov(temp_double_reg, 0.5);
Ben Murdoch257744e2011-11-30 15:57:28 +00003617 vadd(temp_double_reg, input_reg, temp_double_reg);
3618 vcvt_u32_f64(s0, temp_double_reg);
3619 vmov(result_reg, s0);
3620 bind(&done);
3621}
3622
3623
3624void MacroAssembler::LoadInstanceDescriptors(Register map,
3625 Register descriptors) {
3626 ldr(descriptors,
3627 FieldMemOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
3628 Label not_smi;
3629 JumpIfNotSmi(descriptors, &not_smi);
3630 mov(descriptors, Operand(FACTORY->empty_descriptor_array()));
3631 bind(&not_smi);
3632}
3633
3634
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003635bool AreAliased(Register r1, Register r2, Register r3, Register r4) {
3636 if (r1.is(r2)) return true;
3637 if (r1.is(r3)) return true;
3638 if (r1.is(r4)) return true;
3639 if (r2.is(r3)) return true;
3640 if (r2.is(r4)) return true;
3641 if (r3.is(r4)) return true;
3642 return false;
3643}
3644
3645
Steve Blocka7e24c12009-10-30 11:49:00 +00003646CodePatcher::CodePatcher(byte* address, int instructions)
3647 : address_(address),
3648 instructions_(instructions),
3649 size_(instructions * Assembler::kInstrSize),
Ben Murdoch8b112d22011-06-08 16:22:53 +01003650 masm_(Isolate::Current(), address, size_ + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003651 // Create a new macro assembler pointing to the address of the code to patch.
3652 // The size is adjusted with kGap on order for the assembler to generate size
3653 // bytes of instructions without failing with buffer size constraints.
3654 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
3655}
3656
3657
3658CodePatcher::~CodePatcher() {
3659 // Indicate that code has changed.
3660 CPU::FlushICache(address_, size_);
3661
3662 // Check that the code was patched as expected.
3663 ASSERT(masm_.pc_ == address_ + size_);
3664 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
3665}
3666
3667
Steve Block1e0659c2011-05-24 12:43:12 +01003668void CodePatcher::Emit(Instr instr) {
3669 masm()->emit(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003670}
3671
3672
3673void CodePatcher::Emit(Address addr) {
3674 masm()->emit(reinterpret_cast<Instr>(addr));
3675}
Steve Block1e0659c2011-05-24 12:43:12 +01003676
3677
3678void CodePatcher::EmitCondition(Condition cond) {
3679 Instr instr = Assembler::instr_at(masm_.pc_);
3680 instr = (instr & ~kCondMask) | cond;
3681 masm_.emit(instr);
3682}
Steve Blocka7e24c12009-10-30 11:49:00 +00003683
3684
3685} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01003686
3687#endif // V8_TARGET_ARCH_ARM