1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if V8_TARGET_ARCH_ARM
6
7#include "src/codegen.h"
8#include "src/debug/debug.h"
9
10namespace v8 {
11namespace internal {
12
13#define __ ACCESS_MASM(masm)
14
15
16void EmitDebugBreakSlot(MacroAssembler* masm) {
17  Label check_size;
18  __ bind(&check_size);
19  for (int i = 0; i < Assembler::kDebugBreakSlotInstructions; i++) {
20    __ nop(MacroAssembler::DEBUG_BREAK_NOP);
21  }
22  DCHECK_EQ(Assembler::kDebugBreakSlotInstructions,
23            masm->InstructionsGeneratedSince(&check_size));
24}
25
26
27void DebugCodegen::GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode) {
28  // Generate enough nop's to make space for a call instruction. Avoid emitting
29  // the constant pool in the debug break slot code.
30  Assembler::BlockConstPoolScope block_const_pool(masm);
31  masm->RecordDebugBreakSlot(mode);
32  EmitDebugBreakSlot(masm);
33}
34
35
36void DebugCodegen::ClearDebugBreakSlot(Isolate* isolate, Address pc) {
37  CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotInstructions);
38  EmitDebugBreakSlot(patcher.masm());
39}
40
41
42void DebugCodegen::PatchDebugBreakSlot(Isolate* isolate, Address pc,
43                                       Handle<Code> code) {
44  DCHECK_EQ(Code::BUILTIN, code->kind());
45  CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotInstructions);
46  // Patch the code changing the debug break slot code from
47  //   mov r2, r2
48  //   mov r2, r2
49  //   mov r2, r2
50  //   mov r2, r2
51  // to a call to the debug break slot code.
52  //   ldr ip, [pc, #0]
53  //   b skip
54  //   <debug break slot code entry point address>
55  //   skip:
56  //   blx ip
57  Label skip_constant;
58  patcher.masm()->ldr(ip, MemOperand(v8::internal::pc, 0));
59  patcher.masm()->b(&skip_constant);
60  patcher.Emit(code->entry());
61  patcher.masm()->bind(&skip_constant);
62  patcher.masm()->blx(ip);
63}
64
65
66void DebugCodegen::GenerateDebugBreakStub(MacroAssembler* masm,
67                                          DebugBreakCallHelperMode mode) {
68  __ RecordComment("Debug break");
69  {
70    FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
71
72    // Load padding words on stack.
73    __ mov(ip, Operand(Smi::FromInt(LiveEdit::kFramePaddingValue)));
74    for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
75      __ push(ip);
76    }
77    __ mov(ip, Operand(Smi::FromInt(LiveEdit::kFramePaddingInitialSize)));
78    __ push(ip);
79
80    if (mode == SAVE_RESULT_REGISTER) __ push(r0);
81
82    __ mov(r0, Operand::Zero());  // no arguments
83    __ mov(r1,
84           Operand(ExternalReference(
85               Runtime::FunctionForId(Runtime::kDebugBreak), masm->isolate())));
86
87    CEntryStub ceb(masm->isolate(), 1);
88    __ CallStub(&ceb);
89
90    if (FLAG_debug_code) {
91      for (int i = 0; i < kNumJSCallerSaved; i++) {
92        Register reg = {JSCallerSavedCode(i)};
93        __ mov(reg, Operand(kDebugZapValue));
94      }
95    }
96
97    if (mode == SAVE_RESULT_REGISTER) __ pop(r0);
98
99    // Don't bother removing padding bytes pushed on the stack
100    // as the frame is going to be restored right away.
101
102    // Leave the internal frame.
103  }
104
105  // Now that the break point has been handled, resume normal execution by
106  // jumping to the target address intended by the caller and that was
107  // overwritten by the address of DebugBreakXXX.
108  ExternalReference after_break_target =
109      ExternalReference::debug_after_break_target_address(masm->isolate());
110  __ mov(ip, Operand(after_break_target));
111  __ ldr(ip, MemOperand(ip));
112  __ Jump(ip);
113}
114
115
116void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
117  // Load the function pointer off of our current stack frame.
118  __ ldr(r1, MemOperand(fp,
119         StandardFrameConstants::kConstantPoolOffset - kPointerSize));
120
121  // Pop return address, frame and constant pool pointer (if
122  // FLAG_enable_embedded_constant_pool).
123  __ LeaveFrame(StackFrame::INTERNAL);
124
125  ParameterCount dummy(0);
126  __ FloodFunctionIfStepping(r1, no_reg, dummy, dummy);
127
128  { ConstantPoolUnavailableScope constant_pool_unavailable(masm);
129    // Load context from the function.
130    __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
131
132    // Clear new.target as a safety measure.
133    __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
134
135    // Get function code.
136    __ ldr(ip, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
137    __ ldr(ip, FieldMemOperand(ip, SharedFunctionInfo::kCodeOffset));
138    __ add(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
139
140    // Re-run JSFunction, r1 is function, cp is context.
141    __ Jump(ip);
142  }
143}
144
145
146const bool LiveEdit::kFrameDropperSupported = true;
147
148#undef __
149
150}  // namespace internal
151}  // namespace v8
152
153#endif  // V8_TARGET_ARCH_ARM
154