blob: 31d0a49fa55f7ddbf7d07290d59a656a52b9f9e9 [file] [log] [blame]
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001// 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
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000030#include "codegen-inl.h"
31#include "register-allocator-inl.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000032#include "virtual-frame-inl.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000036
37// -------------------------------------------------------------------------
38// Result implementation.
39
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000040
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000041Result::Result(Register reg, TypeInfo info) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000042 ASSERT(reg.is_valid() && !RegisterAllocator::IsReserved(reg));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000043 CodeGeneratorScope::Current()->allocator()->Use(reg);
ager@chromium.org5c838252010-02-19 08:53:10 +000044 value_ = TypeField::encode(REGISTER)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000045 | TypeInfoField::encode(info.ToInt())
ager@chromium.org5c838252010-02-19 08:53:10 +000046 | DataField::encode(reg.code_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000047}
48
49
kasperl@chromium.orge959c182009-07-27 08:59:04 +000050Result::ZoneObjectList* Result::ConstantList() {
51 static ZoneObjectList list(10);
52 return &list;
53}
54
55
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000056// -------------------------------------------------------------------------
57// RegisterAllocator implementation.
58
59
60Result RegisterAllocator::AllocateWithoutSpilling() {
61 // Return the first free register, if any.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000062 int num = registers_.ScanForFreeRegister();
63 if (num == RegisterAllocator::kInvalidRegister) {
64 return Result();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000065 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000066 return Result(RegisterAllocator::ToRegister(num));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000067}
68
69
70Result RegisterAllocator::Allocate() {
71 Result result = AllocateWithoutSpilling();
72 if (!result.is_valid()) {
73 // Ask the current frame to spill a register.
74 ASSERT(cgen_->has_valid_frame());
75 Register free_reg = cgen_->frame()->SpillAnyRegister();
76 if (free_reg.is_valid()) {
77 ASSERT(!is_used(free_reg));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000078 return Result(free_reg);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000079 }
80 }
81 return result;
82}
83
84
85Result RegisterAllocator::Allocate(Register target) {
86 // If the target is not referenced, it can simply be allocated.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000087 if (!is_used(RegisterAllocator::ToNumber(target))) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000088 return Result(target);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000089 }
90 // If the target is only referenced in the frame, it can be spilled and
91 // then allocated.
92 ASSERT(cgen_->has_valid_frame());
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000093 if (cgen_->frame()->is_used(RegisterAllocator::ToNumber(target)) &&
94 count(target) == 1) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000095 cgen_->frame()->Spill(target);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000096 ASSERT(!is_used(RegisterAllocator::ToNumber(target)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000097 return Result(target);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000098 }
99 // Otherwise (if it's referenced outside the frame) we cannot allocate it.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000100 return Result();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000101}
102
103
104} } // namespace v8::internal