OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 17 matching lines...) Expand all Loading... |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "codegen-inl.h" | 30 #include "codegen-inl.h" |
31 #include "register-allocator-inl.h" | 31 #include "register-allocator-inl.h" |
32 | 32 |
33 namespace v8 { namespace internal { | 33 namespace v8 { namespace internal { |
34 | 34 |
35 // ------------------------------------------------------------------------- | 35 // ------------------------------------------------------------------------- |
36 // Result implementation. | 36 // Result implementation. |
37 | 37 |
38 Result::Result(Register reg, CodeGenerator* cgen) | 38 CodeGenerator* Result::cgen_ = NULL; |
39 : static_type_(), | 39 |
40 type_(REGISTER), | 40 |
41 cgen_(cgen) { | 41 Result::Result(Register reg) { |
42 data_.reg_ = reg; | |
43 ASSERT(reg.is_valid()); | 42 ASSERT(reg.is_valid()); |
44 cgen_->allocator()->Use(reg); | 43 cgen_->allocator()->Use(reg); |
| 44 value_ = StaticTypeField::encode(StaticType::UNKNOWN_TYPE) |
| 45 | TypeField::encode(REGISTER) |
| 46 | DataField::encode(reg.code_); |
45 } | 47 } |
46 | 48 |
47 | 49 |
48 Result::Result(Register reg, CodeGenerator* cgen, StaticType static_type) | 50 Result::Result(Register reg, StaticType type) { |
49 : static_type_(static_type), | |
50 type_(REGISTER), | |
51 cgen_(cgen) { | |
52 data_.reg_ = reg; | |
53 ASSERT(reg.is_valid()); | 51 ASSERT(reg.is_valid()); |
54 cgen_->allocator()->Use(reg); | 52 cgen_->allocator()->Use(reg); |
55 } | 53 value_ = StaticTypeField::encode(type.static_type_) |
56 | 54 | TypeField::encode(REGISTER) |
57 | 55 | DataField::encode(reg.code_); |
58 void Result::CopyTo(Result* destination) const { | |
59 destination->static_type_ = static_type_; | |
60 destination->type_ = type(); | |
61 destination->cgen_ = cgen_; | |
62 | |
63 if (is_register()) { | |
64 destination->data_.reg_ = reg(); | |
65 cgen_->allocator()->Use(reg()); | |
66 } else if (is_constant()) { | |
67 destination->data_.handle_ = data_.handle_; | |
68 } else { | |
69 ASSERT(!is_valid()); | |
70 } | |
71 } | 56 } |
72 | 57 |
73 | 58 |
74 // ------------------------------------------------------------------------- | 59 // ------------------------------------------------------------------------- |
75 // RegisterAllocator implementation. | 60 // RegisterAllocator implementation. |
76 | 61 |
77 | 62 |
78 Result RegisterAllocator::AllocateWithoutSpilling() { | 63 Result RegisterAllocator::AllocateWithoutSpilling() { |
79 // Return the first free register, if any. | 64 // Return the first free register, if any. |
80 int free_reg = registers_.ScanForFreeRegister(); | 65 int free_reg = registers_.ScanForFreeRegister(); |
81 if (free_reg < kNumRegisters) { | 66 if (free_reg < kNumRegisters) { |
82 Register free_result = { free_reg }; | 67 Register free_result = { free_reg }; |
83 return Result(free_result, cgen_); | 68 return Result(free_result); |
84 } | 69 } |
85 return Result(cgen_); | 70 return Result(); |
86 } | 71 } |
87 | 72 |
88 | 73 |
89 Result RegisterAllocator::Allocate() { | 74 Result RegisterAllocator::Allocate() { |
90 Result result = AllocateWithoutSpilling(); | 75 Result result = AllocateWithoutSpilling(); |
91 if (!result.is_valid()) { | 76 if (!result.is_valid()) { |
92 // Ask the current frame to spill a register. | 77 // Ask the current frame to spill a register. |
93 ASSERT(cgen_->has_valid_frame()); | 78 ASSERT(cgen_->has_valid_frame()); |
94 Register free_reg = cgen_->frame()->SpillAnyRegister(); | 79 Register free_reg = cgen_->frame()->SpillAnyRegister(); |
95 if (free_reg.is_valid()) { | 80 if (free_reg.is_valid()) { |
96 ASSERT(!is_used(free_reg)); | 81 ASSERT(!is_used(free_reg)); |
97 return Result(free_reg, cgen_); | 82 return Result(free_reg); |
98 } | 83 } |
99 } | 84 } |
100 return result; | 85 return result; |
101 } | 86 } |
102 | 87 |
103 | 88 |
104 Result RegisterAllocator::Allocate(Register target) { | 89 Result RegisterAllocator::Allocate(Register target) { |
105 // If the target is not referenced, it can simply be allocated. | 90 // If the target is not referenced, it can simply be allocated. |
106 if (!is_used(target)) { | 91 if (!is_used(target)) { |
107 return Result(target, cgen_); | 92 return Result(target); |
108 } | 93 } |
109 // If the target is only referenced in the frame, it can be spilled and | 94 // If the target is only referenced in the frame, it can be spilled and |
110 // then allocated. | 95 // then allocated. |
111 ASSERT(cgen_->has_valid_frame()); | 96 ASSERT(cgen_->has_valid_frame()); |
112 if (cgen_->frame()->is_used(target) && count(target) == 1) { | 97 if (cgen_->frame()->is_used(target) && count(target) == 1) { |
113 cgen_->frame()->Spill(target); | 98 cgen_->frame()->Spill(target); |
114 ASSERT(!is_used(target)); | 99 ASSERT(!is_used(target)); |
115 return Result(target, cgen_); | 100 return Result(target); |
116 } | 101 } |
117 // Otherwise (if it's referenced outside the frame) we cannot allocate it. | 102 // Otherwise (if it's referenced outside the frame) we cannot allocate it. |
118 return Result(cgen_); | 103 return Result(); |
119 } | 104 } |
120 | 105 |
121 | 106 |
122 } } // namespace v8::internal | 107 } } // namespace v8::internal |
OLD | NEW |