blob: 23699549b74569d6378b67f3a3ae2c0c4579588b [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
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700160bool Method::HasSameNameAndDescriptor(const Method* that) const {
161 return (this->GetName()->Equals(that->GetName()) &&
162 this->GetDescriptor()->Equals(that->GetDescriptor()));
163}
164
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700165Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
166 const StringPiece& descriptor) {
167 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700168 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700169 if (method->GetName()->Equals(name) &&
170 method->GetDescriptor()->Equals(descriptor)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700171 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700172 }
173 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700174 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700175}
176
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700177Method* Class::FindDirectMethod(const StringPiece& name,
178 const StringPiece& descriptor) {
179 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
180 Method* method = klass->FindDeclaredDirectMethod(name, descriptor);
181 if (method != NULL) {
182 return method;
183 }
184 }
185 return NULL;
186}
187
188Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
189 const StringPiece& descriptor) {
190 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700191 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700192 if (method->GetName()->Equals(name) &&
193 method->GetDescriptor()->Equals(descriptor)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700194 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700195 }
196 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700197 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700198}
199
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700200Method* Class::FindVirtualMethod(const StringPiece& name,
201 const StringPiece& descriptor) {
202 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
203 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
204 if (method != NULL) {
205 return method;
206 }
207 }
208 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209}
210
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700211// TODO: get global references for these
212Class* String::java_lang_String_ = NULL;
213Class* String::char_array_ = NULL;
214
215void String::InitClasses(Class* java_lang_String, Class* char_array) {
216 java_lang_String_ = java_lang_String;
217 char_array_ = char_array;
218}
219
Elliott Hughes1f359b02011-07-17 14:27:17 -0700220static const char* kClassStatusNames[] = {
221 "Error",
222 "NotReady",
223 "Idx",
224 "Loaded",
225 "Resolved",
226 "Verifying",
227 "Verified",
228 "Initializing",
229 "Initialized"
230};
231std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
232 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700233 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -0700234 } else {
Ian Rogersb033c752011-07-20 12:22:35 -0700235 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -0700236 }
237 return os;
238}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700240} // namespace art