1 /*
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  */
16 
17 #include "managed_register_mips.h"
18 
19 #include "globals.h"
20 
21 namespace art {
22 namespace mips {
23 
Overlaps(const MipsManagedRegister & other) const24 bool MipsManagedRegister::Overlaps(const MipsManagedRegister& other) const {
25   if (IsNoRegister() || other.IsNoRegister()) return false;
26   CHECK(IsValidManagedRegister());
27   CHECK(other.IsValidManagedRegister());
28   if (Equals(other)) return true;
29   if (IsRegisterPair()) {
30     Register low = AsRegisterPairLow();
31     Register high = AsRegisterPairHigh();
32     return MipsManagedRegister::FromCoreRegister(low).Overlaps(other) ||
33         MipsManagedRegister::FromCoreRegister(high).Overlaps(other);
34   }
35   if (IsOverlappingDRegister()) {
36     if (other.IsDRegister()) return Equals(other);
37     if (other.IsFRegister()) {
38       FRegister low = AsOverlappingDRegisterLow();
39       FRegister high = AsOverlappingDRegisterHigh();
40       FRegister other_freg = other.AsFRegister();
41       return (low == other_freg) || (high == other_freg);
42     }
43     return false;
44   }
45   if (other.IsRegisterPair() || other.IsOverlappingDRegister()) {
46     return other.Overlaps(*this);
47   }
48   return false;
49 }
50 
51 
AllocIdLow() const52 int MipsManagedRegister::AllocIdLow() const {
53   CHECK(IsOverlappingDRegister() || IsRegisterPair());
54   const int r = RegId() - (kNumberOfCoreRegIds + kNumberOfFRegIds);
55   int low;
56   if (r < kNumberOfOverlappingDRegIds) {
57     CHECK(IsOverlappingDRegister());
58     low = (r * 2) + kNumberOfCoreRegIds;  // Return an FRegister.
59   } else {
60     CHECK(IsRegisterPair());
61     low = (r - kNumberOfDRegIds) * 2 + 2;  // Return a Register.
62     if (low >= 24) {
63       // we got a pair higher than S6_S7, must be the dalvik special case
64       low = 5;
65     }
66   }
67   return low;
68 }
69 
70 
AllocIdHigh() const71 int MipsManagedRegister::AllocIdHigh() const {
72   return AllocIdLow() + 1;
73 }
74 
75 
Print(std::ostream & os) const76 void MipsManagedRegister::Print(std::ostream& os) const {
77   if (!IsValidManagedRegister()) {
78     os << "No Register";
79   } else if (IsCoreRegister()) {
80     os << "Core: " << static_cast<int>(AsCoreRegister());
81   } else if (IsRegisterPair()) {
82     os << "Pair: " << AsRegisterPairLow() << ", " << AsRegisterPairHigh();
83   } else if (IsFRegister()) {
84     os << "FRegister: " << static_cast<int>(AsFRegister());
85   } else if (IsDRegister()) {
86     os << "DRegister: " << static_cast<int>(AsDRegister());
87   } else {
88     os << "??: " << RegId();
89   }
90 }
91 
operator <<(std::ostream & os,const MipsManagedRegister & reg)92 std::ostream& operator<<(std::ostream& os, const MipsManagedRegister& reg) {
93   reg.Print(os);
94   return os;
95 }
96 
operator <<(std::ostream & os,const RegisterPair & reg)97 std::ostream& operator<<(std::ostream& os, const RegisterPair& reg) {
98   os << MipsManagedRegister::FromRegisterPair(reg);
99   return os;
100 }
101 
102 }  // namespace mips
103 }  // namespace art
104