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" |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 30 | #include "shadow_frame.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 31 | #include "utils_llvm.h" |
| 32 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 33 | #include <llvm/Analysis/Verifier.h> |
| 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> |
| 37 | #include <llvm/Type.h> |
| 38 | |
| 39 | namespace art { |
| 40 | namespace compiler_llvm { |
| 41 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 42 | using namespace runtime_support; |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 43 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 44 | JniCompiler::JniCompiler(CompilationUnit* cunit, |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 45 | Compiler const& compiler, |
| 46 | OatCompilationUnit* oat_compilation_unit) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 47 | : cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()), |
| 48 | context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()), |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 49 | oat_compilation_unit_(oat_compilation_unit), |
| 50 | access_flags_(oat_compilation_unit->access_flags_), |
| 51 | method_idx_(oat_compilation_unit->method_idx_), |
| 52 | class_linker_(oat_compilation_unit->class_linker_), |
| 53 | class_loader_(oat_compilation_unit->class_loader_), |
| 54 | dex_cache_(oat_compilation_unit->dex_cache_), |
| 55 | dex_file_(oat_compilation_unit->dex_file_), |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 56 | method_(dex_cache_->GetResolvedMethod(method_idx_)), |
| 57 | elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 58 | |
| 59 | // Check: Ensure that the method is resolved |
| 60 | CHECK_NE(method_, static_cast<art::Method*>(NULL)); |
| 61 | |
| 62 | // Check: Ensure that JNI compiler will only get "native" method |
| 63 | CHECK((access_flags_ & kAccNative) != 0); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | CompiledMethod* JniCompiler::Compile() { |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 68 | const bool is_static = (access_flags_ & kAccStatic) != 0; |
| 69 | const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0; |
| 70 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_); |
| 71 | char const return_shorty = dex_file_->GetMethodShorty(method_id)[0]; |
| 72 | llvm::Value* this_object_or_class_object; |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 73 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 74 | CreateFunction(); |
| 75 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 76 | // Set argument name |
| 77 | llvm::Function::arg_iterator arg_begin(func_->arg_begin()); |
| 78 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 79 | llvm::Function::arg_iterator arg_iter(arg_begin); |
| 80 | |
| 81 | DCHECK_NE(arg_iter, arg_end); |
| 82 | arg_iter->setName("method"); |
| 83 | llvm::Value* method_object_addr = arg_iter++; |
| 84 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 85 | if (!is_static) { |
| 86 | // Non-static, the second argument is "this object" |
| 87 | this_object_or_class_object = arg_iter++; |
| 88 | } else { |
| 89 | // Load class object |
| 90 | this_object_or_class_object = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 91 | irb_.LoadFromObjectOffset(method_object_addr, |
| 92 | Method::DeclaringClassOffset().Int32Value(), |
| 93 | irb_.getJObjectTy()); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 94 | } |
| 95 | // Actual argument (ignore method and this object) |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 96 | arg_begin = arg_iter; |
| 97 | |
| 98 | // Count the number of Object* arguments |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 99 | uint32_t sirt_size = 1; |
| 100 | // "this" object pointer for non-static |
| 101 | // "class" object pointer for static |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 102 | for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) { |
| 103 | arg_iter->setName(StringPrintf("a%u", i)); |
| 104 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
| 105 | ++sirt_size; |
| 106 | } |
| 107 | } |
| 108 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 109 | // Get thread object |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 110 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 111 | |
| 112 | // Shadow stack |
| 113 | llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 114 | llvm::AllocaInst* shadow_frame_ = irb_.CreateAlloca(shadow_frame_type); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 115 | |
| 116 | // Zero-initialization of the shadow frame |
| 117 | llvm::ConstantAggregateZero* zero_initializer = |
| 118 | llvm::ConstantAggregateZero::get(shadow_frame_type); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 119 | irb_.CreateStore(zero_initializer, shadow_frame_); |
| 120 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 121 | // Store the method pointer |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 122 | llvm::Value* method_field_addr = |
| 123 | irb_.CreatePtrDisp(shadow_frame_, |
| 124 | irb_.getPtrEquivInt(ShadowFrame::MethodOffset()), |
| 125 | irb_.getJObjectTy()->getPointerTo()); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 126 | irb_.CreateStore(method_object_addr, method_field_addr); |
| 127 | |
TDYa127 | a0f746b | 2012-04-09 22:46:30 -0700 | [diff] [blame] | 128 | // Store the line number |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 129 | irb_.StoreToObjectOffset(shadow_frame_, |
| 130 | ShadowFrame::LineNumOffset(), |
| 131 | irb_.getInt32(dex_file_->GetLineNumFromPC(method_, 0))); |
TDYa127 | a0f746b | 2012-04-09 22:46:30 -0700 | [diff] [blame] | 132 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 133 | // Store the number of the pointer slots |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 134 | irb_.StoreToObjectOffset(shadow_frame_, |
| 135 | ShadowFrame::NumberOfReferencesOffset(), |
| 136 | irb_.getInt32(sirt_size)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 137 | |
| 138 | // Push the shadow frame |
| 139 | llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0); |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 140 | irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 141 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 142 | // Get JNIEnv |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 143 | llvm::Value* jni_env_object_addr = |
| 144 | irb_.LoadFromObjectOffset(thread_object_addr, |
| 145 | Thread::JniEnvOffset().Int32Value(), |
| 146 | irb_.getJObjectTy()); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 147 | |
| 148 | // Set thread state to kNative |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 149 | irb_.StoreToObjectOffset(thread_object_addr, |
| 150 | Thread::StateOffset().Int32Value(), |
| 151 | irb_.getInt32(kNative)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 152 | |
| 153 | // Get callee code_addr |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 154 | llvm::Value* code_addr = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 155 | irb_.LoadFromObjectOffset(method_object_addr, |
| 156 | Method::NativeMethodOffset().Int32Value(), |
| 157 | GetFunctionType(method_idx_, is_static, true)->getPointerTo()); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 158 | |
| 159 | // Load actual parameters |
| 160 | std::vector<llvm::Value*> args; |
| 161 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 162 | // The 1st parameter: JNIEnv* |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 163 | args.push_back(jni_env_object_addr); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 164 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 165 | // Variables for GetElementPtr |
| 166 | llvm::Value* gep_index[] = { |
| 167 | irb_.getInt32(0), // No displacement for shadow frame pointer |
| 168 | irb_.getInt32(1), // SIRT |
| 169 | NULL, |
| 170 | }; |
| 171 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 172 | size_t sirt_member_index = 0; |
| 173 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 174 | // Store the "this object or class object" to SIRT |
| 175 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
| 176 | llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index); |
| 177 | irb_.CreateStore(this_object_or_class_object, sirt_field_addr); |
| 178 | // Push the "this object or class object" to out args |
| 179 | args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy())); |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 180 | // Store arguments to SIRT, and push back to args |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 181 | for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) { |
| 182 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 183 | // Store the reference type arguments to SIRT |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 184 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
| 185 | llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index); |
| 186 | irb_.CreateStore(arg_iter, sirt_field_addr); |
| 187 | // Note null is placed in the SIRT but the jobject passed to the native code must be null |
| 188 | // (not a pointer into the SIRT as with regular references). |
| 189 | llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull()); |
| 190 | llvm::Value* arg = |
| 191 | irb_.CreateSelect(equal_null, |
| 192 | irb_.getJNull(), |
| 193 | irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy())); |
| 194 | args.push_back(arg); |
| 195 | } else { |
| 196 | args.push_back(arg_iter); |
| 197 | } |
| 198 | } |
| 199 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 200 | // Acquire lock for synchronized methods. |
| 201 | if (is_synchronized) { |
| 202 | // Acquire lock |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 203 | irb_.CreateCall(irb_.GetRuntime(LockObject), this_object_or_class_object); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 204 | |
| 205 | // Check exception pending |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 206 | llvm::Value* exception_pending = irb_.CreateCall(irb_.GetRuntime(IsExceptionPending)); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 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 |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 220 | irb_.CreateCall(irb_.GetRuntime(PopShadowFrame)); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 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 | } |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 231 | |
| 232 | // saved_local_ref_cookie = env->local_ref_cookie |
| 233 | llvm::Value* saved_local_ref_cookie = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 234 | irb_.LoadFromObjectOffset(jni_env_object_addr, |
| 235 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 236 | irb_.getInt32Ty()); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 237 | |
| 238 | // env->local_ref_cookie = env->locals.segment_state |
| 239 | llvm::Value* segment_state = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 240 | irb_.LoadFromObjectOffset(jni_env_object_addr, |
| 241 | JNIEnvExt::SegmentStateOffset().Int32Value(), |
| 242 | irb_.getInt32Ty()); |
| 243 | irb_.StoreToObjectOffset(jni_env_object_addr, |
| 244 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 245 | segment_state); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 246 | |
| 247 | |
| 248 | // Call!!! |
| 249 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 250 | |
| 251 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 252 | // Release lock for synchronized methods. |
| 253 | if (is_synchronized) { |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 254 | irb_.CreateCall(irb_.GetRuntime(UnlockObject), this_object_or_class_object); |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 255 | } |
| 256 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 257 | // Set thread state to kRunnable |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 258 | irb_.StoreToObjectOffset(thread_object_addr, |
| 259 | Thread::StateOffset().Int32Value(), |
| 260 | irb_.getInt32(kRunnable)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 261 | |
TDYa127 | 69eafaa | 2012-04-17 10:51:25 -0700 | [diff] [blame] | 262 | // Do a suspend check |
| 263 | irb_.CreateCall(irb_.GetRuntime(TestSuspend)); |
| 264 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 265 | if (return_shorty == 'L') { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 266 | // 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] | 267 | retval = irb_.CreateCall2(irb_.GetRuntime(DecodeJObjectInThread), |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 268 | thread_object_addr, |
| 269 | retval); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // env->locals.segment_state = env->local_ref_cookie |
| 273 | llvm::Value* local_ref_cookie = |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 274 | irb_.LoadFromObjectOffset(jni_env_object_addr, |
| 275 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 276 | irb_.getInt32Ty()); |
| 277 | irb_.StoreToObjectOffset(jni_env_object_addr, |
| 278 | JNIEnvExt::SegmentStateOffset().Int32Value(), |
| 279 | local_ref_cookie); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 280 | |
| 281 | // env->local_ref_cookie = saved_local_ref_cookie |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 282 | irb_.StoreToObjectOffset(jni_env_object_addr, |
| 283 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 284 | saved_local_ref_cookie); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 285 | |
| 286 | // Pop the shadow frame |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 287 | irb_.CreateCall(irb_.GetRuntime(PopShadowFrame)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 288 | |
| 289 | // Return! |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 290 | if (return_shorty != 'V') { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 291 | irb_.CreateRet(retval); |
| 292 | } else { |
| 293 | irb_.CreateRetVoid(); |
| 294 | } |
| 295 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 296 | // Verify the generated bitcode |
| 297 | llvm::verifyFunction(*func_, llvm::PrintMessageAction); |
| 298 | |
TDYa127 | 0200d07 | 2012-04-17 20:55:08 -0700 | [diff] [blame] | 299 | // Add the memory usage approximation of the compilation unit |
| 300 | cunit_->AddMemUsageApproximation((sirt_size * 4 + 50) * 50); |
| 301 | // NOTE: We will emit 4 LLVM instructions per object argument, |
| 302 | // And about 50 instructions for other operations. (Some runtime support will be inlined.) |
| 303 | // Beside, we guess that we have to use 50 bytes to represent one LLVM instruction. |
| 304 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 305 | CompiledMethod* compiled_method = |
| 306 | new CompiledMethod(cunit_->GetInstructionSet(), |
| 307 | cunit_->GetElfIndex(), |
| 308 | elf_func_idx_); |
| 309 | |
| 310 | cunit_->RegisterCompiledMethod(func_, compiled_method); |
| 311 | |
| 312 | return compiled_method; |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | void JniCompiler::CreateFunction() { |
| 317 | // LLVM function name |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 318 | std::string func_name(ElfFuncName(elf_func_idx_)); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 319 | |
| 320 | // Get function type |
| 321 | llvm::FunctionType* func_type = |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 322 | GetFunctionType(method_idx_, method_->IsStatic(), false); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 323 | |
| 324 | // Create function |
| 325 | func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, |
| 326 | func_name, module_); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 327 | |
| 328 | // Create basic block |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 329 | llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_); |
| 330 | |
| 331 | // Set insert point |
| 332 | irb_.SetInsertPoint(basic_block); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
| 336 | llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx, |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 337 | bool is_static, bool is_native_function) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 338 | // Get method signature |
| 339 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx); |
| 340 | |
| 341 | uint32_t shorty_size; |
| 342 | char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
| 343 | CHECK_GE(shorty_size, 1u); |
| 344 | |
| 345 | // Get return type |
| 346 | llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate); |
| 347 | |
| 348 | // Get argument type |
| 349 | std::vector<llvm::Type*> args_type; |
| 350 | |
| 351 | args_type.push_back(irb_.getJObjectTy()); // method object pointer |
| 352 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 353 | if (!is_static || is_native_function) { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 354 | // "this" object pointer for non-static |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 355 | // "class" object pointer for static naitve |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 356 | args_type.push_back(irb_.getJType('L', kAccurate)); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | for (uint32_t i = 1; i < shorty_size; ++i) { |
| 360 | args_type.push_back(irb_.getJType(shorty[i], kAccurate)); |
| 361 | } |
| 362 | |
| 363 | return llvm::FunctionType::get(ret_type, args_type, false); |
| 364 | } |
| 365 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 366 | } // namespace compiler_llvm |
| 367 | } // namespace art |