~ubuntu-branches/ubuntu/trusty/libv8/trusty

« back to all changes in this revision

Viewing changes to src/x64/deoptimizer-x64.cc

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2012-02-20 14:08:17 UTC
  • mfrom: (15.1.24 sid)
  • Revision ID: package-import@ubuntu.com-20120220140817-bsvmeoa4sxsj5hbz
Tags: 3.7.12.22-3
Fix mipsel build, allow test debug-step-3 to fail (non-crucial)

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 
43
43
 
44
44
int Deoptimizer::patch_size() {
45
 
  return MacroAssembler::kCallInstructionLength;
46
 
}
47
 
 
48
 
 
49
 
#ifdef DEBUG
50
 
// Overwrites code with int3 instructions.
51
 
static void ZapCodeRange(Address from, Address to) {
52
 
  CHECK(from <= to);
53
 
  int length = static_cast<int>(to - from);
54
 
  CodePatcher destroyer(from, length);
55
 
  while (length-- > 0) {
56
 
    destroyer.masm()->int3();
57
 
  }
58
 
}
59
 
#endif
60
 
 
61
 
 
62
 
// Iterate through the entries of a SafepointTable that corresponds to
63
 
// deoptimization points.
64
 
class SafepointTableDeoptimiztionEntryIterator {
65
 
 public:
66
 
  explicit SafepointTableDeoptimiztionEntryIterator(Code* code)
67
 
      : code_(code), table_(code), index_(-1), limit_(table_.length()) {
68
 
    FindNextIndex();
69
 
  }
70
 
 
71
 
  SafepointEntry Next(Address* pc) {
72
 
    if (index_ >= limit_) {
73
 
      *pc = NULL;
74
 
      return SafepointEntry();  // Invalid entry.
75
 
    }
76
 
    *pc = code_->instruction_start() + table_.GetPcOffset(index_);
77
 
    SafepointEntry entry = table_.GetEntry(index_);
78
 
    FindNextIndex();
79
 
    return entry;
80
 
  }
81
 
 
82
 
 private:
83
 
  void FindNextIndex() {
84
 
    ASSERT(index_ < limit_);
85
 
    while (++index_ < limit_) {
86
 
      if (table_.GetEntry(index_).deoptimization_index() !=
87
 
          Safepoint::kNoDeoptimizationIndex) {
88
 
        return;
89
 
      }
90
 
    }
91
 
  }
92
 
 
93
 
  Code* code_;
94
 
  SafepointTable table_;
95
 
  // Index of next deoptimization entry. If negative after calling
96
 
  // FindNextIndex, there are no more, and Next will return an invalid
97
 
  // SafepointEntry.
98
 
  int index_;
99
 
  // Table length.
100
 
  int limit_;
101
 
};
102
 
 
103
 
 
104
 
void Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code) {
105
 
  // TODO(1276): Implement.
 
45
  return Assembler::kCallInstructionLength;
106
46
}
107
47
 
108
48
 
119
59
  // code patching below, and is not needed any more.
120
60
  code->InvalidateRelocation();
121
61
 
122
 
  // For each return after a safepoint insert a absolute call to the
 
62
  // For each LLazyBailout instruction insert a absolute call to the
123
63
  // corresponding deoptimization entry, or a short call to an absolute
124
64
  // jump if space is short. The absolute jumps are put in a table just
125
65
  // before the safepoint table (space was allocated there when the Code
126
66
  // object was created, if necessary).
127
67
 
128
68
  Address instruction_start = function->code()->instruction_start();
129
 
  Address jump_table_address =
130
 
      instruction_start + function->code()->safepoint_table_offset();
131
 
#ifdef DEBUG
132
 
  Address previous_pc = instruction_start;
133
 
#endif
134
 
 
135
 
  SafepointTableDeoptimiztionEntryIterator deoptimizations(function->code());
136
 
  Address entry_pc = NULL;
137
 
 
138
 
  SafepointEntry current_entry = deoptimizations.Next(&entry_pc);
139
 
  while (current_entry.is_valid()) {
140
 
    int gap_code_size = current_entry.gap_code_size();
141
 
    unsigned deoptimization_index = current_entry.deoptimization_index();
142
 
 
143
 
#ifdef DEBUG
144
 
    // Destroy the code which is not supposed to run again.
145
 
    ZapCodeRange(previous_pc, entry_pc);
146
 
#endif
 
69
#ifdef DEBUG
 
70
  Address prev_call_address = NULL;
 
71
#endif
 
72
  DeoptimizationInputData* deopt_data =
 
73
      DeoptimizationInputData::cast(code->deoptimization_data());
 
74
  for (int i = 0; i < deopt_data->DeoptCount(); i++) {
 
75
    if (deopt_data->Pc(i)->value() == -1) continue;
147
76
    // Position where Call will be patched in.
148
 
    Address call_address = entry_pc + gap_code_size;
149
 
    // End of call instruction, if using a direct call to a 64-bit address.
150
 
    Address call_end_address =
151
 
        call_address + MacroAssembler::kCallInstructionLength;
152
 
 
153
 
    // Find next deoptimization entry, if any.
154
 
    Address next_pc = NULL;
155
 
    SafepointEntry next_entry = deoptimizations.Next(&next_pc);
156
 
 
157
 
    if (!next_entry.is_valid() || next_pc >= call_end_address) {
158
 
      // Room enough to write a long call instruction.
159
 
      CodePatcher patcher(call_address, Assembler::kCallInstructionLength);
160
 
      patcher.masm()->Call(GetDeoptimizationEntry(deoptimization_index, LAZY),
161
 
                           RelocInfo::NONE);
162
 
#ifdef DEBUG
163
 
      previous_pc = call_end_address;
164
 
#endif
165
 
    } else {
166
 
      // Not room enough for a long Call instruction. Write a short call
167
 
      // instruction to a long jump placed elsewhere in the code.
168
 
#ifdef DEBUG
169
 
      Address short_call_end_address =
170
 
          call_address + MacroAssembler::kShortCallInstructionLength;
171
 
#endif
172
 
      ASSERT(next_pc >= short_call_end_address);
173
 
 
174
 
      // Write jump in jump-table.
175
 
      jump_table_address -= MacroAssembler::kJumpInstructionLength;
176
 
      CodePatcher jump_patcher(jump_table_address,
177
 
                               MacroAssembler::kJumpInstructionLength);
178
 
      jump_patcher.masm()->Jump(
179
 
          GetDeoptimizationEntry(deoptimization_index, LAZY),
180
 
          RelocInfo::NONE);
181
 
 
182
 
      // Write call to jump at call_offset.
183
 
      CodePatcher call_patcher(call_address,
184
 
                               MacroAssembler::kShortCallInstructionLength);
185
 
      call_patcher.masm()->call(jump_table_address);
186
 
#ifdef DEBUG
187
 
      previous_pc = short_call_end_address;
188
 
#endif
189
 
    }
190
 
 
191
 
    // Continue with next deoptimization entry.
192
 
    current_entry = next_entry;
193
 
    entry_pc = next_pc;
 
77
    Address call_address = instruction_start + deopt_data->Pc(i)->value();
 
78
    // There is room enough to write a long call instruction because we pad
 
79
    // LLazyBailout instructions with nops if necessary.
 
80
    CodePatcher patcher(call_address, Assembler::kCallInstructionLength);
 
81
    patcher.masm()->Call(GetDeoptimizationEntry(i, LAZY), RelocInfo::NONE);
 
82
    ASSERT(prev_call_address == NULL ||
 
83
           call_address >= prev_call_address + patch_size());
 
84
    ASSERT(call_address + patch_size() <= code->instruction_end());
 
85
#ifdef DEBUG
 
86
    prev_call_address = call_address;
 
87
#endif
194
88
  }
195
89
 
196
 
#ifdef DEBUG
197
 
  // Destroy the code which is not supposed to run again.
198
 
  ZapCodeRange(previous_pc, jump_table_address);
199
 
#endif
 
90
  Isolate* isolate = code->GetIsolate();
200
91
 
201
92
  // Add the deoptimizing code to the list.
202
93
  DeoptimizingCodeListNode* node = new DeoptimizingCodeListNode(code);
203
 
  DeoptimizerData* data = code->GetIsolate()->deoptimizer_data();
 
94
  DeoptimizerData* data = isolate->deoptimizer_data();
204
95
  node->set_next(data->deoptimizing_code_list_);
205
96
  data->deoptimizing_code_list_ = node;
206
97
 
 
98
  // We might be in the middle of incremental marking with compaction.
 
99
  // Tell collector to treat this code object in a special way and
 
100
  // ignore all slots that might have been recorded on it.
 
101
  isolate->heap()->mark_compact_collector()->InvalidateCode(code);
 
102
 
207
103
  // Set the code for the function to non-optimized version.
208
104
  function->ReplaceCode(function->shared()->code());
209
105
 
211
107
    PrintF("[forced deoptimization: ");
212
108
    function->PrintName();
213
109
    PrintF(" / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(function));
214
 
#ifdef DEBUG
215
 
    if (FLAG_print_code) {
216
 
      code->PrintLn();
217
 
    }
218
 
#endif
219
110
  }
220
111
}
221
112
 
222
113
 
223
 
void Deoptimizer::PatchStackCheckCodeAt(Address pc_after,
 
114
void Deoptimizer::PatchStackCheckCodeAt(Code* unoptimized_code,
 
115
                                        Address pc_after,
224
116
                                        Code* check_code,
225
117
                                        Code* replacement_code) {
226
118
  Address call_target_address = pc_after - kIntSize;
250
142
  *(call_target_address - 2) = 0x90;  // nop
251
143
  Assembler::set_target_address_at(call_target_address,
252
144
                                   replacement_code->entry());
 
145
 
 
146
  unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
 
147
      unoptimized_code, call_target_address, replacement_code);
253
148
}
254
149
 
255
150
 
256
 
void Deoptimizer::RevertStackCheckCodeAt(Address pc_after,
 
151
void Deoptimizer::RevertStackCheckCodeAt(Code* unoptimized_code,
 
152
                                         Address pc_after,
257
153
                                         Code* check_code,
258
154
                                         Code* replacement_code) {
259
155
  Address call_target_address = pc_after - kIntSize;
268
164
  *(call_target_address - 2) = 0x07;  // offset
269
165
  Assembler::set_target_address_at(call_target_address,
270
166
                                   check_code->entry());
 
167
 
 
168
  check_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
 
169
      unoptimized_code, call_target_address, check_code);
271
170
}
272
171
 
273
172
 
713
612
 
714
613
  Isolate* isolate = masm()->isolate();
715
614
 
716
 
  __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
 
615
  {
 
616
    AllowExternalCallThatCantCauseGC scope(masm());
 
617
    __ CallCFunction(ExternalReference::new_deoptimizer_function(isolate), 6);
 
618
  }
717
619
  // Preserve deoptimizer object in register rax and get the input
718
620
  // frame descriptor pointer.
719
621
  __ movq(rbx, Operand(rax, Deoptimizer::input_offset()));
759
661
  __ PrepareCallCFunction(2);
760
662
  __ movq(arg1, rax);
761
663
  __ LoadAddress(arg2, ExternalReference::isolate_address());
762
 
  __ CallCFunction(
763
 
      ExternalReference::compute_output_frames_function(isolate), 2);
 
664
  {
 
665
    AllowExternalCallThatCantCauseGC scope(masm());
 
666
    __ CallCFunction(
 
667
        ExternalReference::compute_output_frames_function(isolate), 2);
 
668
  }
764
669
  __ pop(rax);
765
670
 
766
671
  // Replace the current frame with the output frames.