blob: 284676c36eeef1152ef3731d716240b5058aeb25 [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 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
122 static Deoptimizer* New(JSFunction* function,
123 BailoutType type,
124 unsigned bailout_id,
125 Address from,
Steve Block44f0eee2011-05-26 01:26:41 +0100126 int fp_to_sp_delta,
127 Isolate* isolate);
128 static Deoptimizer* Grab(Isolate* isolate);
129
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000130#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 Block44f0eee2011-05-26 01:26:41 +0100140 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100146
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 Block1e0659c2011-05-24 12:43:12 +0100165 // The size in bytes of the code required at a lazy deopt patch site.
166 static int patch_size();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100167
Steve Block1e0659c2011-05-24 12:43:12 +0100168 // 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.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000176 static void PatchStackCheckCodeAt(Code* unoptimized_code,
177 Address pc_after,
Steve Block1e0659c2011-05-24 12:43:12 +0100178 Code* check_code,
179 Code* replacement_code);
180
181 // Change all patched stack guard checks in the unoptimized code
182 // back to a normal stack guard check.
183 static void RevertStackCheckCode(Code* unoptimized_code,
184 Code* check_code,
185 Code* replacement_code);
186
187 // Change all patched stack guard checks in the unoptimized code
188 // back to a normal stack guard check.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000189 static void RevertStackCheckCodeAt(Code* unoptimized_code,
190 Address pc_after,
Steve Block1e0659c2011-05-24 12:43:12 +0100191 Code* check_code,
192 Code* replacement_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100193
194 ~Deoptimizer();
195
Ben Murdoch8b112d22011-06-08 16:22:53 +0100196 void MaterializeHeapNumbers();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000197#ifdef ENABLE_DEBUGGER_SUPPORT
198 void MaterializeHeapNumbersForDebuggerInspectableFrame(
199 Address top, uint32_t size, DeoptimizedFrameInfo* info);
200#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100201
Ben Murdoch8b112d22011-06-08 16:22:53 +0100202 static void ComputeOutputFrames(Deoptimizer* deoptimizer);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100203
204 static Address GetDeoptimizationEntry(int id, BailoutType type);
205 static int GetDeoptimizationId(Address addr, BailoutType type);
Steve Block9fac8402011-05-12 15:51:54 +0100206 static int GetOutputInfo(DeoptimizationOutputData* data,
207 unsigned node_id,
208 SharedFunctionInfo* shared);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209
Ben Murdochb0fe1622011-05-05 13:52:32 +0100210 // Code generation support.
211 static int input_offset() { return OFFSET_OF(Deoptimizer, input_); }
212 static int output_count_offset() {
213 return OFFSET_OF(Deoptimizer, output_count_);
214 }
215 static int output_offset() { return OFFSET_OF(Deoptimizer, output_); }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000216 static int frame_alignment_marker_offset() {
217 return OFFSET_OF(Deoptimizer, frame_alignment_marker_); }
218 static int has_alignment_padding_offset() {
219 return OFFSET_OF(Deoptimizer, has_alignment_padding_);
220 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100221
Steve Block44f0eee2011-05-26 01:26:41 +0100222 static int GetDeoptimizedCodeCount(Isolate* isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100223
224 static const int kNotDeoptimizationEntry = -1;
225
226 // Generators for the deoptimization entry code.
227 class EntryGenerator BASE_EMBEDDED {
228 public:
229 EntryGenerator(MacroAssembler* masm, BailoutType type)
230 : masm_(masm), type_(type) { }
231 virtual ~EntryGenerator() { }
232
233 void Generate();
234
235 protected:
236 MacroAssembler* masm() const { return masm_; }
237 BailoutType type() const { return type_; }
238
239 virtual void GeneratePrologue() { }
240
241 private:
242 MacroAssembler* masm_;
243 Deoptimizer::BailoutType type_;
244 };
245
246 class TableEntryGenerator : public EntryGenerator {
247 public:
248 TableEntryGenerator(MacroAssembler* masm, BailoutType type, int count)
249 : EntryGenerator(masm, type), count_(count) { }
250
251 protected:
252 virtual void GeneratePrologue();
253
254 private:
255 int count() const { return count_; }
256
257 int count_;
258 };
259
260 private:
261 static const int kNumberOfEntries = 4096;
262
Steve Block44f0eee2011-05-26 01:26:41 +0100263 Deoptimizer(Isolate* isolate,
264 JSFunction* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100265 BailoutType type,
266 unsigned bailout_id,
267 Address from,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000268 int fp_to_sp_delta,
269 Code* optimized_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100270 void DeleteFrameDescriptions();
271
272 void DoComputeOutputFrames();
273 void DoComputeOsrOutputFrame();
274 void DoComputeFrame(TranslationIterator* iterator, int frame_index);
275 void DoTranslateCommand(TranslationIterator* iterator,
276 int frame_index,
277 unsigned output_offset);
278 // Translate a command for OSR. Updates the input offset to be used for
279 // the next command. Returns false if translation of the command failed
280 // (e.g., a number conversion failed) and may or may not have updated the
281 // input offset.
282 bool DoOsrTranslateCommand(TranslationIterator* iterator,
283 int* input_offset);
284
285 unsigned ComputeInputFrameSize() const;
286 unsigned ComputeFixedSize(JSFunction* function) const;
287
288 unsigned ComputeIncomingArgumentSize(JSFunction* function) const;
289 unsigned ComputeOutgoingArgumentSize() const;
290
291 Object* ComputeLiteral(int index) const;
292
Ben Murdoch8b112d22011-06-08 16:22:53 +0100293 void AddDoubleValue(intptr_t slot_address, double value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100294
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000295 static MemoryChunk* CreateCode(BailoutType type);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100296 static void GenerateDeoptimizationEntries(
297 MacroAssembler* masm, int count, BailoutType type);
298
299 // Weak handle callback for deoptimizing code objects.
300 static void HandleWeakDeoptimizedCode(
301 v8::Persistent<v8::Value> obj, void* data);
302 static Code* FindDeoptimizingCodeFromAddress(Address addr);
303 static void RemoveDeoptimizingCode(Code* code);
304
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000305 // Fill the input from from a JavaScript frame. This is used when
306 // the debugger needs to inspect an optimized frame. For normal
307 // deoptimizations the input frame is filled in generated code.
308 void FillInputFrame(Address tos, JavaScriptFrame* frame);
309
Steve Block44f0eee2011-05-26 01:26:41 +0100310 Isolate* isolate_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100311 JSFunction* function_;
312 Code* optimized_code_;
313 unsigned bailout_id_;
314 BailoutType bailout_type_;
315 Address from_;
316 int fp_to_sp_delta_;
317
318 // Input frame description.
319 FrameDescription* input_;
320 // Number of output frames.
321 int output_count_;
322 // Array of output frame descriptions.
323 FrameDescription** output_;
324
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000325 // Frames can be dynamically padded on ia32 to align untagged doubles.
326 Object* frame_alignment_marker_;
327 intptr_t has_alignment_padding_;
328
Ben Murdoch8b112d22011-06-08 16:22:53 +0100329 List<HeapNumberMaterializationDescriptor> deferred_heap_numbers_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100330
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000331 static const int table_entry_size_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100332
333 friend class FrameDescription;
334 friend class DeoptimizingCodeListNode;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000335 friend class DeoptimizedFrameInfo;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100336};
337
338
339class FrameDescription {
340 public:
341 FrameDescription(uint32_t frame_size,
342 JSFunction* function);
343
344 void* operator new(size_t size, uint32_t frame_size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100345 // Subtracts kPointerSize, as the member frame_content_ already supplies
346 // the first element of the area to store the frame.
347 return malloc(size + frame_size - kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100348 }
349
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000350 void operator delete(void* pointer, uint32_t frame_size) {
351 free(pointer);
352 }
353
Ben Murdochb0fe1622011-05-05 13:52:32 +0100354 void operator delete(void* description) {
355 free(description);
356 }
357
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000358 uint32_t GetFrameSize() const {
359 ASSERT(static_cast<uint32_t>(frame_size_) == frame_size_);
360 return static_cast<uint32_t>(frame_size_);
361 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100362
363 JSFunction* GetFunction() const { return function_; }
364
365 unsigned GetOffsetFromSlotIndex(Deoptimizer* deoptimizer, int slot_index);
366
367 intptr_t GetFrameSlot(unsigned offset) {
368 return *GetFrameSlotPointer(offset);
369 }
370
371 double GetDoubleFrameSlot(unsigned offset) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000372 intptr_t* ptr = GetFrameSlotPointer(offset);
373#if V8_TARGET_ARCH_MIPS
374 // Prevent gcc from using load-double (mips ldc1) on (possibly)
375 // non-64-bit aligned double. Uses two lwc1 instructions.
376 union conversion {
377 double d;
378 uint32_t u[2];
379 } c;
380 c.u[0] = *reinterpret_cast<uint32_t*>(ptr);
381 c.u[1] = *(reinterpret_cast<uint32_t*>(ptr) + 1);
382 return c.d;
383#else
384 return *reinterpret_cast<double*>(ptr);
385#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100386 }
387
388 void SetFrameSlot(unsigned offset, intptr_t value) {
389 *GetFrameSlotPointer(offset) = value;
390 }
391
392 intptr_t GetRegister(unsigned n) const {
393 ASSERT(n < ARRAY_SIZE(registers_));
394 return registers_[n];
395 }
396
397 double GetDoubleRegister(unsigned n) const {
398 ASSERT(n < ARRAY_SIZE(double_registers_));
399 return double_registers_[n];
400 }
401
402 void SetRegister(unsigned n, intptr_t value) {
403 ASSERT(n < ARRAY_SIZE(registers_));
404 registers_[n] = value;
405 }
406
407 void SetDoubleRegister(unsigned n, double value) {
408 ASSERT(n < ARRAY_SIZE(double_registers_));
409 double_registers_[n] = value;
410 }
411
412 intptr_t GetTop() const { return top_; }
413 void SetTop(intptr_t top) { top_ = top; }
414
415 intptr_t GetPc() const { return pc_; }
416 void SetPc(intptr_t pc) { pc_ = pc; }
417
418 intptr_t GetFp() const { return fp_; }
419 void SetFp(intptr_t fp) { fp_ = fp; }
420
421 Smi* GetState() const { return state_; }
422 void SetState(Smi* state) { state_ = state; }
423
424 void SetContinuation(intptr_t pc) { continuation_ = pc; }
425
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000426#ifdef DEBUG
427 Code::Kind GetKind() const { return kind_; }
428 void SetKind(Code::Kind kind) { kind_ = kind; }
429#endif
430
431 // Get the incoming arguments count.
432 int ComputeParametersCount();
433
434 // Get a parameter value for an unoptimized frame.
435 Object* GetParameter(Deoptimizer* deoptimizer, int index);
436
437 // Get the expression stack height for a unoptimized frame.
438 unsigned GetExpressionCount(Deoptimizer* deoptimizer);
439
440 // Get the expression stack value for an unoptimized frame.
441 Object* GetExpression(Deoptimizer* deoptimizer, int index);
442
Ben Murdochb0fe1622011-05-05 13:52:32 +0100443 static int registers_offset() {
444 return OFFSET_OF(FrameDescription, registers_);
445 }
446
447 static int double_registers_offset() {
448 return OFFSET_OF(FrameDescription, double_registers_);
449 }
450
451 static int frame_size_offset() {
452 return OFFSET_OF(FrameDescription, frame_size_);
453 }
454
455 static int pc_offset() {
456 return OFFSET_OF(FrameDescription, pc_);
457 }
458
459 static int state_offset() {
460 return OFFSET_OF(FrameDescription, state_);
461 }
462
463 static int continuation_offset() {
464 return OFFSET_OF(FrameDescription, continuation_);
465 }
466
467 static int frame_content_offset() {
Steve Block44f0eee2011-05-26 01:26:41 +0100468 return OFFSET_OF(FrameDescription, frame_content_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100469 }
470
471 private:
472 static const uint32_t kZapUint32 = 0xbeeddead;
473
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000474 // Frame_size_ must hold a uint32_t value. It is only a uintptr_t to
475 // keep the variable-size array frame_content_ of type intptr_t at
476 // the end of the structure aligned.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100477 uintptr_t frame_size_; // Number of bytes.
478 JSFunction* function_;
479 intptr_t registers_[Register::kNumRegisters];
480 double double_registers_[DoubleRegister::kNumAllocatableRegisters];
481 intptr_t top_;
482 intptr_t pc_;
483 intptr_t fp_;
484 Smi* state_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000485#ifdef DEBUG
486 Code::Kind kind_;
487#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100488
489 // Continuation is the PC where the execution continues after
490 // deoptimizing.
491 intptr_t continuation_;
492
Steve Block44f0eee2011-05-26 01:26:41 +0100493 // This must be at the end of the object as the object is allocated larger
494 // than it's definition indicate to extend this array.
495 intptr_t frame_content_[1];
496
Ben Murdochb0fe1622011-05-05 13:52:32 +0100497 intptr_t* GetFrameSlotPointer(unsigned offset) {
498 ASSERT(offset < frame_size_);
499 return reinterpret_cast<intptr_t*>(
500 reinterpret_cast<Address>(this) + frame_content_offset() + offset);
501 }
502};
503
504
505class TranslationBuffer BASE_EMBEDDED {
506 public:
507 TranslationBuffer() : contents_(256) { }
508
509 int CurrentIndex() const { return contents_.length(); }
510 void Add(int32_t value);
511
512 Handle<ByteArray> CreateByteArray();
513
514 private:
515 ZoneList<uint8_t> contents_;
516};
517
518
519class TranslationIterator BASE_EMBEDDED {
520 public:
521 TranslationIterator(ByteArray* buffer, int index)
522 : buffer_(buffer), index_(index) {
523 ASSERT(index >= 0 && index < buffer->length());
524 }
525
526 int32_t Next();
527
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000528 bool HasNext() const { return index_ < buffer_->length(); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100529
530 void Skip(int n) {
531 for (int i = 0; i < n; i++) Next();
532 }
533
534 private:
535 ByteArray* buffer_;
536 int index_;
537};
538
539
540class Translation BASE_EMBEDDED {
541 public:
542 enum Opcode {
543 BEGIN,
544 FRAME,
545 REGISTER,
546 INT32_REGISTER,
547 DOUBLE_REGISTER,
548 STACK_SLOT,
549 INT32_STACK_SLOT,
550 DOUBLE_STACK_SLOT,
551 LITERAL,
552 ARGUMENTS_OBJECT,
553
554 // A prefix indicating that the next command is a duplicate of the one
555 // that follows it.
556 DUPLICATE
557 };
558
559 Translation(TranslationBuffer* buffer, int frame_count)
560 : buffer_(buffer),
561 index_(buffer->CurrentIndex()) {
562 buffer_->Add(BEGIN);
563 buffer_->Add(frame_count);
564 }
565
566 int index() const { return index_; }
567
568 // Commands.
569 void BeginFrame(int node_id, int literal_id, unsigned height);
570 void StoreRegister(Register reg);
571 void StoreInt32Register(Register reg);
572 void StoreDoubleRegister(DoubleRegister reg);
573 void StoreStackSlot(int index);
574 void StoreInt32StackSlot(int index);
575 void StoreDoubleStackSlot(int index);
576 void StoreLiteral(int literal_id);
577 void StoreArgumentsObject();
578 void MarkDuplicate();
579
580 static int NumberOfOperandsFor(Opcode opcode);
581
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000582#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100583 static const char* StringFor(Opcode opcode);
584#endif
585
586 private:
587 TranslationBuffer* buffer_;
588 int index_;
589};
590
591
592// Linked list holding deoptimizing code objects. The deoptimizing code objects
593// are kept as weak handles until they are no longer activated on the stack.
594class DeoptimizingCodeListNode : public Malloced {
595 public:
596 explicit DeoptimizingCodeListNode(Code* code);
597 ~DeoptimizingCodeListNode();
598
599 DeoptimizingCodeListNode* next() const { return next_; }
600 void set_next(DeoptimizingCodeListNode* next) { next_ = next; }
601 Handle<Code> code() const { return code_; }
602
603 private:
604 // Global (weak) handle to the deoptimizing code object.
605 Handle<Code> code_;
606
607 // Next pointer for linked list.
608 DeoptimizingCodeListNode* next_;
609};
610
611
Ben Murdoch8b112d22011-06-08 16:22:53 +0100612class SlotRef BASE_EMBEDDED {
613 public:
614 enum SlotRepresentation {
615 UNKNOWN,
616 TAGGED,
617 INT32,
618 DOUBLE,
619 LITERAL
620 };
621
622 SlotRef()
623 : addr_(NULL), representation_(UNKNOWN) { }
624
625 SlotRef(Address addr, SlotRepresentation representation)
626 : addr_(addr), representation_(representation) { }
627
628 explicit SlotRef(Object* literal)
629 : literal_(literal), representation_(LITERAL) { }
630
631 Handle<Object> GetValue() {
632 switch (representation_) {
633 case TAGGED:
634 return Handle<Object>(Memory::Object_at(addr_));
635
636 case INT32: {
637 int value = Memory::int32_at(addr_);
638 if (Smi::IsValid(value)) {
639 return Handle<Object>(Smi::FromInt(value));
640 } else {
641 return Isolate::Current()->factory()->NewNumberFromInt(value);
642 }
643 }
644
645 case DOUBLE: {
646 double value = Memory::double_at(addr_);
647 return Isolate::Current()->factory()->NewNumber(value);
648 }
649
650 case LITERAL:
651 return literal_;
652
653 default:
654 UNREACHABLE();
655 return Handle<Object>::null();
656 }
657 }
658
659 static void ComputeSlotMappingForArguments(JavaScriptFrame* frame,
660 int inlined_frame_index,
661 Vector<SlotRef>* args_slots);
662
663 private:
664 Address addr_;
665 Handle<Object> literal_;
666 SlotRepresentation representation_;
667
668 static Address SlotAddress(JavaScriptFrame* frame, int slot_index) {
669 if (slot_index >= 0) {
670 const int offset = JavaScriptFrameConstants::kLocal0Offset;
671 return frame->fp() + offset - (slot_index * kPointerSize);
672 } else {
673 const int offset = JavaScriptFrameConstants::kLastParameterOffset;
674 return frame->fp() + offset - ((slot_index + 1) * kPointerSize);
675 }
676 }
677
678 static SlotRef ComputeSlotForNextArgument(TranslationIterator* iterator,
679 DeoptimizationInputData* data,
680 JavaScriptFrame* frame);
681};
682
683
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000684#ifdef ENABLE_DEBUGGER_SUPPORT
685// Class used to represent an unoptimized frame when the debugger
686// needs to inspect a frame that is part of an optimized frame. The
687// internally used FrameDescription objects are not GC safe so for use
688// by the debugger frame information is copied to an object of this type.
689class DeoptimizedFrameInfo : public Malloced {
690 public:
691 DeoptimizedFrameInfo(Deoptimizer* deoptimizer, int frame_index);
692 virtual ~DeoptimizedFrameInfo();
693
694 // GC support.
695 void Iterate(ObjectVisitor* v);
696
697 // Return the number of incoming arguments.
698 int parameters_count() { return parameters_count_; }
699
700 // Return the height of the expression stack.
701 int expression_count() { return expression_count_; }
702
703 // Get the frame function.
704 JSFunction* GetFunction() {
705 return function_;
706 }
707
708 // Get an incoming argument.
709 Object* GetParameter(int index) {
710 ASSERT(0 <= index && index < parameters_count());
711 return parameters_[index];
712 }
713
714 // Get an expression from the expression stack.
715 Object* GetExpression(int index) {
716 ASSERT(0 <= index && index < expression_count());
717 return expression_stack_[index];
718 }
719
720 private:
721 // Set the frame function.
722 void SetFunction(JSFunction* function) {
723 function_ = function;
724 }
725
726 // Set an incoming argument.
727 void SetParameter(int index, Object* obj) {
728 ASSERT(0 <= index && index < parameters_count());
729 parameters_[index] = obj;
730 }
731
732 // Set an expression on the expression stack.
733 void SetExpression(int index, Object* obj) {
734 ASSERT(0 <= index && index < expression_count());
735 expression_stack_[index] = obj;
736 }
737
738 JSFunction* function_;
739 int parameters_count_;
740 int expression_count_;
741 Object** parameters_;
742 Object** expression_stack_;
743
744 friend class Deoptimizer;
745};
746#endif
747
Ben Murdochb0fe1622011-05-05 13:52:32 +0100748} } // namespace v8::internal
749
750#endif // V8_DEOPTIMIZER_H_