Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1 | // Copyright 2012 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: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 89 | MemoryChunk* eager_deoptimization_entry_code_; |
| 90 | MemoryChunk* lazy_deoptimization_entry_code_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 122 | // Number of created JS frames. Not all created frames are necessarily JS. |
| 123 | int jsframe_count() const { return jsframe_count_; } |
| 124 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 125 | static Deoptimizer* New(JSFunction* function, |
| 126 | BailoutType type, |
| 127 | unsigned bailout_id, |
| 128 | Address from, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 129 | int fp_to_sp_delta, |
| 130 | Isolate* isolate); |
| 131 | static Deoptimizer* Grab(Isolate* isolate); |
| 132 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 133 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 134 | // The returned object with information on the optimized frame needs to be |
| 135 | // freed before another one can be generated. |
| 136 | static DeoptimizedFrameInfo* DebuggerInspectableFrame(JavaScriptFrame* frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 137 | int jsframe_index, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 138 | Isolate* isolate); |
| 139 | static void DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info, |
| 140 | Isolate* isolate); |
| 141 | #endif |
| 142 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 143 | // Makes sure that there is enough room in the relocation |
| 144 | // information of a code object to perform lazy deoptimization |
| 145 | // patching. If there is not enough room a new relocation |
| 146 | // information object is allocated and comments are added until it |
| 147 | // is big enough. |
| 148 | static void EnsureRelocSpaceForLazyDeoptimization(Handle<Code> code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 149 | |
| 150 | // Deoptimize the function now. Its current optimized code will never be run |
| 151 | // again and any activations of the optimized code will get deoptimized when |
| 152 | // execution returns. |
| 153 | static void DeoptimizeFunction(JSFunction* function); |
| 154 | |
| 155 | // Deoptimize all functions in the heap. |
| 156 | static void DeoptimizeAll(); |
| 157 | |
| 158 | static void DeoptimizeGlobalObject(JSObject* object); |
| 159 | |
| 160 | static void VisitAllOptimizedFunctionsForContext( |
| 161 | Context* context, OptimizedFunctionVisitor* visitor); |
| 162 | |
| 163 | static void VisitAllOptimizedFunctionsForGlobalObject( |
| 164 | JSObject* object, OptimizedFunctionVisitor* visitor); |
| 165 | |
| 166 | static void VisitAllOptimizedFunctions(OptimizedFunctionVisitor* visitor); |
| 167 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 168 | // The size in bytes of the code required at a lazy deopt patch site. |
| 169 | static int patch_size(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 170 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 171 | // Patch all stack guard checks in the unoptimized code to |
| 172 | // unconditionally call replacement_code. |
| 173 | static void PatchStackCheckCode(Code* unoptimized_code, |
| 174 | Code* check_code, |
| 175 | Code* replacement_code); |
| 176 | |
| 177 | // Patch stack guard check at instruction before pc_after in |
| 178 | // the unoptimized code to unconditionally call replacement_code. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 179 | static void PatchStackCheckCodeAt(Code* unoptimized_code, |
| 180 | Address pc_after, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 181 | Code* check_code, |
| 182 | Code* replacement_code); |
| 183 | |
| 184 | // Change all patched stack guard checks in the unoptimized code |
| 185 | // back to a normal stack guard check. |
| 186 | static void RevertStackCheckCode(Code* unoptimized_code, |
| 187 | Code* check_code, |
| 188 | Code* replacement_code); |
| 189 | |
| 190 | // Change all patched stack guard checks in the unoptimized code |
| 191 | // back to a normal stack guard check. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 192 | static void RevertStackCheckCodeAt(Code* unoptimized_code, |
| 193 | Address pc_after, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 194 | Code* check_code, |
| 195 | Code* replacement_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 196 | |
| 197 | ~Deoptimizer(); |
| 198 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 199 | void MaterializeHeapNumbers(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 200 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 201 | void MaterializeHeapNumbersForDebuggerInspectableFrame( |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 202 | Address parameters_top, |
| 203 | uint32_t parameters_size, |
| 204 | Address expressions_top, |
| 205 | uint32_t expressions_size, |
| 206 | DeoptimizedFrameInfo* info); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 207 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 208 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 209 | static void ComputeOutputFrames(Deoptimizer* deoptimizer); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 210 | |
| 211 | static Address GetDeoptimizationEntry(int id, BailoutType type); |
| 212 | static int GetDeoptimizationId(Address addr, BailoutType type); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 213 | static int GetOutputInfo(DeoptimizationOutputData* data, |
| 214 | unsigned node_id, |
| 215 | SharedFunctionInfo* shared); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 216 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 217 | // Code generation support. |
| 218 | static int input_offset() { return OFFSET_OF(Deoptimizer, input_); } |
| 219 | static int output_count_offset() { |
| 220 | return OFFSET_OF(Deoptimizer, output_count_); |
| 221 | } |
| 222 | static int output_offset() { return OFFSET_OF(Deoptimizer, output_); } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 223 | static int frame_alignment_marker_offset() { |
| 224 | return OFFSET_OF(Deoptimizer, frame_alignment_marker_); } |
| 225 | static int has_alignment_padding_offset() { |
| 226 | return OFFSET_OF(Deoptimizer, has_alignment_padding_); |
| 227 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 228 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 229 | static int GetDeoptimizedCodeCount(Isolate* isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 230 | |
| 231 | static const int kNotDeoptimizationEntry = -1; |
| 232 | |
| 233 | // Generators for the deoptimization entry code. |
| 234 | class EntryGenerator BASE_EMBEDDED { |
| 235 | public: |
| 236 | EntryGenerator(MacroAssembler* masm, BailoutType type) |
| 237 | : masm_(masm), type_(type) { } |
| 238 | virtual ~EntryGenerator() { } |
| 239 | |
| 240 | void Generate(); |
| 241 | |
| 242 | protected: |
| 243 | MacroAssembler* masm() const { return masm_; } |
| 244 | BailoutType type() const { return type_; } |
| 245 | |
| 246 | virtual void GeneratePrologue() { } |
| 247 | |
| 248 | private: |
| 249 | MacroAssembler* masm_; |
| 250 | Deoptimizer::BailoutType type_; |
| 251 | }; |
| 252 | |
| 253 | class TableEntryGenerator : public EntryGenerator { |
| 254 | public: |
| 255 | TableEntryGenerator(MacroAssembler* masm, BailoutType type, int count) |
| 256 | : EntryGenerator(masm, type), count_(count) { } |
| 257 | |
| 258 | protected: |
| 259 | virtual void GeneratePrologue(); |
| 260 | |
| 261 | private: |
| 262 | int count() const { return count_; } |
| 263 | |
| 264 | int count_; |
| 265 | }; |
| 266 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 267 | int ConvertJSFrameIndexToFrameIndex(int jsframe_index); |
| 268 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 269 | private: |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 270 | #ifdef V8_TARGET_ARCH_MIPS |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 271 | static const int kNumberOfEntries = 4096; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 272 | #else |
| 273 | static const int kNumberOfEntries = 8192; |
| 274 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 275 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 276 | Deoptimizer(Isolate* isolate, |
| 277 | JSFunction* function, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 278 | BailoutType type, |
| 279 | unsigned bailout_id, |
| 280 | Address from, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 281 | int fp_to_sp_delta, |
| 282 | Code* optimized_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 283 | void DeleteFrameDescriptions(); |
| 284 | |
| 285 | void DoComputeOutputFrames(); |
| 286 | void DoComputeOsrOutputFrame(); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 287 | void DoComputeJSFrame(TranslationIterator* iterator, int frame_index); |
| 288 | void DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator, |
| 289 | int frame_index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 290 | void DoTranslateCommand(TranslationIterator* iterator, |
| 291 | int frame_index, |
| 292 | unsigned output_offset); |
| 293 | // Translate a command for OSR. Updates the input offset to be used for |
| 294 | // the next command. Returns false if translation of the command failed |
| 295 | // (e.g., a number conversion failed) and may or may not have updated the |
| 296 | // input offset. |
| 297 | bool DoOsrTranslateCommand(TranslationIterator* iterator, |
| 298 | int* input_offset); |
| 299 | |
| 300 | unsigned ComputeInputFrameSize() const; |
| 301 | unsigned ComputeFixedSize(JSFunction* function) const; |
| 302 | |
| 303 | unsigned ComputeIncomingArgumentSize(JSFunction* function) const; |
| 304 | unsigned ComputeOutgoingArgumentSize() const; |
| 305 | |
| 306 | Object* ComputeLiteral(int index) const; |
| 307 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 308 | void AddDoubleValue(intptr_t slot_address, double value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 309 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 310 | static MemoryChunk* CreateCode(BailoutType type); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 311 | static void GenerateDeoptimizationEntries( |
| 312 | MacroAssembler* masm, int count, BailoutType type); |
| 313 | |
| 314 | // Weak handle callback for deoptimizing code objects. |
| 315 | static void HandleWeakDeoptimizedCode( |
| 316 | v8::Persistent<v8::Value> obj, void* data); |
| 317 | static Code* FindDeoptimizingCodeFromAddress(Address addr); |
| 318 | static void RemoveDeoptimizingCode(Code* code); |
| 319 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 320 | // Fill the input from from a JavaScript frame. This is used when |
| 321 | // the debugger needs to inspect an optimized frame. For normal |
| 322 | // deoptimizations the input frame is filled in generated code. |
| 323 | void FillInputFrame(Address tos, JavaScriptFrame* frame); |
| 324 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | Isolate* isolate_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 326 | JSFunction* function_; |
| 327 | Code* optimized_code_; |
| 328 | unsigned bailout_id_; |
| 329 | BailoutType bailout_type_; |
| 330 | Address from_; |
| 331 | int fp_to_sp_delta_; |
| 332 | |
| 333 | // Input frame description. |
| 334 | FrameDescription* input_; |
| 335 | // Number of output frames. |
| 336 | int output_count_; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 337 | // Number of output js frames. |
| 338 | int jsframe_count_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 339 | // Array of output frame descriptions. |
| 340 | FrameDescription** output_; |
| 341 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 342 | // Frames can be dynamically padded on ia32 to align untagged doubles. |
| 343 | Object* frame_alignment_marker_; |
| 344 | intptr_t has_alignment_padding_; |
| 345 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 346 | List<HeapNumberMaterializationDescriptor> deferred_heap_numbers_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 347 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 348 | static const int table_entry_size_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 349 | |
| 350 | friend class FrameDescription; |
| 351 | friend class DeoptimizingCodeListNode; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 352 | friend class DeoptimizedFrameInfo; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | |
| 356 | class FrameDescription { |
| 357 | public: |
| 358 | FrameDescription(uint32_t frame_size, |
| 359 | JSFunction* function); |
| 360 | |
| 361 | void* operator new(size_t size, uint32_t frame_size) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 362 | // Subtracts kPointerSize, as the member frame_content_ already supplies |
| 363 | // the first element of the area to store the frame. |
| 364 | return malloc(size + frame_size - kPointerSize); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 367 | void operator delete(void* pointer, uint32_t frame_size) { |
| 368 | free(pointer); |
| 369 | } |
| 370 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 371 | void operator delete(void* description) { |
| 372 | free(description); |
| 373 | } |
| 374 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 375 | uint32_t GetFrameSize() const { |
| 376 | ASSERT(static_cast<uint32_t>(frame_size_) == frame_size_); |
| 377 | return static_cast<uint32_t>(frame_size_); |
| 378 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 379 | |
| 380 | JSFunction* GetFunction() const { return function_; } |
| 381 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 382 | unsigned GetOffsetFromSlotIndex(int slot_index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 383 | |
| 384 | intptr_t GetFrameSlot(unsigned offset) { |
| 385 | return *GetFrameSlotPointer(offset); |
| 386 | } |
| 387 | |
| 388 | double GetDoubleFrameSlot(unsigned offset) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 389 | intptr_t* ptr = GetFrameSlotPointer(offset); |
| 390 | #if V8_TARGET_ARCH_MIPS |
| 391 | // Prevent gcc from using load-double (mips ldc1) on (possibly) |
| 392 | // non-64-bit aligned double. Uses two lwc1 instructions. |
| 393 | union conversion { |
| 394 | double d; |
| 395 | uint32_t u[2]; |
| 396 | } c; |
| 397 | c.u[0] = *reinterpret_cast<uint32_t*>(ptr); |
| 398 | c.u[1] = *(reinterpret_cast<uint32_t*>(ptr) + 1); |
| 399 | return c.d; |
| 400 | #else |
| 401 | return *reinterpret_cast<double*>(ptr); |
| 402 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void SetFrameSlot(unsigned offset, intptr_t value) { |
| 406 | *GetFrameSlotPointer(offset) = value; |
| 407 | } |
| 408 | |
| 409 | intptr_t GetRegister(unsigned n) const { |
| 410 | ASSERT(n < ARRAY_SIZE(registers_)); |
| 411 | return registers_[n]; |
| 412 | } |
| 413 | |
| 414 | double GetDoubleRegister(unsigned n) const { |
| 415 | ASSERT(n < ARRAY_SIZE(double_registers_)); |
| 416 | return double_registers_[n]; |
| 417 | } |
| 418 | |
| 419 | void SetRegister(unsigned n, intptr_t value) { |
| 420 | ASSERT(n < ARRAY_SIZE(registers_)); |
| 421 | registers_[n] = value; |
| 422 | } |
| 423 | |
| 424 | void SetDoubleRegister(unsigned n, double value) { |
| 425 | ASSERT(n < ARRAY_SIZE(double_registers_)); |
| 426 | double_registers_[n] = value; |
| 427 | } |
| 428 | |
| 429 | intptr_t GetTop() const { return top_; } |
| 430 | void SetTop(intptr_t top) { top_ = top; } |
| 431 | |
| 432 | intptr_t GetPc() const { return pc_; } |
| 433 | void SetPc(intptr_t pc) { pc_ = pc; } |
| 434 | |
| 435 | intptr_t GetFp() const { return fp_; } |
| 436 | void SetFp(intptr_t fp) { fp_ = fp; } |
| 437 | |
| 438 | Smi* GetState() const { return state_; } |
| 439 | void SetState(Smi* state) { state_ = state; } |
| 440 | |
| 441 | void SetContinuation(intptr_t pc) { continuation_ = pc; } |
| 442 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 443 | StackFrame::Type GetFrameType() const { return type_; } |
| 444 | void SetFrameType(StackFrame::Type type) { type_ = type; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 445 | |
| 446 | // Get the incoming arguments count. |
| 447 | int ComputeParametersCount(); |
| 448 | |
| 449 | // Get a parameter value for an unoptimized frame. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 450 | Object* GetParameter(int index); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 451 | |
| 452 | // Get the expression stack height for a unoptimized frame. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 453 | unsigned GetExpressionCount(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 454 | |
| 455 | // Get the expression stack value for an unoptimized frame. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 456 | Object* GetExpression(int index); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 457 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 458 | static int registers_offset() { |
| 459 | return OFFSET_OF(FrameDescription, registers_); |
| 460 | } |
| 461 | |
| 462 | static int double_registers_offset() { |
| 463 | return OFFSET_OF(FrameDescription, double_registers_); |
| 464 | } |
| 465 | |
| 466 | static int frame_size_offset() { |
| 467 | return OFFSET_OF(FrameDescription, frame_size_); |
| 468 | } |
| 469 | |
| 470 | static int pc_offset() { |
| 471 | return OFFSET_OF(FrameDescription, pc_); |
| 472 | } |
| 473 | |
| 474 | static int state_offset() { |
| 475 | return OFFSET_OF(FrameDescription, state_); |
| 476 | } |
| 477 | |
| 478 | static int continuation_offset() { |
| 479 | return OFFSET_OF(FrameDescription, continuation_); |
| 480 | } |
| 481 | |
| 482 | static int frame_content_offset() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 483 | return OFFSET_OF(FrameDescription, frame_content_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | private: |
| 487 | static const uint32_t kZapUint32 = 0xbeeddead; |
| 488 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 489 | // Frame_size_ must hold a uint32_t value. It is only a uintptr_t to |
| 490 | // keep the variable-size array frame_content_ of type intptr_t at |
| 491 | // the end of the structure aligned. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 492 | uintptr_t frame_size_; // Number of bytes. |
| 493 | JSFunction* function_; |
| 494 | intptr_t registers_[Register::kNumRegisters]; |
| 495 | double double_registers_[DoubleRegister::kNumAllocatableRegisters]; |
| 496 | intptr_t top_; |
| 497 | intptr_t pc_; |
| 498 | intptr_t fp_; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 499 | StackFrame::Type type_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 500 | Smi* state_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 501 | #ifdef DEBUG |
| 502 | Code::Kind kind_; |
| 503 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 504 | |
| 505 | // Continuation is the PC where the execution continues after |
| 506 | // deoptimizing. |
| 507 | intptr_t continuation_; |
| 508 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 509 | // This must be at the end of the object as the object is allocated larger |
| 510 | // than it's definition indicate to extend this array. |
| 511 | intptr_t frame_content_[1]; |
| 512 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 513 | intptr_t* GetFrameSlotPointer(unsigned offset) { |
| 514 | ASSERT(offset < frame_size_); |
| 515 | return reinterpret_cast<intptr_t*>( |
| 516 | reinterpret_cast<Address>(this) + frame_content_offset() + offset); |
| 517 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 518 | |
| 519 | int ComputeFixedSize(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | |
| 523 | class TranslationBuffer BASE_EMBEDDED { |
| 524 | public: |
| 525 | TranslationBuffer() : contents_(256) { } |
| 526 | |
| 527 | int CurrentIndex() const { return contents_.length(); } |
| 528 | void Add(int32_t value); |
| 529 | |
| 530 | Handle<ByteArray> CreateByteArray(); |
| 531 | |
| 532 | private: |
| 533 | ZoneList<uint8_t> contents_; |
| 534 | }; |
| 535 | |
| 536 | |
| 537 | class TranslationIterator BASE_EMBEDDED { |
| 538 | public: |
| 539 | TranslationIterator(ByteArray* buffer, int index) |
| 540 | : buffer_(buffer), index_(index) { |
| 541 | ASSERT(index >= 0 && index < buffer->length()); |
| 542 | } |
| 543 | |
| 544 | int32_t Next(); |
| 545 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 546 | bool HasNext() const { return index_ < buffer_->length(); } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 547 | |
| 548 | void Skip(int n) { |
| 549 | for (int i = 0; i < n; i++) Next(); |
| 550 | } |
| 551 | |
| 552 | private: |
| 553 | ByteArray* buffer_; |
| 554 | int index_; |
| 555 | }; |
| 556 | |
| 557 | |
| 558 | class Translation BASE_EMBEDDED { |
| 559 | public: |
| 560 | enum Opcode { |
| 561 | BEGIN, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 562 | JS_FRAME, |
| 563 | ARGUMENTS_ADAPTOR_FRAME, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 564 | REGISTER, |
| 565 | INT32_REGISTER, |
| 566 | DOUBLE_REGISTER, |
| 567 | STACK_SLOT, |
| 568 | INT32_STACK_SLOT, |
| 569 | DOUBLE_STACK_SLOT, |
| 570 | LITERAL, |
| 571 | ARGUMENTS_OBJECT, |
| 572 | |
| 573 | // A prefix indicating that the next command is a duplicate of the one |
| 574 | // that follows it. |
| 575 | DUPLICATE |
| 576 | }; |
| 577 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 578 | Translation(TranslationBuffer* buffer, int frame_count, int jsframe_count) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 579 | : buffer_(buffer), |
| 580 | index_(buffer->CurrentIndex()) { |
| 581 | buffer_->Add(BEGIN); |
| 582 | buffer_->Add(frame_count); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 583 | buffer_->Add(jsframe_count); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | int index() const { return index_; } |
| 587 | |
| 588 | // Commands. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 589 | void BeginJSFrame(int node_id, int literal_id, unsigned height); |
| 590 | void BeginArgumentsAdaptorFrame(int literal_id, unsigned height); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 591 | void StoreRegister(Register reg); |
| 592 | void StoreInt32Register(Register reg); |
| 593 | void StoreDoubleRegister(DoubleRegister reg); |
| 594 | void StoreStackSlot(int index); |
| 595 | void StoreInt32StackSlot(int index); |
| 596 | void StoreDoubleStackSlot(int index); |
| 597 | void StoreLiteral(int literal_id); |
| 598 | void StoreArgumentsObject(); |
| 599 | void MarkDuplicate(); |
| 600 | |
| 601 | static int NumberOfOperandsFor(Opcode opcode); |
| 602 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 603 | #if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 604 | static const char* StringFor(Opcode opcode); |
| 605 | #endif |
| 606 | |
| 607 | private: |
| 608 | TranslationBuffer* buffer_; |
| 609 | int index_; |
| 610 | }; |
| 611 | |
| 612 | |
| 613 | // Linked list holding deoptimizing code objects. The deoptimizing code objects |
| 614 | // are kept as weak handles until they are no longer activated on the stack. |
| 615 | class DeoptimizingCodeListNode : public Malloced { |
| 616 | public: |
| 617 | explicit DeoptimizingCodeListNode(Code* code); |
| 618 | ~DeoptimizingCodeListNode(); |
| 619 | |
| 620 | DeoptimizingCodeListNode* next() const { return next_; } |
| 621 | void set_next(DeoptimizingCodeListNode* next) { next_ = next; } |
| 622 | Handle<Code> code() const { return code_; } |
| 623 | |
| 624 | private: |
| 625 | // Global (weak) handle to the deoptimizing code object. |
| 626 | Handle<Code> code_; |
| 627 | |
| 628 | // Next pointer for linked list. |
| 629 | DeoptimizingCodeListNode* next_; |
| 630 | }; |
| 631 | |
| 632 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 633 | class SlotRef BASE_EMBEDDED { |
| 634 | public: |
| 635 | enum SlotRepresentation { |
| 636 | UNKNOWN, |
| 637 | TAGGED, |
| 638 | INT32, |
| 639 | DOUBLE, |
| 640 | LITERAL |
| 641 | }; |
| 642 | |
| 643 | SlotRef() |
| 644 | : addr_(NULL), representation_(UNKNOWN) { } |
| 645 | |
| 646 | SlotRef(Address addr, SlotRepresentation representation) |
| 647 | : addr_(addr), representation_(representation) { } |
| 648 | |
| 649 | explicit SlotRef(Object* literal) |
| 650 | : literal_(literal), representation_(LITERAL) { } |
| 651 | |
| 652 | Handle<Object> GetValue() { |
| 653 | switch (representation_) { |
| 654 | case TAGGED: |
| 655 | return Handle<Object>(Memory::Object_at(addr_)); |
| 656 | |
| 657 | case INT32: { |
| 658 | int value = Memory::int32_at(addr_); |
| 659 | if (Smi::IsValid(value)) { |
| 660 | return Handle<Object>(Smi::FromInt(value)); |
| 661 | } else { |
| 662 | return Isolate::Current()->factory()->NewNumberFromInt(value); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | case DOUBLE: { |
| 667 | double value = Memory::double_at(addr_); |
| 668 | return Isolate::Current()->factory()->NewNumber(value); |
| 669 | } |
| 670 | |
| 671 | case LITERAL: |
| 672 | return literal_; |
| 673 | |
| 674 | default: |
| 675 | UNREACHABLE(); |
| 676 | return Handle<Object>::null(); |
| 677 | } |
| 678 | } |
| 679 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 680 | static Vector<SlotRef> ComputeSlotMappingForArguments( |
| 681 | JavaScriptFrame* frame, |
| 682 | int inlined_frame_index, |
| 683 | int formal_parameter_count); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 684 | |
| 685 | private: |
| 686 | Address addr_; |
| 687 | Handle<Object> literal_; |
| 688 | SlotRepresentation representation_; |
| 689 | |
| 690 | static Address SlotAddress(JavaScriptFrame* frame, int slot_index) { |
| 691 | if (slot_index >= 0) { |
| 692 | const int offset = JavaScriptFrameConstants::kLocal0Offset; |
| 693 | return frame->fp() + offset - (slot_index * kPointerSize); |
| 694 | } else { |
| 695 | const int offset = JavaScriptFrameConstants::kLastParameterOffset; |
| 696 | return frame->fp() + offset - ((slot_index + 1) * kPointerSize); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static SlotRef ComputeSlotForNextArgument(TranslationIterator* iterator, |
| 701 | DeoptimizationInputData* data, |
| 702 | JavaScriptFrame* frame); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 703 | |
| 704 | static void ComputeSlotsForArguments( |
| 705 | Vector<SlotRef>* args_slots, |
| 706 | TranslationIterator* iterator, |
| 707 | DeoptimizationInputData* data, |
| 708 | JavaScriptFrame* frame); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 709 | }; |
| 710 | |
| 711 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 712 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 713 | // Class used to represent an unoptimized frame when the debugger |
| 714 | // needs to inspect a frame that is part of an optimized frame. The |
| 715 | // internally used FrameDescription objects are not GC safe so for use |
| 716 | // by the debugger frame information is copied to an object of this type. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 717 | // Represents parameters in unadapted form so their number might mismatch |
| 718 | // formal parameter count. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 719 | class DeoptimizedFrameInfo : public Malloced { |
| 720 | public: |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 721 | DeoptimizedFrameInfo(Deoptimizer* deoptimizer, |
| 722 | int frame_index, |
| 723 | bool has_arguments_adaptor); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 724 | virtual ~DeoptimizedFrameInfo(); |
| 725 | |
| 726 | // GC support. |
| 727 | void Iterate(ObjectVisitor* v); |
| 728 | |
| 729 | // Return the number of incoming arguments. |
| 730 | int parameters_count() { return parameters_count_; } |
| 731 | |
| 732 | // Return the height of the expression stack. |
| 733 | int expression_count() { return expression_count_; } |
| 734 | |
| 735 | // Get the frame function. |
| 736 | JSFunction* GetFunction() { |
| 737 | return function_; |
| 738 | } |
| 739 | |
| 740 | // Get an incoming argument. |
| 741 | Object* GetParameter(int index) { |
| 742 | ASSERT(0 <= index && index < parameters_count()); |
| 743 | return parameters_[index]; |
| 744 | } |
| 745 | |
| 746 | // Get an expression from the expression stack. |
| 747 | Object* GetExpression(int index) { |
| 748 | ASSERT(0 <= index && index < expression_count()); |
| 749 | return expression_stack_[index]; |
| 750 | } |
| 751 | |
| 752 | private: |
| 753 | // Set the frame function. |
| 754 | void SetFunction(JSFunction* function) { |
| 755 | function_ = function; |
| 756 | } |
| 757 | |
| 758 | // Set an incoming argument. |
| 759 | void SetParameter(int index, Object* obj) { |
| 760 | ASSERT(0 <= index && index < parameters_count()); |
| 761 | parameters_[index] = obj; |
| 762 | } |
| 763 | |
| 764 | // Set an expression on the expression stack. |
| 765 | void SetExpression(int index, Object* obj) { |
| 766 | ASSERT(0 <= index && index < expression_count()); |
| 767 | expression_stack_[index] = obj; |
| 768 | } |
| 769 | |
| 770 | JSFunction* function_; |
| 771 | int parameters_count_; |
| 772 | int expression_count_; |
| 773 | Object** parameters_; |
| 774 | Object** expression_stack_; |
| 775 | |
| 776 | friend class Deoptimizer; |
| 777 | }; |
| 778 | #endif |
| 779 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 780 | } } // namespace v8::internal |
| 781 | |
| 782 | #endif // V8_DEOPTIMIZER_H_ |