1014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Copyright 2012 the V8 project authors. All rights reserved.
2014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// found in the LICENSE file.
4014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#if V8_TARGET_ARCH_ARM
6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
7014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/codegen.h"
8014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/debug/debug.h"
9014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
10014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace v8 {
11014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace internal {
12014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
13014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#define __ ACCESS_MASM(masm)
14014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
15014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
16014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid EmitDebugBreakSlot(MacroAssembler* masm) {
17014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Label check_size;
18014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ bind(&check_size);
19014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  for (int i = 0; i < Assembler::kDebugBreakSlotInstructions; i++) {
20014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ nop(MacroAssembler::DEBUG_BREAK_NOP);
21014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
22014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  DCHECK_EQ(Assembler::kDebugBreakSlotInstructions,
23014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            masm->InstructionsGeneratedSince(&check_size));
24014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
25014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
26014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
27014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid DebugCodegen::GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode) {
28014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Generate enough nop's to make space for a call instruction. Avoid emitting
29014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // the constant pool in the debug break slot code.
30014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Assembler::BlockConstPoolScope block_const_pool(masm);
31014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  masm->RecordDebugBreakSlot(mode);
32014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  EmitDebugBreakSlot(masm);
33014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
34014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
35014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
36014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid DebugCodegen::ClearDebugBreakSlot(Isolate* isolate, Address pc) {
37014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotInstructions);
38014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  EmitDebugBreakSlot(patcher.masm());
39014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
40014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
41014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
42014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid DebugCodegen::PatchDebugBreakSlot(Isolate* isolate, Address pc,
43014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                       Handle<Code> code) {
4421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  DCHECK(code->is_debug_stub());
45014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotInstructions);
46014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Patch the code changing the debug break slot code from
47014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   mov r2, r2
48014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   mov r2, r2
49014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   mov r2, r2
50014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   mov r2, r2
51014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // to a call to the debug break slot code.
52014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   ldr ip, [pc, #0]
53014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   b skip
54014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   <debug break slot code entry point address>
55014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   skip:
56014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   blx ip
57014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Label skip_constant;
58014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  patcher.masm()->ldr(ip, MemOperand(v8::internal::pc, 0));
59014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  patcher.masm()->b(&skip_constant);
60014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  patcher.Emit(code->entry());
61014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  patcher.masm()->bind(&skip_constant);
62014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  patcher.masm()->blx(ip);
63014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
64014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
65342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochbool DebugCodegen::DebugBreakSlotIsPatched(Address pc) {
66342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  Instr current_instr = Assembler::instr_at(pc);
67342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
68342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch}
69014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
70014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid DebugCodegen::GenerateDebugBreakStub(MacroAssembler* masm,
71014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                          DebugBreakCallHelperMode mode) {
72014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ RecordComment("Debug break");
73014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  {
74014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
75014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
76014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Load padding words on stack.
77014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ mov(ip, Operand(Smi::FromInt(LiveEdit::kFramePaddingValue)));
78014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
79014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      __ push(ip);
80014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
81014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ mov(ip, Operand(Smi::FromInt(LiveEdit::kFramePaddingInitialSize)));
82014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ push(ip);
83014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
841b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    // Push arguments for DebugBreak call.
851b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    if (mode == SAVE_RESULT_REGISTER) {
861b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      // Break on return.
871b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      __ push(r0);
881b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    } else {
891b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      // Non-return breaks.
901b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      __ Push(masm->isolate()->factory()->the_hole_value());
911b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    }
921b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    __ mov(r0, Operand(1));
93014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ mov(r1,
94014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch           Operand(ExternalReference(
95014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch               Runtime::FunctionForId(Runtime::kDebugBreak), masm->isolate())));
96014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
97014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    CEntryStub ceb(masm->isolate(), 1);
98014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ CallStub(&ceb);
99014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (FLAG_debug_code) {
101014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      for (int i = 0; i < kNumJSCallerSaved; i++) {
102014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        Register reg = {JSCallerSavedCode(i)};
1031b268ca467c924004286c97bac133db489cf43d0Ben Murdoch        // Do not clobber r0 if mode is SAVE_RESULT_REGISTER. It will
1041b268ca467c924004286c97bac133db489cf43d0Ben Murdoch        // contain return value of the function.
1051b268ca467c924004286c97bac133db489cf43d0Ben Murdoch        if (!(reg.is(r0) && (mode == SAVE_RESULT_REGISTER))) {
1061b268ca467c924004286c97bac133db489cf43d0Ben Murdoch          __ mov(reg, Operand(kDebugZapValue));
1071b268ca467c924004286c97bac133db489cf43d0Ben Murdoch        }
108014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
109014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
110014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
111014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Don't bother removing padding bytes pushed on the stack
112014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // as the frame is going to be restored right away.
113014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
114014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Leave the internal frame.
115014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
116014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
117014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Now that the break point has been handled, resume normal execution by
118014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // jumping to the target address intended by the caller and that was
119014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // overwritten by the address of DebugBreakXXX.
120014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ExternalReference after_break_target =
121014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      ExternalReference::debug_after_break_target_address(masm->isolate());
122014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ mov(ip, Operand(after_break_target));
123014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ ldr(ip, MemOperand(ip));
124014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ Jump(ip);
125014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
126014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
127014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
129014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Load the function pointer off of our current stack frame.
1301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  __ ldr(r1, MemOperand(fp, FrameDropperFrameConstants::kFunctionOffset));
131014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
132014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Pop return address, frame and constant pool pointer (if
133014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // FLAG_enable_embedded_constant_pool).
134014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ LeaveFrame(StackFrame::INTERNAL);
135014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
136014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ParameterCount dummy(0);
137014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  __ FloodFunctionIfStepping(r1, no_reg, dummy, dummy);
138014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
139014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  { ConstantPoolUnavailableScope constant_pool_unavailable(masm);
140014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Load context from the function.
141014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
142014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
143014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Clear new.target as a safety measure.
144014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
145014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
146014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Get function code.
147014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ ldr(ip, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
148014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ ldr(ip, FieldMemOperand(ip, SharedFunctionInfo::kCodeOffset));
149014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ add(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
150014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
151014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Re-run JSFunction, r1 is function, cp is context.
152014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    __ Jump(ip);
153014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
154014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
155014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
156014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
157014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochconst bool LiveEdit::kFrameDropperSupported = true;
158014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
159014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#undef __
160014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace internal
162014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace v8
163014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
164014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#endif  // V8_TARGET_ARCH_ARM
165