blob: 936fb669870e3f111d6dae198a182e77a86f9131 [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/Analysis/Verifier.h>
34#include <llvm/BasicBlock.h>
Logan Chien88894ee2012-02-13 16:42:22 +080035#include <llvm/DerivedTypes.h>
36#include <llvm/Function.h>
37#include <llvm/Type.h>
38
39namespace art {
40namespace compiler_llvm {
41
42
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_),
55 method_(dex_cache_->GetResolvedMethod(method_idx_)) {
56
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 =
89 LoadFromObjectOffset(method_object_addr,
90 Method::DeclaringClassOffset().Int32Value(),
91 irb_.getJObjectTy());
92 }
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) {
101 arg_iter->setName(StringPrintf("a%u", i));
102 if (arg_iter->getType() == irb_.getJObjectTy()) {
103 ++sirt_size;
104 }
105 }
106
TDYa12731a99332012-03-19 02:58:02 -0700107 // Get thread object
TDYa12728f1a142012-03-15 21:51:52 -0700108 llvm::Value* thread_object_addr =
109 irb_.CreateCall(irb_.GetRuntime(runtime_support::GetCurrentThread));
110
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
127 // Store the number of the pointer slots
TDYa12731a99332012-03-19 02:58:02 -0700128 StoreToObjectOffset(shadow_frame_,
129 ShadowFrame::NumberOfReferencesOffset(),
130 irb_.getInt32(sirt_size));
TDYa12728f1a142012-03-15 21:51:52 -0700131
132 // Push the shadow frame
133 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
134 irb_.CreateCall(irb_.GetRuntime(runtime_support::PushShadowFrame), shadow_frame_upcast);
135
136 // Set top of managed stack to the method field in the SIRT
TDYa12731a99332012-03-19 02:58:02 -0700137 StoreToObjectOffset(thread_object_addr,
138 Thread::TopOfManagedStackOffset().Int32Value(),
139 method_field_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700140
141 // Get JNIEnv
142 llvm::Value* jni_env_object_addr = LoadFromObjectOffset(thread_object_addr,
143 Thread::JniEnvOffset().Int32Value(),
144 irb_.getJObjectTy());
145
146 // Set thread state to kNative
TDYa12731a99332012-03-19 02:58:02 -0700147 StoreToObjectOffset(thread_object_addr,
148 Thread::StateOffset().Int32Value(),
149 irb_.getInt32(Thread::kNative));
TDYa12728f1a142012-03-15 21:51:52 -0700150
151 // Get callee code_addr
152 llvm::Value* code_addr =
153 LoadFromObjectOffset(method_object_addr,
154 Method::NativeMethodOffset().Int32Value(),
155 GetFunctionType(method_idx_, is_static, true)->getPointerTo());
156
157
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
202 irb_.CreateCall(irb_.GetRuntime(runtime_support::LockObject), this_object_or_class_object);
203
204 // Check exception pending
205 llvm::Value* exception_pending =
206 irb_.CreateCall(irb_.GetRuntime(runtime_support::IsExceptionPending));
207
208 // Create two basic block for branch
209 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "B.cont", func_);
210 llvm::BasicBlock* block_exception_ = llvm::BasicBlock::Create(*context_, "B.exception", func_);
211
212 // Branch by exception_pending
213 irb_.CreateCondBr(exception_pending, block_exception_, block_cont);
214
215
216 // If exception pending
217 irb_.SetInsertPoint(block_exception_);
218 // TODO: Set thread state?
219 // Pop the shadow frame
220 irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame));
221 // Unwind
222 if (return_shorty != 'V') {
223 irb_.CreateRet(irb_.getJZero(return_shorty));
224 } else {
225 irb_.CreateRetVoid();
226 }
227
228 // If no exception pending
229 irb_.SetInsertPoint(block_cont);
230 }
TDYa12728f1a142012-03-15 21:51:52 -0700231
232 // saved_local_ref_cookie = env->local_ref_cookie
233 llvm::Value* saved_local_ref_cookie =
234 LoadFromObjectOffset(jni_env_object_addr,
235 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
236 irb_.getInt32Ty());
237
238 // env->local_ref_cookie = env->locals.segment_state
239 llvm::Value* segment_state =
240 LoadFromObjectOffset(jni_env_object_addr,
241 JNIEnvExt::SegmentStateOffset().Int32Value(),
242 irb_.getInt32Ty());
243 StoreToObjectOffset(jni_env_object_addr,
244 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa12728f1a142012-03-15 21:51:52 -0700245 segment_state);
246
247
248 // Call!!!
249 llvm::Value* retval = irb_.CreateCall(code_addr, args);
250
251
TDYa1279000a842012-03-23 17:43:08 -0700252 // Release lock for synchronized methods.
253 if (is_synchronized) {
254 irb_.CreateCall(irb_.GetRuntime(runtime_support::UnlockObject), this_object_or_class_object);
255 }
256
TDYa12728f1a142012-03-15 21:51:52 -0700257 // Set thread state to kRunnable
TDYa12731a99332012-03-19 02:58:02 -0700258 StoreToObjectOffset(thread_object_addr,
259 Thread::StateOffset().Int32Value(),
260 irb_.getInt32(Thread::kRunnable));
TDYa12728f1a142012-03-15 21:51:52 -0700261
TDYa1279000a842012-03-23 17:43:08 -0700262 if (return_shorty == 'L') {
TDYa12728f1a142012-03-15 21:51:52 -0700263 // If the return value is reference, it may point to SIRT, we should decode it.
264 retval = irb_.CreateCall2(irb_.GetRuntime(runtime_support::DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700265 thread_object_addr,
266 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700267 }
268
269 // env->locals.segment_state = env->local_ref_cookie
270 llvm::Value* local_ref_cookie =
271 LoadFromObjectOffset(jni_env_object_addr,
272 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
273 irb_.getInt32Ty());
274 StoreToObjectOffset(jni_env_object_addr,
275 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa12728f1a142012-03-15 21:51:52 -0700276 local_ref_cookie);
277
278 // env->local_ref_cookie = saved_local_ref_cookie
TDYa12731a99332012-03-19 02:58:02 -0700279 StoreToObjectOffset(jni_env_object_addr,
280 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
281 saved_local_ref_cookie);
TDYa12728f1a142012-03-15 21:51:52 -0700282
283 // Pop the shadow frame
284 irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame));
285
286 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700287 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700288 irb_.CreateRet(retval);
289 } else {
290 irb_.CreateRetVoid();
291 }
292
TDYa12728f1a142012-03-15 21:51:52 -0700293 // Verify the generated bitcode
294 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
295
Logan Chien6920bce2012-03-17 21:44:01 +0800296 return new CompiledMethod(cunit_->GetInstructionSet(),
297 cunit_->GetElfIndex());
Logan Chien88894ee2012-02-13 16:42:22 +0800298}
299
300
301void JniCompiler::CreateFunction() {
302 // LLVM function name
303 std::string func_name(LLVMLongName(method_));
304
305 // Get function type
306 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700307 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800308
309 // Create function
310 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
311 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700312
313 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700314 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
315
316 // Set insert point
317 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800318}
319
320
321llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700322 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800323 // Get method signature
324 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
325
326 uint32_t shorty_size;
327 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
328 CHECK_GE(shorty_size, 1u);
329
330 // Get return type
331 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
332
333 // Get argument type
334 std::vector<llvm::Type*> args_type;
335
336 args_type.push_back(irb_.getJObjectTy()); // method object pointer
337
TDYa1279000a842012-03-23 17:43:08 -0700338 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700339 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700340 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700341 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800342 }
343
344 for (uint32_t i = 1; i < shorty_size; ++i) {
345 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
346 }
347
348 return llvm::FunctionType::get(ret_type, args_type, false);
349}
350
TDYa12731a99332012-03-19 02:58:02 -0700351llvm::Value* JniCompiler::LoadFromObjectOffset(llvm::Value* object_addr,
352 int32_t offset,
TDYa12728f1a142012-03-15 21:51:52 -0700353 llvm::Type* type) {
354 // Convert offset to llvm::value
355 llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset);
356 // Calculate the value's address
357 llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
358 // Load
359 return irb_.CreateLoad(value_addr);
360}
361
TDYa12731a99332012-03-19 02:58:02 -0700362void JniCompiler::StoreToObjectOffset(llvm::Value* object_addr,
363 int32_t offset,
364 llvm::Value* new_value) {
TDYa12728f1a142012-03-15 21:51:52 -0700365 // Convert offset to llvm::value
366 llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset);
367 // Calculate the value's address
TDYa12731a99332012-03-19 02:58:02 -0700368 llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr,
369 llvm_offset,
370 new_value->getType()->getPointerTo());
TDYa12728f1a142012-03-15 21:51:52 -0700371 // Store
TDYa12731a99332012-03-19 02:58:02 -0700372 irb_.CreateStore(new_value, value_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700373}
Logan Chien88894ee2012-02-13 16:42:22 +0800374
375} // namespace compiler_llvm
376} // namespace art