blob: 097c4f11d037f20d1db1a53d8d40d336bb910ae3 [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
TDYa12728f1a142012-03-15 21:51:52 -0700136 // Get JNIEnv
137 llvm::Value* jni_env_object_addr = LoadFromObjectOffset(thread_object_addr,
138 Thread::JniEnvOffset().Int32Value(),
139 irb_.getJObjectTy());
140
141 // Set thread state to kNative
TDYa12731a99332012-03-19 02:58:02 -0700142 StoreToObjectOffset(thread_object_addr,
143 Thread::StateOffset().Int32Value(),
144 irb_.getInt32(Thread::kNative));
TDYa12728f1a142012-03-15 21:51:52 -0700145
146 // Get callee code_addr
147 llvm::Value* code_addr =
148 LoadFromObjectOffset(method_object_addr,
149 Method::NativeMethodOffset().Int32Value(),
150 GetFunctionType(method_idx_, is_static, true)->getPointerTo());
151
152
153 // Load actual parameters
154 std::vector<llvm::Value*> args;
155
TDYa12731a99332012-03-19 02:58:02 -0700156 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700157 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700158
TDYa12731a99332012-03-19 02:58:02 -0700159 // Variables for GetElementPtr
160 llvm::Value* gep_index[] = {
161 irb_.getInt32(0), // No displacement for shadow frame pointer
162 irb_.getInt32(1), // SIRT
163 NULL,
164 };
165
TDYa12728f1a142012-03-15 21:51:52 -0700166 size_t sirt_member_index = 0;
167
TDYa1279000a842012-03-23 17:43:08 -0700168 // Store the "this object or class object" to SIRT
169 gep_index[2] = irb_.getInt32(sirt_member_index++);
170 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
171 irb_.CreateStore(this_object_or_class_object, sirt_field_addr);
172 // Push the "this object or class object" to out args
173 args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
TDYa12731a99332012-03-19 02:58:02 -0700174 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700175 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
176 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700177 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700178 gep_index[2] = irb_.getInt32(sirt_member_index++);
179 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
180 irb_.CreateStore(arg_iter, sirt_field_addr);
181 // Note null is placed in the SIRT but the jobject passed to the native code must be null
182 // (not a pointer into the SIRT as with regular references).
183 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
184 llvm::Value* arg =
185 irb_.CreateSelect(equal_null,
186 irb_.getJNull(),
187 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
188 args.push_back(arg);
189 } else {
190 args.push_back(arg_iter);
191 }
192 }
193
TDYa1279000a842012-03-23 17:43:08 -0700194 // Acquire lock for synchronized methods.
195 if (is_synchronized) {
196 // Acquire lock
197 irb_.CreateCall(irb_.GetRuntime(runtime_support::LockObject), this_object_or_class_object);
198
199 // Check exception pending
200 llvm::Value* exception_pending =
201 irb_.CreateCall(irb_.GetRuntime(runtime_support::IsExceptionPending));
202
203 // Create two basic block for branch
204 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "B.cont", func_);
205 llvm::BasicBlock* block_exception_ = llvm::BasicBlock::Create(*context_, "B.exception", func_);
206
207 // Branch by exception_pending
208 irb_.CreateCondBr(exception_pending, block_exception_, block_cont);
209
210
211 // If exception pending
212 irb_.SetInsertPoint(block_exception_);
213 // TODO: Set thread state?
214 // Pop the shadow frame
215 irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame));
216 // Unwind
217 if (return_shorty != 'V') {
218 irb_.CreateRet(irb_.getJZero(return_shorty));
219 } else {
220 irb_.CreateRetVoid();
221 }
222
223 // If no exception pending
224 irb_.SetInsertPoint(block_cont);
225 }
TDYa12728f1a142012-03-15 21:51:52 -0700226
227 // saved_local_ref_cookie = env->local_ref_cookie
228 llvm::Value* saved_local_ref_cookie =
229 LoadFromObjectOffset(jni_env_object_addr,
230 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
231 irb_.getInt32Ty());
232
233 // env->local_ref_cookie = env->locals.segment_state
234 llvm::Value* segment_state =
235 LoadFromObjectOffset(jni_env_object_addr,
236 JNIEnvExt::SegmentStateOffset().Int32Value(),
237 irb_.getInt32Ty());
238 StoreToObjectOffset(jni_env_object_addr,
239 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa12728f1a142012-03-15 21:51:52 -0700240 segment_state);
241
242
243 // Call!!!
244 llvm::Value* retval = irb_.CreateCall(code_addr, args);
245
246
TDYa1279000a842012-03-23 17:43:08 -0700247 // Release lock for synchronized methods.
248 if (is_synchronized) {
249 irb_.CreateCall(irb_.GetRuntime(runtime_support::UnlockObject), this_object_or_class_object);
250 }
251
TDYa12728f1a142012-03-15 21:51:52 -0700252 // Set thread state to kRunnable
TDYa12731a99332012-03-19 02:58:02 -0700253 StoreToObjectOffset(thread_object_addr,
254 Thread::StateOffset().Int32Value(),
255 irb_.getInt32(Thread::kRunnable));
TDYa12728f1a142012-03-15 21:51:52 -0700256
TDYa1279000a842012-03-23 17:43:08 -0700257 if (return_shorty == 'L') {
TDYa12728f1a142012-03-15 21:51:52 -0700258 // If the return value is reference, it may point to SIRT, we should decode it.
259 retval = irb_.CreateCall2(irb_.GetRuntime(runtime_support::DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700260 thread_object_addr,
261 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700262 }
263
264 // env->locals.segment_state = env->local_ref_cookie
265 llvm::Value* local_ref_cookie =
266 LoadFromObjectOffset(jni_env_object_addr,
267 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
268 irb_.getInt32Ty());
269 StoreToObjectOffset(jni_env_object_addr,
270 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa12728f1a142012-03-15 21:51:52 -0700271 local_ref_cookie);
272
273 // env->local_ref_cookie = saved_local_ref_cookie
TDYa12731a99332012-03-19 02:58:02 -0700274 StoreToObjectOffset(jni_env_object_addr,
275 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
276 saved_local_ref_cookie);
TDYa12728f1a142012-03-15 21:51:52 -0700277
278 // Pop the shadow frame
279 irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame));
280
281 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700282 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700283 irb_.CreateRet(retval);
284 } else {
285 irb_.CreateRetVoid();
286 }
287
TDYa12728f1a142012-03-15 21:51:52 -0700288 // Verify the generated bitcode
289 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
290
Logan Chien6920bce2012-03-17 21:44:01 +0800291 return new CompiledMethod(cunit_->GetInstructionSet(),
292 cunit_->GetElfIndex());
Logan Chien88894ee2012-02-13 16:42:22 +0800293}
294
295
296void JniCompiler::CreateFunction() {
297 // LLVM function name
298 std::string func_name(LLVMLongName(method_));
299
300 // Get function type
301 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700302 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800303
304 // Create function
305 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
306 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700307
308 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700309 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
310
311 // Set insert point
312 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800313}
314
315
316llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700317 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800318 // Get method signature
319 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
320
321 uint32_t shorty_size;
322 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
323 CHECK_GE(shorty_size, 1u);
324
325 // Get return type
326 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
327
328 // Get argument type
329 std::vector<llvm::Type*> args_type;
330
331 args_type.push_back(irb_.getJObjectTy()); // method object pointer
332
TDYa1279000a842012-03-23 17:43:08 -0700333 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700334 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700335 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700336 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800337 }
338
339 for (uint32_t i = 1; i < shorty_size; ++i) {
340 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
341 }
342
343 return llvm::FunctionType::get(ret_type, args_type, false);
344}
345
TDYa12731a99332012-03-19 02:58:02 -0700346llvm::Value* JniCompiler::LoadFromObjectOffset(llvm::Value* object_addr,
347 int32_t offset,
TDYa12728f1a142012-03-15 21:51:52 -0700348 llvm::Type* type) {
349 // Convert offset to llvm::value
350 llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset);
351 // Calculate the value's address
352 llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
353 // Load
354 return irb_.CreateLoad(value_addr);
355}
356
TDYa12731a99332012-03-19 02:58:02 -0700357void JniCompiler::StoreToObjectOffset(llvm::Value* object_addr,
358 int32_t offset,
359 llvm::Value* new_value) {
TDYa12728f1a142012-03-15 21:51:52 -0700360 // Convert offset to llvm::value
361 llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset);
362 // Calculate the value's address
TDYa12731a99332012-03-19 02:58:02 -0700363 llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr,
364 llvm_offset,
365 new_value->getType()->getPointerTo());
TDYa12728f1a142012-03-15 21:51:52 -0700366 // Store
TDYa12731a99332012-03-19 02:58:02 -0700367 irb_.CreateStore(new_value, value_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700368}
Logan Chien88894ee2012-02-13 16:42:22 +0800369
370} // namespace compiler_llvm
371} // namespace art