blob: 7a1f8024508fdb90fc173dc2f971805f71f53a5a [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Iain Merrick9ac36c92010-09-13 15:29:50 +010028#include <limits.h> // For LONG_MIN, LONG_MAX.
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#include "v8.h"
31
Leon Clarkef7060e22010-06-03 12:02:55 +010032#if defined(V8_TARGET_ARCH_ARM)
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "bootstrapper.h"
Ben Murdoch8b112d22011-06-08 16:22:53 +010035#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "debug.h"
37#include "runtime.h"
38
39namespace v8 {
40namespace internal {
41
Ben Murdoch8b112d22011-06-08 16:22:53 +010042MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
43 : Assembler(arg_isolate, buffer, size),
Steve Blocka7e24c12009-10-30 11:49:00 +000044 generating_stub_(false),
Ben Murdoch85b71792012-04-11 18:30:58 +010045 allow_stub_calls_(true) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010046 if (isolate() != NULL) {
47 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
48 isolate());
49 }
Steve Blocka7e24c12009-10-30 11:49:00 +000050}
51
52
53// We always generate arm code, never thumb code, even if V8 is compiled to
54// thumb, so we require inter-working support
55#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
56#error "flag -mthumb-interwork missing"
57#endif
58
59
60// We do not support thumb inter-working with an arm architecture not supporting
61// the blx instruction (below v5t). If you know what CPU you are compiling for
62// you can use -march=armv7 or similar.
63#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
64# error "For thumb inter-working we require an architecture which supports blx"
65#endif
66
67
Steve Blocka7e24c12009-10-30 11:49:00 +000068// Using bx does not yield better code, so use it only when required
69#if defined(USE_THUMB_INTERWORK)
70#define USE_BX 1
71#endif
72
73
74void MacroAssembler::Jump(Register target, Condition cond) {
75#if USE_BX
76 bx(target, cond);
77#else
78 mov(pc, Operand(target), LeaveCC, cond);
79#endif
80}
81
82
83void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
84 Condition cond) {
85#if USE_BX
Ben Murdoch257744e2011-11-30 15:57:28 +000086 mov(ip, Operand(target, rmode));
Steve Blocka7e24c12009-10-30 11:49:00 +000087 bx(ip, cond);
88#else
89 mov(pc, Operand(target, rmode), LeaveCC, cond);
90#endif
91}
92
93
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000094void MacroAssembler::Jump(Address target, RelocInfo::Mode rmode,
Steve Blocka7e24c12009-10-30 11:49:00 +000095 Condition cond) {
96 ASSERT(!RelocInfo::IsCodeTarget(rmode));
97 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
98}
99
100
101void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
102 Condition cond) {
103 ASSERT(RelocInfo::IsCodeTarget(rmode));
104 // 'code' is always generated ARM code, never THUMB code
105 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
106}
107
108
Steve Block44f0eee2011-05-26 01:26:41 +0100109int MacroAssembler::CallSize(Register target, Condition cond) {
110#if USE_BLX
111 return kInstrSize;
112#else
113 return 2 * kInstrSize;
114#endif
115}
116
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118void MacroAssembler::Call(Register target, Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 // Block constant pool for the call instruction sequence.
120 BlockConstPoolScope block_const_pool(this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000121 Label start;
122 bind(&start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123#if USE_BLX
124 blx(target, cond);
125#else
126 // set lr for return at current pc + 8
127 mov(lr, Operand(pc), LeaveCC, cond);
128 mov(pc, Operand(target), LeaveCC, cond);
129#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000130 ASSERT_EQ(CallSize(target, cond), SizeOfCodeGeneratedSince(&start));
Steve Blocka7e24c12009-10-30 11:49:00 +0000131}
132
133
Steve Block44f0eee2011-05-26 01:26:41 +0100134int MacroAssembler::CallSize(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000135 Address target, RelocInfo::Mode rmode, Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100136 int size = 2 * kInstrSize;
137 Instr mov_instr = cond | MOV | LeaveCC;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000138 intptr_t immediate = reinterpret_cast<intptr_t>(target);
139 if (!Operand(immediate, rmode).is_single_instruction(mov_instr)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100140 size += kInstrSize;
141 }
142 return size;
143}
144
145
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000146void MacroAssembler::Call(Address target,
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 RelocInfo::Mode rmode,
148 Condition cond) {
Steve Block44f0eee2011-05-26 01:26:41 +0100149 // Block constant pool for the call instruction sequence.
150 BlockConstPoolScope block_const_pool(this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000151 Label start;
152 bind(&start);
Steve Block6ded16b2010-05-10 14:33:55 +0100153#if USE_BLX
154 // On ARMv5 and after the recommended call sequence is:
155 // ldr ip, [pc, #...]
156 // blx ip
157
Steve Block44f0eee2011-05-26 01:26:41 +0100158 // Statement positions are expected to be recorded when the target
159 // address is loaded. The mov method will automatically record
160 // positions when pc is the target, since this is not the case here
161 // we have to do it explicitly.
162 positions_recorder()->WriteRecordedPositions();
Steve Block6ded16b2010-05-10 14:33:55 +0100163
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000164 mov(ip, Operand(reinterpret_cast<int32_t>(target), rmode));
Steve Block44f0eee2011-05-26 01:26:41 +0100165 blx(ip, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100166
167 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
168#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 // Set lr for return at current pc + 8.
170 mov(lr, Operand(pc), LeaveCC, cond);
171 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000172 mov(pc, Operand(reinterpret_cast<int32_t>(target), rmode), LeaveCC, cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000173 ASSERT(kCallTargetAddressOffset == kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100174#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000175 ASSERT_EQ(CallSize(target, rmode, cond), SizeOfCodeGeneratedSince(&start));
Steve Blocka7e24c12009-10-30 11:49:00 +0000176}
177
178
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000179int MacroAssembler::CallSize(Handle<Code> code,
180 RelocInfo::Mode rmode,
181 unsigned ast_id,
182 Condition cond) {
183 return CallSize(reinterpret_cast<Address>(code.location()), rmode, cond);
Ben Murdoch257744e2011-11-30 15:57:28 +0000184}
185
186
187void MacroAssembler::Call(Handle<Code> code,
188 RelocInfo::Mode rmode,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000189 unsigned ast_id,
Ben Murdoch257744e2011-11-30 15:57:28 +0000190 Condition cond) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000191 Label start;
192 bind(&start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 ASSERT(RelocInfo::IsCodeTarget(rmode));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000194 if (rmode == RelocInfo::CODE_TARGET && ast_id != kNoASTId) {
195 SetRecordedAstId(ast_id);
196 rmode = RelocInfo::CODE_TARGET_WITH_ID;
197 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 // 'code' is always generated ARM code, never THUMB code
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000199 Call(reinterpret_cast<Address>(code.location()), rmode, cond);
200 ASSERT_EQ(CallSize(code, rmode, ast_id, cond),
201 SizeOfCodeGeneratedSince(&start));
Steve Blocka7e24c12009-10-30 11:49:00 +0000202}
203
204
205void MacroAssembler::Ret(Condition cond) {
206#if USE_BX
207 bx(lr, cond);
208#else
209 mov(pc, Operand(lr), LeaveCC, cond);
210#endif
211}
212
213
Leon Clarkee46be812010-01-19 14:06:41 +0000214void MacroAssembler::Drop(int count, Condition cond) {
215 if (count > 0) {
216 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
217 }
218}
219
220
Ben Murdochb0fe1622011-05-05 13:52:32 +0100221void MacroAssembler::Ret(int drop, Condition cond) {
222 Drop(drop, cond);
223 Ret(cond);
224}
225
226
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100227void MacroAssembler::Swap(Register reg1,
228 Register reg2,
229 Register scratch,
230 Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100231 if (scratch.is(no_reg)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100232 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
233 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
234 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100235 } else {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100236 mov(scratch, reg1, LeaveCC, cond);
237 mov(reg1, reg2, LeaveCC, cond);
238 mov(reg2, scratch, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100239 }
240}
241
242
Leon Clarkee46be812010-01-19 14:06:41 +0000243void MacroAssembler::Call(Label* target) {
244 bl(target);
245}
246
247
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000248void MacroAssembler::Push(Handle<Object> handle) {
249 mov(ip, Operand(handle));
250 push(ip);
251}
252
253
Leon Clarkee46be812010-01-19 14:06:41 +0000254void MacroAssembler::Move(Register dst, Handle<Object> value) {
255 mov(dst, Operand(value));
256}
Steve Blockd0582a62009-12-15 09:54:21 +0000257
258
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000259void MacroAssembler::Move(Register dst, Register src, Condition cond) {
Steve Block6ded16b2010-05-10 14:33:55 +0100260 if (!dst.is(src)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000261 mov(dst, src, LeaveCC, cond);
Steve Block6ded16b2010-05-10 14:33:55 +0100262 }
263}
264
265
Ben Murdoch257744e2011-11-30 15:57:28 +0000266void MacroAssembler::Move(DoubleRegister dst, DoubleRegister src) {
267 ASSERT(CpuFeatures::IsSupported(VFP3));
268 CpuFeatures::Scope scope(VFP3);
269 if (!dst.is(src)) {
270 vmov(dst, src);
271 }
272}
273
274
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100275void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
276 Condition cond) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800277 if (!src2.is_reg() &&
278 !src2.must_use_constant_pool() &&
279 src2.immediate() == 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +0100280 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800281
282 } else if (!src2.is_single_instruction() &&
283 !src2.must_use_constant_pool() &&
Ben Murdoch8b112d22011-06-08 16:22:53 +0100284 CpuFeatures::IsSupported(ARMv7) &&
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800285 IsPowerOf2(src2.immediate() + 1)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000286 ubfx(dst, src1, 0,
287 WhichPowerOf2(static_cast<uint32_t>(src2.immediate()) + 1), cond);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800288
289 } else {
290 and_(dst, src1, src2, LeaveCC, cond);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100291 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100292}
293
294
295void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
296 Condition cond) {
297 ASSERT(lsb < 32);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100298 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100299 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
300 and_(dst, src1, Operand(mask), LeaveCC, cond);
301 if (lsb != 0) {
302 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
303 }
304 } else {
305 ubfx(dst, src1, lsb, width, cond);
306 }
307}
308
309
310void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
311 Condition cond) {
312 ASSERT(lsb < 32);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100313 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100314 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
315 and_(dst, src1, Operand(mask), LeaveCC, cond);
316 int shift_up = 32 - lsb - width;
317 int shift_down = lsb + shift_up;
318 if (shift_up != 0) {
319 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
320 }
321 if (shift_down != 0) {
322 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
323 }
324 } else {
325 sbfx(dst, src1, lsb, width, cond);
326 }
327}
328
329
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100330void MacroAssembler::Bfi(Register dst,
331 Register src,
332 Register scratch,
333 int lsb,
334 int width,
335 Condition cond) {
336 ASSERT(0 <= lsb && lsb < 32);
337 ASSERT(0 <= width && width < 32);
338 ASSERT(lsb + width < 32);
339 ASSERT(!scratch.is(dst));
340 if (width == 0) return;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100341 if (!CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100342 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
343 bic(dst, dst, Operand(mask));
344 and_(scratch, src, Operand((1 << width) - 1));
345 mov(scratch, Operand(scratch, LSL, lsb));
346 orr(dst, dst, scratch);
347 } else {
348 bfi(dst, src, lsb, width, cond);
349 }
350}
351
352
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100353void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
354 ASSERT(lsb < 32);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100355 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100356 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
357 bic(dst, dst, Operand(mask));
358 } else {
359 bfc(dst, lsb, width, cond);
360 }
361}
362
363
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100364void MacroAssembler::Usat(Register dst, int satpos, const Operand& src,
365 Condition cond) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100366 if (!CpuFeatures::IsSupported(ARMv7)) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100367 ASSERT(!dst.is(pc) && !src.rm().is(pc));
368 ASSERT((satpos >= 0) && (satpos <= 31));
369
370 // These asserts are required to ensure compatibility with the ARMv7
371 // implementation.
372 ASSERT((src.shift_op() == ASR) || (src.shift_op() == LSL));
373 ASSERT(src.rs().is(no_reg));
374
375 Label done;
376 int satval = (1 << satpos) - 1;
377
378 if (cond != al) {
379 b(NegateCondition(cond), &done); // Skip saturate if !condition.
380 }
381 if (!(src.is_reg() && dst.is(src.rm()))) {
382 mov(dst, src);
383 }
384 tst(dst, Operand(~satval));
385 b(eq, &done);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100386 mov(dst, Operand(0, RelocInfo::NONE), LeaveCC, mi); // 0 if negative.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100387 mov(dst, Operand(satval), LeaveCC, pl); // satval if positive.
388 bind(&done);
389 } else {
390 usat(dst, satpos, src, cond);
391 }
392}
393
394
Steve Blocka7e24c12009-10-30 11:49:00 +0000395void MacroAssembler::LoadRoot(Register destination,
396 Heap::RootListIndex index,
397 Condition cond) {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000398 ldr(destination, MemOperand(kRootRegister, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000399}
400
401
Kristian Monsen25f61362010-05-21 11:50:48 +0100402void MacroAssembler::StoreRoot(Register source,
403 Heap::RootListIndex index,
404 Condition cond) {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000405 str(source, MemOperand(kRootRegister, index << kPointerSizeLog2), cond);
406}
407
408
Ben Murdoch85b71792012-04-11 18:30:58 +0100409void MacroAssembler::RecordWriteHelper(Register object,
410 Register address,
411 Register scratch) {
412 if (emit_debug_code()) {
413 // Check that the object is not in new space.
414 Label not_in_new_space;
415 InNewSpace(object, scratch, ne, &not_in_new_space);
416 Abort("new-space object passed to RecordWriteHelper");
417 bind(&not_in_new_space);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000418 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100419
420 // Calculate page address.
421 Bfc(object, 0, kPageSizeBits);
422
423 // Calculate region number.
424 Ubfx(address, address, Page::kRegionSizeLog2,
425 kPageSizeBits - Page::kRegionSizeLog2);
426
427 // Mark region dirty.
428 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
429 mov(ip, Operand(1));
430 orr(scratch, scratch, Operand(ip, LSL, address));
431 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
Steve Block6ded16b2010-05-10 14:33:55 +0100432}
433
434
435void MacroAssembler::InNewSpace(Register object,
436 Register scratch,
Steve Block1e0659c2011-05-24 12:43:12 +0100437 Condition cond,
Steve Block6ded16b2010-05-10 14:33:55 +0100438 Label* branch) {
Steve Block1e0659c2011-05-24 12:43:12 +0100439 ASSERT(cond == eq || cond == ne);
Steve Block44f0eee2011-05-26 01:26:41 +0100440 and_(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
441 cmp(scratch, Operand(ExternalReference::new_space_start(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +0100442 b(cond, branch);
Steve Block6ded16b2010-05-10 14:33:55 +0100443}
444
445
Ben Murdoch85b71792012-04-11 18:30:58 +0100446// Will clobber 4 registers: object, offset, scratch, ip. The
447// register 'object' contains a heap object pointer. The heap object
448// tag is shifted away.
449void MacroAssembler::RecordWrite(Register object,
450 Operand offset,
451 Register scratch0,
452 Register scratch1) {
453 // The compiled code assumes that record write doesn't change the
454 // context register, so we check that none of the clobbered
455 // registers are cp.
456 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
457
Steve Block6ded16b2010-05-10 14:33:55 +0100458 Label done;
459
Ben Murdoch85b71792012-04-11 18:30:58 +0100460 // First, test that the object is not in the new space. We cannot set
461 // region marks for new space pages.
462 InNewSpace(object, scratch0, eq, &done);
Steve Block6ded16b2010-05-10 14:33:55 +0100463
Ben Murdoch85b71792012-04-11 18:30:58 +0100464 // Add offset into the object.
465 add(scratch0, object, offset);
Steve Block8defd9f2010-07-08 12:39:36 +0100466
Ben Murdoch85b71792012-04-11 18:30:58 +0100467 // Record the actual write.
468 RecordWriteHelper(object, scratch0, scratch1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000469
470 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000471
Ben Murdoch85b71792012-04-11 18:30:58 +0100472 // Clobber all input registers when running with the debug-code flag
Leon Clarke4515c472010-02-03 11:58:03 +0000473 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100474 if (emit_debug_code()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100475 mov(object, Operand(BitCast<int32_t>(kZapValue)));
476 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
477 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
Leon Clarke4515c472010-02-03 11:58:03 +0000478 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000479}
480
481
Steve Block8defd9f2010-07-08 12:39:36 +0100482// Will clobber 4 registers: object, address, scratch, ip. The
483// register 'object' contains a heap object pointer. The heap object
484// tag is shifted away.
485void MacroAssembler::RecordWrite(Register object,
486 Register address,
Ben Murdoch85b71792012-04-11 18:30:58 +0100487 Register scratch) {
Steve Block8defd9f2010-07-08 12:39:36 +0100488 // The compiled code assumes that record write doesn't change the
489 // context register, so we check that none of the clobbered
490 // registers are cp.
Ben Murdoch85b71792012-04-11 18:30:58 +0100491 ASSERT(!object.is(cp) && !address.is(cp) && !scratch.is(cp));
Steve Block8defd9f2010-07-08 12:39:36 +0100492
493 Label done;
494
Ben Murdoch85b71792012-04-11 18:30:58 +0100495 // First, test that the object is not in the new space. We cannot set
496 // region marks for new space pages.
497 InNewSpace(object, scratch, eq, &done);
Steve Block8defd9f2010-07-08 12:39:36 +0100498
499 // Record the actual write.
Ben Murdoch85b71792012-04-11 18:30:58 +0100500 RecordWriteHelper(object, address, scratch);
Steve Block8defd9f2010-07-08 12:39:36 +0100501
502 bind(&done);
503
Ben Murdoch85b71792012-04-11 18:30:58 +0100504 // Clobber all input registers when running with the debug-code flag
Steve Block8defd9f2010-07-08 12:39:36 +0100505 // turned on to provoke errors.
Steve Block44f0eee2011-05-26 01:26:41 +0100506 if (emit_debug_code()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100507 mov(object, Operand(BitCast<int32_t>(kZapValue)));
508 mov(address, Operand(BitCast<int32_t>(kZapValue)));
509 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
Steve Block8defd9f2010-07-08 12:39:36 +0100510 }
511}
512
513
Ben Murdochb0fe1622011-05-05 13:52:32 +0100514// Push and pop all registers that can hold pointers.
515void MacroAssembler::PushSafepointRegisters() {
516 // Safepoints expect a block of contiguous register values starting with r0:
517 ASSERT(((1 << kNumSafepointSavedRegisters) - 1) == kSafepointSavedRegisters);
518 // Safepoints expect a block of kNumSafepointRegisters values on the
519 // stack, so adjust the stack for unsaved registers.
520 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
521 ASSERT(num_unsaved >= 0);
522 sub(sp, sp, Operand(num_unsaved * kPointerSize));
523 stm(db_w, sp, kSafepointSavedRegisters);
524}
525
526
527void MacroAssembler::PopSafepointRegisters() {
528 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
529 ldm(ia_w, sp, kSafepointSavedRegisters);
530 add(sp, sp, Operand(num_unsaved * kPointerSize));
531}
532
533
Ben Murdochb8e0da22011-05-16 14:20:40 +0100534void MacroAssembler::PushSafepointRegistersAndDoubles() {
535 PushSafepointRegisters();
536 sub(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
537 kDoubleSize));
538 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
539 vstr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
540 }
541}
542
543
544void MacroAssembler::PopSafepointRegistersAndDoubles() {
545 for (int i = 0; i < DwVfpRegister::kNumAllocatableRegisters; i++) {
546 vldr(DwVfpRegister::FromAllocationIndex(i), sp, i * kDoubleSize);
547 }
548 add(sp, sp, Operand(DwVfpRegister::kNumAllocatableRegisters *
549 kDoubleSize));
550 PopSafepointRegisters();
551}
552
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100553void MacroAssembler::StoreToSafepointRegistersAndDoublesSlot(Register src,
554 Register dst) {
555 str(src, SafepointRegistersAndDoublesSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100556}
557
558
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100559void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
560 str(src, SafepointRegisterSlot(dst));
Steve Block1e0659c2011-05-24 12:43:12 +0100561}
562
563
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100564void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
565 ldr(dst, SafepointRegisterSlot(src));
Steve Block1e0659c2011-05-24 12:43:12 +0100566}
567
568
Ben Murdochb0fe1622011-05-05 13:52:32 +0100569int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
570 // The registers are pushed starting with the highest encoding,
571 // which means that lowest encodings are closest to the stack pointer.
572 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
573 return reg_code;
574}
575
576
Steve Block1e0659c2011-05-24 12:43:12 +0100577MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
578 return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
579}
580
581
582MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
583 // General purpose registers are pushed last on the stack.
584 int doubles_size = DwVfpRegister::kNumAllocatableRegisters * kDoubleSize;
585 int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
586 return MemOperand(sp, doubles_size + register_offset);
587}
588
589
Leon Clarkef7060e22010-06-03 12:02:55 +0100590void MacroAssembler::Ldrd(Register dst1, Register dst2,
591 const MemOperand& src, Condition cond) {
592 ASSERT(src.rm().is(no_reg));
593 ASSERT(!dst1.is(lr)); // r14.
594 ASSERT_EQ(0, dst1.code() % 2);
595 ASSERT_EQ(dst1.code() + 1, dst2.code());
596
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000597 // V8 does not use this addressing mode, so the fallback code
598 // below doesn't support it yet.
599 ASSERT((src.am() != PreIndex) && (src.am() != NegPreIndex));
600
Leon Clarkef7060e22010-06-03 12:02:55 +0100601 // Generate two ldr instructions if ldrd is not available.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100602 if (CpuFeatures::IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100603 CpuFeatures::Scope scope(ARMv7);
604 ldrd(dst1, dst2, src, cond);
605 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000606 if ((src.am() == Offset) || (src.am() == NegOffset)) {
607 MemOperand src2(src);
608 src2.set_offset(src2.offset() + 4);
609 if (dst1.is(src.rn())) {
610 ldr(dst2, src2, cond);
611 ldr(dst1, src, cond);
612 } else {
613 ldr(dst1, src, cond);
614 ldr(dst2, src2, cond);
615 }
616 } else { // PostIndex or NegPostIndex.
617 ASSERT((src.am() == PostIndex) || (src.am() == NegPostIndex));
618 if (dst1.is(src.rn())) {
619 ldr(dst2, MemOperand(src.rn(), 4, Offset), cond);
620 ldr(dst1, src, cond);
621 } else {
622 MemOperand src2(src);
623 src2.set_offset(src2.offset() - 4);
624 ldr(dst1, MemOperand(src.rn(), 4, PostIndex), cond);
625 ldr(dst2, src2, cond);
626 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100627 }
628 }
629}
630
631
632void MacroAssembler::Strd(Register src1, Register src2,
633 const MemOperand& dst, Condition cond) {
634 ASSERT(dst.rm().is(no_reg));
635 ASSERT(!src1.is(lr)); // r14.
636 ASSERT_EQ(0, src1.code() % 2);
637 ASSERT_EQ(src1.code() + 1, src2.code());
638
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000639 // V8 does not use this addressing mode, so the fallback code
640 // below doesn't support it yet.
641 ASSERT((dst.am() != PreIndex) && (dst.am() != NegPreIndex));
642
Leon Clarkef7060e22010-06-03 12:02:55 +0100643 // Generate two str instructions if strd is not available.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100644 if (CpuFeatures::IsSupported(ARMv7)) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100645 CpuFeatures::Scope scope(ARMv7);
646 strd(src1, src2, dst, cond);
647 } else {
648 MemOperand dst2(dst);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000649 if ((dst.am() == Offset) || (dst.am() == NegOffset)) {
650 dst2.set_offset(dst2.offset() + 4);
651 str(src1, dst, cond);
652 str(src2, dst2, cond);
653 } else { // PostIndex or NegPostIndex.
654 ASSERT((dst.am() == PostIndex) || (dst.am() == NegPostIndex));
655 dst2.set_offset(dst2.offset() - 4);
656 str(src1, MemOperand(dst.rn(), 4, PostIndex), cond);
657 str(src2, dst2, cond);
658 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100659 }
660}
661
662
Ben Murdochb8e0da22011-05-16 14:20:40 +0100663void MacroAssembler::ClearFPSCRBits(const uint32_t bits_to_clear,
664 const Register scratch,
665 const Condition cond) {
666 vmrs(scratch, cond);
667 bic(scratch, scratch, Operand(bits_to_clear), LeaveCC, cond);
668 vmsr(scratch, cond);
669}
670
671
672void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
673 const DwVfpRegister src2,
674 const Condition cond) {
675 // Compare and move FPSCR flags to the normal condition flags.
676 VFPCompareAndLoadFlags(src1, src2, pc, cond);
677}
678
679void MacroAssembler::VFPCompareAndSetFlags(const DwVfpRegister src1,
680 const double src2,
681 const Condition cond) {
682 // Compare and move FPSCR flags to the normal condition flags.
683 VFPCompareAndLoadFlags(src1, src2, pc, cond);
684}
685
686
687void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
688 const DwVfpRegister src2,
689 const Register fpscr_flags,
690 const Condition cond) {
691 // Compare and load FPSCR.
692 vcmp(src1, src2, cond);
693 vmrs(fpscr_flags, cond);
694}
695
696void MacroAssembler::VFPCompareAndLoadFlags(const DwVfpRegister src1,
697 const double src2,
698 const Register fpscr_flags,
699 const Condition cond) {
700 // Compare and load FPSCR.
701 vcmp(src1, src2, cond);
702 vmrs(fpscr_flags, cond);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100703}
704
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000705void MacroAssembler::Vmov(const DwVfpRegister dst,
706 const double imm,
707 const Condition cond) {
708 ASSERT(CpuFeatures::IsEnabled(VFP3));
709 static const DoubleRepresentation minus_zero(-0.0);
710 static const DoubleRepresentation zero(0.0);
711 DoubleRepresentation value(imm);
712 // Handle special values first.
713 if (value.bits == zero.bits) {
714 vmov(dst, kDoubleRegZero, cond);
715 } else if (value.bits == minus_zero.bits) {
716 vneg(dst, kDoubleRegZero, cond);
717 } else {
718 vmov(dst, imm, cond);
719 }
720}
721
Ben Murdoch086aeea2011-05-13 15:57:08 +0100722
Steve Blocka7e24c12009-10-30 11:49:00 +0000723void MacroAssembler::EnterFrame(StackFrame::Type type) {
724 // r0-r3: preserved
725 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
726 mov(ip, Operand(Smi::FromInt(type)));
727 push(ip);
728 mov(ip, Operand(CodeObject()));
729 push(ip);
730 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
731}
732
733
734void MacroAssembler::LeaveFrame(StackFrame::Type type) {
735 // r0: preserved
736 // r1: preserved
737 // r2: preserved
738
739 // Drop the execution stack down to the frame pointer and restore
740 // the caller frame pointer and return address.
741 mov(sp, fp);
742 ldm(ia_w, sp, fp.bit() | lr.bit());
743}
744
745
Steve Block1e0659c2011-05-24 12:43:12 +0100746void MacroAssembler::EnterExitFrame(bool save_doubles, int stack_space) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100747 // Setup the frame structure on the stack.
Steve Block1e0659c2011-05-24 12:43:12 +0100748 ASSERT_EQ(2 * kPointerSize, ExitFrameConstants::kCallerSPDisplacement);
749 ASSERT_EQ(1 * kPointerSize, ExitFrameConstants::kCallerPCOffset);
750 ASSERT_EQ(0 * kPointerSize, ExitFrameConstants::kCallerFPOffset);
751 Push(lr, fp);
Ben Murdoch85b71792012-04-11 18:30:58 +0100752 mov(fp, Operand(sp)); // Setup new frame pointer.
Steve Block1e0659c2011-05-24 12:43:12 +0100753 // Reserve room for saved entry sp and code object.
754 sub(sp, sp, Operand(2 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100755 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100756 mov(ip, Operand(0));
757 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
758 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000759 mov(ip, Operand(CodeObject()));
Steve Block1e0659c2011-05-24 12:43:12 +0100760 str(ip, MemOperand(fp, ExitFrameConstants::kCodeOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000761
762 // Save the frame pointer and the context in top.
Ben Murdoch589d6972011-11-30 16:04:58 +0000763 mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000764 str(fp, MemOperand(ip));
Ben Murdoch589d6972011-11-30 16:04:58 +0000765 mov(ip, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 str(cp, MemOperand(ip));
767
Ben Murdochb0fe1622011-05-05 13:52:32 +0100768 // Optionally save all double registers.
769 if (save_doubles) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100770 DwVfpRegister first = d0;
771 DwVfpRegister last =
772 DwVfpRegister::from_code(DwVfpRegister::kNumRegisters - 1);
773 vstm(db_w, sp, first, last);
Steve Block1e0659c2011-05-24 12:43:12 +0100774 // Note that d0 will be accessible at
775 // fp - 2 * kPointerSize - DwVfpRegister::kNumRegisters * kDoubleSize,
776 // since the sp slot and code slot were pushed after the fp.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100777 }
Steve Block1e0659c2011-05-24 12:43:12 +0100778
779 // Reserve place for the return address and stack space and align the frame
780 // preparing for calling the runtime function.
781 const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
782 sub(sp, sp, Operand((stack_space + 1) * kPointerSize));
783 if (frame_alignment > 0) {
784 ASSERT(IsPowerOf2(frame_alignment));
785 and_(sp, sp, Operand(-frame_alignment));
786 }
787
788 // Set the exit frame sp value to point just before the return address
789 // location.
790 add(ip, sp, Operand(kPointerSize));
791 str(ip, MemOperand(fp, ExitFrameConstants::kSPOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000792}
793
794
Steve Block6ded16b2010-05-10 14:33:55 +0100795void MacroAssembler::InitializeNewString(Register string,
796 Register length,
797 Heap::RootListIndex map_index,
798 Register scratch1,
799 Register scratch2) {
800 mov(scratch1, Operand(length, LSL, kSmiTagSize));
801 LoadRoot(scratch2, map_index);
802 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
803 mov(scratch1, Operand(String::kEmptyHashField));
804 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
805 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
806}
807
808
809int MacroAssembler::ActivationFrameAlignment() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000810#if defined(V8_HOST_ARCH_ARM)
811 // Running on the real platform. Use the alignment as mandated by the local
812 // environment.
813 // Note: This will break if we ever start generating snapshots on one ARM
814 // platform for another ARM platform with a different alignment.
Steve Block6ded16b2010-05-10 14:33:55 +0100815 return OS::ActivationFrameAlignment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000816#else // defined(V8_HOST_ARCH_ARM)
817 // If we are using the simulator then we should always align to the expected
818 // alignment. As the simulator is used to generate snapshots we do not know
Steve Block6ded16b2010-05-10 14:33:55 +0100819 // if the target platform will need alignment, so this is controlled from a
820 // flag.
821 return FLAG_sim_stack_alignment;
Steve Blocka7e24c12009-10-30 11:49:00 +0000822#endif // defined(V8_HOST_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +0000823}
824
825
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100826void MacroAssembler::LeaveExitFrame(bool save_doubles,
827 Register argument_count) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100828 // Optionally restore all double registers.
829 if (save_doubles) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100830 // Calculate the stack location of the saved doubles and restore them.
831 const int offset = 2 * kPointerSize;
832 sub(r3, fp, Operand(offset + DwVfpRegister::kNumRegisters * kDoubleSize));
833 DwVfpRegister first = d0;
834 DwVfpRegister last =
835 DwVfpRegister::from_code(DwVfpRegister::kNumRegisters - 1);
836 vldm(ia, r3, first, last);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100837 }
838
Steve Blocka7e24c12009-10-30 11:49:00 +0000839 // Clear top frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100840 mov(r3, Operand(0, RelocInfo::NONE));
Ben Murdoch589d6972011-11-30 16:04:58 +0000841 mov(ip, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 str(r3, MemOperand(ip));
843
844 // Restore current context from top and clear it in debug mode.
Ben Murdoch589d6972011-11-30 16:04:58 +0000845 mov(ip, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 ldr(cp, MemOperand(ip));
847#ifdef DEBUG
848 str(r3, MemOperand(ip));
849#endif
850
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100851 // Tear down the exit frame, pop the arguments, and return.
Steve Block1e0659c2011-05-24 12:43:12 +0100852 mov(sp, Operand(fp));
853 ldm(ia_w, sp, fp.bit() | lr.bit());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100854 if (argument_count.is_valid()) {
855 add(sp, sp, Operand(argument_count, LSL, kPointerSizeLog2));
856 }
857}
858
859void MacroAssembler::GetCFunctionDoubleResult(const DoubleRegister dst) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000860 if (use_eabi_hardfloat()) {
861 Move(dst, d0);
862 } else {
863 vmov(dst, r0, r1);
864 }
865}
866
867
868void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
869 // This macro takes the dst register to make the code more readable
870 // at the call sites. However, the dst register has to be r5 to
871 // follow the calling convention which requires the call type to be
872 // in r5.
873 ASSERT(dst.is(r5));
874 if (call_kind == CALL_AS_FUNCTION) {
875 mov(dst, Operand(Smi::FromInt(1)));
876 } else {
877 mov(dst, Operand(Smi::FromInt(0)));
878 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000879}
880
881
882void MacroAssembler::InvokePrologue(const ParameterCount& expected,
883 const ParameterCount& actual,
884 Handle<Code> code_constant,
885 Register code_reg,
886 Label* done,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100887 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000888 const CallWrapper& call_wrapper,
889 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000890 bool definitely_matches = false;
891 Label regular_invoke;
892
893 // Check whether the expected and actual arguments count match. If not,
894 // setup registers according to contract with ArgumentsAdaptorTrampoline:
895 // r0: actual arguments count
896 // r1: function (passed through to callee)
897 // r2: expected arguments count
898 // r3: callee code entry
899
900 // The code below is made a lot easier because the calling code already sets
901 // up actual and expected registers according to the contract if values are
902 // passed in registers.
903 ASSERT(actual.is_immediate() || actual.reg().is(r0));
904 ASSERT(expected.is_immediate() || expected.reg().is(r2));
905 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
906
907 if (expected.is_immediate()) {
908 ASSERT(actual.is_immediate());
909 if (expected.immediate() == actual.immediate()) {
910 definitely_matches = true;
911 } else {
912 mov(r0, Operand(actual.immediate()));
913 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
914 if (expected.immediate() == sentinel) {
915 // Don't worry about adapting arguments for builtins that
916 // don't want that done. Skip adaption code by making it look
917 // like we have a match between expected and actual number of
918 // arguments.
919 definitely_matches = true;
920 } else {
921 mov(r2, Operand(expected.immediate()));
922 }
923 }
924 } else {
925 if (actual.is_immediate()) {
926 cmp(expected.reg(), Operand(actual.immediate()));
927 b(eq, &regular_invoke);
928 mov(r0, Operand(actual.immediate()));
929 } else {
930 cmp(expected.reg(), Operand(actual.reg()));
931 b(eq, &regular_invoke);
932 }
933 }
934
935 if (!definitely_matches) {
936 if (!code_constant.is_null()) {
937 mov(r3, Operand(code_constant));
938 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
939 }
940
941 Handle<Code> adaptor =
Steve Block44f0eee2011-05-26 01:26:41 +0100942 isolate()->builtins()->ArgumentsAdaptorTrampoline();
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 if (flag == CALL_FUNCTION) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000944 call_wrapper.BeforeCall(CallSize(adaptor));
Ben Murdoch257744e2011-11-30 15:57:28 +0000945 SetCallKind(r5, call_kind);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000946 Call(adaptor);
Ben Murdoch257744e2011-11-30 15:57:28 +0000947 call_wrapper.AfterCall();
Ben Murdoch85b71792012-04-11 18:30:58 +0100948 b(done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000950 SetCallKind(r5, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 Jump(adaptor, RelocInfo::CODE_TARGET);
952 }
953 bind(&regular_invoke);
954 }
955}
956
957
958void MacroAssembler::InvokeCode(Register code,
959 const ParameterCount& expected,
960 const ParameterCount& actual,
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 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000965
Ben Murdoch85b71792012-04-11 18:30:58 +0100966 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag,
967 call_wrapper, call_kind);
968 if (flag == CALL_FUNCTION) {
969 call_wrapper.BeforeCall(CallSize(code));
970 SetCallKind(r5, call_kind);
971 Call(code);
972 call_wrapper.AfterCall();
973 } else {
974 ASSERT(flag == JUMP_FUNCTION);
975 SetCallKind(r5, call_kind);
976 Jump(code);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000977 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100978
979 // Continue here if InvokePrologue does handle the invocation due to
980 // mismatched parameter counts.
981 bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000982}
983
984
985void MacroAssembler::InvokeCode(Handle<Code> code,
986 const ParameterCount& expected,
987 const ParameterCount& actual,
988 RelocInfo::Mode rmode,
Ben Murdoch257744e2011-11-30 15:57:28 +0000989 InvokeFlag flag,
990 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000991 Label done;
Steve Blocka7e24c12009-10-30 11:49:00 +0000992
Ben Murdoch85b71792012-04-11 18:30:58 +0100993 InvokePrologue(expected, actual, code, no_reg, &done, flag,
994 NullCallWrapper(), call_kind);
995 if (flag == CALL_FUNCTION) {
996 SetCallKind(r5, call_kind);
997 Call(code, rmode);
998 } else {
999 SetCallKind(r5, call_kind);
1000 Jump(code, rmode);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001001 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001002
1003 // Continue here if InvokePrologue does handle the invocation due to
1004 // mismatched parameter counts.
1005 bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +00001006}
1007
1008
1009void MacroAssembler::InvokeFunction(Register fun,
1010 const ParameterCount& actual,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001011 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +00001012 const CallWrapper& call_wrapper,
1013 CallKind call_kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 // Contract with called JS functions requires that function is passed in r1.
1015 ASSERT(fun.is(r1));
1016
1017 Register expected_reg = r2;
1018 Register code_reg = r3;
1019
1020 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1021 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1022 ldr(expected_reg,
1023 FieldMemOperand(code_reg,
1024 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001025 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001026 ldr(code_reg,
Steve Block791712a2010-08-27 10:21:07 +01001027 FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001028
1029 ParameterCount expected(expected_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00001030 InvokeCode(code_reg, expected, actual, flag, call_wrapper, call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001031}
1032
1033
Ben Murdoch85b71792012-04-11 18:30:58 +01001034void MacroAssembler::InvokeFunction(JSFunction* function,
Andrei Popescu402d9372010-02-26 13:31:12 +00001035 const ParameterCount& actual,
Ben Murdoch257744e2011-11-30 15:57:28 +00001036 InvokeFlag flag,
1037 CallKind call_kind) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001038 ASSERT(function->is_compiled());
Andrei Popescu402d9372010-02-26 13:31:12 +00001039
1040 // Get the function and setup the context.
Ben Murdoch85b71792012-04-11 18:30:58 +01001041 mov(r1, Operand(Handle<JSFunction>(function)));
Andrei Popescu402d9372010-02-26 13:31:12 +00001042 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1043
Ben Murdoch85b71792012-04-11 18:30:58 +01001044 // Invoke the cached code.
1045 Handle<Code> code(function->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001046 ParameterCount expected(function->shared()->formal_parameter_count());
Ben Murdoch85b71792012-04-11 18:30:58 +01001047 if (V8::UseCrankshaft()) {
1048 // TODO(kasperl): For now, we always call indirectly through the
1049 // code field in the function to allow recompilation to take effect
1050 // without changing any of the call sites.
1051 ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1052 InvokeCode(r3, expected, actual, flag, NullCallWrapper(), call_kind);
1053 } else {
1054 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag, call_kind);
1055 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001056}
1057
1058
1059void MacroAssembler::IsObjectJSObjectType(Register heap_object,
1060 Register map,
1061 Register scratch,
1062 Label* fail) {
1063 ldr(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
1064 IsInstanceJSObjectType(map, scratch, fail);
1065}
1066
1067
1068void MacroAssembler::IsInstanceJSObjectType(Register map,
1069 Register scratch,
1070 Label* fail) {
1071 ldrb(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001072 cmp(scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001073 b(lt, fail);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001074 cmp(scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001075 b(gt, fail);
1076}
1077
1078
1079void MacroAssembler::IsObjectJSStringType(Register object,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001080 Register scratch,
1081 Label* fail) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001082 ASSERT(kNotStringTag != 0);
1083
1084 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
1085 ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1086 tst(scratch, Operand(kIsNotStringMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001087 b(ne, fail);
Andrei Popescu402d9372010-02-26 13:31:12 +00001088}
1089
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001090
Steve Blocka7e24c12009-10-30 11:49:00 +00001091#ifdef ENABLE_DEBUGGER_SUPPORT
Andrei Popescu402d9372010-02-26 13:31:12 +00001092void MacroAssembler::DebugBreak() {
Ben Murdoch85b71792012-04-11 18:30:58 +01001093 ASSERT(allow_stub_calls());
Iain Merrick9ac36c92010-09-13 15:29:50 +01001094 mov(r0, Operand(0, RelocInfo::NONE));
Steve Block44f0eee2011-05-26 01:26:41 +01001095 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak, isolate())));
Andrei Popescu402d9372010-02-26 13:31:12 +00001096 CEntryStub ces(1);
1097 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
1098}
Steve Blocka7e24c12009-10-30 11:49:00 +00001099#endif
1100
1101
Ben Murdoch85b71792012-04-11 18:30:58 +01001102void MacroAssembler::PushTryHandler(CodeLocation try_location,
1103 HandlerType type) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001104 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001105 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1106 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch85b71792012-04-11 18:30:58 +01001107 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize);
1108 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 2 * kPointerSize);
1109 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 3 * kPointerSize);
1110 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 4 * kPointerSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001111
Ben Murdoch85b71792012-04-11 18:30:58 +01001112 // The pc (return address) is passed in register lr.
1113 if (try_location == IN_JAVASCRIPT) {
1114 if (type == TRY_CATCH_HANDLER) {
1115 mov(r3, Operand(StackHandler::TRY_CATCH));
1116 } else {
1117 mov(r3, Operand(StackHandler::TRY_FINALLY));
1118 }
1119 stm(db_w, sp, r3.bit() | cp.bit() | fp.bit() | lr.bit());
1120 // Save the current handler as the next handler.
1121 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1122 ldr(r1, MemOperand(r3));
1123 push(r1);
1124 // Link this handler as the new current one.
1125 str(sp, MemOperand(r3));
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001126 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +01001127 // Must preserve r0-r4, r5-r7 are available.
1128 ASSERT(try_location == IN_JS_ENTRY);
1129 // The frame pointer does not point to a JS frame so we save NULL
1130 // for fp. We expect the code throwing an exception to check fp
1131 // before dereferencing it to restore the context.
1132 mov(r5, Operand(StackHandler::ENTRY)); // State.
1133 mov(r6, Operand(Smi::FromInt(0))); // Indicates no context.
1134 mov(r7, Operand(0, RelocInfo::NONE)); // NULL frame pointer.
1135 stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | lr.bit());
1136 // Save the current handler as the next handler.
1137 mov(r7, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1138 ldr(r6, MemOperand(r7));
1139 push(r6);
1140 // Link this handler as the new current one.
1141 str(sp, MemOperand(r7));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001142 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001143}
1144
1145
Leon Clarkee46be812010-01-19 14:06:41 +00001146void MacroAssembler::PopTryHandler() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001147 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
Leon Clarkee46be812010-01-19 14:06:41 +00001148 pop(r1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001149 mov(ip, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Leon Clarkee46be812010-01-19 14:06:41 +00001150 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1151 str(r1, MemOperand(ip));
1152}
1153
1154
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001155void MacroAssembler::Throw(Register value) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001156 // Adjust this code if not the case.
1157 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
Ben Murdoch85b71792012-04-11 18:30:58 +01001158 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
1159 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize);
1160 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 2 * kPointerSize);
1161 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 3 * kPointerSize);
1162 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 4 * kPointerSize);
1163 // r0 is expected to hold the exception.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001164 if (!value.is(r0)) {
1165 mov(r0, value);
1166 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001167
1168 // Drop the sp to the top of the handler.
Ben Murdoch589d6972011-11-30 16:04:58 +00001169 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001170 ldr(sp, MemOperand(r3));
Ben Murdoch85b71792012-04-11 18:30:58 +01001171
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001172 // Restore the next handler.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001173 pop(r2);
1174 str(r2, MemOperand(r3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001175
Ben Murdoch85b71792012-04-11 18:30:58 +01001176 // Restore context and frame pointer, discard state (r3).
1177 ldm(ia_w, sp, r3.bit() | cp.bit() | fp.bit());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001178
1179 // If the handler is a JS frame, restore the context to the frame.
Ben Murdoch85b71792012-04-11 18:30:58 +01001180 // (r3 == ENTRY) == (fp == 0) == (cp == 0), so we could test any
1181 // of them.
1182 cmp(r3, Operand(StackHandler::ENTRY));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001183 str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1184
Ben Murdoch85b71792012-04-11 18:30:58 +01001185#ifdef DEBUG
1186 if (emit_debug_code()) {
1187 mov(lr, Operand(pc));
1188 }
1189#endif
1190 pop(pc);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001191}
1192
1193
Ben Murdoch85b71792012-04-11 18:30:58 +01001194void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type,
1195 Register value) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001196 // Adjust this code if not the case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001197 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
1198 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdoch85b71792012-04-11 18:30:58 +01001199 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize);
1200 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 2 * kPointerSize);
1201 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 3 * kPointerSize);
1202 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 4 * kPointerSize);
1203 // r0 is expected to hold the exception.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001204 if (!value.is(r0)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001205 mov(r0, value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001206 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001207
1208 // Drop sp to the top stack handler.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001209 mov(r3, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
1210 ldr(sp, MemOperand(r3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001211
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001212 // Unwind the handlers until the ENTRY handler is found.
Ben Murdoch85b71792012-04-11 18:30:58 +01001213 Label loop, done;
1214 bind(&loop);
1215 // Load the type of the current stack handler.
1216 const int kStateOffset = StackHandlerConstants::kStateOffset;
1217 ldr(r2, MemOperand(sp, kStateOffset));
1218 cmp(r2, Operand(StackHandler::ENTRY));
1219 b(eq, &done);
1220 // Fetch the next handler in the list.
1221 const int kNextOffset = StackHandlerConstants::kNextOffset;
1222 ldr(sp, MemOperand(sp, kNextOffset));
1223 jmp(&loop);
1224 bind(&done);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001225
Ben Murdoch85b71792012-04-11 18:30:58 +01001226 // Set the top handler address to next handler past the current ENTRY handler.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001227 pop(r2);
1228 str(r2, MemOperand(r3));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001229
Ben Murdoch85b71792012-04-11 18:30:58 +01001230 if (type == OUT_OF_MEMORY) {
1231 // Set external caught exception to false.
1232 ExternalReference external_caught(
1233 Isolate::kExternalCaughtExceptionAddress, isolate());
1234 mov(r0, Operand(false, RelocInfo::NONE));
1235 mov(r2, Operand(external_caught));
1236 str(r0, MemOperand(r2));
1237
1238 // Set pending exception and r0 to out of memory exception.
1239 Failure* out_of_memory = Failure::OutOfMemoryException();
1240 mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1241 mov(r2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
1242 isolate())));
1243 str(r0, MemOperand(r2));
1244 }
1245
1246 // Stack layout at this point. See also StackHandlerConstants.
1247 // sp -> state (ENTRY)
1248 // cp
1249 // fp
1250 // lr
1251
1252 // Restore context and frame pointer, discard state (r2).
1253 ldm(ia_w, sp, r2.bit() | cp.bit() | fp.bit());
1254#ifdef DEBUG
1255 if (emit_debug_code()) {
1256 mov(lr, Operand(pc));
1257 }
1258#endif
1259 pop(pc);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001260}
1261
1262
Steve Blocka7e24c12009-10-30 11:49:00 +00001263void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1264 Register scratch,
1265 Label* miss) {
1266 Label same_contexts;
1267
1268 ASSERT(!holder_reg.is(scratch));
1269 ASSERT(!holder_reg.is(ip));
1270 ASSERT(!scratch.is(ip));
1271
1272 // Load current lexical context from the stack frame.
1273 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
1274 // In debug mode, make sure the lexical context is set.
1275#ifdef DEBUG
Iain Merrick9ac36c92010-09-13 15:29:50 +01001276 cmp(scratch, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001277 Check(ne, "we should not have an empty lexical context");
1278#endif
1279
1280 // Load the global context of the current context.
1281 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1282 ldr(scratch, FieldMemOperand(scratch, offset));
1283 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
1284
1285 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001286 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001287 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1288 // Cannot use ip as a temporary in this verification code. Due to the fact
1289 // that ip is clobbered as part of cmp with an object Operand.
1290 push(holder_reg); // Temporarily save holder on the stack.
1291 // Read the first word and compare to the global_context_map.
1292 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
1293 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1294 cmp(holder_reg, ip);
1295 Check(eq, "JSGlobalObject::global_context should be a global context.");
1296 pop(holder_reg); // Restore holder.
1297 }
1298
1299 // Check if both contexts are the same.
1300 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1301 cmp(scratch, Operand(ip));
1302 b(eq, &same_contexts);
1303
1304 // Check the context is a global context.
Steve Block44f0eee2011-05-26 01:26:41 +01001305 if (emit_debug_code()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1307 // Cannot use ip as a temporary in this verification code. Due to the fact
1308 // that ip is clobbered as part of cmp with an object Operand.
1309 push(holder_reg); // Temporarily save holder on the stack.
1310 mov(holder_reg, ip); // Move ip to its holding place.
1311 LoadRoot(ip, Heap::kNullValueRootIndex);
1312 cmp(holder_reg, ip);
1313 Check(ne, "JSGlobalProxy::context() should not be null.");
1314
1315 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
1316 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
1317 cmp(holder_reg, ip);
1318 Check(eq, "JSGlobalObject::global_context should be a global context.");
1319 // Restore ip is not needed. ip is reloaded below.
1320 pop(holder_reg); // Restore holder.
1321 // Restore ip to holder's context.
1322 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
1323 }
1324
1325 // Check that the security token in the calling global object is
1326 // compatible with the security token in the receiving global
1327 // object.
1328 int token_offset = Context::kHeaderSize +
1329 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1330
1331 ldr(scratch, FieldMemOperand(scratch, token_offset));
1332 ldr(ip, FieldMemOperand(ip, token_offset));
1333 cmp(scratch, Operand(ip));
1334 b(ne, miss);
1335
1336 bind(&same_contexts);
1337}
1338
1339
Ben Murdochc7cc0282012-03-05 14:35:55 +00001340void MacroAssembler::GetNumberHash(Register t0, Register scratch) {
1341 // First of all we assign the hash seed to scratch.
1342 LoadRoot(scratch, Heap::kHashSeedRootIndex);
1343 SmiUntag(scratch);
1344
1345 // Xor original key with a seed.
1346 eor(t0, t0, Operand(scratch));
1347
1348 // Compute the hash code from the untagged key. This must be kept in sync
1349 // with ComputeIntegerHash in utils.h.
1350 //
1351 // hash = ~hash + (hash << 15);
1352 mvn(scratch, Operand(t0));
1353 add(t0, scratch, Operand(t0, LSL, 15));
1354 // hash = hash ^ (hash >> 12);
1355 eor(t0, t0, Operand(t0, LSR, 12));
1356 // hash = hash + (hash << 2);
1357 add(t0, t0, Operand(t0, LSL, 2));
1358 // hash = hash ^ (hash >> 4);
1359 eor(t0, t0, Operand(t0, LSR, 4));
1360 // hash = hash * 2057;
Ben Murdoch85b71792012-04-11 18:30:58 +01001361 mov(scratch, Operand(2057));
1362 mul(t0, t0, scratch);
Ben Murdochc7cc0282012-03-05 14:35:55 +00001363 // hash = hash ^ (hash >> 16);
1364 eor(t0, t0, Operand(t0, LSR, 16));
1365}
1366
1367
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001368void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1369 Register elements,
1370 Register key,
1371 Register result,
1372 Register t0,
1373 Register t1,
1374 Register t2) {
1375 // Register use:
1376 //
1377 // elements - holds the slow-case elements of the receiver on entry.
1378 // Unchanged unless 'result' is the same register.
1379 //
1380 // key - holds the smi key on entry.
1381 // Unchanged unless 'result' is the same register.
1382 //
1383 // result - holds the result on exit if the load succeeded.
1384 // Allowed to be the same as 'key' or 'result'.
1385 // Unchanged on bailout so 'key' or 'result' can be used
1386 // in further computation.
1387 //
1388 // Scratch registers:
1389 //
1390 // t0 - holds the untagged key on entry and holds the hash once computed.
1391 //
1392 // t1 - used to hold the capacity mask of the dictionary
1393 //
1394 // t2 - used for the index into the dictionary.
1395 Label done;
1396
Ben Murdochc7cc0282012-03-05 14:35:55 +00001397 GetNumberHash(t0, t1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001398
1399 // Compute the capacity mask.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001400 ldr(t1, FieldMemOperand(elements, SeededNumberDictionary::kCapacityOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001401 mov(t1, Operand(t1, ASR, kSmiTagSize)); // convert smi to int
1402 sub(t1, t1, Operand(1));
1403
1404 // Generate an unrolled loop that performs a few probes before giving up.
1405 static const int kProbes = 4;
1406 for (int i = 0; i < kProbes; i++) {
1407 // Use t2 for index calculations and keep the hash intact in t0.
1408 mov(t2, t0);
1409 // Compute the masked index: (hash + i + i * i) & mask.
1410 if (i > 0) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001411 add(t2, t2, Operand(SeededNumberDictionary::GetProbeOffset(i)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001412 }
1413 and_(t2, t2, Operand(t1));
1414
1415 // Scale the index by multiplying by the element size.
Ben Murdochc7cc0282012-03-05 14:35:55 +00001416 ASSERT(SeededNumberDictionary::kEntrySize == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001417 add(t2, t2, Operand(t2, LSL, 1)); // t2 = t2 * 3
1418
1419 // Check if the key is identical to the name.
1420 add(t2, elements, Operand(t2, LSL, kPointerSizeLog2));
Ben Murdochc7cc0282012-03-05 14:35:55 +00001421 ldr(ip, FieldMemOperand(t2, SeededNumberDictionary::kElementsStartOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001422 cmp(key, Operand(ip));
1423 if (i != kProbes - 1) {
1424 b(eq, &done);
1425 } else {
1426 b(ne, miss);
1427 }
1428 }
1429
1430 bind(&done);
1431 // Check that the value is a normal property.
1432 // t2: elements + (index * kPointerSize)
1433 const int kDetailsOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001434 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001435 ldr(t1, FieldMemOperand(t2, kDetailsOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +00001436 tst(t1, Operand(Smi::FromInt(PropertyDetails::TypeField::kMask)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001437 b(ne, miss);
1438
1439 // Get the value at the masked, scaled index and return.
1440 const int kValueOffset =
Ben Murdochc7cc0282012-03-05 14:35:55 +00001441 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001442 ldr(result, FieldMemOperand(t2, kValueOffset));
1443}
1444
1445
Steve Blocka7e24c12009-10-30 11:49:00 +00001446void MacroAssembler::AllocateInNewSpace(int object_size,
1447 Register result,
1448 Register scratch1,
1449 Register scratch2,
1450 Label* gc_required,
1451 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001452 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001453 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001454 // Trash the registers to simulate an allocation failure.
1455 mov(result, Operand(0x7091));
1456 mov(scratch1, Operand(0x7191));
1457 mov(scratch2, Operand(0x7291));
1458 }
1459 jmp(gc_required);
1460 return;
1461 }
1462
Steve Blocka7e24c12009-10-30 11:49:00 +00001463 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001464 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001465 ASSERT(!scratch1.is(scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001466 ASSERT(!scratch1.is(ip));
1467 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001468
Kristian Monsen25f61362010-05-21 11:50:48 +01001469 // Make object size into bytes.
1470 if ((flags & SIZE_IN_WORDS) != 0) {
1471 object_size *= kPointerSize;
1472 }
1473 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1474
Ben Murdochb0fe1622011-05-05 13:52:32 +01001475 // Check relative positions of allocation top and limit addresses.
1476 // The values must be adjacent in memory to allow the use of LDM.
1477 // Also, assert that the registers are numbered such that the values
1478 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001479 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001480 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001481 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001482 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001483 intptr_t top =
1484 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1485 intptr_t limit =
1486 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1487 ASSERT((limit - top) == kPointerSize);
1488 ASSERT(result.code() < ip.code());
1489
1490 // Set up allocation top address and object size registers.
1491 Register topaddr = scratch1;
1492 Register obj_size_reg = scratch2;
1493 mov(topaddr, Operand(new_space_allocation_top));
1494 mov(obj_size_reg, Operand(object_size));
1495
1496 // This code stores a temporary value in ip. This is OK, as the code below
1497 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001498 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001499 // Load allocation top into result and allocation limit into ip.
1500 ldm(ia, topaddr, result.bit() | ip.bit());
1501 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001502 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001503 // Assert that result actually contains top on entry. ip is used
1504 // immediately below so this use of ip does not cause difference with
1505 // respect to register content between debug and release mode.
1506 ldr(ip, MemOperand(topaddr));
1507 cmp(result, ip);
1508 Check(eq, "Unexpected allocation top");
1509 }
1510 // Load allocation limit into ip. Result already contains allocation top.
1511 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001512 }
1513
1514 // Calculate new top and bail out if new space is exhausted. Use result
1515 // to calculate the new top.
Steve Block1e0659c2011-05-24 12:43:12 +01001516 add(scratch2, result, Operand(obj_size_reg), SetCC);
1517 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001518 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001519 b(hi, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001520 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001521
Ben Murdochb0fe1622011-05-05 13:52:32 +01001522 // Tag object if requested.
Steve Blocka7e24c12009-10-30 11:49:00 +00001523 if ((flags & TAG_OBJECT) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001524 add(result, result, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 }
1526}
1527
1528
1529void MacroAssembler::AllocateInNewSpace(Register object_size,
1530 Register result,
1531 Register scratch1,
1532 Register scratch2,
1533 Label* gc_required,
1534 AllocationFlags flags) {
John Reck59135872010-11-02 12:39:01 -07001535 if (!FLAG_inline_new) {
Steve Block44f0eee2011-05-26 01:26:41 +01001536 if (emit_debug_code()) {
John Reck59135872010-11-02 12:39:01 -07001537 // Trash the registers to simulate an allocation failure.
1538 mov(result, Operand(0x7091));
1539 mov(scratch1, Operand(0x7191));
1540 mov(scratch2, Operand(0x7291));
1541 }
1542 jmp(gc_required);
1543 return;
1544 }
1545
Ben Murdochb0fe1622011-05-05 13:52:32 +01001546 // Assert that the register arguments are different and that none of
1547 // them are ip. ip is used explicitly in the code generated below.
Steve Blocka7e24c12009-10-30 11:49:00 +00001548 ASSERT(!result.is(scratch1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001549 ASSERT(!result.is(scratch2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001550 ASSERT(!scratch1.is(scratch2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001551 ASSERT(!result.is(ip));
1552 ASSERT(!scratch1.is(ip));
1553 ASSERT(!scratch2.is(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001554
Ben Murdochb0fe1622011-05-05 13:52:32 +01001555 // Check relative positions of allocation top and limit addresses.
1556 // The values must be adjacent in memory to allow the use of LDM.
1557 // Also, assert that the registers are numbered such that the values
1558 // are loaded in the correct order.
Steve Blocka7e24c12009-10-30 11:49:00 +00001559 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001560 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001561 ExternalReference new_space_allocation_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001562 ExternalReference::new_space_allocation_limit_address(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001563 intptr_t top =
1564 reinterpret_cast<intptr_t>(new_space_allocation_top.address());
1565 intptr_t limit =
1566 reinterpret_cast<intptr_t>(new_space_allocation_limit.address());
1567 ASSERT((limit - top) == kPointerSize);
1568 ASSERT(result.code() < ip.code());
1569
1570 // Set up allocation top address.
1571 Register topaddr = scratch1;
1572 mov(topaddr, Operand(new_space_allocation_top));
1573
1574 // This code stores a temporary value in ip. This is OK, as the code below
1575 // does not need ip for implicit literal generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00001576 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001577 // Load allocation top into result and allocation limit into ip.
1578 ldm(ia, topaddr, result.bit() | ip.bit());
1579 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001580 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001581 // Assert that result actually contains top on entry. ip is used
1582 // immediately below so this use of ip does not cause difference with
1583 // respect to register content between debug and release mode.
1584 ldr(ip, MemOperand(topaddr));
1585 cmp(result, ip);
1586 Check(eq, "Unexpected allocation top");
1587 }
1588 // Load allocation limit into ip. Result already contains allocation top.
1589 ldr(ip, MemOperand(topaddr, limit - top));
Steve Blocka7e24c12009-10-30 11:49:00 +00001590 }
1591
1592 // Calculate new top and bail out if new space is exhausted. Use result
Ben Murdochb0fe1622011-05-05 13:52:32 +01001593 // to calculate the new top. Object size may be in words so a shift is
1594 // required to get the number of bytes.
Kristian Monsen25f61362010-05-21 11:50:48 +01001595 if ((flags & SIZE_IN_WORDS) != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001596 add(scratch2, result, Operand(object_size, LSL, kPointerSizeLog2), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001597 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001598 add(scratch2, result, Operand(object_size), SetCC);
Kristian Monsen25f61362010-05-21 11:50:48 +01001599 }
Steve Block1e0659c2011-05-24 12:43:12 +01001600 b(cs, gc_required);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001601 cmp(scratch2, Operand(ip));
Steve Blocka7e24c12009-10-30 11:49:00 +00001602 b(hi, gc_required);
1603
Steve Blockd0582a62009-12-15 09:54:21 +00001604 // Update allocation top. result temporarily holds the new top.
Steve Block44f0eee2011-05-26 01:26:41 +01001605 if (emit_debug_code()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001606 tst(scratch2, Operand(kObjectAlignmentMask));
Steve Blockd0582a62009-12-15 09:54:21 +00001607 Check(eq, "Unaligned allocation in new space");
1608 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001609 str(scratch2, MemOperand(topaddr));
Steve Blocka7e24c12009-10-30 11:49:00 +00001610
1611 // Tag object if requested.
1612 if ((flags & TAG_OBJECT) != 0) {
1613 add(result, result, Operand(kHeapObjectTag));
1614 }
1615}
1616
1617
1618void MacroAssembler::UndoAllocationInNewSpace(Register object,
1619 Register scratch) {
1620 ExternalReference new_space_allocation_top =
Steve Block44f0eee2011-05-26 01:26:41 +01001621 ExternalReference::new_space_allocation_top_address(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001622
1623 // Make sure the object has no tag before resetting top.
1624 and_(object, object, Operand(~kHeapObjectTagMask));
1625#ifdef DEBUG
1626 // Check that the object un-allocated is below the current top.
1627 mov(scratch, Operand(new_space_allocation_top));
1628 ldr(scratch, MemOperand(scratch));
1629 cmp(object, scratch);
1630 Check(lt, "Undo allocation of non allocated memory");
1631#endif
1632 // Write the address of the object to un-allocate as the current top.
1633 mov(scratch, Operand(new_space_allocation_top));
1634 str(object, MemOperand(scratch));
1635}
1636
1637
Andrei Popescu31002712010-02-23 13:46:05 +00001638void MacroAssembler::AllocateTwoByteString(Register result,
1639 Register length,
1640 Register scratch1,
1641 Register scratch2,
1642 Register scratch3,
1643 Label* gc_required) {
1644 // Calculate the number of bytes needed for the characters in the string while
1645 // observing object alignment.
1646 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1647 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1648 add(scratch1, scratch1,
1649 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001650 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001651
1652 // Allocate two-byte string in new space.
1653 AllocateInNewSpace(scratch1,
1654 result,
1655 scratch2,
1656 scratch3,
1657 gc_required,
1658 TAG_OBJECT);
1659
1660 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001661 InitializeNewString(result,
1662 length,
1663 Heap::kStringMapRootIndex,
1664 scratch1,
1665 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001666}
1667
1668
1669void MacroAssembler::AllocateAsciiString(Register result,
1670 Register length,
1671 Register scratch1,
1672 Register scratch2,
1673 Register scratch3,
1674 Label* gc_required) {
1675 // Calculate the number of bytes needed for the characters in the string while
1676 // observing object alignment.
1677 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1678 ASSERT(kCharSize == 1);
1679 add(scratch1, length,
1680 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
Kristian Monsen25f61362010-05-21 11:50:48 +01001681 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
Andrei Popescu31002712010-02-23 13:46:05 +00001682
1683 // Allocate ASCII string in new space.
1684 AllocateInNewSpace(scratch1,
1685 result,
1686 scratch2,
1687 scratch3,
1688 gc_required,
1689 TAG_OBJECT);
1690
1691 // Set the map, length and hash field.
Steve Block6ded16b2010-05-10 14:33:55 +01001692 InitializeNewString(result,
1693 length,
1694 Heap::kAsciiStringMapRootIndex,
1695 scratch1,
1696 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001697}
1698
1699
1700void MacroAssembler::AllocateTwoByteConsString(Register result,
1701 Register length,
1702 Register scratch1,
1703 Register scratch2,
1704 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001705 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001706 result,
1707 scratch1,
1708 scratch2,
1709 gc_required,
1710 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001711
1712 InitializeNewString(result,
1713 length,
1714 Heap::kConsStringMapRootIndex,
1715 scratch1,
1716 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001717}
1718
1719
1720void MacroAssembler::AllocateAsciiConsString(Register result,
1721 Register length,
1722 Register scratch1,
1723 Register scratch2,
1724 Label* gc_required) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001725 AllocateInNewSpace(ConsString::kSize,
Andrei Popescu31002712010-02-23 13:46:05 +00001726 result,
1727 scratch1,
1728 scratch2,
1729 gc_required,
1730 TAG_OBJECT);
Steve Block6ded16b2010-05-10 14:33:55 +01001731
1732 InitializeNewString(result,
1733 length,
1734 Heap::kConsAsciiStringMapRootIndex,
1735 scratch1,
1736 scratch2);
Andrei Popescu31002712010-02-23 13:46:05 +00001737}
1738
1739
Ben Murdoch589d6972011-11-30 16:04:58 +00001740void MacroAssembler::AllocateTwoByteSlicedString(Register result,
1741 Register length,
1742 Register scratch1,
1743 Register scratch2,
1744 Label* gc_required) {
1745 AllocateInNewSpace(SlicedString::kSize,
1746 result,
1747 scratch1,
1748 scratch2,
1749 gc_required,
1750 TAG_OBJECT);
1751
1752 InitializeNewString(result,
1753 length,
1754 Heap::kSlicedStringMapRootIndex,
1755 scratch1,
1756 scratch2);
1757}
1758
1759
1760void MacroAssembler::AllocateAsciiSlicedString(Register result,
1761 Register length,
1762 Register scratch1,
1763 Register scratch2,
1764 Label* gc_required) {
1765 AllocateInNewSpace(SlicedString::kSize,
1766 result,
1767 scratch1,
1768 scratch2,
1769 gc_required,
1770 TAG_OBJECT);
1771
1772 InitializeNewString(result,
1773 length,
1774 Heap::kSlicedAsciiStringMapRootIndex,
1775 scratch1,
1776 scratch2);
1777}
1778
1779
Steve Block6ded16b2010-05-10 14:33:55 +01001780void MacroAssembler::CompareObjectType(Register object,
Steve Blocka7e24c12009-10-30 11:49:00 +00001781 Register map,
1782 Register type_reg,
1783 InstanceType type) {
Steve Block6ded16b2010-05-10 14:33:55 +01001784 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001785 CompareInstanceType(map, type_reg, type);
1786}
1787
1788
1789void MacroAssembler::CompareInstanceType(Register map,
1790 Register type_reg,
1791 InstanceType type) {
1792 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1793 cmp(type_reg, Operand(type));
1794}
1795
1796
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001797void MacroAssembler::CompareRoot(Register obj,
1798 Heap::RootListIndex index) {
1799 ASSERT(!obj.is(ip));
1800 LoadRoot(ip, index);
1801 cmp(obj, ip);
1802}
1803
1804
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001805void MacroAssembler::CheckFastElements(Register map,
1806 Register scratch,
1807 Label* fail) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001808 STATIC_ASSERT(FAST_ELEMENTS == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001809 ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
1810 cmp(scratch, Operand(Map::kMaximumBitField2FastElementValue));
1811 b(hi, fail);
1812}
1813
1814
Andrei Popescu31002712010-02-23 13:46:05 +00001815void MacroAssembler::CheckMap(Register obj,
1816 Register scratch,
1817 Handle<Map> map,
1818 Label* fail,
Ben Murdoch85b71792012-04-11 18:30:58 +01001819 SmiCheckType smi_check_type) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001820 if (smi_check_type == DO_SMI_CHECK) {
Steve Block1e0659c2011-05-24 12:43:12 +01001821 JumpIfSmi(obj, fail);
Andrei Popescu31002712010-02-23 13:46:05 +00001822 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001823 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1824 mov(ip, Operand(map));
1825 cmp(scratch, ip);
Andrei Popescu31002712010-02-23 13:46:05 +00001826 b(ne, fail);
1827}
1828
1829
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001830void MacroAssembler::CheckMap(Register obj,
1831 Register scratch,
1832 Heap::RootListIndex index,
1833 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +00001834 SmiCheckType smi_check_type) {
1835 if (smi_check_type == DO_SMI_CHECK) {
Steve Block1e0659c2011-05-24 12:43:12 +01001836 JumpIfSmi(obj, fail);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001837 }
1838 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1839 LoadRoot(ip, index);
1840 cmp(scratch, ip);
1841 b(ne, fail);
1842}
1843
1844
Ben Murdoch257744e2011-11-30 15:57:28 +00001845void MacroAssembler::DispatchMap(Register obj,
1846 Register scratch,
1847 Handle<Map> map,
1848 Handle<Code> success,
1849 SmiCheckType smi_check_type) {
1850 Label fail;
1851 if (smi_check_type == DO_SMI_CHECK) {
1852 JumpIfSmi(obj, &fail);
1853 }
1854 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1855 mov(ip, Operand(map));
1856 cmp(scratch, ip);
1857 Jump(success, RelocInfo::CODE_TARGET, eq);
1858 bind(&fail);
1859}
1860
1861
Steve Blocka7e24c12009-10-30 11:49:00 +00001862void MacroAssembler::TryGetFunctionPrototype(Register function,
1863 Register result,
1864 Register scratch,
Ben Murdoch85b71792012-04-11 18:30:58 +01001865 Label* miss) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001866 // Check that the receiver isn't a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01001867 JumpIfSmi(function, miss);
Steve Blocka7e24c12009-10-30 11:49:00 +00001868
1869 // Check that the function really is a function. Load map into result reg.
1870 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1871 b(ne, miss);
1872
1873 // Make sure that the function has an instance prototype.
1874 Label non_instance;
1875 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1876 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1877 b(ne, &non_instance);
1878
1879 // Get the prototype or initial map from the function.
1880 ldr(result,
1881 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1882
1883 // If the prototype or initial map is the hole, don't return it and
1884 // simply miss the cache instead. This will allow us to allocate a
1885 // prototype object on-demand in the runtime system.
1886 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1887 cmp(result, ip);
1888 b(eq, miss);
1889
1890 // If the function does not have an initial map, we're done.
1891 Label done;
1892 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1893 b(ne, &done);
1894
1895 // Get the prototype from the initial map.
1896 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1897 jmp(&done);
1898
1899 // Non-instance prototype: Fetch prototype from constructor field
1900 // in initial map.
1901 bind(&non_instance);
1902 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1903
1904 // All done.
1905 bind(&done);
1906}
1907
1908
1909void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001910 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001911 Call(stub->GetCode(), RelocInfo::CODE_TARGET, kNoASTId, cond);
Steve Blocka7e24c12009-10-30 11:49:00 +00001912}
1913
1914
Ben Murdoch85b71792012-04-11 18:30:58 +01001915MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub, Condition cond) {
1916 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
1917 Object* result;
1918 { MaybeObject* maybe_result = stub->TryGetCode();
1919 if (!maybe_result->ToObject(&result)) return maybe_result;
1920 }
1921 Handle<Code> code(Code::cast(result));
1922 Call(code, RelocInfo::CODE_TARGET, kNoASTId, cond);
1923 return result;
1924}
1925
1926
Andrei Popescu31002712010-02-23 13:46:05 +00001927void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001928 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
Andrei Popescu31002712010-02-23 13:46:05 +00001929 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1930}
1931
1932
Ben Murdoch85b71792012-04-11 18:30:58 +01001933MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub, Condition cond) {
1934 ASSERT(allow_stub_calls()); // Stub calls are not allowed in some stubs.
1935 Object* result;
1936 { MaybeObject* maybe_result = stub->TryGetCode();
1937 if (!maybe_result->ToObject(&result)) return maybe_result;
1938 }
1939 Jump(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET, cond);
1940 return result;
1941}
1942
1943
Steve Block1e0659c2011-05-24 12:43:12 +01001944static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
1945 return ref0.address() - ref1.address();
1946}
1947
1948
Ben Murdoch85b71792012-04-11 18:30:58 +01001949MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(
1950 ExternalReference function, int stack_space) {
Steve Block1e0659c2011-05-24 12:43:12 +01001951 ExternalReference next_address =
1952 ExternalReference::handle_scope_next_address();
1953 const int kNextOffset = 0;
1954 const int kLimitOffset = AddressOffset(
1955 ExternalReference::handle_scope_limit_address(),
1956 next_address);
1957 const int kLevelOffset = AddressOffset(
1958 ExternalReference::handle_scope_level_address(),
1959 next_address);
1960
1961 // Allocate HandleScope in callee-save registers.
1962 mov(r7, Operand(next_address));
1963 ldr(r4, MemOperand(r7, kNextOffset));
1964 ldr(r5, MemOperand(r7, kLimitOffset));
1965 ldr(r6, MemOperand(r7, kLevelOffset));
1966 add(r6, r6, Operand(1));
1967 str(r6, MemOperand(r7, kLevelOffset));
1968
1969 // Native call returns to the DirectCEntry stub which redirects to the
1970 // return address pushed on stack (could have moved after GC).
1971 // DirectCEntry stub itself is generated early and never moves.
1972 DirectCEntryStub stub;
1973 stub.GenerateCall(this, function);
1974
1975 Label promote_scheduled_exception;
1976 Label delete_allocated_handles;
1977 Label leave_exit_frame;
1978
1979 // If result is non-zero, dereference to get the result value
1980 // otherwise set it to undefined.
1981 cmp(r0, Operand(0));
1982 LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
1983 ldr(r0, MemOperand(r0), ne);
1984
1985 // No more valid handles (the result handle was the last one). Restore
1986 // previous handle scope.
1987 str(r4, MemOperand(r7, kNextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001988 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001989 ldr(r1, MemOperand(r7, kLevelOffset));
1990 cmp(r1, r6);
1991 Check(eq, "Unexpected level after return from api call");
1992 }
1993 sub(r6, r6, Operand(1));
1994 str(r6, MemOperand(r7, kLevelOffset));
1995 ldr(ip, MemOperand(r7, kLimitOffset));
1996 cmp(r5, ip);
1997 b(ne, &delete_allocated_handles);
1998
1999 // Check if the function scheduled an exception.
2000 bind(&leave_exit_frame);
2001 LoadRoot(r4, Heap::kTheHoleValueRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01002002 mov(ip, Operand(ExternalReference::scheduled_exception_address(isolate())));
Steve Block1e0659c2011-05-24 12:43:12 +01002003 ldr(r5, MemOperand(ip));
2004 cmp(r4, r5);
2005 b(ne, &promote_scheduled_exception);
2006
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002007 // LeaveExitFrame expects unwind space to be in a register.
Steve Block1e0659c2011-05-24 12:43:12 +01002008 mov(r4, Operand(stack_space));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002009 LeaveExitFrame(false, r4);
2010 mov(pc, lr);
Steve Block1e0659c2011-05-24 12:43:12 +01002011
2012 bind(&promote_scheduled_exception);
Ben Murdoch85b71792012-04-11 18:30:58 +01002013 MaybeObject* result
2014 = TryTailCallExternalReference(
2015 ExternalReference(Runtime::kPromoteScheduledException, isolate()),
2016 0,
2017 1);
2018 if (result->IsFailure()) {
2019 return result;
2020 }
Steve Block1e0659c2011-05-24 12:43:12 +01002021
2022 // HandleScope limit has changed. Delete allocated extensions.
2023 bind(&delete_allocated_handles);
2024 str(r5, MemOperand(r7, kLimitOffset));
2025 mov(r4, r0);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002026 PrepareCallCFunction(1, r5);
2027 mov(r0, Operand(ExternalReference::isolate_address()));
Steve Block44f0eee2011-05-26 01:26:41 +01002028 CallCFunction(
Ben Murdoch8b112d22011-06-08 16:22:53 +01002029 ExternalReference::delete_handle_scope_extensions(isolate()), 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002030 mov(r0, r4);
2031 jmp(&leave_exit_frame);
2032
Ben Murdoch85b71792012-04-11 18:30:58 +01002033 return result;
Steve Block1e0659c2011-05-24 12:43:12 +01002034}
2035
2036
Steve Blocka7e24c12009-10-30 11:49:00 +00002037void MacroAssembler::IllegalOperation(int num_arguments) {
2038 if (num_arguments > 0) {
2039 add(sp, sp, Operand(num_arguments * kPointerSize));
2040 }
2041 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2042}
2043
2044
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002045void MacroAssembler::IndexFromHash(Register hash, Register index) {
2046 // If the hash field contains an array index pick it out. The assert checks
2047 // that the constants for the maximum number of digits for an array index
2048 // cached in the hash field and the number of bits reserved for it does not
2049 // conflict.
2050 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
2051 (1 << String::kArrayIndexValueBits));
2052 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
2053 // the low kHashShift bits.
2054 STATIC_ASSERT(kSmiTag == 0);
2055 Ubfx(hash, hash, String::kHashShift, String::kArrayIndexValueBits);
2056 mov(index, Operand(hash, LSL, kSmiTagSize));
2057}
2058
2059
Steve Blockd0582a62009-12-15 09:54:21 +00002060void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
2061 Register outHighReg,
2062 Register outLowReg) {
2063 // ARMv7 VFP3 instructions to implement integer to double conversion.
2064 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00002065 vmov(s15, r7);
Steve Block6ded16b2010-05-10 14:33:55 +01002066 vcvt_f64_s32(d7, s15);
Leon Clarkee46be812010-01-19 14:06:41 +00002067 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00002068}
2069
2070
Steve Block8defd9f2010-07-08 12:39:36 +01002071void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
2072 DwVfpRegister result,
2073 Register scratch1,
2074 Register scratch2,
2075 Register heap_number_map,
2076 SwVfpRegister scratch3,
2077 Label* not_number,
2078 ObjectToDoubleFlags flags) {
2079 Label done;
2080 if ((flags & OBJECT_NOT_SMI) == 0) {
2081 Label not_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01002082 JumpIfNotSmi(object, &not_smi);
Steve Block8defd9f2010-07-08 12:39:36 +01002083 // Remove smi tag and convert to double.
2084 mov(scratch1, Operand(object, ASR, kSmiTagSize));
2085 vmov(scratch3, scratch1);
2086 vcvt_f64_s32(result, scratch3);
2087 b(&done);
2088 bind(&not_smi);
2089 }
2090 // Check for heap number and load double value from it.
2091 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
2092 sub(scratch2, object, Operand(kHeapObjectTag));
2093 cmp(scratch1, heap_number_map);
2094 b(ne, not_number);
2095 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
2096 // If exponent is all ones the number is either a NaN or +/-Infinity.
2097 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
2098 Sbfx(scratch1,
2099 scratch1,
2100 HeapNumber::kExponentShift,
2101 HeapNumber::kExponentBits);
2102 // All-one value sign extend to -1.
2103 cmp(scratch1, Operand(-1));
2104 b(eq, not_number);
2105 }
2106 vldr(result, scratch2, HeapNumber::kValueOffset);
2107 bind(&done);
2108}
2109
2110
2111void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
2112 DwVfpRegister value,
2113 Register scratch1,
2114 SwVfpRegister scratch2) {
2115 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
2116 vmov(scratch2, scratch1);
2117 vcvt_f64_s32(value, scratch2);
2118}
2119
2120
Iain Merrick9ac36c92010-09-13 15:29:50 +01002121// Tries to get a signed int32 out of a double precision floating point heap
2122// number. Rounds towards 0. Branch to 'not_int32' if the double is out of the
2123// 32bits signed integer range.
2124void MacroAssembler::ConvertToInt32(Register source,
2125 Register dest,
2126 Register scratch,
2127 Register scratch2,
Steve Block1e0659c2011-05-24 12:43:12 +01002128 DwVfpRegister double_scratch,
Iain Merrick9ac36c92010-09-13 15:29:50 +01002129 Label *not_int32) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002130 if (CpuFeatures::IsSupported(VFP3)) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01002131 CpuFeatures::Scope scope(VFP3);
2132 sub(scratch, source, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01002133 vldr(double_scratch, scratch, HeapNumber::kValueOffset);
2134 vcvt_s32_f64(double_scratch.low(), double_scratch);
2135 vmov(dest, double_scratch.low());
Iain Merrick9ac36c92010-09-13 15:29:50 +01002136 // Signed vcvt instruction will saturate to the minimum (0x80000000) or
2137 // maximun (0x7fffffff) signed 32bits integer when the double is out of
2138 // range. When substracting one, the minimum signed integer becomes the
2139 // maximun signed integer.
2140 sub(scratch, dest, Operand(1));
2141 cmp(scratch, Operand(LONG_MAX - 1));
2142 // If equal then dest was LONG_MAX, if greater dest was LONG_MIN.
2143 b(ge, not_int32);
2144 } else {
2145 // This code is faster for doubles that are in the ranges -0x7fffffff to
2146 // -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds almost to
2147 // the range of signed int32 values that are not Smis. Jumps to the label
2148 // 'not_int32' if the double isn't in the range -0x80000000.0 to
2149 // 0x80000000.0 (excluding the endpoints).
2150 Label right_exponent, done;
2151 // Get exponent word.
2152 ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
2153 // Get exponent alone in scratch2.
2154 Ubfx(scratch2,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002155 scratch,
2156 HeapNumber::kExponentShift,
2157 HeapNumber::kExponentBits);
Iain Merrick9ac36c92010-09-13 15:29:50 +01002158 // Load dest with zero. We use this either for the final shift or
2159 // for the answer.
2160 mov(dest, Operand(0, RelocInfo::NONE));
2161 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
2162 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
2163 // the exponent that we are fastest at and also the highest exponent we can
2164 // handle here.
2165 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
2166 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
2167 // split it up to avoid a constant pool entry. You can't do that in general
2168 // for cmp because of the overflow flag, but we know the exponent is in the
2169 // range 0-2047 so there is no overflow.
2170 int fudge_factor = 0x400;
2171 sub(scratch2, scratch2, Operand(fudge_factor));
2172 cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
2173 // If we have a match of the int32-but-not-Smi exponent then skip some
2174 // logic.
2175 b(eq, &right_exponent);
2176 // If the exponent is higher than that then go to slow case. This catches
2177 // numbers that don't fit in a signed int32, infinities and NaNs.
2178 b(gt, not_int32);
2179
2180 // We know the exponent is smaller than 30 (biased). If it is less than
Ben Murdoch85b71792012-04-11 18:30:58 +01002181 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
Iain Merrick9ac36c92010-09-13 15:29:50 +01002182 // it rounds to zero.
2183 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
2184 sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
2185 // Dest already has a Smi zero.
2186 b(lt, &done);
2187
2188 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
2189 // get how much to shift down.
2190 rsb(dest, scratch2, Operand(30));
2191
2192 bind(&right_exponent);
2193 // Get the top bits of the mantissa.
2194 and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
2195 // Put back the implicit 1.
2196 orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
2197 // Shift up the mantissa bits to take up the space the exponent used to
2198 // take. We just orred in the implicit bit so that took care of one and
2199 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
2200 // distance.
2201 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
2202 mov(scratch2, Operand(scratch2, LSL, shift_distance));
2203 // Put sign in zero flag.
2204 tst(scratch, Operand(HeapNumber::kSignMask));
2205 // Get the second half of the double. For some exponents we don't
2206 // actually need this because the bits get shifted out again, but
2207 // it's probably slower to test than just to do it.
2208 ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
2209 // Shift down 22 bits to get the last 10 bits.
2210 orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
2211 // Move down according to the exponent.
2212 mov(dest, Operand(scratch, LSR, dest));
2213 // Fix sign if sign bit was set.
2214 rsb(dest, dest, Operand(0, RelocInfo::NONE), LeaveCC, ne);
2215 bind(&done);
2216 }
2217}
2218
2219
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002220void MacroAssembler::EmitVFPTruncate(VFPRoundingMode rounding_mode,
2221 SwVfpRegister result,
2222 DwVfpRegister double_input,
2223 Register scratch1,
2224 Register scratch2,
2225 CheckForInexactConversion check_inexact) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002226 ASSERT(CpuFeatures::IsSupported(VFP3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002227 CpuFeatures::Scope scope(VFP3);
2228 Register prev_fpscr = scratch1;
2229 Register scratch = scratch2;
2230
2231 int32_t check_inexact_conversion =
2232 (check_inexact == kCheckForInexactConversion) ? kVFPInexactExceptionBit : 0;
2233
2234 // Set custom FPCSR:
2235 // - Set rounding mode.
2236 // - Clear vfp cumulative exception flags.
2237 // - Make sure Flush-to-zero mode control bit is unset.
2238 vmrs(prev_fpscr);
2239 bic(scratch,
2240 prev_fpscr,
2241 Operand(kVFPExceptionMask |
2242 check_inexact_conversion |
2243 kVFPRoundingModeMask |
2244 kVFPFlushToZeroMask));
2245 // 'Round To Nearest' is encoded by 0b00 so no bits need to be set.
2246 if (rounding_mode != kRoundToNearest) {
2247 orr(scratch, scratch, Operand(rounding_mode));
2248 }
2249 vmsr(scratch);
2250
2251 // Convert the argument to an integer.
2252 vcvt_s32_f64(result,
2253 double_input,
2254 (rounding_mode == kRoundToZero) ? kDefaultRoundToZero
2255 : kFPSCRRounding);
2256
2257 // Retrieve FPSCR.
2258 vmrs(scratch);
2259 // Restore FPSCR.
2260 vmsr(prev_fpscr);
2261 // Check for vfp exceptions.
2262 tst(scratch, Operand(kVFPExceptionMask | check_inexact_conversion));
2263}
2264
2265
Steve Block44f0eee2011-05-26 01:26:41 +01002266void MacroAssembler::EmitOutOfInt32RangeTruncate(Register result,
2267 Register input_high,
2268 Register input_low,
2269 Register scratch) {
2270 Label done, normal_exponent, restore_sign;
2271
2272 // Extract the biased exponent in result.
2273 Ubfx(result,
2274 input_high,
2275 HeapNumber::kExponentShift,
2276 HeapNumber::kExponentBits);
2277
2278 // Check for Infinity and NaNs, which should return 0.
2279 cmp(result, Operand(HeapNumber::kExponentMask));
2280 mov(result, Operand(0), LeaveCC, eq);
2281 b(eq, &done);
2282
2283 // Express exponent as delta to (number of mantissa bits + 31).
2284 sub(result,
2285 result,
2286 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31),
2287 SetCC);
2288
2289 // If the delta is strictly positive, all bits would be shifted away,
2290 // which means that we can return 0.
2291 b(le, &normal_exponent);
2292 mov(result, Operand(0));
2293 b(&done);
2294
2295 bind(&normal_exponent);
2296 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
2297 // Calculate shift.
2298 add(scratch, result, Operand(kShiftBase + HeapNumber::kMantissaBits), SetCC);
2299
2300 // Save the sign.
2301 Register sign = result;
2302 result = no_reg;
2303 and_(sign, input_high, Operand(HeapNumber::kSignMask));
2304
2305 // Set the implicit 1 before the mantissa part in input_high.
2306 orr(input_high,
2307 input_high,
2308 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
2309 // Shift the mantissa bits to the correct position.
2310 // We don't need to clear non-mantissa bits as they will be shifted away.
2311 // If they weren't, it would mean that the answer is in the 32bit range.
2312 mov(input_high, Operand(input_high, LSL, scratch));
2313
2314 // Replace the shifted bits with bits from the lower mantissa word.
2315 Label pos_shift, shift_done;
2316 rsb(scratch, scratch, Operand(32), SetCC);
2317 b(&pos_shift, ge);
2318
2319 // Negate scratch.
2320 rsb(scratch, scratch, Operand(0));
2321 mov(input_low, Operand(input_low, LSL, scratch));
2322 b(&shift_done);
2323
2324 bind(&pos_shift);
2325 mov(input_low, Operand(input_low, LSR, scratch));
2326
2327 bind(&shift_done);
2328 orr(input_high, input_high, Operand(input_low));
2329 // Restore sign if necessary.
2330 cmp(sign, Operand(0));
2331 result = sign;
2332 sign = no_reg;
2333 rsb(result, input_high, Operand(0), LeaveCC, ne);
2334 mov(result, input_high, LeaveCC, eq);
2335 bind(&done);
2336}
2337
2338
2339void MacroAssembler::EmitECMATruncate(Register result,
2340 DwVfpRegister double_input,
2341 SwVfpRegister single_scratch,
2342 Register scratch,
2343 Register input_high,
2344 Register input_low) {
2345 CpuFeatures::Scope scope(VFP3);
2346 ASSERT(!input_high.is(result));
2347 ASSERT(!input_low.is(result));
2348 ASSERT(!input_low.is(input_high));
2349 ASSERT(!scratch.is(result) &&
2350 !scratch.is(input_high) &&
2351 !scratch.is(input_low));
2352 ASSERT(!single_scratch.is(double_input.low()) &&
2353 !single_scratch.is(double_input.high()));
2354
2355 Label done;
2356
2357 // Clear cumulative exception flags.
2358 ClearFPSCRBits(kVFPExceptionMask, scratch);
2359 // Try a conversion to a signed integer.
2360 vcvt_s32_f64(single_scratch, double_input);
2361 vmov(result, single_scratch);
2362 // Retrieve he FPSCR.
2363 vmrs(scratch);
2364 // Check for overflow and NaNs.
2365 tst(scratch, Operand(kVFPOverflowExceptionBit |
2366 kVFPUnderflowExceptionBit |
2367 kVFPInvalidOpExceptionBit));
2368 // If we had no exceptions we are done.
2369 b(eq, &done);
2370
2371 // Load the double value and perform a manual truncation.
2372 vmov(input_low, input_high, double_input);
2373 EmitOutOfInt32RangeTruncate(result,
2374 input_high,
2375 input_low,
2376 scratch);
2377 bind(&done);
2378}
2379
2380
Andrei Popescu31002712010-02-23 13:46:05 +00002381void MacroAssembler::GetLeastBitsFromSmi(Register dst,
2382 Register src,
2383 int num_least_bits) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002384 if (CpuFeatures::IsSupported(ARMv7)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002385 ubfx(dst, src, kSmiTagSize, num_least_bits);
Andrei Popescu31002712010-02-23 13:46:05 +00002386 } else {
2387 mov(dst, Operand(src, ASR, kSmiTagSize));
2388 and_(dst, dst, Operand((1 << num_least_bits) - 1));
2389 }
2390}
2391
2392
Steve Block1e0659c2011-05-24 12:43:12 +01002393void MacroAssembler::GetLeastBitsFromInt32(Register dst,
2394 Register src,
2395 int num_least_bits) {
2396 and_(dst, src, Operand((1 << num_least_bits) - 1));
2397}
2398
2399
Steve Block44f0eee2011-05-26 01:26:41 +01002400void MacroAssembler::CallRuntime(const Runtime::Function* f,
2401 int num_arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002402 // All parameters are on the stack. r0 has the return value after call.
2403
2404 // If the expected number of arguments of the runtime function is
2405 // constant, we check that the actual number of arguments match the
2406 // expectation.
2407 if (f->nargs >= 0 && f->nargs != num_arguments) {
2408 IllegalOperation(num_arguments);
2409 return;
2410 }
2411
Leon Clarke4515c472010-02-03 11:58:03 +00002412 // TODO(1236192): Most runtime routines don't need the number of
2413 // arguments passed in because it is constant. At some point we
2414 // should remove this need and make the runtime routine entry code
2415 // smarter.
2416 mov(r0, Operand(num_arguments));
Steve Block44f0eee2011-05-26 01:26:41 +01002417 mov(r1, Operand(ExternalReference(f, isolate())));
Leon Clarke4515c472010-02-03 11:58:03 +00002418 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002419 CallStub(&stub);
2420}
2421
2422
2423void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
2424 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
2425}
2426
2427
Ben Murdochb0fe1622011-05-05 13:52:32 +01002428void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
Steve Block44f0eee2011-05-26 01:26:41 +01002429 const Runtime::Function* function = Runtime::FunctionForId(id);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002430 mov(r0, Operand(function->nargs));
Steve Block44f0eee2011-05-26 01:26:41 +01002431 mov(r1, Operand(ExternalReference(function, isolate())));
Ben Murdoch85b71792012-04-11 18:30:58 +01002432 CEntryStub stub(1);
2433 stub.SaveDoubles();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002434 CallStub(&stub);
2435}
2436
2437
Andrei Popescu402d9372010-02-26 13:31:12 +00002438void MacroAssembler::CallExternalReference(const ExternalReference& ext,
2439 int num_arguments) {
2440 mov(r0, Operand(num_arguments));
2441 mov(r1, Operand(ext));
2442
2443 CEntryStub stub(1);
2444 CallStub(&stub);
2445}
2446
2447
Steve Block6ded16b2010-05-10 14:33:55 +01002448void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
2449 int num_arguments,
2450 int result_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002451 // TODO(1236192): Most runtime routines don't need the number of
2452 // arguments passed in because it is constant. At some point we
2453 // should remove this need and make the runtime routine entry code
2454 // smarter.
2455 mov(r0, Operand(num_arguments));
Steve Block6ded16b2010-05-10 14:33:55 +01002456 JumpToExternalReference(ext);
Steve Blocka7e24c12009-10-30 11:49:00 +00002457}
2458
2459
Ben Murdoch85b71792012-04-11 18:30:58 +01002460MaybeObject* MacroAssembler::TryTailCallExternalReference(
2461 const ExternalReference& ext, int num_arguments, int result_size) {
2462 // TODO(1236192): Most runtime routines don't need the number of
2463 // arguments passed in because it is constant. At some point we
2464 // should remove this need and make the runtime routine entry code
2465 // smarter.
2466 mov(r0, Operand(num_arguments));
2467 return TryJumpToExternalReference(ext);
2468}
2469
2470
Steve Block6ded16b2010-05-10 14:33:55 +01002471void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
2472 int num_arguments,
2473 int result_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002474 TailCallExternalReference(ExternalReference(fid, isolate()),
2475 num_arguments,
2476 result_size);
Steve Block6ded16b2010-05-10 14:33:55 +01002477}
2478
2479
2480void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002481#if defined(__thumb__)
2482 // Thumb mode builtin.
2483 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2484#endif
2485 mov(r1, Operand(builtin));
2486 CEntryStub stub(1);
2487 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2488}
2489
2490
Ben Murdoch85b71792012-04-11 18:30:58 +01002491MaybeObject* MacroAssembler::TryJumpToExternalReference(
2492 const ExternalReference& builtin) {
2493#if defined(__thumb__)
2494 // Thumb mode builtin.
2495 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
2496#endif
2497 mov(r1, Operand(builtin));
2498 CEntryStub stub(1);
2499 return TryTailCallStub(&stub);
2500}
2501
2502
Steve Blocka7e24c12009-10-30 11:49:00 +00002503void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
Ben Murdoch257744e2011-11-30 15:57:28 +00002504 InvokeFlag flag,
2505 const CallWrapper& call_wrapper) {
Andrei Popescu402d9372010-02-26 13:31:12 +00002506 GetBuiltinEntry(r2, id);
Ben Murdoch257744e2011-11-30 15:57:28 +00002507 if (flag == CALL_FUNCTION) {
2508 call_wrapper.BeforeCall(CallSize(r2));
2509 SetCallKind(r5, CALL_AS_METHOD);
Andrei Popescu402d9372010-02-26 13:31:12 +00002510 Call(r2);
Ben Murdoch257744e2011-11-30 15:57:28 +00002511 call_wrapper.AfterCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00002512 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002513 ASSERT(flag == JUMP_FUNCTION);
2514 SetCallKind(r5, CALL_AS_METHOD);
Andrei Popescu402d9372010-02-26 13:31:12 +00002515 Jump(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002516 }
2517}
2518
2519
Steve Block791712a2010-08-27 10:21:07 +01002520void MacroAssembler::GetBuiltinFunction(Register target,
2521 Builtins::JavaScript id) {
Steve Block6ded16b2010-05-10 14:33:55 +01002522 // Load the builtins object into target register.
2523 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2524 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00002525 // Load the JavaScript builtin function from the builtins object.
Steve Block6ded16b2010-05-10 14:33:55 +01002526 ldr(target, FieldMemOperand(target,
Steve Block791712a2010-08-27 10:21:07 +01002527 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2528}
2529
2530
2531void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2532 ASSERT(!target.is(r1));
2533 GetBuiltinFunction(r1, id);
2534 // Load the code entry point from the builtins object.
2535 ldr(target, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00002536}
2537
2538
2539void MacroAssembler::SetCounter(StatsCounter* counter, int value,
2540 Register scratch1, Register scratch2) {
2541 if (FLAG_native_code_counters && counter->Enabled()) {
2542 mov(scratch1, Operand(value));
2543 mov(scratch2, Operand(ExternalReference(counter)));
2544 str(scratch1, MemOperand(scratch2));
2545 }
2546}
2547
2548
2549void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2550 Register scratch1, Register scratch2) {
2551 ASSERT(value > 0);
2552 if (FLAG_native_code_counters && counter->Enabled()) {
2553 mov(scratch2, Operand(ExternalReference(counter)));
2554 ldr(scratch1, MemOperand(scratch2));
2555 add(scratch1, scratch1, Operand(value));
2556 str(scratch1, MemOperand(scratch2));
2557 }
2558}
2559
2560
2561void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2562 Register scratch1, Register scratch2) {
2563 ASSERT(value > 0);
2564 if (FLAG_native_code_counters && counter->Enabled()) {
2565 mov(scratch2, Operand(ExternalReference(counter)));
2566 ldr(scratch1, MemOperand(scratch2));
2567 sub(scratch1, scratch1, Operand(value));
2568 str(scratch1, MemOperand(scratch2));
2569 }
2570}
2571
2572
Steve Block1e0659c2011-05-24 12:43:12 +01002573void MacroAssembler::Assert(Condition cond, const char* msg) {
Steve Block44f0eee2011-05-26 01:26:41 +01002574 if (emit_debug_code())
Steve Block1e0659c2011-05-24 12:43:12 +01002575 Check(cond, msg);
Steve Blocka7e24c12009-10-30 11:49:00 +00002576}
2577
2578
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002579void MacroAssembler::AssertRegisterIsRoot(Register reg,
2580 Heap::RootListIndex index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002581 if (emit_debug_code()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002582 LoadRoot(ip, index);
2583 cmp(reg, ip);
2584 Check(eq, "Register did not match expected root");
2585 }
2586}
2587
2588
Iain Merrick75681382010-08-19 15:07:18 +01002589void MacroAssembler::AssertFastElements(Register elements) {
Steve Block44f0eee2011-05-26 01:26:41 +01002590 if (emit_debug_code()) {
Iain Merrick75681382010-08-19 15:07:18 +01002591 ASSERT(!elements.is(ip));
2592 Label ok;
2593 push(elements);
2594 ldr(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
2595 LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2596 cmp(elements, ip);
2597 b(eq, &ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002598 LoadRoot(ip, Heap::kFixedDoubleArrayMapRootIndex);
2599 cmp(elements, ip);
2600 b(eq, &ok);
Iain Merrick75681382010-08-19 15:07:18 +01002601 LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2602 cmp(elements, ip);
2603 b(eq, &ok);
2604 Abort("JSObject with fast elements map has slow elements");
2605 bind(&ok);
2606 pop(elements);
2607 }
2608}
2609
2610
Steve Block1e0659c2011-05-24 12:43:12 +01002611void MacroAssembler::Check(Condition cond, const char* msg) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002612 Label L;
Steve Block1e0659c2011-05-24 12:43:12 +01002613 b(cond, &L);
Steve Blocka7e24c12009-10-30 11:49:00 +00002614 Abort(msg);
2615 // will not return here
2616 bind(&L);
2617}
2618
2619
2620void MacroAssembler::Abort(const char* msg) {
Steve Block8defd9f2010-07-08 12:39:36 +01002621 Label abort_start;
2622 bind(&abort_start);
Steve Blocka7e24c12009-10-30 11:49:00 +00002623 // We want to pass the msg string like a smi to avoid GC
2624 // problems, however msg is not guaranteed to be aligned
2625 // properly. Instead, we pass an aligned pointer that is
2626 // a proper v8 smi, but also pass the alignment difference
2627 // from the real pointer as a smi.
2628 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2629 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2630 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2631#ifdef DEBUG
2632 if (msg != NULL) {
2633 RecordComment("Abort message: ");
2634 RecordComment(msg);
2635 }
2636#endif
Ben Murdoch85b71792012-04-11 18:30:58 +01002637 // Disable stub call restrictions to always allow calls to abort.
2638 AllowStubCallsScope allow_scope(this, true);
Steve Blockd0582a62009-12-15 09:54:21 +00002639
Steve Blocka7e24c12009-10-30 11:49:00 +00002640 mov(r0, Operand(p0));
2641 push(r0);
2642 mov(r0, Operand(Smi::FromInt(p1 - p0)));
2643 push(r0);
Ben Murdoch85b71792012-04-11 18:30:58 +01002644 CallRuntime(Runtime::kAbort, 2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002645 // will not return here
Steve Block8defd9f2010-07-08 12:39:36 +01002646 if (is_const_pool_blocked()) {
2647 // If the calling code cares about the exact number of
2648 // instructions generated, we insert padding here to keep the size
2649 // of the Abort macro constant.
2650 static const int kExpectedAbortInstructions = 10;
2651 int abort_instructions = InstructionsGeneratedSince(&abort_start);
2652 ASSERT(abort_instructions <= kExpectedAbortInstructions);
2653 while (abort_instructions++ < kExpectedAbortInstructions) {
2654 nop();
2655 }
2656 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002657}
2658
2659
Steve Blockd0582a62009-12-15 09:54:21 +00002660void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2661 if (context_chain_length > 0) {
2662 // Move up the chain of contexts to the context containing the slot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002663 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002664 for (int i = 1; i < context_chain_length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002665 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Blockd0582a62009-12-15 09:54:21 +00002666 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002667 } else {
2668 // Slot is in the current function context. Move it into the
2669 // destination register in case we store into it (the write barrier
2670 // cannot be allowed to destroy the context in esi).
2671 mov(dst, cp);
2672 }
Steve Blockd0582a62009-12-15 09:54:21 +00002673}
2674
2675
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002676void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2677 // Load the global or builtins object from the current context.
2678 ldr(function, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
2679 // Load the global context from the global or builtins object.
2680 ldr(function, FieldMemOperand(function,
2681 GlobalObject::kGlobalContextOffset));
2682 // Load the function from the global context.
2683 ldr(function, MemOperand(function, Context::SlotOffset(index)));
2684}
2685
2686
2687void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2688 Register map,
2689 Register scratch) {
2690 // Load the initial map. The global functions all have initial maps.
2691 ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002692 if (emit_debug_code()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002693 Label ok, fail;
Ben Murdoch257744e2011-11-30 15:57:28 +00002694 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002695 b(&ok);
2696 bind(&fail);
2697 Abort("Global functions must have initial map");
2698 bind(&ok);
2699 }
2700}
2701
2702
Steve Block1e0659c2011-05-24 12:43:12 +01002703void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
2704 Register reg,
2705 Register scratch,
2706 Label* not_power_of_two_or_zero) {
2707 sub(scratch, reg, Operand(1), SetCC);
2708 b(mi, not_power_of_two_or_zero);
2709 tst(scratch, reg);
2710 b(ne, not_power_of_two_or_zero);
2711}
2712
2713
Steve Block44f0eee2011-05-26 01:26:41 +01002714void MacroAssembler::JumpIfNotPowerOfTwoOrZeroAndNeg(
2715 Register reg,
2716 Register scratch,
2717 Label* zero_and_neg,
2718 Label* not_power_of_two) {
2719 sub(scratch, reg, Operand(1), SetCC);
2720 b(mi, zero_and_neg);
2721 tst(scratch, reg);
2722 b(ne, not_power_of_two);
2723}
2724
2725
Andrei Popescu31002712010-02-23 13:46:05 +00002726void MacroAssembler::JumpIfNotBothSmi(Register reg1,
2727 Register reg2,
2728 Label* on_not_both_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002729 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002730 tst(reg1, Operand(kSmiTagMask));
2731 tst(reg2, Operand(kSmiTagMask), eq);
2732 b(ne, on_not_both_smi);
2733}
2734
2735
2736void MacroAssembler::JumpIfEitherSmi(Register reg1,
2737 Register reg2,
2738 Label* on_either_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01002739 STATIC_ASSERT(kSmiTag == 0);
Andrei Popescu31002712010-02-23 13:46:05 +00002740 tst(reg1, Operand(kSmiTagMask));
2741 tst(reg2, Operand(kSmiTagMask), ne);
2742 b(eq, on_either_smi);
2743}
2744
2745
Iain Merrick75681382010-08-19 15:07:18 +01002746void MacroAssembler::AbortIfSmi(Register object) {
Steve Block1e0659c2011-05-24 12:43:12 +01002747 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +01002748 tst(object, Operand(kSmiTagMask));
2749 Assert(ne, "Operand is a smi");
2750}
2751
2752
Steve Block1e0659c2011-05-24 12:43:12 +01002753void MacroAssembler::AbortIfNotSmi(Register object) {
2754 STATIC_ASSERT(kSmiTag == 0);
2755 tst(object, Operand(kSmiTagMask));
2756 Assert(eq, "Operand is not smi");
2757}
2758
2759
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002760void MacroAssembler::AbortIfNotString(Register object) {
2761 STATIC_ASSERT(kSmiTag == 0);
2762 tst(object, Operand(kSmiTagMask));
2763 Assert(ne, "Operand is not a string");
2764 push(object);
2765 ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2766 CompareInstanceType(object, object, FIRST_NONSTRING_TYPE);
2767 pop(object);
2768 Assert(lo, "Operand is not a string");
2769}
2770
2771
2772
Steve Block1e0659c2011-05-24 12:43:12 +01002773void MacroAssembler::AbortIfNotRootValue(Register src,
2774 Heap::RootListIndex root_value_index,
2775 const char* message) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002776 CompareRoot(src, root_value_index);
Steve Block1e0659c2011-05-24 12:43:12 +01002777 Assert(eq, message);
2778}
2779
2780
2781void MacroAssembler::JumpIfNotHeapNumber(Register object,
2782 Register heap_number_map,
2783 Register scratch,
2784 Label* on_not_heap_number) {
2785 ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2786 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2787 cmp(scratch, heap_number_map);
2788 b(ne, on_not_heap_number);
2789}
2790
2791
Leon Clarked91b9f72010-01-27 17:25:45 +00002792void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
2793 Register first,
2794 Register second,
2795 Register scratch1,
2796 Register scratch2,
2797 Label* failure) {
2798 // Test that both first and second are sequential ASCII strings.
2799 // Assume that they are non-smis.
2800 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
2801 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
2802 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
2803 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002804
2805 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
2806 scratch2,
2807 scratch1,
2808 scratch2,
2809 failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002810}
2811
2812void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
2813 Register second,
2814 Register scratch1,
2815 Register scratch2,
2816 Label* failure) {
2817 // Check that neither is a smi.
Steve Block1e0659c2011-05-24 12:43:12 +01002818 STATIC_ASSERT(kSmiTag == 0);
Leon Clarked91b9f72010-01-27 17:25:45 +00002819 and_(scratch1, first, Operand(second));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002820 JumpIfSmi(scratch1, failure);
Leon Clarked91b9f72010-01-27 17:25:45 +00002821 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
2822 second,
2823 scratch1,
2824 scratch2,
2825 failure);
2826}
2827
Steve Blockd0582a62009-12-15 09:54:21 +00002828
Steve Block6ded16b2010-05-10 14:33:55 +01002829// Allocates a heap number or jumps to the need_gc label if the young space
2830// is full and a scavenge is needed.
2831void MacroAssembler::AllocateHeapNumber(Register result,
2832 Register scratch1,
2833 Register scratch2,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002834 Register heap_number_map,
Steve Block6ded16b2010-05-10 14:33:55 +01002835 Label* gc_required) {
2836 // Allocate an object in the heap for the heap number and tag it as a heap
2837 // object.
Kristian Monsen25f61362010-05-21 11:50:48 +01002838 AllocateInNewSpace(HeapNumber::kSize,
Steve Block6ded16b2010-05-10 14:33:55 +01002839 result,
2840 scratch1,
2841 scratch2,
2842 gc_required,
2843 TAG_OBJECT);
2844
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002845 // Store heap number map in the allocated object.
2846 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2847 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01002848}
2849
2850
Steve Block8defd9f2010-07-08 12:39:36 +01002851void MacroAssembler::AllocateHeapNumberWithValue(Register result,
2852 DwVfpRegister value,
2853 Register scratch1,
2854 Register scratch2,
2855 Register heap_number_map,
2856 Label* gc_required) {
2857 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
2858 sub(scratch1, result, Operand(kHeapObjectTag));
2859 vstr(value, scratch1, HeapNumber::kValueOffset);
2860}
2861
2862
Ben Murdochbb769b22010-08-11 14:56:33 +01002863// Copies a fixed number of fields of heap objects from src to dst.
2864void MacroAssembler::CopyFields(Register dst,
2865 Register src,
2866 RegList temps,
2867 int field_count) {
2868 // At least one bit set in the first 15 registers.
2869 ASSERT((temps & ((1 << 15) - 1)) != 0);
2870 ASSERT((temps & dst.bit()) == 0);
2871 ASSERT((temps & src.bit()) == 0);
2872 // Primitive implementation using only one temporary register.
2873
2874 Register tmp = no_reg;
2875 // Find a temp register in temps list.
2876 for (int i = 0; i < 15; i++) {
2877 if ((temps & (1 << i)) != 0) {
2878 tmp.set_code(i);
2879 break;
2880 }
2881 }
2882 ASSERT(!tmp.is(no_reg));
2883
2884 for (int i = 0; i < field_count; i++) {
2885 ldr(tmp, FieldMemOperand(src, i * kPointerSize));
2886 str(tmp, FieldMemOperand(dst, i * kPointerSize));
2887 }
2888}
2889
2890
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002891void MacroAssembler::CopyBytes(Register src,
2892 Register dst,
2893 Register length,
2894 Register scratch) {
2895 Label align_loop, align_loop_1, word_loop, byte_loop, byte_loop_1, done;
2896
2897 // Align src before copying in word size chunks.
2898 bind(&align_loop);
2899 cmp(length, Operand(0));
2900 b(eq, &done);
2901 bind(&align_loop_1);
2902 tst(src, Operand(kPointerSize - 1));
2903 b(eq, &word_loop);
2904 ldrb(scratch, MemOperand(src, 1, PostIndex));
2905 strb(scratch, MemOperand(dst, 1, PostIndex));
2906 sub(length, length, Operand(1), SetCC);
2907 b(ne, &byte_loop_1);
2908
2909 // Copy bytes in word size chunks.
2910 bind(&word_loop);
Steve Block44f0eee2011-05-26 01:26:41 +01002911 if (emit_debug_code()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002912 tst(src, Operand(kPointerSize - 1));
2913 Assert(eq, "Expecting alignment for CopyBytes");
2914 }
2915 cmp(length, Operand(kPointerSize));
2916 b(lt, &byte_loop);
2917 ldr(scratch, MemOperand(src, kPointerSize, PostIndex));
2918#if CAN_USE_UNALIGNED_ACCESSES
2919 str(scratch, MemOperand(dst, kPointerSize, PostIndex));
2920#else
2921 strb(scratch, MemOperand(dst, 1, PostIndex));
2922 mov(scratch, Operand(scratch, LSR, 8));
2923 strb(scratch, MemOperand(dst, 1, PostIndex));
2924 mov(scratch, Operand(scratch, LSR, 8));
2925 strb(scratch, MemOperand(dst, 1, PostIndex));
2926 mov(scratch, Operand(scratch, LSR, 8));
2927 strb(scratch, MemOperand(dst, 1, PostIndex));
2928#endif
2929 sub(length, length, Operand(kPointerSize));
2930 b(&word_loop);
2931
2932 // Copy the last bytes if any left.
2933 bind(&byte_loop);
2934 cmp(length, Operand(0));
2935 b(eq, &done);
2936 bind(&byte_loop_1);
2937 ldrb(scratch, MemOperand(src, 1, PostIndex));
2938 strb(scratch, MemOperand(dst, 1, PostIndex));
2939 sub(length, length, Operand(1), SetCC);
2940 b(ne, &byte_loop_1);
2941 bind(&done);
2942}
2943
2944
Steve Block8defd9f2010-07-08 12:39:36 +01002945void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
2946 Register source, // Input.
2947 Register scratch) {
Steve Block1e0659c2011-05-24 12:43:12 +01002948 ASSERT(!zeros.is(source) || !source.is(scratch));
Steve Block8defd9f2010-07-08 12:39:36 +01002949 ASSERT(!zeros.is(scratch));
2950 ASSERT(!scratch.is(ip));
2951 ASSERT(!source.is(ip));
2952 ASSERT(!zeros.is(ip));
Steve Block6ded16b2010-05-10 14:33:55 +01002953#ifdef CAN_USE_ARMV5_INSTRUCTIONS
2954 clz(zeros, source); // This instruction is only supported after ARM5.
2955#else
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002956 mov(zeros, Operand(0, RelocInfo::NONE));
Ben Murdoch85b71792012-04-11 18:30:58 +01002957 Move(scratch, source);
Steve Block6ded16b2010-05-10 14:33:55 +01002958 // Top 16.
2959 tst(scratch, Operand(0xffff0000));
2960 add(zeros, zeros, Operand(16), LeaveCC, eq);
2961 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
2962 // Top 8.
2963 tst(scratch, Operand(0xff000000));
2964 add(zeros, zeros, Operand(8), LeaveCC, eq);
2965 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
2966 // Top 4.
2967 tst(scratch, Operand(0xf0000000));
2968 add(zeros, zeros, Operand(4), LeaveCC, eq);
2969 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
2970 // Top 2.
2971 tst(scratch, Operand(0xc0000000));
2972 add(zeros, zeros, Operand(2), LeaveCC, eq);
2973 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
2974 // Top bit.
2975 tst(scratch, Operand(0x80000000u));
2976 add(zeros, zeros, Operand(1), LeaveCC, eq);
2977#endif
2978}
2979
2980
2981void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2982 Register first,
2983 Register second,
2984 Register scratch1,
2985 Register scratch2,
2986 Label* failure) {
2987 int kFlatAsciiStringMask =
2988 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2989 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2990 and_(scratch1, first, Operand(kFlatAsciiStringMask));
2991 and_(scratch2, second, Operand(kFlatAsciiStringMask));
2992 cmp(scratch1, Operand(kFlatAsciiStringTag));
2993 // Ignore second test if first test failed.
2994 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
2995 b(ne, failure);
2996}
2997
2998
2999void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
3000 Register scratch,
3001 Label* failure) {
3002 int kFlatAsciiStringMask =
3003 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
3004 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
3005 and_(scratch, type, Operand(kFlatAsciiStringMask));
3006 cmp(scratch, Operand(kFlatAsciiStringTag));
3007 b(ne, failure);
3008}
3009
Steve Block44f0eee2011-05-26 01:26:41 +01003010static const int kRegisterPassedArguments = 4;
Steve Block6ded16b2010-05-10 14:33:55 +01003011
Steve Block44f0eee2011-05-26 01:26:41 +01003012
Ben Murdoch257744e2011-11-30 15:57:28 +00003013int MacroAssembler::CalculateStackPassedWords(int num_reg_arguments,
3014 int num_double_arguments) {
3015 int stack_passed_words = 0;
3016 if (use_eabi_hardfloat()) {
3017 // In the hard floating point calling convention, we can use
3018 // all double registers to pass doubles.
3019 if (num_double_arguments > DoubleRegister::kNumRegisters) {
3020 stack_passed_words +=
3021 2 * (num_double_arguments - DoubleRegister::kNumRegisters);
3022 }
3023 } else {
3024 // In the soft floating point calling convention, every double
3025 // argument is passed using two registers.
3026 num_reg_arguments += 2 * num_double_arguments;
3027 }
Steve Block6ded16b2010-05-10 14:33:55 +01003028 // Up to four simple arguments are passed in registers r0..r3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003029 if (num_reg_arguments > kRegisterPassedArguments) {
3030 stack_passed_words += num_reg_arguments - kRegisterPassedArguments;
3031 }
3032 return stack_passed_words;
3033}
3034
3035
3036void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
3037 int num_double_arguments,
3038 Register scratch) {
3039 int frame_alignment = ActivationFrameAlignment();
3040 int stack_passed_arguments = CalculateStackPassedWords(
3041 num_reg_arguments, num_double_arguments);
Steve Block6ded16b2010-05-10 14:33:55 +01003042 if (frame_alignment > kPointerSize) {
3043 // Make stack end at alignment and make room for num_arguments - 4 words
3044 // and the original value of sp.
3045 mov(scratch, sp);
3046 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
3047 ASSERT(IsPowerOf2(frame_alignment));
3048 and_(sp, sp, Operand(-frame_alignment));
3049 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
3050 } else {
3051 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
3052 }
3053}
3054
3055
Ben Murdoch257744e2011-11-30 15:57:28 +00003056void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
3057 Register scratch) {
3058 PrepareCallCFunction(num_reg_arguments, 0, scratch);
3059}
3060
3061
3062void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg) {
3063 if (use_eabi_hardfloat()) {
3064 Move(d0, dreg);
3065 } else {
3066 vmov(r0, r1, dreg);
3067 }
3068}
3069
3070
3071void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg1,
3072 DoubleRegister dreg2) {
3073 if (use_eabi_hardfloat()) {
3074 if (dreg2.is(d0)) {
3075 ASSERT(!dreg1.is(d1));
3076 Move(d1, dreg2);
3077 Move(d0, dreg1);
3078 } else {
3079 Move(d0, dreg1);
3080 Move(d1, dreg2);
3081 }
3082 } else {
3083 vmov(r0, r1, dreg1);
3084 vmov(r2, r3, dreg2);
3085 }
3086}
3087
3088
3089void MacroAssembler::SetCallCDoubleArguments(DoubleRegister dreg,
3090 Register reg) {
3091 if (use_eabi_hardfloat()) {
3092 Move(d0, dreg);
3093 Move(r0, reg);
3094 } else {
3095 Move(r2, reg);
3096 vmov(r0, r1, dreg);
3097 }
3098}
3099
3100
3101void MacroAssembler::CallCFunction(ExternalReference function,
3102 int num_reg_arguments,
3103 int num_double_arguments) {
Ben Murdoch85b71792012-04-11 18:30:58 +01003104 CallCFunctionHelper(no_reg,
3105 function,
3106 ip,
3107 num_reg_arguments,
3108 num_double_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +00003109}
3110
3111
3112void MacroAssembler::CallCFunction(Register function,
Ben Murdoch85b71792012-04-11 18:30:58 +01003113 Register scratch,
3114 int num_reg_arguments,
3115 int num_double_arguments) {
3116 CallCFunctionHelper(function,
3117 ExternalReference::the_hole_value_location(isolate()),
3118 scratch,
3119 num_reg_arguments,
3120 num_double_arguments);
Ben Murdoch257744e2011-11-30 15:57:28 +00003121}
3122
3123
Steve Block6ded16b2010-05-10 14:33:55 +01003124void MacroAssembler::CallCFunction(ExternalReference function,
3125 int num_arguments) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003126 CallCFunction(function, num_arguments, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003127}
3128
Ben Murdoch257744e2011-11-30 15:57:28 +00003129
Steve Block44f0eee2011-05-26 01:26:41 +01003130void MacroAssembler::CallCFunction(Register function,
Ben Murdoch85b71792012-04-11 18:30:58 +01003131 Register scratch,
Steve Block44f0eee2011-05-26 01:26:41 +01003132 int num_arguments) {
Ben Murdoch85b71792012-04-11 18:30:58 +01003133 CallCFunction(function, scratch, num_arguments, 0);
Steve Block6ded16b2010-05-10 14:33:55 +01003134}
3135
3136
Steve Block44f0eee2011-05-26 01:26:41 +01003137void MacroAssembler::CallCFunctionHelper(Register function,
Ben Murdoch85b71792012-04-11 18:30:58 +01003138 ExternalReference function_reference,
3139 Register scratch,
Ben Murdoch257744e2011-11-30 15:57:28 +00003140 int num_reg_arguments,
3141 int num_double_arguments) {
Steve Block6ded16b2010-05-10 14:33:55 +01003142 // Make sure that the stack is aligned before calling a C function unless
3143 // running in the simulator. The simulator has its own alignment check which
3144 // provides more information.
3145#if defined(V8_HOST_ARCH_ARM)
Steve Block44f0eee2011-05-26 01:26:41 +01003146 if (emit_debug_code()) {
Steve Block6ded16b2010-05-10 14:33:55 +01003147 int frame_alignment = OS::ActivationFrameAlignment();
3148 int frame_alignment_mask = frame_alignment - 1;
3149 if (frame_alignment > kPointerSize) {
3150 ASSERT(IsPowerOf2(frame_alignment));
3151 Label alignment_as_expected;
3152 tst(sp, Operand(frame_alignment_mask));
3153 b(eq, &alignment_as_expected);
3154 // Don't use Check here, as it will call Runtime_Abort possibly
3155 // re-entering here.
3156 stop("Unexpected alignment");
3157 bind(&alignment_as_expected);
3158 }
3159 }
3160#endif
3161
3162 // Just call directly. The function called cannot cause a GC, or
3163 // allow preemption, so the return address in the link register
3164 // stays correct.
Ben Murdoch85b71792012-04-11 18:30:58 +01003165 if (function.is(no_reg)) {
3166 mov(scratch, Operand(function_reference));
3167 function = scratch;
3168 }
Steve Block6ded16b2010-05-10 14:33:55 +01003169 Call(function);
Ben Murdoch257744e2011-11-30 15:57:28 +00003170 int stack_passed_arguments = CalculateStackPassedWords(
3171 num_reg_arguments, num_double_arguments);
3172 if (ActivationFrameAlignment() > kPointerSize) {
Steve Block6ded16b2010-05-10 14:33:55 +01003173 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
3174 } else {
3175 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
3176 }
3177}
3178
3179
Steve Block1e0659c2011-05-24 12:43:12 +01003180void MacroAssembler::GetRelocatedValueLocation(Register ldr_location,
3181 Register result) {
3182 const uint32_t kLdrOffsetMask = (1 << 12) - 1;
3183 const int32_t kPCRegOffset = 2 * kPointerSize;
3184 ldr(result, MemOperand(ldr_location));
Steve Block44f0eee2011-05-26 01:26:41 +01003185 if (emit_debug_code()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003186 // Check that the instruction is a ldr reg, [pc + offset] .
3187 and_(result, result, Operand(kLdrPCPattern));
3188 cmp(result, Operand(kLdrPCPattern));
3189 Check(eq, "The instruction to patch should be a load from pc.");
3190 // Result was clobbered. Restore it.
3191 ldr(result, MemOperand(ldr_location));
3192 }
3193 // Get the address of the constant.
3194 and_(result, result, Operand(kLdrOffsetMask));
3195 add(result, ldr_location, Operand(result));
3196 add(result, result, Operand(kPCRegOffset));
3197}
3198
3199
Ben Murdoch257744e2011-11-30 15:57:28 +00003200void MacroAssembler::ClampUint8(Register output_reg, Register input_reg) {
3201 Usat(output_reg, 8, Operand(input_reg));
3202}
3203
3204
3205void MacroAssembler::ClampDoubleToUint8(Register result_reg,
3206 DoubleRegister input_reg,
3207 DoubleRegister temp_double_reg) {
3208 Label above_zero;
3209 Label done;
3210 Label in_bounds;
3211
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003212 Vmov(temp_double_reg, 0.0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003213 VFPCompareAndSetFlags(input_reg, temp_double_reg);
3214 b(gt, &above_zero);
3215
3216 // Double value is less than zero, NaN or Inf, return 0.
3217 mov(result_reg, Operand(0));
3218 b(al, &done);
3219
3220 // Double value is >= 255, return 255.
3221 bind(&above_zero);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003222 Vmov(temp_double_reg, 255.0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003223 VFPCompareAndSetFlags(input_reg, temp_double_reg);
3224 b(le, &in_bounds);
3225 mov(result_reg, Operand(255));
3226 b(al, &done);
3227
3228 // In 0-255 range, round and truncate.
3229 bind(&in_bounds);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003230 Vmov(temp_double_reg, 0.5);
Ben Murdoch257744e2011-11-30 15:57:28 +00003231 vadd(temp_double_reg, input_reg, temp_double_reg);
Ben Murdoch85b71792012-04-11 18:30:58 +01003232 vcvt_u32_f64(s0, temp_double_reg);
3233 vmov(result_reg, s0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003234 bind(&done);
3235}
3236
3237
3238void MacroAssembler::LoadInstanceDescriptors(Register map,
3239 Register descriptors) {
3240 ldr(descriptors,
3241 FieldMemOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
3242 Label not_smi;
3243 JumpIfNotSmi(descriptors, &not_smi);
3244 mov(descriptors, Operand(FACTORY->empty_descriptor_array()));
3245 bind(&not_smi);
3246}
3247
3248
Steve Blocka7e24c12009-10-30 11:49:00 +00003249CodePatcher::CodePatcher(byte* address, int instructions)
3250 : address_(address),
3251 instructions_(instructions),
3252 size_(instructions * Assembler::kInstrSize),
Ben Murdoch8b112d22011-06-08 16:22:53 +01003253 masm_(Isolate::Current(), address, size_ + Assembler::kGap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003254 // Create a new macro assembler pointing to the address of the code to patch.
3255 // The size is adjusted with kGap on order for the assembler to generate size
3256 // bytes of instructions without failing with buffer size constraints.
3257 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
3258}
3259
3260
3261CodePatcher::~CodePatcher() {
3262 // Indicate that code has changed.
3263 CPU::FlushICache(address_, size_);
3264
3265 // Check that the code was patched as expected.
3266 ASSERT(masm_.pc_ == address_ + size_);
3267 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
3268}
3269
3270
Steve Block1e0659c2011-05-24 12:43:12 +01003271void CodePatcher::Emit(Instr instr) {
3272 masm()->emit(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003273}
3274
3275
3276void CodePatcher::Emit(Address addr) {
3277 masm()->emit(reinterpret_cast<Instr>(addr));
3278}
Steve Block1e0659c2011-05-24 12:43:12 +01003279
3280
3281void CodePatcher::EmitCondition(Condition cond) {
3282 Instr instr = Assembler::instr_at(masm_.pc_);
3283 instr = (instr & ~kCondMask) | cond;
3284 masm_.emit(instr);
3285}
Steve Blocka7e24c12009-10-30 11:49:00 +00003286
3287
3288} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01003289
3290#endif // V8_TARGET_ARCH_ARM