blob: a3270aa774e52c07666f6816251276c6e69d9bb3 [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);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800426 void SetSourcePosition(
427 int pos,
428 PositionRecordingType recording_type = NORMAL_POSITION);
Leon Clarked91b9f72010-01-27 17:25:45 +0000429
430 // Non-local control flow support.
431 void EnterFinallyBlock();
432 void ExitFinallyBlock();
433
434 // Loop nesting counter.
435 int loop_depth() { return loop_depth_; }
436 void increment_loop_depth() { loop_depth_++; }
437 void decrement_loop_depth() {
438 ASSERT(loop_depth_ > 0);
439 loop_depth_--;
440 }
441
442 MacroAssembler* masm() { return masm_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000443
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100444 class ExpressionContext;
445 const ExpressionContext* context() { return context_; }
446 void set_new_context(const ExpressionContext* context) { context_ = context; }
447
Andrei Popescu31002712010-02-23 13:46:05 +0000448 Handle<Script> script() { return info_->script(); }
449 bool is_eval() { return info_->is_eval(); }
450 FunctionLiteral* function() { return info_->function(); }
451 Scope* scope() { return info_->scope(); }
452
Leon Clarked91b9f72010-01-27 17:25:45 +0000453 static Register result_register();
454 static Register context_register();
455
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100456 // Helper for calling an IC stub.
457 void EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode);
458
Leon Clarked91b9f72010-01-27 17:25:45 +0000459 // Set fields in the stack frame. Offsets are the frame pointer relative
460 // offsets defined in, e.g., StandardFrameConstants.
461 void StoreToFrameField(int frame_offset, Register value);
462
463 // Load a value from the current context. Indices are defined as an enum
464 // in v8::internal::Context.
465 void LoadContextField(Register dst, int context_index);
466
Steve Block59151502010-09-22 15:07:15 +0100467 // Create an operand for a context field.
468 MemOperand ContextOperand(Register context, int context_index);
469
Leon Clarked91b9f72010-01-27 17:25:45 +0000470 // AST node visit functions.
471#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
472 AST_NODE_LIST(DECLARE_VISIT)
473#undef DECLARE_VISIT
474 // Handles the shortcutted logical binary operations in VisitBinaryOperation.
475 void EmitLogicalOperation(BinaryOperation* expr);
476
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100477 void VisitForTypeofValue(Expression* expr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100478
Leon Clarked91b9f72010-01-27 17:25:45 +0000479 MacroAssembler* masm_;
Andrei Popescu31002712010-02-23 13:46:05 +0000480 CompilationInfo* info_;
Leon Clarke4515c472010-02-03 11:58:03 +0000481
Leon Clarked91b9f72010-01-27 17:25:45 +0000482 Label return_label_;
483 NestedStatement* nesting_stack_;
484 int loop_depth_;
485
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100486 class ExpressionContext {
487 public:
488 explicit ExpressionContext(FullCodeGenerator* codegen)
489 : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
490 codegen->set_new_context(this);
491 }
492
493 virtual ~ExpressionContext() {
494 codegen_->set_new_context(old_);
495 }
496
497 // Convert constant control flow (true or false) to the result expected for
498 // this expression context.
499 virtual void Plug(bool flag) const = 0;
500
501 // Emit code to convert a pure value (in a register, slot, as a literal,
502 // or on top of the stack) into the result expected according to this
503 // expression context.
504 virtual void Plug(Register reg) const = 0;
505 virtual void Plug(Slot* slot) const = 0;
506 virtual void Plug(Handle<Object> lit) const = 0;
507 virtual void Plug(Heap::RootListIndex index) const = 0;
508 virtual void PlugTOS() const = 0;
509
510 // Emit code to convert pure control flow to a pair of unbound labels into
511 // the result expected according to this expression context. The
512 // implementation may decide to bind either of the labels.
513 virtual void Plug(Label* materialize_true,
514 Label* materialize_false) const = 0;
515
516 // Emit code to discard count elements from the top of stack, then convert
517 // a pure value into the result expected according to this expression
518 // context.
519 virtual void DropAndPlug(int count, Register reg) const = 0;
520
521 // For shortcutting operations || and &&.
522 virtual void EmitLogicalLeft(BinaryOperation* expr,
523 Label* eval_right,
524 Label* done) const = 0;
525
526 // Set up branch labels for a test expression. The three Label** parameters
527 // are output parameters.
528 virtual void PrepareTest(Label* materialize_true,
529 Label* materialize_false,
530 Label** if_true,
531 Label** if_false,
532 Label** fall_through) const = 0;
533
534 // Returns true if we are evaluating only for side effects (ie if the result
535 // will be discarded.
536 virtual bool IsEffect() const { return false; }
537
538 // Returns true if we are branching on the value rather than materializing
539 // it.
540 virtual bool IsTest() const { return false; }
541
542 protected:
543 FullCodeGenerator* codegen() const { return codegen_; }
544 MacroAssembler* masm() const { return masm_; }
545 MacroAssembler* masm_;
546
547 private:
548 const ExpressionContext* old_;
549 FullCodeGenerator* codegen_;
550 };
551
552 class AccumulatorValueContext : public ExpressionContext {
553 public:
554 explicit AccumulatorValueContext(FullCodeGenerator* codegen)
555 : ExpressionContext(codegen) { }
556
557 virtual void Plug(bool flag) const;
558 virtual void Plug(Register reg) const;
559 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
560 virtual void Plug(Slot* slot) const;
561 virtual void Plug(Handle<Object> lit) const;
562 virtual void Plug(Heap::RootListIndex) const;
563 virtual void PlugTOS() const;
564 virtual void DropAndPlug(int count, Register reg) const;
565 virtual void EmitLogicalLeft(BinaryOperation* expr,
566 Label* eval_right,
567 Label* done) const;
568 virtual void PrepareTest(Label* materialize_true,
569 Label* materialize_false,
570 Label** if_true,
571 Label** if_false,
572 Label** fall_through) const;
573 };
574
575 class StackValueContext : public ExpressionContext {
576 public:
577 explicit StackValueContext(FullCodeGenerator* codegen)
578 : ExpressionContext(codegen) { }
579
580 virtual void Plug(bool flag) const;
581 virtual void Plug(Register reg) const;
582 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
583 virtual void Plug(Slot* slot) const;
584 virtual void Plug(Handle<Object> lit) const;
585 virtual void Plug(Heap::RootListIndex) const;
586 virtual void PlugTOS() const;
587 virtual void DropAndPlug(int count, Register reg) const;
588 virtual void EmitLogicalLeft(BinaryOperation* expr,
589 Label* eval_right,
590 Label* done) const;
591 virtual void PrepareTest(Label* materialize_true,
592 Label* materialize_false,
593 Label** if_true,
594 Label** if_false,
595 Label** fall_through) const;
596 };
597
598 class TestContext : public ExpressionContext {
599 public:
600 explicit TestContext(FullCodeGenerator* codegen,
601 Label* true_label,
602 Label* false_label,
603 Label* fall_through)
604 : ExpressionContext(codegen),
605 true_label_(true_label),
606 false_label_(false_label),
607 fall_through_(fall_through) { }
608
Ben Murdochf87a2032010-10-22 12:50:53 +0100609 static const TestContext* cast(const ExpressionContext* context) {
610 ASSERT(context->IsTest());
611 return reinterpret_cast<const TestContext*>(context);
612 }
613
614 Label* true_label() const { return true_label_; }
615 Label* false_label() const { return false_label_; }
616 Label* fall_through() const { return fall_through_; }
617
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100618 virtual void Plug(bool flag) const;
619 virtual void Plug(Register reg) const;
620 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
621 virtual void Plug(Slot* slot) const;
622 virtual void Plug(Handle<Object> lit) const;
623 virtual void Plug(Heap::RootListIndex) const;
624 virtual void PlugTOS() const;
625 virtual void DropAndPlug(int count, Register reg) const;
626 virtual void EmitLogicalLeft(BinaryOperation* expr,
627 Label* eval_right,
628 Label* done) const;
629 virtual void PrepareTest(Label* materialize_true,
630 Label* materialize_false,
631 Label** if_true,
632 Label** if_false,
633 Label** fall_through) const;
634 virtual bool IsTest() const { return true; }
635
636 private:
637 Label* true_label_;
638 Label* false_label_;
639 Label* fall_through_;
640 };
641
642 class EffectContext : public ExpressionContext {
643 public:
644 explicit EffectContext(FullCodeGenerator* codegen)
645 : ExpressionContext(codegen) { }
646
647 virtual void Plug(bool flag) const;
648 virtual void Plug(Register reg) const;
649 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
650 virtual void Plug(Slot* slot) const;
651 virtual void Plug(Handle<Object> lit) const;
652 virtual void Plug(Heap::RootListIndex) const;
653 virtual void PlugTOS() const;
654 virtual void DropAndPlug(int count, Register reg) const;
655 virtual void EmitLogicalLeft(BinaryOperation* expr,
656 Label* eval_right,
657 Label* done) const;
658 virtual void PrepareTest(Label* materialize_true,
659 Label* materialize_false,
660 Label** if_true,
661 Label** if_false,
662 Label** fall_through) const;
663 virtual bool IsEffect() const { return true; }
664 };
665
666 const ExpressionContext* context_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000667
668 friend class NestedStatement;
669
670 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
671};
672
673
674} } // namespace v8::internal
675
676#endif // V8_FULL_CODEGEN_H_