blob: b39404e7f91a560ee896097853523a4523b95fc1 [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) {
199 ldr(destination, MemOperand(r10, index << kPointerSizeLog2), cond);
200}
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
943void MacroAssembler::CompareObjectType(Register function,
944 Register map,
945 Register type_reg,
946 InstanceType type) {
947 ldr(map, FieldMemOperand(function, HeapObject::kMapOffset));
948 CompareInstanceType(map, type_reg, type);
949}
950
951
952void MacroAssembler::CompareInstanceType(Register map,
953 Register type_reg,
954 InstanceType type) {
955 ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
956 cmp(type_reg, Operand(type));
957}
958
959
960void MacroAssembler::TryGetFunctionPrototype(Register function,
961 Register result,
962 Register scratch,
963 Label* miss) {
964 // Check that the receiver isn't a smi.
965 BranchOnSmi(function, miss);
966
967 // Check that the function really is a function. Load map into result reg.
968 CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
969 b(ne, miss);
970
971 // Make sure that the function has an instance prototype.
972 Label non_instance;
973 ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
974 tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
975 b(ne, &non_instance);
976
977 // Get the prototype or initial map from the function.
978 ldr(result,
979 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
980
981 // If the prototype or initial map is the hole, don't return it and
982 // simply miss the cache instead. This will allow us to allocate a
983 // prototype object on-demand in the runtime system.
984 LoadRoot(ip, Heap::kTheHoleValueRootIndex);
985 cmp(result, ip);
986 b(eq, miss);
987
988 // If the function does not have an initial map, we're done.
989 Label done;
990 CompareObjectType(result, scratch, scratch, MAP_TYPE);
991 b(ne, &done);
992
993 // Get the prototype from the initial map.
994 ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
995 jmp(&done);
996
997 // Non-instance prototype: Fetch prototype from constructor field
998 // in initial map.
999 bind(&non_instance);
1000 ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
1001
1002 // All done.
1003 bind(&done);
1004}
1005
1006
1007void MacroAssembler::CallStub(CodeStub* stub, Condition cond) {
1008 ASSERT(allow_stub_calls()); // stub calls are not allowed in some stubs
1009 Call(stub->GetCode(), RelocInfo::CODE_TARGET, cond);
1010}
1011
1012
1013void MacroAssembler::StubReturn(int argc) {
1014 ASSERT(argc >= 1 && generating_stub());
1015 if (argc > 1)
1016 add(sp, sp, Operand((argc - 1) * kPointerSize));
1017 Ret();
1018}
1019
1020
1021void MacroAssembler::IllegalOperation(int num_arguments) {
1022 if (num_arguments > 0) {
1023 add(sp, sp, Operand(num_arguments * kPointerSize));
1024 }
1025 LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1026}
1027
1028
Steve Blockd0582a62009-12-15 09:54:21 +00001029void MacroAssembler::IntegerToDoubleConversionWithVFP3(Register inReg,
1030 Register outHighReg,
1031 Register outLowReg) {
1032 // ARMv7 VFP3 instructions to implement integer to double conversion.
1033 mov(r7, Operand(inReg, ASR, kSmiTagSize));
Leon Clarkee46be812010-01-19 14:06:41 +00001034 vmov(s15, r7);
1035 vcvt(d7, s15);
1036 vmov(outLowReg, outHighReg, d7);
Steve Blockd0582a62009-12-15 09:54:21 +00001037}
1038
1039
Steve Blocka7e24c12009-10-30 11:49:00 +00001040void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) {
1041 // All parameters are on the stack. r0 has the return value after call.
1042
1043 // If the expected number of arguments of the runtime function is
1044 // constant, we check that the actual number of arguments match the
1045 // expectation.
1046 if (f->nargs >= 0 && f->nargs != num_arguments) {
1047 IllegalOperation(num_arguments);
1048 return;
1049 }
1050
Leon Clarke4515c472010-02-03 11:58:03 +00001051 // TODO(1236192): Most runtime routines don't need the number of
1052 // arguments passed in because it is constant. At some point we
1053 // should remove this need and make the runtime routine entry code
1054 // smarter.
1055 mov(r0, Operand(num_arguments));
1056 mov(r1, Operand(ExternalReference(f)));
1057 CEntryStub stub(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001058 CallStub(&stub);
1059}
1060
1061
1062void MacroAssembler::CallRuntime(Runtime::FunctionId fid, int num_arguments) {
1063 CallRuntime(Runtime::FunctionForId(fid), num_arguments);
1064}
1065
1066
1067void MacroAssembler::TailCallRuntime(const ExternalReference& ext,
1068 int num_arguments,
1069 int result_size) {
1070 // TODO(1236192): Most runtime routines don't need the number of
1071 // arguments passed in because it is constant. At some point we
1072 // should remove this need and make the runtime routine entry code
1073 // smarter.
1074 mov(r0, Operand(num_arguments));
1075 JumpToRuntime(ext);
1076}
1077
1078
1079void MacroAssembler::JumpToRuntime(const ExternalReference& builtin) {
1080#if defined(__thumb__)
1081 // Thumb mode builtin.
1082 ASSERT((reinterpret_cast<intptr_t>(builtin.address()) & 1) == 1);
1083#endif
1084 mov(r1, Operand(builtin));
1085 CEntryStub stub(1);
1086 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1087}
1088
1089
1090Handle<Code> MacroAssembler::ResolveBuiltin(Builtins::JavaScript id,
1091 bool* resolved) {
1092 // Contract with compiled functions is that the function is passed in r1.
1093 int builtins_offset =
1094 JSBuiltinsObject::kJSBuiltinsOffset + (id * kPointerSize);
1095 ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
1096 ldr(r1, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
1097 ldr(r1, FieldMemOperand(r1, builtins_offset));
1098
1099 return Builtins::GetCode(id, resolved);
1100}
1101
1102
1103void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
1104 InvokeJSFlags flags) {
1105 bool resolved;
1106 Handle<Code> code = ResolveBuiltin(id, &resolved);
1107
1108 if (flags == CALL_JS) {
1109 Call(code, RelocInfo::CODE_TARGET);
1110 } else {
1111 ASSERT(flags == JUMP_JS);
1112 Jump(code, RelocInfo::CODE_TARGET);
1113 }
1114
1115 if (!resolved) {
1116 const char* name = Builtins::GetName(id);
1117 int argc = Builtins::GetArgumentsCount(id);
1118 uint32_t flags =
1119 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
Steve Blocka7e24c12009-10-30 11:49:00 +00001120 Bootstrapper::FixupFlagsUseCodeObject::encode(false);
1121 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
1122 unresolved_.Add(entry);
1123 }
1124}
1125
1126
1127void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
1128 bool resolved;
1129 Handle<Code> code = ResolveBuiltin(id, &resolved);
1130
1131 mov(target, Operand(code));
1132 if (!resolved) {
1133 const char* name = Builtins::GetName(id);
1134 int argc = Builtins::GetArgumentsCount(id);
1135 uint32_t flags =
1136 Bootstrapper::FixupFlagsArgumentsCount::encode(argc) |
Steve Blocka7e24c12009-10-30 11:49:00 +00001137 Bootstrapper::FixupFlagsUseCodeObject::encode(true);
1138 Unresolved entry = { pc_offset() - kInstrSize, flags, name };
1139 unresolved_.Add(entry);
1140 }
1141
1142 add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
1143}
1144
1145
1146void MacroAssembler::SetCounter(StatsCounter* counter, int value,
1147 Register scratch1, Register scratch2) {
1148 if (FLAG_native_code_counters && counter->Enabled()) {
1149 mov(scratch1, Operand(value));
1150 mov(scratch2, Operand(ExternalReference(counter)));
1151 str(scratch1, MemOperand(scratch2));
1152 }
1153}
1154
1155
1156void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
1157 Register scratch1, Register scratch2) {
1158 ASSERT(value > 0);
1159 if (FLAG_native_code_counters && counter->Enabled()) {
1160 mov(scratch2, Operand(ExternalReference(counter)));
1161 ldr(scratch1, MemOperand(scratch2));
1162 add(scratch1, scratch1, Operand(value));
1163 str(scratch1, MemOperand(scratch2));
1164 }
1165}
1166
1167
1168void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
1169 Register scratch1, Register scratch2) {
1170 ASSERT(value > 0);
1171 if (FLAG_native_code_counters && counter->Enabled()) {
1172 mov(scratch2, Operand(ExternalReference(counter)));
1173 ldr(scratch1, MemOperand(scratch2));
1174 sub(scratch1, scratch1, Operand(value));
1175 str(scratch1, MemOperand(scratch2));
1176 }
1177}
1178
1179
1180void MacroAssembler::Assert(Condition cc, const char* msg) {
1181 if (FLAG_debug_code)
1182 Check(cc, msg);
1183}
1184
1185
1186void MacroAssembler::Check(Condition cc, const char* msg) {
1187 Label L;
1188 b(cc, &L);
1189 Abort(msg);
1190 // will not return here
1191 bind(&L);
1192}
1193
1194
1195void MacroAssembler::Abort(const char* msg) {
1196 // We want to pass the msg string like a smi to avoid GC
1197 // problems, however msg is not guaranteed to be aligned
1198 // properly. Instead, we pass an aligned pointer that is
1199 // a proper v8 smi, but also pass the alignment difference
1200 // from the real pointer as a smi.
1201 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
1202 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
1203 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
1204#ifdef DEBUG
1205 if (msg != NULL) {
1206 RecordComment("Abort message: ");
1207 RecordComment(msg);
1208 }
1209#endif
Steve Blockd0582a62009-12-15 09:54:21 +00001210 // Disable stub call restrictions to always allow calls to abort.
1211 set_allow_stub_calls(true);
1212
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 mov(r0, Operand(p0));
1214 push(r0);
1215 mov(r0, Operand(Smi::FromInt(p1 - p0)));
1216 push(r0);
1217 CallRuntime(Runtime::kAbort, 2);
1218 // will not return here
1219}
1220
1221
Steve Blockd0582a62009-12-15 09:54:21 +00001222void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
1223 if (context_chain_length > 0) {
1224 // Move up the chain of contexts to the context containing the slot.
1225 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::CLOSURE_INDEX)));
1226 // Load the function context (which is the incoming, outer context).
1227 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1228 for (int i = 1; i < context_chain_length; i++) {
1229 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::CLOSURE_INDEX)));
1230 ldr(dst, FieldMemOperand(dst, JSFunction::kContextOffset));
1231 }
1232 // The context may be an intermediate context, not a function context.
1233 ldr(dst, MemOperand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1234 } else { // Slot is in the current function context.
1235 // The context may be an intermediate context, not a function context.
1236 ldr(dst, MemOperand(cp, Context::SlotOffset(Context::FCONTEXT_INDEX)));
1237 }
1238}
1239
1240
Leon Clarked91b9f72010-01-27 17:25:45 +00001241void MacroAssembler::JumpIfNonSmisNotBothSequentialAsciiStrings(
1242 Register first,
1243 Register second,
1244 Register scratch1,
1245 Register scratch2,
1246 Label* failure) {
1247 // Test that both first and second are sequential ASCII strings.
1248 // Assume that they are non-smis.
1249 ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
1250 ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
1251 ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
1252 ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
1253 int kFlatAsciiStringMask =
1254 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
1255 int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1256 and_(scratch1, scratch1, Operand(kFlatAsciiStringMask));
1257 and_(scratch2, scratch2, Operand(kFlatAsciiStringMask));
1258 cmp(scratch1, Operand(kFlatAsciiStringTag));
1259 // Ignore second test if first test failed.
1260 cmp(scratch2, Operand(kFlatAsciiStringTag), eq);
1261 b(ne, failure);
1262}
1263
1264void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first,
1265 Register second,
1266 Register scratch1,
1267 Register scratch2,
1268 Label* failure) {
1269 // Check that neither is a smi.
1270 ASSERT_EQ(0, kSmiTag);
1271 and_(scratch1, first, Operand(second));
1272 tst(scratch1, Operand(kSmiTagMask));
1273 b(eq, failure);
1274 JumpIfNonSmisNotBothSequentialAsciiStrings(first,
1275 second,
1276 scratch1,
1277 scratch2,
1278 failure);
1279}
1280
Steve Blockd0582a62009-12-15 09:54:21 +00001281
Steve Blocka7e24c12009-10-30 11:49:00 +00001282#ifdef ENABLE_DEBUGGER_SUPPORT
1283CodePatcher::CodePatcher(byte* address, int instructions)
1284 : address_(address),
1285 instructions_(instructions),
1286 size_(instructions * Assembler::kInstrSize),
1287 masm_(address, size_ + Assembler::kGap) {
1288 // Create a new macro assembler pointing to the address of the code to patch.
1289 // The size is adjusted with kGap on order for the assembler to generate size
1290 // bytes of instructions without failing with buffer size constraints.
1291 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1292}
1293
1294
1295CodePatcher::~CodePatcher() {
1296 // Indicate that code has changed.
1297 CPU::FlushICache(address_, size_);
1298
1299 // Check that the code was patched as expected.
1300 ASSERT(masm_.pc_ == address_ + size_);
1301 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
1302}
1303
1304
1305void CodePatcher::Emit(Instr x) {
1306 masm()->emit(x);
1307}
1308
1309
1310void CodePatcher::Emit(Address addr) {
1311 masm()->emit(reinterpret_cast<Instr>(addr));
1312}
1313#endif // ENABLE_DEBUGGER_SUPPORT
1314
1315
1316} } // namespace v8::internal