blob: 181f8c32647817fbe199f87ed34923d4c86e9a22 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Logan Chien88894ee2012-02-13 16:42:22 +080020#include "class_linker.h"
Logan Chien8b977d32012-02-21 19:14:55 +080021#include "compilation_unit.h"
Logan Chien88894ee2012-02-13 16:42:22 +080022#include "compiled_method.h"
23#include "compiler.h"
24#include "compiler_llvm.h"
25#include "ir_builder.h"
Ian Rogers98573f92013-01-30 17:26:32 -080026#include "mirror/abstract_method.h"
Logan Chien88894ee2012-02-13 16:42:22 +080027#include "oat_compilation_unit.h"
Logan Chien88894ee2012-02-13 16:42:22 +080028#include "runtime.h"
TDYa12728f1a142012-03-15 21:51:52 -070029#include "runtime_support_func.h"
Ian Rogers785e6182013-01-30 18:28:29 -080030#include "stack.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>
TDYa1273d71d802012-08-15 03:47:03 -070036#include <llvm/ADT/SmallVector.h>
Logan Chien88894ee2012-02-13 16:42:22 +080037#include <llvm/Type.h>
38
39namespace art {
40namespace compiler_llvm {
41
TDYa1270b686e52012-04-09 22:43:35 -070042using namespace runtime_support;
Logan Chien88894ee2012-02-13 16:42:22 +080043
Logan Chien8b977d32012-02-21 19:14:55 +080044JniCompiler::JniCompiler(CompilationUnit* cunit,
Logan Chien88894ee2012-02-13 16:42:22 +080045 Compiler const& compiler,
46 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080047: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
48 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()),
Logan Chien88894ee2012-02-13 16:42:22 +080049 oat_compilation_unit_(oat_compilation_unit),
50 access_flags_(oat_compilation_unit->access_flags_),
51 method_idx_(oat_compilation_unit->method_idx_),
Shih-wei Liaocd05a622012-08-15 00:02:05 -070052 dex_file_(oat_compilation_unit->dex_file_) {
Logan Chien88894ee2012-02-13 16:42:22 +080053
54 // Check: Ensure that JNI compiler will only get "native" method
55 CHECK((access_flags_ & kAccNative) != 0);
56}
57
58
59CompiledMethod* JniCompiler::Compile() {
TDYa1279000a842012-03-23 17:43:08 -070060 const bool is_static = (access_flags_ & kAccStatic) != 0;
61 const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0;
62 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_);
63 char const return_shorty = dex_file_->GetMethodShorty(method_id)[0];
64 llvm::Value* this_object_or_class_object;
TDYa12728f1a142012-03-15 21:51:52 -070065
Logan Chien88894ee2012-02-13 16:42:22 +080066 CreateFunction();
67
TDYa12728f1a142012-03-15 21:51:52 -070068 // Set argument name
69 llvm::Function::arg_iterator arg_begin(func_->arg_begin());
70 llvm::Function::arg_iterator arg_end(func_->arg_end());
71 llvm::Function::arg_iterator arg_iter(arg_begin);
72
73 DCHECK_NE(arg_iter, arg_end);
74 arg_iter->setName("method");
75 llvm::Value* method_object_addr = arg_iter++;
76
TDYa1279000a842012-03-23 17:43:08 -070077 if (!is_static) {
78 // Non-static, the second argument is "this object"
79 this_object_or_class_object = arg_iter++;
80 } else {
81 // Load class object
82 this_object_or_class_object =
TDYa1275bb86012012-04-11 05:57:28 -070083 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -080084 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -070085 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -070086 kTBAAConstJObject);
TDYa1279000a842012-03-23 17:43:08 -070087 }
88 // Actual argument (ignore method and this object)
TDYa12728f1a142012-03-15 21:51:52 -070089 arg_begin = arg_iter;
90
91 // Count the number of Object* arguments
TDYa1279000a842012-03-23 17:43:08 -070092 uint32_t sirt_size = 1;
93 // "this" object pointer for non-static
94 // "class" object pointer for static
TDYa12728f1a142012-03-15 21:51:52 -070095 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
TDYa12767ae8ff2012-05-02 19:08:02 -070096#if !defined(NDEBUG)
TDYa12728f1a142012-03-15 21:51:52 -070097 arg_iter->setName(StringPrintf("a%u", i));
TDYa12767ae8ff2012-05-02 19:08:02 -070098#endif
TDYa12728f1a142012-03-15 21:51:52 -070099 if (arg_iter->getType() == irb_.getJObjectTy()) {
100 ++sirt_size;
101 }
102 }
103
TDYa12728f1a142012-03-15 21:51:52 -0700104 // Shadow stack
TDYa127ce4cc0d2012-11-18 16:59:53 -0800105 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
TDYa1279000a842012-03-23 17:43:08 -0700106 llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
TDYa12728f1a142012-03-15 21:51:52 -0700107
TDYa127c8dc1012012-04-19 07:03:33 -0700108 // Store the dex pc
TDYa1275bb86012012-04-11 05:57:28 -0700109 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127c8dc1012012-04-19 07:03:33 -0700110 ShadowFrame::DexPCOffset(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700111 irb_.getInt32(DexFile::kDexNoIndex),
TDYa127d955bec2012-05-11 10:54:02 -0700112 kTBAAShadowFrame);
TDYa127a0f746b2012-04-09 22:46:30 -0700113
TDYa12728f1a142012-03-15 21:51:52 -0700114 // Push the shadow frame
115 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
TDYa1270de52be2012-05-27 20:49:31 -0700116 llvm::Value* old_shadow_frame =
TDYa127ce4cc0d2012-11-18 16:59:53 -0800117 irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr, sirt_size);
TDYa12728f1a142012-03-15 21:51:52 -0700118
TDYa12728f1a142012-03-15 21:51:52 -0700119 // Get JNIEnv
TDYa1275bb86012012-04-11 05:57:28 -0700120 llvm::Value* jni_env_object_addr =
TDYa127de479be2012-05-31 08:03:26 -0700121 irb_.Runtime().EmitLoadFromThreadOffset(Thread::JniEnvOffset().Int32Value(),
TDYa1273d71d802012-08-15 03:47:03 -0700122 irb_.getJObjectTy(),
TDYa127ce4cc0d2012-11-18 16:59:53 -0800123 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700124
125 // Get callee code_addr
TDYa1270b686e52012-04-09 22:43:35 -0700126 llvm::Value* code_addr =
TDYa1275bb86012012-04-11 05:57:28 -0700127 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800128 mirror::AbstractMethod::NativeMethodOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700129 GetFunctionType(method_idx_, is_static, true)->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -0800130 kTBAARuntimeInfo);
TDYa12728f1a142012-03-15 21:51:52 -0700131
132 // Load actual parameters
133 std::vector<llvm::Value*> args;
134
TDYa12731a99332012-03-19 02:58:02 -0700135 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700136 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700137
TDYa12731a99332012-03-19 02:58:02 -0700138 // Variables for GetElementPtr
139 llvm::Value* gep_index[] = {
140 irb_.getInt32(0), // No displacement for shadow frame pointer
141 irb_.getInt32(1), // SIRT
142 NULL,
143 };
144
TDYa12728f1a142012-03-15 21:51:52 -0700145 size_t sirt_member_index = 0;
146
TDYa1279000a842012-03-23 17:43:08 -0700147 // Store the "this object or class object" to SIRT
148 gep_index[2] = irb_.getInt32(sirt_member_index++);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800149 llvm::Value* sirt_field_addr = irb_.CreateBitCast(irb_.CreateGEP(shadow_frame_, gep_index),
150 irb_.getJObjectTy()->getPointerTo());
TDYa127d955bec2012-05-11 10:54:02 -0700151 irb_.CreateStore(this_object_or_class_object, sirt_field_addr, kTBAAShadowFrame);
TDYa1279000a842012-03-23 17:43:08 -0700152 // Push the "this object or class object" to out args
TDYa1273d71d802012-08-15 03:47:03 -0700153 this_object_or_class_object = irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy());
154 args.push_back(this_object_or_class_object);
TDYa12731a99332012-03-19 02:58:02 -0700155 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700156 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
157 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700158 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700159 gep_index[2] = irb_.getInt32(sirt_member_index++);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800160 llvm::Value* sirt_field_addr = irb_.CreateBitCast(irb_.CreateGEP(shadow_frame_, gep_index),
161 irb_.getJObjectTy()->getPointerTo());
TDYa127d955bec2012-05-11 10:54:02 -0700162 irb_.CreateStore(arg_iter, sirt_field_addr, kTBAAShadowFrame);
TDYa12728f1a142012-03-15 21:51:52 -0700163 // Note null is placed in the SIRT but the jobject passed to the native code must be null
164 // (not a pointer into the SIRT as with regular references).
165 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
166 llvm::Value* arg =
167 irb_.CreateSelect(equal_null,
168 irb_.getJNull(),
169 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
170 args.push_back(arg);
171 } else {
172 args.push_back(arg_iter);
173 }
174 }
175
TDYa1273d71d802012-08-15 03:47:03 -0700176 llvm::Value* saved_local_ref_cookie;
177 { // JniMethodStart
178 RuntimeId func_id = is_synchronized ? JniMethodStartSynchronized
179 : JniMethodStart;
180 llvm::SmallVector<llvm::Value*, 2> args;
181 if (is_synchronized) {
182 args.push_back(this_object_or_class_object);
183 }
184 args.push_back(irb_.Runtime().EmitGetCurrentThread());
185 saved_local_ref_cookie =
186 irb_.CreateCall(irb_.GetRuntime(func_id), args);
TDYa1279000a842012-03-23 17:43:08 -0700187 }
TDYa12728f1a142012-03-15 21:51:52 -0700188
TDYa12728f1a142012-03-15 21:51:52 -0700189 // Call!!!
190 llvm::Value* retval = irb_.CreateCall(code_addr, args);
191
TDYa1273d71d802012-08-15 03:47:03 -0700192 { // JniMethodEnd
193 bool is_return_ref = return_shorty == 'L';
194 RuntimeId func_id =
195 is_return_ref ? (is_synchronized ? JniMethodEndWithReferenceSynchronized
196 : JniMethodEndWithReference)
197 : (is_synchronized ? JniMethodEndSynchronized
198 : JniMethodEnd);
199 llvm::SmallVector<llvm::Value*, 4> args;
200 if (is_return_ref) {
201 args.push_back(retval);
202 }
203 args.push_back(saved_local_ref_cookie);
204 if (is_synchronized) {
205 args.push_back(this_object_or_class_object);
206 }
207 args.push_back(irb_.Runtime().EmitGetCurrentThread());
TDYa12728f1a142012-03-15 21:51:52 -0700208
TDYa1273d71d802012-08-15 03:47:03 -0700209 llvm::Value* decoded_jobject =
210 irb_.CreateCall(irb_.GetRuntime(func_id), args);
211
212 // Return decoded jobject if return reference.
213 if (is_return_ref) {
214 retval = decoded_jobject;
215 }
TDYa1279000a842012-03-23 17:43:08 -0700216 }
217
TDYa12728f1a142012-03-15 21:51:52 -0700218 // Pop the shadow frame
TDYa127de479be2012-05-31 08:03:26 -0700219 irb_.Runtime().EmitPopShadowFrame(old_shadow_frame);
TDYa12728f1a142012-03-15 21:51:52 -0700220
221 // Return!
TDYa1279000a842012-03-23 17:43:08 -0700222 if (return_shorty != 'V') {
TDYa12728f1a142012-03-15 21:51:52 -0700223 irb_.CreateRet(retval);
224 } else {
225 irb_.CreateRetVoid();
226 }
227
TDYa12728f1a142012-03-15 21:51:52 -0700228 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -0700229 VERIFY_LLVM_FUNCTION(*func_);
TDYa12728f1a142012-03-15 21:51:52 -0700230
Logan Chien971bf3f2012-05-01 15:47:55 +0800231 cunit_->Materialize();
TDYa1270200d072012-04-17 20:55:08 -0700232
Logan Chien971bf3f2012-05-01 15:47:55 +0800233 return new CompiledMethod(cunit_->GetInstructionSet(),
234 cunit_->GetCompiledCode());
Logan Chien88894ee2012-02-13 16:42:22 +0800235}
236
237
238void JniCompiler::CreateFunction() {
239 // LLVM function name
Logan Chien971bf3f2012-05-01 15:47:55 +0800240 std::string func_name(ElfFuncName(cunit_->GetIndex()));
Logan Chien88894ee2012-02-13 16:42:22 +0800241
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700242 const bool is_static = (access_flags_ & kAccStatic) != 0;
243
Logan Chien88894ee2012-02-13 16:42:22 +0800244 // Get function type
245 llvm::FunctionType* func_type =
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700246 GetFunctionType(method_idx_, is_static, false);
Logan Chien88894ee2012-02-13 16:42:22 +0800247
248 // Create function
249 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
250 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700251
252 // Create basic block
TDYa1279000a842012-03-23 17:43:08 -0700253 llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_);
254
255 // Set insert point
256 irb_.SetInsertPoint(basic_block);
Logan Chien88894ee2012-02-13 16:42:22 +0800257}
258
259
260llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa1279000a842012-03-23 17:43:08 -0700261 bool is_static, bool is_native_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800262 // Get method signature
263 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
264
265 uint32_t shorty_size;
Logan Chien12584172012-07-10 04:07:28 -0700266 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien88894ee2012-02-13 16:42:22 +0800267 CHECK_GE(shorty_size, 1u);
268
269 // Get return type
270 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
271
272 // Get argument type
273 std::vector<llvm::Type*> args_type;
274
275 args_type.push_back(irb_.getJObjectTy()); // method object pointer
276
TDYa1279000a842012-03-23 17:43:08 -0700277 if (!is_static || is_native_function) {
TDYa12728f1a142012-03-15 21:51:52 -0700278 // "this" object pointer for non-static
TDYa1279000a842012-03-23 17:43:08 -0700279 // "class" object pointer for static naitve
TDYa12728f1a142012-03-15 21:51:52 -0700280 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800281 }
282
283 for (uint32_t i = 1; i < shorty_size; ++i) {
284 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
285 }
286
287 return llvm::FunctionType::get(ret_type, args_type, false);
288}
289
Logan Chien88894ee2012-02-13 16:42:22 +0800290} // namespace compiler_llvm
291} // namespace art