Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 20 | #include "class_linker.h" |
| 21 | #include "compiled_method.h" |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame^] | 22 | #include "compiler/driver/compiler_driver.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 23 | #include "compiler_llvm.h" |
| 24 | #include "ir_builder.h" |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 25 | #include "llvm_compilation_unit.h" |
Ian Rogers | 98573f9 | 2013-01-30 17:26:32 -0800 | [diff] [blame] | 26 | #include "mirror/abstract_method.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 27 | #include "oat_compilation_unit.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 28 | #include "runtime.h" |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 29 | #include "runtime_support_func.h" |
Ian Rogers | 785e618 | 2013-01-30 18:28:29 -0800 | [diff] [blame] | 30 | #include "stack.h" |
Ian Rogers | 6cbd384 | 2013-01-30 18:53:39 -0800 | [diff] [blame] | 31 | #include "thread.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 32 | #include "utils_llvm.h" |
| 33 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 34 | #include <llvm/BasicBlock.h> |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 35 | #include <llvm/DerivedTypes.h> |
| 36 | #include <llvm/Function.h> |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 37 | #include <llvm/ADT/SmallVector.h> |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 38 | #include <llvm/Type.h> |
| 39 | |
| 40 | namespace art { |
| 41 | namespace compiler_llvm { |
| 42 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 43 | using namespace runtime_support; |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 44 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 45 | JniCompiler::JniCompiler(LlvmCompilationUnit* cunit, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame^] | 46 | const CompilerDriver& driver, |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 47 | OatCompilationUnit* oat_compilation_unit) |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame^] | 48 | : cunit_(cunit), driver_(&driver), module_(cunit_->GetModule()), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 49 | context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()), |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 50 | oat_compilation_unit_(oat_compilation_unit), |
| 51 | access_flags_(oat_compilation_unit->access_flags_), |
| 52 | method_idx_(oat_compilation_unit->method_idx_), |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame^] | 53 | dex_file_(oat_compilation_unit->dex_file_), |
| 54 | func_(NULL), elf_func_idx_(0) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 55 | |
| 56 | // Check: Ensure that JNI compiler will only get "native" method |
| 57 | CHECK((access_flags_ & kAccNative) != 0); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | CompiledMethod* JniCompiler::Compile() { |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 62 | const bool is_static = (access_flags_ & kAccStatic) != 0; |
| 63 | const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0; |
| 64 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_); |
| 65 | char const return_shorty = dex_file_->GetMethodShorty(method_id)[0]; |
| 66 | llvm::Value* this_object_or_class_object; |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 67 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 68 | CreateFunction(); |
| 69 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 70 | // Set argument name |
| 71 | llvm::Function::arg_iterator arg_begin(func_->arg_begin()); |
| 72 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 73 | llvm::Function::arg_iterator arg_iter(arg_begin); |
| 74 | |
| 75 | DCHECK_NE(arg_iter, arg_end); |
| 76 | arg_iter->setName("method"); |
| 77 | llvm::Value* method_object_addr = arg_iter++; |
| 78 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 79 | if (!is_static) { |
| 80 | // Non-static, the second argument is "this object" |
| 81 | this_object_or_class_object = arg_iter++; |
| 82 | } else { |
| 83 | // Load class object |
| 84 | this_object_or_class_object = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 85 | irb_.LoadFromObjectOffset(method_object_addr, |
Ian Rogers | 98573f9 | 2013-01-30 17:26:32 -0800 | [diff] [blame] | 86 | mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 87 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 88 | kTBAAConstJObject); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 89 | } |
| 90 | // Actual argument (ignore method and this object) |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 91 | arg_begin = arg_iter; |
| 92 | |
| 93 | // Count the number of Object* arguments |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 94 | uint32_t sirt_size = 1; |
| 95 | // "this" object pointer for non-static |
| 96 | // "class" object pointer for static |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 97 | for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) { |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 98 | #if !defined(NDEBUG) |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 99 | arg_iter->setName(StringPrintf("a%u", i)); |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 100 | #endif |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 101 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
| 102 | ++sirt_size; |
| 103 | } |
| 104 | } |
| 105 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 106 | // Shadow stack |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 107 | llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 108 | llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 109 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 110 | // Store the dex pc |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 111 | irb_.StoreToObjectOffset(shadow_frame_, |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 112 | ShadowFrame::DexPCOffset(), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 113 | irb_.getInt32(DexFile::kDexNoIndex), |
TDYa127 | d955bec | 2012-05-11 10:54:02 -0700 | [diff] [blame] | 114 | kTBAAShadowFrame); |
TDYa127 | a0f746b | 2012-04-09 22:46:30 -0700 | [diff] [blame] | 115 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 116 | // Push the shadow frame |
| 117 | llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0); |
TDYa127 | 0de52be | 2012-05-27 20:49:31 -0700 | [diff] [blame] | 118 | llvm::Value* old_shadow_frame = |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 119 | irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr, sirt_size); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 120 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 121 | // Get JNIEnv |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 122 | llvm::Value* jni_env_object_addr = |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 123 | irb_.Runtime().EmitLoadFromThreadOffset(Thread::JniEnvOffset().Int32Value(), |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 124 | irb_.getJObjectTy(), |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 125 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 126 | |
| 127 | // Get callee code_addr |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 128 | llvm::Value* code_addr = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 129 | irb_.LoadFromObjectOffset(method_object_addr, |
Ian Rogers | 98573f9 | 2013-01-30 17:26:32 -0800 | [diff] [blame] | 130 | mirror::AbstractMethod::NativeMethodOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 131 | GetFunctionType(method_idx_, is_static, true)->getPointerTo(), |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 132 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 133 | |
| 134 | // Load actual parameters |
| 135 | std::vector<llvm::Value*> args; |
| 136 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 137 | // The 1st parameter: JNIEnv* |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 138 | args.push_back(jni_env_object_addr); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 139 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 140 | // Variables for GetElementPtr |
| 141 | llvm::Value* gep_index[] = { |
| 142 | irb_.getInt32(0), // No displacement for shadow frame pointer |
| 143 | irb_.getInt32(1), // SIRT |
| 144 | NULL, |
| 145 | }; |
| 146 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 147 | size_t sirt_member_index = 0; |
| 148 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 149 | // Store the "this object or class object" to SIRT |
| 150 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 151 | llvm::Value* sirt_field_addr = irb_.CreateBitCast(irb_.CreateGEP(shadow_frame_, gep_index), |
| 152 | irb_.getJObjectTy()->getPointerTo()); |
TDYa127 | d955bec | 2012-05-11 10:54:02 -0700 | [diff] [blame] | 153 | irb_.CreateStore(this_object_or_class_object, sirt_field_addr, kTBAAShadowFrame); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 154 | // Push the "this object or class object" to out args |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 155 | this_object_or_class_object = irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()); |
| 156 | args.push_back(this_object_or_class_object); |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 157 | // Store arguments to SIRT, and push back to args |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 158 | for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) { |
| 159 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 160 | // Store the reference type arguments to SIRT |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 161 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 162 | llvm::Value* sirt_field_addr = irb_.CreateBitCast(irb_.CreateGEP(shadow_frame_, gep_index), |
| 163 | irb_.getJObjectTy()->getPointerTo()); |
TDYa127 | d955bec | 2012-05-11 10:54:02 -0700 | [diff] [blame] | 164 | irb_.CreateStore(arg_iter, sirt_field_addr, kTBAAShadowFrame); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 165 | // Note null is placed in the SIRT but the jobject passed to the native code must be null |
| 166 | // (not a pointer into the SIRT as with regular references). |
| 167 | llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull()); |
| 168 | llvm::Value* arg = |
| 169 | irb_.CreateSelect(equal_null, |
| 170 | irb_.getJNull(), |
| 171 | irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy())); |
| 172 | args.push_back(arg); |
| 173 | } else { |
| 174 | args.push_back(arg_iter); |
| 175 | } |
| 176 | } |
| 177 | |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 178 | llvm::Value* saved_local_ref_cookie; |
| 179 | { // JniMethodStart |
| 180 | RuntimeId func_id = is_synchronized ? JniMethodStartSynchronized |
| 181 | : JniMethodStart; |
| 182 | llvm::SmallVector<llvm::Value*, 2> args; |
| 183 | if (is_synchronized) { |
| 184 | args.push_back(this_object_or_class_object); |
| 185 | } |
| 186 | args.push_back(irb_.Runtime().EmitGetCurrentThread()); |
| 187 | saved_local_ref_cookie = |
| 188 | irb_.CreateCall(irb_.GetRuntime(func_id), args); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 189 | } |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 190 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 191 | // Call!!! |
| 192 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 193 | |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 194 | { // JniMethodEnd |
| 195 | bool is_return_ref = return_shorty == 'L'; |
| 196 | RuntimeId func_id = |
| 197 | is_return_ref ? (is_synchronized ? JniMethodEndWithReferenceSynchronized |
| 198 | : JniMethodEndWithReference) |
| 199 | : (is_synchronized ? JniMethodEndSynchronized |
| 200 | : JniMethodEnd); |
| 201 | llvm::SmallVector<llvm::Value*, 4> args; |
| 202 | if (is_return_ref) { |
| 203 | args.push_back(retval); |
| 204 | } |
| 205 | args.push_back(saved_local_ref_cookie); |
| 206 | if (is_synchronized) { |
| 207 | args.push_back(this_object_or_class_object); |
| 208 | } |
| 209 | args.push_back(irb_.Runtime().EmitGetCurrentThread()); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 210 | |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 211 | llvm::Value* decoded_jobject = |
| 212 | irb_.CreateCall(irb_.GetRuntime(func_id), args); |
| 213 | |
| 214 | // Return decoded jobject if return reference. |
| 215 | if (is_return_ref) { |
| 216 | retval = decoded_jobject; |
| 217 | } |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 218 | } |
| 219 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 220 | // Pop the shadow frame |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 221 | irb_.Runtime().EmitPopShadowFrame(old_shadow_frame); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 222 | |
| 223 | // Return! |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 224 | if (return_shorty != 'V') { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 225 | irb_.CreateRet(retval); |
| 226 | } else { |
| 227 | irb_.CreateRetVoid(); |
| 228 | } |
| 229 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 230 | // Verify the generated bitcode |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 231 | VERIFY_LLVM_FUNCTION(*func_); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 232 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 233 | cunit_->Materialize(); |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 234 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 235 | return new CompiledMethod(cunit_->GetInstructionSet(), |
| 236 | cunit_->GetCompiledCode()); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | void JniCompiler::CreateFunction() { |
| 241 | // LLVM function name |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 242 | std::string func_name(ElfFuncName(cunit_->GetIndex())); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 243 | |
Shih-wei Liao | cd05a62 | 2012-08-15 00:02:05 -0700 | [diff] [blame] | 244 | const bool is_static = (access_flags_ & kAccStatic) != 0; |
| 245 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 246 | // Get function type |
| 247 | llvm::FunctionType* func_type = |
Shih-wei Liao | cd05a62 | 2012-08-15 00:02:05 -0700 | [diff] [blame] | 248 | GetFunctionType(method_idx_, is_static, false); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 249 | |
| 250 | // Create function |
| 251 | func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, |
| 252 | func_name, module_); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 253 | |
| 254 | // Create basic block |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 255 | llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_); |
| 256 | |
| 257 | // Set insert point |
| 258 | irb_.SetInsertPoint(basic_block); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx, |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 263 | bool is_static, bool is_native_function) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 264 | // Get method signature |
| 265 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx); |
| 266 | |
| 267 | uint32_t shorty_size; |
Logan Chien | 1258417 | 2012-07-10 04:07:28 -0700 | [diff] [blame] | 268 | const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 269 | CHECK_GE(shorty_size, 1u); |
| 270 | |
| 271 | // Get return type |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 272 | llvm::Type* ret_type = irb_.getJType(shorty[0]); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 273 | |
| 274 | // Get argument type |
| 275 | std::vector<llvm::Type*> args_type; |
| 276 | |
| 277 | args_type.push_back(irb_.getJObjectTy()); // method object pointer |
| 278 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 279 | if (!is_static || is_native_function) { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 280 | // "this" object pointer for non-static |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 281 | // "class" object pointer for static naitve |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 282 | args_type.push_back(irb_.getJType('L')); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | for (uint32_t i = 1; i < shorty_size; ++i) { |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 286 | args_type.push_back(irb_.getJType(shorty[i])); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return llvm::FunctionType::get(ret_type, args_type, false); |
| 290 | } |
| 291 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 292 | } // namespace compiler_llvm |
| 293 | } // namespace art |