Home | History | Annotate | Download | only in x64
      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_X64
      8 
      9 #include "src/assembler.h"
     10 #include "src/codegen.h"
     11 #include "src/debug.h"
     12 
     13 
     14 namespace v8 {
     15 namespace internal {
     16 
     17 bool BreakLocationIterator::IsDebugBreakAtReturn()  {
     18   return Debug::IsDebugBreakAtReturn(rinfo());
     19 }
     20 
     21 
     22 // Patch the JS frame exit code with a debug break call. See
     23 // CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-x64.cc
     24 // for the precise return instructions sequence.
     25 void BreakLocationIterator::SetDebugBreakAtReturn()  {
     26   ASSERT(Assembler::kJSReturnSequenceLength >= Assembler::kCallSequenceLength);
     27   rinfo()->PatchCodeWithCall(
     28       debug_info_->GetIsolate()->builtins()->Return_DebugBreak()->entry(),
     29       Assembler::kJSReturnSequenceLength - Assembler::kCallSequenceLength);
     30 }
     31 
     32 
     33 // Restore the JS frame exit code.
     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.
     42 bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) {
     43   ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
     44   return rinfo->IsPatchedReturnSequence();
     45 }
     46 
     47 
     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 
     55 void BreakLocationIterator::SetDebugBreakAtSlot() {
     56   ASSERT(IsDebugBreakSlot());
     57   rinfo()->PatchCodeWithCall(
     58       debug_info_->GetIsolate()->builtins()->Slot_DebugBreak()->entry(),
     59       Assembler::kDebugBreakSlotLength - Assembler::kCallSequenceLength);
     60 }
     61 
     62 
     63 void BreakLocationIterator::ClearDebugBreakAtSlot() {
     64   ASSERT(IsDebugBreakSlot());
     65   rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
     66 }
     67 
     68 
     69 #define __ ACCESS_MASM(masm)
     70 
     71 
     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(Smi::FromInt(LiveEdit::kFramePaddingValue));
     83     }
     84     __ Push(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 as two smis 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       ASSERT(!reg.is(kScratchRegister));
     96       if ((object_regs & (1 << r)) != 0) {
     97         __ Push(reg);
     98       }
     99       if ((non_object_regs & (1 << r)) != 0) {
    100         __ PushRegisterAsTwoSmis(reg);
    101       }
    102     }
    103 
    104 #ifdef DEBUG
    105     __ RecordComment("// Calling from debug break to runtime - come in - over");
    106 #endif
    107     __ Set(rax, 0);  // No arguments (argc == 0).
    108     __ Move(rbx, ExternalReference::debug_break(masm->isolate()));
    109 
    110     CEntryStub ceb(masm->isolate(), 1);
    111     __ CallStub(&ceb);
    112 
    113     // Restore the register values from the expression stack.
    114     for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
    115       int r = JSCallerSavedCode(i);
    116       Register reg = { r };
    117       if (FLAG_debug_code) {
    118         __ Set(reg, kDebugZapValue);
    119       }
    120       if ((object_regs & (1 << r)) != 0) {
    121         __ Pop(reg);
    122       }
    123       // Reconstruct the 64-bit value from two smis.
    124       if ((non_object_regs & (1 << r)) != 0) {
    125         __ PopRegisterAsTwoSmis(reg);
    126       }
    127     }
    128 
    129     // Read current padding counter and skip corresponding number of words.
    130     __ Pop(kScratchRegister);
    131     __ SmiToInteger32(kScratchRegister, kScratchRegister);
    132     __ leap(rsp, Operand(rsp, kScratchRegister, times_pointer_size, 0));
    133 
    134     // Get rid of the internal frame.
    135   }
    136 
    137   // If this call did not replace a call but patched other code then there will
    138   // be an unwanted return address left on the stack. Here we get rid of that.
    139   if (convert_call_to_jmp) {
    140     __ addp(rsp, Immediate(kPCOnStackSize));
    141   }
    142 
    143   // Now that the break point has been handled, resume normal execution by
    144   // jumping to the target address intended by the caller and that was
    145   // overwritten by the address of DebugBreakXXX.
    146   ExternalReference after_break_target =
    147       ExternalReference::debug_after_break_target_address(masm->isolate());
    148   __ Move(kScratchRegister, after_break_target);
    149   __ Jump(Operand(kScratchRegister, 0));
    150 }
    151 
    152 
    153 void DebugCodegen::GenerateCallICStubDebugBreak(MacroAssembler* masm) {
    154   // Register state for CallICStub
    155   // ----------- S t a t e -------------
    156   //  -- rdx    : type feedback slot (smi)
    157   //  -- rdi    : function
    158   // -----------------------------------
    159   Generate_DebugBreakCallHelper(masm, rdx.bit() | rdi.bit(), 0, false);
    160 }
    161 
    162 
    163 void DebugCodegen::GenerateLoadICDebugBreak(MacroAssembler* masm) {
    164   // Register state for IC load call (from ic-x64.cc).
    165   // ----------- S t a t e -------------
    166   //  -- rax    : receiver
    167   //  -- rcx    : name
    168   // -----------------------------------
    169   Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), 0, false);
    170 }
    171 
    172 
    173 void DebugCodegen::GenerateStoreICDebugBreak(MacroAssembler* masm) {
    174   // Register state for IC store call (from ic-x64.cc).
    175   // ----------- S t a t e -------------
    176   //  -- rax    : value
    177   //  -- rcx    : name
    178   //  -- rdx    : receiver
    179   // -----------------------------------
    180   Generate_DebugBreakCallHelper(
    181       masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
    182 }
    183 
    184 
    185 void DebugCodegen::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
    186   // Register state for keyed IC load call (from ic-x64.cc).
    187   // ----------- S t a t e -------------
    188   //  -- rax     : key
    189   //  -- rdx     : receiver
    190   // -----------------------------------
    191   Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), 0, false);
    192 }
    193 
    194 
    195 void DebugCodegen::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
    196   // Register state for keyed IC load call (from ic-x64.cc).
    197   // ----------- S t a t e -------------
    198   //  -- rax    : value
    199   //  -- rcx    : key
    200   //  -- rdx    : receiver
    201   // -----------------------------------
    202   Generate_DebugBreakCallHelper(
    203       masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
    204 }
    205 
    206 
    207 void DebugCodegen::GenerateCompareNilICDebugBreak(MacroAssembler* masm) {
    208   // Register state for CompareNil IC
    209   // ----------- S t a t e -------------
    210   //  -- rax    : value
    211   // -----------------------------------
    212   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, false);
    213 }
    214 
    215 
    216 void DebugCodegen::GenerateReturnDebugBreak(MacroAssembler* masm) {
    217   // Register state just before return from JS function (from codegen-x64.cc).
    218   // ----------- S t a t e -------------
    219   //  -- rax: return value
    220   // -----------------------------------
    221   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true);
    222 }
    223 
    224 
    225 void DebugCodegen::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
    226   // Register state for CallFunctionStub (from code-stubs-x64.cc).
    227   // ----------- S t a t e -------------
    228   //  -- rdi : function
    229   // -----------------------------------
    230   Generate_DebugBreakCallHelper(masm, rdi.bit(), 0, false);
    231 }
    232 
    233 
    234 void DebugCodegen::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
    235   // Register state for CallConstructStub (from code-stubs-x64.cc).
    236   // rax is the actual number of arguments not encoded as a smi, see comment
    237   // above IC call.
    238   // ----------- S t a t e -------------
    239   //  -- rax: number of arguments
    240   // -----------------------------------
    241   // The number of arguments in rax is not smi encoded.
    242   Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false);
    243 }
    244 
    245 
    246 void DebugCodegen::GenerateCallConstructStubRecordDebugBreak(
    247     MacroAssembler* masm) {
    248   // Register state for CallConstructStub (from code-stubs-x64.cc).
    249   // rax is the actual number of arguments not encoded as a smi, see comment
    250   // above IC call.
    251   // ----------- S t a t e -------------
    252   //  -- rax: number of arguments
    253   //  -- rbx: feedback array
    254   //  -- rdx: feedback slot (smi)
    255   // -----------------------------------
    256   // The number of arguments in rax is not smi encoded.
    257   Generate_DebugBreakCallHelper(masm, rbx.bit() | rdx.bit() | rdi.bit(),
    258                                 rax.bit(), false);
    259 }
    260 
    261 
    262 void DebugCodegen::GenerateSlot(MacroAssembler* masm) {
    263   // Generate enough nop's to make space for a call instruction.
    264   Label check_codesize;
    265   __ bind(&check_codesize);
    266   __ RecordDebugBreakSlot();
    267   __ Nop(Assembler::kDebugBreakSlotLength);
    268   ASSERT_EQ(Assembler::kDebugBreakSlotLength,
    269             masm->SizeOfCodeGeneratedSince(&check_codesize));
    270 }
    271 
    272 
    273 void DebugCodegen::GenerateSlotDebugBreak(MacroAssembler* masm) {
    274   // In the places where a debug break slot is inserted no registers can contain
    275   // object pointers.
    276   Generate_DebugBreakCallHelper(masm, 0, 0, true);
    277 }
    278 
    279 
    280 void DebugCodegen::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
    281   masm->ret(0);
    282 }
    283 
    284 
    285 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
    286   ExternalReference restarter_frame_function_slot =
    287       ExternalReference::debug_restarter_frame_function_pointer_address(
    288           masm->isolate());
    289   __ Move(rax, restarter_frame_function_slot);
    290   __ movp(Operand(rax, 0), Immediate(0));
    291 
    292   // We do not know our frame height, but set rsp based on rbp.
    293   __ leap(rsp, Operand(rbp, -1 * kPointerSize));
    294 
    295   __ Pop(rdi);  // Function.
    296   __ popq(rbp);
    297 
    298   // Load context from the function.
    299   __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
    300 
    301   // Get function code.
    302   __ movp(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
    303   __ movp(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
    304   __ leap(rdx, FieldOperand(rdx, Code::kHeaderSize));
    305 
    306   // Re-run JSFunction, rdi is function, rsi is context.
    307   __ jmp(rdx);
    308 }
    309 
    310 const bool LiveEdit::kFrameDropperSupported = true;
    311 
    312 #undef __
    313 
    314 } }  // namespace v8::internal
    315 
    316 #endif  // V8_TARGET_ARCH_X64
    317