blob: b96e0be5c43777c7f8afbe27dd53dfacfbec8343 [file] [log] [blame]
Logan Chien88894ee2012-02-13 16:42:22 +08001/*
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 */
16
17#include "jni_compiler.h"
18
19#include "class_linker.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Logan Chien88894ee2012-02-13 16:42:22 +080021#include "compiled_method.h"
22#include "compiler.h"
23#include "compiler_llvm.h"
24#include "ir_builder.h"
25#include "logging.h"
26#include "oat_compilation_unit.h"
27#include "object.h"
28#include "runtime.h"
TDYa12728f1a142012-03-15 21:51:52 -070029#include "runtime_support_func.h"
Logan Chien88894ee2012-02-13 16:42:22 +080030#include "utils_llvm.h"
31
TDYa12728f1a142012-03-15 21:51:52 -070032#include <llvm/BasicBlock.h>
Logan Chien88894ee2012-02-13 16:42:22 +080033#include <llvm/DerivedTypes.h>
34#include <llvm/Function.h>
35#include <llvm/Type.h>
36
37namespace art {
38namespace compiler_llvm {
39
TDYa1270b686e52012-04-09 22:43:35 -070040using namespace runtime_support;
Logan Chien88894ee2012-02-13 16:42:22 +080041
Logan Chien8b977d32012-02-21 19:14:55 +080042JniCompiler::JniCompiler(CompilationUnit* cunit,
Logan Chien88894ee2012-02-13 16:42:22 +080043 Compiler const& compiler,
44 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080045: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
46 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()),
Logan Chien88894ee2012-02-13 16:42:22 +080047 oat_compilation_unit_(oat_compilation_unit),
48 access_flags_(oat_compilation_unit->access_flags_),
49 method_idx_(oat_compilation_unit->method_idx_),
50 class_linker_(oat_compilation_unit->class_linker_),
51 class_loader_(oat_compilation_unit->class_loader_),
52 dex_cache_(oat_compilation_unit->dex_cache_),
53 dex_file_(oat_compilation_unit->dex_file_),
Logan Chien971bf3f2012-05-01 15:47:55 +080054 method_(dex_cache_->GetResolvedMethod(method_idx_)) {
Logan Chien88894ee2012-02-13 16:42:22 +080055
56 // Check: Ensure that the method is resolved
57 CHECK_NE(method_, static_cast<art::Method*>(NULL));
58
59 // Check: Ensure that JNI compiler will only get "native" method
60 CHECK((access_flags_ & kAccNative) != 0);
61}
62
63
64CompiledMethod* JniCompiler::Compile() {
TDYa1279000a842012-03-23 17:43:08 -070065 const bool is_static = (access_flags_ & kAccStatic) != 0;
66 const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0;
67 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_);
68 char const return_shorty = dex_file_->GetMethodShorty(method_id)[0];
69 llvm::Value* this_object_or_class_object;
TDYa12728f1a142012-03-15 21:51:52 -070070
Logan Chien88894ee2012-02-13 16:42:22 +080071 CreateFunction();
72
TDYa12728f1a142012-03-15 21:51:52 -070073 // Set argument name
74 llvm::Function::arg_iterator arg_begin(func_->arg_begin());
75 llvm::Function::arg_iterator arg_end(func_->arg_end());
76 llvm::Function::arg_iterator arg_iter(arg_begin);
77
78 DCHECK_NE(arg_iter, arg_end);
79 arg_iter->setName("method");
80 llvm::Value* method_object_addr = arg_iter++;
81
TDYa1279000a842012-03-23 17:43:08 -070082 if (!is_static) {
83 // Non-static, the second argument is "this object"
84 this_object_or_class_object = arg_iter++;
85 } else {
86 // Load class object
87 this_object_or_class_object =
TDYa1275bb86012012-04-11 05:57:28 -070088 irb_.LoadFromObjectOffset(method_object_addr,
89 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -070090 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -070091 kTBAAConstJObject);
TDYa1279000a842012-03-23 17:43:08 -070092 }
93 // Actual argument (ignore method and this object)
TDYa12728f1a142012-03-15 21:51:52 -070094 arg_begin = arg_iter;
95
96 // Count the number of Object* arguments
TDYa1279000a842012-03-23 17:43:08 -070097 uint32_t sirt_size = 1;
98 // "this" object pointer for non-static
99 // "class" object pointer for static
TDYa12728f1a142012-03-15 21:51:52 -0700100 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
TDYa12767ae8ff2012-05-02 19:08:02 -0700101#if !defined(NDEBUG)
TDYa12728f1a142012-03-15 21:51:52 -0700102 arg_iter->setName(StringPrintf("a%u", i));
TDYa12767ae8ff2012-05-02 19:08:02 -0700103#endif
TDYa12728f1a142012-03-15 21:51:52 -0700104 if (arg_iter->getType() == irb_.getJObjectTy()) {
105 ++sirt_size;
106 }
107 }
108
TDYa12728f1a142012-03-15 21:51:52 -0700109 // Shadow stack
110 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
TDYa1279000a842012-03-23 17:43:08 -0700111 llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
TDYa12728f1a142012-03-15 21:51:52 -0700112
TDYa127c8dc1012012-04-19 07:03:33 -0700113 // Store the dex pc
TDYa1275bb86012012-04-11 05:57:28 -0700114 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127c8dc1012012-04-19 07:03:33 -0700115 ShadowFrame::DexPCOffset(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700116 irb_.getInt32(DexFile::kDexNoIndex),
TDYa127d955bec2012-05-11 10:54:02 -0700117 kTBAAShadowFrame);
TDYa127a0f746b2012-04-09 22:46:30 -0700118
TDYa12728f1a142012-03-15 21:51:52 -0700119 // Push the shadow frame
120 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
TDYa1270de52be2012-05-27 20:49:31 -0700121 llvm::Value* old_shadow_frame =
TDYa127de479be2012-05-31 08:03:26 -0700122 irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr, sirt_size);
TDYa12728f1a142012-03-15 21:51:52 -0700123
TDYa12728f1a142012-03-15 21:51:52 -0700124 // Get JNIEnv
TDYa1275bb86012012-04-11 05:57:28 -0700125 llvm::Value* jni_env_object_addr =
TDYa127de479be2012-05-31 08:03:26 -0700126 irb_.Runtime().EmitLoadFromThreadOffset(Thread::JniEnvOffset().Int32Value(),
127 irb_.getJObjectTy(),
128 kTBAAJRuntime);
TDYa12728f1a142012-03-15 21:51:52 -0700129
130 // Set thread state to kNative
TDYa127de479be2012-05-31 08:03:26 -0700131 irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(),
132 irb_.getInt32(kNative),
133 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700134
135 // Get callee code_addr
TDYa1270b686e52012-04-09 22:43:35 -0700136 llvm::Value* code_addr =
TDYa1275bb86012012-04-11 05:57:28 -0700137 irb_.LoadFromObjectOffset(method_object_addr,
138 Method::NativeMethodOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700139 GetFunctionType(method_idx_, is_static, true)->getPointerTo(),
TDYa1278ca10052012-05-05 19:57:06 -0700140 kTBAAJRuntime);
TDYa12728f1a142012-03-15 21:51:52 -0700141
142 // Load actual parameters
143 std::vector<llvm::Value*> args;
144
TDYa12731a99332012-03-19 02:58:02 -0700145 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700146 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700147
TDYa12731a99332012-03-19 02:58:02 -0700148 // Variables for GetElementPtr
149 llvm::Value* gep_index[] = {
150 irb_.getInt32(0), // No displacement for shadow frame pointer
151 irb_.getInt32(1), // SIRT
152 NULL,
153 };
154
TDYa12728f1a142012-03-15 21:51:52 -0700155 size_t sirt_member_index = 0;
156
TDYa1279000a842012-03-23 17:43:08 -0700157 // Store the "this object or class object" to SIRT
158 gep_index[2] = irb_.getInt32(sirt_member_index++);
159 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127d955bec2012-05-11 10:54:02 -0700160 irb_.CreateStore(this_object_or_class_object, sirt_field_addr, kTBAAShadowFrame);
TDYa1279000a842012-03-23 17:43:08 -0700161 // Push the "this object or class object" to out args
162 args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
TDYa12731a99332012-03-19 02:58:02 -0700163 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700164 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
165 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700166 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700167 gep_index[2] = irb_.getInt32(sirt_member_index++);
168 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127d955bec2012-05-11 10:54:02 -0700169 irb_.CreateStore(arg_iter, sirt_field_addr, kTBAAShadowFrame);
TDYa12728f1a142012-03-15 21:51:52 -0700170 // Note null is placed in the SIRT but the jobject passed to the native code must be null
171 // (not a pointer into the SIRT as with regular references).
172 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
173 llvm::Value* arg =
174 irb_.CreateSelect(equal_null,
175 irb_.getJNull(),
176 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
177 args.push_back(arg);
178 } else {
179 args.push_back(arg_iter);
180 }
181 }
182
TDYa1279000a842012-03-23 17:43:08 -0700183 // Acquire lock for synchronized methods.
184 if (is_synchronized) {
TDYa127b08ed122012-06-05 23:51:19 -0700185 irb_.Runtime().EmitLockObject(this_object_or_class_object);
TDYa1279000a842012-03-23 17:43:08 -0700186 }
TDYa12728f1a142012-03-15 21:51:52 -0700187
188 // saved_local_ref_cookie = env->local_ref_cookie
189 llvm::Value* saved_local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700190 irb_.LoadFromObjectOffset(jni_env_object_addr,
191 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700192 irb_.getInt32Ty(),
193 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700194
195 // env->local_ref_cookie = env->locals.segment_state
196 llvm::Value* segment_state =
TDYa1275bb86012012-04-11 05:57:28 -0700197 irb_.LoadFromObjectOffset(jni_env_object_addr,
198 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700199 irb_.getInt32Ty(),
200 kTBAARuntimeInfo);
TDYa1275bb86012012-04-11 05:57:28 -0700201 irb_.StoreToObjectOffset(jni_env_object_addr,
202 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700203 segment_state,
204 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700205
206
207 // Call!!!
208 llvm::Value* retval = irb_.CreateCall(code_addr, args);
209
210
TDYa1279000a842012-03-23 17:43:08 -0700211 // Release lock for synchronized methods.
212 if (is_synchronized) {
TDYa127b08ed122012-06-05 23:51:19 -0700213 irb_.Runtime().EmitUnlockObject(this_object_or_class_object);
TDYa1279000a842012-03-23 17:43:08 -0700214 }
215
TDYa12728f1a142012-03-15 21:51:52 -0700216 // Set thread state to kRunnable
TDYa127de479be2012-05-31 08:03:26 -0700217 irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(),
218 irb_.getInt32(kRunnable),
219 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700220
TDYa12769eafaa2012-04-17 10:51:25 -0700221 // Do a suspend check
TDYa127de479be2012-05-31 08:03:26 -0700222 irb_.Runtime().EmitTestSuspend();
TDYa12769eafaa2012-04-17 10:51:25 -0700223
TDYa1279000a842012-03-23 17:43:08 -0700224 if (return_shorty == 'L') {
TDYa127b08ed122012-06-05 23:51:19 -0700225 // Get thread object
226 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
227
TDYa12728f1a142012-03-15 21:51:52 -0700228 // If the return value is reference, it may point to SIRT, we should decode it.
TDYa1270b686e52012-04-09 22:43:35 -0700229 retval = irb_.CreateCall2(irb_.GetRuntime(DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700230 thread_object_addr,
231 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700232 }
233
234 // env->locals.segment_state = env->local_ref_cookie
235 llvm::Value* local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700236 irb_.LoadFromObjectOffset(jni_env_object_addr,
237 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700238 irb_.getInt32Ty(),
239 kTBAARuntimeInfo);
TDYa1275bb86012012-04-11 05:57:28 -0700240 irb_.StoreToObjectOffset(jni_env_object_addr,
241 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700242 local_ref_cookie,
243 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700244
245 // env->local_ref_cookie = saved_local_ref_cookie
TDYa1275bb86012012-04-11 05:57:28 -0700246 irb_.StoreToObjectOffset(jni_env_object_addr,
247 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700248 saved_local_ref_cookie,
249 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700250
251 // Pop the shadow frame
TDYa127de479be2012-05-31 08:03:26 -0700252 irb_.Runtime().EmitPopShadowFrame(old_shadow_frame);
TDYa12728f1a142012-03-15 21:51:52 -0700253
254 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700255 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700256 irb_.CreateRet(retval);
257 } else {
258 irb_.CreateRetVoid();
259 }
260
TDYa12728f1a142012-03-15 21:51:52 -0700261 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -0700262 VERIFY_LLVM_FUNCTION(*func_);
TDYa12728f1a142012-03-15 21:51:52 -0700263
Logan Chien971bf3f2012-05-01 15:47:55 +0800264 cunit_->Materialize();
TDYa1270200d072012-04-17 20:55:08 -0700265
Logan Chien971bf3f2012-05-01 15:47:55 +0800266 return new CompiledMethod(cunit_->GetInstructionSet(),
267 cunit_->GetCompiledCode());
Logan Chien88894ee2012-02-13 16:42:22 +0800268}
269
270
271void JniCompiler::CreateFunction() {
272 // LLVM function name
Logan Chien971bf3f2012-05-01 15:47:55 +0800273 std::string func_name(ElfFuncName(cunit_->GetIndex()));
Logan Chien88894ee2012-02-13 16:42:22 +0800274
275 // Get function type
276 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700277 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800278
279 // Create function
280 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
281 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700282
283 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700284 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
285
286 // Set insert point
287 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800288}
289
290
291llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700292 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800293 // Get method signature
294 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
295
296 uint32_t shorty_size;
Logan Chien12584172012-07-10 04:07:28 -0700297 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien88894ee2012-02-13 16:42:22 +0800298 CHECK_GE(shorty_size, 1u);
299
300 // Get return type
301 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
302
303 // Get argument type
304 std::vector<llvm::Type*> args_type;
305
306 args_type.push_back(irb_.getJObjectTy()); // method object pointer
307
TDYa1279000a842012-03-23 17:43:08 -0700308 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700309 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700310 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700311 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800312 }
313
314 for (uint32_t i = 1; i < shorty_size; ++i) {
315 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
316 }
317
318 return llvm::FunctionType::get(ret_type, args_type, false);
319}
320
Logan Chien88894ee2012-02-13 16:42:22 +0800321} // namespace compiler_llvm
322} // namespace art