OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #ifndef VM_INTERMEDIATE_LANGUAGE_H_ |
| 6 #define VM_INTERMEDIATE_LANGUAGE_H_ |
| 7 |
| 8 #include "vm/allocation.h" |
| 9 #include "vm/growable_array.h" |
| 10 #include "vm/handles_impl.h" |
| 11 #include "vm/object.h" |
| 12 |
| 13 namespace dart { |
| 14 |
| 15 class LocalVariable; |
| 16 |
| 17 // Computations and values. |
| 18 // |
| 19 // <Computation> ::= <Value> |
| 20 // | AssertAssignable <Value> <AbstractType> |
| 21 // | InstanceCall <cstring> <Value> ... |
| 22 // | StaticCall <Function> <Value> ... |
| 23 // | LoadLocal <LocalVariable> |
| 24 // | StoreLocal <LocalVariable> <Value> |
| 25 // |
| 26 // <Value> ::= Temp <int> |
| 27 // | Constant <Instance> |
| 28 |
| 29 class Computation : public ZoneAllocated { |
| 30 public: |
| 31 Computation() { } |
| 32 |
| 33 // Prints a computation without indentation or trailing newlines. |
| 34 virtual void Print() const = 0; |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(Computation); |
| 38 }; |
| 39 |
| 40 |
| 41 class Value : public Computation { |
| 42 public: |
| 43 Value() { } |
| 44 |
| 45 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(Value); |
| 47 }; |
| 48 |
| 49 |
| 50 class AssertAssignableComp : public Computation { |
| 51 public: |
| 52 AssertAssignableComp(Value* value, const AbstractType& type) |
| 53 : value_(value), type_(type) { } |
| 54 |
| 55 virtual void Print() const; |
| 56 |
| 57 private: |
| 58 Value* value_; |
| 59 const AbstractType& type_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(AssertAssignableComp); |
| 62 }; |
| 63 |
| 64 |
| 65 class InstanceCallComp : public Computation { |
| 66 public: |
| 67 InstanceCallComp(const char* name, ZoneGrowableArray<Value*>* arguments) |
| 68 : name_(name), arguments_(arguments) { } |
| 69 |
| 70 virtual void Print() const; |
| 71 |
| 72 private: |
| 73 const char* name_; |
| 74 ZoneGrowableArray<Value*>* arguments_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(InstanceCallComp); |
| 77 }; |
| 78 |
| 79 |
| 80 class StaticCallComp : public Computation { |
| 81 public: |
| 82 StaticCallComp(const Function& function, ZoneGrowableArray<Value*>* arguments) |
| 83 : function_(function), arguments_(arguments) { |
| 84 ASSERT(function.IsZoneHandle()); |
| 85 } |
| 86 |
| 87 virtual void Print() const; |
| 88 |
| 89 private: |
| 90 const Function& function_; |
| 91 ZoneGrowableArray<Value*>* arguments_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(StaticCallComp); |
| 94 }; |
| 95 |
| 96 |
| 97 class LoadLocalComp : public Computation { |
| 98 public: |
| 99 explicit LoadLocalComp(const LocalVariable& local) : local_(local) { } |
| 100 |
| 101 virtual void Print() const; |
| 102 |
| 103 private: |
| 104 const LocalVariable& local_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(LoadLocalComp); |
| 107 }; |
| 108 |
| 109 |
| 110 class StoreLocalComp : public Computation { |
| 111 public: |
| 112 StoreLocalComp(const LocalVariable& local, Value* value) |
| 113 : local_(local), value_(value) { } |
| 114 |
| 115 virtual void Print() const; |
| 116 |
| 117 private: |
| 118 const LocalVariable& local_; |
| 119 Value* value_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(StoreLocalComp); |
| 122 }; |
| 123 |
| 124 |
| 125 class TempValue : public Value { |
| 126 public: |
| 127 explicit TempValue(intptr_t index) : index_(index) { } |
| 128 |
| 129 virtual void Print() const; |
| 130 |
| 131 private: |
| 132 intptr_t index_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(TempValue); |
| 135 }; |
| 136 |
| 137 |
| 138 class ConstantValue: public Value { |
| 139 public: |
| 140 explicit ConstantValue(const Instance& instance) : instance_(instance) { } |
| 141 |
| 142 virtual void Print() const; |
| 143 |
| 144 private: |
| 145 const Instance& instance_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(ConstantValue); |
| 148 }; |
| 149 |
| 150 |
| 151 // Instructions. |
| 152 // |
| 153 // <Instruction> ::= Do <Computation> <Instruction> |
| 154 // | Bind <int> <Computation> <Instruction> |
| 155 // | Return <Value> |
| 156 // | Branch <Value> <Instruction> <Instruction> |
| 157 // | Empty <Instruction> |
| 158 |
| 159 class Instruction : public ZoneAllocated { |
| 160 public: |
| 161 Instruction() : mark_(false) { } |
| 162 |
| 163 virtual void set_successor(Instruction* instr) = 0; |
| 164 |
| 165 // Perform a postorder traversal of the instruction graph reachable from |
| 166 // this instruction. Append the result to the end of the in/out parameter |
| 167 // visited. |
| 168 virtual void Postorder(GrowableArray<Instruction*>* visited) = 0; |
| 169 |
| 170 // Print an instruction without indentation, instruction number, or a |
| 171 // trailing newline. |
| 172 virtual void Print( |
| 173 intptr_t instruction_index, |
| 174 const GrowableArray<Instruction*>& instruction_list) const = 0; |
| 175 |
| 176 // Mark bit to support non-reentrant recursive traversal (i.e., |
| 177 // identification of cycles). Before and after a traversal, all the nodes |
| 178 // must have the same mark. |
| 179 bool mark() const { return mark_; } |
| 180 void flip_mark() { mark_ = !mark_; } |
| 181 |
| 182 protected: |
| 183 // Helper for print handling of successors of nodes with a single successor. |
| 184 // "goto %d" is printed if the successor is not the next instruction. |
| 185 void PrintGotoSuccessor( |
| 186 Instruction* successor, |
| 187 intptr_t instruction_index, |
| 188 const GrowableArray<Instruction*>& instruction_list) const; |
| 189 |
| 190 private: |
| 191 bool mark_; |
| 192 }; |
| 193 |
| 194 |
| 195 class DoInstr : public Instruction { |
| 196 public: |
| 197 explicit DoInstr(Computation* comp) |
| 198 : Instruction(), computation_(comp), successor_(NULL) { } |
| 199 |
| 200 virtual void set_successor(Instruction* instr) { |
| 201 ASSERT(successor_ == NULL); |
| 202 successor_ = instr; |
| 203 } |
| 204 |
| 205 virtual void Postorder(GrowableArray<Instruction*>* visited); |
| 206 |
| 207 virtual void Print( |
| 208 intptr_t instruction_index, |
| 209 const GrowableArray<Instruction*>& instruction_list) const; |
| 210 |
| 211 private: |
| 212 Computation* computation_; |
| 213 Instruction* successor_; |
| 214 }; |
| 215 |
| 216 |
| 217 class BindInstr : public Instruction { |
| 218 public: |
| 219 BindInstr(intptr_t temp_index, Computation* computation) |
| 220 : Instruction(), |
| 221 temp_index_(temp_index), |
| 222 computation_(computation), |
| 223 successor_(NULL) { } |
| 224 |
| 225 virtual void set_successor(Instruction* instr) { |
| 226 ASSERT(successor_ == NULL); |
| 227 successor_ = instr; |
| 228 } |
| 229 |
| 230 virtual void Postorder(GrowableArray<Instruction*>* visited); |
| 231 |
| 232 virtual void Print( |
| 233 intptr_t instruction_index, |
| 234 const GrowableArray<Instruction*>& instruction_list) const; |
| 235 |
| 236 private: |
| 237 const intptr_t temp_index_; |
| 238 Computation* computation_; |
| 239 Instruction* successor_; |
| 240 }; |
| 241 |
| 242 |
| 243 class JoinEntryInstr : public Instruction { |
| 244 public: |
| 245 JoinEntryInstr() : Instruction(), successor_(NULL) { } |
| 246 |
| 247 virtual void set_successor(Instruction* instr) { |
| 248 ASSERT(successor_ == NULL); |
| 249 successor_ = instr; |
| 250 } |
| 251 |
| 252 virtual void Postorder(GrowableArray<Instruction*>* visited); |
| 253 |
| 254 virtual void Print( |
| 255 intptr_t instruction_index, |
| 256 const GrowableArray<Instruction*>& instruction_list) const; |
| 257 |
| 258 private: |
| 259 Instruction* successor_; |
| 260 }; |
| 261 |
| 262 |
| 263 class TargetEntryInstr : public Instruction { |
| 264 public: |
| 265 TargetEntryInstr() : Instruction(), successor_(NULL) { } |
| 266 |
| 267 virtual void set_successor(Instruction* instr) { |
| 268 ASSERT(successor_ == NULL); |
| 269 successor_ = instr; |
| 270 } |
| 271 |
| 272 virtual void Postorder(GrowableArray<Instruction*>* visited); |
| 273 |
| 274 virtual void Print( |
| 275 intptr_t instruction_index, |
| 276 const GrowableArray<Instruction*>& instruction_list) const; |
| 277 |
| 278 private: |
| 279 Instruction* successor_; |
| 280 }; |
| 281 |
| 282 |
| 283 class ReturnInstr : public Instruction { |
| 284 public: |
| 285 explicit ReturnInstr(Value* value) : Instruction(), value_(value) { } |
| 286 |
| 287 virtual void set_successor(Instruction* instr) { UNREACHABLE(); } |
| 288 |
| 289 virtual void Postorder(GrowableArray<Instruction*>* visited); |
| 290 |
| 291 virtual void Print( |
| 292 intptr_t instruction_index, |
| 293 const GrowableArray<Instruction*>& instruction_list) const; |
| 294 |
| 295 private: |
| 296 Value* value_; |
| 297 }; |
| 298 |
| 299 |
| 300 class BranchInstr : public Instruction { |
| 301 public: |
| 302 explicit BranchInstr(Value* value) |
| 303 : Instruction(), |
| 304 value_(value), |
| 305 true_successor_(NULL), |
| 306 false_successor_(NULL) { } |
| 307 |
| 308 virtual void set_successor(Instruction* instr) { UNREACHABLE(); } |
| 309 |
| 310 TargetEntryInstr** true_successor_address() { return &true_successor_; } |
| 311 TargetEntryInstr** false_successor_address() { return &false_successor_; } |
| 312 |
| 313 virtual void Postorder(GrowableArray<Instruction*>* visited); |
| 314 |
| 315 virtual void Print( |
| 316 intptr_t instruction_index, |
| 317 const GrowableArray<Instruction*>& instruction_list) const; |
| 318 |
| 319 private: |
| 320 Value* value_; |
| 321 TargetEntryInstr* true_successor_; |
| 322 TargetEntryInstr* false_successor_; |
| 323 }; |
| 324 |
| 325 |
| 326 } // namespace dart |
| 327 |
| 328 #endif // VM_INTERMEDIATE_LANGUAGE_H_ |
OLD | NEW |