blob: e27662e0e3aa706900ab1d4960b3ccb23444a324 [file] [log] [blame]
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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
lrn@chromium.org1c092762011-05-09 09:42:16 +000033#include "allocation.h"
danno@chromium.org59400602013-08-13 17:09:37 +000034#include "assert-scope.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000035#include "ast.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "code-stubs.h"
37#include "codegen.h"
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000038#include "compiler.h"
jkummerow@chromium.org59297c72013-01-09 16:32:23 +000039#include "data-flow.h"
danno@chromium.org59400602013-08-13 17:09:37 +000040#include "globals.h"
41#include "objects.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000042
43namespace v8 {
44namespace internal {
45
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000046// Forward declarations.
47class JumpPatchSite;
48
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000049// AST node visitor which can tell whether a given statement will be breakable
50// when the code is compiled by the full compiler in the debugger. This means
51// that there will be an IC (load/store/call) in the code generated for the
52// debugger to piggybag on.
53class BreakableStatementChecker: public AstVisitor {
54 public:
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +000055 explicit BreakableStatementChecker(Isolate* isolate) : is_breakable_(false) {
56 InitializeAstVisitor(isolate);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000057 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000058
59 void Check(Statement* stmt);
60 void Check(Expression* stmt);
61
62 bool is_breakable() { return is_breakable_; }
63
64 private:
65 // AST node visit functions.
66#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
67 AST_NODE_LIST(DECLARE_VISIT)
68#undef DECLARE_VISIT
69
70 bool is_breakable_;
71
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000072 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000073 DISALLOW_COPY_AND_ASSIGN(BreakableStatementChecker);
74};
75
76
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000077// -----------------------------------------------------------------------------
78// Full code generator.
79
80class FullCodeGenerator: public AstVisitor {
81 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +000082 enum State {
83 NO_REGISTERS,
84 TOS_REG
85 };
86
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000087 FullCodeGenerator(MacroAssembler* masm, CompilationInfo* info)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000088 : masm_(masm),
yangguo@chromium.org56454712012-02-16 15:33:53 +000089 info_(info),
90 scope_(info->scope()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000091 nesting_stack_(NULL),
92 loop_depth_(0),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +000093 globals_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +000094 context_(NULL),
yangguo@chromium.org56454712012-02-16 15:33:53 +000095 bailout_entries_(info->HasDeoptimizationSupport()
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000096 ? info->function()->ast_node_count() : 0,
97 info->zone()),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000098 back_edges_(2, info->zone()),
yangguo@chromium.org56454712012-02-16 15:33:53 +000099 type_feedback_cells_(info->HasDeoptimizationSupport()
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000100 ? info->function()->ast_node_count() : 0,
101 info->zone()),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000102 ic_total_count_(0),
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000103 zone_(info->zone()) {
104 Initialize();
105 }
106
107 void Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000108
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000109 static bool MakeCode(CompilationInfo* info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000110
yangguo@chromium.org56454712012-02-16 15:33:53 +0000111 // Encode state and pc-offset as a BitField<type, start, size>.
112 // Only use 30 bits because we encode the result as a smi.
113 class StateField : public BitField<State, 0, 1> { };
114 class PcField : public BitField<unsigned, 1, 30-1> { };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000115
116 static const char* State2String(State state) {
117 switch (state) {
118 case NO_REGISTERS: return "NO_REGISTERS";
119 case TOS_REG: return "TOS_REG";
120 }
121 UNREACHABLE();
122 return NULL;
123 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000124
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000125 Zone* zone() const { return zone_; }
126
danno@chromium.org129d3982012-07-25 15:01:47 +0000127 static const int kMaxBackEdgeWeight = 127;
128
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000129 // Platform-specific code size multiplier.
danno@chromium.org129d3982012-07-25 15:01:47 +0000130#if V8_TARGET_ARCH_IA32
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000131 static const int kCodeSizeMultiplier = 100;
danno@chromium.org129d3982012-07-25 15:01:47 +0000132#elif V8_TARGET_ARCH_X64
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000133 static const int kCodeSizeMultiplier = 162;
danno@chromium.org129d3982012-07-25 15:01:47 +0000134#elif V8_TARGET_ARCH_ARM
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000135 static const int kCodeSizeMultiplier = 142;
danno@chromium.org129d3982012-07-25 15:01:47 +0000136#elif V8_TARGET_ARCH_MIPS
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000137 static const int kCodeSizeMultiplier = 142;
danno@chromium.org129d3982012-07-25 15:01:47 +0000138#else
139#error Unsupported target architecture.
140#endif
141
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000142 private:
143 class Breakable;
144 class Iteration;
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000145
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000146 class TestContext;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000147
148 class NestedStatement BASE_EMBEDDED {
149 public:
150 explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
151 // Link into codegen's nesting stack.
152 previous_ = codegen->nesting_stack_;
153 codegen->nesting_stack_ = this;
154 }
155 virtual ~NestedStatement() {
156 // Unlink from codegen's nesting stack.
157 ASSERT_EQ(this, codegen_->nesting_stack_);
158 codegen_->nesting_stack_ = previous_;
159 }
160
161 virtual Breakable* AsBreakable() { return NULL; }
162 virtual Iteration* AsIteration() { return NULL; }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000163
164 virtual bool IsContinueTarget(Statement* target) { return false; }
165 virtual bool IsBreakTarget(Statement* target) { return false; }
166
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000167 // Notify the statement that we are exiting it via break, continue, or
168 // return and give it a chance to generate cleanup code. Return the
169 // next outer statement in the nesting stack. We accumulate in
170 // *stack_depth the amount to drop the stack and in *context_length the
171 // number of context chain links to unwind as we traverse the nesting
172 // stack from an exit to its target.
173 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
174 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000175 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000176
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +0000177 protected:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000178 MacroAssembler* masm() { return codegen_->masm(); }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000179
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000180 FullCodeGenerator* codegen_;
181 NestedStatement* previous_;
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +0000182
183 private:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000184 DISALLOW_COPY_AND_ASSIGN(NestedStatement);
185 };
186
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000187 // A breakable statement such as a block.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000188 class Breakable : public NestedStatement {
189 public:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000190 Breakable(FullCodeGenerator* codegen, BreakableStatement* statement)
191 : NestedStatement(codegen), statement_(statement) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000192 }
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000193 virtual ~Breakable() {}
194
195 virtual Breakable* AsBreakable() { return this; }
196 virtual bool IsBreakTarget(Statement* target) {
197 return statement() == target;
198 }
199
200 BreakableStatement* statement() { return statement_; }
201 Label* break_label() { return &break_label_; }
202
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000203 private:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000204 BreakableStatement* statement_;
205 Label break_label_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000206 };
207
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000208 // An iteration statement such as a while, for, or do loop.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000209 class Iteration : public Breakable {
210 public:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000211 Iteration(FullCodeGenerator* codegen, IterationStatement* statement)
212 : Breakable(codegen, statement) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000213 }
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000214 virtual ~Iteration() {}
215
216 virtual Iteration* AsIteration() { return this; }
217 virtual bool IsContinueTarget(Statement* target) {
218 return statement() == target;
219 }
220
221 Label* continue_label() { return &continue_label_; }
222
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000223 private:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000224 Label continue_label_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000225 };
226
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000227 // A nested block statement.
228 class NestedBlock : public Breakable {
229 public:
230 NestedBlock(FullCodeGenerator* codegen, Block* block)
231 : Breakable(codegen, block) {
232 }
233 virtual ~NestedBlock() {}
234
235 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000236 if (statement()->AsBlock()->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000237 ++(*context_length);
238 }
239 return previous_;
240 };
241 };
242
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000243 // The try block of a try/catch statement.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 class TryCatch : public NestedStatement {
245 public:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000246 explicit TryCatch(FullCodeGenerator* codegen) : NestedStatement(codegen) {
247 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000248 virtual ~TryCatch() {}
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000249
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000250 virtual NestedStatement* Exit(int* stack_depth, int* context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000251 };
252
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000253 // The try block of a try/finally statement.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000254 class TryFinally : public NestedStatement {
255 public:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000256 TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
257 : NestedStatement(codegen), finally_entry_(finally_entry) {
258 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000259 virtual ~TryFinally() {}
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000260
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000261 virtual NestedStatement* Exit(int* stack_depth, int* context_length);
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000262
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000263 private:
264 Label* finally_entry_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000265 };
266
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000267 // The finally block of a try/finally statement.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000268 class Finally : public NestedStatement {
269 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000270 static const int kElementCount = 5;
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000271
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000272 explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { }
273 virtual ~Finally() {}
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000274
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000275 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000276 *stack_depth += kElementCount;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000277 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000278 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000279 };
280
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000281 // The body of a for/in loop.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000282 class ForIn : public Iteration {
283 public:
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000284 static const int kElementCount = 5;
285
286 ForIn(FullCodeGenerator* codegen, ForInStatement* statement)
287 : Iteration(codegen, statement) {
288 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000289 virtual ~ForIn() {}
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000290
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000291 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000292 *stack_depth += kElementCount;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000293 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000294 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000295 };
296
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000297
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000298 // The body of a with or catch.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000299 class WithOrCatch : public NestedStatement {
300 public:
301 explicit WithOrCatch(FullCodeGenerator* codegen)
302 : NestedStatement(codegen) {
303 }
304 virtual ~WithOrCatch() {}
305
306 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
307 ++(*context_length);
308 return previous_;
309 }
310 };
311
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000312 // Type of a member function that generates inline code for a native function.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000313 typedef void (FullCodeGenerator::*InlineFunctionGenerator)(CallRuntime* expr);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000314
315 static const InlineFunctionGenerator kInlineFunctionGenerators[];
316
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000317 // A platform-specific utility to overwrite the accumulator register
318 // with a GC-safe value.
319 void ClearAccumulator();
320
ricow@chromium.org65fae842010-08-25 15:26:24 +0000321 // Determine whether or not to inline the smi case for the given
322 // operation.
323 bool ShouldInlineSmiCase(Token::Value op);
324
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000325 // Helper function to convert a pure value into a test context. The value
326 // is expected on the stack or the accumulator, depending on the platform.
327 // See the platform-specific implementation for details.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000328 void DoTest(Expression* condition,
329 Label* if_true,
330 Label* if_false,
331 Label* fall_through);
332 void DoTest(const TestContext* context);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000333
334 // Helper function to split control flow and avoid a branch to the
335 // fall-through label if it is set up.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000336#if V8_TARGET_ARCH_MIPS
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000337 void Split(Condition cc,
338 Register lhs,
339 const Operand& rhs,
340 Label* if_true,
341 Label* if_false,
342 Label* fall_through);
343#else // All non-mips arch.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000344 void Split(Condition cc,
345 Label* if_true,
346 Label* if_false,
347 Label* fall_through);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000348#endif // V8_TARGET_ARCH_MIPS
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000349
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000350 // Load the value of a known (PARAMETER, LOCAL, or CONTEXT) variable into
351 // a register. Emits a context chain walk if if necessary (so does
352 // SetVar) so avoid calling both on the same variable.
353 void GetVar(Register destination, Variable* var);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000354
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000355 // Assign to a known (PARAMETER, LOCAL, or CONTEXT) variable. If it's in
356 // the context, the write barrier will be emitted and source, scratch0,
357 // scratch1 will be clobbered. Emits a context chain walk if if necessary
358 // (so does GetVar) so avoid calling both on the same variable.
359 void SetVar(Variable* var,
360 Register source,
361 Register scratch0,
362 Register scratch1);
363
364 // An operand used to read/write a stack-allocated (PARAMETER or LOCAL)
365 // variable. Writing does not need the write barrier.
366 MemOperand StackOperand(Variable* var);
367
368 // An operand used to read/write a known (PARAMETER, LOCAL, or CONTEXT)
369 // variable. May emit code to traverse the context chain, loading the
370 // found context into the scratch register. Writing to this operand will
371 // need the write barrier if location is CONTEXT.
372 MemOperand VarOperand(Variable* var, Register scratch);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000373
374 void VisitForEffect(Expression* expr) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000375 EffectContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000376 Visit(expr);
377 PrepareForBailout(expr, NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000378 }
379
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000380 void VisitForAccumulatorValue(Expression* expr) {
381 AccumulatorValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000382 Visit(expr);
383 PrepareForBailout(expr, TOS_REG);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000384 }
385
386 void VisitForStackValue(Expression* expr) {
387 StackValueContext context(this);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000388 Visit(expr);
389 PrepareForBailout(expr, NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000390 }
391
ricow@chromium.org65fae842010-08-25 15:26:24 +0000392 void VisitForControl(Expression* expr,
393 Label* if_true,
394 Label* if_false,
395 Label* fall_through) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000396 TestContext context(this, expr, if_true, if_false, fall_through);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000397 Visit(expr);
398 // For test contexts, we prepare for bailout before branching, not at
399 // the end of the entire expression. This happens as part of visiting
400 // the expression.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000401 }
402
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000403 void VisitInDuplicateContext(Expression* expr);
404
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000405 void VisitDeclarations(ZoneList<Declaration*>* declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000406 void DeclareModules(Handle<FixedArray> descriptions);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000407 void DeclareGlobals(Handle<FixedArray> pairs);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000408 int DeclareGlobalsFlags();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000409
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000410 // Generate code to allocate all (including nested) modules and contexts.
411 // Because of recursive linking and the presence of module alias declarations,
412 // this has to be a separate pass _before_ populating or executing any module.
413 void AllocateModules(ZoneList<Declaration*>* declarations);
414
danno@chromium.org41728482013-06-12 22:31:22 +0000415 // Generate code to create an iterator result object. The "value" property is
416 // set to a value popped from the stack, and "done" is set according to the
417 // argument. The result object is left in the result register.
418 void EmitCreateIteratorResult(bool done);
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000419
ricow@chromium.org65fae842010-08-25 15:26:24 +0000420 // Try to perform a comparison as a fast inlined literal compare if
421 // the operands allow it. Returns true if the compare operations
422 // has been matched and all code generated; false otherwise.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000423 bool TryLiteralCompare(CompareOperation* compare);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000424
ager@chromium.org04921a82011-06-27 13:21:41 +0000425 // Platform-specific code for comparing the type of a value with
426 // a given literal string.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000427 void EmitLiteralCompareTypeof(Expression* expr,
428 Expression* sub_expr,
429 Handle<String> check);
ager@chromium.org04921a82011-06-27 13:21:41 +0000430
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000431 // Platform-specific code for equality comparison with a nil-like value.
432 void EmitLiteralCompareNil(CompareOperation* expr,
433 Expression* sub_expr,
434 NilValue nil);
ager@chromium.org04921a82011-06-27 13:21:41 +0000435
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000436 // Bailout support.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000437 void PrepareForBailout(Expression* node, State state);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000438 void PrepareForBailoutForId(BailoutId id, State state);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000439
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000440 // Cache cell support. This associates AST ids with global property cells
441 // that will be cleared during GC and collected by the type-feedback oracle.
danno@chromium.org41728482013-06-12 22:31:22 +0000442 void RecordTypeFeedbackCell(TypeFeedbackId id, Handle<Cell> cell);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000443
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000444 // Record a call's return site offset, used to rebuild the frame if the
445 // called function was inlined at the site.
446 void RecordJSReturnSite(Call* call);
447
448 // Prepare for bailout before a test (or compare) and branch. If
449 // should_normalize, then the following comparison will not handle the
450 // canonical JS true value so we will insert a (dead) test against true at
451 // the actual bailout target from the optimized code. If not
452 // should_normalize, the true and false labels are ignored.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000453 void PrepareForBailoutBeforeSplit(Expression* expr,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000454 bool should_normalize,
455 Label* if_true,
456 Label* if_false);
457
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000458 // If enabled, emit debug code for checking that the current context is
459 // neither a with nor a catch context.
460 void EmitDebugCheckDeclarationContext(Variable* variable);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000461
yangguo@chromium.org56454712012-02-16 15:33:53 +0000462 // This is meant to be called at loop back edges, |back_edge_target| is
463 // the jump target of the back edge and is used to approximate the amount
464 // of code inside the loop.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000465 void EmitBackEdgeBookkeeping(IterationStatement* stmt,
466 Label* back_edge_target);
467 // Record the OSR AST id corresponding to a back edge in the code.
468 void RecordBackEdge(BailoutId osr_ast_id);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000469 // Emit a table of back edge ids, pcs and loop depths into the code stream.
470 // Return the offset of the start of the table.
471 unsigned EmitBackEdgeTable();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000472
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000473 void EmitProfilingCounterDecrement(int delta);
474 void EmitProfilingCounterReset();
475
danno@chromium.org41728482013-06-12 22:31:22 +0000476 // Emit code to pop values from the stack associated with nested statements
477 // like try/catch, try/finally, etc, running the finallies and unwinding the
478 // handlers as needed.
479 void EmitUnwindBeforeReturn();
480
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000481 // Platform-specific return sequence
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000482 void EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000483
484 // Platform-specific code sequences for calls
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000485 void EmitCallWithStub(Call* expr, CallFunctionFlags flags);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000486 void EmitCallWithIC(Call* expr, Handle<Object> name, RelocInfo::Mode mode);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000487 void EmitKeyedCallWithIC(Call* expr, Expression* key);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000488
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000489 // Platform-specific code for inline runtime calls.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000490 InlineFunctionGenerator FindInlineFunctionGenerator(Runtime::FunctionId id);
491
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000492 void EmitInlineRuntimeCall(CallRuntime* expr);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000493
494#define EMIT_INLINE_RUNTIME_CALL(name, x, y) \
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000495 void Emit##name(CallRuntime* expr);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000496 INLINE_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000497 INLINE_RUNTIME_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
498#undef EMIT_INLINE_RUNTIME_CALL
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000499
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000500 void EmitSeqStringSetCharCheck(Register string,
501 Register index,
502 Register value,
503 uint32_t encoding_mask);
504
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000505 // Platform-specific code for resuming generators.
506 void EmitGeneratorResume(Expression *generator,
507 Expression *value,
508 JSGeneratorObject::ResumeMode resume_mode);
509
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000510 // Platform-specific code for loading variables.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000511 void EmitLoadGlobalCheckExtensions(Variable* var,
512 TypeofState typeof_state,
513 Label* slow);
514 MemOperand ContextSlotOperandCheckExtensions(Variable* var, Label* slow);
515 void EmitDynamicLookupFastCase(Variable* var,
516 TypeofState typeof_state,
517 Label* slow,
518 Label* done);
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000519 void EmitVariableLoad(VariableProxy* proxy);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000520
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000521 void EmitAccessor(Expression* expression);
522
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000523 // Expects the arguments and the function already pushed.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000524 void EmitResolvePossiblyDirectEval(int arg_count);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000525
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000526 // Platform-specific support for allocating a new closure based on
527 // the given function info.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000528 void EmitNewClosure(Handle<SharedFunctionInfo> info, bool pretenure);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000529
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 // Platform-specific support for compiling assignments.
531
532 // Load a value from a named property.
533 // The receiver is left on the stack by the IC.
534 void EmitNamedPropertyLoad(Property* expr);
535
536 // Load a value from a keyed property.
537 // The receiver and the key is left on the stack by the IC.
538 void EmitKeyedPropertyLoad(Property* expr);
539
540 // Apply the compound assignment operator. Expects the left operand on top
541 // of the stack and the right one in the accumulator.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000542 void EmitBinaryOp(BinaryOperation* expr,
543 Token::Value op,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000544 OverwriteMode mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000545
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000546 // Helper functions for generating inlined smi code for certain
547 // binary operations.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000548 void EmitInlineSmiBinaryOp(BinaryOperation* expr,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000549 Token::Value op,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000550 OverwriteMode mode,
551 Expression* left,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000552 Expression* right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000553
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000554 // Assign to the given expression as if via '='. The right-hand-side value
555 // is expected in the accumulator.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000556 void EmitAssignment(Expression* expr);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000557
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000558 // Complete a variable assignment. The right-hand-side value is expected
559 // in the accumulator.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000560 void EmitVariableAssignment(Variable* var,
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000561 Token::Value op);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000562
563 // Complete a named property assignment. The receiver is expected on top
564 // of the stack and the right-hand-side value in the accumulator.
565 void EmitNamedPropertyAssignment(Assignment* expr);
566
567 // Complete a keyed property assignment. The receiver and key are
568 // expected on top of the stack and the right-hand-side value in the
569 // accumulator.
570 void EmitKeyedPropertyAssignment(Assignment* expr);
571
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000572 void CallIC(Handle<Code> code,
573 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000574 TypeFeedbackId id = TypeFeedbackId::None());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000575
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000576 void SetFunctionPosition(FunctionLiteral* fun);
577 void SetReturnPosition(FunctionLiteral* fun);
578 void SetStatementPosition(Statement* stmt);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000579 void SetExpressionPosition(Expression* expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000580 void SetStatementPosition(int pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000581 void SetSourcePosition(int pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000582
583 // Non-local control flow support.
584 void EnterFinallyBlock();
585 void ExitFinallyBlock();
586
587 // Loop nesting counter.
588 int loop_depth() { return loop_depth_; }
589 void increment_loop_depth() { loop_depth_++; }
590 void decrement_loop_depth() {
591 ASSERT(loop_depth_ > 0);
592 loop_depth_--;
593 }
594
595 MacroAssembler* masm() { return masm_; }
ager@chromium.org5c838252010-02-19 08:53:10 +0000596
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000597 class ExpressionContext;
598 const ExpressionContext* context() { return context_; }
599 void set_new_context(const ExpressionContext* context) { context_ = context; }
600
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 Handle<Script> script() { return info_->script(); }
602 bool is_eval() { return info_->is_eval(); }
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000603 bool is_native() { return info_->is_native(); }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000604 bool is_classic_mode() { return language_mode() == CLASSIC_MODE; }
605 LanguageMode language_mode() { return function()->language_mode(); }
ager@chromium.org5c838252010-02-19 08:53:10 +0000606 FunctionLiteral* function() { return info_->function(); }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000607 Scope* scope() { return scope_; }
ager@chromium.org5c838252010-02-19 08:53:10 +0000608
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000609 static Register result_register();
610 static Register context_register();
611
612 // Set fields in the stack frame. Offsets are the frame pointer relative
613 // offsets defined in, e.g., StandardFrameConstants.
614 void StoreToFrameField(int frame_offset, Register value);
615
616 // Load a value from the current context. Indices are defined as an enum
617 // in v8::internal::Context.
618 void LoadContextField(Register dst, int context_index);
619
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000620 // Push the function argument for the runtime functions PushWithContext
621 // and PushCatchContext.
622 void PushFunctionArgumentForContextAllocation();
623
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000624 // AST node visit functions.
625#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
626 AST_NODE_LIST(DECLARE_VISIT)
627#undef DECLARE_VISIT
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000628
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000629 void VisitComma(BinaryOperation* expr);
630 void VisitLogicalExpression(BinaryOperation* expr);
631 void VisitArithmeticExpression(BinaryOperation* expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000632
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000633 void VisitForTypeofValue(Expression* expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000634
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000635 void Generate();
636 void PopulateDeoptimizationData(Handle<Code> code);
637 void PopulateTypeFeedbackInfo(Handle<Code> code);
638 void PopulateTypeFeedbackCells(Handle<Code> code);
639
640 Handle<FixedArray> handler_table() { return handler_table_; }
641
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000642 struct BailoutEntry {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000643 BailoutId id;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000644 unsigned pc_and_state;
645 };
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000646
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000647 struct BackEdgeEntry {
648 BailoutId id;
649 unsigned pc;
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000650 uint32_t loop_depth;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000651 };
652
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000653 struct TypeFeedbackCellEntry {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000654 TypeFeedbackId ast_id;
danno@chromium.org41728482013-06-12 22:31:22 +0000655 Handle<Cell> cell;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000656 };
657
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000658
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000659 class ExpressionContext BASE_EMBEDDED {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000660 public:
661 explicit ExpressionContext(FullCodeGenerator* codegen)
662 : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
663 codegen->set_new_context(this);
664 }
665
666 virtual ~ExpressionContext() {
667 codegen_->set_new_context(old_);
668 }
669
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000670 Isolate* isolate() const { return codegen_->isolate(); }
671
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000672 // Convert constant control flow (true or false) to the result expected for
673 // this expression context.
674 virtual void Plug(bool flag) const = 0;
675
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000676 // Emit code to convert a pure value (in a register, known variable
677 // location, as a literal, or on top of the stack) into the result
678 // expected according to this expression context.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000679 virtual void Plug(Register reg) const = 0;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000680 virtual void Plug(Variable* var) const = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000681 virtual void Plug(Handle<Object> lit) const = 0;
682 virtual void Plug(Heap::RootListIndex index) const = 0;
683 virtual void PlugTOS() const = 0;
684
685 // Emit code to convert pure control flow to a pair of unbound labels into
686 // the result expected according to this expression context. The
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000687 // implementation will bind both labels unless it's a TestContext, which
688 // won't bind them at this point.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000689 virtual void Plug(Label* materialize_true,
690 Label* materialize_false) const = 0;
691
692 // Emit code to discard count elements from the top of stack, then convert
693 // a pure value into the result expected according to this expression
694 // context.
695 virtual void DropAndPlug(int count, Register reg) const = 0;
696
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000697 // Set up branch labels for a test expression. The three Label** parameters
698 // are output parameters.
699 virtual void PrepareTest(Label* materialize_true,
700 Label* materialize_false,
701 Label** if_true,
702 Label** if_false,
703 Label** fall_through) const = 0;
704
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000705 // Returns true if we are evaluating only for side effects (i.e. if the
706 // result will be discarded).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000707 virtual bool IsEffect() const { return false; }
708
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000709 // Returns true if we are evaluating for the value (in accu/on stack).
710 virtual bool IsAccumulatorValue() const { return false; }
711 virtual bool IsStackValue() const { return false; }
712
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000713 // Returns true if we are branching on the value rather than materializing
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000714 // it. Only used for asserts.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000715 virtual bool IsTest() const { return false; }
716
717 protected:
718 FullCodeGenerator* codegen() const { return codegen_; }
719 MacroAssembler* masm() const { return masm_; }
720 MacroAssembler* masm_;
721
722 private:
723 const ExpressionContext* old_;
724 FullCodeGenerator* codegen_;
725 };
726
727 class AccumulatorValueContext : public ExpressionContext {
728 public:
729 explicit AccumulatorValueContext(FullCodeGenerator* codegen)
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000730 : ExpressionContext(codegen) { }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000731
732 virtual void Plug(bool flag) const;
733 virtual void Plug(Register reg) const;
734 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000735 virtual void Plug(Variable* var) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000736 virtual void Plug(Handle<Object> lit) const;
737 virtual void Plug(Heap::RootListIndex) const;
738 virtual void PlugTOS() const;
739 virtual void DropAndPlug(int count, Register reg) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000740 virtual void PrepareTest(Label* materialize_true,
741 Label* materialize_false,
742 Label** if_true,
743 Label** if_false,
744 Label** fall_through) const;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000745 virtual bool IsAccumulatorValue() const { return true; }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000746 };
747
748 class StackValueContext : public ExpressionContext {
749 public:
750 explicit StackValueContext(FullCodeGenerator* codegen)
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000751 : ExpressionContext(codegen) { }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000752
753 virtual void Plug(bool flag) const;
754 virtual void Plug(Register reg) const;
755 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000756 virtual void Plug(Variable* var) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000757 virtual void Plug(Handle<Object> lit) const;
758 virtual void Plug(Heap::RootListIndex) const;
759 virtual void PlugTOS() const;
760 virtual void DropAndPlug(int count, Register reg) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000761 virtual void PrepareTest(Label* materialize_true,
762 Label* materialize_false,
763 Label** if_true,
764 Label** if_false,
765 Label** fall_through) const;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000766 virtual bool IsStackValue() const { return true; }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000767 };
768
769 class TestContext : public ExpressionContext {
770 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000771 TestContext(FullCodeGenerator* codegen,
772 Expression* condition,
773 Label* true_label,
774 Label* false_label,
775 Label* fall_through)
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000776 : ExpressionContext(codegen),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000777 condition_(condition),
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000778 true_label_(true_label),
779 false_label_(false_label),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000780 fall_through_(fall_through) { }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000781
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000782 static const TestContext* cast(const ExpressionContext* context) {
783 ASSERT(context->IsTest());
784 return reinterpret_cast<const TestContext*>(context);
785 }
786
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000787 Expression* condition() const { return condition_; }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000788 Label* true_label() const { return true_label_; }
789 Label* false_label() const { return false_label_; }
790 Label* fall_through() const { return fall_through_; }
791
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000792 virtual void Plug(bool flag) const;
793 virtual void Plug(Register reg) const;
794 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000795 virtual void Plug(Variable* var) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000796 virtual void Plug(Handle<Object> lit) const;
797 virtual void Plug(Heap::RootListIndex) const;
798 virtual void PlugTOS() const;
799 virtual void DropAndPlug(int count, Register reg) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000800 virtual void PrepareTest(Label* materialize_true,
801 Label* materialize_false,
802 Label** if_true,
803 Label** if_false,
804 Label** fall_through) const;
805 virtual bool IsTest() const { return true; }
806
807 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000808 Expression* condition_;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000809 Label* true_label_;
810 Label* false_label_;
811 Label* fall_through_;
812 };
813
814 class EffectContext : public ExpressionContext {
815 public:
816 explicit EffectContext(FullCodeGenerator* codegen)
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000817 : ExpressionContext(codegen) { }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000818
819 virtual void Plug(bool flag) const;
820 virtual void Plug(Register reg) const;
821 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000822 virtual void Plug(Variable* var) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000823 virtual void Plug(Handle<Object> lit) const;
824 virtual void Plug(Heap::RootListIndex) const;
825 virtual void PlugTOS() const;
826 virtual void DropAndPlug(int count, Register reg) const;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000827 virtual void PrepareTest(Label* materialize_true,
828 Label* materialize_false,
829 Label** if_true,
830 Label** if_false,
831 Label** fall_through) const;
832 virtual bool IsEffect() const { return true; }
833 };
834
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000835 MacroAssembler* masm_;
836 CompilationInfo* info_;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000837 Scope* scope_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000838 Label return_label_;
839 NestedStatement* nesting_stack_;
840 int loop_depth_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000841 ZoneList<Handle<Object> >* globals_;
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000842 Handle<FixedArray> modules_;
843 int module_index_;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000844 const ExpressionContext* context_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000845 ZoneList<BailoutEntry> bailout_entries_;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000846 GrowableBitVector prepared_bailout_ids_;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000847 ZoneList<BackEdgeEntry> back_edges_;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000848 ZoneList<TypeFeedbackCellEntry> type_feedback_cells_;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000849 int ic_total_count_;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000850 Handle<FixedArray> handler_table_;
danno@chromium.org41728482013-06-12 22:31:22 +0000851 Handle<Cell> profiling_counter_;
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000852 bool generate_debug_code_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000853 Zone* zone_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000854
855 friend class NestedStatement;
856
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000857 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000858 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
859};
860
861
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000862// A map from property names to getter/setter pairs allocated in the zone.
863class AccessorTable: public TemplateHashMap<Literal,
864 ObjectLiteral::Accessors,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000865 ZoneAllocationPolicy> {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000866 public:
867 explicit AccessorTable(Zone* zone) :
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000868 TemplateHashMap<Literal, ObjectLiteral::Accessors,
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000869 ZoneAllocationPolicy>(Literal::Match,
870 ZoneAllocationPolicy(zone)),
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000871 zone_(zone) { }
872
873 Iterator lookup(Literal* literal) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000874 Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000875 if (it->second == NULL) it->second = new(zone_) ObjectLiteral::Accessors();
876 return it;
877 }
878
879 private:
880 Zone* zone_;
881};
882
883
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000884class BackEdgeTable {
885 public:
886 BackEdgeTable(Code* code, DisallowHeapAllocation* required) {
887 ASSERT(code->kind() == Code::FUNCTION);
888 instruction_start_ = code->instruction_start();
889 Address table_address = instruction_start_ + code->back_edge_table_offset();
890 length_ = Memory::uint32_at(table_address);
891 start_ = table_address + kTableLengthSize;
892 }
893
894 uint32_t length() { return length_; }
895
896 BailoutId ast_id(uint32_t index) {
897 return BailoutId(static_cast<int>(
898 Memory::uint32_at(entry_at(index) + kAstIdOffset)));
899 }
900
901 uint32_t loop_depth(uint32_t index) {
902 return Memory::uint32_at(entry_at(index) + kLoopDepthOffset);
903 }
904
905 uint32_t pc_offset(uint32_t index) {
906 return Memory::uint32_at(entry_at(index) + kPcOffsetOffset);
907 }
908
909 Address pc(uint32_t index) {
910 return instruction_start_ + pc_offset(index);
911 }
912
913 enum BackEdgeState {
914 INTERRUPT,
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000915 ON_STACK_REPLACEMENT,
916 OSR_AFTER_STACK_CHECK
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000917 };
918
919 // Patch all interrupts with allowed loop depth in the unoptimized code to
920 // unconditionally call replacement_code.
921 static void Patch(Isolate* isolate,
922 Code* unoptimized_code);
923
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000924 // Patch the back edge to the target state, provided the correct callee.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000925 static void PatchAt(Code* unoptimized_code,
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000926 Address pc,
927 BackEdgeState target_state,
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000928 Code* replacement_code);
929
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000930 // Change all patched back edges back to normal interrupts.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000931 static void Revert(Isolate* isolate,
932 Code* unoptimized_code);
933
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000934 // Change a back edge patched for on-stack replacement to perform a
935 // stack check first.
936 static void AddStackCheck(CompilationInfo* info);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000937
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000938 // Remove the stack check, if available, and replace by on-stack replacement.
939 static void RemoveStackCheck(CompilationInfo* info);
940
941 // Return the current patch state of the back edge.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000942 static BackEdgeState GetBackEdgeState(Isolate* isolate,
943 Code* unoptimized_code,
944 Address pc_after);
945
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +0000946#ifdef DEBUG
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000947 // Verify that all back edges of a certain loop depth are patched.
948 static bool Verify(Isolate* isolate,
949 Code* unoptimized_code,
950 int loop_nesting_level);
951#endif // DEBUG
952
953 private:
954 Address entry_at(uint32_t index) {
955 ASSERT(index < length_);
956 return start_ + index * kEntrySize;
957 }
958
959 static const int kTableLengthSize = kIntSize;
960 static const int kAstIdOffset = 0 * kIntSize;
961 static const int kPcOffsetOffset = 1 * kIntSize;
962 static const int kLoopDepthOffset = 2 * kIntSize;
963 static const int kEntrySize = 3 * kIntSize;
964
965 Address start_;
966 Address instruction_start_;
967 uint32_t length_;
968};
969
970
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000971} } // namespace v8::internal
972
973#endif // V8_FULL_CODEGEN_H_