Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / v8 / src / mips / debug-mips.cc
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
6
7 #include "src/v8.h"
8
9 #if V8_TARGET_ARCH_MIPS
10
11 #include "src/codegen.h"
12 #include "src/debug.h"
13
14 namespace v8 {
15 namespace internal {
16
17 bool BreakLocationIterator::IsDebugBreakAtReturn() {
18   return Debug::IsDebugBreakAtReturn(rinfo());
19 }
20
21
22 void BreakLocationIterator::SetDebugBreakAtReturn() {
23   // Mips return sequence:
24   // mov sp, fp
25   // lw fp, sp(0)
26   // lw ra, sp(4)
27   // addiu sp, sp, 8
28   // addiu sp, sp, N
29   // jr ra
30   // nop (in branch delay slot)
31
32   // Make sure this constant matches the number if instrucntions we emit.
33   ASSERT(Assembler::kJSReturnSequenceInstructions == 7);
34   CodePatcher patcher(rinfo()->pc(), Assembler::kJSReturnSequenceInstructions);
35   // li and Call pseudo-instructions emit two instructions each.
36   patcher.masm()->li(v8::internal::t9, Operand(reinterpret_cast<int32_t>(
37       debug_info_->GetIsolate()->builtins()->Return_DebugBreak()->entry())));
38   patcher.masm()->Call(v8::internal::t9);
39   patcher.masm()->nop();
40   patcher.masm()->nop();
41   patcher.masm()->nop();
42
43   // TODO(mips): Open issue about using breakpoint instruction instead of nops.
44   // patcher.masm()->bkpt(0);
45 }
46
47
48 // Restore the JS frame exit code.
49 void BreakLocationIterator::ClearDebugBreakAtReturn() {
50   rinfo()->PatchCode(original_rinfo()->pc(),
51                      Assembler::kJSReturnSequenceInstructions);
52 }
53
54
55 // A debug break in the exit code is identified by the JS frame exit code
56 // having been patched with li/call psuedo-instrunction (liu/ori/jalr).
57 bool Debug::IsDebugBreakAtReturn(RelocInfo* rinfo) {
58   ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
59   return rinfo->IsPatchedReturnSequence();
60 }
61
62
63 bool BreakLocationIterator::IsDebugBreakAtSlot() {
64   ASSERT(IsDebugBreakSlot());
65   // Check whether the debug break slot instructions have been patched.
66   return rinfo()->IsPatchedDebugBreakSlotSequence();
67 }
68
69
70 void BreakLocationIterator::SetDebugBreakAtSlot() {
71   ASSERT(IsDebugBreakSlot());
72   // Patch the code changing the debug break slot code from:
73   //   nop(DEBUG_BREAK_NOP) - nop(1) is sll(zero_reg, zero_reg, 1)
74   //   nop(DEBUG_BREAK_NOP)
75   //   nop(DEBUG_BREAK_NOP)
76   //   nop(DEBUG_BREAK_NOP)
77   // to a call to the debug break slot code.
78   //   li t9, address   (lui t9 / ori t9 instruction pair)
79   //   call t9          (jalr t9 / nop instruction pair)
80   CodePatcher patcher(rinfo()->pc(), Assembler::kDebugBreakSlotInstructions);
81   patcher.masm()->li(v8::internal::t9, Operand(reinterpret_cast<int32_t>(
82       debug_info_->GetIsolate()->builtins()->Slot_DebugBreak()->entry())));
83   patcher.masm()->Call(v8::internal::t9);
84 }
85
86
87 void BreakLocationIterator::ClearDebugBreakAtSlot() {
88   ASSERT(IsDebugBreakSlot());
89   rinfo()->PatchCode(original_rinfo()->pc(),
90                      Assembler::kDebugBreakSlotInstructions);
91 }
92
93
94 #define __ ACCESS_MASM(masm)
95
96
97
98 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
99                                           RegList object_regs,
100                                           RegList non_object_regs) {
101   {
102     FrameScope scope(masm, StackFrame::INTERNAL);
103
104     // Store the registers containing live values on the expression stack to
105     // make sure that these are correctly updated during GC. Non object values
106     // are stored as a smi causing it to be untouched by GC.
107     ASSERT((object_regs & ~kJSCallerSaved) == 0);
108     ASSERT((non_object_regs & ~kJSCallerSaved) == 0);
109     ASSERT((object_regs & non_object_regs) == 0);
110     if ((object_regs | non_object_regs) != 0) {
111       for (int i = 0; i < kNumJSCallerSaved; i++) {
112         int r = JSCallerSavedCode(i);
113         Register reg = { r };
114         if ((non_object_regs & (1 << r)) != 0) {
115           if (FLAG_debug_code) {
116             __ And(at, reg, 0xc0000000);
117             __ Assert(eq, kUnableToEncodeValueAsSmi, at, Operand(zero_reg));
118           }
119           __ sll(reg, reg, kSmiTagSize);
120         }
121       }
122       __ MultiPush(object_regs | non_object_regs);
123     }
124
125 #ifdef DEBUG
126     __ RecordComment("// Calling from debug break to runtime - come in - over");
127 #endif
128     __ PrepareCEntryArgs(0);  // No arguments.
129     __ PrepareCEntryFunction(ExternalReference::debug_break(masm->isolate()));
130
131     CEntryStub ceb(masm->isolate(), 1);
132     __ CallStub(&ceb);
133
134     // Restore the register values from the expression stack.
135     if ((object_regs | non_object_regs) != 0) {
136       __ MultiPop(object_regs | non_object_regs);
137       for (int i = 0; i < kNumJSCallerSaved; i++) {
138         int r = JSCallerSavedCode(i);
139         Register reg = { r };
140         if ((non_object_regs & (1 << r)) != 0) {
141           __ srl(reg, reg, kSmiTagSize);
142         }
143         if (FLAG_debug_code &&
144             (((object_regs |non_object_regs) & (1 << r)) == 0)) {
145           __ li(reg, kDebugZapValue);
146         }
147       }
148     }
149
150     // Leave the internal frame.
151   }
152
153   // Now that the break point has been handled, resume normal execution by
154   // jumping to the target address intended by the caller and that was
155   // overwritten by the address of DebugBreakXXX.
156   ExternalReference after_break_target =
157       ExternalReference::debug_after_break_target_address(masm->isolate());
158   __ li(t9, Operand(after_break_target));
159   __ lw(t9, MemOperand(t9));
160   __ Jump(t9);
161 }
162
163
164 void DebugCodegen::GenerateCallICStubDebugBreak(MacroAssembler* masm) {
165   // Register state for CallICStub
166   // ----------- S t a t e -------------
167   //  -- a1 : function
168   //  -- a3 : slot in feedback array (smi)
169   // -----------------------------------
170   Generate_DebugBreakCallHelper(masm, a1.bit() | a3.bit(), 0);
171 }
172
173
174 void DebugCodegen::GenerateLoadICDebugBreak(MacroAssembler* masm) {
175   // Calling convention for IC load (from ic-mips.cc).
176   // ----------- S t a t e -------------
177   //  -- a2    : name
178   //  -- ra    : return address
179   //  -- a0    : receiver
180   //  -- [sp]  : receiver
181   // -----------------------------------
182   // Registers a0 and a2 contain objects that need to be pushed on the
183   // expression stack of the fake JS frame.
184   Generate_DebugBreakCallHelper(masm, a0.bit() | a2.bit(), 0);
185 }
186
187
188 void DebugCodegen::GenerateStoreICDebugBreak(MacroAssembler* masm) {
189   // Calling convention for IC store (from ic-mips.cc).
190   // ----------- S t a t e -------------
191   //  -- a0    : value
192   //  -- a1    : receiver
193   //  -- a2    : name
194   //  -- ra    : return address
195   // -----------------------------------
196   // Registers a0, a1, and a2 contain objects that need to be pushed on the
197   // expression stack of the fake JS frame.
198   Generate_DebugBreakCallHelper(masm, a0.bit() | a1.bit() | a2.bit(), 0);
199 }
200
201
202 void DebugCodegen::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
203   // ---------- S t a t e --------------
204   //  -- ra  : return address
205   //  -- a0  : key
206   //  -- a1  : receiver
207   Generate_DebugBreakCallHelper(masm, a0.bit() | a1.bit(), 0);
208 }
209
210
211 void DebugCodegen::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
212   // ---------- S t a t e --------------
213   //  -- a0     : value
214   //  -- a1     : key
215   //  -- a2     : receiver
216   //  -- ra     : return address
217   Generate_DebugBreakCallHelper(masm, a0.bit() | a1.bit() | a2.bit(), 0);
218 }
219
220
221 void DebugCodegen::GenerateCompareNilICDebugBreak(MacroAssembler* masm) {
222   // Register state for CompareNil IC
223   // ----------- S t a t e -------------
224   //  -- a0    : value
225   // -----------------------------------
226   Generate_DebugBreakCallHelper(masm, a0.bit(), 0);
227 }
228
229
230 void DebugCodegen::GenerateReturnDebugBreak(MacroAssembler* masm) {
231   // In places other than IC call sites it is expected that v0 is TOS which
232   // is an object - this is not generally the case so this should be used with
233   // care.
234   Generate_DebugBreakCallHelper(masm, v0.bit(), 0);
235 }
236
237
238 void DebugCodegen::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
239   // Register state for CallFunctionStub (from code-stubs-mips.cc).
240   // ----------- S t a t e -------------
241   //  -- a1 : function
242   // -----------------------------------
243   Generate_DebugBreakCallHelper(masm, a1.bit(), 0);
244 }
245
246
247 void DebugCodegen::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
248   // Calling convention for CallConstructStub (from code-stubs-mips.cc).
249   // ----------- S t a t e -------------
250   //  -- a0     : number of arguments (not smi)
251   //  -- a1     : constructor function
252   // -----------------------------------
253   Generate_DebugBreakCallHelper(masm, a1.bit() , a0.bit());
254 }
255
256
257 void DebugCodegen::GenerateCallConstructStubRecordDebugBreak(
258     MacroAssembler* masm) {
259   // Calling convention for CallConstructStub (from code-stubs-mips.cc).
260   // ----------- S t a t e -------------
261   //  -- a0     : number of arguments (not smi)
262   //  -- a1     : constructor function
263   //  -- a2     : feedback array
264   //  -- a3     : feedback slot (smi)
265   // -----------------------------------
266   Generate_DebugBreakCallHelper(masm, a1.bit() | a2.bit() | a3.bit(), a0.bit());
267 }
268
269
270 void DebugCodegen::GenerateSlot(MacroAssembler* masm) {
271   // Generate enough nop's to make space for a call instruction. Avoid emitting
272   // the trampoline pool in the debug break slot code.
273   Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
274   Label check_codesize;
275   __ bind(&check_codesize);
276   __ RecordDebugBreakSlot();
277   for (int i = 0; i < Assembler::kDebugBreakSlotInstructions; i++) {
278     __ nop(MacroAssembler::DEBUG_BREAK_NOP);
279   }
280   ASSERT_EQ(Assembler::kDebugBreakSlotInstructions,
281             masm->InstructionsGeneratedSince(&check_codesize));
282 }
283
284
285 void DebugCodegen::GenerateSlotDebugBreak(MacroAssembler* masm) {
286   // In the places where a debug break slot is inserted no registers can contain
287   // object pointers.
288   Generate_DebugBreakCallHelper(masm, 0, 0);
289 }
290
291
292 void DebugCodegen::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
293   masm->Abort(kLiveEditFrameDroppingIsNotSupportedOnMips);
294 }
295
296
297 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
298   masm->Abort(kLiveEditFrameDroppingIsNotSupportedOnMips);
299 }
300
301
302 const bool LiveEdit::kFrameDropperSupported = false;
303
304 #undef __
305
306 } }  // namespace v8::internal
307
308 #endif  // V8_TARGET_ARCH_MIPS