blob: 630e0b80ac1b9263d04bb326222877d39b5e390d [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
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000273void MacroAssembler::Bfc(Register dst, int lsb, int width, Condition cond) {
274 ASSERT(lsb < 32);
275 if (!CpuFeatures::IsSupported(ARMv7)) {
276 int mask = (1 << (width + lsb)) - 1 - ((1 << lsb) - 1);
277 bic(dst, dst, Operand(mask));
278 } else {
279 bfc(dst, lsb, width, cond);
280 }
281}
282
283
ager@chromium.org8bb60582008-12-11 12:02:20 +0000284void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
285 // Empty the const pool.
286 CheckConstPool(true, true);
287 add(pc, pc, Operand(index,
288 LSL,
289 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000290 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000291 nop(); // Jump table alignment.
292 for (int i = 0; i < targets.length(); i++) {
293 b(targets[i]);
294 }
295}
296
297
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000298void MacroAssembler::LoadRoot(Register destination,
299 Heap::RootListIndex index,
300 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000301 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000302}
303
304
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000305void MacroAssembler::StoreRoot(Register source,
306 Heap::RootListIndex index,
307 Condition cond) {
308 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
309}
310
311
ager@chromium.orgac091b72010-05-05 07:34:42 +0000312void MacroAssembler::RecordWriteHelper(Register object,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000313 Operand offset,
314 Register scratch0,
315 Register scratch1) {
ager@chromium.orgac091b72010-05-05 07:34:42 +0000316 if (FLAG_debug_code) {
317 // Check that the object is not in new space.
318 Label not_in_new_space;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000319 InNewSpace(object, scratch1, ne, &not_in_new_space);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000320 Abort("new-space object passed to RecordWriteHelper");
321 bind(&not_in_new_space);
322 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000323
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000324 // Add offset into the object.
325 add(scratch0, object, offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000327 // Calculate page address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000328 Bfc(object, 0, kPageSizeBits);
329
330 // Calculate region number.
331 Ubfx(scratch0, scratch0, Page::kRegionSizeLog2,
332 kPageSizeBits - Page::kRegionSizeLog2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000334 // Mark region dirty.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000335 ldr(scratch1, MemOperand(object, Page::kDirtyFlagOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 mov(ip, Operand(1));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000337 orr(scratch1, scratch1, Operand(ip, LSL, scratch0));
338 str(scratch1, MemOperand(object, Page::kDirtyFlagOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000339}
340
341
342void MacroAssembler::InNewSpace(Register object,
343 Register scratch,
344 Condition cc,
345 Label* branch) {
346 ASSERT(cc == eq || cc == ne);
347 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
348 cmp(scratch, Operand(ExternalReference::new_space_start()));
349 b(cc, branch);
350}
351
352
353// Will clobber 4 registers: object, offset, scratch, ip. The
354// register 'object' contains a heap object pointer. The heap object
355// tag is shifted away.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000356void MacroAssembler::RecordWrite(Register object,
357 Operand offset,
358 Register scratch0,
359 Register scratch1) {
ager@chromium.orgac091b72010-05-05 07:34:42 +0000360 // The compiled code assumes that record write doesn't change the
361 // context register, so we check that none of the clobbered
362 // registers are cp.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000363 ASSERT(!object.is(cp) && !scratch0.is(cp) && !scratch1.is(cp));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000364
365 Label done;
366
367 // First, test that the object is not in the new space. We cannot set
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000368 // region marks for new space pages.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000369 InNewSpace(object, scratch0, eq, &done);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000370
371 // Record the actual write.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000372 RecordWriteHelper(object, offset, scratch0, scratch1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373
374 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000375
376 // Clobber all input registers when running with the debug-code flag
377 // turned on to provoke errors.
378 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000379 mov(object, Operand(BitCast<int32_t>(kZapValue)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000380 mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
381 mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000382 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383}
384
385
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000386void MacroAssembler::Ldrd(Register dst1, Register dst2,
387 const MemOperand& src, Condition cond) {
388 ASSERT(src.rm().is(no_reg));
389 ASSERT(!dst1.is(lr)); // r14.
390 ASSERT_EQ(0, dst1.code() % 2);
391 ASSERT_EQ(dst1.code() + 1, dst2.code());
392
393 // Generate two ldr instructions if ldrd is not available.
394 if (CpuFeatures::IsSupported(ARMv7)) {
395 CpuFeatures::Scope scope(ARMv7);
396 ldrd(dst1, dst2, src, cond);
397 } else {
398 MemOperand src2(src);
399 src2.set_offset(src2.offset() + 4);
400 if (dst1.is(src.rn())) {
401 ldr(dst2, src2, cond);
402 ldr(dst1, src, cond);
403 } else {
404 ldr(dst1, src, cond);
405 ldr(dst2, src2, cond);
406 }
407 }
408}
409
410
411void MacroAssembler::Strd(Register src1, Register src2,
412 const MemOperand& dst, Condition cond) {
413 ASSERT(dst.rm().is(no_reg));
414 ASSERT(!src1.is(lr)); // r14.
415 ASSERT_EQ(0, src1.code() % 2);
416 ASSERT_EQ(src1.code() + 1, src2.code());
417
418 // Generate two str instructions if strd is not available.
419 if (CpuFeatures::IsSupported(ARMv7)) {
420 CpuFeatures::Scope scope(ARMv7);
421 strd(src1, src2, dst, cond);
422 } else {
423 MemOperand dst2(dst);
424 dst2.set_offset(dst2.offset() + 4);
425 str(src1, dst, cond);
426 str(src2, dst2, cond);
427 }
428}
429
430
ager@chromium.org7c537e22008-10-16 08:43:32 +0000431void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000432 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000433 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
434 mov(ip, Operand(Smi::FromInt(type)));
435 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000436 mov(ip, Operand(CodeObject()));
437 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000438 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439}
440
441
ager@chromium.org7c537e22008-10-16 08:43:32 +0000442void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000443 // r0: preserved
444 // r1: preserved
445 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
ager@chromium.org7c537e22008-10-16 08:43:32 +0000447 // Drop the execution stack down to the frame pointer and restore
448 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000449 mov(sp, fp);
450 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451}
452
453
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000454void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000455 // Compute the argv pointer and keep it in a callee-saved register.
456 // r0 is argc.
457 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
458 sub(r6, r6, Operand(kPointerSize));
459
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000460 // Compute callee's stack pointer before making changes and save it as
461 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000462 // popping the args.
463
464 // ip = sp + kPointerSize * #args;
465 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
466
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000467 // Prepare the stack to be aligned when calling into C. After this point there
468 // are 5 pushes before the call into C, so the stack needs to be aligned after
469 // 5 pushes.
470 int frame_alignment = ActivationFrameAlignment();
471 int frame_alignment_mask = frame_alignment - 1;
472 if (frame_alignment != kPointerSize) {
473 // The following code needs to be more general if this assert does not hold.
474 ASSERT(frame_alignment == 2 * kPointerSize);
475 // With 5 pushes left the frame must be unaligned at this point.
476 mov(r7, Operand(Smi::FromInt(0)));
477 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
478 push(r7, eq); // Push if aligned to make it unaligned.
479 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000480
ager@chromium.org236ad962008-09-25 09:45:57 +0000481 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
482 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +0000483 mov(fp, Operand(sp)); // Setup new frame pointer.
ager@chromium.org236ad962008-09-25 09:45:57 +0000484
ager@chromium.org5c838252010-02-19 08:53:10 +0000485 mov(ip, Operand(CodeObject()));
486 push(ip); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000487
488 // Save the frame pointer and the context in top.
489 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
490 str(fp, MemOperand(ip));
491 mov(ip, Operand(ExternalReference(Top::k_context_address)));
492 str(cp, MemOperand(ip));
493
494 // Setup argc and the builtin function in callee-saved registers.
495 mov(r4, Operand(r0));
496 mov(r5, Operand(r1));
497
ager@chromium.org236ad962008-09-25 09:45:57 +0000498
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000499#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000500 // Save the state of all registers to the stack from the memory
501 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000502 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000503 // Use sp as base to push.
504 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
505 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000506#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000507}
508
509
ager@chromium.orgac091b72010-05-05 07:34:42 +0000510void MacroAssembler::InitializeNewString(Register string,
511 Register length,
512 Heap::RootListIndex map_index,
513 Register scratch1,
514 Register scratch2) {
515 mov(scratch1, Operand(length, LSL, kSmiTagSize));
516 LoadRoot(scratch2, map_index);
517 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
518 mov(scratch1, Operand(String::kEmptyHashField));
519 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
520 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
521}
522
523
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000524int MacroAssembler::ActivationFrameAlignment() {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000525#if defined(V8_HOST_ARCH_ARM)
526 // Running on the real platform. Use the alignment as mandated by the local
527 // environment.
528 // Note: This will break if we ever start generating snapshots on one ARM
529 // platform for another ARM platform with a different alignment.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000530 return OS::ActivationFrameAlignment();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000531#else // defined(V8_HOST_ARCH_ARM)
532 // If we are using the simulator then we should always align to the expected
533 // alignment. As the simulator is used to generate snapshots we do not know
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000534 // if the target platform will need alignment, so this is controlled from a
535 // flag.
536 return FLAG_sim_stack_alignment;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000537#endif // defined(V8_HOST_ARCH_ARM)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000538}
539
540
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000541void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000542#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000543 // Restore the memory copy of the registers by digging them out from
544 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000545 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000546 // This code intentionally clobbers r2 and r3.
547 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000548 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000549 add(r3, fp, Operand(kOffset));
550 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
551 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000552#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000553
554 // Clear top frame.
555 mov(r3, Operand(0));
556 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
557 str(r3, MemOperand(ip));
558
559 // Restore current context from top and clear it in debug mode.
560 mov(ip, Operand(ExternalReference(Top::k_context_address)));
561 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000562#ifdef DEBUG
563 str(r3, MemOperand(ip));
564#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000565
566 // Pop the arguments, restore registers, and return.
567 mov(sp, Operand(fp)); // respect ABI stack constraint
568 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
569}
570
571
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000572void MacroAssembler::InvokePrologue(const ParameterCount& expected,
573 const ParameterCount& actual,
574 Handle<Code> code_constant,
575 Register code_reg,
576 Label* done,
577 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000578 bool definitely_matches = false;
579 Label regular_invoke;
580
581 // Check whether the expected and actual arguments count match. If not,
582 // setup registers according to contract with ArgumentsAdaptorTrampoline:
583 // r0: actual arguments count
584 // r1: function (passed through to callee)
585 // r2: expected arguments count
586 // r3: callee code entry
587
588 // The code below is made a lot easier because the calling code already sets
589 // up actual and expected registers according to the contract if values are
590 // passed in registers.
591 ASSERT(actual.is_immediate() || actual.reg().is(r0));
592 ASSERT(expected.is_immediate() || expected.reg().is(r2));
593 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
594
595 if (expected.is_immediate()) {
596 ASSERT(actual.is_immediate());
597 if (expected.immediate() == actual.immediate()) {
598 definitely_matches = true;
599 } else {
600 mov(r0, Operand(actual.immediate()));
601 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
602 if (expected.immediate() == sentinel) {
603 // Don't worry about adapting arguments for builtins that
604 // don't want that done. Skip adaption code by making it look
605 // like we have a match between expected and actual number of
606 // arguments.
607 definitely_matches = true;
608 } else {
609 mov(r2, Operand(expected.immediate()));
610 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000611 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000612 } else {
613 if (actual.is_immediate()) {
614 cmp(expected.reg(), Operand(actual.immediate()));
615 b(eq, &regular_invoke);
616 mov(r0, Operand(actual.immediate()));
617 } else {
618 cmp(expected.reg(), Operand(actual.reg()));
619 b(eq, &regular_invoke);
620 }
621 }
622
623 if (!definitely_matches) {
624 if (!code_constant.is_null()) {
625 mov(r3, Operand(code_constant));
626 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
627 }
628
629 Handle<Code> adaptor =
630 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
631 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000632 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000633 b(done);
634 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000635 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000636 }
637 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000638 }
639}
640
641
642void MacroAssembler::InvokeCode(Register code,
643 const ParameterCount& expected,
644 const ParameterCount& actual,
645 InvokeFlag flag) {
646 Label done;
647
648 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
649 if (flag == CALL_FUNCTION) {
650 Call(code);
651 } else {
652 ASSERT(flag == JUMP_FUNCTION);
653 Jump(code);
654 }
655
656 // Continue here if InvokePrologue does handle the invocation due to
657 // mismatched parameter counts.
658 bind(&done);
659}
660
661
662void MacroAssembler::InvokeCode(Handle<Code> code,
663 const ParameterCount& expected,
664 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000665 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000666 InvokeFlag flag) {
667 Label done;
668
669 InvokePrologue(expected, actual, code, no_reg, &done, flag);
670 if (flag == CALL_FUNCTION) {
671 Call(code, rmode);
672 } else {
673 Jump(code, rmode);
674 }
675
676 // Continue here if InvokePrologue does handle the invocation due to
677 // mismatched parameter counts.
678 bind(&done);
679}
680
681
682void MacroAssembler::InvokeFunction(Register fun,
683 const ParameterCount& actual,
684 InvokeFlag flag) {
685 // Contract with called JS functions requires that function is passed in r1.
686 ASSERT(fun.is(r1));
687
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000688 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000689 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000690
691 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
692 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
693 ldr(expected_reg,
694 FieldMemOperand(code_reg,
695 SharedFunctionInfo::kFormalParameterCountOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000696 mov(expected_reg, Operand(expected_reg, ASR, kSmiTagSize));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000697 ldr(code_reg,
698 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
699 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
700
701 ParameterCount expected(expected_reg);
702 InvokeCode(code_reg, expected, actual, flag);
703}
704
705
ager@chromium.org5c838252010-02-19 08:53:10 +0000706void MacroAssembler::InvokeFunction(JSFunction* function,
707 const ParameterCount& actual,
708 InvokeFlag flag) {
709 ASSERT(function->is_compiled());
710
711 // Get the function and setup the context.
712 mov(r1, Operand(Handle<JSFunction>(function)));
713 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
714
715 // Invoke the cached code.
716 Handle<Code> code(function->code());
717 ParameterCount expected(function->shared()->formal_parameter_count());
718 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
719}
720
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000721#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722void MacroAssembler::SaveRegistersToMemory(RegList regs) {
723 ASSERT((regs & ~kJSCallerSaved) == 0);
724 // Copy the content of registers to memory location.
725 for (int i = 0; i < kNumJSCallerSaved; i++) {
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 str(reg, MemOperand(ip));
731 }
732 }
733}
734
735
736void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
737 ASSERT((regs & ~kJSCallerSaved) == 0);
738 // Copy the content of memory location to registers.
739 for (int i = kNumJSCallerSaved; --i >= 0;) {
740 int r = JSCallerSavedCode(i);
741 if ((regs & (1 << r)) != 0) {
742 Register reg = { r };
743 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
744 ldr(reg, MemOperand(ip));
745 }
746 }
747}
748
749
750void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
751 RegList regs) {
752 ASSERT((regs & ~kJSCallerSaved) == 0);
753 // Copy the content of the memory location to the stack and adjust base.
754 for (int i = kNumJSCallerSaved; --i >= 0;) {
755 int r = JSCallerSavedCode(i);
756 if ((regs & (1 << r)) != 0) {
757 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
758 ldr(ip, MemOperand(ip));
759 str(ip, MemOperand(base, 4, NegPreIndex));
760 }
761 }
762}
763
764
765void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
766 Register scratch,
767 RegList regs) {
768 ASSERT((regs & ~kJSCallerSaved) == 0);
769 // Copy the content of the stack to the memory location and adjust base.
770 for (int i = 0; i < kNumJSCallerSaved; i++) {
771 int r = JSCallerSavedCode(i);
772 if ((regs & (1 << r)) != 0) {
773 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
774 ldr(scratch, MemOperand(base, 4, PostIndex));
775 str(scratch, MemOperand(ip));
776 }
777 }
778}
ager@chromium.org5c838252010-02-19 08:53:10 +0000779
780
781void MacroAssembler::DebugBreak() {
782 ASSERT(allow_stub_calls());
783 mov(r0, Operand(0));
784 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
785 CEntryStub ces(1);
786 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
787}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000788#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791void MacroAssembler::PushTryHandler(CodeLocation try_location,
792 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000793 // Adjust this code if not the case.
794 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 // The pc (return address) is passed in register lr.
796 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 if (type == TRY_CATCH_HANDLER) {
798 mov(r3, Operand(StackHandler::TRY_CATCH));
799 } else {
800 mov(r3, Operand(StackHandler::TRY_FINALLY));
801 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000802 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
803 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
804 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
805 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
806 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
808 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000809 ASSERT(StackHandlerConstants::kNextOffset == 0);
810 push(r1);
811 // Link this handler as the new current one.
812 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000814 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000816 // The frame pointer does not point to a JS frame so we save NULL
817 // for fp. We expect the code throwing an exception to check fp
818 // before dereferencing it to restore the context.
819 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000821 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
822 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
823 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
824 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
825 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
827 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000828 ASSERT(StackHandlerConstants::kNextOffset == 0);
829 push(r6);
830 // Link this handler as the new current one.
831 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 }
833}
834
835
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000836void MacroAssembler::PopTryHandler() {
837 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
838 pop(r1);
839 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
840 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
841 str(r1, MemOperand(ip));
842}
843
844
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
846 JSObject* holder, Register holder_reg,
847 Register scratch,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000848 int save_at_depth,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 Label* miss) {
850 // Make sure there's no overlap between scratch and the other
851 // registers.
852 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
853
854 // Keep track of the current object in register reg.
855 Register reg = object_reg;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000856 int depth = 0;
857
858 if (save_at_depth == depth) {
859 str(reg, MemOperand(sp));
860 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861
862 // Check the maps in the prototype chain.
863 // Traverse the prototype chain from the object and do map checks.
864 while (object != holder) {
865 depth++;
866
867 // Only global objects and objects that do not require access
868 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000869 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
871 // Get the map of the current object.
872 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
873 cmp(scratch, Operand(Handle<Map>(object->map())));
874
875 // Branch on the result of the map check.
876 b(ne, miss);
877
878 // Check access rights to the global object. This has to happen
879 // after the map check so that we know that the object is
880 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000881 if (object->IsJSGlobalProxy()) {
882 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 // Restore scratch register to be the map of the object. In the
884 // new space case below, we load the prototype from the map in
885 // the scratch register.
886 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
887 }
888
889 reg = holder_reg; // from now the object is in holder_reg
890 JSObject* prototype = JSObject::cast(object->GetPrototype());
891 if (Heap::InNewSpace(prototype)) {
892 // The prototype is in new space; we cannot store a reference
893 // to it in the code. Load it from the map.
894 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
895 } else {
896 // The prototype is in old space; load it directly.
897 mov(reg, Operand(Handle<JSObject>(prototype)));
898 }
899
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000900 if (save_at_depth == depth) {
901 str(reg, MemOperand(sp));
902 }
903
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 // Go to the next object in the prototype chain.
905 object = prototype;
906 }
907
908 // Check the holder map.
909 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
910 cmp(scratch, Operand(Handle<Map>(object->map())));
911 b(ne, miss);
912
913 // Log the check depth.
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000914 LOG(IntEvent("check-maps-depth", depth + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
916 // Perform security check for access to the global object and return
917 // the holder register.
918 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000919 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
920 if (object->IsJSGlobalProxy()) {
921 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 }
923 return reg;
924}
925
926
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000927void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
928 Register scratch,
929 Label* miss) {
930 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000932 ASSERT(!holder_reg.is(scratch));
933 ASSERT(!holder_reg.is(ip));
934 ASSERT(!scratch.is(ip));
935
936 // Load current lexical context from the stack frame.
937 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
938 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000939#ifdef DEBUG
940 cmp(scratch, Operand(0));
941 Check(ne, "we should not have an empty lexical context");
942#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000944 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
946 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000947 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
948
949 // Check the context is a global context.
950 if (FLAG_debug_code) {
951 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
952 // Cannot use ip as a temporary in this verification code. Due to the fact
953 // that ip is clobbered as part of cmp with an object Operand.
954 push(holder_reg); // Temporarily save holder on the stack.
955 // Read the first word and compare to the global_context_map.
956 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000957 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
958 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000959 Check(eq, "JSGlobalObject::global_context should be a global context.");
960 pop(holder_reg); // Restore holder.
961 }
962
963 // Check if both contexts are the same.
964 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
965 cmp(scratch, Operand(ip));
966 b(eq, &same_contexts);
967
968 // Check the context is a global context.
969 if (FLAG_debug_code) {
970 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
971 // Cannot use ip as a temporary in this verification code. Due to the fact
972 // that ip is clobbered as part of cmp with an object Operand.
973 push(holder_reg); // Temporarily save holder on the stack.
974 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000975 LoadRoot(ip, Heap::kNullValueRootIndex);
976 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000977 Check(ne, "JSGlobalProxy::context() should not be null.");
978
979 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000980 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
981 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000982 Check(eq, "JSGlobalObject::global_context should be a global context.");
983 // Restore ip is not needed. ip is reloaded below.
984 pop(holder_reg); // Restore holder.
985 // Restore ip to holder's context.
986 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
987 }
988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 // Check that the security token in the calling global object is
990 // compatible with the security token in the receiving global
991 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000992 int token_offset = Context::kHeaderSize +
993 Context::SECURITY_TOKEN_INDEX * kPointerSize;
994
995 ldr(scratch, FieldMemOperand(scratch, token_offset));
996 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 cmp(scratch, Operand(ip));
998 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000999
1000 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001}
1002
1003
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001004void MacroAssembler::AllocateInNewSpace(int object_size,
1005 Register result,
1006 Register scratch1,
1007 Register scratch2,
1008 Label* gc_required,
1009 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001010 ASSERT(!result.is(scratch1));
1011 ASSERT(!scratch1.is(scratch2));
1012
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001013 // Make object size into bytes.
1014 if ((flags & SIZE_IN_WORDS) != 0) {
1015 object_size *= kPointerSize;
1016 }
1017 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
1018
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001019 // Load address of new object into result and allocation top address into
1020 // scratch1.
1021 ExternalReference new_space_allocation_top =
1022 ExternalReference::new_space_allocation_top_address();
1023 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001024 if ((flags & RESULT_CONTAINS_TOP) == 0) {
1025 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001026 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001027 // Assert that result actually contains top on entry. scratch2 is used
1028 // immediately below so this use of scratch2 does not cause difference with
1029 // respect to register content between debug and release mode.
1030 ldr(scratch2, MemOperand(scratch1));
1031 cmp(result, scratch2);
1032 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +00001033 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001034
1035 // Calculate new top and bail out if new space is exhausted. Use result
1036 // to calculate the new top.
1037 ExternalReference new_space_allocation_limit =
1038 ExternalReference::new_space_allocation_limit_address();
1039 mov(scratch2, Operand(new_space_allocation_limit));
1040 ldr(scratch2, MemOperand(scratch2));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001041 add(result, result, Operand(object_size));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001042 cmp(result, Operand(scratch2));
1043 b(hi, gc_required);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001044 str(result, MemOperand(scratch1));
1045
1046 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001047 if ((flags & TAG_OBJECT) != 0) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001048 sub(result, result, Operand(object_size - kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001049 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001050 sub(result, result, Operand(object_size));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001051 }
1052}
1053
1054
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001055void MacroAssembler::AllocateInNewSpace(Register object_size,
1056 Register result,
1057 Register scratch1,
1058 Register scratch2,
1059 Label* gc_required,
1060 AllocationFlags flags) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001061 ASSERT(!result.is(scratch1));
1062 ASSERT(!scratch1.is(scratch2));
1063
1064 // Load address of new object into result and allocation top address into
1065 // scratch1.
1066 ExternalReference new_space_allocation_top =
1067 ExternalReference::new_space_allocation_top_address();
1068 mov(scratch1, Operand(new_space_allocation_top));
1069 if ((flags & RESULT_CONTAINS_TOP) == 0) {
1070 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001071 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001072 // Assert that result actually contains top on entry. scratch2 is used
1073 // immediately below so this use of scratch2 does not cause difference with
1074 // respect to register content between debug and release mode.
1075 ldr(scratch2, MemOperand(scratch1));
1076 cmp(result, scratch2);
1077 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +00001078 }
1079
1080 // Calculate new top and bail out if new space is exhausted. Use result
1081 // to calculate the new top. Object size is in words so a shift is required to
1082 // get the number of bytes
1083 ExternalReference new_space_allocation_limit =
1084 ExternalReference::new_space_allocation_limit_address();
1085 mov(scratch2, Operand(new_space_allocation_limit));
1086 ldr(scratch2, MemOperand(scratch2));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001087 if ((flags & SIZE_IN_WORDS) != 0) {
1088 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1089 } else {
1090 add(result, result, Operand(object_size));
1091 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001092 cmp(result, Operand(scratch2));
1093 b(hi, gc_required);
1094
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001095 // Update allocation top. result temporarily holds the new top.
1096 if (FLAG_debug_code) {
1097 tst(result, Operand(kObjectAlignmentMask));
1098 Check(eq, "Unaligned allocation in new space");
1099 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001100 str(result, MemOperand(scratch1));
1101
1102 // Adjust back to start of new object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001103 if ((flags & SIZE_IN_WORDS) != 0) {
1104 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1105 } else {
1106 sub(result, result, Operand(object_size));
1107 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001108
1109 // Tag object if requested.
1110 if ((flags & TAG_OBJECT) != 0) {
1111 add(result, result, Operand(kHeapObjectTag));
1112 }
1113}
1114
1115
1116void MacroAssembler::UndoAllocationInNewSpace(Register object,
1117 Register scratch) {
1118 ExternalReference new_space_allocation_top =
1119 ExternalReference::new_space_allocation_top_address();
1120
1121 // Make sure the object has no tag before resetting top.
1122 and_(object, object, Operand(~kHeapObjectTagMask));
1123#ifdef DEBUG
1124 // Check that the object un-allocated is below the current top.
1125 mov(scratch, Operand(new_space_allocation_top));
1126 ldr(scratch, MemOperand(scratch));
1127 cmp(object, scratch);
1128 Check(lt, "Undo allocation of non allocated memory");
1129#endif
1130 // Write the address of the object to un-allocate as the current top.
1131 mov(scratch, Operand(new_space_allocation_top));
1132 str(object, MemOperand(scratch));
1133}
1134
1135
ager@chromium.org5c838252010-02-19 08:53:10 +00001136void MacroAssembler::AllocateTwoByteString(Register result,
1137 Register length,
1138 Register scratch1,
1139 Register scratch2,
1140 Register scratch3,
1141 Label* gc_required) {
1142 // Calculate the number of bytes needed for the characters in the string while
1143 // observing object alignment.
1144 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1145 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1146 add(scratch1, scratch1,
1147 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001148 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
ager@chromium.org5c838252010-02-19 08:53:10 +00001149
1150 // Allocate two-byte string in new space.
1151 AllocateInNewSpace(scratch1,
1152 result,
1153 scratch2,
1154 scratch3,
1155 gc_required,
1156 TAG_OBJECT);
1157
1158 // Set the map, length and hash field.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001159 InitializeNewString(result,
1160 length,
1161 Heap::kStringMapRootIndex,
1162 scratch1,
1163 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001164}
1165
1166
1167void MacroAssembler::AllocateAsciiString(Register result,
1168 Register length,
1169 Register scratch1,
1170 Register scratch2,
1171 Register scratch3,
1172 Label* gc_required) {
1173 // Calculate the number of bytes needed for the characters in the string while
1174 // observing object alignment.
1175 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1176 ASSERT(kCharSize == 1);
1177 add(scratch1, length,
1178 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001179 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
ager@chromium.org5c838252010-02-19 08:53:10 +00001180
1181 // Allocate ASCII string in new space.
1182 AllocateInNewSpace(scratch1,
1183 result,
1184 scratch2,
1185 scratch3,
1186 gc_required,
1187 TAG_OBJECT);
1188
1189 // Set the map, length and hash field.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001190 InitializeNewString(result,
1191 length,
1192 Heap::kAsciiStringMapRootIndex,
1193 scratch1,
1194 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001195}
1196
1197
1198void MacroAssembler::AllocateTwoByteConsString(Register result,
1199 Register length,
1200 Register scratch1,
1201 Register scratch2,
1202 Label* gc_required) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001203 AllocateInNewSpace(ConsString::kSize,
ager@chromium.org5c838252010-02-19 08:53:10 +00001204 result,
1205 scratch1,
1206 scratch2,
1207 gc_required,
1208 TAG_OBJECT);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001209
1210 InitializeNewString(result,
1211 length,
1212 Heap::kConsStringMapRootIndex,
1213 scratch1,
1214 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001215}
1216
1217
1218void MacroAssembler::AllocateAsciiConsString(Register result,
1219 Register length,
1220 Register scratch1,
1221 Register scratch2,
1222 Label* gc_required) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001223 AllocateInNewSpace(ConsString::kSize,
ager@chromium.org5c838252010-02-19 08:53:10 +00001224 result,
1225 scratch1,
1226 scratch2,
1227 gc_required,
1228 TAG_OBJECT);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001229
1230 InitializeNewString(result,
1231 length,
1232 Heap::kConsAsciiStringMapRootIndex,
1233 scratch1,
1234 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001235}
1236
1237
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001238void MacroAssembler::CompareObjectType(Register object,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001239 Register map,
1240 Register type_reg,
1241 InstanceType type) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001242 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001243 CompareInstanceType(map, type_reg, type);
1244}
1245
1246
1247void MacroAssembler::CompareInstanceType(Register map,
1248 Register type_reg,
1249 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001250 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1251 cmp(type_reg, Operand(type));
1252}
1253
1254
ager@chromium.org5c838252010-02-19 08:53:10 +00001255void MacroAssembler::CheckMap(Register obj,
1256 Register scratch,
1257 Handle<Map> map,
1258 Label* fail,
1259 bool is_heap_object) {
1260 if (!is_heap_object) {
1261 BranchOnSmi(obj, fail);
1262 }
1263 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1264 mov(ip, Operand(map));
1265 cmp(scratch, ip);
1266 b(ne, fail);
1267}
1268
1269
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001270void MacroAssembler::CheckMap(Register obj,
1271 Register scratch,
1272 Heap::RootListIndex index,
1273 Label* fail,
1274 bool is_heap_object) {
1275 if (!is_heap_object) {
1276 BranchOnSmi(obj, fail);
1277 }
1278 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1279 LoadRoot(ip, index);
1280 cmp(scratch, ip);
1281 b(ne, fail);
1282}
1283
1284
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001285void MacroAssembler::TryGetFunctionPrototype(Register function,
1286 Register result,
1287 Register scratch,
1288 Label* miss) {
1289 // Check that the receiver isn't a smi.
1290 BranchOnSmi(function, miss);
1291
1292 // Check that the function really is a function. Load map into result reg.
1293 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1294 b(ne, miss);
1295
1296 // Make sure that the function has an instance prototype.
1297 Label non_instance;
1298 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1299 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1300 b(ne, &non_instance);
1301
1302 // Get the prototype or initial map from the function.
1303 ldr(result,
1304 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1305
1306 // If the prototype or initial map is the hole, don't return it and
1307 // simply miss the cache instead. This will allow us to allocate a
1308 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001309 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1310 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001311 b(eq, miss);
1312
1313 // If the function does not have an initial map, we're done.
1314 Label done;
1315 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1316 b(ne, &done);
1317
1318 // Get the prototype from the initial map.
1319 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1320 jmp(&done);
1321
1322 // Non-instance prototype: Fetch prototype from constructor field
1323 // in initial map.
1324 bind(&non_instance);
1325 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1326
1327 // All done.
1328 bind(&done);
1329}
1330
1331
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001332void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +00001333 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001334 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335}
1336
1337
ager@chromium.org5c838252010-02-19 08:53:10 +00001338void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1339 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1340 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1341}
1342
1343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344void MacroAssembler::StubReturn(int argc) {
1345 ASSERT(argc >= 1 && generating_stub());
ager@chromium.org5c838252010-02-19 08:53:10 +00001346 if (argc > 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 add(sp, sp, Operand((argc - 1) * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00001348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 Ret();
1350}
1351
mads.s.ager31e71382008-08-13 09:32:07 +00001352
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001353void MacroAssembler::IllegalOperation(int num_arguments) {
1354 if (num_arguments > 0) {
1355 add(sp, sp, Operand(num_arguments * kPointerSize));
1356 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001357 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001358}
1359
1360
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001361void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1362 Register outHighReg,
1363 Register outLowReg) {
1364 // ARMv7 VFP3 instructions to implement integer to double conversion.
1365 mov(r7, Operand(inReg, ASR, kSmiTagSize));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001366 vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001367 vcvt_f64_s32(d7, s15);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001368 vmov(outLowReg, outHighReg, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001369}
1370
1371
lrn@chromium.org32d961d2010-06-30 09:09:34 +00001372void MacroAssembler::ObjectToDoubleVFPRegister(Register object,
1373 DwVfpRegister result,
1374 Register scratch1,
1375 Register scratch2,
1376 Register heap_number_map,
1377 SwVfpRegister scratch3,
1378 Label* not_number,
1379 ObjectToDoubleFlags flags) {
1380 Label done;
1381 if ((flags & OBJECT_NOT_SMI) == 0) {
1382 Label not_smi;
1383 BranchOnNotSmi(object, &not_smi);
1384 // Remove smi tag and convert to double.
1385 mov(scratch1, Operand(object, ASR, kSmiTagSize));
1386 vmov(scratch3, scratch1);
1387 vcvt_f64_s32(result, scratch3);
1388 b(&done);
1389 bind(&not_smi);
1390 }
1391 // Check for heap number and load double value from it.
1392 ldr(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
1393 sub(scratch2, object, Operand(kHeapObjectTag));
1394 cmp(scratch1, heap_number_map);
1395 b(ne, not_number);
1396 if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
1397 // If exponent is all ones the number is either a NaN or +/-Infinity.
1398 ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
1399 Sbfx(scratch1,
1400 scratch1,
1401 HeapNumber::kExponentShift,
1402 HeapNumber::kExponentBits);
1403 // All-one value sign extend to -1.
1404 cmp(scratch1, Operand(-1));
1405 b(eq, not_number);
1406 }
1407 vldr(result, scratch2, HeapNumber::kValueOffset);
1408 bind(&done);
1409}
1410
1411
1412void MacroAssembler::SmiToDoubleVFPRegister(Register smi,
1413 DwVfpRegister value,
1414 Register scratch1,
1415 SwVfpRegister scratch2) {
1416 mov(scratch1, Operand(smi, ASR, kSmiTagSize));
1417 vmov(scratch2, scratch1);
1418 vcvt_f64_s32(value, scratch2);
1419}
1420
1421
ager@chromium.org5c838252010-02-19 08:53:10 +00001422void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1423 Register src,
1424 int num_least_bits) {
1425 if (CpuFeatures::IsSupported(ARMv7)) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001426 ubfx(dst, src, kSmiTagSize, num_least_bits);
ager@chromium.org5c838252010-02-19 08:53:10 +00001427 } else {
1428 mov(dst, Operand(src, ASR, kSmiTagSize));
1429 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1430 }
1431}
1432
1433
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001435 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001437 // If the expected number of arguments of the runtime function is
1438 // constant, we check that the actual number of arguments match the
1439 // expectation.
1440 if (f->nargs >= 0 && f->nargs != num_arguments) {
1441 IllegalOperation(num_arguments);
1442 return;
1443 }
kasper.lund7276f142008-07-30 08:49:36 +00001444
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001445 // TODO(1236192): Most runtime routines don't need the number of
1446 // arguments passed in because it is constant. At some point we
1447 // should remove this need and make the runtime routine entry code
1448 // smarter.
1449 mov(r0, Operand(num_arguments));
1450 mov(r1, Operand(ExternalReference(f)));
1451 CEntryStub stub(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 CallStub(&stub);
1453}
1454
1455
1456void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1457 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1458}
1459
1460
ager@chromium.org5c838252010-02-19 08:53:10 +00001461void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1462 int num_arguments) {
1463 mov(r0, Operand(num_arguments));
1464 mov(r1, Operand(ext));
1465
1466 CEntryStub stub(1);
1467 CallStub(&stub);
1468}
1469
1470
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001471void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1472 int num_arguments,
1473 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001474 // TODO(1236192): Most runtime routines don't need the number of
1475 // arguments passed in because it is constant. At some point we
1476 // should remove this need and make the runtime routine entry code
1477 // smarter.
1478 mov(r0, Operand(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001479 JumpToExternalReference(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480}
1481
1482
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001483void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1484 int num_arguments,
1485 int result_size) {
1486 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1487}
1488
1489
1490void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491#if defined(__thumb__)
1492 // Thumb mode builtin.
1493 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1494#endif
1495 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001496 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001497 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498}
1499
1500
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001501void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1502 InvokeJSFlags flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001503 GetBuiltinEntry(r2, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 if (flags == CALL_JS) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001505 Call(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 } else {
1507 ASSERT(flags == JUMP_JS);
ager@chromium.org5c838252010-02-19 08:53:10 +00001508 Jump(r2);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001509 }
1510}
1511
1512
1513void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001514 ASSERT(!target.is(r1));
1515
1516 // Load the builtins object into target register.
1517 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1518 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1519
ager@chromium.org5c838252010-02-19 08:53:10 +00001520 // Load the JavaScript builtin function from the builtins object.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001521 ldr(r1, FieldMemOperand(target,
1522 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1523
1524 // Load the code entry point from the builtins object.
1525 ldr(target, FieldMemOperand(target,
1526 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1527 if (FLAG_debug_code) {
1528 // Make sure the code objects in the builtins object and in the
1529 // builtin function are the same.
1530 push(r1);
1531 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1532 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1533 cmp(r1, target);
1534 Assert(eq, "Builtin code object changed");
1535 pop(r1);
1536 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001537 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538}
1539
1540
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001541void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1542 Register scratch1, Register scratch2) {
1543 if (FLAG_native_code_counters && counter->Enabled()) {
1544 mov(scratch1, Operand(value));
1545 mov(scratch2, Operand(ExternalReference(counter)));
1546 str(scratch1, MemOperand(scratch2));
1547 }
1548}
1549
1550
1551void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1552 Register scratch1, Register scratch2) {
1553 ASSERT(value > 0);
1554 if (FLAG_native_code_counters && counter->Enabled()) {
1555 mov(scratch2, Operand(ExternalReference(counter)));
1556 ldr(scratch1, MemOperand(scratch2));
1557 add(scratch1, scratch1, Operand(value));
1558 str(scratch1, MemOperand(scratch2));
1559 }
1560}
1561
1562
1563void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1564 Register scratch1, Register scratch2) {
1565 ASSERT(value > 0);
1566 if (FLAG_native_code_counters && counter->Enabled()) {
1567 mov(scratch2, Operand(ExternalReference(counter)));
1568 ldr(scratch1, MemOperand(scratch2));
1569 sub(scratch1, scratch1, Operand(value));
1570 str(scratch1, MemOperand(scratch2));
1571 }
1572}
1573
1574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575void MacroAssembler::Assert(Condition cc, const char* msg) {
1576 if (FLAG_debug_code)
1577 Check(cc, msg);
1578}
1579
1580
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001581void MacroAssembler::AssertRegisterIsRoot(Register reg,
1582 Heap::RootListIndex index) {
1583 if (FLAG_debug_code) {
1584 LoadRoot(ip, index);
1585 cmp(reg, ip);
1586 Check(eq, "Register did not match expected root");
1587 }
1588}
1589
1590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591void MacroAssembler::Check(Condition cc, const char* msg) {
1592 Label L;
1593 b(cc, &L);
1594 Abort(msg);
1595 // will not return here
1596 bind(&L);
1597}
1598
1599
1600void MacroAssembler::Abort(const char* msg) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001601 Label abort_start;
1602 bind(&abort_start);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 // We want to pass the msg string like a smi to avoid GC
1604 // problems, however msg is not guaranteed to be aligned
1605 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001606 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 // from the real pointer as a smi.
1608 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1609 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1610 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1611#ifdef DEBUG
1612 if (msg != NULL) {
1613 RecordComment("Abort message: ");
1614 RecordComment(msg);
1615 }
1616#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001617 // Disable stub call restrictions to always allow calls to abort.
1618 set_allow_stub_calls(true);
1619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 mov(r0, Operand(p0));
1621 push(r0);
1622 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001623 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 CallRuntime(Runtime::kAbort, 2);
1625 // will not return here
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001626 if (is_const_pool_blocked()) {
1627 // If the calling code cares about the exact number of
1628 // instructions generated, we insert padding here to keep the size
1629 // of the Abort macro constant.
1630 static const int kExpectedAbortInstructions = 10;
1631 int abort_instructions = InstructionsGeneratedSince(&abort_start);
1632 ASSERT(abort_instructions <= kExpectedAbortInstructions);
1633 while (abort_instructions++ < kExpectedAbortInstructions) {
1634 nop();
1635 }
1636 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637}
1638
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001639
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001640void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1641 if (context_chain_length > 0) {
1642 // Move up the chain of contexts to the context containing the slot.
1643 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1644 // Load the function context (which is the incoming, outer context).
1645 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1646 for (int i = 1; i < context_chain_length; i++) {
1647 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1648 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1649 }
1650 // The context may be an intermediate context, not a function context.
1651 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1652 } else { // Slot is in the current function context.
1653 // The context may be an intermediate context, not a function context.
1654 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1655 }
1656}
1657
1658
ager@chromium.org5c838252010-02-19 08:53:10 +00001659void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1660 Register reg2,
1661 Label* on_not_both_smi) {
1662 ASSERT_EQ(0, kSmiTag);
1663 tst(reg1, Operand(kSmiTagMask));
1664 tst(reg2, Operand(kSmiTagMask), eq);
1665 b(ne, on_not_both_smi);
1666}
1667
1668
1669void MacroAssembler::JumpIfEitherSmi(Register reg1,
1670 Register reg2,
1671 Label* on_either_smi) {
1672 ASSERT_EQ(0, kSmiTag);
1673 tst(reg1, Operand(kSmiTagMask));
1674 tst(reg2, Operand(kSmiTagMask), ne);
1675 b(eq, on_either_smi);
1676}
1677
1678
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001679void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1680 Register first,
1681 Register second,
1682 Register scratch1,
1683 Register scratch2,
1684 Label* failure) {
1685 // Test that both first and second are sequential ASCII strings.
1686 // Assume that they are non-smis.
1687 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1688 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1689 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1690 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001691
1692 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1693 scratch2,
1694 scratch1,
1695 scratch2,
1696 failure);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001697}
1698
1699void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1700 Register second,
1701 Register scratch1,
1702 Register scratch2,
1703 Label* failure) {
1704 // Check that neither is a smi.
1705 ASSERT_EQ(0, kSmiTag);
1706 and_(scratch1, first, Operand(second));
1707 tst(scratch1, Operand(kSmiTagMask));
1708 b(eq, failure);
1709 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1710 second,
1711 scratch1,
1712 scratch2,
1713 failure);
1714}
1715
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001716
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001717// Allocates a heap number or jumps to the need_gc label if the young space
1718// is full and a scavenge is needed.
1719void MacroAssembler::AllocateHeapNumber(Register result,
1720 Register scratch1,
1721 Register scratch2,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001722 Register heap_number_map,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001723 Label* gc_required) {
1724 // Allocate an object in the heap for the heap number and tag it as a heap
1725 // object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001726 AllocateInNewSpace(HeapNumber::kSize,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001727 result,
1728 scratch1,
1729 scratch2,
1730 gc_required,
1731 TAG_OBJECT);
1732
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001733 // Store heap number map in the allocated object.
1734 AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1735 str(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001736}
1737
1738
lrn@chromium.org32d961d2010-06-30 09:09:34 +00001739void MacroAssembler::AllocateHeapNumberWithValue(Register result,
1740 DwVfpRegister value,
1741 Register scratch1,
1742 Register scratch2,
1743 Register heap_number_map,
1744 Label* gc_required) {
1745 AllocateHeapNumber(result, scratch1, scratch2, heap_number_map, gc_required);
1746 sub(scratch1, result, Operand(kHeapObjectTag));
1747 vstr(value, scratch1, HeapNumber::kValueOffset);
1748}
1749
1750
1751void MacroAssembler::CountLeadingZeros(Register zeros, // Answer.
1752 Register source, // Input.
1753 Register scratch) {
1754 ASSERT(!zeros.is(source) || !source.is(zeros));
1755 ASSERT(!zeros.is(scratch));
1756 ASSERT(!scratch.is(ip));
1757 ASSERT(!source.is(ip));
1758 ASSERT(!zeros.is(ip));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001759#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1760 clz(zeros, source); // This instruction is only supported after ARM5.
1761#else
1762 mov(zeros, Operand(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00001763 Move(scratch, source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001764 // Top 16.
1765 tst(scratch, Operand(0xffff0000));
1766 add(zeros, zeros, Operand(16), LeaveCC, eq);
1767 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1768 // Top 8.
1769 tst(scratch, Operand(0xff000000));
1770 add(zeros, zeros, Operand(8), LeaveCC, eq);
1771 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1772 // Top 4.
1773 tst(scratch, Operand(0xf0000000));
1774 add(zeros, zeros, Operand(4), LeaveCC, eq);
1775 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1776 // Top 2.
1777 tst(scratch, Operand(0xc0000000));
1778 add(zeros, zeros, Operand(2), LeaveCC, eq);
1779 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1780 // Top bit.
1781 tst(scratch, Operand(0x80000000u));
1782 add(zeros, zeros, Operand(1), LeaveCC, eq);
1783#endif
1784}
1785
1786
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001787void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1788 Register first,
1789 Register second,
1790 Register scratch1,
1791 Register scratch2,
1792 Label* failure) {
1793 int kFlatAsciiStringMask =
1794 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1795 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1796 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1797 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1798 cmp(scratch1, Operand(kFlatAsciiStringTag));
1799 // Ignore second test if first test failed.
1800 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1801 b(ne, failure);
1802}
1803
1804
1805void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1806 Register scratch,
1807 Label* failure) {
1808 int kFlatAsciiStringMask =
1809 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1810 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1811 and_(scratch, type, Operand(kFlatAsciiStringMask));
1812 cmp(scratch, Operand(kFlatAsciiStringTag));
1813 b(ne, failure);
1814}
1815
1816
ager@chromium.org357bf652010-04-12 11:30:10 +00001817void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001818 int frame_alignment = ActivationFrameAlignment();
ager@chromium.org357bf652010-04-12 11:30:10 +00001819 // Up to four simple arguments are passed in registers r0..r3.
1820 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001821 if (frame_alignment > kPointerSize) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001822 // Make stack end at alignment and make room for num_arguments - 4 words
1823 // and the original value of sp.
1824 mov(scratch, sp);
1825 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001826 ASSERT(IsPowerOf2(frame_alignment));
1827 and_(sp, sp, Operand(-frame_alignment));
ager@chromium.org357bf652010-04-12 11:30:10 +00001828 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1829 } else {
1830 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1831 }
1832}
1833
1834
1835void MacroAssembler::CallCFunction(ExternalReference function,
1836 int num_arguments) {
1837 mov(ip, Operand(function));
1838 CallCFunction(ip, num_arguments);
1839}
1840
1841
1842void MacroAssembler::CallCFunction(Register function, int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001843 // Make sure that the stack is aligned before calling a C function unless
1844 // running in the simulator. The simulator has its own alignment check which
1845 // provides more information.
1846#if defined(V8_HOST_ARCH_ARM)
1847 if (FLAG_debug_code) {
1848 int frame_alignment = OS::ActivationFrameAlignment();
1849 int frame_alignment_mask = frame_alignment - 1;
1850 if (frame_alignment > kPointerSize) {
1851 ASSERT(IsPowerOf2(frame_alignment));
1852 Label alignment_as_expected;
1853 tst(sp, Operand(frame_alignment_mask));
1854 b(eq, &alignment_as_expected);
1855 // Don't use Check here, as it will call Runtime_Abort possibly
1856 // re-entering here.
1857 stop("Unexpected alignment");
1858 bind(&alignment_as_expected);
1859 }
1860 }
1861#endif
1862
ager@chromium.org357bf652010-04-12 11:30:10 +00001863 // Just call directly. The function called cannot cause a GC, or
1864 // allow preemption, so the return address in the link register
1865 // stays correct.
1866 Call(function);
1867 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1868 if (OS::ActivationFrameAlignment() > kPointerSize) {
1869 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1870 } else {
1871 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1872 }
1873}
1874
1875
ager@chromium.org4af710e2009-09-15 12:20:11 +00001876#ifdef ENABLE_DEBUGGER_SUPPORT
1877CodePatcher::CodePatcher(byte* address, int instructions)
1878 : address_(address),
1879 instructions_(instructions),
1880 size_(instructions * Assembler::kInstrSize),
1881 masm_(address, size_ + Assembler::kGap) {
1882 // Create a new macro assembler pointing to the address of the code to patch.
1883 // The size is adjusted with kGap on order for the assembler to generate size
1884 // bytes of instructions without failing with buffer size constraints.
1885 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1886}
1887
1888
1889CodePatcher::~CodePatcher() {
1890 // Indicate that code has changed.
1891 CPU::FlushICache(address_, size_);
1892
1893 // Check that the code was patched as expected.
1894 ASSERT(masm_.pc_ == address_ + size_);
1895 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1896}
1897
1898
1899void CodePatcher::Emit(Instr x) {
1900 masm()->emit(x);
1901}
1902
1903
1904void CodePatcher::Emit(Address addr) {
1905 masm()->emit(reinterpret_cast<Instr>(addr));
1906}
1907#endif // ENABLE_DEBUGGER_SUPPORT
1908
1909
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001911
1912#endif // V8_TARGET_ARCH_ARM