blob: 453cab79b48fb6191a1fc9435cfdde0c20399cea [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersb033c752011-07-20 12:22:35 -070016
Ian Rogers2c8f6532011-09-02 17:16:34 -070017#include "managed_register_x86.h"
18
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "globals.h"
20#include "calling_convention.h"
Ian Rogersb033c752011-07-20 12:22:35 -070021
22namespace art {
Ian Rogers2c8f6532011-09-02 17:16:34 -070023namespace x86 {
Ian Rogersb033c752011-07-20 12:22:35 -070024
25// These cpu registers are never available for allocation.
26static const Register kReservedCpuRegistersArray[] = { EBP, ESP };
27
28
29// We reduce the number of available registers for allocation in debug-code
30// mode in order to increase register pressure.
31
32// We need all registers for caching.
33static const int kNumberOfAvailableCpuRegisters = kNumberOfCpuRegisters;
34static const int kNumberOfAvailableXmmRegisters = kNumberOfXmmRegisters;
35static const int kNumberOfAvailableRegisterPairs = kNumberOfRegisterPairs;
36
37
38// Define register pairs.
39// This list must be kept in sync with the RegisterPair enum.
40#define REGISTER_PAIR_LIST(P) \
41 P(EAX, EDX) \
42 P(EAX, ECX) \
43 P(EAX, EBX) \
44 P(EAX, EDI) \
45 P(EDX, ECX) \
46 P(EDX, EBX) \
47 P(EDX, EDI) \
48 P(ECX, EBX) \
49 P(ECX, EDI) \
50 P(EBX, EDI)
51
52
53struct RegisterPairDescriptor {
54 RegisterPair reg; // Used to verify that the enum is in sync.
55 Register low;
56 Register high;
57};
58
59
60static const RegisterPairDescriptor kRegisterPairs[] = {
61#define REGISTER_PAIR_ENUMERATION(low, high) { low##_##high, low, high },
62 REGISTER_PAIR_LIST(REGISTER_PAIR_ENUMERATION)
63#undef REGISTER_PAIR_ENUMERATION
64};
65
66std::ostream& operator<<(std::ostream& os, const RegisterPair& reg) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070067 os << X86ManagedRegister::FromRegisterPair(reg);
Ian Rogersb033c752011-07-20 12:22:35 -070068 return os;
69}
70
Ian Rogers2c8f6532011-09-02 17:16:34 -070071bool X86ManagedRegister::Overlaps(const X86ManagedRegister& other) const {
Ian Rogersb033c752011-07-20 12:22:35 -070072 if (IsNoRegister() || other.IsNoRegister()) return false;
73 CHECK(IsValidManagedRegister());
74 CHECK(other.IsValidManagedRegister());
75 if (Equals(other)) return true;
76 if (IsRegisterPair()) {
77 Register low = AsRegisterPairLow();
78 Register high = AsRegisterPairHigh();
Ian Rogers2c8f6532011-09-02 17:16:34 -070079 return X86ManagedRegister::FromCpuRegister(low).Overlaps(other) ||
80 X86ManagedRegister::FromCpuRegister(high).Overlaps(other);
Ian Rogersb033c752011-07-20 12:22:35 -070081 }
82 if (other.IsRegisterPair()) {
83 return other.Overlaps(*this);
84 }
85 return false;
86}
87
88
Ian Rogers2c8f6532011-09-02 17:16:34 -070089int X86ManagedRegister::AllocIdLow() const {
Ian Rogersb033c752011-07-20 12:22:35 -070090 CHECK(IsRegisterPair());
91 const int r = RegId() - (kNumberOfCpuRegIds + kNumberOfXmmRegIds +
92 kNumberOfX87RegIds);
93 CHECK_EQ(r, kRegisterPairs[r].reg);
94 return kRegisterPairs[r].low;
95}
96
97
Ian Rogers2c8f6532011-09-02 17:16:34 -070098int X86ManagedRegister::AllocIdHigh() const {
Ian Rogersb033c752011-07-20 12:22:35 -070099 CHECK(IsRegisterPair());
100 const int r = RegId() - (kNumberOfCpuRegIds + kNumberOfXmmRegIds +
101 kNumberOfX87RegIds);
102 CHECK_EQ(r, kRegisterPairs[r].reg);
103 return kRegisterPairs[r].high;
104}
105
106
Ian Rogers2c8f6532011-09-02 17:16:34 -0700107void X86ManagedRegister::Print(std::ostream& os) const {
Ian Rogersb033c752011-07-20 12:22:35 -0700108 if (!IsValidManagedRegister()) {
109 os << "No Register";
110 } else if (IsXmmRegister()) {
111 os << "XMM: " << static_cast<int>(AsXmmRegister());
112 } else if (IsX87Register()) {
113 os << "X87: " << static_cast<int>(AsX87Register());
114 } else if (IsCpuRegister()) {
115 os << "CPU: " << static_cast<int>(AsCpuRegister());
116 } else if (IsRegisterPair()) {
117 os << "Pair: " << AsRegisterPairLow() << ", " << AsRegisterPairHigh();
118 } else {
119 os << "??: " << RegId();
120 }
121}
122
Ian Rogers2c8f6532011-09-02 17:16:34 -0700123std::ostream& operator<<(std::ostream& os, const X86ManagedRegister& reg) {
Ian Rogersb033c752011-07-20 12:22:35 -0700124 reg.Print(os);
125 return os;
126}
127
Ian Rogers2c8f6532011-09-02 17:16:34 -0700128} // namespace x86
Ian Rogersb033c752011-07-20 12:22:35 -0700129} // namespace art