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_arm.h"
18 
19 #include "base/globals.h"
20 
21 namespace art {
22 namespace arm {
23 
24 // Returns true if this managed-register overlaps the other managed-register.
Overlaps(const ArmManagedRegister & other) const25 bool ArmManagedRegister::Overlaps(const ArmManagedRegister& other) const {
26   if (IsNoRegister() || other.IsNoRegister()) return false;
27   if (Equals(other)) return true;
28   if (IsRegisterPair()) {
29     Register low = AsRegisterPairLow();
30     Register high = AsRegisterPairHigh();
31     return ArmManagedRegister::FromCoreRegister(low).Overlaps(other) ||
32         ArmManagedRegister::FromCoreRegister(high).Overlaps(other);
33   }
34   if (IsOverlappingDRegister()) {
35     if (other.IsDRegister()) return Equals(other);
36     if (other.IsSRegister()) {
37       SRegister low = AsOverlappingDRegisterLow();
38       SRegister high = AsOverlappingDRegisterHigh();
39       SRegister other_sreg = other.AsSRegister();
40       return (low == other_sreg) || (high == other_sreg);
41     }
42     return false;
43   }
44   if (other.IsRegisterPair() || other.IsOverlappingDRegister()) {
45     return other.Overlaps(*this);
46   }
47   return false;
48 }
49 
50 
AllocIdLow() const51 int ArmManagedRegister::AllocIdLow() const {
52   CHECK(IsOverlappingDRegister() || IsRegisterPair());
53   const int r = RegId() - (kNumberOfCoreRegIds + kNumberOfSRegIds);
54   int low;
55   if (r < kNumberOfOverlappingDRegIds) {
56     CHECK(IsOverlappingDRegister());
57     low = (r * 2) + kNumberOfCoreRegIds;  // Return a SRegister.
58   } else {
59     CHECK(IsRegisterPair());
60     low = (r - kNumberOfDRegIds) * 2;  // Return a Register.
61     if (low > 6) {
62       // we didn't got a pair higher than R6_R7, must be the dalvik special case
63       low = 1;
64     }
65   }
66   return low;
67 }
68 
69 
AllocIdHigh() const70 int ArmManagedRegister::AllocIdHigh() const {
71   return AllocIdLow() + 1;
72 }
73 
74 
Print(std::ostream & os) const75 void ArmManagedRegister::Print(std::ostream& os) const {
76   if (!IsValidManagedRegister()) {
77     os << "No Register";
78   } else if (IsCoreRegister()) {
79     os << "Core: " << static_cast<int>(AsCoreRegister());
80   } else if (IsRegisterPair()) {
81     os << "Pair: " << static_cast<int>(AsRegisterPairLow()) << ", "
82        << static_cast<int>(AsRegisterPairHigh());
83   } else if (IsSRegister()) {
84     os << "SRegister: " << static_cast<int>(AsSRegister());
85   } else if (IsDRegister()) {
86     os << "DRegister: " << static_cast<int>(AsDRegister());
87   } else {
88     os << "??: " << RegId();
89   }
90 }
91 
operator <<(std::ostream & os,const ArmManagedRegister & reg)92 std::ostream& operator<<(std::ostream& os, const ArmManagedRegister& reg) {
93   reg.Print(os);
94   return os;
95 }
96 
operator <<(std::ostream & os,const RegisterPair & r)97 std::ostream& operator<<(std::ostream& os, const RegisterPair& r) {
98   os << ArmManagedRegister::FromRegisterPair(r);
99   return os;
100 }
101 
102 }  // namespace arm
103 }  // namespace art
104