blob: fbcfa8b830adf7016cba44d9561c936942f69bb8 [file] [log] [blame]
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "object.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <string.h>
Ian Rogersdf20fe02011-07-20 20:34:16 -07006#include <algorithm>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007
8#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07009#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070011#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "dex_file.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070013
14namespace art {
15
Ian Rogersb033c752011-07-20 12:22:35 -070016bool Class::IsInSamePackage(const StringPiece& descriptor1,
17 const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070018 size_t i = 0;
19 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
20 ++i;
21 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -070022 if (descriptor1.find('/', i) != StringPiece::npos ||
23 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070024 return false;
25 } else {
26 return true;
27 }
28}
29
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070030#if 0
Ian Rogersb033c752011-07-20 12:22:35 -070031bool Class::IsInSamePackage(const StringPiece& descriptor1,
32 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -070034 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -070035 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
36 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
38}
39#endif
40
Carl Shapiro894d0fa2011-06-30 14:48:49 -070041bool Class::IsInSamePackage(const Class* that) const {
42 const Class* klass1 = this;
43 const Class* klass2 = that;
44 if (klass1 == klass2) {
45 return true;
46 }
47 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070049 return false;
50 }
51 // Arrays are in the same package when their element classes are.
52 if (klass1->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -070054 }
55 if (klass2->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -070057 }
58 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070059 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -070060}
61
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062uint32_t Method::NumArgRegisters() {
63 CHECK(shorty_ != NULL);
64 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -070065 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -070066 char ch = shorty_[i];
67 if (ch == 'D' || ch == 'J') {
68 num_registers += 2;
69 } else {
70 num_registers += 1;
71 }
72 }
73 return num_registers;
74}
75
Ian Rogersb033c752011-07-20 12:22:35 -070076// The number of reference arguments to this method including implicit this
77// pointer
78size_t Method::NumReferenceArgs() const {
79 size_t result = IsStatic() ? 0 : 1;
80 for (int i = 1; i < shorty_.length(); i++) {
81 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
82 result++;
83 }
84 }
85 return result;
86}
87
88// The number of long or double arguments
89size_t Method::NumLongOrDoubleArgs() const {
90 size_t result = 0;
91 for (int i = 1; i < shorty_.length(); i++) {
92 if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) {
93 result++;
94 }
95 }
96 return result;
97}
98
99// The number of reference arguments to this method before the given parameter
100// index
101size_t Method::NumReferenceArgsBefore(unsigned int param) const {
102 CHECK_LT(param, NumArgs());
103 unsigned int result = IsStatic() ? 0 : 1;
104 for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) &&
105 (i < (param + 1)); i++) {
106 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
107 result++;
108 }
109 }
110 return result;
111}
112
113// Is the given method parameter a reference?
114bool Method::IsParamAReference(unsigned int param) const {
115 CHECK_LT(param, NumArgs());
116 if (IsStatic()) {
117 param++; // 0th argument must skip return value at start of the shorty
118 } else if (param == 0) {
119 return true; // this argument
120 }
121 return ((shorty_[param] == 'L') || (shorty_[param] == '['));
122}
123
124// Is the given method parameter a long or double?
125bool Method::IsParamALongOrDouble(unsigned int param) const {
126 CHECK_LT(param, NumArgs());
127 if (IsStatic()) {
128 param++; // 0th argument must skip return value at start of the shorty
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700129 } else if (param == 0) {
130 return false; // this argument
Ian Rogersb033c752011-07-20 12:22:35 -0700131 }
132 return (shorty_[param] == 'J') || (shorty_[param] == 'D');
133}
134
Ian Rogersdf20fe02011-07-20 20:34:16 -0700135static size_t ShortyCharToSize(char x) {
136 switch (x) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700137 case 'V': return 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700138 case '[': return kPointerSize;
139 case 'L': return kPointerSize;
140 case 'D': return 8;
141 case 'J': return 8;
142 default: return 4;
143 }
144}
145
Ian Rogersdf20fe02011-07-20 20:34:16 -0700146size_t Method::ParamSize(unsigned int param) const {
147 CHECK_LT(param, NumArgs());
148 if (IsStatic()) {
149 param++; // 0th argument must skip return value at start of the shorty
150 } else if (param == 0) {
151 return kPointerSize; // this argument
152 }
153 return ShortyCharToSize(shorty_[param]);
154}
155
156size_t Method::ReturnSize() const {
157 return ShortyCharToSize(shorty_[0]);
158}
159
Ian Rogersb033c752011-07-20 12:22:35 -0700160Method* Class::FindDirectMethod(const StringPiece& name) const {
161 Method* result = NULL;
162 for (size_t i = 0; i < NumDirectMethods(); i++) {
163 Method* method = GetDirectMethod(i);
164 if (method->GetName().compare(name) == 0) {
165 result = method;
166 break;
167 }
168 }
169 return result;
170}
171
172Method* Class::FindVirtualMethod(const StringPiece& name) const {
173 Method* result = NULL;
174 for (size_t i = 0; i < NumVirtualMethods(); i++) {
175 Method* method = GetVirtualMethod(i);
176 if (method->GetName().compare(name) == 0) {
177 result = method;
178 break;
179 }
180 }
181 return result;
182}
183
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700184Method* Class::FindDirectMethodLocally(const StringPiece& name,
185 const StringPiece& descriptor) const {
186 return NULL; // TODO
187}
188
Elliott Hughes1f359b02011-07-17 14:27:17 -0700189static const char* kClassStatusNames[] = {
190 "Error",
191 "NotReady",
192 "Idx",
193 "Loaded",
194 "Resolved",
195 "Verifying",
196 "Verified",
197 "Initializing",
198 "Initialized"
199};
200std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
201 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
202 os << kClassStatusNames[rhs - 1];
203 } else {
Ian Rogersb033c752011-07-20 12:22:35 -0700204 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -0700205 }
206 return os;
207}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700208
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700209} // namespace art