blob: 840c82501453f6d1ced851bbc8164f1a5f173f5d [file] [log] [blame]
Leon Clarkef7060e22010-06-03 12:02:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Leon Clarked91b9f72010-01-27 17:25:45 +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_FULL_CODEGEN_H_
29#define V8_FULL_CODEGEN_H_
30
31#include "v8.h"
32
33#include "ast.h"
Leon Clarkef7060e22010-06-03 12:02:55 +010034#include "compiler.h"
Leon Clarked91b9f72010-01-27 17:25:45 +000035
36namespace v8 {
37namespace internal {
38
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010039// AST node visitor which can tell whether a given statement will be breakable
40// when the code is compiled by the full compiler in the debugger. This means
41// that there will be an IC (load/store/call) in the code generated for the
42// debugger to piggybag on.
43class BreakableStatementChecker: public AstVisitor {
44 public:
45 BreakableStatementChecker() : is_breakable_(false) {}
46
47 void Check(Statement* stmt);
48 void Check(Expression* stmt);
49
50 bool is_breakable() { return is_breakable_; }
51
52 private:
53 // AST node visit functions.
54#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
55 AST_NODE_LIST(DECLARE_VISIT)
56#undef DECLARE_VISIT
57
58 bool is_breakable_;
59
60 DISALLOW_COPY_AND_ASSIGN(BreakableStatementChecker);
61};
62
63
Leon Clarked91b9f72010-01-27 17:25:45 +000064// -----------------------------------------------------------------------------
65// Full code generator.
66
67class FullCodeGenerator: public AstVisitor {
68 public:
Andrei Popescu31002712010-02-23 13:46:05 +000069 explicit FullCodeGenerator(MacroAssembler* masm)
Leon Clarked91b9f72010-01-27 17:25:45 +000070 : masm_(masm),
Andrei Popescu31002712010-02-23 13:46:05 +000071 info_(NULL),
Leon Clarked91b9f72010-01-27 17:25:45 +000072 nesting_stack_(NULL),
73 loop_depth_(0),
74 location_(kStack),
75 true_label_(NULL),
Kristian Monsen80d68ea2010-09-08 11:05:35 +010076 false_label_(NULL),
77 fall_through_(NULL) {
Leon Clarked91b9f72010-01-27 17:25:45 +000078 }
79
Andrei Popescu31002712010-02-23 13:46:05 +000080 static Handle<Code> MakeCode(CompilationInfo* info);
Leon Clarked91b9f72010-01-27 17:25:45 +000081
Iain Merrick75681382010-08-19 15:07:18 +010082 void Generate(CompilationInfo* info);
Leon Clarked91b9f72010-01-27 17:25:45 +000083
84 private:
85 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:
94 explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
95 // 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:
132 FullCodeGenerator* codegen_;
133 NestedStatement* previous_;
134 DISALLOW_COPY_AND_ASSIGN(NestedStatement);
135 };
136
137 class Breakable : public NestedStatement {
138 public:
139 Breakable(FullCodeGenerator* codegen,
140 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:
158 Iteration(FullCodeGenerator* codegen,
159 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:
175 explicit TryCatch(FullCodeGenerator* codegen, Label* catch_entry)
176 : 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:
189 explicit TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
190 : 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:
205 explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { }
206 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:
222 ForIn(FullCodeGenerator* codegen,
223 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:
Leon Clarked91b9f72010-01-27 17:25:45 +0000231 static const int kForInStackElementCount = 5;
232 DISALLOW_COPY_AND_ASSIGN(ForIn);
233 };
234
235 enum Location {
236 kAccumulator,
237 kStack
238 };
239
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100240 enum ConstantOperand {
241 kNoConstants,
242 kLeftConstant,
243 kRightConstant
244 };
245
246 // Compute the frame pointer relative offset for a given local or
247 // parameter slot.
Leon Clarked91b9f72010-01-27 17:25:45 +0000248 int SlotOffset(Slot* slot);
249
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100250 // Determine whether or not to inline the smi case for the given
251 // operation.
252 bool ShouldInlineSmiCase(Token::Value op);
253
254 // Compute which (if any) of the operands is a compile-time constant.
255 ConstantOperand GetConstantOperand(Token::Value op,
256 Expression* left,
257 Expression* right);
258
Leon Clarked91b9f72010-01-27 17:25:45 +0000259 // Emit code to convert a pure value (in a register, slot, as a literal,
260 // or on top of the stack) into the result expected according to an
261 // expression context.
262 void Apply(Expression::Context context, Register reg);
263
264 // Slot cannot have type Slot::LOOKUP.
265 void Apply(Expression::Context context, Slot* slot);
266
267 void Apply(Expression::Context context, Literal* lit);
268 void ApplyTOS(Expression::Context context);
269
270 // Emit code to discard count elements from the top of stack, then convert
271 // a pure value into the result expected according to an expression
272 // context.
273 void DropAndApply(int count, Expression::Context context, Register reg);
274
Leon Clarkef7060e22010-06-03 12:02:55 +0100275 // Set up branch labels for a test expression.
276 void PrepareTest(Label* materialize_true,
277 Label* materialize_false,
278 Label** if_true,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100279 Label** if_false,
280 Label** fall_through);
Leon Clarkef7060e22010-06-03 12:02:55 +0100281
Leon Clarked91b9f72010-01-27 17:25:45 +0000282 // Emit code to convert pure control flow to a pair of labels into the
283 // result expected according to an expression context.
284 void Apply(Expression::Context context,
285 Label* materialize_true,
286 Label* materialize_false);
287
Leon Clarkef7060e22010-06-03 12:02:55 +0100288 // Emit code to convert constant control flow (true or false) into
289 // the result expected according to an expression context.
290 void Apply(Expression::Context context, bool flag);
291
Leon Clarked91b9f72010-01-27 17:25:45 +0000292 // Helper function to convert a pure value into a test context. The value
293 // is expected on the stack or the accumulator, depending on the platform.
294 // See the platform-specific implementation for details.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100295 void DoTest(Label* if_true, Label* if_false, Label* fall_through);
296
297 // Helper function to split control flow and avoid a branch to the
298 // fall-through label if it is set up.
299 void Split(Condition cc,
300 Label* if_true,
301 Label* if_false,
302 Label* fall_through);
Leon Clarked91b9f72010-01-27 17:25:45 +0000303
304 void Move(Slot* dst, Register source, Register scratch1, Register scratch2);
305 void Move(Register dst, Slot* source);
306
307 // Return an operand used to read/write to a known (ie, non-LOOKUP) slot.
308 // May emit code to traverse the context chain, destroying the scratch
309 // register.
310 MemOperand EmitSlotSearch(Slot* slot, Register scratch);
311
312 void VisitForEffect(Expression* expr) {
313 Expression::Context saved_context = context_;
314 context_ = Expression::kEffect;
315 Visit(expr);
316 context_ = saved_context;
317 }
318
319 void VisitForValue(Expression* expr, Location where) {
320 Expression::Context saved_context = context_;
321 Location saved_location = location_;
322 context_ = Expression::kValue;
323 location_ = where;
324 Visit(expr);
325 context_ = saved_context;
326 location_ = saved_location;
327 }
328
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100329 void VisitForControl(Expression* expr,
330 Label* if_true,
331 Label* if_false,
332 Label* fall_through) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000333 Expression::Context saved_context = context_;
334 Label* saved_true = true_label_;
335 Label* saved_false = false_label_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100336 Label* saved_fall_through = fall_through_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000337 context_ = Expression::kTest;
338 true_label_ = if_true;
339 false_label_ = if_false;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100340 fall_through_ = fall_through;
Leon Clarked91b9f72010-01-27 17:25:45 +0000341 Visit(expr);
342 context_ = saved_context;
343 true_label_ = saved_true;
344 false_label_ = saved_false;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100345 fall_through_ = saved_fall_through;
Leon Clarked91b9f72010-01-27 17:25:45 +0000346 }
347
348 void VisitDeclarations(ZoneList<Declaration*>* declarations);
349 void DeclareGlobals(Handle<FixedArray> pairs);
350
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100351 // Try to perform a comparison as a fast inlined literal compare if
352 // the operands allow it. Returns true if the compare operations
353 // has been matched and all code generated; false otherwise.
354 bool TryLiteralCompare(Token::Value op,
355 Expression* left,
356 Expression* right,
357 Label* if_true,
358 Label* if_false,
359 Label* fall_through);
360
Leon Clarkef7060e22010-06-03 12:02:55 +0100361 // Platform-specific code for a variable, constant, or function
362 // declaration. Functions have an initial value.
363 void EmitDeclaration(Variable* variable,
364 Variable::Mode mode,
365 FunctionLiteral* function);
366
Leon Clarked91b9f72010-01-27 17:25:45 +0000367 // Platform-specific return sequence
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100368 void EmitReturnSequence();
Leon Clarked91b9f72010-01-27 17:25:45 +0000369
370 // Platform-specific code sequences for calls
371 void EmitCallWithStub(Call* expr);
372 void EmitCallWithIC(Call* expr, Handle<Object> name, RelocInfo::Mode mode);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100373 void EmitKeyedCallWithIC(Call* expr, Expression* key, RelocInfo::Mode mode);
Leon Clarked91b9f72010-01-27 17:25:45 +0000374
Leon Clarkef7060e22010-06-03 12:02:55 +0100375 // Platform-specific code for inline runtime calls.
376 void EmitInlineRuntimeCall(CallRuntime* expr);
Steve Block791712a2010-08-27 10:21:07 +0100377
378#define EMIT_INLINE_RUNTIME_CALL(name, x, y) \
379 void Emit##name(ZoneList<Expression*>* arguments);
380 INLINE_RUNTIME_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
381#undef EMIT_INLINE_RUNTIME_CALL
Leon Clarkef7060e22010-06-03 12:02:55 +0100382
Leon Clarked91b9f72010-01-27 17:25:45 +0000383 // Platform-specific code for loading variables.
384 void EmitVariableLoad(Variable* expr, Expression::Context context);
385
Leon Clarkef7060e22010-06-03 12:02:55 +0100386 // Platform-specific support for allocating a new closure based on
387 // the given function info.
388 void EmitNewClosure(Handle<SharedFunctionInfo> info);
389
Leon Clarked91b9f72010-01-27 17:25:45 +0000390 // Platform-specific support for compiling assignments.
391
392 // Load a value from a named property.
393 // The receiver is left on the stack by the IC.
394 void EmitNamedPropertyLoad(Property* expr);
395
396 // Load a value from a keyed property.
397 // The receiver and the key is left on the stack by the IC.
398 void EmitKeyedPropertyLoad(Property* expr);
399
400 // Apply the compound assignment operator. Expects the left operand on top
401 // of the stack and the right one in the accumulator.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100402 void EmitBinaryOp(Token::Value op,
403 Expression::Context context,
404 OverwriteMode mode);
405
406 // Helper functions for generating inlined smi code for certain
407 // binary operations.
408 void EmitInlineSmiBinaryOp(Expression* expr,
409 Token::Value op,
410 Expression::Context context,
411 OverwriteMode mode,
412 Expression* left,
413 Expression* right,
414 ConstantOperand constant);
415
416 void EmitConstantSmiBinaryOp(Expression* expr,
417 Token::Value op,
418 Expression::Context context,
419 OverwriteMode mode,
420 bool left_is_constant_smi,
421 Smi* value);
422
423 void EmitConstantSmiBitOp(Expression* expr,
424 Token::Value op,
425 Expression::Context context,
426 OverwriteMode mode,
427 Smi* value);
428
429 void EmitConstantSmiShiftOp(Expression* expr,
430 Token::Value op,
431 Expression::Context context,
432 OverwriteMode mode,
433 Smi* value);
434
435 void EmitConstantSmiAdd(Expression* expr,
436 Expression::Context context,
437 OverwriteMode mode,
438 bool left_is_constant_smi,
439 Smi* value);
440
441 void EmitConstantSmiSub(Expression* expr,
442 Expression::Context context,
443 OverwriteMode mode,
444 bool left_is_constant_smi,
445 Smi* value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000446
Leon Clarkef7060e22010-06-03 12:02:55 +0100447 // Assign to the given expression as if via '='. The right-hand-side value
448 // is expected in the accumulator.
449 void EmitAssignment(Expression* expr);
450
Leon Clarked91b9f72010-01-27 17:25:45 +0000451 // Complete a variable assignment. The right-hand-side value is expected
452 // in the accumulator.
Leon Clarkef7060e22010-06-03 12:02:55 +0100453 void EmitVariableAssignment(Variable* var,
454 Token::Value op,
455 Expression::Context context);
Leon Clarked91b9f72010-01-27 17:25:45 +0000456
457 // Complete a named property assignment. The receiver is expected on top
458 // of the stack and the right-hand-side value in the accumulator.
459 void EmitNamedPropertyAssignment(Assignment* expr);
460
461 // Complete a keyed property assignment. The receiver and key are
462 // expected on top of the stack and the right-hand-side value in the
463 // accumulator.
464 void EmitKeyedPropertyAssignment(Assignment* expr);
465
466 void SetFunctionPosition(FunctionLiteral* fun);
467 void SetReturnPosition(FunctionLiteral* fun);
468 void SetStatementPosition(Statement* stmt);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100469 void SetExpressionPosition(Expression* expr, int pos);
Leon Clarked91b9f72010-01-27 17:25:45 +0000470 void SetStatementPosition(int pos);
471 void SetSourcePosition(int pos);
472
473 // Non-local control flow support.
474 void EnterFinallyBlock();
475 void ExitFinallyBlock();
476
477 // Loop nesting counter.
478 int loop_depth() { return loop_depth_; }
479 void increment_loop_depth() { loop_depth_++; }
480 void decrement_loop_depth() {
481 ASSERT(loop_depth_ > 0);
482 loop_depth_--;
483 }
484
485 MacroAssembler* masm() { return masm_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000486
487 Handle<Script> script() { return info_->script(); }
488 bool is_eval() { return info_->is_eval(); }
489 FunctionLiteral* function() { return info_->function(); }
490 Scope* scope() { return info_->scope(); }
491
Leon Clarked91b9f72010-01-27 17:25:45 +0000492 static Register result_register();
493 static Register context_register();
494
495 // Set fields in the stack frame. Offsets are the frame pointer relative
496 // offsets defined in, e.g., StandardFrameConstants.
497 void StoreToFrameField(int frame_offset, Register value);
498
499 // Load a value from the current context. Indices are defined as an enum
500 // in v8::internal::Context.
501 void LoadContextField(Register dst, int context_index);
502
503 // AST node visit functions.
504#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
505 AST_NODE_LIST(DECLARE_VISIT)
506#undef DECLARE_VISIT
507 // Handles the shortcutted logical binary operations in VisitBinaryOperation.
508 void EmitLogicalOperation(BinaryOperation* expr);
509
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100510 void VisitForTypeofValue(Expression* expr, Location where);
511
512 void VisitLogicalForValue(Expression* expr,
513 Token::Value op,
514 Location where,
515 Label* done);
516
517
Leon Clarked91b9f72010-01-27 17:25:45 +0000518 MacroAssembler* masm_;
Andrei Popescu31002712010-02-23 13:46:05 +0000519 CompilationInfo* info_;
Leon Clarke4515c472010-02-03 11:58:03 +0000520
Leon Clarked91b9f72010-01-27 17:25:45 +0000521 Label return_label_;
522 NestedStatement* nesting_stack_;
523 int loop_depth_;
524
525 Expression::Context context_;
526 Location location_;
527 Label* true_label_;
528 Label* false_label_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100529 Label* fall_through_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000530
531 friend class NestedStatement;
532
533 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
534};
535
536
537} } // namespace v8::internal
538
539#endif // V8_FULL_CODEGEN_H_