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 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_IA32
8
9 #include "src/codegen.h"
10 #include "src/debug.h"
11
12
13 namespace v8 {
14 namespace internal {
15
IsDebugBreakAtReturn()16 bool BreakLocationIterator::IsDebugBreakAtReturn() {
17 return Debug::IsDebugBreakAtReturn(rinfo());
18 }
19
20
21 // Patch the JS frame exit code with a debug break call. See
22 // CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-ia32.cc
23 // for the precise return instructions sequence.
SetDebugBreakAtReturn()24 void BreakLocationIterator::SetDebugBreakAtReturn() {
25 ASSERT(Assembler::kJSReturnSequenceLength >=
26 Assembler::kCallInstructionLength);
27 rinfo()->PatchCodeWithCall(
28 debug_info_->GetIsolate()->builtins()->Return_DebugBreak()->entry(),
29 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
30 }
31
32
33 // Restore the JS frame exit code.
ClearDebugBreakAtReturn()34 void BreakLocationIterator::ClearDebugBreakAtReturn() {
35 rinfo()->PatchCode(original_rinfo()->pc(),
36 Assembler::kJSReturnSequenceLength);
37 }
38
39
40 // A debug break in the frame exit code is identified by the JS frame exit code
41 // having been patched with a call instruction.
IsDebugBreakAtReturn(RelocInfo * rinfo)42 bool Debug::IsDebugBreakAtReturn(RelocInfo* rinfo) {
43 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
44 return rinfo->IsPatchedReturnSequence();
45 }
46
47
IsDebugBreakAtSlot()48 bool BreakLocationIterator::IsDebugBreakAtSlot() {
49 ASSERT(IsDebugBreakSlot());
50 // Check whether the debug break slot instructions have been patched.
51 return rinfo()->IsPatchedDebugBreakSlotSequence();
52 }
53
54
SetDebugBreakAtSlot()55 void BreakLocationIterator::SetDebugBreakAtSlot() {
56 ASSERT(IsDebugBreakSlot());
57 Isolate* isolate = debug_info_->GetIsolate();
58 rinfo()->PatchCodeWithCall(
59 isolate->builtins()->Slot_DebugBreak()->entry(),
60 Assembler::kDebugBreakSlotLength - Assembler::kCallInstructionLength);
61 }
62
63
ClearDebugBreakAtSlot()64 void BreakLocationIterator::ClearDebugBreakAtSlot() {
65 ASSERT(IsDebugBreakSlot());
66 rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
67 }
68
69
70 #define __ ACCESS_MASM(masm)
71
Generate_DebugBreakCallHelper(MacroAssembler * masm,RegList object_regs,RegList non_object_regs,bool convert_call_to_jmp)72 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
73 RegList object_regs,
74 RegList non_object_regs,
75 bool convert_call_to_jmp) {
76 // Enter an internal frame.
77 {
78 FrameScope scope(masm, StackFrame::INTERNAL);
79
80 // Load padding words on stack.
81 for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
82 __ push(Immediate(Smi::FromInt(LiveEdit::kFramePaddingValue)));
83 }
84 __ push(Immediate(Smi::FromInt(LiveEdit::kFramePaddingInitialSize)));
85
86 // Store the registers containing live values on the expression stack to
87 // make sure that these are correctly updated during GC. Non object values
88 // are stored as a smi causing it to be untouched by GC.
89 ASSERT((object_regs & ~kJSCallerSaved) == 0);
90 ASSERT((non_object_regs & ~kJSCallerSaved) == 0);
91 ASSERT((object_regs & non_object_regs) == 0);
92 for (int i = 0; i < kNumJSCallerSaved; i++) {
93 int r = JSCallerSavedCode(i);
94 Register reg = { r };
95 if ((object_regs & (1 << r)) != 0) {
96 __ push(reg);
97 }
98 if ((non_object_regs & (1 << r)) != 0) {
99 if (FLAG_debug_code) {
100 __ test(reg, Immediate(0xc0000000));
101 __ Assert(zero, kUnableToEncodeValueAsSmi);
102 }
103 __ SmiTag(reg);
104 __ push(reg);
105 }
106 }
107
108 #ifdef DEBUG
109 __ RecordComment("// Calling from debug break to runtime - come in - over");
110 #endif
111 __ Move(eax, Immediate(0)); // No arguments.
112 __ mov(ebx, Immediate(ExternalReference::debug_break(masm->isolate())));
113
114 CEntryStub ceb(masm->isolate(), 1);
115 __ CallStub(&ceb);
116
117 // Automatically find register that could be used after register restore.
118 // We need one register for padding skip instructions.
119 Register unused_reg = { -1 };
120
121 // Restore the register values containing object pointers from the
122 // expression stack.
123 for (int i = kNumJSCallerSaved; --i >= 0;) {
124 int r = JSCallerSavedCode(i);
125 Register reg = { r };
126 if (FLAG_debug_code) {
127 __ Move(reg, Immediate(kDebugZapValue));
128 }
129 bool taken = reg.code() == esi.code();
130 if ((object_regs & (1 << r)) != 0) {
131 __ pop(reg);
132 taken = true;
133 }
134 if ((non_object_regs & (1 << r)) != 0) {
135 __ pop(reg);
136 __ SmiUntag(reg);
137 taken = true;
138 }
139 if (!taken) {
140 unused_reg = reg;
141 }
142 }
143
144 ASSERT(unused_reg.code() != -1);
145
146 // Read current padding counter and skip corresponding number of words.
147 __ pop(unused_reg);
148 // We divide stored value by 2 (untagging) and multiply it by word's size.
149 STATIC_ASSERT(kSmiTagSize == 1 && kSmiShiftSize == 0);
150 __ lea(esp, Operand(esp, unused_reg, times_half_pointer_size, 0));
151
152 // Get rid of the internal frame.
153 }
154
155 // If this call did not replace a call but patched other code then there will
156 // be an unwanted return address left on the stack. Here we get rid of that.
157 if (convert_call_to_jmp) {
158 __ add(esp, Immediate(kPointerSize));
159 }
160
161 // Now that the break point has been handled, resume normal execution by
162 // jumping to the target address intended by the caller and that was
163 // overwritten by the address of DebugBreakXXX.
164 ExternalReference after_break_target =
165 ExternalReference::debug_after_break_target_address(masm->isolate());
166 __ jmp(Operand::StaticVariable(after_break_target));
167 }
168
169
GenerateCallICStubDebugBreak(MacroAssembler * masm)170 void DebugCodegen::GenerateCallICStubDebugBreak(MacroAssembler* masm) {
171 // Register state for CallICStub
172 // ----------- S t a t e -------------
173 // -- edx : type feedback slot (smi)
174 // -- edi : function
175 // -----------------------------------
176 Generate_DebugBreakCallHelper(masm, edx.bit() | edi.bit(),
177 0, false);
178 }
179
180
GenerateLoadICDebugBreak(MacroAssembler * masm)181 void DebugCodegen::GenerateLoadICDebugBreak(MacroAssembler* masm) {
182 // Register state for IC load call (from ic-ia32.cc).
183 // ----------- S t a t e -------------
184 // -- ecx : name
185 // -- edx : receiver
186 // -----------------------------------
187 Generate_DebugBreakCallHelper(masm, ecx.bit() | edx.bit(), 0, false);
188 }
189
190
GenerateStoreICDebugBreak(MacroAssembler * masm)191 void DebugCodegen::GenerateStoreICDebugBreak(MacroAssembler* masm) {
192 // Register state for IC store call (from ic-ia32.cc).
193 // ----------- S t a t e -------------
194 // -- eax : value
195 // -- ecx : name
196 // -- edx : receiver
197 // -----------------------------------
198 Generate_DebugBreakCallHelper(
199 masm, eax.bit() | ecx.bit() | edx.bit(), 0, false);
200 }
201
202
GenerateKeyedLoadICDebugBreak(MacroAssembler * masm)203 void DebugCodegen::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
204 // Register state for keyed IC load call (from ic-ia32.cc).
205 // ----------- S t a t e -------------
206 // -- ecx : key
207 // -- edx : receiver
208 // -----------------------------------
209 Generate_DebugBreakCallHelper(masm, ecx.bit() | edx.bit(), 0, false);
210 }
211
212
GenerateKeyedStoreICDebugBreak(MacroAssembler * masm)213 void DebugCodegen::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
214 // Register state for keyed IC load call (from ic-ia32.cc).
215 // ----------- S t a t e -------------
216 // -- eax : value
217 // -- ecx : key
218 // -- edx : receiver
219 // -----------------------------------
220 Generate_DebugBreakCallHelper(
221 masm, eax.bit() | ecx.bit() | edx.bit(), 0, false);
222 }
223
224
GenerateCompareNilICDebugBreak(MacroAssembler * masm)225 void DebugCodegen::GenerateCompareNilICDebugBreak(MacroAssembler* masm) {
226 // Register state for CompareNil IC
227 // ----------- S t a t e -------------
228 // -- eax : value
229 // -----------------------------------
230 Generate_DebugBreakCallHelper(masm, eax.bit(), 0, false);
231 }
232
233
GenerateReturnDebugBreak(MacroAssembler * masm)234 void DebugCodegen::GenerateReturnDebugBreak(MacroAssembler* masm) {
235 // Register state just before return from JS function (from codegen-ia32.cc).
236 // ----------- S t a t e -------------
237 // -- eax: return value
238 // -----------------------------------
239 Generate_DebugBreakCallHelper(masm, eax.bit(), 0, true);
240 }
241
242
GenerateCallFunctionStubDebugBreak(MacroAssembler * masm)243 void DebugCodegen::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
244 // Register state for CallFunctionStub (from code-stubs-ia32.cc).
245 // ----------- S t a t e -------------
246 // -- edi: function
247 // -----------------------------------
248 Generate_DebugBreakCallHelper(masm, edi.bit(), 0, false);
249 }
250
251
GenerateCallConstructStubDebugBreak(MacroAssembler * masm)252 void DebugCodegen::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
253 // Register state for CallConstructStub (from code-stubs-ia32.cc).
254 // eax is the actual number of arguments not encoded as a smi see comment
255 // above IC call.
256 // ----------- S t a t e -------------
257 // -- eax: number of arguments (not smi)
258 // -- edi: constructor function
259 // -----------------------------------
260 // The number of arguments in eax is not smi encoded.
261 Generate_DebugBreakCallHelper(masm, edi.bit(), eax.bit(), false);
262 }
263
264
GenerateCallConstructStubRecordDebugBreak(MacroAssembler * masm)265 void DebugCodegen::GenerateCallConstructStubRecordDebugBreak(
266 MacroAssembler* masm) {
267 // Register state for CallConstructStub (from code-stubs-ia32.cc).
268 // eax is the actual number of arguments not encoded as a smi see comment
269 // above IC call.
270 // ----------- S t a t e -------------
271 // -- eax: number of arguments (not smi)
272 // -- ebx: feedback array
273 // -- edx: feedback slot (smi)
274 // -- edi: constructor function
275 // -----------------------------------
276 // The number of arguments in eax is not smi encoded.
277 Generate_DebugBreakCallHelper(masm, ebx.bit() | edx.bit() | edi.bit(),
278 eax.bit(), false);
279 }
280
281
GenerateSlot(MacroAssembler * masm)282 void DebugCodegen::GenerateSlot(MacroAssembler* masm) {
283 // Generate enough nop's to make space for a call instruction.
284 Label check_codesize;
285 __ bind(&check_codesize);
286 __ RecordDebugBreakSlot();
287 __ Nop(Assembler::kDebugBreakSlotLength);
288 ASSERT_EQ(Assembler::kDebugBreakSlotLength,
289 masm->SizeOfCodeGeneratedSince(&check_codesize));
290 }
291
292
GenerateSlotDebugBreak(MacroAssembler * masm)293 void DebugCodegen::GenerateSlotDebugBreak(MacroAssembler* masm) {
294 // In the places where a debug break slot is inserted no registers can contain
295 // object pointers.
296 Generate_DebugBreakCallHelper(masm, 0, 0, true);
297 }
298
299
GeneratePlainReturnLiveEdit(MacroAssembler * masm)300 void DebugCodegen::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
301 masm->ret(0);
302 }
303
304
GenerateFrameDropperLiveEdit(MacroAssembler * masm)305 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
306 ExternalReference restarter_frame_function_slot =
307 ExternalReference::debug_restarter_frame_function_pointer_address(
308 masm->isolate());
309 __ mov(Operand::StaticVariable(restarter_frame_function_slot), Immediate(0));
310
311 // We do not know our frame height, but set esp based on ebp.
312 __ lea(esp, Operand(ebp, -1 * kPointerSize));
313
314 __ pop(edi); // Function.
315 __ pop(ebp);
316
317 // Load context from the function.
318 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
319
320 // Get function code.
321 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
322 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
323 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
324
325 // Re-run JSFunction, edi is function, esi is context.
326 __ jmp(edx);
327 }
328
329
330 const bool LiveEdit::kFrameDropperSupported = true;
331
332 #undef __
333
334 } } // namespace v8::internal
335
336 #endif // V8_TARGET_ARCH_IA32
337