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 #ifndef ART_COMPILER_UTILS_MANAGED_REGISTER_H_
18 #define ART_COMPILER_UTILS_MANAGED_REGISTER_H_
19 
20 #include <type_traits>
21 #include <vector>
22 
23 #include "base/value_object.h"
24 
25 namespace art {
26 
27 namespace arm {
28 class ArmManagedRegister;
29 }  // namespace arm
30 namespace arm64 {
31 class Arm64ManagedRegister;
32 }  // namespace arm64
33 
34 namespace x86 {
35 class X86ManagedRegister;
36 }  // namespace x86
37 
38 namespace x86_64 {
39 class X86_64ManagedRegister;
40 }  // namespace x86_64
41 
42 class ManagedRegister : public ValueObject {
43  public:
44   // ManagedRegister is a value class. There exists no method to change the
45   // internal state. We therefore allow a copy constructor and an
46   // assignment-operator.
47   constexpr ManagedRegister(const ManagedRegister& other) = default;
48 
49   ManagedRegister& operator=(const ManagedRegister& other) = default;
50 
51   constexpr arm::ArmManagedRegister AsArm() const;
52   constexpr arm64::Arm64ManagedRegister AsArm64() const;
53   constexpr x86::X86ManagedRegister AsX86() const;
54   constexpr x86_64::X86_64ManagedRegister AsX86_64() const;
55 
56   // It is valid to invoke Equals on and with a NoRegister.
Equals(const ManagedRegister & other)57   constexpr bool Equals(const ManagedRegister& other) const {
58     return id_ == other.id_;
59   }
60 
IsRegister()61   constexpr bool IsRegister() const {
62     return id_ != kNoRegister;
63   }
64 
IsNoRegister()65   constexpr bool IsNoRegister() const {
66     return id_ == kNoRegister;
67   }
68 
NoRegister()69   static constexpr ManagedRegister NoRegister() {
70     return ManagedRegister();
71   }
72 
RegId()73   constexpr int RegId() const { return id_; }
ManagedRegister(int reg_id)74   explicit constexpr ManagedRegister(int reg_id) : id_(reg_id) { }
75 
76  protected:
77   static const int kNoRegister = -1;
78 
ManagedRegister()79   constexpr ManagedRegister() : id_(kNoRegister) { }
80 
81   int id_;
82 };
83 
84 static_assert(std::is_trivially_copyable<ManagedRegister>::value,
85               "ManagedRegister should be trivially copyable");
86 
87 class ManagedRegisterSpill : public ManagedRegister {
88  public:
89   // ManagedRegisterSpill contains information about data type size and location in caller frame
90   // These additional attributes could be defined by calling convention (EntrySpills)
ManagedRegisterSpill(const ManagedRegister & other,uint32_t size,uint32_t spill_offset)91   ManagedRegisterSpill(const ManagedRegister& other, uint32_t size, uint32_t spill_offset)
92       : ManagedRegister(other), size_(size), spill_offset_(spill_offset)  { }
93 
ManagedRegisterSpill(const ManagedRegister & other)94   explicit ManagedRegisterSpill(const ManagedRegister& other)
95       : ManagedRegister(other), size_(-1), spill_offset_(-1) { }
96 
ManagedRegisterSpill(const ManagedRegister & other,int32_t size)97   ManagedRegisterSpill(const ManagedRegister& other, int32_t size)
98       : ManagedRegister(other), size_(size), spill_offset_(-1) { }
99 
getSpillOffset()100   int32_t getSpillOffset() const {
101     return spill_offset_;
102   }
103 
getSize()104   int32_t getSize() const {
105     return size_;
106   }
107 
108  private:
109   int32_t size_;
110   int32_t spill_offset_;
111 };
112 
113 class ManagedRegisterEntrySpills : public std::vector<ManagedRegisterSpill> {
114  public:
115   // The ManagedRegister does not have information about size and offset.
116   // In this case it's size and offset determined by BuildFrame (assembler)
push_back(ManagedRegister x)117   void push_back(ManagedRegister x) {
118     ManagedRegisterSpill spill(x);
119     std::vector<ManagedRegisterSpill>::push_back(spill);
120   }
121 
push_back(ManagedRegister x,int32_t size)122   void push_back(ManagedRegister x, int32_t size) {
123     ManagedRegisterSpill spill(x, size);
124     std::vector<ManagedRegisterSpill>::push_back(spill);
125   }
126 
push_back(ManagedRegisterSpill x)127   void push_back(ManagedRegisterSpill x) {
128     std::vector<ManagedRegisterSpill>::push_back(x);
129   }
130  private:
131 };
132 
133 }  // namespace art
134 
135 #endif  // ART_COMPILER_UTILS_MANAGED_REGISTER_H_
136