blob: 8b1152d52957a7da6e37ecb56c4a07d17767accd [file] [log] [blame]
Ben Murdochc7cc0282012-03-05 14:35:55 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// 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 Murdoch257744e2011-11-30 15:57:28 +000033#include "allocation.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010034#include "macro-assembler.h"
35#include "zone-inl.h"
36
37
38namespace v8 {
39namespace internal {
40
41class FrameDescription;
42class TranslationIterator;
43class DeoptimizingCodeListNode;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000044class DeoptimizedFrameInfo;
Ben Murdochb0fe1622011-05-05 13:52:32 +010045
Ben Murdoch8b112d22011-06-08 16:22:53 +010046class HeapNumberMaterializationDescriptor BASE_EMBEDDED {
Ben Murdochb0fe1622011-05-05 13:52:32 +010047 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +010048 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 Murdochb0fe1622011-05-05 13:52:32 +010053
54 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +010055 Address slot_address_;
56 double val_;
Ben Murdochb0fe1622011-05-05 13:52:32 +010057};
58
59
60class 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 Block44f0eee2011-05-26 01:26:41 +010076class Deoptimizer;
77
78
79class DeoptimizerData {
80 public:
81 DeoptimizerData();
82 ~DeoptimizerData();
83
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000084#ifdef ENABLE_DEBUGGER_SUPPORT
85 void Iterate(ObjectVisitor* v);
86#endif
87
Steve Block44f0eee2011-05-26 01:26:41 +010088 private:
Ben Murdoch592a9fc2012-03-05 11:04:45 +000089 MemoryChunk* eager_deoptimization_entry_code_;
90 MemoryChunk* lazy_deoptimization_entry_code_;
Steve Block44f0eee2011-05-26 01:26:41 +010091 Deoptimizer* current_;
92
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000093#ifdef ENABLE_DEBUGGER_SUPPORT
94 DeoptimizedFrameInfo* deoptimized_frame_info_;
95#endif
96
Steve Block44f0eee2011-05-26 01:26:41 +010097 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100109class Deoptimizer : public Malloced {
110 public:
111 enum BailoutType {
112 EAGER,
113 LAZY,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000114 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 Murdochb0fe1622011-05-05 13:52:32 +0100118 };
119
120 int output_count() const { return output_count_; }
121
Ben Murdochc7cc0282012-03-05 14:35:55 +0000122 // Number of created JS frames. Not all created frames are necessarily JS.
123 int jsframe_count() const { return jsframe_count_; }
124
Ben Murdochb0fe1622011-05-05 13:52:32 +0100125 static Deoptimizer* New(JSFunction* function,
126 BailoutType type,
127 unsigned bailout_id,
128 Address from,
Steve Block44f0eee2011-05-26 01:26:41 +0100129 int fp_to_sp_delta,
130 Isolate* isolate);
131 static Deoptimizer* Grab(Isolate* isolate);
132
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000133#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 Murdochc7cc0282012-03-05 14:35:55 +0000137 int jsframe_index,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000138 Isolate* isolate);
139 static void DeleteDebuggerInspectableFrame(DeoptimizedFrameInfo* info,
140 Isolate* isolate);
141#endif
142
Steve Block44f0eee2011-05-26 01:26:41 +0100143 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100149
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 Block1e0659c2011-05-24 12:43:12 +0100168 // The size in bytes of the code required at a lazy deopt patch site.
169 static int patch_size();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100170
Steve Block1e0659c2011-05-24 12:43:12 +0100171 // 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 Murdoch592a9fc2012-03-05 11:04:45 +0000179 static void PatchStackCheckCodeAt(Code* unoptimized_code,
180 Address pc_after,
Steve Block1e0659c2011-05-24 12:43:12 +0100181 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 Murdoch592a9fc2012-03-05 11:04:45 +0000192 static void RevertStackCheckCodeAt(Code* unoptimized_code,
193 Address pc_after,
Steve Block1e0659c2011-05-24 12:43:12 +0100194 Code* check_code,
195 Code* replacement_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100196
197 ~Deoptimizer();
198
Ben Murdoch8b112d22011-06-08 16:22:53 +0100199 void MaterializeHeapNumbers();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000200#ifdef ENABLE_DEBUGGER_SUPPORT
201 void MaterializeHeapNumbersForDebuggerInspectableFrame(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000202 Address parameters_top,
203 uint32_t parameters_size,
204 Address expressions_top,
205 uint32_t expressions_size,
206 DeoptimizedFrameInfo* info);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000207#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100208
Ben Murdoch8b112d22011-06-08 16:22:53 +0100209 static void ComputeOutputFrames(Deoptimizer* deoptimizer);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100210
211 static Address GetDeoptimizationEntry(int id, BailoutType type);
212 static int GetDeoptimizationId(Address addr, BailoutType type);
Steve Block9fac8402011-05-12 15:51:54 +0100213 static int GetOutputInfo(DeoptimizationOutputData* data,
214 unsigned node_id,
215 SharedFunctionInfo* shared);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100216
Ben Murdochb0fe1622011-05-05 13:52:32 +0100217 // 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 Murdoch592a9fc2012-03-05 11:04:45 +0000223 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 Murdochb0fe1622011-05-05 13:52:32 +0100228
Steve Block44f0eee2011-05-26 01:26:41 +0100229 static int GetDeoptimizedCodeCount(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100230
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 Murdochc7cc0282012-03-05 14:35:55 +0000267 int ConvertJSFrameIndexToFrameIndex(int jsframe_index);
268
Ben Murdochb0fe1622011-05-05 13:52:32 +0100269 private:
Ben Murdochc7cc0282012-03-05 14:35:55 +0000270#ifdef V8_TARGET_ARCH_MIPS
Ben Murdochb0fe1622011-05-05 13:52:32 +0100271 static const int kNumberOfEntries = 4096;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000272#else
273 static const int kNumberOfEntries = 8192;
274#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100275
Steve Block44f0eee2011-05-26 01:26:41 +0100276 Deoptimizer(Isolate* isolate,
277 JSFunction* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100278 BailoutType type,
279 unsigned bailout_id,
280 Address from,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000281 int fp_to_sp_delta,
282 Code* optimized_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100283 void DeleteFrameDescriptions();
284
285 void DoComputeOutputFrames();
286 void DoComputeOsrOutputFrame();
Ben Murdochc7cc0282012-03-05 14:35:55 +0000287 void DoComputeJSFrame(TranslationIterator* iterator, int frame_index);
288 void DoComputeArgumentsAdaptorFrame(TranslationIterator* iterator,
289 int frame_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100290 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 Murdoch8b112d22011-06-08 16:22:53 +0100308 void AddDoubleValue(intptr_t slot_address, double value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100309
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000310 static MemoryChunk* CreateCode(BailoutType type);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100311 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 Murdoch3fb3ca82011-12-02 17:19:32 +0000320 // 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 Block44f0eee2011-05-26 01:26:41 +0100325 Isolate* isolate_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326 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 Murdochc7cc0282012-03-05 14:35:55 +0000337 // Number of output js frames.
338 int jsframe_count_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100339 // Array of output frame descriptions.
340 FrameDescription** output_;
341
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000342 // Frames can be dynamically padded on ia32 to align untagged doubles.
343 Object* frame_alignment_marker_;
344 intptr_t has_alignment_padding_;
345
Ben Murdoch8b112d22011-06-08 16:22:53 +0100346 List<HeapNumberMaterializationDescriptor> deferred_heap_numbers_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100347
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000348 static const int table_entry_size_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100349
350 friend class FrameDescription;
351 friend class DeoptimizingCodeListNode;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000352 friend class DeoptimizedFrameInfo;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100353};
354
355
356class 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 Block44f0eee2011-05-26 01:26:41 +0100362 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100365 }
366
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000367 void operator delete(void* pointer, uint32_t frame_size) {
368 free(pointer);
369 }
370
Ben Murdochb0fe1622011-05-05 13:52:32 +0100371 void operator delete(void* description) {
372 free(description);
373 }
374
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000375 uint32_t GetFrameSize() const {
376 ASSERT(static_cast<uint32_t>(frame_size_) == frame_size_);
377 return static_cast<uint32_t>(frame_size_);
378 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100379
380 JSFunction* GetFunction() const { return function_; }
381
Ben Murdochc7cc0282012-03-05 14:35:55 +0000382 unsigned GetOffsetFromSlotIndex(int slot_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100383
384 intptr_t GetFrameSlot(unsigned offset) {
385 return *GetFrameSlotPointer(offset);
386 }
387
388 double GetDoubleFrameSlot(unsigned offset) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000389 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 Murdochb0fe1622011-05-05 13:52:32 +0100403 }
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 Murdochc7cc0282012-03-05 14:35:55 +0000443 StackFrame::Type GetFrameType() const { return type_; }
444 void SetFrameType(StackFrame::Type type) { type_ = type; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000445
446 // Get the incoming arguments count.
447 int ComputeParametersCount();
448
449 // Get a parameter value for an unoptimized frame.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000450 Object* GetParameter(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000451
452 // Get the expression stack height for a unoptimized frame.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000453 unsigned GetExpressionCount();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000454
455 // Get the expression stack value for an unoptimized frame.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000456 Object* GetExpression(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000457
Ben Murdochb0fe1622011-05-05 13:52:32 +0100458 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 Block44f0eee2011-05-26 01:26:41 +0100483 return OFFSET_OF(FrameDescription, frame_content_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100484 }
485
486 private:
487 static const uint32_t kZapUint32 = 0xbeeddead;
488
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000489 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100492 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 Murdochc7cc0282012-03-05 14:35:55 +0000499 StackFrame::Type type_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100500 Smi* state_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000501#ifdef DEBUG
502 Code::Kind kind_;
503#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100504
505 // Continuation is the PC where the execution continues after
506 // deoptimizing.
507 intptr_t continuation_;
508
Steve Block44f0eee2011-05-26 01:26:41 +0100509 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100513 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 Murdochc7cc0282012-03-05 14:35:55 +0000518
519 int ComputeFixedSize();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100520};
521
522
523class 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
537class 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 Murdoch69a99ed2011-11-30 16:03:39 +0000546 bool HasNext() const { return index_ < buffer_->length(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100547
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
558class Translation BASE_EMBEDDED {
559 public:
560 enum Opcode {
561 BEGIN,
Ben Murdochc7cc0282012-03-05 14:35:55 +0000562 JS_FRAME,
563 ARGUMENTS_ADAPTOR_FRAME,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100564 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 Murdochc7cc0282012-03-05 14:35:55 +0000578 Translation(TranslationBuffer* buffer, int frame_count, int jsframe_count)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100579 : buffer_(buffer),
580 index_(buffer->CurrentIndex()) {
581 buffer_->Add(BEGIN);
582 buffer_->Add(frame_count);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000583 buffer_->Add(jsframe_count);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100584 }
585
586 int index() const { return index_; }
587
588 // Commands.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000589 void BeginJSFrame(int node_id, int literal_id, unsigned height);
590 void BeginArgumentsAdaptorFrame(int literal_id, unsigned height);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100591 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 Murdoch3fb3ca82011-12-02 17:19:32 +0000603#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100604 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.
615class 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 Murdoch8b112d22011-06-08 16:22:53 +0100633class 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 Murdochc7cc0282012-03-05 14:35:55 +0000680 static Vector<SlotRef> ComputeSlotMappingForArguments(
681 JavaScriptFrame* frame,
682 int inlined_frame_index,
683 int formal_parameter_count);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100684
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 Murdochc7cc0282012-03-05 14:35:55 +0000703
704 static void ComputeSlotsForArguments(
705 Vector<SlotRef>* args_slots,
706 TranslationIterator* iterator,
707 DeoptimizationInputData* data,
708 JavaScriptFrame* frame);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100709};
710
711
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000712#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 Murdochc7cc0282012-03-05 14:35:55 +0000717// Represents parameters in unadapted form so their number might mismatch
718// formal parameter count.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000719class DeoptimizedFrameInfo : public Malloced {
720 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +0000721 DeoptimizedFrameInfo(Deoptimizer* deoptimizer,
722 int frame_index,
723 bool has_arguments_adaptor);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000724 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 Murdochb0fe1622011-05-05 13:52:32 +0100780} } // namespace v8::internal
781
782#endif // V8_DEOPTIMIZER_H_