OLD | NEW |
---|---|
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 17 matching lines...) Expand all Loading... | |
28 #ifndef V8_FAST_CODEGEN_H_ | 28 #ifndef V8_FAST_CODEGEN_H_ |
29 #define V8_FAST_CODEGEN_H_ | 29 #define V8_FAST_CODEGEN_H_ |
30 | 30 |
31 #include "v8.h" | 31 #include "v8.h" |
32 | 32 |
33 #include "ast.h" | 33 #include "ast.h" |
34 | 34 |
35 namespace v8 { | 35 namespace v8 { |
36 namespace internal { | 36 namespace internal { |
37 | 37 |
38 // ----------------------------------------------------------------------------- | |
39 // Fast code generator. | |
38 | 40 |
39 class FastCodeGenerator: public AstVisitor { | 41 class FastCodeGenerator: public AstVisitor { |
40 public: | 42 public: |
41 FastCodeGenerator(MacroAssembler* masm, Handle<Script> script, bool is_eval) | 43 FastCodeGenerator(MacroAssembler* masm, Handle<Script> script, bool is_eval) |
42 : masm_(masm), | 44 : masm_(masm), |
43 function_(NULL), | 45 function_(NULL), |
44 script_(script), | 46 script_(script), |
45 is_eval_(is_eval), | 47 is_eval_(is_eval), |
48 nesting_stack_(NULL), | |
46 loop_depth_(0), | 49 loop_depth_(0), |
47 true_label_(NULL), | 50 true_label_(NULL), |
48 false_label_(NULL) { | 51 false_label_(NULL) { |
49 } | 52 } |
50 | 53 |
51 static Handle<Code> MakeCode(FunctionLiteral* fun, | 54 static Handle<Code> MakeCode(FunctionLiteral* fun, |
52 Handle<Script> script, | 55 Handle<Script> script, |
53 bool is_eval); | 56 bool is_eval); |
54 | 57 |
55 void Generate(FunctionLiteral* fun); | 58 void Generate(FunctionLiteral* fun); |
56 | 59 |
57 private: | 60 private: |
61 class Breakable; | |
62 class Iteration; | |
63 class TryCatch; | |
64 class TryFinally; | |
65 class Finally; | |
66 class ForIn; | |
67 | |
68 class NestedStatement BASE_EMBEDDED { | |
69 public: | |
70 explicit NestedStatement(FastCodeGenerator* codegen) : codegen_(codegen) { | |
71 // Link into codegen's nesting stack. | |
72 previous_ = codegen->nesting_stack_; | |
73 codegen->nesting_stack_ = this; | |
74 } | |
75 virtual ~NestedStatement() { | |
76 // Unlink from codegen's nesting stack. | |
77 ASSERT_EQ(this, codegen_->nesting_stack_); | |
78 codegen_->nesting_stack_ = previous_; | |
79 } | |
80 | |
81 virtual Breakable* AsBreakable() { return NULL; } | |
82 virtual Iteration* AsIteration() { return NULL; } | |
83 virtual TryCatch* AsTryCatch() { return NULL; } | |
84 virtual TryFinally* AsTryFinally() { return NULL; } | |
85 virtual Finally* AsFinally() { return NULL; } | |
86 virtual ForIn* AsForIn() { return NULL; } | |
87 | |
88 virtual bool IsContinueTarget(Statement* target) { return false; } | |
89 virtual bool IsBreakTarget(Statement* target) { return false; } | |
90 | |
91 // Generate code to leave the nested statement. This includes | |
Kevin Millikin (Chromium)
2009/12/10 13:09:26
We should probably document (in several places) th
Lasse Reichstein
2009/12/10 13:55:32
It's documented here now, and I'll put it on the f
| |
92 // cleaning up any stack elements in use and restoring the | |
93 // stack to the expectations of the surrounding statements. | |
94 // Takes a number of stack elements currently on top of the | |
95 // nested statement's stack, and returns a number of stack | |
96 // elements left on top of the surrounding statement's stack. | |
97 virtual int Exit(int stack_depth) { | |
98 // Default implementation for the case where there is | |
99 // nothing to clean up. | |
100 return stack_depth; | |
101 } | |
102 NestedStatement* outer() { return previous_; } | |
103 protected: | |
104 MacroAssembler* masm() { return codegen_->masm(); } | |
105 private: | |
106 FastCodeGenerator *codegen_; | |
Kevin Millikin (Chromium)
2009/12/10 13:09:26
Space after star.
Lasse Reichstein
2009/12/10 13:55:32
Fixed
| |
107 NestedStatement *previous_; | |
Kevin Millikin (Chromium)
2009/12/10 13:09:26
Same.
Lasse Reichstein
2009/12/10 13:55:32
Fixed
| |
108 DISALLOW_COPY_AND_ASSIGN(NestedStatement); | |
109 }; | |
110 | |
111 class Breakable : public NestedStatement { | |
112 public: | |
113 Breakable(FastCodeGenerator* codegen, | |
114 BreakableStatement* break_target, | |
115 Label* target_label) | |
116 : NestedStatement(codegen), | |
117 target_(break_target), | |
118 break_target_label_(target_label) {} | |
119 virtual ~Breakable() {} | |
120 virtual Breakable* AsBreakable() { return this; } | |
121 virtual bool IsBreakTarget(Statement* statement) { | |
122 return target_ == statement; | |
123 } | |
124 Statement* statement() { return target_; } | |
Kevin Millikin (Chromium)
2009/12/10 13:09:26
Might make return type BreakableStatement* in case
Lasse Reichstein
2009/12/10 13:55:32
Fixed.
| |
125 Label* break_target() { return break_target_label_; } | |
126 private: | |
127 BreakableStatement* target_; | |
128 Label* break_target_label_; | |
Kevin Millikin (Chromium)
2009/12/10 13:09:26
Instead of passing a label pointer to the construc
| |
129 DISALLOW_COPY_AND_ASSIGN(Breakable); | |
130 }; | |
131 | |
132 class Iteration : public Breakable { | |
133 public: | |
134 Iteration(FastCodeGenerator* codegen, | |
135 IterationStatement* iteration_statement, | |
136 Label* break_target_label, | |
137 Label* continue_target_label) | |
138 : Breakable(codegen, iteration_statement, break_target_label), | |
139 continue_target_label_(continue_target_label) {} | |
140 virtual ~Iteration() {} | |
141 virtual Iteration* AsIteration() { return this; } | |
142 virtual bool IsContinueTarget(Statement* statement) { | |
143 return this->statement() == statement; | |
144 } | |
145 Label* continue_target() { return continue_target_label_; } | |
146 private: | |
147 Label* continue_target_label_; | |
148 DISALLOW_COPY_AND_ASSIGN(Iteration); | |
149 }; | |
150 | |
151 // The environment inside the try block of a try/catch statement. | |
152 class TryCatch : public NestedStatement { | |
153 public: | |
154 explicit TryCatch(FastCodeGenerator* codegen, Label* catch_entry) | |
155 : NestedStatement(codegen), catch_entry_(catch_entry) { } | |
156 virtual ~TryCatch() {} | |
157 virtual TryCatch* AsTryCatch() { return this; } | |
158 Label* catch_entry() { return catch_entry_; } | |
159 virtual int Exit(int stack_depth); | |
160 private: | |
161 Label* catch_entry_; | |
162 DISALLOW_COPY_AND_ASSIGN(TryCatch); | |
163 }; | |
164 | |
165 // The environment inside the try block of a try/finally statement. | |
166 class TryFinally : public NestedStatement { | |
167 public: | |
168 explicit TryFinally(FastCodeGenerator* codegen, Label* finally_entry) | |
169 : NestedStatement(codegen), finally_entry_(finally_entry) { } | |
170 virtual ~TryFinally() {} | |
171 virtual TryFinally* AsTryFinally() { return this; } | |
172 Label* finally_entry() { return finally_entry_; } | |
173 virtual int Exit(int stack_depth); | |
174 private: | |
175 Label* finally_entry_; | |
176 DISALLOW_COPY_AND_ASSIGN(TryFinally); | |
177 }; | |
178 | |
179 // A FinallyEnvironment represents being inside a finally block. | |
180 // Abnormal termination of the finally block needs to clean up | |
181 // the block's parameters from the stack. | |
182 class Finally : public NestedStatement { | |
183 public: | |
184 explicit Finally(FastCodeGenerator* codegen) : NestedStatement(codegen) { } | |
185 virtual ~Finally() {} | |
186 virtual Finally* AsFinally() { return this; } | |
187 virtual int Exit(int stack_depth) { | |
188 return stack_depth + kFinallyStackElementCount; | |
189 } | |
190 private: | |
191 // Number of extra stack slots occupied during a finally block. | |
192 static const int kFinallyStackElementCount = 2; | |
193 DISALLOW_COPY_AND_ASSIGN(Finally); | |
194 }; | |
195 | |
196 // A ForInEnvironment represents being inside a for-in loop. | |
197 // Abnormal termination of the for-in block needs to clean up | |
198 // the block's temporary storage from the stack. | |
199 class ForIn : public Iteration { | |
200 public: | |
201 ForIn(FastCodeGenerator* codegen, | |
202 ForInStatement* statement, | |
203 Label* break_target, | |
204 Label* continue_target) | |
205 : Iteration(codegen, statement, break_target, continue_target) { } | |
206 virtual ~ForIn() {} | |
207 virtual ForIn* AsForIn() { return this; } | |
208 virtual int Exit(int stack_depth) { | |
209 return stack_depth + kForInStackElementCount; | |
210 } | |
211 private: | |
212 // TODO(lrn): Check that this value is correct when implementing | |
213 // for-in. | |
214 static const int kForInStackElementCount = 5; | |
215 DISALLOW_COPY_AND_ASSIGN(ForIn); | |
216 }; | |
217 | |
218 | |
58 int SlotOffset(Slot* slot); | 219 int SlotOffset(Slot* slot); |
59 void Move(Expression::Context destination, Register source); | 220 void Move(Expression::Context destination, Register source); |
60 void Move(Expression::Context destination, Slot* source, Register scratch); | 221 void Move(Expression::Context destination, Slot* source, Register scratch); |
61 void Move(Expression::Context destination, Literal* source); | 222 void Move(Expression::Context destination, Literal* source); |
62 void Move(Slot* dst, Register source, Register scratch1, Register scratch2); | 223 void Move(Slot* dst, Register source, Register scratch1, Register scratch2); |
63 void Move(Register dst, Slot* source); | 224 void Move(Register dst, Slot* source); |
64 | 225 |
65 // Templated to allow for Operand on intel and MemOperand on ARM. | 226 // Templated to allow for Operand on intel and MemOperand on ARM. |
66 template <typename MemoryLocation> | 227 template <typename MemoryLocation> |
67 MemoryLocation CreateSlotOperand(Slot* slot, Register scratch); | 228 MemoryLocation CreateSlotOperand(Slot* slot, Register scratch); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 void SetStatementPosition(Statement* stmt); | 266 void SetStatementPosition(Statement* stmt); |
106 void SetSourcePosition(int pos); | 267 void SetSourcePosition(int pos); |
107 | 268 |
108 int loop_depth() { return loop_depth_; } | 269 int loop_depth() { return loop_depth_; } |
109 void increment_loop_depth() { loop_depth_++; } | 270 void increment_loop_depth() { loop_depth_++; } |
110 void decrement_loop_depth() { | 271 void decrement_loop_depth() { |
111 ASSERT(loop_depth_ > 0); | 272 ASSERT(loop_depth_ > 0); |
112 loop_depth_--; | 273 loop_depth_--; |
113 } | 274 } |
114 | 275 |
276 MacroAssembler* masm() { return masm_; } | |
277 static Register result_register(); | |
278 | |
115 // AST node visit functions. | 279 // AST node visit functions. |
116 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); | 280 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
117 AST_NODE_LIST(DECLARE_VISIT) | 281 AST_NODE_LIST(DECLARE_VISIT) |
118 #undef DECLARE_VISIT | 282 #undef DECLARE_VISIT |
119 | |
120 // Handles the shortcutted logical binary operations in VisitBinaryOperation. | 283 // Handles the shortcutted logical binary operations in VisitBinaryOperation. |
121 void EmitLogicalOperation(BinaryOperation* expr); | 284 void EmitLogicalOperation(BinaryOperation* expr); |
122 | 285 |
123 MacroAssembler* masm_; | 286 MacroAssembler* masm_; |
124 FunctionLiteral* function_; | 287 FunctionLiteral* function_; |
125 Handle<Script> script_; | 288 Handle<Script> script_; |
126 bool is_eval_; | 289 bool is_eval_; |
127 Label return_label_; | 290 Label return_label_; |
291 NestedStatement* nesting_stack_; | |
128 int loop_depth_; | 292 int loop_depth_; |
129 | 293 |
130 Label* true_label_; | 294 Label* true_label_; |
131 Label* false_label_; | 295 Label* false_label_; |
132 | 296 |
297 friend class NestedStatement; | |
298 | |
133 DISALLOW_COPY_AND_ASSIGN(FastCodeGenerator); | 299 DISALLOW_COPY_AND_ASSIGN(FastCodeGenerator); |
134 }; | 300 }; |
135 | 301 |
136 | 302 |
137 } } // namespace v8::internal | 303 } } // namespace v8::internal |
138 | 304 |
139 #endif // V8_FAST_CODEGEN_H_ | 305 #endif // V8_FAST_CODEGEN_H_ |
OLD | NEW |