blob: 033d92480bd8a113f0c4c39e1a3633ec6f56a6ca [file] [log] [blame]
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// 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
lrn@chromium.org1c092762011-05-09 09:42:16 +000033#include "allocation.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "macro-assembler.h"
35#include "zone-inl.h"
36
37
38namespace v8 {
39namespace internal {
40
41class FrameDescription;
42class TranslationIterator;
43class DeoptimizingCodeListNode;
ricow@chromium.org4f693d62011-07-04 14:01:31 +000044class DeoptimizedFrameInfo;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000045
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000046class HeapNumberMaterializationDescriptor BASE_EMBEDDED {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000047 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000048 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_; }
kasperl@chromium.orga5551262010-12-07 12:49:48 +000053
54 private:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000055 Address slot_address_;
56 double val_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000057};
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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000076class Deoptimizer;
77
78
79class DeoptimizerData {
80 public:
81 DeoptimizerData();
82 ~DeoptimizerData();
83
ricow@chromium.org4f693d62011-07-04 14:01:31 +000084#ifdef ENABLE_DEBUGGER_SUPPORT
85 void Iterate(ObjectVisitor* v);
86#endif
87
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000088 private:
89 LargeObjectChunk* eager_deoptimization_entry_code_;
90 LargeObjectChunk* lazy_deoptimization_entry_code_;
91 Deoptimizer* current_;
92
ricow@chromium.org4f693d62011-07-04 14:01:31 +000093#ifdef ENABLE_DEBUGGER_SUPPORT
94 DeoptimizedFrameInfo* deoptimized_frame_info_;
95#endif
96
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097 // 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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000109class Deoptimizer : public Malloced {
110 public:
111 enum BailoutType {
112 EAGER,
113 LAZY,
ricow@chromium.org4f693d62011-07-04 14:01:31 +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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000118 };
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,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000126 int fp_to_sp_delta,
127 Isolate* isolate);
128 static Deoptimizer* Grab(Isolate* isolate);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129
ricow@chromium.org4f693d62011-07-04 14:01:31 +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
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000140 // 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);
146
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000147 // 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
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000165 // The size in bytes of the code required at a lazy deopt patch site.
166 static int patch_size();
167
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000168 // 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);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000173
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000174 // Patch stack guard check at instruction before pc_after in
175 // the unoptimized code to unconditionally call replacement_code.
176 static void PatchStackCheckCodeAt(Address pc_after,
177 Code* check_code,
178 Code* replacement_code);
179
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000180 // Change all patched stack guard checks in the unoptimized code
181 // back to a normal stack guard check.
182 static void RevertStackCheckCode(Code* unoptimized_code,
183 Code* check_code,
184 Code* replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000185
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000186 // Change all patched stack guard checks in the unoptimized code
187 // back to a normal stack guard check.
188 static void RevertStackCheckCodeAt(Address pc_after,
189 Code* check_code,
190 Code* replacement_code);
191
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000192 ~Deoptimizer();
193
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000194 void MaterializeHeapNumbers();
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000195#ifdef ENABLE_DEBUGGER_SUPPORT
196 void MaterializeHeapNumbersForDebuggerInspectableFrame(
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000197 Address top, uint32_t size, DeoptimizedFrameInfo* info);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000198#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000199
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000200 static void ComputeOutputFrames(Deoptimizer* deoptimizer);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000201
202 static Address GetDeoptimizationEntry(int id, BailoutType type);
203 static int GetDeoptimizationId(Address addr, BailoutType type);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000204 static int GetOutputInfo(DeoptimizationOutputData* data,
205 unsigned node_id,
206 SharedFunctionInfo* shared);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000207
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000208 // Code generation support.
209 static int input_offset() { return OFFSET_OF(Deoptimizer, input_); }
210 static int output_count_offset() {
211 return OFFSET_OF(Deoptimizer, output_count_);
212 }
213 static int output_offset() { return OFFSET_OF(Deoptimizer, output_); }
214
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000215 static int GetDeoptimizedCodeCount(Isolate* isolate);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000216
217 static const int kNotDeoptimizationEntry = -1;
218
219 // Generators for the deoptimization entry code.
220 class EntryGenerator BASE_EMBEDDED {
221 public:
222 EntryGenerator(MacroAssembler* masm, BailoutType type)
223 : masm_(masm), type_(type) { }
224 virtual ~EntryGenerator() { }
225
226 void Generate();
227
228 protected:
229 MacroAssembler* masm() const { return masm_; }
230 BailoutType type() const { return type_; }
231
232 virtual void GeneratePrologue() { }
233
234 private:
235 MacroAssembler* masm_;
236 Deoptimizer::BailoutType type_;
237 };
238
239 class TableEntryGenerator : public EntryGenerator {
240 public:
241 TableEntryGenerator(MacroAssembler* masm, BailoutType type, int count)
242 : EntryGenerator(masm, type), count_(count) { }
243
244 protected:
245 virtual void GeneratePrologue();
246
247 private:
248 int count() const { return count_; }
249
250 int count_;
251 };
252
253 private:
254 static const int kNumberOfEntries = 4096;
255
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000256 Deoptimizer(Isolate* isolate,
257 JSFunction* function,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000258 BailoutType type,
259 unsigned bailout_id,
260 Address from,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000261 int fp_to_sp_delta,
262 Code* optimized_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000263 void DeleteFrameDescriptions();
264
265 void DoComputeOutputFrames();
266 void DoComputeOsrOutputFrame();
267 void DoComputeFrame(TranslationIterator* iterator, int frame_index);
268 void DoTranslateCommand(TranslationIterator* iterator,
269 int frame_index,
270 unsigned output_offset);
271 // Translate a command for OSR. Updates the input offset to be used for
272 // the next command. Returns false if translation of the command failed
273 // (e.g., a number conversion failed) and may or may not have updated the
274 // input offset.
275 bool DoOsrTranslateCommand(TranslationIterator* iterator,
276 int* input_offset);
277
278 unsigned ComputeInputFrameSize() const;
279 unsigned ComputeFixedSize(JSFunction* function) const;
280
281 unsigned ComputeIncomingArgumentSize(JSFunction* function) const;
282 unsigned ComputeOutgoingArgumentSize() const;
283
284 Object* ComputeLiteral(int index) const;
285
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000286 void AddDoubleValue(intptr_t slot_address, double value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000287
288 static LargeObjectChunk* CreateCode(BailoutType type);
289 static void GenerateDeoptimizationEntries(
290 MacroAssembler* masm, int count, BailoutType type);
291
292 // Weak handle callback for deoptimizing code objects.
293 static void HandleWeakDeoptimizedCode(
294 v8::Persistent<v8::Value> obj, void* data);
295 static Code* FindDeoptimizingCodeFromAddress(Address addr);
296 static void RemoveDeoptimizingCode(Code* code);
297
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000298 // Fill the input from from a JavaScript frame. This is used when
299 // the debugger needs to inspect an optimized frame. For normal
300 // deoptimizations the input frame is filled in generated code.
301 void FillInputFrame(Address tos, JavaScriptFrame* frame);
302
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303 Isolate* isolate_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000304 JSFunction* function_;
305 Code* optimized_code_;
306 unsigned bailout_id_;
307 BailoutType bailout_type_;
308 Address from_;
309 int fp_to_sp_delta_;
310
311 // Input frame description.
312 FrameDescription* input_;
313 // Number of output frames.
314 int output_count_;
315 // Array of output frame descriptions.
316 FrameDescription** output_;
317
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000318 List<HeapNumberMaterializationDescriptor> deferred_heap_numbers_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000319
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000320 static const int table_entry_size_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000321
322 friend class FrameDescription;
323 friend class DeoptimizingCodeListNode;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000324 friend class DeoptimizedFrameInfo;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000325};
326
327
328class FrameDescription {
329 public:
330 FrameDescription(uint32_t frame_size,
331 JSFunction* function);
332
333 void* operator new(size_t size, uint32_t frame_size) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000334 // Subtracts kPointerSize, as the member frame_content_ already supplies
335 // the first element of the area to store the frame.
336 return malloc(size + frame_size - kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000337 }
338
339 void operator delete(void* description) {
340 free(description);
341 }
342
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000343 uint32_t GetFrameSize() const {
344 ASSERT(static_cast<uint32_t>(frame_size_) == frame_size_);
345 return static_cast<uint32_t>(frame_size_);
346 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000347
348 JSFunction* GetFunction() const { return function_; }
349
350 unsigned GetOffsetFromSlotIndex(Deoptimizer* deoptimizer, int slot_index);
351
352 intptr_t GetFrameSlot(unsigned offset) {
353 return *GetFrameSlotPointer(offset);
354 }
355
356 double GetDoubleFrameSlot(unsigned offset) {
357 return *reinterpret_cast<double*>(GetFrameSlotPointer(offset));
358 }
359
360 void SetFrameSlot(unsigned offset, intptr_t value) {
361 *GetFrameSlotPointer(offset) = value;
362 }
363
364 intptr_t GetRegister(unsigned n) const {
365 ASSERT(n < ARRAY_SIZE(registers_));
366 return registers_[n];
367 }
368
369 double GetDoubleRegister(unsigned n) const {
370 ASSERT(n < ARRAY_SIZE(double_registers_));
371 return double_registers_[n];
372 }
373
374 void SetRegister(unsigned n, intptr_t value) {
375 ASSERT(n < ARRAY_SIZE(registers_));
376 registers_[n] = value;
377 }
378
379 void SetDoubleRegister(unsigned n, double value) {
380 ASSERT(n < ARRAY_SIZE(double_registers_));
381 double_registers_[n] = value;
382 }
383
384 intptr_t GetTop() const { return top_; }
385 void SetTop(intptr_t top) { top_ = top; }
386
387 intptr_t GetPc() const { return pc_; }
388 void SetPc(intptr_t pc) { pc_ = pc; }
389
390 intptr_t GetFp() const { return fp_; }
391 void SetFp(intptr_t fp) { fp_ = fp; }
392
393 Smi* GetState() const { return state_; }
394 void SetState(Smi* state) { state_ = state; }
395
396 void SetContinuation(intptr_t pc) { continuation_ = pc; }
397
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000398#ifdef DEBUG
399 Code::Kind GetKind() const { return kind_; }
400 void SetKind(Code::Kind kind) { kind_ = kind; }
401#endif
402
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000403 // Get the incoming arguments count.
404 int ComputeParametersCount();
405
406 // Get a parameter value for an unoptimized frame.
407 Object* GetParameter(Deoptimizer* deoptimizer, int index);
408
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000409 // Get the expression stack height for a unoptimized frame.
410 unsigned GetExpressionCount(Deoptimizer* deoptimizer);
411
412 // Get the expression stack value for an unoptimized frame.
413 Object* GetExpression(Deoptimizer* deoptimizer, int index);
414
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000415 static int registers_offset() {
416 return OFFSET_OF(FrameDescription, registers_);
417 }
418
419 static int double_registers_offset() {
420 return OFFSET_OF(FrameDescription, double_registers_);
421 }
422
423 static int frame_size_offset() {
424 return OFFSET_OF(FrameDescription, frame_size_);
425 }
426
427 static int pc_offset() {
428 return OFFSET_OF(FrameDescription, pc_);
429 }
430
431 static int state_offset() {
432 return OFFSET_OF(FrameDescription, state_);
433 }
434
435 static int continuation_offset() {
436 return OFFSET_OF(FrameDescription, continuation_);
437 }
438
439 static int frame_content_offset() {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000440 return OFFSET_OF(FrameDescription, frame_content_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000441 }
442
443 private:
444 static const uint32_t kZapUint32 = 0xbeeddead;
445
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000446 // Frame_size_ must hold a uint32_t value. It is only a uintptr_t to
447 // keep the variable-size array frame_content_ of type intptr_t at
448 // the end of the structure aligned.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000449 uintptr_t frame_size_; // Number of bytes.
450 JSFunction* function_;
451 intptr_t registers_[Register::kNumRegisters];
452 double double_registers_[DoubleRegister::kNumAllocatableRegisters];
453 intptr_t top_;
454 intptr_t pc_;
455 intptr_t fp_;
456 Smi* state_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000457#ifdef DEBUG
458 Code::Kind kind_;
459#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000460
461 // Continuation is the PC where the execution continues after
462 // deoptimizing.
463 intptr_t continuation_;
464
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000465 // This must be at the end of the object as the object is allocated larger
466 // than it's definition indicate to extend this array.
467 intptr_t frame_content_[1];
468
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000469 intptr_t* GetFrameSlotPointer(unsigned offset) {
470 ASSERT(offset < frame_size_);
471 return reinterpret_cast<intptr_t*>(
472 reinterpret_cast<Address>(this) + frame_content_offset() + offset);
473 }
474};
475
476
477class TranslationBuffer BASE_EMBEDDED {
478 public:
479 TranslationBuffer() : contents_(256) { }
480
481 int CurrentIndex() const { return contents_.length(); }
482 void Add(int32_t value);
483
484 Handle<ByteArray> CreateByteArray();
485
486 private:
487 ZoneList<uint8_t> contents_;
488};
489
490
491class TranslationIterator BASE_EMBEDDED {
492 public:
493 TranslationIterator(ByteArray* buffer, int index)
494 : buffer_(buffer), index_(index) {
495 ASSERT(index >= 0 && index < buffer->length());
496 }
497
498 int32_t Next();
499
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000500 bool HasNext() const { return index_ < buffer_->length(); }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000501
502 void Skip(int n) {
503 for (int i = 0; i < n; i++) Next();
504 }
505
506 private:
507 ByteArray* buffer_;
508 int index_;
509};
510
511
512class Translation BASE_EMBEDDED {
513 public:
514 enum Opcode {
515 BEGIN,
516 FRAME,
517 REGISTER,
518 INT32_REGISTER,
519 DOUBLE_REGISTER,
520 STACK_SLOT,
521 INT32_STACK_SLOT,
522 DOUBLE_STACK_SLOT,
523 LITERAL,
524 ARGUMENTS_OBJECT,
525
526 // A prefix indicating that the next command is a duplicate of the one
527 // that follows it.
528 DUPLICATE
529 };
530
531 Translation(TranslationBuffer* buffer, int frame_count)
532 : buffer_(buffer),
533 index_(buffer->CurrentIndex()) {
534 buffer_->Add(BEGIN);
535 buffer_->Add(frame_count);
536 }
537
538 int index() const { return index_; }
539
540 // Commands.
541 void BeginFrame(int node_id, int literal_id, unsigned height);
542 void StoreRegister(Register reg);
543 void StoreInt32Register(Register reg);
544 void StoreDoubleRegister(DoubleRegister reg);
545 void StoreStackSlot(int index);
546 void StoreInt32StackSlot(int index);
547 void StoreDoubleStackSlot(int index);
548 void StoreLiteral(int literal_id);
549 void StoreArgumentsObject();
550 void MarkDuplicate();
551
552 static int NumberOfOperandsFor(Opcode opcode);
553
whesse@chromium.org7b260152011-06-20 15:33:18 +0000554#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000555 static const char* StringFor(Opcode opcode);
556#endif
557
558 private:
559 TranslationBuffer* buffer_;
560 int index_;
561};
562
563
564// Linked list holding deoptimizing code objects. The deoptimizing code objects
565// are kept as weak handles until they are no longer activated on the stack.
566class DeoptimizingCodeListNode : public Malloced {
567 public:
568 explicit DeoptimizingCodeListNode(Code* code);
569 ~DeoptimizingCodeListNode();
570
571 DeoptimizingCodeListNode* next() const { return next_; }
572 void set_next(DeoptimizingCodeListNode* next) { next_ = next; }
573 Handle<Code> code() const { return code_; }
574
575 private:
576 // Global (weak) handle to the deoptimizing code object.
577 Handle<Code> code_;
578
579 // Next pointer for linked list.
580 DeoptimizingCodeListNode* next_;
581};
582
583
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000584class SlotRef BASE_EMBEDDED {
585 public:
586 enum SlotRepresentation {
587 UNKNOWN,
588 TAGGED,
589 INT32,
590 DOUBLE,
591 LITERAL
592 };
593
594 SlotRef()
595 : addr_(NULL), representation_(UNKNOWN) { }
596
597 SlotRef(Address addr, SlotRepresentation representation)
598 : addr_(addr), representation_(representation) { }
599
600 explicit SlotRef(Object* literal)
601 : literal_(literal), representation_(LITERAL) { }
602
603 Handle<Object> GetValue() {
604 switch (representation_) {
605 case TAGGED:
606 return Handle<Object>(Memory::Object_at(addr_));
607
608 case INT32: {
609 int value = Memory::int32_at(addr_);
610 if (Smi::IsValid(value)) {
611 return Handle<Object>(Smi::FromInt(value));
612 } else {
613 return Isolate::Current()->factory()->NewNumberFromInt(value);
614 }
615 }
616
617 case DOUBLE: {
618 double value = Memory::double_at(addr_);
619 return Isolate::Current()->factory()->NewNumber(value);
620 }
621
622 case LITERAL:
623 return literal_;
624
625 default:
626 UNREACHABLE();
627 return Handle<Object>::null();
628 }
629 }
630
631 static void ComputeSlotMappingForArguments(JavaScriptFrame* frame,
632 int inlined_frame_index,
633 Vector<SlotRef>* args_slots);
634
635 private:
636 Address addr_;
637 Handle<Object> literal_;
638 SlotRepresentation representation_;
639
640 static Address SlotAddress(JavaScriptFrame* frame, int slot_index) {
641 if (slot_index >= 0) {
642 const int offset = JavaScriptFrameConstants::kLocal0Offset;
643 return frame->fp() + offset - (slot_index * kPointerSize);
644 } else {
645 const int offset = JavaScriptFrameConstants::kLastParameterOffset;
646 return frame->fp() + offset - ((slot_index + 1) * kPointerSize);
647 }
648 }
649
650 static SlotRef ComputeSlotForNextArgument(TranslationIterator* iterator,
651 DeoptimizationInputData* data,
652 JavaScriptFrame* frame);
653};
654
655
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000656#ifdef ENABLE_DEBUGGER_SUPPORT
657// Class used to represent an unoptimized frame when the debugger
658// needs to inspect a frame that is part of an optimized frame. The
659// internally used FrameDescription objects are not GC safe so for use
660// by the debugger frame information is copied to an object of this type.
661class DeoptimizedFrameInfo : public Malloced {
662 public:
663 DeoptimizedFrameInfo(Deoptimizer* deoptimizer, int frame_index);
664 virtual ~DeoptimizedFrameInfo();
665
666 // GC support.
667 void Iterate(ObjectVisitor* v);
668
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000669 // Return the number of incoming arguments.
670 int parameters_count() { return parameters_count_; }
671
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000672 // Return the height of the expression stack.
673 int expression_count() { return expression_count_; }
674
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000675 // Get the frame function.
676 JSFunction* GetFunction() {
677 return function_;
678 }
679
680 // Get an incoming argument.
681 Object* GetParameter(int index) {
682 ASSERT(0 <= index && index < parameters_count());
683 return parameters_[index];
684 }
685
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000686 // Get an expression from the expression stack.
687 Object* GetExpression(int index) {
688 ASSERT(0 <= index && index < expression_count());
689 return expression_stack_[index];
690 }
691
692 private:
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000693 // Set the frame function.
694 void SetFunction(JSFunction* function) {
695 function_ = function;
696 }
697
698 // Set an incoming argument.
699 void SetParameter(int index, Object* obj) {
700 ASSERT(0 <= index && index < parameters_count());
701 parameters_[index] = obj;
702 }
703
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000704 // Set an expression on the expression stack.
705 void SetExpression(int index, Object* obj) {
706 ASSERT(0 <= index && index < expression_count());
707 expression_stack_[index] = obj;
708 }
709
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000710 JSFunction* function_;
711 int parameters_count_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000712 int expression_count_;
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000713 Object** parameters_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000714 Object** expression_stack_;
715
716 friend class Deoptimizer;
717};
718#endif
719
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000720} } // namespace v8::internal
721
722#endif // V8_DEOPTIMIZER_H_