1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "runtime_intrinsics.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "class_linker.h"
21*795d594fSAndroid Build Coastguard Worker #include "dex/invoke_type.h"
22*795d594fSAndroid Build Coastguard Worker #include "intrinsics_enum.h"
23*795d594fSAndroid Build Coastguard Worker #include "intrinsics_list.h"
24*795d594fSAndroid Build Coastguard Worker #include "mirror/class.h"
25*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
26*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "thread.h"
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker namespace {
32*795d594fSAndroid Build Coastguard Worker
FindIntrinsicMethod(Thread * self,const char * class_name,const char * method_name,const char * signature)33*795d594fSAndroid Build Coastguard Worker ArtMethod* FindIntrinsicMethod(Thread* self,
34*795d594fSAndroid Build Coastguard Worker const char* class_name,
35*795d594fSAndroid Build Coastguard Worker const char* method_name,
36*795d594fSAndroid Build Coastguard Worker const char* signature)
37*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
38*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
39*795d594fSAndroid Build Coastguard Worker PointerSize image_size = class_linker->GetImagePointerSize();
40*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> cls = class_linker->FindSystemClass(self, class_name);
41*795d594fSAndroid Build Coastguard Worker if (cls == nullptr) {
42*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Could not find class of intrinsic " << class_name;
43*795d594fSAndroid Build Coastguard Worker }
44*795d594fSAndroid Build Coastguard Worker
45*795d594fSAndroid Build Coastguard Worker ArtMethod* method = cls->FindClassMethod(method_name, signature, image_size);
46*795d594fSAndroid Build Coastguard Worker if (method == nullptr || method->GetDeclaringClass() != cls) {
47*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Could not find method of intrinsic "
48*795d594fSAndroid Build Coastguard Worker << class_name << " " << method_name << " " << signature;
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker return method;
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker // Initialize an intrinsic. Returns true if the intrinsic is already
54*795d594fSAndroid Build Coastguard Worker // initialized, false otherwise.
InitializeIntrinsic(Thread * self,Intrinsics intrinsic,InvokeType invoke_type,const char * class_name,const char * method_name,const char * signature)55*795d594fSAndroid Build Coastguard Worker bool InitializeIntrinsic(Thread* self,
56*795d594fSAndroid Build Coastguard Worker Intrinsics intrinsic,
57*795d594fSAndroid Build Coastguard Worker InvokeType invoke_type,
58*795d594fSAndroid Build Coastguard Worker const char* class_name,
59*795d594fSAndroid Build Coastguard Worker const char* method_name,
60*795d594fSAndroid Build Coastguard Worker const char* signature)
61*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
62*795d594fSAndroid Build Coastguard Worker ArtMethod* method = FindIntrinsicMethod(self, class_name, method_name, signature);
63*795d594fSAndroid Build Coastguard Worker
64*795d594fSAndroid Build Coastguard Worker CHECK_EQ(method->GetInvokeType(), invoke_type);
65*795d594fSAndroid Build Coastguard Worker if (method->IsIntrinsic()) {
66*795d594fSAndroid Build Coastguard Worker CHECK_EQ(method->GetIntrinsic(), intrinsic);
67*795d594fSAndroid Build Coastguard Worker return true;
68*795d594fSAndroid Build Coastguard Worker } else {
69*795d594fSAndroid Build Coastguard Worker method->SetIntrinsic(intrinsic);
70*795d594fSAndroid Build Coastguard Worker return false;
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker }
73*795d594fSAndroid Build Coastguard Worker
74*795d594fSAndroid Build Coastguard Worker // Returns true if the intrinsic is already initialized, false otherwise.
IsIntrinsicInitialized(Thread * self,Intrinsics intrinsic,InvokeType invoke_type,const char * class_name,const char * method_name,const char * signature)75*795d594fSAndroid Build Coastguard Worker bool IsIntrinsicInitialized(Thread* self,
76*795d594fSAndroid Build Coastguard Worker Intrinsics intrinsic,
77*795d594fSAndroid Build Coastguard Worker InvokeType invoke_type,
78*795d594fSAndroid Build Coastguard Worker const char* class_name,
79*795d594fSAndroid Build Coastguard Worker const char* method_name,
80*795d594fSAndroid Build Coastguard Worker const char* signature)
81*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
82*795d594fSAndroid Build Coastguard Worker ArtMethod* method = FindIntrinsicMethod(self, class_name, method_name, signature);
83*795d594fSAndroid Build Coastguard Worker
84*795d594fSAndroid Build Coastguard Worker CHECK_EQ(method->GetInvokeType(), invoke_type);
85*795d594fSAndroid Build Coastguard Worker if (method->IsIntrinsic()) {
86*795d594fSAndroid Build Coastguard Worker CHECK_EQ(method->GetIntrinsic(), intrinsic);
87*795d594fSAndroid Build Coastguard Worker return true;
88*795d594fSAndroid Build Coastguard Worker } else {
89*795d594fSAndroid Build Coastguard Worker return false;
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker
AreAllIntrinsicsInitialized()93*795d594fSAndroid Build Coastguard Worker bool AreAllIntrinsicsInitialized() REQUIRES_SHARED(Locks::mutator_lock_) {
94*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
95*795d594fSAndroid Build Coastguard Worker #define IS_INTRINSIC_INITIALIZED(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
96*795d594fSAndroid Build Coastguard Worker IsIntrinsicInitialized(self, \
97*795d594fSAndroid Build Coastguard Worker Intrinsics::k##Name, \
98*795d594fSAndroid Build Coastguard Worker InvokeType, \
99*795d594fSAndroid Build Coastguard Worker ClassName, \
100*795d594fSAndroid Build Coastguard Worker MethodName, \
101*795d594fSAndroid Build Coastguard Worker Signature) &&
102*795d594fSAndroid Build Coastguard Worker bool result = ART_INTRINSICS_LIST(IS_INTRINSIC_INITIALIZED) true;
103*795d594fSAndroid Build Coastguard Worker #undef IS_INTRINSIC_INITIALIZED
104*795d594fSAndroid Build Coastguard Worker return result;
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
107*795d594fSAndroid Build Coastguard Worker } // namespace
108*795d594fSAndroid Build Coastguard Worker
InitializeIntrinsics()109*795d594fSAndroid Build Coastguard Worker void InitializeIntrinsics() {
110*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
111*795d594fSAndroid Build Coastguard Worker // Initialization here uses the short-circuit operator || to stop
112*795d594fSAndroid Build Coastguard Worker // initializing if there's an already initialized intrinsic.
113*795d594fSAndroid Build Coastguard Worker #define INITIALIZE_INTRINSIC(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
114*795d594fSAndroid Build Coastguard Worker InitializeIntrinsic(self, \
115*795d594fSAndroid Build Coastguard Worker Intrinsics::k##Name, \
116*795d594fSAndroid Build Coastguard Worker InvokeType, \
117*795d594fSAndroid Build Coastguard Worker ClassName, \
118*795d594fSAndroid Build Coastguard Worker MethodName, \
119*795d594fSAndroid Build Coastguard Worker Signature) ||
120*795d594fSAndroid Build Coastguard Worker ART_INTRINSICS_LIST(INITIALIZE_INTRINSIC) true;
121*795d594fSAndroid Build Coastguard Worker #undef INITIALIZE_INTRINSIC
122*795d594fSAndroid Build Coastguard Worker DCHECK(AreAllIntrinsicsInitialized());
123*795d594fSAndroid Build Coastguard Worker }
124*795d594fSAndroid Build Coastguard Worker
125*795d594fSAndroid Build Coastguard Worker } // namespace art
126