blob: 02985f79b4e9364f0cb18fdae70154fc9b266d66 [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(),
92 irb_.getJObjectTy());
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) {
102 arg_iter->setName(StringPrintf("a%u", i));
103 if (arg_iter->getType() == irb_.getJObjectTy()) {
104 ++sirt_size;
105 }
106 }
107
TDYa12731a99332012-03-19 02:58:02 -0700108 // Get thread object
TDYa1270b686e52012-04-09 22:43:35 -0700109 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
TDYa12728f1a142012-03-15 21:51:52 -0700110
111 // 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
115 // Zero-initialization of the shadow frame
116 llvm::ConstantAggregateZero* zero_initializer =
117 llvm::ConstantAggregateZero::get(shadow_frame_type);
TDYa12728f1a142012-03-15 21:51:52 -0700118 irb_.CreateStore(zero_initializer, shadow_frame_);
119
TDYa12728f1a142012-03-15 21:51:52 -0700120 // Store the method pointer
TDYa12731a99332012-03-19 02:58:02 -0700121 llvm::Value* method_field_addr =
122 irb_.CreatePtrDisp(shadow_frame_,
123 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
124 irb_.getJObjectTy()->getPointerTo());
TDYa12728f1a142012-03-15 21:51:52 -0700125 irb_.CreateStore(method_object_addr, method_field_addr);
126
TDYa127c8dc1012012-04-19 07:03:33 -0700127 // Store the dex pc
TDYa1275bb86012012-04-11 05:57:28 -0700128 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127c8dc1012012-04-19 07:03:33 -0700129 ShadowFrame::DexPCOffset(),
130 irb_.getInt32(0));
TDYa127a0f746b2012-04-09 22:46:30 -0700131
TDYa12728f1a142012-03-15 21:51:52 -0700132 // Store the number of the pointer slots
TDYa1275bb86012012-04-11 05:57:28 -0700133 irb_.StoreToObjectOffset(shadow_frame_,
134 ShadowFrame::NumberOfReferencesOffset(),
135 irb_.getInt32(sirt_size));
TDYa12728f1a142012-03-15 21:51:52 -0700136
137 // Push the shadow frame
138 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
TDYa1270b686e52012-04-09 22:43:35 -0700139 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
TDYa12728f1a142012-03-15 21:51:52 -0700140
TDYa12728f1a142012-03-15 21:51:52 -0700141 // Get JNIEnv
TDYa1275bb86012012-04-11 05:57:28 -0700142 llvm::Value* jni_env_object_addr =
143 irb_.LoadFromObjectOffset(thread_object_addr,
144 Thread::JniEnvOffset().Int32Value(),
145 irb_.getJObjectTy());
TDYa12728f1a142012-03-15 21:51:52 -0700146
147 // Set thread state to kNative
TDYa1275bb86012012-04-11 05:57:28 -0700148 irb_.StoreToObjectOffset(thread_object_addr,
149 Thread::StateOffset().Int32Value(),
150 irb_.getInt32(kNative));
TDYa12728f1a142012-03-15 21:51:52 -0700151
152 // Get callee code_addr
TDYa1270b686e52012-04-09 22:43:35 -0700153 llvm::Value* code_addr =
TDYa1275bb86012012-04-11 05:57:28 -0700154 irb_.LoadFromObjectOffset(method_object_addr,
155 Method::NativeMethodOffset().Int32Value(),
156 GetFunctionType(method_idx_, is_static, true)->getPointerTo());
TDYa12728f1a142012-03-15 21:51:52 -0700157
158 // Load actual parameters
159 std::vector<llvm::Value*> args;
160
TDYa12731a99332012-03-19 02:58:02 -0700161 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700162 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700163
TDYa12731a99332012-03-19 02:58:02 -0700164 // Variables for GetElementPtr
165 llvm::Value* gep_index[] = {
166 irb_.getInt32(0), // No displacement for shadow frame pointer
167 irb_.getInt32(1), // SIRT
168 NULL,
169 };
170
TDYa12728f1a142012-03-15 21:51:52 -0700171 size_t sirt_member_index = 0;
172
TDYa1279000a842012-03-23 17:43:08 -0700173 // Store the "this object or class object" to SIRT
174 gep_index[2] = irb_.getInt32(sirt_member_index++);
175 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
176 irb_.CreateStore(this_object_or_class_object, sirt_field_addr);
177 // Push the "this object or class object" to out args
178 args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
TDYa12731a99332012-03-19 02:58:02 -0700179 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700180 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
181 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700182 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700183 gep_index[2] = irb_.getInt32(sirt_member_index++);
184 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
185 irb_.CreateStore(arg_iter, sirt_field_addr);
186 // Note null is placed in the SIRT but the jobject passed to the native code must be null
187 // (not a pointer into the SIRT as with regular references).
188 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
189 llvm::Value* arg =
190 irb_.CreateSelect(equal_null,
191 irb_.getJNull(),
192 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
193 args.push_back(arg);
194 } else {
195 args.push_back(arg_iter);
196 }
197 }
198
TDYa1279000a842012-03-23 17:43:08 -0700199 // Acquire lock for synchronized methods.
200 if (is_synchronized) {
201 // Acquire lock
TDYa127706e9b62012-04-19 12:24:26 -0700202 irb_.CreateCall2(irb_.GetRuntime(LockObject),
203 this_object_or_class_object,
204 thread_object_addr);
TDYa1279000a842012-03-23 17:43:08 -0700205
206 // Check exception pending
TDYa1270b686e52012-04-09 22:43:35 -0700207 llvm::Value* exception_pending = irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
TDYa1279000a842012-03-23 17:43:08 -0700208
209 // Create two basic block for branch
210 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "B.cont", func_);
211 llvm::BasicBlock* block_exception_ = llvm::BasicBlock::Create(*context_, "B.exception", func_);
212
213 // Branch by exception_pending
214 irb_.CreateCondBr(exception_pending, block_exception_, block_cont);
215
216
217 // If exception pending
218 irb_.SetInsertPoint(block_exception_);
219 // TODO: Set thread state?
220 // Pop the shadow frame
TDYa1270b686e52012-04-09 22:43:35 -0700221 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
TDYa1279000a842012-03-23 17:43:08 -0700222 // Unwind
223 if (return_shorty != 'V') {
224 irb_.CreateRet(irb_.getJZero(return_shorty));
225 } else {
226 irb_.CreateRetVoid();
227 }
228
229 // If no exception pending
230 irb_.SetInsertPoint(block_cont);
231 }
TDYa12728f1a142012-03-15 21:51:52 -0700232
233 // saved_local_ref_cookie = env->local_ref_cookie
234 llvm::Value* saved_local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700235 irb_.LoadFromObjectOffset(jni_env_object_addr,
236 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
237 irb_.getInt32Ty());
TDYa12728f1a142012-03-15 21:51:52 -0700238
239 // env->local_ref_cookie = env->locals.segment_state
240 llvm::Value* segment_state =
TDYa1275bb86012012-04-11 05:57:28 -0700241 irb_.LoadFromObjectOffset(jni_env_object_addr,
242 JNIEnvExt::SegmentStateOffset().Int32Value(),
243 irb_.getInt32Ty());
244 irb_.StoreToObjectOffset(jni_env_object_addr,
245 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
246 segment_state);
TDYa12728f1a142012-03-15 21:51:52 -0700247
248
249 // Call!!!
250 llvm::Value* retval = irb_.CreateCall(code_addr, args);
251
252
TDYa1279000a842012-03-23 17:43:08 -0700253 // Release lock for synchronized methods.
254 if (is_synchronized) {
TDYa127706e9b62012-04-19 12:24:26 -0700255 irb_.CreateCall2(irb_.GetRuntime(UnlockObject),
256 this_object_or_class_object,
257 thread_object_addr);
TDYa1279000a842012-03-23 17:43:08 -0700258 }
259
TDYa12728f1a142012-03-15 21:51:52 -0700260 // Set thread state to kRunnable
TDYa1275bb86012012-04-11 05:57:28 -0700261 irb_.StoreToObjectOffset(thread_object_addr,
262 Thread::StateOffset().Int32Value(),
263 irb_.getInt32(kRunnable));
TDYa12728f1a142012-03-15 21:51:52 -0700264
TDYa12769eafaa2012-04-17 10:51:25 -0700265 // Do a suspend check
TDYa127853cd092012-04-21 22:15:31 -0700266 irb_.CreateCall(irb_.GetRuntime(TestSuspend), thread_object_addr);
TDYa12769eafaa2012-04-17 10:51:25 -0700267
TDYa1279000a842012-03-23 17:43:08 -0700268 if (return_shorty == 'L') {
TDYa12728f1a142012-03-15 21:51:52 -0700269 // If the return value is reference, it may point to SIRT, we should decode it.
TDYa1270b686e52012-04-09 22:43:35 -0700270 retval = irb_.CreateCall2(irb_.GetRuntime(DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700271 thread_object_addr,
272 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700273 }
274
275 // env->locals.segment_state = env->local_ref_cookie
276 llvm::Value* local_ref_cookie =
TDYa1275bb86012012-04-11 05:57:28 -0700277 irb_.LoadFromObjectOffset(jni_env_object_addr,
278 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
279 irb_.getInt32Ty());
280 irb_.StoreToObjectOffset(jni_env_object_addr,
281 JNIEnvExt::SegmentStateOffset().Int32Value(),
282 local_ref_cookie);
TDYa12728f1a142012-03-15 21:51:52 -0700283
284 // env->local_ref_cookie = saved_local_ref_cookie
TDYa1275bb86012012-04-11 05:57:28 -0700285 irb_.StoreToObjectOffset(jni_env_object_addr,
286 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
287 saved_local_ref_cookie);
TDYa12728f1a142012-03-15 21:51:52 -0700288
289 // Pop the shadow frame
TDYa1270b686e52012-04-09 22:43:35 -0700290 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
TDYa12728f1a142012-03-15 21:51:52 -0700291
292 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700293 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700294 irb_.CreateRet(retval);
295 } else {
296 irb_.CreateRetVoid();
297 }
298
TDYa12728f1a142012-03-15 21:51:52 -0700299 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -0700300 VERIFY_LLVM_FUNCTION(*func_);
TDYa12728f1a142012-03-15 21:51:52 -0700301
TDYa1270200d072012-04-17 20:55:08 -0700302 // Add the memory usage approximation of the compilation unit
303 cunit_->AddMemUsageApproximation((sirt_size * 4 + 50) * 50);
304 // NOTE: We will emit 4 LLVM instructions per object argument,
305 // And about 50 instructions for other operations. (Some runtime support will be inlined.)
306 // Beside, we guess that we have to use 50 bytes to represent one LLVM instruction.
307
Logan Chien110bcba2012-04-16 19:11:28 +0800308 CompiledMethod* compiled_method =
309 new CompiledMethod(cunit_->GetInstructionSet(),
310 cunit_->GetElfIndex(),
311 elf_func_idx_);
312
313 cunit_->RegisterCompiledMethod(func_, compiled_method);
314
315 return compiled_method;
Logan Chien88894ee2012-02-13 16:42:22 +0800316}
317
318
319void JniCompiler::CreateFunction() {
320 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +0800321 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien88894ee2012-02-13 16:42:22 +0800322
323 // Get function type
324 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700325 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800326
327 // Create function
328 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
329 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700330
331 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700332 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
333
334 // Set insert point
335 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800336}
337
338
339llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700340 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800341 // Get method signature
342 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
343
344 uint32_t shorty_size;
345 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
346 CHECK_GE(shorty_size, 1u);
347
348 // Get return type
349 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
350
351 // Get argument type
352 std::vector<llvm::Type*> args_type;
353
354 args_type.push_back(irb_.getJObjectTy()); // method object pointer
355
TDYa1279000a842012-03-23 17:43:08 -0700356 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700357 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700358 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700359 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800360 }
361
362 for (uint32_t i = 1; i < shorty_size; ++i) {
363 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
364 }
365
366 return llvm::FunctionType::get(ret_type, args_type, false);
367}
368
Logan Chien88894ee2012-02-13 16:42:22 +0800369} // namespace compiler_llvm
370} // namespace art