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/zone-containers.h" | 10 #include "src/zone-containers.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 protected: | 22 protected: |
23 BytecodeArrayBuilder* builder() const { return builder_; } | 23 BytecodeArrayBuilder* builder() const { return builder_; } |
24 | 24 |
25 private: | 25 private: |
26 BytecodeArrayBuilder* builder_; | 26 BytecodeArrayBuilder* builder_; |
27 | 27 |
28 DISALLOW_COPY_AND_ASSIGN(ControlFlowBuilder); | 28 DISALLOW_COPY_AND_ASSIGN(ControlFlowBuilder); |
29 }; | 29 }; |
30 | 30 |
| 31 class BreakableControlFlowBuilder : public ControlFlowBuilder { |
| 32 public: |
| 33 explicit BreakableControlFlowBuilder(BytecodeArrayBuilder* builder) |
| 34 : ControlFlowBuilder(builder), |
| 35 break_sites_(builder->zone()) {} |
| 36 virtual ~BreakableControlFlowBuilder(); |
| 37 |
| 38 // This method should be called by the control flow owner before |
| 39 // destruction to update sites that emit jumps for break. |
| 40 void SetBreakTarget(const BytecodeLabel& break_target); |
| 41 |
| 42 // This method is called when visiting break statements in the AST. |
| 43 // Inserts a jump to a unbound label that is patched when the corresponding |
| 44 // SetBreakTarget is called. |
| 45 void Break() { EmitJump(&break_sites_); } |
| 46 void BreakIfTrue() { EmitJumpIfTrue(&break_sites_); } |
| 47 void BreakIfUndefined() { EmitJumpIfUndefined(&break_sites_); } |
| 48 void BreakIfNull() { EmitJumpIfNull(&break_sites_); } |
| 49 |
| 50 protected: |
| 51 void EmitJump(ZoneVector<BytecodeLabel>* labels); |
| 52 void EmitJump(ZoneVector<BytecodeLabel>* labels, int index); |
| 53 void EmitJumpIfTrue(ZoneVector<BytecodeLabel>* labels); |
| 54 void EmitJumpIfTrue(ZoneVector<BytecodeLabel>* labels, int index); |
| 55 void EmitJumpIfUndefined(ZoneVector<BytecodeLabel>* labels); |
| 56 void EmitJumpIfNull(ZoneVector<BytecodeLabel>* labels); |
| 57 |
| 58 void BindLabels(const BytecodeLabel& target, ZoneVector<BytecodeLabel>* site); |
| 59 |
| 60 private: |
| 61 // Unbound labels that identify jumps for break statements in the code. |
| 62 ZoneVector<BytecodeLabel> break_sites_; |
| 63 }; |
| 64 |
31 // A class to help with co-ordinating break and continue statements with | 65 // A class to help with co-ordinating break and continue statements with |
32 // their loop. | 66 // their loop. |
33 // TODO(oth): add support for TF branch/merge info. | 67 // TODO(oth): add support for TF branch/merge info. |
34 class LoopBuilder : public ControlFlowBuilder { | 68 class LoopBuilder final : public BreakableControlFlowBuilder { |
35 public: | 69 public: |
36 explicit LoopBuilder(BytecodeArrayBuilder* builder) | 70 explicit LoopBuilder(BytecodeArrayBuilder* builder) |
37 : ControlFlowBuilder(builder), | 71 : BreakableControlFlowBuilder(builder), |
38 continue_sites_(builder->zone()), | 72 continue_sites_(builder->zone()) {} |
39 break_sites_(builder->zone()) {} | |
40 ~LoopBuilder(); | 73 ~LoopBuilder(); |
41 | 74 |
42 // These methods should be called by the LoopBuilder owner before | 75 // This methods should be called by the LoopBuilder owner before |
43 // destruction to update sites that emit jumps for break/continue. | 76 // destruction to update sites that emit jumps for continue. |
44 void SetContinueTarget(const BytecodeLabel& continue_target); | 77 void SetContinueTarget(const BytecodeLabel& continue_target); |
45 void SetBreakTarget(const BytecodeLabel& break_target); | |
46 | 78 |
47 // These methods are called when visiting break and continue | 79 // This method is called when visiting continue statements in the AST. |
48 // statements in the AST. Inserts a jump to a unbound label that is | 80 // Inserts a jump to a unbound label that is patched when the corresponding |
49 // patched when the corresponding SetContinueTarget/SetBreakTarget | 81 // SetContinueTarget is called. |
50 // is called. | |
51 void Break() { EmitJump(&break_sites_); } | |
52 void BreakIfTrue() { EmitJumpIfTrue(&break_sites_); } | |
53 void BreakIfUndefined() { EmitJumpIfUndefined(&break_sites_); } | |
54 void BreakIfNull() { EmitJumpIfNull(&break_sites_); } | |
55 void Continue() { EmitJump(&continue_sites_); } | 82 void Continue() { EmitJump(&continue_sites_); } |
56 void ContinueIfTrue() { EmitJumpIfTrue(&continue_sites_); } | 83 void ContinueIfTrue() { EmitJumpIfTrue(&continue_sites_); } |
57 void ContinueIfUndefined() { EmitJumpIfUndefined(&continue_sites_); } | 84 void ContinueIfUndefined() { EmitJumpIfUndefined(&continue_sites_); } |
58 void ContinueIfNull() { EmitJumpIfNull(&continue_sites_); } | 85 void ContinueIfNull() { EmitJumpIfNull(&continue_sites_); } |
59 | 86 |
60 private: | 87 private: |
61 void BindLabels(const BytecodeLabel& target, ZoneVector<BytecodeLabel>* site); | 88 // Unbound labels that identify jumps for continue statements in the code. |
62 void EmitJump(ZoneVector<BytecodeLabel>* labels); | 89 ZoneVector<BytecodeLabel> continue_sites_; |
63 void EmitJumpIfTrue(ZoneVector<BytecodeLabel>* labels); | 90 }; |
64 void EmitJumpIfUndefined(ZoneVector<BytecodeLabel>* labels); | |
65 void EmitJumpIfNull(ZoneVector<BytecodeLabel>* labels); | |
66 | 91 |
67 // Unbound labels that identify jumps for continue/break statements | 92 // A class to help with co-ordinating break statements with their switch. |
68 // in the code. | 93 // TODO(oth): add support for TF branch/merge info. |
69 ZoneVector<BytecodeLabel> continue_sites_; | 94 class SwitchBuilder final : public BreakableControlFlowBuilder { |
70 ZoneVector<BytecodeLabel> break_sites_; | 95 public: |
| 96 explicit SwitchBuilder(BytecodeArrayBuilder* builder, int number_of_cases) |
| 97 : BreakableControlFlowBuilder(builder), |
| 98 case_sites_(builder->zone()) { |
| 99 case_sites_.resize(number_of_cases); |
| 100 } |
| 101 ~SwitchBuilder(); |
| 102 |
| 103 // This method should be called by the SwitchBuilder owner when the case |
| 104 // statement with |index| is emitted to update the case jump site. |
| 105 void SetCaseTarget(int index); |
| 106 |
| 107 // This method is called when visiting case comparison operation for |index|. |
| 108 // Inserts a JumpIfTrue to a unbound label that is patched when the |
| 109 // corresponding SetCaseTarget is called. |
| 110 void Case(int index) { EmitJumpIfTrue(&case_sites_, index); } |
| 111 |
| 112 // This method is called when all cases comparisons have been emitted if there |
| 113 // is a default case statement. Inserts a Jump to a unbound label that is |
| 114 // patched when the corresponding SetCaseTarget is called. |
| 115 void DefaultAt(int index) { EmitJump(&case_sites_, index); } |
| 116 |
| 117 private: |
| 118 // Unbound labels that identify jumps for case statements in the code. |
| 119 ZoneVector<BytecodeLabel> case_sites_; |
71 }; | 120 }; |
72 | 121 |
73 } // namespace interpreter | 122 } // namespace interpreter |
74 } // namespace internal | 123 } // namespace internal |
75 } // namespace v8 | 124 } // namespace v8 |
76 | 125 |
77 #endif // V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ | 126 #endif // V8_INTERPRETER_CONTROL_FLOW_BUILDERS_H_ |
OLD | NEW |