blob: 35ed25f13bb001634470bc4b5397422d3b610b35 [file] [log] [blame]
Steve Block3ce2e202009-11-05 08:53:23 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_FAST_CODEGEN_H_
29#define V8_FAST_CODEGEN_H_
30
31#include "v8.h"
32
33#include "ast.h"
34
35namespace v8 {
36namespace internal {
37
Leon Clarke888f6722010-01-27 15:57:47 +000038class FullCodeGenSyntaxChecker: public AstVisitor {
Steve Block3ce2e202009-11-05 08:53:23 +000039 public:
Leon Clarke888f6722010-01-27 15:57:47 +000040 FullCodeGenSyntaxChecker() : has_supported_syntax_(true) {}
41
42 void Check(FunctionLiteral* fun);
43
44 bool has_supported_syntax() { return has_supported_syntax_; }
45
46 private:
47 void VisitDeclarations(ZoneList<Declaration*>* decls);
48 void VisitStatements(ZoneList<Statement*>* stmts);
49
50 // AST node visit functions.
51#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
52 AST_NODE_LIST(DECLARE_VISIT)
53#undef DECLARE_VISIT
54
55 bool has_supported_syntax_;
56
57 DISALLOW_COPY_AND_ASSIGN(FullCodeGenSyntaxChecker);
58};
59
60
61// -----------------------------------------------------------------------------
62// Full code generator.
63
64class FullCodeGenerator: public AstVisitor {
65 public:
66 FullCodeGenerator(MacroAssembler* masm, Handle<Script> script, bool is_eval)
Steve Blockd0582a62009-12-15 09:54:21 +000067 : masm_(masm),
68 function_(NULL),
69 script_(script),
70 is_eval_(is_eval),
Leon Clarkee46be812010-01-19 14:06:41 +000071 nesting_stack_(NULL),
Steve Blockd0582a62009-12-15 09:54:21 +000072 loop_depth_(0),
Leon Clarkee46be812010-01-19 14:06:41 +000073 location_(kStack),
Steve Blockd0582a62009-12-15 09:54:21 +000074 true_label_(NULL),
75 false_label_(NULL) {
Steve Block3ce2e202009-11-05 08:53:23 +000076 }
77
78 static Handle<Code> MakeCode(FunctionLiteral* fun,
79 Handle<Script> script,
80 bool is_eval);
81
82 void Generate(FunctionLiteral* fun);
83
84 private:
Leon Clarkee46be812010-01-19 14:06:41 +000085 class Breakable;
86 class Iteration;
87 class TryCatch;
88 class TryFinally;
89 class Finally;
90 class ForIn;
91
92 class NestedStatement BASE_EMBEDDED {
93 public:
Leon Clarke888f6722010-01-27 15:57:47 +000094 explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
Leon Clarkee46be812010-01-19 14:06:41 +000095 // Link into codegen's nesting stack.
96 previous_ = codegen->nesting_stack_;
97 codegen->nesting_stack_ = this;
98 }
99 virtual ~NestedStatement() {
100 // Unlink from codegen's nesting stack.
101 ASSERT_EQ(this, codegen_->nesting_stack_);
102 codegen_->nesting_stack_ = previous_;
103 }
104
105 virtual Breakable* AsBreakable() { return NULL; }
106 virtual Iteration* AsIteration() { return NULL; }
107 virtual TryCatch* AsTryCatch() { return NULL; }
108 virtual TryFinally* AsTryFinally() { return NULL; }
109 virtual Finally* AsFinally() { return NULL; }
110 virtual ForIn* AsForIn() { return NULL; }
111
112 virtual bool IsContinueTarget(Statement* target) { return false; }
113 virtual bool IsBreakTarget(Statement* target) { return false; }
114
115 // Generate code to leave the nested statement. This includes
116 // cleaning up any stack elements in use and restoring the
117 // stack to the expectations of the surrounding statements.
118 // Takes a number of stack elements currently on top of the
119 // nested statement's stack, and returns a number of stack
120 // elements left on top of the surrounding statement's stack.
121 // The generated code must preserve the result register (which
122 // contains the value in case of a return).
123 virtual int Exit(int stack_depth) {
124 // Default implementation for the case where there is
125 // nothing to clean up.
126 return stack_depth;
127 }
128 NestedStatement* outer() { return previous_; }
129 protected:
130 MacroAssembler* masm() { return codegen_->masm(); }
131 private:
Leon Clarke888f6722010-01-27 15:57:47 +0000132 FullCodeGenerator* codegen_;
Leon Clarkee46be812010-01-19 14:06:41 +0000133 NestedStatement* previous_;
134 DISALLOW_COPY_AND_ASSIGN(NestedStatement);
135 };
136
137 class Breakable : public NestedStatement {
138 public:
Leon Clarke888f6722010-01-27 15:57:47 +0000139 Breakable(FullCodeGenerator* codegen,
Leon Clarkee46be812010-01-19 14:06:41 +0000140 BreakableStatement* break_target)
141 : NestedStatement(codegen),
142 target_(break_target) {}
143 virtual ~Breakable() {}
144 virtual Breakable* AsBreakable() { return this; }
145 virtual bool IsBreakTarget(Statement* statement) {
146 return target_ == statement;
147 }
148 BreakableStatement* statement() { return target_; }
149 Label* break_target() { return &break_target_label_; }
150 private:
151 BreakableStatement* target_;
152 Label break_target_label_;
153 DISALLOW_COPY_AND_ASSIGN(Breakable);
154 };
155
156 class Iteration : public Breakable {
157 public:
Leon Clarke888f6722010-01-27 15:57:47 +0000158 Iteration(FullCodeGenerator* codegen,
Leon Clarkee46be812010-01-19 14:06:41 +0000159 IterationStatement* iteration_statement)
160 : Breakable(codegen, iteration_statement) {}
161 virtual ~Iteration() {}
162 virtual Iteration* AsIteration() { return this; }
163 virtual bool IsContinueTarget(Statement* statement) {
164 return this->statement() == statement;
165 }
166 Label* continue_target() { return &continue_target_label_; }
167 private:
168 Label continue_target_label_;
169 DISALLOW_COPY_AND_ASSIGN(Iteration);
170 };
171
172 // The environment inside the try block of a try/catch statement.
173 class TryCatch : public NestedStatement {
174 public:
Leon Clarke888f6722010-01-27 15:57:47 +0000175 explicit TryCatch(FullCodeGenerator* codegen, Label* catch_entry)
Leon Clarkee46be812010-01-19 14:06:41 +0000176 : NestedStatement(codegen), catch_entry_(catch_entry) { }
177 virtual ~TryCatch() {}
178 virtual TryCatch* AsTryCatch() { return this; }
179 Label* catch_entry() { return catch_entry_; }
180 virtual int Exit(int stack_depth);
181 private:
182 Label* catch_entry_;
183 DISALLOW_COPY_AND_ASSIGN(TryCatch);
184 };
185
186 // The environment inside the try block of a try/finally statement.
187 class TryFinally : public NestedStatement {
188 public:
Leon Clarke888f6722010-01-27 15:57:47 +0000189 explicit TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
Leon Clarkee46be812010-01-19 14:06:41 +0000190 : NestedStatement(codegen), finally_entry_(finally_entry) { }
191 virtual ~TryFinally() {}
192 virtual TryFinally* AsTryFinally() { return this; }
193 Label* finally_entry() { return finally_entry_; }
194 virtual int Exit(int stack_depth);
195 private:
196 Label* finally_entry_;
197 DISALLOW_COPY_AND_ASSIGN(TryFinally);
198 };
199
200 // A FinallyEnvironment represents being inside a finally block.
201 // Abnormal termination of the finally block needs to clean up
202 // the block's parameters from the stack.
203 class Finally : public NestedStatement {
204 public:
Leon Clarke888f6722010-01-27 15:57:47 +0000205 explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { }
Leon Clarkee46be812010-01-19 14:06:41 +0000206 virtual ~Finally() {}
207 virtual Finally* AsFinally() { return this; }
208 virtual int Exit(int stack_depth) {
209 return stack_depth + kFinallyStackElementCount;
210 }
211 private:
212 // Number of extra stack slots occupied during a finally block.
213 static const int kFinallyStackElementCount = 2;
214 DISALLOW_COPY_AND_ASSIGN(Finally);
215 };
216
217 // A ForInEnvironment represents being inside a for-in loop.
218 // Abnormal termination of the for-in block needs to clean up
219 // the block's temporary storage from the stack.
220 class ForIn : public Iteration {
221 public:
Leon Clarke888f6722010-01-27 15:57:47 +0000222 ForIn(FullCodeGenerator* codegen,
Leon Clarkee46be812010-01-19 14:06:41 +0000223 ForInStatement* statement)
224 : Iteration(codegen, statement) { }
225 virtual ~ForIn() {}
226 virtual ForIn* AsForIn() { return this; }
227 virtual int Exit(int stack_depth) {
228 return stack_depth + kForInStackElementCount;
229 }
230 private:
231 // TODO(lrn): Check that this value is correct when implementing
232 // for-in.
233 static const int kForInStackElementCount = 5;
234 DISALLOW_COPY_AND_ASSIGN(ForIn);
235 };
236
237 enum Location {
238 kAccumulator,
239 kStack
240 };
241
Steve Block3ce2e202009-11-05 08:53:23 +0000242 int SlotOffset(Slot* slot);
Leon Clarkee46be812010-01-19 14:06:41 +0000243
244 // Emit code to convert a pure value (in a register, slot, as a literal,
245 // or on top of the stack) into the result expected according to an
246 // expression context.
247 void Apply(Expression::Context context, Register reg);
Leon Clarke888f6722010-01-27 15:57:47 +0000248
249 // Slot cannot have type Slot::LOOKUP.
Leon Clarkee46be812010-01-19 14:06:41 +0000250 void Apply(Expression::Context context, Slot* slot);
Leon Clarke888f6722010-01-27 15:57:47 +0000251
Leon Clarkee46be812010-01-19 14:06:41 +0000252 void Apply(Expression::Context context, Literal* lit);
253 void ApplyTOS(Expression::Context context);
254
255 // Emit code to discard count elements from the top of stack, then convert
256 // a pure value into the result expected according to an expression
257 // context.
258 void DropAndApply(int count, Expression::Context context, Register reg);
259
260 // Emit code to convert pure control flow to a pair of labels into the
261 // result expected according to an expression context.
262 void Apply(Expression::Context context,
263 Label* materialize_true,
264 Label* materialize_false);
265
266 // Helper function to convert a pure value into a test context. The value
267 // is expected on the stack or the accumulator, depending on the platform.
268 // See the platform-specific implementation for details.
269 void DoTest(Expression::Context context);
270
Steve Blockd0582a62009-12-15 09:54:21 +0000271 void Move(Slot* dst, Register source, Register scratch1, Register scratch2);
272 void Move(Register dst, Slot* source);
273
Leon Clarkee46be812010-01-19 14:06:41 +0000274 // Return an operand used to read/write to a known (ie, non-LOOKUP) slot.
275 // May emit code to traverse the context chain, destroying the scratch
276 // register.
277 MemOperand EmitSlotSearch(Slot* slot, Register scratch);
Steve Blockd0582a62009-12-15 09:54:21 +0000278
Leon Clarkee46be812010-01-19 14:06:41 +0000279 void VisitForEffect(Expression* expr) {
280 Expression::Context saved_context = context_;
281 context_ = Expression::kEffect;
282 Visit(expr);
283 context_ = saved_context;
284 }
Steve Blockd0582a62009-12-15 09:54:21 +0000285
Leon Clarkee46be812010-01-19 14:06:41 +0000286 void VisitForValue(Expression* expr, Location where) {
287 Expression::Context saved_context = context_;
288 Location saved_location = location_;
289 context_ = Expression::kValue;
290 location_ = where;
291 Visit(expr);
292 context_ = saved_context;
293 location_ = saved_location;
294 }
295
296 void VisitForControl(Expression* expr, Label* if_true, Label* if_false) {
297 Expression::Context saved_context = context_;
298 Label* saved_true = true_label_;
299 Label* saved_false = false_label_;
300 context_ = Expression::kTest;
301 true_label_ = if_true;
302 false_label_ = if_false;
303 Visit(expr);
304 context_ = saved_context;
305 true_label_ = saved_true;
306 false_label_ = saved_false;
307 }
308
309 void VisitForValueControl(Expression* expr,
310 Location where,
311 Label* if_true,
312 Label* if_false) {
313 Expression::Context saved_context = context_;
314 Location saved_location = location_;
315 Label* saved_true = true_label_;
316 Label* saved_false = false_label_;
317 context_ = Expression::kValueTest;
318 location_ = where;
319 true_label_ = if_true;
320 false_label_ = if_false;
321 Visit(expr);
322 context_ = saved_context;
323 location_ = saved_location;
324 true_label_ = saved_true;
325 false_label_ = saved_false;
326 }
327
328 void VisitForControlValue(Expression* expr,
329 Location where,
330 Label* if_true,
331 Label* if_false) {
332 Expression::Context saved_context = context_;
333 Location saved_location = location_;
334 Label* saved_true = true_label_;
335 Label* saved_false = false_label_;
336 context_ = Expression::kTestValue;
337 location_ = where;
338 true_label_ = if_true;
339 false_label_ = if_false;
340 Visit(expr);
341 context_ = saved_context;
342 location_ = saved_location;
343 true_label_ = saved_true;
344 false_label_ = saved_false;
345 }
Steve Block3ce2e202009-11-05 08:53:23 +0000346
347 void VisitDeclarations(ZoneList<Declaration*>* declarations);
Steve Block3ce2e202009-11-05 08:53:23 +0000348 void DeclareGlobals(Handle<FixedArray> pairs);
349
Steve Blockd0582a62009-12-15 09:54:21 +0000350 // Platform-specific return sequence
351 void EmitReturnSequence(int position);
352
353 // Platform-specific code sequences for calls
354 void EmitCallWithStub(Call* expr);
Leon Clarkee46be812010-01-19 14:06:41 +0000355 void EmitCallWithIC(Call* expr, Handle<Object> name, RelocInfo::Mode mode);
356
357 // Platform-specific code for loading variables.
358 void EmitVariableLoad(Variable* expr, Expression::Context context);
Steve Blockd0582a62009-12-15 09:54:21 +0000359
360 // Platform-specific support for compiling assignments.
361
Leon Clarkee46be812010-01-19 14:06:41 +0000362 // Load a value from a named property.
363 // The receiver is left on the stack by the IC.
364 void EmitNamedPropertyLoad(Property* expr);
Steve Blockd0582a62009-12-15 09:54:21 +0000365
Leon Clarkee46be812010-01-19 14:06:41 +0000366 // Load a value from a keyed property.
367 // The receiver and the key is left on the stack by the IC.
368 void EmitKeyedPropertyLoad(Property* expr);
369
370 // Apply the compound assignment operator. Expects the left operand on top
371 // of the stack and the right one in the accumulator.
372 void EmitBinaryOp(Token::Value op, Expression::Context context);
373
374 // Complete a variable assignment. The right-hand-side value is expected
375 // in the accumulator.
376 void EmitVariableAssignment(Variable* var, Expression::Context context);
377
378 // Complete a named property assignment. The receiver is expected on top
379 // of the stack and the right-hand-side value in the accumulator.
Steve Blockd0582a62009-12-15 09:54:21 +0000380 void EmitNamedPropertyAssignment(Assignment* expr);
381
Leon Clarkee46be812010-01-19 14:06:41 +0000382 // Complete a keyed property assignment. The receiver and key are
383 // expected on top of the stack and the right-hand-side value in the
384 // accumulator.
Steve Blockd0582a62009-12-15 09:54:21 +0000385 void EmitKeyedPropertyAssignment(Assignment* expr);
386
Steve Block3ce2e202009-11-05 08:53:23 +0000387 void SetFunctionPosition(FunctionLiteral* fun);
388 void SetReturnPosition(FunctionLiteral* fun);
389 void SetStatementPosition(Statement* stmt);
Leon Clarkee46be812010-01-19 14:06:41 +0000390 void SetStatementPosition(int pos);
Steve Block3ce2e202009-11-05 08:53:23 +0000391 void SetSourcePosition(int pos);
392
Leon Clarkee46be812010-01-19 14:06:41 +0000393 // Non-local control flow support.
394 void EnterFinallyBlock();
395 void ExitFinallyBlock();
396
397 // Loop nesting counter.
Steve Blockd0582a62009-12-15 09:54:21 +0000398 int loop_depth() { return loop_depth_; }
399 void increment_loop_depth() { loop_depth_++; }
400 void decrement_loop_depth() {
401 ASSERT(loop_depth_ > 0);
402 loop_depth_--;
403 }
404
Leon Clarkee46be812010-01-19 14:06:41 +0000405 MacroAssembler* masm() { return masm_; }
406 static Register result_register();
407 static Register context_register();
408
409 // Set fields in the stack frame. Offsets are the frame pointer relative
410 // offsets defined in, e.g., StandardFrameConstants.
411 void StoreToFrameField(int frame_offset, Register value);
412
413 // Load a value from the current context. Indices are defined as an enum
414 // in v8::internal::Context.
415 void LoadContextField(Register dst, int context_index);
416
Steve Block3ce2e202009-11-05 08:53:23 +0000417 // AST node visit functions.
418#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
419 AST_NODE_LIST(DECLARE_VISIT)
420#undef DECLARE_VISIT
Steve Blockd0582a62009-12-15 09:54:21 +0000421 // Handles the shortcutted logical binary operations in VisitBinaryOperation.
422 void EmitLogicalOperation(BinaryOperation* expr);
423
Steve Block3ce2e202009-11-05 08:53:23 +0000424 MacroAssembler* masm_;
425 FunctionLiteral* function_;
426 Handle<Script> script_;
427 bool is_eval_;
Steve Blockd0582a62009-12-15 09:54:21 +0000428 Label return_label_;
Leon Clarkee46be812010-01-19 14:06:41 +0000429 NestedStatement* nesting_stack_;
Steve Blockd0582a62009-12-15 09:54:21 +0000430 int loop_depth_;
431
Leon Clarkee46be812010-01-19 14:06:41 +0000432 Expression::Context context_;
433 Location location_;
Steve Blockd0582a62009-12-15 09:54:21 +0000434 Label* true_label_;
435 Label* false_label_;
Steve Block3ce2e202009-11-05 08:53:23 +0000436
Leon Clarkee46be812010-01-19 14:06:41 +0000437 friend class NestedStatement;
438
Leon Clarke888f6722010-01-27 15:57:47 +0000439 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
Steve Block3ce2e202009-11-05 08:53:23 +0000440};
441
442
443} } // namespace v8::internal
444
445#endif // V8_FAST_CODEGEN_H_