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