| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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. | |
| 4 | |
| 5 #ifndef V8_CCTEST_COMPILER_STRUCTURED_MACHINE_ASSEMBLER_H_ | |
| 6 #define V8_CCTEST_COMPILER_STRUCTURED_MACHINE_ASSEMBLER_H_ | |
| 7 | |
| 8 #include "src/v8.h" | |
| 9 | |
| 10 #include "src/compiler/common-operator.h" | |
| 11 #include "src/compiler/graph-builder.h" | |
| 12 #include "src/compiler/machine-operator.h" | |
| 13 #include "src/compiler/node.h" | |
| 14 #include "src/compiler/operator.h" | |
| 15 #include "src/compiler/raw-machine-assembler.h" | |
| 16 | |
| 17 | |
| 18 namespace v8 { | |
| 19 namespace internal { | |
| 20 namespace compiler { | |
| 21 | |
| 22 class BasicBlock; | |
| 23 class Schedule; | |
| 24 class StructuredMachineAssembler; | |
| 25 | |
| 26 | |
| 27 class Variable : public ZoneObject { | |
| 28 public: | |
| 29 Node* Get() const; | |
| 30 void Set(Node* value) const; | |
| 31 | |
| 32 private: | |
| 33 Variable(StructuredMachineAssembler* smasm, int offset) | |
| 34 : smasm_(smasm), offset_(offset) {} | |
| 35 | |
| 36 friend class StructuredMachineAssembler; | |
| 37 friend class StructuredMachineAssemblerFriend; | |
| 38 StructuredMachineAssembler* const smasm_; | |
| 39 const int offset_; | |
| 40 }; | |
| 41 | |
| 42 | |
| 43 class StructuredMachineAssembler : public RawMachineAssembler { | |
| 44 public: | |
| 45 class Environment : public ZoneObject { | |
| 46 public: | |
| 47 Environment(Zone* zone, BasicBlock* block, bool is_dead_); | |
| 48 | |
| 49 private: | |
| 50 BasicBlock* block_; | |
| 51 NodeVector variables_; | |
| 52 bool is_dead_; | |
| 53 friend class StructuredMachineAssembler; | |
| 54 DISALLOW_COPY_AND_ASSIGN(Environment); | |
| 55 }; | |
| 56 | |
| 57 class IfBuilder; | |
| 58 friend class IfBuilder; | |
| 59 class LoopBuilder; | |
| 60 friend class LoopBuilder; | |
| 61 | |
| 62 StructuredMachineAssembler(Graph* graph, MachineSignature* machine_sig, | |
| 63 MachineType word = kMachPtr); | |
| 64 virtual ~StructuredMachineAssembler() {} | |
| 65 | |
| 66 // Variables. | |
| 67 Variable NewVariable(Node* initial_value); | |
| 68 // Control flow. | |
| 69 void Return(Node* value); | |
| 70 | |
| 71 protected: | |
| 72 virtual Node* MakeNode(Operator* op, int input_count, Node** inputs); | |
| 73 | |
| 74 private: | |
| 75 typedef ZoneVector<Environment*> EnvironmentVector; | |
| 76 | |
| 77 NodeVector* CurrentVars() { return ¤t_environment_->variables_; } | |
| 78 Node*& VariableAt(Environment* environment, int offset); | |
| 79 Node* GetVariable(int offset); | |
| 80 void SetVariable(int offset, Node* value); | |
| 81 | |
| 82 void AddBranch(Environment* environment, Node* condition, | |
| 83 Environment* true_val, Environment* false_val); | |
| 84 void AddGoto(Environment* from, Environment* to); | |
| 85 BasicBlock* TrampolineFor(BasicBlock* block); | |
| 86 | |
| 87 void CopyCurrentAsDead(); | |
| 88 Environment* Copy(Environment* environment) { | |
| 89 return Copy(environment, static_cast<int>(environment->variables_.size())); | |
| 90 } | |
| 91 Environment* Copy(Environment* environment, int truncate_at); | |
| 92 void Merge(EnvironmentVector* environments, int truncate_at); | |
| 93 Environment* CopyForLoopHeader(Environment* environment); | |
| 94 void MergeBackEdgesToLoopHeader(Environment* header, | |
| 95 EnvironmentVector* environments); | |
| 96 | |
| 97 Environment* current_environment_; | |
| 98 int number_of_variables_; | |
| 99 | |
| 100 friend class Variable; | |
| 101 // For testing only. | |
| 102 friend class StructuredMachineAssemblerFriend; | |
| 103 DISALLOW_COPY_AND_ASSIGN(StructuredMachineAssembler); | |
| 104 }; | |
| 105 | |
| 106 // IfBuilder constructs of nested if-else expressions which more or less follow | |
| 107 // C semantics. Foe example: | |
| 108 // | |
| 109 // if (x) {do_x} else if (y) {do_y} else {do_z} | |
| 110 // | |
| 111 // would look like this: | |
| 112 // | |
| 113 // IfBuilder b; | |
| 114 // b.If(x).Then(); | |
| 115 // do_x | |
| 116 // b.Else(); | |
| 117 // b.If().Then(); | |
| 118 // do_y | |
| 119 // b.Else(); | |
| 120 // do_z | |
| 121 // b.End(); | |
| 122 // | |
| 123 // Then() and Else() can be skipped, representing an empty block in C. | |
| 124 // Combinations like If(x).Then().If(x).Then() are legitimate, but | |
| 125 // Else().Else() is not. That is, once you've nested an If(), you can't get to a | |
| 126 // higher level If() branch. | |
| 127 // TODO(dcarney): describe expressions once the api is finalized. | |
| 128 class StructuredMachineAssembler::IfBuilder { | |
| 129 public: | |
| 130 explicit IfBuilder(StructuredMachineAssembler* smasm); | |
| 131 ~IfBuilder() { | |
| 132 if (!IsDone()) End(); | |
| 133 } | |
| 134 | |
| 135 IfBuilder& If(); // TODO(dcarney): this should take an expression. | |
| 136 IfBuilder& If(Node* condition); | |
| 137 void Then(); | |
| 138 void Else(); | |
| 139 void End(); | |
| 140 | |
| 141 // The next 4 functions are exposed for expression support. | |
| 142 // They will be private once I have a nice expression api. | |
| 143 void And(); | |
| 144 void Or(); | |
| 145 IfBuilder& OpenParen() { | |
| 146 DCHECK(smasm_->current_environment_ != NULL); | |
| 147 CurrentClause()->PushNewExpressionState(); | |
| 148 return *this; | |
| 149 } | |
| 150 IfBuilder& CloseParen() { | |
| 151 DCHECK(smasm_->current_environment_ == NULL); | |
| 152 CurrentClause()->PopExpressionState(); | |
| 153 return *this; | |
| 154 } | |
| 155 | |
| 156 private: | |
| 157 // UnresolvedBranch represents the chain of environments created while | |
| 158 // generating an expression. At this point, a branch Node | |
| 159 // cannot be created, as the target environments of the branch are not yet | |
| 160 // available, so everything required to create the branch Node is | |
| 161 // stored in this structure until the target environments are resolved. | |
| 162 struct UnresolvedBranch : public ZoneObject { | |
| 163 UnresolvedBranch(Environment* environment, Node* condition, | |
| 164 UnresolvedBranch* next) | |
| 165 : environment_(environment), condition_(condition), next_(next) {} | |
| 166 // environment_ will eventually be terminated by a branch on condition_. | |
| 167 Environment* environment_; | |
| 168 Node* condition_; | |
| 169 // next_ is the next link in the UnresolvedBranch chain, and will be | |
| 170 // either the true or false branch jumped to from environment_. | |
| 171 UnresolvedBranch* next_; | |
| 172 }; | |
| 173 | |
| 174 struct ExpressionState { | |
| 175 int pending_then_size_; | |
| 176 int pending_else_size_; | |
| 177 }; | |
| 178 | |
| 179 typedef ZoneVector<ExpressionState> ExpressionStates; | |
| 180 typedef ZoneVector<UnresolvedBranch*> PendingMergeStack; | |
| 181 struct IfClause; | |
| 182 typedef ZoneVector<IfClause*> IfClauses; | |
| 183 | |
| 184 struct PendingMergeStackRange { | |
| 185 PendingMergeStack* merge_stack_; | |
| 186 int start_; | |
| 187 int size_; | |
| 188 }; | |
| 189 | |
| 190 enum CombineType { kCombineThen, kCombineElse }; | |
| 191 enum ResolutionType { kExpressionTerm, kExpressionDone }; | |
| 192 | |
| 193 // IfClause represents one level of if-then-else nesting plus the associated | |
| 194 // expression. | |
| 195 // A call to If() triggers creation of a new nesting level after expression | |
| 196 // creation is complete - ie Then() or Else() has been called. | |
| 197 struct IfClause : public ZoneObject { | |
| 198 IfClause(Zone* zone, int initial_environment_size); | |
| 199 void CopyEnvironments(const PendingMergeStackRange& data, | |
| 200 EnvironmentVector* environments); | |
| 201 void ResolvePendingMerges(StructuredMachineAssembler* smasm, | |
| 202 CombineType combine_type, | |
| 203 ResolutionType resolution_type); | |
| 204 PendingMergeStackRange ComputeRelevantMerges(CombineType combine_type); | |
| 205 void FinalizeBranches(StructuredMachineAssembler* smasm, | |
| 206 const PendingMergeStackRange& offset_data, | |
| 207 CombineType combine_type, | |
| 208 Environment* then_environment, | |
| 209 Environment* else_environment); | |
| 210 void PushNewExpressionState(); | |
| 211 void PopExpressionState(); | |
| 212 | |
| 213 // Each invocation of And or Or creates a new UnresolvedBranch. | |
| 214 // These form a singly-linked list, of which we only need to keep track of | |
| 215 // the tail. On creation of an UnresolvedBranch, pending_then_merges_ and | |
| 216 // pending_else_merges_ each push a copy, which are removed on merges to the | |
| 217 // respective environment. | |
| 218 UnresolvedBranch* unresolved_list_tail_; | |
| 219 int initial_environment_size_; | |
| 220 // expression_states_ keeps track of the state of pending_*_merges_, | |
| 221 // pushing and popping the lengths of these on | |
| 222 // OpenParend() and CloseParend() respectively. | |
| 223 ExpressionStates expression_states_; | |
| 224 PendingMergeStack pending_then_merges_; | |
| 225 PendingMergeStack pending_else_merges_; | |
| 226 // then_environment_ is created iff there is a call to Then(), otherwise | |
| 227 // branches which would merge to it merge to the exit environment instead. | |
| 228 // Likewise for else_environment_. | |
| 229 Environment* then_environment_; | |
| 230 Environment* else_environment_; | |
| 231 }; | |
| 232 | |
| 233 IfClause* CurrentClause() { return if_clauses_.back(); } | |
| 234 void AddCurrentToPending(); | |
| 235 void PushNewIfClause(); | |
| 236 bool IsDone() { return if_clauses_.empty(); } | |
| 237 | |
| 238 StructuredMachineAssembler* smasm_; | |
| 239 IfClauses if_clauses_; | |
| 240 EnvironmentVector pending_exit_merges_; | |
| 241 DISALLOW_COPY_AND_ASSIGN(IfBuilder); | |
| 242 }; | |
| 243 | |
| 244 | |
| 245 class StructuredMachineAssembler::LoopBuilder { | |
| 246 public: | |
| 247 explicit LoopBuilder(StructuredMachineAssembler* smasm); | |
| 248 ~LoopBuilder() { | |
| 249 if (!IsDone()) End(); | |
| 250 } | |
| 251 | |
| 252 void Break(); | |
| 253 void Continue(); | |
| 254 void End(); | |
| 255 | |
| 256 private: | |
| 257 friend class StructuredMachineAssembler; | |
| 258 bool IsDone() { return header_environment_ == NULL; } | |
| 259 | |
| 260 StructuredMachineAssembler* smasm_; | |
| 261 Environment* header_environment_; | |
| 262 EnvironmentVector pending_header_merges_; | |
| 263 EnvironmentVector pending_exit_merges_; | |
| 264 DISALLOW_COPY_AND_ASSIGN(LoopBuilder); | |
| 265 }; | |
| 266 | |
| 267 } // namespace compiler | |
| 268 } // namespace internal | |
| 269 } // namespace v8 | |
| 270 | |
| 271 #endif // V8_CCTEST_COMPILER_STRUCTURED_MACHINE_ASSEMBLER_H_ | |
| OLD | NEW |