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