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 | |
| 19 | #include "class_linker.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 20 | #include "compilation_unit.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 21 | #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" |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 29 | #include "runtime_support_func.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 30 | #include "utils_llvm.h" |
| 31 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 32 | #include <llvm/BasicBlock.h> |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 33 | #include <llvm/DerivedTypes.h> |
| 34 | #include <llvm/Function.h> |
| 35 | #include <llvm/Type.h> |
| 36 | |
| 37 | namespace art { |
| 38 | namespace compiler_llvm { |
| 39 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 40 | using namespace runtime_support; |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 41 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 42 | JniCompiler::JniCompiler(CompilationUnit* cunit, |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 43 | Compiler const& compiler, |
| 44 | OatCompilationUnit* oat_compilation_unit) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 45 | : cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()), |
| 46 | context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()), |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 47 | oat_compilation_unit_(oat_compilation_unit), |
| 48 | access_flags_(oat_compilation_unit->access_flags_), |
| 49 | method_idx_(oat_compilation_unit->method_idx_), |
Shih-wei Liao | cd05a62 | 2012-08-15 00:02:05 -0700 | [diff] [blame^] | 50 | dex_file_(oat_compilation_unit->dex_file_) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 51 | |
| 52 | // Check: Ensure that JNI compiler will only get "native" method |
| 53 | CHECK((access_flags_ & kAccNative) != 0); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | CompiledMethod* JniCompiler::Compile() { |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 58 | const bool is_static = (access_flags_ & kAccStatic) != 0; |
| 59 | const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0; |
| 60 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_); |
| 61 | char const return_shorty = dex_file_->GetMethodShorty(method_id)[0]; |
| 62 | llvm::Value* this_object_or_class_object; |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 63 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 64 | CreateFunction(); |
| 65 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 66 | // Set argument name |
| 67 | llvm::Function::arg_iterator arg_begin(func_->arg_begin()); |
| 68 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 69 | llvm::Function::arg_iterator arg_iter(arg_begin); |
| 70 | |
| 71 | DCHECK_NE(arg_iter, arg_end); |
| 72 | arg_iter->setName("method"); |
| 73 | llvm::Value* method_object_addr = arg_iter++; |
| 74 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 75 | if (!is_static) { |
| 76 | // Non-static, the second argument is "this object" |
| 77 | this_object_or_class_object = arg_iter++; |
| 78 | } else { |
| 79 | // Load class object |
| 80 | this_object_or_class_object = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 81 | irb_.LoadFromObjectOffset(method_object_addr, |
| 82 | Method::DeclaringClassOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 83 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 84 | kTBAAConstJObject); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 85 | } |
| 86 | // Actual argument (ignore method and this object) |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 87 | arg_begin = arg_iter; |
| 88 | |
| 89 | // Count the number of Object* arguments |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 90 | uint32_t sirt_size = 1; |
| 91 | // "this" object pointer for non-static |
| 92 | // "class" object pointer for static |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 93 | for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) { |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 94 | #if !defined(NDEBUG) |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 95 | arg_iter->setName(StringPrintf("a%u", i)); |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 96 | #endif |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 97 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
| 98 | ++sirt_size; |
| 99 | } |
| 100 | } |
| 101 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 102 | // Shadow stack |
| 103 | llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 104 | llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 105 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 106 | // Store the dex pc |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 107 | irb_.StoreToObjectOffset(shadow_frame_, |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 108 | ShadowFrame::DexPCOffset(), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 109 | irb_.getInt32(DexFile::kDexNoIndex), |
TDYa127 | d955bec | 2012-05-11 10:54:02 -0700 | [diff] [blame] | 110 | kTBAAShadowFrame); |
TDYa127 | a0f746b | 2012-04-09 22:46:30 -0700 | [diff] [blame] | 111 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 112 | // Push the shadow frame |
| 113 | llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0); |
TDYa127 | 0de52be | 2012-05-27 20:49:31 -0700 | [diff] [blame] | 114 | llvm::Value* old_shadow_frame = |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 115 | irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr, sirt_size); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 116 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 117 | // Get JNIEnv |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 118 | llvm::Value* jni_env_object_addr = |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 119 | irb_.Runtime().EmitLoadFromThreadOffset(Thread::JniEnvOffset().Int32Value(), |
| 120 | irb_.getJObjectTy(), |
| 121 | kTBAAJRuntime); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 122 | |
| 123 | // Set thread state to kNative |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 124 | irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(), |
| 125 | irb_.getInt32(kNative), |
| 126 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 127 | |
| 128 | // Get callee code_addr |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 129 | llvm::Value* code_addr = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 130 | irb_.LoadFromObjectOffset(method_object_addr, |
| 131 | Method::NativeMethodOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 132 | GetFunctionType(method_idx_, is_static, true)->getPointerTo(), |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 133 | kTBAAJRuntime); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 134 | |
| 135 | // Load actual parameters |
| 136 | std::vector<llvm::Value*> args; |
| 137 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 138 | // The 1st parameter: JNIEnv* |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 139 | args.push_back(jni_env_object_addr); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 140 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 141 | // Variables for GetElementPtr |
| 142 | llvm::Value* gep_index[] = { |
| 143 | irb_.getInt32(0), // No displacement for shadow frame pointer |
| 144 | irb_.getInt32(1), // SIRT |
| 145 | NULL, |
| 146 | }; |
| 147 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 148 | size_t sirt_member_index = 0; |
| 149 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 150 | // Store the "this object or class object" to SIRT |
| 151 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
| 152 | llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index); |
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 |
| 155 | args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy())); |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 156 | // Store arguments to SIRT, and push back to args |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 157 | for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) { |
| 158 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 159 | // Store the reference type arguments to SIRT |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 160 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
| 161 | llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index); |
TDYa127 | d955bec | 2012-05-11 10:54:02 -0700 | [diff] [blame] | 162 | irb_.CreateStore(arg_iter, sirt_field_addr, kTBAAShadowFrame); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 163 | // 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 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 176 | // Acquire lock for synchronized methods. |
| 177 | if (is_synchronized) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 178 | irb_.Runtime().EmitLockObject(this_object_or_class_object); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 179 | } |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 180 | |
| 181 | // saved_local_ref_cookie = env->local_ref_cookie |
| 182 | llvm::Value* saved_local_ref_cookie = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 183 | irb_.LoadFromObjectOffset(jni_env_object_addr, |
| 184 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 185 | irb_.getInt32Ty(), |
| 186 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 187 | |
| 188 | // env->local_ref_cookie = env->locals.segment_state |
| 189 | llvm::Value* segment_state = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 190 | irb_.LoadFromObjectOffset(jni_env_object_addr, |
| 191 | JNIEnvExt::SegmentStateOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 192 | irb_.getInt32Ty(), |
| 193 | kTBAARuntimeInfo); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 194 | irb_.StoreToObjectOffset(jni_env_object_addr, |
| 195 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 196 | segment_state, |
| 197 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 198 | |
| 199 | |
| 200 | // Call!!! |
| 201 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 202 | |
| 203 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 204 | // Release lock for synchronized methods. |
| 205 | if (is_synchronized) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 206 | irb_.Runtime().EmitUnlockObject(this_object_or_class_object); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 207 | } |
| 208 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 209 | // Set thread state to kRunnable |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 210 | irb_.Runtime().EmitStoreToThreadOffset(Thread::StateOffset().Int32Value(), |
| 211 | irb_.getInt32(kRunnable), |
| 212 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 213 | |
TDYa127 | 69eafaa | 2012-04-17 10:51:25 -0700 | [diff] [blame] | 214 | // Do a suspend check |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 215 | irb_.Runtime().EmitTestSuspend(); |
TDYa127 | 69eafaa | 2012-04-17 10:51:25 -0700 | [diff] [blame] | 216 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 217 | if (return_shorty == 'L') { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 218 | // Get thread object |
| 219 | llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread(); |
| 220 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 221 | // If the return value is reference, it may point to SIRT, we should decode it. |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 222 | retval = irb_.CreateCall2(irb_.GetRuntime(DecodeJObjectInThread), |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 223 | thread_object_addr, |
| 224 | retval); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // env->locals.segment_state = env->local_ref_cookie |
| 228 | llvm::Value* local_ref_cookie = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 229 | irb_.LoadFromObjectOffset(jni_env_object_addr, |
| 230 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 231 | irb_.getInt32Ty(), |
| 232 | kTBAARuntimeInfo); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 233 | irb_.StoreToObjectOffset(jni_env_object_addr, |
| 234 | JNIEnvExt::SegmentStateOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 235 | local_ref_cookie, |
| 236 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 237 | |
| 238 | // env->local_ref_cookie = saved_local_ref_cookie |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 239 | irb_.StoreToObjectOffset(jni_env_object_addr, |
| 240 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 241 | saved_local_ref_cookie, |
| 242 | kTBAARuntimeInfo); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 243 | |
| 244 | // Pop the shadow frame |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 245 | irb_.Runtime().EmitPopShadowFrame(old_shadow_frame); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 246 | |
| 247 | // Return! |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 248 | if (return_shorty != 'V') { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 249 | irb_.CreateRet(retval); |
| 250 | } else { |
| 251 | irb_.CreateRetVoid(); |
| 252 | } |
| 253 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 254 | // Verify the generated bitcode |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 255 | VERIFY_LLVM_FUNCTION(*func_); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 256 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 257 | cunit_->Materialize(); |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 258 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 259 | return new CompiledMethod(cunit_->GetInstructionSet(), |
| 260 | cunit_->GetCompiledCode()); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | void JniCompiler::CreateFunction() { |
| 265 | // LLVM function name |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 266 | std::string func_name(ElfFuncName(cunit_->GetIndex())); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 267 | |
Shih-wei Liao | cd05a62 | 2012-08-15 00:02:05 -0700 | [diff] [blame^] | 268 | const bool is_static = (access_flags_ & kAccStatic) != 0; |
| 269 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 270 | // Get function type |
| 271 | llvm::FunctionType* func_type = |
Shih-wei Liao | cd05a62 | 2012-08-15 00:02:05 -0700 | [diff] [blame^] | 272 | GetFunctionType(method_idx_, is_static, false); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 273 | |
| 274 | // Create function |
| 275 | func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, |
| 276 | func_name, module_); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 277 | |
| 278 | // Create basic block |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 279 | llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_); |
| 280 | |
| 281 | // Set insert point |
| 282 | irb_.SetInsertPoint(basic_block); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx, |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 287 | bool is_static, bool is_native_function) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 288 | // Get method signature |
| 289 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx); |
| 290 | |
| 291 | uint32_t shorty_size; |
Logan Chien | 1258417 | 2012-07-10 04:07:28 -0700 | [diff] [blame] | 292 | const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 293 | CHECK_GE(shorty_size, 1u); |
| 294 | |
| 295 | // Get return type |
| 296 | llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate); |
| 297 | |
| 298 | // Get argument type |
| 299 | std::vector<llvm::Type*> args_type; |
| 300 | |
| 301 | args_type.push_back(irb_.getJObjectTy()); // method object pointer |
| 302 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 303 | if (!is_static || is_native_function) { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 304 | // "this" object pointer for non-static |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 305 | // "class" object pointer for static naitve |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 306 | args_type.push_back(irb_.getJType('L', kAccurate)); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | for (uint32_t i = 1; i < shorty_size; ++i) { |
| 310 | args_type.push_back(irb_.getJType(shorty[i], kAccurate)); |
| 311 | } |
| 312 | |
| 313 | return llvm::FunctionType::get(ret_type, args_type, false); |
| 314 | } |
| 315 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 316 | } // namespace compiler_llvm |
| 317 | } // namespace art |