| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ | 5 #ifndef V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ |
| 6 #define V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ | 6 #define V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ |
| 7 | 7 |
| 8 #include "src/interpreter/bytecode-array-builder.h" | 8 #include "src/interpreter/bytecode-array-builder.h" |
| 9 | 9 |
| 10 #include "src/interpreter/bytecode-label.h" | 10 #include "src/interpreter/bytecode-label.h" |
| 11 #include "src/zone/zone-containers.h" | 11 #include "src/zone/zone-containers.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 namespace interpreter { | 15 namespace interpreter { |
| 16 | 16 |
| 17 class ControlFlowBuilder BASE_EMBEDDED { | 17 class V8_EXPORT_PRIVATE ControlFlowBuilder BASE_EMBEDDED { |
| 18 public: | 18 public: |
| 19 explicit ControlFlowBuilder(BytecodeArrayBuilder* builder) | 19 explicit ControlFlowBuilder(BytecodeArrayBuilder* builder) |
| 20 : builder_(builder) {} | 20 : builder_(builder) {} |
| 21 virtual ~ControlFlowBuilder() {} | 21 virtual ~ControlFlowBuilder() {} |
| 22 | 22 |
| 23 protected: | 23 protected: |
| 24 BytecodeArrayBuilder* builder() const { return builder_; } | 24 BytecodeArrayBuilder* builder() const { return builder_; } |
| 25 | 25 |
| 26 private: | 26 private: |
| 27 BytecodeArrayBuilder* builder_; | 27 BytecodeArrayBuilder* builder_; |
| 28 | 28 |
| 29 DISALLOW_COPY_AND_ASSIGN(ControlFlowBuilder); | 29 DISALLOW_COPY_AND_ASSIGN(ControlFlowBuilder); |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 class BreakableControlFlowBuilder : public ControlFlowBuilder { | 32 class V8_EXPORT_PRIVATE BreakableControlFlowBuilder |
| 33 : public ControlFlowBuilder { |
| 33 public: | 34 public: |
| 34 explicit BreakableControlFlowBuilder(BytecodeArrayBuilder* builder) | 35 explicit BreakableControlFlowBuilder(BytecodeArrayBuilder* builder) |
| 35 : ControlFlowBuilder(builder), break_labels_(builder->zone()) {} | 36 : ControlFlowBuilder(builder), break_labels_(builder->zone()) {} |
| 36 virtual ~BreakableControlFlowBuilder(); | 37 virtual ~BreakableControlFlowBuilder(); |
| 37 | 38 |
| 38 // This method should be called by the control flow owner before | 39 // This method should be called by the control flow owner before |
| 39 // destruction to update sites that emit jumps for break. | 40 // destruction to update sites that emit jumps for break. |
| 40 void BindBreakTarget(); | 41 void BindBreakTarget(); |
| 41 | 42 |
| 42 // This method is called when visiting break statements in the AST. | 43 // This method is called when visiting break statements in the AST. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 56 void EmitJumpIfFalse(BytecodeLabels* labels); | 57 void EmitJumpIfFalse(BytecodeLabels* labels); |
| 57 void EmitJumpIfUndefined(BytecodeLabels* labels); | 58 void EmitJumpIfUndefined(BytecodeLabels* labels); |
| 58 void EmitJumpIfNull(BytecodeLabels* labels); | 59 void EmitJumpIfNull(BytecodeLabels* labels); |
| 59 | 60 |
| 60 // Unbound labels that identify jumps for break statements in the code. | 61 // Unbound labels that identify jumps for break statements in the code. |
| 61 BytecodeLabels break_labels_; | 62 BytecodeLabels break_labels_; |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 | 65 |
| 65 // Class to track control flow for block statements (which can break in JS). | 66 // Class to track control flow for block statements (which can break in JS). |
| 66 class BlockBuilder final : public BreakableControlFlowBuilder { | 67 class V8_EXPORT_PRIVATE BlockBuilder final |
| 68 : public BreakableControlFlowBuilder { |
| 67 public: | 69 public: |
| 68 explicit BlockBuilder(BytecodeArrayBuilder* builder) | 70 explicit BlockBuilder(BytecodeArrayBuilder* builder) |
| 69 : BreakableControlFlowBuilder(builder) {} | 71 : BreakableControlFlowBuilder(builder) {} |
| 70 | 72 |
| 71 void EndBlock(); | 73 void EndBlock(); |
| 72 | 74 |
| 73 private: | 75 private: |
| 74 BytecodeLabel block_end_; | 76 BytecodeLabel block_end_; |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 | 79 |
| 78 // A class to help with co-ordinating break and continue statements with | 80 // A class to help with co-ordinating break and continue statements with |
| 79 // their loop. | 81 // their loop. |
| 80 class LoopBuilder final : public BreakableControlFlowBuilder { | 82 class V8_EXPORT_PRIVATE LoopBuilder final : public BreakableControlFlowBuilder { |
| 81 public: | 83 public: |
| 82 explicit LoopBuilder(BytecodeArrayBuilder* builder) | 84 explicit LoopBuilder(BytecodeArrayBuilder* builder) |
| 83 : BreakableControlFlowBuilder(builder), | 85 : BreakableControlFlowBuilder(builder), |
| 84 continue_labels_(builder->zone()), | 86 continue_labels_(builder->zone()), |
| 85 header_labels_(builder->zone()) {} | 87 header_labels_(builder->zone()) {} |
| 86 ~LoopBuilder(); | 88 ~LoopBuilder(); |
| 87 | 89 |
| 88 void LoopHeader(ZoneVector<BytecodeLabel>* additional_labels); | 90 void LoopHeader(ZoneVector<BytecodeLabel>* additional_labels = nullptr); |
| 89 void JumpToHeader(int loop_depth); | 91 void JumpToHeader(int loop_depth); |
| 90 void BindContinueTarget(); | 92 void BindContinueTarget(); |
| 91 void EndLoop(); | 93 void EndLoop(); |
| 92 | 94 |
| 93 // This method is called when visiting continue statements in the AST. | 95 // This method is called when visiting continue statements in the AST. |
| 94 // Inserts a jump to an unbound label that is patched when BindContinueTarget | 96 // Inserts a jump to an unbound label that is patched when BindContinueTarget |
| 95 // is called. | 97 // is called. |
| 96 void Continue() { EmitJump(&continue_labels_); } | 98 void Continue() { EmitJump(&continue_labels_); } |
| 97 void ContinueIfTrue() { EmitJumpIfTrue(&continue_labels_); } | 99 void ContinueIfTrue() { EmitJumpIfTrue(&continue_labels_); } |
| 98 void ContinueIfUndefined() { EmitJumpIfUndefined(&continue_labels_); } | 100 void ContinueIfUndefined() { EmitJumpIfUndefined(&continue_labels_); } |
| 99 void ContinueIfNull() { EmitJumpIfNull(&continue_labels_); } | 101 void ContinueIfNull() { EmitJumpIfNull(&continue_labels_); } |
| 100 | 102 |
| 101 private: | 103 private: |
| 102 BytecodeLabel loop_header_; | 104 BytecodeLabel loop_header_; |
| 103 | 105 |
| 104 // Unbound labels that identify jumps for continue statements in the code and | 106 // Unbound labels that identify jumps for continue statements in the code and |
| 105 // jumps from checking the loop condition to the header for do-while loops. | 107 // jumps from checking the loop condition to the header for do-while loops. |
| 106 BytecodeLabels continue_labels_; | 108 BytecodeLabels continue_labels_; |
| 107 BytecodeLabels header_labels_; | 109 BytecodeLabels header_labels_; |
| 108 }; | 110 }; |
| 109 | 111 |
| 110 | 112 |
| 111 // A class to help with co-ordinating break statements with their switch. | 113 // A class to help with co-ordinating break statements with their switch. |
| 112 class SwitchBuilder final : public BreakableControlFlowBuilder { | 114 class V8_EXPORT_PRIVATE SwitchBuilder final |
| 115 : public BreakableControlFlowBuilder { |
| 113 public: | 116 public: |
| 114 explicit SwitchBuilder(BytecodeArrayBuilder* builder, int number_of_cases) | 117 explicit SwitchBuilder(BytecodeArrayBuilder* builder, int number_of_cases) |
| 115 : BreakableControlFlowBuilder(builder), | 118 : BreakableControlFlowBuilder(builder), |
| 116 case_sites_(builder->zone()) { | 119 case_sites_(builder->zone()) { |
| 117 case_sites_.resize(number_of_cases); | 120 case_sites_.resize(number_of_cases); |
| 118 } | 121 } |
| 119 ~SwitchBuilder(); | 122 ~SwitchBuilder(); |
| 120 | 123 |
| 121 // This method should be called by the SwitchBuilder owner when the case | 124 // This method should be called by the SwitchBuilder owner when the case |
| 122 // statement with |index| is emitted to update the case jump site. | 125 // statement with |index| is emitted to update the case jump site. |
| 123 void SetCaseTarget(int index); | 126 void SetCaseTarget(int index); |
| 124 | 127 |
| 125 // This method is called when visiting case comparison operation for |index|. | 128 // This method is called when visiting case comparison operation for |index|. |
| 126 // Inserts a JumpIfTrue to a unbound label that is patched when the | 129 // Inserts a JumpIfTrue to a unbound label that is patched when the |
| 127 // corresponding SetCaseTarget is called. | 130 // corresponding SetCaseTarget is called. |
| 128 void Case(int index) { builder()->JumpIfTrue(&case_sites_.at(index)); } | 131 void Case(int index) { builder()->JumpIfTrue(&case_sites_.at(index)); } |
| 129 | 132 |
| 130 // This method is called when all cases comparisons have been emitted if there | 133 // This method is called when all cases comparisons have been emitted if there |
| 131 // is a default case statement. Inserts a Jump to a unbound label that is | 134 // is a default case statement. Inserts a Jump to a unbound label that is |
| 132 // patched when the corresponding SetCaseTarget is called. | 135 // patched when the corresponding SetCaseTarget is called. |
| 133 void DefaultAt(int index) { builder()->Jump(&case_sites_.at(index)); } | 136 void DefaultAt(int index) { builder()->Jump(&case_sites_.at(index)); } |
| 134 | 137 |
| 135 private: | 138 private: |
| 136 // Unbound labels that identify jumps for case statements in the code. | 139 // Unbound labels that identify jumps for case statements in the code. |
| 137 ZoneVector<BytecodeLabel> case_sites_; | 140 ZoneVector<BytecodeLabel> case_sites_; |
| 138 }; | 141 }; |
| 139 | 142 |
| 140 | 143 |
| 141 // A class to help with co-ordinating control flow in try-catch statements. | 144 // A class to help with co-ordinating control flow in try-catch statements. |
| 142 class TryCatchBuilder final : public ControlFlowBuilder { | 145 class V8_EXPORT_PRIVATE TryCatchBuilder final : public ControlFlowBuilder { |
| 143 public: | 146 public: |
| 144 explicit TryCatchBuilder(BytecodeArrayBuilder* builder, | 147 explicit TryCatchBuilder(BytecodeArrayBuilder* builder, |
| 145 HandlerTable::CatchPrediction catch_prediction) | 148 HandlerTable::CatchPrediction catch_prediction) |
| 146 : ControlFlowBuilder(builder), | 149 : ControlFlowBuilder(builder), |
| 147 handler_id_(builder->NewHandlerEntry()), | 150 handler_id_(builder->NewHandlerEntry()), |
| 148 catch_prediction_(catch_prediction) {} | 151 catch_prediction_(catch_prediction) {} |
| 149 | 152 |
| 150 void BeginTry(Register context); | 153 void BeginTry(Register context); |
| 151 void EndTry(); | 154 void EndTry(); |
| 152 void EndCatch(); | 155 void EndCatch(); |
| 153 | 156 |
| 154 private: | 157 private: |
| 155 int handler_id_; | 158 int handler_id_; |
| 156 HandlerTable::CatchPrediction catch_prediction_; | 159 HandlerTable::CatchPrediction catch_prediction_; |
| 157 BytecodeLabel handler_; | 160 BytecodeLabel handler_; |
| 158 BytecodeLabel exit_; | 161 BytecodeLabel exit_; |
| 159 }; | 162 }; |
| 160 | 163 |
| 161 | 164 |
| 162 // A class to help with co-ordinating control flow in try-finally statements. | 165 // A class to help with co-ordinating control flow in try-finally statements. |
| 163 class TryFinallyBuilder final : public ControlFlowBuilder { | 166 class V8_EXPORT_PRIVATE TryFinallyBuilder final : public ControlFlowBuilder { |
| 164 public: | 167 public: |
| 165 explicit TryFinallyBuilder(BytecodeArrayBuilder* builder, | 168 explicit TryFinallyBuilder(BytecodeArrayBuilder* builder, |
| 166 HandlerTable::CatchPrediction catch_prediction) | 169 HandlerTable::CatchPrediction catch_prediction) |
| 167 : ControlFlowBuilder(builder), | 170 : ControlFlowBuilder(builder), |
| 168 handler_id_(builder->NewHandlerEntry()), | 171 handler_id_(builder->NewHandlerEntry()), |
| 169 catch_prediction_(catch_prediction), | 172 catch_prediction_(catch_prediction), |
| 170 finalization_sites_(builder->zone()) {} | 173 finalization_sites_(builder->zone()) {} |
| 171 | 174 |
| 172 void BeginTry(Register context); | 175 void BeginTry(Register context); |
| 173 void LeaveTry(); | 176 void LeaveTry(); |
| 174 void EndTry(); | 177 void EndTry(); |
| 175 void BeginHandler(); | 178 void BeginHandler(); |
| 176 void BeginFinally(); | 179 void BeginFinally(); |
| 177 void EndFinally(); | 180 void EndFinally(); |
| 178 | 181 |
| 179 private: | 182 private: |
| 180 int handler_id_; | 183 int handler_id_; |
| 181 HandlerTable::CatchPrediction catch_prediction_; | 184 HandlerTable::CatchPrediction catch_prediction_; |
| 182 BytecodeLabel handler_; | 185 BytecodeLabel handler_; |
| 183 | 186 |
| 184 // Unbound labels that identify jumps to the finally block in the code. | 187 // Unbound labels that identify jumps to the finally block in the code. |
| 185 BytecodeLabels finalization_sites_; | 188 BytecodeLabels finalization_sites_; |
| 186 }; | 189 }; |
| 187 | 190 |
| 188 } // namespace interpreter | 191 } // namespace interpreter |
| 189 } // namespace internal | 192 } // namespace internal |
| 190 } // namespace v8 | 193 } // namespace v8 |
| 191 | 194 |
| 192 #endif // V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ | 195 #endif // V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ |
| OLD | NEW |