blob: bb45facba64ab80843a664cc5f925bc7d0ca3c4f [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"
TDYa12731a99332012-03-19 02:58:02 -070030#include "shadow_frame.h"
Logan Chien88894ee2012-02-13 16:42:22 +080031#include "utils_llvm.h"
32
TDYa12728f1a142012-03-15 21:51:52 -070033#include <llvm/BasicBlock.h>
Logan Chien88894ee2012-02-13 16:42:22 +080034#include <llvm/DerivedTypes.h>
35#include <llvm/Function.h>
36#include <llvm/Type.h>
37
38namespace art {
39namespace compiler_llvm {
40
TDYa1270b686e52012-04-09 22:43:35 -070041using namespace runtime_support;
Logan Chien88894ee2012-02-13 16:42:22 +080042
Logan Chien8b977d32012-02-21 19:14:55 +080043JniCompiler::JniCompiler(CompilationUnit* cunit,
Logan Chien88894ee2012-02-13 16:42:22 +080044 Compiler const& compiler,
45 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080046: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
47 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()),
Logan Chien88894ee2012-02-13 16:42:22 +080048 oat_compilation_unit_(oat_compilation_unit),
49 access_flags_(oat_compilation_unit->access_flags_),
50 method_idx_(oat_compilation_unit->method_idx_),
51 class_linker_(oat_compilation_unit->class_linker_),
52 class_loader_(oat_compilation_unit->class_loader_),
53 dex_cache_(oat_compilation_unit->dex_cache_),
54 dex_file_(oat_compilation_unit->dex_file_),
Logan Chien937105a2012-04-02 02:37:37 +080055 method_(dex_cache_->GetResolvedMethod(method_idx_)),
56 elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Logan Chien88894ee2012-02-13 16:42:22 +080057
58 // Check: Ensure that the method is resolved
59 CHECK_NE(method_, static_cast<art::Method*>(NULL));
60
61 // Check: Ensure that JNI compiler will only get "native" method
62 CHECK((access_flags_ & kAccNative) != 0);
63}
64
65
66CompiledMethod* JniCompiler::Compile() {
TDYa1279000a842012-03-23 17:43:08 -070067 const bool is_static = (access_flags_ & kAccStatic) != 0;
68 const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0;
69 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_);
70 char const return_shorty = dex_file_->GetMethodShorty(method_id)[0];
71 llvm::Value* this_object_or_class_object;
TDYa12728f1a142012-03-15 21:51:52 -070072
Logan Chien88894ee2012-02-13 16:42:22 +080073 CreateFunction();
74
TDYa12728f1a142012-03-15 21:51:52 -070075 // Set argument name
76 llvm::Function::arg_iterator arg_begin(func_->arg_begin());
77 llvm::Function::arg_iterator arg_end(func_->arg_end());
78 llvm::Function::arg_iterator arg_iter(arg_begin);
79
80 DCHECK_NE(arg_iter, arg_end);
81 arg_iter->setName("method");
82 llvm::Value* method_object_addr = arg_iter++;
83
TDYa1279000a842012-03-23 17:43:08 -070084 if (!is_static) {
85 // Non-static, the second argument is "this object"
86 this_object_or_class_object = arg_iter++;
87 } else {
88 // Load class object
89 this_object_or_class_object =
TDYa1275bb86012012-04-11 05:57:28 -070090 irb_.LoadFromObjectOffset(method_object_addr,
91 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -070092 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -070093 kTBAAConstJObject);
TDYa1279000a842012-03-23 17:43:08 -070094 }
95 // Actual argument (ignore method and this object)
TDYa12728f1a142012-03-15 21:51:52 -070096 arg_begin = arg_iter;
97
98 // Count the number of Object* arguments
TDYa1279000a842012-03-23 17:43:08 -070099 uint32_t sirt_size = 1;
100 // "this" object pointer for non-static
101 // "class" object pointer for static
TDYa12728f1a142012-03-15 21:51:52 -0700102 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
TDYa12767ae8ff2012-05-02 19:08:02 -0700103#if !defined(NDEBUG)
TDYa12728f1a142012-03-15 21:51:52 -0700104 arg_iter->setName(StringPrintf("a%u", i));
TDYa12767ae8ff2012-05-02 19:08:02 -0700105#endif
TDYa12728f1a142012-03-15 21:51:52 -0700106 if (arg_iter->getType() == irb_.getJObjectTy()) {
107 ++sirt_size;
108 }
109 }
110
TDYa12728f1a142012-03-15 21:51:52 -0700111 // Shadow stack
112 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
TDYa1279000a842012-03-23 17:43:08 -0700113 llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
TDYa12728f1a142012-03-15 21:51:52 -0700114
TDYa127c8dc1012012-04-19 07:03:33 -0700115 // Store the dex pc
TDYa1275bb86012012-04-11 05:57:28 -0700116 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127c8dc1012012-04-19 07:03:33 -0700117 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700118 irb_.getInt32(0),
TDYa127d955bec2012-05-11 10:54:02 -0700119 kTBAAShadowFrame);
TDYa127a0f746b2012-04-09 22:46:30 -0700120
TDYa12728f1a142012-03-15 21:51:52 -0700121 // Push the shadow frame
122 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
TDYa1270de52be2012-05-27 20:49:31 -0700123 llvm::Value* old_shadow_frame =
TDYa127de479be2012-05-31 08:03:26 -0700124 irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr, sirt_size);
TDYa12728f1a142012-03-15 21:51:52 -0700125
TDYa12728f1a142012-03-15 21:51:52 -0700126 // Get JNIEnv
TDYa1275bb86012012-04-11 05:57:28 -0700127 llvm::Value* jni_env_object_addr =
TDYa127de479be2012-05-31 08:03:26 -0700128 irb_.Runtime().EmitLoadFromThreadOffset(Thread::JniEnvOffset().Int32Value(),
129 irb_.getJObjectTy(),
130 kTBAAJRuntime);
TDYa12728f1a142012-03-15 21:51:52 -0700131
132 // Set thread state to kNative
TDYa127de479be2012-05-31 08:03:26 -0700133 irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(),
134 irb_.getInt32(kNative),
135 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700136
137 // Get callee code_addr
TDYa1270b686e52012-04-09 22:43:35 -0700138 llvm::Value* code_addr =
TDYa1275bb86012012-04-11 05:57:28 -0700139 irb_.LoadFromObjectOffset(method_object_addr,
140 Method::NativeMethodOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700141 GetFunctionType(method_idx_, is_static, true)->getPointerTo(),
TDYa1278ca10052012-05-05 19:57:06 -0700142 kTBAAJRuntime);
TDYa12728f1a142012-03-15 21:51:52 -0700143
144 // Load actual parameters
145 std::vector<llvm::Value*> args;
146
TDYa12731a99332012-03-19 02:58:02 -0700147 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700148 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700149
TDYa12731a99332012-03-19 02:58:02 -0700150 // Variables for GetElementPtr
151 llvm::Value* gep_index[] = {
152 irb_.getInt32(0), // No displacement for shadow frame pointer
153 irb_.getInt32(1), // SIRT
154 NULL,
155 };
156
TDYa12728f1a142012-03-15 21:51:52 -0700157 size_t sirt_member_index = 0;
158
TDYa1279000a842012-03-23 17:43:08 -0700159 // Store the "this object or class object" to SIRT
160 gep_index[2] = irb_.getInt32(sirt_member_index++);
161 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127d955bec2012-05-11 10:54:02 -0700162 irb_.CreateStore(this_object_or_class_object, sirt_field_addr, kTBAAShadowFrame);
TDYa1279000a842012-03-23 17:43:08 -0700163 // Push the "this object or class object" to out args
164 args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
TDYa12731a99332012-03-19 02:58:02 -0700165 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700166 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
167 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700168 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700169 gep_index[2] = irb_.getInt32(sirt_member_index++);
170 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127d955bec2012-05-11 10:54:02 -0700171 irb_.CreateStore(arg_iter, sirt_field_addr, kTBAAShadowFrame);
TDYa12728f1a142012-03-15 21:51:52 -0700172 // Note null is placed in the SIRT but the jobject passed to the native code must be null
173 // (not a pointer into the SIRT as with regular references).
174 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
175 llvm::Value* arg =
176 irb_.CreateSelect(equal_null,
177 irb_.getJNull(),
178 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
179 args.push_back(arg);
180 } else {
181 args.push_back(arg_iter);
182 }
183 }
184
TDYa1279000a842012-03-23 17:43:08 -0700185 // Acquire lock for synchronized methods.
186 if (is_synchronized) {
TDYa127b08ed122012-06-05 23:51:19 -0700187 irb_.Runtime().EmitLockObject(this_object_or_class_object);
TDYa1279000a842012-03-23 17:43:08 -0700188 }
TDYa12728f1a142012-03-15 21:51:52 -0700189
190 // saved_local_ref_cookie = env->local_ref_cookie
191 llvm::Value* saved_local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700192 irb_.LoadFromObjectOffset(jni_env_object_addr,
193 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700194 irb_.getInt32Ty(),
195 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700196
197 // env->local_ref_cookie = env->locals.segment_state
198 llvm::Value* segment_state =
TDYa1275bb86012012-04-11 05:57:28 -0700199 irb_.LoadFromObjectOffset(jni_env_object_addr,
200 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700201 irb_.getInt32Ty(),
202 kTBAARuntimeInfo);
TDYa1275bb86012012-04-11 05:57:28 -0700203 irb_.StoreToObjectOffset(jni_env_object_addr,
204 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700205 segment_state,
206 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700207
208
209 // Call!!!
210 llvm::Value* retval = irb_.CreateCall(code_addr, args);
211
212
TDYa1279000a842012-03-23 17:43:08 -0700213 // Release lock for synchronized methods.
214 if (is_synchronized) {
TDYa127b08ed122012-06-05 23:51:19 -0700215 irb_.Runtime().EmitUnlockObject(this_object_or_class_object);
TDYa1279000a842012-03-23 17:43:08 -0700216 }
217
TDYa12728f1a142012-03-15 21:51:52 -0700218 // Set thread state to kRunnable
TDYa127de479be2012-05-31 08:03:26 -0700219 irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(),
220 irb_.getInt32(kRunnable),
221 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700222
TDYa12769eafaa2012-04-17 10:51:25 -0700223 // Do a suspend check
TDYa127de479be2012-05-31 08:03:26 -0700224 irb_.Runtime().EmitTestSuspend();
TDYa12769eafaa2012-04-17 10:51:25 -0700225
TDYa1279000a842012-03-23 17:43:08 -0700226 if (return_shorty == 'L') {
TDYa127b08ed122012-06-05 23:51:19 -0700227 // Get thread object
228 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
229
TDYa12728f1a142012-03-15 21:51:52 -0700230 // If the return value is reference, it may point to SIRT, we should decode it.
TDYa1270b686e52012-04-09 22:43:35 -0700231 retval = irb_.CreateCall2(irb_.GetRuntime(DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700232 thread_object_addr,
233 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700234 }
235
236 // env->locals.segment_state = env->local_ref_cookie
237 llvm::Value* local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700238 irb_.LoadFromObjectOffset(jni_env_object_addr,
239 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700240 irb_.getInt32Ty(),
241 kTBAARuntimeInfo);
TDYa1275bb86012012-04-11 05:57:28 -0700242 irb_.StoreToObjectOffset(jni_env_object_addr,
243 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700244 local_ref_cookie,
245 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700246
247 // env->local_ref_cookie = saved_local_ref_cookie
TDYa1275bb86012012-04-11 05:57:28 -0700248 irb_.StoreToObjectOffset(jni_env_object_addr,
249 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700250 saved_local_ref_cookie,
251 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700252
253 // Pop the shadow frame
TDYa127de479be2012-05-31 08:03:26 -0700254 irb_.Runtime().EmitPopShadowFrame(old_shadow_frame);
TDYa12728f1a142012-03-15 21:51:52 -0700255
256 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700257 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700258 irb_.CreateRet(retval);
259 } else {
260 irb_.CreateRetVoid();
261 }
262
TDYa12728f1a142012-03-15 21:51:52 -0700263 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -0700264 VERIFY_LLVM_FUNCTION(*func_);
TDYa12728f1a142012-03-15 21:51:52 -0700265
TDYa1270200d072012-04-17 20:55:08 -0700266 // Add the memory usage approximation of the compilation unit
267 cunit_->AddMemUsageApproximation((sirt_size * 4 + 50) * 50);
268 // NOTE: We will emit 4 LLVM instructions per object argument,
269 // And about 50 instructions for other operations. (Some runtime support will be inlined.)
270 // Beside, we guess that we have to use 50 bytes to represent one LLVM instruction.
271
Logan Chien110bcba2012-04-16 19:11:28 +0800272 CompiledMethod* compiled_method =
273 new CompiledMethod(cunit_->GetInstructionSet(),
274 cunit_->GetElfIndex(),
275 elf_func_idx_);
276
277 cunit_->RegisterCompiledMethod(func_, compiled_method);
278
279 return compiled_method;
Logan Chien88894ee2012-02-13 16:42:22 +0800280}
281
282
283void JniCompiler::CreateFunction() {
284 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +0800285 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien88894ee2012-02-13 16:42:22 +0800286
287 // Get function type
288 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700289 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800290
291 // Create function
292 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
293 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700294
295 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700296 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
297
298 // Set insert point
299 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800300}
301
302
303llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700304 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800305 // Get method signature
306 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
307
308 uint32_t shorty_size;
309 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
310 CHECK_GE(shorty_size, 1u);
311
312 // Get return type
313 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
314
315 // Get argument type
316 std::vector<llvm::Type*> args_type;
317
318 args_type.push_back(irb_.getJObjectTy()); // method object pointer
319
TDYa1279000a842012-03-23 17:43:08 -0700320 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700321 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700322 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700323 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800324 }
325
326 for (uint32_t i = 1; i < shorty_size; ++i) {
327 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
328 }
329
330 return llvm::FunctionType::get(ret_type, args_type, false);
331}
332
Logan Chien88894ee2012-02-13 16:42:22 +0800333} // namespace compiler_llvm
334} // namespace art