blob: a668cb1f71891c834bf898386eac145fc0632a9a [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),
40 unresolved_(0),
kasper.lund7276f142008-07-30 08:49:36 +000041 generating_stub_(false),
kasperl@chromium.org061ef742009-02-27 12:16:20 +000042 allow_stub_calls_(true),
43 code_object_(Heap::undefined_value()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044}
45
46
47// We always generate arm code, never thumb code, even if V8 is compiled to
48// thumb, so we require inter-working support
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050#error "flag -mthumb-interwork missing"
51#endif
52
53
54// We do not support thumb inter-working with an arm architecture not supporting
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000055// the blx instruction (below v5t). If you know what CPU you are compiling for
56// you can use -march=armv7 or similar.
57#if defined(USE_THUMB_INTERWORK) && !defined(CAN_USE_THUMB_INSTRUCTIONS)
58# error "For thumb inter-working we require an architecture which supports blx"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059#endif
60
61
62// Using blx may yield better code, so use it when required or when available
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000063#if defined(USE_THUMB_INTERWORK) || defined(CAN_USE_ARMV5_INSTRUCTIONS)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064#define USE_BLX 1
65#endif
66
67// Using bx does not yield better code, so use it only when required
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000068#if defined(USE_THUMB_INTERWORK)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069#define USE_BX 1
70#endif
71
72
73void MacroAssembler::Jump(Register target, Condition cond) {
74#if USE_BX
75 bx(target, cond);
76#else
77 mov(pc, Operand(target), LeaveCC, cond);
78#endif
79}
80
81
ager@chromium.org236ad962008-09-25 09:45:57 +000082void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
83 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084#if USE_BX
85 mov(ip, Operand(target, rmode), LeaveCC, cond);
86 bx(ip, cond);
87#else
88 mov(pc, Operand(target, rmode), LeaveCC, cond);
89#endif
90}
91
92
ager@chromium.org236ad962008-09-25 09:45:57 +000093void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
94 Condition cond) {
95 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
97}
98
99
ager@chromium.org236ad962008-09-25 09:45:57 +0000100void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
101 Condition cond) {
102 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 // 'code' is always generated ARM code, never THUMB code
104 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
105}
106
107
108void MacroAssembler::Call(Register target, Condition cond) {
109#if USE_BLX
110 blx(target, cond);
111#else
112 // set lr for return at current pc + 8
113 mov(lr, Operand(pc), LeaveCC, cond);
114 mov(pc, Operand(target), LeaveCC, cond);
115#endif
116}
117
118
ager@chromium.org236ad962008-09-25 09:45:57 +0000119void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
120 Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 // Set lr for return at current pc + 8.
122 mov(lr, Operand(pc), LeaveCC, cond);
123 // Emit a ldr<cond> pc, [pc + offset of target in constant pool].
124 mov(pc, Operand(target, rmode), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 // If USE_BLX is defined, we could emit a 'mov ip, target', followed by a
126 // 'blx ip'; however, the code would not be shorter than the above sequence
127 // and the target address of the call would be referenced by the first
128 // instruction rather than the second one, which would make it harder to patch
129 // (two instructions before the return address, instead of one).
ager@chromium.org4af710e2009-09-15 12:20:11 +0000130 ASSERT(kCallTargetAddressOffset == kInstrSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131}
132
133
ager@chromium.org236ad962008-09-25 09:45:57 +0000134void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
135 Condition cond) {
136 ASSERT(!RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
138}
139
140
ager@chromium.org236ad962008-09-25 09:45:57 +0000141void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
142 Condition cond) {
143 ASSERT(RelocInfo::IsCodeTarget(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 // 'code' is always generated ARM code, never THUMB code
145 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
146}
147
148
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000149void MacroAssembler::Ret(Condition cond) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150#if USE_BX
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000151 bx(lr, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152#else
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000153 mov(pc, Operand(lr), LeaveCC, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154#endif
155}
156
157
ager@chromium.org8bb60582008-12-11 12:02:20 +0000158void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
159 // Empty the const pool.
160 CheckConstPool(true, true);
161 add(pc, pc, Operand(index,
162 LSL,
163 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000164 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000165 nop(); // Jump table alignment.
166 for (int i = 0; i < targets.length(); i++) {
167 b(targets[i]);
168 }
169}
170
171
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000172void MacroAssembler::LoadRoot(Register destination,
173 Heap::RootListIndex index,
174 Condition cond) {
175 ldr(destination, MemOperand(r10, index << kPointerSizeLog2), cond);
176}
177
178
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179// Will clobber 4 registers: object, offset, scratch, ip. The
180// register 'object' contains a heap object pointer. The heap object
181// tag is shifted away.
182void MacroAssembler::RecordWrite(Register object, Register offset,
183 Register scratch) {
184 // This is how much we shift the remembered set bit offset to get the
185 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
186 // shift right 5) and then multiply by kIntSize (4, shift left 2).
187 const int kRSetWordShift = 3;
188
189 Label fast, done;
190
kasper.lund7276f142008-07-30 08:49:36 +0000191 // First, test that the object is not in the new space. We cannot set
192 // remembered set bits in the new space.
193 // object: heap object pointer (with tag)
194 // offset: offset to store location from the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 and_(scratch, object, Operand(Heap::NewSpaceMask()));
196 cmp(scratch, Operand(ExternalReference::new_space_start()));
197 b(eq, &done);
198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 // Compute the bit offset in the remembered set.
kasper.lund7276f142008-07-30 08:49:36 +0000200 // object: heap object pointer (with tag)
201 // offset: offset to store location from the object
202 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
203 and_(scratch, object, Operand(ip)); // offset into page of the object
204 add(offset, scratch, Operand(offset)); // add offset into the object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
206
207 // Compute the page address from the heap object pointer.
kasper.lund7276f142008-07-30 08:49:36 +0000208 // object: heap object pointer (with tag)
209 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 bic(object, object, Operand(ip));
211
212 // If the bit offset lies beyond the normal remembered set range, it is in
213 // the extra remembered set area of a large object.
kasper.lund7276f142008-07-30 08:49:36 +0000214 // object: page start
215 // offset: bit offset of store position in the remembered set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 cmp(offset, Operand(Page::kPageSize / kPointerSize));
217 b(lt, &fast);
218
219 // Adjust the bit offset to be relative to the start of the extra
220 // remembered set and the start address to be the address of the extra
221 // remembered set.
222 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
223 // Load the array length into 'scratch' and multiply by four to get the
224 // size in bytes of the elements.
225 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
226 + FixedArray::kLengthOffset));
227 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
228 // Add the page header (including remembered set), array header, and array
229 // body size to the page address.
230 add(object, object, Operand(Page::kObjectStartOffset
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000231 + FixedArray::kHeaderSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232 add(object, object, Operand(scratch));
233
234 bind(&fast);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 // Get address of the rset word.
kasper.lund7276f142008-07-30 08:49:36 +0000236 // object: start of the remembered set (page start for the fast case)
237 // offset: bit offset of store position in the remembered set
238 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
239 add(object, object, Operand(scratch, LSR, kRSetWordShift));
240 // Get bit offset in the rset word.
241 // object: address of remembered set word
242 // offset: bit offset of store position
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 and_(offset, offset, Operand(kBitsPerInt - 1));
244
245 ldr(scratch, MemOperand(object));
246 mov(ip, Operand(1));
247 orr(scratch, scratch, Operand(ip, LSL, offset));
248 str(scratch, MemOperand(object));
249
250 bind(&done);
251}
252
253
ager@chromium.org7c537e22008-10-16 08:43:32 +0000254void MacroAssembler::EnterFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000255 // r0-r3: preserved
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000256 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
257 mov(ip, Operand(Smi::FromInt(type)));
258 push(ip);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000259 mov(ip, Operand(CodeObject()));
260 push(ip);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000261 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262}
263
264
ager@chromium.org7c537e22008-10-16 08:43:32 +0000265void MacroAssembler::LeaveFrame(StackFrame::Type type) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000266 // r0: preserved
267 // r1: preserved
268 // r2: preserved
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
ager@chromium.org7c537e22008-10-16 08:43:32 +0000270 // Drop the execution stack down to the frame pointer and restore
271 // the caller frame pointer and return address.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000272 mov(sp, fp);
273 ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274}
275
276
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000277void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000278 // Compute the argv pointer and keep it in a callee-saved register.
279 // r0 is argc.
280 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
281 sub(r6, r6, Operand(kPointerSize));
282
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000283 // Compute callee's stack pointer before making changes and save it as
284 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000285 // popping the args.
286
287 // ip = sp + kPointerSize * #args;
288 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
289
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000290 // Align the stack at this point. After this point we have 5 pushes,
291 // so in fact we have to unalign here! See also the assert on the
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000292 // alignment in AlignStack.
293 AlignStack(1);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000294
ager@chromium.org236ad962008-09-25 09:45:57 +0000295 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
296 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
297 mov(fp, Operand(sp)); // setup new frame pointer
298
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000299 if (mode == ExitFrame::MODE_DEBUG) {
300 mov(ip, Operand(Smi::FromInt(0)));
301 } else {
302 mov(ip, Operand(CodeObject()));
303 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000304 push(ip);
305
306 // Save the frame pointer and the context in top.
307 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
308 str(fp, MemOperand(ip));
309 mov(ip, Operand(ExternalReference(Top::k_context_address)));
310 str(cp, MemOperand(ip));
311
312 // Setup argc and the builtin function in callee-saved registers.
313 mov(r4, Operand(r0));
314 mov(r5, Operand(r1));
315
ager@chromium.org236ad962008-09-25 09:45:57 +0000316
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000317#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000318 // Save the state of all registers to the stack from the memory
319 // location. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000320 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000321 // Use sp as base to push.
322 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
323 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000324#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000325}
326
327
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000328void MacroAssembler::AlignStack(int offset) {
329#if defined(V8_HOST_ARCH_ARM)
330 // Running on the real platform. Use the alignment as mandated by the local
331 // environment.
332 // Note: This will break if we ever start generating snapshots on one ARM
333 // platform for another ARM platform with a different alignment.
334 int activation_frame_alignment = OS::ActivationFrameAlignment();
335#else // defined(V8_HOST_ARCH_ARM)
336 // If we are using the simulator then we should always align to the expected
337 // alignment. As the simulator is used to generate snapshots we do not know
338 // if the target platform will need alignment, so we will always align at
339 // this point here.
340 int activation_frame_alignment = 2 * kPointerSize;
341#endif // defined(V8_HOST_ARCH_ARM)
342 if (activation_frame_alignment != kPointerSize) {
343 // This code needs to be made more general if this assert doesn't hold.
344 ASSERT(activation_frame_alignment == 2 * kPointerSize);
345 mov(r7, Operand(Smi::FromInt(0)));
346 tst(sp, Operand(activation_frame_alignment - offset));
347 push(r7, eq); // Conditional push instruction.
348 }
349}
350
351
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000352void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000353#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000354 // Restore the memory copy of the registers by digging them out from
355 // the stack. This is needed to allow nested break points.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000356 if (mode == ExitFrame::MODE_DEBUG) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000357 // This code intentionally clobbers r2 and r3.
358 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000359 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
ager@chromium.org236ad962008-09-25 09:45:57 +0000360 add(r3, fp, Operand(kOffset));
361 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
362 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000363#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000364
365 // Clear top frame.
366 mov(r3, Operand(0));
367 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
368 str(r3, MemOperand(ip));
369
370 // Restore current context from top and clear it in debug mode.
371 mov(ip, Operand(ExternalReference(Top::k_context_address)));
372 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000373#ifdef DEBUG
374 str(r3, MemOperand(ip));
375#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000376
377 // Pop the arguments, restore registers, and return.
378 mov(sp, Operand(fp)); // respect ABI stack constraint
379 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
380}
381
382
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000383void MacroAssembler::InvokePrologue(const ParameterCount& expected,
384 const ParameterCount& actual,
385 Handle<Code> code_constant,
386 Register code_reg,
387 Label* done,
388 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000389 bool definitely_matches = false;
390 Label regular_invoke;
391
392 // Check whether the expected and actual arguments count match. If not,
393 // setup registers according to contract with ArgumentsAdaptorTrampoline:
394 // r0: actual arguments count
395 // r1: function (passed through to callee)
396 // r2: expected arguments count
397 // r3: callee code entry
398
399 // The code below is made a lot easier because the calling code already sets
400 // up actual and expected registers according to the contract if values are
401 // passed in registers.
402 ASSERT(actual.is_immediate() || actual.reg().is(r0));
403 ASSERT(expected.is_immediate() || expected.reg().is(r2));
404 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
405
406 if (expected.is_immediate()) {
407 ASSERT(actual.is_immediate());
408 if (expected.immediate() == actual.immediate()) {
409 definitely_matches = true;
410 } else {
411 mov(r0, Operand(actual.immediate()));
412 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
413 if (expected.immediate() == sentinel) {
414 // Don't worry about adapting arguments for builtins that
415 // don't want that done. Skip adaption code by making it look
416 // like we have a match between expected and actual number of
417 // arguments.
418 definitely_matches = true;
419 } else {
420 mov(r2, Operand(expected.immediate()));
421 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000422 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000423 } else {
424 if (actual.is_immediate()) {
425 cmp(expected.reg(), Operand(actual.immediate()));
426 b(eq, &regular_invoke);
427 mov(r0, Operand(actual.immediate()));
428 } else {
429 cmp(expected.reg(), Operand(actual.reg()));
430 b(eq, &regular_invoke);
431 }
432 }
433
434 if (!definitely_matches) {
435 if (!code_constant.is_null()) {
436 mov(r3, Operand(code_constant));
437 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
438 }
439
440 Handle<Code> adaptor =
441 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
442 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000443 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000444 b(done);
445 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000446 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000447 }
448 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000449 }
450}
451
452
453void MacroAssembler::InvokeCode(Register code,
454 const ParameterCount& expected,
455 const ParameterCount& actual,
456 InvokeFlag flag) {
457 Label done;
458
459 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
460 if (flag == CALL_FUNCTION) {
461 Call(code);
462 } else {
463 ASSERT(flag == JUMP_FUNCTION);
464 Jump(code);
465 }
466
467 // Continue here if InvokePrologue does handle the invocation due to
468 // mismatched parameter counts.
469 bind(&done);
470}
471
472
473void MacroAssembler::InvokeCode(Handle<Code> code,
474 const ParameterCount& expected,
475 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000476 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000477 InvokeFlag flag) {
478 Label done;
479
480 InvokePrologue(expected, actual, code, no_reg, &done, flag);
481 if (flag == CALL_FUNCTION) {
482 Call(code, rmode);
483 } else {
484 Jump(code, rmode);
485 }
486
487 // Continue here if InvokePrologue does handle the invocation due to
488 // mismatched parameter counts.
489 bind(&done);
490}
491
492
493void MacroAssembler::InvokeFunction(Register fun,
494 const ParameterCount& actual,
495 InvokeFlag flag) {
496 // Contract with called JS functions requires that function is passed in r1.
497 ASSERT(fun.is(r1));
498
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000499 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000500 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000501
502 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
503 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
504 ldr(expected_reg,
505 FieldMemOperand(code_reg,
506 SharedFunctionInfo::kFormalParameterCountOffset));
507 ldr(code_reg,
508 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
509 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
510
511 ParameterCount expected(expected_reg);
512 InvokeCode(code_reg, expected, actual, flag);
513}
514
515
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000516#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517void MacroAssembler::SaveRegistersToMemory(RegList regs) {
518 ASSERT((regs & ~kJSCallerSaved) == 0);
519 // Copy the content of registers to memory location.
520 for (int i = 0; i < kNumJSCallerSaved; i++) {
521 int r = JSCallerSavedCode(i);
522 if ((regs & (1 << r)) != 0) {
523 Register reg = { r };
524 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
525 str(reg, MemOperand(ip));
526 }
527 }
528}
529
530
531void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
532 ASSERT((regs & ~kJSCallerSaved) == 0);
533 // Copy the content of memory location to registers.
534 for (int i = kNumJSCallerSaved; --i >= 0;) {
535 int r = JSCallerSavedCode(i);
536 if ((regs & (1 << r)) != 0) {
537 Register reg = { r };
538 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
539 ldr(reg, MemOperand(ip));
540 }
541 }
542}
543
544
545void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
546 RegList regs) {
547 ASSERT((regs & ~kJSCallerSaved) == 0);
548 // Copy the content of the memory location to the stack and adjust base.
549 for (int i = kNumJSCallerSaved; --i >= 0;) {
550 int r = JSCallerSavedCode(i);
551 if ((regs & (1 << r)) != 0) {
552 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
553 ldr(ip, MemOperand(ip));
554 str(ip, MemOperand(base, 4, NegPreIndex));
555 }
556 }
557}
558
559
560void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
561 Register scratch,
562 RegList regs) {
563 ASSERT((regs & ~kJSCallerSaved) == 0);
564 // Copy the content of the stack to the memory location and adjust base.
565 for (int i = 0; i < kNumJSCallerSaved; i++) {
566 int r = JSCallerSavedCode(i);
567 if ((regs & (1 << r)) != 0) {
568 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
569 ldr(scratch, MemOperand(base, 4, PostIndex));
570 str(scratch, MemOperand(ip));
571 }
572 }
573}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000574#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577void MacroAssembler::PushTryHandler(CodeLocation try_location,
578 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000579 // Adjust this code if not the case.
580 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 // The pc (return address) is passed in register lr.
582 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 if (type == TRY_CATCH_HANDLER) {
584 mov(r3, Operand(StackHandler::TRY_CATCH));
585 } else {
586 mov(r3, Operand(StackHandler::TRY_FINALLY));
587 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000588 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
589 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
590 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
591 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
592 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
594 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000595 ASSERT(StackHandlerConstants::kNextOffset == 0);
596 push(r1);
597 // Link this handler as the new current one.
598 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000600 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000602 // The frame pointer does not point to a JS frame so we save NULL
603 // for fp. We expect the code throwing an exception to check fp
604 // before dereferencing it to restore the context.
605 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000607 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
608 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
609 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
610 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
611 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
613 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000614 ASSERT(StackHandlerConstants::kNextOffset == 0);
615 push(r6);
616 // Link this handler as the new current one.
617 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 }
619}
620
621
622Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
623 JSObject* holder, Register holder_reg,
624 Register scratch,
625 Label* miss) {
626 // Make sure there's no overlap between scratch and the other
627 // registers.
628 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
629
630 // Keep track of the current object in register reg.
631 Register reg = object_reg;
632 int depth = 1;
633
634 // Check the maps in the prototype chain.
635 // Traverse the prototype chain from the object and do map checks.
636 while (object != holder) {
637 depth++;
638
639 // Only global objects and objects that do not require access
640 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000641 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642
643 // Get the map of the current object.
644 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
645 cmp(scratch, Operand(Handle<Map>(object->map())));
646
647 // Branch on the result of the map check.
648 b(ne, miss);
649
650 // Check access rights to the global object. This has to happen
651 // after the map check so that we know that the object is
652 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000653 if (object->IsJSGlobalProxy()) {
654 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 // Restore scratch register to be the map of the object. In the
656 // new space case below, we load the prototype from the map in
657 // the scratch register.
658 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
659 }
660
661 reg = holder_reg; // from now the object is in holder_reg
662 JSObject* prototype = JSObject::cast(object->GetPrototype());
663 if (Heap::InNewSpace(prototype)) {
664 // The prototype is in new space; we cannot store a reference
665 // to it in the code. Load it from the map.
666 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
667 } else {
668 // The prototype is in old space; load it directly.
669 mov(reg, Operand(Handle<JSObject>(prototype)));
670 }
671
672 // Go to the next object in the prototype chain.
673 object = prototype;
674 }
675
676 // Check the holder map.
677 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
678 cmp(scratch, Operand(Handle<Map>(object->map())));
679 b(ne, miss);
680
681 // Log the check depth.
682 LOG(IntEvent("check-maps-depth", depth));
683
684 // Perform security check for access to the global object and return
685 // the holder register.
686 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000687 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
688 if (object->IsJSGlobalProxy()) {
689 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 }
691 return reg;
692}
693
694
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000695void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
696 Register scratch,
697 Label* miss) {
698 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000700 ASSERT(!holder_reg.is(scratch));
701 ASSERT(!holder_reg.is(ip));
702 ASSERT(!scratch.is(ip));
703
704 // Load current lexical context from the stack frame.
705 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
706 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000707#ifdef DEBUG
708 cmp(scratch, Operand(0));
709 Check(ne, "we should not have an empty lexical context");
710#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000712 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
714 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000715 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
716
717 // Check the context is a global context.
718 if (FLAG_debug_code) {
719 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
720 // Cannot use ip as a temporary in this verification code. Due to the fact
721 // that ip is clobbered as part of cmp with an object Operand.
722 push(holder_reg); // Temporarily save holder on the stack.
723 // Read the first word and compare to the global_context_map.
724 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000725 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
726 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000727 Check(eq, "JSGlobalObject::global_context should be a global context.");
728 pop(holder_reg); // Restore holder.
729 }
730
731 // Check if both contexts are the same.
732 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
733 cmp(scratch, Operand(ip));
734 b(eq, &same_contexts);
735
736 // Check the context is a global context.
737 if (FLAG_debug_code) {
738 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
739 // Cannot use ip as a temporary in this verification code. Due to the fact
740 // that ip is clobbered as part of cmp with an object Operand.
741 push(holder_reg); // Temporarily save holder on the stack.
742 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000743 LoadRoot(ip, Heap::kNullValueRootIndex);
744 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000745 Check(ne, "JSGlobalProxy::context() should not be null.");
746
747 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000748 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
749 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000750 Check(eq, "JSGlobalObject::global_context should be a global context.");
751 // Restore ip is not needed. ip is reloaded below.
752 pop(holder_reg); // Restore holder.
753 // Restore ip to holder's context.
754 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
755 }
756
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 // Check that the security token in the calling global object is
758 // compatible with the security token in the receiving global
759 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000760 int token_offset = Context::kHeaderSize +
761 Context::SECURITY_TOKEN_INDEX * kPointerSize;
762
763 ldr(scratch, FieldMemOperand(scratch, token_offset));
764 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 cmp(scratch, Operand(ip));
766 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000767
768 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769}
770
771
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000772void MacroAssembler::AllocateInNewSpace(int object_size,
773 Register result,
774 Register scratch1,
775 Register scratch2,
776 Label* gc_required,
777 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000778 ASSERT(!result.is(scratch1));
779 ASSERT(!scratch1.is(scratch2));
780
781 // Load address of new object into result and allocation top address into
782 // scratch1.
783 ExternalReference new_space_allocation_top =
784 ExternalReference::new_space_allocation_top_address();
785 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000786 if ((flags & RESULT_CONTAINS_TOP) == 0) {
787 ldr(result, MemOperand(scratch1));
788 } else {
789#ifdef DEBUG
790 // Assert that result actually contains top on entry. scratch2 is used
791 // immediately below so this use of scratch2 does not cause difference with
792 // respect to register content between debug and release mode.
793 ldr(scratch2, MemOperand(scratch1));
794 cmp(result, scratch2);
795 Check(eq, "Unexpected allocation top");
796#endif
797 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000798
799 // Calculate new top and bail out if new space is exhausted. Use result
800 // to calculate the new top.
801 ExternalReference new_space_allocation_limit =
802 ExternalReference::new_space_allocation_limit_address();
803 mov(scratch2, Operand(new_space_allocation_limit));
804 ldr(scratch2, MemOperand(scratch2));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000805 add(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000806 cmp(result, Operand(scratch2));
807 b(hi, gc_required);
808
809 // Update allocation top. result temporarily holds the new top,
810 str(result, MemOperand(scratch1));
811
812 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000813 if ((flags & TAG_OBJECT) != 0) {
814 sub(result, result, Operand((object_size * kPointerSize) -
815 kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000816 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000817 sub(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000818 }
819}
820
821
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000822void MacroAssembler::AllocateInNewSpace(Register object_size,
823 Register result,
824 Register scratch1,
825 Register scratch2,
826 Label* gc_required,
827 AllocationFlags flags) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000828 ASSERT(!result.is(scratch1));
829 ASSERT(!scratch1.is(scratch2));
830
831 // Load address of new object into result and allocation top address into
832 // scratch1.
833 ExternalReference new_space_allocation_top =
834 ExternalReference::new_space_allocation_top_address();
835 mov(scratch1, Operand(new_space_allocation_top));
836 if ((flags & RESULT_CONTAINS_TOP) == 0) {
837 ldr(result, MemOperand(scratch1));
838 } else {
839#ifdef DEBUG
840 // Assert that result actually contains top on entry. scratch2 is used
841 // immediately below so this use of scratch2 does not cause difference with
842 // respect to register content between debug and release mode.
843 ldr(scratch2, MemOperand(scratch1));
844 cmp(result, scratch2);
845 Check(eq, "Unexpected allocation top");
846#endif
847 }
848
849 // Calculate new top and bail out if new space is exhausted. Use result
850 // to calculate the new top. Object size is in words so a shift is required to
851 // get the number of bytes
852 ExternalReference new_space_allocation_limit =
853 ExternalReference::new_space_allocation_limit_address();
854 mov(scratch2, Operand(new_space_allocation_limit));
855 ldr(scratch2, MemOperand(scratch2));
856 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
857 cmp(result, Operand(scratch2));
858 b(hi, gc_required);
859
860 // Update allocation top. result temporarily holds the new top,
861 str(result, MemOperand(scratch1));
862
863 // Adjust back to start of new object.
864 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
865
866 // Tag object if requested.
867 if ((flags & TAG_OBJECT) != 0) {
868 add(result, result, Operand(kHeapObjectTag));
869 }
870}
871
872
873void MacroAssembler::UndoAllocationInNewSpace(Register object,
874 Register scratch) {
875 ExternalReference new_space_allocation_top =
876 ExternalReference::new_space_allocation_top_address();
877
878 // Make sure the object has no tag before resetting top.
879 and_(object, object, Operand(~kHeapObjectTagMask));
880#ifdef DEBUG
881 // Check that the object un-allocated is below the current top.
882 mov(scratch, Operand(new_space_allocation_top));
883 ldr(scratch, MemOperand(scratch));
884 cmp(object, scratch);
885 Check(lt, "Undo allocation of non allocated memory");
886#endif
887 // Write the address of the object to un-allocate as the current top.
888 mov(scratch, Operand(new_space_allocation_top));
889 str(object, MemOperand(scratch));
890}
891
892
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000893void MacroAssembler::CompareObjectType(Register function,
894 Register map,
895 Register type_reg,
896 InstanceType type) {
897 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000898 CompareInstanceType(map, type_reg, type);
899}
900
901
902void MacroAssembler::CompareInstanceType(Register map,
903 Register type_reg,
904 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000905 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
906 cmp(type_reg, Operand(type));
907}
908
909
910void MacroAssembler::TryGetFunctionPrototype(Register function,
911 Register result,
912 Register scratch,
913 Label* miss) {
914 // Check that the receiver isn't a smi.
915 BranchOnSmi(function, miss);
916
917 // Check that the function really is a function. Load map into result reg.
918 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
919 b(ne, miss);
920
921 // Make sure that the function has an instance prototype.
922 Label non_instance;
923 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
924 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
925 b(ne, &non_instance);
926
927 // Get the prototype or initial map from the function.
928 ldr(result,
929 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
930
931 // If the prototype or initial map is the hole, don't return it and
932 // simply miss the cache instead. This will allow us to allocate a
933 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000934 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
935 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000936 b(eq, miss);
937
938 // If the function does not have an initial map, we're done.
939 Label done;
940 CompareObjectType(result, scratch, scratch, MAP_TYPE);
941 b(ne, &done);
942
943 // Get the prototype from the initial map.
944 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
945 jmp(&done);
946
947 // Non-instance prototype: Fetch prototype from constructor field
948 // in initial map.
949 bind(&non_instance);
950 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
951
952 // All done.
953 bind(&done);
954}
955
956
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000957void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +0000958 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000959 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960}
961
962
963void MacroAssembler::StubReturn(int argc) {
964 ASSERT(argc >= 1 && generating_stub());
965 if (argc > 1)
966 add(sp, sp, Operand((argc - 1) * kPointerSize));
967 Ret();
968}
969
mads.s.ager31e71382008-08-13 09:32:07 +0000970
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000971void MacroAssembler::IllegalOperation(int num_arguments) {
972 if (num_arguments > 0) {
973 add(sp, sp, Operand(num_arguments * kPointerSize));
974 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000975 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000976}
977
978
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000979void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
980 Register outHighReg,
981 Register outLowReg) {
982 // ARMv7 VFP3 instructions to implement integer to double conversion.
983 mov(r7, Operand(inReg, ASR, kSmiTagSize));
984 fmsr(s15, r7);
985 fsitod(d7, s15);
986 fmrrd(outLowReg, outHighReg, d7);
987}
988
989
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000991 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000993 // If the expected number of arguments of the runtime function is
994 // constant, we check that the actual number of arguments match the
995 // expectation.
996 if (f->nargs >= 0 && f->nargs != num_arguments) {
997 IllegalOperation(num_arguments);
998 return;
999 }
kasper.lund7276f142008-07-30 08:49:36 +00001000
mads.s.ager31e71382008-08-13 09:32:07 +00001001 Runtime::FunctionId function_id =
1002 static_cast<Runtime::FunctionId>(f->stub_id);
1003 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 CallStub(&stub);
1005}
1006
1007
1008void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1009 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1010}
1011
1012
mads.s.ager31e71382008-08-13 09:32:07 +00001013void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001014 int num_arguments,
1015 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +00001016 // TODO(1236192): Most runtime routines don't need the number of
1017 // arguments passed in because it is constant. At some point we
1018 // should remove this need and make the runtime routine entry code
1019 // smarter.
1020 mov(r0, Operand(num_arguments));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001021 JumpToRuntime(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022}
1023
1024
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001025void MacroAssembler::JumpToRuntime(const ExternalReference& builtin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026#if defined(__thumb__)
1027 // Thumb mode builtin.
1028 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1029#endif
1030 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001031 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001032 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033}
1034
1035
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001036Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1037 bool* resolved) {
1038 // Contract with compiled functions is that the function is passed in r1.
1039 int builtins_offset =
1040 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1041 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1042 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
1043 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001045 return Builtins::GetCode(id, resolved);
1046}
1047
1048
1049void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1050 InvokeJSFlags flags) {
1051 bool resolved;
1052 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
1054 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001055 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 } else {
1057 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +00001058 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 }
1060
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001061 if (!resolved) {
1062 const char* name = Builtins::GetName(id);
1063 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 uint32_t flags =
1065 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001066 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
ager@chromium.org4af710e2009-09-15 12:20:11 +00001067 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001068 unresolved_.Add(entry);
1069 }
1070}
1071
1072
1073void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1074 bool resolved;
1075 Handle<Code> code = ResolveBuiltin(id, &resolved);
1076
1077 mov(target, Operand(code));
1078 if (!resolved) {
1079 const char* name = Builtins::GetName(id);
1080 int argc = Builtins::GetArgumentsCount(id);
1081 uint32_t flags =
1082 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001083 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
ager@chromium.org4af710e2009-09-15 12:20:11 +00001084 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 unresolved_.Add(entry);
1086 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001087
1088 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089}
1090
1091
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001092void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1093 Register scratch1, Register scratch2) {
1094 if (FLAG_native_code_counters && counter->Enabled()) {
1095 mov(scratch1, Operand(value));
1096 mov(scratch2, Operand(ExternalReference(counter)));
1097 str(scratch1, MemOperand(scratch2));
1098 }
1099}
1100
1101
1102void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1103 Register scratch1, Register scratch2) {
1104 ASSERT(value > 0);
1105 if (FLAG_native_code_counters && counter->Enabled()) {
1106 mov(scratch2, Operand(ExternalReference(counter)));
1107 ldr(scratch1, MemOperand(scratch2));
1108 add(scratch1, scratch1, Operand(value));
1109 str(scratch1, MemOperand(scratch2));
1110 }
1111}
1112
1113
1114void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1115 Register scratch1, Register scratch2) {
1116 ASSERT(value > 0);
1117 if (FLAG_native_code_counters && counter->Enabled()) {
1118 mov(scratch2, Operand(ExternalReference(counter)));
1119 ldr(scratch1, MemOperand(scratch2));
1120 sub(scratch1, scratch1, Operand(value));
1121 str(scratch1, MemOperand(scratch2));
1122 }
1123}
1124
1125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126void MacroAssembler::Assert(Condition cc, const char* msg) {
1127 if (FLAG_debug_code)
1128 Check(cc, msg);
1129}
1130
1131
1132void MacroAssembler::Check(Condition cc, const char* msg) {
1133 Label L;
1134 b(cc, &L);
1135 Abort(msg);
1136 // will not return here
1137 bind(&L);
1138}
1139
1140
1141void MacroAssembler::Abort(const char* msg) {
1142 // We want to pass the msg string like a smi to avoid GC
1143 // problems, however msg is not guaranteed to be aligned
1144 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001145 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 // from the real pointer as a smi.
1147 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1148 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1149 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1150#ifdef DEBUG
1151 if (msg != NULL) {
1152 RecordComment("Abort message: ");
1153 RecordComment(msg);
1154 }
1155#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 mov(r0, Operand(p0));
1157 push(r0);
1158 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001159 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 CallRuntime(Runtime::kAbort, 2);
1161 // will not return here
1162}
1163
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001164
ager@chromium.org4af710e2009-09-15 12:20:11 +00001165#ifdef ENABLE_DEBUGGER_SUPPORT
1166CodePatcher::CodePatcher(byte* address, int instructions)
1167 : address_(address),
1168 instructions_(instructions),
1169 size_(instructions * Assembler::kInstrSize),
1170 masm_(address, size_ + Assembler::kGap) {
1171 // Create a new macro assembler pointing to the address of the code to patch.
1172 // The size is adjusted with kGap on order for the assembler to generate size
1173 // bytes of instructions without failing with buffer size constraints.
1174 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1175}
1176
1177
1178CodePatcher::~CodePatcher() {
1179 // Indicate that code has changed.
1180 CPU::FlushICache(address_, size_);
1181
1182 // Check that the code was patched as expected.
1183 ASSERT(masm_.pc_ == address_ + size_);
1184 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1185}
1186
1187
1188void CodePatcher::Emit(Instr x) {
1189 masm()->emit(x);
1190}
1191
1192
1193void CodePatcher::Emit(Address addr) {
1194 masm()->emit(reinterpret_cast<Instr>(addr));
1195}
1196#endif // ENABLE_DEBUGGER_SUPPORT
1197
1198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199} } // namespace v8::internal