OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 12 matching lines...) Expand all Loading... |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_REGISTER_ALLOCATOR_H_ | 28 #ifndef V8_REGISTER_ALLOCATOR_H_ |
29 #define V8_REGISTER_ALLOCATOR_H_ | 29 #define V8_REGISTER_ALLOCATOR_H_ |
30 | 30 |
31 #include "macro-assembler.h" | 31 #include "macro-assembler.h" |
32 | 32 |
| 33 #if V8_TARGET_ARCH_IA32 |
| 34 #include "ia32/register-allocator-ia32.h" |
| 35 #elif V8_TARGET_ARCH_X64 |
| 36 #include "x64/register-allocator-x64.h" |
| 37 #elif V8_TARGET_ARCH_ARM |
| 38 #include "arm/register-allocator-arm.h" |
| 39 #else |
| 40 #error Unsupported target architecture. |
| 41 #endif |
| 42 |
33 namespace v8 { | 43 namespace v8 { |
34 namespace internal { | 44 namespace internal { |
35 | 45 |
36 | 46 |
37 // ------------------------------------------------------------------------- | 47 // ------------------------------------------------------------------------- |
38 // StaticType | 48 // StaticType |
39 // | 49 // |
40 // StaticType represent the type of an expression or a word at runtime. | 50 // StaticType represent the type of an expression or a word at runtime. |
41 // The types are ordered by knowledge, so that if a value can come about | 51 // The types are ordered by knowledge, so that if a value can come about |
42 // in more than one way, and there are different static types inferred | 52 // in more than one way, and there are different static types inferred |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 class RegisterFile BASE_EMBEDDED { | 244 class RegisterFile BASE_EMBEDDED { |
235 public: | 245 public: |
236 RegisterFile() { Reset(); } | 246 RegisterFile() { Reset(); } |
237 | 247 |
238 void Reset() { | 248 void Reset() { |
239 for (int i = 0; i < kNumRegisters; i++) { | 249 for (int i = 0; i < kNumRegisters; i++) { |
240 ref_counts_[i] = 0; | 250 ref_counts_[i] = 0; |
241 } | 251 } |
242 } | 252 } |
243 | 253 |
244 // Predicates and accessors for the reference counts. The versions | 254 // Predicates and accessors for the reference counts. |
245 // that take a register code rather than a register are for | 255 bool is_used(int num) { |
246 // convenience in loops over the register codes. | 256 ASSERT(0 <= num && num < kNumRegisters); |
247 bool is_used(int reg_code) const { return ref_counts_[reg_code] > 0; } | 257 return ref_counts_[num] > 0; |
248 bool is_used(Register reg) const { return is_used(reg.code()); } | 258 } |
249 int count(int reg_code) const { return ref_counts_[reg_code]; } | 259 |
250 int count(Register reg) const { return count(reg.code()); } | 260 int count(int num) { |
| 261 ASSERT(0 <= num && num < kNumRegisters); |
| 262 return ref_counts_[num]; |
| 263 } |
251 | 264 |
252 // Record a use of a register by incrementing its reference count. | 265 // Record a use of a register by incrementing its reference count. |
253 void Use(Register reg) { | 266 void Use(int num) { |
254 ref_counts_[reg.code()]++; | 267 ASSERT(0 <= num && num < kNumRegisters); |
| 268 ref_counts_[num]++; |
255 } | 269 } |
256 | 270 |
257 // Record that a register will no longer be used by decrementing its | 271 // Record that a register will no longer be used by decrementing its |
258 // reference count. | 272 // reference count. |
259 void Unuse(Register reg) { | 273 void Unuse(int num) { |
260 ASSERT(!reg.is(no_reg)); | 274 ASSERT(is_used(num)); |
261 ASSERT(is_used(reg.code())); | 275 ref_counts_[num]--; |
262 ref_counts_[reg.code()]--; | |
263 } | 276 } |
264 | 277 |
265 // Copy the reference counts from this register file to the other. | 278 // Copy the reference counts from this register file to the other. |
266 void CopyTo(RegisterFile* other) { | 279 void CopyTo(RegisterFile* other) { |
267 for (int i = 0; i < kNumRegisters; i++) { | 280 for (int i = 0; i < kNumRegisters; i++) { |
268 other->ref_counts_[i] = ref_counts_[i]; | 281 other->ref_counts_[i] = ref_counts_[i]; |
269 } | 282 } |
270 } | 283 } |
271 | 284 |
272 private: | 285 private: |
| 286 static const int kNumRegisters = RegisterAllocatorConstants::kNumRegisters; |
| 287 |
273 int ref_counts_[kNumRegisters]; | 288 int ref_counts_[kNumRegisters]; |
274 | 289 |
275 // Very fast inlined loop to find a free register. | 290 // Very fast inlined loop to find a free register. Used in |
276 // Used in RegisterAllocator::AllocateWithoutSpilling. | 291 // RegisterAllocator::AllocateWithoutSpilling. Returns |
277 // Returns kNumRegisters if no free register found. | 292 // kInvalidRegister if no free register found. |
278 inline int ScanForFreeRegister() { | 293 int ScanForFreeRegister() { |
279 int i = 0; | 294 for (int i = 0; i < RegisterAllocatorConstants::kNumRegisters; i++) { |
280 for (; i < kNumRegisters ; ++i) { | 295 if (!is_used(i)) return i; |
281 if (ref_counts_[i] == 0) break; | |
282 } | 296 } |
283 return i; | 297 return RegisterAllocatorConstants::kInvalidRegister; |
284 } | 298 } |
285 | 299 |
286 friend class RegisterAllocator; | 300 friend class RegisterAllocator; |
287 }; | 301 }; |
288 | 302 |
289 | 303 |
290 // ------------------------------------------------------------------------- | 304 // ------------------------------------------------------------------------- |
291 // Register allocator | 305 // Register allocator |
292 // | 306 // |
293 | 307 |
294 class RegisterAllocator BASE_EMBEDDED { | 308 class RegisterAllocator BASE_EMBEDDED { |
295 public: | 309 public: |
| 310 static const int kNumRegisters = |
| 311 RegisterAllocatorConstants::kNumRegisters; |
| 312 static const int kInvalidRegister = |
| 313 RegisterAllocatorConstants::kInvalidRegister; |
| 314 |
296 explicit RegisterAllocator(CodeGenerator* cgen) : cgen_(cgen) {} | 315 explicit RegisterAllocator(CodeGenerator* cgen) : cgen_(cgen) {} |
297 | 316 |
298 // A register file with each of the reserved registers counted once. | 317 // True if the register is reserved by the code generator, false if it |
299 static RegisterFile Reserved(); | 318 // can be freely used by the allocator Defined in the |
| 319 // platform-specific XXX-inl.h files.. |
| 320 static inline bool IsReserved(Register reg); |
300 | 321 |
301 // Unuse all the reserved registers in a register file. | 322 // Convert between (unreserved) assembler registers and allocator |
302 static void UnuseReserved(RegisterFile* register_file); | 323 // numbers. Defined in the platform-specific XXX-inl.h files. |
303 | 324 static inline int ToNumber(Register reg); |
304 // True if the register is reserved by the code generator, false if it | 325 static inline Register ToRegister(int num); |
305 // can be freely used by the allocator. | |
306 static bool IsReserved(int reg_code); | |
307 static bool IsReserved(Register reg) { return IsReserved(reg); } | |
308 | 326 |
309 // Predicates and accessors for the registers' reference counts. | 327 // Predicates and accessors for the registers' reference counts. |
310 bool is_used(int reg_code) const { return registers_.is_used(reg_code); } | 328 bool is_used(int num) { return registers_.is_used(num); } |
311 bool is_used(Register reg) const { return registers_.is_used(reg.code()); } | 329 bool is_used(Register reg) { return registers_.is_used(ToNumber(reg)); } |
312 int count(int reg_code) const { return registers_.count(reg_code); } | 330 |
313 int count(Register reg) const { return registers_.count(reg.code()); } | 331 int count(int num) { return registers_.count(num); } |
| 332 int count(Register reg) { return registers_.count(ToNumber(reg)); } |
314 | 333 |
315 // Explicitly record a reference to a register. | 334 // Explicitly record a reference to a register. |
316 void Use(Register reg) { registers_.Use(reg); } | 335 void Use(int num) { registers_.Use(num); } |
| 336 void Use(Register reg) { registers_.Use(ToNumber(reg)); } |
317 | 337 |
318 // Explicitly record that a register will no longer be used. | 338 // Explicitly record that a register will no longer be used. |
319 void Unuse(Register reg) { registers_.Unuse(reg); } | 339 void Unuse(int num) { registers_.Unuse(num); } |
| 340 void Unuse(Register reg) { registers_.Unuse(ToNumber(reg)); } |
| 341 |
| 342 // Reset the register reference counts to free all non-reserved registers. |
| 343 void Reset() { registers_.Reset(); } |
320 | 344 |
321 // Initialize the register allocator for entry to a JS function. On | 345 // Initialize the register allocator for entry to a JS function. On |
322 // entry, the registers used by the JS calling convention are | 346 // entry, the (non-reserved) registers used by the JS calling |
323 // externally referenced (ie, outside the virtual frame); and the | 347 // convention are referenced and the other (non-reserved) registers |
324 // other registers are free. | 348 // are free. |
325 void Initialize(); | 349 inline void Initialize(); |
326 | |
327 // Reset the register reference counts to free all non-reserved registers. | |
328 // A frame-external reference is kept to each of the reserved registers. | |
329 void Reset(); | |
330 | 350 |
331 // Allocate a free register and return a register result if possible or | 351 // Allocate a free register and return a register result if possible or |
332 // fail and return an invalid result. | 352 // fail and return an invalid result. |
333 Result Allocate(); | 353 Result Allocate(); |
334 | 354 |
335 // Allocate a specific register if possible, spilling it from the frame if | 355 // Allocate a specific register if possible, spilling it from the |
336 // necessary, or else fail and return an invalid result. | 356 // current frame if necessary, or else fail and return an invalid |
| 357 // result. |
337 Result Allocate(Register target); | 358 Result Allocate(Register target); |
338 | 359 |
339 // Allocate a free register without spilling any from the current frame or | 360 // Allocate a free register without spilling any from the current |
340 // fail and return an invalid result. | 361 // frame or fail and return an invalid result. |
341 Result AllocateWithoutSpilling(); | 362 Result AllocateWithoutSpilling(); |
342 | 363 |
343 // Allocate a free byte register without spilling any from the | 364 // Allocate a free byte register without spilling any from the current |
344 // current frame or fail and return an invalid result. | 365 // frame or fail and return an invalid result. |
345 Result AllocateByteRegisterWithoutSpilling(); | 366 Result AllocateByteRegisterWithoutSpilling(); |
346 | 367 |
347 // Copy the internal state to a register file, to be restored later by | 368 // Copy the internal state to a register file, to be restored later by |
348 // RestoreFrom. | 369 // RestoreFrom. |
349 void SaveTo(RegisterFile* register_file) { | 370 void SaveTo(RegisterFile* register_file) { |
350 registers_.CopyTo(register_file); | 371 registers_.CopyTo(register_file); |
351 } | 372 } |
352 | 373 |
| 374 // Restore the internal state. |
353 void RestoreFrom(RegisterFile* register_file) { | 375 void RestoreFrom(RegisterFile* register_file) { |
354 register_file->CopyTo(®isters_); | 376 register_file->CopyTo(®isters_); |
355 } | 377 } |
356 | 378 |
357 private: | 379 private: |
358 CodeGenerator* cgen_; | 380 CodeGenerator* cgen_; |
359 RegisterFile registers_; | 381 RegisterFile registers_; |
360 }; | 382 }; |
361 | 383 |
362 } } // namespace v8::internal | 384 } } // namespace v8::internal |
363 | 385 |
364 #endif // V8_REGISTER_ALLOCATOR_H_ | 386 #endif // V8_REGISTER_ALLOCATOR_H_ |
OLD | NEW |