blob: fff7b71e4bf491ecfdd5ff93b9bb43588468d006 [file] [log] [blame]
Elliott Hughes0f3c5532012-03-30 14:51:51 -07001/*
2 * Copyright (C) 2012 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 */
buzbee54330722011-08-23 16:46:55 -070016
Ian Rogers7655f292013-07-29 11:07:13 -070017#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
Ian Rogers450dcb52013-09-20 17:36:02 -070019
20#include "base/macros.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070021#include "class_linker.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070022#include "common_throws.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070023#include "dex_file.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070024#include "indirect_reference_table.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070025#include "invoke_type.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070026#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/array.h"
Ian Rogers693ff612013-02-01 10:56:12 -080029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/throwable.h"
Ian Rogers450dcb52013-09-20 17:36:02 -070031#include "object_utils.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070032
Ian Rogers57b86d42012-03-27 16:05:41 -070033#include "thread.h"
34
Shih-wei Liao2d831012011-09-28 22:06:53 -070035namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070036
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037namespace mirror {
Ian Rogers848871b2013-08-05 10:56:33 -070038 class Class;
Brian Carlstromea46f952013-07-30 01:26:50 -070039 class ArtField;
Ian Rogers848871b2013-08-05 10:56:33 -070040 class Object;
41} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070042
Ian Rogers57b86d42012-03-27 16:05:41 -070043// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
44// cannot be resolved, throw an error. If it can, use it to create an instance.
45// When verification/compiler hasn't been able to verify access, optionally perform an access
46// check.
Brian Carlstromea46f952013-07-30 01:26:50 -070047static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 Thread* self,
49 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070052 Runtime* runtime = Runtime::Current();
53 if (UNLIKELY(klass == NULL)) {
54 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
55 if (klass == NULL) {
56 DCHECK(self->IsExceptionPending());
57 return NULL; // Failure
58 }
59 }
60 if (access_check) {
61 if (UNLIKELY(!klass->IsInstantiable())) {
Ian Rogers62d6c772013-02-27 08:32:07 -080062 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
63 self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;",
Ian Rogers57b86d42012-03-27 16:05:41 -070064 PrettyDescriptor(klass).c_str());
65 return NULL; // Failure
66 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -070068 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070069 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070070 return NULL; // Failure
71 }
72 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -070073 if (!klass->IsInitialized() &&
74 !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070075 DCHECK(self->IsExceptionPending());
76 return NULL; // Failure
77 }
Ian Rogers50b35e22012-10-04 10:09:15 -070078 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070079}
80
81// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
82// it cannot be resolved, throw an error. If it can, use it to create an array.
83// When verification/compiler hasn't been able to verify access, optionally perform an access
84// check.
Brian Carlstromea46f952013-07-30 01:26:50 -070085static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 int32_t component_count,
87 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070089 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080090 ThrowNegativeArraySizeException(component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070091 return NULL; // Failure
92 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070094 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
95 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
96 if (klass == NULL) { // Error
97 DCHECK(Thread::Current()->IsExceptionPending());
98 return NULL; // Failure
99 }
100 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
101 }
102 if (access_check) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700104 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700105 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700106 return NULL; // Failure
107 }
108 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700110}
111
Brian Carlstromea46f952013-07-30 01:26:50 -0700112extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 int32_t component_count,
114 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700116
Ian Rogers08f753d2012-08-24 14:35:25 -0700117// Type of find field operation for fast and slow case.
118enum FindFieldType {
119 InstanceObjectRead,
120 InstanceObjectWrite,
121 InstancePrimitiveRead,
122 InstancePrimitiveWrite,
123 StaticObjectRead,
124 StaticObjectWrite,
125 StaticPrimitiveRead,
126 StaticPrimitiveWrite,
127};
128
129// Slow field find that can initialize classes and may throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700130extern mirror::ArtField* FindFieldFromCode(uint32_t field_idx, const mirror::ArtMethod* referrer,
131 Thread* self, FindFieldType type, size_t expected_size,
132 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700134
Ian Rogers08f753d2012-08-24 14:35:25 -0700135// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700136static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
137 const mirror::ArtMethod* referrer,
138 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700140 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700142 if (UNLIKELY(resolved_field == NULL)) {
143 return NULL;
144 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700146 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700147 if (UNLIKELY(!fields_class->IsInitializing())) {
148 return NULL;
149 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700150 // Check for incompatible class change.
151 bool is_primitive;
152 bool is_set;
153 bool is_static;
154 switch (type) {
155 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
156 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
157 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
158 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
159 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
160 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
161 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
162 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700163 default:
164 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
165 is_primitive = true;
166 is_set = true;
167 is_static = true;
168 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700169 }
170 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
171 // Incompatible class change.
172 return NULL;
173 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700175 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
176 !referring_class->CanAccessMember(fields_class,
177 resolved_field->GetAccessFlags()) ||
178 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700179 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700180 return NULL;
181 }
182 FieldHelper fh(resolved_field);
183 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
184 fh.FieldSize() != expected_size)) {
185 return NULL;
186 }
187 return resolved_field;
188}
189
Ian Rogers08f753d2012-08-24 14:35:25 -0700190// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700191static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
192 mirror::Object* this_object,
193 const mirror::ArtMethod* referrer,
194 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700196 bool is_direct = type == kStatic || type == kDirect;
197 if (UNLIKELY(this_object == NULL && !is_direct)) {
198 return NULL;
199 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700200 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700201 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
202 if (UNLIKELY(resolved_method == NULL)) {
203 return NULL;
204 }
205 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700206 // Check for incompatible class change errors and access.
207 bool icce = resolved_method->CheckIncompatibleClassChange(type);
208 if (UNLIKELY(icce)) {
209 return NULL;
210 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
212 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700213 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
214 !referring_class->CanAccessMember(methods_class,
215 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700216 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700217 return NULL;
218 }
219 }
220 if (type == kInterface) { // Most common form of slow path dispatch.
221 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
222 } else if (is_direct) {
223 return resolved_method;
224 } else if (type == kSuper) {
225 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
226 Get(resolved_method->GetMethodIndex());
227 } else {
228 DCHECK(type == kVirtual);
229 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
230 }
231}
232
Brian Carlstromea46f952013-07-30 01:26:50 -0700233extern mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
234 mirror::ArtMethod* referrer,
235 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700237
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700238static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 const mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700240 Thread* self, bool can_run_clinit,
241 bool verify_access)
242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
244 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
245 if (UNLIKELY(klass == NULL)) {
246 CHECK(self->IsExceptionPending());
247 return NULL; // Failure - Indicate to caller to deliver exception
248 }
249 // Perform access check if necessary.
250 mirror::Class* referring_class = referrer->GetDeclaringClass();
251 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
252 ThrowIllegalAccessErrorClass(referring_class, klass);
253 return NULL; // Failure - Indicate to caller to deliver exception
254 }
255 // If we're just implementing const-class, we shouldn't call <clinit>.
256 if (!can_run_clinit) {
257 return klass;
258 }
259 // If we are the <clinit> of this class, just return our storage.
260 //
261 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
262 // running.
263 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
264 return klass;
265 }
266 if (!class_linker->EnsureInitialized(klass, true, true)) {
267 CHECK(self->IsExceptionPending());
268 return NULL; // Failure - Indicate to caller to deliver exception
269 }
270 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
271 return klass;
272}
Ian Rogers57b86d42012-03-27 16:05:41 -0700273
jeffhaod7521322012-11-21 15:38:24 -0800274extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
275
Brian Carlstromea46f952013-07-30 01:26:50 -0700276static inline mirror::String* ResolveStringFromCode(const mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700279 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
280 return class_linker->ResolveString(string_idx, referrer);
281}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700282
TDYa1273d71d802012-08-15 03:47:03 -0700283static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700285 UNLOCK_FUNCTION(monitor_lock_) {
286 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 ThrowLocation saved_throw_location;
TDYa1273d71d802012-08-15 03:47:03 -0700289 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700291 self->ClearException();
292 }
293 // Decode locked object and unlock, before popping local references.
294 self->DecodeJObject(locked)->MonitorExit(self);
295 if (UNLIKELY(self->IsExceptionPending())) {
296 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
297 << saved_exception->Dump()
298 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700300 }
301 // Restore pending exception.
302 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 self->SetException(saved_throw_location, saved_exception);
TDYa1273d71d802012-08-15 03:47:03 -0700304 }
305}
306
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700309 if (o == NULL) {
310 return;
311 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700312 mirror::ArtMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700313 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700315 }
316 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 mirror::Class* return_type = MethodHelper(m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700318
319 if (!o->InstanceOf(return_type)) {
320 JniAbortF(NULL, "attempt to return an instance of %s from %s",
321 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
322 }
323}
324
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800325static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800326 for (;;) {
327 if (thread->ReadFlag(kCheckpointRequest)) {
328 thread->RunCheckpointFunction();
329 thread->AtomicClearFlag(kCheckpointRequest);
330 } else if (thread->ReadFlag(kSuspendRequest)) {
331 thread->FullSuspendCheck();
332 } else {
333 break;
334 }
335 }
336}
337
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800338JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700339 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800340 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800342
Jeff Hao58df3272013-04-22 15:28:53 -0700343// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700344extern "C" void art_quick_deoptimize();
345static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700346 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
347}
348
349// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700350extern "C" void art_quick_instrumentation_entry(void*);
351static inline void* GetQuickInstrumentationEntryPoint() {
352 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700353}
354
355// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700356extern "C" void art_quick_instrumentation_exit();
357static inline uintptr_t GetQuickInstrumentationExitPc() {
358 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
359}
360
Brian Carlstromea46f952013-07-30 01:26:50 -0700361extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700362static inline const void* GetPortableToInterpreterBridge() {
363 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
364}
365
Brian Carlstromea46f952013-07-30 01:26:50 -0700366extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700367static inline const void* GetQuickToInterpreterBridge() {
368 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700369}
370
371// Return address of interpreter stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700372static inline const void* GetCompiledCodeToInterpreterBridge() {
373#if defined(ART_USE_PORTABLE_COMPILER)
374 return GetPortableToInterpreterBridge();
375#else
376 return GetQuickToInterpreterBridge();
377#endif
Jeff Hao58df3272013-04-22 15:28:53 -0700378}
379
Ian Rogers848871b2013-08-05 10:56:33 -0700380
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700381static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
382 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700383}
384
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700385static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
386 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700387}
388
389// Return address of resolution trampoline stub for defined compiler.
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700390static inline const void* GetResolutionTrampoline(ClassLinker* class_linker) {
Jeff Hao58df3272013-04-22 15:28:53 -0700391#if defined(ART_USE_PORTABLE_COMPILER)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700392 return GetPortableResolutionTrampoline(class_linker);
Jeff Hao58df3272013-04-22 15:28:53 -0700393#else
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700394 return GetQuickResolutionTrampoline(class_linker);
Jeff Hao58df3272013-04-22 15:28:53 -0700395#endif
Jeff Hao79fe5392013-04-24 18:41:58 -0700396}
397
Ian Rogers848871b2013-08-05 10:56:33 -0700398extern "C" void art_portable_proxy_invoke_handler();
399static inline const void* GetPortableProxyInvokeHandler() {
400 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700401}
402
Ian Rogers848871b2013-08-05 10:56:33 -0700403extern "C" void art_quick_proxy_invoke_handler();
404static inline const void* GetQuickProxyInvokeHandler() {
405 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700406}
407
Ian Rogers848871b2013-08-05 10:56:33 -0700408static inline const void* GetProxyInvokeHandler() {
Jeff Hao79fe5392013-04-24 18:41:58 -0700409#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers848871b2013-08-05 10:56:33 -0700410 return GetPortableProxyInvokeHandler();
Jeff Hao79fe5392013-04-24 18:41:58 -0700411#else
Ian Rogers848871b2013-08-05 10:56:33 -0700412 return GetQuickProxyInvokeHandler();
Jeff Hao79fe5392013-04-24 18:41:58 -0700413#endif
414}
415
Ian Rogers848871b2013-08-05 10:56:33 -0700416extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700417static inline void* GetJniDlsymLookupStub() {
418 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
419}
Jeff Hao58df3272013-04-22 15:28:53 -0700420
Ian Rogers450dcb52013-09-20 17:36:02 -0700421template <typename INT_TYPE, typename FLOAT_TYPE>
422static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
423 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
424 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
425 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
426 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
427 if (LIKELY(f > kMinIntAsFloat)) {
428 if (LIKELY(f < kMaxIntAsFloat)) {
429 return static_cast<INT_TYPE>(f);
430 } else {
431 return kMaxInt;
432 }
433 } else {
434 return (f != f) ? 0 : kMinInt; // f != f implies NaN
435 }
436}
437
Shih-wei Liao2d831012011-09-28 22:06:53 -0700438} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700439
Ian Rogers7655f292013-07-29 11:07:13 -0700440#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_