blob: 17d6a10053e55435697943d7c47fbe8aa71ff892 [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
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000120 // The two instructions (ldr and blx) could be separated by a constant
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000121 // 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.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000123 { BlockConstPoolScope block_const_pool(this);
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();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000129
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000130 mov(ip, Operand(target, rmode), LeaveCC, cond);
131 blx(ip, cond);
132 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000133
134 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
135#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 // Set lr for return at current pc + 8.
137 mov(lr, Operand(pc), LeaveCC, cond);
138 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
139 mov(pc, Operand(target, rmode), LeaveCC, cond);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000140
ager@chromium.org4af710e2009-09-15 12:20:11 +0000141 ASSERT(kCallTargetAddressOffset == kInstrSize);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000142#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143}
144
145
ager@chromium.org236ad962008-09-25 09:45:57 +0000146void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
147 Condition cond) {
148 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
150}
151
152
ager@chromium.org236ad962008-09-25 09:45:57 +0000153void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
154 Condition cond) {
155 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 // 'code' is always generated ARM code, never THUMB code
157 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
158}
159
160
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000161void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000163 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000165 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166#endif
167}
168
169
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000170void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
171 LoadRoot(ip, Heap::kStackLimitRootIndex);
172 cmp(sp, Operand(ip));
173 b(lo, on_stack_overflow);
174}
175
176
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000177void MacroAssembler::Drop(int count, Condition cond) {
178 if (count > 0) {
179 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000180 }
181}
182
183
ager@chromium.org357bf652010-04-12 11:30:10 +0000184void MacroAssembler::Swap(Register reg1, Register reg2, Register scratch) {
185 if (scratch.is(no_reg)) {
186 eor(reg1, reg1, Operand(reg2));
187 eor(reg2, reg2, Operand(reg1));
188 eor(reg1, reg1, Operand(reg2));
189 } else {
190 mov(scratch, reg1);
191 mov(reg1, reg2);
192 mov(reg2, scratch);
193 }
194}
195
196
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000197void MacroAssembler::Call(Label* target) {
198 bl(target);
199}
200
201
202void MacroAssembler::Move(Register dst, Handle<Object> value) {
203 mov(dst, Operand(value));
204}
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000205
206
ager@chromium.org357bf652010-04-12 11:30:10 +0000207void MacroAssembler::Move(Register dst, Register src) {
208 if (!dst.is(src)) {
209 mov(dst, src);
210 }
211}
212
213
ager@chromium.org8bb60582008-12-11 12:02:20 +0000214void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
215 // Empty the const pool.
216 CheckConstPool(true, true);
217 add(pc, pc, Operand(index,
218 LSL,
219 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000220 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000221 nop(); // Jump table alignment.
222 for (int i = 0; i < targets.length(); i++) {
223 b(targets[i]);
224 }
225}
226
227
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000228void MacroAssembler::LoadRoot(Register destination,
229 Heap::RootListIndex index,
230 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000231 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000232}
233
234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235// Will clobber 4 registers: object, offset, scratch, ip. The
236// register 'object' contains a heap object pointer. The heap object
237// tag is shifted away.
238void MacroAssembler::RecordWrite(Register object, Register offset,
239 Register scratch) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000240 // The compiled code assumes that record write doesn't change the
241 // context register, so we check that none of the clobbered
242 // registers are cp.
243 ASSERT(!object.is(cp) && !offset.is(cp) && !scratch.is(cp));
244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 // This is how much we shift the remembered set bit offset to get the
246 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
247 // shift right 5) and then multiply by kIntSize (4, shift left 2).
248 const int kRSetWordShift = 3;
249
250 Label fast, done;
251
kasper.lund7276f142008-07-30 08:49:36 +0000252 // First, test that the object is not in the new space. We cannot set
253 // remembered set bits in the new space.
254 // object: heap object pointer (with tag)
255 // offset: offset to store location from the object
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000256 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 cmp(scratch, Operand(ExternalReference::new_space_start()));
258 b(eq, &done);
259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000261 // object: heap object pointer (with tag)
262 // offset: offset to store location from the object
263 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
264 and_(scratch, object, Operand(ip)); // offset into page of the object
265 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
267
268 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000269 // object: heap object pointer (with tag)
270 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 bic(object, object, Operand(ip));
272
273 // If the bit offset lies beyond the normal remembered set range, it is in
274 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000275 // object: page start
276 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 cmp(offset, Operand(Page::kPageSize / kPointerSize));
278 b(lt, &fast);
279
280 // Adjust the bit offset to be relative to the start of the extra
281 // remembered set and the start address to be the address of the extra
282 // remembered set.
283 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
284 // Load the array length into 'scratch' and multiply by four to get the
285 // size in bytes of the elements.
286 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
287 + FixedArray::kLengthOffset));
288 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
289 // Add the page header (including remembered set), array header, and array
290 // body size to the page address.
291 add(object, object, Operand(Page::kObjectStartOffset
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000292 + FixedArray::kHeaderSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 add(object, object, Operand(scratch));
294
295 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000297 // object: start of the remembered set (page start for the fast case)
298 // offset: bit offset of store position in the remembered set
299 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
300 add(object, object, Operand(scratch, LSR, kRSetWordShift));
301 // Get bit offset in the rset word.
302 // object: address of remembered set word
303 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 and_(offset, offset, Operand(kBitsPerInt - 1));
305
306 ldr(scratch, MemOperand(object));
307 mov(ip, Operand(1));
308 orr(scratch, scratch, Operand(ip, LSL, offset));
309 str(scratch, MemOperand(object));
310
311 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000312
313 // Clobber all input registers when running with the debug-code flag
314 // turned on to provoke errors.
315 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000316 mov(object, Operand(BitCast<int32_t>(kZapValue)));
317 mov(offset, Operand(BitCast<int32_t>(kZapValue)));
318 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320}
321
322
ager@chromium.org7c537e22008-10-16 08:43:32 +0000323void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000324 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000325 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
326 mov(ip, Operand(Smi::FromInt(type)));
327 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000328 mov(ip, Operand(CodeObject()));
329 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000330 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331}
332
333
ager@chromium.org7c537e22008-10-16 08:43:32 +0000334void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000335 // r0: preserved
336 // r1: preserved
337 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338
ager@chromium.org7c537e22008-10-16 08:43:32 +0000339 // Drop the execution stack down to the frame pointer and restore
340 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000341 mov(sp, fp);
342 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343}
344
345
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000346void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000347 // Compute the argv pointer and keep it in a callee-saved register.
348 // r0 is argc.
349 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
350 sub(r6, r6, Operand(kPointerSize));
351
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000352 // Compute callee's stack pointer before making changes and save it as
353 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000354 // popping the args.
355
356 // ip = sp + kPointerSize * #args;
357 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
358
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000359 // Prepare the stack to be aligned when calling into C. After this point there
360 // are 5 pushes before the call into C, so the stack needs to be aligned after
361 // 5 pushes.
362 int frame_alignment = ActivationFrameAlignment();
363 int frame_alignment_mask = frame_alignment - 1;
364 if (frame_alignment != kPointerSize) {
365 // The following code needs to be more general if this assert does not hold.
366 ASSERT(frame_alignment == 2 * kPointerSize);
367 // With 5 pushes left the frame must be unaligned at this point.
368 mov(r7, Operand(Smi::FromInt(0)));
369 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
370 push(r7, eq); // Push if aligned to make it unaligned.
371 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000372
ager@chromium.org236ad962008-09-25 09:45:57 +0000373 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
374 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +0000375 mov(fp, Operand(sp)); // Setup new frame pointer.
ager@chromium.org236ad962008-09-25 09:45:57 +0000376
ager@chromium.org5c838252010-02-19 08:53:10 +0000377 mov(ip, Operand(CodeObject()));
378 push(ip); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000379
380 // Save the frame pointer and the context in top.
381 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
382 str(fp, MemOperand(ip));
383 mov(ip, Operand(ExternalReference(Top::k_context_address)));
384 str(cp, MemOperand(ip));
385
386 // Setup argc and the builtin function in callee-saved registers.
387 mov(r4, Operand(r0));
388 mov(r5, Operand(r1));
389
ager@chromium.org236ad962008-09-25 09:45:57 +0000390
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000391#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000392 // Save the state of all registers to the stack from the memory
393 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000394 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000395 // Use sp as base to push.
396 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
397 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000398#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000399}
400
401
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000402int MacroAssembler::ActivationFrameAlignment() {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000403#if defined(V8_HOST_ARCH_ARM)
404 // Running on the real platform. Use the alignment as mandated by the local
405 // environment.
406 // Note: This will break if we ever start generating snapshots on one ARM
407 // platform for another ARM platform with a different alignment.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000408 return OS::ActivationFrameAlignment();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000409#else // defined(V8_HOST_ARCH_ARM)
410 // If we are using the simulator then we should always align to the expected
411 // alignment. As the simulator is used to generate snapshots we do not know
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000412 // if the target platform will need alignment, so this is controlled from a
413 // flag.
414 return FLAG_sim_stack_alignment;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000415#endif // defined(V8_HOST_ARCH_ARM)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000416}
417
418
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000419void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000420#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000421 // Restore the memory copy of the registers by digging them out from
422 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000423 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000424 // This code intentionally clobbers r2 and r3.
425 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000426 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000427 add(r3, fp, Operand(kOffset));
428 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
429 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000430#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000431
432 // Clear top frame.
433 mov(r3, Operand(0));
434 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
435 str(r3, MemOperand(ip));
436
437 // Restore current context from top and clear it in debug mode.
438 mov(ip, Operand(ExternalReference(Top::k_context_address)));
439 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000440#ifdef DEBUG
441 str(r3, MemOperand(ip));
442#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000443
444 // Pop the arguments, restore registers, and return.
445 mov(sp, Operand(fp)); // respect ABI stack constraint
446 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
447}
448
449
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000450void MacroAssembler::InvokePrologue(const ParameterCount& expected,
451 const ParameterCount& actual,
452 Handle<Code> code_constant,
453 Register code_reg,
454 Label* done,
455 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000456 bool definitely_matches = false;
457 Label regular_invoke;
458
459 // Check whether the expected and actual arguments count match. If not,
460 // setup registers according to contract with ArgumentsAdaptorTrampoline:
461 // r0: actual arguments count
462 // r1: function (passed through to callee)
463 // r2: expected arguments count
464 // r3: callee code entry
465
466 // The code below is made a lot easier because the calling code already sets
467 // up actual and expected registers according to the contract if values are
468 // passed in registers.
469 ASSERT(actual.is_immediate() || actual.reg().is(r0));
470 ASSERT(expected.is_immediate() || expected.reg().is(r2));
471 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
472
473 if (expected.is_immediate()) {
474 ASSERT(actual.is_immediate());
475 if (expected.immediate() == actual.immediate()) {
476 definitely_matches = true;
477 } else {
478 mov(r0, Operand(actual.immediate()));
479 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
480 if (expected.immediate() == sentinel) {
481 // Don't worry about adapting arguments for builtins that
482 // don't want that done. Skip adaption code by making it look
483 // like we have a match between expected and actual number of
484 // arguments.
485 definitely_matches = true;
486 } else {
487 mov(r2, Operand(expected.immediate()));
488 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000489 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000490 } else {
491 if (actual.is_immediate()) {
492 cmp(expected.reg(), Operand(actual.immediate()));
493 b(eq, &regular_invoke);
494 mov(r0, Operand(actual.immediate()));
495 } else {
496 cmp(expected.reg(), Operand(actual.reg()));
497 b(eq, &regular_invoke);
498 }
499 }
500
501 if (!definitely_matches) {
502 if (!code_constant.is_null()) {
503 mov(r3, Operand(code_constant));
504 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
505 }
506
507 Handle<Code> adaptor =
508 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
509 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000510 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000511 b(done);
512 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000513 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000514 }
515 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000516 }
517}
518
519
520void MacroAssembler::InvokeCode(Register code,
521 const ParameterCount& expected,
522 const ParameterCount& actual,
523 InvokeFlag flag) {
524 Label done;
525
526 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
527 if (flag == CALL_FUNCTION) {
528 Call(code);
529 } else {
530 ASSERT(flag == JUMP_FUNCTION);
531 Jump(code);
532 }
533
534 // Continue here if InvokePrologue does handle the invocation due to
535 // mismatched parameter counts.
536 bind(&done);
537}
538
539
540void MacroAssembler::InvokeCode(Handle<Code> code,
541 const ParameterCount& expected,
542 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000543 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000544 InvokeFlag flag) {
545 Label done;
546
547 InvokePrologue(expected, actual, code, no_reg, &done, flag);
548 if (flag == CALL_FUNCTION) {
549 Call(code, rmode);
550 } else {
551 Jump(code, rmode);
552 }
553
554 // Continue here if InvokePrologue does handle the invocation due to
555 // mismatched parameter counts.
556 bind(&done);
557}
558
559
560void MacroAssembler::InvokeFunction(Register fun,
561 const ParameterCount& actual,
562 InvokeFlag flag) {
563 // Contract with called JS functions requires that function is passed in r1.
564 ASSERT(fun.is(r1));
565
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000566 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000567 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000568
569 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
570 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
571 ldr(expected_reg,
572 FieldMemOperand(code_reg,
573 SharedFunctionInfo::kFormalParameterCountOffset));
574 ldr(code_reg,
575 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
576 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
577
578 ParameterCount expected(expected_reg);
579 InvokeCode(code_reg, expected, actual, flag);
580}
581
582
ager@chromium.org5c838252010-02-19 08:53:10 +0000583void MacroAssembler::InvokeFunction(JSFunction* function,
584 const ParameterCount& actual,
585 InvokeFlag flag) {
586 ASSERT(function->is_compiled());
587
588 // Get the function and setup the context.
589 mov(r1, Operand(Handle<JSFunction>(function)));
590 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
591
592 // Invoke the cached code.
593 Handle<Code> code(function->code());
594 ParameterCount expected(function->shared()->formal_parameter_count());
595 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
596}
597
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000598#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599void MacroAssembler::SaveRegistersToMemory(RegList regs) {
600 ASSERT((regs & ~kJSCallerSaved) == 0);
601 // Copy the content of registers to memory location.
602 for (int i = 0; i < kNumJSCallerSaved; i++) {
603 int r = JSCallerSavedCode(i);
604 if ((regs & (1 << r)) != 0) {
605 Register reg = { r };
606 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
607 str(reg, MemOperand(ip));
608 }
609 }
610}
611
612
613void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
614 ASSERT((regs & ~kJSCallerSaved) == 0);
615 // Copy the content of memory location to registers.
616 for (int i = kNumJSCallerSaved; --i >= 0;) {
617 int r = JSCallerSavedCode(i);
618 if ((regs & (1 << r)) != 0) {
619 Register reg = { r };
620 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
621 ldr(reg, MemOperand(ip));
622 }
623 }
624}
625
626
627void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
628 RegList regs) {
629 ASSERT((regs & ~kJSCallerSaved) == 0);
630 // Copy the content of the memory location to the stack and adjust base.
631 for (int i = kNumJSCallerSaved; --i >= 0;) {
632 int r = JSCallerSavedCode(i);
633 if ((regs & (1 << r)) != 0) {
634 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
635 ldr(ip, MemOperand(ip));
636 str(ip, MemOperand(base, 4, NegPreIndex));
637 }
638 }
639}
640
641
642void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
643 Register scratch,
644 RegList regs) {
645 ASSERT((regs & ~kJSCallerSaved) == 0);
646 // Copy the content of the stack to the memory location and adjust base.
647 for (int i = 0; i < kNumJSCallerSaved; i++) {
648 int r = JSCallerSavedCode(i);
649 if ((regs & (1 << r)) != 0) {
650 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
651 ldr(scratch, MemOperand(base, 4, PostIndex));
652 str(scratch, MemOperand(ip));
653 }
654 }
655}
ager@chromium.org5c838252010-02-19 08:53:10 +0000656
657
658void MacroAssembler::DebugBreak() {
659 ASSERT(allow_stub_calls());
660 mov(r0, Operand(0));
661 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
662 CEntryStub ces(1);
663 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
664}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000665#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000667
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668void MacroAssembler::PushTryHandler(CodeLocation try_location,
669 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000670 // Adjust this code if not the case.
671 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 // The pc (return address) is passed in register lr.
673 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 if (type == TRY_CATCH_HANDLER) {
675 mov(r3, Operand(StackHandler::TRY_CATCH));
676 } else {
677 mov(r3, Operand(StackHandler::TRY_FINALLY));
678 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000679 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
680 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
681 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
682 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
683 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
685 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000686 ASSERT(StackHandlerConstants::kNextOffset == 0);
687 push(r1);
688 // Link this handler as the new current one.
689 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000691 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000693 // The frame pointer does not point to a JS frame so we save NULL
694 // for fp. We expect the code throwing an exception to check fp
695 // before dereferencing it to restore the context.
696 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000698 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
699 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
700 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
701 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
702 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
704 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000705 ASSERT(StackHandlerConstants::kNextOffset == 0);
706 push(r6);
707 // Link this handler as the new current one.
708 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 }
710}
711
712
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000713void MacroAssembler::PopTryHandler() {
714 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
715 pop(r1);
716 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
717 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
718 str(r1, MemOperand(ip));
719}
720
721
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
723 JSObject* holder, Register holder_reg,
724 Register scratch,
725 Label* miss) {
726 // Make sure there's no overlap between scratch and the other
727 // registers.
728 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
729
730 // Keep track of the current object in register reg.
731 Register reg = object_reg;
732 int depth = 1;
733
734 // Check the maps in the prototype chain.
735 // Traverse the prototype chain from the object and do map checks.
736 while (object != holder) {
737 depth++;
738
739 // Only global objects and objects that do not require access
740 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000741 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
743 // Get the map of the current object.
744 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
745 cmp(scratch, Operand(Handle<Map>(object->map())));
746
747 // Branch on the result of the map check.
748 b(ne, miss);
749
750 // Check access rights to the global object. This has to happen
751 // after the map check so that we know that the object is
752 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000753 if (object->IsJSGlobalProxy()) {
754 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 // Restore scratch register to be the map of the object. In the
756 // new space case below, we load the prototype from the map in
757 // the scratch register.
758 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
759 }
760
761 reg = holder_reg; // from now the object is in holder_reg
762 JSObject* prototype = JSObject::cast(object->GetPrototype());
763 if (Heap::InNewSpace(prototype)) {
764 // The prototype is in new space; we cannot store a reference
765 // to it in the code. Load it from the map.
766 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
767 } else {
768 // The prototype is in old space; load it directly.
769 mov(reg, Operand(Handle<JSObject>(prototype)));
770 }
771
772 // Go to the next object in the prototype chain.
773 object = prototype;
774 }
775
776 // Check the holder map.
777 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
778 cmp(scratch, Operand(Handle<Map>(object->map())));
779 b(ne, miss);
780
781 // Log the check depth.
782 LOG(IntEvent("check-maps-depth", depth));
783
784 // Perform security check for access to the global object and return
785 // the holder register.
786 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000787 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
788 if (object->IsJSGlobalProxy()) {
789 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 }
791 return reg;
792}
793
794
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000795void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
796 Register scratch,
797 Label* miss) {
798 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000800 ASSERT(!holder_reg.is(scratch));
801 ASSERT(!holder_reg.is(ip));
802 ASSERT(!scratch.is(ip));
803
804 // Load current lexical context from the stack frame.
805 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
806 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000807#ifdef DEBUG
808 cmp(scratch, Operand(0));
809 Check(ne, "we should not have an empty lexical context");
810#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000812 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
814 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000815 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
816
817 // Check the context is a global context.
818 if (FLAG_debug_code) {
819 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
820 // Cannot use ip as a temporary in this verification code. Due to the fact
821 // that ip is clobbered as part of cmp with an object Operand.
822 push(holder_reg); // Temporarily save holder on the stack.
823 // Read the first word and compare to the global_context_map.
824 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000825 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
826 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000827 Check(eq, "JSGlobalObject::global_context should be a global context.");
828 pop(holder_reg); // Restore holder.
829 }
830
831 // Check if both contexts are the same.
832 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
833 cmp(scratch, Operand(ip));
834 b(eq, &same_contexts);
835
836 // Check the context is a global context.
837 if (FLAG_debug_code) {
838 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
839 // Cannot use ip as a temporary in this verification code. Due to the fact
840 // that ip is clobbered as part of cmp with an object Operand.
841 push(holder_reg); // Temporarily save holder on the stack.
842 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000843 LoadRoot(ip, Heap::kNullValueRootIndex);
844 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000845 Check(ne, "JSGlobalProxy::context() should not be null.");
846
847 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000848 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
849 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000850 Check(eq, "JSGlobalObject::global_context should be a global context.");
851 // Restore ip is not needed. ip is reloaded below.
852 pop(holder_reg); // Restore holder.
853 // Restore ip to holder's context.
854 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
855 }
856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 // Check that the security token in the calling global object is
858 // compatible with the security token in the receiving global
859 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000860 int token_offset = Context::kHeaderSize +
861 Context::SECURITY_TOKEN_INDEX * kPointerSize;
862
863 ldr(scratch, FieldMemOperand(scratch, token_offset));
864 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 cmp(scratch, Operand(ip));
866 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000867
868 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869}
870
871
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000872void MacroAssembler::AllocateInNewSpace(int object_size,
873 Register result,
874 Register scratch1,
875 Register scratch2,
876 Label* gc_required,
877 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000878 ASSERT(!result.is(scratch1));
879 ASSERT(!scratch1.is(scratch2));
880
881 // Load address of new object into result and allocation top address into
882 // scratch1.
883 ExternalReference new_space_allocation_top =
884 ExternalReference::new_space_allocation_top_address();
885 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000886 if ((flags & RESULT_CONTAINS_TOP) == 0) {
887 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000888 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000889 // Assert that result actually contains top on entry. scratch2 is used
890 // immediately below so this use of scratch2 does not cause difference with
891 // respect to register content between debug and release mode.
892 ldr(scratch2, MemOperand(scratch1));
893 cmp(result, scratch2);
894 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +0000895 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000896
897 // Calculate new top and bail out if new space is exhausted. Use result
898 // to calculate the new top.
899 ExternalReference new_space_allocation_limit =
900 ExternalReference::new_space_allocation_limit_address();
901 mov(scratch2, Operand(new_space_allocation_limit));
902 ldr(scratch2, MemOperand(scratch2));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000903 add(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000904 cmp(result, Operand(scratch2));
905 b(hi, gc_required);
906
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000907 // Update allocation top. result temporarily holds the new top.
908 if (FLAG_debug_code) {
909 tst(result, Operand(kObjectAlignmentMask));
910 Check(eq, "Unaligned allocation in new space");
911 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000912 str(result, MemOperand(scratch1));
913
914 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000915 if ((flags & TAG_OBJECT) != 0) {
916 sub(result, result, Operand((object_size * kPointerSize) -
917 kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000918 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000919 sub(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000920 }
921}
922
923
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000924void MacroAssembler::AllocateInNewSpace(Register object_size,
925 Register result,
926 Register scratch1,
927 Register scratch2,
928 Label* gc_required,
929 AllocationFlags flags) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000930 ASSERT(!result.is(scratch1));
931 ASSERT(!scratch1.is(scratch2));
932
933 // Load address of new object into result and allocation top address into
934 // scratch1.
935 ExternalReference new_space_allocation_top =
936 ExternalReference::new_space_allocation_top_address();
937 mov(scratch1, Operand(new_space_allocation_top));
938 if ((flags & RESULT_CONTAINS_TOP) == 0) {
939 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000940 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000941 // Assert that result actually contains top on entry. scratch2 is used
942 // immediately below so this use of scratch2 does not cause difference with
943 // respect to register content between debug and release mode.
944 ldr(scratch2, MemOperand(scratch1));
945 cmp(result, scratch2);
946 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +0000947 }
948
949 // Calculate new top and bail out if new space is exhausted. Use result
950 // to calculate the new top. Object size is in words so a shift is required to
951 // get the number of bytes
952 ExternalReference new_space_allocation_limit =
953 ExternalReference::new_space_allocation_limit_address();
954 mov(scratch2, Operand(new_space_allocation_limit));
955 ldr(scratch2, MemOperand(scratch2));
956 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
957 cmp(result, Operand(scratch2));
958 b(hi, gc_required);
959
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000960 // Update allocation top. result temporarily holds the new top.
961 if (FLAG_debug_code) {
962 tst(result, Operand(kObjectAlignmentMask));
963 Check(eq, "Unaligned allocation in new space");
964 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000965 str(result, MemOperand(scratch1));
966
967 // Adjust back to start of new object.
968 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
969
970 // Tag object if requested.
971 if ((flags & TAG_OBJECT) != 0) {
972 add(result, result, Operand(kHeapObjectTag));
973 }
974}
975
976
977void MacroAssembler::UndoAllocationInNewSpace(Register object,
978 Register scratch) {
979 ExternalReference new_space_allocation_top =
980 ExternalReference::new_space_allocation_top_address();
981
982 // Make sure the object has no tag before resetting top.
983 and_(object, object, Operand(~kHeapObjectTagMask));
984#ifdef DEBUG
985 // Check that the object un-allocated is below the current top.
986 mov(scratch, Operand(new_space_allocation_top));
987 ldr(scratch, MemOperand(scratch));
988 cmp(object, scratch);
989 Check(lt, "Undo allocation of non allocated memory");
990#endif
991 // Write the address of the object to un-allocate as the current top.
992 mov(scratch, Operand(new_space_allocation_top));
993 str(object, MemOperand(scratch));
994}
995
996
ager@chromium.org5c838252010-02-19 08:53:10 +0000997void MacroAssembler::AllocateTwoByteString(Register result,
998 Register length,
999 Register scratch1,
1000 Register scratch2,
1001 Register scratch3,
1002 Label* gc_required) {
1003 // Calculate the number of bytes needed for the characters in the string while
1004 // observing object alignment.
1005 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1006 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1007 add(scratch1, scratch1,
1008 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
1009 // AllocateInNewSpace expects the size in words, so we can round down
1010 // to kObjectAlignment and divide by kPointerSize in the same shift.
1011 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
1012 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
1013
1014 // Allocate two-byte string in new space.
1015 AllocateInNewSpace(scratch1,
1016 result,
1017 scratch2,
1018 scratch3,
1019 gc_required,
1020 TAG_OBJECT);
1021
1022 // Set the map, length and hash field.
1023 LoadRoot(scratch1, Heap::kStringMapRootIndex);
1024 str(length, FieldMemOperand(result, String::kLengthOffset));
1025 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1026 mov(scratch2, Operand(String::kEmptyHashField));
1027 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1028}
1029
1030
1031void MacroAssembler::AllocateAsciiString(Register result,
1032 Register length,
1033 Register scratch1,
1034 Register scratch2,
1035 Register scratch3,
1036 Label* gc_required) {
1037 // Calculate the number of bytes needed for the characters in the string while
1038 // observing object alignment.
1039 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1040 ASSERT(kCharSize == 1);
1041 add(scratch1, length,
1042 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
1043 // AllocateInNewSpace expects the size in words, so we can round down
1044 // to kObjectAlignment and divide by kPointerSize in the same shift.
1045 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
1046 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
1047
1048 // Allocate ASCII string in new space.
1049 AllocateInNewSpace(scratch1,
1050 result,
1051 scratch2,
1052 scratch3,
1053 gc_required,
1054 TAG_OBJECT);
1055
1056 // Set the map, length and hash field.
1057 LoadRoot(scratch1, Heap::kAsciiStringMapRootIndex);
1058 mov(scratch1, Operand(Factory::ascii_string_map()));
1059 str(length, FieldMemOperand(result, String::kLengthOffset));
1060 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1061 mov(scratch2, Operand(String::kEmptyHashField));
1062 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1063}
1064
1065
1066void MacroAssembler::AllocateTwoByteConsString(Register result,
1067 Register length,
1068 Register scratch1,
1069 Register scratch2,
1070 Label* gc_required) {
1071 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1072 result,
1073 scratch1,
1074 scratch2,
1075 gc_required,
1076 TAG_OBJECT);
1077 LoadRoot(scratch1, Heap::kConsStringMapRootIndex);
1078 mov(scratch2, Operand(String::kEmptyHashField));
1079 str(length, FieldMemOperand(result, String::kLengthOffset));
1080 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1081 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1082}
1083
1084
1085void MacroAssembler::AllocateAsciiConsString(Register result,
1086 Register length,
1087 Register scratch1,
1088 Register scratch2,
1089 Label* gc_required) {
1090 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1091 result,
1092 scratch1,
1093 scratch2,
1094 gc_required,
1095 TAG_OBJECT);
1096 LoadRoot(scratch1, Heap::kConsAsciiStringMapRootIndex);
1097 mov(scratch2, Operand(String::kEmptyHashField));
1098 str(length, FieldMemOperand(result, String::kLengthOffset));
1099 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1100 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1101}
1102
1103
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001104void MacroAssembler::CompareObjectType(Register function,
1105 Register map,
1106 Register type_reg,
1107 InstanceType type) {
1108 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001109 CompareInstanceType(map, type_reg, type);
1110}
1111
1112
1113void MacroAssembler::CompareInstanceType(Register map,
1114 Register type_reg,
1115 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001116 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1117 cmp(type_reg, Operand(type));
1118}
1119
1120
ager@chromium.org5c838252010-02-19 08:53:10 +00001121void MacroAssembler::CheckMap(Register obj,
1122 Register scratch,
1123 Handle<Map> map,
1124 Label* fail,
1125 bool is_heap_object) {
1126 if (!is_heap_object) {
1127 BranchOnSmi(obj, fail);
1128 }
1129 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1130 mov(ip, Operand(map));
1131 cmp(scratch, ip);
1132 b(ne, fail);
1133}
1134
1135
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001136void MacroAssembler::TryGetFunctionPrototype(Register function,
1137 Register result,
1138 Register scratch,
1139 Label* miss) {
1140 // Check that the receiver isn't a smi.
1141 BranchOnSmi(function, miss);
1142
1143 // Check that the function really is a function. Load map into result reg.
1144 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1145 b(ne, miss);
1146
1147 // Make sure that the function has an instance prototype.
1148 Label non_instance;
1149 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1150 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1151 b(ne, &non_instance);
1152
1153 // Get the prototype or initial map from the function.
1154 ldr(result,
1155 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1156
1157 // If the prototype or initial map is the hole, don't return it and
1158 // simply miss the cache instead. This will allow us to allocate a
1159 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001160 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1161 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001162 b(eq, miss);
1163
1164 // If the function does not have an initial map, we're done.
1165 Label done;
1166 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1167 b(ne, &done);
1168
1169 // Get the prototype from the initial map.
1170 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1171 jmp(&done);
1172
1173 // Non-instance prototype: Fetch prototype from constructor field
1174 // in initial map.
1175 bind(&non_instance);
1176 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1177
1178 // All done.
1179 bind(&done);
1180}
1181
1182
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001183void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +00001184 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001185 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186}
1187
1188
ager@chromium.org5c838252010-02-19 08:53:10 +00001189void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1190 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1191 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1192}
1193
1194
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195void MacroAssembler::StubReturn(int argc) {
1196 ASSERT(argc >= 1 && generating_stub());
ager@chromium.org5c838252010-02-19 08:53:10 +00001197 if (argc > 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 add(sp, sp, Operand((argc - 1) * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00001199 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 Ret();
1201}
1202
mads.s.ager31e71382008-08-13 09:32:07 +00001203
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001204void MacroAssembler::IllegalOperation(int num_arguments) {
1205 if (num_arguments > 0) {
1206 add(sp, sp, Operand(num_arguments * kPointerSize));
1207 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001208 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001209}
1210
1211
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001212void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1213 Register outHighReg,
1214 Register outLowReg) {
1215 // ARMv7 VFP3 instructions to implement integer to double conversion.
1216 mov(r7, Operand(inReg, ASR, kSmiTagSize));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001217 vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001218 vcvt_f64_s32(d7, s15);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001219 vmov(outLowReg, outHighReg, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001220}
1221
1222
ager@chromium.org5c838252010-02-19 08:53:10 +00001223void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1224 Register src,
1225 int num_least_bits) {
1226 if (CpuFeatures::IsSupported(ARMv7)) {
1227 ubfx(dst, src, Operand(kSmiTagSize), Operand(num_least_bits - 1));
1228 } else {
1229 mov(dst, Operand(src, ASR, kSmiTagSize));
1230 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1231 }
1232}
1233
1234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001236 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001238 // If the expected number of arguments of the runtime function is
1239 // constant, we check that the actual number of arguments match the
1240 // expectation.
1241 if (f->nargs >= 0 && f->nargs != num_arguments) {
1242 IllegalOperation(num_arguments);
1243 return;
1244 }
kasper.lund7276f142008-07-30 08:49:36 +00001245
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001246 // TODO(1236192): Most runtime routines don't need the number of
1247 // arguments passed in because it is constant. At some point we
1248 // should remove this need and make the runtime routine entry code
1249 // smarter.
1250 mov(r0, Operand(num_arguments));
1251 mov(r1, Operand(ExternalReference(f)));
1252 CEntryStub stub(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 CallStub(&stub);
1254}
1255
1256
1257void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1258 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1259}
1260
1261
ager@chromium.org5c838252010-02-19 08:53:10 +00001262void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1263 int num_arguments) {
1264 mov(r0, Operand(num_arguments));
1265 mov(r1, Operand(ext));
1266
1267 CEntryStub stub(1);
1268 CallStub(&stub);
1269}
1270
1271
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001272void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1273 int num_arguments,
1274 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001275 // TODO(1236192): Most runtime routines don't need the number of
1276 // arguments passed in because it is constant. At some point we
1277 // should remove this need and make the runtime routine entry code
1278 // smarter.
1279 mov(r0, Operand(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001280 JumpToExternalReference(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281}
1282
1283
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001284void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1285 int num_arguments,
1286 int result_size) {
1287 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1288}
1289
1290
1291void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292#if defined(__thumb__)
1293 // Thumb mode builtin.
1294 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1295#endif
1296 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001297 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001298 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299}
1300
1301
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001302void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1303 InvokeJSFlags flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001304 GetBuiltinEntry(r2, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 if (flags == CALL_JS) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001306 Call(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 } else {
1308 ASSERT(flags == JUMP_JS);
ager@chromium.org5c838252010-02-19 08:53:10 +00001309 Jump(r2);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001310 }
1311}
1312
1313
1314void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001315 ASSERT(!target.is(r1));
1316
1317 // Load the builtins object into target register.
1318 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1319 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1320
ager@chromium.org5c838252010-02-19 08:53:10 +00001321 // Load the JavaScript builtin function from the builtins object.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001322 ldr(r1, FieldMemOperand(target,
1323 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1324
1325 // Load the code entry point from the builtins object.
1326 ldr(target, FieldMemOperand(target,
1327 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1328 if (FLAG_debug_code) {
1329 // Make sure the code objects in the builtins object and in the
1330 // builtin function are the same.
1331 push(r1);
1332 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1333 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1334 cmp(r1, target);
1335 Assert(eq, "Builtin code object changed");
1336 pop(r1);
1337 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001338 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339}
1340
1341
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001342void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1343 Register scratch1, Register scratch2) {
1344 if (FLAG_native_code_counters && counter->Enabled()) {
1345 mov(scratch1, Operand(value));
1346 mov(scratch2, Operand(ExternalReference(counter)));
1347 str(scratch1, MemOperand(scratch2));
1348 }
1349}
1350
1351
1352void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1353 Register scratch1, Register scratch2) {
1354 ASSERT(value > 0);
1355 if (FLAG_native_code_counters && counter->Enabled()) {
1356 mov(scratch2, Operand(ExternalReference(counter)));
1357 ldr(scratch1, MemOperand(scratch2));
1358 add(scratch1, scratch1, Operand(value));
1359 str(scratch1, MemOperand(scratch2));
1360 }
1361}
1362
1363
1364void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1365 Register scratch1, Register scratch2) {
1366 ASSERT(value > 0);
1367 if (FLAG_native_code_counters && counter->Enabled()) {
1368 mov(scratch2, Operand(ExternalReference(counter)));
1369 ldr(scratch1, MemOperand(scratch2));
1370 sub(scratch1, scratch1, Operand(value));
1371 str(scratch1, MemOperand(scratch2));
1372 }
1373}
1374
1375
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376void MacroAssembler::Assert(Condition cc, const char* msg) {
1377 if (FLAG_debug_code)
1378 Check(cc, msg);
1379}
1380
1381
1382void MacroAssembler::Check(Condition cc, const char* msg) {
1383 Label L;
1384 b(cc, &L);
1385 Abort(msg);
1386 // will not return here
1387 bind(&L);
1388}
1389
1390
1391void MacroAssembler::Abort(const char* msg) {
1392 // We want to pass the msg string like a smi to avoid GC
1393 // problems, however msg is not guaranteed to be aligned
1394 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001395 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 // from the real pointer as a smi.
1397 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1398 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1399 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1400#ifdef DEBUG
1401 if (msg != NULL) {
1402 RecordComment("Abort message: ");
1403 RecordComment(msg);
1404 }
1405#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001406 // Disable stub call restrictions to always allow calls to abort.
1407 set_allow_stub_calls(true);
1408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409 mov(r0, Operand(p0));
1410 push(r0);
1411 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001412 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 CallRuntime(Runtime::kAbort, 2);
1414 // will not return here
1415}
1416
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001417
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001418void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1419 if (context_chain_length > 0) {
1420 // Move up the chain of contexts to the context containing the slot.
1421 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1422 // Load the function context (which is the incoming, outer context).
1423 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1424 for (int i = 1; i < context_chain_length; i++) {
1425 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1426 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1427 }
1428 // The context may be an intermediate context, not a function context.
1429 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1430 } else { // Slot is in the current function context.
1431 // The context may be an intermediate context, not a function context.
1432 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1433 }
1434}
1435
1436
ager@chromium.org5c838252010-02-19 08:53:10 +00001437void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1438 Register reg2,
1439 Label* on_not_both_smi) {
1440 ASSERT_EQ(0, kSmiTag);
1441 tst(reg1, Operand(kSmiTagMask));
1442 tst(reg2, Operand(kSmiTagMask), eq);
1443 b(ne, on_not_both_smi);
1444}
1445
1446
1447void MacroAssembler::JumpIfEitherSmi(Register reg1,
1448 Register reg2,
1449 Label* on_either_smi) {
1450 ASSERT_EQ(0, kSmiTag);
1451 tst(reg1, Operand(kSmiTagMask));
1452 tst(reg2, Operand(kSmiTagMask), ne);
1453 b(eq, on_either_smi);
1454}
1455
1456
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001457void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1458 Register first,
1459 Register second,
1460 Register scratch1,
1461 Register scratch2,
1462 Label* failure) {
1463 // Test that both first and second are sequential ASCII strings.
1464 // Assume that they are non-smis.
1465 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1466 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1467 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1468 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001469
1470 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1471 scratch2,
1472 scratch1,
1473 scratch2,
1474 failure);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001475}
1476
1477void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1478 Register second,
1479 Register scratch1,
1480 Register scratch2,
1481 Label* failure) {
1482 // Check that neither is a smi.
1483 ASSERT_EQ(0, kSmiTag);
1484 and_(scratch1, first, Operand(second));
1485 tst(scratch1, Operand(kSmiTagMask));
1486 b(eq, failure);
1487 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1488 second,
1489 scratch1,
1490 scratch2,
1491 failure);
1492}
1493
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001494
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001495// Allocates a heap number or jumps to the need_gc label if the young space
1496// is full and a scavenge is needed.
1497void MacroAssembler::AllocateHeapNumber(Register result,
1498 Register scratch1,
1499 Register scratch2,
1500 Label* gc_required) {
1501 // Allocate an object in the heap for the heap number and tag it as a heap
1502 // object.
1503 AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
1504 result,
1505 scratch1,
1506 scratch2,
1507 gc_required,
1508 TAG_OBJECT);
1509
1510 // Get heap number map and store it in the allocated object.
1511 LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
1512 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1513}
1514
1515
1516void MacroAssembler::CountLeadingZeros(Register source,
1517 Register scratch,
1518 Register zeros) {
1519#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1520 clz(zeros, source); // This instruction is only supported after ARM5.
1521#else
1522 mov(zeros, Operand(0));
1523 mov(scratch, source);
1524 // Top 16.
1525 tst(scratch, Operand(0xffff0000));
1526 add(zeros, zeros, Operand(16), LeaveCC, eq);
1527 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1528 // Top 8.
1529 tst(scratch, Operand(0xff000000));
1530 add(zeros, zeros, Operand(8), LeaveCC, eq);
1531 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1532 // Top 4.
1533 tst(scratch, Operand(0xf0000000));
1534 add(zeros, zeros, Operand(4), LeaveCC, eq);
1535 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1536 // Top 2.
1537 tst(scratch, Operand(0xc0000000));
1538 add(zeros, zeros, Operand(2), LeaveCC, eq);
1539 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1540 // Top bit.
1541 tst(scratch, Operand(0x80000000u));
1542 add(zeros, zeros, Operand(1), LeaveCC, eq);
1543#endif
1544}
1545
1546
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001547void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1548 Register first,
1549 Register second,
1550 Register scratch1,
1551 Register scratch2,
1552 Label* failure) {
1553 int kFlatAsciiStringMask =
1554 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1555 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1556 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1557 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1558 cmp(scratch1, Operand(kFlatAsciiStringTag));
1559 // Ignore second test if first test failed.
1560 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1561 b(ne, failure);
1562}
1563
1564
1565void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1566 Register scratch,
1567 Label* failure) {
1568 int kFlatAsciiStringMask =
1569 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1570 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1571 and_(scratch, type, Operand(kFlatAsciiStringMask));
1572 cmp(scratch, Operand(kFlatAsciiStringTag));
1573 b(ne, failure);
1574}
1575
1576
ager@chromium.org357bf652010-04-12 11:30:10 +00001577void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001578 int frame_alignment = ActivationFrameAlignment();
ager@chromium.org357bf652010-04-12 11:30:10 +00001579 // Up to four simple arguments are passed in registers r0..r3.
1580 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001581 if (frame_alignment > kPointerSize) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001582 // Make stack end at alignment and make room for num_arguments - 4 words
1583 // and the original value of sp.
1584 mov(scratch, sp);
1585 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001586 ASSERT(IsPowerOf2(frame_alignment));
1587 and_(sp, sp, Operand(-frame_alignment));
ager@chromium.org357bf652010-04-12 11:30:10 +00001588 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1589 } else {
1590 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1591 }
1592}
1593
1594
1595void MacroAssembler::CallCFunction(ExternalReference function,
1596 int num_arguments) {
1597 mov(ip, Operand(function));
1598 CallCFunction(ip, num_arguments);
1599}
1600
1601
1602void MacroAssembler::CallCFunction(Register function, int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001603 // Make sure that the stack is aligned before calling a C function unless
1604 // running in the simulator. The simulator has its own alignment check which
1605 // provides more information.
1606#if defined(V8_HOST_ARCH_ARM)
1607 if (FLAG_debug_code) {
1608 int frame_alignment = OS::ActivationFrameAlignment();
1609 int frame_alignment_mask = frame_alignment - 1;
1610 if (frame_alignment > kPointerSize) {
1611 ASSERT(IsPowerOf2(frame_alignment));
1612 Label alignment_as_expected;
1613 tst(sp, Operand(frame_alignment_mask));
1614 b(eq, &alignment_as_expected);
1615 // Don't use Check here, as it will call Runtime_Abort possibly
1616 // re-entering here.
1617 stop("Unexpected alignment");
1618 bind(&alignment_as_expected);
1619 }
1620 }
1621#endif
1622
ager@chromium.org357bf652010-04-12 11:30:10 +00001623 // Just call directly. The function called cannot cause a GC, or
1624 // allow preemption, so the return address in the link register
1625 // stays correct.
1626 Call(function);
1627 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1628 if (OS::ActivationFrameAlignment() > kPointerSize) {
1629 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1630 } else {
1631 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1632 }
1633}
1634
1635
ager@chromium.org4af710e2009-09-15 12:20:11 +00001636#ifdef ENABLE_DEBUGGER_SUPPORT
1637CodePatcher::CodePatcher(byte* address, int instructions)
1638 : address_(address),
1639 instructions_(instructions),
1640 size_(instructions * Assembler::kInstrSize),
1641 masm_(address, size_ + Assembler::kGap) {
1642 // Create a new macro assembler pointing to the address of the code to patch.
1643 // The size is adjusted with kGap on order for the assembler to generate size
1644 // bytes of instructions without failing with buffer size constraints.
1645 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1646}
1647
1648
1649CodePatcher::~CodePatcher() {
1650 // Indicate that code has changed.
1651 CPU::FlushICache(address_, size_);
1652
1653 // Check that the code was patched as expected.
1654 ASSERT(masm_.pc_ == address_ + size_);
1655 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1656}
1657
1658
1659void CodePatcher::Emit(Instr x) {
1660 masm()->emit(x);
1661}
1662
1663
1664void CodePatcher::Emit(Address addr) {
1665 masm()->emit(reinterpret_cast<Instr>(addr));
1666}
1667#endif // ENABLE_DEBUGGER_SUPPORT
1668
1669
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670} } // namespace v8::internal