1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_COMPILER_CONTROL_BUILDERS_H_
6 #define V8_COMPILER_CONTROL_BUILDERS_H_
8 #include "src/compiler/ast-graph-builder.h"
9 #include "src/compiler/node.h"
15 // Base class for all control builders. Also provides a common interface for
16 // control builders to handle 'break' statements when they are used to model
17 // breakable statements.
18 class ControlBuilder {
20 explicit ControlBuilder(AstGraphBuilder* builder) : builder_(builder) {}
21 virtual ~ControlBuilder() {}
23 // Interface for break.
24 virtual void Break() { UNREACHABLE(); }
27 typedef AstGraphBuilder Builder;
28 typedef AstGraphBuilder::Environment Environment;
30 Zone* zone() const { return builder_->local_zone(); }
31 Environment* environment() { return builder_->environment(); }
32 void set_environment(Environment* env) { builder_->set_environment(env); }
33 Node* the_hole() const { return builder_->jsgraph()->TheHoleConstant(); }
39 // Tracks control flow for a conditional statement.
40 class IfBuilder FINAL : public ControlBuilder {
42 explicit IfBuilder(AstGraphBuilder* builder)
43 : ControlBuilder(builder),
44 then_environment_(NULL),
45 else_environment_(NULL) {}
47 // Primitive control commands.
48 void If(Node* condition, BranchHint hint = BranchHint::kNone);
54 Environment* then_environment_; // Environment after the 'then' body.
55 Environment* else_environment_; // Environment for the 'else' body.
59 // Tracks control flow for an iteration statement.
60 class LoopBuilder FINAL : public ControlBuilder {
62 explicit LoopBuilder(AstGraphBuilder* builder)
63 : ControlBuilder(builder),
64 loop_environment_(NULL),
65 continue_environment_(NULL),
66 break_environment_(NULL) {}
68 // Primitive control commands.
69 void BeginLoop(BitVector* assigned, bool is_osr = false);
74 // Primitive support for break.
77 // Compound control commands for conditional break.
78 void BreakUnless(Node* condition);
79 void BreakWhen(Node* condition);
82 Environment* loop_environment_; // Environment of the loop header.
83 Environment* continue_environment_; // Environment after the loop body.
84 Environment* break_environment_; // Environment after the loop exits.
88 // Tracks control flow for a switch statement.
89 class SwitchBuilder FINAL : public ControlBuilder {
91 explicit SwitchBuilder(AstGraphBuilder* builder, int case_count)
92 : ControlBuilder(builder),
93 body_environment_(NULL),
94 label_environment_(NULL),
95 break_environment_(NULL),
96 body_environments_(case_count, zone()) {}
98 // Primitive control commands.
100 void BeginLabel(int index, Node* condition);
102 void DefaultAt(int index);
103 void BeginCase(int index);
107 // Primitive support for break.
110 // The number of cases within a switch is statically known.
111 size_t case_count() const { return body_environments_.size(); }
114 Environment* body_environment_; // Environment after last case body.
115 Environment* label_environment_; // Environment for next label condition.
116 Environment* break_environment_; // Environment after the switch exits.
117 ZoneVector<Environment*> body_environments_;
121 // Tracks control flow for a block statement.
122 class BlockBuilder FINAL : public ControlBuilder {
124 explicit BlockBuilder(AstGraphBuilder* builder)
125 : ControlBuilder(builder), break_environment_(NULL) {}
127 // Primitive control commands.
131 // Primitive support for break.
135 Environment* break_environment_; // Environment after the block exits.
139 // Tracks control flow for a try-catch statement.
140 class TryCatchBuilder FINAL : public ControlBuilder {
142 explicit TryCatchBuilder(AstGraphBuilder* builder)
143 : ControlBuilder(builder),
144 catch_environment_(NULL),
145 exit_environment_(NULL),
146 exception_node_(NULL) {}
148 // Primitive control commands.
150 void Throw(Node* exception);
154 // Returns the exception value inside the 'catch' body.
155 Node* GetExceptionNode() const { return exception_node_; }
158 Environment* catch_environment_; // Environment for the 'catch' body.
159 Environment* exit_environment_; // Environment after the statement.
160 Node* exception_node_; // Node for exception in 'catch' body.
164 // Tracks control flow for a try-finally statement.
165 class TryFinallyBuilder FINAL : public ControlBuilder {
167 explicit TryFinallyBuilder(AstGraphBuilder* builder)
168 : ControlBuilder(builder),
169 finally_environment_(NULL),
173 // Primitive control commands.
175 void LeaveTry(Node* token, Node* value);
176 void EndTry(Node* token, Node* value);
179 // Returns the dispatch token value inside the 'finally' body.
180 Node* GetDispatchTokenNode() const { return token_node_; }
182 // Returns the saved result value inside the 'finally' body.
183 Node* GetResultValueNode() const { return value_node_; }
186 Environment* finally_environment_; // Environment for the 'finally' body.
187 Node* token_node_; // Node for token in 'finally' body.
188 Node* value_node_; // Node for value in 'finally' body.
191 } // namespace compiler
192 } // namespace internal
195 #endif // V8_COMPILER_CONTROL_BUILDERS_H_