blob: 376c4fbe819261c6143d5d889349a1e111d7c051 [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
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
33#include "runtime.h"
34
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038MacroAssembler::MacroAssembler(void* buffer, int size)
39 : Assembler(buffer, size),
kasper.lund7276f142008-07-30 08:49:36 +000040 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000041 allow_stub_calls_(true),
42 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043}
44
45
46// We always generate arm code, never thumb code, even if V8 is compiled to
47// thumb, so we require inter-working support
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000048#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049#error "flag -mthumb-interwork missing"
50#endif
51
52
53// We do not support thumb inter-working with an arm architecture not supporting
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000054// the blx instruction (below v5t). If you know what CPU you are compiling for
55// you can use -march=armv7 or similar.
56#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
57# error "For thumb inter-working we require an architecture which supports blx"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058#endif
59
60
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061// Using bx does not yield better code, so use it only when required
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000062#if defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063#define USE_BX 1
64#endif
65
66
67void MacroAssembler::Jump(Register target, Condition cond) {
68#if USE_BX
69 bx(target, cond);
70#else
71 mov(pc, Operand(target), LeaveCC, cond);
72#endif
73}
74
75
ager@chromium.org236ad962008-09-25 09:45:57 +000076void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
77 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078#if USE_BX
79 mov(ip, Operand(target, rmode), LeaveCC, cond);
80 bx(ip, cond);
81#else
82 mov(pc, Operand(target, rmode), LeaveCC, cond);
83#endif
84}
85
86
ager@chromium.org236ad962008-09-25 09:45:57 +000087void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
88 Condition cond) {
89 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
91}
92
93
ager@chromium.org236ad962008-09-25 09:45:57 +000094void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
95 Condition cond) {
96 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 // 'code' is always generated ARM code, never THUMB code
98 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
99}
100
101
102void MacroAssembler::Call(Register target, Condition cond) {
103#if USE_BLX
104 blx(target, cond);
105#else
106 // set lr for return at current pc + 8
107 mov(lr, Operand(pc), LeaveCC, cond);
108 mov(pc, Operand(target), LeaveCC, cond);
109#endif
110}
111
112
ager@chromium.org236ad962008-09-25 09:45:57 +0000113void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
114 Condition cond) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000115#if USE_BLX
116 // On ARMv5 and after the recommended call sequence is:
117 // ldr ip, [pc, #...]
118 // blx ip
119
120 // The two instructions (ldr and blx) could be separated by a literal
121 // pool and the code would still work. The issue comes from the
122 // patching code which expect the ldr to be just above the blx.
123 BlockConstPoolFor(2);
124 // Statement positions are expected to be recorded when the target
125 // address is loaded. The mov method will automatically record
126 // positions when pc is the target, since this is not the case here
127 // we have to do it explicitly.
128 WriteRecordedPositions();
129
130 mov(ip, Operand(target, rmode), LeaveCC, cond);
131 blx(ip, cond);
132
133 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
134#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 // Set lr for return at current pc + 8.
136 mov(lr, Operand(pc), LeaveCC, cond);
137 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
138 mov(pc, Operand(target, rmode), LeaveCC, cond);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000139
ager@chromium.org4af710e2009-09-15 12:20:11 +0000140 ASSERT(kCallTargetAddressOffset == kInstrSize);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000141#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142}
143
144
ager@chromium.org236ad962008-09-25 09:45:57 +0000145void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
146 Condition cond) {
147 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
149}
150
151
ager@chromium.org236ad962008-09-25 09:45:57 +0000152void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
153 Condition cond) {
154 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 // 'code' is always generated ARM code, never THUMB code
156 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
157}
158
159
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000160void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000162 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000164 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165#endif
166}
167
168
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000169void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
170 LoadRoot(ip, Heap::kStackLimitRootIndex);
171 cmp(sp, Operand(ip));
172 b(lo, on_stack_overflow);
173}
174
175
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000176void MacroAssembler::Drop(int count, Condition cond) {
177 if (count > 0) {
178 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000179 }
180}
181
182
ager@chromium.org357bf652010-04-12 11:30:10 +0000183void MacroAssembler::Swap(Register reg1, Register reg2, Register scratch) {
184 if (scratch.is(no_reg)) {
185 eor(reg1, reg1, Operand(reg2));
186 eor(reg2, reg2, Operand(reg1));
187 eor(reg1, reg1, Operand(reg2));
188 } else {
189 mov(scratch, reg1);
190 mov(reg1, reg2);
191 mov(reg2, scratch);
192 }
193}
194
195
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000196void MacroAssembler::Call(Label* target) {
197 bl(target);
198}
199
200
201void MacroAssembler::Move(Register dst, Handle<Object> value) {
202 mov(dst, Operand(value));
203}
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000204
205
ager@chromium.org357bf652010-04-12 11:30:10 +0000206void MacroAssembler::Move(Register dst, Register src) {
207 if (!dst.is(src)) {
208 mov(dst, src);
209 }
210}
211
212
ager@chromium.org8bb60582008-12-11 12:02:20 +0000213void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
214 // Empty the const pool.
215 CheckConstPool(true, true);
216 add(pc, pc, Operand(index,
217 LSL,
218 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000219 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000220 nop(); // Jump table alignment.
221 for (int i = 0; i < targets.length(); i++) {
222 b(targets[i]);
223 }
224}
225
226
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000227void MacroAssembler::LoadRoot(Register destination,
228 Heap::RootListIndex index,
229 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000230 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000231}
232
233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234// Will clobber 4 registers: object, offset, scratch, ip. The
235// register 'object' contains a heap object pointer. The heap object
236// tag is shifted away.
237void MacroAssembler::RecordWrite(Register object, Register offset,
238 Register scratch) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000239 // The compiled code assumes that record write doesn't change the
240 // context register, so we check that none of the clobbered
241 // registers are cp.
242 ASSERT(!object.is(cp) && !offset.is(cp) && !scratch.is(cp));
243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 // This is how much we shift the remembered set bit offset to get the
245 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
246 // shift right 5) and then multiply by kIntSize (4, shift left 2).
247 const int kRSetWordShift = 3;
248
249 Label fast, done;
250
kasper.lund7276f142008-07-30 08:49:36 +0000251 // First, test that the object is not in the new space. We cannot set
252 // remembered set bits in the new space.
253 // object: heap object pointer (with tag)
254 // offset: offset to store location from the object
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000255 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 cmp(scratch, Operand(ExternalReference::new_space_start()));
257 b(eq, &done);
258
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000260 // object: heap object pointer (with tag)
261 // offset: offset to store location from the object
262 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
263 and_(scratch, object, Operand(ip)); // offset into page of the object
264 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
266
267 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000268 // object: heap object pointer (with tag)
269 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 bic(object, object, Operand(ip));
271
272 // If the bit offset lies beyond the normal remembered set range, it is in
273 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000274 // object: page start
275 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 cmp(offset, Operand(Page::kPageSize / kPointerSize));
277 b(lt, &fast);
278
279 // Adjust the bit offset to be relative to the start of the extra
280 // remembered set and the start address to be the address of the extra
281 // remembered set.
282 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
283 // Load the array length into 'scratch' and multiply by four to get the
284 // size in bytes of the elements.
285 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
286 + FixedArray::kLengthOffset));
287 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
288 // Add the page header (including remembered set), array header, and array
289 // body size to the page address.
290 add(object, object, Operand(Page::kObjectStartOffset
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000291 + FixedArray::kHeaderSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 add(object, object, Operand(scratch));
293
294 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000296 // object: start of the remembered set (page start for the fast case)
297 // offset: bit offset of store position in the remembered set
298 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
299 add(object, object, Operand(scratch, LSR, kRSetWordShift));
300 // Get bit offset in the rset word.
301 // object: address of remembered set word
302 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 and_(offset, offset, Operand(kBitsPerInt - 1));
304
305 ldr(scratch, MemOperand(object));
306 mov(ip, Operand(1));
307 orr(scratch, scratch, Operand(ip, LSL, offset));
308 str(scratch, MemOperand(object));
309
310 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311
312 // Clobber all input registers when running with the debug-code flag
313 // turned on to provoke errors.
314 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000315 mov(object, Operand(BitCast<int32_t>(kZapValue)));
316 mov(offset, Operand(BitCast<int32_t>(kZapValue)));
317 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319}
320
321
ager@chromium.org7c537e22008-10-16 08:43:32 +0000322void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000323 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000324 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
325 mov(ip, Operand(Smi::FromInt(type)));
326 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000327 mov(ip, Operand(CodeObject()));
328 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000329 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330}
331
332
ager@chromium.org7c537e22008-10-16 08:43:32 +0000333void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000334 // r0: preserved
335 // r1: preserved
336 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
ager@chromium.org7c537e22008-10-16 08:43:32 +0000338 // Drop the execution stack down to the frame pointer and restore
339 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000340 mov(sp, fp);
341 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342}
343
344
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000345void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000346 // Compute the argv pointer and keep it in a callee-saved register.
347 // r0 is argc.
348 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
349 sub(r6, r6, Operand(kPointerSize));
350
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000351 // Compute callee's stack pointer before making changes and save it as
352 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000353 // popping the args.
354
355 // ip = sp + kPointerSize * #args;
356 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
357
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000358 // Prepare the stack to be aligned when calling into C. After this point there
359 // are 5 pushes before the call into C, so the stack needs to be aligned after
360 // 5 pushes.
361 int frame_alignment = ActivationFrameAlignment();
362 int frame_alignment_mask = frame_alignment - 1;
363 if (frame_alignment != kPointerSize) {
364 // The following code needs to be more general if this assert does not hold.
365 ASSERT(frame_alignment == 2 * kPointerSize);
366 // With 5 pushes left the frame must be unaligned at this point.
367 mov(r7, Operand(Smi::FromInt(0)));
368 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
369 push(r7, eq); // Push if aligned to make it unaligned.
370 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000371
ager@chromium.org236ad962008-09-25 09:45:57 +0000372 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
373 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +0000374 mov(fp, Operand(sp)); // Setup new frame pointer.
ager@chromium.org236ad962008-09-25 09:45:57 +0000375
ager@chromium.org5c838252010-02-19 08:53:10 +0000376 mov(ip, Operand(CodeObject()));
377 push(ip); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000378
379 // Save the frame pointer and the context in top.
380 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
381 str(fp, MemOperand(ip));
382 mov(ip, Operand(ExternalReference(Top::k_context_address)));
383 str(cp, MemOperand(ip));
384
385 // Setup argc and the builtin function in callee-saved registers.
386 mov(r4, Operand(r0));
387 mov(r5, Operand(r1));
388
ager@chromium.org236ad962008-09-25 09:45:57 +0000389
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000390#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000391 // Save the state of all registers to the stack from the memory
392 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000393 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000394 // Use sp as base to push.
395 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
396 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000397#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000398}
399
400
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000401int MacroAssembler::ActivationFrameAlignment() {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000402#if defined(V8_HOST_ARCH_ARM)
403 // Running on the real platform. Use the alignment as mandated by the local
404 // environment.
405 // Note: This will break if we ever start generating snapshots on one ARM
406 // platform for another ARM platform with a different alignment.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000407 return OS::ActivationFrameAlignment();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000408#else // defined(V8_HOST_ARCH_ARM)
409 // If we are using the simulator then we should always align to the expected
410 // alignment. As the simulator is used to generate snapshots we do not know
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000411 // if the target platform will need alignment, so this is controlled from a
412 // flag.
413 return FLAG_sim_stack_alignment;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000414#endif // defined(V8_HOST_ARCH_ARM)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000415}
416
417
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000418void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000419#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000420 // Restore the memory copy of the registers by digging them out from
421 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000422 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000423 // This code intentionally clobbers r2 and r3.
424 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000425 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000426 add(r3, fp, Operand(kOffset));
427 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
428 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000429#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000430
431 // Clear top frame.
432 mov(r3, Operand(0));
433 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
434 str(r3, MemOperand(ip));
435
436 // Restore current context from top and clear it in debug mode.
437 mov(ip, Operand(ExternalReference(Top::k_context_address)));
438 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000439#ifdef DEBUG
440 str(r3, MemOperand(ip));
441#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000442
443 // Pop the arguments, restore registers, and return.
444 mov(sp, Operand(fp)); // respect ABI stack constraint
445 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
446}
447
448
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000449void MacroAssembler::InvokePrologue(const ParameterCount& expected,
450 const ParameterCount& actual,
451 Handle<Code> code_constant,
452 Register code_reg,
453 Label* done,
454 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000455 bool definitely_matches = false;
456 Label regular_invoke;
457
458 // Check whether the expected and actual arguments count match. If not,
459 // setup registers according to contract with ArgumentsAdaptorTrampoline:
460 // r0: actual arguments count
461 // r1: function (passed through to callee)
462 // r2: expected arguments count
463 // r3: callee code entry
464
465 // The code below is made a lot easier because the calling code already sets
466 // up actual and expected registers according to the contract if values are
467 // passed in registers.
468 ASSERT(actual.is_immediate() || actual.reg().is(r0));
469 ASSERT(expected.is_immediate() || expected.reg().is(r2));
470 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
471
472 if (expected.is_immediate()) {
473 ASSERT(actual.is_immediate());
474 if (expected.immediate() == actual.immediate()) {
475 definitely_matches = true;
476 } else {
477 mov(r0, Operand(actual.immediate()));
478 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
479 if (expected.immediate() == sentinel) {
480 // Don't worry about adapting arguments for builtins that
481 // don't want that done. Skip adaption code by making it look
482 // like we have a match between expected and actual number of
483 // arguments.
484 definitely_matches = true;
485 } else {
486 mov(r2, Operand(expected.immediate()));
487 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000488 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000489 } else {
490 if (actual.is_immediate()) {
491 cmp(expected.reg(), Operand(actual.immediate()));
492 b(eq, &regular_invoke);
493 mov(r0, Operand(actual.immediate()));
494 } else {
495 cmp(expected.reg(), Operand(actual.reg()));
496 b(eq, &regular_invoke);
497 }
498 }
499
500 if (!definitely_matches) {
501 if (!code_constant.is_null()) {
502 mov(r3, Operand(code_constant));
503 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
504 }
505
506 Handle<Code> adaptor =
507 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
508 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000509 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000510 b(done);
511 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000512 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000513 }
514 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000515 }
516}
517
518
519void MacroAssembler::InvokeCode(Register code,
520 const ParameterCount& expected,
521 const ParameterCount& actual,
522 InvokeFlag flag) {
523 Label done;
524
525 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
526 if (flag == CALL_FUNCTION) {
527 Call(code);
528 } else {
529 ASSERT(flag == JUMP_FUNCTION);
530 Jump(code);
531 }
532
533 // Continue here if InvokePrologue does handle the invocation due to
534 // mismatched parameter counts.
535 bind(&done);
536}
537
538
539void MacroAssembler::InvokeCode(Handle<Code> code,
540 const ParameterCount& expected,
541 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000542 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000543 InvokeFlag flag) {
544 Label done;
545
546 InvokePrologue(expected, actual, code, no_reg, &done, flag);
547 if (flag == CALL_FUNCTION) {
548 Call(code, rmode);
549 } else {
550 Jump(code, rmode);
551 }
552
553 // Continue here if InvokePrologue does handle the invocation due to
554 // mismatched parameter counts.
555 bind(&done);
556}
557
558
559void MacroAssembler::InvokeFunction(Register fun,
560 const ParameterCount& actual,
561 InvokeFlag flag) {
562 // Contract with called JS functions requires that function is passed in r1.
563 ASSERT(fun.is(r1));
564
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000565 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000566 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000567
568 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
569 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
570 ldr(expected_reg,
571 FieldMemOperand(code_reg,
572 SharedFunctionInfo::kFormalParameterCountOffset));
573 ldr(code_reg,
574 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
575 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
576
577 ParameterCount expected(expected_reg);
578 InvokeCode(code_reg, expected, actual, flag);
579}
580
581
ager@chromium.org5c838252010-02-19 08:53:10 +0000582void MacroAssembler::InvokeFunction(JSFunction* function,
583 const ParameterCount& actual,
584 InvokeFlag flag) {
585 ASSERT(function->is_compiled());
586
587 // Get the function and setup the context.
588 mov(r1, Operand(Handle<JSFunction>(function)));
589 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
590
591 // Invoke the cached code.
592 Handle<Code> code(function->code());
593 ParameterCount expected(function->shared()->formal_parameter_count());
594 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
595}
596
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000597#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598void MacroAssembler::SaveRegistersToMemory(RegList regs) {
599 ASSERT((regs & ~kJSCallerSaved) == 0);
600 // Copy the content of registers to memory location.
601 for (int i = 0; i < kNumJSCallerSaved; i++) {
602 int r = JSCallerSavedCode(i);
603 if ((regs & (1 << r)) != 0) {
604 Register reg = { r };
605 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
606 str(reg, MemOperand(ip));
607 }
608 }
609}
610
611
612void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
613 ASSERT((regs & ~kJSCallerSaved) == 0);
614 // Copy the content of memory location to registers.
615 for (int i = kNumJSCallerSaved; --i >= 0;) {
616 int r = JSCallerSavedCode(i);
617 if ((regs & (1 << r)) != 0) {
618 Register reg = { r };
619 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
620 ldr(reg, MemOperand(ip));
621 }
622 }
623}
624
625
626void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
627 RegList regs) {
628 ASSERT((regs & ~kJSCallerSaved) == 0);
629 // Copy the content of the memory location to the stack and adjust base.
630 for (int i = kNumJSCallerSaved; --i >= 0;) {
631 int r = JSCallerSavedCode(i);
632 if ((regs & (1 << r)) != 0) {
633 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
634 ldr(ip, MemOperand(ip));
635 str(ip, MemOperand(base, 4, NegPreIndex));
636 }
637 }
638}
639
640
641void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
642 Register scratch,
643 RegList regs) {
644 ASSERT((regs & ~kJSCallerSaved) == 0);
645 // Copy the content of the stack to the memory location and adjust base.
646 for (int i = 0; i < kNumJSCallerSaved; i++) {
647 int r = JSCallerSavedCode(i);
648 if ((regs & (1 << r)) != 0) {
649 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
650 ldr(scratch, MemOperand(base, 4, PostIndex));
651 str(scratch, MemOperand(ip));
652 }
653 }
654}
ager@chromium.org5c838252010-02-19 08:53:10 +0000655
656
657void MacroAssembler::DebugBreak() {
658 ASSERT(allow_stub_calls());
659 mov(r0, Operand(0));
660 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
661 CEntryStub ces(1);
662 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
663}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000664#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000666
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667void MacroAssembler::PushTryHandler(CodeLocation try_location,
668 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000669 // Adjust this code if not the case.
670 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 // The pc (return address) is passed in register lr.
672 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 if (type == TRY_CATCH_HANDLER) {
674 mov(r3, Operand(StackHandler::TRY_CATCH));
675 } else {
676 mov(r3, Operand(StackHandler::TRY_FINALLY));
677 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000678 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
679 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
680 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
681 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
682 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
684 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000685 ASSERT(StackHandlerConstants::kNextOffset == 0);
686 push(r1);
687 // Link this handler as the new current one.
688 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000690 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000692 // The frame pointer does not point to a JS frame so we save NULL
693 // for fp. We expect the code throwing an exception to check fp
694 // before dereferencing it to restore the context.
695 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000697 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
698 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
699 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
700 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
701 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
703 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000704 ASSERT(StackHandlerConstants::kNextOffset == 0);
705 push(r6);
706 // Link this handler as the new current one.
707 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 }
709}
710
711
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000712void MacroAssembler::PopTryHandler() {
713 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
714 pop(r1);
715 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
716 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
717 str(r1, MemOperand(ip));
718}
719
720
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
722 JSObject* holder, Register holder_reg,
723 Register scratch,
724 Label* miss) {
725 // Make sure there's no overlap between scratch and the other
726 // registers.
727 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
728
729 // Keep track of the current object in register reg.
730 Register reg = object_reg;
731 int depth = 1;
732
733 // Check the maps in the prototype chain.
734 // Traverse the prototype chain from the object and do map checks.
735 while (object != holder) {
736 depth++;
737
738 // Only global objects and objects that do not require access
739 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000740 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741
742 // Get the map of the current object.
743 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
744 cmp(scratch, Operand(Handle<Map>(object->map())));
745
746 // Branch on the result of the map check.
747 b(ne, miss);
748
749 // Check access rights to the global object. This has to happen
750 // after the map check so that we know that the object is
751 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000752 if (object->IsJSGlobalProxy()) {
753 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 // Restore scratch register to be the map of the object. In the
755 // new space case below, we load the prototype from the map in
756 // the scratch register.
757 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
758 }
759
760 reg = holder_reg; // from now the object is in holder_reg
761 JSObject* prototype = JSObject::cast(object->GetPrototype());
762 if (Heap::InNewSpace(prototype)) {
763 // The prototype is in new space; we cannot store a reference
764 // to it in the code. Load it from the map.
765 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
766 } else {
767 // The prototype is in old space; load it directly.
768 mov(reg, Operand(Handle<JSObject>(prototype)));
769 }
770
771 // Go to the next object in the prototype chain.
772 object = prototype;
773 }
774
775 // Check the holder map.
776 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
777 cmp(scratch, Operand(Handle<Map>(object->map())));
778 b(ne, miss);
779
780 // Log the check depth.
781 LOG(IntEvent("check-maps-depth", depth));
782
783 // Perform security check for access to the global object and return
784 // the holder register.
785 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000786 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
787 if (object->IsJSGlobalProxy()) {
788 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 }
790 return reg;
791}
792
793
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000794void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
795 Register scratch,
796 Label* miss) {
797 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000799 ASSERT(!holder_reg.is(scratch));
800 ASSERT(!holder_reg.is(ip));
801 ASSERT(!scratch.is(ip));
802
803 // Load current lexical context from the stack frame.
804 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
805 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000806#ifdef DEBUG
807 cmp(scratch, Operand(0));
808 Check(ne, "we should not have an empty lexical context");
809#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000811 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
813 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000814 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
815
816 // Check the context is a global context.
817 if (FLAG_debug_code) {
818 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
819 // Cannot use ip as a temporary in this verification code. Due to the fact
820 // that ip is clobbered as part of cmp with an object Operand.
821 push(holder_reg); // Temporarily save holder on the stack.
822 // Read the first word and compare to the global_context_map.
823 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000824 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
825 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000826 Check(eq, "JSGlobalObject::global_context should be a global context.");
827 pop(holder_reg); // Restore holder.
828 }
829
830 // Check if both contexts are the same.
831 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
832 cmp(scratch, Operand(ip));
833 b(eq, &same_contexts);
834
835 // Check the context is a global context.
836 if (FLAG_debug_code) {
837 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
838 // Cannot use ip as a temporary in this verification code. Due to the fact
839 // that ip is clobbered as part of cmp with an object Operand.
840 push(holder_reg); // Temporarily save holder on the stack.
841 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000842 LoadRoot(ip, Heap::kNullValueRootIndex);
843 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000844 Check(ne, "JSGlobalProxy::context() should not be null.");
845
846 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000847 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
848 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000849 Check(eq, "JSGlobalObject::global_context should be a global context.");
850 // Restore ip is not needed. ip is reloaded below.
851 pop(holder_reg); // Restore holder.
852 // Restore ip to holder's context.
853 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
854 }
855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 // Check that the security token in the calling global object is
857 // compatible with the security token in the receiving global
858 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000859 int token_offset = Context::kHeaderSize +
860 Context::SECURITY_TOKEN_INDEX * kPointerSize;
861
862 ldr(scratch, FieldMemOperand(scratch, token_offset));
863 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 cmp(scratch, Operand(ip));
865 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000866
867 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868}
869
870
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000871void MacroAssembler::AllocateInNewSpace(int object_size,
872 Register result,
873 Register scratch1,
874 Register scratch2,
875 Label* gc_required,
876 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000877 ASSERT(!result.is(scratch1));
878 ASSERT(!scratch1.is(scratch2));
879
880 // Load address of new object into result and allocation top address into
881 // scratch1.
882 ExternalReference new_space_allocation_top =
883 ExternalReference::new_space_allocation_top_address();
884 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000885 if ((flags & RESULT_CONTAINS_TOP) == 0) {
886 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000887 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000888 // Assert that result actually contains top on entry. scratch2 is used
889 // immediately below so this use of scratch2 does not cause difference with
890 // respect to register content between debug and release mode.
891 ldr(scratch2, MemOperand(scratch1));
892 cmp(result, scratch2);
893 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +0000894 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000895
896 // Calculate new top and bail out if new space is exhausted. Use result
897 // to calculate the new top.
898 ExternalReference new_space_allocation_limit =
899 ExternalReference::new_space_allocation_limit_address();
900 mov(scratch2, Operand(new_space_allocation_limit));
901 ldr(scratch2, MemOperand(scratch2));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000902 add(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000903 cmp(result, Operand(scratch2));
904 b(hi, gc_required);
905
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000906 // Update allocation top. result temporarily holds the new top.
907 if (FLAG_debug_code) {
908 tst(result, Operand(kObjectAlignmentMask));
909 Check(eq, "Unaligned allocation in new space");
910 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000911 str(result, MemOperand(scratch1));
912
913 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000914 if ((flags & TAG_OBJECT) != 0) {
915 sub(result, result, Operand((object_size * kPointerSize) -
916 kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000917 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000918 sub(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000919 }
920}
921
922
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000923void MacroAssembler::AllocateInNewSpace(Register object_size,
924 Register result,
925 Register scratch1,
926 Register scratch2,
927 Label* gc_required,
928 AllocationFlags flags) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000929 ASSERT(!result.is(scratch1));
930 ASSERT(!scratch1.is(scratch2));
931
932 // Load address of new object into result and allocation top address into
933 // scratch1.
934 ExternalReference new_space_allocation_top =
935 ExternalReference::new_space_allocation_top_address();
936 mov(scratch1, Operand(new_space_allocation_top));
937 if ((flags & RESULT_CONTAINS_TOP) == 0) {
938 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000939 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000940 // Assert that result actually contains top on entry. scratch2 is used
941 // immediately below so this use of scratch2 does not cause difference with
942 // respect to register content between debug and release mode.
943 ldr(scratch2, MemOperand(scratch1));
944 cmp(result, scratch2);
945 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +0000946 }
947
948 // Calculate new top and bail out if new space is exhausted. Use result
949 // to calculate the new top. Object size is in words so a shift is required to
950 // get the number of bytes
951 ExternalReference new_space_allocation_limit =
952 ExternalReference::new_space_allocation_limit_address();
953 mov(scratch2, Operand(new_space_allocation_limit));
954 ldr(scratch2, MemOperand(scratch2));
955 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
956 cmp(result, Operand(scratch2));
957 b(hi, gc_required);
958
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000959 // Update allocation top. result temporarily holds the new top.
960 if (FLAG_debug_code) {
961 tst(result, Operand(kObjectAlignmentMask));
962 Check(eq, "Unaligned allocation in new space");
963 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000964 str(result, MemOperand(scratch1));
965
966 // Adjust back to start of new object.
967 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
968
969 // Tag object if requested.
970 if ((flags & TAG_OBJECT) != 0) {
971 add(result, result, Operand(kHeapObjectTag));
972 }
973}
974
975
976void MacroAssembler::UndoAllocationInNewSpace(Register object,
977 Register scratch) {
978 ExternalReference new_space_allocation_top =
979 ExternalReference::new_space_allocation_top_address();
980
981 // Make sure the object has no tag before resetting top.
982 and_(object, object, Operand(~kHeapObjectTagMask));
983#ifdef DEBUG
984 // Check that the object un-allocated is below the current top.
985 mov(scratch, Operand(new_space_allocation_top));
986 ldr(scratch, MemOperand(scratch));
987 cmp(object, scratch);
988 Check(lt, "Undo allocation of non allocated memory");
989#endif
990 // Write the address of the object to un-allocate as the current top.
991 mov(scratch, Operand(new_space_allocation_top));
992 str(object, MemOperand(scratch));
993}
994
995
ager@chromium.org5c838252010-02-19 08:53:10 +0000996void MacroAssembler::AllocateTwoByteString(Register result,
997 Register length,
998 Register scratch1,
999 Register scratch2,
1000 Register scratch3,
1001 Label* gc_required) {
1002 // Calculate the number of bytes needed for the characters in the string while
1003 // observing object alignment.
1004 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1005 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1006 add(scratch1, scratch1,
1007 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
1008 // AllocateInNewSpace expects the size in words, so we can round down
1009 // to kObjectAlignment and divide by kPointerSize in the same shift.
1010 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
1011 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
1012
1013 // Allocate two-byte string in new space.
1014 AllocateInNewSpace(scratch1,
1015 result,
1016 scratch2,
1017 scratch3,
1018 gc_required,
1019 TAG_OBJECT);
1020
1021 // Set the map, length and hash field.
1022 LoadRoot(scratch1, Heap::kStringMapRootIndex);
1023 str(length, FieldMemOperand(result, String::kLengthOffset));
1024 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1025 mov(scratch2, Operand(String::kEmptyHashField));
1026 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1027}
1028
1029
1030void MacroAssembler::AllocateAsciiString(Register result,
1031 Register length,
1032 Register scratch1,
1033 Register scratch2,
1034 Register scratch3,
1035 Label* gc_required) {
1036 // Calculate the number of bytes needed for the characters in the string while
1037 // observing object alignment.
1038 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1039 ASSERT(kCharSize == 1);
1040 add(scratch1, length,
1041 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
1042 // AllocateInNewSpace expects the size in words, so we can round down
1043 // to kObjectAlignment and divide by kPointerSize in the same shift.
1044 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
1045 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
1046
1047 // Allocate ASCII string in new space.
1048 AllocateInNewSpace(scratch1,
1049 result,
1050 scratch2,
1051 scratch3,
1052 gc_required,
1053 TAG_OBJECT);
1054
1055 // Set the map, length and hash field.
1056 LoadRoot(scratch1, Heap::kAsciiStringMapRootIndex);
1057 mov(scratch1, Operand(Factory::ascii_string_map()));
1058 str(length, FieldMemOperand(result, String::kLengthOffset));
1059 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1060 mov(scratch2, Operand(String::kEmptyHashField));
1061 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1062}
1063
1064
1065void MacroAssembler::AllocateTwoByteConsString(Register result,
1066 Register length,
1067 Register scratch1,
1068 Register scratch2,
1069 Label* gc_required) {
1070 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1071 result,
1072 scratch1,
1073 scratch2,
1074 gc_required,
1075 TAG_OBJECT);
1076 LoadRoot(scratch1, Heap::kConsStringMapRootIndex);
1077 mov(scratch2, Operand(String::kEmptyHashField));
1078 str(length, FieldMemOperand(result, String::kLengthOffset));
1079 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1080 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1081}
1082
1083
1084void MacroAssembler::AllocateAsciiConsString(Register result,
1085 Register length,
1086 Register scratch1,
1087 Register scratch2,
1088 Label* gc_required) {
1089 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1090 result,
1091 scratch1,
1092 scratch2,
1093 gc_required,
1094 TAG_OBJECT);
1095 LoadRoot(scratch1, Heap::kConsAsciiStringMapRootIndex);
1096 mov(scratch2, Operand(String::kEmptyHashField));
1097 str(length, FieldMemOperand(result, String::kLengthOffset));
1098 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1099 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1100}
1101
1102
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001103void MacroAssembler::CompareObjectType(Register function,
1104 Register map,
1105 Register type_reg,
1106 InstanceType type) {
1107 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001108 CompareInstanceType(map, type_reg, type);
1109}
1110
1111
1112void MacroAssembler::CompareInstanceType(Register map,
1113 Register type_reg,
1114 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001115 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1116 cmp(type_reg, Operand(type));
1117}
1118
1119
ager@chromium.org5c838252010-02-19 08:53:10 +00001120void MacroAssembler::CheckMap(Register obj,
1121 Register scratch,
1122 Handle<Map> map,
1123 Label* fail,
1124 bool is_heap_object) {
1125 if (!is_heap_object) {
1126 BranchOnSmi(obj, fail);
1127 }
1128 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1129 mov(ip, Operand(map));
1130 cmp(scratch, ip);
1131 b(ne, fail);
1132}
1133
1134
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001135void MacroAssembler::TryGetFunctionPrototype(Register function,
1136 Register result,
1137 Register scratch,
1138 Label* miss) {
1139 // Check that the receiver isn't a smi.
1140 BranchOnSmi(function, miss);
1141
1142 // Check that the function really is a function. Load map into result reg.
1143 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1144 b(ne, miss);
1145
1146 // Make sure that the function has an instance prototype.
1147 Label non_instance;
1148 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1149 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1150 b(ne, &non_instance);
1151
1152 // Get the prototype or initial map from the function.
1153 ldr(result,
1154 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1155
1156 // If the prototype or initial map is the hole, don't return it and
1157 // simply miss the cache instead. This will allow us to allocate a
1158 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001159 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1160 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001161 b(eq, miss);
1162
1163 // If the function does not have an initial map, we're done.
1164 Label done;
1165 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1166 b(ne, &done);
1167
1168 // Get the prototype from the initial map.
1169 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1170 jmp(&done);
1171
1172 // Non-instance prototype: Fetch prototype from constructor field
1173 // in initial map.
1174 bind(&non_instance);
1175 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1176
1177 // All done.
1178 bind(&done);
1179}
1180
1181
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001182void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +00001183 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001184 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185}
1186
1187
ager@chromium.org5c838252010-02-19 08:53:10 +00001188void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1189 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1190 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1191}
1192
1193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194void MacroAssembler::StubReturn(int argc) {
1195 ASSERT(argc >= 1 && generating_stub());
ager@chromium.org5c838252010-02-19 08:53:10 +00001196 if (argc > 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197 add(sp, sp, Operand((argc - 1) * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00001198 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 Ret();
1200}
1201
mads.s.ager31e71382008-08-13 09:32:07 +00001202
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001203void MacroAssembler::IllegalOperation(int num_arguments) {
1204 if (num_arguments > 0) {
1205 add(sp, sp, Operand(num_arguments * kPointerSize));
1206 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001207 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001208}
1209
1210
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001211void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1212 Register outHighReg,
1213 Register outLowReg) {
1214 // ARMv7 VFP3 instructions to implement integer to double conversion.
1215 mov(r7, Operand(inReg, ASR, kSmiTagSize));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001216 vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001217 vcvt_f64_s32(d7, s15);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001218 vmov(outLowReg, outHighReg, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001219}
1220
1221
ager@chromium.org5c838252010-02-19 08:53:10 +00001222void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1223 Register src,
1224 int num_least_bits) {
1225 if (CpuFeatures::IsSupported(ARMv7)) {
1226 ubfx(dst, src, Operand(kSmiTagSize), Operand(num_least_bits - 1));
1227 } else {
1228 mov(dst, Operand(src, ASR, kSmiTagSize));
1229 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1230 }
1231}
1232
1233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001235 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001237 // If the expected number of arguments of the runtime function is
1238 // constant, we check that the actual number of arguments match the
1239 // expectation.
1240 if (f->nargs >= 0 && f->nargs != num_arguments) {
1241 IllegalOperation(num_arguments);
1242 return;
1243 }
kasper.lund7276f142008-07-30 08:49:36 +00001244
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 // TODO(1236192): Most runtime routines don't need the number of
1246 // arguments passed in because it is constant. At some point we
1247 // should remove this need and make the runtime routine entry code
1248 // smarter.
1249 mov(r0, Operand(num_arguments));
1250 mov(r1, Operand(ExternalReference(f)));
1251 CEntryStub stub(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252 CallStub(&stub);
1253}
1254
1255
1256void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1257 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1258}
1259
1260
ager@chromium.org5c838252010-02-19 08:53:10 +00001261void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1262 int num_arguments) {
1263 mov(r0, Operand(num_arguments));
1264 mov(r1, Operand(ext));
1265
1266 CEntryStub stub(1);
1267 CallStub(&stub);
1268}
1269
1270
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001271void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1272 int num_arguments,
1273 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001274 // TODO(1236192): Most runtime routines don't need the number of
1275 // arguments passed in because it is constant. At some point we
1276 // should remove this need and make the runtime routine entry code
1277 // smarter.
1278 mov(r0, Operand(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001279 JumpToExternalReference(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280}
1281
1282
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001283void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1284 int num_arguments,
1285 int result_size) {
1286 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1287}
1288
1289
1290void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291#if defined(__thumb__)
1292 // Thumb mode builtin.
1293 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1294#endif
1295 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001296 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001297 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298}
1299
1300
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001301void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1302 InvokeJSFlags flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001303 GetBuiltinEntry(r2, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 if (flags == CALL_JS) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001305 Call(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 } else {
1307 ASSERT(flags == JUMP_JS);
ager@chromium.org5c838252010-02-19 08:53:10 +00001308 Jump(r2);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001309 }
1310}
1311
1312
1313void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001314 ASSERT(!target.is(r1));
1315
1316 // Load the builtins object into target register.
1317 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1318 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1319
ager@chromium.org5c838252010-02-19 08:53:10 +00001320 // Load the JavaScript builtin function from the builtins object.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001321 ldr(r1, FieldMemOperand(target,
1322 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1323
1324 // Load the code entry point from the builtins object.
1325 ldr(target, FieldMemOperand(target,
1326 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1327 if (FLAG_debug_code) {
1328 // Make sure the code objects in the builtins object and in the
1329 // builtin function are the same.
1330 push(r1);
1331 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1332 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1333 cmp(r1, target);
1334 Assert(eq, "Builtin code object changed");
1335 pop(r1);
1336 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001337 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338}
1339
1340
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001341void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1342 Register scratch1, Register scratch2) {
1343 if (FLAG_native_code_counters && counter->Enabled()) {
1344 mov(scratch1, Operand(value));
1345 mov(scratch2, Operand(ExternalReference(counter)));
1346 str(scratch1, MemOperand(scratch2));
1347 }
1348}
1349
1350
1351void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1352 Register scratch1, Register scratch2) {
1353 ASSERT(value > 0);
1354 if (FLAG_native_code_counters && counter->Enabled()) {
1355 mov(scratch2, Operand(ExternalReference(counter)));
1356 ldr(scratch1, MemOperand(scratch2));
1357 add(scratch1, scratch1, Operand(value));
1358 str(scratch1, MemOperand(scratch2));
1359 }
1360}
1361
1362
1363void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1364 Register scratch1, Register scratch2) {
1365 ASSERT(value > 0);
1366 if (FLAG_native_code_counters && counter->Enabled()) {
1367 mov(scratch2, Operand(ExternalReference(counter)));
1368 ldr(scratch1, MemOperand(scratch2));
1369 sub(scratch1, scratch1, Operand(value));
1370 str(scratch1, MemOperand(scratch2));
1371 }
1372}
1373
1374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375void MacroAssembler::Assert(Condition cc, const char* msg) {
1376 if (FLAG_debug_code)
1377 Check(cc, msg);
1378}
1379
1380
1381void MacroAssembler::Check(Condition cc, const char* msg) {
1382 Label L;
1383 b(cc, &L);
1384 Abort(msg);
1385 // will not return here
1386 bind(&L);
1387}
1388
1389
1390void MacroAssembler::Abort(const char* msg) {
1391 // We want to pass the msg string like a smi to avoid GC
1392 // problems, however msg is not guaranteed to be aligned
1393 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001394 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 // from the real pointer as a smi.
1396 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1397 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1398 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1399#ifdef DEBUG
1400 if (msg != NULL) {
1401 RecordComment("Abort message: ");
1402 RecordComment(msg);
1403 }
1404#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001405 // Disable stub call restrictions to always allow calls to abort.
1406 set_allow_stub_calls(true);
1407
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 mov(r0, Operand(p0));
1409 push(r0);
1410 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001411 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 CallRuntime(Runtime::kAbort, 2);
1413 // will not return here
1414}
1415
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001416
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001417void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1418 if (context_chain_length > 0) {
1419 // Move up the chain of contexts to the context containing the slot.
1420 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1421 // Load the function context (which is the incoming, outer context).
1422 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1423 for (int i = 1; i < context_chain_length; i++) {
1424 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1425 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1426 }
1427 // The context may be an intermediate context, not a function context.
1428 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1429 } else { // Slot is in the current function context.
1430 // The context may be an intermediate context, not a function context.
1431 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1432 }
1433}
1434
1435
ager@chromium.org5c838252010-02-19 08:53:10 +00001436void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1437 Register reg2,
1438 Label* on_not_both_smi) {
1439 ASSERT_EQ(0, kSmiTag);
1440 tst(reg1, Operand(kSmiTagMask));
1441 tst(reg2, Operand(kSmiTagMask), eq);
1442 b(ne, on_not_both_smi);
1443}
1444
1445
1446void MacroAssembler::JumpIfEitherSmi(Register reg1,
1447 Register reg2,
1448 Label* on_either_smi) {
1449 ASSERT_EQ(0, kSmiTag);
1450 tst(reg1, Operand(kSmiTagMask));
1451 tst(reg2, Operand(kSmiTagMask), ne);
1452 b(eq, on_either_smi);
1453}
1454
1455
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001456void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1457 Register first,
1458 Register second,
1459 Register scratch1,
1460 Register scratch2,
1461 Label* failure) {
1462 // Test that both first and second are sequential ASCII strings.
1463 // Assume that they are non-smis.
1464 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1465 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1466 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1467 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001468
1469 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1470 scratch2,
1471 scratch1,
1472 scratch2,
1473 failure);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001474}
1475
1476void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1477 Register second,
1478 Register scratch1,
1479 Register scratch2,
1480 Label* failure) {
1481 // Check that neither is a smi.
1482 ASSERT_EQ(0, kSmiTag);
1483 and_(scratch1, first, Operand(second));
1484 tst(scratch1, Operand(kSmiTagMask));
1485 b(eq, failure);
1486 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1487 second,
1488 scratch1,
1489 scratch2,
1490 failure);
1491}
1492
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001493
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001494// Allocates a heap number or jumps to the need_gc label if the young space
1495// is full and a scavenge is needed.
1496void MacroAssembler::AllocateHeapNumber(Register result,
1497 Register scratch1,
1498 Register scratch2,
1499 Label* gc_required) {
1500 // Allocate an object in the heap for the heap number and tag it as a heap
1501 // object.
1502 AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
1503 result,
1504 scratch1,
1505 scratch2,
1506 gc_required,
1507 TAG_OBJECT);
1508
1509 // Get heap number map and store it in the allocated object.
1510 LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
1511 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1512}
1513
1514
1515void MacroAssembler::CountLeadingZeros(Register source,
1516 Register scratch,
1517 Register zeros) {
1518#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1519 clz(zeros, source); // This instruction is only supported after ARM5.
1520#else
1521 mov(zeros, Operand(0));
1522 mov(scratch, source);
1523 // Top 16.
1524 tst(scratch, Operand(0xffff0000));
1525 add(zeros, zeros, Operand(16), LeaveCC, eq);
1526 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1527 // Top 8.
1528 tst(scratch, Operand(0xff000000));
1529 add(zeros, zeros, Operand(8), LeaveCC, eq);
1530 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1531 // Top 4.
1532 tst(scratch, Operand(0xf0000000));
1533 add(zeros, zeros, Operand(4), LeaveCC, eq);
1534 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1535 // Top 2.
1536 tst(scratch, Operand(0xc0000000));
1537 add(zeros, zeros, Operand(2), LeaveCC, eq);
1538 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1539 // Top bit.
1540 tst(scratch, Operand(0x80000000u));
1541 add(zeros, zeros, Operand(1), LeaveCC, eq);
1542#endif
1543}
1544
1545
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001546void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1547 Register first,
1548 Register second,
1549 Register scratch1,
1550 Register scratch2,
1551 Label* failure) {
1552 int kFlatAsciiStringMask =
1553 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1554 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1555 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1556 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1557 cmp(scratch1, Operand(kFlatAsciiStringTag));
1558 // Ignore second test if first test failed.
1559 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1560 b(ne, failure);
1561}
1562
1563
1564void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1565 Register scratch,
1566 Label* failure) {
1567 int kFlatAsciiStringMask =
1568 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1569 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1570 and_(scratch, type, Operand(kFlatAsciiStringMask));
1571 cmp(scratch, Operand(kFlatAsciiStringTag));
1572 b(ne, failure);
1573}
1574
1575
ager@chromium.org357bf652010-04-12 11:30:10 +00001576void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001577 int frame_alignment = ActivationFrameAlignment();
ager@chromium.org357bf652010-04-12 11:30:10 +00001578 // Up to four simple arguments are passed in registers r0..r3.
1579 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001580 if (frame_alignment > kPointerSize) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001581 // Make stack end at alignment and make room for num_arguments - 4 words
1582 // and the original value of sp.
1583 mov(scratch, sp);
1584 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001585 ASSERT(IsPowerOf2(frame_alignment));
1586 and_(sp, sp, Operand(-frame_alignment));
ager@chromium.org357bf652010-04-12 11:30:10 +00001587 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1588 } else {
1589 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1590 }
1591}
1592
1593
1594void MacroAssembler::CallCFunction(ExternalReference function,
1595 int num_arguments) {
1596 mov(ip, Operand(function));
1597 CallCFunction(ip, num_arguments);
1598}
1599
1600
1601void MacroAssembler::CallCFunction(Register function, int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001602 // Make sure that the stack is aligned before calling a C function unless
1603 // running in the simulator. The simulator has its own alignment check which
1604 // provides more information.
1605#if defined(V8_HOST_ARCH_ARM)
1606 if (FLAG_debug_code) {
1607 int frame_alignment = OS::ActivationFrameAlignment();
1608 int frame_alignment_mask = frame_alignment - 1;
1609 if (frame_alignment > kPointerSize) {
1610 ASSERT(IsPowerOf2(frame_alignment));
1611 Label alignment_as_expected;
1612 tst(sp, Operand(frame_alignment_mask));
1613 b(eq, &alignment_as_expected);
1614 // Don't use Check here, as it will call Runtime_Abort possibly
1615 // re-entering here.
1616 stop("Unexpected alignment");
1617 bind(&alignment_as_expected);
1618 }
1619 }
1620#endif
1621
ager@chromium.org357bf652010-04-12 11:30:10 +00001622 // Just call directly. The function called cannot cause a GC, or
1623 // allow preemption, so the return address in the link register
1624 // stays correct.
1625 Call(function);
1626 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1627 if (OS::ActivationFrameAlignment() > kPointerSize) {
1628 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1629 } else {
1630 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1631 }
1632}
1633
1634
ager@chromium.org4af710e2009-09-15 12:20:11 +00001635#ifdef ENABLE_DEBUGGER_SUPPORT
1636CodePatcher::CodePatcher(byte* address, int instructions)
1637 : address_(address),
1638 instructions_(instructions),
1639 size_(instructions * Assembler::kInstrSize),
1640 masm_(address, size_ + Assembler::kGap) {
1641 // Create a new macro assembler pointing to the address of the code to patch.
1642 // The size is adjusted with kGap on order for the assembler to generate size
1643 // bytes of instructions without failing with buffer size constraints.
1644 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1645}
1646
1647
1648CodePatcher::~CodePatcher() {
1649 // Indicate that code has changed.
1650 CPU::FlushICache(address_, size_);
1651
1652 // Check that the code was patched as expected.
1653 ASSERT(masm_.pc_ == address_ + size_);
1654 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1655}
1656
1657
1658void CodePatcher::Emit(Instr x) {
1659 masm()->emit(x);
1660}
1661
1662
1663void CodePatcher::Emit(Address addr) {
1664 masm()->emit(reinterpret_cast<Instr>(addr));
1665}
1666#endif // ENABLE_DEBUGGER_SUPPORT
1667
1668
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669} } // namespace v8::internal