blob: ca1fdea0d103e76a3b834aa64e4da45448ab4e6c [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);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070086 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -070087 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);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700104 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", "length=%d; index=%d",
105 limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700106 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);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700113 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", "abstract method \"%s\"",
114 PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700115 thread->DeliverException();
116}
117
118extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
119 // Place a special frame at the TOS that will save all callee saves
120 Runtime* runtime = Runtime::Current();
121 *sp = runtime->GetCalleeSaveMethod();
122 thread->SetTopOfStack(sp, 0);
123 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700124 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
125 "stack size %zdkb; default stack size: %zdkb",
126 thread->GetStackSize() / KB, runtime->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700127 thread->ResetDefaultStackEnd(); // Return to default stack size
128 thread->DeliverException();
129}
130
131extern "C" void artThrowVerificationErrorFromCode(int32_t src1, int32_t ref, Thread* thread, Method** sp) {
132 // Place a special frame at the TOS that will save all callee saves
133 Runtime* runtime = Runtime::Current();
134 *sp = runtime->GetCalleeSaveMethod();
135 thread->SetTopOfStack(sp, 0);
136 LOG(WARNING) << "TODO: verifcation error detail message. src1=" << src1 << " ref=" << ref;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700137 thread->ThrowNewExceptionF("Ljava/lang/VerifyError;",
138 "TODO: verification error detail message. src1=%d; ref=%d", src1, ref);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700139 thread->DeliverException();
140}
141
142extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
143 // Place a special frame at the TOS that will save all callee saves
144 Runtime* runtime = Runtime::Current();
145 *sp = runtime->GetCalleeSaveMethod();
146 thread->SetTopOfStack(sp, 0);
147 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700148 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700149 thread->DeliverException();
150}
151
152extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
153 // Place a special frame at the TOS that will save all callee saves
154 Runtime* runtime = Runtime::Current();
155 *sp = runtime->GetCalleeSaveMethod();
156 thread->SetTopOfStack(sp, 0);
157 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700158 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700159 thread->DeliverException();
160}
161
162extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* thread, Method** sp) {
163 // Place a special frame at the TOS that will save all callee saves
164 Runtime* runtime = Runtime::Current();
165 *sp = runtime->GetCalleeSaveMethod();
166 thread->SetTopOfStack(sp, 0);
167 LOG(WARNING) << "TODO: no such method exception detail message. method_idx=" << method_idx;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700168 thread->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;", "method_idx=%d", method_idx);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700169 thread->DeliverException();
170}
171
172extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
173 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
174 // Place a special frame at the TOS that will save all callee saves
175 Runtime* runtime = Runtime::Current();
176 *sp = runtime->GetCalleeSaveMethod();
177 thread->SetTopOfStack(sp, 0);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700178 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700179 thread->DeliverException();
180}
181
182// TODO: placeholder. Helper function to type
183Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) {
184 /*
185 * Should initialize & fix up method->dex_cache_resolved_types_[].
186 * Returns initialized type. Does not return normally if an exception
187 * is thrown, but instead initiates the catch. Should be similar to
188 * ClassLinker::InitializeStaticStorageFromCode.
189 */
190 UNIMPLEMENTED(FATAL);
191 return NULL;
192}
193
194// TODO: placeholder. Helper function to resolve virtual method
195void ResolveMethodFromCode(Method* method, uint32_t method_idx) {
196 /*
197 * Slow-path handler on invoke virtual method path in which
198 * base method is unresolved at compile-time. Doesn't need to
199 * return anything - just either ensure that
200 * method->dex_cache_resolved_methods_(method_idx) != NULL or
201 * throw and unwind. The caller will restart call sequence
202 * from the beginning.
203 */
204}
205
206// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
207// cannot be resolved, throw an error. If it can, use it to create an instance.
208extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method) {
209 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
210 if (klass == NULL) {
211 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
212 if (klass == NULL) {
213 DCHECK(Thread::Current()->IsExceptionPending());
214 return NULL; // Failure
215 }
216 }
217 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(klass, true)) {
218 DCHECK(Thread::Current()->IsExceptionPending());
219 return NULL; // Failure
220 }
221 return klass->AllocObject();
222}
223
224// Helper function to alloc array for OP_FILLED_NEW_ARRAY
225extern "C" Array* artCheckAndArrayAllocFromCode(uint32_t type_idx, Method* method,
226 int32_t component_count) {
227 if (component_count < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700228 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700229 component_count);
230 return NULL; // Failure
231 }
232 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
233 if (klass == NULL) { // Not in dex cache so try to resolve
234 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
235 if (klass == NULL) { // Error
236 DCHECK(Thread::Current()->IsExceptionPending());
237 return NULL; // Failure
238 }
239 }
240 if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) {
241 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700242 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700243 "Bad filled array request for type %s",
244 PrettyDescriptor(klass->GetDescriptor()).c_str());
245 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700246 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700247 "Found type %s; filled-new-array not implemented for anything but \'int\'",
248 PrettyDescriptor(klass->GetDescriptor()).c_str());
249 }
250 return NULL; // Failure
251 } else {
252 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
253 return Array::Alloc(klass, component_count);
254 }
255}
256
257// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
258// it cannot be resolved, throw an error. If it can, use it to create an array.
259extern "C" Array* artArrayAllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
260 if (component_count < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700261 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700262 component_count);
263 return NULL; // Failure
264 }
265 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
266 if (klass == NULL) { // Not in dex cache so try to resolve
267 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
268 if (klass == NULL) { // Error
269 DCHECK(Thread::Current()->IsExceptionPending());
270 return NULL; // Failure
271 }
272 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
273 }
274 return Array::Alloc(klass, component_count);
275}
276
277// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
278extern "C" int artCheckCastFromCode(const Class* a, const Class* b) {
279 DCHECK(a->IsClass()) << PrettyClass(a);
280 DCHECK(b->IsClass()) << PrettyClass(b);
281 if (b->IsAssignableFrom(a)) {
282 return 0; // Success
283 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700284 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700285 "%s cannot be cast to %s",
286 PrettyDescriptor(a->GetDescriptor()).c_str(),
287 PrettyDescriptor(b->GetDescriptor()).c_str());
288 return -1; // Failure
289 }
290}
291
292// Tests whether 'element' can be assigned into an array of type 'array_class'.
293// Returns 0 on success and -1 if an exception is pending.
294extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class) {
295 DCHECK(array_class != NULL);
296 // element can't be NULL as we catch this is screened in runtime_support
297 Class* element_class = element->GetClass();
298 Class* component_type = array_class->GetComponentType();
299 if (component_type->IsAssignableFrom(element_class)) {
300 return 0; // Success
301 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700302 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700303 "Cannot store an object of type %s in to an array of type %s",
304 PrettyDescriptor(element_class->GetDescriptor()).c_str(),
305 PrettyDescriptor(array_class->GetDescriptor()).c_str());
306 return -1; // Failure
307 }
308}
309
310extern "C" int artUnlockObjectFromCode(Thread* thread, Object* obj) {
311 DCHECK(obj != NULL); // Assumed to have been checked before entry
312 return obj->MonitorExit(thread) ? 0 /* Success */ : -1 /* Failure */;
313}
314
315void LockObjectFromCode(Thread* thread, Object* obj) {
316 DCHECK(obj != NULL); // Assumed to have been checked before entry
317 obj->MonitorEnter(thread);
318 DCHECK(thread->HoldsLock(obj));
319 // Only possible exception is NPE and is handled before entry
320 DCHECK(!thread->IsExceptionPending());
321}
322
323extern "C" void artCheckSuspendFromCode(Thread* thread) {
324 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
325}
326
327/*
328 * Fill the array with predefined constant values, throwing exceptions if the array is null or
329 * not of sufficient length.
330 *
331 * NOTE: When dealing with a raw dex file, the data to be copied uses
332 * little-endian ordering. Require that oat2dex do any required swapping
333 * so this routine can get by with a memcpy().
334 *
335 * Format of the data:
336 * ushort ident = 0x0300 magic value
337 * ushort width width of each element in the table
338 * uint size number of elements in the table
339 * ubyte data[size*width] table of data values (may contain a single-byte
340 * padding at the end)
341 */
342extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table) {
343 DCHECK_EQ(table[0], 0x0300);
344 if (array == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700345 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
346 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700347 return -1; // Error
348 }
349 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
350 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
351 if (static_cast<int32_t>(size) > array->GetLength()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700352 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
353 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700354 return -1; // Error
355 }
356 uint16_t width = table[1];
357 uint32_t size_in_bytes = size * width;
358 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
359 return 0; // Success
360}
361
362// See comments in runtime_support_asm.S
363extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
364 Object* this_object ,
365 Method* caller_method) {
366 Thread* thread = Thread::Current();
367 if (this_object == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700368 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
369 "null receiver during interface dispatch");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700370 return 0;
371 }
372 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
373 Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
374 if (interface_method == NULL) {
375 // Could not resolve interface method. Throw error and unwind
376 CHECK(thread->IsExceptionPending());
377 return 0;
378 }
379 Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
380 if (method == NULL) {
381 CHECK(thread->IsExceptionPending());
382 return 0;
383 }
384 const void* code = method->GetCode();
385
386 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
387 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
388 uint64_t result = ((code_uint << 32) | method_uint);
389 return result;
390}
391
392/*
393 * Float/double conversion requires clamping to min and max of integer form. If
394 * target doesn't support this normally, use these.
395 */
396int64_t D2L(double d) {
397 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
398 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
399 if (d >= kMaxLong)
400 return (int64_t)0x7fffffffffffffffULL;
401 else if (d <= kMinLong)
402 return (int64_t)0x8000000000000000ULL;
403 else if (d != d) // NaN case
404 return 0;
405 else
406 return (int64_t)d;
407}
408
409int64_t F2L(float f) {
410 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
411 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
412 if (f >= kMaxLong)
413 return (int64_t)0x7fffffffffffffffULL;
414 else if (f <= kMinLong)
415 return (int64_t)0x8000000000000000ULL;
416 else if (f != f) // NaN case
417 return 0;
418 else
419 return (int64_t)f;
420}
421
422} // namespace art