blob: 37ca82f2ff243f9e7755e03bab2bd52a4fdcf045 [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 Chien937105a2012-04-02 02:37:37 +080054 method_(dex_cache_->GetResolvedMethod(method_idx_)),
55 elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Logan Chien88894ee2012-02-13 16:42:22 +080056
57 // Check: Ensure that the method is resolved
58 CHECK_NE(method_, static_cast<art::Method*>(NULL));
59
60 // Check: Ensure that JNI compiler will only get "native" method
61 CHECK((access_flags_ & kAccNative) != 0);
62}
63
64
65CompiledMethod* JniCompiler::Compile() {
TDYa1279000a842012-03-23 17:43:08 -070066 const bool is_static = (access_flags_ & kAccStatic) != 0;
67 const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0;
68 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_);
69 char const return_shorty = dex_file_->GetMethodShorty(method_id)[0];
70 llvm::Value* this_object_or_class_object;
TDYa12728f1a142012-03-15 21:51:52 -070071
Logan Chien88894ee2012-02-13 16:42:22 +080072 CreateFunction();
73
TDYa12728f1a142012-03-15 21:51:52 -070074 // Set argument name
75 llvm::Function::arg_iterator arg_begin(func_->arg_begin());
76 llvm::Function::arg_iterator arg_end(func_->arg_end());
77 llvm::Function::arg_iterator arg_iter(arg_begin);
78
79 DCHECK_NE(arg_iter, arg_end);
80 arg_iter->setName("method");
81 llvm::Value* method_object_addr = arg_iter++;
82
TDYa1279000a842012-03-23 17:43:08 -070083 if (!is_static) {
84 // Non-static, the second argument is "this object"
85 this_object_or_class_object = arg_iter++;
86 } else {
87 // Load class object
88 this_object_or_class_object =
TDYa1275bb86012012-04-11 05:57:28 -070089 irb_.LoadFromObjectOffset(method_object_addr,
90 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -070091 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -070092 kTBAAConstJObject);
TDYa1279000a842012-03-23 17:43:08 -070093 }
94 // Actual argument (ignore method and this object)
TDYa12728f1a142012-03-15 21:51:52 -070095 arg_begin = arg_iter;
96
97 // Count the number of Object* arguments
TDYa1279000a842012-03-23 17:43:08 -070098 uint32_t sirt_size = 1;
99 // "this" object pointer for non-static
100 // "class" object pointer for static
TDYa12728f1a142012-03-15 21:51:52 -0700101 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
TDYa12767ae8ff2012-05-02 19:08:02 -0700102#if !defined(NDEBUG)
TDYa12728f1a142012-03-15 21:51:52 -0700103 arg_iter->setName(StringPrintf("a%u", i));
TDYa12767ae8ff2012-05-02 19:08:02 -0700104#endif
TDYa12728f1a142012-03-15 21:51:52 -0700105 if (arg_iter->getType() == irb_.getJObjectTy()) {
106 ++sirt_size;
107 }
108 }
109
TDYa12728f1a142012-03-15 21:51:52 -0700110 // Shadow stack
111 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
TDYa1279000a842012-03-23 17:43:08 -0700112 llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
TDYa12728f1a142012-03-15 21:51:52 -0700113
TDYa127c8dc1012012-04-19 07:03:33 -0700114 // Store the dex pc
TDYa1275bb86012012-04-11 05:57:28 -0700115 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127c8dc1012012-04-19 07:03:33 -0700116 ShadowFrame::DexPCOffset(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700117 irb_.getInt32(DexFile::kDexNoIndex),
TDYa127d955bec2012-05-11 10:54:02 -0700118 kTBAAShadowFrame);
TDYa127a0f746b2012-04-09 22:46:30 -0700119
TDYa12728f1a142012-03-15 21:51:52 -0700120 // Push the shadow frame
121 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
TDYa1270de52be2012-05-27 20:49:31 -0700122 llvm::Value* old_shadow_frame =
TDYa127de479be2012-05-31 08:03:26 -0700123 irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr, sirt_size);
TDYa12728f1a142012-03-15 21:51:52 -0700124
TDYa12728f1a142012-03-15 21:51:52 -0700125 // Get JNIEnv
TDYa1275bb86012012-04-11 05:57:28 -0700126 llvm::Value* jni_env_object_addr =
TDYa127de479be2012-05-31 08:03:26 -0700127 irb_.Runtime().EmitLoadFromThreadOffset(Thread::JniEnvOffset().Int32Value(),
128 irb_.getJObjectTy(),
129 kTBAAJRuntime);
TDYa12728f1a142012-03-15 21:51:52 -0700130
131 // Set thread state to kNative
TDYa127de479be2012-05-31 08:03:26 -0700132 irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(),
133 irb_.getInt32(kNative),
134 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700135
136 // Get callee code_addr
TDYa1270b686e52012-04-09 22:43:35 -0700137 llvm::Value* code_addr =
TDYa1275bb86012012-04-11 05:57:28 -0700138 irb_.LoadFromObjectOffset(method_object_addr,
139 Method::NativeMethodOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700140 GetFunctionType(method_idx_, is_static, true)->getPointerTo(),
TDYa1278ca10052012-05-05 19:57:06 -0700141 kTBAAJRuntime);
TDYa12728f1a142012-03-15 21:51:52 -0700142
143 // Load actual parameters
144 std::vector<llvm::Value*> args;
145
TDYa12731a99332012-03-19 02:58:02 -0700146 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700147 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700148
TDYa12731a99332012-03-19 02:58:02 -0700149 // Variables for GetElementPtr
150 llvm::Value* gep_index[] = {
151 irb_.getInt32(0), // No displacement for shadow frame pointer
152 irb_.getInt32(1), // SIRT
153 NULL,
154 };
155
TDYa12728f1a142012-03-15 21:51:52 -0700156 size_t sirt_member_index = 0;
157
TDYa1279000a842012-03-23 17:43:08 -0700158 // Store the "this object or class object" to SIRT
159 gep_index[2] = irb_.getInt32(sirt_member_index++);
160 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127d955bec2012-05-11 10:54:02 -0700161 irb_.CreateStore(this_object_or_class_object, sirt_field_addr, kTBAAShadowFrame);
TDYa1279000a842012-03-23 17:43:08 -0700162 // Push the "this object or class object" to out args
163 args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
TDYa12731a99332012-03-19 02:58:02 -0700164 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700165 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
166 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700167 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700168 gep_index[2] = irb_.getInt32(sirt_member_index++);
169 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127d955bec2012-05-11 10:54:02 -0700170 irb_.CreateStore(arg_iter, sirt_field_addr, kTBAAShadowFrame);
TDYa12728f1a142012-03-15 21:51:52 -0700171 // Note null is placed in the SIRT but the jobject passed to the native code must be null
172 // (not a pointer into the SIRT as with regular references).
173 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
174 llvm::Value* arg =
175 irb_.CreateSelect(equal_null,
176 irb_.getJNull(),
177 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
178 args.push_back(arg);
179 } else {
180 args.push_back(arg_iter);
181 }
182 }
183
TDYa1279000a842012-03-23 17:43:08 -0700184 // Acquire lock for synchronized methods.
185 if (is_synchronized) {
TDYa127b08ed122012-06-05 23:51:19 -0700186 irb_.Runtime().EmitLockObject(this_object_or_class_object);
TDYa1279000a842012-03-23 17:43:08 -0700187 }
TDYa12728f1a142012-03-15 21:51:52 -0700188
189 // saved_local_ref_cookie = env->local_ref_cookie
190 llvm::Value* saved_local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700191 irb_.LoadFromObjectOffset(jni_env_object_addr,
192 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700193 irb_.getInt32Ty(),
194 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700195
196 // env->local_ref_cookie = env->locals.segment_state
197 llvm::Value* segment_state =
TDYa1275bb86012012-04-11 05:57:28 -0700198 irb_.LoadFromObjectOffset(jni_env_object_addr,
199 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700200 irb_.getInt32Ty(),
201 kTBAARuntimeInfo);
TDYa1275bb86012012-04-11 05:57:28 -0700202 irb_.StoreToObjectOffset(jni_env_object_addr,
203 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700204 segment_state,
205 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700206
207
208 // Call!!!
209 llvm::Value* retval = irb_.CreateCall(code_addr, args);
210
211
TDYa1279000a842012-03-23 17:43:08 -0700212 // Release lock for synchronized methods.
213 if (is_synchronized) {
TDYa127b08ed122012-06-05 23:51:19 -0700214 irb_.Runtime().EmitUnlockObject(this_object_or_class_object);
TDYa1279000a842012-03-23 17:43:08 -0700215 }
216
TDYa12728f1a142012-03-15 21:51:52 -0700217 // Set thread state to kRunnable
TDYa127de479be2012-05-31 08:03:26 -0700218 irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(),
219 irb_.getInt32(kRunnable),
220 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700221
TDYa12769eafaa2012-04-17 10:51:25 -0700222 // Do a suspend check
TDYa127de479be2012-05-31 08:03:26 -0700223 irb_.Runtime().EmitTestSuspend();
TDYa12769eafaa2012-04-17 10:51:25 -0700224
TDYa1279000a842012-03-23 17:43:08 -0700225 if (return_shorty == 'L') {
TDYa127b08ed122012-06-05 23:51:19 -0700226 // Get thread object
227 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
228
TDYa12728f1a142012-03-15 21:51:52 -0700229 // If the return value is reference, it may point to SIRT, we should decode it.
TDYa1270b686e52012-04-09 22:43:35 -0700230 retval = irb_.CreateCall2(irb_.GetRuntime(DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700231 thread_object_addr,
232 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700233 }
234
235 // env->locals.segment_state = env->local_ref_cookie
236 llvm::Value* local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700237 irb_.LoadFromObjectOffset(jni_env_object_addr,
238 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700239 irb_.getInt32Ty(),
240 kTBAARuntimeInfo);
TDYa1275bb86012012-04-11 05:57:28 -0700241 irb_.StoreToObjectOffset(jni_env_object_addr,
242 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700243 local_ref_cookie,
244 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700245
246 // env->local_ref_cookie = saved_local_ref_cookie
TDYa1275bb86012012-04-11 05:57:28 -0700247 irb_.StoreToObjectOffset(jni_env_object_addr,
248 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700249 saved_local_ref_cookie,
250 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700251
252 // Pop the shadow frame
TDYa127de479be2012-05-31 08:03:26 -0700253 irb_.Runtime().EmitPopShadowFrame(old_shadow_frame);
TDYa12728f1a142012-03-15 21:51:52 -0700254
255 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700256 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700257 irb_.CreateRet(retval);
258 } else {
259 irb_.CreateRetVoid();
260 }
261
TDYa12728f1a142012-03-15 21:51:52 -0700262 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -0700263 VERIFY_LLVM_FUNCTION(*func_);
TDYa12728f1a142012-03-15 21:51:52 -0700264
TDYa1270200d072012-04-17 20:55:08 -0700265 // Add the memory usage approximation of the compilation unit
266 cunit_->AddMemUsageApproximation((sirt_size * 4 + 50) * 50);
267 // NOTE: We will emit 4 LLVM instructions per object argument,
268 // And about 50 instructions for other operations. (Some runtime support will be inlined.)
269 // Beside, we guess that we have to use 50 bytes to represent one LLVM instruction.
270
Logan Chien110bcba2012-04-16 19:11:28 +0800271 CompiledMethod* compiled_method =
272 new CompiledMethod(cunit_->GetInstructionSet(),
273 cunit_->GetElfIndex(),
274 elf_func_idx_);
275
276 cunit_->RegisterCompiledMethod(func_, compiled_method);
277
278 return compiled_method;
Logan Chien88894ee2012-02-13 16:42:22 +0800279}
280
281
282void JniCompiler::CreateFunction() {
283 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +0800284 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien88894ee2012-02-13 16:42:22 +0800285
286 // Get function type
287 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700288 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800289
290 // Create function
291 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
292 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700293
294 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700295 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
296
297 // Set insert point
298 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800299}
300
301
302llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700303 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800304 // Get method signature
305 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
306
307 uint32_t shorty_size;
308 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
309 CHECK_GE(shorty_size, 1u);
310
311 // Get return type
312 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
313
314 // Get argument type
315 std::vector<llvm::Type*> args_type;
316
317 args_type.push_back(irb_.getJObjectTy()); // method object pointer
318
TDYa1279000a842012-03-23 17:43:08 -0700319 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700320 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700321 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700322 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800323 }
324
325 for (uint32_t i = 1; i < shorty_size; ++i) {
326 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
327 }
328
329 return llvm::FunctionType::get(ret_type, args_type, false);
330}
331
Logan Chien88894ee2012-02-13 16:42:22 +0800332} // namespace compiler_llvm
333} // namespace art