blob: 6dd9b8faab71ffae673bd2e06b425dc66afc726a [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.org236ad962008-09-25 09:45:57 +0000277void MacroAssembler::EnterExitFrame(StackFrame::Type type) {
278 ASSERT(type == StackFrame::EXIT || type == StackFrame::EXIT_DEBUG);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000279
280 // Compute the argv pointer and keep it in a callee-saved register.
281 // r0 is argc.
282 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
283 sub(r6, r6, Operand(kPointerSize));
284
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000285 // Compute callee's stack pointer before making changes and save it as
286 // ip register so that it is restored as sp register on exit, thereby
ager@chromium.org236ad962008-09-25 09:45:57 +0000287 // popping the args.
288
289 // ip = sp + kPointerSize * #args;
290 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
291
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000292 // Align the stack at this point. After this point we have 5 pushes,
293 // so in fact we have to unalign here! See also the assert on the
294 // alignment immediately below.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000295#if defined(V8_HOST_ARCH_ARM)
296 // Running on the real platform. Use the alignment as mandated by the local
297 // environment.
298 // Note: This will break if we ever start generating snapshots on one ARM
299 // platform for another ARM platform with a different alignment.
300 int activation_frame_alignment = OS::ActivationFrameAlignment();
301#else // defined(V8_HOST_ARCH_ARM)
302 // If we are using the simulator then we should always align to the expected
303 // alignment. As the simulator is used to generate snapshots we do not know
304 // if the target platform will need alignment, so we will always align at
305 // this point here.
306 int activation_frame_alignment = 2 * kPointerSize;
307#endif // defined(V8_HOST_ARCH_ARM)
308 if (activation_frame_alignment != kPointerSize) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000309 // This code needs to be made more general if this assert doesn't hold.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000310 ASSERT(activation_frame_alignment == 2 * kPointerSize);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000311 mov(r7, Operand(Smi::FromInt(0)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000312 tst(sp, Operand(activation_frame_alignment - 1));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000313 push(r7, eq); // Conditional push instruction.
314 }
315
ager@chromium.org236ad962008-09-25 09:45:57 +0000316 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
317 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
318 mov(fp, Operand(sp)); // setup new frame pointer
319
320 // Push debug marker.
321 mov(ip, Operand(type == StackFrame::EXIT_DEBUG ? 1 : 0));
322 push(ip);
323
324 // Save the frame pointer and the context in top.
325 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
326 str(fp, MemOperand(ip));
327 mov(ip, Operand(ExternalReference(Top::k_context_address)));
328 str(cp, MemOperand(ip));
329
330 // Setup argc and the builtin function in callee-saved registers.
331 mov(r4, Operand(r0));
332 mov(r5, Operand(r1));
333
ager@chromium.org236ad962008-09-25 09:45:57 +0000334
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000335#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000336 // Save the state of all registers to the stack from the memory
337 // location. This is needed to allow nested break points.
338 if (type == StackFrame::EXIT_DEBUG) {
339 // Use sp as base to push.
340 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
341 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000342#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000343}
344
345
346void MacroAssembler::LeaveExitFrame(StackFrame::Type type) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000347#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org236ad962008-09-25 09:45:57 +0000348 // Restore the memory copy of the registers by digging them out from
349 // the stack. This is needed to allow nested break points.
350 if (type == StackFrame::EXIT_DEBUG) {
351 // This code intentionally clobbers r2 and r3.
352 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
353 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
354 add(r3, fp, Operand(kOffset));
355 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
356 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000357#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000358
359 // Clear top frame.
360 mov(r3, Operand(0));
361 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
362 str(r3, MemOperand(ip));
363
364 // Restore current context from top and clear it in debug mode.
365 mov(ip, Operand(ExternalReference(Top::k_context_address)));
366 ldr(cp, MemOperand(ip));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000367#ifdef DEBUG
368 str(r3, MemOperand(ip));
369#endif
ager@chromium.org236ad962008-09-25 09:45:57 +0000370
371 // Pop the arguments, restore registers, and return.
372 mov(sp, Operand(fp)); // respect ABI stack constraint
373 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
374}
375
376
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000377void MacroAssembler::InvokePrologue(const ParameterCount& expected,
378 const ParameterCount& actual,
379 Handle<Code> code_constant,
380 Register code_reg,
381 Label* done,
382 InvokeFlag flag) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000383 bool definitely_matches = false;
384 Label regular_invoke;
385
386 // Check whether the expected and actual arguments count match. If not,
387 // setup registers according to contract with ArgumentsAdaptorTrampoline:
388 // r0: actual arguments count
389 // r1: function (passed through to callee)
390 // r2: expected arguments count
391 // r3: callee code entry
392
393 // The code below is made a lot easier because the calling code already sets
394 // up actual and expected registers according to the contract if values are
395 // passed in registers.
396 ASSERT(actual.is_immediate() || actual.reg().is(r0));
397 ASSERT(expected.is_immediate() || expected.reg().is(r2));
398 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
399
400 if (expected.is_immediate()) {
401 ASSERT(actual.is_immediate());
402 if (expected.immediate() == actual.immediate()) {
403 definitely_matches = true;
404 } else {
405 mov(r0, Operand(actual.immediate()));
406 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
407 if (expected.immediate() == sentinel) {
408 // Don't worry about adapting arguments for builtins that
409 // don't want that done. Skip adaption code by making it look
410 // like we have a match between expected and actual number of
411 // arguments.
412 definitely_matches = true;
413 } else {
414 mov(r2, Operand(expected.immediate()));
415 }
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000416 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000417 } else {
418 if (actual.is_immediate()) {
419 cmp(expected.reg(), Operand(actual.immediate()));
420 b(eq, &regular_invoke);
421 mov(r0, Operand(actual.immediate()));
422 } else {
423 cmp(expected.reg(), Operand(actual.reg()));
424 b(eq, &regular_invoke);
425 }
426 }
427
428 if (!definitely_matches) {
429 if (!code_constant.is_null()) {
430 mov(r3, Operand(code_constant));
431 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
432 }
433
434 Handle<Code> adaptor =
435 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
436 if (flag == CALL_FUNCTION) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000437 Call(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000438 b(done);
439 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000440 Jump(adaptor, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000441 }
442 bind(&regular_invoke);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000443 }
444}
445
446
447void MacroAssembler::InvokeCode(Register code,
448 const ParameterCount& expected,
449 const ParameterCount& actual,
450 InvokeFlag flag) {
451 Label done;
452
453 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
454 if (flag == CALL_FUNCTION) {
455 Call(code);
456 } else {
457 ASSERT(flag == JUMP_FUNCTION);
458 Jump(code);
459 }
460
461 // Continue here if InvokePrologue does handle the invocation due to
462 // mismatched parameter counts.
463 bind(&done);
464}
465
466
467void MacroAssembler::InvokeCode(Handle<Code> code,
468 const ParameterCount& expected,
469 const ParameterCount& actual,
ager@chromium.org236ad962008-09-25 09:45:57 +0000470 RelocInfo::Mode rmode,
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000471 InvokeFlag flag) {
472 Label done;
473
474 InvokePrologue(expected, actual, code, no_reg, &done, flag);
475 if (flag == CALL_FUNCTION) {
476 Call(code, rmode);
477 } else {
478 Jump(code, rmode);
479 }
480
481 // Continue here if InvokePrologue does handle the invocation due to
482 // mismatched parameter counts.
483 bind(&done);
484}
485
486
487void MacroAssembler::InvokeFunction(Register fun,
488 const ParameterCount& actual,
489 InvokeFlag flag) {
490 // Contract with called JS functions requires that function is passed in r1.
491 ASSERT(fun.is(r1));
492
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000493 Register expected_reg = r2;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000494 Register code_reg = r3;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000495
496 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
497 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
498 ldr(expected_reg,
499 FieldMemOperand(code_reg,
500 SharedFunctionInfo::kFormalParameterCountOffset));
501 ldr(code_reg,
502 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
503 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
504
505 ParameterCount expected(expected_reg);
506 InvokeCode(code_reg, expected, actual, flag);
507}
508
509
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000510#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511void MacroAssembler::SaveRegistersToMemory(RegList regs) {
512 ASSERT((regs & ~kJSCallerSaved) == 0);
513 // Copy the content of registers to memory location.
514 for (int i = 0; i < kNumJSCallerSaved; i++) {
515 int r = JSCallerSavedCode(i);
516 if ((regs & (1 << r)) != 0) {
517 Register reg = { r };
518 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
519 str(reg, MemOperand(ip));
520 }
521 }
522}
523
524
525void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
526 ASSERT((regs & ~kJSCallerSaved) == 0);
527 // Copy the content of memory location to registers.
528 for (int i = kNumJSCallerSaved; --i >= 0;) {
529 int r = JSCallerSavedCode(i);
530 if ((regs & (1 << r)) != 0) {
531 Register reg = { r };
532 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
533 ldr(reg, MemOperand(ip));
534 }
535 }
536}
537
538
539void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
540 RegList regs) {
541 ASSERT((regs & ~kJSCallerSaved) == 0);
542 // Copy the content of the memory location to the stack and adjust base.
543 for (int i = kNumJSCallerSaved; --i >= 0;) {
544 int r = JSCallerSavedCode(i);
545 if ((regs & (1 << r)) != 0) {
546 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
547 ldr(ip, MemOperand(ip));
548 str(ip, MemOperand(base, 4, NegPreIndex));
549 }
550 }
551}
552
553
554void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
555 Register scratch,
556 RegList regs) {
557 ASSERT((regs & ~kJSCallerSaved) == 0);
558 // Copy the content of the stack to the memory location and adjust base.
559 for (int i = 0; i < kNumJSCallerSaved; i++) {
560 int r = JSCallerSavedCode(i);
561 if ((regs & (1 << r)) != 0) {
562 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
563 ldr(scratch, MemOperand(base, 4, PostIndex));
564 str(scratch, MemOperand(ip));
565 }
566 }
567}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000568#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571void MacroAssembler::PushTryHandler(CodeLocation try_location,
572 HandlerType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000573 // Adjust this code if not the case.
574 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 // The pc (return address) is passed in register lr.
576 if (try_location == IN_JAVASCRIPT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 if (type == TRY_CATCH_HANDLER) {
578 mov(r3, Operand(StackHandler::TRY_CATCH));
579 } else {
580 mov(r3, Operand(StackHandler::TRY_FINALLY));
581 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000582 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
583 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
584 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
585 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
586 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
588 ldr(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000589 ASSERT(StackHandlerConstants::kNextOffset == 0);
590 push(r1);
591 // Link this handler as the new current one.
592 str(sp, MemOperand(r3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000594 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 ASSERT(try_location == IN_JS_ENTRY);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000596 // The frame pointer does not point to a JS frame so we save NULL
597 // for fp. We expect the code throwing an exception to check fp
598 // before dereferencing it to restore the context.
599 mov(ip, Operand(0)); // To save a NULL frame pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 mov(r6, Operand(StackHandler::ENTRY));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000601 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
602 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
603 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
604 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
605 // Save the current handler as the next handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
607 ldr(r6, MemOperand(r7));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000608 ASSERT(StackHandlerConstants::kNextOffset == 0);
609 push(r6);
610 // Link this handler as the new current one.
611 str(sp, MemOperand(r7));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612 }
613}
614
615
616Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
617 JSObject* holder, Register holder_reg,
618 Register scratch,
619 Label* miss) {
620 // Make sure there's no overlap between scratch and the other
621 // registers.
622 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
623
624 // Keep track of the current object in register reg.
625 Register reg = object_reg;
626 int depth = 1;
627
628 // Check the maps in the prototype chain.
629 // Traverse the prototype chain from the object and do map checks.
630 while (object != holder) {
631 depth++;
632
633 // Only global objects and objects that do not require access
634 // checks are allowed in stubs.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000635 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636
637 // Get the map of the current object.
638 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
639 cmp(scratch, Operand(Handle<Map>(object->map())));
640
641 // Branch on the result of the map check.
642 b(ne, miss);
643
644 // Check access rights to the global object. This has to happen
645 // after the map check so that we know that the object is
646 // actually a global object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000647 if (object->IsJSGlobalProxy()) {
648 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 // Restore scratch register to be the map of the object. In the
650 // new space case below, we load the prototype from the map in
651 // the scratch register.
652 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
653 }
654
655 reg = holder_reg; // from now the object is in holder_reg
656 JSObject* prototype = JSObject::cast(object->GetPrototype());
657 if (Heap::InNewSpace(prototype)) {
658 // The prototype is in new space; we cannot store a reference
659 // to it in the code. Load it from the map.
660 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
661 } else {
662 // The prototype is in old space; load it directly.
663 mov(reg, Operand(Handle<JSObject>(prototype)));
664 }
665
666 // Go to the next object in the prototype chain.
667 object = prototype;
668 }
669
670 // Check the holder map.
671 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
672 cmp(scratch, Operand(Handle<Map>(object->map())));
673 b(ne, miss);
674
675 // Log the check depth.
676 LOG(IntEvent("check-maps-depth", depth));
677
678 // Perform security check for access to the global object and return
679 // the holder register.
680 ASSERT(object == holder);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000681 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
682 if (object->IsJSGlobalProxy()) {
683 CheckAccessGlobalProxy(reg, scratch, miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684 }
685 return reg;
686}
687
688
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000689void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
690 Register scratch,
691 Label* miss) {
692 Label same_contexts;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000694 ASSERT(!holder_reg.is(scratch));
695 ASSERT(!holder_reg.is(ip));
696 ASSERT(!scratch.is(ip));
697
698 // Load current lexical context from the stack frame.
699 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
700 // In debug mode, make sure the lexical context is set.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000701#ifdef DEBUG
702 cmp(scratch, Operand(0));
703 Check(ne, "we should not have an empty lexical context");
704#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000706 // Load the global context of the current context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
708 ldr(scratch, FieldMemOperand(scratch, offset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000709 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
710
711 // Check the context is a global context.
712 if (FLAG_debug_code) {
713 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
714 // Cannot use ip as a temporary in this verification code. Due to the fact
715 // that ip is clobbered as part of cmp with an object Operand.
716 push(holder_reg); // Temporarily save holder on the stack.
717 // Read the first word and compare to the global_context_map.
718 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000719 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
720 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000721 Check(eq, "JSGlobalObject::global_context should be a global context.");
722 pop(holder_reg); // Restore holder.
723 }
724
725 // Check if both contexts are the same.
726 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
727 cmp(scratch, Operand(ip));
728 b(eq, &same_contexts);
729
730 // Check the context is a global context.
731 if (FLAG_debug_code) {
732 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
733 // Cannot use ip as a temporary in this verification code. Due to the fact
734 // that ip is clobbered as part of cmp with an object Operand.
735 push(holder_reg); // Temporarily save holder on the stack.
736 mov(holder_reg, ip); // Move ip to its holding place.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000737 LoadRoot(ip, Heap::kNullValueRootIndex);
738 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000739 Check(ne, "JSGlobalProxy::context() should not be null.");
740
741 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000742 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
743 cmp(holder_reg, ip);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000744 Check(eq, "JSGlobalObject::global_context should be a global context.");
745 // Restore ip is not needed. ip is reloaded below.
746 pop(holder_reg); // Restore holder.
747 // Restore ip to holder's context.
748 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
749 }
750
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 // Check that the security token in the calling global object is
752 // compatible with the security token in the receiving global
753 // object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000754 int token_offset = Context::kHeaderSize +
755 Context::SECURITY_TOKEN_INDEX * kPointerSize;
756
757 ldr(scratch, FieldMemOperand(scratch, token_offset));
758 ldr(ip, FieldMemOperand(ip, token_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 cmp(scratch, Operand(ip));
760 b(ne, miss);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000761
762 bind(&same_contexts);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763}
764
765
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000766void MacroAssembler::AllocateObjectInNewSpace(int object_size,
767 Register result,
768 Register scratch1,
769 Register scratch2,
770 Label* gc_required,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000771 AllocationFlags flags) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000772 ASSERT(!result.is(scratch1));
773 ASSERT(!scratch1.is(scratch2));
774
775 // Load address of new object into result and allocation top address into
776 // scratch1.
777 ExternalReference new_space_allocation_top =
778 ExternalReference::new_space_allocation_top_address();
779 mov(scratch1, Operand(new_space_allocation_top));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000780 if ((flags & RESULT_CONTAINS_TOP) == 0) {
781 ldr(result, MemOperand(scratch1));
782 } else {
783#ifdef DEBUG
784 // Assert that result actually contains top on entry. scratch2 is used
785 // immediately below so this use of scratch2 does not cause difference with
786 // respect to register content between debug and release mode.
787 ldr(scratch2, MemOperand(scratch1));
788 cmp(result, scratch2);
789 Check(eq, "Unexpected allocation top");
790#endif
791 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000792
793 // Calculate new top and bail out if new space is exhausted. Use result
794 // to calculate the new top.
795 ExternalReference new_space_allocation_limit =
796 ExternalReference::new_space_allocation_limit_address();
797 mov(scratch2, Operand(new_space_allocation_limit));
798 ldr(scratch2, MemOperand(scratch2));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000799 add(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000800 cmp(result, Operand(scratch2));
801 b(hi, gc_required);
802
803 // Update allocation top. result temporarily holds the new top,
804 str(result, MemOperand(scratch1));
805
806 // Tag and adjust back to start of new object.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000807 if ((flags & TAG_OBJECT) != 0) {
808 sub(result, result, Operand((object_size * kPointerSize) -
809 kHeapObjectTag));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000810 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000811 sub(result, result, Operand(object_size * kPointerSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000812 }
813}
814
815
ager@chromium.orga1645e22009-09-09 19:27:10 +0000816void MacroAssembler::AllocateObjectInNewSpace(Register object_size,
817 Register result,
818 Register scratch1,
819 Register scratch2,
820 Label* gc_required,
821 AllocationFlags flags) {
822 ASSERT(!result.is(scratch1));
823 ASSERT(!scratch1.is(scratch2));
824
825 // Load address of new object into result and allocation top address into
826 // scratch1.
827 ExternalReference new_space_allocation_top =
828 ExternalReference::new_space_allocation_top_address();
829 mov(scratch1, Operand(new_space_allocation_top));
830 if ((flags & RESULT_CONTAINS_TOP) == 0) {
831 ldr(result, MemOperand(scratch1));
832 } else {
833#ifdef DEBUG
834 // Assert that result actually contains top on entry. scratch2 is used
835 // immediately below so this use of scratch2 does not cause difference with
836 // respect to register content between debug and release mode.
837 ldr(scratch2, MemOperand(scratch1));
838 cmp(result, scratch2);
839 Check(eq, "Unexpected allocation top");
840#endif
841 }
842
843 // Calculate new top and bail out if new space is exhausted. Use result
844 // to calculate the new top. Object size is in words so a shift is required to
845 // get the number of bytes
846 ExternalReference new_space_allocation_limit =
847 ExternalReference::new_space_allocation_limit_address();
848 mov(scratch2, Operand(new_space_allocation_limit));
849 ldr(scratch2, MemOperand(scratch2));
850 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
851 cmp(result, Operand(scratch2));
852 b(hi, gc_required);
853
854 // Update allocation top. result temporarily holds the new top,
855 str(result, MemOperand(scratch1));
856
857 // Adjust back to start of new object.
858 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
859
860 // Tag object if requested.
861 if ((flags & TAG_OBJECT) != 0) {
862 add(result, result, Operand(kHeapObjectTag));
863 }
864}
865
866
867void MacroAssembler::UndoAllocationInNewSpace(Register object,
868 Register scratch) {
869 ExternalReference new_space_allocation_top =
870 ExternalReference::new_space_allocation_top_address();
871
872 // Make sure the object has no tag before resetting top.
873 and_(object, object, Operand(~kHeapObjectTagMask));
874#ifdef DEBUG
875 // Check that the object un-allocated is below the current top.
876 mov(scratch, Operand(new_space_allocation_top));
877 ldr(scratch, MemOperand(scratch));
878 cmp(object, scratch);
879 Check(lt, "Undo allocation of non allocated memory");
880#endif
881 // Write the address of the object to un-allocate as the current top.
882 mov(scratch, Operand(new_space_allocation_top));
883 str(object, MemOperand(scratch));
884}
885
886
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000887void MacroAssembler::CompareObjectType(Register function,
888 Register map,
889 Register type_reg,
890 InstanceType type) {
891 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000892 CompareInstanceType(map, type_reg, type);
893}
894
895
896void MacroAssembler::CompareInstanceType(Register map,
897 Register type_reg,
898 InstanceType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000899 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
900 cmp(type_reg, Operand(type));
901}
902
903
904void MacroAssembler::TryGetFunctionPrototype(Register function,
905 Register result,
906 Register scratch,
907 Label* miss) {
908 // Check that the receiver isn't a smi.
909 BranchOnSmi(function, miss);
910
911 // Check that the function really is a function. Load map into result reg.
912 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
913 b(ne, miss);
914
915 // Make sure that the function has an instance prototype.
916 Label non_instance;
917 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
918 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
919 b(ne, &non_instance);
920
921 // Get the prototype or initial map from the function.
922 ldr(result,
923 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
924
925 // If the prototype or initial map is the hole, don't return it and
926 // simply miss the cache instead. This will allow us to allocate a
927 // prototype object on-demand in the runtime system.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000928 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
929 cmp(result, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000930 b(eq, miss);
931
932 // If the function does not have an initial map, we're done.
933 Label done;
934 CompareObjectType(result, scratch, scratch, MAP_TYPE);
935 b(ne, &done);
936
937 // Get the prototype from the initial map.
938 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
939 jmp(&done);
940
941 // Non-instance prototype: Fetch prototype from constructor field
942 // in initial map.
943 bind(&non_instance);
944 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
945
946 // All done.
947 bind(&done);
948}
949
950
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000951void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
kasper.lund7276f142008-07-30 08:49:36 +0000952 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000953 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954}
955
956
957void MacroAssembler::StubReturn(int argc) {
958 ASSERT(argc >= 1 && generating_stub());
959 if (argc > 1)
960 add(sp, sp, Operand((argc - 1) * kPointerSize));
961 Ret();
962}
963
mads.s.ager31e71382008-08-13 09:32:07 +0000964
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000965void MacroAssembler::IllegalOperation(int num_arguments) {
966 if (num_arguments > 0) {
967 add(sp, sp, Operand(num_arguments * kPointerSize));
968 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000969 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000970}
971
972
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
mads.s.ager31e71382008-08-13 09:32:07 +0000974 // All parameters are on the stack. r0 has the return value after call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000976 // If the expected number of arguments of the runtime function is
977 // constant, we check that the actual number of arguments match the
978 // expectation.
979 if (f->nargs >= 0 && f->nargs != num_arguments) {
980 IllegalOperation(num_arguments);
981 return;
982 }
kasper.lund7276f142008-07-30 08:49:36 +0000983
mads.s.ager31e71382008-08-13 09:32:07 +0000984 Runtime::FunctionId function_id =
985 static_cast<Runtime::FunctionId>(f->stub_id);
986 RuntimeStub stub(function_id, num_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 CallStub(&stub);
988}
989
990
991void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
992 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
993}
994
995
mads.s.ager31e71382008-08-13 09:32:07 +0000996void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000997 int num_arguments,
998 int result_size) {
mads.s.ager31e71382008-08-13 09:32:07 +0000999 // TODO(1236192): Most runtime routines don't need the number of
1000 // arguments passed in because it is constant. At some point we
1001 // should remove this need and make the runtime routine entry code
1002 // smarter.
1003 mov(r0, Operand(num_arguments));
1004 JumpToBuiltin(ext);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005}
1006
1007
1008void MacroAssembler::JumpToBuiltin(const ExternalReference& builtin) {
1009#if defined(__thumb__)
1010 // Thumb mode builtin.
1011 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1012#endif
1013 mov(r1, Operand(builtin));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001014 CEntryStub stub(1);
ager@chromium.org236ad962008-09-25 09:45:57 +00001015 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016}
1017
1018
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001019Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1020 bool* resolved) {
1021 // Contract with compiled functions is that the function is passed in r1.
1022 int builtins_offset =
1023 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1024 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1025 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
1026 ldr(r1, FieldMemOperand(r1, builtins_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001028 return Builtins::GetCode(id, resolved);
1029}
1030
1031
1032void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1033 InvokeJSFlags flags) {
1034 bool resolved;
1035 Handle<Code> code = ResolveBuiltin(id, &resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036
1037 if (flags == CALL_JS) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001038 Call(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 } else {
1040 ASSERT(flags == JUMP_JS);
ager@chromium.org236ad962008-09-25 09:45:57 +00001041 Jump(code, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 }
1043
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001044 if (!resolved) {
1045 const char* name = Builtins::GetName(id);
1046 int argc = Builtins::GetArgumentsCount(id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 uint32_t flags =
1048 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001049 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
1050 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
ager@chromium.org4af710e2009-09-15 12:20:11 +00001051 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001052 unresolved_.Add(entry);
1053 }
1054}
1055
1056
1057void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1058 bool resolved;
1059 Handle<Code> code = ResolveBuiltin(id, &resolved);
1060
1061 mov(target, Operand(code));
1062 if (!resolved) {
1063 const char* name = Builtins::GetName(id);
1064 int argc = Builtins::GetArgumentsCount(id);
1065 uint32_t flags =
1066 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001067 Bootstrapper::FixupFlagsIsPCRelative::encode(true) |
1068 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
ager@chromium.org4af710e2009-09-15 12:20:11 +00001069 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 unresolved_.Add(entry);
1071 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001072
1073 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074}
1075
1076
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001077void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1078 Register scratch1, Register scratch2) {
1079 if (FLAG_native_code_counters && counter->Enabled()) {
1080 mov(scratch1, Operand(value));
1081 mov(scratch2, Operand(ExternalReference(counter)));
1082 str(scratch1, MemOperand(scratch2));
1083 }
1084}
1085
1086
1087void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1088 Register scratch1, Register scratch2) {
1089 ASSERT(value > 0);
1090 if (FLAG_native_code_counters && counter->Enabled()) {
1091 mov(scratch2, Operand(ExternalReference(counter)));
1092 ldr(scratch1, MemOperand(scratch2));
1093 add(scratch1, scratch1, Operand(value));
1094 str(scratch1, MemOperand(scratch2));
1095 }
1096}
1097
1098
1099void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1100 Register scratch1, Register scratch2) {
1101 ASSERT(value > 0);
1102 if (FLAG_native_code_counters && counter->Enabled()) {
1103 mov(scratch2, Operand(ExternalReference(counter)));
1104 ldr(scratch1, MemOperand(scratch2));
1105 sub(scratch1, scratch1, Operand(value));
1106 str(scratch1, MemOperand(scratch2));
1107 }
1108}
1109
1110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111void MacroAssembler::Assert(Condition cc, const char* msg) {
1112 if (FLAG_debug_code)
1113 Check(cc, msg);
1114}
1115
1116
1117void MacroAssembler::Check(Condition cc, const char* msg) {
1118 Label L;
1119 b(cc, &L);
1120 Abort(msg);
1121 // will not return here
1122 bind(&L);
1123}
1124
1125
1126void MacroAssembler::Abort(const char* msg) {
1127 // We want to pass the msg string like a smi to avoid GC
1128 // problems, however msg is not guaranteed to be aligned
1129 // properly. Instead, we pass an aligned pointer that is
ager@chromium.org32912102009-01-16 10:38:43 +00001130 // a proper v8 smi, but also pass the alignment difference
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 // from the real pointer as a smi.
1132 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1133 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1134 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1135#ifdef DEBUG
1136 if (msg != NULL) {
1137 RecordComment("Abort message: ");
1138 RecordComment(msg);
1139 }
1140#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 mov(r0, Operand(p0));
1142 push(r0);
1143 mov(r0, Operand(Smi::FromInt(p1 - p0)));
mads.s.ager31e71382008-08-13 09:32:07 +00001144 push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 CallRuntime(Runtime::kAbort, 2);
1146 // will not return here
1147}
1148
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001149
ager@chromium.org4af710e2009-09-15 12:20:11 +00001150#ifdef ENABLE_DEBUGGER_SUPPORT
1151CodePatcher::CodePatcher(byte* address, int instructions)
1152 : address_(address),
1153 instructions_(instructions),
1154 size_(instructions * Assembler::kInstrSize),
1155 masm_(address, size_ + Assembler::kGap) {
1156 // Create a new macro assembler pointing to the address of the code to patch.
1157 // The size is adjusted with kGap on order for the assembler to generate size
1158 // bytes of instructions without failing with buffer size constraints.
1159 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1160}
1161
1162
1163CodePatcher::~CodePatcher() {
1164 // Indicate that code has changed.
1165 CPU::FlushICache(address_, size_);
1166
1167 // Check that the code was patched as expected.
1168 ASSERT(masm_.pc_ == address_ + size_);
1169 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1170}
1171
1172
1173void CodePatcher::Emit(Instr x) {
1174 masm()->emit(x);
1175}
1176
1177
1178void CodePatcher::Emit(Address addr) {
1179 masm()->emit(reinterpret_cast<Instr>(addr));
1180}
1181#endif // ENABLE_DEBUGGER_SUPPORT
1182
1183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184} } // namespace v8::internal