blob: 1f08c7cf3684bbd111462f46b89e1beba7aca584 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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
35namespace v8 {
36namespace internal {
37
38MacroAssembler::MacroAssembler(void* buffer, int size)
39 : Assembler(buffer, size),
40 unresolved_(0),
41 generating_stub_(false),
42 allow_stub_calls_(true),
43 code_object_(Heap::undefined_value()) {
44}
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
49#if defined(__thumb__) && !defined(USE_THUMB_INTERWORK)
50#error "flag -mthumb-interwork missing"
51#endif
52
53
54// We do not support thumb inter-working with an arm architecture not supporting
55// 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"
59#endif
60
61
62// Using blx may yield better code, so use it when required or when available
63#if defined(USE_THUMB_INTERWORK) || defined(CAN_USE_ARMV5_INSTRUCTIONS)
64#define USE_BLX 1
65#endif
66
67// Using bx does not yield better code, so use it only when required
68#if defined(USE_THUMB_INTERWORK)
69#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
82void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
83 Condition cond) {
84#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
93void MacroAssembler::Jump(byte* target, RelocInfo::Mode rmode,
94 Condition cond) {
95 ASSERT(!RelocInfo::IsCodeTarget(rmode));
96 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
97}
98
99
100void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
101 Condition cond) {
102 ASSERT(RelocInfo::IsCodeTarget(rmode));
103 // '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
119void MacroAssembler::Call(intptr_t target, RelocInfo::Mode rmode,
120 Condition cond) {
121 // 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);
125 // 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).
130 ASSERT(kCallTargetAddressOffset == kInstrSize);
131}
132
133
134void MacroAssembler::Call(byte* target, RelocInfo::Mode rmode,
135 Condition cond) {
136 ASSERT(!RelocInfo::IsCodeTarget(rmode));
137 Call(reinterpret_cast<intptr_t>(target), rmode, cond);
138}
139
140
141void MacroAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode,
142 Condition cond) {
143 ASSERT(RelocInfo::IsCodeTarget(rmode));
144 // 'code' is always generated ARM code, never THUMB code
145 Call(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
146}
147
148
149void MacroAssembler::Ret(Condition cond) {
150#if USE_BX
151 bx(lr, cond);
152#else
153 mov(pc, Operand(lr), LeaveCC, cond);
154#endif
155}
156
157
Steve Blockd0582a62009-12-15 09:54:21 +0000158void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) {
159 LoadRoot(ip, Heap::kStackLimitRootIndex);
160 cmp(sp, Operand(ip));
161 b(lo, on_stack_overflow);
162}
163
164
Leon Clarkee46be812010-01-19 14:06:41 +0000165void MacroAssembler::Drop(int count, Condition cond) {
166 if (count > 0) {
167 add(sp, sp, Operand(count * kPointerSize), LeaveCC, cond);
168 }
169}
170
171
172void MacroAssembler::Call(Label* target) {
173 bl(target);
174}
175
176
177void MacroAssembler::Move(Register dst, Handle<Object> value) {
178 mov(dst, Operand(value));
179}
Steve Blockd0582a62009-12-15 09:54:21 +0000180
181
Steve Blocka7e24c12009-10-30 11:49:00 +0000182void MacroAssembler::SmiJumpTable(Register index, Vector<Label*> targets) {
183 // Empty the const pool.
184 CheckConstPool(true, true);
185 add(pc, pc, Operand(index,
186 LSL,
187 assembler::arm::Instr::kInstrSizeLog2 - kSmiTagSize));
188 BlockConstPoolBefore(pc_offset() + (targets.length() + 1) * kInstrSize);
189 nop(); // Jump table alignment.
190 for (int i = 0; i < targets.length(); i++) {
191 b(targets[i]);
192 }
193}
194
195
196void MacroAssembler::LoadRoot(Register destination,
197 Heap::RootListIndex index,
198 Condition cond) {
Andrei Popescu31002712010-02-23 13:46:05 +0000199 ldr(destination, MemOperand(roots, index << kPointerSizeLog2), cond);
Steve Blocka7e24c12009-10-30 11:49:00 +0000200}
201
202
203// Will clobber 4 registers: object, offset, scratch, ip. The
204// register 'object' contains a heap object pointer. The heap object
205// tag is shifted away.
206void MacroAssembler::RecordWrite(Register object, Register offset,
207 Register scratch) {
Leon Clarke4515c472010-02-03 11:58:03 +0000208 // The compiled code assumes that record write doesn't change the
209 // context register, so we check that none of the clobbered
210 // registers are cp.
211 ASSERT(!object.is(cp) && !offset.is(cp) && !scratch.is(cp));
212
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 // This is how much we shift the remembered set bit offset to get the
214 // offset of the word in the remembered set. We divide by kBitsPerInt (32,
215 // shift right 5) and then multiply by kIntSize (4, shift left 2).
216 const int kRSetWordShift = 3;
217
218 Label fast, done;
219
220 // First, test that the object is not in the new space. We cannot set
221 // remembered set bits in the new space.
222 // object: heap object pointer (with tag)
223 // offset: offset to store location from the object
224 and_(scratch, object, Operand(Heap::NewSpaceMask()));
225 cmp(scratch, Operand(ExternalReference::new_space_start()));
226 b(eq, &done);
227
228 // Compute the bit offset in the remembered set.
229 // object: heap object pointer (with tag)
230 // offset: offset to store location from the object
231 mov(ip, Operand(Page::kPageAlignmentMask)); // load mask only once
232 and_(scratch, object, Operand(ip)); // offset into page of the object
233 add(offset, scratch, Operand(offset)); // add offset into the object
234 mov(offset, Operand(offset, LSR, kObjectAlignmentBits));
235
236 // Compute the page address from the heap object pointer.
237 // object: heap object pointer (with tag)
238 // offset: bit offset of store position in the remembered set
239 bic(object, object, Operand(ip));
240
241 // If the bit offset lies beyond the normal remembered set range, it is in
242 // the extra remembered set area of a large object.
243 // object: page start
244 // offset: bit offset of store position in the remembered set
245 cmp(offset, Operand(Page::kPageSize / kPointerSize));
246 b(lt, &fast);
247
248 // Adjust the bit offset to be relative to the start of the extra
249 // remembered set and the start address to be the address of the extra
250 // remembered set.
251 sub(offset, offset, Operand(Page::kPageSize / kPointerSize));
252 // Load the array length into 'scratch' and multiply by four to get the
253 // size in bytes of the elements.
254 ldr(scratch, MemOperand(object, Page::kObjectStartOffset
255 + FixedArray::kLengthOffset));
256 mov(scratch, Operand(scratch, LSL, kObjectAlignmentBits));
257 // Add the page header (including remembered set), array header, and array
258 // body size to the page address.
259 add(object, object, Operand(Page::kObjectStartOffset
260 + FixedArray::kHeaderSize));
261 add(object, object, Operand(scratch));
262
263 bind(&fast);
264 // Get address of the rset word.
265 // object: start of the remembered set (page start for the fast case)
266 // offset: bit offset of store position in the remembered set
267 bic(scratch, offset, Operand(kBitsPerInt - 1)); // clear the bit offset
268 add(object, object, Operand(scratch, LSR, kRSetWordShift));
269 // Get bit offset in the rset word.
270 // object: address of remembered set word
271 // offset: bit offset of store position
272 and_(offset, offset, Operand(kBitsPerInt - 1));
273
274 ldr(scratch, MemOperand(object));
275 mov(ip, Operand(1));
276 orr(scratch, scratch, Operand(ip, LSL, offset));
277 str(scratch, MemOperand(object));
278
279 bind(&done);
Leon Clarke4515c472010-02-03 11:58:03 +0000280
281 // Clobber all input registers when running with the debug-code flag
282 // turned on to provoke errors.
283 if (FLAG_debug_code) {
284 mov(object, Operand(bit_cast<int32_t>(kZapValue)));
285 mov(offset, Operand(bit_cast<int32_t>(kZapValue)));
286 mov(scratch, Operand(bit_cast<int32_t>(kZapValue)));
287 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000288}
289
290
291void MacroAssembler::EnterFrame(StackFrame::Type type) {
292 // r0-r3: preserved
293 stm(db_w, sp, cp.bit() | fp.bit() | lr.bit());
294 mov(ip, Operand(Smi::FromInt(type)));
295 push(ip);
296 mov(ip, Operand(CodeObject()));
297 push(ip);
298 add(fp, sp, Operand(3 * kPointerSize)); // Adjust FP to point to saved FP.
299}
300
301
302void MacroAssembler::LeaveFrame(StackFrame::Type type) {
303 // r0: preserved
304 // r1: preserved
305 // r2: preserved
306
307 // Drop the execution stack down to the frame pointer and restore
308 // the caller frame pointer and return address.
309 mov(sp, fp);
310 ldm(ia_w, sp, fp.bit() | lr.bit());
311}
312
313
Steve Blockd0582a62009-12-15 09:54:21 +0000314void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 // Compute the argv pointer and keep it in a callee-saved register.
316 // r0 is argc.
317 add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
318 sub(r6, r6, Operand(kPointerSize));
319
320 // Compute callee's stack pointer before making changes and save it as
321 // ip register so that it is restored as sp register on exit, thereby
322 // popping the args.
323
324 // ip = sp + kPointerSize * #args;
325 add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
326
327 // Align the stack at this point. After this point we have 5 pushes,
328 // so in fact we have to unalign here! See also the assert on the
329 // alignment in AlignStack.
330 AlignStack(1);
331
332 // Push in reverse order: caller_fp, sp_on_exit, and caller_pc.
333 stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
334 mov(fp, Operand(sp)); // setup new frame pointer
335
Steve Blockd0582a62009-12-15 09:54:21 +0000336 if (mode == ExitFrame::MODE_DEBUG) {
337 mov(ip, Operand(Smi::FromInt(0)));
338 } else {
339 mov(ip, Operand(CodeObject()));
340 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 push(ip);
342
343 // Save the frame pointer and the context in top.
344 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
345 str(fp, MemOperand(ip));
346 mov(ip, Operand(ExternalReference(Top::k_context_address)));
347 str(cp, MemOperand(ip));
348
349 // Setup argc and the builtin function in callee-saved registers.
350 mov(r4, Operand(r0));
351 mov(r5, Operand(r1));
352
353
354#ifdef ENABLE_DEBUGGER_SUPPORT
355 // Save the state of all registers to the stack from the memory
356 // location. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000357 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 // Use sp as base to push.
359 CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
360 }
361#endif
362}
363
364
365void MacroAssembler::AlignStack(int offset) {
366#if defined(V8_HOST_ARCH_ARM)
367 // Running on the real platform. Use the alignment as mandated by the local
368 // environment.
369 // Note: This will break if we ever start generating snapshots on one ARM
370 // platform for another ARM platform with a different alignment.
371 int activation_frame_alignment = OS::ActivationFrameAlignment();
372#else // defined(V8_HOST_ARCH_ARM)
373 // If we are using the simulator then we should always align to the expected
374 // alignment. As the simulator is used to generate snapshots we do not know
375 // if the target platform will need alignment, so we will always align at
376 // this point here.
377 int activation_frame_alignment = 2 * kPointerSize;
378#endif // defined(V8_HOST_ARCH_ARM)
379 if (activation_frame_alignment != kPointerSize) {
380 // This code needs to be made more general if this assert doesn't hold.
381 ASSERT(activation_frame_alignment == 2 * kPointerSize);
382 mov(r7, Operand(Smi::FromInt(0)));
383 tst(sp, Operand(activation_frame_alignment - offset));
384 push(r7, eq); // Conditional push instruction.
385 }
386}
387
388
Steve Blockd0582a62009-12-15 09:54:21 +0000389void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000390#ifdef ENABLE_DEBUGGER_SUPPORT
391 // Restore the memory copy of the registers by digging them out from
392 // the stack. This is needed to allow nested break points.
Steve Blockd0582a62009-12-15 09:54:21 +0000393 if (mode == ExitFrame::MODE_DEBUG) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 // This code intentionally clobbers r2 and r3.
395 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +0000396 const int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 add(r3, fp, Operand(kOffset));
398 CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
399 }
400#endif
401
402 // Clear top frame.
403 mov(r3, Operand(0));
404 mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
405 str(r3, MemOperand(ip));
406
407 // Restore current context from top and clear it in debug mode.
408 mov(ip, Operand(ExternalReference(Top::k_context_address)));
409 ldr(cp, MemOperand(ip));
410#ifdef DEBUG
411 str(r3, MemOperand(ip));
412#endif
413
414 // Pop the arguments, restore registers, and return.
415 mov(sp, Operand(fp)); // respect ABI stack constraint
416 ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
417}
418
419
420void MacroAssembler::InvokePrologue(const ParameterCount& expected,
421 const ParameterCount& actual,
422 Handle<Code> code_constant,
423 Register code_reg,
424 Label* done,
425 InvokeFlag flag) {
426 bool definitely_matches = false;
427 Label regular_invoke;
428
429 // Check whether the expected and actual arguments count match. If not,
430 // setup registers according to contract with ArgumentsAdaptorTrampoline:
431 // r0: actual arguments count
432 // r1: function (passed through to callee)
433 // r2: expected arguments count
434 // r3: callee code entry
435
436 // The code below is made a lot easier because the calling code already sets
437 // up actual and expected registers according to the contract if values are
438 // passed in registers.
439 ASSERT(actual.is_immediate() || actual.reg().is(r0));
440 ASSERT(expected.is_immediate() || expected.reg().is(r2));
441 ASSERT((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(r3));
442
443 if (expected.is_immediate()) {
444 ASSERT(actual.is_immediate());
445 if (expected.immediate() == actual.immediate()) {
446 definitely_matches = true;
447 } else {
448 mov(r0, Operand(actual.immediate()));
449 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
450 if (expected.immediate() == sentinel) {
451 // Don't worry about adapting arguments for builtins that
452 // don't want that done. Skip adaption code by making it look
453 // like we have a match between expected and actual number of
454 // arguments.
455 definitely_matches = true;
456 } else {
457 mov(r2, Operand(expected.immediate()));
458 }
459 }
460 } else {
461 if (actual.is_immediate()) {
462 cmp(expected.reg(), Operand(actual.immediate()));
463 b(eq, &regular_invoke);
464 mov(r0, Operand(actual.immediate()));
465 } else {
466 cmp(expected.reg(), Operand(actual.reg()));
467 b(eq, &regular_invoke);
468 }
469 }
470
471 if (!definitely_matches) {
472 if (!code_constant.is_null()) {
473 mov(r3, Operand(code_constant));
474 add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
475 }
476
477 Handle<Code> adaptor =
478 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
479 if (flag == CALL_FUNCTION) {
480 Call(adaptor, RelocInfo::CODE_TARGET);
481 b(done);
482 } else {
483 Jump(adaptor, RelocInfo::CODE_TARGET);
484 }
485 bind(&regular_invoke);
486 }
487}
488
489
490void MacroAssembler::InvokeCode(Register code,
491 const ParameterCount& expected,
492 const ParameterCount& actual,
493 InvokeFlag flag) {
494 Label done;
495
496 InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag);
497 if (flag == CALL_FUNCTION) {
498 Call(code);
499 } else {
500 ASSERT(flag == JUMP_FUNCTION);
501 Jump(code);
502 }
503
504 // Continue here if InvokePrologue does handle the invocation due to
505 // mismatched parameter counts.
506 bind(&done);
507}
508
509
510void MacroAssembler::InvokeCode(Handle<Code> code,
511 const ParameterCount& expected,
512 const ParameterCount& actual,
513 RelocInfo::Mode rmode,
514 InvokeFlag flag) {
515 Label done;
516
517 InvokePrologue(expected, actual, code, no_reg, &done, flag);
518 if (flag == CALL_FUNCTION) {
519 Call(code, rmode);
520 } else {
521 Jump(code, rmode);
522 }
523
524 // Continue here if InvokePrologue does handle the invocation due to
525 // mismatched parameter counts.
526 bind(&done);
527}
528
529
530void MacroAssembler::InvokeFunction(Register fun,
531 const ParameterCount& actual,
532 InvokeFlag flag) {
533 // Contract with called JS functions requires that function is passed in r1.
534 ASSERT(fun.is(r1));
535
536 Register expected_reg = r2;
537 Register code_reg = r3;
538
539 ldr(code_reg, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
540 ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
541 ldr(expected_reg,
542 FieldMemOperand(code_reg,
543 SharedFunctionInfo::kFormalParameterCountOffset));
544 ldr(code_reg,
545 MemOperand(code_reg, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
546 add(code_reg, code_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
547
548 ParameterCount expected(expected_reg);
549 InvokeCode(code_reg, expected, actual, flag);
550}
551
552
553#ifdef ENABLE_DEBUGGER_SUPPORT
554void MacroAssembler::SaveRegistersToMemory(RegList regs) {
555 ASSERT((regs & ~kJSCallerSaved) == 0);
556 // Copy the content of registers to memory location.
557 for (int i = 0; i < kNumJSCallerSaved; i++) {
558 int r = JSCallerSavedCode(i);
559 if ((regs & (1 << r)) != 0) {
560 Register reg = { r };
561 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
562 str(reg, MemOperand(ip));
563 }
564 }
565}
566
567
568void MacroAssembler::RestoreRegistersFromMemory(RegList regs) {
569 ASSERT((regs & ~kJSCallerSaved) == 0);
570 // Copy the content of memory location to registers.
571 for (int i = kNumJSCallerSaved; --i >= 0;) {
572 int r = JSCallerSavedCode(i);
573 if ((regs & (1 << r)) != 0) {
574 Register reg = { r };
575 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
576 ldr(reg, MemOperand(ip));
577 }
578 }
579}
580
581
582void MacroAssembler::CopyRegistersFromMemoryToStack(Register base,
583 RegList regs) {
584 ASSERT((regs & ~kJSCallerSaved) == 0);
585 // Copy the content of the memory location to the stack and adjust base.
586 for (int i = kNumJSCallerSaved; --i >= 0;) {
587 int r = JSCallerSavedCode(i);
588 if ((regs & (1 << r)) != 0) {
589 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
590 ldr(ip, MemOperand(ip));
591 str(ip, MemOperand(base, 4, NegPreIndex));
592 }
593 }
594}
595
596
597void MacroAssembler::CopyRegistersFromStackToMemory(Register base,
598 Register scratch,
599 RegList regs) {
600 ASSERT((regs & ~kJSCallerSaved) == 0);
601 // Copy the content of the stack to the memory location and adjust base.
602 for (int i = 0; i < kNumJSCallerSaved; i++) {
603 int r = JSCallerSavedCode(i);
604 if ((regs & (1 << r)) != 0) {
605 mov(ip, Operand(ExternalReference(Debug_Address::Register(i))));
606 ldr(scratch, MemOperand(base, 4, PostIndex));
607 str(scratch, MemOperand(ip));
608 }
609 }
610}
611#endif
612
613
614void MacroAssembler::PushTryHandler(CodeLocation try_location,
615 HandlerType type) {
616 // Adjust this code if not the case.
617 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
618 // The pc (return address) is passed in register lr.
619 if (try_location == IN_JAVASCRIPT) {
620 if (type == TRY_CATCH_HANDLER) {
621 mov(r3, Operand(StackHandler::TRY_CATCH));
622 } else {
623 mov(r3, Operand(StackHandler::TRY_FINALLY));
624 }
625 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
626 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
627 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
628 stm(db_w, sp, r3.bit() | fp.bit() | lr.bit());
629 // Save the current handler as the next handler.
630 mov(r3, Operand(ExternalReference(Top::k_handler_address)));
631 ldr(r1, MemOperand(r3));
632 ASSERT(StackHandlerConstants::kNextOffset == 0);
633 push(r1);
634 // Link this handler as the new current one.
635 str(sp, MemOperand(r3));
636 } else {
637 // Must preserve r0-r4, r5-r7 are available.
638 ASSERT(try_location == IN_JS_ENTRY);
639 // The frame pointer does not point to a JS frame so we save NULL
640 // for fp. We expect the code throwing an exception to check fp
641 // before dereferencing it to restore the context.
642 mov(ip, Operand(0)); // To save a NULL frame pointer.
643 mov(r6, Operand(StackHandler::ENTRY));
644 ASSERT(StackHandlerConstants::kStateOffset == 1 * kPointerSize
645 && StackHandlerConstants::kFPOffset == 2 * kPointerSize
646 && StackHandlerConstants::kPCOffset == 3 * kPointerSize);
647 stm(db_w, sp, r6.bit() | ip.bit() | lr.bit());
648 // Save the current handler as the next handler.
649 mov(r7, Operand(ExternalReference(Top::k_handler_address)));
650 ldr(r6, MemOperand(r7));
651 ASSERT(StackHandlerConstants::kNextOffset == 0);
652 push(r6);
653 // Link this handler as the new current one.
654 str(sp, MemOperand(r7));
655 }
656}
657
658
Leon Clarkee46be812010-01-19 14:06:41 +0000659void MacroAssembler::PopTryHandler() {
660 ASSERT_EQ(0, StackHandlerConstants::kNextOffset);
661 pop(r1);
662 mov(ip, Operand(ExternalReference(Top::k_handler_address)));
663 add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
664 str(r1, MemOperand(ip));
665}
666
667
Steve Blocka7e24c12009-10-30 11:49:00 +0000668Register MacroAssembler::CheckMaps(JSObject* object, Register object_reg,
669 JSObject* holder, Register holder_reg,
670 Register scratch,
671 Label* miss) {
672 // Make sure there's no overlap between scratch and the other
673 // registers.
674 ASSERT(!scratch.is(object_reg) && !scratch.is(holder_reg));
675
676 // Keep track of the current object in register reg.
677 Register reg = object_reg;
678 int depth = 1;
679
680 // Check the maps in the prototype chain.
681 // Traverse the prototype chain from the object and do map checks.
682 while (object != holder) {
683 depth++;
684
685 // Only global objects and objects that do not require access
686 // checks are allowed in stubs.
687 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
688
689 // Get the map of the current object.
690 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
691 cmp(scratch, Operand(Handle<Map>(object->map())));
692
693 // Branch on the result of the map check.
694 b(ne, miss);
695
696 // Check access rights to the global object. This has to happen
697 // after the map check so that we know that the object is
698 // actually a global object.
699 if (object->IsJSGlobalProxy()) {
700 CheckAccessGlobalProxy(reg, scratch, miss);
701 // Restore scratch register to be the map of the object. In the
702 // new space case below, we load the prototype from the map in
703 // the scratch register.
704 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
705 }
706
707 reg = holder_reg; // from now the object is in holder_reg
708 JSObject* prototype = JSObject::cast(object->GetPrototype());
709 if (Heap::InNewSpace(prototype)) {
710 // The prototype is in new space; we cannot store a reference
711 // to it in the code. Load it from the map.
712 ldr(reg, FieldMemOperand(scratch, Map::kPrototypeOffset));
713 } else {
714 // The prototype is in old space; load it directly.
715 mov(reg, Operand(Handle<JSObject>(prototype)));
716 }
717
718 // Go to the next object in the prototype chain.
719 object = prototype;
720 }
721
722 // Check the holder map.
723 ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
724 cmp(scratch, Operand(Handle<Map>(object->map())));
725 b(ne, miss);
726
727 // Log the check depth.
728 LOG(IntEvent("check-maps-depth", depth));
729
730 // Perform security check for access to the global object and return
731 // the holder register.
732 ASSERT(object == holder);
733 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
734 if (object->IsJSGlobalProxy()) {
735 CheckAccessGlobalProxy(reg, scratch, miss);
736 }
737 return reg;
738}
739
740
741void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
742 Register scratch,
743 Label* miss) {
744 Label same_contexts;
745
746 ASSERT(!holder_reg.is(scratch));
747 ASSERT(!holder_reg.is(ip));
748 ASSERT(!scratch.is(ip));
749
750 // Load current lexical context from the stack frame.
751 ldr(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
752 // In debug mode, make sure the lexical context is set.
753#ifdef DEBUG
754 cmp(scratch, Operand(0));
755 Check(ne, "we should not have an empty lexical context");
756#endif
757
758 // Load the global context of the current context.
759 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
760 ldr(scratch, FieldMemOperand(scratch, offset));
761 ldr(scratch, FieldMemOperand(scratch, GlobalObject::kGlobalContextOffset));
762
763 // Check the context is a global context.
764 if (FLAG_debug_code) {
765 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
766 // Cannot use ip as a temporary in this verification code. Due to the fact
767 // that ip is clobbered as part of cmp with an object Operand.
768 push(holder_reg); // Temporarily save holder on the stack.
769 // Read the first word and compare to the global_context_map.
770 ldr(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
771 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
772 cmp(holder_reg, ip);
773 Check(eq, "JSGlobalObject::global_context should be a global context.");
774 pop(holder_reg); // Restore holder.
775 }
776
777 // Check if both contexts are the same.
778 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
779 cmp(scratch, Operand(ip));
780 b(eq, &same_contexts);
781
782 // Check the context is a global context.
783 if (FLAG_debug_code) {
784 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
785 // Cannot use ip as a temporary in this verification code. Due to the fact
786 // that ip is clobbered as part of cmp with an object Operand.
787 push(holder_reg); // Temporarily save holder on the stack.
788 mov(holder_reg, ip); // Move ip to its holding place.
789 LoadRoot(ip, Heap::kNullValueRootIndex);
790 cmp(holder_reg, ip);
791 Check(ne, "JSGlobalProxy::context() should not be null.");
792
793 ldr(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
794 LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
795 cmp(holder_reg, ip);
796 Check(eq, "JSGlobalObject::global_context should be a global context.");
797 // Restore ip is not needed. ip is reloaded below.
798 pop(holder_reg); // Restore holder.
799 // Restore ip to holder's context.
800 ldr(ip, FieldMemOperand(holder_reg, JSGlobalProxy::kContextOffset));
801 }
802
803 // Check that the security token in the calling global object is
804 // compatible with the security token in the receiving global
805 // object.
806 int token_offset = Context::kHeaderSize +
807 Context::SECURITY_TOKEN_INDEX * kPointerSize;
808
809 ldr(scratch, FieldMemOperand(scratch, token_offset));
810 ldr(ip, FieldMemOperand(ip, token_offset));
811 cmp(scratch, Operand(ip));
812 b(ne, miss);
813
814 bind(&same_contexts);
815}
816
817
818void MacroAssembler::AllocateInNewSpace(int object_size,
819 Register result,
820 Register scratch1,
821 Register scratch2,
822 Label* gc_required,
823 AllocationFlags flags) {
824 ASSERT(!result.is(scratch1));
825 ASSERT(!scratch1.is(scratch2));
826
827 // Load address of new object into result and allocation top address into
828 // scratch1.
829 ExternalReference new_space_allocation_top =
830 ExternalReference::new_space_allocation_top_address();
831 mov(scratch1, Operand(new_space_allocation_top));
832 if ((flags & RESULT_CONTAINS_TOP) == 0) {
833 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000834 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 // Assert that result actually contains top on entry. scratch2 is used
836 // immediately below so this use of scratch2 does not cause difference with
837 // respect to register content between debug and release mode.
838 ldr(scratch2, MemOperand(scratch1));
839 cmp(result, scratch2);
840 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 }
842
843 // Calculate new top and bail out if new space is exhausted. Use result
844 // to calculate the new top.
845 ExternalReference new_space_allocation_limit =
846 ExternalReference::new_space_allocation_limit_address();
847 mov(scratch2, Operand(new_space_allocation_limit));
848 ldr(scratch2, MemOperand(scratch2));
849 add(result, result, Operand(object_size * kPointerSize));
850 cmp(result, Operand(scratch2));
851 b(hi, gc_required);
852
Steve Blockd0582a62009-12-15 09:54:21 +0000853 // Update allocation top. result temporarily holds the new top.
854 if (FLAG_debug_code) {
855 tst(result, Operand(kObjectAlignmentMask));
856 Check(eq, "Unaligned allocation in new space");
857 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 str(result, MemOperand(scratch1));
859
860 // Tag and adjust back to start of new object.
861 if ((flags & TAG_OBJECT) != 0) {
862 sub(result, result, Operand((object_size * kPointerSize) -
863 kHeapObjectTag));
864 } else {
865 sub(result, result, Operand(object_size * kPointerSize));
866 }
867}
868
869
870void MacroAssembler::AllocateInNewSpace(Register object_size,
871 Register result,
872 Register scratch1,
873 Register scratch2,
874 Label* gc_required,
875 AllocationFlags flags) {
876 ASSERT(!result.is(scratch1));
877 ASSERT(!scratch1.is(scratch2));
878
879 // Load address of new object into result and allocation top address into
880 // scratch1.
881 ExternalReference new_space_allocation_top =
882 ExternalReference::new_space_allocation_top_address();
883 mov(scratch1, Operand(new_space_allocation_top));
884 if ((flags & RESULT_CONTAINS_TOP) == 0) {
885 ldr(result, MemOperand(scratch1));
Steve Blockd0582a62009-12-15 09:54:21 +0000886 } else if (FLAG_debug_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000887 // Assert that result actually contains top on entry. scratch2 is used
888 // immediately below so this use of scratch2 does not cause difference with
889 // respect to register content between debug and release mode.
890 ldr(scratch2, MemOperand(scratch1));
891 cmp(result, scratch2);
892 Check(eq, "Unexpected allocation top");
Steve Blocka7e24c12009-10-30 11:49:00 +0000893 }
894
895 // Calculate new top and bail out if new space is exhausted. Use result
896 // to calculate the new top. Object size is in words so a shift is required to
897 // get the number of bytes
898 ExternalReference new_space_allocation_limit =
899 ExternalReference::new_space_allocation_limit_address();
900 mov(scratch2, Operand(new_space_allocation_limit));
901 ldr(scratch2, MemOperand(scratch2));
902 add(result, result, Operand(object_size, LSL, kPointerSizeLog2));
903 cmp(result, Operand(scratch2));
904 b(hi, gc_required);
905
Steve Blockd0582a62009-12-15 09:54:21 +0000906 // Update allocation top. result temporarily holds the new top.
907 if (FLAG_debug_code) {
908 tst(result, Operand(kObjectAlignmentMask));
909 Check(eq, "Unaligned allocation in new space");
910 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000911 str(result, MemOperand(scratch1));
912
913 // Adjust back to start of new object.
914 sub(result, result, Operand(object_size, LSL, kPointerSizeLog2));
915
916 // Tag object if requested.
917 if ((flags & TAG_OBJECT) != 0) {
918 add(result, result, Operand(kHeapObjectTag));
919 }
920}
921
922
923void MacroAssembler::UndoAllocationInNewSpace(Register object,
924 Register scratch) {
925 ExternalReference new_space_allocation_top =
926 ExternalReference::new_space_allocation_top_address();
927
928 // Make sure the object has no tag before resetting top.
929 and_(object, object, Operand(~kHeapObjectTagMask));
930#ifdef DEBUG
931 // Check that the object un-allocated is below the current top.
932 mov(scratch, Operand(new_space_allocation_top));
933 ldr(scratch, MemOperand(scratch));
934 cmp(object, scratch);
935 Check(lt, "Undo allocation of non allocated memory");
936#endif
937 // Write the address of the object to un-allocate as the current top.
938 mov(scratch, Operand(new_space_allocation_top));
939 str(object, MemOperand(scratch));
940}
941
942
Andrei Popescu31002712010-02-23 13:46:05 +0000943void MacroAssembler::AllocateTwoByteString(Register result,
944 Register length,
945 Register scratch1,
946 Register scratch2,
947 Register scratch3,
948 Label* gc_required) {
949 // Calculate the number of bytes needed for the characters in the string while
950 // observing object alignment.
951 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
952 mov(scratch1, Operand(length, LSL, 1)); // Length in bytes, not chars.
953 add(scratch1, scratch1,
954 Operand(kObjectAlignmentMask + SeqTwoByteString::kHeaderSize));
955 // AllocateInNewSpace expects the size in words, so we can round down
956 // to kObjectAlignment and divide by kPointerSize in the same shift.
957 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
958 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
959
960 // Allocate two-byte string in new space.
961 AllocateInNewSpace(scratch1,
962 result,
963 scratch2,
964 scratch3,
965 gc_required,
966 TAG_OBJECT);
967
968 // Set the map, length and hash field.
969 LoadRoot(scratch1, Heap::kStringMapRootIndex);
970 str(length, FieldMemOperand(result, String::kLengthOffset));
971 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
972 mov(scratch2, Operand(String::kEmptyHashField));
973 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
974}
975
976
977void MacroAssembler::AllocateAsciiString(Register result,
978 Register length,
979 Register scratch1,
980 Register scratch2,
981 Register scratch3,
982 Label* gc_required) {
983 // Calculate the number of bytes needed for the characters in the string while
984 // observing object alignment.
985 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
986 ASSERT(kCharSize == 1);
987 add(scratch1, length,
988 Operand(kObjectAlignmentMask + SeqAsciiString::kHeaderSize));
989 // AllocateInNewSpace expects the size in words, so we can round down
990 // to kObjectAlignment and divide by kPointerSize in the same shift.
991 ASSERT_EQ(kPointerSize, kObjectAlignmentMask + 1);
992 mov(scratch1, Operand(scratch1, ASR, kPointerSizeLog2));
993
994 // Allocate ASCII string in new space.
995 AllocateInNewSpace(scratch1,
996 result,
997 scratch2,
998 scratch3,
999 gc_required,
1000 TAG_OBJECT);
1001
1002 // Set the map, length and hash field.
1003 LoadRoot(scratch1, Heap::kAsciiStringMapRootIndex);
1004 mov(scratch1, Operand(Factory::ascii_string_map()));
1005 str(length, FieldMemOperand(result, String::kLengthOffset));
1006 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1007 mov(scratch2, Operand(String::kEmptyHashField));
1008 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1009}
1010
1011
1012void MacroAssembler::AllocateTwoByteConsString(Register result,
1013 Register length,
1014 Register scratch1,
1015 Register scratch2,
1016 Label* gc_required) {
1017 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1018 result,
1019 scratch1,
1020 scratch2,
1021 gc_required,
1022 TAG_OBJECT);
1023 LoadRoot(scratch1, Heap::kConsStringMapRootIndex);
1024 mov(scratch2, Operand(String::kEmptyHashField));
1025 str(length, FieldMemOperand(result, String::kLengthOffset));
1026 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1027 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1028}
1029
1030
1031void MacroAssembler::AllocateAsciiConsString(Register result,
1032 Register length,
1033 Register scratch1,
1034 Register scratch2,
1035 Label* gc_required) {
1036 AllocateInNewSpace(ConsString::kSize / kPointerSize,
1037 result,
1038 scratch1,
1039 scratch2,
1040 gc_required,
1041 TAG_OBJECT);
1042 LoadRoot(scratch1, Heap::kConsAsciiStringMapRootIndex);
1043 mov(scratch2, Operand(String::kEmptyHashField));
1044 str(length, FieldMemOperand(result, String::kLengthOffset));
1045 str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
1046 str(scratch2, FieldMemOperand(result, String::kHashFieldOffset));
1047}
1048
1049
Steve Blocka7e24c12009-10-30 11:49:00 +00001050void MacroAssembler::CompareObjectType(Register function,
1051 Register map,
1052 Register type_reg,
1053 InstanceType type) {
1054 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
1055 CompareInstanceType(map, type_reg, type);
1056}
1057
1058
1059void MacroAssembler::CompareInstanceType(Register map,
1060 Register type_reg,
1061 InstanceType type) {
1062 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
1063 cmp(type_reg, Operand(type));
1064}
1065
1066
Andrei Popescu31002712010-02-23 13:46:05 +00001067void MacroAssembler::CheckMap(Register obj,
1068 Register scratch,
1069 Handle<Map> map,
1070 Label* fail,
1071 bool is_heap_object) {
1072 if (!is_heap_object) {
1073 BranchOnSmi(obj, fail);
1074 }
1075 ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
1076 mov(ip, Operand(map));
1077 cmp(scratch, ip);
1078 b(ne, fail);
1079}
1080
1081
Steve Blocka7e24c12009-10-30 11:49:00 +00001082void MacroAssembler::TryGetFunctionPrototype(Register function,
1083 Register result,
1084 Register scratch,
1085 Label* miss) {
1086 // Check that the receiver isn't a smi.
1087 BranchOnSmi(function, miss);
1088
1089 // Check that the function really is a function. Load map into result reg.
1090 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
1091 b(ne, miss);
1092
1093 // Make sure that the function has an instance prototype.
1094 Label non_instance;
1095 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
1096 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
1097 b(ne, &non_instance);
1098
1099 // Get the prototype or initial map from the function.
1100 ldr(result,
1101 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1102
1103 // If the prototype or initial map is the hole, don't return it and
1104 // simply miss the cache instead. This will allow us to allocate a
1105 // prototype object on-demand in the runtime system.
1106 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1107 cmp(result, ip);
1108 b(eq, miss);
1109
1110 // If the function does not have an initial map, we're done.
1111 Label done;
1112 CompareObjectType(result, scratch, scratch, MAP_TYPE);
1113 b(ne, &done);
1114
1115 // Get the prototype from the initial map.
1116 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
1117 jmp(&done);
1118
1119 // Non-instance prototype: Fetch prototype from constructor field
1120 // in initial map.
1121 bind(&non_instance);
1122 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1123
1124 // All done.
1125 bind(&done);
1126}
1127
1128
1129void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1130 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1131 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1132}
1133
1134
Andrei Popescu31002712010-02-23 13:46:05 +00001135void MacroAssembler::TailCallStub(CodeStub* stub, Condition cond) {
1136 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1137 Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1138}
1139
1140
Steve Blocka7e24c12009-10-30 11:49:00 +00001141void MacroAssembler::StubReturn(int argc) {
1142 ASSERT(argc >= 1 && generating_stub());
Andrei Popescu31002712010-02-23 13:46:05 +00001143 if (argc > 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001144 add(sp, sp, Operand((argc - 1) * kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +00001145 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 Ret();
1147}
1148
1149
1150void MacroAssembler::IllegalOperation(int num_arguments) {
1151 if (num_arguments > 0) {
1152 add(sp, sp, Operand(num_arguments * kPointerSize));
1153 }
1154 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1155}
1156
1157
Steve Blockd0582a62009-12-15 09:54:21 +00001158void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1159 Register outHighReg,
1160 Register outLowReg) {
1161 // ARMv7 VFP3 instructions to implement integer to double conversion.
1162 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001163 vmov(s15, r7);
1164 vcvt(d7, s15);
1165 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001166}
1167
1168
Andrei Popescu31002712010-02-23 13:46:05 +00001169void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1170 Register src,
1171 int num_least_bits) {
1172 if (CpuFeatures::IsSupported(ARMv7)) {
1173 ubfx(dst, src, Operand(kSmiTagSize), Operand(num_least_bits - 1));
1174 } else {
1175 mov(dst, Operand(src, ASR, kSmiTagSize));
1176 and_(dst, dst, Operand((1 << num_least_bits) - 1));
1177 }
1178}
1179
1180
Steve Blocka7e24c12009-10-30 11:49:00 +00001181void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1182 // All parameters are on the stack. r0 has the return value after call.
1183
1184 // If the expected number of arguments of the runtime function is
1185 // constant, we check that the actual number of arguments match the
1186 // expectation.
1187 if (f->nargs >= 0 && f->nargs != num_arguments) {
1188 IllegalOperation(num_arguments);
1189 return;
1190 }
1191
Leon Clarke4515c472010-02-03 11:58:03 +00001192 // TODO(1236192): Most runtime routines don't need the number of
1193 // arguments passed in because it is constant. At some point we
1194 // should remove this need and make the runtime routine entry code
1195 // smarter.
1196 mov(r0, Operand(num_arguments));
1197 mov(r1, Operand(ExternalReference(f)));
1198 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001199 CallStub(&stub);
1200}
1201
1202
1203void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1204 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1205}
1206
1207
1208void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
1209 int num_arguments,
1210 int result_size) {
1211 // TODO(1236192): Most runtime routines don't need the number of
1212 // arguments passed in because it is constant. At some point we
1213 // should remove this need and make the runtime routine entry code
1214 // smarter.
1215 mov(r0, Operand(num_arguments));
1216 JumpToRuntime(ext);
1217}
1218
1219
1220void MacroAssembler::JumpToRuntime(const ExternalReference& builtin) {
1221#if defined(__thumb__)
1222 // Thumb mode builtin.
1223 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1224#endif
1225 mov(r1, Operand(builtin));
1226 CEntryStub stub(1);
1227 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1228}
1229
1230
1231Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1232 bool* resolved) {
1233 // Contract with compiled functions is that the function is passed in r1.
1234 int builtins_offset =
1235 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1236 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1237 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
1238 ldr(r1, FieldMemOperand(r1, builtins_offset));
1239
1240 return Builtins::GetCode(id, resolved);
1241}
1242
1243
1244void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1245 InvokeJSFlags flags) {
1246 bool resolved;
1247 Handle<Code> code = ResolveBuiltin(id, &resolved);
1248
1249 if (flags == CALL_JS) {
1250 Call(code, RelocInfo::CODE_TARGET);
1251 } else {
1252 ASSERT(flags == JUMP_JS);
1253 Jump(code, RelocInfo::CODE_TARGET);
1254 }
1255
1256 if (!resolved) {
1257 const char* name = Builtins::GetName(id);
1258 int argc = Builtins::GetArgumentsCount(id);
1259 uint32_t flags =
1260 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
Steve Blocka7e24c12009-10-30 11:49:00 +00001261 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
1262 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
1263 unresolved_.Add(entry);
1264 }
1265}
1266
1267
1268void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1269 bool resolved;
1270 Handle<Code> code = ResolveBuiltin(id, &resolved);
1271
1272 mov(target, Operand(code));
1273 if (!resolved) {
1274 const char* name = Builtins::GetName(id);
1275 int argc = Builtins::GetArgumentsCount(id);
1276 uint32_t flags =
1277 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
Steve Blocka7e24c12009-10-30 11:49:00 +00001278 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
1279 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
1280 unresolved_.Add(entry);
1281 }
1282
1283 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
1284}
1285
1286
1287void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1288 Register scratch1, Register scratch2) {
1289 if (FLAG_native_code_counters && counter->Enabled()) {
1290 mov(scratch1, Operand(value));
1291 mov(scratch2, Operand(ExternalReference(counter)));
1292 str(scratch1, MemOperand(scratch2));
1293 }
1294}
1295
1296
1297void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1298 Register scratch1, Register scratch2) {
1299 ASSERT(value > 0);
1300 if (FLAG_native_code_counters && counter->Enabled()) {
1301 mov(scratch2, Operand(ExternalReference(counter)));
1302 ldr(scratch1, MemOperand(scratch2));
1303 add(scratch1, scratch1, Operand(value));
1304 str(scratch1, MemOperand(scratch2));
1305 }
1306}
1307
1308
1309void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1310 Register scratch1, Register scratch2) {
1311 ASSERT(value > 0);
1312 if (FLAG_native_code_counters && counter->Enabled()) {
1313 mov(scratch2, Operand(ExternalReference(counter)));
1314 ldr(scratch1, MemOperand(scratch2));
1315 sub(scratch1, scratch1, Operand(value));
1316 str(scratch1, MemOperand(scratch2));
1317 }
1318}
1319
1320
1321void MacroAssembler::Assert(Condition cc, const char* msg) {
1322 if (FLAG_debug_code)
1323 Check(cc, msg);
1324}
1325
1326
1327void MacroAssembler::Check(Condition cc, const char* msg) {
1328 Label L;
1329 b(cc, &L);
1330 Abort(msg);
1331 // will not return here
1332 bind(&L);
1333}
1334
1335
1336void MacroAssembler::Abort(const char* msg) {
1337 // We want to pass the msg string like a smi to avoid GC
1338 // problems, however msg is not guaranteed to be aligned
1339 // properly. Instead, we pass an aligned pointer that is
1340 // a proper v8 smi, but also pass the alignment difference
1341 // from the real pointer as a smi.
1342 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1343 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1344 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1345#ifdef DEBUG
1346 if (msg != NULL) {
1347 RecordComment("Abort message: ");
1348 RecordComment(msg);
1349 }
1350#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001351 // Disable stub call restrictions to always allow calls to abort.
1352 set_allow_stub_calls(true);
1353
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 mov(r0, Operand(p0));
1355 push(r0);
1356 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1357 push(r0);
1358 CallRuntime(Runtime::kAbort, 2);
1359 // will not return here
1360}
1361
1362
Steve Blockd0582a62009-12-15 09:54:21 +00001363void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1364 if (context_chain_length > 0) {
1365 // Move up the chain of contexts to the context containing the slot.
1366 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1367 // Load the function context (which is the incoming, outer context).
1368 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1369 for (int i = 1; i < context_chain_length; i++) {
1370 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1371 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1372 }
1373 // The context may be an intermediate context, not a function context.
1374 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1375 } else { // Slot is in the current function context.
1376 // The context may be an intermediate context, not a function context.
1377 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1378 }
1379}
1380
1381
Andrei Popescu31002712010-02-23 13:46:05 +00001382void MacroAssembler::JumpIfNotBothSmi(Register reg1,
1383 Register reg2,
1384 Label* on_not_both_smi) {
1385 ASSERT_EQ(0, kSmiTag);
1386 tst(reg1, Operand(kSmiTagMask));
1387 tst(reg2, Operand(kSmiTagMask), eq);
1388 b(ne, on_not_both_smi);
1389}
1390
1391
1392void MacroAssembler::JumpIfEitherSmi(Register reg1,
1393 Register reg2,
1394 Label* on_either_smi) {
1395 ASSERT_EQ(0, kSmiTag);
1396 tst(reg1, Operand(kSmiTagMask));
1397 tst(reg2, Operand(kSmiTagMask), ne);
1398 b(eq, on_either_smi);
1399}
1400
1401
Leon Clarked91b9f72010-01-27 17:25:45 +00001402void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1403 Register first,
1404 Register second,
1405 Register scratch1,
1406 Register scratch2,
1407 Label* failure) {
1408 // Test that both first and second are sequential ASCII strings.
1409 // Assume that they are non-smis.
1410 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1411 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1412 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1413 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
1414 int kFlatAsciiStringMask =
1415 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1416 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1417 and_(scratch1, scratch1, Operand(kFlatAsciiStringMask));
1418 and_(scratch2, scratch2, Operand(kFlatAsciiStringMask));
1419 cmp(scratch1, Operand(kFlatAsciiStringTag));
1420 // Ignore second test if first test failed.
1421 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1422 b(ne, failure);
1423}
1424
1425void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1426 Register second,
1427 Register scratch1,
1428 Register scratch2,
1429 Label* failure) {
1430 // Check that neither is a smi.
1431 ASSERT_EQ(0, kSmiTag);
1432 and_(scratch1, first, Operand(second));
1433 tst(scratch1, Operand(kSmiTagMask));
1434 b(eq, failure);
1435 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1436 second,
1437 scratch1,
1438 scratch2,
1439 failure);
1440}
1441
Steve Blockd0582a62009-12-15 09:54:21 +00001442
Steve Blocka7e24c12009-10-30 11:49:00 +00001443#ifdef ENABLE_DEBUGGER_SUPPORT
1444CodePatcher::CodePatcher(byte* address, int instructions)
1445 : address_(address),
1446 instructions_(instructions),
1447 size_(instructions * Assembler::kInstrSize),
1448 masm_(address, size_ + Assembler::kGap) {
1449 // Create a new macro assembler pointing to the address of the code to patch.
1450 // The size is adjusted with kGap on order for the assembler to generate size
1451 // bytes of instructions without failing with buffer size constraints.
1452 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1453}
1454
1455
1456CodePatcher::~CodePatcher() {
1457 // Indicate that code has changed.
1458 CPU::FlushICache(address_, size_);
1459
1460 // Check that the code was patched as expected.
1461 ASSERT(masm_.pc_ == address_ + size_);
1462 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1463}
1464
1465
1466void CodePatcher::Emit(Instr x) {
1467 masm()->emit(x);
1468}
1469
1470
1471void CodePatcher::Emit(Address addr) {
1472 masm()->emit(reinterpret_cast<Instr>(addr));
1473}
1474#endif // ENABLE_DEBUGGER_SUPPORT
1475
1476
1477} } // namespace v8::internal