blob: 1672c49a9b60b02ee4a9566c6ba4ff9f5ea3489b [file] [log] [blame]
Orion Hodson26ef34c2017-11-01 13:32:41 +00001/*
2 * Copyright (C) 2017 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 */
16
17#include "runtime_intrinsics.h"
18
19#include "art_method-inl.h"
20#include "class_linker.h"
David Sehr8c0961f2018-01-23 16:11:38 -080021#include "dex/invoke_type.h"
Orion Hodson26ef34c2017-11-01 13:32:41 +000022#include "intrinsics_enum.h"
Evgeny Astigeevichffffa9c2020-05-28 12:38:30 +010023#include "intrinsics_list.h"
Orion Hodson26ef34c2017-11-01 13:32:41 +000024#include "mirror/class.h"
25#include "runtime.h"
26#include "scoped_thread_state_change-inl.h"
27#include "thread.h"
28
29namespace art {
30
31namespace {
32
Evgeny Astigeevichffffa9c2020-05-28 12:38:30 +010033ArtMethod* FindIntrinsicMethod(Thread* self,
34 const char* class_name,
35 const char* method_name,
36 const char* signature)
Orion Hodson26ef34c2017-11-01 13:32:41 +000037 REQUIRES_SHARED(Locks::mutator_lock_) {
38 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
39 PointerSize image_size = class_linker->GetImagePointerSize();
40 ObjPtr<mirror::Class> cls = class_linker->FindSystemClass(self, class_name);
41 if (cls == nullptr) {
42 LOG(FATAL) << "Could not find class of intrinsic " << class_name;
43 }
44
45 ArtMethod* method = cls->FindClassMethod(method_name, signature, image_size);
46 if (method == nullptr || method->GetDeclaringClass() != cls) {
47 LOG(FATAL) << "Could not find method of intrinsic "
48 << class_name << " " << method_name << " " << signature;
49 }
Evgeny Astigeevichffffa9c2020-05-28 12:38:30 +010050 return method;
51}
52
53// Initialize an intrinsic. Returns true if the intrinsic is already
54// initialized, false otherwise.
55bool InitializeIntrinsic(Thread* self,
56 Intrinsics intrinsic,
57 InvokeType invoke_type,
58 const char* class_name,
59 const char* method_name,
60 const char* signature)
61 REQUIRES_SHARED(Locks::mutator_lock_) {
62 ArtMethod* method = FindIntrinsicMethod(self, class_name, method_name, signature);
Orion Hodson26ef34c2017-11-01 13:32:41 +000063
64 CHECK_EQ(method->GetInvokeType(), invoke_type);
65 if (method->IsIntrinsic()) {
66 CHECK_EQ(method->GetIntrinsic(), static_cast<uint32_t>(intrinsic));
67 return true;
68 } else {
69 method->SetIntrinsic(static_cast<uint32_t>(intrinsic));
70 return false;
71 }
72}
73
Evgeny Astigeevichffffa9c2020-05-28 12:38:30 +010074// Returns true if the intrinsic is already initialized, false otherwise.
75bool IsIntrinsicInitialized(Thread* self,
76 Intrinsics intrinsic,
77 InvokeType invoke_type,
78 const char* class_name,
79 const char* method_name,
80 const char* signature)
81 REQUIRES_SHARED(Locks::mutator_lock_) {
82 ArtMethod* method = FindIntrinsicMethod(self, class_name, method_name, signature);
83
84 CHECK_EQ(method->GetInvokeType(), invoke_type);
85 if (method->IsIntrinsic()) {
86 CHECK_EQ(method->GetIntrinsic(), static_cast<uint32_t>(intrinsic));
87 return true;
88 } else {
89 return false;
90 }
91}
92
93bool AreAllIntrinsicsInitialized() {
94 ScopedObjectAccess soa(Thread::Current());
95#define IS_INTRINSIC_INITIALIZED(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
96 IsIntrinsicInitialized(soa.Self(), \
97 Intrinsics::k##Name, \
98 InvokeType, \
99 ClassName, \
100 MethodName, \
101 Signature) &&
102 bool result = INTRINSICS_LIST(IS_INTRINSIC_INITIALIZED) true;
103#undef IS_INTRINSIC_INITIALIZED
104 return result;
105}
106
Orion Hodson26ef34c2017-11-01 13:32:41 +0000107} // namespace
108
109void InitializeIntrinsics() {
110 ScopedObjectAccess soa(Thread::Current());
111 // Initialization here uses the short-circuit operator || to stop
112 // initializing if there's an already initialized intrinsic.
Evgeny Astigeevichffffa9c2020-05-28 12:38:30 +0100113#define INITIALIZE_INTRINSIC(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
114 InitializeIntrinsic(soa.Self(), \
115 Intrinsics::k##Name, \
116 InvokeType, \
117 ClassName, \
118 MethodName, \
Orion Hodson26ef34c2017-11-01 13:32:41 +0000119 Signature) ||
Evgeny Astigeevichffffa9c2020-05-28 12:38:30 +0100120 INTRINSICS_LIST(INITIALIZE_INTRINSIC) true;
121#undef INITIALIZE_INTRINSIC
122 DCHECK(AreAllIntrinsicsInitialized());
Orion Hodson26ef34c2017-11-01 13:32:41 +0000123}
124
125} // namespace art