blob: 9bbc31f7739dc36aed50a9d7de08ca9139ea4b93 [file] [log] [blame]
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
34#include "debug.h"
35#include "runtime.h"
36
kasperl@chromium.org71affb52009-05-26 05:44:31 +000037namespace v8 {
38namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040MacroAssembler::MacroAssembler(void* buffer, int size)
41 : Assembler(buffer, size),
kasper.lund7276f142008-07-30 08:49:36 +000042 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000043 allow_stub_calls_(true),
44 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045}
46
47
48// We always generate arm code, never thumb code, even if V8 is compiled to
49// thumb, so we require inter-working support
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000050#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051#error "flag -mthumb-interwork missing"
52#endif
53
54
55// We do not support thumb inter-working with an arm architecture not supporting
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000056// the blx instruction (below v5t). If you know what CPU you are compiling for
57// you can use -march=armv7 or similar.
58#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
59# error "For thumb inter-working we require an architecture which supports blx"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060#endif
61
62
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063// Using bx does not yield better code, so use it only when required
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000064#if defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065#define USE_BX 1
66#endif
67
68
69void MacroAssembler::Jump(Register target, Condition cond) {
70#if USE_BX
71 bx(target, cond);
72#else
73 mov(pc, Operand(target), LeaveCC, cond);
74#endif
75}
76
77
ager@chromium.org236ad962008-09-25 09:45:57 +000078void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
79 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080#if USE_BX
81 mov(ip, Operand(target, rmode), LeaveCC, cond);
82 bx(ip, cond);
83#else
84 mov(pc, Operand(target, rmode), LeaveCC, cond);
85#endif
86}
87
88
ager@chromium.org236ad962008-09-25 09:45:57 +000089void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
90 Condition cond) {
91 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
93}
94
95
ager@chromium.org236ad962008-09-25 09:45:57 +000096void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
97 Condition cond) {
98 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 // 'code' is always generated ARM code, never THUMB code
100 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
101}
102
103
104void MacroAssembler::Call(Register target, Condition cond) {
105#if USE_BLX
106 blx(target, cond);
107#else
108 // set lr for return at current pc + 8
109 mov(lr, Operand(pc), LeaveCC, cond);
110 mov(pc, Operand(target), LeaveCC, cond);
111#endif
112}
113
114
ager@chromium.org236ad962008-09-25 09:45:57 +0000115void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
116 Condition cond) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000117#if USE_BLX
118 // On ARMv5 and after the recommended call sequence is:
119 // ldr ip, [pc, #...]
120 // blx ip
121
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000122 // The two instructions (ldr and blx) could be separated by a constant
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000123 // pool and the code would still work. The issue comes from the
124 // patching code which expect the ldr to be just above the blx.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000125 { BlockConstPoolScope block_const_pool(this);
126 // Statement positions are expected to be recorded when the target
127 // address is loaded. The mov method will automatically record
128 // positions when pc is the target, since this is not the case here
129 // we have to do it explicitly.
130 WriteRecordedPositions();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000131
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000132 mov(ip, Operand(target, rmode), LeaveCC, cond);
133 blx(ip, cond);
134 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000135
136 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
137#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 // Set lr for return at current pc + 8.
139 mov(lr, Operand(pc), LeaveCC, cond);
140 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
141 mov(pc, Operand(target, rmode), LeaveCC, cond);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000142
ager@chromium.org4af710e2009-09-15 12:20:11 +0000143 ASSERT(kCallTargetAddressOffset == kInstrSize);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000144#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145}
146
147
ager@chromium.org236ad962008-09-25 09:45:57 +0000148void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
149 Condition cond) {
150 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
152}
153
154
ager@chromium.org236ad962008-09-25 09:45:57 +0000155void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
156 Condition cond) {
157 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 // 'code' is always generated ARM code, never THUMB code
159 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
160}
161
162
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000163void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000165 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000167 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168#endif
169}
170
171
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000172void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
173 LoadRoot(ip, Heap::kStackLimitRootIndex);
174 cmp(sp, Operand(ip));
175 b(lo, on_stack_overflow);
176}
177
178
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000179void MacroAssembler::Drop(int count, Condition cond) {
180 if (count > 0) {
181 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000182 }
183}
184
185
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000186void MacroAssembler::Swap(Register reg1,
187 Register reg2,
188 Register scratch,
189 Condition cond) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000190 if (scratch.is(no_reg)) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000191 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
192 eor(reg2, reg2, Operand(reg1), LeaveCC, cond);
193 eor(reg1, reg1, Operand(reg2), LeaveCC, cond);
ager@chromium.org357bf652010-04-12 11:30:10 +0000194 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000195 mov(scratch, reg1, LeaveCC, cond);
196 mov(reg1, reg2, LeaveCC, cond);
197 mov(reg2, scratch, LeaveCC, cond);
ager@chromium.org357bf652010-04-12 11:30:10 +0000198 }
199}
200
201
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000202void MacroAssembler::Call(Label* target) {
203 bl(target);
204}
205
206
207void MacroAssembler::Move(Register dst, Handle<Object> value) {
208 mov(dst, Operand(value));
209}
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000210
211
ager@chromium.org357bf652010-04-12 11:30:10 +0000212void MacroAssembler::Move(Register dst, Register src) {
213 if (!dst.is(src)) {
214 mov(dst, src);
215 }
216}
217
218
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000219void MacroAssembler::And(Register dst, Register src1, const Operand& src2,
220 Condition cond) {
221 if (!CpuFeatures::IsSupported(ARMv7) || src2.is_single_instruction()) {
222 and_(dst, src1, src2, LeaveCC, cond);
223 return;
224 }
225 int32_t immediate = src2.immediate();
226 if (immediate == 0) {
227 mov(dst, Operand(0), LeaveCC, cond);
228 return;
229 }
230 if (IsPowerOf2(immediate + 1) && ((immediate & 1) != 0)) {
231 ubfx(dst, src1, 0, WhichPowerOf2(immediate + 1), cond);
232 return;
233 }
234 and_(dst, src1, src2, LeaveCC, cond);
235}
236
237
238void MacroAssembler::Ubfx(Register dst, Register src1, int lsb, int width,
239 Condition cond) {
240 ASSERT(lsb < 32);
241 if (!CpuFeatures::IsSupported(ARMv7)) {
242 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
243 and_(dst, src1, Operand(mask), LeaveCC, cond);
244 if (lsb != 0) {
245 mov(dst, Operand(dst, LSR, lsb), LeaveCC, cond);
246 }
247 } else {
248 ubfx(dst, src1, lsb, width, cond);
249 }
250}
251
252
253void MacroAssembler::Sbfx(Register dst, Register src1, int lsb, int width,
254 Condition cond) {
255 ASSERT(lsb < 32);
256 if (!CpuFeatures::IsSupported(ARMv7)) {
257 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
258 and_(dst, src1, Operand(mask), LeaveCC, cond);
259 int shift_up = 32 - lsb - width;
260 int shift_down = lsb + shift_up;
261 if (shift_up != 0) {
262 mov(dst, Operand(dst, LSL, shift_up), LeaveCC, cond);
263 }
264 if (shift_down != 0) {
265 mov(dst, Operand(dst, ASR, shift_down), LeaveCC, cond);
266 }
267 } else {
268 sbfx(dst, src1, lsb, width, cond);
269 }
270}
271
272
ager@chromium.org8bb60582008-12-11 12:02:20 +0000273void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
274 // Empty the const pool.
275 CheckConstPool(true, true);
276 add(pc, pc, Operand(index,
277 LSL,
278 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000279 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000280 nop(); // Jump table alignment.
281 for (int i = 0; i < targets.length(); i++) {
282 b(targets[i]);
283 }
284}
285
286
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000287void MacroAssembler::LoadRoot(Register destination,
288 Heap::RootListIndex index,
289 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000290 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000291}
292
293
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000294void MacroAssembler::StoreRoot(Register source,
295 Heap::RootListIndex index,
296 Condition cond) {
297 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
298}
299
300
ager@chromium.orgac091b72010-05-05 07:34:42 +0000301void MacroAssembler::RecordWriteHelper(Register object,
302 Register offset,
303 Register scratch) {
304 if (FLAG_debug_code) {
305 // Check that the object is not in new space.
306 Label not_in_new_space;
307 InNewSpace(object, scratch, ne, &not_in_new_space);
308 Abort("new-space object passed to RecordWriteHelper");
309 bind(&not_in_new_space);
310 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000312 mov(ip, Operand(Page::kPageAlignmentMask)); // Load mask only once.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000314 // Calculate region number.
315 add(offset, object, Operand(offset)); // Add offset into the object.
316 and_(offset, offset, Operand(ip)); // Offset into page of the object.
317 mov(offset, Operand(offset, LSR, Page::kRegionSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000319 // Calculate page address.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 bic(object, object, Operand(ip));
321
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000322 // Mark region dirty.
323 ldr(scratch, MemOperand(object, Page::kDirtyFlagOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 mov(ip, Operand(1));
325 orr(scratch, scratch, Operand(ip, LSL, offset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000326 str(scratch, MemOperand(object, Page::kDirtyFlagOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000327}
328
329
330void MacroAssembler::InNewSpace(Register object,
331 Register scratch,
332 Condition cc,
333 Label* branch) {
334 ASSERT(cc == eq || cc == ne);
335 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
336 cmp(scratch, Operand(ExternalReference::new_space_start()));
337 b(cc, branch);
338}
339
340
341// Will clobber 4 registers: object, offset, scratch, ip. The
342// register 'object' contains a heap object pointer. The heap object
343// tag is shifted away.
344void MacroAssembler::RecordWrite(Register object, Register offset,
345 Register scratch) {
346 // The compiled code assumes that record write doesn't change the
347 // context register, so we check that none of the clobbered
348 // registers are cp.
349 ASSERT(!object.is(cp) && !offset.is(cp) && !scratch.is(cp));
350
351 Label done;
352
353 // First, test that the object is not in the new space. We cannot set
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000354 // region marks for new space pages.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000355 InNewSpace(object, scratch, eq, &done);
356
357 // Record the actual write.
358 RecordWriteHelper(object, offset, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359
360 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000361
362 // Clobber all input registers when running with the debug-code flag
363 // turned on to provoke errors.
364 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000365 mov(object, Operand(BitCast<int32_t>(kZapValue)));
366 mov(offset, Operand(BitCast<int32_t>(kZapValue)));
367 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000368 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369}
370
371
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000372void MacroAssembler::Ldrd(Register dst1, Register dst2,
373 const MemOperand& src, Condition cond) {
374 ASSERT(src.rm().is(no_reg));
375 ASSERT(!dst1.is(lr)); // r14.
376 ASSERT_EQ(0, dst1.code() % 2);
377 ASSERT_EQ(dst1.code() + 1, dst2.code());
378
379 // Generate two ldr instructions if ldrd is not available.
380 if (CpuFeatures::IsSupported(ARMv7)) {
381 CpuFeatures::Scope scope(ARMv7);
382 ldrd(dst1, dst2, src, cond);
383 } else {
384 MemOperand src2(src);
385 src2.set_offset(src2.offset() + 4);
386 if (dst1.is(src.rn())) {
387 ldr(dst2, src2, cond);
388 ldr(dst1, src, cond);
389 } else {
390 ldr(dst1, src, cond);
391 ldr(dst2, src2, cond);
392 }
393 }
394}
395
396
397void MacroAssembler::Strd(Register src1, Register src2,
398 const MemOperand& dst, Condition cond) {
399 ASSERT(dst.rm().is(no_reg));
400 ASSERT(!src1.is(lr)); // r14.
401 ASSERT_EQ(0, src1.code() % 2);
402 ASSERT_EQ(src1.code() + 1, src2.code());
403
404 // Generate two str instructions if strd is not available.
405 if (CpuFeatures::IsSupported(ARMv7)) {
406 CpuFeatures::Scope scope(ARMv7);
407 strd(src1, src2, dst, cond);
408 } else {
409 MemOperand dst2(dst);
410 dst2.set_offset(dst2.offset() + 4);
411 str(src1, dst, cond);
412 str(src2, dst2, cond);
413 }
414}
415
416
ager@chromium.org7c537e22008-10-16 08:43:32 +0000417void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000418 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000419 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
420 mov(ip, Operand(Smi::FromInt(type)));
421 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000422 mov(ip, Operand(CodeObject()));
423 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000424 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425}
426
427
ager@chromium.org7c537e22008-10-16 08:43:32 +0000428void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000429 // r0: preserved
430 // r1: preserved
431 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432
ager@chromium.org7c537e22008-10-16 08:43:32 +0000433 // Drop the execution stack down to the frame pointer and restore
434 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000435 mov(sp, fp);
436 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437}
438
439
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000440void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000441 // Compute the argv pointer and keep it in a callee-saved register.
442 // r0 is argc.
443 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
444 sub(r6, r6, Operand(kPointerSize));
445
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000446 // Compute callee's stack pointer before making changes and save it as
447 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000448 // popping the args.
449
450 // ip = sp + kPointerSize * #args;
451 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
452
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000453 // Prepare the stack to be aligned when calling into C. After this point there
454 // are 5 pushes before the call into C, so the stack needs to be aligned after
455 // 5 pushes.
456 int frame_alignment = ActivationFrameAlignment();
457 int frame_alignment_mask = frame_alignment - 1;
458 if (frame_alignment != kPointerSize) {
459 // The following code needs to be more general if this assert does not hold.
460 ASSERT(frame_alignment == 2 * kPointerSize);
461 // With 5 pushes left the frame must be unaligned at this point.
462 mov(r7, Operand(Smi::FromInt(0)));
463 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
464 push(r7, eq); // Push if aligned to make it unaligned.
465 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000466
ager@chromium.org236ad962008-09-25 09:45:57 +0000467 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
468 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +0000469 mov(fp, Operand(sp)); // Setup new frame pointer.
ager@chromium.org236ad962008-09-25 09:45:57 +0000470
ager@chromium.org5c838252010-02-19 08:53:10 +0000471 mov(ip, Operand(CodeObject()));
472 push(ip); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000473
474 // Save the frame pointer and the context in top.
475 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
476 str(fp, MemOperand(ip));
477 mov(ip, Operand(ExternalReference(Top::k_context_address)));
478 str(cp, MemOperand(ip));
479
480 // Setup argc and the builtin function in callee-saved registers.
481 mov(r4, Operand(r0));
482 mov(r5, Operand(r1));
483
ager@chromium.org236ad962008-09-25 09:45:57 +0000484
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000485#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000486 // Save the state of all registers to the stack from the memory
487 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000488 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000489 // Use sp as base to push.
490 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
491 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000492#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000493}
494
495
ager@chromium.orgac091b72010-05-05 07:34:42 +0000496void MacroAssembler::InitializeNewString(Register string,
497 Register length,
498 Heap::RootListIndex map_index,
499 Register scratch1,
500 Register scratch2) {
501 mov(scratch1, Operand(length, LSL, kSmiTagSize));
502 LoadRoot(scratch2, map_index);
503 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
504 mov(scratch1, Operand(String::kEmptyHashField));
505 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
506 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
507}
508
509
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000510int MacroAssembler::ActivationFrameAlignment() {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000511#if defined(V8_HOST_ARCH_ARM)
512 // Running on the real platform. Use the alignment as mandated by the local
513 // environment.
514 // Note: This will break if we ever start generating snapshots on one ARM
515 // platform for another ARM platform with a different alignment.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000516 return OS::ActivationFrameAlignment();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000517#else // defined(V8_HOST_ARCH_ARM)
518 // If we are using the simulator then we should always align to the expected
519 // alignment. As the simulator is used to generate snapshots we do not know
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000520 // if the target platform will need alignment, so this is controlled from a
521 // flag.
522 return FLAG_sim_stack_alignment;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000523#endif // defined(V8_HOST_ARCH_ARM)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000524}
525
526
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000527void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000528#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000529 // Restore the memory copy of the registers by digging them out from
530 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000531 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000532 // This code intentionally clobbers r2 and r3.
533 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000534 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000535 add(r3, fp, Operand(kOffset));
536 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
537 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000538#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000539
540 // Clear top frame.
541 mov(r3, Operand(0));
542 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
543 str(r3, MemOperand(ip));
544
545 // Restore current context from top and clear it in debug mode.
546 mov(ip, Operand(ExternalReference(Top::k_context_address)));
547 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000548#ifdef DEBUG
549 str(r3, MemOperand(ip));
550#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000551
552 // Pop the arguments, restore registers, and return.
553 mov(sp, Operand(fp)); // respect ABI stack constraint
554 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
555}
556
557
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000558void MacroAssembler::InvokePrologue(const ParameterCount& expected,
559 const ParameterCount& actual,
560 Handle<Code> code_constant,
561 Register code_reg,
562 Label* done,
563 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000564 bool definitely_matches = false;
565 Label regular_invoke;
566
567 // Check whether the expected and actual arguments count match. If not,
568 // setup registers according to contract with ArgumentsAdaptorTrampoline:
569 // r0: actual arguments count
570 // r1: function (passed through to callee)
571 // r2: expected arguments count
572 // r3: callee code entry
573
574 // The code below is made a lot easier because the calling code already sets
575 // up actual and expected registers according to the contract if values are
576 // passed in registers.
577 ASSERT(actual.is_immediate() || actual.reg().is(r0));
578 ASSERT(expected.is_immediate() || expected.reg().is(r2));
579 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
580
581 if (expected.is_immediate()) {
582 ASSERT(actual.is_immediate());
583 if (expected.immediate() == actual.immediate()) {
584 definitely_matches = true;
585 } else {
586 mov(r0, Operand(actual.immediate()));
587 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
588 if (expected.immediate() == sentinel) {
589 // Don't worry about adapting arguments for builtins that
590 // don't want that done. Skip adaption code by making it look
591 // like we have a match between expected and actual number of
592 // arguments.
593 definitely_matches = true;
594 } else {
595 mov(r2, Operand(expected.immediate()));
596 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000597 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000598 } else {
599 if (actual.is_immediate()) {
600 cmp(expected.reg(), Operand(actual.immediate()));
601 b(eq, &regular_invoke);
602 mov(r0, Operand(actual.immediate()));
603 } else {
604 cmp(expected.reg(), Operand(actual.reg()));
605 b(eq, &regular_invoke);
606 }
607 }
608
609 if (!definitely_matches) {
610 if (!code_constant.is_null()) {
611 mov(r3, Operand(code_constant));
612 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
613 }
614
615 Handle<Code> adaptor =
616 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
617 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000618 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000619 b(done);
620 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000621 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000622 }
623 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000624 }
625}
626
627
628void MacroAssembler::InvokeCode(Register code,
629 const ParameterCount& expected,
630 const ParameterCount& actual,
631 InvokeFlag flag) {
632 Label done;
633
634 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
635 if (flag == CALL_FUNCTION) {
636 Call(code);
637 } else {
638 ASSERT(flag == JUMP_FUNCTION);
639 Jump(code);
640 }
641
642 // Continue here if InvokePrologue does handle the invocation due to
643 // mismatched parameter counts.
644 bind(&done);
645}
646
647
648void MacroAssembler::InvokeCode(Handle<Code> code,
649 const ParameterCount& expected,
650 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000651 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000652 InvokeFlag flag) {
653 Label done;
654
655 InvokePrologue(expected, actual, code, no_reg, &done, flag);
656 if (flag == CALL_FUNCTION) {
657 Call(code, rmode);
658 } else {
659 Jump(code, rmode);
660 }
661
662 // Continue here if InvokePrologue does handle the invocation due to
663 // mismatched parameter counts.
664 bind(&done);
665}
666
667
668void MacroAssembler::InvokeFunction(Register fun,
669 const ParameterCount& actual,
670 InvokeFlag flag) {
671 // Contract with called JS functions requires that function is passed in r1.
672 ASSERT(fun.is(r1));
673
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000674 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000675 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000676
677 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
678 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
679 ldr(expected_reg,
680 FieldMemOperand(code_reg,
681 SharedFunctionInfo::kFormalParameterCountOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000682 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000683 ldr(code_reg,
684 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
685 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
686
687 ParameterCount expected(expected_reg);
688 InvokeCode(code_reg, expected, actual, flag);
689}
690
691
ager@chromium.org5c838252010-02-19 08:53:10 +0000692void MacroAssembler::InvokeFunction(JSFunction* function,
693 const ParameterCount& actual,
694 InvokeFlag flag) {
695 ASSERT(function->is_compiled());
696
697 // Get the function and setup the context.
698 mov(r1, Operand(Handle<JSFunction>(function)));
699 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
700
701 // Invoke the cached code.
702 Handle<Code> code(function->code());
703 ParameterCount expected(function->shared()->formal_parameter_count());
704 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
705}
706
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000707#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708void MacroAssembler::SaveRegistersToMemory(RegList regs) {
709 ASSERT((regs & ~kJSCallerSaved) == 0);
710 // Copy the content of registers to memory location.
711 for (int i = 0; i < kNumJSCallerSaved; i++) {
712 int r = JSCallerSavedCode(i);
713 if ((regs & (1 << r)) != 0) {
714 Register reg = { r };
715 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
716 str(reg, MemOperand(ip));
717 }
718 }
719}
720
721
722void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
723 ASSERT((regs & ~kJSCallerSaved) == 0);
724 // Copy the content of memory location to registers.
725 for (int i = kNumJSCallerSaved; --i >= 0;) {
726 int r = JSCallerSavedCode(i);
727 if ((regs & (1 << r)) != 0) {
728 Register reg = { r };
729 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
730 ldr(reg, MemOperand(ip));
731 }
732 }
733}
734
735
736void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
737 RegList regs) {
738 ASSERT((regs & ~kJSCallerSaved) == 0);
739 // Copy the content of the memory location to the stack and adjust base.
740 for (int i = kNumJSCallerSaved; --i >= 0;) {
741 int r = JSCallerSavedCode(i);
742 if ((regs & (1 << r)) != 0) {
743 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
744 ldr(ip, MemOperand(ip));
745 str(ip, MemOperand(base, 4, NegPreIndex));
746 }
747 }
748}
749
750
751void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
752 Register scratch,
753 RegList regs) {
754 ASSERT((regs & ~kJSCallerSaved) == 0);
755 // Copy the content of the stack to the memory location and adjust base.
756 for (int i = 0; i < kNumJSCallerSaved; i++) {
757 int r = JSCallerSavedCode(i);
758 if ((regs & (1 << r)) != 0) {
759 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
760 ldr(scratch, MemOperand(base, 4, PostIndex));
761 str(scratch, MemOperand(ip));
762 }
763 }
764}
ager@chromium.org5c838252010-02-19 08:53:10 +0000765
766
767void MacroAssembler::DebugBreak() {
768 ASSERT(allow_stub_calls());
769 mov(r0, Operand(0));
770 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
771 CEntryStub ces(1);
772 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
773}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000774#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777void MacroAssembler::PushTryHandler(CodeLocation try_location,
778 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000779 // Adjust this code if not the case.
780 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 // The pc (return address) is passed in register lr.
782 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783 if (type == TRY_CATCH_HANDLER) {
784 mov(r3, Operand(StackHandler::TRY_CATCH));
785 } else {
786 mov(r3, Operand(StackHandler::TRY_FINALLY));
787 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000788 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
789 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
790 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
791 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
792 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
794 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000795 ASSERT(StackHandlerConstants::kNextOffset == 0);
796 push(r1);
797 // Link this handler as the new current one.
798 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000800 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000802 // The frame pointer does not point to a JS frame so we save NULL
803 // for fp. We expect the code throwing an exception to check fp
804 // before dereferencing it to restore the context.
805 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000807 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
808 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
809 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
810 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
811 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
813 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000814 ASSERT(StackHandlerConstants::kNextOffset == 0);
815 push(r6);
816 // Link this handler as the new current one.
817 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 }
819}
820
821
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000822void MacroAssembler::PopTryHandler() {
823 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
824 pop(r1);
825 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
826 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
827 str(r1, MemOperand(ip));
828}
829
830
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
832 JSObject* holder, Register holder_reg,
833 Register scratch,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000834 int save_at_depth,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 Label* miss) {
836 // Make sure there's no overlap between scratch and the other
837 // registers.
838 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
839
840 // Keep track of the current object in register reg.
841 Register reg = object_reg;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000842 int depth = 0;
843
844 if (save_at_depth == depth) {
845 str(reg, MemOperand(sp));
846 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847
848 // Check the maps in the prototype chain.
849 // Traverse the prototype chain from the object and do map checks.
850 while (object != holder) {
851 depth++;
852
853 // Only global objects and objects that do not require access
854 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000855 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 // Get the map of the current object.
858 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
859 cmp(scratch, Operand(Handle<Map>(object->map())));
860
861 // Branch on the result of the map check.
862 b(ne, miss);
863
864 // Check access rights to the global object. This has to happen
865 // after the map check so that we know that the object is
866 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000867 if (object->IsJSGlobalProxy()) {
868 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 // Restore scratch register to be the map of the object. In the
870 // new space case below, we load the prototype from the map in
871 // the scratch register.
872 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
873 }
874
875 reg = holder_reg; // from now the object is in holder_reg
876 JSObject* prototype = JSObject::cast(object->GetPrototype());
877 if (Heap::InNewSpace(prototype)) {
878 // The prototype is in new space; we cannot store a reference
879 // to it in the code. Load it from the map.
880 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
881 } else {
882 // The prototype is in old space; load it directly.
883 mov(reg, Operand(Handle<JSObject>(prototype)));
884 }
885
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000886 if (save_at_depth == depth) {
887 str(reg, MemOperand(sp));
888 }
889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 // Go to the next object in the prototype chain.
891 object = prototype;
892 }
893
894 // Check the holder map.
895 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
896 cmp(scratch, Operand(Handle<Map>(object->map())));
897 b(ne, miss);
898
899 // Log the check depth.
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000900 LOG(IntEvent("check-maps-depth", depth + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
902 // Perform security check for access to the global object and return
903 // the holder register.
904 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000905 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
906 if (object->IsJSGlobalProxy()) {
907 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 }
909 return reg;
910}
911
912
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000913void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
914 Register scratch,
915 Label* miss) {
916 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000918 ASSERT(!holder_reg.is(scratch));
919 ASSERT(!holder_reg.is(ip));
920 ASSERT(!scratch.is(ip));
921
922 // Load current lexical context from the stack frame.
923 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
924 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000925#ifdef DEBUG
926 cmp(scratch, Operand(0));
927 Check(ne, "we should not have an empty lexical context");
928#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000930 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
932 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000933 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
934
935 // Check the context is a global context.
936 if (FLAG_debug_code) {
937 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
938 // Cannot use ip as a temporary in this verification code. Due to the fact
939 // that ip is clobbered as part of cmp with an object Operand.
940 push(holder_reg); // Temporarily save holder on the stack.
941 // Read the first word and compare to the global_context_map.
942 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000943 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
944 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000945 Check(eq, "JSGlobalObject::global_context should be a global context.");
946 pop(holder_reg); // Restore holder.
947 }
948
949 // Check if both contexts are the same.
950 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
951 cmp(scratch, Operand(ip));
952 b(eq, &same_contexts);
953
954 // Check the context is a global context.
955 if (FLAG_debug_code) {
956 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
957 // Cannot use ip as a temporary in this verification code. Due to the fact
958 // that ip is clobbered as part of cmp with an object Operand.
959 push(holder_reg); // Temporarily save holder on the stack.
960 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000961 LoadRoot(ip, Heap::kNullValueRootIndex);
962 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000963 Check(ne, "JSGlobalProxy::context() should not be null.");
964
965 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000966 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
967 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000968 Check(eq, "JSGlobalObject::global_context should be a global context.");
969 // Restore ip is not needed. ip is reloaded below.
970 pop(holder_reg); // Restore holder.
971 // Restore ip to holder's context.
972 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
973 }
974
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 // Check that the security token in the calling global object is
976 // compatible with the security token in the receiving global
977 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000978 int token_offset = Context::kHeaderSize +
979 Context::SECURITY_TOKEN_INDEX * kPointerSize;
980
981 ldr(scratch, FieldMemOperand(scratch, token_offset));
982 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 cmp(scratch, Operand(ip));
984 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000985
986 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987}
988
989
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000990void MacroAssembler::AllocateInNewSpace(int object_size,
991 Register result,
992 Register scratch1,
993 Register scratch2,
994 Label* gc_required,
995 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000996 ASSERT(!result.is(scratch1));
997 ASSERT(!scratch1.is(scratch2));
998
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000999 // Make object size into bytes.
1000 if ((flags & SIZE_IN_WORDS) != 0) {
1001 object_size *= kPointerSize;
1002 }
1003 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1004
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001005 // Load address of new object into result and allocation top address into
1006 // scratch1.
1007 ExternalReference new_space_allocation_top =
1008 ExternalReference::new_space_allocation_top_address();
1009 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001010 if ((flags & RESULT_CONTAINS_TOP) == 0) {
1011 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001012 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001013 // Assert that result actually contains top on entry. scratch2 is used
1014 // immediately below so this use of scratch2 does not cause difference with
1015 // respect to register content between debug and release mode.
1016 ldr(scratch2, MemOperand(scratch1));
1017 cmp(result, scratch2);
1018 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +00001019 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001020
1021 // Calculate new top and bail out if new space is exhausted. Use result
1022 // to calculate the new top.
1023 ExternalReference new_space_allocation_limit =
1024 ExternalReference::new_space_allocation_limit_address();
1025 mov(scratch2, Operand(new_space_allocation_limit));
1026 ldr(scratch2, MemOperand(scratch2));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001027 add(result, result, Operand(object_size));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001028 cmp(result, Operand(scratch2));
1029 b(hi, gc_required);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001030 str(result, MemOperand(scratch1));
1031
1032 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001033 if ((flags & TAG_OBJECT) != 0) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001034 sub(result, result, Operand(object_size - kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001035 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001036 sub(result, result, Operand(object_size));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001037 }
1038}
1039
1040
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001041void MacroAssembler::AllocateInNewSpace(Register object_size,
1042 Register result,
1043 Register scratch1,
1044 Register scratch2,
1045 Label* gc_required,
1046 AllocationFlags flags) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001047 ASSERT(!result.is(scratch1));
1048 ASSERT(!scratch1.is(scratch2));
1049
1050 // Load address of new object into result and allocation top address into
1051 // scratch1.
1052 ExternalReference new_space_allocation_top =
1053 ExternalReference::new_space_allocation_top_address();
1054 mov(scratch1, Operand(new_space_allocation_top));
1055 if ((flags & RESULT_CONTAINS_TOP) == 0) {
1056 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001057 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001058 // Assert that result actually contains top on entry. scratch2 is used
1059 // immediately below so this use of scratch2 does not cause difference with
1060 // respect to register content between debug and release mode.
1061 ldr(scratch2, MemOperand(scratch1));
1062 cmp(result, scratch2);
1063 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +00001064 }
1065
1066 // Calculate new top and bail out if new space is exhausted. Use result
1067 // to calculate the new top. Object size is in words so a shift is required to
1068 // get the number of bytes
1069 ExternalReference new_space_allocation_limit =
1070 ExternalReference::new_space_allocation_limit_address();
1071 mov(scratch2, Operand(new_space_allocation_limit));
1072 ldr(scratch2, MemOperand(scratch2));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001073 if ((flags & SIZE_IN_WORDS) != 0) {
1074 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1075 } else {
1076 add(result, result, Operand(object_size));
1077 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001078 cmp(result, Operand(scratch2));
1079 b(hi, gc_required);
1080
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001081 // Update allocation top. result temporarily holds the new top.
1082 if (FLAG_debug_code) {
1083 tst(result, Operand(kObjectAlignmentMask));
1084 Check(eq, "Unaligned allocation in new space");
1085 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001086 str(result, MemOperand(scratch1));
1087
1088 // Adjust back to start of new object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001089 if ((flags & SIZE_IN_WORDS) != 0) {
1090 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1091 } else {
1092 sub(result, result, Operand(object_size));
1093 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001094
1095 // Tag object if requested.
1096 if ((flags & TAG_OBJECT) != 0) {
1097 add(result, result, Operand(kHeapObjectTag));
1098 }
1099}
1100
1101
1102void MacroAssembler::UndoAllocationInNewSpace(Register object,
1103 Register scratch) {
1104 ExternalReference new_space_allocation_top =
1105 ExternalReference::new_space_allocation_top_address();
1106
1107 // Make sure the object has no tag before resetting top.
1108 and_(object, object, Operand(~kHeapObjectTagMask));
1109#ifdef DEBUG
1110 // Check that the object un-allocated is below the current top.
1111 mov(scratch, Operand(new_space_allocation_top));
1112 ldr(scratch, MemOperand(scratch));
1113 cmp(object, scratch);
1114 Check(lt, "Undo allocation of non allocated memory");
1115#endif
1116 // Write the address of the object to un-allocate as the current top.
1117 mov(scratch, Operand(new_space_allocation_top));
1118 str(object, MemOperand(scratch));
1119}
1120
1121
ager@chromium.org5c838252010-02-19 08:53:10 +00001122void MacroAssembler::AllocateTwoByteString(Register result,
1123 Register length,
1124 Register scratch1,
1125 Register scratch2,
1126 Register scratch3,
1127 Label* gc_required) {
1128 // Calculate the number of bytes needed for the characters in the string while
1129 // observing object alignment.
1130 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1131 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1132 add(scratch1, scratch1,
1133 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001134 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
ager@chromium.org5c838252010-02-19 08:53:10 +00001135
1136 // Allocate two-byte string in new space.
1137 AllocateInNewSpace(scratch1,
1138 result,
1139 scratch2,
1140 scratch3,
1141 gc_required,
1142 TAG_OBJECT);
1143
1144 // Set the map, length and hash field.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001145 InitializeNewString(result,
1146 length,
1147 Heap::kStringMapRootIndex,
1148 scratch1,
1149 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001150}
1151
1152
1153void MacroAssembler::AllocateAsciiString(Register result,
1154 Register length,
1155 Register scratch1,
1156 Register scratch2,
1157 Register scratch3,
1158 Label* gc_required) {
1159 // Calculate the number of bytes needed for the characters in the string while
1160 // observing object alignment.
1161 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1162 ASSERT(kCharSize == 1);
1163 add(scratch1, length,
1164 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001165 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
ager@chromium.org5c838252010-02-19 08:53:10 +00001166
1167 // Allocate ASCII string in new space.
1168 AllocateInNewSpace(scratch1,
1169 result,
1170 scratch2,
1171 scratch3,
1172 gc_required,
1173 TAG_OBJECT);
1174
1175 // Set the map, length and hash field.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001176 InitializeNewString(result,
1177 length,
1178 Heap::kAsciiStringMapRootIndex,
1179 scratch1,
1180 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001181}
1182
1183
1184void MacroAssembler::AllocateTwoByteConsString(Register result,
1185 Register length,
1186 Register scratch1,
1187 Register scratch2,
1188 Label* gc_required) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001189 AllocateInNewSpace(ConsString::kSize,
ager@chromium.org5c838252010-02-19 08:53:10 +00001190 result,
1191 scratch1,
1192 scratch2,
1193 gc_required,
1194 TAG_OBJECT);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001195
1196 InitializeNewString(result,
1197 length,
1198 Heap::kConsStringMapRootIndex,
1199 scratch1,
1200 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001201}
1202
1203
1204void MacroAssembler::AllocateAsciiConsString(Register result,
1205 Register length,
1206 Register scratch1,
1207 Register scratch2,
1208 Label* gc_required) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001209 AllocateInNewSpace(ConsString::kSize,
ager@chromium.org5c838252010-02-19 08:53:10 +00001210 result,
1211 scratch1,
1212 scratch2,
1213 gc_required,
1214 TAG_OBJECT);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001215
1216 InitializeNewString(result,
1217 length,
1218 Heap::kConsAsciiStringMapRootIndex,
1219 scratch1,
1220 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001221}
1222
1223
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001224void MacroAssembler::CompareObjectType(Register object,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001225 Register map,
1226 Register type_reg,
1227 InstanceType type) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001228 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001229 CompareInstanceType(map, type_reg, type);
1230}
1231
1232
1233void MacroAssembler::CompareInstanceType(Register map,
1234 Register type_reg,
1235 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001236 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1237 cmp(type_reg, Operand(type));
1238}
1239
1240
ager@chromium.org5c838252010-02-19 08:53:10 +00001241void MacroAssembler::CheckMap(Register obj,
1242 Register scratch,
1243 Handle<Map> map,
1244 Label* fail,
1245 bool is_heap_object) {
1246 if (!is_heap_object) {
1247 BranchOnSmi(obj, fail);
1248 }
1249 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1250 mov(ip, Operand(map));
1251 cmp(scratch, ip);
1252 b(ne, fail);
1253}
1254
1255
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001256void MacroAssembler::CheckMap(Register obj,
1257 Register scratch,
1258 Heap::RootListIndex index,
1259 Label* fail,
1260 bool is_heap_object) {
1261 if (!is_heap_object) {
1262 BranchOnSmi(obj, fail);
1263 }
1264 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1265 LoadRoot(ip, index);
1266 cmp(scratch, ip);
1267 b(ne, fail);
1268}
1269
1270
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001271void MacroAssembler::TryGetFunctionPrototype(Register function,
1272 Register result,
1273 Register scratch,
1274 Label* miss) {
1275 // Check that the receiver isn't a smi.
1276 BranchOnSmi(function, miss);
1277
1278 // Check that the function really is a function. Load map into result reg.
1279 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1280 b(ne, miss);
1281
1282 // Make sure that the function has an instance prototype.
1283 Label non_instance;
1284 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1285 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1286 b(ne, &non_instance);
1287
1288 // Get the prototype or initial map from the function.
1289 ldr(result,
1290 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1291
1292 // If the prototype or initial map is the hole, don't return it and
1293 // simply miss the cache instead. This will allow us to allocate a
1294 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001295 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1296 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001297 b(eq, miss);
1298
1299 // If the function does not have an initial map, we're done.
1300 Label done;
1301 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1302 b(ne, &done);
1303
1304 // Get the prototype from the initial map.
1305 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1306 jmp(&done);
1307
1308 // Non-instance prototype: Fetch prototype from constructor field
1309 // in initial map.
1310 bind(&non_instance);
1311 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1312
1313 // All done.
1314 bind(&done);
1315}
1316
1317
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001318void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +00001319 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001320 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321}
1322
1323
ager@chromium.org5c838252010-02-19 08:53:10 +00001324void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1325 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1326 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1327}
1328
1329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330void MacroAssembler::StubReturn(int argc) {
1331 ASSERT(argc >= 1 && generating_stub());
ager@chromium.org5c838252010-02-19 08:53:10 +00001332 if (argc > 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 add(sp, sp, Operand((argc - 1) * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00001334 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335 Ret();
1336}
1337
mads.s.ager31e71382008-08-13 09:32:07 +00001338
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001339void MacroAssembler::IllegalOperation(int num_arguments) {
1340 if (num_arguments > 0) {
1341 add(sp, sp, Operand(num_arguments * kPointerSize));
1342 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001343 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001344}
1345
1346
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001347void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1348 Register outHighReg,
1349 Register outLowReg) {
1350 // ARMv7 VFP3 instructions to implement integer to double conversion.
1351 mov(r7, Operand(inReg, ASR, kSmiTagSize));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001352 vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001353 vcvt_f64_s32(d7, s15);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001354 vmov(outLowReg, outHighReg, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001355}
1356
1357
ager@chromium.org5c838252010-02-19 08:53:10 +00001358void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1359 Register src,
1360 int num_least_bits) {
1361 if (CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001362 ubfx(dst, src, kSmiTagSize, num_least_bits);
ager@chromium.org5c838252010-02-19 08:53:10 +00001363 } else {
1364 mov(dst, Operand(src, ASR, kSmiTagSize));
1365 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1366 }
1367}
1368
1369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001371 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001373 // If the expected number of arguments of the runtime function is
1374 // constant, we check that the actual number of arguments match the
1375 // expectation.
1376 if (f->nargs >= 0 && f->nargs != num_arguments) {
1377 IllegalOperation(num_arguments);
1378 return;
1379 }
kasper.lund7276f142008-07-30 08:49:36 +00001380
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001381 // TODO(1236192): Most runtime routines don't need the number of
1382 // arguments passed in because it is constant. At some point we
1383 // should remove this need and make the runtime routine entry code
1384 // smarter.
1385 mov(r0, Operand(num_arguments));
1386 mov(r1, Operand(ExternalReference(f)));
1387 CEntryStub stub(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 CallStub(&stub);
1389}
1390
1391
1392void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1393 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1394}
1395
1396
ager@chromium.org5c838252010-02-19 08:53:10 +00001397void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1398 int num_arguments) {
1399 mov(r0, Operand(num_arguments));
1400 mov(r1, Operand(ext));
1401
1402 CEntryStub stub(1);
1403 CallStub(&stub);
1404}
1405
1406
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001407void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1408 int num_arguments,
1409 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001410 // TODO(1236192): Most runtime routines don't need the number of
1411 // arguments passed in because it is constant. At some point we
1412 // should remove this need and make the runtime routine entry code
1413 // smarter.
1414 mov(r0, Operand(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001415 JumpToExternalReference(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416}
1417
1418
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001419void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1420 int num_arguments,
1421 int result_size) {
1422 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1423}
1424
1425
1426void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427#if defined(__thumb__)
1428 // Thumb mode builtin.
1429 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1430#endif
1431 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001432 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001433 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434}
1435
1436
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001437void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1438 InvokeJSFlags flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001439 GetBuiltinEntry(r2, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 if (flags == CALL_JS) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001441 Call(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 } else {
1443 ASSERT(flags == JUMP_JS);
ager@chromium.org5c838252010-02-19 08:53:10 +00001444 Jump(r2);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001445 }
1446}
1447
1448
1449void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001450 ASSERT(!target.is(r1));
1451
1452 // Load the builtins object into target register.
1453 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1454 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1455
ager@chromium.org5c838252010-02-19 08:53:10 +00001456 // Load the JavaScript builtin function from the builtins object.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001457 ldr(r1, FieldMemOperand(target,
1458 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1459
1460 // Load the code entry point from the builtins object.
1461 ldr(target, FieldMemOperand(target,
1462 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1463 if (FLAG_debug_code) {
1464 // Make sure the code objects in the builtins object and in the
1465 // builtin function are the same.
1466 push(r1);
1467 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1468 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1469 cmp(r1, target);
1470 Assert(eq, "Builtin code object changed");
1471 pop(r1);
1472 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001473 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474}
1475
1476
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001477void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1478 Register scratch1, Register scratch2) {
1479 if (FLAG_native_code_counters && counter->Enabled()) {
1480 mov(scratch1, Operand(value));
1481 mov(scratch2, Operand(ExternalReference(counter)));
1482 str(scratch1, MemOperand(scratch2));
1483 }
1484}
1485
1486
1487void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1488 Register scratch1, Register scratch2) {
1489 ASSERT(value > 0);
1490 if (FLAG_native_code_counters && counter->Enabled()) {
1491 mov(scratch2, Operand(ExternalReference(counter)));
1492 ldr(scratch1, MemOperand(scratch2));
1493 add(scratch1, scratch1, Operand(value));
1494 str(scratch1, MemOperand(scratch2));
1495 }
1496}
1497
1498
1499void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1500 Register scratch1, Register scratch2) {
1501 ASSERT(value > 0);
1502 if (FLAG_native_code_counters && counter->Enabled()) {
1503 mov(scratch2, Operand(ExternalReference(counter)));
1504 ldr(scratch1, MemOperand(scratch2));
1505 sub(scratch1, scratch1, Operand(value));
1506 str(scratch1, MemOperand(scratch2));
1507 }
1508}
1509
1510
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511void MacroAssembler::Assert(Condition cc, const char* msg) {
1512 if (FLAG_debug_code)
1513 Check(cc, msg);
1514}
1515
1516
1517void MacroAssembler::Check(Condition cc, const char* msg) {
1518 Label L;
1519 b(cc, &L);
1520 Abort(msg);
1521 // will not return here
1522 bind(&L);
1523}
1524
1525
1526void MacroAssembler::Abort(const char* msg) {
1527 // We want to pass the msg string like a smi to avoid GC
1528 // problems, however msg is not guaranteed to be aligned
1529 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001530 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 // from the real pointer as a smi.
1532 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1533 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1534 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1535#ifdef DEBUG
1536 if (msg != NULL) {
1537 RecordComment("Abort message: ");
1538 RecordComment(msg);
1539 }
1540#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001541 // Disable stub call restrictions to always allow calls to abort.
1542 set_allow_stub_calls(true);
1543
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544 mov(r0, Operand(p0));
1545 push(r0);
1546 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001547 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 CallRuntime(Runtime::kAbort, 2);
1549 // will not return here
1550}
1551
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001552
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001553void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1554 if (context_chain_length > 0) {
1555 // Move up the chain of contexts to the context containing the slot.
1556 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1557 // Load the function context (which is the incoming, outer context).
1558 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1559 for (int i = 1; i < context_chain_length; i++) {
1560 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1561 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1562 }
1563 // The context may be an intermediate context, not a function context.
1564 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1565 } else { // Slot is in the current function context.
1566 // The context may be an intermediate context, not a function context.
1567 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1568 }
1569}
1570
1571
ager@chromium.org5c838252010-02-19 08:53:10 +00001572void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1573 Register reg2,
1574 Label* on_not_both_smi) {
1575 ASSERT_EQ(0, kSmiTag);
1576 tst(reg1, Operand(kSmiTagMask));
1577 tst(reg2, Operand(kSmiTagMask), eq);
1578 b(ne, on_not_both_smi);
1579}
1580
1581
1582void MacroAssembler::JumpIfEitherSmi(Register reg1,
1583 Register reg2,
1584 Label* on_either_smi) {
1585 ASSERT_EQ(0, kSmiTag);
1586 tst(reg1, Operand(kSmiTagMask));
1587 tst(reg2, Operand(kSmiTagMask), ne);
1588 b(eq, on_either_smi);
1589}
1590
1591
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001592void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1593 Register first,
1594 Register second,
1595 Register scratch1,
1596 Register scratch2,
1597 Label* failure) {
1598 // Test that both first and second are sequential ASCII strings.
1599 // Assume that they are non-smis.
1600 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1601 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1602 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1603 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001604
1605 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1606 scratch2,
1607 scratch1,
1608 scratch2,
1609 failure);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001610}
1611
1612void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1613 Register second,
1614 Register scratch1,
1615 Register scratch2,
1616 Label* failure) {
1617 // Check that neither is a smi.
1618 ASSERT_EQ(0, kSmiTag);
1619 and_(scratch1, first, Operand(second));
1620 tst(scratch1, Operand(kSmiTagMask));
1621 b(eq, failure);
1622 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1623 second,
1624 scratch1,
1625 scratch2,
1626 failure);
1627}
1628
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001629
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001630// Allocates a heap number or jumps to the need_gc label if the young space
1631// is full and a scavenge is needed.
1632void MacroAssembler::AllocateHeapNumber(Register result,
1633 Register scratch1,
1634 Register scratch2,
1635 Label* gc_required) {
1636 // Allocate an object in the heap for the heap number and tag it as a heap
1637 // object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001638 AllocateInNewSpace(HeapNumber::kSize,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001639 result,
1640 scratch1,
1641 scratch2,
1642 gc_required,
1643 TAG_OBJECT);
1644
1645 // Get heap number map and store it in the allocated object.
1646 LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
1647 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1648}
1649
1650
1651void MacroAssembler::CountLeadingZeros(Register source,
1652 Register scratch,
1653 Register zeros) {
1654#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1655 clz(zeros, source); // This instruction is only supported after ARM5.
1656#else
1657 mov(zeros, Operand(0));
1658 mov(scratch, source);
1659 // Top 16.
1660 tst(scratch, Operand(0xffff0000));
1661 add(zeros, zeros, Operand(16), LeaveCC, eq);
1662 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1663 // Top 8.
1664 tst(scratch, Operand(0xff000000));
1665 add(zeros, zeros, Operand(8), LeaveCC, eq);
1666 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1667 // Top 4.
1668 tst(scratch, Operand(0xf0000000));
1669 add(zeros, zeros, Operand(4), LeaveCC, eq);
1670 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1671 // Top 2.
1672 tst(scratch, Operand(0xc0000000));
1673 add(zeros, zeros, Operand(2), LeaveCC, eq);
1674 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1675 // Top bit.
1676 tst(scratch, Operand(0x80000000u));
1677 add(zeros, zeros, Operand(1), LeaveCC, eq);
1678#endif
1679}
1680
1681
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001682void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1683 Register first,
1684 Register second,
1685 Register scratch1,
1686 Register scratch2,
1687 Label* failure) {
1688 int kFlatAsciiStringMask =
1689 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1690 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1691 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1692 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1693 cmp(scratch1, Operand(kFlatAsciiStringTag));
1694 // Ignore second test if first test failed.
1695 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1696 b(ne, failure);
1697}
1698
1699
1700void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1701 Register scratch,
1702 Label* failure) {
1703 int kFlatAsciiStringMask =
1704 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1705 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1706 and_(scratch, type, Operand(kFlatAsciiStringMask));
1707 cmp(scratch, Operand(kFlatAsciiStringTag));
1708 b(ne, failure);
1709}
1710
1711
ager@chromium.org357bf652010-04-12 11:30:10 +00001712void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001713 int frame_alignment = ActivationFrameAlignment();
ager@chromium.org357bf652010-04-12 11:30:10 +00001714 // Up to four simple arguments are passed in registers r0..r3.
1715 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001716 if (frame_alignment > kPointerSize) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001717 // Make stack end at alignment and make room for num_arguments - 4 words
1718 // and the original value of sp.
1719 mov(scratch, sp);
1720 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001721 ASSERT(IsPowerOf2(frame_alignment));
1722 and_(sp, sp, Operand(-frame_alignment));
ager@chromium.org357bf652010-04-12 11:30:10 +00001723 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1724 } else {
1725 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1726 }
1727}
1728
1729
1730void MacroAssembler::CallCFunction(ExternalReference function,
1731 int num_arguments) {
1732 mov(ip, Operand(function));
1733 CallCFunction(ip, num_arguments);
1734}
1735
1736
1737void MacroAssembler::CallCFunction(Register function, int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001738 // Make sure that the stack is aligned before calling a C function unless
1739 // running in the simulator. The simulator has its own alignment check which
1740 // provides more information.
1741#if defined(V8_HOST_ARCH_ARM)
1742 if (FLAG_debug_code) {
1743 int frame_alignment = OS::ActivationFrameAlignment();
1744 int frame_alignment_mask = frame_alignment - 1;
1745 if (frame_alignment > kPointerSize) {
1746 ASSERT(IsPowerOf2(frame_alignment));
1747 Label alignment_as_expected;
1748 tst(sp, Operand(frame_alignment_mask));
1749 b(eq, &alignment_as_expected);
1750 // Don't use Check here, as it will call Runtime_Abort possibly
1751 // re-entering here.
1752 stop("Unexpected alignment");
1753 bind(&alignment_as_expected);
1754 }
1755 }
1756#endif
1757
ager@chromium.org357bf652010-04-12 11:30:10 +00001758 // Just call directly. The function called cannot cause a GC, or
1759 // allow preemption, so the return address in the link register
1760 // stays correct.
1761 Call(function);
1762 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1763 if (OS::ActivationFrameAlignment() > kPointerSize) {
1764 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1765 } else {
1766 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1767 }
1768}
1769
1770
ager@chromium.org4af710e2009-09-15 12:20:11 +00001771#ifdef ENABLE_DEBUGGER_SUPPORT
1772CodePatcher::CodePatcher(byte* address, int instructions)
1773 : address_(address),
1774 instructions_(instructions),
1775 size_(instructions * Assembler::kInstrSize),
1776 masm_(address, size_ + Assembler::kGap) {
1777 // Create a new macro assembler pointing to the address of the code to patch.
1778 // The size is adjusted with kGap on order for the assembler to generate size
1779 // bytes of instructions without failing with buffer size constraints.
1780 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1781}
1782
1783
1784CodePatcher::~CodePatcher() {
1785 // Indicate that code has changed.
1786 CPU::FlushICache(address_, size_);
1787
1788 // Check that the code was patched as expected.
1789 ASSERT(masm_.pc_ == address_ + size_);
1790 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1791}
1792
1793
1794void CodePatcher::Emit(Instr x) {
1795 masm()->emit(x);
1796}
1797
1798
1799void CodePatcher::Emit(Address addr) {
1800 masm()->emit(reinterpret_cast<Instr>(addr));
1801}
1802#endif // ENABLE_DEBUGGER_SUPPORT
1803
1804
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001806
1807#endif // V8_TARGET_ARCH_ARM