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 | |
| 42 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 43 | JniCompiler::JniCompiler(CompilationUnit* cunit, |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 44 | Compiler const& compiler, |
| 45 | OatCompilationUnit* oat_compilation_unit) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 46 | : cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()), |
| 47 | context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()), |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 48 | 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_), |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 55 | method_(dex_cache_->GetResolvedMethod(method_idx_)), |
| 56 | elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 57 | |
| 58 | // Check: Ensure that the method is resolved |
| 59 | CHECK_NE(method_, static_cast<art::Method*>(NULL)); |
| 60 | |
| 61 | // Check: Ensure that JNI compiler will only get "native" method |
| 62 | CHECK((access_flags_ & kAccNative) != 0); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | CompiledMethod* JniCompiler::Compile() { |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 67 | const bool is_static = (access_flags_ & kAccStatic) != 0; |
| 68 | const bool is_synchronized = (access_flags_ & kAccSynchronized) != 0; |
| 69 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_); |
| 70 | char const return_shorty = dex_file_->GetMethodShorty(method_id)[0]; |
| 71 | llvm::Value* this_object_or_class_object; |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 72 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 73 | CreateFunction(); |
| 74 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 75 | // Set argument name |
| 76 | llvm::Function::arg_iterator arg_begin(func_->arg_begin()); |
| 77 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 78 | llvm::Function::arg_iterator arg_iter(arg_begin); |
| 79 | |
| 80 | DCHECK_NE(arg_iter, arg_end); |
| 81 | arg_iter->setName("method"); |
| 82 | llvm::Value* method_object_addr = arg_iter++; |
| 83 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 84 | if (!is_static) { |
| 85 | // Non-static, the second argument is "this object" |
| 86 | this_object_or_class_object = arg_iter++; |
| 87 | } else { |
| 88 | // Load class object |
| 89 | this_object_or_class_object = |
| 90 | LoadFromObjectOffset(method_object_addr, |
| 91 | Method::DeclaringClassOffset().Int32Value(), |
| 92 | irb_.getJObjectTy()); |
| 93 | } |
| 94 | // Actual argument (ignore method and this object) |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 95 | arg_begin = arg_iter; |
| 96 | |
| 97 | // Count the number of Object* arguments |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 98 | uint32_t sirt_size = 1; |
| 99 | // "this" object pointer for non-static |
| 100 | // "class" object pointer for static |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 101 | for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) { |
| 102 | arg_iter->setName(StringPrintf("a%u", i)); |
| 103 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
| 104 | ++sirt_size; |
| 105 | } |
| 106 | } |
| 107 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 108 | // Get thread object |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 109 | llvm::Value* thread_object_addr = |
| 110 | irb_.CreateCall(irb_.GetRuntime(runtime_support::GetCurrentThread)); |
| 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 | |
| 128 | // Store the number of the pointer slots |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 129 | StoreToObjectOffset(shadow_frame_, |
| 130 | ShadowFrame::NumberOfReferencesOffset(), |
| 131 | irb_.getInt32(sirt_size)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 132 | |
| 133 | // Push the shadow frame |
| 134 | llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0); |
| 135 | irb_.CreateCall(irb_.GetRuntime(runtime_support::PushShadowFrame), shadow_frame_upcast); |
| 136 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 137 | // Get JNIEnv |
| 138 | llvm::Value* jni_env_object_addr = LoadFromObjectOffset(thread_object_addr, |
| 139 | Thread::JniEnvOffset().Int32Value(), |
| 140 | irb_.getJObjectTy()); |
| 141 | |
| 142 | // Set thread state to kNative |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 143 | StoreToObjectOffset(thread_object_addr, |
| 144 | Thread::StateOffset().Int32Value(), |
| 145 | irb_.getInt32(Thread::kNative)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 146 | |
| 147 | // Get callee code_addr |
| 148 | llvm::Value* code_addr = |
| 149 | LoadFromObjectOffset(method_object_addr, |
| 150 | Method::NativeMethodOffset().Int32Value(), |
| 151 | GetFunctionType(method_idx_, is_static, true)->getPointerTo()); |
| 152 | |
| 153 | |
| 154 | // Load actual parameters |
| 155 | std::vector<llvm::Value*> args; |
| 156 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 157 | // The 1st parameter: JNIEnv* |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 158 | args.push_back(jni_env_object_addr); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 159 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 160 | // Variables for GetElementPtr |
| 161 | llvm::Value* gep_index[] = { |
| 162 | irb_.getInt32(0), // No displacement for shadow frame pointer |
| 163 | irb_.getInt32(1), // SIRT |
| 164 | NULL, |
| 165 | }; |
| 166 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 167 | size_t sirt_member_index = 0; |
| 168 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 169 | // Store the "this object or class object" to SIRT |
| 170 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
| 171 | llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index); |
| 172 | irb_.CreateStore(this_object_or_class_object, sirt_field_addr); |
| 173 | // Push the "this object or class object" to out args |
| 174 | args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy())); |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 175 | // Store arguments to SIRT, and push back to args |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 176 | for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) { |
| 177 | if (arg_iter->getType() == irb_.getJObjectTy()) { |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 178 | // Store the reference type arguments to SIRT |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 179 | gep_index[2] = irb_.getInt32(sirt_member_index++); |
| 180 | llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index); |
| 181 | irb_.CreateStore(arg_iter, sirt_field_addr); |
| 182 | // Note null is placed in the SIRT but the jobject passed to the native code must be null |
| 183 | // (not a pointer into the SIRT as with regular references). |
| 184 | llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull()); |
| 185 | llvm::Value* arg = |
| 186 | irb_.CreateSelect(equal_null, |
| 187 | irb_.getJNull(), |
| 188 | irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy())); |
| 189 | args.push_back(arg); |
| 190 | } else { |
| 191 | args.push_back(arg_iter); |
| 192 | } |
| 193 | } |
| 194 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 195 | // Acquire lock for synchronized methods. |
| 196 | if (is_synchronized) { |
| 197 | // Acquire lock |
| 198 | irb_.CreateCall(irb_.GetRuntime(runtime_support::LockObject), this_object_or_class_object); |
| 199 | |
| 200 | // Check exception pending |
| 201 | llvm::Value* exception_pending = |
| 202 | irb_.CreateCall(irb_.GetRuntime(runtime_support::IsExceptionPending)); |
| 203 | |
| 204 | // Create two basic block for branch |
| 205 | llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "B.cont", func_); |
| 206 | llvm::BasicBlock* block_exception_ = llvm::BasicBlock::Create(*context_, "B.exception", func_); |
| 207 | |
| 208 | // Branch by exception_pending |
| 209 | irb_.CreateCondBr(exception_pending, block_exception_, block_cont); |
| 210 | |
| 211 | |
| 212 | // If exception pending |
| 213 | irb_.SetInsertPoint(block_exception_); |
| 214 | // TODO: Set thread state? |
| 215 | // Pop the shadow frame |
| 216 | irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame)); |
| 217 | // Unwind |
| 218 | if (return_shorty != 'V') { |
| 219 | irb_.CreateRet(irb_.getJZero(return_shorty)); |
| 220 | } else { |
| 221 | irb_.CreateRetVoid(); |
| 222 | } |
| 223 | |
| 224 | // If no exception pending |
| 225 | irb_.SetInsertPoint(block_cont); |
| 226 | } |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 227 | |
| 228 | // saved_local_ref_cookie = env->local_ref_cookie |
| 229 | llvm::Value* saved_local_ref_cookie = |
| 230 | LoadFromObjectOffset(jni_env_object_addr, |
| 231 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 232 | irb_.getInt32Ty()); |
| 233 | |
| 234 | // env->local_ref_cookie = env->locals.segment_state |
| 235 | llvm::Value* segment_state = |
| 236 | LoadFromObjectOffset(jni_env_object_addr, |
| 237 | JNIEnvExt::SegmentStateOffset().Int32Value(), |
| 238 | irb_.getInt32Ty()); |
| 239 | StoreToObjectOffset(jni_env_object_addr, |
| 240 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 241 | segment_state); |
| 242 | |
| 243 | |
| 244 | // Call!!! |
| 245 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 246 | |
| 247 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 248 | // Release lock for synchronized methods. |
| 249 | if (is_synchronized) { |
| 250 | irb_.CreateCall(irb_.GetRuntime(runtime_support::UnlockObject), this_object_or_class_object); |
| 251 | } |
| 252 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 253 | // Set thread state to kRunnable |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 254 | StoreToObjectOffset(thread_object_addr, |
| 255 | Thread::StateOffset().Int32Value(), |
| 256 | irb_.getInt32(Thread::kRunnable)); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 257 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 258 | if (return_shorty == 'L') { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 259 | // If the return value is reference, it may point to SIRT, we should decode it. |
| 260 | retval = irb_.CreateCall2(irb_.GetRuntime(runtime_support::DecodeJObjectInThread), |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 261 | thread_object_addr, |
| 262 | retval); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // env->locals.segment_state = env->local_ref_cookie |
| 266 | llvm::Value* local_ref_cookie = |
| 267 | LoadFromObjectOffset(jni_env_object_addr, |
| 268 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 269 | irb_.getInt32Ty()); |
| 270 | StoreToObjectOffset(jni_env_object_addr, |
| 271 | JNIEnvExt::SegmentStateOffset().Int32Value(), |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 272 | local_ref_cookie); |
| 273 | |
| 274 | // env->local_ref_cookie = saved_local_ref_cookie |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 275 | StoreToObjectOffset(jni_env_object_addr, |
| 276 | JNIEnvExt::LocalRefCookieOffset().Int32Value(), |
| 277 | saved_local_ref_cookie); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 278 | |
| 279 | // Pop the shadow frame |
| 280 | irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame)); |
| 281 | |
| 282 | // Return! |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 283 | if (return_shorty != 'V') { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 284 | irb_.CreateRet(retval); |
| 285 | } else { |
| 286 | irb_.CreateRetVoid(); |
| 287 | } |
| 288 | |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 289 | // Verify the generated bitcode |
| 290 | llvm::verifyFunction(*func_, llvm::PrintMessageAction); |
| 291 | |
Logan Chien | 6920bce | 2012-03-17 21:44:01 +0800 | [diff] [blame] | 292 | return new CompiledMethod(cunit_->GetInstructionSet(), |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 293 | cunit_->GetElfIndex(), |
| 294 | elf_func_idx_); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
| 298 | void JniCompiler::CreateFunction() { |
| 299 | // LLVM function name |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 300 | std::string func_name(ElfFuncName(elf_func_idx_)); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 301 | |
| 302 | // Get function type |
| 303 | llvm::FunctionType* func_type = |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 304 | GetFunctionType(method_idx_, method_->IsStatic(), false); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 305 | |
| 306 | // Create function |
| 307 | func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, |
| 308 | func_name, module_); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 309 | |
| 310 | // Create basic block |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 311 | llvm::BasicBlock* basic_block = llvm::BasicBlock::Create(*context_, "B0", func_); |
| 312 | |
| 313 | // Set insert point |
| 314 | irb_.SetInsertPoint(basic_block); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
| 318 | llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx, |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 319 | bool is_static, bool is_native_function) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 320 | // Get method signature |
| 321 | DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx); |
| 322 | |
| 323 | uint32_t shorty_size; |
| 324 | char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
| 325 | CHECK_GE(shorty_size, 1u); |
| 326 | |
| 327 | // Get return type |
| 328 | llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate); |
| 329 | |
| 330 | // Get argument type |
| 331 | std::vector<llvm::Type*> args_type; |
| 332 | |
| 333 | args_type.push_back(irb_.getJObjectTy()); // method object pointer |
| 334 | |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 335 | if (!is_static || is_native_function) { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 336 | // "this" object pointer for non-static |
TDYa127 | 9000a84 | 2012-03-23 17:43:08 -0700 | [diff] [blame] | 337 | // "class" object pointer for static naitve |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 338 | args_type.push_back(irb_.getJType('L', kAccurate)); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | for (uint32_t i = 1; i < shorty_size; ++i) { |
| 342 | args_type.push_back(irb_.getJType(shorty[i], kAccurate)); |
| 343 | } |
| 344 | |
| 345 | return llvm::FunctionType::get(ret_type, args_type, false); |
| 346 | } |
| 347 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 348 | llvm::Value* JniCompiler::LoadFromObjectOffset(llvm::Value* object_addr, |
| 349 | int32_t offset, |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 350 | llvm::Type* type) { |
| 351 | // Convert offset to llvm::value |
| 352 | llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset); |
| 353 | // Calculate the value's address |
| 354 | llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo()); |
| 355 | // Load |
| 356 | return irb_.CreateLoad(value_addr); |
| 357 | } |
| 358 | |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 359 | void JniCompiler::StoreToObjectOffset(llvm::Value* object_addr, |
| 360 | int32_t offset, |
| 361 | llvm::Value* new_value) { |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 362 | // Convert offset to llvm::value |
| 363 | llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset); |
| 364 | // Calculate the value's address |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 365 | llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr, |
| 366 | llvm_offset, |
| 367 | new_value->getType()->getPointerTo()); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 368 | // Store |
TDYa127 | 31a9933 | 2012-03-19 02:58:02 -0700 | [diff] [blame] | 369 | irb_.CreateStore(new_value, value_addr); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 370 | } |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 371 | |
| 372 | } // namespace compiler_llvm |
| 373 | } // namespace art |