blob: e356d55e1bba01a07ceca3b0c7b0c5044a4b8a10 [file] [log] [blame]
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
34#include "debug.h"
35#include "runtime.h"
36
kasperl@chromium.org71affb52009-05-26 05:44:31 +000037namespace v8 {
38namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040MacroAssembler::MacroAssembler(void* buffer, int size)
41 : Assembler(buffer, size),
kasper.lund7276f142008-07-30 08:49:36 +000042 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000043 allow_stub_calls_(true),
44 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045}
46
47
48// We always generate arm code, never thumb code, even if V8 is compiled to
49// thumb, so we require inter-working support
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000050#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051#error "flag -mthumb-interwork missing"
52#endif
53
54
55// We do not support thumb inter-working with an arm architecture not supporting
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000056// the blx instruction (below v5t). If you know what CPU you are compiling for
57// you can use -march=armv7 or similar.
58#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
59# error "For thumb inter-working we require an architecture which supports blx"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060#endif
61
62
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063// Using bx does not yield better code, so use it only when required
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000064#if defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065#define USE_BX 1
66#endif
67
68
69void MacroAssembler::Jump(Register target, Condition cond) {
70#if USE_BX
71 bx(target, cond);
72#else
73 mov(pc, Operand(target), LeaveCC, cond);
74#endif
75}
76
77
ager@chromium.org236ad962008-09-25 09:45:57 +000078void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
79 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080#if USE_BX
81 mov(ip, Operand(target, rmode), LeaveCC, cond);
82 bx(ip, cond);
83#else
84 mov(pc, Operand(target, rmode), LeaveCC, cond);
85#endif
86}
87
88
ager@chromium.org236ad962008-09-25 09:45:57 +000089void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
90 Condition cond) {
91 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
93}
94
95
ager@chromium.org236ad962008-09-25 09:45:57 +000096void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
97 Condition cond) {
98 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 // 'code' is always generated ARM code, never THUMB code
100 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
101}
102
103
104void MacroAssembler::Call(Register target, Condition cond) {
105#if USE_BLX
106 blx(target, cond);
107#else
108 // set lr for return at current pc + 8
109 mov(lr, Operand(pc), LeaveCC, cond);
110 mov(pc, Operand(target), LeaveCC, cond);
111#endif
112}
113
114
ager@chromium.org236ad962008-09-25 09:45:57 +0000115void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
116 Condition cond) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000117#if USE_BLX
118 // On ARMv5 and after the recommended call sequence is:
119 // ldr ip, [pc, #...]
120 // blx ip
121
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000122 // The two instructions (ldr and blx) could be separated by a constant
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000123 // pool and the code would still work. The issue comes from the
124 // patching code which expect the ldr to be just above the blx.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000125 { BlockConstPoolScope block_const_pool(this);
126 // Statement positions are expected to be recorded when the target
127 // address is loaded. The mov method will automatically record
128 // positions when pc is the target, since this is not the case here
129 // we have to do it explicitly.
130 WriteRecordedPositions();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000131
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000132 mov(ip, Operand(target, rmode), LeaveCC, cond);
133 blx(ip, cond);
134 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000135
136 ASSERT(kCallTargetAddressOffset == 2 * kInstrSize);
137#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 // Set lr for return at current pc + 8.
139 mov(lr, Operand(pc), LeaveCC, cond);
140 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
141 mov(pc, Operand(target, rmode), LeaveCC, cond);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000142
ager@chromium.org4af710e2009-09-15 12:20:11 +0000143 ASSERT(kCallTargetAddressOffset == kInstrSize);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000144#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145}
146
147
ager@chromium.org236ad962008-09-25 09:45:57 +0000148void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
149 Condition cond) {
150 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
152}
153
154
ager@chromium.org236ad962008-09-25 09:45:57 +0000155void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
156 Condition cond) {
157 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 // 'code' is always generated ARM code, never THUMB code
159 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
160}
161
162
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000163void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000165 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000167 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168#endif
169}
170
171
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000172void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
173 LoadRoot(ip, Heap::kStackLimitRootIndex);
174 cmp(sp, Operand(ip));
175 b(lo, on_stack_overflow);
176}
177
178
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000179void MacroAssembler::Drop(int count, Condition cond) {
180 if (count > 0) {
181 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000182 }
183}
184
185
ager@chromium.org357bf652010-04-12 11:30:10 +0000186void MacroAssembler::Swap(Register reg1, Register reg2, Register scratch) {
187 if (scratch.is(no_reg)) {
188 eor(reg1, reg1, Operand(reg2));
189 eor(reg2, reg2, Operand(reg1));
190 eor(reg1, reg1, Operand(reg2));
191 } else {
192 mov(scratch, reg1);
193 mov(reg1, reg2);
194 mov(reg2, scratch);
195 }
196}
197
198
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000199void MacroAssembler::Call(Label* target) {
200 bl(target);
201}
202
203
204void MacroAssembler::Move(Register dst, Handle<Object> value) {
205 mov(dst, Operand(value));
206}
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000207
208
ager@chromium.org357bf652010-04-12 11:30:10 +0000209void MacroAssembler::Move(Register dst, Register src) {
210 if (!dst.is(src)) {
211 mov(dst, src);
212 }
213}
214
215
ager@chromium.org8bb60582008-12-11 12:02:20 +0000216void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
217 // Empty the const pool.
218 CheckConstPool(true, true);
219 add(pc, pc, Operand(index,
220 LSL,
221 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000222 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000223 nop(); // Jump table alignment.
224 for (int i = 0; i < targets.length(); i++) {
225 b(targets[i]);
226 }
227}
228
229
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000230void MacroAssembler::LoadRoot(Register destination,
231 Heap::RootListIndex index,
232 Condition cond) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000233 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000234}
235
236
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000237void MacroAssembler::StoreRoot(Register source,
238 Heap::RootListIndex index,
239 Condition cond) {
240 str(source, MemOperand(roots, index << kPointerSizeLog2), cond);
241}
242
243
ager@chromium.orgac091b72010-05-05 07:34:42 +0000244void MacroAssembler::RecordWriteHelper(Register object,
245 Register offset,
246 Register scratch) {
247 if (FLAG_debug_code) {
248 // Check that the object is not in new space.
249 Label not_in_new_space;
250 InNewSpace(object, scratch, ne, &not_in_new_space);
251 Abort("new-space object passed to RecordWriteHelper");
252 bind(&not_in_new_space);
253 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 // This is how much we shift the remembered set bit offset to get the
256 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
257 // shift right 5) and then multiply by kIntSize (4, shift left 2).
258 const int kRSetWordShift = 3;
259
ager@chromium.orgac091b72010-05-05 07:34:42 +0000260 Label fast;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000263 // object: heap object pointer (with tag)
264 // offset: offset to store location from the object
265 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
266 and_(scratch, object, Operand(ip)); // offset into page of the object
267 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
269
270 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000271 // object: heap object pointer (with tag)
272 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 bic(object, object, Operand(ip));
274
275 // If the bit offset lies beyond the normal remembered set range, it is in
276 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000277 // object: page start
278 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 cmp(offset, Operand(Page::kPageSize / kPointerSize));
280 b(lt, &fast);
281
282 // Adjust the bit offset to be relative to the start of the extra
283 // remembered set and the start address to be the address of the extra
284 // remembered set.
285 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
286 // Load the array length into 'scratch' and multiply by four to get the
287 // size in bytes of the elements.
288 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
289 + FixedArray::kLengthOffset));
290 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
291 // Add the page header (including remembered set), array header, and array
292 // body size to the page address.
293 add(object, object, Operand(Page::kObjectStartOffset
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000294 + FixedArray::kHeaderSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 add(object, object, Operand(scratch));
296
297 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000299 // object: start of the remembered set (page start for the fast case)
300 // offset: bit offset of store position in the remembered set
301 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
302 add(object, object, Operand(scratch, LSR, kRSetWordShift));
303 // Get bit offset in the rset word.
304 // object: address of remembered set word
305 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 and_(offset, offset, Operand(kBitsPerInt - 1));
307
308 ldr(scratch, MemOperand(object));
309 mov(ip, Operand(1));
310 orr(scratch, scratch, Operand(ip, LSL, offset));
311 str(scratch, MemOperand(object));
ager@chromium.orgac091b72010-05-05 07:34:42 +0000312}
313
314
315void MacroAssembler::InNewSpace(Register object,
316 Register scratch,
317 Condition cc,
318 Label* branch) {
319 ASSERT(cc == eq || cc == ne);
320 and_(scratch, object, Operand(ExternalReference::new_space_mask()));
321 cmp(scratch, Operand(ExternalReference::new_space_start()));
322 b(cc, branch);
323}
324
325
326// Will clobber 4 registers: object, offset, scratch, ip. The
327// register 'object' contains a heap object pointer. The heap object
328// tag is shifted away.
329void MacroAssembler::RecordWrite(Register object, Register offset,
330 Register scratch) {
331 // The compiled code assumes that record write doesn't change the
332 // context register, so we check that none of the clobbered
333 // registers are cp.
334 ASSERT(!object.is(cp) && !offset.is(cp) && !scratch.is(cp));
335
336 Label done;
337
338 // First, test that the object is not in the new space. We cannot set
339 // remembered set bits in the new space.
340 InNewSpace(object, scratch, eq, &done);
341
342 // Record the actual write.
343 RecordWriteHelper(object, offset, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344
345 bind(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000346
347 // Clobber all input registers when running with the debug-code flag
348 // turned on to provoke errors.
349 if (FLAG_debug_code) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000350 mov(object, Operand(BitCast<int32_t>(kZapValue)));
351 mov(offset, Operand(BitCast<int32_t>(kZapValue)));
352 mov(scratch, Operand(BitCast<int32_t>(kZapValue)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000353 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354}
355
356
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000358 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000359 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
360 mov(ip, Operand(Smi::FromInt(type)));
361 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000362 mov(ip, Operand(CodeObject()));
363 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000364 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365}
366
367
ager@chromium.org7c537e22008-10-16 08:43:32 +0000368void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000369 // r0: preserved
370 // r1: preserved
371 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372
ager@chromium.org7c537e22008-10-16 08:43:32 +0000373 // Drop the execution stack down to the frame pointer and restore
374 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000375 mov(sp, fp);
376 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377}
378
379
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000380void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000381 // Compute the argv pointer and keep it in a callee-saved register.
382 // r0 is argc.
383 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
384 sub(r6, r6, Operand(kPointerSize));
385
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000386 // Compute callee's stack pointer before making changes and save it as
387 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000388 // popping the args.
389
390 // ip = sp + kPointerSize * #args;
391 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
392
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000393 // Prepare the stack to be aligned when calling into C. After this point there
394 // are 5 pushes before the call into C, so the stack needs to be aligned after
395 // 5 pushes.
396 int frame_alignment = ActivationFrameAlignment();
397 int frame_alignment_mask = frame_alignment - 1;
398 if (frame_alignment != kPointerSize) {
399 // The following code needs to be more general if this assert does not hold.
400 ASSERT(frame_alignment == 2 * kPointerSize);
401 // With 5 pushes left the frame must be unaligned at this point.
402 mov(r7, Operand(Smi::FromInt(0)));
403 tst(sp, Operand((frame_alignment - kPointerSize) & frame_alignment_mask));
404 push(r7, eq); // Push if aligned to make it unaligned.
405 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000406
ager@chromium.org236ad962008-09-25 09:45:57 +0000407 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
408 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +0000409 mov(fp, Operand(sp)); // Setup new frame pointer.
ager@chromium.org236ad962008-09-25 09:45:57 +0000410
ager@chromium.org5c838252010-02-19 08:53:10 +0000411 mov(ip, Operand(CodeObject()));
412 push(ip); // Accessed from ExitFrame::code_slot.
ager@chromium.org236ad962008-09-25 09:45:57 +0000413
414 // Save the frame pointer and the context in top.
415 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
416 str(fp, MemOperand(ip));
417 mov(ip, Operand(ExternalReference(Top::k_context_address)));
418 str(cp, MemOperand(ip));
419
420 // Setup argc and the builtin function in callee-saved registers.
421 mov(r4, Operand(r0));
422 mov(r5, Operand(r1));
423
ager@chromium.org236ad962008-09-25 09:45:57 +0000424
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000425#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000426 // Save the state of all registers to the stack from the memory
427 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000428 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000429 // Use sp as base to push.
430 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
431 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000432#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000433}
434
435
ager@chromium.orgac091b72010-05-05 07:34:42 +0000436void MacroAssembler::InitializeNewString(Register string,
437 Register length,
438 Heap::RootListIndex map_index,
439 Register scratch1,
440 Register scratch2) {
441 mov(scratch1, Operand(length, LSL, kSmiTagSize));
442 LoadRoot(scratch2, map_index);
443 str(scratch1, FieldMemOperand(string, String::kLengthOffset));
444 mov(scratch1, Operand(String::kEmptyHashField));
445 str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
446 str(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
447}
448
449
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000450int MacroAssembler::ActivationFrameAlignment() {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000451#if defined(V8_HOST_ARCH_ARM)
452 // Running on the real platform. Use the alignment as mandated by the local
453 // environment.
454 // Note: This will break if we ever start generating snapshots on one ARM
455 // platform for another ARM platform with a different alignment.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000456 return OS::ActivationFrameAlignment();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000457#else // defined(V8_HOST_ARCH_ARM)
458 // If we are using the simulator then we should always align to the expected
459 // alignment. As the simulator is used to generate snapshots we do not know
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000460 // if the target platform will need alignment, so this is controlled from a
461 // flag.
462 return FLAG_sim_stack_alignment;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000463#endif // defined(V8_HOST_ARCH_ARM)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000464}
465
466
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000467void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000468#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000469 // Restore the memory copy of the registers by digging them out from
470 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000471 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000472 // This code intentionally clobbers r2 and r3.
473 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000474 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000475 add(r3, fp, Operand(kOffset));
476 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
477 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000478#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000479
480 // Clear top frame.
481 mov(r3, Operand(0));
482 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
483 str(r3, MemOperand(ip));
484
485 // Restore current context from top and clear it in debug mode.
486 mov(ip, Operand(ExternalReference(Top::k_context_address)));
487 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000488#ifdef DEBUG
489 str(r3, MemOperand(ip));
490#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000491
492 // Pop the arguments, restore registers, and return.
493 mov(sp, Operand(fp)); // respect ABI stack constraint
494 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
495}
496
497
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000498void MacroAssembler::InvokePrologue(const ParameterCount& expected,
499 const ParameterCount& actual,
500 Handle<Code> code_constant,
501 Register code_reg,
502 Label* done,
503 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000504 bool definitely_matches = false;
505 Label regular_invoke;
506
507 // Check whether the expected and actual arguments count match. If not,
508 // setup registers according to contract with ArgumentsAdaptorTrampoline:
509 // r0: actual arguments count
510 // r1: function (passed through to callee)
511 // r2: expected arguments count
512 // r3: callee code entry
513
514 // The code below is made a lot easier because the calling code already sets
515 // up actual and expected registers according to the contract if values are
516 // passed in registers.
517 ASSERT(actual.is_immediate() || actual.reg().is(r0));
518 ASSERT(expected.is_immediate() || expected.reg().is(r2));
519 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
520
521 if (expected.is_immediate()) {
522 ASSERT(actual.is_immediate());
523 if (expected.immediate() == actual.immediate()) {
524 definitely_matches = true;
525 } else {
526 mov(r0, Operand(actual.immediate()));
527 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
528 if (expected.immediate() == sentinel) {
529 // Don't worry about adapting arguments for builtins that
530 // don't want that done. Skip adaption code by making it look
531 // like we have a match between expected and actual number of
532 // arguments.
533 definitely_matches = true;
534 } else {
535 mov(r2, Operand(expected.immediate()));
536 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000537 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000538 } else {
539 if (actual.is_immediate()) {
540 cmp(expected.reg(), Operand(actual.immediate()));
541 b(eq, &regular_invoke);
542 mov(r0, Operand(actual.immediate()));
543 } else {
544 cmp(expected.reg(), Operand(actual.reg()));
545 b(eq, &regular_invoke);
546 }
547 }
548
549 if (!definitely_matches) {
550 if (!code_constant.is_null()) {
551 mov(r3, Operand(code_constant));
552 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
553 }
554
555 Handle<Code> adaptor =
556 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
557 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000558 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000559 b(done);
560 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000561 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000562 }
563 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000564 }
565}
566
567
568void MacroAssembler::InvokeCode(Register code,
569 const ParameterCount& expected,
570 const ParameterCount& actual,
571 InvokeFlag flag) {
572 Label done;
573
574 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
575 if (flag == CALL_FUNCTION) {
576 Call(code);
577 } else {
578 ASSERT(flag == JUMP_FUNCTION);
579 Jump(code);
580 }
581
582 // Continue here if InvokePrologue does handle the invocation due to
583 // mismatched parameter counts.
584 bind(&done);
585}
586
587
588void MacroAssembler::InvokeCode(Handle<Code> code,
589 const ParameterCount& expected,
590 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000591 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000592 InvokeFlag flag) {
593 Label done;
594
595 InvokePrologue(expected, actual, code, no_reg, &done, flag);
596 if (flag == CALL_FUNCTION) {
597 Call(code, rmode);
598 } else {
599 Jump(code, rmode);
600 }
601
602 // Continue here if InvokePrologue does handle the invocation due to
603 // mismatched parameter counts.
604 bind(&done);
605}
606
607
608void MacroAssembler::InvokeFunction(Register fun,
609 const ParameterCount& actual,
610 InvokeFlag flag) {
611 // Contract with called JS functions requires that function is passed in r1.
612 ASSERT(fun.is(r1));
613
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000614 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000615 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000616
617 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
618 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
619 ldr(expected_reg,
620 FieldMemOperand(code_reg,
621 SharedFunctionInfo::kFormalParameterCountOffset));
622 ldr(code_reg,
623 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
624 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
625
626 ParameterCount expected(expected_reg);
627 InvokeCode(code_reg, expected, actual, flag);
628}
629
630
ager@chromium.org5c838252010-02-19 08:53:10 +0000631void MacroAssembler::InvokeFunction(JSFunction* function,
632 const ParameterCount& actual,
633 InvokeFlag flag) {
634 ASSERT(function->is_compiled());
635
636 // Get the function and setup the context.
637 mov(r1, Operand(Handle<JSFunction>(function)));
638 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
639
640 // Invoke the cached code.
641 Handle<Code> code(function->code());
642 ParameterCount expected(function->shared()->formal_parameter_count());
643 InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag);
644}
645
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000646#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647void MacroAssembler::SaveRegistersToMemory(RegList regs) {
648 ASSERT((regs & ~kJSCallerSaved) == 0);
649 // Copy the content of registers to memory location.
650 for (int i = 0; i < kNumJSCallerSaved; i++) {
651 int r = JSCallerSavedCode(i);
652 if ((regs & (1 << r)) != 0) {
653 Register reg = { r };
654 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
655 str(reg, MemOperand(ip));
656 }
657 }
658}
659
660
661void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
662 ASSERT((regs & ~kJSCallerSaved) == 0);
663 // Copy the content of memory location to registers.
664 for (int i = kNumJSCallerSaved; --i >= 0;) {
665 int r = JSCallerSavedCode(i);
666 if ((regs & (1 << r)) != 0) {
667 Register reg = { r };
668 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
669 ldr(reg, MemOperand(ip));
670 }
671 }
672}
673
674
675void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
676 RegList regs) {
677 ASSERT((regs & ~kJSCallerSaved) == 0);
678 // Copy the content of the memory location to the stack and adjust base.
679 for (int i = kNumJSCallerSaved; --i >= 0;) {
680 int r = JSCallerSavedCode(i);
681 if ((regs & (1 << r)) != 0) {
682 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
683 ldr(ip, MemOperand(ip));
684 str(ip, MemOperand(base, 4, NegPreIndex));
685 }
686 }
687}
688
689
690void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
691 Register scratch,
692 RegList regs) {
693 ASSERT((regs & ~kJSCallerSaved) == 0);
694 // Copy the content of the stack to the memory location and adjust base.
695 for (int i = 0; i < kNumJSCallerSaved; i++) {
696 int r = JSCallerSavedCode(i);
697 if ((regs & (1 << r)) != 0) {
698 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
699 ldr(scratch, MemOperand(base, 4, PostIndex));
700 str(scratch, MemOperand(ip));
701 }
702 }
703}
ager@chromium.org5c838252010-02-19 08:53:10 +0000704
705
706void MacroAssembler::DebugBreak() {
707 ASSERT(allow_stub_calls());
708 mov(r0, Operand(0));
709 mov(r1, Operand(ExternalReference(Runtime::kDebugBreak)));
710 CEntryStub ces(1);
711 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
712}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000713#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716void MacroAssembler::PushTryHandler(CodeLocation try_location,
717 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000718 // Adjust this code if not the case.
719 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 // The pc (return address) is passed in register lr.
721 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 if (type == TRY_CATCH_HANDLER) {
723 mov(r3, Operand(StackHandler::TRY_CATCH));
724 } else {
725 mov(r3, Operand(StackHandler::TRY_FINALLY));
726 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000727 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
728 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
729 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
730 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
731 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
733 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000734 ASSERT(StackHandlerConstants::kNextOffset == 0);
735 push(r1);
736 // Link this handler as the new current one.
737 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000739 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000741 // The frame pointer does not point to a JS frame so we save NULL
742 // for fp. We expect the code throwing an exception to check fp
743 // before dereferencing it to restore the context.
744 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000746 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
747 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
748 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
749 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
750 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
752 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000753 ASSERT(StackHandlerConstants::kNextOffset == 0);
754 push(r6);
755 // Link this handler as the new current one.
756 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 }
758}
759
760
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000761void MacroAssembler::PopTryHandler() {
762 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
763 pop(r1);
764 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
765 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
766 str(r1, MemOperand(ip));
767}
768
769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
771 JSObject* holder, Register holder_reg,
772 Register scratch,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000773 int save_at_depth,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 Label* miss) {
775 // Make sure there's no overlap between scratch and the other
776 // registers.
777 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
778
779 // Keep track of the current object in register reg.
780 Register reg = object_reg;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000781 int depth = 0;
782
783 if (save_at_depth == depth) {
784 str(reg, MemOperand(sp));
785 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786
787 // Check the maps in the prototype chain.
788 // Traverse the prototype chain from the object and do map checks.
789 while (object != holder) {
790 depth++;
791
792 // Only global objects and objects that do not require access
793 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000794 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795
796 // Get the map of the current object.
797 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
798 cmp(scratch, Operand(Handle<Map>(object->map())));
799
800 // Branch on the result of the map check.
801 b(ne, miss);
802
803 // Check access rights to the global object. This has to happen
804 // after the map check so that we know that the object is
805 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000806 if (object->IsJSGlobalProxy()) {
807 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808 // Restore scratch register to be the map of the object. In the
809 // new space case below, we load the prototype from the map in
810 // the scratch register.
811 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
812 }
813
814 reg = holder_reg; // from now the object is in holder_reg
815 JSObject* prototype = JSObject::cast(object->GetPrototype());
816 if (Heap::InNewSpace(prototype)) {
817 // The prototype is in new space; we cannot store a reference
818 // to it in the code. Load it from the map.
819 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
820 } else {
821 // The prototype is in old space; load it directly.
822 mov(reg, Operand(Handle<JSObject>(prototype)));
823 }
824
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000825 if (save_at_depth == depth) {
826 str(reg, MemOperand(sp));
827 }
828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 // Go to the next object in the prototype chain.
830 object = prototype;
831 }
832
833 // Check the holder map.
834 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
835 cmp(scratch, Operand(Handle<Map>(object->map())));
836 b(ne, miss);
837
838 // Log the check depth.
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000839 LOG(IntEvent("check-maps-depth", depth + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
841 // Perform security check for access to the global object and return
842 // the holder register.
843 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000844 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
845 if (object->IsJSGlobalProxy()) {
846 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847 }
848 return reg;
849}
850
851
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000852void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
853 Register scratch,
854 Label* miss) {
855 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000857 ASSERT(!holder_reg.is(scratch));
858 ASSERT(!holder_reg.is(ip));
859 ASSERT(!scratch.is(ip));
860
861 // Load current lexical context from the stack frame.
862 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
863 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000864#ifdef DEBUG
865 cmp(scratch, Operand(0));
866 Check(ne, "we should not have an empty lexical context");
867#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000869 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
871 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000872 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
873
874 // Check the context is a global context.
875 if (FLAG_debug_code) {
876 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
877 // Cannot use ip as a temporary in this verification code. Due to the fact
878 // that ip is clobbered as part of cmp with an object Operand.
879 push(holder_reg); // Temporarily save holder on the stack.
880 // Read the first word and compare to the global_context_map.
881 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000882 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
883 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000884 Check(eq, "JSGlobalObject::global_context should be a global context.");
885 pop(holder_reg); // Restore holder.
886 }
887
888 // Check if both contexts are the same.
889 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
890 cmp(scratch, Operand(ip));
891 b(eq, &same_contexts);
892
893 // Check the context is a global context.
894 if (FLAG_debug_code) {
895 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
896 // Cannot use ip as a temporary in this verification code. Due to the fact
897 // that ip is clobbered as part of cmp with an object Operand.
898 push(holder_reg); // Temporarily save holder on the stack.
899 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000900 LoadRoot(ip, Heap::kNullValueRootIndex);
901 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000902 Check(ne, "JSGlobalProxy::context() should not be null.");
903
904 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000905 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
906 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000907 Check(eq, "JSGlobalObject::global_context should be a global context.");
908 // Restore ip is not needed. ip is reloaded below.
909 pop(holder_reg); // Restore holder.
910 // Restore ip to holder's context.
911 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
912 }
913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 // Check that the security token in the calling global object is
915 // compatible with the security token in the receiving global
916 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000917 int token_offset = Context::kHeaderSize +
918 Context::SECURITY_TOKEN_INDEX * kPointerSize;
919
920 ldr(scratch, FieldMemOperand(scratch, token_offset));
921 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 cmp(scratch, Operand(ip));
923 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000924
925 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926}
927
928
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000929void MacroAssembler::AllocateInNewSpace(int object_size,
930 Register result,
931 Register scratch1,
932 Register scratch2,
933 Label* gc_required,
934 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000935 ASSERT(!result.is(scratch1));
936 ASSERT(!scratch1.is(scratch2));
937
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000938 // Make object size into bytes.
939 if ((flags & SIZE_IN_WORDS) != 0) {
940 object_size *= kPointerSize;
941 }
942 ASSERT_EQ(0, object_size & kObjectAlignmentMask);
943
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000944 // Load address of new object into result and allocation top address into
945 // scratch1.
946 ExternalReference new_space_allocation_top =
947 ExternalReference::new_space_allocation_top_address();
948 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000949 if ((flags & RESULT_CONTAINS_TOP) == 0) {
950 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000951 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000952 // Assert that result actually contains top on entry. scratch2 is used
953 // immediately below so this use of scratch2 does not cause difference with
954 // respect to register content between debug and release mode.
955 ldr(scratch2, MemOperand(scratch1));
956 cmp(result, scratch2);
957 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +0000958 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000959
960 // Calculate new top and bail out if new space is exhausted. Use result
961 // to calculate the new top.
962 ExternalReference new_space_allocation_limit =
963 ExternalReference::new_space_allocation_limit_address();
964 mov(scratch2, Operand(new_space_allocation_limit));
965 ldr(scratch2, MemOperand(scratch2));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000966 add(result, result, Operand(object_size));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000967 cmp(result, Operand(scratch2));
968 b(hi, gc_required);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000969 str(result, MemOperand(scratch1));
970
971 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000972 if ((flags & TAG_OBJECT) != 0) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000973 sub(result, result, Operand(object_size - kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000974 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000975 sub(result, result, Operand(object_size));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000976 }
977}
978
979
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000980void MacroAssembler::AllocateInNewSpace(Register object_size,
981 Register result,
982 Register scratch1,
983 Register scratch2,
984 Label* gc_required,
985 AllocationFlags flags) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000986 ASSERT(!result.is(scratch1));
987 ASSERT(!scratch1.is(scratch2));
988
989 // Load address of new object into result and allocation top address into
990 // scratch1.
991 ExternalReference new_space_allocation_top =
992 ExternalReference::new_space_allocation_top_address();
993 mov(scratch1, Operand(new_space_allocation_top));
994 if ((flags & RESULT_CONTAINS_TOP) == 0) {
995 ldr(result, MemOperand(scratch1));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000996 } else if (FLAG_debug_code) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000997 // Assert that result actually contains top on entry. scratch2 is used
998 // immediately below so this use of scratch2 does not cause difference with
999 // respect to register content between debug and release mode.
1000 ldr(scratch2, MemOperand(scratch1));
1001 cmp(result, scratch2);
1002 Check(eq, "Unexpected allocation top");
ager@chromium.orga1645e22009-09-09 19:27:10 +00001003 }
1004
1005 // Calculate new top and bail out if new space is exhausted. Use result
1006 // to calculate the new top. Object size is in words so a shift is required to
1007 // get the number of bytes
1008 ExternalReference new_space_allocation_limit =
1009 ExternalReference::new_space_allocation_limit_address();
1010 mov(scratch2, Operand(new_space_allocation_limit));
1011 ldr(scratch2, MemOperand(scratch2));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001012 if ((flags & SIZE_IN_WORDS) != 0) {
1013 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1014 } else {
1015 add(result, result, Operand(object_size));
1016 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001017 cmp(result, Operand(scratch2));
1018 b(hi, gc_required);
1019
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001020 // Update allocation top. result temporarily holds the new top.
1021 if (FLAG_debug_code) {
1022 tst(result, Operand(kObjectAlignmentMask));
1023 Check(eq, "Unaligned allocation in new space");
1024 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001025 str(result, MemOperand(scratch1));
1026
1027 // Adjust back to start of new object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001028 if ((flags & SIZE_IN_WORDS) != 0) {
1029 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
1030 } else {
1031 sub(result, result, Operand(object_size));
1032 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001033
1034 // Tag object if requested.
1035 if ((flags & TAG_OBJECT) != 0) {
1036 add(result, result, Operand(kHeapObjectTag));
1037 }
1038}
1039
1040
1041void MacroAssembler::UndoAllocationInNewSpace(Register object,
1042 Register scratch) {
1043 ExternalReference new_space_allocation_top =
1044 ExternalReference::new_space_allocation_top_address();
1045
1046 // Make sure the object has no tag before resetting top.
1047 and_(object, object, Operand(~kHeapObjectTagMask));
1048#ifdef DEBUG
1049 // Check that the object un-allocated is below the current top.
1050 mov(scratch, Operand(new_space_allocation_top));
1051 ldr(scratch, MemOperand(scratch));
1052 cmp(object, scratch);
1053 Check(lt, "Undo allocation of non allocated memory");
1054#endif
1055 // Write the address of the object to un-allocate as the current top.
1056 mov(scratch, Operand(new_space_allocation_top));
1057 str(object, MemOperand(scratch));
1058}
1059
1060
ager@chromium.org5c838252010-02-19 08:53:10 +00001061void MacroAssembler::AllocateTwoByteString(Register result,
1062 Register length,
1063 Register scratch1,
1064 Register scratch2,
1065 Register scratch3,
1066 Label* gc_required) {
1067 // Calculate the number of bytes needed for the characters in the string while
1068 // observing object alignment.
1069 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1070 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
1071 add(scratch1, scratch1,
1072 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001073 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
ager@chromium.org5c838252010-02-19 08:53:10 +00001074
1075 // Allocate two-byte string in new space.
1076 AllocateInNewSpace(scratch1,
1077 result,
1078 scratch2,
1079 scratch3,
1080 gc_required,
1081 TAG_OBJECT);
1082
1083 // Set the map, length and hash field.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001084 InitializeNewString(result,
1085 length,
1086 Heap::kStringMapRootIndex,
1087 scratch1,
1088 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001089}
1090
1091
1092void MacroAssembler::AllocateAsciiString(Register result,
1093 Register length,
1094 Register scratch1,
1095 Register scratch2,
1096 Register scratch3,
1097 Label* gc_required) {
1098 // Calculate the number of bytes needed for the characters in the string while
1099 // observing object alignment.
1100 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1101 ASSERT(kCharSize == 1);
1102 add(scratch1, length,
1103 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001104 and_(scratch1, scratch1, Operand(~kObjectAlignmentMask));
ager@chromium.org5c838252010-02-19 08:53:10 +00001105
1106 // Allocate ASCII string in new space.
1107 AllocateInNewSpace(scratch1,
1108 result,
1109 scratch2,
1110 scratch3,
1111 gc_required,
1112 TAG_OBJECT);
1113
1114 // Set the map, length and hash field.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001115 InitializeNewString(result,
1116 length,
1117 Heap::kAsciiStringMapRootIndex,
1118 scratch1,
1119 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001120}
1121
1122
1123void MacroAssembler::AllocateTwoByteConsString(Register result,
1124 Register length,
1125 Register scratch1,
1126 Register scratch2,
1127 Label* gc_required) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001128 AllocateInNewSpace(ConsString::kSize,
ager@chromium.org5c838252010-02-19 08:53:10 +00001129 result,
1130 scratch1,
1131 scratch2,
1132 gc_required,
1133 TAG_OBJECT);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001134
1135 InitializeNewString(result,
1136 length,
1137 Heap::kConsStringMapRootIndex,
1138 scratch1,
1139 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001140}
1141
1142
1143void MacroAssembler::AllocateAsciiConsString(Register result,
1144 Register length,
1145 Register scratch1,
1146 Register scratch2,
1147 Label* gc_required) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001148 AllocateInNewSpace(ConsString::kSize,
ager@chromium.org5c838252010-02-19 08:53:10 +00001149 result,
1150 scratch1,
1151 scratch2,
1152 gc_required,
1153 TAG_OBJECT);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001154
1155 InitializeNewString(result,
1156 length,
1157 Heap::kConsAsciiStringMapRootIndex,
1158 scratch1,
1159 scratch2);
ager@chromium.org5c838252010-02-19 08:53:10 +00001160}
1161
1162
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001163void MacroAssembler::CompareObjectType(Register object,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001164 Register map,
1165 Register type_reg,
1166 InstanceType type) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001167 ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001168 CompareInstanceType(map, type_reg, type);
1169}
1170
1171
1172void MacroAssembler::CompareInstanceType(Register map,
1173 Register type_reg,
1174 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001175 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1176 cmp(type_reg, Operand(type));
1177}
1178
1179
ager@chromium.org5c838252010-02-19 08:53:10 +00001180void MacroAssembler::CheckMap(Register obj,
1181 Register scratch,
1182 Handle<Map> map,
1183 Label* fail,
1184 bool is_heap_object) {
1185 if (!is_heap_object) {
1186 BranchOnSmi(obj, fail);
1187 }
1188 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1189 mov(ip, Operand(map));
1190 cmp(scratch, ip);
1191 b(ne, fail);
1192}
1193
1194
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001195void MacroAssembler::TryGetFunctionPrototype(Register function,
1196 Register result,
1197 Register scratch,
1198 Label* miss) {
1199 // Check that the receiver isn't a smi.
1200 BranchOnSmi(function, miss);
1201
1202 // Check that the function really is a function. Load map into result reg.
1203 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1204 b(ne, miss);
1205
1206 // Make sure that the function has an instance prototype.
1207 Label non_instance;
1208 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1209 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1210 b(ne, &non_instance);
1211
1212 // Get the prototype or initial map from the function.
1213 ldr(result,
1214 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1215
1216 // If the prototype or initial map is the hole, don't return it and
1217 // simply miss the cache instead. This will allow us to allocate a
1218 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001219 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1220 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001221 b(eq, miss);
1222
1223 // If the function does not have an initial map, we're done.
1224 Label done;
1225 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1226 b(ne, &done);
1227
1228 // Get the prototype from the initial map.
1229 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1230 jmp(&done);
1231
1232 // Non-instance prototype: Fetch prototype from constructor field
1233 // in initial map.
1234 bind(&non_instance);
1235 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1236
1237 // All done.
1238 bind(&done);
1239}
1240
1241
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001242void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +00001243 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001244 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245}
1246
1247
ager@chromium.org5c838252010-02-19 08:53:10 +00001248void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1249 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1250 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1251}
1252
1253
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254void MacroAssembler::StubReturn(int argc) {
1255 ASSERT(argc >= 1 && generating_stub());
ager@chromium.org5c838252010-02-19 08:53:10 +00001256 if (argc > 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 add(sp, sp, Operand((argc - 1) * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00001258 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 Ret();
1260}
1261
mads.s.ager31e71382008-08-13 09:32:07 +00001262
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001263void MacroAssembler::IllegalOperation(int num_arguments) {
1264 if (num_arguments > 0) {
1265 add(sp, sp, Operand(num_arguments * kPointerSize));
1266 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001267 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001268}
1269
1270
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001271void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1272 Register outHighReg,
1273 Register outLowReg) {
1274 // ARMv7 VFP3 instructions to implement integer to double conversion.
1275 mov(r7, Operand(inReg, ASR, kSmiTagSize));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001276 vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001277 vcvt_f64_s32(d7, s15);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001278 vmov(outLowReg, outHighReg, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001279}
1280
1281
ager@chromium.org5c838252010-02-19 08:53:10 +00001282void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1283 Register src,
1284 int num_least_bits) {
1285 if (CpuFeatures::IsSupported(ARMv7)) {
1286 ubfx(dst, src, Operand(kSmiTagSize), Operand(num_least_bits - 1));
1287 } else {
1288 mov(dst, Operand(src, ASR, kSmiTagSize));
1289 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1290 }
1291}
1292
1293
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +00001295 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001297 // If the expected number of arguments of the runtime function is
1298 // constant, we check that the actual number of arguments match the
1299 // expectation.
1300 if (f->nargs >= 0 && f->nargs != num_arguments) {
1301 IllegalOperation(num_arguments);
1302 return;
1303 }
kasper.lund7276f142008-07-30 08:49:36 +00001304
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305 // TODO(1236192): Most runtime routines don't need the number of
1306 // arguments passed in because it is constant. At some point we
1307 // should remove this need and make the runtime routine entry code
1308 // smarter.
1309 mov(r0, Operand(num_arguments));
1310 mov(r1, Operand(ExternalReference(f)));
1311 CEntryStub stub(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 CallStub(&stub);
1313}
1314
1315
1316void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1317 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1318}
1319
1320
ager@chromium.org5c838252010-02-19 08:53:10 +00001321void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1322 int num_arguments) {
1323 mov(r0, Operand(num_arguments));
1324 mov(r1, Operand(ext));
1325
1326 CEntryStub stub(1);
1327 CallStub(&stub);
1328}
1329
1330
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001331void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1332 int num_arguments,
1333 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001334 // TODO(1236192): Most runtime routines don't need the number of
1335 // arguments passed in because it is constant. At some point we
1336 // should remove this need and make the runtime routine entry code
1337 // smarter.
1338 mov(r0, Operand(num_arguments));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001339 JumpToExternalReference(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340}
1341
1342
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001343void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1344 int num_arguments,
1345 int result_size) {
1346 TailCallExternalReference(ExternalReference(fid), num_arguments, result_size);
1347}
1348
1349
1350void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351#if defined(__thumb__)
1352 // Thumb mode builtin.
1353 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1354#endif
1355 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001356 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001357 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358}
1359
1360
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001361void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1362 InvokeJSFlags flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001363 GetBuiltinEntry(r2, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 if (flags == CALL_JS) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001365 Call(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 } else {
1367 ASSERT(flags == JUMP_JS);
ager@chromium.org5c838252010-02-19 08:53:10 +00001368 Jump(r2);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001369 }
1370}
1371
1372
1373void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001374 ASSERT(!target.is(r1));
1375
1376 // Load the builtins object into target register.
1377 ldr(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1378 ldr(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
1379
ager@chromium.org5c838252010-02-19 08:53:10 +00001380 // Load the JavaScript builtin function from the builtins object.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001381 ldr(r1, FieldMemOperand(target,
1382 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
1383
1384 // Load the code entry point from the builtins object.
1385 ldr(target, FieldMemOperand(target,
1386 JSBuiltinsObject::OffsetOfCodeWithId(id)));
1387 if (FLAG_debug_code) {
1388 // Make sure the code objects in the builtins object and in the
1389 // builtin function are the same.
1390 push(r1);
1391 ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1392 ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
1393 cmp(r1, target);
1394 Assert(eq, "Builtin code object changed");
1395 pop(r1);
1396 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001397 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398}
1399
1400
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001401void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1402 Register scratch1, Register scratch2) {
1403 if (FLAG_native_code_counters && counter->Enabled()) {
1404 mov(scratch1, Operand(value));
1405 mov(scratch2, Operand(ExternalReference(counter)));
1406 str(scratch1, MemOperand(scratch2));
1407 }
1408}
1409
1410
1411void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1412 Register scratch1, Register scratch2) {
1413 ASSERT(value > 0);
1414 if (FLAG_native_code_counters && counter->Enabled()) {
1415 mov(scratch2, Operand(ExternalReference(counter)));
1416 ldr(scratch1, MemOperand(scratch2));
1417 add(scratch1, scratch1, Operand(value));
1418 str(scratch1, MemOperand(scratch2));
1419 }
1420}
1421
1422
1423void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1424 Register scratch1, Register scratch2) {
1425 ASSERT(value > 0);
1426 if (FLAG_native_code_counters && counter->Enabled()) {
1427 mov(scratch2, Operand(ExternalReference(counter)));
1428 ldr(scratch1, MemOperand(scratch2));
1429 sub(scratch1, scratch1, Operand(value));
1430 str(scratch1, MemOperand(scratch2));
1431 }
1432}
1433
1434
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435void MacroAssembler::Assert(Condition cc, const char* msg) {
1436 if (FLAG_debug_code)
1437 Check(cc, msg);
1438}
1439
1440
1441void MacroAssembler::Check(Condition cc, const char* msg) {
1442 Label L;
1443 b(cc, &L);
1444 Abort(msg);
1445 // will not return here
1446 bind(&L);
1447}
1448
1449
1450void MacroAssembler::Abort(const char* msg) {
1451 // We want to pass the msg string like a smi to avoid GC
1452 // problems, however msg is not guaranteed to be aligned
1453 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001454 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 // from the real pointer as a smi.
1456 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1457 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1458 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1459#ifdef DEBUG
1460 if (msg != NULL) {
1461 RecordComment("Abort message: ");
1462 RecordComment(msg);
1463 }
1464#endif
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001465 // Disable stub call restrictions to always allow calls to abort.
1466 set_allow_stub_calls(true);
1467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 mov(r0, Operand(p0));
1469 push(r0);
1470 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001471 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 CallRuntime(Runtime::kAbort, 2);
1473 // will not return here
1474}
1475
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001476
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001477void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1478 if (context_chain_length > 0) {
1479 // Move up the chain of contexts to the context containing the slot.
1480 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1481 // Load the function context (which is the incoming, outer context).
1482 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1483 for (int i = 1; i < context_chain_length; i++) {
1484 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1485 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1486 }
1487 // The context may be an intermediate context, not a function context.
1488 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1489 } else { // Slot is in the current function context.
1490 // The context may be an intermediate context, not a function context.
1491 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1492 }
1493}
1494
1495
ager@chromium.org5c838252010-02-19 08:53:10 +00001496void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1497 Register reg2,
1498 Label* on_not_both_smi) {
1499 ASSERT_EQ(0, kSmiTag);
1500 tst(reg1, Operand(kSmiTagMask));
1501 tst(reg2, Operand(kSmiTagMask), eq);
1502 b(ne, on_not_both_smi);
1503}
1504
1505
1506void MacroAssembler::JumpIfEitherSmi(Register reg1,
1507 Register reg2,
1508 Label* on_either_smi) {
1509 ASSERT_EQ(0, kSmiTag);
1510 tst(reg1, Operand(kSmiTagMask));
1511 tst(reg2, Operand(kSmiTagMask), ne);
1512 b(eq, on_either_smi);
1513}
1514
1515
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1517 Register first,
1518 Register second,
1519 Register scratch1,
1520 Register scratch2,
1521 Label* failure) {
1522 // Test that both first and second are sequential ASCII strings.
1523 // Assume that they are non-smis.
1524 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1525 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1526 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1527 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001528
1529 JumpIfBothInstanceTypesAreNotSequentialAscii(scratch1,
1530 scratch2,
1531 scratch1,
1532 scratch2,
1533 failure);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001534}
1535
1536void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1537 Register second,
1538 Register scratch1,
1539 Register scratch2,
1540 Label* failure) {
1541 // Check that neither is a smi.
1542 ASSERT_EQ(0, kSmiTag);
1543 and_(scratch1, first, Operand(second));
1544 tst(scratch1, Operand(kSmiTagMask));
1545 b(eq, failure);
1546 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1547 second,
1548 scratch1,
1549 scratch2,
1550 failure);
1551}
1552
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001553
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001554// Allocates a heap number or jumps to the need_gc label if the young space
1555// is full and a scavenge is needed.
1556void MacroAssembler::AllocateHeapNumber(Register result,
1557 Register scratch1,
1558 Register scratch2,
1559 Label* gc_required) {
1560 // Allocate an object in the heap for the heap number and tag it as a heap
1561 // object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001562 AllocateInNewSpace(HeapNumber::kSize,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001563 result,
1564 scratch1,
1565 scratch2,
1566 gc_required,
1567 TAG_OBJECT);
1568
1569 // Get heap number map and store it in the allocated object.
1570 LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
1571 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1572}
1573
1574
1575void MacroAssembler::CountLeadingZeros(Register source,
1576 Register scratch,
1577 Register zeros) {
1578#ifdef CAN_USE_ARMV5_INSTRUCTIONS
1579 clz(zeros, source); // This instruction is only supported after ARM5.
1580#else
1581 mov(zeros, Operand(0));
1582 mov(scratch, source);
1583 // Top 16.
1584 tst(scratch, Operand(0xffff0000));
1585 add(zeros, zeros, Operand(16), LeaveCC, eq);
1586 mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
1587 // Top 8.
1588 tst(scratch, Operand(0xff000000));
1589 add(zeros, zeros, Operand(8), LeaveCC, eq);
1590 mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
1591 // Top 4.
1592 tst(scratch, Operand(0xf0000000));
1593 add(zeros, zeros, Operand(4), LeaveCC, eq);
1594 mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
1595 // Top 2.
1596 tst(scratch, Operand(0xc0000000));
1597 add(zeros, zeros, Operand(2), LeaveCC, eq);
1598 mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
1599 // Top bit.
1600 tst(scratch, Operand(0x80000000u));
1601 add(zeros, zeros, Operand(1), LeaveCC, eq);
1602#endif
1603}
1604
1605
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001606void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1607 Register first,
1608 Register second,
1609 Register scratch1,
1610 Register scratch2,
1611 Label* failure) {
1612 int kFlatAsciiStringMask =
1613 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1614 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1615 and_(scratch1, first, Operand(kFlatAsciiStringMask));
1616 and_(scratch2, second, Operand(kFlatAsciiStringMask));
1617 cmp(scratch1, Operand(kFlatAsciiStringTag));
1618 // Ignore second test if first test failed.
1619 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1620 b(ne, failure);
1621}
1622
1623
1624void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(Register type,
1625 Register scratch,
1626 Label* failure) {
1627 int kFlatAsciiStringMask =
1628 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1629 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1630 and_(scratch, type, Operand(kFlatAsciiStringMask));
1631 cmp(scratch, Operand(kFlatAsciiStringTag));
1632 b(ne, failure);
1633}
1634
1635
ager@chromium.org357bf652010-04-12 11:30:10 +00001636void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001637 int frame_alignment = ActivationFrameAlignment();
ager@chromium.org357bf652010-04-12 11:30:10 +00001638 // Up to four simple arguments are passed in registers r0..r3.
1639 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001640 if (frame_alignment > kPointerSize) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001641 // Make stack end at alignment and make room for num_arguments - 4 words
1642 // and the original value of sp.
1643 mov(scratch, sp);
1644 sub(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001645 ASSERT(IsPowerOf2(frame_alignment));
1646 and_(sp, sp, Operand(-frame_alignment));
ager@chromium.org357bf652010-04-12 11:30:10 +00001647 str(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
1648 } else {
1649 sub(sp, sp, Operand(stack_passed_arguments * kPointerSize));
1650 }
1651}
1652
1653
1654void MacroAssembler::CallCFunction(ExternalReference function,
1655 int num_arguments) {
1656 mov(ip, Operand(function));
1657 CallCFunction(ip, num_arguments);
1658}
1659
1660
1661void MacroAssembler::CallCFunction(Register function, int num_arguments) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001662 // Make sure that the stack is aligned before calling a C function unless
1663 // running in the simulator. The simulator has its own alignment check which
1664 // provides more information.
1665#if defined(V8_HOST_ARCH_ARM)
1666 if (FLAG_debug_code) {
1667 int frame_alignment = OS::ActivationFrameAlignment();
1668 int frame_alignment_mask = frame_alignment - 1;
1669 if (frame_alignment > kPointerSize) {
1670 ASSERT(IsPowerOf2(frame_alignment));
1671 Label alignment_as_expected;
1672 tst(sp, Operand(frame_alignment_mask));
1673 b(eq, &alignment_as_expected);
1674 // Don't use Check here, as it will call Runtime_Abort possibly
1675 // re-entering here.
1676 stop("Unexpected alignment");
1677 bind(&alignment_as_expected);
1678 }
1679 }
1680#endif
1681
ager@chromium.org357bf652010-04-12 11:30:10 +00001682 // Just call directly. The function called cannot cause a GC, or
1683 // allow preemption, so the return address in the link register
1684 // stays correct.
1685 Call(function);
1686 int stack_passed_arguments = (num_arguments <= 4) ? 0 : num_arguments - 4;
1687 if (OS::ActivationFrameAlignment() > kPointerSize) {
1688 ldr(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
1689 } else {
1690 add(sp, sp, Operand(stack_passed_arguments * sizeof(kPointerSize)));
1691 }
1692}
1693
1694
ager@chromium.org4af710e2009-09-15 12:20:11 +00001695#ifdef ENABLE_DEBUGGER_SUPPORT
1696CodePatcher::CodePatcher(byte* address, int instructions)
1697 : address_(address),
1698 instructions_(instructions),
1699 size_(instructions * Assembler::kInstrSize),
1700 masm_(address, size_ + Assembler::kGap) {
1701 // Create a new macro assembler pointing to the address of the code to patch.
1702 // The size is adjusted with kGap on order for the assembler to generate size
1703 // bytes of instructions without failing with buffer size constraints.
1704 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1705}
1706
1707
1708CodePatcher::~CodePatcher() {
1709 // Indicate that code has changed.
1710 CPU::FlushICache(address_, size_);
1711
1712 // Check that the code was patched as expected.
1713 ASSERT(masm_.pc_ == address_ + size_);
1714 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1715}
1716
1717
1718void CodePatcher::Emit(Instr x) {
1719 masm()->emit(x);
1720}
1721
1722
1723void CodePatcher::Emit(Address addr) {
1724 masm()->emit(reinterpret_cast<Instr>(addr));
1725}
1726#endif // ENABLE_DEBUGGER_SUPPORT
1727
1728
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001730
1731#endif // V8_TARGET_ARCH_ARM