blob: e5fbfd2bdeb3185e196e0d746d9d801633100167 [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
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 "runtime_support.h"
18
19namespace art {
20
21// Temporary debugging hook for compiler.
22extern void DebugMe(Method* method, uint32_t info) {
23 LOG(INFO) << "DebugMe";
24 if (method != NULL) {
25 LOG(INFO) << PrettyMethod(method);
26 }
27 LOG(INFO) << "Info: " << info;
28}
29
30// Return value helper for jobject return types
31extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
32 return thread->DecodeJObject(obj);
33}
34
35extern void* FindNativeMethod(Thread* thread) {
36 DCHECK(Thread::Current() == thread);
37
38 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
39 DCHECK(method != NULL);
40
41 // Lookup symbol address for method, on failure we'll return NULL with an
42 // exception set, otherwise we return the address of the method we found.
43 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
44 if (native_code == NULL) {
45 DCHECK(thread->IsExceptionPending());
46 return NULL;
47 } else {
48 // Register so that future calls don't come here
49 method->RegisterNative(native_code);
50 return native_code;
51 }
52}
53
54// Called by generated call to throw an exception
55extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
56 /*
57 * exception may be NULL, in which case this routine should
58 * throw NPE. NOTE: this is a convenience for generated code,
59 * which previously did the null check inline and constructed
60 * and threw a NPE if NULL. This routine responsible for setting
61 * exception_ in thread and delivering the exception.
62 */
63 // Place a special frame at the TOS that will save all callee saves
64 *sp = Runtime::Current()->GetCalleeSaveMethod();
65 thread->SetTopOfStack(sp, 0);
66 if (exception == NULL) {
67 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
68 } else {
69 thread->SetException(exception);
70 }
71 thread->DeliverException();
72}
73
74// Deliver an exception that's pending on thread helping set up a callee save frame on the way
75extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
76 *sp = Runtime::Current()->GetCalleeSaveMethod();
77 thread->SetTopOfStack(sp, 0);
78 thread->DeliverException();
79}
80
81// Called by generated call to throw a NPE exception
82extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
83 // Place a special frame at the TOS that will save all callee saves
84 *sp = Runtime::Current()->GetCalleeSaveMethod();
85 thread->SetTopOfStack(sp, 0);
86 thread->ThrowNewException("Ljava/lang/NullPointerException;", "unexpected null reference");
87 thread->DeliverException();
88}
89
90// Called by generated call to throw an arithmetic divide by zero exception
91extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
92 // Place a special frame at the TOS that will save all callee saves
93 *sp = Runtime::Current()->GetCalleeSaveMethod();
94 thread->SetTopOfStack(sp, 0);
95 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
96 thread->DeliverException();
97}
98
99// Called by generated call to throw an arithmetic divide by zero exception
100extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
101 // Place a special frame at the TOS that will save all callee saves
102 *sp = Runtime::Current()->GetCalleeSaveMethod();
103 thread->SetTopOfStack(sp, 0);
104 thread->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
105 "length=%d; index=%d", limit, index);
106 thread->DeliverException();
107}
108
109// Called by the AbstractMethodError stub (not runtime support)
110extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
111 *sp = Runtime::Current()->GetCalleeSaveMethod();
112 thread->SetTopOfStack(sp, 0);
113 thread->ThrowNewException("Ljava/lang/AbstractMethodError;",
114 "abstract method \"%s\"",
115 PrettyMethod(method).c_str());
116 thread->DeliverException();
117}
118
119extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
120 // Place a special frame at the TOS that will save all callee saves
121 Runtime* runtime = Runtime::Current();
122 *sp = runtime->GetCalleeSaveMethod();
123 thread->SetTopOfStack(sp, 0);
124 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
125 thread->ThrowNewException("Ljava/lang/StackOverflowError;",
126 "stack size %zdkb; default stack size: %zdkb",
127 thread->GetStackSize() / KB, runtime->GetDefaultStackSize() / KB);
128 thread->ResetDefaultStackEnd(); // Return to default stack size
129 thread->DeliverException();
130}
131
132extern "C" void artThrowVerificationErrorFromCode(int32_t src1, int32_t ref, Thread* thread, Method** sp) {
133 // Place a special frame at the TOS that will save all callee saves
134 Runtime* runtime = Runtime::Current();
135 *sp = runtime->GetCalleeSaveMethod();
136 thread->SetTopOfStack(sp, 0);
137 LOG(WARNING) << "TODO: verifcation error detail message. src1=" << src1 << " ref=" << ref;
138 thread->ThrowNewException("Ljava/lang/VerifyError;",
139 "TODO: verifcation error detail message. src1=%d; ref=%d", src1, ref);
140 thread->DeliverException();
141}
142
143extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
144 // Place a special frame at the TOS that will save all callee saves
145 Runtime* runtime = Runtime::Current();
146 *sp = runtime->GetCalleeSaveMethod();
147 thread->SetTopOfStack(sp, 0);
148 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
149 thread->ThrowNewException("Ljava/lang/InternalError;", "errnum=%d", errnum);
150 thread->DeliverException();
151}
152
153extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
154 // Place a special frame at the TOS that will save all callee saves
155 Runtime* runtime = Runtime::Current();
156 *sp = runtime->GetCalleeSaveMethod();
157 thread->SetTopOfStack(sp, 0);
158 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
159 thread->ThrowNewException("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
160 thread->DeliverException();
161}
162
163extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* thread, Method** sp) {
164 // Place a special frame at the TOS that will save all callee saves
165 Runtime* runtime = Runtime::Current();
166 *sp = runtime->GetCalleeSaveMethod();
167 thread->SetTopOfStack(sp, 0);
168 LOG(WARNING) << "TODO: no such method exception detail message. method_idx=" << method_idx;
169 thread->ThrowNewException("Ljava/lang/NoSuchMethodError;", "method_idx=%d", method_idx);
170 thread->DeliverException();
171}
172
173extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
174 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
175 // Place a special frame at the TOS that will save all callee saves
176 Runtime* runtime = Runtime::Current();
177 *sp = runtime->GetCalleeSaveMethod();
178 thread->SetTopOfStack(sp, 0);
179 thread->ThrowNewException("Ljava/lang/NegativeArraySizeException;", "%d", size);
180 thread->DeliverException();
181}
182
183// TODO: placeholder. Helper function to type
184Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) {
185 /*
186 * Should initialize & fix up method->dex_cache_resolved_types_[].
187 * Returns initialized type. Does not return normally if an exception
188 * is thrown, but instead initiates the catch. Should be similar to
189 * ClassLinker::InitializeStaticStorageFromCode.
190 */
191 UNIMPLEMENTED(FATAL);
192 return NULL;
193}
194
195// TODO: placeholder. Helper function to resolve virtual method
196void ResolveMethodFromCode(Method* method, uint32_t method_idx) {
197 /*
198 * Slow-path handler on invoke virtual method path in which
199 * base method is unresolved at compile-time. Doesn't need to
200 * return anything - just either ensure that
201 * method->dex_cache_resolved_methods_(method_idx) != NULL or
202 * throw and unwind. The caller will restart call sequence
203 * from the beginning.
204 */
205}
206
207// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
208// cannot be resolved, throw an error. If it can, use it to create an instance.
209extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method) {
210 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
211 if (klass == NULL) {
212 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
213 if (klass == NULL) {
214 DCHECK(Thread::Current()->IsExceptionPending());
215 return NULL; // Failure
216 }
217 }
218 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(klass, true)) {
219 DCHECK(Thread::Current()->IsExceptionPending());
220 return NULL; // Failure
221 }
222 return klass->AllocObject();
223}
224
225// Helper function to alloc array for OP_FILLED_NEW_ARRAY
226extern "C" Array* artCheckAndArrayAllocFromCode(uint32_t type_idx, Method* method,
227 int32_t component_count) {
228 if (component_count < 0) {
229 Thread::Current()->ThrowNewException("Ljava/lang/NegativeArraySizeException;", "%d",
230 component_count);
231 return NULL; // Failure
232 }
233 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
234 if (klass == NULL) { // Not in dex cache so try to resolve
235 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
236 if (klass == NULL) { // Error
237 DCHECK(Thread::Current()->IsExceptionPending());
238 return NULL; // Failure
239 }
240 }
241 if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) {
242 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
243 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
244 "Bad filled array request for type %s",
245 PrettyDescriptor(klass->GetDescriptor()).c_str());
246 } else {
247 Thread::Current()->ThrowNewException("Ljava/lang/InternalError;",
248 "Found type %s; filled-new-array not implemented for anything but \'int\'",
249 PrettyDescriptor(klass->GetDescriptor()).c_str());
250 }
251 return NULL; // Failure
252 } else {
253 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
254 return Array::Alloc(klass, component_count);
255 }
256}
257
258// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
259// it cannot be resolved, throw an error. If it can, use it to create an array.
260extern "C" Array* artArrayAllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
261 if (component_count < 0) {
262 Thread::Current()->ThrowNewException("Ljava/lang/NegativeArraySizeException;", "%d",
263 component_count);
264 return NULL; // Failure
265 }
266 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
267 if (klass == NULL) { // Not in dex cache so try to resolve
268 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
269 if (klass == NULL) { // Error
270 DCHECK(Thread::Current()->IsExceptionPending());
271 return NULL; // Failure
272 }
273 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
274 }
275 return Array::Alloc(klass, component_count);
276}
277
278// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
279extern "C" int artCheckCastFromCode(const Class* a, const Class* b) {
280 DCHECK(a->IsClass()) << PrettyClass(a);
281 DCHECK(b->IsClass()) << PrettyClass(b);
282 if (b->IsAssignableFrom(a)) {
283 return 0; // Success
284 } else {
285 Thread::Current()->ThrowNewException("Ljava/lang/ClassCastException;",
286 "%s cannot be cast to %s",
287 PrettyDescriptor(a->GetDescriptor()).c_str(),
288 PrettyDescriptor(b->GetDescriptor()).c_str());
289 return -1; // Failure
290 }
291}
292
293// Tests whether 'element' can be assigned into an array of type 'array_class'.
294// Returns 0 on success and -1 if an exception is pending.
295extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class) {
296 DCHECK(array_class != NULL);
297 // element can't be NULL as we catch this is screened in runtime_support
298 Class* element_class = element->GetClass();
299 Class* component_type = array_class->GetComponentType();
300 if (component_type->IsAssignableFrom(element_class)) {
301 return 0; // Success
302 } else {
303 Thread::Current()->ThrowNewException("Ljava/lang/ArrayStoreException;",
304 "Cannot store an object of type %s in to an array of type %s",
305 PrettyDescriptor(element_class->GetDescriptor()).c_str(),
306 PrettyDescriptor(array_class->GetDescriptor()).c_str());
307 return -1; // Failure
308 }
309}
310
311extern "C" int artUnlockObjectFromCode(Thread* thread, Object* obj) {
312 DCHECK(obj != NULL); // Assumed to have been checked before entry
313 return obj->MonitorExit(thread) ? 0 /* Success */ : -1 /* Failure */;
314}
315
316void LockObjectFromCode(Thread* thread, Object* obj) {
317 DCHECK(obj != NULL); // Assumed to have been checked before entry
318 obj->MonitorEnter(thread);
319 DCHECK(thread->HoldsLock(obj));
320 // Only possible exception is NPE and is handled before entry
321 DCHECK(!thread->IsExceptionPending());
322}
323
324extern "C" void artCheckSuspendFromCode(Thread* thread) {
325 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
326}
327
328/*
329 * Fill the array with predefined constant values, throwing exceptions if the array is null or
330 * not of sufficient length.
331 *
332 * NOTE: When dealing with a raw dex file, the data to be copied uses
333 * little-endian ordering. Require that oat2dex do any required swapping
334 * so this routine can get by with a memcpy().
335 *
336 * Format of the data:
337 * ushort ident = 0x0300 magic value
338 * ushort width width of each element in the table
339 * uint size number of elements in the table
340 * ubyte data[size*width] table of data values (may contain a single-byte
341 * padding at the end)
342 */
343extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table) {
344 DCHECK_EQ(table[0], 0x0300);
345 if (array == NULL) {
346 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
347 "null array in fill array");
348 return -1; // Error
349 }
350 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
351 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
352 if (static_cast<int32_t>(size) > array->GetLength()) {
353 Thread::Current()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
354 "failed array fill. length=%d; index=%d",
355 array->GetLength(), size);
356 return -1; // Error
357 }
358 uint16_t width = table[1];
359 uint32_t size_in_bytes = size * width;
360 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
361 return 0; // Success
362}
363
364// See comments in runtime_support_asm.S
365extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
366 Object* this_object ,
367 Method* caller_method) {
368 Thread* thread = Thread::Current();
369 if (this_object == NULL) {
370 thread->ThrowNewException("Ljava/lang/NullPointerException;",
371 "null receiver during interface dispatch");
372 return 0;
373 }
374 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
375 Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
376 if (interface_method == NULL) {
377 // Could not resolve interface method. Throw error and unwind
378 CHECK(thread->IsExceptionPending());
379 return 0;
380 }
381 Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
382 if (method == NULL) {
383 CHECK(thread->IsExceptionPending());
384 return 0;
385 }
386 const void* code = method->GetCode();
387
388 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
389 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
390 uint64_t result = ((code_uint << 32) | method_uint);
391 return result;
392}
393
394/*
395 * Float/double conversion requires clamping to min and max of integer form. If
396 * target doesn't support this normally, use these.
397 */
398int64_t D2L(double d) {
399 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
400 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
401 if (d >= kMaxLong)
402 return (int64_t)0x7fffffffffffffffULL;
403 else if (d <= kMinLong)
404 return (int64_t)0x8000000000000000ULL;
405 else if (d != d) // NaN case
406 return 0;
407 else
408 return (int64_t)d;
409}
410
411int64_t F2L(float f) {
412 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
413 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
414 if (f >= kMaxLong)
415 return (int64_t)0x7fffffffffffffffULL;
416 else if (f <= kMinLong)
417 return (int64_t)0x8000000000000000ULL;
418 else if (f != f) // NaN case
419 return 0;
420 else
421 return (int64_t)f;
422}
423
424} // namespace art