1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28 #include "v8.h"
29
30 #include "codegen-inl.h"
31 #include "register-allocator-inl.h"
32
33 namespace v8 {
34 namespace internal {
35
36 // -------------------------------------------------------------------------
37 // Result implementation.
38
39
Result(Register reg)40 Result::Result(Register reg) {
41 ASSERT(reg.is_valid() && !RegisterAllocator::IsReserved(reg));
42 CodeGeneratorScope::Current()->allocator()->Use(reg);
43 value_ = TypeField::encode(REGISTER) | DataField::encode(reg.code_);
44 }
45
46
ConstantList()47 Result::ZoneObjectList* Result::ConstantList() {
48 static ZoneObjectList list(10);
49 return &list;
50 }
51
52
53 // -------------------------------------------------------------------------
54 // RegisterAllocator implementation.
55
56
AllocateWithoutSpilling()57 Result RegisterAllocator::AllocateWithoutSpilling() {
58 // Return the first free register, if any.
59 int num = registers_.ScanForFreeRegister();
60 if (num == RegisterAllocator::kInvalidRegister) {
61 return Result();
62 }
63 return Result(RegisterAllocator::ToRegister(num));
64 }
65
66
Allocate()67 Result RegisterAllocator::Allocate() {
68 Result result = AllocateWithoutSpilling();
69 if (!result.is_valid()) {
70 // Ask the current frame to spill a register.
71 ASSERT(cgen_->has_valid_frame());
72 Register free_reg = cgen_->frame()->SpillAnyRegister();
73 if (free_reg.is_valid()) {
74 ASSERT(!is_used(free_reg));
75 return Result(free_reg);
76 }
77 }
78 return result;
79 }
80
81
Allocate(Register target)82 Result RegisterAllocator::Allocate(Register target) {
83 // If the target is not referenced, it can simply be allocated.
84 if (!is_used(target)) {
85 return Result(target);
86 }
87 // If the target is only referenced in the frame, it can be spilled and
88 // then allocated.
89 ASSERT(cgen_->has_valid_frame());
90 if (cgen_->frame()->is_used(target) && count(target) == 1) {
91 cgen_->frame()->Spill(target);
92 ASSERT(!is_used(target));
93 return Result(target);
94 }
95 // Otherwise (if it's referenced outside the frame) we cannot allocate it.
96 return Result();
97 }
98
99
100 } } // namespace v8::internal
101