blob: 201507b2af338ea6e32b59d36c6a8cc145e22e0a [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),
Kristian Monsen0d5e1162010-09-30 15:31:59 +010074 context_(NULL) {
Leon Clarked91b9f72010-01-27 17:25:45 +000075 }
76
Ben Murdochf87a2032010-10-22 12:50:53 +010077 static bool MakeCode(CompilationInfo* info);
Leon Clarked91b9f72010-01-27 17:25:45 +000078
Iain Merrick75681382010-08-19 15:07:18 +010079 void Generate(CompilationInfo* info);
Leon Clarked91b9f72010-01-27 17:25:45 +000080
81 private:
82 class Breakable;
83 class Iteration;
84 class TryCatch;
85 class TryFinally;
86 class Finally;
87 class ForIn;
88
89 class NestedStatement BASE_EMBEDDED {
90 public:
91 explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
92 // Link into codegen's nesting stack.
93 previous_ = codegen->nesting_stack_;
94 codegen->nesting_stack_ = this;
95 }
96 virtual ~NestedStatement() {
97 // Unlink from codegen's nesting stack.
98 ASSERT_EQ(this, codegen_->nesting_stack_);
99 codegen_->nesting_stack_ = previous_;
100 }
101
102 virtual Breakable* AsBreakable() { return NULL; }
103 virtual Iteration* AsIteration() { return NULL; }
104 virtual TryCatch* AsTryCatch() { return NULL; }
105 virtual TryFinally* AsTryFinally() { return NULL; }
106 virtual Finally* AsFinally() { return NULL; }
107 virtual ForIn* AsForIn() { return NULL; }
108
109 virtual bool IsContinueTarget(Statement* target) { return false; }
110 virtual bool IsBreakTarget(Statement* target) { return false; }
111
112 // Generate code to leave the nested statement. This includes
113 // cleaning up any stack elements in use and restoring the
114 // stack to the expectations of the surrounding statements.
115 // Takes a number of stack elements currently on top of the
116 // nested statement's stack, and returns a number of stack
117 // elements left on top of the surrounding statement's stack.
118 // The generated code must preserve the result register (which
119 // contains the value in case of a return).
120 virtual int Exit(int stack_depth) {
121 // Default implementation for the case where there is
122 // nothing to clean up.
123 return stack_depth;
124 }
125 NestedStatement* outer() { return previous_; }
126 protected:
127 MacroAssembler* masm() { return codegen_->masm(); }
128 private:
129 FullCodeGenerator* codegen_;
130 NestedStatement* previous_;
131 DISALLOW_COPY_AND_ASSIGN(NestedStatement);
132 };
133
134 class Breakable : public NestedStatement {
135 public:
136 Breakable(FullCodeGenerator* codegen,
137 BreakableStatement* break_target)
138 : NestedStatement(codegen),
139 target_(break_target) {}
140 virtual ~Breakable() {}
141 virtual Breakable* AsBreakable() { return this; }
142 virtual bool IsBreakTarget(Statement* statement) {
143 return target_ == statement;
144 }
145 BreakableStatement* statement() { return target_; }
146 Label* break_target() { return &break_target_label_; }
147 private:
148 BreakableStatement* target_;
149 Label break_target_label_;
150 DISALLOW_COPY_AND_ASSIGN(Breakable);
151 };
152
153 class Iteration : public Breakable {
154 public:
155 Iteration(FullCodeGenerator* codegen,
156 IterationStatement* iteration_statement)
157 : Breakable(codegen, iteration_statement) {}
158 virtual ~Iteration() {}
159 virtual Iteration* AsIteration() { return this; }
160 virtual bool IsContinueTarget(Statement* statement) {
161 return this->statement() == statement;
162 }
163 Label* continue_target() { return &continue_target_label_; }
164 private:
165 Label continue_target_label_;
166 DISALLOW_COPY_AND_ASSIGN(Iteration);
167 };
168
169 // The environment inside the try block of a try/catch statement.
170 class TryCatch : public NestedStatement {
171 public:
172 explicit TryCatch(FullCodeGenerator* codegen, Label* catch_entry)
173 : NestedStatement(codegen), catch_entry_(catch_entry) { }
174 virtual ~TryCatch() {}
175 virtual TryCatch* AsTryCatch() { return this; }
176 Label* catch_entry() { return catch_entry_; }
177 virtual int Exit(int stack_depth);
178 private:
179 Label* catch_entry_;
180 DISALLOW_COPY_AND_ASSIGN(TryCatch);
181 };
182
183 // The environment inside the try block of a try/finally statement.
184 class TryFinally : public NestedStatement {
185 public:
186 explicit TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
187 : NestedStatement(codegen), finally_entry_(finally_entry) { }
188 virtual ~TryFinally() {}
189 virtual TryFinally* AsTryFinally() { return this; }
190 Label* finally_entry() { return finally_entry_; }
191 virtual int Exit(int stack_depth);
192 private:
193 Label* finally_entry_;
194 DISALLOW_COPY_AND_ASSIGN(TryFinally);
195 };
196
197 // A FinallyEnvironment represents being inside a finally block.
198 // Abnormal termination of the finally block needs to clean up
199 // the block's parameters from the stack.
200 class Finally : public NestedStatement {
201 public:
202 explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { }
203 virtual ~Finally() {}
204 virtual Finally* AsFinally() { return this; }
205 virtual int Exit(int stack_depth) {
206 return stack_depth + kFinallyStackElementCount;
207 }
208 private:
209 // Number of extra stack slots occupied during a finally block.
210 static const int kFinallyStackElementCount = 2;
211 DISALLOW_COPY_AND_ASSIGN(Finally);
212 };
213
214 // A ForInEnvironment represents being inside a for-in loop.
215 // Abnormal termination of the for-in block needs to clean up
216 // the block's temporary storage from the stack.
217 class ForIn : public Iteration {
218 public:
219 ForIn(FullCodeGenerator* codegen,
220 ForInStatement* statement)
221 : Iteration(codegen, statement) { }
222 virtual ~ForIn() {}
223 virtual ForIn* AsForIn() { return this; }
224 virtual int Exit(int stack_depth) {
225 return stack_depth + kForInStackElementCount;
226 }
227 private:
Leon Clarked91b9f72010-01-27 17:25:45 +0000228 static const int kForInStackElementCount = 5;
229 DISALLOW_COPY_AND_ASSIGN(ForIn);
230 };
231
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100232 enum ConstantOperand {
233 kNoConstants,
234 kLeftConstant,
235 kRightConstant
236 };
237
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100238 // Type of a member function that generates inline code for a native function.
239 typedef void (FullCodeGenerator::*InlineFunctionGenerator)
240 (ZoneList<Expression*>*);
241
242 static const InlineFunctionGenerator kInlineFunctionGenerators[];
243
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100244 // Compute the frame pointer relative offset for a given local or
245 // parameter slot.
Leon Clarked91b9f72010-01-27 17:25:45 +0000246 int SlotOffset(Slot* slot);
247
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100248 // Determine whether or not to inline the smi case for the given
249 // operation.
250 bool ShouldInlineSmiCase(Token::Value op);
251
252 // Compute which (if any) of the operands is a compile-time constant.
253 ConstantOperand GetConstantOperand(Token::Value op,
254 Expression* left,
255 Expression* right);
256
Leon Clarked91b9f72010-01-27 17:25:45 +0000257 // Helper function to convert a pure value into a test context. The value
258 // is expected on the stack or the accumulator, depending on the platform.
259 // See the platform-specific implementation for details.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100260 void DoTest(Label* if_true, Label* if_false, Label* fall_through);
261
262 // Helper function to split control flow and avoid a branch to the
263 // fall-through label if it is set up.
264 void Split(Condition cc,
265 Label* if_true,
266 Label* if_false,
267 Label* fall_through);
Leon Clarked91b9f72010-01-27 17:25:45 +0000268
269 void Move(Slot* dst, Register source, Register scratch1, Register scratch2);
270 void Move(Register dst, Slot* source);
271
272 // Return an operand used to read/write to a known (ie, non-LOOKUP) slot.
273 // May emit code to traverse the context chain, destroying the scratch
274 // register.
275 MemOperand EmitSlotSearch(Slot* slot, Register scratch);
276
277 void VisitForEffect(Expression* expr) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100278 EffectContext context(this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000279 Visit(expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000280 }
281
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100282 void VisitForAccumulatorValue(Expression* expr) {
283 AccumulatorValueContext context(this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000284 Visit(expr);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100285 }
286
287 void VisitForStackValue(Expression* expr) {
288 StackValueContext context(this);
289 Visit(expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000290 }
291
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100292 void VisitForControl(Expression* expr,
293 Label* if_true,
294 Label* if_false,
295 Label* fall_through) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100296 TestContext context(this, if_true, if_false, fall_through);
Leon Clarked91b9f72010-01-27 17:25:45 +0000297 Visit(expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000298 }
299
300 void VisitDeclarations(ZoneList<Declaration*>* declarations);
301 void DeclareGlobals(Handle<FixedArray> pairs);
302
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100303 // Try to perform a comparison as a fast inlined literal compare if
304 // the operands allow it. Returns true if the compare operations
305 // has been matched and all code generated; false otherwise.
306 bool TryLiteralCompare(Token::Value op,
307 Expression* left,
308 Expression* right,
309 Label* if_true,
310 Label* if_false,
311 Label* fall_through);
312
Leon Clarkef7060e22010-06-03 12:02:55 +0100313 // Platform-specific code for a variable, constant, or function
314 // declaration. Functions have an initial value.
315 void EmitDeclaration(Variable* variable,
316 Variable::Mode mode,
317 FunctionLiteral* function);
318
Leon Clarked91b9f72010-01-27 17:25:45 +0000319 // Platform-specific return sequence
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100320 void EmitReturnSequence();
Leon Clarked91b9f72010-01-27 17:25:45 +0000321
322 // Platform-specific code sequences for calls
323 void EmitCallWithStub(Call* expr);
324 void EmitCallWithIC(Call* expr, Handle<Object> name, RelocInfo::Mode mode);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100325 void EmitKeyedCallWithIC(Call* expr, Expression* key, RelocInfo::Mode mode);
Leon Clarked91b9f72010-01-27 17:25:45 +0000326
Leon Clarkef7060e22010-06-03 12:02:55 +0100327 // Platform-specific code for inline runtime calls.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100328 InlineFunctionGenerator FindInlineFunctionGenerator(Runtime::FunctionId id);
329
Leon Clarkef7060e22010-06-03 12:02:55 +0100330 void EmitInlineRuntimeCall(CallRuntime* expr);
Steve Block791712a2010-08-27 10:21:07 +0100331
332#define EMIT_INLINE_RUNTIME_CALL(name, x, y) \
333 void Emit##name(ZoneList<Expression*>* arguments);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100334 INLINE_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
Steve Block791712a2010-08-27 10:21:07 +0100335 INLINE_RUNTIME_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
336#undef EMIT_INLINE_RUNTIME_CALL
Leon Clarkef7060e22010-06-03 12:02:55 +0100337
Leon Clarked91b9f72010-01-27 17:25:45 +0000338 // Platform-specific code for loading variables.
Steve Block59151502010-09-22 15:07:15 +0100339 void EmitLoadGlobalSlotCheckExtensions(Slot* slot,
340 TypeofState typeof_state,
341 Label* slow);
342 MemOperand ContextSlotOperandCheckExtensions(Slot* slot, Label* slow);
343 void EmitDynamicLoadFromSlotFastCase(Slot* slot,
344 TypeofState typeof_state,
345 Label* slow,
346 Label* done);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100347 void EmitVariableLoad(Variable* expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000348
Leon Clarkef7060e22010-06-03 12:02:55 +0100349 // Platform-specific support for allocating a new closure based on
350 // the given function info.
351 void EmitNewClosure(Handle<SharedFunctionInfo> info);
352
Leon Clarked91b9f72010-01-27 17:25:45 +0000353 // Platform-specific support for compiling assignments.
354
355 // Load a value from a named property.
356 // The receiver is left on the stack by the IC.
357 void EmitNamedPropertyLoad(Property* expr);
358
359 // Load a value from a keyed property.
360 // The receiver and the key is left on the stack by the IC.
361 void EmitKeyedPropertyLoad(Property* expr);
362
363 // Apply the compound assignment operator. Expects the left operand on top
364 // of the stack and the right one in the accumulator.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100365 void EmitBinaryOp(Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100366 OverwriteMode mode);
367
368 // Helper functions for generating inlined smi code for certain
369 // binary operations.
370 void EmitInlineSmiBinaryOp(Expression* expr,
371 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100372 OverwriteMode mode,
373 Expression* left,
374 Expression* right,
375 ConstantOperand constant);
376
377 void EmitConstantSmiBinaryOp(Expression* expr,
378 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100379 OverwriteMode mode,
380 bool left_is_constant_smi,
381 Smi* value);
382
383 void EmitConstantSmiBitOp(Expression* expr,
384 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100385 OverwriteMode mode,
386 Smi* value);
387
388 void EmitConstantSmiShiftOp(Expression* expr,
389 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100390 OverwriteMode mode,
391 Smi* value);
392
393 void EmitConstantSmiAdd(Expression* expr,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100394 OverwriteMode mode,
395 bool left_is_constant_smi,
396 Smi* value);
397
398 void EmitConstantSmiSub(Expression* expr,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100399 OverwriteMode mode,
400 bool left_is_constant_smi,
401 Smi* value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000402
Leon Clarkef7060e22010-06-03 12:02:55 +0100403 // Assign to the given expression as if via '='. The right-hand-side value
404 // is expected in the accumulator.
405 void EmitAssignment(Expression* expr);
406
Leon Clarked91b9f72010-01-27 17:25:45 +0000407 // Complete a variable assignment. The right-hand-side value is expected
408 // in the accumulator.
Leon Clarkef7060e22010-06-03 12:02:55 +0100409 void EmitVariableAssignment(Variable* var,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100410 Token::Value op);
Leon Clarked91b9f72010-01-27 17:25:45 +0000411
412 // Complete a named property assignment. The receiver is expected on top
413 // of the stack and the right-hand-side value in the accumulator.
414 void EmitNamedPropertyAssignment(Assignment* expr);
415
416 // Complete a keyed property assignment. The receiver and key are
417 // expected on top of the stack and the right-hand-side value in the
418 // accumulator.
419 void EmitKeyedPropertyAssignment(Assignment* expr);
420
421 void SetFunctionPosition(FunctionLiteral* fun);
422 void SetReturnPosition(FunctionLiteral* fun);
423 void SetStatementPosition(Statement* stmt);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100424 void SetExpressionPosition(Expression* expr, int pos);
Leon Clarked91b9f72010-01-27 17:25:45 +0000425 void SetStatementPosition(int pos);
426 void SetSourcePosition(int pos);
427
428 // Non-local control flow support.
429 void EnterFinallyBlock();
430 void ExitFinallyBlock();
431
432 // Loop nesting counter.
433 int loop_depth() { return loop_depth_; }
434 void increment_loop_depth() { loop_depth_++; }
435 void decrement_loop_depth() {
436 ASSERT(loop_depth_ > 0);
437 loop_depth_--;
438 }
439
440 MacroAssembler* masm() { return masm_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000441
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100442 class ExpressionContext;
443 const ExpressionContext* context() { return context_; }
444 void set_new_context(const ExpressionContext* context) { context_ = context; }
445
Andrei Popescu31002712010-02-23 13:46:05 +0000446 Handle<Script> script() { return info_->script(); }
447 bool is_eval() { return info_->is_eval(); }
448 FunctionLiteral* function() { return info_->function(); }
449 Scope* scope() { return info_->scope(); }
450
Leon Clarked91b9f72010-01-27 17:25:45 +0000451 static Register result_register();
452 static Register context_register();
453
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100454 // Helper for calling an IC stub.
455 void EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode);
456
Leon Clarked91b9f72010-01-27 17:25:45 +0000457 // Set fields in the stack frame. Offsets are the frame pointer relative
458 // offsets defined in, e.g., StandardFrameConstants.
459 void StoreToFrameField(int frame_offset, Register value);
460
461 // Load a value from the current context. Indices are defined as an enum
462 // in v8::internal::Context.
463 void LoadContextField(Register dst, int context_index);
464
Steve Block59151502010-09-22 15:07:15 +0100465 // Create an operand for a context field.
466 MemOperand ContextOperand(Register context, int context_index);
467
Leon Clarked91b9f72010-01-27 17:25:45 +0000468 // AST node visit functions.
469#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
470 AST_NODE_LIST(DECLARE_VISIT)
471#undef DECLARE_VISIT
472 // Handles the shortcutted logical binary operations in VisitBinaryOperation.
473 void EmitLogicalOperation(BinaryOperation* expr);
474
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100475 void VisitForTypeofValue(Expression* expr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100476
Leon Clarked91b9f72010-01-27 17:25:45 +0000477 MacroAssembler* masm_;
Andrei Popescu31002712010-02-23 13:46:05 +0000478 CompilationInfo* info_;
Leon Clarke4515c472010-02-03 11:58:03 +0000479
Leon Clarked91b9f72010-01-27 17:25:45 +0000480 Label return_label_;
481 NestedStatement* nesting_stack_;
482 int loop_depth_;
483
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100484 class ExpressionContext {
485 public:
486 explicit ExpressionContext(FullCodeGenerator* codegen)
487 : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
488 codegen->set_new_context(this);
489 }
490
491 virtual ~ExpressionContext() {
492 codegen_->set_new_context(old_);
493 }
494
495 // Convert constant control flow (true or false) to the result expected for
496 // this expression context.
497 virtual void Plug(bool flag) const = 0;
498
499 // Emit code to convert a pure value (in a register, slot, as a literal,
500 // or on top of the stack) into the result expected according to this
501 // expression context.
502 virtual void Plug(Register reg) const = 0;
503 virtual void Plug(Slot* slot) const = 0;
504 virtual void Plug(Handle<Object> lit) const = 0;
505 virtual void Plug(Heap::RootListIndex index) const = 0;
506 virtual void PlugTOS() const = 0;
507
508 // Emit code to convert pure control flow to a pair of unbound labels into
509 // the result expected according to this expression context. The
510 // implementation may decide to bind either of the labels.
511 virtual void Plug(Label* materialize_true,
512 Label* materialize_false) const = 0;
513
514 // Emit code to discard count elements from the top of stack, then convert
515 // a pure value into the result expected according to this expression
516 // context.
517 virtual void DropAndPlug(int count, Register reg) const = 0;
518
519 // For shortcutting operations || and &&.
520 virtual void EmitLogicalLeft(BinaryOperation* expr,
521 Label* eval_right,
522 Label* done) const = 0;
523
524 // Set up branch labels for a test expression. The three Label** parameters
525 // are output parameters.
526 virtual void PrepareTest(Label* materialize_true,
527 Label* materialize_false,
528 Label** if_true,
529 Label** if_false,
530 Label** fall_through) const = 0;
531
532 // Returns true if we are evaluating only for side effects (ie if the result
533 // will be discarded.
534 virtual bool IsEffect() const { return false; }
535
536 // Returns true if we are branching on the value rather than materializing
537 // it.
538 virtual bool IsTest() const { return false; }
539
540 protected:
541 FullCodeGenerator* codegen() const { return codegen_; }
542 MacroAssembler* masm() const { return masm_; }
543 MacroAssembler* masm_;
544
545 private:
546 const ExpressionContext* old_;
547 FullCodeGenerator* codegen_;
548 };
549
550 class AccumulatorValueContext : public ExpressionContext {
551 public:
552 explicit AccumulatorValueContext(FullCodeGenerator* codegen)
553 : ExpressionContext(codegen) { }
554
555 virtual void Plug(bool flag) const;
556 virtual void Plug(Register reg) const;
557 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
558 virtual void Plug(Slot* slot) const;
559 virtual void Plug(Handle<Object> lit) const;
560 virtual void Plug(Heap::RootListIndex) const;
561 virtual void PlugTOS() const;
562 virtual void DropAndPlug(int count, Register reg) const;
563 virtual void EmitLogicalLeft(BinaryOperation* expr,
564 Label* eval_right,
565 Label* done) const;
566 virtual void PrepareTest(Label* materialize_true,
567 Label* materialize_false,
568 Label** if_true,
569 Label** if_false,
570 Label** fall_through) const;
571 };
572
573 class StackValueContext : public ExpressionContext {
574 public:
575 explicit StackValueContext(FullCodeGenerator* codegen)
576 : ExpressionContext(codegen) { }
577
578 virtual void Plug(bool flag) const;
579 virtual void Plug(Register reg) const;
580 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
581 virtual void Plug(Slot* slot) const;
582 virtual void Plug(Handle<Object> lit) const;
583 virtual void Plug(Heap::RootListIndex) const;
584 virtual void PlugTOS() const;
585 virtual void DropAndPlug(int count, Register reg) const;
586 virtual void EmitLogicalLeft(BinaryOperation* expr,
587 Label* eval_right,
588 Label* done) const;
589 virtual void PrepareTest(Label* materialize_true,
590 Label* materialize_false,
591 Label** if_true,
592 Label** if_false,
593 Label** fall_through) const;
594 };
595
596 class TestContext : public ExpressionContext {
597 public:
598 explicit TestContext(FullCodeGenerator* codegen,
599 Label* true_label,
600 Label* false_label,
601 Label* fall_through)
602 : ExpressionContext(codegen),
603 true_label_(true_label),
604 false_label_(false_label),
605 fall_through_(fall_through) { }
606
Ben Murdochf87a2032010-10-22 12:50:53 +0100607 static const TestContext* cast(const ExpressionContext* context) {
608 ASSERT(context->IsTest());
609 return reinterpret_cast<const TestContext*>(context);
610 }
611
612 Label* true_label() const { return true_label_; }
613 Label* false_label() const { return false_label_; }
614 Label* fall_through() const { return fall_through_; }
615
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100616 virtual void Plug(bool flag) const;
617 virtual void Plug(Register reg) const;
618 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
619 virtual void Plug(Slot* slot) const;
620 virtual void Plug(Handle<Object> lit) const;
621 virtual void Plug(Heap::RootListIndex) const;
622 virtual void PlugTOS() const;
623 virtual void DropAndPlug(int count, Register reg) const;
624 virtual void EmitLogicalLeft(BinaryOperation* expr,
625 Label* eval_right,
626 Label* done) const;
627 virtual void PrepareTest(Label* materialize_true,
628 Label* materialize_false,
629 Label** if_true,
630 Label** if_false,
631 Label** fall_through) const;
632 virtual bool IsTest() const { return true; }
633
634 private:
635 Label* true_label_;
636 Label* false_label_;
637 Label* fall_through_;
638 };
639
640 class EffectContext : public ExpressionContext {
641 public:
642 explicit EffectContext(FullCodeGenerator* codegen)
643 : ExpressionContext(codegen) { }
644
645 virtual void Plug(bool flag) const;
646 virtual void Plug(Register reg) const;
647 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
648 virtual void Plug(Slot* slot) const;
649 virtual void Plug(Handle<Object> lit) const;
650 virtual void Plug(Heap::RootListIndex) const;
651 virtual void PlugTOS() const;
652 virtual void DropAndPlug(int count, Register reg) const;
653 virtual void EmitLogicalLeft(BinaryOperation* expr,
654 Label* eval_right,
655 Label* done) const;
656 virtual void PrepareTest(Label* materialize_true,
657 Label* materialize_false,
658 Label** if_true,
659 Label** if_false,
660 Label** fall_through) const;
661 virtual bool IsEffect() const { return true; }
662 };
663
664 const ExpressionContext* context_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000665
666 friend class NestedStatement;
667
668 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
669};
670
671
672} } // namespace v8::internal
673
674#endif // V8_FULL_CODEGEN_H_