Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_DEOPTIMIZER_H_ |
| 29 | #define V8_DEOPTIMIZER_H_ |
| 30 | |
| 31 | #include "v8.h" |
| 32 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 33 | #include "allocation.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 34 | #include "macro-assembler.h" |
| 35 | #include "zone-inl.h" |
| 36 | |
| 37 | |
| 38 | namespace v8 { |
| 39 | namespace internal { |
| 40 | |
| 41 | class FrameDescription; |
| 42 | class TranslationIterator; |
| 43 | class DeoptimizingCodeListNode; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 44 | class DeoptimizedFrameInfo; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 45 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 46 | class HeapNumberMaterializationDescriptor BASE_EMBEDDED { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 47 | public: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 48 | HeapNumberMaterializationDescriptor(Address slot_address, double val) |
| 49 | : slot_address_(slot_address), val_(val) { } |
| 50 | |
| 51 | Address slot_address() const { return slot_address_; } |
| 52 | double value() const { return val_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 53 | |
| 54 | private: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 55 | Address slot_address_; |
| 56 | double val_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
| 60 | class OptimizedFunctionVisitor BASE_EMBEDDED { |
| 61 | public: |
| 62 | virtual ~OptimizedFunctionVisitor() {} |
| 63 | |
| 64 | // Function which is called before iteration of any optimized functions |
| 65 | // from given global context. |
| 66 | virtual void EnterContext(Context* context) = 0; |
| 67 | |
| 68 | virtual void VisitFunction(JSFunction* function) = 0; |
| 69 | |
| 70 | // Function which is called after iteration of all optimized functions |
| 71 | // from given global context. |
| 72 | virtual void LeaveContext(Context* context) = 0; |
| 73 | }; |
| 74 | |
| 75 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 76 | class Deoptimizer; |
| 77 | |
| 78 | |
| 79 | class DeoptimizerData { |
| 80 | public: |
| 81 | DeoptimizerData(); |
| 82 | ~DeoptimizerData(); |
| 83 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 84 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 85 | void Iterate(ObjectVisitor* v); |
| 86 | #endif |
| 87 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 88 | private: |
| 89 | LargeObjectChunk* eager_deoptimization_entry_code_; |
| 90 | LargeObjectChunk* lazy_deoptimization_entry_code_; |
| 91 | Deoptimizer* current_; |
| 92 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 93 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 94 | DeoptimizedFrameInfo* deoptimized_frame_info_; |
| 95 | #endif |
| 96 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 97 | // List of deoptimized code which still have references from active stack |
| 98 | // frames. These code objects are needed by the deoptimizer when deoptimizing |
| 99 | // a frame for which the code object for the function function has been |
| 100 | // changed from the code present when deoptimizing was done. |
| 101 | DeoptimizingCodeListNode* deoptimizing_code_list_; |
| 102 | |
| 103 | friend class Deoptimizer; |
| 104 | |
| 105 | DISALLOW_COPY_AND_ASSIGN(DeoptimizerData); |
| 106 | }; |
| 107 | |
| 108 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 109 | class Deoptimizer : public Malloced { |
| 110 | public: |
| 111 | enum BailoutType { |
| 112 | EAGER, |
| 113 | LAZY, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 114 | OSR, |
| 115 | // This last bailout type is not really a bailout, but used by the |
| 116 | // debugger to deoptimize stack frames to allow inspection. |
| 117 | DEBUGGER |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | int output_count() const { return output_count_; } |
| 121 | |
| 122 | static Deoptimizer* New(JSFunction* function, |
| 123 | BailoutType type, |
| 124 | unsigned bailout_id, |
| 125 | Address from, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 126 | int fp_to_sp_delta, |
| 127 | Isolate* isolate); |
| 128 | static Deoptimizer* Grab(Isolate* isolate); |
| 129 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 130 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 131 | // The returned object with information on the optimized frame needs to be |
| 132 | // freed before another one can be generated. |
| 133 | static DeoptimizedFrameInfo* DebuggerInspectableFrame(JavaScriptFrame* frame, |
| 134 | int frame_index, |
| 135 | Isolate* isolate); |
| 136 | static void DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info, |
| 137 | Isolate* isolate); |
| 138 | #endif |
| 139 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 140 | // Makes sure that there is enough room in the relocation |
| 141 | // information of a code object to perform lazy deoptimization |
| 142 | // patching. If there is not enough room a new relocation |
| 143 | // information object is allocated and comments are added until it |
| 144 | // is big enough. |
| 145 | static void EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 146 | |
| 147 | // Deoptimize the function now. Its current optimized code will never be run |
| 148 | // again and any activations of the optimized code will get deoptimized when |
| 149 | // execution returns. |
| 150 | static void DeoptimizeFunction(JSFunction* function); |
| 151 | |
| 152 | // Deoptimize all functions in the heap. |
| 153 | static void DeoptimizeAll(); |
| 154 | |
| 155 | static void DeoptimizeGlobalObject(JSObject* object); |
| 156 | |
| 157 | static void VisitAllOptimizedFunctionsForContext( |
| 158 | Context* context, OptimizedFunctionVisitor* visitor); |
| 159 | |
| 160 | static void VisitAllOptimizedFunctionsForGlobalObject( |
| 161 | JSObject* object, OptimizedFunctionVisitor* visitor); |
| 162 | |
| 163 | static void VisitAllOptimizedFunctions(OptimizedFunctionVisitor* visitor); |
| 164 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 165 | // The size in bytes of the code required at a lazy deopt patch site. |
| 166 | static int patch_size(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 167 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 168 | // Patch all stack guard checks in the unoptimized code to |
| 169 | // unconditionally call replacement_code. |
| 170 | static void PatchStackCheckCode(Code* unoptimized_code, |
| 171 | Code* check_code, |
| 172 | Code* replacement_code); |
| 173 | |
| 174 | // Patch stack guard check at instruction before pc_after in |
| 175 | // the unoptimized code to unconditionally call replacement_code. |
| 176 | static void PatchStackCheckCodeAt(Address pc_after, |
| 177 | Code* check_code, |
| 178 | Code* replacement_code); |
| 179 | |
| 180 | // Change all patched stack guard checks in the unoptimized code |
| 181 | // back to a normal stack guard check. |
| 182 | static void RevertStackCheckCode(Code* unoptimized_code, |
| 183 | Code* check_code, |
| 184 | Code* replacement_code); |
| 185 | |
| 186 | // Change all patched stack guard checks in the unoptimized code |
| 187 | // back to a normal stack guard check. |
| 188 | static void RevertStackCheckCodeAt(Address pc_after, |
| 189 | Code* check_code, |
| 190 | Code* replacement_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 191 | |
| 192 | ~Deoptimizer(); |
| 193 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 194 | void MaterializeHeapNumbers(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 195 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 196 | void MaterializeHeapNumbersForDebuggerInspectableFrame( |
| 197 | Address top, uint32_t size, DeoptimizedFrameInfo* info); |
| 198 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 199 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 200 | static void ComputeOutputFrames(Deoptimizer* deoptimizer); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 201 | |
| 202 | static Address GetDeoptimizationEntry(int id, BailoutType type); |
| 203 | static int GetDeoptimizationId(Address addr, BailoutType type); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 204 | static int GetOutputInfo(DeoptimizationOutputData* data, |
| 205 | unsigned node_id, |
| 206 | SharedFunctionInfo* shared); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 207 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 208 | // Code generation support. |
| 209 | static int input_offset() { return OFFSET_OF(Deoptimizer, input_); } |
| 210 | static int output_count_offset() { |
| 211 | return OFFSET_OF(Deoptimizer, output_count_); |
| 212 | } |
| 213 | static int output_offset() { return OFFSET_OF(Deoptimizer, output_); } |
| 214 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 215 | static int GetDeoptimizedCodeCount(Isolate* isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 216 | |
| 217 | static const int kNotDeoptimizationEntry = -1; |
| 218 | |
| 219 | // Generators for the deoptimization entry code. |
| 220 | class EntryGenerator BASE_EMBEDDED { |
| 221 | public: |
| 222 | EntryGenerator(MacroAssembler* masm, BailoutType type) |
| 223 | : masm_(masm), type_(type) { } |
| 224 | virtual ~EntryGenerator() { } |
| 225 | |
| 226 | void Generate(); |
| 227 | |
| 228 | protected: |
| 229 | MacroAssembler* masm() const { return masm_; } |
| 230 | BailoutType type() const { return type_; } |
| 231 | |
| 232 | virtual void GeneratePrologue() { } |
| 233 | |
| 234 | private: |
| 235 | MacroAssembler* masm_; |
| 236 | Deoptimizer::BailoutType type_; |
| 237 | }; |
| 238 | |
| 239 | class TableEntryGenerator : public EntryGenerator { |
| 240 | public: |
| 241 | TableEntryGenerator(MacroAssembler* masm, BailoutType type, int count) |
| 242 | : EntryGenerator(masm, type), count_(count) { } |
| 243 | |
| 244 | protected: |
| 245 | virtual void GeneratePrologue(); |
| 246 | |
| 247 | private: |
| 248 | int count() const { return count_; } |
| 249 | |
| 250 | int count_; |
| 251 | }; |
| 252 | |
| 253 | private: |
| 254 | static const int kNumberOfEntries = 4096; |
| 255 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 256 | Deoptimizer(Isolate* isolate, |
| 257 | JSFunction* function, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 258 | BailoutType type, |
| 259 | unsigned bailout_id, |
| 260 | Address from, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 261 | int fp_to_sp_delta, |
| 262 | Code* optimized_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 263 | void DeleteFrameDescriptions(); |
| 264 | |
| 265 | void DoComputeOutputFrames(); |
| 266 | void DoComputeOsrOutputFrame(); |
| 267 | void DoComputeFrame(TranslationIterator* iterator, int frame_index); |
| 268 | void DoTranslateCommand(TranslationIterator* iterator, |
| 269 | int frame_index, |
| 270 | unsigned output_offset); |
| 271 | // Translate a command for OSR. Updates the input offset to be used for |
| 272 | // the next command. Returns false if translation of the command failed |
| 273 | // (e.g., a number conversion failed) and may or may not have updated the |
| 274 | // input offset. |
| 275 | bool DoOsrTranslateCommand(TranslationIterator* iterator, |
| 276 | int* input_offset); |
| 277 | |
| 278 | unsigned ComputeInputFrameSize() const; |
| 279 | unsigned ComputeFixedSize(JSFunction* function) const; |
| 280 | |
| 281 | unsigned ComputeIncomingArgumentSize(JSFunction* function) const; |
| 282 | unsigned ComputeOutgoingArgumentSize() const; |
| 283 | |
| 284 | Object* ComputeLiteral(int index) const; |
| 285 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 286 | void AddDoubleValue(intptr_t slot_address, double value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 287 | |
| 288 | static LargeObjectChunk* CreateCode(BailoutType type); |
| 289 | static void GenerateDeoptimizationEntries( |
| 290 | MacroAssembler* masm, int count, BailoutType type); |
| 291 | |
| 292 | // Weak handle callback for deoptimizing code objects. |
| 293 | static void HandleWeakDeoptimizedCode( |
| 294 | v8::Persistent<v8::Value> obj, void* data); |
| 295 | static Code* FindDeoptimizingCodeFromAddress(Address addr); |
| 296 | static void RemoveDeoptimizingCode(Code* code); |
| 297 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 298 | // Fill the input from from a JavaScript frame. This is used when |
| 299 | // the debugger needs to inspect an optimized frame. For normal |
| 300 | // deoptimizations the input frame is filled in generated code. |
| 301 | void FillInputFrame(Address tos, JavaScriptFrame* frame); |
| 302 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 303 | Isolate* isolate_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 304 | JSFunction* function_; |
| 305 | Code* optimized_code_; |
| 306 | unsigned bailout_id_; |
| 307 | BailoutType bailout_type_; |
| 308 | Address from_; |
| 309 | int fp_to_sp_delta_; |
| 310 | |
| 311 | // Input frame description. |
| 312 | FrameDescription* input_; |
| 313 | // Number of output frames. |
| 314 | int output_count_; |
| 315 | // Array of output frame descriptions. |
| 316 | FrameDescription** output_; |
| 317 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 318 | List<HeapNumberMaterializationDescriptor> deferred_heap_numbers_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 319 | |
| 320 | static int table_entry_size_; |
| 321 | |
| 322 | friend class FrameDescription; |
| 323 | friend class DeoptimizingCodeListNode; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 324 | friend class DeoptimizedFrameInfo; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | |
| 328 | class FrameDescription { |
| 329 | public: |
| 330 | FrameDescription(uint32_t frame_size, |
| 331 | JSFunction* function); |
| 332 | |
| 333 | void* operator new(size_t size, uint32_t frame_size) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 334 | // Subtracts kPointerSize, as the member frame_content_ already supplies |
| 335 | // the first element of the area to store the frame. |
| 336 | return malloc(size + frame_size - kPointerSize); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void operator delete(void* description) { |
| 340 | free(description); |
| 341 | } |
| 342 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 343 | uint32_t GetFrameSize() const { |
| 344 | ASSERT(static_cast<uint32_t>(frame_size_) == frame_size_); |
| 345 | return static_cast<uint32_t>(frame_size_); |
| 346 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 347 | |
| 348 | JSFunction* GetFunction() const { return function_; } |
| 349 | |
| 350 | unsigned GetOffsetFromSlotIndex(Deoptimizer* deoptimizer, int slot_index); |
| 351 | |
| 352 | intptr_t GetFrameSlot(unsigned offset) { |
| 353 | return *GetFrameSlotPointer(offset); |
| 354 | } |
| 355 | |
| 356 | double GetDoubleFrameSlot(unsigned offset) { |
| 357 | return *reinterpret_cast<double*>(GetFrameSlotPointer(offset)); |
| 358 | } |
| 359 | |
| 360 | void SetFrameSlot(unsigned offset, intptr_t value) { |
| 361 | *GetFrameSlotPointer(offset) = value; |
| 362 | } |
| 363 | |
| 364 | intptr_t GetRegister(unsigned n) const { |
| 365 | ASSERT(n < ARRAY_SIZE(registers_)); |
| 366 | return registers_[n]; |
| 367 | } |
| 368 | |
| 369 | double GetDoubleRegister(unsigned n) const { |
| 370 | ASSERT(n < ARRAY_SIZE(double_registers_)); |
| 371 | return double_registers_[n]; |
| 372 | } |
| 373 | |
| 374 | void SetRegister(unsigned n, intptr_t value) { |
| 375 | ASSERT(n < ARRAY_SIZE(registers_)); |
| 376 | registers_[n] = value; |
| 377 | } |
| 378 | |
| 379 | void SetDoubleRegister(unsigned n, double value) { |
| 380 | ASSERT(n < ARRAY_SIZE(double_registers_)); |
| 381 | double_registers_[n] = value; |
| 382 | } |
| 383 | |
| 384 | intptr_t GetTop() const { return top_; } |
| 385 | void SetTop(intptr_t top) { top_ = top; } |
| 386 | |
| 387 | intptr_t GetPc() const { return pc_; } |
| 388 | void SetPc(intptr_t pc) { pc_ = pc; } |
| 389 | |
| 390 | intptr_t GetFp() const { return fp_; } |
| 391 | void SetFp(intptr_t fp) { fp_ = fp; } |
| 392 | |
| 393 | Smi* GetState() const { return state_; } |
| 394 | void SetState(Smi* state) { state_ = state; } |
| 395 | |
| 396 | void SetContinuation(intptr_t pc) { continuation_ = pc; } |
| 397 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 398 | #ifdef DEBUG |
| 399 | Code::Kind GetKind() const { return kind_; } |
| 400 | void SetKind(Code::Kind kind) { kind_ = kind; } |
| 401 | #endif |
| 402 | |
| 403 | // Get the incoming arguments count. |
| 404 | int ComputeParametersCount(); |
| 405 | |
| 406 | // Get a parameter value for an unoptimized frame. |
| 407 | Object* GetParameter(Deoptimizer* deoptimizer, int index); |
| 408 | |
| 409 | // Get the expression stack height for a unoptimized frame. |
| 410 | unsigned GetExpressionCount(Deoptimizer* deoptimizer); |
| 411 | |
| 412 | // Get the expression stack value for an unoptimized frame. |
| 413 | Object* GetExpression(Deoptimizer* deoptimizer, int index); |
| 414 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 415 | static int registers_offset() { |
| 416 | return OFFSET_OF(FrameDescription, registers_); |
| 417 | } |
| 418 | |
| 419 | static int double_registers_offset() { |
| 420 | return OFFSET_OF(FrameDescription, double_registers_); |
| 421 | } |
| 422 | |
| 423 | static int frame_size_offset() { |
| 424 | return OFFSET_OF(FrameDescription, frame_size_); |
| 425 | } |
| 426 | |
| 427 | static int pc_offset() { |
| 428 | return OFFSET_OF(FrameDescription, pc_); |
| 429 | } |
| 430 | |
| 431 | static int state_offset() { |
| 432 | return OFFSET_OF(FrameDescription, state_); |
| 433 | } |
| 434 | |
| 435 | static int continuation_offset() { |
| 436 | return OFFSET_OF(FrameDescription, continuation_); |
| 437 | } |
| 438 | |
| 439 | static int frame_content_offset() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 440 | return OFFSET_OF(FrameDescription, frame_content_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | private: |
| 444 | static const uint32_t kZapUint32 = 0xbeeddead; |
| 445 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 446 | // Frame_size_ must hold a uint32_t value. It is only a uintptr_t to |
| 447 | // keep the variable-size array frame_content_ of type intptr_t at |
| 448 | // the end of the structure aligned. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 449 | uintptr_t frame_size_; // Number of bytes. |
| 450 | JSFunction* function_; |
| 451 | intptr_t registers_[Register::kNumRegisters]; |
| 452 | double double_registers_[DoubleRegister::kNumAllocatableRegisters]; |
| 453 | intptr_t top_; |
| 454 | intptr_t pc_; |
| 455 | intptr_t fp_; |
| 456 | Smi* state_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 457 | #ifdef DEBUG |
| 458 | Code::Kind kind_; |
| 459 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 460 | |
| 461 | // Continuation is the PC where the execution continues after |
| 462 | // deoptimizing. |
| 463 | intptr_t continuation_; |
| 464 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 465 | // This must be at the end of the object as the object is allocated larger |
| 466 | // than it's definition indicate to extend this array. |
| 467 | intptr_t frame_content_[1]; |
| 468 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 469 | intptr_t* GetFrameSlotPointer(unsigned offset) { |
| 470 | ASSERT(offset < frame_size_); |
| 471 | return reinterpret_cast<intptr_t*>( |
| 472 | reinterpret_cast<Address>(this) + frame_content_offset() + offset); |
| 473 | } |
| 474 | }; |
| 475 | |
| 476 | |
| 477 | class TranslationBuffer BASE_EMBEDDED { |
| 478 | public: |
| 479 | TranslationBuffer() : contents_(256) { } |
| 480 | |
| 481 | int CurrentIndex() const { return contents_.length(); } |
| 482 | void Add(int32_t value); |
| 483 | |
| 484 | Handle<ByteArray> CreateByteArray(); |
| 485 | |
| 486 | private: |
| 487 | ZoneList<uint8_t> contents_; |
| 488 | }; |
| 489 | |
| 490 | |
| 491 | class TranslationIterator BASE_EMBEDDED { |
| 492 | public: |
| 493 | TranslationIterator(ByteArray* buffer, int index) |
| 494 | : buffer_(buffer), index_(index) { |
| 495 | ASSERT(index >= 0 && index < buffer->length()); |
| 496 | } |
| 497 | |
| 498 | int32_t Next(); |
| 499 | |
| 500 | bool HasNext() const { return index_ >= 0; } |
| 501 | |
| 502 | void Done() { index_ = -1; } |
| 503 | |
| 504 | void Skip(int n) { |
| 505 | for (int i = 0; i < n; i++) Next(); |
| 506 | } |
| 507 | |
| 508 | private: |
| 509 | ByteArray* buffer_; |
| 510 | int index_; |
| 511 | }; |
| 512 | |
| 513 | |
| 514 | class Translation BASE_EMBEDDED { |
| 515 | public: |
| 516 | enum Opcode { |
| 517 | BEGIN, |
| 518 | FRAME, |
| 519 | REGISTER, |
| 520 | INT32_REGISTER, |
| 521 | DOUBLE_REGISTER, |
| 522 | STACK_SLOT, |
| 523 | INT32_STACK_SLOT, |
| 524 | DOUBLE_STACK_SLOT, |
| 525 | LITERAL, |
| 526 | ARGUMENTS_OBJECT, |
| 527 | |
| 528 | // A prefix indicating that the next command is a duplicate of the one |
| 529 | // that follows it. |
| 530 | DUPLICATE |
| 531 | }; |
| 532 | |
| 533 | Translation(TranslationBuffer* buffer, int frame_count) |
| 534 | : buffer_(buffer), |
| 535 | index_(buffer->CurrentIndex()) { |
| 536 | buffer_->Add(BEGIN); |
| 537 | buffer_->Add(frame_count); |
| 538 | } |
| 539 | |
| 540 | int index() const { return index_; } |
| 541 | |
| 542 | // Commands. |
| 543 | void BeginFrame(int node_id, int literal_id, unsigned height); |
| 544 | void StoreRegister(Register reg); |
| 545 | void StoreInt32Register(Register reg); |
| 546 | void StoreDoubleRegister(DoubleRegister reg); |
| 547 | void StoreStackSlot(int index); |
| 548 | void StoreInt32StackSlot(int index); |
| 549 | void StoreDoubleStackSlot(int index); |
| 550 | void StoreLiteral(int literal_id); |
| 551 | void StoreArgumentsObject(); |
| 552 | void MarkDuplicate(); |
| 553 | |
| 554 | static int NumberOfOperandsFor(Opcode opcode); |
| 555 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 556 | #if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 557 | static const char* StringFor(Opcode opcode); |
| 558 | #endif |
| 559 | |
| 560 | private: |
| 561 | TranslationBuffer* buffer_; |
| 562 | int index_; |
| 563 | }; |
| 564 | |
| 565 | |
| 566 | // Linked list holding deoptimizing code objects. The deoptimizing code objects |
| 567 | // are kept as weak handles until they are no longer activated on the stack. |
| 568 | class DeoptimizingCodeListNode : public Malloced { |
| 569 | public: |
| 570 | explicit DeoptimizingCodeListNode(Code* code); |
| 571 | ~DeoptimizingCodeListNode(); |
| 572 | |
| 573 | DeoptimizingCodeListNode* next() const { return next_; } |
| 574 | void set_next(DeoptimizingCodeListNode* next) { next_ = next; } |
| 575 | Handle<Code> code() const { return code_; } |
| 576 | |
| 577 | private: |
| 578 | // Global (weak) handle to the deoptimizing code object. |
| 579 | Handle<Code> code_; |
| 580 | |
| 581 | // Next pointer for linked list. |
| 582 | DeoptimizingCodeListNode* next_; |
| 583 | }; |
| 584 | |
| 585 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 586 | class SlotRef BASE_EMBEDDED { |
| 587 | public: |
| 588 | enum SlotRepresentation { |
| 589 | UNKNOWN, |
| 590 | TAGGED, |
| 591 | INT32, |
| 592 | DOUBLE, |
| 593 | LITERAL |
| 594 | }; |
| 595 | |
| 596 | SlotRef() |
| 597 | : addr_(NULL), representation_(UNKNOWN) { } |
| 598 | |
| 599 | SlotRef(Address addr, SlotRepresentation representation) |
| 600 | : addr_(addr), representation_(representation) { } |
| 601 | |
| 602 | explicit SlotRef(Object* literal) |
| 603 | : literal_(literal), representation_(LITERAL) { } |
| 604 | |
| 605 | Handle<Object> GetValue() { |
| 606 | switch (representation_) { |
| 607 | case TAGGED: |
| 608 | return Handle<Object>(Memory::Object_at(addr_)); |
| 609 | |
| 610 | case INT32: { |
| 611 | int value = Memory::int32_at(addr_); |
| 612 | if (Smi::IsValid(value)) { |
| 613 | return Handle<Object>(Smi::FromInt(value)); |
| 614 | } else { |
| 615 | return Isolate::Current()->factory()->NewNumberFromInt(value); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | case DOUBLE: { |
| 620 | double value = Memory::double_at(addr_); |
| 621 | return Isolate::Current()->factory()->NewNumber(value); |
| 622 | } |
| 623 | |
| 624 | case LITERAL: |
| 625 | return literal_; |
| 626 | |
| 627 | default: |
| 628 | UNREACHABLE(); |
| 629 | return Handle<Object>::null(); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static void ComputeSlotMappingForArguments(JavaScriptFrame* frame, |
| 634 | int inlined_frame_index, |
| 635 | Vector<SlotRef>* args_slots); |
| 636 | |
| 637 | private: |
| 638 | Address addr_; |
| 639 | Handle<Object> literal_; |
| 640 | SlotRepresentation representation_; |
| 641 | |
| 642 | static Address SlotAddress(JavaScriptFrame* frame, int slot_index) { |
| 643 | if (slot_index >= 0) { |
| 644 | const int offset = JavaScriptFrameConstants::kLocal0Offset; |
| 645 | return frame->fp() + offset - (slot_index * kPointerSize); |
| 646 | } else { |
| 647 | const int offset = JavaScriptFrameConstants::kLastParameterOffset; |
| 648 | return frame->fp() + offset - ((slot_index + 1) * kPointerSize); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | static SlotRef ComputeSlotForNextArgument(TranslationIterator* iterator, |
| 653 | DeoptimizationInputData* data, |
| 654 | JavaScriptFrame* frame); |
| 655 | }; |
| 656 | |
| 657 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 658 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 659 | // Class used to represent an unoptimized frame when the debugger |
| 660 | // needs to inspect a frame that is part of an optimized frame. The |
| 661 | // internally used FrameDescription objects are not GC safe so for use |
| 662 | // by the debugger frame information is copied to an object of this type. |
| 663 | class DeoptimizedFrameInfo : public Malloced { |
| 664 | public: |
| 665 | DeoptimizedFrameInfo(Deoptimizer* deoptimizer, int frame_index); |
| 666 | virtual ~DeoptimizedFrameInfo(); |
| 667 | |
| 668 | // GC support. |
| 669 | void Iterate(ObjectVisitor* v); |
| 670 | |
| 671 | // Return the number of incoming arguments. |
| 672 | int parameters_count() { return parameters_count_; } |
| 673 | |
| 674 | // Return the height of the expression stack. |
| 675 | int expression_count() { return expression_count_; } |
| 676 | |
| 677 | // Get the frame function. |
| 678 | JSFunction* GetFunction() { |
| 679 | return function_; |
| 680 | } |
| 681 | |
| 682 | // Get an incoming argument. |
| 683 | Object* GetParameter(int index) { |
| 684 | ASSERT(0 <= index && index < parameters_count()); |
| 685 | return parameters_[index]; |
| 686 | } |
| 687 | |
| 688 | // Get an expression from the expression stack. |
| 689 | Object* GetExpression(int index) { |
| 690 | ASSERT(0 <= index && index < expression_count()); |
| 691 | return expression_stack_[index]; |
| 692 | } |
| 693 | |
| 694 | private: |
| 695 | // Set the frame function. |
| 696 | void SetFunction(JSFunction* function) { |
| 697 | function_ = function; |
| 698 | } |
| 699 | |
| 700 | // Set an incoming argument. |
| 701 | void SetParameter(int index, Object* obj) { |
| 702 | ASSERT(0 <= index && index < parameters_count()); |
| 703 | parameters_[index] = obj; |
| 704 | } |
| 705 | |
| 706 | // Set an expression on the expression stack. |
| 707 | void SetExpression(int index, Object* obj) { |
| 708 | ASSERT(0 <= index && index < expression_count()); |
| 709 | expression_stack_[index] = obj; |
| 710 | } |
| 711 | |
| 712 | JSFunction* function_; |
| 713 | int parameters_count_; |
| 714 | int expression_count_; |
| 715 | Object** parameters_; |
| 716 | Object** expression_stack_; |
| 717 | |
| 718 | friend class Deoptimizer; |
| 719 | }; |
| 720 | #endif |
| 721 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 722 | } } // namespace v8::internal |
| 723 | |
| 724 | #endif // V8_DEOPTIMIZER_H_ |