blob: 02335a928f14e0ed1234b045cea5243d9cf03048 [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
Ben Murdochdb5a90a2011-01-06 18:27:03 +0000244 // A platform-specific utility to overwrite the accumulator register
245 // with a GC-safe value.
246 void ClearAccumulator();
247
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100248 // Compute the frame pointer relative offset for a given local or
249 // parameter slot.
Leon Clarked91b9f72010-01-27 17:25:45 +0000250 int SlotOffset(Slot* slot);
251
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100252 // Determine whether or not to inline the smi case for the given
253 // operation.
254 bool ShouldInlineSmiCase(Token::Value op);
255
256 // Compute which (if any) of the operands is a compile-time constant.
257 ConstantOperand GetConstantOperand(Token::Value op,
258 Expression* left,
259 Expression* right);
260
Leon Clarked91b9f72010-01-27 17:25:45 +0000261 // Helper function to convert a pure value into a test context. The value
262 // is expected on the stack or the accumulator, depending on the platform.
263 // See the platform-specific implementation for details.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100264 void DoTest(Label* if_true, Label* if_false, Label* fall_through);
265
266 // Helper function to split control flow and avoid a branch to the
267 // fall-through label if it is set up.
268 void Split(Condition cc,
269 Label* if_true,
270 Label* if_false,
271 Label* fall_through);
Leon Clarked91b9f72010-01-27 17:25:45 +0000272
273 void Move(Slot* dst, Register source, Register scratch1, Register scratch2);
274 void Move(Register dst, Slot* source);
275
276 // Return an operand used to read/write to a known (ie, non-LOOKUP) slot.
277 // May emit code to traverse the context chain, destroying the scratch
278 // register.
279 MemOperand EmitSlotSearch(Slot* slot, Register scratch);
280
281 void VisitForEffect(Expression* expr) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100282 EffectContext context(this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000283 Visit(expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000284 }
285
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100286 void VisitForAccumulatorValue(Expression* expr) {
287 AccumulatorValueContext context(this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000288 Visit(expr);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100289 }
290
291 void VisitForStackValue(Expression* expr) {
292 StackValueContext context(this);
293 Visit(expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000294 }
295
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100296 void VisitForControl(Expression* expr,
297 Label* if_true,
298 Label* if_false,
299 Label* fall_through) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100300 TestContext context(this, if_true, if_false, fall_through);
Leon Clarked91b9f72010-01-27 17:25:45 +0000301 Visit(expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000302 }
303
304 void VisitDeclarations(ZoneList<Declaration*>* declarations);
305 void DeclareGlobals(Handle<FixedArray> pairs);
306
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100307 // Try to perform a comparison as a fast inlined literal compare if
308 // the operands allow it. Returns true if the compare operations
309 // has been matched and all code generated; false otherwise.
310 bool TryLiteralCompare(Token::Value op,
311 Expression* left,
312 Expression* right,
313 Label* if_true,
314 Label* if_false,
315 Label* fall_through);
316
Leon Clarkef7060e22010-06-03 12:02:55 +0100317 // Platform-specific code for a variable, constant, or function
318 // declaration. Functions have an initial value.
319 void EmitDeclaration(Variable* variable,
320 Variable::Mode mode,
321 FunctionLiteral* function);
322
Leon Clarked91b9f72010-01-27 17:25:45 +0000323 // Platform-specific return sequence
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100324 void EmitReturnSequence();
Leon Clarked91b9f72010-01-27 17:25:45 +0000325
326 // Platform-specific code sequences for calls
327 void EmitCallWithStub(Call* expr);
328 void EmitCallWithIC(Call* expr, Handle<Object> name, RelocInfo::Mode mode);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100329 void EmitKeyedCallWithIC(Call* expr, Expression* key, RelocInfo::Mode mode);
Leon Clarked91b9f72010-01-27 17:25:45 +0000330
Leon Clarkef7060e22010-06-03 12:02:55 +0100331 // Platform-specific code for inline runtime calls.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100332 InlineFunctionGenerator FindInlineFunctionGenerator(Runtime::FunctionId id);
333
Leon Clarkef7060e22010-06-03 12:02:55 +0100334 void EmitInlineRuntimeCall(CallRuntime* expr);
Steve Block791712a2010-08-27 10:21:07 +0100335
336#define EMIT_INLINE_RUNTIME_CALL(name, x, y) \
337 void Emit##name(ZoneList<Expression*>* arguments);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100338 INLINE_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
Steve Block791712a2010-08-27 10:21:07 +0100339 INLINE_RUNTIME_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
340#undef EMIT_INLINE_RUNTIME_CALL
Leon Clarkef7060e22010-06-03 12:02:55 +0100341
Leon Clarked91b9f72010-01-27 17:25:45 +0000342 // Platform-specific code for loading variables.
Steve Block59151502010-09-22 15:07:15 +0100343 void EmitLoadGlobalSlotCheckExtensions(Slot* slot,
344 TypeofState typeof_state,
345 Label* slow);
346 MemOperand ContextSlotOperandCheckExtensions(Slot* slot, Label* slow);
347 void EmitDynamicLoadFromSlotFastCase(Slot* slot,
348 TypeofState typeof_state,
349 Label* slow,
350 Label* done);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100351 void EmitVariableLoad(Variable* expr);
Leon Clarked91b9f72010-01-27 17:25:45 +0000352
Leon Clarkef7060e22010-06-03 12:02:55 +0100353 // Platform-specific support for allocating a new closure based on
354 // the given function info.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800355 void EmitNewClosure(Handle<SharedFunctionInfo> info, bool pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +0100356
Leon Clarked91b9f72010-01-27 17:25:45 +0000357 // Platform-specific support for compiling assignments.
358
359 // Load a value from a named property.
360 // The receiver is left on the stack by the IC.
361 void EmitNamedPropertyLoad(Property* expr);
362
363 // Load a value from a keyed property.
364 // The receiver and the key is left on the stack by the IC.
365 void EmitKeyedPropertyLoad(Property* expr);
366
367 // Apply the compound assignment operator. Expects the left operand on top
368 // of the stack and the right one in the accumulator.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100369 void EmitBinaryOp(Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100370 OverwriteMode mode);
371
372 // Helper functions for generating inlined smi code for certain
373 // binary operations.
374 void EmitInlineSmiBinaryOp(Expression* expr,
375 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100376 OverwriteMode mode,
377 Expression* left,
378 Expression* right,
379 ConstantOperand constant);
380
381 void EmitConstantSmiBinaryOp(Expression* expr,
382 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100383 OverwriteMode mode,
384 bool left_is_constant_smi,
385 Smi* value);
386
387 void EmitConstantSmiBitOp(Expression* expr,
388 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100389 OverwriteMode mode,
390 Smi* value);
391
392 void EmitConstantSmiShiftOp(Expression* expr,
393 Token::Value op,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100394 OverwriteMode mode,
395 Smi* value);
396
397 void EmitConstantSmiAdd(Expression* expr,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100398 OverwriteMode mode,
399 bool left_is_constant_smi,
400 Smi* value);
401
402 void EmitConstantSmiSub(Expression* expr,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100403 OverwriteMode mode,
404 bool left_is_constant_smi,
405 Smi* value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000406
Leon Clarkef7060e22010-06-03 12:02:55 +0100407 // Assign to the given expression as if via '='. The right-hand-side value
408 // is expected in the accumulator.
409 void EmitAssignment(Expression* expr);
410
Leon Clarked91b9f72010-01-27 17:25:45 +0000411 // Complete a variable assignment. The right-hand-side value is expected
412 // in the accumulator.
Leon Clarkef7060e22010-06-03 12:02:55 +0100413 void EmitVariableAssignment(Variable* var,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100414 Token::Value op);
Leon Clarked91b9f72010-01-27 17:25:45 +0000415
416 // Complete a named property assignment. The receiver is expected on top
417 // of the stack and the right-hand-side value in the accumulator.
418 void EmitNamedPropertyAssignment(Assignment* expr);
419
420 // Complete a keyed property assignment. The receiver and key are
421 // expected on top of the stack and the right-hand-side value in the
422 // accumulator.
423 void EmitKeyedPropertyAssignment(Assignment* expr);
424
425 void SetFunctionPosition(FunctionLiteral* fun);
426 void SetReturnPosition(FunctionLiteral* fun);
427 void SetStatementPosition(Statement* stmt);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100428 void SetExpressionPosition(Expression* expr, int pos);
Leon Clarked91b9f72010-01-27 17:25:45 +0000429 void SetStatementPosition(int pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800430 void SetSourcePosition(
431 int pos,
432 PositionRecordingType recording_type = NORMAL_POSITION);
Leon Clarked91b9f72010-01-27 17:25:45 +0000433
434 // Non-local control flow support.
435 void EnterFinallyBlock();
436 void ExitFinallyBlock();
437
438 // Loop nesting counter.
439 int loop_depth() { return loop_depth_; }
440 void increment_loop_depth() { loop_depth_++; }
441 void decrement_loop_depth() {
442 ASSERT(loop_depth_ > 0);
443 loop_depth_--;
444 }
445
446 MacroAssembler* masm() { return masm_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000447
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100448 class ExpressionContext;
449 const ExpressionContext* context() { return context_; }
450 void set_new_context(const ExpressionContext* context) { context_ = context; }
451
Andrei Popescu31002712010-02-23 13:46:05 +0000452 Handle<Script> script() { return info_->script(); }
453 bool is_eval() { return info_->is_eval(); }
454 FunctionLiteral* function() { return info_->function(); }
455 Scope* scope() { return info_->scope(); }
456
Leon Clarked91b9f72010-01-27 17:25:45 +0000457 static Register result_register();
458 static Register context_register();
459
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100460 // Helper for calling an IC stub.
461 void EmitCallIC(Handle<Code> ic, RelocInfo::Mode mode);
462
Leon Clarked91b9f72010-01-27 17:25:45 +0000463 // Set fields in the stack frame. Offsets are the frame pointer relative
464 // offsets defined in, e.g., StandardFrameConstants.
465 void StoreToFrameField(int frame_offset, Register value);
466
467 // Load a value from the current context. Indices are defined as an enum
468 // in v8::internal::Context.
469 void LoadContextField(Register dst, int context_index);
470
471 // AST node visit functions.
472#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
473 AST_NODE_LIST(DECLARE_VISIT)
474#undef DECLARE_VISIT
475 // Handles the shortcutted logical binary operations in VisitBinaryOperation.
476 void EmitLogicalOperation(BinaryOperation* expr);
477
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100478 void VisitForTypeofValue(Expression* expr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100479
Leon Clarked91b9f72010-01-27 17:25:45 +0000480 MacroAssembler* masm_;
Andrei Popescu31002712010-02-23 13:46:05 +0000481 CompilationInfo* info_;
Leon Clarke4515c472010-02-03 11:58:03 +0000482
Leon Clarked91b9f72010-01-27 17:25:45 +0000483 Label return_label_;
484 NestedStatement* nesting_stack_;
485 int loop_depth_;
486
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100487 class ExpressionContext {
488 public:
489 explicit ExpressionContext(FullCodeGenerator* codegen)
490 : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
491 codegen->set_new_context(this);
492 }
493
494 virtual ~ExpressionContext() {
495 codegen_->set_new_context(old_);
496 }
497
498 // Convert constant control flow (true or false) to the result expected for
499 // this expression context.
500 virtual void Plug(bool flag) const = 0;
501
502 // Emit code to convert a pure value (in a register, slot, as a literal,
503 // or on top of the stack) into the result expected according to this
504 // expression context.
505 virtual void Plug(Register reg) const = 0;
506 virtual void Plug(Slot* slot) const = 0;
507 virtual void Plug(Handle<Object> lit) const = 0;
508 virtual void Plug(Heap::RootListIndex index) const = 0;
509 virtual void PlugTOS() const = 0;
510
511 // Emit code to convert pure control flow to a pair of unbound labels into
512 // the result expected according to this expression context. The
513 // implementation may decide to bind either of the labels.
514 virtual void Plug(Label* materialize_true,
515 Label* materialize_false) const = 0;
516
517 // Emit code to discard count elements from the top of stack, then convert
518 // a pure value into the result expected according to this expression
519 // context.
520 virtual void DropAndPlug(int count, Register reg) const = 0;
521
522 // For shortcutting operations || and &&.
523 virtual void EmitLogicalLeft(BinaryOperation* expr,
524 Label* eval_right,
525 Label* done) const = 0;
526
527 // Set up branch labels for a test expression. The three Label** parameters
528 // are output parameters.
529 virtual void PrepareTest(Label* materialize_true,
530 Label* materialize_false,
531 Label** if_true,
532 Label** if_false,
533 Label** fall_through) const = 0;
534
535 // Returns true if we are evaluating only for side effects (ie if the result
536 // will be discarded.
537 virtual bool IsEffect() const { return false; }
538
539 // Returns true if we are branching on the value rather than materializing
540 // it.
541 virtual bool IsTest() const { return false; }
542
543 protected:
544 FullCodeGenerator* codegen() const { return codegen_; }
545 MacroAssembler* masm() const { return masm_; }
546 MacroAssembler* masm_;
547
548 private:
549 const ExpressionContext* old_;
550 FullCodeGenerator* codegen_;
551 };
552
553 class AccumulatorValueContext : public ExpressionContext {
554 public:
555 explicit AccumulatorValueContext(FullCodeGenerator* codegen)
556 : ExpressionContext(codegen) { }
557
558 virtual void Plug(bool flag) const;
559 virtual void Plug(Register reg) const;
560 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
561 virtual void Plug(Slot* slot) const;
562 virtual void Plug(Handle<Object> lit) const;
563 virtual void Plug(Heap::RootListIndex) const;
564 virtual void PlugTOS() const;
565 virtual void DropAndPlug(int count, Register reg) const;
566 virtual void EmitLogicalLeft(BinaryOperation* expr,
567 Label* eval_right,
568 Label* done) const;
569 virtual void PrepareTest(Label* materialize_true,
570 Label* materialize_false,
571 Label** if_true,
572 Label** if_false,
573 Label** fall_through) const;
574 };
575
576 class StackValueContext : public ExpressionContext {
577 public:
578 explicit StackValueContext(FullCodeGenerator* codegen)
579 : ExpressionContext(codegen) { }
580
581 virtual void Plug(bool flag) const;
582 virtual void Plug(Register reg) const;
583 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
584 virtual void Plug(Slot* slot) const;
585 virtual void Plug(Handle<Object> lit) const;
586 virtual void Plug(Heap::RootListIndex) const;
587 virtual void PlugTOS() const;
588 virtual void DropAndPlug(int count, Register reg) const;
589 virtual void EmitLogicalLeft(BinaryOperation* expr,
590 Label* eval_right,
591 Label* done) const;
592 virtual void PrepareTest(Label* materialize_true,
593 Label* materialize_false,
594 Label** if_true,
595 Label** if_false,
596 Label** fall_through) const;
597 };
598
599 class TestContext : public ExpressionContext {
600 public:
601 explicit TestContext(FullCodeGenerator* codegen,
602 Label* true_label,
603 Label* false_label,
604 Label* fall_through)
605 : ExpressionContext(codegen),
606 true_label_(true_label),
607 false_label_(false_label),
608 fall_through_(fall_through) { }
609
Ben Murdochf87a2032010-10-22 12:50:53 +0100610 static const TestContext* cast(const ExpressionContext* context) {
611 ASSERT(context->IsTest());
612 return reinterpret_cast<const TestContext*>(context);
613 }
614
615 Label* true_label() const { return true_label_; }
616 Label* false_label() const { return false_label_; }
617 Label* fall_through() const { return fall_through_; }
618
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100619 virtual void Plug(bool flag) const;
620 virtual void Plug(Register reg) const;
621 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
622 virtual void Plug(Slot* slot) const;
623 virtual void Plug(Handle<Object> lit) const;
624 virtual void Plug(Heap::RootListIndex) const;
625 virtual void PlugTOS() const;
626 virtual void DropAndPlug(int count, Register reg) const;
627 virtual void EmitLogicalLeft(BinaryOperation* expr,
628 Label* eval_right,
629 Label* done) const;
630 virtual void PrepareTest(Label* materialize_true,
631 Label* materialize_false,
632 Label** if_true,
633 Label** if_false,
634 Label** fall_through) const;
635 virtual bool IsTest() const { return true; }
636
637 private:
638 Label* true_label_;
639 Label* false_label_;
640 Label* fall_through_;
641 };
642
643 class EffectContext : public ExpressionContext {
644 public:
645 explicit EffectContext(FullCodeGenerator* codegen)
646 : ExpressionContext(codegen) { }
647
648 virtual void Plug(bool flag) const;
649 virtual void Plug(Register reg) const;
650 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
651 virtual void Plug(Slot* slot) const;
652 virtual void Plug(Handle<Object> lit) const;
653 virtual void Plug(Heap::RootListIndex) const;
654 virtual void PlugTOS() const;
655 virtual void DropAndPlug(int count, Register reg) const;
656 virtual void EmitLogicalLeft(BinaryOperation* expr,
657 Label* eval_right,
658 Label* done) const;
659 virtual void PrepareTest(Label* materialize_true,
660 Label* materialize_false,
661 Label** if_true,
662 Label** if_false,
663 Label** fall_through) const;
664 virtual bool IsEffect() const { return true; }
665 };
666
667 const ExpressionContext* context_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000668
669 friend class NestedStatement;
670
671 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
672};
673
674
675} } // namespace v8::internal
676
677#endif // V8_FULL_CODEGEN_H_