| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_FLOW_GRAPH_BUILDER_H_ | 5 #ifndef VM_FLOW_GRAPH_BUILDER_H_ |
| 6 #define VM_FLOW_GRAPH_BUILDER_H_ | 6 #define VM_FLOW_GRAPH_BUILDER_H_ |
| 7 | 7 |
| 8 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
| 9 #include "vm/ast.h" | 9 #include "vm/ast.h" |
| 10 #include "vm/growable_array.h" | 10 #include "vm/growable_array.h" |
| 11 #include "vm/intermediate_language.h" | 11 #include "vm/intermediate_language.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 class FlowGraph; | 15 class FlowGraph; |
| 16 class Instruction; | 16 class Instruction; |
| 17 class ParsedFunction; | 17 class ParsedFunction; |
| 18 | 18 |
| 19 // Build a flow graph from a parsed function's AST. | 19 // Build a flow graph from a parsed function's AST. |
| 20 class FlowGraphBuilder: public ValueObject { | 20 class FlowGraphBuilder: public ValueObject { |
| 21 public: | 21 public: |
| 22 explicit FlowGraphBuilder(const ParsedFunction& parsed_function); | 22 explicit FlowGraphBuilder(const ParsedFunction& parsed_function); |
| 23 | 23 |
| 24 enum InliningContext { |
| 25 kNotInlining, |
| 26 kValueContext, |
| 27 kEffectContext, |
| 28 kTestContext |
| 29 }; |
| 30 |
| 24 FlowGraph* BuildGraph(); | 31 FlowGraph* BuildGraph(); |
| 32 FlowGraph* BuildGraphForInlining(InliningContext context); |
| 25 | 33 |
| 26 const ParsedFunction& parsed_function() const { return parsed_function_; } | 34 const ParsedFunction& parsed_function() const { return parsed_function_; } |
| 27 | 35 |
| 28 void Bailout(const char* reason); | 36 void Bailout(const char* reason); |
| 29 | 37 |
| 30 void set_context_level(intptr_t value) { context_level_ = value; } | 38 void set_context_level(intptr_t value) { context_level_ = value; } |
| 31 intptr_t context_level() const { return context_level_; } | 39 intptr_t context_level() const { return context_level_; } |
| 32 | 40 |
| 33 // Each try in this function gets its own try index. | 41 // Each try in this function gets its own try index. |
| 34 intptr_t AllocateTryIndex() { return ++last_used_try_index_; } | 42 intptr_t AllocateTryIndex() { return ++last_used_try_index_; } |
| 35 | 43 |
| 36 // Manage the currently active try index. | 44 // Manage the currently active try index. |
| 37 void set_try_index(intptr_t value) { try_index_ = value; } | 45 void set_try_index(intptr_t value) { try_index_ = value; } |
| 38 intptr_t try_index() const { return try_index_; } | 46 intptr_t try_index() const { return try_index_; } |
| 39 | 47 |
| 40 void AddCatchEntry(TargetEntryInstr* entry); | 48 void AddCatchEntry(TargetEntryInstr* entry); |
| 41 | 49 |
| 42 intptr_t copied_parameter_count() const { | 50 intptr_t copied_parameter_count() const { |
| 43 return copied_parameter_count_; | 51 return copied_parameter_count_; |
| 44 } | 52 } |
| 45 intptr_t non_copied_parameter_count() const { | 53 intptr_t non_copied_parameter_count() const { |
| 46 return non_copied_parameter_count_; | 54 return non_copied_parameter_count_; |
| 47 } | 55 } |
| 48 intptr_t stack_local_count() const { | 56 intptr_t stack_local_count() const { |
| 49 return stack_local_count_; | 57 return stack_local_count_; |
| 50 } | 58 } |
| 51 | 59 |
| 60 bool InInliningContext() const { return inlining_context_ != kNotInlining; } |
| 61 void AddReturnExit(ReturnInstr* return_instr) { |
| 62 if (InInliningContext()) { |
| 63 ASSERT(exits_ != NULL); |
| 64 exits_->Add(return_instr); |
| 65 } |
| 66 } |
| 67 |
| 52 private: | 68 private: |
| 53 intptr_t parameter_count() const { | 69 intptr_t parameter_count() const { |
| 54 return copied_parameter_count_ + non_copied_parameter_count_; | 70 return copied_parameter_count_ + non_copied_parameter_count_; |
| 55 } | 71 } |
| 56 intptr_t variable_count() const { | 72 intptr_t variable_count() const { |
| 57 return parameter_count() + stack_local_count_; | 73 return parameter_count() + stack_local_count_; |
| 58 } | 74 } |
| 59 | 75 |
| 60 const ParsedFunction& parsed_function_; | 76 const ParsedFunction& parsed_function_; |
| 61 | 77 |
| 62 const intptr_t copied_parameter_count_; | 78 const intptr_t copied_parameter_count_; |
| 63 const intptr_t non_copied_parameter_count_; | 79 const intptr_t non_copied_parameter_count_; |
| 64 const intptr_t stack_local_count_; // Does not include any parameters. | 80 const intptr_t stack_local_count_; // Does not include any parameters. |
| 65 | 81 |
| 66 intptr_t context_level_; | 82 intptr_t context_level_; |
| 67 intptr_t last_used_try_index_; | 83 intptr_t last_used_try_index_; |
| 68 intptr_t try_index_; | 84 intptr_t try_index_; |
| 69 GraphEntryInstr* graph_entry_; | 85 GraphEntryInstr* graph_entry_; |
| 86 InliningContext inlining_context_; |
| 87 ZoneGrowableArray<ReturnInstr*>* exits_; |
| 70 | 88 |
| 71 DISALLOW_IMPLICIT_CONSTRUCTORS(FlowGraphBuilder); | 89 DISALLOW_IMPLICIT_CONSTRUCTORS(FlowGraphBuilder); |
| 72 }; | 90 }; |
| 73 | 91 |
| 74 | 92 |
| 75 class TestGraphVisitor; | 93 class TestGraphVisitor; |
| 76 | 94 |
| 77 // Translate an AstNode to a control-flow graph fragment for its effects | 95 // Translate an AstNode to a control-flow graph fragment for its effects |
| 78 // (e.g., a statement or an expression in an effect context). Implements a | 96 // (e.g., a statement or an expression in an effect context). Implements a |
| 79 // function from an AstNode and next temporary index to a graph fragment | 97 // function from an AstNode and next temporary index to a graph fragment |
| (...skipping 17 matching lines...) Expand all Loading... |
| 97 | 115 |
| 98 FlowGraphBuilder* owner() const { return owner_; } | 116 FlowGraphBuilder* owner() const { return owner_; } |
| 99 intptr_t temp_index() const { return temp_index_; } | 117 intptr_t temp_index() const { return temp_index_; } |
| 100 Instruction* entry() const { return entry_; } | 118 Instruction* entry() const { return entry_; } |
| 101 Instruction* exit() const { return exit_; } | 119 Instruction* exit() const { return exit_; } |
| 102 | 120 |
| 103 bool is_empty() const { return entry_ == NULL; } | 121 bool is_empty() const { return entry_ == NULL; } |
| 104 bool is_open() const { return is_empty() || exit_ != NULL; } | 122 bool is_open() const { return is_empty() || exit_ != NULL; } |
| 105 | 123 |
| 106 void Bailout(const char* reason); | 124 void Bailout(const char* reason); |
| 125 void InlineBailout(const char* reason); |
| 107 | 126 |
| 108 // Append a graph fragment to this graph. Assumes this graph is open. | 127 // Append a graph fragment to this graph. Assumes this graph is open. |
| 109 void Append(const EffectGraphVisitor& other_fragment); | 128 void Append(const EffectGraphVisitor& other_fragment); |
| 110 // Append a computation with one use. Assumes this graph is open. | 129 // Append a computation with one use. Assumes this graph is open. |
| 111 UseVal* Bind(Computation* computation); | 130 UseVal* Bind(Computation* computation); |
| 112 // Append a computation with no uses. Assumes this graph is open. | 131 // Append a computation with no uses. Assumes this graph is open. |
| 113 void Do(Computation* computation); | 132 void Do(Computation* computation); |
| 114 // Append a single (non-Definition, non-Entry) instruction. Assumes this | 133 // Append a single (non-Definition, non-Entry) instruction. Assumes this |
| 115 // graph is open. | 134 // graph is open. |
| 116 void AddInstruction(Instruction* instruction); | 135 void AddInstruction(Instruction* instruction); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 127 // Append a 'while loop' test and back edge to this graph, depending on | 146 // Append a 'while loop' test and back edge to this graph, depending on |
| 128 // which parts are reachable. Afterward, the graph exit is the false | 147 // which parts are reachable. Afterward, the graph exit is the false |
| 129 // successor of the loop condition. | 148 // successor of the loop condition. |
| 130 void TieLoop(const TestGraphVisitor& test_fragment, | 149 void TieLoop(const TestGraphVisitor& test_fragment, |
| 131 const EffectGraphVisitor& body_fragment); | 150 const EffectGraphVisitor& body_fragment); |
| 132 | 151 |
| 133 // Wraps a value in a push-argument instruction and adds the result to the | 152 // Wraps a value in a push-argument instruction and adds the result to the |
| 134 // graph. | 153 // graph. |
| 135 PushArgumentInstr* PushArgument(Value* value); | 154 PushArgumentInstr* PushArgument(Value* value); |
| 136 | 155 |
| 156 // This implementation shares state among visitors by using the builder. |
| 157 // The implementation is incorrect if a visitor that hits a return is not |
| 158 // actually added to the graph. |
| 159 void AddReturnExit(ReturnInstr* return_instr) { |
| 160 owner()->AddReturnExit(return_instr); |
| 161 } |
| 162 |
| 137 protected: | 163 protected: |
| 138 Computation* BuildStoreLocal(const LocalVariable& local, Value* value); | 164 Computation* BuildStoreLocal(const LocalVariable& local, Value* value); |
| 139 Computation* BuildLoadLocal(const LocalVariable& local); | 165 Computation* BuildLoadLocal(const LocalVariable& local); |
| 140 | 166 |
| 141 // Helpers for translating parts of the AST. | 167 // Helpers for translating parts of the AST. |
| 142 void TranslateArgumentList(const ArgumentListNode& node, | 168 void TranslateArgumentList(const ArgumentListNode& node, |
| 143 ZoneGrowableArray<Value*>* values); | 169 ZoneGrowableArray<Value*>* values); |
| 144 void BuildPushArguments(const ArgumentListNode& node, | 170 void BuildPushArguments(const ArgumentListNode& node, |
| 145 ZoneGrowableArray<PushArgumentInstr*>* values); | 171 ZoneGrowableArray<PushArgumentInstr*>* values); |
| 146 | 172 |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // Output parameters. | 376 // Output parameters. |
| 351 TargetEntryInstr** true_successor_address_; | 377 TargetEntryInstr** true_successor_address_; |
| 352 TargetEntryInstr** false_successor_address_; | 378 TargetEntryInstr** false_successor_address_; |
| 353 | 379 |
| 354 intptr_t condition_token_pos_; | 380 intptr_t condition_token_pos_; |
| 355 }; | 381 }; |
| 356 | 382 |
| 357 } // namespace dart | 383 } // namespace dart |
| 358 | 384 |
| 359 #endif // VM_FLOW_GRAPH_BUILDER_H_ | 385 #endif // VM_FLOW_GRAPH_BUILDER_H_ |
| OLD | NEW |