OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_LOCATIONS_H_ | 5 #ifndef VM_LOCATIONS_H_ |
6 #define VM_LOCATIONS_H_ | 6 #define VM_LOCATIONS_H_ |
7 | 7 |
8 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
9 #include "vm/assembler.h" | 9 #include "vm/assembler.h" |
10 #include "vm/bitfield.h" | 10 #include "vm/bitfield.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 class BufferFormatter; | 14 class BufferFormatter; |
15 class Value; | 15 class Value; |
| 16 class PairLocation; |
16 | 17 |
17 | 18 |
18 enum Representation { | 19 enum Representation { |
19 kNoRepresentation, | 20 kNoRepresentation, |
20 kTagged, | 21 kTagged, |
21 kUntagged, | 22 kUntagged, |
22 kUnboxedDouble, | 23 kUnboxedDouble, |
23 kUnboxedMint, | 24 kUnboxedMint, |
24 kUnboxedFloat32x4, | 25 kUnboxedFloat32x4, |
25 kUnboxedInt32x4, | 26 kUnboxedInt32x4, |
26 kUnboxedFloat64x2, | 27 kUnboxedFloat64x2, |
| 28 kPairOfTagged, |
| 29 kPairOfUnboxedDouble, |
27 kNumRepresentations | 30 kNumRepresentations |
28 }; | 31 }; |
29 | 32 |
30 | 33 |
31 // Location objects are used to connect register allocator and code generator. | 34 // Location objects are used to connect register allocator and code generator. |
32 // Instruction templates used by code generator have a corresponding | 35 // Instruction templates used by code generator have a corresponding |
33 // LocationSummary object which specifies expected location for every input | 36 // LocationSummary object which specifies expected location for every input |
34 // and output. | 37 // and output. |
35 // Each location is encoded as a single word: for non-constant locations | 38 // Each location is encoded as a single word: for non-constant locations |
36 // low 3 bits denote location kind, rest is kind specific location payload | 39 // low 4 bits denote location kind, rest is kind specific location payload |
37 // e.g. for REGISTER kind payload is register code (value of the Register | 40 // e.g. for REGISTER kind payload is register code (value of the Register |
38 // enumeration), constant locations contain a tagged (low 2 bits are set to 01) | 41 // enumeration), constant locations contain a tagged (low 2 bits are set to 01) |
39 // Object handle. | 42 // Object handle. |
40 // | 43 // |
41 // Locations must satisfy the following invariant: if two locations' encodings | 44 // Locations must satisfy the following invariant: if two locations' encodings |
42 // are bitwise unequal then these two locations are guaranteed to be disjoint. | 45 // are bitwise unequal then these two locations are guaranteed to be disjoint. |
43 // Properties like representation belong to the value that is stored in | 46 // Properties like representation belong to the value that is stored in |
44 // the location not to the location itself. | 47 // the location not to the location itself. |
45 class Location : public ValueObject { | 48 class Location : public ValueObject { |
46 private: | 49 private: |
47 enum { | 50 enum { |
48 // Number of bits required to encode Kind value. | 51 // Number of bits required to encode Kind value. |
49 kBitsForKind = 4, | 52 kBitsForKind = 4, |
50 kBitsForPayload = kWordSize * kBitsPerByte - kBitsForKind, | 53 kBitsForPayload = kWordSize * kBitsPerByte - kBitsForKind, |
51 }; | 54 }; |
52 | 55 |
53 static const uword kInvalidLocation = 0; | 56 static const uword kInvalidLocation = 0; |
54 static const uword kConstantMask = 0x3; | 57 static const uword kLocationTagMask = 0x3; |
55 | |
56 static const intptr_t kMachineRegisterMask = 0x6; | |
57 static const intptr_t kMachineRegister = 0x6; | |
58 | 58 |
59 public: | 59 public: |
60 // Constant payload can overlap with kind field so Kind values | 60 // Constant payload can overlap with kind field so Kind values |
61 // have to be chosen in a way that their last 2 bits are never | 61 // have to be chosen in a way that their last 2 bits are never |
62 // the same as kConstant. | 62 // the same as kConstantTag or kPairLocationTag. |
63 // Note that two locations with different kinds should never point to | 63 // Note that two locations with different kinds should never point to |
64 // the same place. For example kQuadStackSlot location should never intersect | 64 // the same place. For example kQuadStackSlot location should never intersect |
65 // with kDoubleStackSlot location. | 65 // with kDoubleStackSlot location. |
66 enum Kind { | 66 enum Kind { |
67 // This location is invalid. Payload must be zero. | 67 // This location is invalid. Payload must be zero. |
68 kInvalid = 0, | 68 kInvalid = 0, |
69 | 69 |
70 // Constant value. This location contains a tagged Object handle. | 70 // Constant value. This location contains a tagged Object handle. |
71 kConstant = 1, | 71 kConstantTag = 1, |
| 72 |
| 73 // This location contains a tagged pointer to a PairLocation. |
| 74 kPairLocationTag = 2, |
72 | 75 |
73 // Unallocated location represents a location that is not fixed and can be | 76 // Unallocated location represents a location that is not fixed and can be |
74 // allocated by a register allocator. Each unallocated location has | 77 // allocated by a register allocator. Each unallocated location has |
75 // a policy that specifies what kind of location is suitable. Payload | 78 // a policy that specifies what kind of location is suitable. Payload |
76 // contains register allocation policy. | 79 // contains register allocation policy. |
77 kUnallocated = 2, | 80 kUnallocated = 3, |
78 | 81 |
79 // Spill slots allocated by the register allocator. Payload contains | 82 // Spill slots allocated by the register allocator. Payload contains |
80 // a spill index. | 83 // a spill index. |
81 kStackSlot = 3, // Word size slot. | 84 kStackSlot = 4, // Word size slot. |
82 kDoubleStackSlot = 4, // 64bit stack slot. | 85 kDoubleStackSlot = 7, // 64bit stack slot. |
83 kQuadStackSlot = 8, // 128bit stack slot. | 86 kQuadStackSlot = 11, // 128bit stack slot. |
84 | 87 |
85 // Register location represents a fixed register. Payload contains | 88 // Register location represents a fixed register. Payload contains |
86 // register code. | 89 // register code. |
87 kRegister = 6, | 90 kRegister = 8, |
88 | 91 |
89 // FpuRegister location represents a fixed fpu register. Payload contains | 92 // FpuRegister location represents a fixed fpu register. Payload contains |
90 // its code. | 93 // its code. |
91 kFpuRegister = 7, | 94 kFpuRegister = 12, |
92 }; | 95 }; |
93 | 96 |
94 Location() : value_(kInvalidLocation) { | 97 Location() : value_(kInvalidLocation) { |
| 98 // Verify that non-tagged location kinds do not interfere with location tags |
| 99 // (kConstantTag and kPairLocationTag). |
| 100 COMPILE_ASSERT(((kInvalid & kLocationTagMask) != kConstantTag), |
| 101 invalid_conflicts_with_constant_tag); |
| 102 COMPILE_ASSERT(((kInvalid & kLocationTagMask) != kPairLocationTag), |
| 103 invalid_conflicts_with_pair_tag); |
| 104 |
| 105 COMPILE_ASSERT(((kUnallocated & kLocationTagMask) != kConstantTag), |
| 106 unallocated_conflicts_with_constant_tag); |
| 107 COMPILE_ASSERT(((kUnallocated & kLocationTagMask) != kPairLocationTag), |
| 108 unallocated_conflicts_with_pair_tag); |
| 109 |
| 110 COMPILE_ASSERT(((kStackSlot & kLocationTagMask) != kConstantTag), |
| 111 stackslot_conflicts_with_constant_tag); |
| 112 COMPILE_ASSERT(((kStackSlot & kLocationTagMask) != kPairLocationTag), |
| 113 stackslot_conflicts_with_pair_tag); |
| 114 |
| 115 COMPILE_ASSERT(((kDoubleStackSlot & kLocationTagMask) != kConstantTag), |
| 116 doublestackslot_conflicts_with_constant_tag); |
| 117 COMPILE_ASSERT(((kDoubleStackSlot & kLocationTagMask) != kPairLocationTag), |
| 118 doublestackslot_conflicts_with_pair_tag); |
| 119 |
| 120 COMPILE_ASSERT(((kQuadStackSlot & kLocationTagMask) != kConstantTag), |
| 121 quadstackslot_conflicts_with_constant_tag); |
| 122 COMPILE_ASSERT(((kQuadStackSlot & kLocationTagMask) != kPairLocationTag), |
| 123 quadstackslot_conflicts_with_pair_tag); |
| 124 |
| 125 COMPILE_ASSERT(((kRegister & kLocationTagMask) != kConstantTag), |
| 126 register_conflicts_with_constant_tag); |
| 127 COMPILE_ASSERT(((kRegister & kLocationTagMask) != kPairLocationTag), |
| 128 register_conflicts_with_pair_tag); |
| 129 |
| 130 COMPILE_ASSERT(((kFpuRegister & kLocationTagMask) != kConstantTag), |
| 131 fpuregister_conflicts_with_constant_tag); |
| 132 COMPILE_ASSERT(((kFpuRegister & kLocationTagMask) != kPairLocationTag), |
| 133 fpuregister_conflicts_with_pair_tag); |
| 134 |
| 135 // Verify tags and tagmask. |
| 136 COMPILE_ASSERT(((kConstantTag & kLocationTagMask) == kConstantTag), |
| 137 bad_constant_tag); |
| 138 |
| 139 COMPILE_ASSERT(((kPairLocationTag & kLocationTagMask) == kPairLocationTag), |
| 140 bad_pair_tag); |
| 141 |
95 ASSERT(IsInvalid()); | 142 ASSERT(IsInvalid()); |
96 } | 143 } |
97 | 144 |
98 Location(const Location& other) : ValueObject(), value_(other.value_) { } | 145 Location(const Location& other) : ValueObject(), value_(other.value_) { } |
99 | 146 |
100 Location& operator=(const Location& other) { | 147 Location& operator=(const Location& other) { |
101 value_ = other.value_; | 148 value_ = other.value_; |
102 return *this; | 149 return *this; |
103 } | 150 } |
104 | 151 |
105 bool IsInvalid() const { | 152 bool IsInvalid() const { |
106 return value_ == kInvalidLocation; | 153 return value_ == kInvalidLocation; |
107 } | 154 } |
108 | 155 |
109 // Constants. | 156 // Constants. |
110 bool IsConstant() const { | 157 bool IsConstant() const { |
111 ASSERT((kConstant & kConstantMask) == kConstant); | 158 return (value_ & kLocationTagMask) == kConstantTag; |
112 return (value_ & kConstantMask) == kConstant; | |
113 } | 159 } |
114 | 160 |
115 static Location Constant(const Object& obj) { | 161 static Location Constant(const Object& obj) { |
116 Location loc(reinterpret_cast<uword>(&obj) | kConstant); | 162 Location loc(reinterpret_cast<uword>(&obj) | kConstantTag); |
117 ASSERT(&obj == &loc.constant()); | 163 ASSERT(&obj == &loc.constant()); |
118 return loc; | 164 return loc; |
119 } | 165 } |
120 | 166 |
121 const Object& constant() const { | 167 const Object& constant() const { |
122 ASSERT(IsConstant()); | 168 ASSERT(IsConstant()); |
123 return *reinterpret_cast<const Object*>(value_ & ~kConstantMask); | 169 return *reinterpret_cast<const Object*>(value_ & ~kLocationTagMask); |
124 } | 170 } |
125 | 171 |
| 172 bool IsPairLocation() const { |
| 173 return (value_ & kLocationTagMask) == kPairLocationTag; |
| 174 } |
| 175 |
| 176 static Location Pair(Location first, Location second); |
| 177 |
| 178 PairLocation* AsPairLocation() const; |
| 179 |
126 // Unallocated locations. | 180 // Unallocated locations. |
127 enum Policy { | 181 enum Policy { |
128 kAny, | 182 kAny, |
129 kPrefersRegister, | 183 kPrefersRegister, |
130 kRequiresRegister, | 184 kRequiresRegister, |
131 kRequiresFpuRegister, | 185 kRequiresFpuRegister, |
132 kWritableRegister, | 186 kWritableRegister, |
133 kSameAsFirstInput, | 187 kSameAsFirstInput, |
134 }; | 188 }; |
135 | 189 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 bool IsFpuRegister() const { | 258 bool IsFpuRegister() const { |
205 return kind() == kFpuRegister; | 259 return kind() == kFpuRegister; |
206 } | 260 } |
207 | 261 |
208 FpuRegister fpu_reg() const { | 262 FpuRegister fpu_reg() const { |
209 ASSERT(IsFpuRegister()); | 263 ASSERT(IsFpuRegister()); |
210 return static_cast<FpuRegister>(payload()); | 264 return static_cast<FpuRegister>(payload()); |
211 } | 265 } |
212 | 266 |
213 static bool IsMachineRegisterKind(Kind kind) { | 267 static bool IsMachineRegisterKind(Kind kind) { |
214 return (kind & kMachineRegisterMask) == kMachineRegister; | 268 return (kind == kRegister) || (kind == kFpuRegister); |
215 } | 269 } |
216 | 270 |
217 static Location MachineRegisterLocation(Kind kind, | 271 static Location MachineRegisterLocation(Kind kind, |
218 intptr_t reg) { | 272 intptr_t reg) { |
219 if (kind == kRegister) { | 273 if (kind == kRegister) { |
220 return RegisterLocation(static_cast<Register>(reg)); | 274 return RegisterLocation(static_cast<Register>(reg)); |
221 } else { | 275 } else { |
222 ASSERT(kind == kFpuRegister); | 276 ASSERT(kind == kFpuRegister); |
223 return FpuRegisterLocation(static_cast<FpuRegister>(reg)); | 277 return FpuRegisterLocation(static_cast<FpuRegister>(reg)); |
224 } | 278 } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 static const intptr_t kStackIndexBias = | 384 static const intptr_t kStackIndexBias = |
331 static_cast<intptr_t>(1) << (kBitsForPayload - 1); | 385 static_cast<intptr_t>(1) << (kBitsForPayload - 1); |
332 | 386 |
333 // Location either contains kind and payload fields or a tagged handle for | 387 // Location either contains kind and payload fields or a tagged handle for |
334 // a constant locations. Values of enumeration Kind are selected in such a | 388 // a constant locations. Values of enumeration Kind are selected in such a |
335 // way that none of them can be interpreted as a kConstant tag. | 389 // way that none of them can be interpreted as a kConstant tag. |
336 uword value_; | 390 uword value_; |
337 }; | 391 }; |
338 | 392 |
339 | 393 |
| 394 class PairLocation : public ZoneAllocated { |
| 395 public: |
| 396 PairLocation() { |
| 397 for (intptr_t i = 0; i < kPairLength; i++) { |
| 398 ASSERT(locations_[i].IsInvalid()); |
| 399 } |
| 400 } |
| 401 |
| 402 intptr_t length() const { return kPairLength; } |
| 403 |
| 404 Location At(intptr_t i) const { |
| 405 ASSERT(i >= 0); |
| 406 ASSERT(i < kPairLength); |
| 407 return locations_[i]; |
| 408 } |
| 409 |
| 410 void SetAt(intptr_t i, Location loc) { |
| 411 ASSERT(i >= 0); |
| 412 ASSERT(i < kPairLength); |
| 413 locations_[i] = loc; |
| 414 } |
| 415 |
| 416 Location* SlotAt(intptr_t i) { |
| 417 ASSERT(i >= 0); |
| 418 ASSERT(i < kPairLength); |
| 419 return &locations_[i]; |
| 420 } |
| 421 |
| 422 private: |
| 423 static const intptr_t kPairLength = 2; |
| 424 Location locations_[kPairLength]; |
| 425 }; |
| 426 |
| 427 |
340 class RegisterSet : public ValueObject { | 428 class RegisterSet : public ValueObject { |
341 public: | 429 public: |
342 RegisterSet() : cpu_registers_(0), fpu_registers_(0) { | 430 RegisterSet() : cpu_registers_(0), fpu_registers_(0) { |
343 ASSERT(kNumberOfCpuRegisters <= (kWordSize * kBitsPerByte)); | 431 ASSERT(kNumberOfCpuRegisters <= (kWordSize * kBitsPerByte)); |
344 ASSERT(kNumberOfFpuRegisters <= (kWordSize * kBitsPerByte)); | 432 ASSERT(kNumberOfFpuRegisters <= (kWordSize * kBitsPerByte)); |
345 } | 433 } |
346 | 434 |
347 | 435 |
348 void Add(Location loc) { | 436 void Add(Location loc) { |
349 if (loc.IsRegister()) { | 437 if (loc.IsRegister()) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 | 537 |
450 Location out(intptr_t index) const { | 538 Location out(intptr_t index) const { |
451 return output_locations_[index]; | 539 return output_locations_[index]; |
452 } | 540 } |
453 | 541 |
454 Location* out_slot(intptr_t index) { | 542 Location* out_slot(intptr_t index) { |
455 return &output_locations_[index]; | 543 return &output_locations_[index]; |
456 } | 544 } |
457 | 545 |
458 void set_out(intptr_t index, Location loc) { | 546 void set_out(intptr_t index, Location loc) { |
459 ASSERT(!always_calls() || (loc.IsMachineRegister() || loc.IsInvalid())); | 547 ASSERT(!always_calls() || |
| 548 (loc.IsMachineRegister() || loc.IsInvalid() || |
| 549 loc.IsPairLocation())); |
460 output_locations_[index] = loc; | 550 output_locations_[index] = loc; |
461 } | 551 } |
462 | 552 |
463 BitmapBuilder* stack_bitmap() const { return stack_bitmap_; } | 553 BitmapBuilder* stack_bitmap() const { return stack_bitmap_; } |
464 | 554 |
465 bool always_calls() const { | 555 bool always_calls() const { |
466 return contains_call_ == kCall; | 556 return contains_call_ == kCall; |
467 } | 557 } |
468 | 558 |
469 bool can_call() { | 559 bool can_call() { |
(...skipping 18 matching lines...) Expand all Loading... |
488 BitmapBuilder* stack_bitmap_; | 578 BitmapBuilder* stack_bitmap_; |
489 | 579 |
490 const ContainsCall contains_call_; | 580 const ContainsCall contains_call_; |
491 RegisterSet live_registers_; | 581 RegisterSet live_registers_; |
492 }; | 582 }; |
493 | 583 |
494 | 584 |
495 } // namespace dart | 585 } // namespace dart |
496 | 586 |
497 #endif // VM_LOCATIONS_H_ | 587 #endif // VM_LOCATIONS_H_ |
OLD | NEW |